318ceebf2b760cafa5240f95213e0ae9f0a4183e
[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 is resized.
116    * @param[in] forceUpdate The flag to update force
117    */
118   void SurfaceResized( bool forceUpdate );
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 { return mIsBeingDeleted; }
158
159   /**
160    * @copydoc Dali::Integration::SceneHolder::FeedTouchPoint
161    */
162   void FeedTouchPoint( Dali::Integration::Point& point, int timeStamp );
163
164   /**
165    * @copydoc Dali::Integration::SceneHolder::FeedWheelEvent
166    */
167   void FeedWheelEvent( Dali::Integration::WheelEvent& wheelEvent );
168
169   /**
170    * @copydoc Dali::Integration::SceneHolder::FeedKeyEvent
171    */
172   void FeedKeyEvent( Dali::Integration::KeyEvent& keyEvent );
173
174   /**
175    * @copydoc Dali::Integration::SceneHolder::Get()
176    */
177   static Dali::Integration::SceneHolder Get( Dali::Actor actor );
178
179   /**
180    * @copydoc Dali::Integration::SceneHolder::KeyEventSignal()
181    */
182   Dali::Integration::SceneHolder::KeyEventSignalType& KeyEventSignal() { return mScene.KeyEventSignal(); }
183
184   /**
185    * @copydoc Dali::Integration::SceneHolder::KeyEventGeneratedSignal()
186    */
187   Dali::Integration::SceneHolder::KeyEventGeneratedSignalType& KeyEventGeneratedSignal() { return mScene.KeyEventGeneratedSignal(); }
188
189   /**
190    * @copydoc Dali::Integration::SceneHolder::TouchSignal()
191    */
192   Dali::Integration::SceneHolder::TouchSignalType& TouchSignal() { return mScene.TouchSignal(); }
193
194   /**
195    * @copydoc Dali::Integration::SceneHolder::WheelEventSignal()
196    */
197   Dali::Integration::SceneHolder::WheelEventSignalType& WheelEventSignal() { return mScene.WheelEventSignal(); }
198
199 public: // The following methods can be overridden if required
200
201   /**
202    * @brief Returns whether the Scene is visible or not.
203    * @return True if the Scene is visible, false otherwise.
204    */
205   virtual bool IsVisible() const;
206
207 public: // The following methods must be overridden
208
209   /**
210    * @copydoc Dali::Integration::SceneHolder::GetNativeHandle
211    */
212   virtual Dali::Any GetNativeHandle() const = 0;
213
214 protected:
215
216   // Constructor
217   SceneHolder();
218
219   // Undefined
220   SceneHolder(const SceneHolder&) = delete;
221
222   // Undefined
223   SceneHolder& operator=(const SceneHolder& rhs) = delete;
224
225   /**
226    * virtual destructor
227    */
228   virtual ~SceneHolder();
229
230 private: // The following methods can be overridden if required
231
232   /**
233    * @brief Called by the base class to inform deriving classes that the adaptor has been set.
234    * @param[in] adaptor The adaptor
235    */
236   virtual void OnAdaptorSet( Dali::Adaptor& adaptor ) {};
237
238   /**
239    * @brief Called by the base class to inform deriving classes that a new surface has been set.
240    * @param[in] surface The new render surface
241    */
242   virtual void OnSurfaceSet( Dali::RenderSurfaceInterface* surface ) {};
243
244   /**
245    * @brief Called by the base class to inform deriving classes that we are being paused.
246    */
247   virtual void OnPause() {};
248
249   /**
250    * @brief Called by the base class to inform deriving classes that we are resuming from a paused state.
251    */
252   virtual void OnResume() {};
253
254   /**
255    * Recalculate the touch position if required
256    * @param[in,out] point The touch point
257    */
258   virtual void RecalculateTouchPosition( Integration::Point& point ) {};
259
260 private:
261
262   /**
263    * Resets the event handling.
264    */
265   void Reset();
266
267 private:
268
269   static uint32_t                                 mSceneHolderCounter; ///< A counter to track the SceneHolder creation
270
271   class SceneHolderLifeCycleObserver;
272   std::unique_ptr< SceneHolderLifeCycleObserver > mLifeCycleObserver;  ///< The adaptor life cycle observer
273
274 protected:
275
276   uint32_t                                        mId;                 ///< A unique ID to identify the SceneHolder starting from 0
277   Dali::Integration::Scene                        mScene;              ///< The Scene
278   std::string                                     mName;               ///< The name of the SceneHolder
279
280   std::unique_ptr< Dali::RenderSurfaceInterface > mSurface;            ///< The window rendering surface
281   Adaptor*                                        mAdaptor;            ///< The adaptor
282
283   Dali::Integration::TouchEventCombiner           mCombiner;           ///< Combines multi-touch events.
284
285   std::atomic<bool>                               mIsBeingDeleted;     ///< This is set only from the event thread and read only from the render thread
286
287   bool                                            mAdaptorStarted:1;   ///< Whether the adaptor has started or not
288   bool                                            mVisible:1;          ///< Whether the scene is visible or not
289 };
290
291 } // Adaptor
292
293 } // Internal
294
295 // Get impl of handle
296 inline Internal::Adaptor::SceneHolder& GetImplementation( Dali::Integration::SceneHolder& sceneHolder )
297 {
298   DALI_ASSERT_ALWAYS( sceneHolder && "SceneHolder handle is empty" );
299   Dali::RefObject& object = sceneHolder.GetBaseObject();
300   return static_cast<Internal::Adaptor::SceneHolder&>( object );
301 }
302
303 inline const Internal::Adaptor::SceneHolder& GetImplementation( const Dali::Integration::SceneHolder& sceneHolder )
304 {
305   DALI_ASSERT_ALWAYS( sceneHolder && "SceneHolder handle is empty" );
306   const Dali::RefObject& object = sceneHolder.GetBaseObject();
307   return static_cast<const Internal::Adaptor::SceneHolder&>( object );
308 }
309
310 } // Dali
311
312 #endif // DALI_INTEGRATION_INTERNAL_SCENEHOLDER_H