Synchronize the window removal between main thread and render thread
[platform/core/uifw/dali-core.git] / dali / internal / event / common / scene-impl.cpp
1 /*
2  * Copyright (c) 2019 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/public-api/object/type-registry.h>
32 #include <dali/public-api/render-tasks/render-task-list.h>
33 #include <dali/internal/event/rendering/frame-buffer-impl.h>
34 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
35
36 using Dali::Internal::SceneGraph::Node;
37
38 namespace Dali
39 {
40
41 namespace Internal
42 {
43
44 namespace
45 {
46
47 const Vector4 DEFAULT_BACKGROUND_COLOR(0.0f, 0.0f, 0.0f, 1.0f); // Default background color
48
49 } //Unnamed namespace
50
51 ScenePtr Scene::New( const Size& size )
52 {
53   ScenePtr scene = new Scene( size );
54
55   // Second-phase construction
56   scene->Initialize();
57
58   return scene;
59 }
60
61 Scene::Scene( const Size& size )
62 : mSurface( nullptr ),
63   mSize( size ),
64   mSurfaceSize( Vector2::ZERO ),
65   mDpi( Vector2::ZERO ),
66   mBackgroundColor( DEFAULT_BACKGROUND_COLOR ),
67   mDepthTreeDirty( false ),
68   mEventProcessor( *this, ThreadLocalStorage::GetInternal()->GetGestureEventProcessor() )
69 {
70 }
71
72 Scene::~Scene()
73 {
74   if( mDefaultCamera )
75   {
76     // its enough to release the handle so the object is released
77     // don't need to remove it from root actor as root actor will delete the object
78     mDefaultCamera.Reset();
79   }
80
81   if( mRootLayer )
82   {
83     // we are closing down so just delete the root, no point emit disconnect
84     // signals or send messages to update
85     mRootLayer.Reset();
86   }
87
88   if( mRenderTaskList )
89   {
90     mRenderTaskList.Reset();
91   }
92
93   if ( mFrameBuffer )
94   {
95     mFrameBuffer.Reset();
96   }
97
98   // Discard this Scene from the Core
99   Discard();
100 }
101
102 void Scene::Initialize()
103 {
104   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
105
106   DALI_ASSERT_ALWAYS( tls && "Attempt to create scene before core exists!" );
107
108   tls->AddScene( this );
109
110   SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
111
112   // Create the ordered list of layers
113   mLayerList = LayerList::New( updateManager );
114
115   // The scene owns the default layer
116   mRootLayer = Layer::NewRoot( *mLayerList );
117   mRootLayer->SetName("RootLayer");
118   mRootLayer->SetScene( *this );
119
120   // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
121   // This stops actors parented to the stage having their relayout requests propagating
122   // up to the root layer, and down through other children unnecessarily.
123   mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
124
125   // Create the default camera actor first; this is needed by the RenderTaskList
126   // The default camera attributes and position is such that children of the default layer,
127   // can be positioned at (0,0) and be at the top-left of the viewport.
128   mDefaultCamera = CameraActor::New( mSize );
129   mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
130   Add(*(mDefaultCamera.Get()));
131
132   // Create the list of render-tasks
133   mRenderTaskList = RenderTaskList::New();
134
135   // Create the default render-task
136   mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
137 }
138
139 void Scene::Add(Actor& actor)
140 {
141   mRootLayer->Add( actor );
142 }
143
144 void Scene::Remove(Actor& actor)
145 {
146   mRootLayer->Remove( actor );
147 }
148
149 Size Scene::GetSize() const
150 {
151   return mSize;
152 }
153
154 void Scene::SetDpi(Vector2 dpi)
155 {
156   mDpi = dpi;
157 }
158
159 Vector2 Scene::GetDpi() const
160 {
161   return mDpi;
162 }
163
164 RenderTaskList& Scene::GetRenderTaskList() const
165 {
166   return *mRenderTaskList;
167 }
168
169 Dali::Layer Scene::GetRootLayer() const
170 {
171   return Dali::Layer( mRootLayer.Get() );
172 }
173
174 LayerList& Scene::GetLayerList() const
175 {
176   return *mLayerList;
177 }
178
179 uint32_t Scene::GetLayerCount() const
180 {
181   return mLayerList->GetLayerCount();
182 }
183
184 Dali::Layer Scene::GetLayer( uint32_t depth ) const
185 {
186   return Dali::Layer(mLayerList->GetLayer( depth ));
187 }
188
189 CameraActor& Scene::GetDefaultCameraActor()
190 {
191   return *mDefaultCamera;
192 }
193
194 Actor& Scene::GetDefaultRootActor()
195 {
196   return *mRootLayer;
197 }
198
199 void Scene::SetSurface( Integration::RenderSurface& surface )
200 {
201   mSurface = &surface;
202   if ( mSurface )
203   {
204     RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
205
206     mFrameBuffer = Dali::Internal::FrameBuffer::New( surface, Dali::FrameBuffer::Attachment::NONE );
207     defaultRenderTask->SetFrameBuffer( mFrameBuffer );
208
209     SurfaceResized();
210   }
211 }
212
213 void Scene::SurfaceResized()
214 {
215   if( mSurface )
216   {
217     const float fWidth = static_cast<float>( mSurface->GetPositionSize().width );
218     const float fHeight = static_cast<float>( mSurface->GetPositionSize().height );
219
220     if( ( fabsf( mSurfaceSize.width - fWidth ) > Math::MACHINE_EPSILON_1 ) || ( fabsf( mSurfaceSize.height - fHeight ) > Math::MACHINE_EPSILON_1 ) )
221     {
222       Rect<int32_t> newSize( 0, 0, static_cast<int32_t>( mSurface->GetPositionSize().width ), static_cast<int32_t>( mSurface->GetPositionSize().height ) );
223
224       mSurfaceSize.width = fWidth;
225       mSurfaceSize.height = fHeight;
226
227       mSize.width = mSurfaceSize.width;
228       mSize.height = mSurfaceSize.height;
229
230       // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
231       mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
232
233       mRootLayer->SetSize( mSize.width, mSize.height );
234
235       ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
236       SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
237       SetDefaultSurfaceRectMessage( updateManager, newSize ); // truncated
238
239       RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
240
241       // if single render task to screen then set its viewport parameters
242       if( 1 == mRenderTaskList->GetTaskCount() )
243       {
244         if( !defaultRenderTask->GetTargetFrameBuffer() )
245         {
246           defaultRenderTask->SetViewport( newSize ); // truncated
247         }
248       }
249
250       defaultRenderTask->GetFrameBuffer()->SetSize( static_cast<uint32_t>( newSize.width ), static_cast<uint32_t>( newSize.height ) );
251     }
252   }
253 }
254
255 void Scene::SurfaceDeleted()
256 {
257   if ( mFrameBuffer )
258   {
259     // The frame buffer doesn't have a valid render surface any more.
260     mFrameBuffer->MarkSurfaceAsInvalid();
261   }
262 }
263
264 Integration::RenderSurface* Scene::GetSurface() const
265 {
266   return mSurface;
267 }
268
269 void Scene::Discard()
270 {
271   if( ThreadLocalStorage::Created() )
272   {
273     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
274     tls->RemoveScene( this );
275   }
276 }
277
278 void Scene::RequestRebuildDepthTree()
279 {
280   mDepthTreeDirty = true;
281 }
282
283 void Scene::QueueEvent( const Integration::Event& event )
284 {
285   mEventProcessor.QueueEvent( event );
286 }
287
288 void Scene::ProcessEvents()
289 {
290   mEventProcessor.ProcessEvents();
291 }
292
293 void Scene::RebuildDepthTree()
294 {
295   // If the depth tree needs rebuilding, do it in this frame only.
296   if( mDepthTreeDirty )
297   {
298     ActorPtr actor( mRootLayer.Get() );
299     actor->RebuildDepthTree();
300     mDepthTreeDirty = false;
301   }
302 }
303
304 void Scene::SetBackgroundColor( const Vector4& color )
305 {
306   mBackgroundColor = color;
307
308   if( mSurface )
309   {
310     mRenderTaskList->GetTask( 0u )->GetFrameBuffer()->SetBackgroundColor( color );
311   }
312 }
313
314 Vector4 Scene::GetBackgroundColor() const
315 {
316   return mBackgroundColor;
317 }
318
319 void Scene::EmitKeyEventSignal(const KeyEvent& event)
320 {
321   if ( !mKeyEventSignal.Empty() )
322   {
323     Dali::Integration::Scene handle( this );
324     mKeyEventSignal.Emit( event );
325   }
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 TouchEvent& touchEvent, const Dali::TouchData& touch )
338 {
339   Dali::Integration::Scene handle( this );
340   if ( !mTouchedSignal.Empty() )
341   {
342     mTouchedSignal.Emit( touchEvent );
343   }
344   if ( !mTouchSignal.Empty() )
345   {
346     mTouchSignal.Emit( touch );
347   }
348 }
349
350 void Scene::EmitWheelEventSignal(const WheelEvent& event)
351 {
352   if ( !mWheelEventSignal.Empty() )
353   {
354     Dali::Integration::Scene handle( this );
355     mWheelEventSignal.Emit( event );
356   }
357 }
358
359 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
360 {
361   return mKeyEventSignal;
362 }
363
364 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
365 {
366   return mEventProcessingFinishedSignal;
367 }
368
369 Scene::TouchedSignalType& Scene::TouchedSignal()
370 {
371   return mTouchedSignal;
372 }
373
374 Integration::Scene::TouchSignalType& Scene::TouchSignal()
375 {
376   return mTouchSignal;
377 }
378
379 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
380 {
381   return mWheelEventSignal;
382 }
383
384 } // Internal
385
386 } // Dali