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