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);
269 void SceneHolder::CreateRenderTarget()
271 Graphics::RenderTargetCreateInfo rtInfo{};
273 .SetSurface(mSurface.get())
274 .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
275 .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
277 mScene.SetSurfaceRenderTarget(rtInfo);
280 void SceneHolder::Pause()
287 void SceneHolder::Resume()
294 void SceneHolder::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
296 mScene.SurfaceRotated(width, height, windowOrientation, screenOrientation);
299 void SceneHolder::SetRotationCompletedAcknowledgement()
301 mScene.SetRotationCompletedAcknowledgement();
304 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
306 if(DALI_UNLIKELY(!mAdaptorStarted))
308 DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
314 timeStamp = TimeService::GetMilliSeconds();
317 Vector2 convertedPosition = RecalculatePosition(point.GetScreenPosition());
318 point.SetScreenPosition(convertedPosition);
320 Integration::TouchEvent touchEvent;
321 Integration::HoverEvent hoverEvent;
322 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
323 if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
325 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);
327 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
328 // Keep the handle alive until the core events are processed.
329 Dali::BaseHandle sceneHolder(this);
331 // First the touch and/or hover event & related gesture events are queued
332 if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
334 mLastTouchEvent = Dali::Integration::NewTouchEvent(timeStamp, point);
335 mScene.QueueEvent(touchEvent);
338 if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
340 mLastHoverEvent = Dali::Integration::NewHoverEvent(timeStamp, point);
341 mScene.QueueEvent(hoverEvent);
344 // Next the events are processed with a single call into Core
345 mAdaptor->ProcessCoreEvents();
349 const Dali::TouchEvent& SceneHolder::GetLastTouchEvent() const
351 return mLastTouchEvent;
354 const Dali::HoverEvent& SceneHolder::GetLastHoverEvent() const
356 return mLastHoverEvent;
359 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
361 if(DALI_UNLIKELY(!mAdaptorStarted))
363 DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
367 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
368 // Keep the handle alive until the core events are processed.
369 Dali::BaseHandle sceneHolder(this);
371 Vector2 convertedPosition = RecalculatePosition(wheelEvent.point);
372 wheelEvent.point = convertedPosition;
374 mScene.QueueEvent(wheelEvent);
375 mAdaptor->ProcessCoreEvents();
378 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
380 if(DALI_UNLIKELY(!mAdaptorStarted))
382 DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
386 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
389 if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
391 GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
395 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
396 // Keep the handle alive until the core events are processed.
397 Dali::BaseHandle sceneHolder(this);
399 // Create send KeyEvent to Core.
400 mScene.QueueEvent(keyEvent);
401 mAdaptor->ProcessCoreEvents();
404 void SceneHolder::FeedHoverEvent(Dali::Integration::Point& point)
406 if(DALI_UNLIKELY(!mAdaptorStarted))
408 DALI_LOG_ERROR("Adaptor is stopped, or not be started yet. Ignore this feed.\n");
412 Integration::HoverEvent hoverEvent;
414 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
415 // Keep the handle alive until the core events are processed.
416 Dali::BaseHandle sceneHolder(this);
418 // Create send HoverEvent to Core.
419 hoverEvent.time = TimeService::GetMilliSeconds();
420 hoverEvent.AddPoint(point);
422 mScene.QueueEvent(hoverEvent);
423 mAdaptor->ProcessCoreEvents();
426 void SceneHolder::SetGeometryHittestEnabled(bool enabled)
428 mScene.SetGeometryHittestEnabled(enabled);
431 bool SceneHolder::IsGeometryHittestEnabled()
433 return mScene.IsGeometryHittestEnabled();
436 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
438 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
440 DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
443 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
445 mScene.AddFramePresentedCallback(std::move(callback), frameId);
447 DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
450 Dali::RenderTaskList SceneHolder::GetRenderTaskList() const
452 return mScene.GetRenderTaskList();
455 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
457 SceneHolder* sceneHolderImpl = nullptr;
459 if(Internal::Adaptor::Adaptor::IsAvailable())
461 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
462 sceneHolderImpl = adaptor.GetWindow(actor);
465 return Dali::Integration::SceneHolder(sceneHolderImpl);
468 void SceneHolder::Reset()
470 DALI_ASSERT_ALWAYS(mAdaptorStarted && "Adaptor is stopped, or not be started yet!");
474 // Any touch listeners should be told of the interruption.
475 Integration::TouchEvent event;
476 Integration::Point point;
477 point.SetState(PointState::INTERRUPTED);
478 event.AddPoint(point);
480 // First the touch event & related gesture events are queued
481 mScene.QueueEvent(event);
483 // Next the events are processed with a single call into Core
484 mAdaptor->ProcessCoreEvents();
487 void SceneHolder::InitializeDpi()
489 unsigned int dpiHorizontal, dpiVertical;
490 dpiHorizontal = dpiVertical = 0;
492 mSurface->GetDpi(dpiHorizontal, dpiVertical);
493 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
495 mDpi.SetX(dpiHorizontal);
496 mDpi.SetY(dpiVertical);
499 } // namespace Adaptor
501 } // namespace Internal