(FrameCallback) Ensure the callback is removed if the implementation is deleted
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / frame-callback-processor.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_FRAME_CALLBACK_PROCESSOR_H
2 #define DALI_INTERNAL_SCENE_GRAPH_FRAME_CALLBACK_PROCESSOR_H
3
4 /*
5  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <memory>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/vector-wrapper.h>
26 #include <dali/internal/common/buffer-index.h>
27 #include <dali/internal/update/common/property-owner.h>
28 #include <dali/internal/update/manager/update-proxy-impl.h>
29
30 namespace Dali
31 {
32
33 class FrameCallbackInterface;
34
35 namespace Internal
36 {
37
38 namespace SceneGraph
39 {
40
41 class Node;
42 class TransformManager;
43
44 /**
45  * This class processes all the registered frame-callbacks.
46  */
47 class FrameCallbackProcessor : public PropertyOwner::Observer
48 {
49 public:
50
51   /**
52    * Construct a new FrameCallbackProcessor.
53    */
54   FrameCallbackProcessor( TransformManager& transformManager );
55
56   /**
57    * Non-virtual Destructor.
58    */
59   ~FrameCallbackProcessor();
60
61   // Movable but not copyable
62
63   FrameCallbackProcessor( const FrameCallbackProcessor& )            = delete;  ///< Deleted copy constructor.
64   FrameCallbackProcessor( FrameCallbackProcessor&& )                 = default; ///< Default move constructor.
65   FrameCallbackProcessor& operator=( const FrameCallbackProcessor& ) = delete;  ///< Deleted copy assignment operator.
66   FrameCallbackProcessor& operator=( FrameCallbackProcessor&& )      = default; ///< Default move assignment operator.
67
68   /**
69    * Adds an implementation of the FrameCallbackInterface.
70    * @param[in]  frameCallback  A pointer to the implementation of the FrameCallbackInterface
71    * @param[in]  rootNode       A pointer to the root node to apply the FrameCallback to
72    */
73   void AddFrameCallback( FrameCallbackInterface* frameCallback, const Node* rootNode );
74
75   /**
76    * Removes the specified implementation of FrameCallbackInterface.
77    * @param[in]  frameCallback  A pointer to the implementation of the FrameCallbackInterface to remove.
78    */
79   void RemoveFrameCallback( FrameCallbackInterface* frameCallback );
80
81   /**
82    * Called on Update by the UpdateManager.
83    * @param[in]  bufferIndex     The bufferIndex to use
84    * @param[in]  elapsedSeconds  Time elapsed time since the last frame (in seconds)
85    */
86   void Update( BufferIndex bufferIndex, float elapsedSeconds );
87
88   /**
89    * Called by the UpdateManager when the node hierarchy changes.
90    */
91   void NodeHierarchyChanged()
92   {
93     mNodeHierarchyChanged = true;
94   }
95
96 private:
97
98   // From PropertyOwner::Observer
99
100   /**
101    * @copydoc PropertyOwner::Observer::PropertyOwnerConnected()
102    */
103   virtual void PropertyOwnerConnected( PropertyOwner& owner ) { /* Nothing to do */ }
104
105   /**
106    * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
107    */
108   virtual void PropertyOwnerDisconnected( BufferIndex updateBufferIndex, PropertyOwner& owner ) { /* Nothing to do */ }
109
110   /**
111    * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
112    *
113    * Will use this to disconnect the frame-callback if the accompanying node is destroyed
114    */
115   virtual void PropertyOwnerDestroyed( PropertyOwner& owner );
116
117 private:
118
119   struct FrameCallbackInfo
120   {
121     /**
122      * Default Constructor
123      * @param[in]  frameCallbackObject  A pointer to the frame-callback object
124      * @param[in]  updateProxyPtr       A raw pointer to the newly created updateProxy
125      * @note Ownership of @updateProxyPtr is passed to this class.
126      */
127     FrameCallbackInfo( FrameCallbackInterface* frameCallbackObject, UpdateProxy* updateProxyPtr )
128     : frameCallback( frameCallbackObject ),
129       updateProxyImpl( updateProxyPtr )
130     {
131     }
132
133     ~FrameCallbackInfo() = default; ///< Default destructor.
134
135     // Movable but not copyable
136     FrameCallbackInfo( const FrameCallbackInfo& )            = delete;  ///< Deleted copy constructor.
137     FrameCallbackInfo( FrameCallbackInfo&& )                 = default; ///< Default move constructor.
138     FrameCallbackInfo& operator=( const FrameCallbackInfo& ) = delete;  ///< Deleted copy assignment operator.
139     FrameCallbackInfo& operator=( FrameCallbackInfo&& )      = default; ///< Default move assignment operator.
140
141     // Data
142     FrameCallbackInterface* frameCallback{ nullptr }; ///< Pointer to the implementation of the FrameCallbackInterface.
143     std::unique_ptr< UpdateProxy > updateProxyImpl{ nullptr }; ///< A unique pointer to the implementation of the UpdateProxy.
144   };
145
146   std::vector< FrameCallbackInfo > mFrameCallbacks; ///< A container of all the frame-callbacks & accompanying update-proxies.
147
148   TransformManager& mTransformManager;
149
150   bool mNodeHierarchyChanged; ///< Set to true if the node hierarchy changes
151 };
152
153 } // namespace SceneGraph
154
155 } // namespace Internal
156
157 } // namespace Dali
158
159 #endif // DALI_INTERNAL_SCENE_GRAPH_FRAME_CALLBACK_PROCESSOR_H