[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / integration-api / adaptor-framework / scene-holder-impl.cpp
1 /*
2  * Copyright (c) 2023 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/integration-api/adaptor-framework/scene-holder-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/events/hover-event-integ.h>
24 #include <dali/integration-api/events/key-event-integ.h>
25 #include <dali/integration-api/events/touch-event-integ.h>
26 #include <dali/integration-api/events/touch-integ.h>
27 #include <dali/integration-api/events/wheel-event-integ.h>
28 #include <dali/public-api/actors/actor.h>
29 #include <dali/public-api/actors/layer.h>
30 #include <dali/public-api/common/dali-common.h>
31 #include <dali/public-api/render-tasks/render-task-list.h>
32
33 // INTERNAL INCLUDES
34 #include <dali/internal/adaptor/common/adaptor-impl.h>
35 #include <dali/internal/adaptor/common/lifecycle-observer.h>
36 #include <dali/internal/graphics/gles/egl-graphics.h>
37 #include <dali/internal/input/common/key-impl.h>
38 #include <dali/internal/input/common/physical-keyboard-impl.h>
39 #include <dali/internal/system/common/time-service.h>
40
41 namespace Dali
42 {
43 namespace Internal
44 {
45 namespace Adaptor
46 {
47 namespace
48 {
49 #if defined(DEBUG_ENABLED)
50 Debug::Filter* gSceneHolderLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_SCENE_HOLDER");
51 #endif
52 } // unnamed namespace
53
54 uint32_t SceneHolder::mSceneHolderCounter = 0;
55
56 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
57 {
58 public:
59   SceneHolderLifeCycleObserver(Adaptor*& adaptor, bool& adaptorStarted)
60   : mAdaptor(adaptor),
61     mAdaptorStarted(adaptorStarted)
62   {
63   }
64
65 private: // Adaptor::LifeCycleObserver interface
66   void OnStart() override
67   {
68     mAdaptorStarted = true;
69   };
70   void OnPause() override{};
71   void OnResume() override{};
72   void OnStop() override
73   {
74     // Mark adaptor as stopped;
75     mAdaptorStarted = false;
76   };
77   void OnDestroy() override
78   {
79     mAdaptor = nullptr;
80   };
81
82 private:
83   Adaptor*& mAdaptor;
84   bool&     mAdaptorStarted;
85 };
86
87 SceneHolder::SceneHolder()
88 : mLifeCycleObserver(new SceneHolderLifeCycleObserver(mAdaptor, mAdaptorStarted)),
89   mLastTouchEvent(),
90   mLastHoverEvent(),
91   mId(mSceneHolderCounter++),
92   mSurface(nullptr),
93   mAdaptor(nullptr),
94   mDpi(),
95   mAdaptorStarted(false),
96   mVisible(true)
97 {
98 }
99
100 SceneHolder::~SceneHolder()
101 {
102   if(mScene)
103   {
104     // The scene graph object should be removed first.
105     mScene.RemoveSceneObject();
106   }
107
108   if(mAdaptor)
109   {
110     mAdaptor->RemoveObserver(*mLifeCycleObserver.get());
111     mAdaptor->RemoveWindow(this);
112
113     if(mAdaptorStarted)
114     {
115       // The event queue is flushed and we wait for the completion of the surface removal
116       // Note : we don't need to delete surface when adaptor is stopped now.
117       mAdaptor->DeleteSurface(*mSurface.get());
118     }
119
120     mAdaptor = nullptr;
121   }
122
123   if(mScene)
124   {
125     // We should remove the surface from the Core last
126     mScene.Discard();
127   }
128 }
129
130 void SceneHolder::Add(Dali::Actor actor)
131 {
132   if(mScene)
133   {
134     mScene.Add(actor);
135   }
136 }
137
138 void SceneHolder::Remove(Dali::Actor actor)
139 {
140   if(mScene)
141   {
142     mScene.Remove(actor);
143   }
144 }
145
146 Dali::Layer SceneHolder::GetRootLayer() const
147 {
148   return mScene ? mScene.GetRootLayer() : Dali::Layer();
149 }
150
151 Dali::Layer SceneHolder::GetOverlayLayer()
152 {
153   return mScene ? mScene.GetOverlayLayer() : Dali::Layer();
154 }
155
156 uint32_t SceneHolder::GetId() const
157 {
158   return mId;
159 }
160
161 std::string SceneHolder::GetName() const
162 {
163   return mName;
164 }
165
166 bool SceneHolder::IsVisible() const
167 {
168   return mVisible;
169 }
170
171 Dali::Integration::Scene SceneHolder::GetScene()
172 {
173   return mScene;
174 }
175
176 Uint16Pair SceneHolder::GetDpi() const
177 {
178   return mDpi;
179 }
180
181 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
182 {
183   mSurface.reset(surface);
184
185   mScene.SurfaceReplaced();
186
187   PositionSize surfacePositionSize = surface->GetPositionSize();
188
189   SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
190
191   InitializeDpi();
192
193   mSurface->SetAdaptor(*mAdaptor);
194   mSurface->SetScene(mScene);
195
196   // Recreate the render target
197   CreateRenderTarget();
198
199   OnSurfaceSet(surface);
200 }
201
202 void SceneHolder::SurfaceResized(float width, float height)
203 {
204   mScene.SurfaceResized(width, height);
205
206   mSurface->SetFullSwapNextFrame();
207
208   // Recreate the render target
209   CreateRenderTarget();
210 }
211
212 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
213 {
214   return mSurface.get();
215 }
216
217 void SceneHolder::SetBackgroundColor(const Vector4& color)
218 {
219   if(mScene)
220   {
221     mScene.SetBackgroundColor(color);
222
223     mSurface->SetFullSwapNextFrame();
224   }
225 }
226
227 Vector4 SceneHolder::GetBackgroundColor() const
228 {
229   return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
230 }
231
232 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
233 {
234   // Avoid doing this more than once
235   if(mAdaptorStarted)
236   {
237     return;
238   }
239
240   DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
241
242   // We can assume that current adaptor is already started now.
243   mAdaptorStarted = true;
244
245   // Create the scene
246   PositionSize surfacePositionSize = mSurface->GetPositionSize();
247   int          windowOrientation   = mSurface->GetSurfaceOrientation();
248   int          screenOrientation   = mSurface->GetScreenOrientation();
249
250   mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)), windowOrientation, screenOrientation);
251
252   Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
253   mAdaptor                                = &adaptorImpl;
254
255   // Create an observer for the adaptor lifecycle
256   mAdaptor->AddObserver(*mLifeCycleObserver);
257
258   InitializeDpi();
259
260   mSurface->SetAdaptor(*mAdaptor);
261   mSurface->SetScene(mScene);
262
263   // Create the render target
264   CreateRenderTarget();
265
266   OnAdaptorSet(adaptor);
267
268   // Scene is newly created. Let we increase resize counter
269   mAdaptor->IncreaseSurfaceResizeCounter();
270 }
271
272 void SceneHolder::CreateRenderTarget()
273 {
274   Graphics::RenderTargetCreateInfo rtInfo{};
275   rtInfo
276     .SetSurface(mSurface.get())
277     .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
278     .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
279
280   mScene.SetSurfaceRenderTarget(rtInfo);
281 }
282
283 void SceneHolder::Pause()
284 {
285   Reset();
286
287   OnPause();
288 }
289
290 void SceneHolder::Resume()
291 {
292   Reset();
293
294   OnResume();
295 }
296
297 void SceneHolder::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
298 {
299   mScene.SurfaceRotated(width, height, windowOrientation, screenOrientation);
300 }
301
302 void SceneHolder::SetRotationCompletedAcknowledgement()
303 {
304   mScene.SetRotationCompletedAcknowledgement();
305 }
306
307 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
308 {
309   if(DALI_UNLIKELY(!mAdaptorStarted))
310   {
311     DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
312     return;
313   }
314
315   if(timeStamp < 1)
316   {
317     timeStamp = TimeService::GetMilliSeconds();
318   }
319
320   Vector2 convertedPosition = RecalculatePosition(point.GetScreenPosition());
321   point.SetScreenPosition(convertedPosition);
322
323   Integration::TouchEvent                            touchEvent;
324   Integration::HoverEvent                            hoverEvent;
325   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
326   if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
327   {
328     DALI_LOG_INFO(gSceneHolderLogFilter, Debug::Verbose, "%d: Device %d: Button state %d (%.2f, %.2f)\n", timeStamp, point.GetDeviceId(), point.GetState(), point.GetScreenPosition().x, point.GetScreenPosition().y);
329
330     // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
331     // Keep the handle alive until the core events are processed.
332     Dali::BaseHandle sceneHolder(this);
333
334     // First the touch and/or hover event & related gesture events are queued
335     if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
336     {
337       mLastTouchEvent = Dali::Integration::NewTouchEvent(timeStamp, point);
338       mScene.QueueEvent(touchEvent);
339     }
340
341     if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
342     {
343       mLastHoverEvent = Dali::Integration::NewHoverEvent(timeStamp, point);
344       mScene.QueueEvent(hoverEvent);
345     }
346
347     // Next the events are processed with a single call into Core
348     mAdaptor->ProcessCoreEvents();
349   }
350 }
351
352 const Dali::TouchEvent& SceneHolder::GetLastTouchEvent() const
353 {
354   return mLastTouchEvent;
355 }
356
357 const Dali::HoverEvent& SceneHolder::GetLastHoverEvent() const
358 {
359   return mLastHoverEvent;
360 }
361
362 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
363 {
364   if(DALI_UNLIKELY(!mAdaptorStarted))
365   {
366     DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
367     return;
368   }
369
370   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
371   // Keep the handle alive until the core events are processed.
372   Dali::BaseHandle sceneHolder(this);
373
374   Vector2 convertedPosition = RecalculatePosition(wheelEvent.point);
375   wheelEvent.point          = convertedPosition;
376
377   mScene.QueueEvent(wheelEvent);
378   mAdaptor->ProcessCoreEvents();
379 }
380
381 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
382 {
383   if(DALI_UNLIKELY(!mAdaptorStarted))
384   {
385     DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
386     return;
387   }
388
389   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
390   if(physicalKeyboard)
391   {
392     if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
393     {
394       GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
395     }
396   }
397
398   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
399   // Keep the handle alive until the core events are processed.
400   Dali::BaseHandle sceneHolder(this);
401
402   // Create send KeyEvent to Core.
403   mScene.QueueEvent(keyEvent);
404   mAdaptor->ProcessCoreEvents();
405 }
406
407 void SceneHolder::FeedHoverEvent(Dali::Integration::Point& point)
408 {
409   if(DALI_UNLIKELY(!mAdaptorStarted))
410   {
411     DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
412     return;
413   }
414
415   Integration::HoverEvent hoverEvent;
416
417   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
418   // Keep the handle alive until the core events are processed.
419   Dali::BaseHandle sceneHolder(this);
420
421   // Create send HoverEvent to Core.
422   hoverEvent.time = TimeService::GetMilliSeconds();
423   hoverEvent.AddPoint(point);
424
425   mScene.QueueEvent(hoverEvent);
426   mAdaptor->ProcessCoreEvents();
427 }
428
429 void SceneHolder::SetGeometryHittestEnabled(bool enabled)
430 {
431   mScene.SetGeometryHittestEnabled(enabled);
432 }
433
434 bool SceneHolder::IsGeometryHittestEnabled()
435 {
436   return mScene.IsGeometryHittestEnabled();
437 }
438
439 int32_t SceneHolder::GetNativeId() const
440 {
441   return mScene.GetNativeId();
442 }
443
444 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
445 {
446   mScene.AddFrameRenderedCallback(std::move(callback), frameId);
447
448   DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
449 }
450
451 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
452 {
453   mScene.AddFramePresentedCallback(std::move(callback), frameId);
454
455   DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
456 }
457
458 Dali::RenderTaskList SceneHolder::GetRenderTaskList() const
459 {
460   return mScene.GetRenderTaskList();
461 }
462
463 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
464 {
465   SceneHolder* sceneHolderImpl = nullptr;
466
467   if(Internal::Adaptor::Adaptor::IsAvailable())
468   {
469     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
470     sceneHolderImpl                           = adaptor.GetWindow(actor);
471   }
472
473   return Dali::Integration::SceneHolder(sceneHolderImpl);
474 }
475
476 void SceneHolder::Reset()
477 {
478   DALI_ASSERT_ALWAYS(mAdaptorStarted && "Adaptor is stopped, or not be started yet!");
479
480   mCombiner.Reset();
481
482   // Any touch listeners should be told of the interruption.
483   Integration::TouchEvent event;
484   Integration::Point      point;
485   point.SetState(PointState::INTERRUPTED);
486   event.AddPoint(point);
487
488   // First the touch event & related gesture events are queued
489   mScene.QueueEvent(event);
490
491   // Next the events are processed with a single call into Core
492   mAdaptor->ProcessCoreEvents();
493 }
494
495 void SceneHolder::InitializeDpi()
496 {
497   unsigned int dpiHorizontal, dpiVertical;
498   dpiHorizontal = dpiVertical = 0;
499
500   mSurface->GetDpi(dpiHorizontal, dpiVertical);
501   mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
502
503   mDpi.SetX(dpiHorizontal);
504   mDpi.SetY(dpiVertical);
505 }
506
507 } // namespace Adaptor
508
509 } // namespace Internal
510
511 } // namespace Dali