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>
30 #include <dali/public-api/render-tasks/render-task-list.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 // The scene graph object should be removed first.
92 mScene.RemoveSceneObject();
97 mAdaptor->RemoveObserver(*mLifeCycleObserver.get());
98 mAdaptor->RemoveWindow(this);
100 // The event queue is flushed and we wait for the completion of the surface removal
101 mAdaptor->DeleteSurface(*mSurface.get());
108 // We should remove the surface from the Core last
113 void SceneHolder::Add(Dali::Actor actor)
121 void SceneHolder::Remove(Dali::Actor actor)
125 mScene.Remove(actor);
129 Dali::Layer SceneHolder::GetRootLayer() const
131 return mScene ? mScene.GetRootLayer() : Dali::Layer();
134 Dali::Layer SceneHolder::GetOverlayLayer()
136 return mScene ? mScene.GetOverlayLayer() : Dali::Layer();
139 uint32_t SceneHolder::GetId() const
144 std::string SceneHolder::GetName() const
149 bool SceneHolder::IsVisible() const
154 Dali::Integration::Scene SceneHolder::GetScene()
159 Uint16Pair SceneHolder::GetDpi() const
164 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
166 mSurface.reset(surface);
168 mScene.SurfaceReplaced();
170 PositionSize surfacePositionSize = surface->GetPositionSize();
172 SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
176 mSurface->SetAdaptor(*mAdaptor);
177 mSurface->SetScene(mScene);
179 // Recreate the render target
180 CreateRenderTarget();
182 OnSurfaceSet(surface);
185 void SceneHolder::SurfaceResized(float width, float height)
187 mScene.SurfaceResized(width, height);
189 mSurface->SetFullSwapNextFrame();
191 // Recreate the render target
192 CreateRenderTarget();
195 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
197 return mSurface.get();
200 void SceneHolder::SetBackgroundColor(const Vector4& color)
204 mScene.SetBackgroundColor(color);
206 mSurface->SetFullSwapNextFrame();
210 Vector4 SceneHolder::GetBackgroundColor() const
212 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
215 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
217 // Avoid doing this more than once
223 DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
225 mAdaptorStarted = true;
228 PositionSize surfacePositionSize = mSurface->GetPositionSize();
229 int windowOrientation = mSurface->GetSurfaceOrientation();
230 int screenOrientation = mSurface->GetScreenOrientation();
232 mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)), windowOrientation, screenOrientation);
234 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
235 mAdaptor = &adaptorImpl;
237 // Create an observer for the adaptor lifecycle
238 mAdaptor->AddObserver(*mLifeCycleObserver);
242 mSurface->SetAdaptor(*mAdaptor);
243 mSurface->SetScene(mScene);
245 // Create the render target
246 CreateRenderTarget();
248 OnAdaptorSet(adaptor);
251 void SceneHolder::CreateRenderTarget()
253 Graphics::RenderTargetCreateInfo rtInfo{};
255 .SetSurface(mSurface.get())
256 .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
257 .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
259 mScene.SetSurfaceRenderTarget(rtInfo);
262 void SceneHolder::Pause()
269 void SceneHolder::Resume()
276 void SceneHolder::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
278 mScene.SurfaceRotated(width, height, windowOrientation, screenOrientation);
281 void SceneHolder::SetRotationCompletedAcknowledgement()
283 mScene.SetRotationCompletedAcknowledgement();
286 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
290 timeStamp = TimeService::GetMilliSeconds();
293 Vector2 convertedPosition = RecalculatePosition(point.GetScreenPosition());
294 point.SetScreenPosition(convertedPosition);
296 Integration::TouchEvent touchEvent;
297 Integration::HoverEvent hoverEvent;
298 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
299 if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
301 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);
303 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
304 // Keep the handle alive until the core events are processed.
305 Dali::BaseHandle sceneHolder(this);
307 // First the touch and/or hover event & related gesture events are queued
308 if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
310 mScene.QueueEvent(touchEvent);
313 if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
315 mScene.QueueEvent(hoverEvent);
318 // Next the events are processed with a single call into Core
319 mAdaptor->ProcessCoreEvents();
323 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
325 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
326 // Keep the handle alive until the core events are processed.
327 Dali::BaseHandle sceneHolder(this);
329 Vector2 convertedPosition = RecalculatePosition(wheelEvent.point);
330 wheelEvent.point = convertedPosition;
332 mScene.QueueEvent(wheelEvent);
333 mAdaptor->ProcessCoreEvents();
336 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
338 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
341 if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
343 GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
347 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
348 // Keep the handle alive until the core events are processed.
349 Dali::BaseHandle sceneHolder(this);
351 // Create send KeyEvent to Core.
352 mScene.QueueEvent(keyEvent);
353 mAdaptor->ProcessCoreEvents();
356 void SceneHolder::FeedHoverEvent(Dali::Integration::Point& point)
358 Integration::HoverEvent hoverEvent;
360 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
361 // Keep the handle alive until the core events are processed.
362 Dali::BaseHandle sceneHolder(this);
364 // Create send HoverEvent to Core.
365 hoverEvent.time = TimeService::GetMilliSeconds();
366 hoverEvent.AddPoint(point);
368 mScene.QueueEvent(hoverEvent);
369 mAdaptor->ProcessCoreEvents();
372 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
374 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
376 DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
379 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
381 mScene.AddFramePresentedCallback(std::move(callback), frameId);
383 DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
386 Dali::RenderTaskList SceneHolder::GetRenderTaskList() const
388 return mScene.GetRenderTaskList();
391 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
393 SceneHolder* sceneHolderImpl = nullptr;
395 if(Internal::Adaptor::Adaptor::IsAvailable())
397 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
398 sceneHolderImpl = adaptor.GetWindow(actor);
401 return Dali::Integration::SceneHolder(sceneHolderImpl);
404 void SceneHolder::Reset()
408 // Any touch listeners should be told of the interruption.
409 Integration::TouchEvent event;
410 Integration::Point point;
411 point.SetState(PointState::INTERRUPTED);
412 event.AddPoint(point);
414 // First the touch event & related gesture events are queued
415 mScene.QueueEvent(event);
417 // Next the events are processed with a single call into Core
418 mAdaptor->ProcessCoreEvents();
421 void SceneHolder::InitializeDpi()
423 unsigned int dpiHorizontal, dpiVertical;
424 dpiHorizontal = dpiVertical = 0;
426 mSurface->GetDpi(dpiHorizontal, dpiVertical);
427 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
429 mDpi.SetX(dpiHorizontal);
430 mDpi.SetY(dpiVertical);
433 } // namespace Adaptor
435 } // namespace Internal