2 * Copyright (c) 2021 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();
160 mSurface->SetAdaptor(*mAdaptor);
161 mSurface->SetScene(mScene);
163 // Recreate the render target
164 CreateRenderTarget();
166 OnSurfaceSet(surface);
169 void SceneHolder::SurfaceResized()
171 PositionSize surfacePositionSize = mSurface->GetPositionSize();
172 mScene.SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
174 mSurface->SetFullSwapNextFrame();
176 // Recreate the render target
177 CreateRenderTarget();
180 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
182 return mSurface.get();
185 void SceneHolder::SetBackgroundColor(const Vector4& color)
189 mScene.SetBackgroundColor(color);
191 mSurface->SetFullSwapNextFrame();
195 Vector4 SceneHolder::GetBackgroundColor() const
197 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
200 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
202 // Avoid doing this more than once
208 DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
210 mAdaptorStarted = true;
213 PositionSize surfacePositionSize = mSurface->GetPositionSize();
214 int orientation = mSurface->GetOrientation();
215 mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)), orientation);
217 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
218 mAdaptor = &adaptorImpl;
220 // Create an observer for the adaptor lifecycle
221 mAdaptor->AddObserver(*mLifeCycleObserver);
225 mSurface->SetAdaptor(*mAdaptor);
226 mSurface->SetScene(mScene);
228 // Create the render target
229 CreateRenderTarget();
231 OnAdaptorSet(adaptor);
234 void SceneHolder::CreateRenderTarget()
236 Graphics::RenderTargetCreateInfo rtInfo{};
238 .SetSurface(mSurface.get())
239 .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
240 .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
241 mRenderTarget = mAdaptor->GetGraphicsInterface().GetController().CreateRenderTarget(rtInfo, std::move(mRenderTarget));
243 mScene.SetSurfaceRenderTarget(mRenderTarget.get());
246 void SceneHolder::Pause()
253 void SceneHolder::Resume()
260 void SceneHolder::SurfaceRotated(float width, float height, int orientation)
262 mScene.SurfaceRotated(width, height, orientation);
265 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
269 timeStamp = TimeService::GetMilliSeconds();
272 RecalculateTouchPosition(point);
274 Integration::TouchEvent touchEvent;
275 Integration::HoverEvent hoverEvent;
276 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
277 if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
279 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);
281 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
282 // Keep the handle alive until the core events are processed.
283 Dali::BaseHandle sceneHolder(this);
285 // First the touch and/or hover event & related gesture events are queued
286 if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
288 mScene.QueueEvent(touchEvent);
291 if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
293 mScene.QueueEvent(hoverEvent);
296 // Next the events are processed with a single call into Core
297 mAdaptor->ProcessCoreEvents();
301 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
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 mScene.QueueEvent(wheelEvent);
308 mAdaptor->ProcessCoreEvents();
311 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
313 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
316 if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
318 GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
322 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
323 // Keep the handle alive until the core events are processed.
324 Dali::BaseHandle sceneHolder(this);
326 // Create send KeyEvent to Core.
327 mScene.QueueEvent(keyEvent);
328 mAdaptor->ProcessCoreEvents();
331 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
333 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
335 DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
338 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
340 mScene.AddFramePresentedCallback(std::move(callback), frameId);
342 DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
345 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
347 SceneHolder* sceneHolderImpl = nullptr;
349 if(Internal::Adaptor::Adaptor::IsAvailable())
351 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
352 sceneHolderImpl = adaptor.GetWindow(actor);
355 return Dali::Integration::SceneHolder(sceneHolderImpl);
358 void SceneHolder::Reset()
362 // Any touch listeners should be told of the interruption.
363 Integration::TouchEvent event;
364 Integration::Point point;
365 point.SetState(PointState::INTERRUPTED);
366 event.AddPoint(point);
368 // First the touch event & related gesture events are queued
369 mScene.QueueEvent(event);
371 // Next the events are processed with a single call into Core
372 mAdaptor->ProcessCoreEvents();
375 void SceneHolder::InitializeDpi()
377 unsigned int dpiHorizontal, dpiVertical;
378 dpiHorizontal = dpiVertical = 0;
380 mSurface->GetDpi(dpiHorizontal, dpiVertical);
381 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
383 mDpi.SetX(dpiHorizontal);
384 mDpi.SetY(dpiVertical);
387 } // namespace Adaptor
389 } // namespace Internal