[Tizen] Fix the sync issue of window 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 )
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
145   // Create scene graph object
146   mSceneObject = new SceneGraph::Scene();
147   OwnerPointer< SceneGraph::Scene > transferOwnership( const_cast< SceneGraph::Scene* >( mSceneObject ) );
148   AddSceneMessage( updateManager, transferOwnership );
149
150   SurfaceResized( size.width, size.height, orientation, false );
151 }
152
153 void Scene::Add(Actor& actor)
154 {
155   mRootLayer->Add( actor );
156 }
157
158 void Scene::Remove(Actor& actor)
159 {
160   mRootLayer->Remove( actor );
161 }
162
163 Size Scene::GetSize() const
164 {
165   return mSize;
166 }
167
168 void Scene::SetDpi(Vector2 dpi)
169 {
170   mDpi = dpi;
171 }
172
173 Vector2 Scene::GetDpi() const
174 {
175   return mDpi;
176 }
177
178 RenderTaskList& Scene::GetRenderTaskList() const
179 {
180   return *mRenderTaskList;
181 }
182
183 Dali::Layer Scene::GetRootLayer() const
184 {
185   return Dali::Layer( mRootLayer.Get() );
186 }
187
188 LayerList& Scene::GetLayerList() const
189 {
190   return *mLayerList;
191 }
192
193 uint32_t Scene::GetLayerCount() const
194 {
195   return mLayerList->GetLayerCount();
196 }
197
198 Dali::Layer Scene::GetLayer( uint32_t depth ) const
199 {
200   return Dali::Layer(mLayerList->GetLayer( depth ));
201 }
202
203 CameraActor& Scene::GetDefaultCameraActor()
204 {
205   return *mDefaultCamera;
206 }
207
208 Actor& Scene::GetDefaultRootActor()
209 {
210   return *mRootLayer;
211 }
212
213 void Scene::SurfaceResized( float width, float height, int orientation, bool forceUpdate )
214 {
215   if( ( fabsf( mSize.width - width ) > Math::MACHINE_EPSILON_1 ) || ( fabsf( mSize.height - height ) > Math::MACHINE_EPSILON_1 )
216       || ( orientation != mSurfaceOrientation ) || ( forceUpdate ) )
217   {
218     Rect< int32_t > newSize( 0, 0, static_cast< int32_t >( width ), static_cast< int32_t >( height ) ); // truncated
219
220     mSize.width = width;
221     mSize.height = height;
222     mSurfaceOrientation = orientation;
223
224     // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
225     mDefaultCamera->SetPerspectiveProjection( mSize );
226     mDefaultCamera->RotateProjection( mSurfaceOrientation );
227
228     mRootLayer->SetSize( mSize.width, mSize.height );
229
230     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
231     SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
232     SetDefaultSurfaceRectMessage( updateManager, newSize );
233     SetSurfaceOrientationMessage( tls->GetEventThreadServices(), *mSceneObject, mSurfaceOrientation );
234
235     // set default render-task viewport parameters
236     RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
237     defaultRenderTask->SetViewport( newSize );
238   }
239 }
240
241 void Scene::SurfaceReplaced()
242 {
243   if ( mSceneObject )
244   {
245     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
246     SurfaceReplacedMessage( tls->GetUpdateManager(), *mSceneObject );
247   }
248 }
249
250 void Scene::Discard()
251 {
252   if( ThreadLocalStorage::Created() )
253   {
254     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
255     tls->RemoveScene( this );
256   }
257 }
258
259 void Scene::RequestRebuildDepthTree()
260 {
261   mDepthTreeDirty = true;
262 }
263
264 void Scene::QueueEvent( const Integration::Event& event )
265 {
266   mEventProcessor.QueueEvent( event );
267 }
268
269 void Scene::ProcessEvents()
270 {
271   mEventProcessor.ProcessEvents();
272 }
273
274 void Scene::RebuildDepthTree()
275 {
276   // If the depth tree needs rebuilding, do it in this frame only.
277   if( mDepthTreeDirty )
278   {
279     ActorPtr actor( mRootLayer.Get() );
280     actor->RebuildDepthTree();
281     mDepthTreeDirty = false;
282   }
283 }
284
285 void Scene::SetBackgroundColor( const Vector4& color )
286 {
287   mBackgroundColor = color;
288
289   mRenderTaskList->GetTask( 0u )->SetClearColor( color );
290   mRenderTaskList->GetTask( 0u )->SetClearEnabled( true );
291 }
292
293 Vector4 Scene::GetBackgroundColor() const
294 {
295   return mBackgroundColor;
296 }
297
298 SceneGraph::Scene* Scene::GetSceneObject() const
299 {
300   return mSceneObject;
301 }
302
303 void Scene::EmitKeyEventSignal(const Dali::KeyEvent& event)
304 {
305   if ( !mKeyEventSignal.Empty() )
306   {
307     Dali::Integration::Scene handle( this );
308     mKeyEventSignal.Emit( event );
309   }
310 }
311
312 bool Scene::EmitKeyEventGeneratedSignal(const Dali::KeyEvent& event)
313 {
314   // Emit the KeyEventGenerated signal when KeyEvent is generated
315   Dali::Integration::Scene handle( this );
316   return mKeyEventGeneratedSignal.Emit( event );
317 }
318
319 void Scene::EmitEventProcessingFinishedSignal()
320 {
321   if ( !mEventProcessingFinishedSignal.Empty() )
322   {
323     Dali::Integration::Scene handle( this );
324     mEventProcessingFinishedSignal.Emit();
325   }
326 }
327
328 void Scene::EmitTouchedSignal( const Dali::TouchEvent& touch )
329 {
330   Dali::Integration::Scene handle( this );
331   if ( !mTouchedSignal.Empty() )
332   {
333     mTouchedSignal.Emit( touch );
334   }
335 }
336
337 void Scene::EmitWheelEventSignal(const Dali::WheelEvent& event)
338 {
339   if ( !mWheelEventSignal.Empty() )
340   {
341     Dali::Integration::Scene handle( this );
342     mWheelEventSignal.Emit( event );
343   }
344 }
345
346 void Scene::AddFrameRenderedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
347 {
348   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
349   AddFrameRenderedCallbackMessage( tls->GetEventThreadServices(), *mSceneObject, callback.release(), frameId );
350 }
351
352 void Scene::AddFramePresentedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
353 {
354   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
355   AddFramePresentedCallbackMessage( tls->GetEventThreadServices(), *mSceneObject, callback.release(), frameId );
356 }
357
358 void Scene::GetFrameRenderedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks )
359 {
360   mSceneObject->GetFrameRenderedCallback( callbacks );
361 }
362
363 void Scene::GetFramePresentedCallback( Dali::Integration::Scene::FrameCallbackContainer& callbacks )
364 {
365   mSceneObject->GetFramePresentedCallback( callbacks );
366 }
367
368 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
369 {
370   return mKeyEventSignal;
371 }
372
373 Integration::Scene::KeyEventGeneratedSignalType& Scene::KeyEventGeneratedSignal()
374 {
375   return mKeyEventGeneratedSignal;
376 }
377
378 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
379 {
380   return mEventProcessingFinishedSignal;
381 }
382
383 Integration::Scene::TouchEventSignalType& Scene::TouchedSignal()
384 {
385   return mTouchedSignal;
386 }
387
388 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
389 {
390   return mWheelEventSignal;
391 }
392
393 std::vector<Dali::Internal::SceneGraph::DirtyRect>& Scene::GetItemsDirtyRects()
394 {
395   return mItemsDirtyRects;
396 }
397
398 } // Internal
399
400 } // Dali