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