2 * Copyright (c) 2016 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 )
94 return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager ) );
97 void Stage::Initialize()
99 mObjectRegistry = ObjectRegistry::New();
101 // Create the ordered list of layers
102 mLayerList = LayerList::New( mUpdateManager, false/*not system-level*/ );
104 // The stage owns the default layer
105 mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager, false/*not system-level*/ );
106 mRootLayer->SetName("RootLayer");
107 // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
108 // This stops actors parented to the stage having their relayout requests propagating
109 // up to the root layer, and down through other children unnecessarily.
110 mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
112 // Create the default camera actor first; this is needed by the RenderTaskList
113 CreateDefaultCameraActor();
115 // Create the list of render-tasks
116 mRenderTaskList = RenderTaskList::New( *this, *this, false/*not system-level*/ );
118 // Create the default render-task
119 Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
122 void Stage::Uninitialize()
124 // Remove actors added to SystemOverlay
125 delete mSystemOverlay;
126 mSystemOverlay = NULL;
130 // its enough to release the handle so the object is released
131 // don't need to remove it from root actor as root actor will delete the object
132 mDefaultCamera.Reset();
137 // we are closing down so just delete the root, no point emit disconnect
138 // signals or send messages to update
143 StagePtr Stage::GetCurrent()
145 StagePtr stage( NULL );
146 // no checking in this version
147 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
150 stage = tls->GetCurrentStage();
155 bool Stage::IsInstalled()
157 return ThreadLocalStorage::Created();
160 ObjectRegistry& Stage::GetObjectRegistry()
162 return *mObjectRegistry;
165 void Stage::RegisterObject( Dali::BaseObject* object )
167 mObjectRegistry->RegisterObject( object );
170 void Stage::UnregisterObject( Dali::BaseObject* object )
172 mObjectRegistry->UnregisterObject( object );
175 Layer& Stage::GetRootActor()
180 AnimationPlaylist& Stage::GetAnimationPlaylist()
182 return mAnimationPlaylist;
185 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
187 return mPropertyNotificationManager;
190 void Stage::Add( Actor& actor )
192 mRootLayer->Add( actor );
195 void Stage::Remove( Actor& actor )
197 mRootLayer->Remove( actor );
200 void Stage::SurfaceResized(float width, float height)
202 mSurfaceSize.width = width;
203 mSurfaceSize.height = height;
205 // Internally we want to report the actual size of the stage.
207 mSize.height = height - mTopMargin;
209 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
210 mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
212 // Adjust the camera height to allow for top-margin
213 SetDefaultCameraPosition();
215 mRootLayer->SetSize( mSize.width, mSize.height );
217 // Repeat for SystemOverlay actors
220 // Note that the SystemOverlay has a separate camera, configured for the full surface-size.
221 // This will remain unaffected by changes in SetDefaultCameraPosition()
222 mSystemOverlay->GetImpl()->SetSize( width, height );
225 SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
227 // if single render task to screen then set its viewport parameters
228 if( 1 == mRenderTaskList->GetTaskCount() )
230 Dali::RenderTask mDefaultRenderTask = mRenderTaskList->GetTask(0);
232 if(!mDefaultRenderTask.GetTargetFrameBuffer())
234 mDefaultRenderTask.SetViewport( Viewport(0, 0, width, height) );
240 Vector2 Stage::GetSize() const
245 void Stage::SetTopMargin( unsigned int margin )
247 if (mTopMargin == margin)
253 mSize.width = mSurfaceSize.width;
254 mSize.height = mSurfaceSize.height - mTopMargin;
256 // Adjust the camera height to allow for top-margin
257 SetDefaultCameraPosition();
259 mRootLayer->SetSize( mSize.width, mSize.height );
262 RenderTaskList& Stage::GetRenderTaskList() const
264 return *mRenderTaskList;
267 void Stage::CreateDefaultCameraActor()
269 // The default camera attributes and position is such that
270 // children of the default layer, can be positioned at (0,0) and
271 // be at the top-left of the viewport.
272 mDefaultCamera = CameraActor::New( Size::ZERO );
273 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
274 Add(*(mDefaultCamera.Get()));
277 void Stage::SetDefaultCameraPosition()
279 mDefaultCamera->SetY( -(static_cast<float>(mTopMargin) * 0.5f) );
282 Actor& Stage::GetDefaultRootActor()
287 CameraActor& Stage::GetDefaultCameraActor()
289 return *mDefaultCamera;
292 unsigned int Stage::GetLayerCount() const
294 return mLayerList->GetLayerCount();
297 Dali::Layer Stage::GetLayer( unsigned int depth ) const
299 return Dali::Layer(mLayerList->GetLayer( depth ));
302 Dali::Layer Stage::GetRootLayer() const
304 return Dali::Layer( mRootLayer.Get() );
307 LayerList& Stage::GetLayerList()
312 Integration::SystemOverlay& Stage::GetSystemOverlay()
314 // Lazily create system-level if requested
315 if( !mSystemOverlay )
317 mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
318 DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
320 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
323 return *mSystemOverlay;
326 SystemOverlay* Stage::GetSystemOverlayInternal()
328 SystemOverlay* overlay( NULL );
332 overlay = mSystemOverlay->GetImpl();
338 void Stage::SetViewMode( ViewMode viewMode )
340 if( mViewMode != viewMode )
342 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
344 if( mViewMode == MONO )
346 mDefaultCamera->SetOrientation( Dali::ANGLE_180, Vector3::YAXIS );
347 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
349 //Create camera and RenderTask for left eye
350 mLeftCamera = CameraActor::New( Size::ZERO );
351 mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
352 mDefaultCamera->Add( *mLeftCamera.Get() );
353 mLeftRenderTask = mRenderTaskList->CreateTask();
354 mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
355 mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
357 //Create camera and RenderTask for right eye
358 mRightCamera = CameraActor::New( Size::ZERO );
359 mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
360 mDefaultCamera->Add( *mRightCamera.Get() );
361 mRightRenderTask = mRenderTaskList->CreateTask();
362 mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
364 mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
365 mRightCamera->SetType( Dali::Camera::FREE_LOOK );
369 mViewMode = viewMode;
375 // delete extra stereoscopic render tasks and cameras
376 mRenderTaskList->RemoveTask( mLeftRenderTask );
377 mDefaultCamera->Remove( *mLeftCamera.Get() );
378 mLeftRenderTask.Reset();
380 mRenderTaskList->RemoveTask( mRightRenderTask );
381 mDefaultCamera->Remove( *mRightCamera.Get() );
382 mRightRenderTask.Reset();
383 mRightCamera.Reset();
384 mDefaultCamera->SetOrientation( Dali::ANGLE_0, Vector3::YAXIS );
385 mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
386 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
390 case STEREO_HORIZONTAL:
392 //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
393 //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
395 //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
396 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
398 //Calculate aspect ratio
399 float aspect = mSize.width / (mSize.height * 0.5f);
401 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
402 mLeftCamera->SetAspectRatio( aspect );
404 mLeftCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
405 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
406 mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
408 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
409 mRightCamera->SetAspectRatio( aspect );
410 mRightCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
411 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
412 mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
416 case STEREO_VERTICAL:
418 //Calculate separation in pixels along horizontal axis
419 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
421 //Recalculate fov based on viewport size
422 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
424 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
425 mLeftCamera->SetFieldOfView( fov );
426 mLeftCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
427 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
428 mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
430 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
431 mRightCamera->SetFieldOfView( fov );
432 mRightCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
433 mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
434 mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
438 case STEREO_INTERLACED:
446 ViewMode Stage::GetViewMode() const
451 void Stage::SetStereoBase( float stereoBase )
453 if( ! Equals( mStereoBase, stereoBase ) )
455 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)\n", mStereoBase, stereoBase );
456 mStereoBase = stereoBase;
460 case STEREO_HORIZONTAL:
462 stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
463 float aspect = mSize.width / (mSize.height * 0.5f);
465 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
466 mLeftCamera->SetAspectRatio( aspect );
467 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
469 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
470 mRightCamera->SetAspectRatio( aspect );
471 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
475 case STEREO_VERTICAL:
477 stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
478 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
480 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
481 mLeftCamera->SetFieldOfView( fov );
482 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
484 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
485 mRightCamera->SetFieldOfView( fov );
486 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
496 float Stage::GetStereoBase() const
501 void Stage::SetBackgroundColor(Vector4 color)
503 // Cache for public GetBackgroundColor()
504 mBackgroundColor = color;
506 // Send message to change color in next frame
507 SetBackgroundColorMessage( mUpdateManager, color );
510 Vector4 Stage::GetBackgroundColor() const
512 return mBackgroundColor;
515 Vector2 Stage::GetDpi() const
520 void Stage::SetDpi(Vector2 dpi)
525 void Stage::KeepRendering( float durationSeconds )
527 // Send message to keep rendering
528 KeepRenderingMessage( mUpdateManager, durationSeconds );
531 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
533 bool connected( true );
534 Stage* stage = static_cast< Stage* >(object); // TypeRegistry guarantees that this is the correct type.
536 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
538 stage->KeyEventSignal().Connect( tracker, functor );
540 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT_GENERATED ) )
542 stage->KeyEventGeneratedSignal().Connect( tracker, functor );
544 else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
546 stage->EventProcessingFinishedSignal().Connect( tracker, functor );
548 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
550 stage->TouchedSignal().Connect( tracker, functor );
552 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCH ) )
554 stage->TouchSignal().Connect( tracker, functor );
556 else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
558 stage->WheelEventSignal().Connect( tracker, functor );
560 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
562 stage->ContextLostSignal().Connect( tracker, functor );
564 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
566 stage->ContextRegainedSignal().Connect( tracker, functor );
568 else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
570 stage->SceneCreatedSignal().Connect( tracker, functor );
574 // signalName does not match any signal
581 void Stage::EmitKeyEventSignal(const KeyEvent& event)
583 // Emit the key event signal when no actor in the stage has gained the key input focus
585 mKeyEventSignal.Emit( event );
588 bool Stage::EmitKeyEventGeneratedSignal(const KeyEvent& event)
590 // Emit the KeyEventGenerated signal when KeyEvent is generated
592 return mKeyEventGeneratedSignal.Emit( event );
595 void Stage::EmitEventProcessingFinishedSignal()
597 mEventProcessingFinishedSignal.Emit();
600 void Stage::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
602 mTouchedSignal.Emit( touchEvent );
603 mTouchSignal.Emit( touch );
606 void Stage::EmitWheelEventSignal(const WheelEvent& event)
608 // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
610 mWheelEventSignal.Emit( event );
613 void Stage::EmitSceneCreatedSignal()
615 mSceneCreatedSignal.Emit();
618 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
620 return mKeyEventSignal;
623 Dali::DevelStage::KeyEventGeneratedSignalType& Stage::KeyEventGeneratedSignal()
625 return mKeyEventGeneratedSignal;
628 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
630 return mEventProcessingFinishedSignal;
633 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
635 DALI_LOG_WARNING( "Deprecated. Use TouchSignal() instead.\n" );
636 return mTouchedSignal;
639 Dali::Stage::TouchSignalType& Stage::TouchSignal()
644 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
646 return mWheelEventSignal;
649 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
651 return mContextLostSignal;
654 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
656 return mContextRegainedSignal;
659 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
661 return mSceneCreatedSignal;
664 void Stage::NotifyContextLost()
666 mContextLostSignal.Emit();
669 void Stage::NotifyContextRegained()
671 mContextRegainedSignal.Emit();
675 void Stage::RequestRebuildDepthTree()
677 DALI_LOG_INFO(gLogFilter, Debug::General, "RequestRebuildDepthTree()\n");
678 mDepthTreeDirty = true;
681 void Stage::RebuildDepthTree()
683 // If the depth tree needs rebuilding, do it in this frame only.
684 if( mDepthTreeDirty )
686 DALI_LOG_INFO(gLogFilter, Debug::Concise, "RebuildDepthTree() dirty:T\n");
688 ActorPtr actor( mRootLayer.Get() );
689 actor->RebuildDepthTree();
690 mDepthTreeDirty = false;
695 Stage::Stage( AnimationPlaylist& playlist,
696 PropertyNotificationManager& propertyNotificationManager,
697 SceneGraph::UpdateManager& updateManager,
698 NotificationManager& notificationManager )
699 : mAnimationPlaylist( playlist ),
700 mPropertyNotificationManager(propertyNotificationManager),
701 mUpdateManager(updateManager),
702 mNotificationManager(notificationManager),
703 mSize(Vector2::ZERO),
704 mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
706 mStereoBase( DEFAULT_STEREO_BASE ),
708 mSystemOverlay(NULL),
709 mDepthTreeDirty( false )
713 SceneGraph::UpdateManager& Stage::GetUpdateManager()
715 return mUpdateManager;
718 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
720 return mUpdateManager.ReserveMessageSlot( size, updateScene );
723 BufferIndex Stage::GetEventBufferIndex() const
725 return mUpdateManager.GetEventBufferIndex();
730 delete mSystemOverlay;
732 mObjectRegistry.Reset();
735 } // namespace Internal