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 uint32_t SceneHolder::GetId() const
130 std::string SceneHolder::GetName() const
135 bool SceneHolder::IsVisible() const
140 Dali::Integration::Scene SceneHolder::GetScene()
145 Uint16Pair SceneHolder::GetDpi() const
150 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
152 mSurface.reset(surface);
154 mScene.SurfaceReplaced();
156 PositionSize surfacePositionSize = surface->GetPositionSize();
158 SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
162 mSurface->SetAdaptor(*mAdaptor);
163 mSurface->SetScene(mScene);
165 // Recreate the render target
166 CreateRenderTarget();
168 OnSurfaceSet(surface);
171 void SceneHolder::SurfaceResized(float width, float height)
173 mScene.SurfaceResized(width, height);
175 mSurface->SetFullSwapNextFrame();
177 // Recreate the render target
178 CreateRenderTarget();
181 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
183 return mSurface.get();
186 void SceneHolder::SetBackgroundColor(const Vector4& color)
190 mScene.SetBackgroundColor(color);
192 mSurface->SetFullSwapNextFrame();
196 Vector4 SceneHolder::GetBackgroundColor() const
198 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
201 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
203 // Avoid doing this more than once
209 DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
211 mAdaptorStarted = true;
214 PositionSize surfacePositionSize = mSurface->GetPositionSize();
215 int windowOrientation = mSurface->GetSurfaceOrientation();
216 int screenOrientation = mSurface->GetScreenOrientation();
218 mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)), windowOrientation, screenOrientation);
220 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
221 mAdaptor = &adaptorImpl;
223 // Create an observer for the adaptor lifecycle
224 mAdaptor->AddObserver(*mLifeCycleObserver);
228 mSurface->SetAdaptor(*mAdaptor);
229 mSurface->SetScene(mScene);
231 // Create the render target
232 CreateRenderTarget();
234 OnAdaptorSet(adaptor);
237 void SceneHolder::CreateRenderTarget()
239 Graphics::RenderTargetCreateInfo rtInfo{};
241 .SetSurface(mSurface.get())
242 .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
243 .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
245 mScene.SetSurfaceRenderTarget(rtInfo);
248 void SceneHolder::Pause()
255 void SceneHolder::Resume()
262 void SceneHolder::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
264 mScene.SurfaceRotated(width, height, windowOrientation, screenOrientation);
267 void SceneHolder::SetRotationCompletedAcknowledgement()
269 mScene.SetRotationCompletedAcknowledgement();
272 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
276 timeStamp = TimeService::GetMilliSeconds();
279 Vector2 convertedPosition = RecalculatePosition(point.GetScreenPosition());
280 point.SetScreenPosition(convertedPosition);
282 Integration::TouchEvent touchEvent;
283 Integration::HoverEvent hoverEvent;
284 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
285 if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
287 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);
289 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
290 // Keep the handle alive until the core events are processed.
291 Dali::BaseHandle sceneHolder(this);
293 // First the touch and/or hover event & related gesture events are queued
294 if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
296 mScene.QueueEvent(touchEvent);
299 if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
301 mScene.QueueEvent(hoverEvent);
304 // Next the events are processed with a single call into Core
305 mAdaptor->ProcessCoreEvents();
309 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
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 Vector2 convertedPosition = RecalculatePosition(wheelEvent.point);
316 wheelEvent.point = convertedPosition;
318 mScene.QueueEvent(wheelEvent);
319 mAdaptor->ProcessCoreEvents();
322 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
324 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
327 if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
329 GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
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 // Create send KeyEvent to Core.
338 mScene.QueueEvent(keyEvent);
339 mAdaptor->ProcessCoreEvents();
342 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
344 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
346 DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
349 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
351 mScene.AddFramePresentedCallback(std::move(callback), frameId);
353 DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
356 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
358 SceneHolder* sceneHolderImpl = nullptr;
360 if(Internal::Adaptor::Adaptor::IsAvailable())
362 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
363 sceneHolderImpl = adaptor.GetWindow(actor);
366 return Dali::Integration::SceneHolder(sceneHolderImpl);
369 void SceneHolder::Reset()
373 // Any touch listeners should be told of the interruption.
374 Integration::TouchEvent event;
375 Integration::Point point;
376 point.SetState(PointState::INTERRUPTED);
377 event.AddPoint(point);
379 // First the touch event & related gesture events are queued
380 mScene.QueueEvent(event);
382 // Next the events are processed with a single call into Core
383 mAdaptor->ProcessCoreEvents();
386 void SceneHolder::InitializeDpi()
388 unsigned int dpiHorizontal, dpiVertical;
389 dpiHorizontal = dpiVertical = 0;
391 mSurface->GetDpi(dpiHorizontal, dpiVertical);
392 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
394 mDpi.SetX(dpiHorizontal);
395 mDpi.SetY(dpiVertical);
398 } // namespace Adaptor
400 } // namespace Internal