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>
38 #include <dali/internal/system/common/time-service.h>
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gSceneHolderLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_SCENE_HOLDER");
51 } // unnamed namespace
53 uint32_t SceneHolder::mSceneHolderCounter = 0;
55 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
58 SceneHolderLifeCycleObserver(Adaptor*& adaptor)
59 : mAdaptor(adaptor){};
61 private: // Adaptor::LifeCycleObserver interface
62 void OnStart() override{};
63 void OnPause() override{};
64 void OnResume() override{};
65 void OnStop() override{};
66 void OnDestroy() override
75 SceneHolder::SceneHolder()
76 : mLifeCycleObserver(new SceneHolderLifeCycleObserver(mAdaptor)),
77 mId(mSceneHolderCounter++),
81 mIsBeingDeleted(false),
82 mAdaptorStarted(false),
87 SceneHolder::~SceneHolder()
91 mAdaptor->RemoveObserver(*mLifeCycleObserver.get());
92 mAdaptor->RemoveWindow(this);
94 mAdaptor->DeleteSurface(*mSurface.get());
105 void SceneHolder::Add(Dali::Actor actor)
113 void SceneHolder::Remove(Dali::Actor actor)
117 mScene.Remove(actor);
121 Dali::Layer SceneHolder::GetRootLayer() const
123 return mScene ? mScene.GetRootLayer() : Dali::Layer();
126 uint32_t SceneHolder::GetId() const
131 std::string SceneHolder::GetName() const
136 bool SceneHolder::IsVisible() const
141 Dali::Integration::Scene SceneHolder::GetScene()
146 Uint16Pair SceneHolder::GetDpi() const
151 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
153 mSurface.reset(surface);
155 mScene.SurfaceReplaced();
161 mSurface->SetAdaptor(*mAdaptor);
162 mSurface->SetScene(mScene);
164 OnSurfaceSet(surface);
167 void SceneHolder::SurfaceResized()
169 PositionSize surfacePositionSize = mSurface->GetPositionSize();
170 mScene.SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
172 mSurface->SetFullSwapNextFrame();
175 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
177 return mSurface.get();
180 void SceneHolder::SetBackgroundColor(const Vector4& color)
184 mScene.SetBackgroundColor(color);
186 mSurface->SetFullSwapNextFrame();
190 Vector4 SceneHolder::GetBackgroundColor() const
192 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
195 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
197 // Avoid doing this more than once
203 DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
205 mAdaptorStarted = true;
208 PositionSize surfacePositionSize = mSurface->GetPositionSize();
209 mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)));
211 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
212 mAdaptor = &adaptorImpl;
214 // Create an observer for the adaptor lifecycle
215 mAdaptor->AddObserver(*mLifeCycleObserver);
219 mSurface->SetAdaptor(*mAdaptor);
220 mSurface->SetScene(mScene);
222 OnAdaptorSet(adaptor);
225 void SceneHolder::Pause()
232 void SceneHolder::Resume()
239 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
243 timeStamp = TimeService::GetMilliSeconds();
246 RecalculateTouchPosition(point);
248 Integration::TouchEvent touchEvent;
249 Integration::HoverEvent hoverEvent;
250 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
251 if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
253 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);
255 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
256 // Keep the handle alive until the core events are processed.
257 Dali::BaseHandle sceneHolder(this);
259 // First the touch and/or hover event & related gesture events are queued
260 if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
262 mScene.QueueEvent(touchEvent);
265 if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
267 mScene.QueueEvent(hoverEvent);
270 // Next the events are processed with a single call into Core
271 mAdaptor->ProcessCoreEvents();
275 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
277 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
278 // Keep the handle alive until the core events are processed.
279 Dali::BaseHandle sceneHolder(this);
281 mScene.QueueEvent(wheelEvent);
282 mAdaptor->ProcessCoreEvents();
285 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
287 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
290 if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
292 GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
296 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
297 // Keep the handle alive until the core events are processed.
298 Dali::BaseHandle sceneHolder(this);
300 // Create send KeyEvent to Core.
301 mScene.QueueEvent(keyEvent);
302 mAdaptor->ProcessCoreEvents();
305 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
307 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
309 DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
312 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
314 mScene.AddFramePresentedCallback(std::move(callback), frameId);
316 DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
319 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
321 SceneHolder* sceneHolderImpl = nullptr;
323 if(Internal::Adaptor::Adaptor::IsAvailable())
325 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
326 sceneHolderImpl = adaptor.GetWindow(actor);
329 return Dali::Integration::SceneHolder(sceneHolderImpl);
332 void SceneHolder::Reset()
336 // Any touch listeners should be told of the interruption.
337 Integration::TouchEvent event;
338 Integration::Point point;
339 point.SetState(PointState::INTERRUPTED);
340 event.AddPoint(point);
342 // First the touch event & related gesture events are queued
343 mScene.QueueEvent(event);
345 // Next the events are processed with a single call into Core
346 mAdaptor->ProcessCoreEvents();
349 void SceneHolder::InitializeDpi()
351 unsigned int dpiHorizontal, dpiVertical;
352 dpiHorizontal = dpiVertical = 0;
354 mSurface->GetDpi(dpiHorizontal, dpiVertical);
355 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
357 mDpi.SetX(dpiHorizontal);
358 mDpi.SetY(dpiVertical);
361 } // namespace Adaptor
363 } // namespace Internal