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/object/type-registry.h>
40 #include <dali/public-api/render-tasks/render-task-list.h>
42 using Dali::Internal::SceneGraph::Node;
53 const float DEFAULT_STEREO_BASE( 65.0f );
57 const char* const SIGNAL_KEY_EVENT = "keyEvent";
58 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "eventProcessingFinished";
59 const char* const SIGNAL_TOUCHED = "touched";
60 const char* const SIGNAL_WHEEL_EVENT = "wheelEvent";
61 const char* const SIGNAL_CONTEXT_LOST = "contextLost";
62 const char* const SIGNAL_CONTEXT_REGAINED = "contextRegained";
63 const char* const SIGNAL_SCENE_CREATED = "sceneCreated";
65 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
67 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT, &Stage::DoConnectSignal );
68 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
69 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED, &Stage::DoConnectSignal );
70 SignalConnectorType signalConnector4( mType, SIGNAL_WHEEL_EVENT, &Stage::DoConnectSignal );
71 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_LOST, &Stage::DoConnectSignal );
72 SignalConnectorType signalConnector6( mType, SIGNAL_CONTEXT_REGAINED, &Stage::DoConnectSignal );
73 SignalConnectorType signalConnector7( mType, SIGNAL_SCENE_CREATED, &Stage::DoConnectSignal );
75 } // unnamed namespace
77 StagePtr Stage::New( AnimationPlaylist& playlist,
78 PropertyNotificationManager& propertyNotificationManager,
79 SceneGraph::UpdateManager& updateManager,
80 NotificationManager& notificationManager )
82 return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager ) );
85 void Stage::Initialize()
87 mObjectRegistry = ObjectRegistry::New();
89 // Create the ordered list of layers
90 mLayerList = LayerList::New( mUpdateManager, false/*not system-level*/ );
92 // The stage owns the default layer
93 mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager, false/*not system-level*/ );
94 mRootLayer->SetName("RootLayer");
95 // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
96 // This stops actors parented to the stage having their relayout requests propagating
97 // up to the root layer, and down through other children unnecessarily.
98 mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
100 // Create the default camera actor first; this is needed by the RenderTaskList
101 CreateDefaultCameraActor();
103 // Create the list of render-tasks
104 mRenderTaskList = RenderTaskList::New( *this, *this, false/*not system-level*/ );
106 // Create the default render-task
107 Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
110 void Stage::Uninitialize()
112 // Remove actors added to SystemOverlay
113 delete mSystemOverlay;
114 mSystemOverlay = NULL;
118 // its enough to release the handle so the object is released
119 // don't need to remove it from root actor as root actor will delete the object
120 mDefaultCamera.Reset();
125 // we are closing down so just delete the root, no point emit disconnect
126 // signals or send messages to update
131 StagePtr Stage::GetCurrent()
133 StagePtr stage( NULL );
134 // no checking in this version
135 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
138 stage = tls->GetCurrentStage();
143 bool Stage::IsInstalled()
145 return ThreadLocalStorage::Created();
148 ObjectRegistry& Stage::GetObjectRegistry()
150 return *mObjectRegistry;
153 void Stage::RegisterObject( Dali::BaseObject* object )
155 mObjectRegistry->RegisterObject( object );
158 void Stage::UnregisterObject( Dali::BaseObject* object )
160 mObjectRegistry->UnregisterObject( object );
163 Layer& Stage::GetRootActor()
168 AnimationPlaylist& Stage::GetAnimationPlaylist()
170 return mAnimationPlaylist;
173 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
175 return mPropertyNotificationManager;
178 void Stage::Add( Actor& actor )
180 mRootLayer->Add( actor );
183 void Stage::Remove( Actor& actor )
185 mRootLayer->Remove( actor );
188 void Stage::SetSize(float width, float height)
190 // Internally we want to report the actual size of the stage.
192 mSize.height = height;
194 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
195 mDefaultCamera->SetPerspectiveProjection( mSize );
197 // The depth of the stage gets set to the maximun of these values
198 mRootLayer->SetSize( mSize );
200 // Repeat for SystemOverlay actors
203 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
206 SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
208 // if single render task to screen then set its viewport parameters
209 if( 1 == mRenderTaskList->GetTaskCount() )
211 Dali::RenderTask mDefaultRenderTask = mRenderTaskList->GetTask(0);
213 if(!mDefaultRenderTask.GetTargetFrameBuffer())
215 mDefaultRenderTask.SetViewport( Viewport(0, 0, width, height) );
221 Vector2 Stage::GetSize() const
226 RenderTaskList& Stage::GetRenderTaskList() const
228 return *mRenderTaskList;
231 void Stage::CreateDefaultCameraActor()
233 // The default camera attributes and position is such that
234 // children of the default layer, can be positioned at (0,0) and
235 // be at the top-left of the viewport.
236 mDefaultCamera = CameraActor::New( Size::ZERO );
237 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
238 Add(*(mDefaultCamera.Get()));
241 Actor& Stage::GetDefaultRootActor()
246 CameraActor& Stage::GetDefaultCameraActor()
248 return *mDefaultCamera;
251 unsigned int Stage::GetLayerCount() const
253 return mLayerList->GetLayerCount();
256 Dali::Layer Stage::GetLayer( unsigned int depth ) const
258 return Dali::Layer(mLayerList->GetLayer( depth ));
261 Dali::Layer Stage::GetRootLayer() const
263 return Dali::Layer( mRootLayer.Get() );
266 LayerList& Stage::GetLayerList()
271 Integration::SystemOverlay& Stage::GetSystemOverlay()
273 // Lazily create system-level if requested
274 if( !mSystemOverlay )
276 mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
277 DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
279 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
282 return *mSystemOverlay;
285 SystemOverlay* Stage::GetSystemOverlayInternal()
287 SystemOverlay* overlay( NULL );
291 overlay = mSystemOverlay->GetImpl();
297 void Stage::SetViewMode( ViewMode viewMode )
299 if( mViewMode != viewMode )
301 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
303 if( mViewMode == MONO )
305 mDefaultCamera->SetOrientation( Dali::ANGLE_180, Vector3::YAXIS );
306 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
308 //Create camera and RenderTask for left eye
309 mLeftCamera = CameraActor::New( Size::ZERO );
310 mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
311 mDefaultCamera->Add( *mLeftCamera.Get() );
312 mLeftRenderTask = mRenderTaskList->CreateTask();
313 mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
314 mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
316 //Create camera and RenderTask for right eye
317 mRightCamera = CameraActor::New( Size::ZERO );
318 mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
319 mDefaultCamera->Add( *mRightCamera.Get() );
320 mRightRenderTask = mRenderTaskList->CreateTask();
321 mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
323 mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
324 mRightCamera->SetType( Dali::Camera::FREE_LOOK );
328 mViewMode = viewMode;
334 // delete extra stereoscopic render tasks and cameras
335 mRenderTaskList->RemoveTask( mLeftRenderTask );
336 mDefaultCamera->Remove( *mLeftCamera.Get() );
337 mLeftRenderTask.Reset();
339 mRenderTaskList->RemoveTask( mRightRenderTask );
340 mDefaultCamera->Remove( *mRightCamera.Get() );
341 mRightRenderTask.Reset();
342 mRightCamera.Reset();
343 mDefaultCamera->SetOrientation( Dali::ANGLE_0, Vector3::YAXIS );
344 mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
345 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
349 case STEREO_HORIZONTAL:
351 //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
352 //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
354 //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
355 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
357 //Calculate aspect ratio
358 float aspect = mSize.width / (mSize.height * 0.5f);
360 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
361 mLeftCamera->SetAspectRatio( aspect );
363 mLeftCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
364 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
365 mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
367 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
368 mRightCamera->SetAspectRatio( aspect );
369 mRightCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
370 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
371 mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
375 case STEREO_VERTICAL:
377 //Calculate separation in pixels along horizontal axis
378 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
380 //Recalculate fov based on viewport size
381 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
383 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
384 mLeftCamera->SetFieldOfView( fov );
385 mLeftCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
386 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
387 mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
389 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
390 mRightCamera->SetFieldOfView( fov );
391 mRightCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
392 mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
393 mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
397 case STEREO_INTERLACED:
405 ViewMode Stage::GetViewMode() const
410 void Stage::SetStereoBase( float stereoBase )
412 if( ! Equals( mStereoBase, stereoBase ) )
414 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)", mStereoBase, stereoBase );
415 mStereoBase = stereoBase;
419 case STEREO_HORIZONTAL:
421 stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
422 float aspect = mSize.width / (mSize.height * 0.5f);
424 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
425 mLeftCamera->SetAspectRatio( aspect );
426 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
428 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
429 mRightCamera->SetAspectRatio( aspect );
430 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
434 case STEREO_VERTICAL:
436 stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
437 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
439 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
440 mLeftCamera->SetFieldOfView( fov );
441 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
443 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
444 mRightCamera->SetFieldOfView( fov );
445 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
455 float Stage::GetStereoBase() const
460 void Stage::SetBackgroundColor(Vector4 color)
462 // Cache for public GetBackgroundColor()
463 mBackgroundColor = color;
465 // Send message to change color in next frame
466 SetBackgroundColorMessage( mUpdateManager, color );
469 Vector4 Stage::GetBackgroundColor() const
471 return mBackgroundColor;
474 Vector2 Stage::GetDpi() const
479 void Stage::SetDpi(Vector2 dpi)
484 void Stage::KeepRendering( float durationSeconds )
486 // Send message to keep rendering
487 KeepRenderingMessage( mUpdateManager, durationSeconds );
490 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
492 bool connected( true );
493 Stage* stage = dynamic_cast<Stage*>(object);
495 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
497 stage->KeyEventSignal().Connect( tracker, functor );
499 else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
501 stage->EventProcessingFinishedSignal().Connect( tracker, functor );
503 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
505 stage->TouchedSignal().Connect( tracker, functor );
507 else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
509 stage->WheelEventSignal().Connect( tracker, functor );
511 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
513 stage->ContextLostSignal().Connect( tracker, functor );
515 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
517 stage->ContextRegainedSignal().Connect( tracker, functor );
519 else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
521 stage->SceneCreatedSignal().Connect( tracker, functor );
525 // signalName does not match any signal
532 void Stage::EmitKeyEventSignal(const KeyEvent& event)
534 // Emit the key event signal when no actor in the stage has gained the key input focus
536 mKeyEventSignal.Emit( event );
539 void Stage::EmitEventProcessingFinishedSignal()
541 mEventProcessingFinishedSignal.Emit();
544 void Stage::EmitTouchedSignal( const TouchEvent& touch )
546 mTouchedSignal.Emit( touch );
549 void Stage::EmitWheelEventSignal(const WheelEvent& event)
551 // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
553 mWheelEventSignal.Emit( event );
556 void Stage::EmitSceneCreatedSignal()
558 mSceneCreatedSignal.Emit();
561 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
563 return mKeyEventSignal;
566 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
568 return mEventProcessingFinishedSignal;
571 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
573 return mTouchedSignal;
576 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
578 return mWheelEventSignal;
581 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
583 return mContextLostSignal;
586 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
588 return mContextRegainedSignal;
591 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
593 return mSceneCreatedSignal;
596 void Stage::NotifyContextLost()
598 mContextLostSignal.Emit();
601 void Stage::NotifyContextRegained()
603 mContextRegainedSignal.Emit();
606 Stage::Stage( AnimationPlaylist& playlist,
607 PropertyNotificationManager& propertyNotificationManager,
608 SceneGraph::UpdateManager& updateManager,
609 NotificationManager& notificationManager )
610 : mAnimationPlaylist( playlist ),
611 mPropertyNotificationManager(propertyNotificationManager),
612 mUpdateManager(updateManager),
613 mNotificationManager(notificationManager),
614 mSize(Vector2::ZERO),
615 mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
617 mStereoBase( DEFAULT_STEREO_BASE ),
622 SceneGraph::UpdateManager& Stage::GetUpdateManager()
624 return mUpdateManager;
627 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
629 return mUpdateManager.ReserveMessageSlot( size, updateScene );
632 BufferIndex Stage::GetEventBufferIndex() const
634 return mUpdateManager.GetEventBufferIndex();
639 delete mSystemOverlay;
641 mObjectRegistry.Reset();
644 } // namespace Internal