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