2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/integration-api/adaptor-framework/scene-holder-impl.h>
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>
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>
47 #if defined(DEBUG_ENABLED)
48 Debug::Filter* gSceneHolderLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_SCENE_HOLDER");
51 // Copied from x server
52 static uint32_t GetCurrentMilliSeconds(void)
57 static clockid_t clockid;
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)
65 clockid = CLOCK_MONOTONIC_COARSE;
69 if(clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
71 clockid = CLOCK_MONOTONIC;
78 if(clockid != ~0L && clock_gettime(clockid, &tp) == 0)
80 return static_cast<uint32_t>((tp.tv_sec * 1000) + (tp.tv_nsec / 1000000L));
83 gettimeofday(&tv, NULL);
84 return static_cast<uint32_t>((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
87 } // unnamed namespace
89 uint32_t SceneHolder::mSceneHolderCounter = 0;
91 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
94 SceneHolderLifeCycleObserver(Adaptor*& adaptor)
95 : mAdaptor(adaptor){};
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
111 SceneHolder::SceneHolder()
112 : mLifeCycleObserver(new SceneHolderLifeCycleObserver(mAdaptor)),
113 mId(mSceneHolderCounter++),
117 mIsBeingDeleted(false),
118 mAdaptorStarted(false),
123 SceneHolder::~SceneHolder()
127 mAdaptor->RemoveObserver(*mLifeCycleObserver.get());
128 mAdaptor->RemoveWindow(this);
130 mAdaptor->DeleteSurface(*mSurface.get());
141 void SceneHolder::Add(Dali::Actor actor)
149 void SceneHolder::Remove(Dali::Actor actor)
153 mScene.Remove(actor);
157 Dali::Layer SceneHolder::GetRootLayer() const
159 return mScene ? mScene.GetRootLayer() : Dali::Layer();
162 uint32_t SceneHolder::GetId() const
167 std::string SceneHolder::GetName() const
172 bool SceneHolder::IsVisible() const
177 Dali::Integration::Scene SceneHolder::GetScene()
182 Uint16Pair SceneHolder::GetDpi() const
187 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
189 mSurface.reset(surface);
191 mScene.SurfaceReplaced();
197 mSurface->SetAdaptor(*mAdaptor);
198 mSurface->SetScene(mScene);
200 OnSurfaceSet(surface);
203 void SceneHolder::SurfaceResized()
205 PositionSize surfacePositionSize = mSurface->GetPositionSize();
206 mScene.SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
208 mSurface->SetFullSwapNextFrame();
211 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
213 return mSurface.get();
216 void SceneHolder::SetBackgroundColor(const Vector4& color)
220 mScene.SetBackgroundColor(color);
222 mSurface->SetFullSwapNextFrame();
226 Vector4 SceneHolder::GetBackgroundColor() const
228 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
231 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
233 // Avoid doing this more than once
239 DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
241 mAdaptorStarted = true;
244 PositionSize surfacePositionSize = mSurface->GetPositionSize();
245 mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)));
247 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
248 mAdaptor = &adaptorImpl;
250 // Create an observer for the adaptor lifecycle
251 mAdaptor->AddObserver(*mLifeCycleObserver);
255 mSurface->SetAdaptor(*mAdaptor);
256 mSurface->SetScene(mScene);
258 OnAdaptorSet(adaptor);
261 void SceneHolder::Pause()
268 void SceneHolder::Resume()
275 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
279 timeStamp = GetCurrentMilliSeconds();
282 RecalculateTouchPosition(point);
284 Integration::TouchEvent touchEvent;
285 Integration::HoverEvent hoverEvent;
286 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
287 if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
289 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 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
292 // Keep the handle alive until the core events are processed.
293 Dali::BaseHandle sceneHolder(this);
295 // First the touch and/or hover event & related gesture events are queued
296 if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
298 mScene.QueueEvent(touchEvent);
301 if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
303 mScene.QueueEvent(hoverEvent);
306 // Next the events are processed with a single call into Core
307 mAdaptor->ProcessCoreEvents();
311 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
313 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
314 // Keep the handle alive until the core events are processed.
315 Dali::BaseHandle sceneHolder(this);
317 mScene.QueueEvent(wheelEvent);
318 mAdaptor->ProcessCoreEvents();
321 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
323 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
326 if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
328 GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
332 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
333 // Keep the handle alive until the core events are processed.
334 Dali::BaseHandle sceneHolder(this);
336 // Create send KeyEvent to Core.
337 mScene.QueueEvent(keyEvent);
338 mAdaptor->ProcessCoreEvents();
341 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
343 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
345 DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
348 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
350 mScene.AddFramePresentedCallback(std::move(callback), frameId);
352 DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
355 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
357 SceneHolder* sceneHolderImpl = nullptr;
359 if(Internal::Adaptor::Adaptor::IsAvailable())
361 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
362 sceneHolderImpl = adaptor.GetWindow(actor);
365 return Dali::Integration::SceneHolder(sceneHolderImpl);
368 void SceneHolder::Reset()
372 // Any touch listeners should be told of the interruption.
373 Integration::TouchEvent event;
374 Integration::Point point;
375 point.SetState(PointState::INTERRUPTED);
376 event.AddPoint(point);
378 // First the touch event & related gesture events are queued
379 mScene.QueueEvent(event);
381 // Next the events are processed with a single call into Core
382 mAdaptor->ProcessCoreEvents();
385 void SceneHolder::InitializeDpi()
387 unsigned int dpiHorizontal, dpiVertical;
388 dpiHorizontal = dpiVertical = 0;
390 mSurface->GetDpi(dpiHorizontal, dpiVertical);
391 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
393 mDpi.SetX(dpiHorizontal);
394 mDpi.SetY(dpiVertical);
397 } // namespace Adaptor
399 } // namespace Internal