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