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();
193 SurfaceResized(false);
197 mSurface->SetAdaptor(*mAdaptor);
198 mSurface->SetScene(mScene);
200 OnSurfaceSet(surface);
203 void SceneHolder::SurfaceResized(bool forceUpdate)
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);
209 mSurface->SetFullSwapNextFrame();
212 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
214 return mSurface.get();
217 void SceneHolder::SetBackgroundColor(const Vector4& color)
221 mScene.SetBackgroundColor(color);
223 mSurface->SetFullSwapNextFrame();
227 Vector4 SceneHolder::GetBackgroundColor() const
229 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
232 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
234 // Avoid doing this more than once
240 DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
242 mAdaptorStarted = true;
245 PositionSize surfacePositionSize = mSurface->GetPositionSize();
246 mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)));
248 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
249 mAdaptor = &adaptorImpl;
251 // Create an observer for the adaptor lifecycle
252 mAdaptor->AddObserver(*mLifeCycleObserver);
256 mSurface->SetAdaptor(*mAdaptor);
257 mSurface->SetScene(mScene);
259 OnAdaptorSet(adaptor);
262 void SceneHolder::Pause()
269 void SceneHolder::Resume()
276 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
280 timeStamp = GetCurrentMilliSeconds();
283 RecalculateTouchPosition(point);
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)
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);
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);
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)
299 mScene.QueueEvent(touchEvent);
302 if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
304 mScene.QueueEvent(hoverEvent);
307 // Next the events are processed with a single call into Core
308 mAdaptor->ProcessCoreEvents();
312 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
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);
318 mScene.QueueEvent(wheelEvent);
319 mAdaptor->ProcessCoreEvents();
322 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
324 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
327 if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
329 GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
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);
337 // Create send KeyEvent to Core.
338 mScene.QueueEvent(keyEvent);
339 mAdaptor->ProcessCoreEvents();
342 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
344 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
346 DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
349 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
351 mScene.AddFramePresentedCallback(std::move(callback), frameId);
353 DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
356 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
358 SceneHolder* sceneHolderImpl = nullptr;
360 if(Internal::Adaptor::Adaptor::IsAvailable())
362 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
363 sceneHolderImpl = adaptor.GetWindow(actor);
366 return Dali::Integration::SceneHolder(sceneHolderImpl);
369 void SceneHolder::Reset()
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);
379 // First the touch event & related gesture events are queued
380 mScene.QueueEvent(event);
382 // Next the events are processed with a single call into Core
383 mAdaptor->ProcessCoreEvents();
386 void SceneHolder::InitializeDpi()
388 unsigned int dpiHorizontal, dpiVertical;
389 dpiHorizontal = dpiVertical = 0;
391 mSurface->GetDpi(dpiHorizontal, dpiVertical);
392 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
394 mDpi.SetX(dpiHorizontal);
395 mDpi.SetY(dpiVertical);
398 } // namespace Adaptor
400 } // namespace Internal