[Tizen] Add screen and client rotation itself function
[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 is resized.
117    * @param[in] forceUpdate The flag to update force
118    */
119   void SurfaceResized( bool forceUpdate );
120
121   /**
122    * @brief Get the render surface
123    * @return The render surface
124    */
125   Dali::RenderSurfaceInterface* GetSurface() const;
126
127   /**
128    * @brief Set the adaptor to the scene holder
129    * @param[in] adaptor An initialized adaptor
130    */
131   void SetAdaptor(Dali::Adaptor& adaptor);
132
133   /**
134    * @copydoc Dali::Integration::SceneHolder::SetBackgroundColor
135    */
136   void SetBackgroundColor(const Dali::Vector4& color);
137
138   /**
139    * @copydoc Dali::Integration::SceneHolder::GetBackgroundColor
140    */
141   Vector4 GetBackgroundColor() const;
142
143   /**
144    * @brief Pause the rendering of the scene holder.
145    */
146   void Pause();
147
148   /**
149    * @brief Resume the rendering of the scene holder (from pause).
150    */
151   void Resume();
152
153   /**
154    * @brief Checks whether this scene holder is being deleted in the event thread.
155    *
156    * @return true if this scene holder is being deleted in the event thread, or false if not.
157    */
158   bool IsBeingDeleted() const
159   {
160     return mIsBeingDeleted;
161   }
162
163   /**
164    * @copydoc Dali::Integration::SceneHolder::FeedTouchPoint
165    */
166   void FeedTouchPoint(Dali::Integration::Point& point, int timeStamp);
167
168   /**
169    * @copydoc Dali::Integration::SceneHolder::FeedWheelEvent
170    */
171   void FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent);
172
173   /**
174    * @copydoc Dali::Integration::SceneHolder::FeedKeyEvent
175    */
176   void FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent);
177
178   /**
179    * @brief Adds a callback that is called when the frame rendering is done by the graphics driver.
180    *
181    * @param[in] callback The function to call
182    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
183    *
184    * @note A callback of the following type may be used:
185    * @code
186    *   void MyFunction( int frameId );
187    * @endcode
188    * This callback will be deleted once it is called.
189    *
190    * @note Ownership of the callback is passed onto this class.
191    */
192   void AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId);
193
194   /**
195    * @brief Adds a callback that is called when the frame rendering is done by the graphics driver.
196    *
197    * @param[in] callback The function to call
198    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
199    *
200    * @note A callback of the following type may be used:
201    * @code
202    *   void MyFunction( int frameId );
203    * @endcode
204    * This callback will be deleted once it is called.
205    *
206    * @note Ownership of the callback is passed onto this class.
207    */
208   void AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId);
209
210   /**
211    * @copydoc Dali::Integration::SceneHolder::Get()
212    */
213   static Dali::Integration::SceneHolder Get(Dali::Actor actor);
214
215   /**
216    * @copydoc Dali::Integration::SceneHolder::KeyEventSignal()
217    */
218   Dali::Integration::SceneHolder::KeyEventSignalType& KeyEventSignal()
219   {
220     return mScene.KeyEventSignal();
221   }
222
223   /**
224    * @copydoc Dali::Integration::SceneHolder::KeyEventGeneratedSignal()
225    */
226   Dali::Integration::SceneHolder::KeyEventGeneratedSignalType& KeyEventGeneratedSignal()
227   {
228     return mScene.KeyEventGeneratedSignal();
229   }
230
231   /**
232    * @copydoc Dali::Integration::SceneHolder::TouchedSignal()
233    */
234   Dali::Integration::SceneHolder::TouchEventSignalType& TouchedSignal()
235   {
236     return mScene.TouchedSignal();
237   }
238
239   /**
240    * @copydoc Dali::Integration::SceneHolder::WheelEventSignal()
241    */
242   Dali::Integration::SceneHolder::WheelEventSignalType& WheelEventSignal()
243   {
244     return mScene.WheelEventSignal();
245   }
246
247 public: // The following methods can be overridden if required
248   /**
249    * @brief Returns whether the Scene is visible or not.
250    * @return True if the Scene is visible, false otherwise.
251    */
252   virtual bool IsVisible() const;
253
254 public: // The following methods must be overridden
255   /**
256    * @copydoc Dali::Integration::SceneHolder::GetNativeHandle
257    */
258   virtual Dali::Any GetNativeHandle() const = 0;
259
260 protected:
261   // Constructor
262   SceneHolder();
263
264   // Undefined
265   SceneHolder(const SceneHolder&) = delete;
266
267   // Undefined
268   SceneHolder& operator=(const SceneHolder& rhs) = delete;
269
270   /**
271    * virtual destructor
272    */
273   ~SceneHolder() override;
274
275 private: // The following methods can be overridden if required
276   /**
277    * @brief Called by the base class to inform deriving classes that the adaptor has been set.
278    * @param[in] adaptor The adaptor
279    */
280   virtual void OnAdaptorSet(Dali::Adaptor& adaptor){};
281
282   /**
283    * @brief Called by the base class to inform deriving classes that a new surface has been set.
284    * @param[in] surface The new render surface
285    */
286   virtual void OnSurfaceSet(Dali::RenderSurfaceInterface* surface){};
287
288   /**
289    * @brief Called by the base class to inform deriving classes that we are being paused.
290    */
291   virtual void OnPause(){};
292
293   /**
294    * @brief Called by the base class to inform deriving classes that we are resuming from a paused state.
295    */
296   virtual void OnResume(){};
297
298   /**
299    * Recalculate the touch position if required
300    * @param[in,out] point The touch point
301    */
302   virtual void RecalculateTouchPosition(Integration::Point& point){};
303
304 private:
305   /**
306    * Resets the event handling.
307    */
308   void Reset();
309
310   /**
311    * Initializes the DPI for this object.
312    */
313   void InitializeDpi();
314
315 private:
316   static uint32_t mSceneHolderCounter; ///< A counter to track the SceneHolder creation
317
318   class SceneHolderLifeCycleObserver;
319   std::unique_ptr<SceneHolderLifeCycleObserver> mLifeCycleObserver; ///< The adaptor life cycle observer
320
321 protected:
322   uint32_t                 mId;    ///< A unique ID to identify the SceneHolder starting from 0
323   Dali::Integration::Scene mScene; ///< The Scene
324   std::string              mName;  ///< The name of the SceneHolder
325
326   std::unique_ptr<Dali::RenderSurfaceInterface> mSurface; ///< The window rendering surface
327   Adaptor*                                      mAdaptor; ///< The adaptor
328
329   Dali::Integration::TouchEventCombiner mCombiner; ///< Combines multi-touch events.
330
331   Uint16Pair mDpi; ///< The DPI for this SceneHolder.
332
333   std::atomic<bool> mIsBeingDeleted; ///< This is set only from the event thread and read only from the render thread
334
335   bool mAdaptorStarted : 1; ///< Whether the adaptor has started or not
336   bool mVisible : 1;        ///< Whether the scene is visible or not
337 };
338
339 } // namespace Adaptor
340
341 } // namespace Internal
342
343 // Get impl of handle
344 inline Internal::Adaptor::SceneHolder& GetImplementation(Dali::Integration::SceneHolder& sceneHolder)
345 {
346   DALI_ASSERT_ALWAYS(sceneHolder && "SceneHolder handle is empty");
347   Dali::RefObject& object = sceneHolder.GetBaseObject();
348   return static_cast<Internal::Adaptor::SceneHolder&>(object);
349 }
350
351 inline const Internal::Adaptor::SceneHolder& GetImplementation(const Dali::Integration::SceneHolder& sceneHolder)
352 {
353   DALI_ASSERT_ALWAYS(sceneHolder && "SceneHolder handle is empty");
354   const Dali::RefObject& object = sceneHolder.GetBaseObject();
355   return static_cast<const Internal::Adaptor::SceneHolder&>(object);
356 }
357
358 } // namespace Dali
359
360 #endif // DALI_INTEGRATION_INTERNAL_SCENEHOLDER_H