Revert "[Tizen] Apply screen rotation to create window"
[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   mScene                           = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)));
247
248   Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
249   mAdaptor                                = &adaptorImpl;
250
251   // Create an observer for the adaptor lifecycle
252   mAdaptor->AddObserver(*mLifeCycleObserver);
253
254   InitializeDpi();
255
256   mSurface->SetAdaptor(*mAdaptor);
257   mSurface->SetScene(mScene);
258
259   OnAdaptorSet(adaptor);
260 }
261
262 void SceneHolder::Pause()
263 {
264   Reset();
265
266   OnPause();
267 }
268
269 void SceneHolder::Resume()
270 {
271   Reset();
272
273   OnResume();
274 }
275
276 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
277 {
278   if(timeStamp < 1)
279   {
280     timeStamp = GetCurrentMilliSeconds();
281   }
282
283   RecalculateTouchPosition(point);
284
285   Integration::TouchEvent                            touchEvent;
286   Integration::HoverEvent                            hoverEvent;
287   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
288   if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
289   {
290     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);
291
292     // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
293     // Keep the handle alive until the core events are processed.
294     Dali::BaseHandle sceneHolder(this);
295
296     // First the touch and/or hover event & related gesture events are queued
297     if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
298     {
299       mScene.QueueEvent(touchEvent);
300     }
301
302     if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
303     {
304       mScene.QueueEvent(hoverEvent);
305     }
306
307     // Next the events are processed with a single call into Core
308     mAdaptor->ProcessCoreEvents();
309   }
310 }
311
312 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
313 {
314   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
315   // Keep the handle alive until the core events are processed.
316   Dali::BaseHandle sceneHolder(this);
317
318   mScene.QueueEvent(wheelEvent);
319   mAdaptor->ProcessCoreEvents();
320 }
321
322 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
323 {
324   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
325   if(physicalKeyboard)
326   {
327     if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
328     {
329       GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
330     }
331   }
332
333   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
334   // Keep the handle alive until the core events are processed.
335   Dali::BaseHandle sceneHolder(this);
336
337   // Create send KeyEvent to Core.
338   mScene.QueueEvent(keyEvent);
339   mAdaptor->ProcessCoreEvents();
340 }
341
342 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
343 {
344   mScene.AddFrameRenderedCallback(std::move(callback), frameId);
345
346   DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
347 }
348
349 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
350 {
351   mScene.AddFramePresentedCallback(std::move(callback), frameId);
352
353   DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
354 }
355
356 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
357 {
358   SceneHolder* sceneHolderImpl = nullptr;
359
360   if(Internal::Adaptor::Adaptor::IsAvailable())
361   {
362     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
363     sceneHolderImpl                           = adaptor.GetWindow(actor);
364   }
365
366   return Dali::Integration::SceneHolder(sceneHolderImpl);
367 }
368
369 void SceneHolder::Reset()
370 {
371   mCombiner.Reset();
372
373   // Any touch listeners should be told of the interruption.
374   Integration::TouchEvent event;
375   Integration::Point      point;
376   point.SetState(PointState::INTERRUPTED);
377   event.AddPoint(point);
378
379   // First the touch event & related gesture events are queued
380   mScene.QueueEvent(event);
381
382   // Next the events are processed with a single call into Core
383   mAdaptor->ProcessCoreEvents();
384 }
385
386 void SceneHolder::InitializeDpi()
387 {
388   unsigned int dpiHorizontal, dpiVertical;
389   dpiHorizontal = dpiVertical = 0;
390
391   mSurface->GetDpi(dpiHorizontal, dpiVertical);
392   mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
393
394   mDpi.SetX(dpiHorizontal);
395   mDpi.SetY(dpiVertical);
396 }
397
398 } // namespace Adaptor
399
400 } // namespace Internal
401
402 } // namespace Dali