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( Integration::RenderSurface& surface )
53 ScenePtr scene = new Scene;
55 // Second-phase construction
56 scene->Initialize( surface );
62 : mSurface( nullptr ),
63 mSize(), // Don't set the proper value here, this will be set when the surface is set later
65 mBackgroundColor( DEFAULT_BACKGROUND_COLOR ),
66 mSurfaceOrientation( 0 ),
67 mDepthTreeDirty( false ),
68 mEventProcessor( *this, ThreadLocalStorage::GetInternal()->GetGestureEventProcessor() )
76 // its enough to release the handle so the object is released
77 // don't need to remove it from root actor as root actor will delete the object
78 mDefaultCamera.Reset();
83 // we are closing down so just delete the root, no point emit disconnect
84 // signals or send messages to update
90 mRenderTaskList.Reset();
98 // No need to discard this Scene from Core, as Core stores an intrusive_ptr to this scene
99 // When this destructor is called, the scene has either already been removed from Core or Core has already been destroyed
102 void Scene::Initialize( Integration::RenderSurface& surface )
104 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
106 DALI_ASSERT_ALWAYS( tls && "Attempt to create scene before core exists!" );
108 tls->AddScene( this );
110 SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
112 // Create the ordered list of layers
113 mLayerList = LayerList::New( updateManager );
115 // The scene owns the default layer
116 mRootLayer = Layer::NewRoot( *mLayerList );
117 mRootLayer->SetName("RootLayer");
118 mRootLayer->SetScene( *this );
120 // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
121 // This stops actors parented to the stage having their relayout requests propagating
122 // up to the root layer, and down through other children unnecessarily.
123 mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
125 // Create the default camera actor first; this is needed by the RenderTaskList
126 // The default camera attributes and position is such that children of the default layer,
127 // can be positioned at (0,0) and be at the top-left of the viewport.
128 const PositionSize positionSize = surface.GetPositionSize();
129 const Vector2 surfaceSize( static_cast< float >( positionSize.width ), static_cast< float >( positionSize.height ) );
130 mDefaultCamera = CameraActor::New( surfaceSize );
131 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
132 Add(*(mDefaultCamera.Get()));
134 // Create the list of render-tasks
135 mRenderTaskList = RenderTaskList::New();
137 // Create the default render-task
138 mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
141 SetSurface( surface );
144 void Scene::Add(Actor& actor)
146 mRootLayer->Add( actor );
149 void Scene::Remove(Actor& actor)
151 mRootLayer->Remove( actor );
154 mRenderTaskList->GetTask( 0u )->GetFrameBuffer()->SetPartialUpdateEnabled( false );
158 Size Scene::GetSize() const
163 void Scene::SetDpi(Vector2 dpi)
168 Vector2 Scene::GetDpi() const
173 RenderTaskList& Scene::GetRenderTaskList() const
175 return *mRenderTaskList;
178 Dali::Layer Scene::GetRootLayer() const
180 return Dali::Layer( mRootLayer.Get() );
183 LayerList& Scene::GetLayerList() const
188 uint32_t Scene::GetLayerCount() const
190 return mLayerList->GetLayerCount();
193 Dali::Layer Scene::GetLayer( uint32_t depth ) const
195 return Dali::Layer(mLayerList->GetLayer( depth ));
198 CameraActor& Scene::GetDefaultCameraActor()
200 return *mDefaultCamera;
203 Actor& Scene::GetDefaultRootActor()
208 void Scene::SetSurface( Integration::RenderSurface& surface )
210 if( mSurface != &surface )
214 RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
216 mFrameBuffer = Dali::Internal::FrameBuffer::New( surface, Dali::FrameBuffer::Attachment::NONE );
217 defaultRenderTask->SetFrameBuffer( mFrameBuffer );
219 SurfaceResized( false );
223 void Scene::SurfaceResized( bool forceUpdate )
227 const PositionSize surfacePositionSize = mSurface->GetPositionSize();
228 const float fWidth = static_cast< float >( surfacePositionSize.width );
229 const float fHeight = static_cast< float >( surfacePositionSize.height );
230 const int orientation = mSurface->GetOrientation();
232 if( ( ( fabsf( mSize.width - fWidth ) > Math::MACHINE_EPSILON_1 ) || ( fabsf( mSize.height - fHeight ) > Math::MACHINE_EPSILON_1 ) )
233 || ( orientation != mSurfaceOrientation )
236 Rect< int32_t > newSize( 0, 0, static_cast< int32_t >( surfacePositionSize.width ), static_cast< int32_t >( surfacePositionSize.height ) ); // truncated
238 mSize.width = fWidth;
239 mSize.height = fHeight;
240 mSurfaceOrientation = orientation;
242 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
243 mDefaultCamera->SetPerspectiveProjection( mSize );
244 mDefaultCamera->RotateProjection( mSurfaceOrientation );
246 mRootLayer->SetSize( mSize.width, mSize.height );
248 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
249 SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
250 SetDefaultSurfaceRectMessage( updateManager, newSize );
251 SetDefaultSurfaceOrientationMessage( updateManager, mSurfaceOrientation );
253 // set default render-task viewport parameters
254 RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
255 defaultRenderTask->SetViewport( newSize );
256 defaultRenderTask->GetFrameBuffer()->SetSize( static_cast<uint32_t>( newSize.width ), static_cast<uint32_t>( newSize.height ) );
261 void Scene::SurfaceDeleted()
266 // The frame buffer doesn't have a valid render surface any more.
267 mFrameBuffer->MarkSurfaceAsInvalid();
271 Integration::RenderSurface* Scene::GetSurface() const
276 void Scene::Discard()
278 if( ThreadLocalStorage::Created() )
280 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
281 tls->RemoveScene( this );
285 void Scene::RequestRebuildDepthTree()
287 mDepthTreeDirty = true;
290 void Scene::QueueEvent( const Integration::Event& event )
292 mEventProcessor.QueueEvent( event );
295 void Scene::ProcessEvents()
297 mEventProcessor.ProcessEvents();
300 void Scene::RebuildDepthTree()
302 // If the depth tree needs rebuilding, do it in this frame only.
303 if( mDepthTreeDirty )
305 ActorPtr actor( mRootLayer.Get() );
306 actor->RebuildDepthTree();
307 mDepthTreeDirty = false;
311 void Scene::SetBackgroundColor( const Vector4& color )
313 mBackgroundColor = color;
317 mRenderTaskList->GetTask( 0u )->GetFrameBuffer()->SetBackgroundColor( color );
321 Vector4 Scene::GetBackgroundColor() const
323 return mBackgroundColor;
326 void Scene::EmitKeyEventSignal(const KeyEvent& event)
328 if ( !mKeyEventSignal.Empty() )
330 Dali::Integration::Scene handle( this );
331 mKeyEventSignal.Emit( event );
335 bool Scene::EmitKeyEventGeneratedSignal(const KeyEvent& event)
337 // Emit the KeyEventGenerated signal when KeyEvent is generated
338 return mKeyEventGeneratedSignal.Emit( event );
341 void Scene::EmitEventProcessingFinishedSignal()
343 if ( !mEventProcessingFinishedSignal.Empty() )
345 Dali::Integration::Scene handle( this );
346 mEventProcessingFinishedSignal.Emit();
350 void Scene::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
352 Dali::Integration::Scene handle( this );
353 if ( !mTouchedSignal.Empty() )
355 mTouchedSignal.Emit( touchEvent );
357 if ( !mTouchSignal.Empty() )
359 mTouchSignal.Emit( touch );
363 void Scene::EmitWheelEventSignal(const WheelEvent& event)
365 if ( !mWheelEventSignal.Empty() )
367 Dali::Integration::Scene handle( this );
368 mWheelEventSignal.Emit( event );
372 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
374 return mKeyEventSignal;
377 Integration::Scene::KeyEventGeneratedSignalType& Scene::KeyEventGeneratedSignal()
379 return mKeyEventGeneratedSignal;
382 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
384 return mEventProcessingFinishedSignal;
387 Scene::TouchedSignalType& Scene::TouchedSignal()
389 return mTouchedSignal;
392 Integration::Scene::TouchSignalType& Scene::TouchSignal()
397 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
399 return mWheelEventSignal;