(FrameCallback) Ensure the callback is removed if the implementation is deleted
[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 )
66 : mFrameCallbacks(),
67   mTransformManager( transformManager ),
68   mNodeHierarchyChanged( true )
69 {
70 }
71
72 FrameCallbackProcessor::~FrameCallbackProcessor()
73 {
74 }
75
76 void FrameCallbackProcessor::AddFrameCallback( FrameCallbackInterface* frameCallback, const Node* rootNode )
77 {
78   Node& node = const_cast< Node& >( *rootNode ); // Was sent as const from event thread, we need to be able to use non-const version here.
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.emplace_back( FrameCallbackInfo( frameCallback , new UpdateProxy( mTransformManager, node ) ) );
88 }
89
90 void FrameCallbackProcessor::RemoveFrameCallback( FrameCallbackInterface* frameCallback )
91 {
92   std::vector< SceneGraph::Node* > nodesToStopObserving;
93
94   // Find and remove all matching frame-callbacks
95   auto iter =
96     std::remove_if( mFrameCallbacks.begin(), mFrameCallbacks.end(),
97                     [ frameCallback, &nodesToStopObserving ] ( FrameCallbackInfo& info )
98                     {
99                       bool match = false;
100                       if( info.frameCallback == frameCallback )
101                       {
102                         nodesToStopObserving.push_back( &info.updateProxyImpl->GetRootNode() );
103                         match = true;
104                       }
105                       return match;
106                     } );
107   mFrameCallbacks.erase( iter, mFrameCallbacks.end() );
108
109   // Only stop observing the removed frame-callback nodes if none of the other frame-callbacks use them as root nodes
110   for( auto&& node : nodesToStopObserving )
111   {
112     auto nodeMatchingIter = std::find_if( mFrameCallbacks.begin(), mFrameCallbacks.end(), MatchRootNode< FrameCallbackInfo >( *node ) );
113     if( nodeMatchingIter == mFrameCallbacks.end() )
114     {
115       node->RemoveObserver( *this );
116     }
117   }
118 }
119
120 void FrameCallbackProcessor::Update( BufferIndex bufferIndex, float elapsedSeconds )
121 {
122   for( auto&& iter : mFrameCallbacks )
123   {
124     UpdateProxy& updateProxyImpl = *iter.updateProxyImpl;
125     updateProxyImpl.SetCurrentBufferIndex( bufferIndex );
126
127     if( mNodeHierarchyChanged )
128     {
129       updateProxyImpl.NodeHierarchyChanged();
130     }
131
132     Dali::UpdateProxy updateProxy( updateProxyImpl );
133     iter.frameCallback->Update( updateProxy, elapsedSeconds );
134   }
135   mNodeHierarchyChanged = false;
136 }
137
138 void FrameCallbackProcessor::PropertyOwnerDestroyed( PropertyOwner& owner )
139 {
140   auto iter = std::remove_if( mFrameCallbacks.begin(), mFrameCallbacks.end(), MatchRootNode< FrameCallbackInfo >( owner )  );
141   mFrameCallbacks.erase( iter, mFrameCallbacks.end() );
142 }
143
144 } // namespace SceneGraph
145
146 } // namespace Internal
147
148 } // namespace Dali