e1c2409495e3f13f6b6fd12d488f0b816feede84
[platform/core/uifw/dali-core.git] / dali / internal / event / common / stage-impl.h
1 #ifndef DALI_INTERNAL_STAGE_H
2 #define DALI_INTERNAL_STAGE_H
3
4 /*
5  * Copyright (c) 2018 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 // INTERNAL INCLUDES
22 #include <dali/public-api/object/ref-object.h>
23 #include <dali/public-api/common/stage.h>
24 #include <dali/devel-api/common/stage-devel.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/integration-api/context-notifier.h>
27 #include <dali/internal/common/owner-pointer.h>
28 #include <dali/internal/event/actors/layer-impl.h>
29 #include <dali/internal/event/common/event-thread-services.h>
30 #include <dali/internal/event/common/object-registry-impl.h>
31 #include <dali/internal/event/common/stage-def.h>
32 #include <dali/internal/event/render-tasks/render-task-defaults.h>
33 #include <dali/internal/update/manager/update-manager.h>
34 #include <dali/public-api/common/view-mode.h>
35 #include <dali/public-api/math/vector2.h>
36 #include <dali/public-api/math/vector3.h>
37 #include <dali/public-api/math/vector4.h>
38 #include <dali/public-api/render-tasks/render-task.h>
39
40 namespace Dali
41 {
42
43 struct Vector2;
44
45 namespace Integration
46 {
47 class SystemOverlay;
48 class RenderController;
49 }
50
51 namespace Internal
52 {
53
54 namespace SceneGraph
55 {
56 class UpdateManager;
57 }
58
59 class AnimationPlaylist;
60 class PropertyNotificationManager;
61 class Layer;
62 class LayerList;
63 class SystemOverlay;
64 class CameraActor;
65 class RenderTaskList;
66
67 /**
68  * Implementation of Stage
69  */
70 class Stage : public BaseObject, public RenderTaskDefaults, public Integration::ContextNotifierInterface, public EventThreadServices
71 {
72 public:
73
74   /**
75    * Create the stage
76    * @param[in] playlist for animations
77    * @param[in] propertyNotificationManager
78    * @param[in] updateManager
79    * @param[in] notificationManager
80    * @param[in] renderController
81    */
82   static StagePtr New( AnimationPlaylist& playlist,
83                        PropertyNotificationManager& propertyNotificationManager,
84                        SceneGraph::UpdateManager& updateManager,
85                        NotificationManager& notificationManager,
86                        Integration::RenderController& renderController );
87
88   /**
89    * Initialize the stage.
90    * @param[in] renderToFbo Whether to render into a Frame Buffer Object.
91    */
92   void Initialize( bool renderToFbo );
93
94   /**
95    * Uninitialize the stage.
96    */
97   void Uninitialize();
98
99   /**
100    * @copydoc Dali::Stage::GetCurrent()
101    * @note this version is for internal usage so it does not assert
102    */
103   static StagePtr GetCurrent();
104
105   /**
106    * @copydoc Dali::Stage::IsInstalled().
107    */
108   static bool IsInstalled();
109
110   /**
111    * @copydoc Dali::Stage::GetObjectRegistry()
112    */
113   ObjectRegistry& GetObjectRegistry();
114
115   /**
116    * Retrieve the root actor (not publically accessible).
117    * @return The root actor.
118    */
119   Layer& GetRootActor();
120
121   /**
122    * Returns the animation playlist.
123    * @return reference to the animation playlist.
124    */
125   AnimationPlaylist& GetAnimationPlaylist();
126
127   /**
128    * Returns the property notification manager.
129    * @return reference to the property notification manager.
130    */
131   PropertyNotificationManager& GetPropertyNotificationManager();
132
133   // Root actor accessors
134
135   /**
136    * @copydoc Dali::Stage::Add()
137    */
138   void Add( Actor& actor );
139
140   /**
141    * @copydoc Dali::Stage::Remove()
142    */
143   void Remove( Actor& actor );
144
145   /**
146    * Used to calculate the size of the stage and indirectly, the root actor.
147    * @param [in] width  The new surface width.
148    * @param [in] height The new surface height.
149    */
150   void SurfaceResized( float width, float height );
151
152   /**
153    * Used to calculate the size and orientation of the stage and indirectly, the root actor.
154    * @param [in] width  The new surface width.
155    * @param [in] height The new surface height.
156    * @param [in] orientation The new surface orientation.
157    */
158   void SurfaceResized( float width, float height, int orientation );
159
160   /**
161    * Sets the top margin size.
162    * Available stage size is reduced by this size.
163    * The stage is located below the size at the top of the display
164    * initial size is zero before it is assigned
165    * @param[in] margin margin size
166    */
167   void SetTopMargin( unsigned int margin );
168
169   /**
170    * Returns the size of the Stage in pixels as a Vector.
171    * The x component will be the width of the Stage in pixels
172    * The y component will be the height of the Stage in pixels
173    * @return The size of the Stage as a Vector.
174    */
175   Vector2 GetSize() const;
176
177   /**
178    * @copydoc Dali::Stage::GetRenderTaskList()
179    */
180   RenderTaskList& GetRenderTaskList() const;
181
182   /**
183    * Create a default camera actor
184    */
185   void CreateDefaultCameraActor();
186
187   /**
188    * Set position of default camera for current stage size
189    */
190   void SetDefaultCameraPosition();
191
192   /**
193    * From RenderTaskDefaults; retrieve the default root actor.
194    * @return The default root actor.
195    */
196   virtual Actor& GetDefaultRootActor();
197
198   /**
199    * From RenderTaskDefaults; retrieve the default camera actor.
200    * @return The default camera actor.
201    */
202   virtual CameraActor& GetDefaultCameraActor();
203
204   // Layers
205
206   /**
207    * @copydoc Dali::Stage::GetLayerCount()
208    */
209   unsigned int GetLayerCount() const;
210
211   /**
212    * @copydoc Dali::Stage::GetLayer()
213    */
214   Dali::Layer GetLayer( unsigned int depth ) const;
215
216   /**
217    * @copydoc Dali::Stage::GetRootLayer()
218    */
219   Dali::Layer GetRootLayer() const;
220
221   /**
222    * Retrieve the ordered list of on-stage layers.
223    * @return The layer-list.
224    */
225   LayerList& GetLayerList();
226
227   // System-level overlay actors
228
229   /**
230    * @copydoc Dali::Integration::Core::GetSystemOverlay()
231    */
232   Integration::SystemOverlay& GetSystemOverlay();
233
234   /**
235    * Retrieve the internal implementation of the SystemOverlay.
236    * @return The implementation, or NULL if this has never been requested from Integration API.
237    */
238   SystemOverlay* GetSystemOverlayInternal();
239
240   // Stereoscopy
241
242   /**
243    * @copydoc Dali::Integration::Core::SetViewMode()
244    */
245   void SetViewMode( ViewMode viewMode );
246
247   /**
248    * @copydoc Dali::Integration::Core::GetViewMode()
249    */
250   ViewMode GetViewMode() const;
251
252   /**
253    * @copydoc Dali::Integration::Core::SetStereoBase()
254    */
255   void SetStereoBase( float stereoBase );
256
257   /**
258    * @copydoc Dali::Integration::Core::GetStereoBase()
259    */
260   float GetStereoBase() const;
261
262   // Keyboard stuff
263
264   /**
265    * As one virtual keyboard per stage, the stage will hold a pointer to the Actor currently
266    * set to receive keyboard input.
267    * @param[in] actor to receive keyboard input
268    */
269   void SetKeyboardFocusActor( Actor* actor );
270
271   /**
272    * Get the actor that is currently set to receive keyboard inputs
273    * @return Pointer to the actor set to receive keyboard inputs.
274    */
275   Actor* GetKeyboardFocusActor() const;
276
277   /**
278    * Removes the given actor from keyboard focus so it will no longer receive key events from keyboard.
279    * @param [in] actor which should be removed from focus.
280    */
281   void RemoveActorFromKeyFocus( Actor* actor );
282
283   // Misc
284
285   /**
286    * @copydoc Dali::Stage::SetBackgroundColor
287    */
288   void SetBackgroundColor(Vector4 color);
289
290   /**
291    * @copydoc Dali::Stage::GetBackgroundColor
292    */
293   Vector4 GetBackgroundColor() const;
294
295   /**
296    * @copydoc Dali::Stage::GetDpi
297    */
298   Vector2 GetDpi() const;
299
300   /**
301    * Sets horizontal and vertical pixels per inch value that is used by the display
302    * @param[in] dpi Horizontal and vertical dpi value
303    */
304   void SetDpi( Vector2 dpi );
305
306   NotificationManager& GetNotificationManager()
307   {
308     return mNotificationManager;
309   }
310
311   /**
312    * @copydoc Dali::Stage::KeepRendering()
313    */
314   void KeepRendering( float durationSeconds );
315
316   /**
317    * @copydoc Dali::DevelStage::SetRenderingBehavior()
318    */
319   void SetRenderingBehavior( DevelStage::Rendering renderingBehavior );
320
321   /**
322    * @copydoc Dali::DevelStage::GetRenderingBehavior()
323    */
324   DevelStage::Rendering GetRenderingBehavior() const;
325
326   /**
327    * Used by the EventProcessor to emit key event signals.
328    * @param[in] event The key event.
329    */
330   void EmitKeyEventSignal(const KeyEvent& event);
331
332   /**
333    * Used by the KeyEventProcessor to emit KeyEventGenerated signals.
334    * @param[in] event The key event.
335    * @return The return is true if KeyEvent is consumed, otherwise false.
336    */
337   bool EmitKeyEventGeneratedSignal(const KeyEvent& event);
338
339   /**
340    * Emits the event processing finished signal.
341    *
342    * @see Dali::Stage::SignalEventProcessingFinished()
343    */
344   void EmitEventProcessingFinishedSignal();
345
346   /**
347    * Emits the touched signal.
348    * @param[in] touchEvent The touch event details (Old API).
349    * @param[in] touch The touch event details.
350    */
351   void EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch );
352
353   /**
354    * Used by the EventProcessor to emit wheel event signals.
355    * @param[in] event The wheel event.
356    */
357   void EmitWheelEventSignal( const WheelEvent& event );
358
359   /**
360    * Emits the scene created.
361    */
362   void EmitSceneCreatedSignal();
363
364   /**
365    * @copydoc Dali::Stage::KeyEventSignal()
366    */
367   Dali::Stage::KeyEventSignalType& KeyEventSignal();
368
369   /**
370    * @copydoc Dali::Stage::SignalEventProcessingFinished()
371    */
372   Dali::Stage::EventProcessingFinishedSignalType& EventProcessingFinishedSignal();
373
374   /**
375     * @copydoc Dali::Stage::TouchedSignal()
376     */
377   Dali::Stage::TouchedSignalType& TouchedSignal();
378
379   /**
380     * @copydoc Dali::Stage::TouchSignal()
381     */
382   Dali::Stage::TouchSignalType& TouchSignal();
383
384   /**
385    * @copydoc Dali::Stage::WheelEventSignal()
386    */
387   Dali::Stage::WheelEventSignalType& WheelEventSignal();
388
389   /**
390    * @copydoc Dali::Stage::ContextLostSignal()
391    */
392   Dali::Stage::ContextStatusSignal& ContextLostSignal();
393
394   /**
395    * @copydoc Dali::Stage::ContextRegainedSignal()
396    */
397   Dali::Stage::ContextStatusSignal& ContextRegainedSignal();
398
399   /**
400    * @copydoc Dali::Stage::SceneCreatedSignal()
401    */
402   Dali::Stage::SceneCreatedSignalType& SceneCreatedSignal();
403
404   /**
405    * @copydoc Dali::DevelStage::KeyEventGeneratedSignal()
406    */
407   Dali::DevelStage::KeyEventGeneratedSignalType& KeyEventGeneratedSignal();
408
409   /**
410    * Connects a callback function with the object's signals.
411    * @param[in] object The object providing the signal.
412    * @param[in] tracker Used to disconnect the signal.
413    * @param[in] signalName The signal to connect to.
414    * @param[in] functor A newly allocated FunctorDelegate.
415    * @return True if the signal was connected.
416    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
417    */
418   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
419
420 private: // Implementation of ContextNotificationInterface:
421
422   /**
423    * @copydoc Dali::Integration::NotifyContextLost();
424    */
425   virtual void NotifyContextLost();
426
427   /**
428    * @copydoc Dali::Integration::NotifyContextRegained();
429    */
430   virtual void NotifyContextRegained();
431
432 public: // Implementation of EventThreadServices
433
434   /**
435    * @copydoc EventThreadServices::RegisterObject
436    */
437   virtual void RegisterObject( BaseObject* object);
438
439   /**
440    * @copydoc EventThreadServices::UnregisterObject
441    */
442   virtual void UnregisterObject( BaseObject* object);
443
444   /**
445    * @copydoc EventThreadServices::GetUpdateManager
446    */
447   virtual SceneGraph::UpdateManager& GetUpdateManager();
448
449   /**
450    * @copydoc EventThreadServices::GetRenderController
451    */
452   virtual Integration::RenderController& GetRenderController();
453
454   /**
455    * @copydoc EventThreadServices::ReserveMessageSlot
456    */
457   virtual unsigned int* ReserveMessageSlot( std::size_t size, bool updateScene );
458
459   /**
460    * @copydoc EventThreadServices::GetEventBufferIndex
461    */
462   virtual BufferIndex GetEventBufferIndex() const;
463
464   /**
465    * @copydoc EventThreadServices::ForceNextUpdate
466    */
467   virtual void ForceNextUpdate();
468
469   /**
470    * @copydoc EventThreadServices::IsNextUpdateForced
471    */
472   virtual bool IsNextUpdateForced();
473
474   /**
475    * Request that the depth tree is rebuilt
476    */
477   void RequestRebuildDepthTree();
478
479   /**
480    * Rebuilds the depth tree at the end of the event frame if
481    * it was requested this frame.
482    */
483   void RebuildDepthTree();
484
485 private:
486
487   /**
488    * Protected constructor; see also Stage::New()
489    */
490   Stage( AnimationPlaylist& playlist,
491          PropertyNotificationManager& propertyNotificationManager,
492          SceneGraph::UpdateManager& updateManager,
493          NotificationManager& notificationManager,
494          Integration::RenderController& renderController );
495
496   /**
497    * A reference counted object may only be deleted by calling Unreference()
498    */
499   virtual ~Stage();
500
501 private:
502
503   // For 'Fire and forget' animation support
504   AnimationPlaylist& mAnimationPlaylist;
505
506   PropertyNotificationManager& mPropertyNotificationManager;
507
508   SceneGraph::UpdateManager& mUpdateManager;
509
510   NotificationManager& mNotificationManager;
511
512   Integration::RenderController& mRenderController;
513
514   // The stage-size may be less than surface-size (reduced by top-margin)
515   Vector2 mSize;
516   Vector2 mSurfaceSize;
517
518   // Cached for public GetBackgroundColor()
519   Vector4 mBackgroundColor;
520
521   LayerPtr mRootLayer;
522
523   // Ordered list of currently on-stage layers
524   OwnerPointer<LayerList> mLayerList;
525
526   IntrusivePtr<CameraActor> mDefaultCamera;
527
528   ViewMode mViewMode;
529   float mStereoBase;
530
531   unsigned int mTopMargin;
532   Vector2 mDpi;
533
534   // The object registry
535   ObjectRegistryPtr mObjectRegistry;
536
537   // The list of render-tasks
538   IntrusivePtr<RenderTaskList> mRenderTaskList;
539
540   Dali::RenderTask mRightRenderTask;
541   IntrusivePtr<CameraActor> mRightCamera;
542   Dali::RenderTask mLeftRenderTask;
543   IntrusivePtr<CameraActor> mLeftCamera;
544
545   Integration::SystemOverlay* mSystemOverlay; ///< SystemOverlay stage access
546
547   // The key event signal
548   Dali::Stage::KeyEventSignalType                 mKeyEventSignal;
549   Dali::DevelStage::KeyEventGeneratedSignalType   mKeyEventGeneratedSignal;
550
551   // The event processing finished signal
552   Dali::Stage::EventProcessingFinishedSignalType  mEventProcessingFinishedSignal;
553
554   // The touched signals
555   Dali::Stage::TouchedSignalType                  mTouchedSignal;
556   Dali::Stage::TouchSignalType                    mTouchSignal;
557
558   // The wheel event signal
559   Dali::Stage::WheelEventSignalType               mWheelEventSignal;
560
561   Dali::Stage::ContextStatusSignal mContextLostSignal;
562   Dali::Stage::ContextStatusSignal mContextRegainedSignal;
563
564   Dali::Stage::SceneCreatedSignalType mSceneCreatedSignal;
565
566   DevelStage::Rendering mRenderingBehavior; ///< The rendering behavior
567
568   bool mDepthTreeDirty:1;  ///< True if the depth tree needs recalculating
569   bool mForceNextUpdate:1; ///< True if the next rendering is really required.
570   bool mRenderToFbo:1;     ///< Whether to render to a Frame Buffer Object.
571
572   bool mNeedToRotation:1;
573   int mCurrentOrientation;
574 };
575
576 } // namespace Internal
577
578 // Helpers for public-api forwarding methods
579
580 inline Internal::Stage& GetImplementation(Dali::Stage& stage)
581 {
582   DALI_ASSERT_ALWAYS( stage && "Stage handle is empty" );
583
584   BaseObject& handle = stage.GetBaseObject();
585
586   return static_cast<Internal::Stage&>(handle);
587 }
588
589 inline const Internal::Stage& GetImplementation(const Dali::Stage& stage)
590 {
591   DALI_ASSERT_ALWAYS( stage && "Stage handle is empty" );
592
593   const BaseObject& handle = stage.GetBaseObject();
594
595   return static_cast<const Internal::Stage&>(handle);
596 }
597
598 } // namespace Dali
599
600 #endif // DALI_INTERNAL_STAGE_H