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 Size Scene::GetSize() const
159 void Scene::SetDpi(Vector2 dpi)
164 Vector2 Scene::GetDpi() const
169 RenderTaskList& Scene::GetRenderTaskList() const
171 return *mRenderTaskList;
174 Dali::Layer Scene::GetRootLayer() const
176 return Dali::Layer( mRootLayer.Get() );
179 LayerList& Scene::GetLayerList() const
184 uint32_t Scene::GetLayerCount() const
186 return mLayerList->GetLayerCount();
189 Dali::Layer Scene::GetLayer( uint32_t depth ) const
191 return Dali::Layer(mLayerList->GetLayer( depth ));
194 CameraActor& Scene::GetDefaultCameraActor()
196 return *mDefaultCamera;
199 Actor& Scene::GetDefaultRootActor()
204 void Scene::SetSurface( Integration::RenderSurface& surface )
206 if( mSurface != &surface )
210 RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
212 mFrameBuffer = Dali::Internal::FrameBuffer::New( surface, Dali::FrameBuffer::Attachment::NONE );
213 defaultRenderTask->SetFrameBuffer( mFrameBuffer );
215 SurfaceResized( false );
219 void Scene::SurfaceResized( bool forceUpdate )
223 const PositionSize surfacePositionSize = mSurface->GetPositionSize();
224 const float fWidth = static_cast< float >( surfacePositionSize.width );
225 const float fHeight = static_cast< float >( surfacePositionSize.height );
226 const int orientation = mSurface->GetOrientation();
228 if( ( ( fabsf( mSize.width - fWidth ) > Math::MACHINE_EPSILON_1 ) || ( fabsf( mSize.height - fHeight ) > Math::MACHINE_EPSILON_1 ) )
229 || ( orientation != mSurfaceOrientation )
232 Rect< int32_t > newSize( 0, 0, static_cast< int32_t >( surfacePositionSize.width ), static_cast< int32_t >( surfacePositionSize.height ) ); // truncated
234 mSize.width = fWidth;
235 mSize.height = fHeight;
236 mSurfaceOrientation = orientation;
238 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
239 mDefaultCamera->SetPerspectiveProjection( mSize );
240 mDefaultCamera->RotateProjection( mSurfaceOrientation );
242 mRootLayer->SetSize( mSize.width, mSize.height );
244 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
245 SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
246 SetDefaultSurfaceRectMessage( updateManager, newSize );
247 SetDefaultSurfaceOrientationMessage( updateManager, mSurfaceOrientation );
249 // set default render-task viewport parameters
250 RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
251 defaultRenderTask->SetViewport( newSize );
252 defaultRenderTask->GetFrameBuffer()->SetSize( static_cast<uint32_t>( newSize.width ), static_cast<uint32_t>( newSize.height ) );
257 void Scene::SurfaceDeleted()
262 // The frame buffer doesn't have a valid render surface any more.
263 mFrameBuffer->MarkSurfaceAsInvalid();
267 Integration::RenderSurface* Scene::GetSurface() const
272 void Scene::Discard()
274 if( ThreadLocalStorage::Created() )
276 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
277 tls->RemoveScene( this );
281 void Scene::RequestRebuildDepthTree()
283 mDepthTreeDirty = true;
286 void Scene::QueueEvent( const Integration::Event& event )
288 mEventProcessor.QueueEvent( event );
291 void Scene::ProcessEvents()
293 mEventProcessor.ProcessEvents();
296 void Scene::RebuildDepthTree()
298 // If the depth tree needs rebuilding, do it in this frame only.
299 if( mDepthTreeDirty )
301 ActorPtr actor( mRootLayer.Get() );
302 actor->RebuildDepthTree();
303 mDepthTreeDirty = false;
307 void Scene::SetBackgroundColor( const Vector4& color )
309 mBackgroundColor = color;
313 mRenderTaskList->GetTask( 0u )->SetClearColor( color );
314 mRenderTaskList->GetTask( 0u )->SetClearEnabled( true );
318 Vector4 Scene::GetBackgroundColor() const
320 return mBackgroundColor;
323 void Scene::EmitKeyEventSignal(const KeyEvent& event)
325 if ( !mKeyEventSignal.Empty() )
327 Dali::Integration::Scene handle( this );
328 mKeyEventSignal.Emit( event );
332 bool Scene::EmitKeyEventGeneratedSignal(const KeyEvent& event)
334 // Emit the KeyEventGenerated signal when KeyEvent is generated
335 return mKeyEventGeneratedSignal.Emit( event );
338 void Scene::EmitEventProcessingFinishedSignal()
340 if ( !mEventProcessingFinishedSignal.Empty() )
342 Dali::Integration::Scene handle( this );
343 mEventProcessingFinishedSignal.Emit();
347 void Scene::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
349 Dali::Integration::Scene handle( this );
350 if ( !mTouchedSignal.Empty() )
352 mTouchedSignal.Emit( touchEvent );
354 if ( !mTouchSignal.Empty() )
356 mTouchSignal.Emit( touch );
360 void Scene::EmitWheelEventSignal(const WheelEvent& event)
362 if ( !mWheelEventSignal.Empty() )
364 Dali::Integration::Scene handle( this );
365 mWheelEventSignal.Emit( event );
369 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
371 return mKeyEventSignal;
374 Integration::Scene::KeyEventGeneratedSignalType& Scene::KeyEventGeneratedSignal()
376 return mKeyEventGeneratedSignal;
379 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
381 return mEventProcessingFinishedSignal;
384 Scene::TouchedSignalType& Scene::TouchedSignal()
386 return mTouchedSignal;
389 Integration::Scene::TouchSignalType& Scene::TouchSignal()
394 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
396 return mWheelEventSignal;