Merge "Updating non-ecore X window-system with new API" 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, 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
269 void SceneHolder::CreateRenderTarget()
270 {
271   Graphics::RenderTargetCreateInfo rtInfo{};
272   rtInfo
273     .SetSurface(mSurface.get())
274     .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
275     .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
276
277   mScene.SetSurfaceRenderTarget(rtInfo);
278 }
279
280 void SceneHolder::Pause()
281 {
282   Reset();
283
284   OnPause();
285 }
286
287 void SceneHolder::Resume()
288 {
289   Reset();
290
291   OnResume();
292 }
293
294 void SceneHolder::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
295 {
296   mScene.SurfaceRotated(width, height, windowOrientation, screenOrientation);
297 }
298
299 void SceneHolder::SetRotationCompletedAcknowledgement()
300 {
301   mScene.SetRotationCompletedAcknowledgement();
302 }
303
304 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
305 {
306   if(DALI_UNLIKELY(!mAdaptorStarted))
307   {
308     DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
309     return;
310   }
311
312   if(timeStamp < 1)
313   {
314     timeStamp = TimeService::GetMilliSeconds();
315   }
316
317   Vector2 convertedPosition = RecalculatePosition(point.GetScreenPosition());
318   point.SetScreenPosition(convertedPosition);
319
320   Integration::TouchEvent                            touchEvent;
321   Integration::HoverEvent                            hoverEvent;
322   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
323   if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
324   {
325     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);
326
327     // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
328     // Keep the handle alive until the core events are processed.
329     Dali::BaseHandle sceneHolder(this);
330
331     // First the touch and/or hover event & related gesture events are queued
332     if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
333     {
334       mLastTouchEvent = Dali::Integration::NewTouchEvent(timeStamp, point);
335       mScene.QueueEvent(touchEvent);
336     }
337
338     if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
339     {
340       mLastHoverEvent = Dali::Integration::NewHoverEvent(timeStamp, point);
341       mScene.QueueEvent(hoverEvent);
342     }
343
344     // Next the events are processed with a single call into Core
345     mAdaptor->ProcessCoreEvents();
346   }
347 }
348
349 const Dali::TouchEvent& SceneHolder::GetLastTouchEvent() const
350 {
351   return mLastTouchEvent;
352 }
353
354 const Dali::HoverEvent& SceneHolder::GetLastHoverEvent() const
355 {
356   return mLastHoverEvent;
357 }
358
359 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
360 {
361   if(DALI_UNLIKELY(!mAdaptorStarted))
362   {
363     DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
364     return;
365   }
366
367   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
368   // Keep the handle alive until the core events are processed.
369   Dali::BaseHandle sceneHolder(this);
370
371   Vector2 convertedPosition = RecalculatePosition(wheelEvent.point);
372   wheelEvent.point          = convertedPosition;
373
374   mScene.QueueEvent(wheelEvent);
375   mAdaptor->ProcessCoreEvents();
376 }
377
378 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
379 {
380   if(DALI_UNLIKELY(!mAdaptorStarted))
381   {
382     DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
383     return;
384   }
385
386   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
387   if(physicalKeyboard)
388   {
389     if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
390     {
391       GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
392     }
393   }
394
395   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
396   // Keep the handle alive until the core events are processed.
397   Dali::BaseHandle sceneHolder(this);
398
399   // Create send KeyEvent to Core.
400   mScene.QueueEvent(keyEvent);
401   mAdaptor->ProcessCoreEvents();
402 }
403
404 void SceneHolder::FeedHoverEvent(Dali::Integration::Point& point)
405 {
406   if(DALI_UNLIKELY(!mAdaptorStarted))
407   {
408     DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
409     return;
410   }
411
412   Integration::HoverEvent hoverEvent;
413
414   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
415   // Keep the handle alive until the core events are processed.
416   Dali::BaseHandle sceneHolder(this);
417
418   // Create send HoverEvent to Core.
419   hoverEvent.time = TimeService::GetMilliSeconds();
420   hoverEvent.AddPoint(point);
421
422   mScene.QueueEvent(hoverEvent);
423   mAdaptor->ProcessCoreEvents();
424 }
425
426 void SceneHolder::SetGeometryHittestEnabled(bool enabled)
427 {
428   mScene.SetGeometryHittestEnabled(enabled);
429 }
430
431 bool SceneHolder::IsGeometryHittestEnabled()
432 {
433   return mScene.IsGeometryHittestEnabled();
434 }
435
436 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
437 {
438   mScene.AddFrameRenderedCallback(std::move(callback), frameId);
439
440   DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
441 }
442
443 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
444 {
445   mScene.AddFramePresentedCallback(std::move(callback), frameId);
446
447   DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
448 }
449
450 Dali::RenderTaskList SceneHolder::GetRenderTaskList() const
451 {
452   return mScene.GetRenderTaskList();
453 }
454
455 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
456 {
457   SceneHolder* sceneHolderImpl = nullptr;
458
459   if(Internal::Adaptor::Adaptor::IsAvailable())
460   {
461     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
462     sceneHolderImpl                           = adaptor.GetWindow(actor);
463   }
464
465   return Dali::Integration::SceneHolder(sceneHolderImpl);
466 }
467
468 void SceneHolder::Reset()
469 {
470   DALI_ASSERT_ALWAYS(mAdaptorStarted && "Adaptor is stopped, or not be started yet!");
471
472   mCombiner.Reset();
473
474   // Any touch listeners should be told of the interruption.
475   Integration::TouchEvent event;
476   Integration::Point      point;
477   point.SetState(PointState::INTERRUPTED);
478   event.AddPoint(point);
479
480   // First the touch event & related gesture events are queued
481   mScene.QueueEvent(event);
482
483   // Next the events are processed with a single call into Core
484   mAdaptor->ProcessCoreEvents();
485 }
486
487 void SceneHolder::InitializeDpi()
488 {
489   unsigned int dpiHorizontal, dpiVertical;
490   dpiHorizontal = dpiVertical = 0;
491
492   mSurface->GetDpi(dpiHorizontal, dpiVertical);
493   mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
494
495   mDpi.SetX(dpiHorizontal);
496   mDpi.SetY(dpiVertical);
497 }
498
499 } // namespace Adaptor
500
501 } // namespace Internal
502
503 } // namespace Dali