9eaf71d51cf5e26da2077885468f02bebbbccf67
[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) 2023 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/scene-graph-traveler.h>
30 #include <dali/internal/update/manager/update-proxy-impl.h>
31 #include <dali/public-api/common/list-wrapper.h>
32
33 namespace Dali
34 {
35 namespace Internal
36 {
37 namespace SceneGraph
38 {
39 class Node;
40 class TransformManager;
41
42 /**
43  * This is the update-thread owned entity of the FrameCallbackInterface.
44  * @see Dali::FrameCallbackInterface
45  */
46 class FrameCallback final : public PropertyOwner::Observer
47 {
48 public:
49   /**
50    * A set of bit mask options that, when combined, define the requests from this FrameCallback
51    * after being called from the update-thread.
52    */
53   enum RequestFlags
54   {
55     CONTINUE_CALLING = 1 << 0, ///< True if we request to continue calling this FrameCallback.
56     KEEP_RENDERING   = 1 << 1  ///< True if we request to keep rendering
57   };
58
59   /**
60    * Creates a new FrameCallback.
61    * @param[in]  frameCallbackInterface  A reference to the FrameCallbackInterface implementation
62    * @return A new FrameCallback.
63    */
64   static FrameCallback* New(FrameCallbackInterface& frameCallbackInterface);
65
66   /**
67    * Non-virtual Destructor.
68    */
69   ~FrameCallback() override;
70
71   /**
72    * Called from the update-thread when connecting to the scene-graph.
73    * @param[in]  updateManager     The Update Manager
74    * @param[in]  transformManager  The Transform Manager
75    * @param[in]  rootNode          The rootNode of this frame-callback
76    * @param[in]  traveler          The cache of traversal for given rootNode
77    */
78   void ConnectToSceneGraph(UpdateManager& updateManager, TransformManager& transformManager, Node& rootNode, SceneGraphTravelerPtr traveler);
79
80   // Movable but not copyable
81
82   FrameCallback(const FrameCallback&) = delete;            ///< Deleted copy constructor.
83   FrameCallback(FrameCallback&&)      = default;           ///< Default move constructor.
84   FrameCallback& operator=(const FrameCallback&) = delete; ///< Deleted copy assignment operator.
85   FrameCallback& operator=(FrameCallback&&) = default;     ///< Default move assignment operator.
86
87   /**
88    * Called from the update-thread after the scene has been updated, and is ready to render.
89    * @param[in]  bufferIndex           The bufferIndex to use
90    * @param[in]  elapsedSeconds        Time elapsed time since the last frame (in seconds)
91    * @param[in]  nodeHierarchyChanged  Whether the node hierarchy has changed
92    * @return The requests from this FrameCallback.
93    */
94   RequestFlags Update(BufferIndex bufferIndex, float elapsedSeconds, bool nodeHierarchyChanged);
95
96   /**
97    * Called from the update thread when there's a sync point to insert.
98    * @param[in] syncPoint The sync point to insert before the next update
99    */
100   void Notify(Dali::UpdateProxy::NotifySyncPoint syncPoint);
101
102   /**
103    * Invalidates this FrameCallback and will no longer be associated with the FrameCallbackInterface.
104    * @note This method is thread-safe.
105    */
106   void Invalidate();
107
108   /**
109    * Comparison operator between a FrameCallback and a FrameCallbackInterface pointer.
110    * @param[in]  iFace  The FrameCallbackInterface pointer to compare with
111    * @return True if iFace matches our internally stored FrameCallbackInterface.
112    */
113   inline bool operator==(const FrameCallbackInterface* iFace)
114   {
115     return mFrameCallbackInterface == iFace;
116   }
117
118 private:
119   // From PropertyOwner::Observer
120
121   /**
122    * @copydoc PropertyOwner::Observer::PropertyOwnerConnected()
123    */
124   void PropertyOwnerConnected(PropertyOwner& owner) override
125   { /* Nothing to do */
126   }
127
128   /**
129    * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
130    */
131   void PropertyOwnerDisconnected(BufferIndex updateBufferIndex, PropertyOwner& owner) override
132   { /* Nothing to do */
133   }
134
135   /**
136    * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected()
137    *
138    * Will use this to disconnect the frame-callback if the accompanying node is destroyed
139    */
140   void PropertyOwnerDestroyed(PropertyOwner& owner) override;
141
142   // Construction
143
144   /**
145    * Constructor.
146    * @param[in]  frameCallbackInterface  A pointer to the FrameCallbackInterface implementation
147    */
148   FrameCallback(FrameCallbackInterface* frameCallbackInterface);
149
150 private:
151   Mutex                                         mMutex;
152   std::unique_ptr<UpdateProxy>                  mUpdateProxy{nullptr}; ///< A unique pointer to the implementation of the UpdateProxy.
153   FrameCallbackInterface*                       mFrameCallbackInterface;
154   std::list<Dali::UpdateProxy::NotifySyncPoint> mSyncPoints;
155   bool                                          mValid{true}; ///< Set to false when Invalidate() is called.
156 };
157
158 /**
159  * Checks if FrameCallback store iFace internally.
160  * @param[in]  frameCallback  Reference to the owner-pointer of frame-callback
161  * @param[in]  iFace          The FrameCallbackInterface pointer
162  * @return True if iFace matches the internally stored FrameCallbackInterface.
163  */
164 inline bool operator==(const OwnerPointer<FrameCallback>& frameCallback, const FrameCallbackInterface* iFace)
165 {
166   return *frameCallback == iFace;
167 }
168
169 } // namespace SceneGraph
170
171 } // namespace Internal
172
173 } // namespace Dali
174
175 #endif // DALI_INTERNAL_SCENE_GRAPH_FRAME_CALLBACK_H