ea5c689320b355cc9be86193fcd33d5d11f644e2
[platform/core/uifw/dali-core.git] / dali / internal / event / common / scene-impl.cpp
1 /*
2  * Copyright (c) 2022 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/camera-actor-impl.h>
23 #include <dali/internal/event/actors/layer-impl.h>
24 #include <dali/internal/event/actors/layer-list.h>
25 #include <dali/internal/event/common/object-registry-impl.h>
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/internal/event/render-tasks/render-task-impl.h>
28 #include <dali/internal/event/render-tasks/render-task-list-impl.h>
29 #include <dali/internal/event/rendering/frame-buffer-impl.h>
30 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
31 #include <dali/internal/update/common/scene-graph-scene.h>
32 #include <dali/internal/update/manager/update-manager.h>
33 #include <dali/internal/update/nodes/node.h>
34 #include <dali/public-api/common/constants.h>
35 #include <dali/public-api/object/type-registry.h>
36 #include <dali/public-api/render-tasks/render-task-list.h>
37
38 using Dali::Internal::SceneGraph::Node;
39
40 namespace Dali
41 {
42 namespace Internal
43 {
44 ScenePtr Scene::New(Size size, int32_t windowOrientation, int32_t screenOrientation)
45 {
46   ScenePtr scene = new Scene;
47
48   // Second-phase construction
49   scene->Initialize(size, windowOrientation, screenOrientation);
50
51   return scene;
52 }
53
54 Scene::Scene()
55 : mSceneObject(nullptr),
56   mSize(), // Don't set the proper value here, this will be set when the surface is set later
57   mDpi(),
58   mBackgroundColor(DEFAULT_BACKGROUND_COLOR),
59   mDepthTreeDirty(false),
60   mEventProcessor(*this, ThreadLocalStorage::GetInternal()->GetGestureEventProcessor()),
61   mSurfaceOrientation(0),
62   mScreenOrientation(0)
63 {
64 }
65
66 Scene::~Scene()
67 {
68   if(EventThreadServices::IsCoreRunning() && mSceneObject)
69   {
70     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
71     RemoveSceneMessage(tls->GetUpdateManager(), *mSceneObject);
72   }
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   // No need to discard this Scene from Core, as Core stores an intrusive_ptr to this scene
94   // When this destructor is called, the scene has either already been removed from Core or Core has already been destroyed
95 }
96
97 void Scene::Initialize(Size size, int32_t windowOrientation, int32_t screenOrientation)
98 {
99   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
100
101   DALI_ASSERT_ALWAYS(tls && "Attempt to create scene before core exists!");
102
103   tls->AddScene(this);
104
105   SceneGraph::UpdateManager& updateManager = tls->GetUpdateManager();
106
107   // Create the ordered list of layers
108   mLayerList = LayerList::New(updateManager);
109
110   // The scene owns the default layer
111   mRootLayer = Layer::NewRoot(*mLayerList);
112   mRootLayer->SetName("RootLayer");
113   mRootLayer->SetScene(*this);
114
115   // The root layer needs to have a fixed resize policy (as opposed to the default USE_NATURAL_SIZE).
116   // This stops actors parented to the stage having their relayout requests propagating
117   // up to the root layer, and down through other children unnecessarily.
118   mRootLayer->SetResizePolicy(ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS);
119
120   // Create the default camera actor first; this is needed by the RenderTaskList
121   // The default camera attributes and position is such that children of the default layer,
122   // can be positioned at (0,0) and be at the top-left of the viewport.
123   mDefaultCamera = CameraActor::New(size);
124   mDefaultCamera->SetParentOrigin(ParentOrigin::CENTER);
125   Add(*(mDefaultCamera.Get()));
126
127   // Create the list of render-tasks
128   mRenderTaskList = RenderTaskList::New();
129
130   // Create the default render-task and ensure clear is enabled on it to show the background color
131   RenderTaskPtr renderTask = mRenderTaskList->CreateTask(mRootLayer.Get(), mDefaultCamera.Get());
132   renderTask->SetClearEnabled(true);
133
134   // Create scene graph object
135   mSceneObject = new SceneGraph::Scene();
136   OwnerPointer<SceneGraph::Scene> transferOwnership(const_cast<SceneGraph::Scene*>(mSceneObject));
137   AddSceneMessage(updateManager, transferOwnership);
138
139   SurfaceRotated(size.width, size.height, windowOrientation, screenOrientation);
140 }
141
142 void Scene::Add(Actor& actor)
143 {
144   mRootLayer->Add(actor);
145 }
146
147 void Scene::Remove(Actor& actor)
148 {
149   mRootLayer->Remove(actor);
150 }
151
152 Size Scene::GetSize() const
153 {
154   return mSize;
155 }
156
157 void Scene::SetDpi(Vector2 dpi)
158 {
159   mDpi = dpi;
160 }
161
162 Vector2 Scene::GetDpi() const
163 {
164   return mDpi;
165 }
166
167 RenderTaskList& Scene::GetRenderTaskList() const
168 {
169   return *mRenderTaskList;
170 }
171
172 Dali::Layer Scene::GetRootLayer() const
173 {
174   return Dali::Layer(mRootLayer.Get());
175 }
176
177 LayerList& Scene::GetLayerList() const
178 {
179   return *mLayerList;
180 }
181
182 uint32_t Scene::GetLayerCount() const
183 {
184   return mLayerList->GetLayerCount();
185 }
186
187 Dali::Layer Scene::GetLayer(uint32_t depth) const
188 {
189   return Dali::Layer(mLayerList->GetLayer(depth));
190 }
191
192 CameraActor& Scene::GetDefaultCameraActor() const
193 {
194   return *mDefaultCamera;
195 }
196
197 Actor& Scene::GetDefaultRootActor()
198 {
199   return *mRootLayer;
200 }
201
202 void Scene::SurfaceResized(float width, float height)
203 {
204   if((fabsf(mSize.width - width) > Math::MACHINE_EPSILON_1) || (fabsf(mSize.height - height) > Math::MACHINE_EPSILON_1))
205   {
206     ChangedSurface(width, height, mSurfaceOrientation, mScreenOrientation);
207   }
208 }
209
210 void Scene::SurfaceReplaced()
211 {
212   if(mSceneObject)
213   {
214     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
215     SurfaceReplacedMessage(tls->GetUpdateManager(), *mSceneObject);
216   }
217 }
218
219 void Scene::Discard()
220 {
221   if(ThreadLocalStorage::Created())
222   {
223     ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
224     tls->RemoveScene(this);
225   }
226 }
227
228 void Scene::RequestRebuildDepthTree()
229 {
230   mDepthTreeDirty = true;
231 }
232
233 void Scene::QueueEvent(const Integration::Event& event)
234 {
235   mEventProcessor.QueueEvent(event);
236 }
237
238 void Scene::ProcessEvents()
239 {
240   mEventProcessor.ProcessEvents();
241 }
242
243 void Scene::RebuildDepthTree()
244 {
245   // If the depth tree needs rebuilding, do it in this frame only.
246   if(mDepthTreeDirty)
247   {
248     ActorPtr actor(mRootLayer.Get());
249     actor->RebuildDepthTree();
250     mDepthTreeDirty = false;
251   }
252 }
253
254 void Scene::SetBackgroundColor(const Vector4& color)
255 {
256   mBackgroundColor = color;
257
258   mRenderTaskList->GetTask(0u)->SetClearColor(color);
259   mRenderTaskList->GetTask(0u)->SetClearEnabled(true);
260 }
261
262 Vector4 Scene::GetBackgroundColor() const
263 {
264   return mBackgroundColor;
265 }
266
267 SceneGraph::Scene* Scene::GetSceneObject() const
268 {
269   return mSceneObject;
270 }
271
272 void Scene::EmitKeyEventSignal(const Dali::KeyEvent& event)
273 {
274   if(!mKeyEventSignal.Empty())
275   {
276     Dali::Integration::Scene handle(this);
277     mKeyEventSignal.Emit(event);
278   }
279 }
280
281 void Scene::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
282 {
283   ChangedSurface(width, height, windowOrientation, screenOrientation);
284 }
285
286 int32_t Scene::GetCurrentSurfaceOrientation() const
287 {
288   return mSceneObject->GetSurfaceOrientation();
289 }
290
291 int32_t Scene::GetCurrentScreenOrientation() const
292 {
293   return mSceneObject->GetScreenOrientation();
294 }
295
296 const Rect<int32_t>& Scene::GetCurrentSurfaceRect() const
297 {
298   return mSceneObject->GetSurfaceRect();
299 }
300
301 void Scene::ChangedSurface(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
302 {
303   bool changedOrientation = false;
304   Rect<int32_t> newSize(0, 0, static_cast<int32_t>(width), static_cast<int32_t>(height)); // truncated
305   mSize.width  = width;
306   mSize.height = height;
307
308   if(mSurfaceOrientation != windowOrientation || mScreenOrientation != screenOrientation)
309   {
310     changedOrientation = true;
311   }
312
313   mSurfaceOrientation = windowOrientation;
314   mScreenOrientation = screenOrientation;
315
316   // Calculates the aspect ratio, near and far clipping planes, field of view and camera Z position.
317   mDefaultCamera->SetPerspectiveProjection(mSize);
318   // Set the surface orientation to Default camera for window/screen rotation
319   if(changedOrientation)
320   {
321     int32_t orientation = (windowOrientation + screenOrientation) % 360;
322     mDefaultCamera->RotateProjection(orientation);
323   }
324
325   mRootLayer->SetSize(width, height);
326
327   // Send the surface rectangle/orientation to SceneGraph::Scene for calculating glViewport/Scissor
328   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
329   DALI_LOG_RELEASE_INFO("Send Surface Rect Message, width[%d], height[%d]\n", newSize.width, newSize.height);
330   SetSurfaceRectMessage(tls->GetEventThreadServices(), *mSceneObject, newSize);
331   if(changedOrientation)
332   {
333     DALI_LOG_RELEASE_INFO("Send Surface Orientation Message, surface orientation[%d], screen orientation[%d]\n", mSurfaceOrientation, mScreenOrientation);
334     SetSurfaceOrientationsMessage(tls->GetEventThreadServices(), *mSceneObject, mSurfaceOrientation, mScreenOrientation);
335   }
336
337   // set default render-task viewport parameters
338   RenderTaskPtr defaultRenderTask = mRenderTaskList->GetTask(0u);
339   defaultRenderTask->SetViewport(newSize);
340 }
341
342 bool Scene::IsSurfaceRectChanged() const
343 {
344   return mSceneObject->IsSurfaceRectChanged();
345 }
346
347 bool Scene::IsRotationCompletedAcknowledgementSet() const
348 {
349   return mSceneObject->IsRotationCompletedAcknowledgementSet();
350 }
351
352 void Scene::SetRotationCompletedAcknowledgement()
353 {
354   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
355   SetRotationCompletedAcknowledgementMessage(tls->GetEventThreadServices(), *mSceneObject);
356 }
357
358 void Scene::SetSurfaceRenderTarget(const Graphics::RenderTargetCreateInfo& renderTargetCreateInfo)
359 {
360   // Send the surface render target to SceneGraph::Scene
361   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
362   SetSurfaceRenderTargetCreateInfoMessage(tls->GetEventThreadServices(), *mSceneObject, renderTargetCreateInfo);
363 }
364
365 bool Scene::EmitKeyEventGeneratedSignal(const Dali::KeyEvent& event)
366 {
367   // Emit the KeyEventGenerated signal when KeyEvent is generated
368   Dali::Integration::Scene handle(this);
369   return mKeyEventGeneratedSignal.Emit(event);
370 }
371
372 bool Scene::EmitInterceptKeyEventSignal(const Dali::KeyEvent& event)
373 {
374   // Emit the InterceptKeyEvent signal
375   Dali::Integration::Scene handle(this);
376   return mInterceptKeyEventSignal.Emit(event);
377 }
378
379 void Scene::EmitEventProcessingFinishedSignal()
380 {
381   if(!mEventProcessingFinishedSignal.Empty())
382   {
383     Dali::Integration::Scene handle(this);
384     mEventProcessingFinishedSignal.Emit();
385   }
386 }
387
388 void Scene::EmitTouchedSignal(const Dali::TouchEvent& touch)
389 {
390   Dali::Integration::Scene handle(this);
391   if(!mTouchedSignal.Empty())
392   {
393     mTouchedSignal.Emit(touch);
394   }
395 }
396
397 void Scene::EmitWheelEventSignal(const Dali::WheelEvent& event)
398 {
399   if(!mWheelEventSignal.Empty())
400   {
401     Dali::Integration::Scene handle(this);
402     mWheelEventSignal.Emit(event);
403   }
404 }
405
406 bool Scene::EmitWheelEventGeneratedSignal(const Dali::WheelEvent& event)
407 {
408   // Emit the WheelEventGenerated signal when WheelEvent is generated
409   Dali::Integration::Scene handle(this);
410   return mWheelEventGeneratedSignal.Emit(event);
411 }
412
413 void Scene::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
414 {
415   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
416   AddFrameRenderedCallbackMessage(tls->GetEventThreadServices(), *mSceneObject, callback.release(), frameId);
417 }
418
419 void Scene::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
420 {
421   ThreadLocalStorage* tls = ThreadLocalStorage::GetInternal();
422   AddFramePresentedCallbackMessage(tls->GetEventThreadServices(), *mSceneObject, callback.release(), frameId);
423 }
424
425 void Scene::GetFrameRenderedCallback(Dali::Integration::Scene::FrameCallbackContainer& callbacks)
426 {
427   mSceneObject->GetFrameRenderedCallback(callbacks);
428 }
429
430 void Scene::GetFramePresentedCallback(Dali::Integration::Scene::FrameCallbackContainer& callbacks)
431 {
432   mSceneObject->GetFramePresentedCallback(callbacks);
433 }
434
435 Integration::Scene::KeyEventSignalType& Scene::KeyEventSignal()
436 {
437   return mKeyEventSignal;
438 }
439
440 Integration::Scene::KeyEventGeneratedSignalType& Scene::KeyEventGeneratedSignal()
441 {
442   return mKeyEventGeneratedSignal;
443 }
444
445 Integration::Scene::KeyEventGeneratedSignalType& Scene::InterceptKeyEventSignal()
446 {
447   return mInterceptKeyEventSignal;
448 }
449
450 Integration::Scene::EventProcessingFinishedSignalType& Scene::EventProcessingFinishedSignal()
451 {
452   return mEventProcessingFinishedSignal;
453 }
454
455 Integration::Scene::TouchEventSignalType& Scene::TouchedSignal()
456 {
457   return mTouchedSignal;
458 }
459
460 Integration::Scene::WheelEventSignalType& Scene::WheelEventSignal()
461 {
462   return mWheelEventSignal;
463 }
464
465 Integration::Scene::WheelEventGeneratedSignalType& Scene::WheelEventGeneratedSignal()
466 {
467   return mWheelEventGeneratedSignal;
468 }
469
470 } // namespace Internal
471
472 } // namespace Dali