Merge "Added UIThreadLoader to GLIB framework" into 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)
60   : mAdaptor(adaptor){};
61
62 private: // Adaptor::LifeCycleObserver interface
63   void OnStart() override{};
64   void OnPause() override{};
65   void OnResume() override{};
66   void OnStop() override{};
67   void OnDestroy() override
68   {
69     mAdaptor = nullptr;
70   };
71
72 private:
73   Adaptor*& mAdaptor;
74 };
75
76 SceneHolder::SceneHolder()
77 : mLifeCycleObserver(new SceneHolderLifeCycleObserver(mAdaptor)),
78   mLastTouchEvent(),
79   mLastHoverEvent(),
80   mId(mSceneHolderCounter++),
81   mSurface(nullptr),
82   mAdaptor(nullptr),
83   mDpi(),
84   mIsBeingDeleted(false),
85   mAdaptorStarted(false),
86   mVisible(true)
87 {
88 }
89
90 SceneHolder::~SceneHolder()
91 {
92   if(mScene)
93   {
94     // The scene graph object should be removed first.
95     mScene.RemoveSceneObject();
96   }
97
98   if(mAdaptor)
99   {
100     mAdaptor->RemoveObserver(*mLifeCycleObserver.get());
101     mAdaptor->RemoveWindow(this);
102
103     // The event queue is flushed and we wait for the completion of the surface removal
104     mAdaptor->DeleteSurface(*mSurface.get());
105
106     mAdaptor = nullptr;
107   }
108
109   if(mScene)
110   {
111     // We should remove the surface from the Core last
112     mScene.Discard();
113   }
114 }
115
116 void SceneHolder::Add(Dali::Actor actor)
117 {
118   if(mScene)
119   {
120     mScene.Add(actor);
121   }
122 }
123
124 void SceneHolder::Remove(Dali::Actor actor)
125 {
126   if(mScene)
127   {
128     mScene.Remove(actor);
129   }
130 }
131
132 Dali::Layer SceneHolder::GetRootLayer() const
133 {
134   return mScene ? mScene.GetRootLayer() : Dali::Layer();
135 }
136
137 Dali::Layer SceneHolder::GetOverlayLayer()
138 {
139   return mScene ? mScene.GetOverlayLayer() : Dali::Layer();
140 }
141
142 uint32_t SceneHolder::GetId() const
143 {
144   return mId;
145 }
146
147 std::string SceneHolder::GetName() const
148 {
149   return mName;
150 }
151
152 bool SceneHolder::IsVisible() const
153 {
154   return mVisible;
155 }
156
157 Dali::Integration::Scene SceneHolder::GetScene()
158 {
159   return mScene;
160 }
161
162 Uint16Pair SceneHolder::GetDpi() const
163 {
164   return mDpi;
165 }
166
167 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
168 {
169   mSurface.reset(surface);
170
171   mScene.SurfaceReplaced();
172
173   PositionSize surfacePositionSize = surface->GetPositionSize();
174
175   SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
176
177   InitializeDpi();
178
179   mSurface->SetAdaptor(*mAdaptor);
180   mSurface->SetScene(mScene);
181
182   // Recreate the render target
183   CreateRenderTarget();
184
185   OnSurfaceSet(surface);
186 }
187
188 void SceneHolder::SurfaceResized(float width, float height)
189 {
190   mScene.SurfaceResized(width, height);
191
192   mSurface->SetFullSwapNextFrame();
193
194   // Recreate the render target
195   CreateRenderTarget();
196 }
197
198 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
199 {
200   return mSurface.get();
201 }
202
203 void SceneHolder::SetBackgroundColor(const Vector4& color)
204 {
205   if(mScene)
206   {
207     mScene.SetBackgroundColor(color);
208
209     mSurface->SetFullSwapNextFrame();
210   }
211 }
212
213 Vector4 SceneHolder::GetBackgroundColor() const
214 {
215   return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
216 }
217
218 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
219 {
220   // Avoid doing this more than once
221   if(mAdaptorStarted)
222   {
223     return;
224   }
225
226   DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
227
228   mAdaptorStarted = true;
229
230   // Create the scene
231   PositionSize surfacePositionSize = mSurface->GetPositionSize();
232   int          windowOrientation   = mSurface->GetSurfaceOrientation();
233   int          screenOrientation   = mSurface->GetScreenOrientation();
234
235   mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)), windowOrientation, screenOrientation);
236
237   Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
238   mAdaptor                                = &adaptorImpl;
239
240   // Create an observer for the adaptor lifecycle
241   mAdaptor->AddObserver(*mLifeCycleObserver);
242
243   InitializeDpi();
244
245   mSurface->SetAdaptor(*mAdaptor);
246   mSurface->SetScene(mScene);
247
248   // Create the render target
249   CreateRenderTarget();
250
251   OnAdaptorSet(adaptor);
252 }
253
254 void SceneHolder::CreateRenderTarget()
255 {
256   Graphics::RenderTargetCreateInfo rtInfo{};
257   rtInfo
258     .SetSurface(mSurface.get())
259     .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
260     .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
261
262   mScene.SetSurfaceRenderTarget(rtInfo);
263 }
264
265 void SceneHolder::Pause()
266 {
267   Reset();
268
269   OnPause();
270 }
271
272 void SceneHolder::Resume()
273 {
274   Reset();
275
276   OnResume();
277 }
278
279 void SceneHolder::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
280 {
281   mScene.SurfaceRotated(width, height, windowOrientation, screenOrientation);
282 }
283
284 void SceneHolder::SetRotationCompletedAcknowledgement()
285 {
286   mScene.SetRotationCompletedAcknowledgement();
287 }
288
289 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
290 {
291   if(timeStamp < 1)
292   {
293     timeStamp = TimeService::GetMilliSeconds();
294   }
295
296   Vector2 convertedPosition = RecalculatePosition(point.GetScreenPosition());
297   point.SetScreenPosition(convertedPosition);
298
299   Integration::TouchEvent                            touchEvent;
300   Integration::HoverEvent                            hoverEvent;
301   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
302   if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
303   {
304     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);
305
306     // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
307     // Keep the handle alive until the core events are processed.
308     Dali::BaseHandle sceneHolder(this);
309
310     // First the touch and/or hover event & related gesture events are queued
311     if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
312     {
313       mLastTouchEvent = Dali::Integration::NewTouchEvent(timeStamp, point);
314       mScene.QueueEvent(touchEvent);
315     }
316
317     if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
318     {
319       mLastHoverEvent = Dali::Integration::NewHoverEvent(timeStamp, point);
320       mScene.QueueEvent(hoverEvent);
321     }
322
323     // Next the events are processed with a single call into Core
324     mAdaptor->ProcessCoreEvents();
325   }
326 }
327
328 const Dali::TouchEvent& SceneHolder::GetLastTouchEvent() const
329 {
330   return mLastTouchEvent;
331 }
332
333 const Dali::HoverEvent& SceneHolder::GetLastHoverEvent() const
334 {
335   return mLastHoverEvent;
336 }
337
338 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
339 {
340   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
341   // Keep the handle alive until the core events are processed.
342   Dali::BaseHandle sceneHolder(this);
343
344   Vector2 convertedPosition = RecalculatePosition(wheelEvent.point);
345   wheelEvent.point          = convertedPosition;
346
347   mScene.QueueEvent(wheelEvent);
348   mAdaptor->ProcessCoreEvents();
349 }
350
351 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
352 {
353   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
354   if(physicalKeyboard)
355   {
356     if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
357     {
358       GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
359     }
360   }
361
362   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
363   // Keep the handle alive until the core events are processed.
364   Dali::BaseHandle sceneHolder(this);
365
366   // Create send KeyEvent to Core.
367   mScene.QueueEvent(keyEvent);
368   mAdaptor->ProcessCoreEvents();
369 }
370
371 void SceneHolder::FeedHoverEvent(Dali::Integration::Point& point)
372 {
373   Integration::HoverEvent hoverEvent;
374
375   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
376   // Keep the handle alive until the core events are processed.
377   Dali::BaseHandle sceneHolder(this);
378
379   // Create send HoverEvent to Core.
380   hoverEvent.time = TimeService::GetMilliSeconds();
381   hoverEvent.AddPoint(point);
382
383   mScene.QueueEvent(hoverEvent);
384   mAdaptor->ProcessCoreEvents();
385 }
386
387 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
388 {
389   mScene.AddFrameRenderedCallback(std::move(callback), frameId);
390
391   DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
392 }
393
394 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
395 {
396   mScene.AddFramePresentedCallback(std::move(callback), frameId);
397
398   DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
399 }
400
401 Dali::RenderTaskList SceneHolder::GetRenderTaskList() const
402 {
403   return mScene.GetRenderTaskList();
404 }
405
406 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
407 {
408   SceneHolder* sceneHolderImpl = nullptr;
409
410   if(Internal::Adaptor::Adaptor::IsAvailable())
411   {
412     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
413     sceneHolderImpl                           = adaptor.GetWindow(actor);
414   }
415
416   return Dali::Integration::SceneHolder(sceneHolderImpl);
417 }
418
419 void SceneHolder::Reset()
420 {
421   mCombiner.Reset();
422
423   // Any touch listeners should be told of the interruption.
424   Integration::TouchEvent event;
425   Integration::Point      point;
426   point.SetState(PointState::INTERRUPTED);
427   event.AddPoint(point);
428
429   // First the touch event & related gesture events are queued
430   mScene.QueueEvent(event);
431
432   // Next the events are processed with a single call into Core
433   mAdaptor->ProcessCoreEvents();
434 }
435
436 void SceneHolder::InitializeDpi()
437 {
438   unsigned int dpiHorizontal, dpiVertical;
439   dpiHorizontal = dpiVertical = 0;
440
441   mSurface->GetDpi(dpiHorizontal, dpiVertical);
442   mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
443
444   mDpi.SetX(dpiHorizontal);
445   mDpi.SetY(dpiVertical);
446 }
447
448 } // namespace Adaptor
449
450 } // namespace Internal
451
452 } // namespace Dali