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;
47 #if defined(DEBUG_ENABLED)
48 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_DEPTH_TIMER" );
61 const float DEFAULT_STEREO_BASE( 65.0f );
65 const char* const SIGNAL_KEY_EVENT = "keyEvent";
66 const char* const SIGNAL_KEY_EVENT_GENERATED = "keyEventGenerated";
67 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "eventProcessingFinished";
68 const char* const SIGNAL_TOUCHED = "touched";
69 const char* const SIGNAL_TOUCH = "touch";
70 const char* const SIGNAL_WHEEL_EVENT = "wheelEvent";
71 const char* const SIGNAL_CONTEXT_LOST = "contextLost";
72 const char* const SIGNAL_CONTEXT_REGAINED = "contextRegained";
73 const char* const SIGNAL_SCENE_CREATED = "sceneCreated";
75 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
77 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT, &Stage::DoConnectSignal );
78 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
79 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED, &Stage::DoConnectSignal );
80 SignalConnectorType signalConnector4( mType, SIGNAL_WHEEL_EVENT, &Stage::DoConnectSignal );
81 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_LOST, &Stage::DoConnectSignal );
82 SignalConnectorType signalConnector6( mType, SIGNAL_CONTEXT_REGAINED, &Stage::DoConnectSignal );
83 SignalConnectorType signalConnector7( mType, SIGNAL_SCENE_CREATED, &Stage::DoConnectSignal );
84 SignalConnectorType signalConnector8( mType, SIGNAL_KEY_EVENT_GENERATED, &Stage::DoConnectSignal );
85 SignalConnectorType signalConnector9( mType, SIGNAL_TOUCH, &Stage::DoConnectSignal );
87 } // unnamed namespace
89 StagePtr Stage::New( AnimationPlaylist& playlist,
90 PropertyNotificationManager& propertyNotificationManager,
91 SceneGraph::UpdateManager& updateManager,
92 NotificationManager& notificationManager )
94 return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager ) );
97 void Stage::Initialize()
99 mObjectRegistry = ObjectRegistry::New();
101 // Create the ordered list of layers
102 mLayerList = LayerList::New( mUpdateManager, false/*not system-level*/ );
104 // The stage owns the default layer
105 mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager, false/*not system-level*/ );
106 mRootLayer->SetName("RootLayer");
107 // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
108 // This stops actors parented to the stage having their relayout requests propagating
109 // up to the root layer, and down through other children unnecessarily.
110 mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
112 // Create the default camera actor first; this is needed by the RenderTaskList
113 CreateDefaultCameraActor();
115 // Create the list of render-tasks
116 mRenderTaskList = RenderTaskList::New( *this, *this, false/*not system-level*/ );
118 // Create the default render-task
119 Dali::RenderTask defaultRenderTask = mRenderTaskList->CreateTask();
122 void Stage::Uninitialize()
124 // Remove actors added to SystemOverlay
125 delete mSystemOverlay;
126 mSystemOverlay = NULL;
130 // its enough to release the handle so the object is released
131 // don't need to remove it from root actor as root actor will delete the object
132 mDefaultCamera.Reset();
137 // we are closing down so just delete the root, no point emit disconnect
138 // signals or send messages to update
143 StagePtr Stage::GetCurrent()
145 StagePtr stage( NULL );
146 // no checking in this version
147 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
150 stage = tls->GetCurrentStage();
155 bool Stage::IsInstalled()
157 return ThreadLocalStorage::Created();
160 ObjectRegistry& Stage::GetObjectRegistry()
162 return *mObjectRegistry;
165 void Stage::RegisterObject( Dali::BaseObject* object )
167 mObjectRegistry->RegisterObject( object );
170 void Stage::UnregisterObject( Dali::BaseObject* object )
172 mObjectRegistry->UnregisterObject( object );
175 Layer& Stage::GetRootActor()
180 AnimationPlaylist& Stage::GetAnimationPlaylist()
182 return mAnimationPlaylist;
185 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
187 return mPropertyNotificationManager;
190 void Stage::Add( Actor& actor )
192 mRootLayer->Add( actor );
195 void Stage::Remove( Actor& actor )
197 mRootLayer->Remove( actor );
200 void Stage::SurfaceResized( float width, float height )
202 if( ( fabs( width - mSurfaceSize.width ) > Math::MACHINE_EPSILON_1000 ) || ( fabs( height - mSurfaceSize.height ) > Math::MACHINE_EPSILON_1000 ) )
204 mSurfaceSize.width = width;
205 mSurfaceSize.height = height;
207 // Internally we want to report the actual size of the stage.
209 mSize.height = height - mTopMargin;
211 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
212 mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
214 // Adjust the camera height to allow for top-margin
215 SetDefaultCameraPosition();
217 mRootLayer->SetSize( mSize.width, mSize.height );
219 // Repeat for SystemOverlay actors
222 // Note that the SystemOverlay has a separate camera, configured for the full surface-size.
223 // This will remain unaffected by changes in SetDefaultCameraPosition()
224 mSystemOverlay->GetImpl()->SetSize( width, height );
227 SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int>( 0, 0, width, height ) );
229 // if single render task to screen then set its viewport parameters
230 if( 1 == mRenderTaskList->GetTaskCount() )
232 Dali::RenderTask mDefaultRenderTask = mRenderTaskList->GetTask( 0u );
234 if(!mDefaultRenderTask.GetTargetFrameBuffer())
236 mDefaultRenderTask.SetViewport( Viewport(0, 0, width, height) );
242 Vector2 Stage::GetSize() const
247 void Stage::SetTopMargin( unsigned int margin )
249 if (mTopMargin == margin)
255 mSize.width = mSurfaceSize.width;
256 mSize.height = mSurfaceSize.height - mTopMargin;
258 // Adjust the camera height to allow for top-margin
259 SetDefaultCameraPosition();
261 mRootLayer->SetSize( mSize.width, mSize.height );
264 RenderTaskList& Stage::GetRenderTaskList() const
266 return *mRenderTaskList;
269 void Stage::CreateDefaultCameraActor()
271 // The default camera attributes and position is such that
272 // children of the default layer, can be positioned at (0,0) and
273 // be at the top-left of the viewport.
274 mDefaultCamera = CameraActor::New( Size::ZERO );
275 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
276 Add(*(mDefaultCamera.Get()));
279 void Stage::SetDefaultCameraPosition()
281 mDefaultCamera->SetY( -(static_cast<float>(mTopMargin) * 0.5f) );
284 Actor& Stage::GetDefaultRootActor()
289 CameraActor& Stage::GetDefaultCameraActor()
291 return *mDefaultCamera;
294 unsigned int Stage::GetLayerCount() const
296 return mLayerList->GetLayerCount();
299 Dali::Layer Stage::GetLayer( unsigned int depth ) const
301 return Dali::Layer(mLayerList->GetLayer( depth ));
304 Dali::Layer Stage::GetRootLayer() const
306 return Dali::Layer( mRootLayer.Get() );
309 LayerList& Stage::GetLayerList()
314 Integration::SystemOverlay& Stage::GetSystemOverlay()
316 // Lazily create system-level if requested
317 if( !mSystemOverlay )
319 mSystemOverlay = new Integration::SystemOverlay( SystemOverlay::New( *this ) );
320 DALI_ASSERT_ALWAYS( NULL != mSystemOverlay && "Failed to create system overlay" );
322 mSystemOverlay->GetImpl()->SetSize( mSize.width, mSize.height );
325 return *mSystemOverlay;
328 SystemOverlay* Stage::GetSystemOverlayInternal()
330 SystemOverlay* overlay( NULL );
334 overlay = mSystemOverlay->GetImpl();
340 void Stage::SetViewMode( ViewMode viewMode )
342 if( mViewMode != viewMode )
344 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "View mode changed from %d to %d\n", mViewMode, viewMode);
346 if( mViewMode == MONO )
348 mDefaultCamera->SetOrientation( Dali::ANGLE_180, Vector3::YAXIS );
349 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Actor() );
351 //Create camera and RenderTask for left eye
352 mLeftCamera = CameraActor::New( Size::ZERO );
353 mLeftCamera->SetParentOrigin( ParentOrigin::CENTER );
354 mDefaultCamera->Add( *mLeftCamera.Get() );
355 mLeftRenderTask = mRenderTaskList->CreateTask();
356 mLeftRenderTask.SetCameraActor( Dali::CameraActor( mLeftCamera.Get() ) );
357 mLeftCamera->SetType( Dali::Camera::FREE_LOOK );
359 //Create camera and RenderTask for right eye
360 mRightCamera = CameraActor::New( Size::ZERO );
361 mRightCamera->SetParentOrigin( ParentOrigin::CENTER );
362 mDefaultCamera->Add( *mRightCamera.Get() );
363 mRightRenderTask = mRenderTaskList->CreateTask();
364 mRightRenderTask.SetClearColor( Vector4( 1.0f,0.0f,0.0f,1.0f));
366 mRightRenderTask.SetCameraActor( Dali::CameraActor( mRightCamera.Get() ) );
367 mRightCamera->SetType( Dali::Camera::FREE_LOOK );
371 mViewMode = viewMode;
377 // delete extra stereoscopic render tasks and cameras
378 mRenderTaskList->RemoveTask( mLeftRenderTask );
379 mDefaultCamera->Remove( *mLeftCamera.Get() );
380 mLeftRenderTask.Reset();
382 mRenderTaskList->RemoveTask( mRightRenderTask );
383 mDefaultCamera->Remove( *mRightCamera.Get() );
384 mRightRenderTask.Reset();
385 mRightCamera.Reset();
386 mDefaultCamera->SetOrientation( Dali::ANGLE_0, Vector3::YAXIS );
387 mDefaultCamera->SetType( Dali::Camera::LOOK_AT_TARGET );
388 mRenderTaskList->GetTask(0).SetSourceActor( Dali::Layer(mRootLayer.Get()) );
392 case STEREO_HORIZONTAL:
394 //Stereo mode with horizontal split is for landscape mode. That's the reason for the cameras being rotated
395 //Top camera renders the scene as seen from the right eye and bottom camera as seen from left.
397 //Calculate separation in pixels along vertical axis ( mStereoBase is defined in millimetres )
398 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().y ) * 0.5f );
400 //Calculate aspect ratio
401 float aspect = mSize.width / (mSize.height * 0.5f);
403 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0f,stereoBase) );
404 mLeftCamera->SetAspectRatio( aspect );
406 mLeftCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
407 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
408 mLeftRenderTask.SetViewport( Viewport(0, mSize.height * 0.5f, mSize.width, mSize.height * 0.5f) );
410 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
411 mRightCamera->SetAspectRatio( aspect );
412 mRightCamera->SetOrientation( -Dali::ANGLE_90, Vector3::ZAXIS );
413 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
414 mRightRenderTask.SetViewport( Viewport(0, 0, mSize.width, mSize.height * 0.5f ) );
418 case STEREO_VERTICAL:
420 //Calculate separation in pixels along horizontal axis
421 const float stereoBase( ( (mStereoBase / 25.4f) * GetDpi().x ) * 0.5f );
423 //Recalculate fov based on viewport size
424 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
426 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
427 mLeftCamera->SetFieldOfView( fov );
428 mLeftCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
429 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
430 mLeftRenderTask.SetViewport( Viewport(0, 0, mSize.width * 0.5f, mSize.height ) );
432 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
433 mRightCamera->SetFieldOfView( fov );
434 mRightCamera->SetOrientation( Dali::ANGLE_0, Vector3::ZAXIS );
435 mRightCamera->SetPosition( Vector3( -stereoBase, 0.0f, 0.0f ) );
436 mRightRenderTask.SetViewport( Viewport(mSize.width * 0.5f, 0, mSize.width * 0.5f, mSize.height ) );
440 case STEREO_INTERLACED:
448 ViewMode Stage::GetViewMode() const
453 void Stage::SetStereoBase( float stereoBase )
455 if( ! Equals( mStereoBase, stereoBase ) )
457 DALI_LOG_INFO( Debug::Filter::gActor, Debug::Concise, "old( %.2f) new(%.2f)\n", mStereoBase, stereoBase );
458 mStereoBase = stereoBase;
462 case STEREO_HORIZONTAL:
464 stereoBase = mStereoBase / 25.4f * GetDpi().y * 0.5f;
465 float aspect = mSize.width / (mSize.height * 0.5f);
467 mLeftCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, stereoBase) );
468 mLeftCamera->SetAspectRatio( aspect );
469 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
471 mRightCamera->SetPerspectiveProjection( mSize, Vector2( 0.0, -stereoBase) );
472 mRightCamera->SetAspectRatio( aspect );
473 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
477 case STEREO_VERTICAL:
479 stereoBase = mStereoBase / 25.4f * GetDpi().x * 0.5f;
480 const float fov = 2.0f * std::atan( mSize.y / (2.0f * std::max( mSize.x*0.5f, mSize.y )) );
482 mLeftCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(stereoBase,0.0f) );
483 mLeftCamera->SetFieldOfView( fov );
484 mLeftCamera->SetPosition( Vector3( stereoBase, 0.0f, 0.0f ) );
486 mRightCamera->SetPerspectiveProjection( Size( mSize.x * 0.5f, mSize.y ), Vector2(-stereoBase,0.0f) );
487 mRightCamera->SetFieldOfView( fov );
488 mRightCamera->SetPosition( Vector3(-stereoBase, 0.0f, 0.0f ) );
498 float Stage::GetStereoBase() const
503 void Stage::SetBackgroundColor(Vector4 color)
505 // Cache for public GetBackgroundColor()
506 mBackgroundColor = color;
508 // Send message to change color in next frame
509 SetBackgroundColorMessage( mUpdateManager, color );
512 Vector4 Stage::GetBackgroundColor() const
514 return mBackgroundColor;
517 Vector2 Stage::GetDpi() const
522 void Stage::SetDpi(Vector2 dpi)
527 void Stage::KeepRendering( float durationSeconds )
529 // Send message to keep rendering
530 KeepRenderingMessage( mUpdateManager, durationSeconds );
533 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
535 bool connected( true );
536 Stage* stage = static_cast< Stage* >(object); // TypeRegistry guarantees that this is the correct type.
538 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
540 stage->KeyEventSignal().Connect( tracker, functor );
542 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT_GENERATED ) )
544 stage->KeyEventGeneratedSignal().Connect( tracker, functor );
546 else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
548 stage->EventProcessingFinishedSignal().Connect( tracker, functor );
550 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
552 stage->TouchedSignal().Connect( tracker, functor );
554 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCH ) )
556 stage->TouchSignal().Connect( tracker, functor );
558 else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
560 stage->WheelEventSignal().Connect( tracker, functor );
562 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
564 stage->ContextLostSignal().Connect( tracker, functor );
566 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
568 stage->ContextRegainedSignal().Connect( tracker, functor );
570 else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
572 stage->SceneCreatedSignal().Connect( tracker, functor );
576 // signalName does not match any signal
583 void Stage::EmitKeyEventSignal(const KeyEvent& event)
585 // Emit the key event signal when no actor in the stage has gained the key input focus
587 mKeyEventSignal.Emit( event );
590 bool Stage::EmitKeyEventGeneratedSignal(const KeyEvent& event)
592 // Emit the KeyEventGenerated signal when KeyEvent is generated
594 return mKeyEventGeneratedSignal.Emit( event );
597 void Stage::EmitEventProcessingFinishedSignal()
599 mEventProcessingFinishedSignal.Emit();
602 void Stage::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
604 mTouchedSignal.Emit( touchEvent );
605 mTouchSignal.Emit( touch );
608 void Stage::EmitWheelEventSignal(const WheelEvent& event)
610 // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
612 mWheelEventSignal.Emit( event );
615 void Stage::EmitSceneCreatedSignal()
617 mSceneCreatedSignal.Emit();
620 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
622 return mKeyEventSignal;
625 Dali::DevelStage::KeyEventGeneratedSignalType& Stage::KeyEventGeneratedSignal()
627 return mKeyEventGeneratedSignal;
630 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
632 return mEventProcessingFinishedSignal;
635 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
637 DALI_LOG_WARNING( "Deprecated. Use TouchSignal() instead.\n" );
638 return mTouchedSignal;
641 Dali::Stage::TouchSignalType& Stage::TouchSignal()
646 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
648 return mWheelEventSignal;
651 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
653 return mContextLostSignal;
656 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
658 return mContextRegainedSignal;
661 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
663 return mSceneCreatedSignal;
666 void Stage::NotifyContextLost()
668 mContextLostSignal.Emit();
671 void Stage::NotifyContextRegained()
673 mContextRegainedSignal.Emit();
677 void Stage::RequestRebuildDepthTree()
679 DALI_LOG_INFO(gLogFilter, Debug::General, "RequestRebuildDepthTree()\n");
680 mDepthTreeDirty = true;
683 void Stage::RebuildDepthTree()
685 // If the depth tree needs rebuilding, do it in this frame only.
686 if( mDepthTreeDirty )
688 DALI_LOG_INFO(gLogFilter, Debug::Concise, "RebuildDepthTree() dirty:T\n");
690 ActorPtr actor( mRootLayer.Get() );
691 actor->RebuildDepthTree();
692 mDepthTreeDirty = false;
697 Stage::Stage( AnimationPlaylist& playlist,
698 PropertyNotificationManager& propertyNotificationManager,
699 SceneGraph::UpdateManager& updateManager,
700 NotificationManager& notificationManager )
701 : mAnimationPlaylist( playlist ),
702 mPropertyNotificationManager(propertyNotificationManager),
703 mUpdateManager(updateManager),
704 mNotificationManager(notificationManager),
705 mSize(Vector2::ZERO),
706 mBackgroundColor(Dali::Stage::DEFAULT_BACKGROUND_COLOR),
708 mStereoBase( DEFAULT_STEREO_BASE ),
710 mSystemOverlay(NULL),
711 mDepthTreeDirty( false )
715 SceneGraph::UpdateManager& Stage::GetUpdateManager()
717 return mUpdateManager;
720 unsigned int* Stage::ReserveMessageSlot( std::size_t size, bool updateScene )
722 return mUpdateManager.ReserveMessageSlot( size, updateScene );
725 BufferIndex Stage::GetEventBufferIndex() const
727 return mUpdateManager.GetEventBufferIndex();
732 delete mSystemOverlay;
734 mObjectRegistry.Reset();
737 } // namespace Internal