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