0f036e3cfe561be898740c06d3781959b2ba7ae1
[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    * Sets the top margin size.
154    * Available stage size is reduced by this size.
155    * The stage is located below the size at the top of the display
156    * initial size is zero before it is assigned
157    * @param[in] margin margin size
158    */
159   void SetTopMargin( unsigned int margin );
160
161   /**
162    * Returns the size of the Stage in pixels as a Vector.
163    * The x component will be the width of the Stage in pixels
164    * The y component will be the height of the Stage in pixels
165    * @return The size of the Stage as a Vector.
166    */
167   Vector2 GetSize() const;
168
169   /**
170    * @copydoc Dali::Stage::GetRenderTaskList()
171    */
172   RenderTaskList& GetRenderTaskList() const;
173
174   /**
175    * Create a default camera actor
176    */
177   void CreateDefaultCameraActor();
178
179   /**
180    * Set position of default camera for current stage size
181    */
182   void SetDefaultCameraPosition();
183
184   /**
185    * From RenderTaskDefaults; retrieve the default root actor.
186    * @return The default root actor.
187    */
188   virtual Actor& GetDefaultRootActor();
189
190   /**
191    * From RenderTaskDefaults; retrieve the default camera actor.
192    * @return The default camera actor.
193    */
194   virtual CameraActor& GetDefaultCameraActor();
195
196   // Layers
197
198   /**
199    * @copydoc Dali::Stage::GetLayerCount()
200    */
201   unsigned int GetLayerCount() const;
202
203   /**
204    * @copydoc Dali::Stage::GetLayer()
205    */
206   Dali::Layer GetLayer( unsigned int depth ) const;
207
208   /**
209    * @copydoc Dali::Stage::GetRootLayer()
210    */
211   Dali::Layer GetRootLayer() const;
212
213   /**
214    * Retrieve the ordered list of on-stage layers.
215    * @return The layer-list.
216    */
217   LayerList& GetLayerList();
218
219   // System-level overlay actors
220
221   /**
222    * @copydoc Dali::Integration::Core::GetSystemOverlay()
223    */
224   Integration::SystemOverlay& GetSystemOverlay();
225
226   /**
227    * Retrieve the internal implementation of the SystemOverlay.
228    * @return The implementation, or NULL if this has never been requested from Integration API.
229    */
230   SystemOverlay* GetSystemOverlayInternal();
231
232   // Stereoscopy
233
234   /**
235    * @copydoc Dali::Integration::Core::SetViewMode()
236    */
237   void SetViewMode( ViewMode viewMode );
238
239   /**
240    * @copydoc Dali::Integration::Core::GetViewMode()
241    */
242   ViewMode GetViewMode() const;
243
244   /**
245    * @copydoc Dali::Integration::Core::SetStereoBase()
246    */
247   void SetStereoBase( float stereoBase );
248
249   /**
250    * @copydoc Dali::Integration::Core::GetStereoBase()
251    */
252   float GetStereoBase() const;
253
254   // Keyboard stuff
255
256   /**
257    * As one virtual keyboard per stage, the stage will hold a pointer to the Actor currently
258    * set to receive keyboard input.
259    * @param[in] actor to receive keyboard input
260    */
261   void SetKeyboardFocusActor( Actor* actor );
262
263   /**
264    * Get the actor that is currently set to receive keyboard inputs
265    * @return Pointer to the actor set to receive keyboard inputs.
266    */
267   Actor* GetKeyboardFocusActor() const;
268
269   /**
270    * Removes the given actor from keyboard focus so it will no longer receive key events from keyboard.
271    * @param [in] actor which should be removed from focus.
272    */
273   void RemoveActorFromKeyFocus( Actor* actor );
274
275   // Misc
276
277   /**
278    * @copydoc Dali::Stage::SetBackgroundColor
279    */
280   void SetBackgroundColor(Vector4 color);
281
282   /**
283    * @copydoc Dali::Stage::GetBackgroundColor
284    */
285   Vector4 GetBackgroundColor() const;
286
287   /**
288    * @copydoc Dali::Stage::GetDpi
289    */
290   Vector2 GetDpi() const;
291
292   /**
293    * Sets horizontal and vertical pixels per inch value that is used by the display
294    * @param[in] dpi Horizontal and vertical dpi value
295    */
296   void SetDpi( Vector2 dpi );
297
298   NotificationManager& GetNotificationManager()
299   {
300     return mNotificationManager;
301   }
302
303   /**
304    * @copydoc Dali::Stage::KeepRendering()
305    */
306   void KeepRendering( float durationSeconds );
307
308   /**
309    * @copydoc Dali::DevelStage::SetRenderingBehavior()
310    */
311   void SetRenderingBehavior( DevelStage::Rendering renderingBehavior );
312
313   /**
314    * @copydoc Dali::DevelStage::GetRenderingBehavior()
315    */
316   DevelStage::Rendering GetRenderingBehavior() const;
317
318   /**
319    * Used by the EventProcessor to emit key event signals.
320    * @param[in] event The key event.
321    */
322   void EmitKeyEventSignal(const KeyEvent& event);
323
324   /**
325    * Used by the KeyEventProcessor to emit KeyEventGenerated signals.
326    * @param[in] event The key event.
327    * @return The return is true if KeyEvent is consumed, otherwise false.
328    */
329   bool EmitKeyEventGeneratedSignal(const KeyEvent& event);
330
331   /**
332    * Emits the event processing finished signal.
333    *
334    * @see Dali::Stage::SignalEventProcessingFinished()
335    */
336   void EmitEventProcessingFinishedSignal();
337
338   /**
339    * Emits the touched signal.
340    * @param[in] touchEvent The touch event details (Old API).
341    * @param[in] touch The touch event details.
342    */
343   void EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch );
344
345   /**
346    * Used by the EventProcessor to emit wheel event signals.
347    * @param[in] event The wheel event.
348    */
349   void EmitWheelEventSignal( const WheelEvent& event );
350
351   /**
352    * Emits the scene created.
353    */
354   void EmitSceneCreatedSignal();
355
356   /**
357    * @copydoc Dali::Stage::KeyEventSignal()
358    */
359   Dali::Stage::KeyEventSignalType& KeyEventSignal();
360
361   /**
362    * @copydoc Dali::Stage::SignalEventProcessingFinished()
363    */
364   Dali::Stage::EventProcessingFinishedSignalType& EventProcessingFinishedSignal();
365
366   /**
367     * @copydoc Dali::Stage::TouchedSignal()
368     */
369   Dali::Stage::TouchedSignalType& TouchedSignal();
370
371   /**
372     * @copydoc Dali::Stage::TouchSignal()
373     */
374   Dali::Stage::TouchSignalType& TouchSignal();
375
376   /**
377    * @copydoc Dali::Stage::WheelEventSignal()
378    */
379   Dali::Stage::WheelEventSignalType& WheelEventSignal();
380
381   /**
382    * @copydoc Dali::Stage::ContextLostSignal()
383    */
384   Dali::Stage::ContextStatusSignal& ContextLostSignal();
385
386   /**
387    * @copydoc Dali::Stage::ContextRegainedSignal()
388    */
389   Dali::Stage::ContextStatusSignal& ContextRegainedSignal();
390
391   /**
392    * @copydoc Dali::Stage::SceneCreatedSignal()
393    */
394   Dali::Stage::SceneCreatedSignalType& SceneCreatedSignal();
395
396   /**
397    * @copydoc Dali::DevelStage::KeyEventGeneratedSignal()
398    */
399   Dali::DevelStage::KeyEventGeneratedSignalType& KeyEventGeneratedSignal();
400
401   /**
402    * Connects a callback function with the object's signals.
403    * @param[in] object The object providing the signal.
404    * @param[in] tracker Used to disconnect the signal.
405    * @param[in] signalName The signal to connect to.
406    * @param[in] functor A newly allocated FunctorDelegate.
407    * @return True if the signal was connected.
408    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
409    */
410   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
411
412 private: // Implementation of ContextNotificationInterface:
413
414   /**
415    * @copydoc Dali::Integration::NotifyContextLost();
416    */
417   virtual void NotifyContextLost();
418
419   /**
420    * @copydoc Dali::Integration::NotifyContextRegained();
421    */
422   virtual void NotifyContextRegained();
423
424 public: // Implementation of EventThreadServices
425
426   /**
427    * @copydoc EventThreadServices::RegisterObject
428    */
429   virtual void RegisterObject( BaseObject* object);
430
431   /**
432    * @copydoc EventThreadServices::UnregisterObject
433    */
434   virtual void UnregisterObject( BaseObject* object);
435
436   /**
437    * @copydoc EventThreadServices::GetUpdateManager
438    */
439   virtual SceneGraph::UpdateManager& GetUpdateManager();
440
441   /**
442    * @copydoc EventThreadServices::GetRenderController
443    */
444   virtual Integration::RenderController& GetRenderController();
445
446   /**
447    * @copydoc EventThreadServices::ReserveMessageSlot
448    */
449   virtual unsigned int* ReserveMessageSlot( std::size_t size, bool updateScene );
450
451   /**
452    * @copydoc EventThreadServices::GetEventBufferIndex
453    */
454   virtual BufferIndex GetEventBufferIndex() const;
455
456   /**
457    * @copydoc EventThreadServices::ForceNextUpdate
458    */
459   virtual void ForceNextUpdate();
460
461   /**
462    * @copydoc EventThreadServices::IsNextUpdateForced
463    */
464   virtual bool IsNextUpdateForced();
465
466   /**
467    * Request that the depth tree is rebuilt
468    */
469   void RequestRebuildDepthTree();
470
471   /**
472    * Rebuilds the depth tree at the end of the event frame if
473    * it was requested this frame.
474    */
475   void RebuildDepthTree();
476
477 private:
478
479   /**
480    * Protected constructor; see also Stage::New()
481    */
482   Stage( AnimationPlaylist& playlist,
483          PropertyNotificationManager& propertyNotificationManager,
484          SceneGraph::UpdateManager& updateManager,
485          NotificationManager& notificationManager,
486          Integration::RenderController& renderController );
487
488   /**
489    * A reference counted object may only be deleted by calling Unreference()
490    */
491   virtual ~Stage();
492
493 private:
494
495   // For 'Fire and forget' animation support
496   AnimationPlaylist& mAnimationPlaylist;
497
498   PropertyNotificationManager& mPropertyNotificationManager;
499
500   SceneGraph::UpdateManager& mUpdateManager;
501
502   NotificationManager& mNotificationManager;
503
504   Integration::RenderController& mRenderController;
505
506   // The stage-size may be less than surface-size (reduced by top-margin)
507   Vector2 mSize;
508   Vector2 mSurfaceSize;
509
510   // Cached for public GetBackgroundColor()
511   Vector4 mBackgroundColor;
512
513   LayerPtr mRootLayer;
514
515   // Ordered list of currently on-stage layers
516   OwnerPointer<LayerList> mLayerList;
517
518   IntrusivePtr<CameraActor> mDefaultCamera;
519
520   ViewMode mViewMode;
521   float mStereoBase;
522
523   unsigned int mTopMargin;
524   Vector2 mDpi;
525
526   // The object registry
527   ObjectRegistryPtr mObjectRegistry;
528
529   // The list of render-tasks
530   IntrusivePtr<RenderTaskList> mRenderTaskList;
531
532   Dali::RenderTask mRightRenderTask;
533   IntrusivePtr<CameraActor> mRightCamera;
534   Dali::RenderTask mLeftRenderTask;
535   IntrusivePtr<CameraActor> mLeftCamera;
536
537   Integration::SystemOverlay* mSystemOverlay; ///< SystemOverlay stage access
538
539   // The key event signal
540   Dali::Stage::KeyEventSignalType                 mKeyEventSignal;
541   Dali::DevelStage::KeyEventGeneratedSignalType   mKeyEventGeneratedSignal;
542
543   // The event processing finished signal
544   Dali::Stage::EventProcessingFinishedSignalType  mEventProcessingFinishedSignal;
545
546   // The touched signals
547   Dali::Stage::TouchedSignalType                  mTouchedSignal;
548   Dali::Stage::TouchSignalType                    mTouchSignal;
549
550   // The wheel event signal
551   Dali::Stage::WheelEventSignalType               mWheelEventSignal;
552
553   Dali::Stage::ContextStatusSignal mContextLostSignal;
554   Dali::Stage::ContextStatusSignal mContextRegainedSignal;
555
556   Dali::Stage::SceneCreatedSignalType mSceneCreatedSignal;
557
558   DevelStage::Rendering mRenderingBehavior; ///< The rendering behavior
559
560   bool mDepthTreeDirty:1;  ///< True if the depth tree needs recalculating
561   bool mForceNextUpdate:1; ///< True if the next rendering is really required.
562   bool mRenderToFbo:1;     ///< Whether to render to a Frame Buffer Object.
563 };
564
565 } // namespace Internal
566
567 // Helpers for public-api forwarding methods
568
569 inline Internal::Stage& GetImplementation(Dali::Stage& stage)
570 {
571   DALI_ASSERT_ALWAYS( stage && "Stage handle is empty" );
572
573   BaseObject& handle = stage.GetBaseObject();
574
575   return static_cast<Internal::Stage&>(handle);
576 }
577
578 inline const Internal::Stage& GetImplementation(const Dali::Stage& stage)
579 {
580   DALI_ASSERT_ALWAYS( stage && "Stage handle is empty" );
581
582   const BaseObject& handle = stage.GetBaseObject();
583
584   return static_cast<const Internal::Stage&>(handle);
585 }
586
587 } // namespace Dali
588
589 #endif // DALI_INTERNAL_STAGE_H