2 * Copyright (c) 2014 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/actor-attachments/camera-attachment-impl.h>
32 #include <dali/internal/event/common/system-overlay-impl.h>
33 #include <dali/internal/event/common/thread-local-storage.h>
34 #include <dali/internal/event/common/property-notification-manager.h>
35 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
36 #include <dali/internal/update/nodes/node.h>
37 #include <dali/internal/event/common/object-registry-impl.h>
38 #include <dali/integration-api/platform-abstraction.h>
39 #include <dali/public-api/common/constants.h>
40 #include <dali/public-api/object/type-registry.h>
41 #include <dali/public-api/render-tasks/render-task-list.h>
43 #ifdef DYNAMICS_SUPPORT
44 #include <dali/internal/event/dynamics/dynamics-world-config-impl.h>
45 #include <dali/internal/event/dynamics/dynamics-world-impl.h>
46 #include <dali/integration-api/dynamics/dynamics-factory-intf.h>
47 #include <dali/integration-api/dynamics/dynamics-world-settings.h>
50 using Dali::Internal::SceneGraph::Node;
61 const float DEFAULT_STEREO_BASE( 65.0f );
65 const char* const SIGNAL_KEY_EVENT = "key-event";
66 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "event-processing-finished";
67 const char* const SIGNAL_TOUCHED = "touched";
68 const char* const SIGNAL_CONTEXT_LOST = "context-lost";
69 const char* const SIGNAL_CONTEXT_REGAINED = "context-regained";
70 const char* const SIGNAL_SCENE_CREATED = "scene-created";
72 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
74 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT, &Stage::DoConnectSignal );
75 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
76 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED, &Stage::DoConnectSignal );
77 SignalConnectorType signalConnector4( mType, SIGNAL_CONTEXT_LOST, &Stage::DoConnectSignal );
78 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_REGAINED, &Stage::DoConnectSignal );
79 SignalConnectorType signalConnector6( mType, SIGNAL_SCENE_CREATED, &Stage::DoConnectSignal );
81 } // unnamed namespace
83 StagePtr Stage::New( AnimationPlaylist& playlist,
84 PropertyNotificationManager& propertyNotificationManager,
85 SceneGraph::UpdateManager& updateManager,
86 NotificationManager& notificationManager )
88 return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager ) );
91 void Stage::Initialize()
93 mObjectRegistry = ObjectRegistry::New();
95 // Create the ordered list of layers
96 mLayerList = LayerList::New( mUpdateManager, false/*not system-level*/ );
98 // The stage owns the default layer
99 mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager, false/*not system-level*/ );
100 mRootLayer->SetName("RootLayer");
102 // Create the default camera actor first; this is needed by the RenderTaskList
103 CreateDefaultCameraActor();
105 // Create the list of render-tasks
106 mRenderTaskList = RenderTaskList::New( *this, *this, false/*not system-level*/ );
108 // Create the default render-task
109 Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
112 void Stage::Uninitialize()
114 // Remove actors added to SystemOverlay
115 delete mSystemOverlay;
116 mSystemOverlay = NULL;
120 // its enough to release the handle so the object is released
121 // don't need to remove it from root actor as root actor will delete the object
122 mDefaultCamera.Reset();
127 // we are closing down so just delete the root, no point emit disconnect
128 // signals or send messages to update
133 StagePtr Stage::GetCurrent()
135 StagePtr stage( NULL );
136 // no checking in this version
137 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
140 stage = tls->GetCurrentStage();
145 bool Stage::IsInstalled()
147 return ThreadLocalStorage::Created();
150 ObjectRegistry& Stage::GetObjectRegistry()
152 return *mObjectRegistry;
155 void Stage::RegisterObject( Dali::BaseObject* object )
157 mObjectRegistry->RegisterObject( object );
160 void Stage::UnregisterObject( Dali::BaseObject* object )
162 mObjectRegistry->UnregisterObject( object );
165 Layer& Stage::GetRootActor()
170 AnimationPlaylist& Stage::GetAnimationPlaylist()
172 return mAnimationPlaylist;
175 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
177 return mPropertyNotificationManager;
180 void Stage::Add( Actor& actor )
182 mRootLayer->Add( actor );
185 void Stage::Remove( Actor& actor )
187 mRootLayer->Remove( actor );
190 void Stage::SetSize(float width, float height)
192 // Internally we want to report the actual size of the stage.
194 mSize.height = height;
196 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
197 mDefaultCamera->SetPerspectiveProjection( mSize );
199 // The depth of the stage gets set to the maximun of these values
200 mRootLayer->SetSize( mSize );
202 // Repeat for SystemOverlay actors
205 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
208 SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
211 Vector2 Stage::GetSize() const
216 RenderTaskList& Stage::GetRenderTaskList() const
218 return *mRenderTaskList;
221 void Stage::CreateDefaultCameraActor()
223 // The default camera attributes and position is such that
224 // children of the default layer, can be positioned at (0,0) and
225 // be at the top-left of the viewport.
226 mDefaultCamera = CameraActor::New( Size::ZERO );
227 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
228 Add(*(mDefaultCamera.Get()));
231 Actor& Stage::GetDefaultRootActor()
236 CameraActor& Stage::GetDefaultCameraActor()
238 return *mDefaultCamera;
241 unsigned int Stage::GetLayerCount() const
243 return mLayerList->GetLayerCount();
246 Dali::Layer Stage::GetLayer( unsigned int depth ) const
248 return Dali::Layer(mLayerList->GetLayer( depth ));
251 Dali::Layer Stage::GetRootLayer() const
253 return Dali::Layer( mRootLayer.Get() );
256 LayerList& Stage::GetLayerList()
261 Integration::SystemOverlay& Stage::GetSystemOverlay()
263 // Lazily create system-level if requested
264 if( !mSystemOverlay )
266 mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
267 DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
269 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
272 return *mSystemOverlay;
275 SystemOverlay* Stage::GetSystemOverlayInternal()
277 SystemOverlay* overlay( NULL );
281 overlay = mSystemOverlay->GetImpl();
287 void Stage::SetViewMode( ViewMode viewMode )
289 if( mViewMode != viewMode )
291 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
293 if( mViewMode == MONO )
295 mDefaultCamera->SetOrientation( Dali::ANGLE_180, Vector3::YAXIS );
296 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
298 //Create camera and RenderTask for left eye
299 mLeftCamera = CameraActor::New( Size::ZERO );
300 mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
301 mDefaultCamera->Add( *mLeftCamera.Get() );
302 mLeftRenderTask = mRenderTaskList->CreateTask();
303 mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
304 mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
306 //Create camera and RenderTask for right eye
307 mRightCamera = CameraActor::New( Size::ZERO );
308 mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
309 mDefaultCamera->Add( *mRightCamera.Get() );
310 mRightRenderTask = mRenderTaskList->CreateTask();
311 mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
313 mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
314 mRightCamera->SetType( Dali::Camera::FREE_LOOK );
318 mViewMode = viewMode;
324 // delete extra stereoscopic render tasks and cameras
325 mRenderTaskList->RemoveTask( mLeftRenderTask );
326 mDefaultCamera->Remove( *mLeftCamera.Get() );
327 mLeftRenderTask.Reset();
329 mRenderTaskList->RemoveTask( mRightRenderTask );
330 mDefaultCamera->Remove( *mRightCamera.Get() );
331 mRightRenderTask.Reset();
332 mRightCamera.Reset();
333 mDefaultCamera->SetOrientation( Dali::ANGLE_0, Vector3::YAXIS );
334 mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
335 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
339 case STEREO_HORIZONTAL:
341 //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
342 //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
344 //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
345 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
347 //Calculate aspect ratio
348 float aspect = mSize.width / (mSize.height * 0.5f);
350 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
351 mLeftCamera->SetAspectRatio( aspect );
353 mLeftCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
354 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
355 mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
357 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
358 mRightCamera->SetAspectRatio( aspect );
359 mRightCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
360 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
361 mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
365 case STEREO_VERTICAL:
367 //Calculate separation in pixels along horizontal axis
368 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
370 //Recalculate fov based on viewport size
371 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
373 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
374 mLeftCamera->SetFieldOfView( fov );
375 mLeftCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
376 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
377 mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
379 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
380 mRightCamera->SetFieldOfView( fov );
381 mRightCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
382 mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
383 mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
387 case STEREO_INTERLACED:
395 ViewMode Stage::GetViewMode() const
400 void Stage::SetStereoBase( float stereoBase )
402 if( ! Equals( mStereoBase, stereoBase ) )
404 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)", mStereoBase, stereoBase );
405 mStereoBase = stereoBase;
409 case STEREO_HORIZONTAL:
411 stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
412 float aspect = mSize.width / (mSize.height * 0.5f);
414 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
415 mLeftCamera->SetAspectRatio( aspect );
416 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
418 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
419 mRightCamera->SetAspectRatio( aspect );
420 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
424 case STEREO_VERTICAL:
426 stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
427 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
429 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
430 mLeftCamera->SetFieldOfView( fov );
431 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
433 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
434 mRightCamera->SetFieldOfView( fov );
435 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
445 float Stage::GetStereoBase() const
450 void Stage::SetBackgroundColor(Vector4 color)
452 // Cache for public GetBackgroundColor()
453 mBackgroundColor = color;
455 // Send message to change color in next frame
456 SetBackgroundColorMessage( mUpdateManager, color );
459 Vector4 Stage::GetBackgroundColor() const
461 return mBackgroundColor;
464 Vector2 Stage::GetDpi() const
469 void Stage::SetDpi(Vector2 dpi)
474 #ifdef DYNAMICS_SUPPORT
476 DynamicsNotifier& Stage::GetDynamicsNotifier()
478 return mDynamicsNotifier;
481 DynamicsWorldPtr Stage::InitializeDynamics(DynamicsWorldConfigPtr config)
483 if( !mDynamicsFactory )
485 mDynamicsFactory = ThreadLocalStorage::Get().GetPlatformAbstraction().GetDynamicsFactory();
488 if( mDynamicsFactory && !mDynamicsWorld )
490 if( mDynamicsFactory->InitializeDynamics( *(config->GetSettings()) ) )
492 mDynamicsWorld = DynamicsWorld::New();
493 mDynamicsWorld->Initialize( *this, *mDynamicsFactory, config );
496 return mDynamicsWorld;
499 DynamicsWorldPtr Stage::GetDynamicsWorld()
501 return mDynamicsWorld;
504 void Stage::TerminateDynamics()
508 mDynamicsWorld->Terminate(*this);
509 mDynamicsWorld = NULL;
513 #endif // DYNAMICS_SUPPORT
515 void Stage::KeepRendering( float durationSeconds )
517 // Send message to keep rendering
518 KeepRenderingMessage( mUpdateManager, durationSeconds );
521 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
523 bool connected( true );
524 Stage* stage = dynamic_cast<Stage*>(object);
526 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
528 stage->KeyEventSignal().Connect( tracker, functor );
530 else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
532 stage->EventProcessingFinishedSignal().Connect( tracker, functor );
534 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
536 stage->TouchedSignal().Connect( tracker, functor );
538 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
540 stage->ContextLostSignal().Connect( tracker, functor );
542 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
544 stage->ContextRegainedSignal().Connect( tracker, functor );
546 else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
548 stage->SceneCreatedSignal().Connect( tracker, functor );
552 // signalName does not match any signal
559 void Stage::EmitKeyEventSignal(const KeyEvent& event)
561 // Emit the key event signal when no actor in the stage has gained the key input focus
563 mKeyEventSignal.Emit( event );
566 void Stage::EmitEventProcessingFinishedSignal()
568 mEventProcessingFinishedSignal.Emit();
571 void Stage::EmitTouchedSignal( const TouchEvent& touch )
573 mTouchedSignal.Emit( touch );
577 void Stage::EmitSceneCreatedSignal()
579 mSceneCreatedSignal.Emit();
582 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
584 return mKeyEventSignal;
587 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
589 return mEventProcessingFinishedSignal;
592 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
594 return mTouchedSignal;
597 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
599 return mContextLostSignal;
602 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
604 return mContextRegainedSignal;
607 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
609 return mSceneCreatedSignal;
612 void Stage::NotifyContextLost()
614 mContextLostSignal.Emit();
617 void Stage::NotifyContextRegained()
619 mContextRegainedSignal.Emit();
622 Stage::Stage( AnimationPlaylist& playlist,
623 PropertyNotificationManager& propertyNotificationManager,
624 SceneGraph::UpdateManager& updateManager,
625 NotificationManager& notificationManager )
626 : mAnimationPlaylist( playlist ),
627 mPropertyNotificationManager(propertyNotificationManager),
628 mUpdateManager(updateManager),
629 mNotificationManager(notificationManager),
630 mSize(Vector2::ZERO),
631 mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
633 mStereoBase( DEFAULT_STEREO_BASE ),
634 #ifdef DYNAMICS_SUPPORT
635 mDynamicsFactory(NULL),
641 SceneGraph::UpdateManager& Stage::GetUpdateManager()
643 return mUpdateManager;
646 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
648 return mUpdateManager.ReserveMessageSlot( size, updateScene );
651 BufferIndex Stage::GetEventBufferIndex() const
653 return mUpdateManager.GetEventBufferIndex();
658 delete mSystemOverlay;
660 mObjectRegistry.Reset();
663 } // namespace Internal