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