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