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 Dali::Layer SceneHolder::GetOverlayLayer()
127 return mScene ? mScene.GetOverlayLayer() : Dali::Layer();
130 uint32_t SceneHolder::GetId() const
135 std::string SceneHolder::GetName() const
140 bool SceneHolder::IsVisible() const
145 Dali::Integration::Scene SceneHolder::GetScene()
150 Uint16Pair SceneHolder::GetDpi() const
155 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
157 mSurface.reset(surface);
159 mScene.SurfaceReplaced();
161 PositionSize surfacePositionSize = surface->GetPositionSize();
163 SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
167 mSurface->SetAdaptor(*mAdaptor);
168 mSurface->SetScene(mScene);
170 // Recreate the render target
171 CreateRenderTarget();
173 OnSurfaceSet(surface);
176 void SceneHolder::SurfaceResized(float width, float height)
178 if(DALI_LIKELY(mScene))
180 mScene.SurfaceResized(width, height);
183 mSurface->SetFullSwapNextFrame();
185 // Recreate the render target
186 CreateRenderTarget();
189 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
191 return mSurface.get();
194 void SceneHolder::SetBackgroundColor(const Vector4& color)
198 mScene.SetBackgroundColor(color);
200 mSurface->SetFullSwapNextFrame();
204 Vector4 SceneHolder::GetBackgroundColor() const
206 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
209 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
211 // Avoid doing this more than once
217 DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
219 mAdaptorStarted = true;
222 PositionSize surfacePositionSize = mSurface->GetPositionSize();
223 int windowOrientation = mSurface->GetSurfaceOrientation();
224 int screenOrientation = mSurface->GetScreenOrientation();
226 mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)), windowOrientation, screenOrientation);
228 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
229 mAdaptor = &adaptorImpl;
231 // Create an observer for the adaptor lifecycle
232 mAdaptor->AddObserver(*mLifeCycleObserver);
236 mSurface->SetAdaptor(*mAdaptor);
237 mSurface->SetScene(mScene);
239 // Create the render target
240 CreateRenderTarget();
242 OnAdaptorSet(adaptor);
245 void SceneHolder::CreateRenderTarget()
247 Graphics::RenderTargetCreateInfo rtInfo{};
249 .SetSurface(mSurface.get())
250 .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
251 .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
253 mScene.SetSurfaceRenderTarget(rtInfo);
256 void SceneHolder::Pause()
263 void SceneHolder::Resume()
270 void SceneHolder::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
272 if(DALI_LIKELY(mScene))
274 mScene.SurfaceRotated(width, height, windowOrientation, screenOrientation);
278 void SceneHolder::SetRotationCompletedAcknowledgement()
280 if(DALI_LIKELY(mScene))
282 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::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
358 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
360 DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
363 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
365 mScene.AddFramePresentedCallback(std::move(callback), frameId);
367 DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
370 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
372 SceneHolder* sceneHolderImpl = nullptr;
374 if(Internal::Adaptor::Adaptor::IsAvailable())
376 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
377 sceneHolderImpl = adaptor.GetWindow(actor);
380 return Dali::Integration::SceneHolder(sceneHolderImpl);
383 void SceneHolder::Reset()
387 // Any touch listeners should be told of the interruption.
388 Integration::TouchEvent event;
389 Integration::Point point;
390 point.SetState(PointState::INTERRUPTED);
391 event.AddPoint(point);
393 // First the touch event & related gesture events are queued
394 mScene.QueueEvent(event);
396 // Next the events are processed with a single call into Core
397 mAdaptor->ProcessCoreEvents();
400 void SceneHolder::InitializeDpi()
402 unsigned int dpiHorizontal, dpiVertical;
403 dpiHorizontal = dpiVertical = 0;
405 mSurface->GetDpi(dpiHorizontal, dpiVertical);
406 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
408 mDpi.SetX(dpiHorizontal);
409 mDpi.SetY(dpiVertical);
412 } // namespace Adaptor
414 } // namespace Internal