RenderPass and presentation
[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/scene.h>
23 #include <dali/internal/common/message.h>
24 #include <dali/internal/event/common/event-thread-services.h>
25 #include <dali/internal/render/common/render-instruction-container.h>
26 #include <dali/public-api/common/vector-wrapper.h>
27
28 namespace Dali
29 {
30 namespace Internal
31 {
32 class Context;
33
34 namespace SceneGraph
35 {
36 class RenderInstructionContainer;
37
38 class Scene
39 {
40 public:
41   /**
42    * Constructor
43    * @param[in] surface The render surface
44    */
45   Scene();
46
47   /**
48    * Destructor
49    */
50   virtual ~Scene();
51
52   /**
53    * Creates a scene object in the GPU.
54    * @param[in] context The GL context
55    * @param[in] graphicsController The graphics controller
56    */
57   void Initialize(Context& context, Graphics::Controller& graphicsController);
58
59   /**
60    * Gets the context holding the GL state of rendering for the scene
61    * @return the context
62    */
63   Context* GetContext();
64
65   /**
66    * Gets the render instructions for the scene
67    * @return the render instructions
68    */
69   RenderInstructionContainer& GetRenderInstructions();
70
71   /**
72    * @brief Adds a callback that is called when the frame rendering is done by the graphics driver.
73    *
74    * @param[in] callback The function to call
75    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
76    *
77    * @note A callback of the following type may be used:
78    * @code
79    *   void MyFunction( int frameId );
80    * @endcode
81    * This callback will be deleted once it is called.
82    *
83    * @note Ownership of the callback is passed onto this class.
84    */
85   void AddFrameRenderedCallback(CallbackBase* callback, int32_t frameId);
86
87   /**
88    * @brief Adds a callback that is called when the frame is displayed on the display.
89    *
90    * @param[in] callback The function to call
91    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
92    *
93    * @note A callback of the following type may be used:
94    * @code
95    *   void MyFunction( int frameId );
96    * @endcode
97    * This callback will be deleted once it is called.
98    *
99    * @note Ownership of the callback is passed onto this class.
100    */
101   void AddFramePresentedCallback(CallbackBase* callback, int32_t frameId);
102
103   /**
104    * @brief Gets the callback list that is called when the frame rendering is done by the graphics driver.
105    *
106    * @param[out] callbacks The callback list
107    */
108   void GetFrameRenderedCallback(Dali::Integration::Scene::FrameCallbackContainer& callbacks);
109
110   /**
111    * @brief Gets the callback list that is called when the frame is displayed on the display.
112    *
113    * @param[out] callbacks The callback list
114    */
115   void GetFramePresentedCallback(Dali::Integration::Scene::FrameCallbackContainer& callbacks);
116
117   /**
118    * @brief Sets whether rendering should be skipped or not.
119    * @param[in] skip true if rendering should be skipped.
120    */
121   void SetSkipRendering(bool skip);
122
123   /**
124    * @brief Query whether rendering should be skipped or not.
125    * @return true if rendering should be skipped, false otherwise.
126    */
127   bool IsRenderingSkipped() const;
128
129   /**
130    * Set the surface rectangle when surface is resized.
131    *
132    * @param[in] scene The resized scene.
133    * @param[in] rect The retangle representing the surface.
134    */
135   void SetSurfaceRect(const Rect<int32_t>& rect);
136
137   /**
138    * Get the surface rectangle.
139    *
140    * @return the current surface rectangle
141    */
142   const Rect<int32_t>& GetSurfaceRect() const;
143
144   /**
145    * Set the surface orientation when surface is rotated.
146    *
147    * @param[in] scene The rotated scene.
148    * @param[in] orientation The orientation value representing the surface.
149    */
150   void SetSurfaceOrientation(int32_t orientation);
151
152   /**
153    * Get the surface orientation.
154    *
155    * @return the current surface orientation
156    */
157   int32_t GetSurfaceOrientation() const;
158
159   /**
160    * Query wheter the surface rect is changed or not.
161    * @return true if the surface rect is changed.
162    */
163   bool IsSurfaceRectChanged();
164
165   /**
166    * Set the render target of the surface
167    *
168    * @param[in] renderTarget The render target.
169    */
170   void SetSurfaceRenderTarget(Graphics::RenderTarget* renderTarget)
171   {
172     mRenderTarget = renderTarget;
173   }
174
175   /**
176    * Get the render target created for the scene
177    *
178    * @return the render target
179    */
180   [[nodiscard]] Graphics::RenderTarget* GetSurfaceRenderTarget() const
181   {
182     return mRenderTarget;
183   }
184
185   /**
186    * Get the graphics render pass created for the scene
187    *
188    * @return the graphics render pass
189    */
190   [[nodiscard]] Graphics::RenderPass* GetGraphicsRenderPass(Graphics::AttachmentLoadOp loadOp, Graphics::AttachmentStoreOp storeOp) const
191   {
192     if(loadOp == Graphics::AttachmentLoadOp::CLEAR)
193     {
194       return mRenderPass.get();
195     }
196     else
197     {
198       return mRenderPassNoClear.get();
199     }
200   }
201
202   /**
203    * Get an initialized array of clear values which then can be modified and accessed to BeginRenderPass() command.
204    *
205    * @return the array of clear values
206    */
207   [[nodiscard]] auto& GetGraphicsRenderPassClearValues()
208   {
209     return mClearValues;
210   }
211
212 private:
213   Context* mContext; ///< The context holding the GL state of rendering for the scene, not owned
214
215   // Render instructions describe what should be rendered during RenderManager::RenderScene()
216   // Update manager updates instructions for the next frame while we render the current one
217
218   RenderInstructionContainer mInstructions; ///< Render instructions for the scene
219
220   Dali::Integration::Scene::FrameCallbackContainer mFrameRenderedCallbacks;  ///< Frame rendered callbacks
221   Dali::Integration::Scene::FrameCallbackContainer mFramePresentedCallbacks; ///< Frame presented callbacks
222
223   bool mSkipRendering; ///< A flag to skip rendering
224
225   Rect<int32_t> mSurfaceRect;        ///< The rectangle of surface which is related ot this scene.
226   int32_t       mSurfaceOrientation; ///< The orientation of surface which is related of this scene
227   bool          mSurfaceRectChanged; ///< The flag of surface's rectangle is changed when is resized, moved or rotated.
228
229   // Render pass and render target
230
231   /**
232    * Render pass is created on fly depending on Load and Store operations
233    * The default render pass (most likely to be used) is the load = CLEAR
234    * and store = STORE for color attachment.
235    */
236   Graphics::UniquePtr<Graphics::RenderPass> mRenderPass{nullptr};   ///< The render pass created to render the surface
237   Graphics::UniquePtr<Graphics::RenderPass> mRenderPassNoClear{nullptr};   ///< The render pass created to render the surface without clearing color
238   Graphics::RenderTarget*                   mRenderTarget{nullptr}; ///< This is created in the event thread when surface is created/resized/replaced
239
240   // clear colors
241   std::vector<Graphics::ClearValue> mClearValues{};
242 };
243
244 /// Messages
245 inline void AddFrameRenderedCallbackMessage(EventThreadServices& eventThreadServices, const Scene& scene, const CallbackBase* callback, int32_t frameId)
246 {
247   using LocalType = MessageValue2<Scene, CallbackBase*, int32_t>;
248
249   // Reserve some memory inside the message queue
250   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
251
252   // Construct message in the message queue memory; note that delete should not be called on the return value
253   new(slot) LocalType(&scene, &Scene::AddFrameRenderedCallback, const_cast<CallbackBase*>(callback), frameId);
254 }
255
256 inline void AddFramePresentedCallbackMessage(EventThreadServices& eventThreadServices, const Scene& scene, const CallbackBase* callback, int32_t frameId)
257 {
258   using LocalType = MessageValue2<Scene, CallbackBase*, int32_t>;
259
260   // Reserve some memory inside the message queue
261   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
262
263   // Construct message in the message queue memory; note that delete should not be called on the return value
264   new(slot) LocalType(&scene, &Scene::AddFramePresentedCallback, const_cast<CallbackBase*>(callback), frameId);
265 }
266
267 inline void SetSurfaceRectMessage(EventThreadServices& eventThreadServices, const Scene& scene, const Rect<int32_t>& rect)
268 {
269   using LocalType = MessageValue1<Scene, Rect<int32_t> >;
270
271   // Reserve some memory inside the message queue
272   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
273
274   // Construct message in the message queue memory; note that delete should not be called on the return value
275   new(slot) LocalType(&scene, &Scene::SetSurfaceRect, rect);
276 }
277
278 inline void SetSurfaceOrientationMessage(EventThreadServices& eventThreadServices, const Scene& scene, int32_t orientation)
279 {
280   using LocalType = MessageValue1<Scene, int32_t>;
281
282   // Reserve some memory inside the message queue
283   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
284
285   // Construct message in the message queue memory; note that delete should not be called on the return value
286   new(slot) LocalType(&scene, &Scene::SetSurfaceOrientation, orientation);
287 }
288
289 inline void SetSurfaceRenderTargetMessage(EventThreadServices& eventThreadServices, const Scene& scene, Graphics::RenderTarget* renderTarget)
290 {
291   using LocalType = MessageValue1<Scene, Graphics::RenderTarget*>;
292
293   // Reserve some memory inside the message queue
294   uint32_t* slot = eventThreadServices.ReserveMessageSlot(sizeof(LocalType));
295
296   // Construct message in the message queue memory; note that delete should not be called on the return value
297   new(slot) LocalType(&scene, &Scene::SetSurfaceRenderTarget, renderTarget);
298 }
299
300 } // namespace SceneGraph
301
302 } // namespace Internal
303
304 } // namespace Dali
305
306 #endif // DALI_INTERNAL_SCENE_GRAPH_SCENE_H