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");
97 // Create the default camera actor first; this is needed by the RenderTaskList
98 CreateDefaultCameraActor();
100 // Create the list of render-tasks
101 mRenderTaskList = RenderTaskList::New( *this, *this, false/*not system-level*/ );
103 // Create the default render-task
104 Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
107 void Stage::Uninitialize()
109 // Remove actors added to SystemOverlay
110 delete mSystemOverlay;
111 mSystemOverlay = NULL;
115 // its enough to release the handle so the object is released
116 // don't need to remove it from root actor as root actor will delete the object
117 mDefaultCamera.Reset();
122 // we are closing down so just delete the root, no point emit disconnect
123 // signals or send messages to update
128 StagePtr Stage::GetCurrent()
130 StagePtr stage( NULL );
131 // no checking in this version
132 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
135 stage = tls->GetCurrentStage();
140 bool Stage::IsInstalled()
142 return ThreadLocalStorage::Created();
145 ObjectRegistry& Stage::GetObjectRegistry()
147 return *mObjectRegistry;
150 void Stage::RegisterObject( Dali::BaseObject* object )
152 mObjectRegistry->RegisterObject( object );
155 void Stage::UnregisterObject( Dali::BaseObject* object )
157 mObjectRegistry->UnregisterObject( object );
160 Layer& Stage::GetRootActor()
165 AnimationPlaylist& Stage::GetAnimationPlaylist()
167 return mAnimationPlaylist;
170 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
172 return mPropertyNotificationManager;
175 void Stage::Add( Actor& actor )
177 mRootLayer->Add( actor );
180 void Stage::Remove( Actor& actor )
182 mRootLayer->Remove( actor );
185 void Stage::SetSize(float width, float height)
187 // Internally we want to report the actual size of the stage.
189 mSize.height = height;
191 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
192 mDefaultCamera->SetPerspectiveProjection( mSize );
194 // The depth of the stage gets set to the maximun of these values
195 mRootLayer->SetSize( mSize );
197 // Repeat for SystemOverlay actors
200 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
203 SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
206 Vector2 Stage::GetSize() const
211 RenderTaskList& Stage::GetRenderTaskList() const
213 return *mRenderTaskList;
216 void Stage::CreateDefaultCameraActor()
218 // The default camera attributes and position is such that
219 // children of the default layer, can be positioned at (0,0) and
220 // be at the top-left of the viewport.
221 mDefaultCamera = CameraActor::New( Size::ZERO );
222 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
223 Add(*(mDefaultCamera.Get()));
226 Actor& Stage::GetDefaultRootActor()
231 CameraActor& Stage::GetDefaultCameraActor()
233 return *mDefaultCamera;
236 unsigned int Stage::GetLayerCount() const
238 return mLayerList->GetLayerCount();
241 Dali::Layer Stage::GetLayer( unsigned int depth ) const
243 return Dali::Layer(mLayerList->GetLayer( depth ));
246 Dali::Layer Stage::GetRootLayer() const
248 return Dali::Layer( mRootLayer.Get() );
251 LayerList& Stage::GetLayerList()
256 Integration::SystemOverlay& Stage::GetSystemOverlay()
258 // Lazily create system-level if requested
259 if( !mSystemOverlay )
261 mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
262 DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
264 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
267 return *mSystemOverlay;
270 SystemOverlay* Stage::GetSystemOverlayInternal()
272 SystemOverlay* overlay( NULL );
276 overlay = mSystemOverlay->GetImpl();
282 void Stage::SetViewMode( ViewMode viewMode )
284 if( mViewMode != viewMode )
286 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
288 if( mViewMode == MONO )
290 mDefaultCamera->SetOrientation( Dali::ANGLE_180, Vector3::YAXIS );
291 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
293 //Create camera and RenderTask for left eye
294 mLeftCamera = CameraActor::New( Size::ZERO );
295 mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
296 mDefaultCamera->Add( *mLeftCamera.Get() );
297 mLeftRenderTask = mRenderTaskList->CreateTask();
298 mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
299 mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
301 //Create camera and RenderTask for right eye
302 mRightCamera = CameraActor::New( Size::ZERO );
303 mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
304 mDefaultCamera->Add( *mRightCamera.Get() );
305 mRightRenderTask = mRenderTaskList->CreateTask();
306 mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
308 mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
309 mRightCamera->SetType( Dali::Camera::FREE_LOOK );
313 mViewMode = viewMode;
319 // delete extra stereoscopic render tasks and cameras
320 mRenderTaskList->RemoveTask( mLeftRenderTask );
321 mDefaultCamera->Remove( *mLeftCamera.Get() );
322 mLeftRenderTask.Reset();
324 mRenderTaskList->RemoveTask( mRightRenderTask );
325 mDefaultCamera->Remove( *mRightCamera.Get() );
326 mRightRenderTask.Reset();
327 mRightCamera.Reset();
328 mDefaultCamera->SetOrientation( Dali::ANGLE_0, Vector3::YAXIS );
329 mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
330 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
334 case STEREO_HORIZONTAL:
336 //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
337 //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
339 //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
340 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
342 //Calculate aspect ratio
343 float aspect = mSize.width / (mSize.height * 0.5f);
345 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
346 mLeftCamera->SetAspectRatio( aspect );
348 mLeftCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
349 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
350 mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
352 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
353 mRightCamera->SetAspectRatio( aspect );
354 mRightCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
355 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
356 mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
360 case STEREO_VERTICAL:
362 //Calculate separation in pixels along horizontal axis
363 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
365 //Recalculate fov based on viewport size
366 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
368 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
369 mLeftCamera->SetFieldOfView( fov );
370 mLeftCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
371 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
372 mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
374 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
375 mRightCamera->SetFieldOfView( fov );
376 mRightCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
377 mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
378 mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
382 case STEREO_INTERLACED:
390 ViewMode Stage::GetViewMode() const
395 void Stage::SetStereoBase( float stereoBase )
397 if( ! Equals( mStereoBase, stereoBase ) )
399 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)", mStereoBase, stereoBase );
400 mStereoBase = stereoBase;
404 case STEREO_HORIZONTAL:
406 stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
407 float aspect = mSize.width / (mSize.height * 0.5f);
409 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
410 mLeftCamera->SetAspectRatio( aspect );
411 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
413 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
414 mRightCamera->SetAspectRatio( aspect );
415 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
419 case STEREO_VERTICAL:
421 stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
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->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
428 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
429 mRightCamera->SetFieldOfView( fov );
430 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
440 float Stage::GetStereoBase() const
445 void Stage::SetBackgroundColor(Vector4 color)
447 // Cache for public GetBackgroundColor()
448 mBackgroundColor = color;
450 // Send message to change color in next frame
451 SetBackgroundColorMessage( mUpdateManager, color );
454 Vector4 Stage::GetBackgroundColor() const
456 return mBackgroundColor;
459 Vector2 Stage::GetDpi() const
464 void Stage::SetDpi(Vector2 dpi)
469 void Stage::KeepRendering( float durationSeconds )
471 // Send message to keep rendering
472 KeepRenderingMessage( mUpdateManager, durationSeconds );
475 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
477 bool connected( true );
478 Stage* stage = dynamic_cast<Stage*>(object);
480 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
482 stage->KeyEventSignal().Connect( tracker, functor );
484 else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
486 stage->EventProcessingFinishedSignal().Connect( tracker, functor );
488 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
490 stage->TouchedSignal().Connect( tracker, functor );
492 else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
494 stage->WheelEventSignal().Connect( tracker, functor );
496 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
498 stage->ContextLostSignal().Connect( tracker, functor );
500 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
502 stage->ContextRegainedSignal().Connect( tracker, functor );
504 else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
506 stage->SceneCreatedSignal().Connect( tracker, functor );
510 // signalName does not match any signal
517 void Stage::EmitKeyEventSignal(const KeyEvent& event)
519 // Emit the key event signal when no actor in the stage has gained the key input focus
521 mKeyEventSignal.Emit( event );
524 void Stage::EmitEventProcessingFinishedSignal()
526 mEventProcessingFinishedSignal.Emit();
529 void Stage::EmitTouchedSignal( const TouchEvent& touch )
531 mTouchedSignal.Emit( touch );
534 void Stage::EmitWheelEventSignal(const WheelEvent& event)
536 // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
538 mWheelEventSignal.Emit( event );
541 void Stage::EmitSceneCreatedSignal()
543 mSceneCreatedSignal.Emit();
546 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
548 return mKeyEventSignal;
551 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
553 return mEventProcessingFinishedSignal;
556 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
558 return mTouchedSignal;
561 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
563 return mWheelEventSignal;
566 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
568 return mContextLostSignal;
571 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
573 return mContextRegainedSignal;
576 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
578 return mSceneCreatedSignal;
581 void Stage::NotifyContextLost()
583 mContextLostSignal.Emit();
586 void Stage::NotifyContextRegained()
588 mContextRegainedSignal.Emit();
591 Stage::Stage( AnimationPlaylist& playlist,
592 PropertyNotificationManager& propertyNotificationManager,
593 SceneGraph::UpdateManager& updateManager,
594 NotificationManager& notificationManager )
595 : mAnimationPlaylist( playlist ),
596 mPropertyNotificationManager(propertyNotificationManager),
597 mUpdateManager(updateManager),
598 mNotificationManager(notificationManager),
599 mSize(Vector2::ZERO),
600 mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
602 mStereoBase( DEFAULT_STEREO_BASE ),
607 SceneGraph::UpdateManager& Stage::GetUpdateManager()
609 return mUpdateManager;
612 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
614 return mUpdateManager.ReserveMessageSlot( size, updateScene );
617 BufferIndex Stage::GetEventBufferIndex() const
619 return mUpdateManager.GetEventBufferIndex();
624 delete mSystemOverlay;
626 mObjectRegistry.Reset();
629 } // namespace Internal