17423d1351041f2e52457c345c2f14805a216091
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / frame-callback-processor.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/update/manager/frame-callback-processor.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/devel-api/update/frame-callback-interface.h>
23 #include <dali/devel-api/update/update-proxy.h>
24 #include <dali/internal/update/manager/update-proxy-impl.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 namespace SceneGraph
33 {
34
35 namespace
36 {
37
38 template< typename FrameCallbackInfoT >
39 class MatchRootNode
40 {
41 public:
42
43   MatchRootNode( PropertyOwner& rootNode )
44   : mRootNode( rootNode )
45   {
46   }
47
48   bool operator() ( const FrameCallbackInfoT& info )
49   {
50     return &info.updateProxyImpl->GetRootNode() == &mRootNode;
51   }
52
53 private:
54
55   PropertyOwner& mRootNode;
56 };
57
58 } // unnamed namespace
59
60 FrameCallbackProcessor::FrameCallbackProcessor( TransformManager& transformManager, Node& rootNode )
61 : mFrameCallbacks(),
62   mTransformManager( transformManager ),
63   mRootNode( rootNode ),
64   mNodeHierarchyChanged( true )
65 {
66 }
67
68 FrameCallbackProcessor::~FrameCallbackProcessor()
69 {
70 }
71
72 void FrameCallbackProcessor::AddFrameCallback( FrameCallbackInterface* frameCallback, const Node* rootNode )
73 {
74   Node& node = const_cast< Node& >( *rootNode ); // Was sent as const from event thread, we need to be able to use non-const version here.
75
76   FrameCallbackInfo info;
77   info.frameCallback = frameCallback;
78   info.updateProxyImpl = new UpdateProxy( mTransformManager, node );
79
80   // We want to be notified when the node is destroyed (if we're not observing it already)
81   auto iter = std::find_if( mFrameCallbacks.begin(), mFrameCallbacks.end(), MatchRootNode< FrameCallbackInfo >( node ) );
82   if( iter == mFrameCallbacks.end() )
83   {
84     node.AddObserver( *this );
85   }
86
87   mFrameCallbacks.push_back( info );
88 }
89
90 void FrameCallbackProcessor::RemoveFrameCallback( FrameCallbackInterface* frameCallback )
91 {
92   auto iter = std::remove_if( mFrameCallbacks.begin(), mFrameCallbacks.end(),
93                               [ this, frameCallback ]
94                                 ( const FrameCallbackInfo& info )
95                                 {
96                                   info.updateProxyImpl->GetRootNode().RemoveObserver( *this );
97                                   delete info.updateProxyImpl;
98                                   return info.frameCallback == frameCallback;
99                                 } );
100   mFrameCallbacks.erase( iter, mFrameCallbacks.end() );
101 }
102
103 void FrameCallbackProcessor::Update( BufferIndex bufferIndex, float elapsedSeconds )
104 {
105   for( auto&& iter : mFrameCallbacks )
106   {
107     UpdateProxy& updateProxyImpl = *iter.updateProxyImpl;
108     updateProxyImpl.SetCurrentBufferIndex( bufferIndex );
109
110     if( mNodeHierarchyChanged )
111     {
112       updateProxyImpl.NodeHierarchyChanged();
113     }
114
115     Dali::UpdateProxy updateProxy( updateProxyImpl );
116     iter.frameCallback->Update( updateProxy, elapsedSeconds );
117   }
118   mNodeHierarchyChanged = false;
119 }
120
121 void FrameCallbackProcessor::PropertyOwnerDestroyed( PropertyOwner& owner )
122 {
123   auto iter = std::remove_if( mFrameCallbacks.begin(), mFrameCallbacks.end(), MatchRootNode< FrameCallbackInfo >( owner )  );
124   mFrameCallbacks.erase( iter, mFrameCallbacks.end() );
125 }
126
127 } // namespace SceneGraph
128
129 } // namespace Internal
130
131 } // namespace Dali