2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/update/manager/frame-callback-processor.h>
25 #include <dali/devel-api/update/frame-callback-interface.h>
26 #include <dali/devel-api/update/update-proxy.h>
41 * Given a node, it matches all update-proxies using that node as their root-node.
43 template< typename FrameCallbackInfoT >
48 MatchRootNode( PropertyOwner& rootNode )
49 : mRootNode( rootNode )
53 bool operator() ( const FrameCallbackInfoT& info )
55 return &info.updateProxyImpl->GetRootNode() == &mRootNode;
60 const PropertyOwner& mRootNode;
63 } // unnamed namespace
65 FrameCallbackProcessor::FrameCallbackProcessor( TransformManager& transformManager )
67 mTransformManager( transformManager ),
68 mNodeHierarchyChanged( true )
72 FrameCallbackProcessor::~FrameCallbackProcessor()
76 void FrameCallbackProcessor::AddFrameCallback( FrameCallbackInterface* frameCallback, const Node* rootNode )
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.
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() )
84 node.AddObserver( *this );
87 mFrameCallbacks.emplace_back( FrameCallbackInfo( frameCallback , new UpdateProxy( mTransformManager, node ) ) );
90 void FrameCallbackProcessor::RemoveFrameCallback( FrameCallbackInterface* frameCallback )
92 std::vector< SceneGraph::Node* > nodesToStopObserving;
94 // Find and remove all matching frame-callbacks
96 std::remove_if( mFrameCallbacks.begin(), mFrameCallbacks.end(),
97 [ frameCallback, &nodesToStopObserving ] ( FrameCallbackInfo& info )
100 if( info.frameCallback == frameCallback )
102 nodesToStopObserving.push_back( &info.updateProxyImpl->GetRootNode() );
107 mFrameCallbacks.erase( iter, mFrameCallbacks.end() );
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 )
112 auto nodeMatchingIter = std::find_if( mFrameCallbacks.begin(), mFrameCallbacks.end(), MatchRootNode< FrameCallbackInfo >( *node ) );
113 if( nodeMatchingIter == mFrameCallbacks.end() )
115 node->RemoveObserver( *this );
120 void FrameCallbackProcessor::Update( BufferIndex bufferIndex, float elapsedSeconds )
122 for( auto&& iter : mFrameCallbacks )
124 UpdateProxy& updateProxyImpl = *iter.updateProxyImpl;
125 updateProxyImpl.SetCurrentBufferIndex( bufferIndex );
127 if( mNodeHierarchyChanged )
129 updateProxyImpl.NodeHierarchyChanged();
132 Dali::UpdateProxy updateProxy( updateProxyImpl );
133 iter.frameCallback->Update( updateProxy, elapsedSeconds );
135 mNodeHierarchyChanged = false;
138 void FrameCallbackProcessor::PropertyOwnerDestroyed( PropertyOwner& owner )
140 auto iter = std::remove_if( mFrameCallbacks.begin(), mFrameCallbacks.end(), MatchRootNode< FrameCallbackInfo >( owner ) );
141 mFrameCallbacks.erase( iter, mFrameCallbacks.end() );
144 } // namespace SceneGraph
146 } // namespace Internal