87f3b89b13110ba5a63116366c8167f8edee0d15
[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 {
65 }
66
67 FrameCallbackProcessor::~FrameCallbackProcessor()
68 {
69 }
70
71 void FrameCallbackProcessor::AddFrameCallback( FrameCallbackInterface* frameCallback, const Node* rootNode )
72 {
73   Node& node = const_cast< Node& >( *rootNode ); // Was sent as const from event thread, we need to be able to use non-const version here.
74
75   FrameCallbackInfo info;
76   info.frameCallback = frameCallback;
77   info.updateProxyImpl = new UpdateProxy( mTransformManager, node );
78
79   // We want to be notified when the node is destroyed (if we're not observing it already)
80   auto iter = std::find_if( mFrameCallbacks.begin(), mFrameCallbacks.end(), MatchRootNode< FrameCallbackInfo >( node ) );
81   if( iter == mFrameCallbacks.end() )
82   {
83     node.AddObserver( *this );
84   }
85
86   mFrameCallbacks.push_back( info );
87 }
88
89 void FrameCallbackProcessor::RemoveFrameCallback( FrameCallbackInterface* frameCallback )
90 {
91   auto iter = std::remove_if( mFrameCallbacks.begin(), mFrameCallbacks.end(),
92                               [ this, frameCallback ]
93                                 ( const FrameCallbackInfo& info )
94                                 {
95                                   info.updateProxyImpl->GetRootNode().RemoveObserver( *this );
96                                   delete info.updateProxyImpl;
97                                   return info.frameCallback == frameCallback;
98                                 } );
99   mFrameCallbacks.erase( iter, mFrameCallbacks.end() );
100 }
101
102 void FrameCallbackProcessor::Update( BufferIndex bufferIndex, float elapsedSeconds )
103 {
104   for( auto&& iter : mFrameCallbacks )
105   {
106     UpdateProxy& updateProxyImpl = *iter.updateProxyImpl;
107     updateProxyImpl.SetCurrentBufferIndex( bufferIndex );
108     Dali::UpdateProxy updateProxy( updateProxyImpl );
109     iter.frameCallback->Update( updateProxy, elapsedSeconds );
110   }
111 }
112
113 void FrameCallbackProcessor::PropertyOwnerDestroyed( PropertyOwner& owner )
114 {
115   auto iter = std::remove_if( mFrameCallbacks.begin(), mFrameCallbacks.end(), MatchRootNode< FrameCallbackInfo >( owner )  );
116   mFrameCallbacks.erase( iter, mFrameCallbacks.end() );
117 }
118
119 } // namespace SceneGraph
120
121 } // namespace Internal
122
123 } // namespace Dali