Merge "[Tizen] Fix bitfield BMP file error" into tizen_7.0
[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   if(DALI_LIKELY(mScene))
187   {
188     mScene.SurfaceResized(width, height);
189   }
190
191   mSurface->SetFullSwapNextFrame();
192
193   // Recreate the render target
194   CreateRenderTarget();
195 }
196
197 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
198 {
199   return mSurface.get();
200 }
201
202 void SceneHolder::SetBackgroundColor(const Vector4& color)
203 {
204   if(mScene)
205   {
206     mScene.SetBackgroundColor(color);
207
208     mSurface->SetFullSwapNextFrame();
209   }
210 }
211
212 Vector4 SceneHolder::GetBackgroundColor() const
213 {
214   return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
215 }
216
217 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
218 {
219   // Avoid doing this more than once
220   if(mAdaptorStarted)
221   {
222     return;
223   }
224
225   DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
226
227   mAdaptorStarted = true;
228
229   // Create the scene
230   PositionSize surfacePositionSize = mSurface->GetPositionSize();
231   int          windowOrientation   = mSurface->GetSurfaceOrientation();
232   int          screenOrientation   = mSurface->GetScreenOrientation();
233
234   mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)), windowOrientation, screenOrientation);
235
236   Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
237   mAdaptor                                = &adaptorImpl;
238
239   // Create an observer for the adaptor lifecycle
240   mAdaptor->AddObserver(*mLifeCycleObserver);
241
242   InitializeDpi();
243
244   mSurface->SetAdaptor(*mAdaptor);
245   mSurface->SetScene(mScene);
246
247   // Create the render target
248   CreateRenderTarget();
249
250   OnAdaptorSet(adaptor);
251 }
252
253 void SceneHolder::CreateRenderTarget()
254 {
255   Graphics::RenderTargetCreateInfo rtInfo{};
256   rtInfo
257     .SetSurface(mSurface.get())
258     .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
259     .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
260
261   mScene.SetSurfaceRenderTarget(rtInfo);
262 }
263
264 void SceneHolder::Pause()
265 {
266   Reset();
267
268   OnPause();
269 }
270
271 void SceneHolder::Resume()
272 {
273   Reset();
274
275   OnResume();
276 }
277
278 void SceneHolder::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
279 {
280   if(DALI_LIKELY(mScene))
281   {
282     mScene.SurfaceRotated(width, height, windowOrientation, screenOrientation);
283   }
284 }
285
286 void SceneHolder::SetRotationCompletedAcknowledgement()
287 {
288   if(DALI_LIKELY(mScene))
289   {
290     mScene.SetRotationCompletedAcknowledgement();
291   }
292 }
293
294 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
295 {
296   if(timeStamp < 1)
297   {
298     timeStamp = TimeService::GetMilliSeconds();
299   }
300
301   Vector2 convertedPosition = RecalculatePosition(point.GetScreenPosition());
302   point.SetScreenPosition(convertedPosition);
303
304   Integration::TouchEvent                            touchEvent;
305   Integration::HoverEvent                            hoverEvent;
306   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
307   if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
308   {
309     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);
310
311     // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
312     // Keep the handle alive until the core events are processed.
313     Dali::BaseHandle sceneHolder(this);
314
315     // First the touch and/or hover event & related gesture events are queued
316     if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
317     {
318       mScene.QueueEvent(touchEvent);
319     }
320
321     if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
322     {
323       mScene.QueueEvent(hoverEvent);
324     }
325
326     // Next the events are processed with a single call into Core
327     mAdaptor->ProcessCoreEvents();
328   }
329 }
330
331 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
332 {
333   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
334   // Keep the handle alive until the core events are processed.
335   Dali::BaseHandle sceneHolder(this);
336
337   Vector2 convertedPosition = RecalculatePosition(wheelEvent.point);
338   wheelEvent.point          = convertedPosition;
339
340   mScene.QueueEvent(wheelEvent);
341   mAdaptor->ProcessCoreEvents();
342 }
343
344 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
345 {
346   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
347   if(physicalKeyboard)
348   {
349     if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
350     {
351       GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
352     }
353   }
354
355   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
356   // Keep the handle alive until the core events are processed.
357   Dali::BaseHandle sceneHolder(this);
358
359   // Create send KeyEvent to Core.
360   mScene.QueueEvent(keyEvent);
361   mAdaptor->ProcessCoreEvents();
362 }
363
364 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
365 {
366   mScene.AddFrameRenderedCallback(std::move(callback), frameId);
367
368   DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
369 }
370
371 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
372 {
373   mScene.AddFramePresentedCallback(std::move(callback), frameId);
374
375   DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
376 }
377
378 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
379 {
380   SceneHolder* sceneHolderImpl = nullptr;
381
382   if(Internal::Adaptor::Adaptor::IsAvailable())
383   {
384     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
385     sceneHolderImpl                           = adaptor.GetWindow(actor);
386   }
387
388   return Dali::Integration::SceneHolder(sceneHolderImpl);
389 }
390
391 void SceneHolder::Reset()
392 {
393   mCombiner.Reset();
394
395   // Any touch listeners should be told of the interruption.
396   Integration::TouchEvent event;
397   Integration::Point      point;
398   point.SetState(PointState::INTERRUPTED);
399   event.AddPoint(point);
400
401   // First the touch event & related gesture events are queued
402   mScene.QueueEvent(event);
403
404   // Next the events are processed with a single call into Core
405   mAdaptor->ProcessCoreEvents();
406 }
407
408 void SceneHolder::InitializeDpi()
409 {
410   unsigned int dpiHorizontal, dpiVertical;
411   dpiHorizontal = dpiVertical = 0;
412
413   mSurface->GetDpi(dpiHorizontal, dpiVertical);
414   mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
415
416   mDpi.SetX(dpiHorizontal);
417   mDpi.SetY(dpiVertical);
418 }
419
420 } // namespace Adaptor
421
422 } // namespace Internal
423
424 } // namespace Dali