2 * Copyright (c) 2020 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/internal/update/common/scene-graph-scene.h>
32 #include <dali/public-api/common/constants.h>
33 #include <dali/public-api/object/type-registry.h>
34 #include <dali/public-api/render-tasks/render-task-list.h>
35 #include <dali/internal/event/rendering/frame-buffer-impl.h>
36 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
38 using Dali::Internal::SceneGraph::Node;
46 ScenePtr Scene::New(Size size, int orientation)
48 ScenePtr scene = new Scene;
50 // Second-phase construction
51 scene->Initialize(size, orientation);
57 : mSceneObject(nullptr),
58 mSize(), // Don't set the proper value here, this will be set when the surface is set later
60 mBackgroundColor(DEFAULT_BACKGROUND_COLOR),
61 mDepthTreeDirty(false),
62 mEventProcessor(*this, ThreadLocalStorage::GetInternal()->GetGestureEventProcessor()),
63 mSurfaceOrientation(0)
69 if( EventThreadServices::IsCoreRunning() && mSceneObject )
71 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
72 RemoveSceneMessage( tls->GetUpdateManager(), *mSceneObject );
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();
94 // No need to discard this Scene from Core, as Core stores an intrusive_ptr to this scene
95 // When this destructor is called, the scene has either already been removed from Core or Core has already been destroyed
98 void Scene::Initialize(Size size, int orientation)
100 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
102 DALI_ASSERT_ALWAYS( tls && "Attempt to create scene before core exists!" );
104 tls->AddScene( this );
106 SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
108 // Create the ordered list of layers
109 mLayerList = LayerList::New( updateManager );
111 // The scene owns the default layer
112 mRootLayer = Layer::NewRoot( *mLayerList );
113 mRootLayer->SetName("RootLayer");
114 mRootLayer->SetScene( *this );
116 // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
117 // This stops actors parented to the stage having their relayout requests propagating
118 // up to the root layer, and down through other children unnecessarily.
119 mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
121 // Create the default camera actor first; this is needed by the RenderTaskList
122 // The default camera attributes and position is such that children of the default layer,
123 // can be positioned at (0,0) and be at the top-left of the viewport.
124 mDefaultCamera = CameraActor::New( size );
125 mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
126 Add(*(mDefaultCamera.Get()));
128 // Create the list of render-tasks
129 mRenderTaskList = RenderTaskList::New();
131 // Create the default render-task and ensure clear is enabled on it to show the background color
132 RenderTaskPtr renderTask = mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
133 renderTask->SetClearEnabled(true);
134 mSurfaceOrientation = orientation;
136 SurfaceRotated( size.width, size.height, mSurfaceOrientation );
138 // Create scene graph object
139 mSceneObject = new SceneGraph::Scene();
140 OwnerPointer< SceneGraph::Scene > transferOwnership( const_cast< SceneGraph::Scene* >( mSceneObject ) );
141 AddSceneMessage( updateManager, transferOwnership );
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::SurfaceResized(float width, float height)
206 if((fabsf(mSize.width - width) > Math::MACHINE_EPSILON_1) || (fabsf(mSize.height - height) > Math::MACHINE_EPSILON_1))
208 ChangedSurface(width, height, mSurfaceOrientation);
212 void Scene::SurfaceReplaced()
216 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
217 SurfaceReplacedMessage( tls->GetUpdateManager(), *mSceneObject );
221 void Scene::Discard()
223 if( ThreadLocalStorage::Created() )
225 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
226 tls->RemoveScene( this );
230 void Scene::RequestRebuildDepthTree()
232 mDepthTreeDirty = true;
235 void Scene::QueueEvent( const Integration::Event& event )
237 mEventProcessor.QueueEvent( event );
240 void Scene::ProcessEvents()
242 mEventProcessor.ProcessEvents();
245 void Scene::RebuildDepthTree()
247 // If the depth tree needs rebuilding, do it in this frame only.
248 if( mDepthTreeDirty )
250 ActorPtr actor( mRootLayer.Get() );
251 actor->RebuildDepthTree();
252 mDepthTreeDirty = false;
256 void Scene::SetBackgroundColor( const Vector4& color )
258 mBackgroundColor = color;
260 mRenderTaskList->GetTask( 0u )->SetClearColor( color );
261 mRenderTaskList->GetTask( 0u )->SetClearEnabled( true );
264 Vector4 Scene::GetBackgroundColor() const
266 return mBackgroundColor;
269 SceneGraph::Scene* Scene::GetSceneObject() const
274 void Scene::EmitKeyEventSignal(const Dali::KeyEvent& event)
276 if ( !mKeyEventSignal.Empty() )
278 Dali::Integration::Scene handle( this );
279 mKeyEventSignal.Emit( event );
283 void Scene::SurfaceRotated(float width, float height, int orientation)
285 mSurfaceOrientation = orientation;
286 ChangedSurface(width, height, orientation);
289 int Scene::GetSurfaceOrientation()
291 return mSurfaceOrientation;
294 void Scene::ChangedSurface(float width, float height, int orientation)
296 Rect<int32_t> newSize(0, 0, static_cast<int32_t>(width), static_cast<int32_t>(height)); // truncated
299 mSize.height = height;
301 // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
302 mDefaultCamera->SetPerspectiveProjection(mSize);
303 // Set the surface orientation to Default camera for window/screen rotation
304 mDefaultCamera->RotateProjection(orientation);
306 mRootLayer->SetSize(width, height);
308 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
309 SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
310 SetDefaultSurfaceRectMessage(updateManager, newSize);
312 // Send the surface orientation to render manager for calculating glViewport/Scissor
313 SetDefaultSurfaceOrientationMessage(updateManager, orientation);
315 // set default render-task viewport parameters
316 RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask(0u);
317 defaultRenderTask->SetViewport(newSize);
320 bool Scene::EmitKeyEventGeneratedSignal(const Dali::KeyEvent& event)
322 // Emit the KeyEventGenerated signal when KeyEvent is generated
323 Dali::Integration::Scene handle( this );
324 return mKeyEventGeneratedSignal.Emit( event );
327 void Scene::EmitEventProcessingFinishedSignal()
329 if ( !mEventProcessingFinishedSignal.Empty() )
331 Dali::Integration::Scene handle( this );
332 mEventProcessingFinishedSignal.Emit();
336 void Scene::EmitTouchedSignal( const Dali::TouchEvent& touch )
338 Dali::Integration::Scene handle( this );
339 if ( !mTouchedSignal.Empty() )
341 mTouchedSignal.Emit( touch );
345 void Scene::EmitWheelEventSignal(const Dali::WheelEvent& event)
347 if ( !mWheelEventSignal.Empty() )
349 Dali::Integration::Scene handle( this );
350 mWheelEventSignal.Emit( event );
354 void Scene::AddFrameRenderedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
356 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
357 AddFrameRenderedCallbackMessage( tls->GetEventThreadServices(), *mSceneObject, callback.release(), frameId );
360 void Scene::AddFramePresentedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
362 ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
363 AddFramePresentedCallbackMessage( tls->GetEventThreadServices(), *mSceneObject, callback.release(), frameId );
366 void Scene::GetFrameRenderedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks )
368 mSceneObject->GetFrameRenderedCallback( callbacks );
371 void Scene::GetFramePresentedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks )
373 mSceneObject->GetFramePresentedCallback( callbacks );
376 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
378 return mKeyEventSignal;
381 Integration::Scene::KeyEventGeneratedSignalType& Scene::KeyEventGeneratedSignal()
383 return mKeyEventGeneratedSignal;
386 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
388 return mEventProcessingFinishedSignal;
391 Integration::Scene::TouchEventSignalType& Scene::TouchedSignal()
393 return mTouchedSignal;
396 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
398 return mWheelEventSignal;
401 std::vector<Dali::Internal::SceneGraph::DirtyRect>& Scene::GetItemsDirtyRects()
403 return mItemsDirtyRects;