Fix the synchronization issue when window is resized or rotated
[platform/core/uifw/dali-core.git] / dali / internal / event / common / scene-impl.h
1 #ifndef DALI_INTERNAL_SCENE_H
2 #define DALI_INTERNAL_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
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/scene.h>
23 #include <dali/internal/common/owner-pointer.h>
24 #include <dali/internal/event/actors/layer-impl.h>
25 #include <dali/internal/event/events/event-processor.h>
26 #include <dali/internal/event/render-tasks/render-task-defaults.h>
27 #include <dali/public-api/actors/layer.h>
28 #include <dali/public-api/math/vector2.h>
29 #include <dali/public-api/render-tasks/render-task-list.h>
30
31 namespace Dali
32 {
33 namespace Integration
34 {
35 struct Event;
36
37 }
38
39 namespace Internal
40 {
41 //@todo Break this dependence somehow.
42 namespace Render
43 {
44 class Renderer;
45 }
46
47 namespace SceneGraph
48 {
49 class Scene;
50
51 struct DirtyRect
52 {
53   DirtyRect(Node* node, Render::Renderer* renderer, int frame, Rect<int>& rect)
54   : node(node),
55     renderer(renderer),
56     frame(frame),
57     rect(rect),
58     visited(true)
59   {
60   }
61
62   DirtyRect()
63   : node(nullptr),
64     renderer(nullptr),
65     frame(0),
66     rect(),
67     visited(true)
68   {
69   }
70
71   bool operator<(const DirtyRect& rhs) const
72   {
73     if(node == rhs.node)
74     {
75       if(renderer == rhs.renderer)
76       {
77         return frame > rhs.frame; // Most recent rects come first
78       }
79       else
80       {
81         return renderer < rhs.renderer;
82       }
83     }
84     else
85     {
86       return node < rhs.node;
87     }
88   }
89
90   Node*             node;
91   Render::Renderer* renderer;
92   int               frame;
93
94   Rect<int> rect;
95   bool      visited;
96 };
97
98 } // namespace SceneGraph
99
100 class EventProcessor;
101 class Layer;
102 class LayerList;
103 class CameraActor;
104 class RenderTaskList;
105 class FrameBuffer;
106
107 using FrameBufferPtr = IntrusivePtr<FrameBuffer>;
108 using ScenePtr       = IntrusivePtr<Scene>;
109
110 /**
111  * @brief Scene creates a "world" that can be bound to a surface for rendering.
112  */
113 class Scene : public BaseObject, public RenderTaskDefaults
114 {
115 public:
116   /**
117    * @copydoc Dali::Integration::Scene::New
118    */
119   static ScenePtr New(Size size, int orientation = 0);
120
121   /**
122    * virtual destructor
123    */
124   ~Scene() override;
125
126   /**
127    * @copydoc Dali::Integration::Scene::Add
128    */
129   void Add(Actor& actor);
130
131   /**
132    * @copydoc Dali::Integration::Scene::Remove
133    */
134   void Remove(Actor& actor);
135
136   /**
137    * @copydoc Dali::Integration::Scene::GetSize
138    */
139   Size GetSize() const;
140
141   /**
142    * @copydoc Dali::Integration::Scene::SetDpi
143    */
144   void SetDpi(Vector2 dpi);
145
146   /**
147    * @copydoc Dali::Integration::Scene::GetDpi
148    */
149   Vector2 GetDpi() const;
150
151   /**
152    * @copydoc Dali::Integration::Scene::GetRenderTaskList
153    */
154   RenderTaskList& GetRenderTaskList() const;
155
156   /**
157    * @copydoc Dali::Integration::Scene::GetRootLayer
158    */
159   Dali::Layer GetRootLayer() const;
160
161   /**
162    * @copydoc Dali::Integration::Scene::GetLayerCount
163    */
164   uint32_t GetLayerCount() const;
165
166   /**
167    * @copydoc Dali::Integration::Scene::GetLayer
168    */
169   Dali::Layer GetLayer(uint32_t depth) const;
170
171   /**
172    * Notify the surface has been resized.
173    *
174    * @param[in] width The new width of the set surface
175    * @param[in] height The new height of the set surface
176    */
177   void SurfaceResized(float width, float height);
178
179   /**
180    * @copydoc Dali::Integration::Scene::SurfaceReplaced
181    */
182   void SurfaceReplaced();
183
184   /**
185    * @copydoc Dali::Integration::Scene::Discard
186    */
187   void Discard();
188
189   /**
190    * Retrieve the ordered list of on-scene layers.
191    * @return The layer-list.
192    */
193   LayerList& GetLayerList() const;
194
195   /**
196    * Request that the depth tree is rebuilt
197    */
198   void RequestRebuildDepthTree();
199
200   /**
201    * This function is called when an event is queued.
202    * @param[in] event A event to queue.
203    */
204   void QueueEvent(const Integration::Event& event);
205
206   /**
207    * This function is called by Core when events are processed.
208    */
209   void ProcessEvents();
210
211   /**
212    * Rebuilds the depth tree at the end of the event frame if
213    * it was requested this frame.
214    */
215   void RebuildDepthTree();
216
217   /**
218    * @brief Sets the background color of the render surface.
219    * @param[in] color The new background color
220    */
221   void SetBackgroundColor(const Vector4& color);
222
223   /**
224    * @brief Gets the background color of the render surface.
225    * @return The background color
226    */
227   Vector4 GetBackgroundColor() const;
228
229   /**
230    * @brief Get the Scene scene graph object
231    *
232    * @return the Scene scene graph object
233    */
234   SceneGraph::Scene* GetSceneObject() const;
235
236   /**
237    * Notify the surface has been rotated.
238    * When the device is rotated or the rotation event is received by display manager,
239    * this function will be called by window implementation.
240    *
241    * @param[in] width The width of rotated surface
242    * @param[in] height The height of rotated surface
243    * @param[in] orientation The orientation of rotated surface
244    */
245   void SurfaceRotated(float width, float height, int orientation);
246
247   /**
248    * @brief Get surface's current orientation
249    *
250    * @return surface orientation
251    */
252   int GetSurfaceOrientation();
253
254   /**
255    * @copydoc Dali::Integration::Scene::IsSurfaceRectChanged
256    */
257   bool IsSurfaceRectChanged() const;
258
259   /**
260    * Used by the EventProcessor to emit key event signals.
261    * @param[in] event The key event.
262    */
263   void EmitKeyEventSignal(const Dali::KeyEvent& event);
264
265   /**
266    * Used by the KeyEventProcessor to emit KeyEventGenerated signals.
267    * @param[in] event The key event.
268    * @return The return is true if KeyEvent is consumed, otherwise false.
269    */
270   bool EmitKeyEventGeneratedSignal(const Dali::KeyEvent& event);
271
272   /**
273    * Emits the event processing finished signal.
274    *
275    * @see Dali::Scene::SignalEventProcessingFinished()
276    */
277   void EmitEventProcessingFinishedSignal();
278
279   /**
280    * Emits the touched signal.
281    * @param[in] touch The touch event details.
282    */
283   void EmitTouchedSignal(const Dali::TouchEvent& touch);
284
285   /**
286    * Used by the EventProcessor to emit wheel event signals.
287    * @param[in] event The wheel event.
288    */
289   void EmitWheelEventSignal(const Dali::WheelEvent& event);
290
291   /**
292    * @copydoc Dali::Integration::Scene::AddFrameRenderedCallback
293    */
294   void AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId);
295
296   /**
297    * @copydoc Dali::Integration::Scene::AddFramePresentedCallback
298    */
299   void AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId);
300
301   /**
302    * @copydoc Dali::Integration::Scene::GetFrameRenderedCallback
303    */
304   void GetFrameRenderedCallback(Dali::Integration::Scene::FrameCallbackContainer& callbacks);
305
306   /**
307    * @copydoc Dali::Integration::Scene::GetFramePresentedCallback
308    */
309   void GetFramePresentedCallback(Dali::Integration::Scene::FrameCallbackContainer& callbacks);
310
311   /**
312    * @copydoc Integration::Scene::KeyEventSignal()
313    */
314   Integration::Scene::KeyEventSignalType& KeyEventSignal();
315
316   /**
317    * @copydoc Integration::Scene::KeyEventGeneratedSignal()
318    */
319   Integration::Scene::KeyEventGeneratedSignalType& KeyEventGeneratedSignal();
320
321   /**
322    * @copydoc Integration::Scene::SignalEventProcessingFinished()
323    */
324   Integration::Scene::EventProcessingFinishedSignalType& EventProcessingFinishedSignal();
325
326   /**
327     * @copydoc Integration::Scene::TouchedSignal()
328     */
329   Integration::Scene::TouchEventSignalType& TouchedSignal();
330
331   /**
332    * @copydoc Integration::Scene::sWheelEventSignal()
333    */
334   Integration::Scene::WheelEventSignalType& WheelEventSignal();
335
336   /**
337    * @brief Get ItemsDirtyRects
338    *
339    * @return the ItemsDirtyRects
340    */
341   std::vector<Dali::Internal::SceneGraph::DirtyRect>& GetItemsDirtyRects();
342
343 public:
344   /**
345    * From RenderTaskDefaults; retrieve the default root actor.
346    * @return The default root actor.
347    */
348   Actor& GetDefaultRootActor() override;
349
350   /**
351    * From RenderTaskDefaults; retrieve the default camera actor.
352    * @return The default camera actor.
353    */
354   CameraActor& GetDefaultCameraActor() override;
355
356 private:
357   // Constructor
358   Scene();
359
360   /**
361    * Second-phase constructor.
362    *
363    * @param[in] size The size of the set surface
364    * @param[in] orientation The orientation of the set surface for this scene
365    */
366   void Initialize(Size size, int orientation);
367
368   // Undefined
369   Scene(const Scene&) = delete;
370
371   // Undefined
372   Scene& operator=(const Scene& rhs) = delete;
373
374   /**
375    * Informs the scene that the set surface has been resized or rotated.
376    *
377    * @param[in] width The width of rotated surface
378    * @param[in] height The height of rotated surface
379    * @param[in] orientation The orientation of rotated surface
380    */
381   void ChangedSurface(float width, float height, int orientation);
382
383 private:
384   Internal::SceneGraph::Scene* mSceneObject;
385
386   Size mSize;
387
388   Vector2 mDpi;
389
390   Vector4 mBackgroundColor;
391
392   LayerPtr mRootLayer;
393
394   // Ordered list of currently on-stage layers
395   OwnerPointer<LayerList> mLayerList;
396
397   IntrusivePtr<CameraActor> mDefaultCamera;
398
399   // The list of render-tasks
400   IntrusivePtr<RenderTaskList> mRenderTaskList;
401
402   bool mDepthTreeDirty : 1; ///< True if the depth tree needs recalculating
403
404   EventProcessor mEventProcessor;
405
406   // The Surface's orientation
407   int mSurfaceOrientation;
408
409   // The key event signal
410   Integration::Scene::KeyEventSignalType          mKeyEventSignal;
411   Integration::Scene::KeyEventGeneratedSignalType mKeyEventGeneratedSignal;
412
413   // The event processing finished signal
414   Integration::Scene::EventProcessingFinishedSignalType mEventProcessingFinishedSignal;
415
416   // The touch signal
417   Integration::Scene::TouchEventSignalType mTouchedSignal;
418
419   // The wheel event signal
420   Integration::Scene::WheelEventSignalType mWheelEventSignal;
421
422   std::vector<Dali::Internal::SceneGraph::DirtyRect> mItemsDirtyRects;
423 };
424
425 } // namespace Internal
426
427 // Get impl of handle
428 inline Internal::Scene& GetImplementation(Dali::Integration::Scene& scene)
429 {
430   DALI_ASSERT_ALWAYS(scene && "Scene handle is empty");
431   Dali::RefObject& object = scene.GetBaseObject();
432   return static_cast<Internal::Scene&>(object);
433 }
434
435 inline const Internal::Scene& GetImplementation(const Dali::Integration::Scene& scene)
436 {
437   DALI_ASSERT_ALWAYS(scene && "Scene handle is empty");
438   const Dali::RefObject& object = scene.GetBaseObject();
439   return static_cast<const Internal::Scene&>(object);
440 }
441
442 } // namespace Dali
443
444 #endif // DALI_INTERNAL_SCENE_H