[Tizen] Implement partial update
[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   if( mSurface )
153   {
154     mRenderTaskList->GetTask( 0u )->GetFrameBuffer()->SetPartialUpdateEnabled( false );
155   }
156 }
157
158 Size Scene::GetSize() const
159 {
160   return mSize;
161 }
162
163 void Scene::SetDpi(Vector2 dpi)
164 {
165   mDpi = dpi;
166 }
167
168 Vector2 Scene::GetDpi() const
169 {
170   return mDpi;
171 }
172
173 RenderTaskList& Scene::GetRenderTaskList() const
174 {
175   return *mRenderTaskList;
176 }
177
178 Dali::Layer Scene::GetRootLayer() const
179 {
180   return Dali::Layer( mRootLayer.Get() );
181 }
182
183 LayerList& Scene::GetLayerList() const
184 {
185   return *mLayerList;
186 }
187
188 uint32_t Scene::GetLayerCount() const
189 {
190   return mLayerList->GetLayerCount();
191 }
192
193 Dali::Layer Scene::GetLayer( uint32_t depth ) const
194 {
195   return Dali::Layer(mLayerList->GetLayer( depth ));
196 }
197
198 CameraActor& Scene::GetDefaultCameraActor()
199 {
200   return *mDefaultCamera;
201 }
202
203 Actor& Scene::GetDefaultRootActor()
204 {
205   return *mRootLayer;
206 }
207
208 void Scene::SetSurface( Integration::RenderSurface& surface )
209 {
210   if( mSurface != &surface )
211   {
212     mSurface = &surface;
213
214     RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
215
216     mFrameBuffer = Dali::Internal::FrameBuffer::New( surface, Dali::FrameBuffer::Attachment::NONE );
217     defaultRenderTask->SetFrameBuffer( mFrameBuffer );
218
219     SurfaceResized( false );
220   }
221 }
222
223 void Scene::SurfaceResized( bool forceUpdate )
224 {
225   if( mSurface )
226   {
227     const PositionSize surfacePositionSize = mSurface->GetPositionSize();
228     const float fWidth = static_cast< float >( surfacePositionSize.width );
229     const float fHeight = static_cast< float >( surfacePositionSize.height );
230     const int orientation = mSurface->GetOrientation();
231
232     if( ( ( fabsf( mSize.width - fWidth ) > Math::MACHINE_EPSILON_1 ) || ( fabsf( mSize.height - fHeight ) > Math::MACHINE_EPSILON_1 ) )
233             || ( orientation != mSurfaceOrientation )
234             || ( forceUpdate ) )
235     {
236       Rect< int32_t > newSize( 0, 0, static_cast< int32_t >( surfacePositionSize.width ), static_cast< int32_t >( surfacePositionSize.height ) ); // truncated
237
238       mSize.width = fWidth;
239       mSize.height = fHeight;
240       mSurfaceOrientation = orientation;
241
242       // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
243       mDefaultCamera->SetPerspectiveProjection( mSize );
244       mDefaultCamera->RotateProjection( mSurfaceOrientation );
245
246       mRootLayer->SetSize( mSize.width, mSize.height );
247
248       ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
249       SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
250       SetDefaultSurfaceRectMessage( updateManager, newSize );
251       SetDefaultSurfaceOrientationMessage( updateManager, mSurfaceOrientation );
252
253       // set default render-task viewport parameters
254       RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask( 0u );
255       defaultRenderTask->SetViewport( newSize );
256       defaultRenderTask->GetFrameBuffer()->SetSize( static_cast<uint32_t>( newSize.width ), static_cast<uint32_t>( newSize.height ) );
257     }
258   }
259 }
260
261 void Scene::SurfaceDeleted()
262 {
263   mSurface = nullptr;
264   if ( mFrameBuffer )
265   {
266     // The frame buffer doesn't have a valid render surface any more.
267     mFrameBuffer->MarkSurfaceAsInvalid();
268   }
269 }
270
271 Integration::RenderSurface* Scene::GetSurface() const
272 {
273   return mSurface;
274 }
275
276 void Scene::Discard()
277 {
278   if( ThreadLocalStorage::Created() )
279   {
280     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
281     tls->RemoveScene( this );
282   }
283 }
284
285 void Scene::RequestRebuildDepthTree()
286 {
287   mDepthTreeDirty = true;
288 }
289
290 void Scene::QueueEvent( const Integration::Event& event )
291 {
292   mEventProcessor.QueueEvent( event );
293 }
294
295 void Scene::ProcessEvents()
296 {
297   mEventProcessor.ProcessEvents();
298 }
299
300 void Scene::RebuildDepthTree()
301 {
302   // If the depth tree needs rebuilding, do it in this frame only.
303   if( mDepthTreeDirty )
304   {
305     ActorPtr actor( mRootLayer.Get() );
306     actor->RebuildDepthTree();
307     mDepthTreeDirty = false;
308   }
309 }
310
311 void Scene::SetBackgroundColor( const Vector4& color )
312 {
313   mBackgroundColor = color;
314
315   if( mSurface )
316   {
317     mRenderTaskList->GetTask( 0u )->GetFrameBuffer()->SetBackgroundColor( color );
318   }
319 }
320
321 Vector4 Scene::GetBackgroundColor() const
322 {
323   return mBackgroundColor;
324 }
325
326 void Scene::EmitKeyEventSignal(const KeyEvent& event)
327 {
328   if ( !mKeyEventSignal.Empty() )
329   {
330     Dali::Integration::Scene handle( this );
331     mKeyEventSignal.Emit( event );
332   }
333 }
334
335 bool Scene::EmitKeyEventGeneratedSignal(const KeyEvent& event)
336 {
337   // Emit the KeyEventGenerated signal when KeyEvent is generated
338   return mKeyEventGeneratedSignal.Emit( event );
339 }
340
341 void Scene::EmitEventProcessingFinishedSignal()
342 {
343   if ( !mEventProcessingFinishedSignal.Empty() )
344   {
345     Dali::Integration::Scene handle( this );
346     mEventProcessingFinishedSignal.Emit();
347   }
348 }
349
350 void Scene::EmitTouchedSignal( const TouchEvent& touchEvent, const Dali::TouchData& touch )
351 {
352   Dali::Integration::Scene handle( this );
353   if ( !mTouchedSignal.Empty() )
354   {
355     mTouchedSignal.Emit( touchEvent );
356   }
357   if ( !mTouchSignal.Empty() )
358   {
359     mTouchSignal.Emit( touch );
360   }
361 }
362
363 void Scene::EmitWheelEventSignal(const WheelEvent& event)
364 {
365   if ( !mWheelEventSignal.Empty() )
366   {
367     Dali::Integration::Scene handle( this );
368     mWheelEventSignal.Emit( event );
369   }
370 }
371
372 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
373 {
374   return mKeyEventSignal;
375 }
376
377 Integration::Scene::KeyEventGeneratedSignalType& Scene::KeyEventGeneratedSignal()
378 {
379   return mKeyEventGeneratedSignal;
380 }
381
382 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
383 {
384   return mEventProcessingFinishedSignal;
385 }
386
387 Scene::TouchedSignalType& Scene::TouchedSignal()
388 {
389   return mTouchedSignal;
390 }
391
392 Integration::Scene::TouchSignalType& Scene::TouchSignal()
393 {
394   return mTouchSignal;
395 }
396
397 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
398 {
399   return mWheelEventSignal;
400 }
401
402 } // Internal
403
404 } // Dali