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