[Tizen] Support Client Rotation and Screen Rotation
[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   mSurfaceOrientation( 0 ),
68   mDepthTreeDirty( false ),
69   mEventProcessor( *this, ThreadLocalStorage::GetInternal()->GetGestureEventProcessor() )
70 {
71 }
72
73 Scene::~Scene()
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   if ( mFrameBuffer )
95   {
96     mFrameBuffer.Reset();
97   }
98
99   // Discard this Scene from the Core
100   Discard();
101 }
102
103 void Scene::Initialize()
104 {
105   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
106
107   DALI_ASSERT_ALWAYS( tls && "Attempt to create scene before core exists!" );
108
109   tls->AddScene( this );
110
111   SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
112
113   // Create the ordered list of layers
114   mLayerList = LayerList::New( updateManager );
115
116   // The scene owns the default layer
117   mRootLayer = Layer::NewRoot( *mLayerList );
118   mRootLayer->SetName("RootLayer");
119   mRootLayer->SetScene( *this );
120
121   // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
122   // This stops actors parented to the stage having their relayout requests propagating
123   // up to the root layer, and down through other children unnecessarily.
124   mRootLayer->SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
125
126   // Create the default camera actor first; this is needed by the RenderTaskList
127   // The default camera attributes and position is such that children of the default layer,
128   // can be positioned at (0,0) and be at the top-left of the viewport.
129   mDefaultCamera = CameraActor::New( mSize );
130   mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
131   Add(*(mDefaultCamera.Get()));
132
133   // Create the list of render-tasks
134   mRenderTaskList = RenderTaskList::New();
135
136   // Create the default render-task
137   mRenderTaskList->CreateTask( mRootLayer.Get(), mDefaultCamera.Get() );
138 }
139
140 void Scene::Add(Actor& actor)
141 {
142   mRootLayer->Add( actor );
143 }
144
145 void Scene::Remove(Actor& actor)
146 {
147   mRootLayer->Remove( actor );
148 }
149
150 Size Scene::GetSize() const
151 {
152   return mSize;
153 }
154
155 void Scene::SetDpi(Vector2 dpi)
156 {
157   mDpi = dpi;
158 }
159
160 Vector2 Scene::GetDpi() const
161 {
162   return mDpi;
163 }
164
165 RenderTaskList& Scene::GetRenderTaskList() const
166 {
167   return *mRenderTaskList;
168 }
169
170 Dali::Layer Scene::GetRootLayer() const
171 {
172   return Dali::Layer( mRootLayer.Get() );
173 }
174
175 LayerList& Scene::GetLayerList() const
176 {
177   return *mLayerList;
178 }
179
180 uint32_t Scene::GetLayerCount() const
181 {
182   return mLayerList->GetLayerCount();
183 }
184
185 Dali::Layer Scene::GetLayer( uint32_t depth ) const
186 {
187   return Dali::Layer(mLayerList->GetLayer( depth ));
188 }
189
190 CameraActor& Scene::GetDefaultCameraActor()
191 {
192   return *mDefaultCamera;
193 }
194
195 Actor& Scene::GetDefaultRootActor()
196 {
197   return *mRootLayer;
198 }
199
200 void Scene::SetSurface( Integration::RenderSurface& surface, bool forceUpdate )
201 {
202   mSurface = &surface;
203   if ( mSurface )
204   {
205     RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
206
207     mFrameBuffer = Dali::Internal::FrameBuffer::New( surface, Dali::FrameBuffer::Attachment::NONE );
208     defaultRenderTask->SetFrameBuffer( mFrameBuffer );
209
210     SurfaceResized( forceUpdate );
211   }
212 }
213
214 void Scene::SurfaceResized( bool forceUpdate )
215 {
216   if( mSurface )
217   {
218     const float fWidth = static_cast<float>( mSurface->GetPositionSize().width );
219     const float fHeight = static_cast<float>( mSurface->GetPositionSize().height );
220     const int orientation = mSurface->GetOrientation();
221
222     if( ( fabsf( mSurfaceSize.width - fWidth ) > Math::MACHINE_EPSILON_1 )
223         || ( fabsf( mSurfaceSize.height - fHeight ) > Math::MACHINE_EPSILON_1 )
224         || ( orientation != mSurfaceOrientation )
225         || (forceUpdate) )
226     {
227       Rect<int32_t> newSize( 0, 0, static_cast<int32_t>( mSurface->GetPositionSize().width ), static_cast<int32_t>( mSurface->GetPositionSize().height ) );
228
229       mSurfaceSize.width = fWidth;
230       mSurfaceSize.height = fHeight;
231
232       mSize.width = mSurfaceSize.width;
233       mSize.height = mSurfaceSize.height;
234
235       mSurfaceOrientation = orientation;
236
237       // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
238       mDefaultCamera->SetPerspectiveProjection( mSurfaceSize );
239       mDefaultCamera->RotateProjection( mSurfaceOrientation );
240
241       mRootLayer->SetSize( mSize.width, mSize.height );
242
243       ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
244       SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
245       SetDefaultSurfaceRectMessage( updateManager, newSize ); // truncated
246       SetDefaultSurfaceOrientationMessage( updateManager, mSurfaceOrientation );
247
248       RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
249
250       // if single render task to screen then set its viewport parameters
251       if( 1 == mRenderTaskList->GetTaskCount() )
252       {
253         if( !defaultRenderTask->GetTargetFrameBuffer() )
254         {
255           defaultRenderTask->SetViewport( newSize ); // truncated
256         }
257       }
258
259       defaultRenderTask->GetFrameBuffer()->SetSize( static_cast<uint32_t>( newSize.width ), static_cast<uint32_t>( newSize.height ) );
260     }
261   }
262 }
263
264 void Scene::SurfaceDeleted()
265 {
266   if ( mFrameBuffer )
267   {
268     // The frame buffer doesn't have a valid render surface any more.
269     mFrameBuffer->MarkSurfaceAsInvalid();
270   }
271 }
272
273 Integration::RenderSurface* Scene::GetSurface() const
274 {
275   return mSurface;
276 }
277
278 void Scene::Discard()
279 {
280   if( ThreadLocalStorage::Created() )
281   {
282     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
283     tls->RemoveScene( this );
284   }
285 }
286
287 void Scene::RequestRebuildDepthTree()
288 {
289   mDepthTreeDirty = true;
290 }
291
292 void Scene::QueueEvent( const Integration::Event& event )
293 {
294   mEventProcessor.QueueEvent( event );
295 }
296
297 void Scene::ProcessEvents()
298 {
299   mEventProcessor.ProcessEvents();
300 }
301
302 void Scene::RebuildDepthTree()
303 {
304   // If the depth tree needs rebuilding, do it in this frame only.
305   if( mDepthTreeDirty )
306   {
307     ActorPtr actor( mRootLayer.Get() );
308     actor->RebuildDepthTree();
309     mDepthTreeDirty = false;
310   }
311 }
312
313 void Scene::SetBackgroundColor( const Vector4& color )
314 {
315   mBackgroundColor = color;
316
317   if( mSurface )
318   {
319     mRenderTaskList->GetTask( 0u )->GetFrameBuffer()->SetBackgroundColor( color );
320   }
321 }
322
323 Vector4 Scene::GetBackgroundColor() const
324 {
325   return mBackgroundColor;
326 }
327
328 void Scene::EmitKeyEventSignal(const KeyEvent& event)
329 {
330   if ( !mKeyEventSignal.Empty() )
331   {
332     Dali::Integration::Scene handle( this );
333     mKeyEventSignal.Emit( event );
334   }
335 }
336
337 bool Scene::EmitKeyEventGeneratedSignal(const KeyEvent& event)
338 {
339   // Emit the KeyEventGenerated signal when KeyEvent is generated
340   return mKeyEventGeneratedSignal.Emit( event );
341 }
342
343 void Scene::EmitEventProcessingFinishedSignal()
344 {
345   if ( !mEventProcessingFinishedSignal.Empty() )
346   {
347     Dali::Integration::Scene handle( this );
348     mEventProcessingFinishedSignal.Emit();
349   }
350 }
351
352 void Scene::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
353 {
354   Dali::Integration::Scene handle( this );
355   if ( !mTouchedSignal.Empty() )
356   {
357     mTouchedSignal.Emit( touchEvent );
358   }
359   if ( !mTouchSignal.Empty() )
360   {
361     mTouchSignal.Emit( touch );
362   }
363 }
364
365 void Scene::EmitWheelEventSignal(const WheelEvent& event)
366 {
367   if ( !mWheelEventSignal.Empty() )
368   {
369     Dali::Integration::Scene handle( this );
370     mWheelEventSignal.Emit( event );
371   }
372 }
373
374 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
375 {
376   return mKeyEventSignal;
377 }
378
379 Integration::Scene::KeyEventGeneratedSignalType& Scene::KeyEventGeneratedSignal()
380 {
381   return mKeyEventGeneratedSignal;
382 }
383
384 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
385 {
386   return mEventProcessingFinishedSignal;
387 }
388
389 Scene::TouchedSignalType& Scene::TouchedSignal()
390 {
391   return mTouchedSignal;
392 }
393
394 Integration::Scene::TouchSignalType& Scene::TouchSignal()
395 {
396   return mTouchSignal;
397 }
398
399 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
400 {
401   return mWheelEventSignal;
402 }
403
404 } // Internal
405
406 } // Dali