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 int orientation = mSurface->GetOrientation();
247 mScene = Dali::Integration::Scene::New( Size(static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height )), orientation );
249 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
250 mAdaptor = &adaptorImpl;
252 // Create an observer for the adaptor lifecycle
253 mAdaptor->AddObserver(*mLifeCycleObserver);
257 mSurface->SetAdaptor(*mAdaptor);
258 mSurface->SetScene(mScene);
260 OnAdaptorSet(adaptor);
263 void SceneHolder::Pause()
270 void SceneHolder::Resume()
277 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
281 timeStamp = GetCurrentMilliSeconds();
284 RecalculateTouchPosition(point);
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)
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);
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);
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)
300 mScene.QueueEvent(touchEvent);
303 if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
305 mScene.QueueEvent(hoverEvent);
308 // Next the events are processed with a single call into Core
309 mAdaptor->ProcessCoreEvents();
313 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
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);
319 mScene.QueueEvent(wheelEvent);
320 mAdaptor->ProcessCoreEvents();
323 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
325 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
328 if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
330 GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
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);
338 // Create send KeyEvent to Core.
339 mScene.QueueEvent(keyEvent);
340 mAdaptor->ProcessCoreEvents();
343 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
345 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
347 DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
350 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
352 mScene.AddFramePresentedCallback(std::move(callback), frameId);
354 DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
357 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
359 SceneHolder* sceneHolderImpl = nullptr;
361 if(Internal::Adaptor::Adaptor::IsAvailable())
363 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
364 sceneHolderImpl = adaptor.GetWindow(actor);
367 return Dali::Integration::SceneHolder(sceneHolderImpl);
370 void SceneHolder::Reset()
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);
380 // First the touch event & related gesture events are queued
381 mScene.QueueEvent(event);
383 // Next the events are processed with a single call into Core
384 mAdaptor->ProcessCoreEvents();
387 void SceneHolder::InitializeDpi()
389 unsigned int dpiHorizontal, dpiVertical;
390 dpiHorizontal = dpiVertical = 0;
392 mSurface->GetDpi(dpiHorizontal, dpiVertical);
393 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
395 mDpi.SetX(dpiHorizontal);
396 mDpi.SetY(dpiVertical);
399 } // namespace Adaptor
401 } // namespace Internal