917f76a2bc2de6801d8068a3fcf96155b23609f7
[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( Integration::RenderSurface& surface )
52 {
53   ScenePtr scene = new Scene;
54
55   // Second-phase construction
56   scene->Initialize( surface );
57
58   return scene;
59 }
60
61 Scene::Scene()
62 : mSurface( nullptr ),
63   mSize(), // Don't set the proper value here, this will be set when the surface is set later
64   mDpi(),
65   mBackgroundColor( DEFAULT_BACKGROUND_COLOR ),
66   mSurfaceOrientation( 0 ),
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   // No need to discard this Scene from Core, as Core stores an intrusive_ptr to this scene
99   // When this destructor is called, the scene has either already been removed from Core or Core has already been destroyed
100 }
101
102 void Scene::Initialize( Integration::RenderSurface& surface )
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   const PositionSize positionSize = surface.GetPositionSize();
129   const Vector2 surfaceSize( static_cast< float >( positionSize.width ), static_cast< float >( positionSize.height ) );
130   mDefaultCamera = CameraActor::New( surfaceSize );
131   mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
132   Add(*(mDefaultCamera.Get()));
133
134   // Create the list of render-tasks
135   mRenderTaskList = RenderTaskList::New();
136
137   // Create the default render-task
138   mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
139
140   // Set the surface
141   SetSurface( surface );
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::SetSurface( Integration::RenderSurface& surface )
205 {
206   if( mSurface != &surface )
207   {
208     mSurface = &surface;
209
210     RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
211
212     mFrameBuffer = Dali::Internal::FrameBuffer::New( surface, Dali::FrameBuffer::Attachment::NONE );
213     defaultRenderTask->SetFrameBuffer( mFrameBuffer );
214
215     SurfaceResized( false );
216   }
217 }
218
219 void Scene::SurfaceResized( bool forceUpdate )
220 {
221   if( mSurface )
222   {
223     const PositionSize surfacePositionSize = mSurface->GetPositionSize();
224     const float fWidth = static_cast< float >( surfacePositionSize.width );
225     const float fHeight = static_cast< float >( surfacePositionSize.height );
226     const int orientation = mSurface->GetOrientation();
227
228     if( ( ( fabsf( mSize.width - fWidth ) > Math::MACHINE_EPSILON_1 ) || ( fabsf( mSize.height - fHeight ) > Math::MACHINE_EPSILON_1 ) )
229             || ( orientation != mSurfaceOrientation )
230             || ( forceUpdate ) )
231     {
232       Rect< int32_t > newSize( 0, 0, static_cast< int32_t >( surfacePositionSize.width ), static_cast< int32_t >( surfacePositionSize.height ) ); // truncated
233
234       mSize.width = fWidth;
235       mSize.height = fHeight;
236       mSurfaceOrientation = orientation;
237
238       // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
239       mDefaultCamera->SetPerspectiveProjection( mSize );
240       mDefaultCamera->RotateProjection( mSurfaceOrientation );
241
242       mRootLayer->SetSize( mSize.width, mSize.height );
243
244       ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
245       SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
246       SetDefaultSurfaceRectMessage( updateManager, newSize );
247       SetDefaultSurfaceOrientationMessage( updateManager, mSurfaceOrientation );
248
249       // set default render-task viewport parameters
250       RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
251       defaultRenderTask->SetViewport( newSize );
252       defaultRenderTask->GetFrameBuffer()->SetSize( static_cast<uint32_t>( newSize.width ), static_cast<uint32_t>( newSize.height ) );
253     }
254   }
255 }
256
257 void Scene::SurfaceDeleted()
258 {
259   mSurface = nullptr;
260   if ( mFrameBuffer )
261   {
262     // The frame buffer doesn't have a valid render surface any more.
263     mFrameBuffer->MarkSurfaceAsInvalid();
264   }
265 }
266
267 Integration::RenderSurface* Scene::GetSurface() const
268 {
269   return mSurface;
270 }
271
272 void Scene::Discard()
273 {
274   if( ThreadLocalStorage::Created() )
275   {
276     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
277     tls->RemoveScene( this );
278   }
279 }
280
281 void Scene::RequestRebuildDepthTree()
282 {
283   mDepthTreeDirty = true;
284 }
285
286 void Scene::QueueEvent( const Integration::Event& event )
287 {
288   mEventProcessor.QueueEvent( event );
289 }
290
291 void Scene::ProcessEvents()
292 {
293   mEventProcessor.ProcessEvents();
294 }
295
296 void Scene::RebuildDepthTree()
297 {
298   // If the depth tree needs rebuilding, do it in this frame only.
299   if( mDepthTreeDirty )
300   {
301     ActorPtr actor( mRootLayer.Get() );
302     actor->RebuildDepthTree();
303     mDepthTreeDirty = false;
304   }
305 }
306
307 void Scene::SetBackgroundColor( const Vector4& color )
308 {
309   mBackgroundColor = color;
310
311   if( mSurface )
312   {
313     mRenderTaskList->GetTask( 0u )->GetFrameBuffer()->SetBackgroundColor( color );
314   }
315 }
316
317 Vector4 Scene::GetBackgroundColor() const
318 {
319   return mBackgroundColor;
320 }
321
322 void Scene::EmitKeyEventSignal(const KeyEvent& event)
323 {
324   if ( !mKeyEventSignal.Empty() )
325   {
326     Dali::Integration::Scene handle( this );
327     mKeyEventSignal.Emit( event );
328   }
329 }
330
331 bool Scene::EmitKeyEventGeneratedSignal(const KeyEvent& event)
332 {
333   // Emit the KeyEventGenerated signal when KeyEvent is generated
334   return mKeyEventGeneratedSignal.Emit( event );
335 }
336
337 void Scene::EmitEventProcessingFinishedSignal()
338 {
339   if ( !mEventProcessingFinishedSignal.Empty() )
340   {
341     Dali::Integration::Scene handle( this );
342     mEventProcessingFinishedSignal.Emit();
343   }
344 }
345
346 void Scene::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
347 {
348   Dali::Integration::Scene handle( this );
349   if ( !mTouchedSignal.Empty() )
350   {
351     mTouchedSignal.Emit( touchEvent );
352   }
353   if ( !mTouchSignal.Empty() )
354   {
355     mTouchSignal.Emit( touch );
356   }
357 }
358
359 void Scene::EmitWheelEventSignal(const WheelEvent& event)
360 {
361   if ( !mWheelEventSignal.Empty() )
362   {
363     Dali::Integration::Scene handle( this );
364     mWheelEventSignal.Emit( event );
365   }
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 Scene::TouchedSignalType& Scene::TouchedSignal()
384 {
385   return mTouchedSignal;
386 }
387
388 Integration::Scene::TouchSignalType& Scene::TouchSignal()
389 {
390   return mTouchSignal;
391 }
392
393 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
394 {
395   return mWheelEventSignal;
396 }
397
398 } // Internal
399
400 } // Dali