2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/event/common/stage-impl.h>
24 #include <cstring> // for strcmp
27 #include <dali/integration-api/system-overlay.h>
28 #include <dali/internal/event/actors/layer-impl.h>
29 #include <dali/internal/event/actors/layer-list.h>
30 #include <dali/internal/event/actors/camera-actor-impl.h>
31 #include <dali/internal/event/common/system-overlay-impl.h>
32 #include <dali/internal/event/common/thread-local-storage.h>
33 #include <dali/internal/event/common/property-notification-manager.h>
34 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
35 #include <dali/internal/update/nodes/node.h>
36 #include <dali/internal/event/common/object-registry-impl.h>
37 #include <dali/integration-api/platform-abstraction.h>
38 #include <dali/public-api/common/constants.h>
39 #include <dali/public-api/events/touch-data.h>
40 #include <dali/public-api/object/type-registry.h>
41 #include <dali/public-api/render-tasks/render-task-list.h>
43 using Dali::Internal::SceneGraph::Node;
47 #if defined(DEBUG_ENABLED)
48 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_DEPTH_TIMER" );
61 const float DEFAULT_STEREO_BASE( 65.0f );
65 const char* const SIGNAL_KEY_EVENT = "keyEvent";
66 const char* const SIGNAL_KEY_EVENT_GENERATED = "keyEventGenerated";
67 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "eventProcessingFinished";
68 const char* const SIGNAL_TOUCHED = "touched";
69 const char* const SIGNAL_TOUCH = "touch";
70 const char* const SIGNAL_WHEEL_EVENT = "wheelEvent";
71 const char* const SIGNAL_CONTEXT_LOST = "contextLost";
72 const char* const SIGNAL_CONTEXT_REGAINED = "contextRegained";
73 const char* const SIGNAL_SCENE_CREATED = "sceneCreated";
75 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
77 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT, &Stage::DoConnectSignal );
78 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
79 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED, &Stage::DoConnectSignal );
80 SignalConnectorType signalConnector4( mType, SIGNAL_WHEEL_EVENT, &Stage::DoConnectSignal );
81 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_LOST, &Stage::DoConnectSignal );
82 SignalConnectorType signalConnector6( mType, SIGNAL_CONTEXT_REGAINED, &Stage::DoConnectSignal );
83 SignalConnectorType signalConnector7( mType, SIGNAL_SCENE_CREATED, &Stage::DoConnectSignal );
84 SignalConnectorType signalConnector8( mType, SIGNAL_KEY_EVENT_GENERATED, &Stage::DoConnectSignal );
85 SignalConnectorType signalConnector9( mType, SIGNAL_TOUCH, &Stage::DoConnectSignal );
87 } // unnamed namespace
89 StagePtr Stage::New( AnimationPlaylist& playlist,
90 PropertyNotificationManager& propertyNotificationManager,
91 SceneGraph::UpdateManager& updateManager,
92 NotificationManager& notificationManager,
93 Integration::RenderController& renderController )
95 return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager, renderController ) );
98 void Stage::Initialize()
100 mObjectRegistry = ObjectRegistry::New();
102 // Create the ordered list of layers
103 mLayerList = LayerList::New( mUpdateManager, false/*not system-level*/ );
105 // The stage owns the default layer
106 mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager, false/*not system-level*/ );
107 mRootLayer->SetName("RootLayer");
108 // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
109 // This stops actors parented to the stage having their relayout requests propagating
110 // up to the root layer, and down through other children unnecessarily.
111 mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
113 // Create the default camera actor first; this is needed by the RenderTaskList
114 CreateDefaultCameraActor();
116 // Create the list of render-tasks
117 mRenderTaskList = RenderTaskList::New( *this, *this, false/*not system-level*/ );
119 // Create the default render-task
120 Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
123 void Stage::Uninitialize()
125 // Remove actors added to SystemOverlay
126 delete mSystemOverlay;
127 mSystemOverlay = NULL;
131 // its enough to release the handle so the object is released
132 // don't need to remove it from root actor as root actor will delete the object
133 mDefaultCamera.Reset();
138 // we are closing down so just delete the root, no point emit disconnect
139 // signals or send messages to update
144 StagePtr Stage::GetCurrent()
146 StagePtr stage( NULL );
147 // no checking in this version
148 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
151 stage = tls->GetCurrentStage();
156 bool Stage::IsInstalled()
158 return ThreadLocalStorage::Created();
161 ObjectRegistry& Stage::GetObjectRegistry()
163 return *mObjectRegistry;
166 void Stage::RegisterObject( Dali::BaseObject* object )
168 mObjectRegistry->RegisterObject( object );
171 void Stage::UnregisterObject( Dali::BaseObject* object )
173 mObjectRegistry->UnregisterObject( object );
176 Layer& Stage::GetRootActor()
181 AnimationPlaylist& Stage::GetAnimationPlaylist()
183 return mAnimationPlaylist;
186 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
188 return mPropertyNotificationManager;
191 void Stage::Add( Actor& actor )
193 mRootLayer->Add( actor );
196 void Stage::Remove( Actor& actor )
198 mRootLayer->Remove( actor );
201 void Stage::SurfaceResized(float width, float height)
203 mSurfaceSize.width = width;
204 mSurfaceSize.height = height;
206 // Internally we want to report the actual size of the stage.
208 mSize.height = height - mTopMargin;
210 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
211 mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
213 // Adjust the camera height to allow for top-margin
214 SetDefaultCameraPosition();
216 mRootLayer->SetSize( mSize.width, mSize.height );
218 // Repeat for SystemOverlay actors
221 // Note that the SystemOverlay has a separate camera, configured for the full surface-size.
222 // This will remain unaffected by changes in SetDefaultCameraPosition()
223 mSystemOverlay->GetImpl()->SetSize( width, height );
226 SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
228 // if single render task to screen then set its viewport parameters
229 if( 1 == mRenderTaskList->GetTaskCount() )
231 Dali::RenderTask mDefaultRenderTask = mRenderTaskList->GetTask(0);
233 if(!mDefaultRenderTask.GetTargetFrameBuffer())
235 mDefaultRenderTask.SetViewport( Viewport(0, 0, width, height) );
241 Vector2 Stage::GetSize() const
246 void Stage::SetTopMargin( unsigned int margin )
248 if (mTopMargin == margin)
254 mSize.width = mSurfaceSize.width;
255 mSize.height = mSurfaceSize.height - mTopMargin;
257 // Adjust the camera height to allow for top-margin
258 SetDefaultCameraPosition();
260 mRootLayer->SetSize( mSize.width, mSize.height );
263 RenderTaskList& Stage::GetRenderTaskList() const
265 return *mRenderTaskList;
268 void Stage::CreateDefaultCameraActor()
270 // The default camera attributes and position is such that
271 // children of the default layer, can be positioned at (0,0) and
272 // be at the top-left of the viewport.
273 mDefaultCamera = CameraActor::New( Size::ZERO );
274 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
275 Add(*(mDefaultCamera.Get()));
278 void Stage::SetDefaultCameraPosition()
280 mDefaultCamera->SetY( -(static_cast<float>(mTopMargin) * 0.5f) );
283 Actor& Stage::GetDefaultRootActor()
288 CameraActor& Stage::GetDefaultCameraActor()
290 return *mDefaultCamera;
293 unsigned int Stage::GetLayerCount() const
295 return mLayerList->GetLayerCount();
298 Dali::Layer Stage::GetLayer( unsigned int depth ) const
300 return Dali::Layer(mLayerList->GetLayer( depth ));
303 Dali::Layer Stage::GetRootLayer() const
305 return Dali::Layer( mRootLayer.Get() );
308 LayerList& Stage::GetLayerList()
313 Integration::SystemOverlay& Stage::GetSystemOverlay()
315 // Lazily create system-level if requested
316 if( !mSystemOverlay )
318 mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
319 DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
321 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
324 return *mSystemOverlay;
327 SystemOverlay* Stage::GetSystemOverlayInternal()
329 SystemOverlay* overlay( NULL );
333 overlay = mSystemOverlay->GetImpl();
339 void Stage::SetViewMode( ViewMode viewMode )
341 if( mViewMode != viewMode )
343 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
345 if( mViewMode == MONO )
347 mDefaultCamera->SetOrientation( Dali::ANGLE_180, Vector3::YAXIS );
348 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
350 //Create camera and RenderTask for left eye
351 mLeftCamera = CameraActor::New( Size::ZERO );
352 mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
353 mDefaultCamera->Add( *mLeftCamera.Get() );
354 mLeftRenderTask = mRenderTaskList->CreateTask();
355 mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
356 mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
358 //Create camera and RenderTask for right eye
359 mRightCamera = CameraActor::New( Size::ZERO );
360 mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
361 mDefaultCamera->Add( *mRightCamera.Get() );
362 mRightRenderTask = mRenderTaskList->CreateTask();
363 mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
365 mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
366 mRightCamera->SetType( Dali::Camera::FREE_LOOK );
370 mViewMode = viewMode;
376 // delete extra stereoscopic render tasks and cameras
377 mRenderTaskList->RemoveTask( mLeftRenderTask );
378 mDefaultCamera->Remove( *mLeftCamera.Get() );
379 mLeftRenderTask.Reset();
381 mRenderTaskList->RemoveTask( mRightRenderTask );
382 mDefaultCamera->Remove( *mRightCamera.Get() );
383 mRightRenderTask.Reset();
384 mRightCamera.Reset();
385 mDefaultCamera->SetOrientation( Dali::ANGLE_0, Vector3::YAXIS );
386 mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
387 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
391 case STEREO_HORIZONTAL:
393 //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
394 //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
396 //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
397 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
399 //Calculate aspect ratio
400 float aspect = mSize.width / (mSize.height * 0.5f);
402 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
403 mLeftCamera->SetAspectRatio( aspect );
405 mLeftCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
406 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
407 mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
409 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
410 mRightCamera->SetAspectRatio( aspect );
411 mRightCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
412 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
413 mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
417 case STEREO_VERTICAL:
419 //Calculate separation in pixels along horizontal axis
420 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
422 //Recalculate fov based on viewport size
423 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
425 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
426 mLeftCamera->SetFieldOfView( fov );
427 mLeftCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
428 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
429 mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
431 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
432 mRightCamera->SetFieldOfView( fov );
433 mRightCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
434 mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
435 mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
439 case STEREO_INTERLACED:
447 ViewMode Stage::GetViewMode() const
452 void Stage::SetStereoBase( float stereoBase )
454 if( ! Equals( mStereoBase, stereoBase ) )
456 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)\n", mStereoBase, stereoBase );
457 mStereoBase = stereoBase;
461 case STEREO_HORIZONTAL:
463 stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
464 float aspect = mSize.width / (mSize.height * 0.5f);
466 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
467 mLeftCamera->SetAspectRatio( aspect );
468 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
470 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
471 mRightCamera->SetAspectRatio( aspect );
472 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
476 case STEREO_VERTICAL:
478 stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
479 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
481 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
482 mLeftCamera->SetFieldOfView( fov );
483 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
485 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
486 mRightCamera->SetFieldOfView( fov );
487 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
497 float Stage::GetStereoBase() const
502 void Stage::SetBackgroundColor(Vector4 color)
504 // Cache for public GetBackgroundColor()
505 mBackgroundColor = color;
507 // Send message to change color in next frame
508 SetBackgroundColorMessage( mUpdateManager, color );
511 Vector4 Stage::GetBackgroundColor() const
513 return mBackgroundColor;
516 Vector2 Stage::GetDpi() const
521 void Stage::SetDpi(Vector2 dpi)
526 void Stage::KeepRendering( float durationSeconds )
528 // Send message to keep rendering
529 KeepRenderingMessage( mUpdateManager, durationSeconds );
532 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
534 bool connected( true );
535 Stage* stage = static_cast< Stage* >(object); // TypeRegistry guarantees that this is the correct type.
537 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
539 stage->KeyEventSignal().Connect( tracker, functor );
541 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT_GENERATED ) )
543 stage->KeyEventGeneratedSignal().Connect( tracker, functor );
545 else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
547 stage->EventProcessingFinishedSignal().Connect( tracker, functor );
549 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
551 stage->TouchedSignal().Connect( tracker, functor );
553 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCH ) )
555 stage->TouchSignal().Connect( tracker, functor );
557 else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
559 stage->WheelEventSignal().Connect( tracker, functor );
561 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
563 stage->ContextLostSignal().Connect( tracker, functor );
565 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
567 stage->ContextRegainedSignal().Connect( tracker, functor );
569 else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
571 stage->SceneCreatedSignal().Connect( tracker, functor );
575 // signalName does not match any signal
582 void Stage::EmitKeyEventSignal(const KeyEvent& event)
584 // Emit the key event signal when no actor in the stage has gained the key input focus
586 mKeyEventSignal.Emit( event );
589 bool Stage::EmitKeyEventGeneratedSignal(const KeyEvent& event)
591 // Emit the KeyEventGenerated signal when KeyEvent is generated
593 return mKeyEventGeneratedSignal.Emit( event );
596 void Stage::EmitEventProcessingFinishedSignal()
598 mEventProcessingFinishedSignal.Emit();
601 void Stage::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
603 mTouchedSignal.Emit( touchEvent );
604 mTouchSignal.Emit( touch );
607 void Stage::EmitWheelEventSignal(const WheelEvent& event)
609 // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
611 mWheelEventSignal.Emit( event );
614 void Stage::EmitSceneCreatedSignal()
616 mSceneCreatedSignal.Emit();
619 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
621 return mKeyEventSignal;
624 Dali::DevelStage::KeyEventGeneratedSignalType& Stage::KeyEventGeneratedSignal()
626 return mKeyEventGeneratedSignal;
629 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
631 return mEventProcessingFinishedSignal;
634 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
636 DALI_LOG_WARNING( "Deprecated. Use TouchSignal() instead.\n" );
637 return mTouchedSignal;
640 Dali::Stage::TouchSignalType& Stage::TouchSignal()
645 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
647 return mWheelEventSignal;
650 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
652 return mContextLostSignal;
655 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
657 return mContextRegainedSignal;
660 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
662 return mSceneCreatedSignal;
665 void Stage::NotifyContextLost()
667 mContextLostSignal.Emit();
670 void Stage::NotifyContextRegained()
672 mContextRegainedSignal.Emit();
676 void Stage::RequestRebuildDepthTree()
678 DALI_LOG_INFO(gLogFilter, Debug::General, "RequestRebuildDepthTree()\n");
679 mDepthTreeDirty = true;
682 void Stage::RebuildDepthTree()
684 // If the depth tree needs rebuilding, do it in this frame only.
685 if( mDepthTreeDirty )
687 DALI_LOG_INFO(gLogFilter, Debug::Concise, "RebuildDepthTree() dirty:T\n");
689 ActorPtr actor( mRootLayer.Get() );
690 actor->RebuildDepthTree();
691 mDepthTreeDirty = false;
696 Stage::Stage( AnimationPlaylist& playlist,
697 PropertyNotificationManager& propertyNotificationManager,
698 SceneGraph::UpdateManager& updateManager,
699 NotificationManager& notificationManager,
700 Integration::RenderController& renderController )
701 : mAnimationPlaylist( playlist ),
702 mPropertyNotificationManager( propertyNotificationManager ),
703 mUpdateManager( updateManager ),
704 mNotificationManager( notificationManager ),
705 mRenderController( renderController ),
706 mSize( Vector2::ZERO ),
707 mBackgroundColor( Dali::Stage::DEFAULT_BACKGROUND_COLOR ),
709 mStereoBase( DEFAULT_STEREO_BASE ),
711 mSystemOverlay( NULL ),
712 mDepthTreeDirty( false ),
713 mForceNextUpdate( false )
717 SceneGraph::UpdateManager& Stage::GetUpdateManager()
719 return mUpdateManager;
722 Integration::RenderController& Stage::GetRenderController()
724 return mRenderController;
727 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
729 return mUpdateManager.ReserveMessageSlot( size, updateScene );
732 BufferIndex Stage::GetEventBufferIndex() const
734 return mUpdateManager.GetEventBufferIndex();
737 void Stage::ForceNextUpdate()
739 mForceNextUpdate = true;
742 bool Stage::IsNextUpdateForced()
744 bool nextUpdateForced = mForceNextUpdate;
745 mForceNextUpdate = false;
746 return nextUpdateForced;
751 delete mSystemOverlay;
753 mObjectRegistry.Reset();
756 } // namespace Internal