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