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/events/touch-data.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 = "keyEvent";
59 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "eventProcessingFinished";
60 const char* const SIGNAL_TOUCHED = "touched";
61 const char* const SIGNAL_WHEEL_EVENT = "wheelEvent";
62 const char* const SIGNAL_CONTEXT_LOST = "contextLost";
63 const char* const SIGNAL_CONTEXT_REGAINED = "contextRegained";
64 const char* const SIGNAL_SCENE_CREATED = "sceneCreated";
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::SurfaceResized(float width, float height)
191 mSurfaceSize.width = width;
192 mSurfaceSize.height = height;
194 // Internally we want to report the actual size of the stage.
196 mSize.height = height - mTopMargin;
198 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
199 mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
201 // Adjust the camera height to allow for top-margin
202 SetDefaultCameraPosition();
204 mRootLayer->SetSize( mSize.width, mSize.height );
206 // Repeat for SystemOverlay actors
209 // Note that the SystemOverlay has a separate camera, configured for the full surface-size.
210 // This will remain unaffected by changes in SetDefaultCameraPosition()
211 mSystemOverlay->GetImpl()->SetSize( width, height );
214 SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
216 // if single render task to screen then set its viewport parameters
217 if( 1 == mRenderTaskList->GetTaskCount() )
219 Dali::RenderTask mDefaultRenderTask = mRenderTaskList->GetTask(0);
221 if(!mDefaultRenderTask.GetTargetFrameBuffer())
223 mDefaultRenderTask.SetViewport( Viewport(0, 0, width, height) );
229 Vector2 Stage::GetSize() const
234 void Stage::SetTopMargin( unsigned int margin )
236 if (mTopMargin == margin)
242 mSize.width = mSurfaceSize.width;
243 mSize.height = mSurfaceSize.height - mTopMargin;
245 // Adjust the camera height to allow for top-margin
246 SetDefaultCameraPosition();
248 mRootLayer->SetSize( mSize.width, mSize.height );
251 RenderTaskList& Stage::GetRenderTaskList() const
253 return *mRenderTaskList;
256 void Stage::CreateDefaultCameraActor()
258 // The default camera attributes and position is such that
259 // children of the default layer, can be positioned at (0,0) and
260 // be at the top-left of the viewport.
261 mDefaultCamera = CameraActor::New( Size::ZERO );
262 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
263 Add(*(mDefaultCamera.Get()));
266 void Stage::SetDefaultCameraPosition()
268 mDefaultCamera->SetY( -(static_cast<float>(mTopMargin) * 0.5f) );
271 Actor& Stage::GetDefaultRootActor()
276 CameraActor& Stage::GetDefaultCameraActor()
278 return *mDefaultCamera;
281 unsigned int Stage::GetLayerCount() const
283 return mLayerList->GetLayerCount();
286 Dali::Layer Stage::GetLayer( unsigned int depth ) const
288 return Dali::Layer(mLayerList->GetLayer( depth ));
291 Dali::Layer Stage::GetRootLayer() const
293 return Dali::Layer( mRootLayer.Get() );
296 LayerList& Stage::GetLayerList()
301 Integration::SystemOverlay& Stage::GetSystemOverlay()
303 // Lazily create system-level if requested
304 if( !mSystemOverlay )
306 mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
307 DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
309 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
312 return *mSystemOverlay;
315 SystemOverlay* Stage::GetSystemOverlayInternal()
317 SystemOverlay* overlay( NULL );
321 overlay = mSystemOverlay->GetImpl();
327 void Stage::SetViewMode( ViewMode viewMode )
329 if( mViewMode != viewMode )
331 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
333 if( mViewMode == MONO )
335 mDefaultCamera->SetOrientation( Dali::ANGLE_180, Vector3::YAXIS );
336 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
338 //Create camera and RenderTask for left eye
339 mLeftCamera = CameraActor::New( Size::ZERO );
340 mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
341 mDefaultCamera->Add( *mLeftCamera.Get() );
342 mLeftRenderTask = mRenderTaskList->CreateTask();
343 mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
344 mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
346 //Create camera and RenderTask for right eye
347 mRightCamera = CameraActor::New( Size::ZERO );
348 mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
349 mDefaultCamera->Add( *mRightCamera.Get() );
350 mRightRenderTask = mRenderTaskList->CreateTask();
351 mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
353 mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
354 mRightCamera->SetType( Dali::Camera::FREE_LOOK );
358 mViewMode = viewMode;
364 // delete extra stereoscopic render tasks and cameras
365 mRenderTaskList->RemoveTask( mLeftRenderTask );
366 mDefaultCamera->Remove( *mLeftCamera.Get() );
367 mLeftRenderTask.Reset();
369 mRenderTaskList->RemoveTask( mRightRenderTask );
370 mDefaultCamera->Remove( *mRightCamera.Get() );
371 mRightRenderTask.Reset();
372 mRightCamera.Reset();
373 mDefaultCamera->SetOrientation( Dali::ANGLE_0, Vector3::YAXIS );
374 mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
375 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
379 case STEREO_HORIZONTAL:
381 //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
382 //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
384 //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
385 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
387 //Calculate aspect ratio
388 float aspect = mSize.width / (mSize.height * 0.5f);
390 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
391 mLeftCamera->SetAspectRatio( aspect );
393 mLeftCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
394 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
395 mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
397 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
398 mRightCamera->SetAspectRatio( aspect );
399 mRightCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
400 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
401 mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
405 case STEREO_VERTICAL:
407 //Calculate separation in pixels along horizontal axis
408 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
410 //Recalculate fov based on viewport size
411 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
413 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
414 mLeftCamera->SetFieldOfView( fov );
415 mLeftCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
416 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
417 mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
419 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
420 mRightCamera->SetFieldOfView( fov );
421 mRightCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
422 mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
423 mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
427 case STEREO_INTERLACED:
435 ViewMode Stage::GetViewMode() const
440 void Stage::SetStereoBase( float stereoBase )
442 if( ! Equals( mStereoBase, stereoBase ) )
444 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)\n", mStereoBase, stereoBase );
445 mStereoBase = stereoBase;
449 case STEREO_HORIZONTAL:
451 stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
452 float aspect = mSize.width / (mSize.height * 0.5f);
454 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
455 mLeftCamera->SetAspectRatio( aspect );
456 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
458 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
459 mRightCamera->SetAspectRatio( aspect );
460 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
464 case STEREO_VERTICAL:
466 stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
467 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
469 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
470 mLeftCamera->SetFieldOfView( fov );
471 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
473 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
474 mRightCamera->SetFieldOfView( fov );
475 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
485 float Stage::GetStereoBase() const
490 void Stage::SetBackgroundColor(Vector4 color)
492 // Cache for public GetBackgroundColor()
493 mBackgroundColor = color;
495 // Send message to change color in next frame
496 SetBackgroundColorMessage( mUpdateManager, color );
499 Vector4 Stage::GetBackgroundColor() const
501 return mBackgroundColor;
504 Vector2 Stage::GetDpi() const
509 void Stage::SetDpi(Vector2 dpi)
514 void Stage::KeepRendering( float durationSeconds )
516 // Send message to keep rendering
517 KeepRenderingMessage( mUpdateManager, durationSeconds );
520 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
522 bool connected( true );
523 Stage* stage = dynamic_cast<Stage*>(object);
525 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
527 stage->KeyEventSignal().Connect( tracker, functor );
529 else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
531 stage->EventProcessingFinishedSignal().Connect( tracker, functor );
533 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
535 stage->TouchedSignal().Connect( tracker, functor );
537 else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
539 stage->WheelEventSignal().Connect( tracker, functor );
541 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
543 stage->ContextLostSignal().Connect( tracker, functor );
545 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
547 stage->ContextRegainedSignal().Connect( tracker, functor );
549 else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
551 stage->SceneCreatedSignal().Connect( tracker, functor );
555 // signalName does not match any signal
562 void Stage::EmitKeyEventSignal(const KeyEvent& event)
564 // Emit the key event signal when no actor in the stage has gained the key input focus
566 mKeyEventSignal.Emit( event );
569 void Stage::EmitEventProcessingFinishedSignal()
571 mEventProcessingFinishedSignal.Emit();
574 void Stage::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
576 mTouchedSignal.Emit( touchEvent );
577 mTouchSignal.Emit( touch );
580 void Stage::EmitWheelEventSignal(const WheelEvent& event)
582 // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
584 mWheelEventSignal.Emit( event );
587 void Stage::EmitSceneCreatedSignal()
589 mSceneCreatedSignal.Emit();
592 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
594 return mKeyEventSignal;
597 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
599 return mEventProcessingFinishedSignal;
602 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
604 DALI_LOG_WARNING( "Deprecated. Use TouchSignal() instead.\n" );
605 return mTouchedSignal;
608 Dali::Stage::TouchSignalType& Stage::TouchSignal()
613 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
615 return mWheelEventSignal;
618 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
620 return mContextLostSignal;
623 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
625 return mContextRegainedSignal;
628 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
630 return mSceneCreatedSignal;
633 void Stage::NotifyContextLost()
635 mContextLostSignal.Emit();
638 void Stage::NotifyContextRegained()
640 mContextRegainedSignal.Emit();
643 Stage::Stage( AnimationPlaylist& playlist,
644 PropertyNotificationManager& propertyNotificationManager,
645 SceneGraph::UpdateManager& updateManager,
646 NotificationManager& notificationManager )
647 : mAnimationPlaylist( playlist ),
648 mPropertyNotificationManager(propertyNotificationManager),
649 mUpdateManager(updateManager),
650 mNotificationManager(notificationManager),
651 mSize(Vector2::ZERO),
652 mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
654 mStereoBase( DEFAULT_STEREO_BASE ),
660 SceneGraph::UpdateManager& Stage::GetUpdateManager()
662 return mUpdateManager;
665 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
667 return mUpdateManager.ReserveMessageSlot( size, updateScene );
670 BufferIndex Stage::GetEventBufferIndex() const
672 return mUpdateManager.GetEventBufferIndex();
677 delete mSystemOverlay;
679 mObjectRegistry.Reset();
682 } // namespace Internal