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