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