2 * Copyright (c) 2019 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/scene-impl.h>
22 #include <dali/internal/event/actors/layer-impl.h>
23 #include <dali/internal/event/actors/layer-list.h>
24 #include <dali/internal/event/actors/camera-actor-impl.h>
25 #include <dali/internal/event/common/thread-local-storage.h>
26 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
27 #include <dali/internal/event/render-tasks/render-task-impl.h>
28 #include <dali/internal/event/common/object-registry-impl.h>
29 #include <dali/internal/update/nodes/node.h>
30 #include <dali/internal/update/manager/update-manager.h>
31 #include <dali/public-api/object/type-registry.h>
32 #include <dali/public-api/render-tasks/render-task-list.h>
33 #include <dali/internal/event/rendering/frame-buffer-impl.h>
34 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
36 using Dali::Internal::SceneGraph::Node;
47 const Vector4 DEFAULT_BACKGROUND_COLOR(0.0f, 0.0f, 0.0f, 1.0f); // Default background color
51 ScenePtr Scene::New( const Size& size )
53 ScenePtr scene = new Scene( size );
55 // Second-phase construction
61 Scene::Scene( const Size& size )
62 : mSurface( nullptr ),
64 mSurfaceSize( Vector2::ZERO ),
65 mDpi( Vector2::ZERO ),
66 mBackgroundColor( DEFAULT_BACKGROUND_COLOR ),
67 mSurfaceOrientation( 0 ),
68 mDepthTreeDirty( false ),
69 mEventProcessor( *this, ThreadLocalStorage::GetInternal()->GetGestureEventProcessor() )
77 // its enough to release the handle so the object is released
78 // don't need to remove it from root actor as root actor will delete the object
79 mDefaultCamera.Reset();
84 // we are closing down so just delete the root, no point emit disconnect
85 // signals or send messages to update
91 mRenderTaskList.Reset();
99 // Discard this Scene from the Core
103 void Scene::Initialize()
105 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
107 DALI_ASSERT_ALWAYS( tls && "Attempt to create scene before core exists!" );
109 tls->AddScene( this );
111 SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
113 // Create the ordered list of layers
114 mLayerList = LayerList::New( updateManager );
116 // The scene owns the default layer
117 mRootLayer = Layer::NewRoot( *mLayerList );
118 mRootLayer->SetName("RootLayer");
119 mRootLayer->SetScene( *this );
121 // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
122 // This stops actors parented to the stage having their relayout requests propagating
123 // up to the root layer, and down through other children unnecessarily.
124 mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
126 // Create the default camera actor first; this is needed by the RenderTaskList
127 // The default camera attributes and position is such that children of the default layer,
128 // can be positioned at (0,0) and be at the top-left of the viewport.
129 mDefaultCamera = CameraActor::New( mSize );
130 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
131 Add(*(mDefaultCamera.Get()));
133 // Create the list of render-tasks
134 mRenderTaskList = RenderTaskList::New();
136 // Create the default render-task
137 mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
140 void Scene::Add(Actor& actor)
142 mRootLayer->Add( actor );
145 void Scene::Remove(Actor& actor)
147 mRootLayer->Remove( actor );
150 Size Scene::GetSize() const
155 void Scene::SetDpi(Vector2 dpi)
160 Vector2 Scene::GetDpi() const
165 RenderTaskList& Scene::GetRenderTaskList() const
167 return *mRenderTaskList;
170 Dali::Layer Scene::GetRootLayer() const
172 return Dali::Layer( mRootLayer.Get() );
175 LayerList& Scene::GetLayerList() const
180 uint32_t Scene::GetLayerCount() const
182 return mLayerList->GetLayerCount();
185 Dali::Layer Scene::GetLayer( uint32_t depth ) const
187 return Dali::Layer(mLayerList->GetLayer( depth ));
190 CameraActor& Scene::GetDefaultCameraActor()
192 return *mDefaultCamera;
195 Actor& Scene::GetDefaultRootActor()
200 void Scene::SetSurface( Integration::RenderSurface& surface, bool forceUpdate )
205 RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
207 mFrameBuffer = Dali::Internal::FrameBuffer::New( surface, Dali::FrameBuffer::Attachment::NONE );
208 defaultRenderTask->SetFrameBuffer( mFrameBuffer );
210 SurfaceResized( forceUpdate );
214 void Scene::SurfaceResized( bool forceUpdate )
218 const float fWidth = static_cast<float>( mSurface->GetPositionSize().width );
219 const float fHeight = static_cast<float>( mSurface->GetPositionSize().height );
220 const int orientation = mSurface->GetOrientation();
222 if( ( fabsf( mSurfaceSize.width - fWidth ) > Math::MACHINE_EPSILON_1 )
223 || ( fabsf( mSurfaceSize.height - fHeight ) > Math::MACHINE_EPSILON_1 )
224 || ( orientation != mSurfaceOrientation )
227 Rect<int32_t> newSize( 0, 0, static_cast<int32_t>( mSurface->GetPositionSize().width ), static_cast<int32_t>( mSurface->GetPositionSize().height ) );
229 mSurfaceSize.width = fWidth;
230 mSurfaceSize.height = fHeight;
232 mSize.width = mSurfaceSize.width;
233 mSize.height = mSurfaceSize.height;
235 mSurfaceOrientation = orientation;
237 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
238 mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
239 mDefaultCamera->RotateProjection( mSurfaceOrientation );
241 mRootLayer->SetSize( mSize.width, mSize.height );
243 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
244 SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
245 SetDefaultSurfaceRectMessage( updateManager, newSize ); // truncated
246 SetDefaultSurfaceOrientationMessage( updateManager, mSurfaceOrientation );
248 RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
250 // if single render task to screen then set its viewport parameters
251 if( 1 == mRenderTaskList->GetTaskCount() )
253 if( !defaultRenderTask->GetTargetFrameBuffer() )
255 defaultRenderTask->SetViewport( newSize ); // truncated
259 defaultRenderTask->GetFrameBuffer()->SetSize( static_cast<uint32_t>( newSize.width ), static_cast<uint32_t>( newSize.height ) );
264 void Scene::SurfaceDeleted()
268 // The frame buffer doesn't have a valid render surface any more.
269 mFrameBuffer->MarkSurfaceAsInvalid();
273 Integration::RenderSurface* Scene::GetSurface() const
278 void Scene::Discard()
280 if( ThreadLocalStorage::Created() )
282 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
283 tls->RemoveScene( this );
287 void Scene::RequestRebuildDepthTree()
289 mDepthTreeDirty = true;
292 void Scene::QueueEvent( const Integration::Event& event )
294 mEventProcessor.QueueEvent( event );
297 void Scene::ProcessEvents()
299 mEventProcessor.ProcessEvents();
302 void Scene::RebuildDepthTree()
304 // If the depth tree needs rebuilding, do it in this frame only.
305 if( mDepthTreeDirty )
307 ActorPtr actor( mRootLayer.Get() );
308 actor->RebuildDepthTree();
309 mDepthTreeDirty = false;
313 void Scene::SetBackgroundColor( const Vector4& color )
315 mBackgroundColor = color;
319 mRenderTaskList->GetTask( 0u )->GetFrameBuffer()->SetBackgroundColor( color );
323 Vector4 Scene::GetBackgroundColor() const
325 return mBackgroundColor;
328 void Scene::EmitKeyEventSignal(const KeyEvent& event)
330 if ( !mKeyEventSignal.Empty() )
332 Dali::Integration::Scene handle( this );
333 mKeyEventSignal.Emit( event );
337 bool Scene::EmitKeyEventGeneratedSignal(const KeyEvent& event)
339 // Emit the KeyEventGenerated signal when KeyEvent is generated
340 return mKeyEventGeneratedSignal.Emit( event );
343 void Scene::EmitEventProcessingFinishedSignal()
345 if ( !mEventProcessingFinishedSignal.Empty() )
347 Dali::Integration::Scene handle( this );
348 mEventProcessingFinishedSignal.Emit();
352 void Scene::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
354 Dali::Integration::Scene handle( this );
355 if ( !mTouchedSignal.Empty() )
357 mTouchedSignal.Emit( touchEvent );
359 if ( !mTouchSignal.Empty() )
361 mTouchSignal.Emit( touch );
365 void Scene::EmitWheelEventSignal(const WheelEvent& event)
367 if ( !mWheelEventSignal.Empty() )
369 Dali::Integration::Scene handle( this );
370 mWheelEventSignal.Emit( event );
374 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
376 return mKeyEventSignal;
379 Integration::Scene::KeyEventGeneratedSignalType& Scene::KeyEventGeneratedSignal()
381 return mKeyEventGeneratedSignal;
384 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
386 return mEventProcessingFinishedSignal;
389 Scene::TouchedSignalType& Scene::TouchedSignal()
391 return mTouchedSignal;
394 Integration::Scene::TouchSignalType& Scene::TouchSignal()
399 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
401 return mWheelEventSignal;