Ensure a FrameCallback is removed properly from a FrameCallbackInterface
[platform/core/uifw/dali-core.git] / dali / internal / update / manager / scene-graph-frame-callback.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_FRAME_CALLBACK_H
2 #define DALI_INTERNAL_SCENE_GRAPH_FRAME_CALLBACK_H
3
4 /*
5  * Copyright (c) 2021 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/devel-api/threading/mutex.h>
26 #include <dali/devel-api/update/frame-callback-interface.h>
27 #include <dali/internal/common/owner-pointer.h>
28 #include <dali/internal/update/common/property-owner.h>
29 #include <dali/internal/update/manager/update-proxy-impl.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace SceneGraph
36 {
37 class Node;
38 class TransformManager;
39
40 /**
41  * This is the update-thread owned entity of the FrameCallbackInterface.
42  * @see Dali::FrameCallbackInterface
43  */
44 class FrameCallback final : public PropertyOwner::Observer
45 {
46 public:
47   /**
48    * Creates a new FrameCallback.
49    * @param[in]  frameCallbackInterface  A reference to the FrameCallbackInterface implementation
50    * @return A new FrameCallback.
51    */
52   static FrameCallback* New(FrameCallbackInterface& frameCallbackInterface);
53
54   /**
55    * Non-virtual Destructor.
56    */
57   ~FrameCallback() override;
58
59   /**
60    * Called from the update-thread when connecting to the scene-graph.
61    * @param[in]  updateManager     The Update Manager
62    * @param[in]  transformManager  The Transform Manager
63    * @param[in]  rootNode          The rootNode of this frame-callback
64    */
65   void ConnectToSceneGraph(UpdateManager& updateManager, TransformManager& transformManager, Node& rootNode);
66
67   // Movable but not copyable
68
69   FrameCallback(const FrameCallback&) = delete;            ///< Deleted copy constructor.
70   FrameCallback(FrameCallback&&)      = default;           ///< Default move constructor.
71   FrameCallback& operator=(const FrameCallback&) = delete; ///< Deleted copy assignment operator.
72   FrameCallback& operator=(FrameCallback&&) = default;     ///< Default move assignment operator.
73
74   /**
75    * Called from the update-thread after the scene has been updated, and is ready to render.
76    * @param[in]  bufferIndex           The bufferIndex to use
77    * @param[in]  elapsedSeconds        Time elapsed time since the last frame (in seconds)
78    * @param[in]  nodeHierarchyChanged  Whether the node hierarchy has changed
79    * @return Whether to continue calling this FrameCallback or not.
80    */
81   bool Update(BufferIndex bufferIndex, float elapsedSeconds, bool nodeHierarchyChanged);
82
83   /**
84    * Invalidates this FrameCallback and will no longer be associated with the FrameCallbackInterface.
85    * @note This method is thread-safe.
86    */
87   void Invalidate();
88
89   /**
90    * Comparison operator between a FrameCallback and a FrameCallbackInterface pointer.
91    * @param[in]  iFace  The FrameCallbackInterface pointer to compare with
92    * @return True if iFace matches our internally stored FrameCallbackInterface.
93    */
94   inline bool operator==(const FrameCallbackInterface* iFace)
95   {
96     return mFrameCallbackInterface == iFace;
97   }
98
99 private:
100   // From PropertyOwner::Observer
101
102   /**
103    * @copydoc PropertyOwner::Observer::PropertyOwnerConnected()
104    */
105   void PropertyOwnerConnected(PropertyOwner& owner) override
106   { /* Nothing to do */
107   }
108
109   /**
110    * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
111    */
112   void PropertyOwnerDisconnected(BufferIndex updateBufferIndex, PropertyOwner& owner) override
113   { /* Nothing to do */
114   }
115
116   /**
117    * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
118    *
119    * Will use this to disconnect the frame-callback if the accompanying node is destroyed
120    */
121   void PropertyOwnerDestroyed(PropertyOwner& owner) override;
122
123   // Construction
124
125   /**
126    * Constructor.
127    * @param[in]  frameCallbackInterface  A pointer to the FrameCallbackInterface implementation
128    */
129   FrameCallback(FrameCallbackInterface* frameCallbackInterface);
130
131 private:
132   Mutex                        mMutex;
133   std::unique_ptr<UpdateProxy> mUpdateProxy{nullptr}; ///< A unique pointer to the implementation of the UpdateProxy.
134   FrameCallbackInterface*      mFrameCallbackInterface;
135   bool                         mValid{true}; ///< Set to false when Invalidate() is called.
136 };
137
138 /**
139  * Checks if FrameCallback store iFace internally.
140  * @param[in]  frameCallback  Reference to the owner-pointer of frame-callback
141  * @param[in]  iFace          The FrameCallbackInterface pointer
142  * @return True if iFace matches the internally stored FrameCallbackInterface.
143  */
144 inline bool operator==(const OwnerPointer<FrameCallback>& frameCallback, const FrameCallbackInterface* iFace)
145 {
146   return *frameCallback == iFace;
147 }
148
149 } // namespace SceneGraph
150
151 } // namespace Internal
152
153 } // namespace Dali
154
155 #endif // DALI_INTERNAL_SCENE_GRAPH_FRAME_CALLBACK_H