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