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