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