[dali_2.3.23] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / integration-api / adaptor-framework / scene-holder-impl.h
1 #ifndef DALI_INTEGRATION_INTERNAL_SCENEHOLDER_H
2 #define DALI_INTEGRATION_INTERNAL_SCENEHOLDER_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 // EXTERNAL INCLUDES
22 #include <dali/graphics-api/graphics-controller.h>
23 #include <dali/integration-api/events/hover-event-integ.h>
24 #include <dali/integration-api/events/key-event-integ.h>
25 #include <dali/integration-api/events/point.h>
26 #include <dali/integration-api/events/touch-event-combiner.h>
27 #include <dali/integration-api/scene.h>
28 #include <dali/public-api/common/intrusive-ptr.h>
29 #include <dali/public-api/events/hover-event.h>
30 #include <dali/public-api/events/touch-event.h>
31 #include <dali/public-api/math/uint-16-pair.h>
32 #include <dali/public-api/object/base-object.h>
33 #include <atomic>
34 #include <memory>
35 #include <vector>
36
37 // INTERNAL INCLUDES
38 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
39 #include <dali/integration-api/adaptor-framework/scene-holder.h>
40
41 namespace Dali
42 {
43 class Any;
44 class Adaptor;
45 class Actor;
46 class Layer;
47 class WheelEvent;
48 struct TouchPoint;
49 class KeyEvent;
50
51 namespace Integration
52 {
53 class Scene;
54 struct Point;
55 struct KeyEvent;
56 struct WheelEvent;
57
58 } // namespace Integration
59
60 namespace Internal
61 {
62 namespace Adaptor
63 {
64 class Adaptor;
65 class SceneHolder;
66 using SceneHolderPtr = IntrusivePtr<SceneHolder>;
67
68 /**
69  * @brief SceneHolder creates a Scene for rendering.
70  */
71 class DALI_ADAPTOR_API SceneHolder : public BaseObject
72 {
73 public:
74   /**
75    * @copydoc Dali::Integration::SceneHolder::Add
76    */
77   void Add(Dali::Actor actor);
78
79   /**
80    * @copydoc Dali::Integration::SceneHolder::Remove
81    */
82   void Remove(Dali::Actor actor);
83
84   /**
85    * @copydoc Dali::Integration::SceneHolder::GetRootLayer
86    */
87   Dali::Layer GetRootLayer() const;
88
89   /**
90    * @copydoc Dali::Integration::SceneHolder::GetOverlayLayer
91    */
92   Dali::Layer GetOverlayLayer();
93
94   /**
95    * @brief Gets the window name.
96    * @return The name of the window
97    */
98   std::string GetName() const;
99
100   /**
101    * @brief Retrieve the unique ID of the window.
102    * @return The ID
103    */
104   uint32_t GetId() const;
105
106   /**
107    * @brief Retrieve the Scene.
108    * @return The Scene
109    */
110   Dali::Integration::Scene GetScene();
111
112   /**
113    * @brief Retrieves the DPI of this sceneholder.
114    * @return The DPI.
115    */
116   Uint16Pair GetDpi() const;
117
118   /**
119    * @brief Set the render surface
120    * @param[in] surface The render surface
121    */
122   void SetSurface(Dali::RenderSurfaceInterface* surface);
123
124   /**
125    * @brief Called when the surface set is resized.
126    * @param[in] width the resized window's width
127    * @param[in] height the resized window's height
128    */
129   void SurfaceResized(float width, float height);
130
131   /**
132    * @brief Get the render surface
133    * @return The render surface
134    */
135   Dali::RenderSurfaceInterface* GetSurface() const;
136
137   /**
138    * @brief Set the adaptor to the scene holder
139    * @param[in] adaptor An initialized adaptor
140    */
141   void SetAdaptor(Dali::Adaptor& adaptor);
142
143   /**
144    * @copydoc Dali::Integration::SceneHolder::SetBackgroundColor
145    */
146   void SetBackgroundColor(const Dali::Vector4& color);
147
148   /**
149    * @copydoc Dali::Integration::SceneHolder::GetBackgroundColor
150    */
151   Vector4 GetBackgroundColor() const;
152
153   /**
154    * @brief Pause the rendering of the scene holder.
155    */
156   void Pause();
157
158   /**
159    * @brief Resume the rendering of the scene holder (from pause).
160    */
161   void Resume();
162
163   /**
164    * @brief Informs the scene that the set surface has been rotated.
165    *
166    * @param[in] width The width of rotated surface
167    * @param[in] height The height of rotated surface
168    * @param[in] windowOrientation the current window orientation
169    * @param[in] screenOrientation the current screen orientation
170    */
171   void SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation);
172
173   /**
174    * @brief Send message to acknowledge window rotation with current window orientation.
175    * It is to send message to render thread for completing window rotation by user.
176    */
177   void SetRotationCompletedAcknowledgement();
178
179   /**
180    * @copydoc Dali::Integration::SceneHolder::FeedTouchPoint
181    */
182   void FeedTouchPoint(Dali::Integration::Point& point, int timeStamp);
183
184   /**
185    * @brief Get the Last Touch Event
186    *
187    * @return Dali::TouchEvent
188    */
189   const Dali::TouchEvent& GetLastTouchEvent() const;
190
191   /**
192    * @brief Get the Last Hover Event
193    *
194    * @return Dali::HoverEvent
195    */
196   const Dali::HoverEvent& GetLastHoverEvent() const;
197
198   /**
199    * @copydoc Dali::Integration::SceneHolder::FeedWheelEvent
200    */
201   void FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent);
202
203   /**
204    * @copydoc Dali::Integration::SceneHolder::FeedKeyEvent
205    */
206   void FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent);
207
208   /**
209    * @copydoc Dali::Integration::SceneHolder::FeedHoverEvent
210    */
211   void FeedHoverEvent(Dali::Integration::Point& point);
212
213   /**
214    * @copydoc Dali::Integration::SceneHolder::SetGeometryHittestEnabled
215    */
216   void SetGeometryHittestEnabled(bool enabled);
217
218   /**
219    * @copydoc Dali::Integration::SceneHolder::IsGeometryHittestEnabled
220    */
221   bool IsGeometryHittestEnabled();
222
223   /**
224    * @copydoc Dali::Integration::SceneHolder::GetNativeId
225    */
226   int32_t GetNativeId() const;
227
228   /**
229    * @brief Adds a callback that is called when the frame rendering is done by the graphics driver.
230    *
231    * @param[in] callback The function to call
232    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
233    *
234    * @note A callback of the following type may be used:
235    * @code
236    *   void MyFunction( int frameId );
237    * @endcode
238    * This callback will be deleted once it is called.
239    *
240    * @note Ownership of the callback is passed onto this class.
241    */
242   void AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId);
243
244   /**
245    * @brief Adds a callback that is called when the frame rendering is done by the graphics driver.
246    *
247    * @param[in] callback The function to call
248    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
249    *
250    * @note A callback of the following type may be used:
251    * @code
252    *   void MyFunction( int frameId );
253    * @endcode
254    * This callback will be deleted once it is called.
255    *
256    * @note Ownership of the callback is passed onto this class.
257    */
258   void AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId);
259
260   /**
261    * @brief Creates the render target for the surface when the surface is created/resized/replaced.
262    */
263   void CreateRenderTarget();
264
265   /**
266    * @copydoc Dali::Integration::SceneHolder::GetRenderTaskList()
267    */
268   Dali::RenderTaskList GetRenderTaskList() const;
269
270   /**
271    * @copydoc Dali::Integration::SceneHolder::Get()
272    */
273   static Dali::Integration::SceneHolder Get(Dali::Actor actor);
274
275   /**
276    * @copydoc Dali::Integration::SceneHolder::KeyEventSignal()
277    */
278   Dali::Integration::SceneHolder::KeyEventSignalType& KeyEventSignal()
279   {
280     return mScene.KeyEventSignal();
281   }
282
283   /**
284    * @copydoc Dali::Integration::SceneHolder::KeyEventGeneratedSignal()
285    */
286   Dali::Integration::SceneHolder::KeyEventGeneratedSignalType& KeyEventGeneratedSignal()
287   {
288     return mScene.KeyEventGeneratedSignal();
289   }
290
291   /**
292    * @copydoc Dali::Integration::SceneHolder::InterceptKeyEventSignal()
293    */
294   Dali::Integration::SceneHolder::KeyEventGeneratedSignalType& InterceptKeyEventSignal()
295   {
296     return mScene.InterceptKeyEventSignal();
297   }
298
299   /**
300    * @copydoc Dali::Integration::SceneHolder::TouchedSignal()
301    */
302   Dali::Integration::SceneHolder::TouchEventSignalType& TouchedSignal()
303   {
304     return mScene.TouchedSignal();
305   }
306
307   /**
308    * @copydoc Dali::Integration::SceneHolder::WheelEventSignal()
309    */
310   Dali::Integration::SceneHolder::WheelEventSignalType& WheelEventSignal()
311   {
312     return mScene.WheelEventSignal();
313   }
314
315   /**
316    * @copydoc Dali::Integration::SceneHolder::WheelEventGeneratedSignal()
317    */
318   Dali::Integration::SceneHolder::WheelEventGeneratedSignalType& WheelEventGeneratedSignal()
319   {
320     return mScene.WheelEventGeneratedSignal();
321   }
322
323 public: // The following methods can be overridden if required
324   /**
325    * @brief Returns whether the Scene is visible or not.
326    * @return True if the Scene is visible, false otherwise.
327    */
328   virtual bool IsVisible() const;
329
330 public: // The following methods must be overridden
331   /**
332    * @copydoc Dali::Integration::SceneHolder::GetNativeHandle
333    */
334   virtual Dali::Any GetNativeHandle() const = 0;
335
336 protected:
337   // Constructor
338   SceneHolder();
339
340   // Undefined
341   SceneHolder(const SceneHolder&) = delete;
342
343   // Undefined
344   SceneHolder& operator=(const SceneHolder& rhs) = delete;
345
346   /**
347    * virtual destructor
348    */
349   ~SceneHolder() override;
350
351 private: // The following methods can be overridden if required
352   /**
353    * @brief Called by the base class to inform deriving classes that the adaptor has been set.
354    * @param[in] adaptor The adaptor
355    */
356   virtual void OnAdaptorSet(Dali::Adaptor& adaptor){};
357
358   /**
359    * @brief Called by the base class to inform deriving classes that a new surface has been set.
360    * @param[in] surface The new render surface
361    */
362   virtual void OnSurfaceSet(Dali::RenderSurfaceInterface* surface){};
363
364   /**
365    * @brief Called by the base class to inform deriving classes that we are being paused.
366    */
367   virtual void OnPause(){};
368
369   /**
370    * @brief Called by the base class to inform deriving classes that we are resuming from a paused state.
371    */
372   virtual void OnResume(){};
373
374   /**
375    * Recalculate the position if required
376    * @param[in] position The screen position
377    * @return converted position by oriention
378    */
379   virtual Vector2 RecalculatePosition(const Vector2& position)
380   {
381     return position;
382   };
383
384 private:
385   /**
386    * Resets the event handling.
387    */
388   void Reset();
389
390   /**
391    * Initializes the DPI for this object.
392    */
393   void InitializeDpi();
394
395 private:
396   static uint32_t mSceneHolderCounter; ///< A counter to track the SceneHolder creation
397
398   class SceneHolderLifeCycleObserver;
399   std::unique_ptr<SceneHolderLifeCycleObserver> mLifeCycleObserver; ///< The adaptor life cycle observer
400   Dali::TouchEvent                              mLastTouchEvent;
401   Dali::HoverEvent                              mLastHoverEvent;
402
403 protected:
404   uint32_t                 mId;    ///< A unique ID to identify the SceneHolder starting from 0
405   Dali::Integration::Scene mScene; ///< The Scene
406   std::string              mName;  ///< The name of the SceneHolder
407
408   std::unique_ptr<Dali::RenderSurfaceInterface> mSurface; ///< The window rendering surface
409   Adaptor*                                      mAdaptor; ///< The adaptor
410
411   Dali::Integration::TouchEventCombiner mCombiner; ///< Combines multi-touch events.
412
413   Uint16Pair mDpi; ///< The DPI for this SceneHolder.
414
415   bool mAdaptorStarted; ///< Whether the adaptor has started or not
416   bool mVisible : 1;    ///< Whether the scene is visible or not
417 };
418
419 } // namespace Adaptor
420
421 } // namespace Internal
422
423 // Get impl of handle
424 inline Internal::Adaptor::SceneHolder& GetImplementation(Dali::Integration::SceneHolder& sceneHolder)
425 {
426   DALI_ASSERT_ALWAYS(sceneHolder && "SceneHolder handle is empty");
427   Dali::RefObject& object = sceneHolder.GetBaseObject();
428   return static_cast<Internal::Adaptor::SceneHolder&>(object);
429 }
430
431 inline const Internal::Adaptor::SceneHolder& GetImplementation(const Dali::Integration::SceneHolder& sceneHolder)
432 {
433   DALI_ASSERT_ALWAYS(sceneHolder && "SceneHolder handle is empty");
434   const Dali::RefObject& object = sceneHolder.GetBaseObject();
435   return static_cast<const Internal::Adaptor::SceneHolder&>(object);
436 }
437
438 } // namespace Dali
439
440 #endif // DALI_INTEGRATION_INTERNAL_SCENEHOLDER_H