Merge "Add common enum type for Window" into devel/master
[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 #include <sys/time.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(mAdaptor)
90   {
91     mAdaptor->RemoveObserver(*mLifeCycleObserver.get());
92     mAdaptor->RemoveWindow(this);
93
94     mAdaptor->DeleteSurface(*mSurface.get());
95
96     mAdaptor = nullptr;
97   }
98
99   if(mScene)
100   {
101     mScene.Discard();
102   }
103 }
104
105 void SceneHolder::Add(Dali::Actor actor)
106 {
107   if(mScene)
108   {
109     mScene.Add(actor);
110   }
111 }
112
113 void SceneHolder::Remove(Dali::Actor actor)
114 {
115   if(mScene)
116   {
117     mScene.Remove(actor);
118   }
119 }
120
121 Dali::Layer SceneHolder::GetRootLayer() const
122 {
123   return mScene ? mScene.GetRootLayer() : Dali::Layer();
124 }
125
126 uint32_t SceneHolder::GetId() const
127 {
128   return mId;
129 }
130
131 std::string SceneHolder::GetName() const
132 {
133   return mName;
134 }
135
136 bool SceneHolder::IsVisible() const
137 {
138   return mVisible;
139 }
140
141 Dali::Integration::Scene SceneHolder::GetScene()
142 {
143   return mScene;
144 }
145
146 Uint16Pair SceneHolder::GetDpi() const
147 {
148   return mDpi;
149 }
150
151 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
152 {
153   mSurface.reset(surface);
154
155   mScene.SurfaceReplaced();
156
157   SurfaceResized();
158
159   InitializeDpi();
160
161   mSurface->SetAdaptor(*mAdaptor);
162   mSurface->SetScene(mScene);
163
164   OnSurfaceSet(surface);
165 }
166
167 void SceneHolder::SurfaceResized()
168 {
169   PositionSize surfacePositionSize = mSurface->GetPositionSize();
170   mScene.SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
171
172   mSurface->SetFullSwapNextFrame();
173 }
174
175 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
176 {
177   return mSurface.get();
178 }
179
180 void SceneHolder::SetBackgroundColor(const Vector4& color)
181 {
182   if(mScene)
183   {
184     mScene.SetBackgroundColor(color);
185
186     mSurface->SetFullSwapNextFrame();
187   }
188 }
189
190 Vector4 SceneHolder::GetBackgroundColor() const
191 {
192   return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
193 }
194
195 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
196 {
197   // Avoid doing this more than once
198   if(mAdaptorStarted)
199   {
200     return;
201   }
202
203   DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
204
205   mAdaptorStarted = true;
206
207   // Create the scene
208   PositionSize surfacePositionSize = mSurface->GetPositionSize();
209   mScene                           = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)));
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::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
240 {
241   if(timeStamp < 1)
242   {
243     timeStamp = TimeService::GetMilliSeconds();
244   }
245
246   RecalculateTouchPosition(point);
247
248   Integration::TouchEvent                            touchEvent;
249   Integration::HoverEvent                            hoverEvent;
250   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
251   if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
252   {
253     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);
254
255     // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
256     // Keep the handle alive until the core events are processed.
257     Dali::BaseHandle sceneHolder(this);
258
259     // First the touch and/or hover event & related gesture events are queued
260     if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
261     {
262       mScene.QueueEvent(touchEvent);
263     }
264
265     if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
266     {
267       mScene.QueueEvent(hoverEvent);
268     }
269
270     // Next the events are processed with a single call into Core
271     mAdaptor->ProcessCoreEvents();
272   }
273 }
274
275 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
276 {
277   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
278   // Keep the handle alive until the core events are processed.
279   Dali::BaseHandle sceneHolder(this);
280
281   mScene.QueueEvent(wheelEvent);
282   mAdaptor->ProcessCoreEvents();
283 }
284
285 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
286 {
287   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
288   if(physicalKeyboard)
289   {
290     if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
291     {
292       GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
293     }
294   }
295
296   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
297   // Keep the handle alive until the core events are processed.
298   Dali::BaseHandle sceneHolder(this);
299
300   // Create send KeyEvent to Core.
301   mScene.QueueEvent(keyEvent);
302   mAdaptor->ProcessCoreEvents();
303 }
304
305 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
306 {
307   mScene.AddFrameRenderedCallback(std::move(callback), frameId);
308
309   DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
310 }
311
312 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
313 {
314   mScene.AddFramePresentedCallback(std::move(callback), frameId);
315
316   DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
317 }
318
319 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
320 {
321   SceneHolder* sceneHolderImpl = nullptr;
322
323   if(Internal::Adaptor::Adaptor::IsAvailable())
324   {
325     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
326     sceneHolderImpl                           = adaptor.GetWindow(actor);
327   }
328
329   return Dali::Integration::SceneHolder(sceneHolderImpl);
330 }
331
332 void SceneHolder::Reset()
333 {
334   mCombiner.Reset();
335
336   // Any touch listeners should be told of the interruption.
337   Integration::TouchEvent event;
338   Integration::Point      point;
339   point.SetState(PointState::INTERRUPTED);
340   event.AddPoint(point);
341
342   // First the touch event & related gesture events are queued
343   mScene.QueueEvent(event);
344
345   // Next the events are processed with a single call into Core
346   mAdaptor->ProcessCoreEvents();
347 }
348
349 void SceneHolder::InitializeDpi()
350 {
351   unsigned int dpiHorizontal, dpiVertical;
352   dpiHorizontal = dpiVertical = 0;
353
354   mSurface->GetDpi(dpiHorizontal, dpiVertical);
355   mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
356
357   mDpi.SetX(dpiHorizontal);
358   mDpi.SetY(dpiVertical);
359 }
360
361 } // namespace Adaptor
362
363 } // namespace Internal
364
365 } // namespace Dali