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