Merge "Updated patch-coverage script to generate correct HTML" into devel/master
[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 // EXTERNAL INCLUDES
22 #include <algorithm>
23
24 // INTERNAL INCLUDES
25 #include <dali/devel-api/update/frame-callback-interface.h>
26 #include <dali/devel-api/update/update-proxy.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace SceneGraph
35 {
36
37 namespace
38 {
39
40 /**
41  * Given a node, it matches all update-proxies using that node as their root-node.
42  */
43 template< typename FrameCallbackInfoT >
44 class MatchRootNode
45 {
46 public:
47
48   MatchRootNode( PropertyOwner& rootNode )
49   : mRootNode( rootNode )
50   {
51   }
52
53   bool operator() ( const FrameCallbackInfoT& info )
54   {
55     return &info.updateProxyImpl->GetRootNode() == &mRootNode;
56   }
57
58 private:
59
60   const PropertyOwner& mRootNode;
61 };
62
63 } // unnamed namespace
64
65 FrameCallbackProcessor::FrameCallbackProcessor( TransformManager& transformManager, Node& rootNode )
66 : mFrameCallbacks(),
67   mTransformManager( transformManager ),
68   mRootNode( rootNode ),
69   mNodeHierarchyChanged( true )
70 {
71 }
72
73 FrameCallbackProcessor::~FrameCallbackProcessor()
74 {
75 }
76
77 void FrameCallbackProcessor::AddFrameCallback( FrameCallbackInterface* frameCallback, const Node* rootNode )
78 {
79   Node& node = const_cast< Node& >( *rootNode ); // Was sent as const from event thread, we need to be able to use non-const version here.
80
81   // We want to be notified when the node is destroyed (if we're not observing it already)
82   auto iter = std::find_if( mFrameCallbacks.begin(), mFrameCallbacks.end(), MatchRootNode< FrameCallbackInfo >( node ) );
83   if( iter == mFrameCallbacks.end() )
84   {
85     node.AddObserver( *this );
86   }
87
88   mFrameCallbacks.emplace_back( FrameCallbackInfo( frameCallback , new UpdateProxy( mTransformManager, node ) ) );
89 }
90
91 void FrameCallbackProcessor::RemoveFrameCallback( FrameCallbackInterface* frameCallback )
92 {
93   std::vector< SceneGraph::Node* > nodesToStopObserving;
94
95   // Find and remove all matching frame-callbacks
96   auto iter =
97     std::remove_if( mFrameCallbacks.begin(), mFrameCallbacks.end(),
98                     [ frameCallback, &nodesToStopObserving ] ( FrameCallbackInfo& info )
99                     {
100                       bool match = false;
101                       if( info.frameCallback == frameCallback )
102                       {
103                         nodesToStopObserving.push_back( &info.updateProxyImpl->GetRootNode() );
104                         match = true;
105                       }
106                       return match;
107                     } );
108   mFrameCallbacks.erase( iter, mFrameCallbacks.end() );
109
110   // Only stop observing the removed frame-callback nodes if none of the other frame-callbacks use them as root nodes
111   for( auto&& node : nodesToStopObserving )
112   {
113     auto nodeMatchingIter = std::find_if( mFrameCallbacks.begin(), mFrameCallbacks.end(), MatchRootNode< FrameCallbackInfo >( *node ) );
114     if( nodeMatchingIter == mFrameCallbacks.end() )
115     {
116       node->RemoveObserver( *this );
117     }
118   }
119 }
120
121 void FrameCallbackProcessor::Update( BufferIndex bufferIndex, float elapsedSeconds )
122 {
123   for( auto&& iter : mFrameCallbacks )
124   {
125     UpdateProxy& updateProxyImpl = *iter.updateProxyImpl;
126     updateProxyImpl.SetCurrentBufferIndex( bufferIndex );
127
128     if( mNodeHierarchyChanged )
129     {
130       updateProxyImpl.NodeHierarchyChanged();
131     }
132
133     Dali::UpdateProxy updateProxy( updateProxyImpl );
134     iter.frameCallback->Update( updateProxy, elapsedSeconds );
135   }
136   mNodeHierarchyChanged = false;
137 }
138
139 void FrameCallbackProcessor::PropertyOwnerDestroyed( PropertyOwner& owner )
140 {
141   auto iter = std::remove_if( mFrameCallbacks.begin(), mFrameCallbacks.end(), MatchRootNode< FrameCallbackInfo >( owner )  );
142   mFrameCallbacks.erase( iter, mFrameCallbacks.end() );
143 }
144
145 } // namespace SceneGraph
146
147 } // namespace Internal
148
149 } // namespace Dali