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_KEY_EVENT_GENERATED = "keyEventGenerated";
60 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "eventProcessingFinished";
61 const char* const SIGNAL_TOUCHED = "touched";
62 const char* const SIGNAL_TOUCH = "touch";
63 const char* const SIGNAL_WHEEL_EVENT = "wheelEvent";
64 const char* const SIGNAL_CONTEXT_LOST = "contextLost";
65 const char* const SIGNAL_CONTEXT_REGAINED = "contextRegained";
66 const char* const SIGNAL_SCENE_CREATED = "sceneCreated";
68 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
70 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT, &Stage::DoConnectSignal );
71 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
72 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED, &Stage::DoConnectSignal );
73 SignalConnectorType signalConnector4( mType, SIGNAL_WHEEL_EVENT, &Stage::DoConnectSignal );
74 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_LOST, &Stage::DoConnectSignal );
75 SignalConnectorType signalConnector6( mType, SIGNAL_CONTEXT_REGAINED, &Stage::DoConnectSignal );
76 SignalConnectorType signalConnector7( mType, SIGNAL_SCENE_CREATED, &Stage::DoConnectSignal );
77 SignalConnectorType signalConnector8( mType, SIGNAL_KEY_EVENT_GENERATED, &Stage::DoConnectSignal );
78 SignalConnectorType signalConnector9( mType, SIGNAL_TOUCH, &Stage::DoConnectSignal );
80 } // unnamed namespace
82 StagePtr Stage::New( AnimationPlaylist& playlist,
83 PropertyNotificationManager& propertyNotificationManager,
84 SceneGraph::UpdateManager& updateManager,
85 NotificationManager& notificationManager )
87 return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager ) );
90 void Stage::Initialize()
92 mObjectRegistry = ObjectRegistry::New();
94 // Create the ordered list of layers
95 mLayerList = LayerList::New( mUpdateManager, false/*not system-level*/ );
97 // The stage owns the default layer
98 mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager, false/*not system-level*/ );
99 mRootLayer->SetName("RootLayer");
100 // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
101 // This stops actors parented to the stage having their relayout requests propagating
102 // up to the root layer, and down through other children unnecessarily.
103 mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
105 // Create the default camera actor first; this is needed by the RenderTaskList
106 CreateDefaultCameraActor();
108 // Create the list of render-tasks
109 mRenderTaskList = RenderTaskList::New( *this, *this, false/*not system-level*/ );
111 // Create the default render-task
112 Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
115 void Stage::Uninitialize()
117 // Remove actors added to SystemOverlay
118 delete mSystemOverlay;
119 mSystemOverlay = NULL;
123 // its enough to release the handle so the object is released
124 // don't need to remove it from root actor as root actor will delete the object
125 mDefaultCamera.Reset();
130 // we are closing down so just delete the root, no point emit disconnect
131 // signals or send messages to update
136 StagePtr Stage::GetCurrent()
138 StagePtr stage( NULL );
139 // no checking in this version
140 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
143 stage = tls->GetCurrentStage();
148 bool Stage::IsInstalled()
150 return ThreadLocalStorage::Created();
153 ObjectRegistry& Stage::GetObjectRegistry()
155 return *mObjectRegistry;
158 void Stage::RegisterObject( Dali::BaseObject* object )
160 mObjectRegistry->RegisterObject( object );
163 void Stage::UnregisterObject( Dali::BaseObject* object )
165 mObjectRegistry->UnregisterObject( object );
168 Layer& Stage::GetRootActor()
173 AnimationPlaylist& Stage::GetAnimationPlaylist()
175 return mAnimationPlaylist;
178 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
180 return mPropertyNotificationManager;
183 void Stage::Add( Actor& actor )
185 mRootLayer->Add( actor );
188 void Stage::Remove( Actor& actor )
190 mRootLayer->Remove( actor );
193 void Stage::SurfaceResized(float width, float height)
195 mSurfaceSize.width = width;
196 mSurfaceSize.height = height;
198 // Internally we want to report the actual size of the stage.
200 mSize.height = height - mTopMargin;
202 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
203 mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
205 // Adjust the camera height to allow for top-margin
206 SetDefaultCameraPosition();
208 mRootLayer->SetSize( mSize.width, mSize.height );
210 // Repeat for SystemOverlay actors
213 // Note that the SystemOverlay has a separate camera, configured for the full surface-size.
214 // This will remain unaffected by changes in SetDefaultCameraPosition()
215 mSystemOverlay->GetImpl()->SetSize( width, height );
218 SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
220 // if single render task to screen then set its viewport parameters
221 if( 1 == mRenderTaskList->GetTaskCount() )
223 Dali::RenderTask mDefaultRenderTask = mRenderTaskList->GetTask(0);
225 if(!mDefaultRenderTask.GetTargetFrameBuffer())
227 mDefaultRenderTask.SetViewport( Viewport(0, 0, width, height) );
233 Vector2 Stage::GetSize() const
238 void Stage::SetTopMargin( unsigned int margin )
240 if (mTopMargin == margin)
246 mSize.width = mSurfaceSize.width;
247 mSize.height = mSurfaceSize.height - mTopMargin;
249 // Adjust the camera height to allow for top-margin
250 SetDefaultCameraPosition();
252 mRootLayer->SetSize( mSize.width, mSize.height );
255 RenderTaskList& Stage::GetRenderTaskList() const
257 return *mRenderTaskList;
260 void Stage::CreateDefaultCameraActor()
262 // The default camera attributes and position is such that
263 // children of the default layer, can be positioned at (0,0) and
264 // be at the top-left of the viewport.
265 mDefaultCamera = CameraActor::New( Size::ZERO );
266 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
267 Add(*(mDefaultCamera.Get()));
270 void Stage::SetDefaultCameraPosition()
272 mDefaultCamera->SetY( -(static_cast<float>(mTopMargin) * 0.5f) );
275 Actor& Stage::GetDefaultRootActor()
280 CameraActor& Stage::GetDefaultCameraActor()
282 return *mDefaultCamera;
285 unsigned int Stage::GetLayerCount() const
287 return mLayerList->GetLayerCount();
290 Dali::Layer Stage::GetLayer( unsigned int depth ) const
292 return Dali::Layer(mLayerList->GetLayer( depth ));
295 Dali::Layer Stage::GetRootLayer() const
297 return Dali::Layer( mRootLayer.Get() );
300 LayerList& Stage::GetLayerList()
305 Integration::SystemOverlay& Stage::GetSystemOverlay()
307 // Lazily create system-level if requested
308 if( !mSystemOverlay )
310 mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
311 DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
313 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
316 return *mSystemOverlay;
319 SystemOverlay* Stage::GetSystemOverlayInternal()
321 SystemOverlay* overlay( NULL );
325 overlay = mSystemOverlay->GetImpl();
331 void Stage::SetViewMode( ViewMode viewMode )
333 if( mViewMode != viewMode )
335 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
337 if( mViewMode == MONO )
339 mDefaultCamera->SetOrientation( Dali::ANGLE_180, Vector3::YAXIS );
340 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
342 //Create camera and RenderTask for left eye
343 mLeftCamera = CameraActor::New( Size::ZERO );
344 mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
345 mDefaultCamera->Add( *mLeftCamera.Get() );
346 mLeftRenderTask = mRenderTaskList->CreateTask();
347 mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
348 mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
350 //Create camera and RenderTask for right eye
351 mRightCamera = CameraActor::New( Size::ZERO );
352 mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
353 mDefaultCamera->Add( *mRightCamera.Get() );
354 mRightRenderTask = mRenderTaskList->CreateTask();
355 mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
357 mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
358 mRightCamera->SetType( Dali::Camera::FREE_LOOK );
362 mViewMode = viewMode;
368 // delete extra stereoscopic render tasks and cameras
369 mRenderTaskList->RemoveTask( mLeftRenderTask );
370 mDefaultCamera->Remove( *mLeftCamera.Get() );
371 mLeftRenderTask.Reset();
373 mRenderTaskList->RemoveTask( mRightRenderTask );
374 mDefaultCamera->Remove( *mRightCamera.Get() );
375 mRightRenderTask.Reset();
376 mRightCamera.Reset();
377 mDefaultCamera->SetOrientation( Dali::ANGLE_0, Vector3::YAXIS );
378 mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
379 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
383 case STEREO_HORIZONTAL:
385 //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
386 //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
388 //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
389 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
391 //Calculate aspect ratio
392 float aspect = mSize.width / (mSize.height * 0.5f);
394 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
395 mLeftCamera->SetAspectRatio( aspect );
397 mLeftCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
398 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
399 mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
401 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
402 mRightCamera->SetAspectRatio( aspect );
403 mRightCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
404 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
405 mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
409 case STEREO_VERTICAL:
411 //Calculate separation in pixels along horizontal axis
412 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
414 //Recalculate fov based on viewport size
415 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
417 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
418 mLeftCamera->SetFieldOfView( fov );
419 mLeftCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
420 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
421 mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
423 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
424 mRightCamera->SetFieldOfView( fov );
425 mRightCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
426 mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
427 mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
431 case STEREO_INTERLACED:
439 ViewMode Stage::GetViewMode() const
444 void Stage::SetStereoBase( float stereoBase )
446 if( ! Equals( mStereoBase, stereoBase ) )
448 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)\n", mStereoBase, stereoBase );
449 mStereoBase = stereoBase;
453 case STEREO_HORIZONTAL:
455 stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
456 float aspect = mSize.width / (mSize.height * 0.5f);
458 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
459 mLeftCamera->SetAspectRatio( aspect );
460 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
462 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
463 mRightCamera->SetAspectRatio( aspect );
464 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
468 case STEREO_VERTICAL:
470 stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
471 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
473 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
474 mLeftCamera->SetFieldOfView( fov );
475 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
477 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
478 mRightCamera->SetFieldOfView( fov );
479 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
489 float Stage::GetStereoBase() const
494 void Stage::SetBackgroundColor(Vector4 color)
496 // Cache for public GetBackgroundColor()
497 mBackgroundColor = color;
499 // Send message to change color in next frame
500 SetBackgroundColorMessage( mUpdateManager, color );
503 Vector4 Stage::GetBackgroundColor() const
505 return mBackgroundColor;
508 Vector2 Stage::GetDpi() const
513 void Stage::SetDpi(Vector2 dpi)
518 void Stage::KeepRendering( float durationSeconds )
520 // Send message to keep rendering
521 KeepRenderingMessage( mUpdateManager, durationSeconds );
524 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
526 bool connected( true );
527 Stage* stage = static_cast< Stage* >(object); // TypeRegistry guarantees that this is the correct type.
529 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
531 stage->KeyEventSignal().Connect( tracker, functor );
533 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT_GENERATED ) )
535 stage->KeyEventGeneratedSignal().Connect( tracker, functor );
537 else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
539 stage->EventProcessingFinishedSignal().Connect( tracker, functor );
541 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
543 stage->TouchedSignal().Connect( tracker, functor );
545 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCH ) )
547 stage->TouchSignal().Connect( tracker, functor );
549 else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
551 stage->WheelEventSignal().Connect( tracker, functor );
553 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
555 stage->ContextLostSignal().Connect( tracker, functor );
557 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
559 stage->ContextRegainedSignal().Connect( tracker, functor );
561 else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
563 stage->SceneCreatedSignal().Connect( tracker, functor );
567 // signalName does not match any signal
574 void Stage::EmitKeyEventSignal(const KeyEvent& event)
576 // Emit the key event signal when no actor in the stage has gained the key input focus
578 mKeyEventSignal.Emit( event );
581 bool Stage::EmitKeyEventGeneratedSignal(const KeyEvent& event)
583 // Emit the KeyEventGenerated signal when KeyEvent is generated
585 return mKeyEventGeneratedSignal.Emit( event );
588 void Stage::EmitEventProcessingFinishedSignal()
590 mEventProcessingFinishedSignal.Emit();
593 void Stage::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
595 mTouchedSignal.Emit( touchEvent );
596 mTouchSignal.Emit( touch );
599 void Stage::EmitWheelEventSignal(const WheelEvent& event)
601 // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
603 mWheelEventSignal.Emit( event );
606 void Stage::EmitSceneCreatedSignal()
608 mSceneCreatedSignal.Emit();
611 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
613 return mKeyEventSignal;
616 Dali::DevelStage::KeyEventGeneratedSignalType& Stage::KeyEventGeneratedSignal()
618 return mKeyEventGeneratedSignal;
621 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
623 return mEventProcessingFinishedSignal;
626 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
628 DALI_LOG_WARNING( "Deprecated. Use TouchSignal() instead.\n" );
629 return mTouchedSignal;
632 Dali::Stage::TouchSignalType& Stage::TouchSignal()
637 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
639 return mWheelEventSignal;
642 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
644 return mContextLostSignal;
647 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
649 return mContextRegainedSignal;
652 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
654 return mSceneCreatedSignal;
657 void Stage::NotifyContextLost()
659 mContextLostSignal.Emit();
662 void Stage::NotifyContextRegained()
664 mContextRegainedSignal.Emit();
667 Stage::Stage( AnimationPlaylist& playlist,
668 PropertyNotificationManager& propertyNotificationManager,
669 SceneGraph::UpdateManager& updateManager,
670 NotificationManager& notificationManager )
671 : mAnimationPlaylist( playlist ),
672 mPropertyNotificationManager(propertyNotificationManager),
673 mUpdateManager(updateManager),
674 mNotificationManager(notificationManager),
675 mSize(Vector2::ZERO),
676 mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
678 mStereoBase( DEFAULT_STEREO_BASE ),
684 SceneGraph::UpdateManager& Stage::GetUpdateManager()
686 return mUpdateManager;
689 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
691 return mUpdateManager.ReserveMessageSlot( size, updateScene );
694 BufferIndex Stage::GetEventBufferIndex() const
696 return mUpdateManager.GetEventBufferIndex();
701 delete mSystemOverlay;
703 mObjectRegistry.Reset();
706 } // namespace Internal