2 * Copyright (c) 2022 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>
32 #include <dali/internal/adaptor/common/adaptor-impl.h>
33 #include <dali/internal/adaptor/common/lifecycle-observer.h>
34 #include <dali/internal/graphics/gles/egl-graphics.h>
35 #include <dali/internal/input/common/key-impl.h>
36 #include <dali/internal/input/common/physical-keyboard-impl.h>
37 #include <dali/internal/system/common/time-service.h>
47 #if defined(DEBUG_ENABLED)
48 Debug::Filter* gSceneHolderLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_SCENE_HOLDER");
50 } // unnamed namespace
52 uint32_t SceneHolder::mSceneHolderCounter = 0;
54 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
57 SceneHolderLifeCycleObserver(Adaptor*& adaptor)
58 : mAdaptor(adaptor){};
60 private: // Adaptor::LifeCycleObserver interface
61 void OnStart() override{};
62 void OnPause() override{};
63 void OnResume() override{};
64 void OnStop() override{};
65 void OnDestroy() override
74 SceneHolder::SceneHolder()
75 : mLifeCycleObserver(new SceneHolderLifeCycleObserver(mAdaptor)),
76 mId(mSceneHolderCounter++),
80 mIsBeingDeleted(false),
81 mAdaptorStarted(false),
86 SceneHolder::~SceneHolder()
90 mAdaptor->RemoveObserver(*mLifeCycleObserver.get());
91 mAdaptor->RemoveWindow(this);
93 mAdaptor->DeleteSurface(*mSurface.get());
104 void SceneHolder::Add(Dali::Actor actor)
112 void SceneHolder::Remove(Dali::Actor actor)
116 mScene.Remove(actor);
120 Dali::Layer SceneHolder::GetRootLayer() const
122 return mScene ? mScene.GetRootLayer() : Dali::Layer();
125 uint32_t SceneHolder::GetId() const
130 std::string SceneHolder::GetName() const
135 bool SceneHolder::IsVisible() const
140 Dali::Integration::Scene SceneHolder::GetScene()
145 Uint16Pair SceneHolder::GetDpi() const
150 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
152 mSurface.reset(surface);
154 mScene.SurfaceReplaced();
156 PositionSize surfacePositionSize = surface->GetPositionSize();
158 SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
162 mSurface->SetAdaptor(*mAdaptor);
163 mSurface->SetScene(mScene);
165 // Recreate the render target
166 CreateRenderTarget();
168 OnSurfaceSet(surface);
171 void SceneHolder::SurfaceResized(float width, float height)
173 if(DALI_LIKELY(mScene))
175 mScene.SurfaceResized(width, height);
178 mSurface->SetFullSwapNextFrame();
180 // Recreate the render target
181 CreateRenderTarget();
184 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
186 return mSurface.get();
189 void SceneHolder::SetBackgroundColor(const Vector4& color)
193 mScene.SetBackgroundColor(color);
195 mSurface->SetFullSwapNextFrame();
199 Vector4 SceneHolder::GetBackgroundColor() const
201 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
204 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
206 // Avoid doing this more than once
212 DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
214 mAdaptorStarted = true;
217 PositionSize surfacePositionSize = mSurface->GetPositionSize();
218 int windowOrientation = mSurface->GetSurfaceOrientation();
219 int screenOrientation = mSurface->GetScreenOrientation();
221 mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)), windowOrientation, screenOrientation);
223 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
224 mAdaptor = &adaptorImpl;
226 // Create an observer for the adaptor lifecycle
227 mAdaptor->AddObserver(*mLifeCycleObserver);
231 mSurface->SetAdaptor(*mAdaptor);
232 mSurface->SetScene(mScene);
234 // Create the render target
235 CreateRenderTarget();
237 OnAdaptorSet(adaptor);
240 void SceneHolder::CreateRenderTarget()
242 Graphics::RenderTargetCreateInfo rtInfo{};
244 .SetSurface(mSurface.get())
245 .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
246 .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
248 mScene.SetSurfaceRenderTarget(rtInfo);
251 void SceneHolder::Pause()
258 void SceneHolder::Resume()
265 void SceneHolder::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
267 if(DALI_LIKELY(mScene))
269 mScene.SurfaceRotated(width, height, windowOrientation, screenOrientation);
273 void SceneHolder::SetRotationCompletedAcknowledgement()
275 if(DALI_LIKELY(mScene))
277 mScene.SetRotationCompletedAcknowledgement();
281 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
285 timeStamp = TimeService::GetMilliSeconds();
288 Vector2 convertedPosition = RecalculatePosition(point.GetScreenPosition());
289 point.SetScreenPosition(convertedPosition);
291 Integration::TouchEvent touchEvent;
292 Integration::HoverEvent hoverEvent;
293 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
294 if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
296 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);
298 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
299 // Keep the handle alive until the core events are processed.
300 Dali::BaseHandle sceneHolder(this);
302 // First the touch and/or hover event & related gesture events are queued
303 if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
305 mScene.QueueEvent(touchEvent);
308 if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
310 mScene.QueueEvent(hoverEvent);
313 // Next the events are processed with a single call into Core
314 mAdaptor->ProcessCoreEvents();
318 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
320 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
321 // Keep the handle alive until the core events are processed.
322 Dali::BaseHandle sceneHolder(this);
324 Vector2 convertedPosition = RecalculatePosition(wheelEvent.point);
325 wheelEvent.point = convertedPosition;
327 mScene.QueueEvent(wheelEvent);
328 mAdaptor->ProcessCoreEvents();
331 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
333 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
336 if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
338 GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
342 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
343 // Keep the handle alive until the core events are processed.
344 Dali::BaseHandle sceneHolder(this);
346 // Create send KeyEvent to Core.
347 mScene.QueueEvent(keyEvent);
348 mAdaptor->ProcessCoreEvents();
351 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
353 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
355 DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
358 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
360 mScene.AddFramePresentedCallback(std::move(callback), frameId);
362 DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
365 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
367 SceneHolder* sceneHolderImpl = nullptr;
369 if(Internal::Adaptor::Adaptor::IsAvailable())
371 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
372 sceneHolderImpl = adaptor.GetWindow(actor);
375 return Dali::Integration::SceneHolder(sceneHolderImpl);
378 void SceneHolder::Reset()
382 // Any touch listeners should be told of the interruption.
383 Integration::TouchEvent event;
384 Integration::Point point;
385 point.SetState(PointState::INTERRUPTED);
386 event.AddPoint(point);
388 // First the touch event & related gesture events are queued
389 mScene.QueueEvent(event);
391 // Next the events are processed with a single call into Core
392 mAdaptor->ProcessCoreEvents();
395 void SceneHolder::InitializeDpi()
397 unsigned int dpiHorizontal, dpiVertical;
398 dpiHorizontal = dpiVertical = 0;
400 mSurface->GetDpi(dpiHorizontal, dpiVertical);
401 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
403 mDpi.SetX(dpiHorizontal);
404 mDpi.SetY(dpiVertical);
407 } // namespace Adaptor
409 } // namespace Internal