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 using Dali::Internal::SceneGraph::Node;
54 const float DEFAULT_STEREO_BASE( 65.0f );
58 const char* const SIGNAL_KEY_EVENT = "key-event";
59 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "event-processing-finished";
60 const char* const SIGNAL_TOUCHED = "touched";
61 const char* const SIGNAL_WHEEL_EVENT = "wheel-event";
62 const char* const SIGNAL_CONTEXT_LOST = "context-lost";
63 const char* const SIGNAL_CONTEXT_REGAINED = "context-regained";
64 const char* const SIGNAL_SCENE_CREATED = "scene-created";
66 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
68 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT, &Stage::DoConnectSignal );
69 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
70 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED, &Stage::DoConnectSignal );
71 SignalConnectorType signalConnector4( mType, SIGNAL_WHEEL_EVENT, &Stage::DoConnectSignal );
72 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_LOST, &Stage::DoConnectSignal );
73 SignalConnectorType signalConnector6( mType, SIGNAL_CONTEXT_REGAINED, &Stage::DoConnectSignal );
74 SignalConnectorType signalConnector7( mType, SIGNAL_SCENE_CREATED, &Stage::DoConnectSignal );
76 } // unnamed namespace
78 StagePtr Stage::New( AnimationPlaylist& playlist,
79 PropertyNotificationManager& propertyNotificationManager,
80 SceneGraph::UpdateManager& updateManager,
81 NotificationManager& notificationManager )
83 return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager ) );
86 void Stage::Initialize()
88 mObjectRegistry = ObjectRegistry::New();
90 // Create the ordered list of layers
91 mLayerList = LayerList::New( mUpdateManager, false/*not system-level*/ );
93 // The stage owns the default layer
94 mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager, false/*not system-level*/ );
95 mRootLayer->SetName("RootLayer");
96 // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
97 // This stops actors parented to the stage having their relayout requests propagating
98 // up to the root layer, and down through other children unnecessarily.
99 mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
101 // Create the default camera actor first; this is needed by the RenderTaskList
102 CreateDefaultCameraActor();
104 // Create the list of render-tasks
105 mRenderTaskList = RenderTaskList::New( *this, *this, false/*not system-level*/ );
107 // Create the default render-task
108 Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
111 void Stage::Uninitialize()
113 // Remove actors added to SystemOverlay
114 delete mSystemOverlay;
115 mSystemOverlay = NULL;
119 // its enough to release the handle so the object is released
120 // don't need to remove it from root actor as root actor will delete the object
121 mDefaultCamera.Reset();
126 // we are closing down so just delete the root, no point emit disconnect
127 // signals or send messages to update
132 StagePtr Stage::GetCurrent()
134 StagePtr stage( NULL );
135 // no checking in this version
136 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
139 stage = tls->GetCurrentStage();
144 bool Stage::IsInstalled()
146 return ThreadLocalStorage::Created();
149 ObjectRegistry& Stage::GetObjectRegistry()
151 return *mObjectRegistry;
154 void Stage::RegisterObject( Dali::BaseObject* object )
156 mObjectRegistry->RegisterObject( object );
159 void Stage::UnregisterObject( Dali::BaseObject* object )
161 mObjectRegistry->UnregisterObject( object );
164 Layer& Stage::GetRootActor()
169 AnimationPlaylist& Stage::GetAnimationPlaylist()
171 return mAnimationPlaylist;
174 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
176 return mPropertyNotificationManager;
179 void Stage::Add( Actor& actor )
181 mRootLayer->Add( actor );
184 void Stage::Remove( Actor& actor )
186 mRootLayer->Remove( actor );
189 void Stage::SetSize(float width, float height)
191 // Internally we want to report the actual size of the stage.
193 mSize.height = height;
195 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
196 mDefaultCamera->SetPerspectiveProjection( mSize );
198 // The depth of the stage gets set to the maximun of these values
199 mRootLayer->SetSize( mSize );
201 // Repeat for SystemOverlay actors
204 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
207 SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
210 Vector2 Stage::GetSize() const
215 RenderTaskList& Stage::GetRenderTaskList() const
217 return *mRenderTaskList;
220 void Stage::CreateDefaultCameraActor()
222 // The default camera attributes and position is such that
223 // children of the default layer, can be positioned at (0,0) and
224 // be at the top-left of the viewport.
225 mDefaultCamera = CameraActor::New( Size::ZERO );
226 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
227 Add(*(mDefaultCamera.Get()));
230 Actor& Stage::GetDefaultRootActor()
235 CameraActor& Stage::GetDefaultCameraActor()
237 return *mDefaultCamera;
240 unsigned int Stage::GetLayerCount() const
242 return mLayerList->GetLayerCount();
245 Dali::Layer Stage::GetLayer( unsigned int depth ) const
247 return Dali::Layer(mLayerList->GetLayer( depth ));
250 Dali::Layer Stage::GetRootLayer() const
252 return Dali::Layer( mRootLayer.Get() );
255 LayerList& Stage::GetLayerList()
260 Integration::SystemOverlay& Stage::GetSystemOverlay()
262 // Lazily create system-level if requested
263 if( !mSystemOverlay )
265 mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
266 DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
268 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
271 return *mSystemOverlay;
274 SystemOverlay* Stage::GetSystemOverlayInternal()
276 SystemOverlay* overlay( NULL );
280 overlay = mSystemOverlay->GetImpl();
286 void Stage::SetViewMode( ViewMode viewMode )
288 if( mViewMode != viewMode )
290 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
292 if( mViewMode == MONO )
294 mDefaultCamera->SetOrientation( Dali::ANGLE_180, Vector3::YAXIS );
295 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
297 //Create camera and RenderTask for left eye
298 mLeftCamera = CameraActor::New( Size::ZERO );
299 mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
300 mDefaultCamera->Add( *mLeftCamera.Get() );
301 mLeftRenderTask = mRenderTaskList->CreateTask();
302 mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
303 mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
305 //Create camera and RenderTask for right eye
306 mRightCamera = CameraActor::New( Size::ZERO );
307 mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
308 mDefaultCamera->Add( *mRightCamera.Get() );
309 mRightRenderTask = mRenderTaskList->CreateTask();
310 mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
312 mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
313 mRightCamera->SetType( Dali::Camera::FREE_LOOK );
317 mViewMode = viewMode;
323 // delete extra stereoscopic render tasks and cameras
324 mRenderTaskList->RemoveTask( mLeftRenderTask );
325 mDefaultCamera->Remove( *mLeftCamera.Get() );
326 mLeftRenderTask.Reset();
328 mRenderTaskList->RemoveTask( mRightRenderTask );
329 mDefaultCamera->Remove( *mRightCamera.Get() );
330 mRightRenderTask.Reset();
331 mRightCamera.Reset();
332 mDefaultCamera->SetOrientation( Dali::ANGLE_0, Vector3::YAXIS );
333 mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
334 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
338 case STEREO_HORIZONTAL:
340 //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
341 //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
343 //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
344 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
346 //Calculate aspect ratio
347 float aspect = mSize.width / (mSize.height * 0.5f);
349 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
350 mLeftCamera->SetAspectRatio( aspect );
352 mLeftCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
353 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
354 mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
356 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
357 mRightCamera->SetAspectRatio( aspect );
358 mRightCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
359 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
360 mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
364 case STEREO_VERTICAL:
366 //Calculate separation in pixels along horizontal axis
367 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
369 //Recalculate fov based on viewport size
370 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
372 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
373 mLeftCamera->SetFieldOfView( fov );
374 mLeftCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
375 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
376 mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
378 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
379 mRightCamera->SetFieldOfView( fov );
380 mRightCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
381 mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
382 mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
386 case STEREO_INTERLACED:
394 ViewMode Stage::GetViewMode() const
399 void Stage::SetStereoBase( float stereoBase )
401 if( ! Equals( mStereoBase, stereoBase ) )
403 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)", mStereoBase, stereoBase );
404 mStereoBase = stereoBase;
408 case STEREO_HORIZONTAL:
410 stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
411 float aspect = mSize.width / (mSize.height * 0.5f);
413 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
414 mLeftCamera->SetAspectRatio( aspect );
415 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
417 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
418 mRightCamera->SetAspectRatio( aspect );
419 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
423 case STEREO_VERTICAL:
425 stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
426 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
428 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
429 mLeftCamera->SetFieldOfView( fov );
430 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
432 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
433 mRightCamera->SetFieldOfView( fov );
434 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
444 float Stage::GetStereoBase() const
449 void Stage::SetBackgroundColor(Vector4 color)
451 // Cache for public GetBackgroundColor()
452 mBackgroundColor = color;
454 // Send message to change color in next frame
455 SetBackgroundColorMessage( mUpdateManager, color );
458 Vector4 Stage::GetBackgroundColor() const
460 return mBackgroundColor;
463 Vector2 Stage::GetDpi() const
468 void Stage::SetDpi(Vector2 dpi)
473 void Stage::KeepRendering( float durationSeconds )
475 // Send message to keep rendering
476 KeepRenderingMessage( mUpdateManager, durationSeconds );
479 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
481 bool connected( true );
482 Stage* stage = dynamic_cast<Stage*>(object);
484 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
486 stage->KeyEventSignal().Connect( tracker, functor );
488 else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
490 stage->EventProcessingFinishedSignal().Connect( tracker, functor );
492 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
494 stage->TouchedSignal().Connect( tracker, functor );
496 else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
498 stage->WheelEventSignal().Connect( tracker, functor );
500 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
502 stage->ContextLostSignal().Connect( tracker, functor );
504 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
506 stage->ContextRegainedSignal().Connect( tracker, functor );
508 else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
510 stage->SceneCreatedSignal().Connect( tracker, functor );
514 // signalName does not match any signal
521 void Stage::EmitKeyEventSignal(const KeyEvent& event)
523 // Emit the key event signal when no actor in the stage has gained the key input focus
525 mKeyEventSignal.Emit( event );
528 void Stage::EmitEventProcessingFinishedSignal()
530 mEventProcessingFinishedSignal.Emit();
533 void Stage::EmitTouchedSignal( const TouchEvent& touch )
535 mTouchedSignal.Emit( touch );
538 void Stage::EmitWheelEventSignal(const WheelEvent& event)
540 // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
542 mWheelEventSignal.Emit( event );
545 void Stage::EmitSceneCreatedSignal()
547 mSceneCreatedSignal.Emit();
550 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
552 return mKeyEventSignal;
555 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
557 return mEventProcessingFinishedSignal;
560 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
562 return mTouchedSignal;
565 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
567 return mWheelEventSignal;
570 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
572 return mContextLostSignal;
575 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
577 return mContextRegainedSignal;
580 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
582 return mSceneCreatedSignal;
585 void Stage::NotifyContextLost()
587 mContextLostSignal.Emit();
590 void Stage::NotifyContextRegained()
592 mContextRegainedSignal.Emit();
595 Stage::Stage( AnimationPlaylist& playlist,
596 PropertyNotificationManager& propertyNotificationManager,
597 SceneGraph::UpdateManager& updateManager,
598 NotificationManager& notificationManager )
599 : mAnimationPlaylist( playlist ),
600 mPropertyNotificationManager(propertyNotificationManager),
601 mUpdateManager(updateManager),
602 mNotificationManager(notificationManager),
603 mSize(Vector2::ZERO),
604 mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
606 mStereoBase( DEFAULT_STEREO_BASE ),
611 SceneGraph::UpdateManager& Stage::GetUpdateManager()
613 return mUpdateManager;
616 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
618 return mUpdateManager.ReserveMessageSlot( size, updateScene );
621 BufferIndex Stage::GetEventBufferIndex() const
623 return mUpdateManager.GetEventBufferIndex();
628 delete mSystemOverlay;
630 mObjectRegistry.Reset();
633 } // namespace Internal