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