Fix the synchronization issue when window is resized or rotated
[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) 2020 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/integration-api/scene.h>
22 #include <dali/internal/common/message.h>
23 #include <dali/internal/event/common/event-thread-services.h>
24 #include <dali/internal/render/common/render-instruction-container.h>
25 #include <dali/public-api/common/vector-wrapper.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 class Context;
34
35 namespace SceneGraph
36 {
37 class RenderInstructionContainer;
38
39 class Scene
40 {
41 public:
42
43   /**
44    * Constructor
45    * @param[in] surface The render surface
46    */
47   Scene();
48
49   /**
50    * Destructor
51    */
52   virtual ~Scene();
53
54   /**
55    * Creates a scene object in the GPU.
56    * @param[in] context The GL context
57    */
58   void Initialize( Context& context );
59
60   /**
61    * Gets the context holding the GL state of rendering for the scene
62    * @return the context
63    */
64   Context* GetContext();
65
66   /**
67    * Gets the render instructions for the scene
68    * @return the render instructions
69    */
70   RenderInstructionContainer& GetRenderInstructions();
71
72   /**
73    * @brief Adds a callback that is called when the frame rendering is done by the graphics driver.
74    *
75    * @param[in] callback The function to call
76    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
77    *
78    * @note A callback of the following type may be used:
79    * @code
80    *   void MyFunction( int frameId );
81    * @endcode
82    * This callback will be deleted once it is called.
83    *
84    * @note Ownership of the callback is passed onto this class.
85    */
86   void AddFrameRenderedCallback( CallbackBase* callback, int32_t frameId );
87
88   /**
89    * @brief Adds a callback that is called when the frame is displayed on the display.
90    *
91    * @param[in] callback The function to call
92    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
93    *
94    * @note A callback of the following type may be used:
95    * @code
96    *   void MyFunction( int frameId );
97    * @endcode
98    * This callback will be deleted once it is called.
99    *
100    * @note Ownership of the callback is passed onto this class.
101    */
102   void AddFramePresentedCallback( CallbackBase* callback, int32_t frameId );
103
104   /**
105    * @brief Gets the callback list that is called when the frame rendering is done by the graphics driver.
106    *
107    * @param[out] callbacks The callback list
108    */
109   void GetFrameRenderedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks );
110
111   /**
112    * @brief Gets the callback list that is called when the frame is displayed on the display.
113    *
114    * @param[out] callbacks The callback list
115    */
116   void GetFramePresentedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks );
117
118   /**
119    * @brief Sets whether rendering should be skipped or not.
120    * @param[in] skip true if rendering should be skipped.
121    */
122   void SetSkipRendering( bool skip );
123
124   /**
125    * @brief Query whether rendering should be skipped or not.
126    * @return true if rendering should be skipped, false otherwise.
127    */
128   bool IsRenderingSkipped() const;
129
130   /**
131    * Set the surface rectangle when surface is resized.
132    *
133    * @param[in] scene The resized scene.
134    * @param[in] rect The retangle representing the surface.
135    */
136   void SetSurfaceRect( const Rect<int32_t>& rect );
137
138   /**
139    * Get the surface rectangle.
140    *
141    * @return the current surface rectangle
142    */
143   const Rect<int32_t>& GetSurfaceRect() const;
144
145   /**
146    * Set the surface orientation when surface is rotated.
147    *
148    * @param[in] scene The rotated scene.
149    * @param[in] orientation The orientation value representing the surface.
150    */
151   void SetSurfaceOrientation( int32_t orientation );
152
153   /**
154    * Get the surface orientation.
155    *
156    * @return the current surface orientation
157    */
158   int32_t GetSurfaceOrientation() const;
159
160   /**
161    * Query wheter the surface rect is changed or not.
162    * @return true if the surface rect is changed.
163    */
164   bool IsSurfaceRectChanged();
165
166 private:
167
168   Context*                    mContext;   ///< The context holding the GL state of rendering for the scene, not owned
169
170   // Render instructions describe what should be rendered during RenderManager::RenderScene()
171   // Update manager updates instructions for the next frame while we render the current one
172
173   RenderInstructionContainer  mInstructions;   ///< Render instructions for the scene
174
175   Dali::Integration::Scene::FrameCallbackContainer mFrameRenderedCallbacks;   ///< Frame rendered callbacks
176   Dali::Integration::Scene::FrameCallbackContainer mFramePresentedCallbacks;  ///< Frame presented callbacks
177
178   bool                        mSkipRendering;  ///< A flag to skip rendering
179
180   Rect<int32_t>               mSurfaceRect;           ///< The rectangle of surface which is related ot this scene.
181   int32_t                     mSurfaceOrientation;    ///< The orientation of surface which is related of this scene
182   bool                        mSurfaceRectChanged;    ///< The flag of surface's rectangle is changed when is resized, moved or rotated.
183 };
184
185 /// Messages
186 inline void AddFrameRenderedCallbackMessage( EventThreadServices& eventThreadServices, const Scene& scene, const CallbackBase* callback, int32_t frameId )
187 {
188   using LocalType = MessageValue2<Scene, CallbackBase*, int32_t>;
189
190   // Reserve some memory inside the message queue
191   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
192
193   // Construct message in the message queue memory; note that delete should not be called on the return value
194   new (slot) LocalType( &scene, &Scene::AddFrameRenderedCallback, const_cast< CallbackBase* >( callback ), frameId );
195 }
196
197 inline void AddFramePresentedCallbackMessage( EventThreadServices& eventThreadServices, const Scene& scene, const CallbackBase* callback, int32_t frameId )
198 {
199   using LocalType = MessageValue2<Scene, CallbackBase*, int32_t>;
200
201   // Reserve some memory inside the message queue
202   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
203
204   // Construct message in the message queue memory; note that delete should not be called on the return value
205   new (slot) LocalType( &scene, &Scene::AddFramePresentedCallback, const_cast< CallbackBase* >( callback ), frameId );
206 }
207
208 inline void SetSurfaceRectMessage( EventThreadServices& eventThreadServices, const Scene& scene, const Rect<int32_t>& rect )
209 {
210   using LocalType = MessageValue1<Scene, Rect<int32_t> >;
211
212   // Reserve some memory inside the message queue
213   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
214
215   // Construct message in the message queue memory; note that delete should not be called on the return value
216   new (slot) LocalType( &scene, &Scene::SetSurfaceRect, rect );
217 }
218
219 inline void SetSurfaceOrientationMessage( EventThreadServices& eventThreadServices, const Scene& scene, int32_t orientation )
220 {
221   using LocalType = MessageValue1<Scene, int32_t>;
222
223   // Reserve some memory inside the message queue
224   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
225
226   // Construct message in the message queue memory; note that delete should not be called on the return value
227   new (slot) LocalType( &scene, &Scene::SetSurfaceOrientation, orientation );
228 }
229
230 } // namespace SceneGraph
231
232 } // namespace Internal
233
234 } // namespace Dali
235
236
237 #endif // DALI_INTERNAL_SCENE_GRAPH_SCENE_H