[Tizen] Add screen and client rotation itself function
[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 )
47 {
48   ScenePtr scene = new Scene;
49
50   // Second-phase construction
51   scene->Initialize( size, 0 );
52
53   return scene;
54 }
55
56 ScenePtr Scene::New( Size size, int orientation )
57 {
58   ScenePtr scene = new Scene;
59
60   // Second-phase construction
61   scene->Initialize( size, orientation );
62
63   return scene;
64 }
65
66 Scene::Scene()
67 : mSceneObject( nullptr ),
68   mSize(), // Don't set the proper value here, this will be set when the surface is set later
69   mDpi(),
70   mBackgroundColor( DEFAULT_BACKGROUND_COLOR ),
71   mSurfaceOrientation( 0 ),
72   mDepthTreeDirty( false ),
73   mEventProcessor( *this, ThreadLocalStorage::GetInternal()->GetGestureEventProcessor() )
74 {
75 }
76
77 Scene::~Scene()
78 {
79   if( EventThreadServices::IsCoreRunning() && mSceneObject )
80   {
81     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
82     RemoveSceneMessage( tls->GetUpdateManager(), *mSceneObject );
83   }
84
85   if( mDefaultCamera )
86   {
87     // its enough to release the handle so the object is released
88     // don't need to remove it from root actor as root actor will delete the object
89     mDefaultCamera.Reset();
90   }
91
92   if( mRootLayer )
93   {
94     // we are closing down so just delete the root, no point emit disconnect
95     // signals or send messages to update
96     mRootLayer.Reset();
97   }
98
99   if( mRenderTaskList )
100   {
101     mRenderTaskList.Reset();
102   }
103
104   // No need to discard this Scene from Core, as Core stores an intrusive_ptr to this scene
105   // When this destructor is called, the scene has either already been removed from Core or Core has already been destroyed
106 }
107
108 void Scene::Initialize( Size size, int orientation )
109 {
110   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
111
112   DALI_ASSERT_ALWAYS( tls && "Attempt to create scene before core exists!" );
113
114   tls->AddScene( this );
115
116   SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
117
118   // Create the ordered list of layers
119   mLayerList = LayerList::New( updateManager );
120
121   // The scene owns the default layer
122   mRootLayer = Layer::NewRoot( *mLayerList );
123   mRootLayer->SetName("RootLayer");
124   mRootLayer->SetScene( *this );
125
126   // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
127   // This stops actors parented to the stage having their relayout requests propagating
128   // up to the root layer, and down through other children unnecessarily.
129   mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
130
131   // Create the default camera actor first; this is needed by the RenderTaskList
132   // The default camera attributes and position is such that children of the default layer,
133   // can be positioned at (0,0) and be at the top-left of the viewport.
134   mDefaultCamera = CameraActor::New( size );
135   mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
136   Add(*(mDefaultCamera.Get()));
137
138   // Create the list of render-tasks
139   mRenderTaskList = RenderTaskList::New();
140
141   // Create the default render-task and ensure clear is enabled on it to show the background color
142   RenderTaskPtr renderTask = mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
143   renderTask->SetClearEnabled(true);
144   mSurfaceOrientation = orientation;
145
146   SurfaceResized( size.width, size.height, mSurfaceOrientation, false );
147
148   // Create scene graph object
149   mSceneObject = new SceneGraph::Scene();
150   OwnerPointer< SceneGraph::Scene > transferOwnership( const_cast< SceneGraph::Scene* >( mSceneObject ) );
151   AddSceneMessage( updateManager, transferOwnership );
152 }
153
154 void Scene::Add(Actor& actor)
155 {
156   mRootLayer->Add( actor );
157 }
158
159 void Scene::Remove(Actor& actor)
160 {
161   mRootLayer->Remove( actor );
162 }
163
164 Size Scene::GetSize() const
165 {
166   return mSize;
167 }
168
169 void Scene::SetDpi(Vector2 dpi)
170 {
171   mDpi = dpi;
172 }
173
174 Vector2 Scene::GetDpi() const
175 {
176   return mDpi;
177 }
178
179 RenderTaskList& Scene::GetRenderTaskList() const
180 {
181   return *mRenderTaskList;
182 }
183
184 Dali::Layer Scene::GetRootLayer() const
185 {
186   return Dali::Layer( mRootLayer.Get() );
187 }
188
189 LayerList& Scene::GetLayerList() const
190 {
191   return *mLayerList;
192 }
193
194 uint32_t Scene::GetLayerCount() const
195 {
196   return mLayerList->GetLayerCount();
197 }
198
199 Dali::Layer Scene::GetLayer( uint32_t depth ) const
200 {
201   return Dali::Layer(mLayerList->GetLayer( depth ));
202 }
203
204 CameraActor& Scene::GetDefaultCameraActor()
205 {
206   return *mDefaultCamera;
207 }
208
209 Actor& Scene::GetDefaultRootActor()
210 {
211   return *mRootLayer;
212 }
213
214 void Scene::SurfaceResized( float width, float height, int orientation, bool forceUpdate )
215 {
216   if( ( fabsf( mSize.width - width ) > Math::MACHINE_EPSILON_1 ) || ( fabsf( mSize.height - height ) > Math::MACHINE_EPSILON_1 )
217       || ( orientation != mSurfaceOrientation ) || ( forceUpdate ) )
218   {
219     Rect< int32_t > newSize( 0, 0, static_cast< int32_t >( width ), static_cast< int32_t >( height ) ); // truncated
220
221     mSize.width = width;
222     mSize.height = height;
223     mSurfaceOrientation = orientation;
224
225     // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
226     mDefaultCamera->SetPerspectiveProjection( mSize );
227     mDefaultCamera->RotateProjection( mSurfaceOrientation );
228
229     mRootLayer->SetSize( mSize.width, mSize.height );
230
231     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
232     SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
233     SetDefaultSurfaceRectMessage( updateManager, newSize );
234     SetDefaultSurfaceOrientationMessage( updateManager, mSurfaceOrientation );
235
236     // set default render-task viewport parameters
237     RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
238     defaultRenderTask->SetViewport( newSize );
239   }
240 }
241
242 void Scene::SurfaceReplaced()
243 {
244   if ( mSceneObject )
245   {
246     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
247     SurfaceReplacedMessage( tls->GetUpdateManager(), *mSceneObject );
248   }
249 }
250
251 void Scene::Discard()
252 {
253   if( ThreadLocalStorage::Created() )
254   {
255     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
256     tls->RemoveScene( this );
257   }
258 }
259
260 void Scene::RequestRebuildDepthTree()
261 {
262   mDepthTreeDirty = true;
263 }
264
265 void Scene::QueueEvent( const Integration::Event& event )
266 {
267   mEventProcessor.QueueEvent( event );
268 }
269
270 void Scene::ProcessEvents()
271 {
272   mEventProcessor.ProcessEvents();
273 }
274
275 void Scene::RebuildDepthTree()
276 {
277   // If the depth tree needs rebuilding, do it in this frame only.
278   if( mDepthTreeDirty )
279   {
280     ActorPtr actor( mRootLayer.Get() );
281     actor->RebuildDepthTree();
282     mDepthTreeDirty = false;
283   }
284 }
285
286 void Scene::SetBackgroundColor( const Vector4& color )
287 {
288   mBackgroundColor = color;
289
290   mRenderTaskList->GetTask( 0u )->SetClearColor( color );
291   mRenderTaskList->GetTask( 0u )->SetClearEnabled( true );
292 }
293
294 Vector4 Scene::GetBackgroundColor() const
295 {
296   return mBackgroundColor;
297 }
298
299 SceneGraph::Scene* Scene::GetSceneObject() const
300 {
301   return mSceneObject;
302 }
303
304 void Scene::EmitKeyEventSignal(const Dali::KeyEvent& event)
305 {
306   if ( !mKeyEventSignal.Empty() )
307   {
308     Dali::Integration::Scene handle( this );
309     mKeyEventSignal.Emit( event );
310   }
311 }
312
313 bool Scene::EmitKeyEventGeneratedSignal(const Dali::KeyEvent& event)
314 {
315   // Emit the KeyEventGenerated signal when KeyEvent is generated
316   Dali::Integration::Scene handle( this );
317   return mKeyEventGeneratedSignal.Emit( event );
318 }
319
320 void Scene::EmitEventProcessingFinishedSignal()
321 {
322   if ( !mEventProcessingFinishedSignal.Empty() )
323   {
324     Dali::Integration::Scene handle( this );
325     mEventProcessingFinishedSignal.Emit();
326   }
327 }
328
329 void Scene::EmitTouchedSignal( const Dali::TouchEvent& touch )
330 {
331   Dali::Integration::Scene handle( this );
332   if ( !mTouchedSignal.Empty() )
333   {
334     mTouchedSignal.Emit( touch );
335   }
336 }
337
338 void Scene::EmitWheelEventSignal(const Dali::WheelEvent& event)
339 {
340   if ( !mWheelEventSignal.Empty() )
341   {
342     Dali::Integration::Scene handle( this );
343     mWheelEventSignal.Emit( event );
344   }
345 }
346
347 void Scene::AddFrameRenderedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
348 {
349   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
350   AddFrameRenderedCallbackMessage( tls->GetEventThreadServices(), *mSceneObject, callback.release(), frameId );
351 }
352
353 void Scene::AddFramePresentedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
354 {
355   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
356   AddFramePresentedCallbackMessage( tls->GetEventThreadServices(), *mSceneObject, callback.release(), frameId );
357 }
358
359 void Scene::GetFrameRenderedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks )
360 {
361   mSceneObject->GetFrameRenderedCallback( callbacks );
362 }
363
364 void Scene::GetFramePresentedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks )
365 {
366   mSceneObject->GetFramePresentedCallback( callbacks );
367 }
368
369 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
370 {
371   return mKeyEventSignal;
372 }
373
374 Integration::Scene::KeyEventGeneratedSignalType& Scene::KeyEventGeneratedSignal()
375 {
376   return mKeyEventGeneratedSignal;
377 }
378
379 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
380 {
381   return mEventProcessingFinishedSignal;
382 }
383
384 Integration::Scene::TouchEventSignalType& Scene::TouchedSignal()
385 {
386   return mTouchedSignal;
387 }
388
389 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
390 {
391   return mWheelEventSignal;
392 }
393
394 std::vector<Dali::Internal::SceneGraph::DirtyRect>& Scene::GetItemsDirtyRects()
395 {
396   return mItemsDirtyRects;
397 }
398
399 } // Internal
400
401 } // Dali