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_WHEEL_EVENT = "wheel-event";
69 const char* const SIGNAL_CONTEXT_LOST = "context-lost";
70 const char* const SIGNAL_CONTEXT_REGAINED = "context-regained";
71 const char* const SIGNAL_SCENE_CREATED = "scene-created";
73 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
75 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT, &Stage::DoConnectSignal );
76 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
77 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED, &Stage::DoConnectSignal );
78 SignalConnectorType signalConnector4( mType, SIGNAL_WHEEL_EVENT, &Stage::DoConnectSignal );
79 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_LOST, &Stage::DoConnectSignal );
80 SignalConnectorType signalConnector6( mType, SIGNAL_CONTEXT_REGAINED, &Stage::DoConnectSignal );
81 SignalConnectorType signalConnector7( mType, SIGNAL_SCENE_CREATED, &Stage::DoConnectSignal );
83 } // unnamed namespace
85 StagePtr Stage::New( AnimationPlaylist& playlist,
86 PropertyNotificationManager& propertyNotificationManager,
87 SceneGraph::UpdateManager& updateManager,
88 NotificationManager& notificationManager )
90 return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager ) );
93 void Stage::Initialize()
95 mObjectRegistry = ObjectRegistry::New();
97 // Create the ordered list of layers
98 mLayerList = LayerList::New( mUpdateManager, false/*not system-level*/ );
100 // The stage owns the default layer
101 mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager, false/*not system-level*/ );
102 mRootLayer->SetName("RootLayer");
104 // Create the default camera actor first; this is needed by the RenderTaskList
105 CreateDefaultCameraActor();
107 // Create the list of render-tasks
108 mRenderTaskList = RenderTaskList::New( *this, *this, false/*not system-level*/ );
110 // Create the default render-task
111 Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
114 void Stage::Uninitialize()
116 // Remove actors added to SystemOverlay
117 delete mSystemOverlay;
118 mSystemOverlay = NULL;
122 // its enough to release the handle so the object is released
123 // don't need to remove it from root actor as root actor will delete the object
124 mDefaultCamera.Reset();
129 // we are closing down so just delete the root, no point emit disconnect
130 // signals or send messages to update
135 StagePtr Stage::GetCurrent()
137 StagePtr stage( NULL );
138 // no checking in this version
139 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
142 stage = tls->GetCurrentStage();
147 bool Stage::IsInstalled()
149 return ThreadLocalStorage::Created();
152 ObjectRegistry& Stage::GetObjectRegistry()
154 return *mObjectRegistry;
157 void Stage::RegisterObject( Dali::BaseObject* object )
159 mObjectRegistry->RegisterObject( object );
162 void Stage::UnregisterObject( Dali::BaseObject* object )
164 mObjectRegistry->UnregisterObject( object );
167 Layer& Stage::GetRootActor()
172 AnimationPlaylist& Stage::GetAnimationPlaylist()
174 return mAnimationPlaylist;
177 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
179 return mPropertyNotificationManager;
182 void Stage::Add( Actor& actor )
184 mRootLayer->Add( actor );
187 void Stage::Remove( Actor& actor )
189 mRootLayer->Remove( actor );
192 void Stage::SetSize(float width, float height)
194 // Internally we want to report the actual size of the stage.
196 mSize.height = height;
198 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
199 mDefaultCamera->SetPerspectiveProjection( mSize );
201 // The depth of the stage gets set to the maximun of these values
202 mRootLayer->SetSize( mSize );
204 // Repeat for SystemOverlay actors
207 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
210 SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
213 Vector2 Stage::GetSize() const
218 RenderTaskList& Stage::GetRenderTaskList() const
220 return *mRenderTaskList;
223 void Stage::CreateDefaultCameraActor()
225 // The default camera attributes and position is such that
226 // children of the default layer, can be positioned at (0,0) and
227 // be at the top-left of the viewport.
228 mDefaultCamera = CameraActor::New( Size::ZERO );
229 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
230 Add(*(mDefaultCamera.Get()));
233 Actor& Stage::GetDefaultRootActor()
238 CameraActor& Stage::GetDefaultCameraActor()
240 return *mDefaultCamera;
243 unsigned int Stage::GetLayerCount() const
245 return mLayerList->GetLayerCount();
248 Dali::Layer Stage::GetLayer( unsigned int depth ) const
250 return Dali::Layer(mLayerList->GetLayer( depth ));
253 Dali::Layer Stage::GetRootLayer() const
255 return Dali::Layer( mRootLayer.Get() );
258 LayerList& Stage::GetLayerList()
263 Integration::SystemOverlay& Stage::GetSystemOverlay()
265 // Lazily create system-level if requested
266 if( !mSystemOverlay )
268 mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
269 DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
271 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
274 return *mSystemOverlay;
277 SystemOverlay* Stage::GetSystemOverlayInternal()
279 SystemOverlay* overlay( NULL );
283 overlay = mSystemOverlay->GetImpl();
289 void Stage::SetViewMode( ViewMode viewMode )
291 if( mViewMode != viewMode )
293 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
295 if( mViewMode == MONO )
297 mDefaultCamera->SetOrientation( Dali::ANGLE_180, Vector3::YAXIS );
298 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
300 //Create camera and RenderTask for left eye
301 mLeftCamera = CameraActor::New( Size::ZERO );
302 mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
303 mDefaultCamera->Add( *mLeftCamera.Get() );
304 mLeftRenderTask = mRenderTaskList->CreateTask();
305 mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
306 mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
308 //Create camera and RenderTask for right eye
309 mRightCamera = CameraActor::New( Size::ZERO );
310 mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
311 mDefaultCamera->Add( *mRightCamera.Get() );
312 mRightRenderTask = mRenderTaskList->CreateTask();
313 mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
315 mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
316 mRightCamera->SetType( Dali::Camera::FREE_LOOK );
320 mViewMode = viewMode;
326 // delete extra stereoscopic render tasks and cameras
327 mRenderTaskList->RemoveTask( mLeftRenderTask );
328 mDefaultCamera->Remove( *mLeftCamera.Get() );
329 mLeftRenderTask.Reset();
331 mRenderTaskList->RemoveTask( mRightRenderTask );
332 mDefaultCamera->Remove( *mRightCamera.Get() );
333 mRightRenderTask.Reset();
334 mRightCamera.Reset();
335 mDefaultCamera->SetOrientation( Dali::ANGLE_0, Vector3::YAXIS );
336 mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
337 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
341 case STEREO_HORIZONTAL:
343 //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
344 //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
346 //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
347 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
349 //Calculate aspect ratio
350 float aspect = mSize.width / (mSize.height * 0.5f);
352 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
353 mLeftCamera->SetAspectRatio( aspect );
355 mLeftCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
356 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
357 mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
359 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
360 mRightCamera->SetAspectRatio( aspect );
361 mRightCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
362 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
363 mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
367 case STEREO_VERTICAL:
369 //Calculate separation in pixels along horizontal axis
370 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
372 //Recalculate fov based on viewport size
373 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
375 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
376 mLeftCamera->SetFieldOfView( fov );
377 mLeftCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
378 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
379 mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
381 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
382 mRightCamera->SetFieldOfView( fov );
383 mRightCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
384 mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
385 mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
389 case STEREO_INTERLACED:
397 ViewMode Stage::GetViewMode() const
402 void Stage::SetStereoBase( float stereoBase )
404 if( ! Equals( mStereoBase, stereoBase ) )
406 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)", mStereoBase, stereoBase );
407 mStereoBase = stereoBase;
411 case STEREO_HORIZONTAL:
413 stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
414 float aspect = mSize.width / (mSize.height * 0.5f);
416 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
417 mLeftCamera->SetAspectRatio( aspect );
418 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
420 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
421 mRightCamera->SetAspectRatio( aspect );
422 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
426 case STEREO_VERTICAL:
428 stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
429 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
431 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
432 mLeftCamera->SetFieldOfView( fov );
433 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
435 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
436 mRightCamera->SetFieldOfView( fov );
437 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
447 float Stage::GetStereoBase() const
452 void Stage::SetBackgroundColor(Vector4 color)
454 // Cache for public GetBackgroundColor()
455 mBackgroundColor = color;
457 // Send message to change color in next frame
458 SetBackgroundColorMessage( mUpdateManager, color );
461 Vector4 Stage::GetBackgroundColor() const
463 return mBackgroundColor;
466 Vector2 Stage::GetDpi() const
471 void Stage::SetDpi(Vector2 dpi)
476 #ifdef DYNAMICS_SUPPORT
478 DynamicsNotifier& Stage::GetDynamicsNotifier()
480 return mDynamicsNotifier;
483 DynamicsWorldPtr Stage::InitializeDynamics(DynamicsWorldConfigPtr config)
485 if( !mDynamicsFactory )
487 mDynamicsFactory = ThreadLocalStorage::Get().GetPlatformAbstraction().GetDynamicsFactory();
490 if( mDynamicsFactory && !mDynamicsWorld )
492 if( mDynamicsFactory->InitializeDynamics( *(config->GetSettings()) ) )
494 mDynamicsWorld = DynamicsWorld::New();
495 mDynamicsWorld->Initialize( *this, *mDynamicsFactory, config );
498 return mDynamicsWorld;
501 DynamicsWorldPtr Stage::GetDynamicsWorld()
503 return mDynamicsWorld;
506 void Stage::TerminateDynamics()
510 mDynamicsWorld->Terminate(*this);
511 mDynamicsWorld = NULL;
515 #endif // DYNAMICS_SUPPORT
517 void Stage::KeepRendering( float durationSeconds )
519 // Send message to keep rendering
520 KeepRenderingMessage( mUpdateManager, durationSeconds );
523 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
525 bool connected( true );
526 Stage* stage = dynamic_cast<Stage*>(object);
528 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
530 stage->KeyEventSignal().Connect( tracker, functor );
532 else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
534 stage->EventProcessingFinishedSignal().Connect( tracker, functor );
536 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
538 stage->TouchedSignal().Connect( tracker, functor );
540 else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
542 stage->WheelEventSignal().Connect( tracker, functor );
544 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
546 stage->ContextLostSignal().Connect( tracker, functor );
548 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
550 stage->ContextRegainedSignal().Connect( tracker, functor );
552 else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
554 stage->SceneCreatedSignal().Connect( tracker, functor );
558 // signalName does not match any signal
565 void Stage::EmitKeyEventSignal(const KeyEvent& event)
567 // Emit the key event signal when no actor in the stage has gained the key input focus
569 mKeyEventSignal.Emit( event );
572 void Stage::EmitEventProcessingFinishedSignal()
574 mEventProcessingFinishedSignal.Emit();
577 void Stage::EmitTouchedSignal( const TouchEvent& touch )
579 mTouchedSignal.Emit( touch );
582 void Stage::EmitWheelEventSignal(const WheelEvent& event)
584 // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
586 mWheelEventSignal.Emit( event );
589 void Stage::EmitSceneCreatedSignal()
591 mSceneCreatedSignal.Emit();
594 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
596 return mKeyEventSignal;
599 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
601 return mEventProcessingFinishedSignal;
604 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
606 return mTouchedSignal;
609 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
611 return mWheelEventSignal;
614 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
616 return mContextLostSignal;
619 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
621 return mContextRegainedSignal;
624 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
626 return mSceneCreatedSignal;
629 void Stage::NotifyContextLost()
631 mContextLostSignal.Emit();
634 void Stage::NotifyContextRegained()
636 mContextRegainedSignal.Emit();
639 Stage::Stage( AnimationPlaylist& playlist,
640 PropertyNotificationManager& propertyNotificationManager,
641 SceneGraph::UpdateManager& updateManager,
642 NotificationManager& notificationManager )
643 : mAnimationPlaylist( playlist ),
644 mPropertyNotificationManager(propertyNotificationManager),
645 mUpdateManager(updateManager),
646 mNotificationManager(notificationManager),
647 mSize(Vector2::ZERO),
648 mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
650 mStereoBase( DEFAULT_STEREO_BASE ),
651 #ifdef DYNAMICS_SUPPORT
652 mDynamicsFactory(NULL),
658 SceneGraph::UpdateManager& Stage::GetUpdateManager()
660 return mUpdateManager;
663 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
665 return mUpdateManager.ReserveMessageSlot( size, updateScene );
668 BufferIndex Stage::GetEventBufferIndex() const
670 return mUpdateManager.GetEventBufferIndex();
675 delete mSystemOverlay;
677 mObjectRegistry.Reset();
680 } // namespace Internal