2 * Copyright (c) 2018 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/internal/event/actors/layer-impl.h>
28 #include <dali/internal/event/actors/layer-list.h>
29 #include <dali/internal/event/actors/camera-actor-impl.h>
30 #include <dali/internal/event/common/thread-local-storage.h>
31 #include <dali/internal/event/common/property-notification-manager.h>
32 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
33 #include <dali/internal/event/update/frame-callback-interface-impl.h>
34 #include <dali/internal/update/nodes/node.h>
35 #include <dali/internal/update/manager/scene-graph-frame-callback.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>
42 #include <dali/public-api/rendering/frame-buffer.h>
44 using Dali::Internal::SceneGraph::Node;
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_DEPTH_TIMER" );
64 const char* const SIGNAL_KEY_EVENT = "keyEvent";
65 const char* const SIGNAL_KEY_EVENT_GENERATED = "keyEventGenerated";
66 const char* const SIGNAL_EVENT_PROCESSING_FINISHED = "eventProcessingFinished";
67 const char* const SIGNAL_TOUCHED = "touched";
68 const char* const SIGNAL_TOUCH = "touch";
69 const char* const SIGNAL_WHEEL_EVENT = "wheelEvent";
70 const char* const SIGNAL_CONTEXT_LOST = "contextLost";
71 const char* const SIGNAL_CONTEXT_REGAINED = "contextRegained";
72 const char* const SIGNAL_SCENE_CREATED = "sceneCreated";
74 TypeRegistration mType( typeid(Dali::Stage), typeid(Dali::BaseHandle), NULL );
76 SignalConnectorType signalConnector1( mType, SIGNAL_KEY_EVENT, &Stage::DoConnectSignal );
77 SignalConnectorType signalConnector2( mType, SIGNAL_EVENT_PROCESSING_FINISHED, &Stage::DoConnectSignal );
78 SignalConnectorType signalConnector3( mType, SIGNAL_TOUCHED, &Stage::DoConnectSignal );
79 SignalConnectorType signalConnector4( mType, SIGNAL_WHEEL_EVENT, &Stage::DoConnectSignal );
80 SignalConnectorType signalConnector5( mType, SIGNAL_CONTEXT_LOST, &Stage::DoConnectSignal );
81 SignalConnectorType signalConnector6( mType, SIGNAL_CONTEXT_REGAINED, &Stage::DoConnectSignal );
82 SignalConnectorType signalConnector7( mType, SIGNAL_SCENE_CREATED, &Stage::DoConnectSignal );
83 SignalConnectorType signalConnector8( mType, SIGNAL_KEY_EVENT_GENERATED, &Stage::DoConnectSignal );
84 SignalConnectorType signalConnector9( mType, SIGNAL_TOUCH, &Stage::DoConnectSignal );
86 } // unnamed namespace
88 StagePtr Stage::New( AnimationPlaylist& playlist,
89 PropertyNotificationManager& propertyNotificationManager,
90 SceneGraph::UpdateManager& updateManager,
91 NotificationManager& notificationManager,
92 Integration::RenderController& renderController )
94 return StagePtr( new Stage( playlist, propertyNotificationManager, updateManager, notificationManager, renderController ) );
97 void Stage::Initialize( bool renderToFbo )
99 mRenderToFbo = renderToFbo;
100 mObjectRegistry = ObjectRegistry::New();
102 // Create the ordered list of layers
103 mLayerList = LayerList::New( mUpdateManager );
105 // The stage owns the default layer
106 mRootLayer = Layer::NewRoot( *mLayerList, mUpdateManager );
107 mRootLayer->SetName("RootLayer");
108 // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
109 // This stops actors parented to the stage having their relayout requests propagating
110 // up to the root layer, and down through other children unnecessarily.
111 mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
113 // Create the default camera actor first; this is needed by the RenderTaskList
114 CreateDefaultCameraActor();
116 // Create the list of render-tasks
117 mRenderTaskList = RenderTaskList::New();
119 // Create the default render-task (don't need the returned handle)
120 mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
123 void Stage::Uninitialize()
127 // its enough to release the handle so the object is released
128 // don't need to remove it from root actor as root actor will delete the object
129 mDefaultCamera.Reset();
134 // we are closing down so just delete the root, no point emit disconnect
135 // signals or send messages to update
139 if( mRenderTaskList )
141 mRenderTaskList.Reset();
145 StagePtr Stage::GetCurrent()
147 StagePtr stage( NULL );
148 // no checking in this version
149 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
152 stage = tls->GetCurrentStage();
157 bool Stage::IsInstalled()
159 return ThreadLocalStorage::Created();
162 ObjectRegistry& Stage::GetObjectRegistry()
164 return *mObjectRegistry;
167 void Stage::RegisterObject( Dali::BaseObject* object )
169 mObjectRegistry->RegisterObject( object );
172 void Stage::UnregisterObject( Dali::BaseObject* object )
174 mObjectRegistry->UnregisterObject( object );
177 Layer& Stage::GetRootActor()
182 AnimationPlaylist& Stage::GetAnimationPlaylist()
184 return mAnimationPlaylist;
187 PropertyNotificationManager& Stage::GetPropertyNotificationManager()
189 return mPropertyNotificationManager;
192 void Stage::Add( Actor& actor )
194 mRootLayer->Add( actor );
197 void Stage::Remove( Actor& actor )
199 mRootLayer->Remove( actor );
202 void Stage::SurfaceResized( float width, float height )
204 if( ( fabsf( width - mSurfaceSize.width ) > Math::MACHINE_EPSILON_1000 ) || ( fabsf( height - mSurfaceSize.height ) > Math::MACHINE_EPSILON_1000 ) )
206 mSurfaceSize.width = width;
207 mSurfaceSize.height = height;
209 // Internally we want to report the actual size of the stage.
211 mSize.height = height - static_cast<float>( mTopMargin );
213 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
214 mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
216 // Adjust the camera height to allow for top-margin
217 SetDefaultCameraPosition();
219 mRootLayer->SetSize( mSize.width, mSize.height );
221 SetDefaultSurfaceRectMessage( mUpdateManager, Rect<int32_t>( 0, 0, static_cast<int32_t>( width ), static_cast<int32_t>( height ) ) ); // truncated
223 // if single render task to screen then set its viewport parameters
224 if( 1 == mRenderTaskList->GetTaskCount() )
226 RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
228 if(!defaultRenderTask->GetTargetFrameBuffer())
230 defaultRenderTask->SetViewport( Viewport( 0, 0, static_cast<int32_t>( width ), static_cast<int32_t>( height ) ) ); // truncated
236 Dali::FrameBuffer frameBuffer = Dali::FrameBuffer::New( static_cast<uint32_t>( width ), static_cast<uint32_t>( height ), Dali::FrameBuffer::Attachment::NONE );
237 Dali::Texture texture = Dali::Texture::New( Dali::TextureType::TEXTURE_2D, Dali::Pixel::RGB888, static_cast<uint32_t>( width ), static_cast<uint32_t>( height ) );
238 frameBuffer.AttachColorTexture( texture );
240 RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
241 defaultRenderTask->SetFrameBuffer( &GetImplementation( frameBuffer ) );
246 Vector2 Stage::GetSize() const
251 void Stage::SetTopMargin( uint32_t margin )
253 if (mTopMargin == margin)
259 mSize.width = mSurfaceSize.width;
260 mSize.height = mSurfaceSize.height - static_cast<float>( mTopMargin );
262 // Adjust the camera height to allow for top-margin
263 SetDefaultCameraPosition();
265 mRootLayer->SetSize( mSize.width, mSize.height );
268 RenderTaskList& Stage::GetRenderTaskList() const
270 return *mRenderTaskList;
273 void Stage::CreateDefaultCameraActor()
275 // The default camera attributes and position is such that
276 // children of the default layer, can be positioned at (0,0) and
277 // be at the top-left of the viewport.
278 mDefaultCamera = CameraActor::New( Size::ZERO );
279 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
280 Add(*(mDefaultCamera.Get()));
283 void Stage::SetDefaultCameraPosition()
285 mDefaultCamera->SetY( -(static_cast<float>(mTopMargin) * 0.5f) );
288 Actor& Stage::GetDefaultRootActor()
293 CameraActor& Stage::GetDefaultCameraActor()
295 return *mDefaultCamera;
298 uint32_t Stage::GetLayerCount() const
300 return mLayerList->GetLayerCount();
303 Dali::Layer Stage::GetLayer( uint32_t depth ) const
305 return Dali::Layer(mLayerList->GetLayer( depth ));
308 Dali::Layer Stage::GetRootLayer() const
310 return Dali::Layer( mRootLayer.Get() );
313 LayerList& Stage::GetLayerList()
318 void Stage::SetBackgroundColor(Vector4 color)
320 // Cache for public GetBackgroundColor()
321 mBackgroundColor = color;
323 // Send message to change color in next frame
324 SetBackgroundColorMessage( mUpdateManager, color );
327 Vector4 Stage::GetBackgroundColor() const
329 return mBackgroundColor;
332 Vector2 Stage::GetDpi() const
337 void Stage::SetDpi(Vector2 dpi)
342 void Stage::KeepRendering( float durationSeconds )
344 // Send message to keep rendering
345 KeepRenderingMessage( mUpdateManager, durationSeconds );
348 void Stage::SetRenderingBehavior( DevelStage::Rendering renderingBehavior )
350 if( mRenderingBehavior != renderingBehavior )
352 // Send message to change the rendering behavior
353 SetRenderingBehaviorMessage( mUpdateManager, renderingBehavior );
355 mRenderingBehavior = renderingBehavior;
359 DevelStage::Rendering Stage::GetRenderingBehavior() const
361 return mRenderingBehavior;
364 bool Stage::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
366 bool connected( true );
367 Stage* stage = static_cast< Stage* >(object); // TypeRegistry guarantees that this is the correct type.
369 if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT ) )
371 stage->KeyEventSignal().Connect( tracker, functor );
373 else if( 0 == strcmp( signalName.c_str(), SIGNAL_KEY_EVENT_GENERATED ) )
375 stage->KeyEventGeneratedSignal().Connect( tracker, functor );
377 else if( 0 == strcmp( signalName.c_str(), SIGNAL_EVENT_PROCESSING_FINISHED ) )
379 stage->EventProcessingFinishedSignal().Connect( tracker, functor );
381 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCHED ) )
383 stage->TouchedSignal().Connect( tracker, functor );
385 else if( 0 == strcmp( signalName.c_str(), SIGNAL_TOUCH ) )
387 stage->TouchSignal().Connect( tracker, functor );
389 else if( 0 == strcmp( signalName.c_str(), SIGNAL_WHEEL_EVENT ) )
391 stage->WheelEventSignal().Connect( tracker, functor );
393 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_LOST ) )
395 stage->ContextLostSignal().Connect( tracker, functor );
397 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CONTEXT_REGAINED ) )
399 stage->ContextRegainedSignal().Connect( tracker, functor );
401 else if( 0 == strcmp( signalName.c_str(), SIGNAL_SCENE_CREATED ) )
403 stage->SceneCreatedSignal().Connect( tracker, functor );
407 // signalName does not match any signal
414 void Stage::EmitKeyEventSignal(const KeyEvent& event)
416 // Emit the key event signal when no actor in the stage has gained the key input focus
418 mKeyEventSignal.Emit( event );
421 bool Stage::EmitKeyEventGeneratedSignal(const KeyEvent& event)
423 // Emit the KeyEventGenerated signal when KeyEvent is generated
425 return mKeyEventGeneratedSignal.Emit( event );
428 void Stage::EmitEventProcessingFinishedSignal()
430 mEventProcessingFinishedSignal.Emit();
433 void Stage::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
435 mTouchedSignal.Emit( touchEvent );
436 mTouchSignal.Emit( touch );
439 void Stage::EmitWheelEventSignal(const WheelEvent& event)
441 // Emit the wheel event signal when no actor in the stage has gained the wheel input focus
443 mWheelEventSignal.Emit( event );
446 void Stage::EmitSceneCreatedSignal()
448 mSceneCreatedSignal.Emit();
451 Dali::Stage::KeyEventSignalType& Stage::KeyEventSignal()
453 return mKeyEventSignal;
456 Dali::DevelStage::KeyEventGeneratedSignalType& Stage::KeyEventGeneratedSignal()
458 return mKeyEventGeneratedSignal;
461 void Stage::AddFrameCallback( FrameCallbackInterface& frameCallback, Actor& rootActor )
463 DALI_ASSERT_ALWAYS( ( ! FrameCallbackInterface::Impl::Get( frameCallback ).IsConnectedToSceneGraph() )
464 && "FrameCallbackInterface implementation already added" );
466 // Create scene-graph object and transfer to UpdateManager
467 OwnerPointer< SceneGraph::FrameCallback > transferOwnership( SceneGraph::FrameCallback::New( frameCallback ) );
468 AddFrameCallbackMessage( mUpdateManager, transferOwnership, rootActor.GetNode() );
471 void Stage::RemoveFrameCallback( FrameCallbackInterface& frameCallback )
473 FrameCallbackInterface::Impl::Get( frameCallback ).Invalidate();
474 RemoveFrameCallbackMessage( mUpdateManager, frameCallback );
477 Dali::Stage::EventProcessingFinishedSignalType& Stage::EventProcessingFinishedSignal()
479 return mEventProcessingFinishedSignal;
482 Dali::Stage::TouchedSignalType& Stage::TouchedSignal()
484 DALI_LOG_WARNING( "Deprecated. Use TouchSignal() instead.\n" );
485 return mTouchedSignal;
488 Dali::Stage::TouchSignalType& Stage::TouchSignal()
493 Dali::Stage::WheelEventSignalType& Stage::WheelEventSignal()
495 return mWheelEventSignal;
498 Dali::Stage::ContextStatusSignal& Stage::ContextLostSignal()
500 return mContextLostSignal;
503 Dali::Stage::ContextStatusSignal& Stage::ContextRegainedSignal()
505 return mContextRegainedSignal;
508 Dali::Stage::SceneCreatedSignalType& Stage::SceneCreatedSignal()
510 return mSceneCreatedSignal;
513 void Stage::NotifyContextLost()
515 mContextLostSignal.Emit();
518 void Stage::NotifyContextRegained()
520 mContextRegainedSignal.Emit();
524 void Stage::RequestRebuildDepthTree()
526 DALI_LOG_INFO(gLogFilter, Debug::General, "RequestRebuildDepthTree()\n");
527 mDepthTreeDirty = true;
530 void Stage::RebuildDepthTree()
532 // If the depth tree needs rebuilding, do it in this frame only.
533 if( mDepthTreeDirty )
535 DALI_LOG_INFO(gLogFilter, Debug::Concise, "RebuildDepthTree() dirty:T\n");
537 ActorPtr actor( mRootLayer.Get() );
538 actor->RebuildDepthTree();
539 mDepthTreeDirty = false;
544 Stage::Stage( AnimationPlaylist& playlist,
545 PropertyNotificationManager& propertyNotificationManager,
546 SceneGraph::UpdateManager& updateManager,
547 NotificationManager& notificationManager,
548 Integration::RenderController& renderController )
549 : mAnimationPlaylist( playlist ),
550 mPropertyNotificationManager( propertyNotificationManager ),
551 mUpdateManager( updateManager ),
552 mNotificationManager( notificationManager ),
553 mRenderController( renderController ),
554 mSize( Vector2::ZERO ),
555 mSurfaceSize( Vector2::ZERO ),
556 mBackgroundColor( Dali::Stage::DEFAULT_BACKGROUND_COLOR ),
558 mDpi( Vector2::ZERO ),
560 mKeyEventGeneratedSignal(),
561 mEventProcessingFinishedSignal(),
565 mContextLostSignal(),
566 mContextRegainedSignal(),
567 mSceneCreatedSignal(),
568 mRenderingBehavior( DevelStage::Rendering::IF_REQUIRED ),
569 mDepthTreeDirty( false ),
570 mForceNextUpdate( false ),
571 mRenderToFbo( false )
575 SceneGraph::UpdateManager& Stage::GetUpdateManager()
577 return mUpdateManager;
580 Integration::RenderController& Stage::GetRenderController()
582 return mRenderController;
585 uint32_t* Stage::ReserveMessageSlot( uint32_t size, bool updateScene )
587 return mUpdateManager.ReserveMessageSlot( size, updateScene );
590 BufferIndex Stage::GetEventBufferIndex() const
592 return mUpdateManager.GetEventBufferIndex();
595 void Stage::ForceNextUpdate()
597 mForceNextUpdate = true;
600 bool Stage::IsNextUpdateForced()
602 bool nextUpdateForced = mForceNextUpdate;
603 mForceNextUpdate = false;
604 return nextUpdateForced;
611 } // namespace Internal