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