Support screen and client rotation
[platform/core/uifw/dali-core.git] / dali / internal / event / common / scene-impl.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/common/scene-impl.h>
20
21 // INTERNAL INCLUDES
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>
37
38 using Dali::Internal::SceneGraph::Node;
39
40 namespace Dali
41 {
42
43 namespace Internal
44 {
45
46 ScenePtr Scene::New(Size size, int orientation)
47 {
48   ScenePtr scene = new Scene;
49
50   // Second-phase construction
51   scene->Initialize(size, orientation);
52
53   return scene;
54 }
55
56 Scene::Scene()
57 : mSceneObject(nullptr),
58   mSize(), // Don't set the proper value here, this will be set when the surface is set later
59   mDpi(),
60   mBackgroundColor(DEFAULT_BACKGROUND_COLOR),
61   mDepthTreeDirty(false),
62   mEventProcessor(*this, ThreadLocalStorage::GetInternal()->GetGestureEventProcessor()),
63   mSurfaceOrientation(0)
64 {
65 }
66
67 Scene::~Scene()
68 {
69   if( EventThreadServices::IsCoreRunning() && mSceneObject )
70   {
71     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
72     RemoveSceneMessage( tls->GetUpdateManager(), *mSceneObject );
73   }
74
75   if( mDefaultCamera )
76   {
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();
80   }
81
82   if( mRootLayer )
83   {
84     // we are closing down so just delete the root, no point emit disconnect
85     // signals or send messages to update
86     mRootLayer.Reset();
87   }
88
89   if( mRenderTaskList )
90   {
91     mRenderTaskList.Reset();
92   }
93
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
96 }
97
98 void Scene::Initialize(Size size, int orientation)
99 {
100   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
101
102   DALI_ASSERT_ALWAYS( tls && "Attempt to create scene before core exists!" );
103
104   tls->AddScene( this );
105
106   SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
107
108   // Create the ordered list of layers
109   mLayerList = LayerList::New( updateManager );
110
111   // The scene owns the default layer
112   mRootLayer = Layer::NewRoot( *mLayerList );
113   mRootLayer->SetName("RootLayer");
114   mRootLayer->SetScene( *this );
115
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 );
120
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()));
127
128   // Create the list of render-tasks
129   mRenderTaskList = RenderTaskList::New();
130
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;
135
136   SurfaceRotated( size.width, size.height, mSurfaceOrientation );
137
138   // Create scene graph object
139   mSceneObject = new SceneGraph::Scene();
140   OwnerPointer< SceneGraph::Scene > transferOwnership( const_cast< SceneGraph::Scene* >( mSceneObject ) );
141   AddSceneMessage( updateManager, transferOwnership );
142 }
143
144 void Scene::Add(Actor& actor)
145 {
146   mRootLayer->Add( actor );
147 }
148
149 void Scene::Remove(Actor& actor)
150 {
151   mRootLayer->Remove( actor );
152 }
153
154 Size Scene::GetSize() const
155 {
156   return mSize;
157 }
158
159 void Scene::SetDpi(Vector2 dpi)
160 {
161   mDpi = dpi;
162 }
163
164 Vector2 Scene::GetDpi() const
165 {
166   return mDpi;
167 }
168
169 RenderTaskList& Scene::GetRenderTaskList() const
170 {
171   return *mRenderTaskList;
172 }
173
174 Dali::Layer Scene::GetRootLayer() const
175 {
176   return Dali::Layer( mRootLayer.Get() );
177 }
178
179 LayerList& Scene::GetLayerList() const
180 {
181   return *mLayerList;
182 }
183
184 uint32_t Scene::GetLayerCount() const
185 {
186   return mLayerList->GetLayerCount();
187 }
188
189 Dali::Layer Scene::GetLayer( uint32_t depth ) const
190 {
191   return Dali::Layer(mLayerList->GetLayer( depth ));
192 }
193
194 CameraActor& Scene::GetDefaultCameraActor()
195 {
196   return *mDefaultCamera;
197 }
198
199 Actor& Scene::GetDefaultRootActor()
200 {
201   return *mRootLayer;
202 }
203
204 void Scene::SurfaceResized(float width, float height)
205 {
206   if((fabsf(mSize.width - width) > Math::MACHINE_EPSILON_1) || (fabsf(mSize.height - height) > Math::MACHINE_EPSILON_1))
207   {
208     ChangedSurface(width, height, mSurfaceOrientation);
209   }
210 }
211
212 void Scene::SurfaceReplaced()
213 {
214   if ( mSceneObject )
215   {
216     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
217     SurfaceReplacedMessage( tls->GetUpdateManager(), *mSceneObject );
218   }
219 }
220
221 void Scene::Discard()
222 {
223   if( ThreadLocalStorage::Created() )
224   {
225     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
226     tls->RemoveScene( this );
227   }
228 }
229
230 void Scene::RequestRebuildDepthTree()
231 {
232   mDepthTreeDirty = true;
233 }
234
235 void Scene::QueueEvent( const Integration::Event& event )
236 {
237   mEventProcessor.QueueEvent( event );
238 }
239
240 void Scene::ProcessEvents()
241 {
242   mEventProcessor.ProcessEvents();
243 }
244
245 void Scene::RebuildDepthTree()
246 {
247   // If the depth tree needs rebuilding, do it in this frame only.
248   if( mDepthTreeDirty )
249   {
250     ActorPtr actor( mRootLayer.Get() );
251     actor->RebuildDepthTree();
252     mDepthTreeDirty = false;
253   }
254 }
255
256 void Scene::SetBackgroundColor( const Vector4& color )
257 {
258   mBackgroundColor = color;
259
260   mRenderTaskList->GetTask( 0u )->SetClearColor( color );
261   mRenderTaskList->GetTask( 0u )->SetClearEnabled( true );
262 }
263
264 Vector4 Scene::GetBackgroundColor() const
265 {
266   return mBackgroundColor;
267 }
268
269 SceneGraph::Scene* Scene::GetSceneObject() const
270 {
271   return mSceneObject;
272 }
273
274 void Scene::EmitKeyEventSignal(const Dali::KeyEvent& event)
275 {
276   if ( !mKeyEventSignal.Empty() )
277   {
278     Dali::Integration::Scene handle( this );
279     mKeyEventSignal.Emit( event );
280   }
281 }
282
283 void Scene::SurfaceRotated(float width, float height, int orientation)
284 {
285   mSurfaceOrientation = orientation;
286   ChangedSurface(width, height, orientation);
287 }
288
289 int Scene::GetSurfaceOrientation()
290 {
291   return mSurfaceOrientation;
292 }
293
294 void Scene::ChangedSurface(float width, float height, int orientation)
295 {
296   Rect<int32_t> newSize(0, 0, static_cast<int32_t>(width), static_cast<int32_t>(height)); // truncated
297
298   mSize.width  = width;
299   mSize.height = height;
300
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);
305
306   mRootLayer->SetSize(width, height);
307
308   ThreadLocalStorage*        tls           = ThreadLocalStorage::GetInternal();
309   SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
310   SetDefaultSurfaceRectMessage(updateManager, newSize);
311
312   // Send the surface orientation to render manager for calculating glViewport/Scissor
313   SetDefaultSurfaceOrientationMessage(updateManager, orientation);
314
315   // set default render-task viewport parameters
316   RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask(0u);
317   defaultRenderTask->SetViewport(newSize);
318 }
319
320 bool Scene::EmitKeyEventGeneratedSignal(const Dali::KeyEvent& event)
321 {
322   // Emit the KeyEventGenerated signal when KeyEvent is generated
323   Dali::Integration::Scene handle( this );
324   return mKeyEventGeneratedSignal.Emit( event );
325 }
326
327 void Scene::EmitEventProcessingFinishedSignal()
328 {
329   if ( !mEventProcessingFinishedSignal.Empty() )
330   {
331     Dali::Integration::Scene handle( this );
332     mEventProcessingFinishedSignal.Emit();
333   }
334 }
335
336 void Scene::EmitTouchedSignal( const Dali::TouchEvent& touch )
337 {
338   Dali::Integration::Scene handle( this );
339   if ( !mTouchedSignal.Empty() )
340   {
341     mTouchedSignal.Emit( touch );
342   }
343 }
344
345 void Scene::EmitWheelEventSignal(const Dali::WheelEvent& event)
346 {
347   if ( !mWheelEventSignal.Empty() )
348   {
349     Dali::Integration::Scene handle( this );
350     mWheelEventSignal.Emit( event );
351   }
352 }
353
354 void Scene::AddFrameRenderedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
355 {
356   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
357   AddFrameRenderedCallbackMessage( tls->GetEventThreadServices(), *mSceneObject, callback.release(), frameId );
358 }
359
360 void Scene::AddFramePresentedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
361 {
362   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
363   AddFramePresentedCallbackMessage( tls->GetEventThreadServices(), *mSceneObject, callback.release(), frameId );
364 }
365
366 void Scene::GetFrameRenderedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks )
367 {
368   mSceneObject->GetFrameRenderedCallback( callbacks );
369 }
370
371 void Scene::GetFramePresentedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks )
372 {
373   mSceneObject->GetFramePresentedCallback( callbacks );
374 }
375
376 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
377 {
378   return mKeyEventSignal;
379 }
380
381 Integration::Scene::KeyEventGeneratedSignalType& Scene::KeyEventGeneratedSignal()
382 {
383   return mKeyEventGeneratedSignal;
384 }
385
386 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
387 {
388   return mEventProcessingFinishedSignal;
389 }
390
391 Integration::Scene::TouchEventSignalType& Scene::TouchedSignal()
392 {
393   return mTouchedSignal;
394 }
395
396 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
397 {
398   return mWheelEventSignal;
399 }
400
401 std::vector<Dali::Internal::SceneGraph::DirtyRect>& Scene::GetItemsDirtyRects()
402 {
403   return mItemsDirtyRects;
404 }
405
406 } // Internal
407
408 } // Dali