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/touch-integ.h>
27 #include <dali/integration-api/events/wheel-event-integ.h>
28 #include <dali/public-api/actors/actor.h>
29 #include <dali/public-api/actors/layer.h>
30 #include <dali/public-api/common/dali-common.h>
31 #include <dali/public-api/render-tasks/render-task-list.h>
34 #include <dali/internal/adaptor/common/adaptor-impl.h>
35 #include <dali/internal/adaptor/common/lifecycle-observer.h>
36 #include <dali/internal/graphics/gles/egl-graphics.h>
37 #include <dali/internal/input/common/key-impl.h>
38 #include <dali/internal/input/common/physical-keyboard-impl.h>
39 #include <dali/internal/system/common/time-service.h>
49 #if defined(DEBUG_ENABLED)
50 Debug::Filter* gSceneHolderLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_SCENE_HOLDER");
52 } // unnamed namespace
54 uint32_t SceneHolder::mSceneHolderCounter = 0;
56 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
59 SceneHolderLifeCycleObserver(Adaptor*& adaptor, bool& adaptorStarted)
61 mAdaptorStarted(adaptorStarted)
65 private: // Adaptor::LifeCycleObserver interface
66 void OnStart() override
68 mAdaptorStarted = true;
70 void OnPause() override{};
71 void OnResume() override{};
72 void OnStop() override
74 // Mark adaptor as stopped;
75 mAdaptorStarted = false;
77 void OnDestroy() override
84 bool& mAdaptorStarted;
87 SceneHolder::SceneHolder()
88 : mLifeCycleObserver(new SceneHolderLifeCycleObserver(mAdaptor, mAdaptorStarted)),
91 mId(mSceneHolderCounter++),
95 mAdaptorStarted(false),
100 SceneHolder::~SceneHolder()
104 // The scene graph object should be removed first.
105 mScene.RemoveSceneObject();
110 mAdaptor->RemoveObserver(*mLifeCycleObserver.get());
111 mAdaptor->RemoveWindow(this);
115 // The event queue is flushed and we wait for the completion of the surface removal
116 // Note : we don't need to delete surface when adaptor is stopped now.
117 mAdaptor->DeleteSurface(*mSurface.get());
125 // We should remove the surface from the Core last
130 void SceneHolder::Add(Dali::Actor actor)
138 void SceneHolder::Remove(Dali::Actor actor)
142 mScene.Remove(actor);
146 Dali::Layer SceneHolder::GetRootLayer() const
148 return mScene ? mScene.GetRootLayer() : Dali::Layer();
151 Dali::Layer SceneHolder::GetOverlayLayer()
153 return mScene ? mScene.GetOverlayLayer() : Dali::Layer();
156 uint32_t SceneHolder::GetId() const
161 std::string SceneHolder::GetName() const
166 bool SceneHolder::IsVisible() const
171 Dali::Integration::Scene SceneHolder::GetScene()
176 Uint16Pair SceneHolder::GetDpi() const
181 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
183 mSurface.reset(surface);
185 mScene.SurfaceReplaced();
187 PositionSize surfacePositionSize = surface->GetPositionSize();
189 SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
193 mSurface->SetAdaptor(*mAdaptor);
194 mSurface->SetScene(mScene);
196 // Recreate the render target
197 CreateRenderTarget();
199 OnSurfaceSet(surface);
202 void SceneHolder::SurfaceResized(float width, float height)
204 mScene.SurfaceResized(width, height);
206 mSurface->SetFullSwapNextFrame();
208 // Recreate the render target
209 CreateRenderTarget();
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 // We can assume that current adaptor is already started now.
243 mAdaptorStarted = true;
246 PositionSize surfacePositionSize = mSurface->GetPositionSize();
247 int windowOrientation = mSurface->GetSurfaceOrientation();
248 int screenOrientation = mSurface->GetScreenOrientation();
250 mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)), windowOrientation, screenOrientation);
252 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
253 mAdaptor = &adaptorImpl;
255 // Create an observer for the adaptor lifecycle
256 mAdaptor->AddObserver(*mLifeCycleObserver);
260 mSurface->SetAdaptor(*mAdaptor);
261 mSurface->SetScene(mScene);
263 // Create the render target
264 CreateRenderTarget();
266 OnAdaptorSet(adaptor);
268 // Scene is newly created. Let we increase resize counter
269 mAdaptor->IncreaseSurfaceResizeCounter();
272 void SceneHolder::CreateRenderTarget()
274 Graphics::RenderTargetCreateInfo rtInfo{};
276 .SetSurface(mSurface.get())
277 .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
278 .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
280 mScene.SetSurfaceRenderTarget(rtInfo);
283 void SceneHolder::Pause()
290 void SceneHolder::Resume()
297 void SceneHolder::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
299 mScene.SurfaceRotated(width, height, windowOrientation, screenOrientation);
302 void SceneHolder::SetRotationCompletedAcknowledgement()
304 mScene.SetRotationCompletedAcknowledgement();
307 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
309 if(DALI_UNLIKELY(!mAdaptorStarted))
311 DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
317 timeStamp = TimeService::GetMilliSeconds();
320 Vector2 convertedPosition = RecalculatePosition(point.GetScreenPosition());
321 point.SetScreenPosition(convertedPosition);
323 Integration::TouchEvent touchEvent;
324 Integration::HoverEvent hoverEvent;
325 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
326 if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
328 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);
330 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
331 // Keep the handle alive until the core events are processed.
332 Dali::BaseHandle sceneHolder(this);
334 // First the touch and/or hover event & related gesture events are queued
335 if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
337 mLastTouchEvent = Dali::Integration::NewTouchEvent(timeStamp, point);
338 mScene.QueueEvent(touchEvent);
341 if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
343 mLastHoverEvent = Dali::Integration::NewHoverEvent(timeStamp, point);
344 mScene.QueueEvent(hoverEvent);
347 // Next the events are processed with a single call into Core
348 mAdaptor->ProcessCoreEvents();
352 const Dali::TouchEvent& SceneHolder::GetLastTouchEvent() const
354 return mLastTouchEvent;
357 const Dali::HoverEvent& SceneHolder::GetLastHoverEvent() const
359 return mLastHoverEvent;
362 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
364 if(DALI_UNLIKELY(!mAdaptorStarted))
366 DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
370 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
371 // Keep the handle alive until the core events are processed.
372 Dali::BaseHandle sceneHolder(this);
374 Vector2 convertedPosition = RecalculatePosition(wheelEvent.point);
375 wheelEvent.point = convertedPosition;
377 mScene.QueueEvent(wheelEvent);
378 mAdaptor->ProcessCoreEvents();
381 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
383 if(DALI_UNLIKELY(!mAdaptorStarted))
385 DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
389 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
392 if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
394 GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
398 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
399 // Keep the handle alive until the core events are processed.
400 Dali::BaseHandle sceneHolder(this);
402 // Create send KeyEvent to Core.
403 mScene.QueueEvent(keyEvent);
404 mAdaptor->ProcessCoreEvents();
407 void SceneHolder::FeedHoverEvent(Dali::Integration::Point& point)
409 if(DALI_UNLIKELY(!mAdaptorStarted))
411 DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
415 Integration::HoverEvent hoverEvent;
417 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
418 // Keep the handle alive until the core events are processed.
419 Dali::BaseHandle sceneHolder(this);
421 // Create send HoverEvent to Core.
422 hoverEvent.time = TimeService::GetMilliSeconds();
423 hoverEvent.AddPoint(point);
425 mScene.QueueEvent(hoverEvent);
426 mAdaptor->ProcessCoreEvents();
429 void SceneHolder::SetGeometryHittestEnabled(bool enabled)
431 mScene.SetGeometryHittestEnabled(enabled);
434 bool SceneHolder::IsGeometryHittestEnabled()
436 return mScene.IsGeometryHittestEnabled();
439 int32_t SceneHolder::GetNativeId() const
441 return mScene.GetNativeId();
444 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
446 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
448 DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
451 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
453 mScene.AddFramePresentedCallback(std::move(callback), frameId);
455 DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
458 Dali::RenderTaskList SceneHolder::GetRenderTaskList() const
460 return mScene.GetRenderTaskList();
463 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
465 SceneHolder* sceneHolderImpl = nullptr;
467 if(Internal::Adaptor::Adaptor::IsAvailable())
469 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
470 sceneHolderImpl = adaptor.GetWindow(actor);
473 return Dali::Integration::SceneHolder(sceneHolderImpl);
476 void SceneHolder::Reset()
478 DALI_ASSERT_ALWAYS(mAdaptorStarted && "Adaptor is stopped, or not be started yet!");
482 // Any touch listeners should be told of the interruption.
483 Integration::TouchEvent event;
484 Integration::Point point;
485 point.SetState(PointState::INTERRUPTED);
486 event.AddPoint(point);
488 // First the touch event & related gesture events are queued
489 mScene.QueueEvent(event);
491 // Next the events are processed with a single call into Core
492 mAdaptor->ProcessCoreEvents();
495 void SceneHolder::InitializeDpi()
497 unsigned int dpiHorizontal, dpiVertical;
498 dpiHorizontal = dpiVertical = 0;
500 mSurface->GetDpi(dpiHorizontal, dpiVertical);
501 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
503 mDpi.SetX(dpiHorizontal);
504 mDpi.SetY(dpiVertical);
507 } // namespace Adaptor
509 } // namespace Internal