Add GetLastHoverEvent
[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) 2023 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/hover-event-integ.h>
24 #include <dali/integration-api/events/key-event-integ.h>
25 #include <dali/integration-api/events/point.h>
26 #include <dali/integration-api/events/touch-event-combiner.h>
27 #include <dali/integration-api/scene.h>
28 #include <dali/public-api/common/intrusive-ptr.h>
29 #include <dali/public-api/events/hover-event.h>
30 #include <dali/public-api/events/touch-event.h>
31 #include <dali/public-api/math/uint-16-pair.h>
32 #include <dali/public-api/object/base-object.h>
33 #include <atomic>
34 #include <memory>
35 #include <vector>
36
37 // INTERNAL INCLUDES
38 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
39 #include <dali/integration-api/adaptor-framework/scene-holder.h>
40
41 namespace Dali
42 {
43 class Any;
44 class Adaptor;
45 class Actor;
46 class Layer;
47 class WheelEvent;
48 struct TouchPoint;
49 class KeyEvent;
50
51 namespace Integration
52 {
53 class Scene;
54 struct Point;
55 struct KeyEvent;
56 struct WheelEvent;
57
58 } // namespace Integration
59
60 namespace Internal
61 {
62 namespace Adaptor
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 public:
74   /**
75    * @copydoc Dali::Integration::SceneHolder::Add
76    */
77   void Add(Dali::Actor actor);
78
79   /**
80    * @copydoc Dali::Integration::SceneHolder::Remove
81    */
82   void Remove(Dali::Actor actor);
83
84   /**
85    * @copydoc Dali::Integration::SceneHolder::GetRootLayer
86    */
87   Dali::Layer GetRootLayer() const;
88
89   /**
90    * @copydoc Dali::Integration::SceneHolder::GetOverlayLayer
91    */
92   Dali::Layer GetOverlayLayer();
93
94   /**
95    * @brief Gets the window name.
96    * @return The name of the window
97    */
98   std::string GetName() const;
99
100   /**
101    * @brief Retrieve the unique ID of the window.
102    * @return The ID
103    */
104   uint32_t GetId() const;
105
106   /**
107    * @brief Retrieve the Scene.
108    * @return The Scene
109    */
110   Dali::Integration::Scene GetScene();
111
112   /**
113    * @brief Retrieves the DPI of this sceneholder.
114    * @return The DPI.
115    */
116   Uint16Pair GetDpi() const;
117
118   /**
119    * @brief Set the render surface
120    * @param[in] surface The render surface
121    */
122   void SetSurface(Dali::RenderSurfaceInterface* surface);
123
124   /**
125    * @brief Called when the surface set is resized.
126    * @param[in] width the resized window's width
127    * @param[in] height the resized window's height
128    */
129   void SurfaceResized(float width, float height);
130
131   /**
132    * @brief Get the render surface
133    * @return The render surface
134    */
135   Dali::RenderSurfaceInterface* GetSurface() const;
136
137   /**
138    * @brief Set the adaptor to the scene holder
139    * @param[in] adaptor An initialized adaptor
140    */
141   void SetAdaptor(Dali::Adaptor& adaptor);
142
143   /**
144    * @copydoc Dali::Integration::SceneHolder::SetBackgroundColor
145    */
146   void SetBackgroundColor(const Dali::Vector4& color);
147
148   /**
149    * @copydoc Dali::Integration::SceneHolder::GetBackgroundColor
150    */
151   Vector4 GetBackgroundColor() const;
152
153   /**
154    * @brief Pause the rendering of the scene holder.
155    */
156   void Pause();
157
158   /**
159    * @brief Resume the rendering of the scene holder (from pause).
160    */
161   void Resume();
162
163   /**
164    * @brief Checks whether this scene holder is being deleted in the event thread.
165    *
166    * @return true if this scene holder is being deleted in the event thread, or false if not.
167    */
168   bool IsBeingDeleted() const
169   {
170     return mIsBeingDeleted;
171   }
172
173   /**
174    * @brief Informs the scene that the set surface has been rotated.
175    *
176    * @param[in] width The width of rotated surface
177    * @param[in] height The height of rotated surface
178    * @param[in] windowOrientation the current window orientation
179    * @param[in] screenOrientation the current screen orientation
180    */
181   void SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation);
182
183   /**
184    * @brief Send message to acknowledge window rotation with current window orientation.
185    * It is to send message to render thread for completing window rotation by user.
186    */
187   void SetRotationCompletedAcknowledgement();
188
189   /**
190    * @copydoc Dali::Integration::SceneHolder::FeedTouchPoint
191    */
192   void FeedTouchPoint(Dali::Integration::Point& point, int timeStamp);
193
194   /**
195    * @brief Get the Last Touch Event
196    *
197    * @return Dali::TouchEvent
198    */
199   const Dali::TouchEvent& GetLastTouchEvent() const;
200
201   /**
202    * @brief Get the Last Hover Event
203    *
204    * @return Dali::HoverEvent
205    */
206   const Dali::HoverEvent& GetLastHoverEvent() const;
207
208   /**
209    * @copydoc Dali::Integration::SceneHolder::FeedWheelEvent
210    */
211   void FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent);
212
213   /**
214    * @copydoc Dali::Integration::SceneHolder::FeedKeyEvent
215    */
216   void FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent);
217
218   /**
219    * @copydoc Dali::Integration::SceneHolder::FeedHoverEvent
220    */
221   void FeedHoverEvent(Dali::Integration::Point& point);
222
223   /**
224    * @brief Adds a callback that is called when the frame rendering is done by the graphics driver.
225    *
226    * @param[in] callback The function to call
227    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
228    *
229    * @note A callback of the following type may be used:
230    * @code
231    *   void MyFunction( int frameId );
232    * @endcode
233    * This callback will be deleted once it is called.
234    *
235    * @note Ownership of the callback is passed onto this class.
236    */
237   void AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId);
238
239   /**
240    * @brief Adds a callback that is called when the frame rendering is done by the graphics driver.
241    *
242    * @param[in] callback The function to call
243    * @param[in] frameId The Id to specify the frame. It will be passed when the callback is called.
244    *
245    * @note A callback of the following type may be used:
246    * @code
247    *   void MyFunction( int frameId );
248    * @endcode
249    * This callback will be deleted once it is called.
250    *
251    * @note Ownership of the callback is passed onto this class.
252    */
253   void AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId);
254
255   /**
256    * @brief Creates the render target for the surface when the surface is created/resized/replaced.
257    */
258   void CreateRenderTarget();
259
260   /**
261    * @copydoc Dali::Integration::SceneHolder::GetRenderTaskList()
262    */
263   Dali::RenderTaskList GetRenderTaskList() const;
264
265   /**
266    * @copydoc Dali::Integration::SceneHolder::Get()
267    */
268   static Dali::Integration::SceneHolder Get(Dali::Actor actor);
269
270   /**
271    * @copydoc Dali::Integration::SceneHolder::KeyEventSignal()
272    */
273   Dali::Integration::SceneHolder::KeyEventSignalType& KeyEventSignal()
274   {
275     return mScene.KeyEventSignal();
276   }
277
278   /**
279    * @copydoc Dali::Integration::SceneHolder::KeyEventGeneratedSignal()
280    */
281   Dali::Integration::SceneHolder::KeyEventGeneratedSignalType& KeyEventGeneratedSignal()
282   {
283     return mScene.KeyEventGeneratedSignal();
284   }
285
286   /**
287    * @copydoc Dali::Integration::SceneHolder::InterceptKeyEventSignal()
288    */
289   Dali::Integration::SceneHolder::KeyEventGeneratedSignalType& InterceptKeyEventSignal()
290   {
291     return mScene.InterceptKeyEventSignal();
292   }
293
294   /**
295    * @copydoc Dali::Integration::SceneHolder::TouchedSignal()
296    */
297   Dali::Integration::SceneHolder::TouchEventSignalType& TouchedSignal()
298   {
299     return mScene.TouchedSignal();
300   }
301
302   /**
303    * @copydoc Dali::Integration::SceneHolder::WheelEventSignal()
304    */
305   Dali::Integration::SceneHolder::WheelEventSignalType& WheelEventSignal()
306   {
307     return mScene.WheelEventSignal();
308   }
309
310   /**
311    * @copydoc Dali::Integration::SceneHolder::WheelEventGeneratedSignal()
312    */
313   Dali::Integration::SceneHolder::WheelEventGeneratedSignalType& WheelEventGeneratedSignal()
314   {
315     return mScene.WheelEventGeneratedSignal();
316   }
317
318 public: // The following methods can be overridden if required
319   /**
320    * @brief Returns whether the Scene is visible or not.
321    * @return True if the Scene is visible, false otherwise.
322    */
323   virtual bool IsVisible() const;
324
325 public: // The following methods must be overridden
326   /**
327    * @copydoc Dali::Integration::SceneHolder::GetNativeHandle
328    */
329   virtual Dali::Any GetNativeHandle() const = 0;
330
331 protected:
332   // Constructor
333   SceneHolder();
334
335   // Undefined
336   SceneHolder(const SceneHolder&) = delete;
337
338   // Undefined
339   SceneHolder& operator=(const SceneHolder& rhs) = delete;
340
341   /**
342    * virtual destructor
343    */
344   ~SceneHolder() override;
345
346 private: // The following methods can be overridden if required
347   /**
348    * @brief Called by the base class to inform deriving classes that the adaptor has been set.
349    * @param[in] adaptor The adaptor
350    */
351   virtual void OnAdaptorSet(Dali::Adaptor& adaptor){};
352
353   /**
354    * @brief Called by the base class to inform deriving classes that a new surface has been set.
355    * @param[in] surface The new render surface
356    */
357   virtual void OnSurfaceSet(Dali::RenderSurfaceInterface* surface){};
358
359   /**
360    * @brief Called by the base class to inform deriving classes that we are being paused.
361    */
362   virtual void OnPause(){};
363
364   /**
365    * @brief Called by the base class to inform deriving classes that we are resuming from a paused state.
366    */
367   virtual void OnResume(){};
368
369   /**
370    * Recalculate the position if required
371    * @param[in] position The screen position
372    * @return converted position by oriention
373    */
374   virtual Vector2 RecalculatePosition(const Vector2& position)
375   {
376     return position;
377   };
378
379 private:
380   /**
381    * Resets the event handling.
382    */
383   void Reset();
384
385   /**
386    * Initializes the DPI for this object.
387    */
388   void InitializeDpi();
389
390 private:
391   static uint32_t mSceneHolderCounter; ///< A counter to track the SceneHolder creation
392
393   class SceneHolderLifeCycleObserver;
394   std::unique_ptr<SceneHolderLifeCycleObserver> mLifeCycleObserver; ///< The adaptor life cycle observer
395   Dali::TouchEvent                              mLastTouchEvent;
396   Dali::HoverEvent                              mLastHoverEvent;
397
398 protected:
399   uint32_t                 mId;    ///< A unique ID to identify the SceneHolder starting from 0
400   Dali::Integration::Scene mScene; ///< The Scene
401   std::string              mName;  ///< The name of the SceneHolder
402
403   std::unique_ptr<Dali::RenderSurfaceInterface> mSurface; ///< The window rendering surface
404   Adaptor*                                      mAdaptor; ///< The adaptor
405
406   Dali::Integration::TouchEventCombiner mCombiner; ///< Combines multi-touch events.
407
408   Uint16Pair mDpi; ///< The DPI for this SceneHolder.
409
410   std::atomic<bool> mIsBeingDeleted; ///< This is set only from the event thread and read only from the render thread
411
412   bool mAdaptorStarted : 1; ///< Whether the adaptor has started or not
413   bool mVisible : 1;        ///< Whether the scene is visible or not
414 };
415
416 } // namespace Adaptor
417
418 } // namespace Internal
419
420 // Get impl of handle
421 inline Internal::Adaptor::SceneHolder& GetImplementation(Dali::Integration::SceneHolder& sceneHolder)
422 {
423   DALI_ASSERT_ALWAYS(sceneHolder && "SceneHolder handle is empty");
424   Dali::RefObject& object = sceneHolder.GetBaseObject();
425   return static_cast<Internal::Adaptor::SceneHolder&>(object);
426 }
427
428 inline const Internal::Adaptor::SceneHolder& GetImplementation(const Dali::Integration::SceneHolder& sceneHolder)
429 {
430   DALI_ASSERT_ALWAYS(sceneHolder && "SceneHolder handle is empty");
431   const Dali::RefObject& object = sceneHolder.GetBaseObject();
432   return static_cast<const Internal::Adaptor::SceneHolder&>(object);
433 }
434
435 } // namespace Dali
436
437 #endif // DALI_INTEGRATION_INTERNAL_SCENEHOLDER_H