[Tizen] Add screen and client rotation itself function
[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 ) );
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 ); // truncated
247       SetDefaultSurfaceOrientationMessage( updateManager, mSurfaceOrientation );
248
249       RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
250
251       // if single render task to screen then set its viewport parameters
252       if( 1 == mRenderTaskList->GetTaskCount() )
253       {
254         if( !defaultRenderTask->GetTargetFrameBuffer() )
255         {
256           defaultRenderTask->SetViewport( newSize ); // truncated
257         }
258       }
259
260       defaultRenderTask->GetFrameBuffer()->SetSize( static_cast<uint32_t>( newSize.width ), static_cast<uint32_t>( newSize.height ) );
261     }
262   }
263 }
264
265 void Scene::SurfaceDeleted()
266 {
267   mSurface = nullptr;
268   if ( mFrameBuffer )
269   {
270     // The frame buffer doesn't have a valid render surface any more.
271     mFrameBuffer->MarkSurfaceAsInvalid();
272   }
273 }
274
275 Integration::RenderSurface* Scene::GetSurface() const
276 {
277   return mSurface;
278 }
279
280 void Scene::Discard()
281 {
282   if( ThreadLocalStorage::Created() )
283   {
284     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
285     tls->RemoveScene( this );
286   }
287 }
288
289 void Scene::RequestRebuildDepthTree()
290 {
291   mDepthTreeDirty = true;
292 }
293
294 void Scene::QueueEvent( const Integration::Event& event )
295 {
296   mEventProcessor.QueueEvent( event );
297 }
298
299 void Scene::ProcessEvents()
300 {
301   mEventProcessor.ProcessEvents();
302 }
303
304 void Scene::RebuildDepthTree()
305 {
306   // If the depth tree needs rebuilding, do it in this frame only.
307   if( mDepthTreeDirty )
308   {
309     ActorPtr actor( mRootLayer.Get() );
310     actor->RebuildDepthTree();
311     mDepthTreeDirty = false;
312   }
313 }
314
315 void Scene::SetBackgroundColor( const Vector4& color )
316 {
317   mBackgroundColor = color;
318
319   if( mSurface )
320   {
321     mRenderTaskList->GetTask( 0u )->GetFrameBuffer()->SetBackgroundColor( color );
322   }
323 }
324
325 Vector4 Scene::GetBackgroundColor() const
326 {
327   return mBackgroundColor;
328 }
329
330 void Scene::EmitKeyEventSignal(const KeyEvent& event)
331 {
332   if ( !mKeyEventSignal.Empty() )
333   {
334     Dali::Integration::Scene handle( this );
335     mKeyEventSignal.Emit( event );
336   }
337 }
338
339 bool Scene::EmitKeyEventGeneratedSignal(const KeyEvent& event)
340 {
341   // Emit the KeyEventGenerated signal when KeyEvent is generated
342   return mKeyEventGeneratedSignal.Emit( event );
343 }
344
345 void Scene::EmitEventProcessingFinishedSignal()
346 {
347   if ( !mEventProcessingFinishedSignal.Empty() )
348   {
349     Dali::Integration::Scene handle( this );
350     mEventProcessingFinishedSignal.Emit();
351   }
352 }
353
354 void Scene::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
355 {
356   Dali::Integration::Scene handle( this );
357   if ( !mTouchedSignal.Empty() )
358   {
359     mTouchedSignal.Emit( touchEvent );
360   }
361   if ( !mTouchSignal.Empty() )
362   {
363     mTouchSignal.Emit( touch );
364   }
365 }
366
367 void Scene::EmitWheelEventSignal(const WheelEvent& event)
368 {
369   if ( !mWheelEventSignal.Empty() )
370   {
371     Dali::Integration::Scene handle( this );
372     mWheelEventSignal.Emit( event );
373   }
374 }
375
376 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
377 {
378   return mKeyEventSignal;
379 }
380
381 Integration::Scene::KeyEventGeneratedSignalType& Scene::KeyEventGeneratedSignal()
382 {
383   return mKeyEventGeneratedSignal;
384 }
385
386 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
387 {
388   return mEventProcessingFinishedSignal;
389 }
390
391 Scene::TouchedSignalType& Scene::TouchedSignal()
392 {
393   return mTouchedSignal;
394 }
395
396 Integration::Scene::TouchSignalType& Scene::TouchSignal()
397 {
398   return mTouchSignal;
399 }
400
401 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
402 {
403   return mWheelEventSignal;
404 }
405
406 } // Internal
407
408 } // Dali