2 * Copyright (c) 2023 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 // The scene graph object should be removed first.
91 mScene.RemoveSceneObject();
96 mAdaptor->RemoveObserver(*mLifeCycleObserver.get());
97 mAdaptor->RemoveWindow(this);
99 // The event queue is flushed and we wait for the completion of the surface removal
100 mAdaptor->DeleteSurface(*mSurface.get());
107 // We should remove the surface from the Core last
112 void SceneHolder::Add(Dali::Actor actor)
120 void SceneHolder::Remove(Dali::Actor actor)
124 mScene.Remove(actor);
128 Dali::Layer SceneHolder::GetRootLayer() const
130 return mScene ? mScene.GetRootLayer() : Dali::Layer();
133 Dali::Layer SceneHolder::GetOverlayLayer()
135 return mScene ? mScene.GetOverlayLayer() : Dali::Layer();
138 uint32_t SceneHolder::GetId() const
143 std::string SceneHolder::GetName() const
148 bool SceneHolder::IsVisible() const
153 Dali::Integration::Scene SceneHolder::GetScene()
158 Uint16Pair SceneHolder::GetDpi() const
163 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
165 mSurface.reset(surface);
167 mScene.SurfaceReplaced();
169 PositionSize surfacePositionSize = surface->GetPositionSize();
171 SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
175 mSurface->SetAdaptor(*mAdaptor);
176 mSurface->SetScene(mScene);
178 // Recreate the render target
179 CreateRenderTarget();
181 OnSurfaceSet(surface);
184 void SceneHolder::SurfaceResized(float width, float height)
186 if(DALI_LIKELY(mScene))
188 mScene.SurfaceResized(width, height);
191 mSurface->SetFullSwapNextFrame();
193 // Recreate the render target
194 CreateRenderTarget();
197 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
199 return mSurface.get();
202 void SceneHolder::SetBackgroundColor(const Vector4& color)
206 mScene.SetBackgroundColor(color);
208 mSurface->SetFullSwapNextFrame();
212 Vector4 SceneHolder::GetBackgroundColor() const
214 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
217 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
219 // Avoid doing this more than once
225 DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
227 mAdaptorStarted = true;
230 PositionSize surfacePositionSize = mSurface->GetPositionSize();
231 int windowOrientation = mSurface->GetSurfaceOrientation();
232 int screenOrientation = mSurface->GetScreenOrientation();
234 mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)), windowOrientation, screenOrientation);
236 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
237 mAdaptor = &adaptorImpl;
239 // Create an observer for the adaptor lifecycle
240 mAdaptor->AddObserver(*mLifeCycleObserver);
244 mSurface->SetAdaptor(*mAdaptor);
245 mSurface->SetScene(mScene);
247 // Create the render target
248 CreateRenderTarget();
250 OnAdaptorSet(adaptor);
253 void SceneHolder::CreateRenderTarget()
255 Graphics::RenderTargetCreateInfo rtInfo{};
257 .SetSurface(mSurface.get())
258 .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
259 .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
261 mScene.SetSurfaceRenderTarget(rtInfo);
264 void SceneHolder::Pause()
271 void SceneHolder::Resume()
278 void SceneHolder::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
280 if(DALI_LIKELY(mScene))
282 mScene.SurfaceRotated(width, height, windowOrientation, screenOrientation);
286 void SceneHolder::SetRotationCompletedAcknowledgement()
288 if(DALI_LIKELY(mScene))
290 mScene.SetRotationCompletedAcknowledgement();
294 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
298 timeStamp = TimeService::GetMilliSeconds();
301 Vector2 convertedPosition = RecalculatePosition(point.GetScreenPosition());
302 point.SetScreenPosition(convertedPosition);
304 Integration::TouchEvent touchEvent;
305 Integration::HoverEvent hoverEvent;
306 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
307 if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
309 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);
311 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
312 // Keep the handle alive until the core events are processed.
313 Dali::BaseHandle sceneHolder(this);
315 // First the touch and/or hover event & related gesture events are queued
316 if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
318 mScene.QueueEvent(touchEvent);
321 if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
323 mScene.QueueEvent(hoverEvent);
326 // Next the events are processed with a single call into Core
327 mAdaptor->ProcessCoreEvents();
331 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
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 Vector2 convertedPosition = RecalculatePosition(wheelEvent.point);
338 wheelEvent.point = convertedPosition;
340 mScene.QueueEvent(wheelEvent);
341 mAdaptor->ProcessCoreEvents();
344 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
346 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
349 if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
351 GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
355 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
356 // Keep the handle alive until the core events are processed.
357 Dali::BaseHandle sceneHolder(this);
359 // Create send KeyEvent to Core.
360 mScene.QueueEvent(keyEvent);
361 mAdaptor->ProcessCoreEvents();
364 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
366 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
368 DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
371 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
373 mScene.AddFramePresentedCallback(std::move(callback), frameId);
375 DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
378 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
380 SceneHolder* sceneHolderImpl = nullptr;
382 if(Internal::Adaptor::Adaptor::IsAvailable())
384 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
385 sceneHolderImpl = adaptor.GetWindow(actor);
388 return Dali::Integration::SceneHolder(sceneHolderImpl);
391 void SceneHolder::Reset()
395 // Any touch listeners should be told of the interruption.
396 Integration::TouchEvent event;
397 Integration::Point point;
398 point.SetState(PointState::INTERRUPTED);
399 event.AddPoint(point);
401 // First the touch event & related gesture events are queued
402 mScene.QueueEvent(event);
404 // Next the events are processed with a single call into Core
405 mAdaptor->ProcessCoreEvents();
408 void SceneHolder::InitializeDpi()
410 unsigned int dpiHorizontal, dpiVertical;
411 dpiHorizontal = dpiVertical = 0;
413 mSurface->GetDpi(dpiHorizontal, dpiVertical);
414 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
416 mDpi.SetX(dpiHorizontal);
417 mDpi.SetY(dpiVertical);
420 } // namespace Adaptor
422 } // namespace Internal