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 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();
93 // Discard this Scene from the Core
97 void Scene::Initialize()
99 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
101 DALI_ASSERT_ALWAYS( tls && "Attempt to create scene before core exists!" );
103 tls->AddScene( this );
105 SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
107 // Create the ordered list of layers
108 mLayerList = LayerList::New( updateManager );
110 // The scene owns the default layer
111 mRootLayer = Layer::NewRoot( *mLayerList );
112 mRootLayer->SetName("RootLayer");
113 mRootLayer->SetScene( *this );
115 // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
116 // This stops actors parented to the stage having their relayout requests propagating
117 // up to the root layer, and down through other children unnecessarily.
118 mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
120 // Create the default camera actor first; this is needed by the RenderTaskList
121 // The default camera attributes and position is such that children of the default layer,
122 // can be positioned at (0,0) and be at the top-left of the viewport.
123 mDefaultCamera = CameraActor::New( mSize );
124 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
125 Add(*(mDefaultCamera.Get()));
127 // Create the list of render-tasks
128 mRenderTaskList = RenderTaskList::New();
130 // Create the default render-task
131 mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
134 void Scene::Add(Actor& actor)
136 mRootLayer->Add( actor );
139 void Scene::Remove(Actor& actor)
141 mRootLayer->Remove( actor );
144 Size Scene::GetSize() const
149 void Scene::SetDpi(Vector2 dpi)
154 Vector2 Scene::GetDpi() const
159 RenderTaskList& Scene::GetRenderTaskList() const
161 return *mRenderTaskList;
164 Dali::Layer Scene::GetRootLayer() const
166 return Dali::Layer( mRootLayer.Get() );
169 LayerList& Scene::GetLayerList() const
174 uint32_t Scene::GetLayerCount() const
176 return mLayerList->GetLayerCount();
179 Dali::Layer Scene::GetLayer( uint32_t depth ) const
181 return Dali::Layer(mLayerList->GetLayer( depth ));
184 CameraActor& Scene::GetDefaultCameraActor()
186 return *mDefaultCamera;
189 Actor& Scene::GetDefaultRootActor()
194 void Scene::SetSurface( Integration::RenderSurface& surface )
199 RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
201 mFrameBuffer = Dali::Internal::FrameBuffer::New( surface, Dali::FrameBuffer::Attachment::NONE );
202 defaultRenderTask->SetFrameBuffer( mFrameBuffer );
208 void Scene::SurfaceResized()
212 const float fWidth = static_cast<float>( mSurface->GetPositionSize().width );
213 const float fHeight = static_cast<float>( mSurface->GetPositionSize().height );
215 if( ( fabsf( mSurfaceSize.width - fWidth ) > Math::MACHINE_EPSILON_1 ) || ( fabsf( mSurfaceSize.height - fHeight ) > Math::MACHINE_EPSILON_1 ) )
217 Rect<int32_t> newSize( 0, 0, static_cast<int32_t>( mSurface->GetPositionSize().width ), static_cast<int32_t>( mSurface->GetPositionSize().height ) );
219 mSurfaceSize.width = fWidth;
220 mSurfaceSize.height = fHeight;
222 mSize.width = mSurfaceSize.width;
223 mSize.height = mSurfaceSize.height;
225 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
226 mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
228 mRootLayer->SetSize( mSize.width, mSize.height );
230 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
231 SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
232 SetDefaultSurfaceRectMessage( updateManager, newSize ); // truncated
234 RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
236 // if single render task to screen then set its viewport parameters
237 if( 1 == mRenderTaskList->GetTaskCount() )
239 if( !defaultRenderTask->GetTargetFrameBuffer() )
241 defaultRenderTask->SetViewport( newSize ); // truncated
245 defaultRenderTask->GetFrameBuffer()->SetSize( static_cast<uint32_t>( newSize.width ), static_cast<uint32_t>( newSize.height ) );
250 Integration::RenderSurface* Scene::GetSurface() const
255 void Scene::Discard()
257 if( ThreadLocalStorage::Created() )
259 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
260 tls->RemoveScene( this );
264 void Scene::RequestRebuildDepthTree()
266 mDepthTreeDirty = true;
269 void Scene::QueueEvent( const Integration::Event& event )
271 mEventProcessor.QueueEvent( event );
274 void Scene::ProcessEvents()
276 mEventProcessor.ProcessEvents();
279 void Scene::RebuildDepthTree()
281 // If the depth tree needs rebuilding, do it in this frame only.
282 if( mDepthTreeDirty )
284 ActorPtr actor( mRootLayer.Get() );
285 actor->RebuildDepthTree();
286 mDepthTreeDirty = false;
290 void Scene::SetBackgroundColor( const Vector4& color )
292 mBackgroundColor = color;
296 mRenderTaskList->GetTask( 0u )->GetFrameBuffer()->SetBackgroundColor( color );
300 Vector4 Scene::GetBackgroundColor() const
302 return mBackgroundColor;
305 void Scene::EmitKeyEventSignal(const KeyEvent& event)
307 mKeyEventSignal.Emit( event );
310 void Scene::EmitEventProcessingFinishedSignal()
312 mEventProcessingFinishedSignal.Emit();
315 void Scene::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
317 mTouchedSignal.Emit( touchEvent );
318 mTouchSignal.Emit( touch );
321 void Scene::EmitWheelEventSignal(const WheelEvent& event)
323 mWheelEventSignal.Emit( event );
326 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
328 return mKeyEventSignal;
331 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
333 return mEventProcessingFinishedSignal;
336 Scene::TouchedSignalType& Scene::TouchedSignal()
338 return mTouchedSignal;
341 Integration::Scene::TouchSignalType& Scene::TouchSignal()
346 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
348 return mWheelEventSignal;