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