(Partial update) Change resetting the updated flag
[platform/core/uifw/dali-core.git] / dali / internal / update / common / scene-graph-scene.h
1 #ifndef DALI_INTERNAL_SCENE_GRAPH_SCENE_H
2 #define DALI_INTERNAL_SCENE_GRAPH_SCENE_H
3
4 /*
5  * Copyright (c) 2022 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 // INTERNAL INCLUDES
21 #include <dali/graphics-api/graphics-controller.h>
22 #include <dali/integration-api/core.h>
23 #include <dali/integration-api/scene.h>
24 #include <dali/internal/common/message.h>
25 #include <dali/internal/event/common/event-thread-services.h>
26 #include <dali/internal/render/common/render-instruction-container.h>
27 #include <dali/internal/update/nodes/scene-graph-layer.h>
28 #include <dali/public-api/common/vector-wrapper.h>
29
30 namespace Dali
31 {
32 namespace Internal
33 {
34 namespace Render
35 {
36 class Renderer;
37 }
38
39 namespace SceneGraph
40 {
41 class RenderInstructionContainer;
42 class Node;
43
44 struct DirtyRect
45 {
46   DirtyRect(Node* node, Render::Renderer* renderer, int32_t frame, Rect<int>& rect)
47   : node(node),
48     renderer(renderer),
49     frame(frame),
50     rect(rect),
51     visited(true)
52   {
53   }
54
55   DirtyRect() = default;
56
57   bool operator<(const DirtyRect& rhs) const
58   {
59     if(node == rhs.node)
60     {
61       if(renderer == rhs.renderer)
62       {
63         return frame > rhs.frame; // Most recent rects come first
64       }
65       else
66       {
67         return renderer < rhs.renderer;
68       }
69     }
70     else
71     {
72       return node < rhs.node;
73     }
74   }
75
76   Node*             node{nullptr};
77   Render::Renderer* renderer{nullptr};
78   int32_t           frame{0};
79   Rect<int32_t>     rect{};
80   bool              visited{true};
81 };
82
83 class Scene
84 {
85 public:
86   /**
87    * Constructor
88    * @param[in] surface The render surface
89    */
90   Scene();
91
92   /**
93    * Destructor
94    */
95   virtual ~Scene();
96
97   /**
98    * Creates a scene object in the GPU.
99    * @param[in] graphicsController The graphics controller
100    * @param[in] depthBufferAvailable True if there is a depth buffer
101    * @param[in] stencilBufferAvailable True if there is a stencil buffer
102    */
103   void Initialize(Graphics::Controller& graphicsController, Integration::DepthBufferAvailable depthBufferAvailable, Integration::StencilBufferAvailable stencilBufferAvailable);
104
105   /**
106    * Gets the render instructions for the scene
107    * @return the render instructions
108    */
109   RenderInstructionContainer& GetRenderInstructions();
110
111   /**
112    * @brief Adds a callback that is called when the frame rendering is done by the graphics driver.
113    *
114    * @param[in] callback The function to call
115    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
116    *
117    * @note A callback of the following type may be used:
118    * @code
119    *   void MyFunction( int frameId );
120    * @endcode
121    * This callback will be deleted once it is called.
122    *
123    * @note Ownership of the callback is passed onto this class.
124    */
125   void AddFrameRenderedCallback(CallbackBase* callback, int32_t frameId);
126
127   /**
128    * @brief Adds a callback that is called when the frame is displayed on the display.
129    *
130    * @param[in] callback The function to call
131    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
132    *
133    * @note A callback of the following type may be used:
134    * @code
135    *   void MyFunction( int frameId );
136    * @endcode
137    * This callback will be deleted once it is called.
138    *
139    * @note Ownership of the callback is passed onto this class.
140    */
141   void AddFramePresentedCallback(CallbackBase* callback, int32_t frameId);
142
143   /**
144    * @brief Gets the callback list that is called when the frame rendering is done by the graphics driver.
145    *
146    * @param[out] callbacks The callback list
147    */
148   void GetFrameRenderedCallback(Dali::Integration::Scene::FrameCallbackContainer& callbacks);
149
150   /**
151    * @brief Gets the callback list that is called when the frame is displayed on the display.
152    *
153    * @param[out] callbacks The callback list
154    */
155   void GetFramePresentedCallback(Dali::Integration::Scene::FrameCallbackContainer& callbacks);
156
157   /**
158    * @brief Sets whether rendering should be skipped or not.
159    * @param[in] skip true if rendering should be skipped.
160    */
161   void SetSkipRendering(bool skip);
162
163   /**
164    * @brief Query whether rendering should be skipped or not.
165    * @return true if rendering should be skipped, false otherwise.
166    */
167   bool IsRenderingSkipped() const;
168
169   /**
170    * Set the surface rectangle when surface is resized.
171    *
172    * @param[in] scene The resized scene.
173    * @param[in] rect The retangle representing the surface.
174    */
175   void SetSurfaceRect(const Rect<int32_t>& rect);
176
177   /**
178    * Get the surface rectangle.
179    *
180    * @return the current surface rectangle
181    */
182   const Rect<int32_t>& GetSurfaceRect() const;
183
184   /**
185    * Set the surface orientation when surface is rotated.
186    *
187    * @param[in] orientation The orientation value representing the surface.
188    */
189   void SetSurfaceOrientation(int32_t orientation);
190
191   /**
192    * Get the surface orientation.
193    *
194    * @return the current surface orientation
195    */
196   int32_t GetSurfaceOrientation() const;
197
198   /**
199    * Query wheter the surface rect is changed or not.
200    * @return true if the surface rect is changed.
201    */
202   bool IsSurfaceRectChanged();
203
204   /**
205    * @brief Set the internal flag to acknowledge surface rotation.
206    */
207   void SetRotationCompletedAcknowledgement();
208
209   /**
210    * @brief Query wheter is set to acknowledge for completing surface rotation.
211    * @return true it should be acknowledged.
212    */
213   bool IsRotationCompletedAcknowledgementSet();
214
215   /**
216    * Set the render target of the surface
217    *
218    * @param[in] renderTarget The render target.
219    */
220   void SetSurfaceRenderTargetCreateInfo(const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo);
221
222   /**
223    * Get the render target created for the scene
224    *
225    * @return the render target
226    */
227   [[nodiscard]] Graphics::RenderTarget* GetSurfaceRenderTarget() const
228   {
229     return mRenderTarget.get();
230   }
231
232   /**
233    * Get the graphics render pass created for the scene
234    *
235    * @return the graphics render pass
236    */
237   [[nodiscard]] Graphics::RenderPass* GetGraphicsRenderPass(Graphics::AttachmentLoadOp loadOp, Graphics::AttachmentStoreOp storeOp) const
238   {
239     if(loadOp == Graphics::AttachmentLoadOp::CLEAR)
240     {
241       return mRenderPass.get();
242     }
243     else
244     {
245       return mRenderPassNoClear.get();
246     }
247   }
248
249   /**
250    * Get an initialized array of clear values which then can be modified and accessed to BeginRenderPass() command.
251    *
252    * @return the array of clear values
253    */
254   [[nodiscard]] auto& GetGraphicsRenderPassClearValues()
255   {
256     return mClearValues;
257   }
258
259   /**
260    * @brief Set a root of the Scene
261    *
262    * @param layer The root layer
263    */
264   void SetRoot(SceneGraph::Layer* layer)
265   {
266     mRoot = layer;
267   }
268
269   /**
270    * @brief Get a root of the Scene
271    *
272    * @return The root layer
273    */
274   SceneGraph::Layer* GetRoot() const
275   {
276     return mRoot;
277   }
278
279   /**
280    * @brief Get ItemsDirtyRects
281    *
282    * @return the ItemsDirtyRects
283    */
284   std::vector<DirtyRect>& GetItemsDirtyRects();
285
286 private:
287   // Render instructions describe what should be rendered during RenderManager::RenderScene()
288   // Update manager updates instructions for the next frame while we render the current one
289
290   RenderInstructionContainer mInstructions; ///< Render instructions for the scene
291
292   Graphics::Controller* mGraphicsController; ///< Graphics controller
293
294   Dali::Integration::Scene::FrameCallbackContainer mFrameRenderedCallbacks;  ///< Frame rendered callbacks
295   Dali::Integration::Scene::FrameCallbackContainer mFramePresentedCallbacks; ///< Frame presented callbacks
296
297   bool mSkipRendering; ///< A flag to skip rendering
298
299   Rect<int32_t> mSurfaceRect;                      ///< The rectangle of surface which is related ot this scene.
300   int32_t       mSurfaceOrientation;               ///< The orientation of surface which is related of this scene
301   bool          mSurfaceRectChanged;               ///< The flag of surface's rectangle is changed when is resized, moved or rotated.
302   bool          mRotationCompletedAcknowledgement; ///< The flag of sending the acknowledgement to complete window rotation.
303
304   // Render pass and render target
305
306   Graphics::RenderTargetCreateInfo mRenderTargetCreateInfo; // Passed in by message before 2nd stage Initialization happens.
307
308   /**
309    * Render pass is created on fly depending on Load and Store operations
310    * The default render pass (most likely to be used) is the load = CLEAR
311    * and store = STORE for color attachment.
312    */
313   Graphics::UniquePtr<Graphics::RenderPass>   mRenderPass{nullptr};        ///< The render pass created to render the surface
314   Graphics::UniquePtr<Graphics::RenderPass>   mRenderPassNoClear{nullptr}; ///< The render pass created to render the surface without clearing color
315   Graphics::UniquePtr<Graphics::RenderTarget> mRenderTarget{nullptr};      ///< This is created in Update/Render thread when surface is created/resized/replaced
316
317   SceneGraph::Layer* mRoot{nullptr}; ///< Root node
318
319   std::vector<Graphics::ClearValue>                  mClearValues{};     ///< Clear colors
320   std::vector<Dali::Internal::SceneGraph::DirtyRect> mItemsDirtyRects{}; ///< Dirty rect list
321 };
322
323 /// Messages
324 inline void AddFrameRenderedCallbackMessage(EventThreadServices& eventThreadServices, const Scene& scene, const CallbackBase* callback, int32_t frameId)
325 {
326   using LocalType = MessageValue2<Scene, CallbackBase*, int32_t>;
327
328   // Reserve some memory inside the message queue
329   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
330
331   // Construct message in the message queue memory; note that delete should not be called on the return value
332   new(slot) LocalType(&scene, &Scene::AddFrameRenderedCallback, const_cast<CallbackBase*>(callback), frameId);
333 }
334
335 inline void AddFramePresentedCallbackMessage(EventThreadServices& eventThreadServices, const Scene& scene, const CallbackBase* callback, int32_t frameId)
336 {
337   using LocalType = MessageValue2<Scene, CallbackBase*, int32_t>;
338
339   // Reserve some memory inside the message queue
340   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
341
342   // Construct message in the message queue memory; note that delete should not be called on the return value
343   new(slot) LocalType(&scene, &Scene::AddFramePresentedCallback, const_cast<CallbackBase*>(callback), frameId);
344 }
345
346 inline void SetSurfaceRectMessage(EventThreadServices& eventThreadServices, const Scene& scene, const Rect<int32_t>& rect)
347 {
348   using LocalType = MessageValue1<Scene, Rect<int32_t> >;
349
350   // Reserve some memory inside the message queue
351   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
352
353   // Construct message in the message queue memory; note that delete should not be called on the return value
354   new(slot) LocalType(&scene, &Scene::SetSurfaceRect, rect);
355 }
356
357 inline void SetSurfaceOrientationMessage(EventThreadServices& eventThreadServices, const Scene& scene, int32_t orientation)
358 {
359   using LocalType = MessageValue1<Scene, int32_t>;
360
361   // Reserve some memory inside the message queue
362   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
363
364   // Construct message in the message queue memory; note that delete should not be called on the return value
365   new(slot) LocalType(&scene, &Scene::SetSurfaceOrientation, orientation);
366 }
367
368 inline void SetRotationCompletedAcknowledgementMessage(EventThreadServices& eventThreadServices, const Scene& scene)
369 {
370   using LocalType = Message<Scene>;
371
372   // Reserve some memory inside the message queue
373   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
374
375   // Construct message in the message queue memory; note that delete should not be called on the return value
376   new(slot) LocalType(&scene, &Scene::SetRotationCompletedAcknowledgement);
377 }
378
379 inline void SetSurfaceRenderTargetCreateInfoMessage(EventThreadServices& eventThreadServices, const Scene& scene, const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo)
380 {
381   using LocalType = MessageValue1<Scene, Graphics::RenderTargetCreateInfo>;
382
383   // Reserve some memory inside the message queue
384   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
385
386   // Construct message in the message queue memory; note that delete should not be called on the return value
387   new(slot) LocalType(&scene, &Scene::SetSurfaceRenderTargetCreateInfo, renderTargetCreateInfo);
388 }
389
390 } // namespace SceneGraph
391
392 } // namespace Internal
393
394 } // namespace Dali
395
396 #endif // DALI_INTERNAL_SCENE_GRAPH_SCENE_H