ea8a58a7a2e25376108e1eca4f9564e10242bed0
[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
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
51 // Copied from x server
52 static uint32_t GetCurrentMilliSeconds(void)
53 {
54   struct timeval tv;
55
56   struct timespec  tp;
57   static clockid_t clockid;
58
59   if(!clockid)
60   {
61 #ifdef CLOCK_MONOTONIC_COARSE
62     if(clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
63        (tp.tv_nsec / 1000) <= 1000 && clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
64     {
65       clockid = CLOCK_MONOTONIC_COARSE;
66     }
67     else
68 #endif
69       if(clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
70     {
71       clockid = CLOCK_MONOTONIC;
72     }
73     else
74     {
75       clockid = ~0L;
76     }
77   }
78   if(clockid != ~0L && clock_gettime(clockid, &tp) == 0)
79   {
80     return static_cast<uint32_t>((tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L));
81   }
82
83   gettimeofday(&tv, NULL);
84   return static_cast<uint32_t>((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
85 }
86
87 } // unnamed namespace
88
89 uint32_t SceneHolder::mSceneHolderCounter = 0;
90
91 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
92 {
93 public:
94   SceneHolderLifeCycleObserver(Adaptor*& adaptor)
95   : mAdaptor(adaptor){};
96
97 private: // Adaptor::LifeCycleObserver interface
98   void OnStart() override{};
99   void OnPause() override{};
100   void OnResume() override{};
101   void OnStop() override{};
102   void OnDestroy() override
103   {
104     mAdaptor = nullptr;
105   };
106
107 private:
108   Adaptor*& mAdaptor;
109 };
110
111 SceneHolder::SceneHolder()
112 : mLifeCycleObserver(new SceneHolderLifeCycleObserver(mAdaptor)),
113   mId(mSceneHolderCounter++),
114   mSurface(nullptr),
115   mAdaptor(nullptr),
116   mDpi(),
117   mIsBeingDeleted(false),
118   mAdaptorStarted(false),
119   mVisible(true)
120 {
121 }
122
123 SceneHolder::~SceneHolder()
124 {
125   if(mAdaptor)
126   {
127     mAdaptor->RemoveObserver(*mLifeCycleObserver.get());
128     mAdaptor->RemoveWindow(this);
129
130     mAdaptor->DeleteSurface(*mSurface.get());
131
132     mAdaptor = nullptr;
133   }
134
135   if(mScene)
136   {
137     mScene.Discard();
138   }
139 }
140
141 void SceneHolder::Add(Dali::Actor actor)
142 {
143   if(mScene)
144   {
145     mScene.Add(actor);
146   }
147 }
148
149 void SceneHolder::Remove(Dali::Actor actor)
150 {
151   if(mScene)
152   {
153     mScene.Remove(actor);
154   }
155 }
156
157 Dali::Layer SceneHolder::GetRootLayer() const
158 {
159   return mScene ? mScene.GetRootLayer() : Dali::Layer();
160 }
161
162 uint32_t SceneHolder::GetId() const
163 {
164   return mId;
165 }
166
167 std::string SceneHolder::GetName() const
168 {
169   return mName;
170 }
171
172 bool SceneHolder::IsVisible() const
173 {
174   return mVisible;
175 }
176
177 Dali::Integration::Scene SceneHolder::GetScene()
178 {
179   return mScene;
180 }
181
182 Uint16Pair SceneHolder::GetDpi() const
183 {
184   return mDpi;
185 }
186
187 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
188 {
189   mSurface.reset(surface);
190
191   mScene.SurfaceReplaced();
192
193   SurfaceResized(false);
194
195   InitializeDpi();
196
197   mSurface->SetAdaptor(*mAdaptor);
198   mSurface->SetScene(mScene);
199
200   OnSurfaceSet(surface);
201 }
202
203 void SceneHolder::SurfaceResized(bool forceUpdate)
204 {
205   PositionSize surfacePositionSize = mSurface->GetPositionSize();
206   int          orientation         = mSurface->GetOrientation();
207   mScene.SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height), orientation, forceUpdate);
208
209   mSurface->SetFullSwapNextFrame();
210 }
211
212 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
213 {
214   return mSurface.get();
215 }
216
217 void SceneHolder::SetBackgroundColor(const Vector4& color)
218 {
219   if(mScene)
220   {
221     mScene.SetBackgroundColor(color);
222
223     mSurface->SetFullSwapNextFrame();
224   }
225 }
226
227 Vector4 SceneHolder::GetBackgroundColor() const
228 {
229   return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
230 }
231
232 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
233 {
234   // Avoid doing this more than once
235   if(mAdaptorStarted)
236   {
237     return;
238   }
239
240   DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
241
242   mAdaptorStarted = true;
243
244   // Create the scene
245   PositionSize surfacePositionSize = mSurface->GetPositionSize();
246   int orientation = mSurface->GetOrientation();
247   mScene = Dali::Integration::Scene::New( Size(static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height )), orientation );
248
249   Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
250   mAdaptor                                = &adaptorImpl;
251
252   // Create an observer for the adaptor lifecycle
253   mAdaptor->AddObserver(*mLifeCycleObserver);
254
255   InitializeDpi();
256
257   mSurface->SetAdaptor(*mAdaptor);
258   mSurface->SetScene(mScene);
259
260   OnAdaptorSet(adaptor);
261 }
262
263 void SceneHolder::Pause()
264 {
265   Reset();
266
267   OnPause();
268 }
269
270 void SceneHolder::Resume()
271 {
272   Reset();
273
274   OnResume();
275 }
276
277 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
278 {
279   if(timeStamp < 1)
280   {
281     timeStamp = GetCurrentMilliSeconds();
282   }
283
284   RecalculateTouchPosition(point);
285
286   Integration::TouchEvent                            touchEvent;
287   Integration::HoverEvent                            hoverEvent;
288   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
289   if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
290   {
291     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);
292
293     // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
294     // Keep the handle alive until the core events are processed.
295     Dali::BaseHandle sceneHolder(this);
296
297     // First the touch and/or hover event & related gesture events are queued
298     if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
299     {
300       mScene.QueueEvent(touchEvent);
301     }
302
303     if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
304     {
305       mScene.QueueEvent(hoverEvent);
306     }
307
308     // Next the events are processed with a single call into Core
309     mAdaptor->ProcessCoreEvents();
310   }
311 }
312
313 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
314 {
315   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
316   // Keep the handle alive until the core events are processed.
317   Dali::BaseHandle sceneHolder(this);
318
319   mScene.QueueEvent(wheelEvent);
320   mAdaptor->ProcessCoreEvents();
321 }
322
323 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
324 {
325   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
326   if(physicalKeyboard)
327   {
328     if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
329     {
330       GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
331     }
332   }
333
334   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
335   // Keep the handle alive until the core events are processed.
336   Dali::BaseHandle sceneHolder(this);
337
338   // Create send KeyEvent to Core.
339   mScene.QueueEvent(keyEvent);
340   mAdaptor->ProcessCoreEvents();
341 }
342
343 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
344 {
345   mScene.AddFrameRenderedCallback(std::move(callback), frameId);
346
347   DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
348 }
349
350 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
351 {
352   mScene.AddFramePresentedCallback(std::move(callback), frameId);
353
354   DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
355 }
356
357 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
358 {
359   SceneHolder* sceneHolderImpl = nullptr;
360
361   if(Internal::Adaptor::Adaptor::IsAvailable())
362   {
363     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
364     sceneHolderImpl                           = adaptor.GetWindow(actor);
365   }
366
367   return Dali::Integration::SceneHolder(sceneHolderImpl);
368 }
369
370 void SceneHolder::Reset()
371 {
372   mCombiner.Reset();
373
374   // Any touch listeners should be told of the interruption.
375   Integration::TouchEvent event;
376   Integration::Point      point;
377   point.SetState(PointState::INTERRUPTED);
378   event.AddPoint(point);
379
380   // First the touch event & related gesture events are queued
381   mScene.QueueEvent(event);
382
383   // Next the events are processed with a single call into Core
384   mAdaptor->ProcessCoreEvents();
385 }
386
387 void SceneHolder::InitializeDpi()
388 {
389   unsigned int dpiHorizontal, dpiVertical;
390   dpiHorizontal = dpiVertical = 0;
391
392   mSurface->GetDpi(dpiHorizontal, dpiVertical);
393   mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
394
395   mDpi.SetX(dpiHorizontal);
396   mDpi.SetY(dpiVertical);
397 }
398
399 } // namespace Adaptor
400
401 } // namespace Internal
402
403 } // namespace Dali