2 * Copyright (c) 2020 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 SurfaceResized(false);
160 mSurface->SetAdaptor(*mAdaptor);
161 mSurface->SetScene(mScene);
163 OnSurfaceSet(surface);
166 void SceneHolder::SurfaceResized(bool forceUpdate)
168 PositionSize surfacePositionSize = mSurface->GetPositionSize();
169 int orientation = mSurface->GetOrientation();
170 mScene.SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height), orientation, forceUpdate);
172 mSurface->SetFullSwapNextFrame();
175 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
177 return mSurface.get();
180 void SceneHolder::SetBackgroundColor(const Vector4& color)
184 mScene.SetBackgroundColor(color);
186 mSurface->SetFullSwapNextFrame();
190 Vector4 SceneHolder::GetBackgroundColor() const
192 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
195 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
197 // Avoid doing this more than once
203 DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
205 mAdaptorStarted = true;
208 PositionSize surfacePositionSize = mSurface->GetPositionSize();
209 int orientation = mSurface->GetOrientation();
210 mScene = Dali::Integration::Scene::New( Size(static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height )), orientation );
212 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
213 mAdaptor = &adaptorImpl;
215 // Create an observer for the adaptor lifecycle
216 mAdaptor->AddObserver(*mLifeCycleObserver);
220 mSurface->SetAdaptor(*mAdaptor);
221 mSurface->SetScene(mScene);
223 OnAdaptorSet(adaptor);
226 void SceneHolder::Pause()
233 void SceneHolder::Resume()
240 void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
244 timeStamp = TimeService::GetMilliSeconds();
247 RecalculateTouchPosition(point);
249 Integration::TouchEvent touchEvent;
250 Integration::HoverEvent hoverEvent;
251 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
252 if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
254 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);
256 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
257 // Keep the handle alive until the core events are processed.
258 Dali::BaseHandle sceneHolder(this);
260 // First the touch and/or hover event & related gesture events are queued
261 if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
263 mScene.QueueEvent(touchEvent);
266 if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
268 mScene.QueueEvent(hoverEvent);
271 // Next the events are processed with a single call into Core
272 mAdaptor->ProcessCoreEvents();
276 void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
278 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
279 // Keep the handle alive until the core events are processed.
280 Dali::BaseHandle sceneHolder(this);
282 mScene.QueueEvent(wheelEvent);
283 mAdaptor->ProcessCoreEvents();
286 void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
288 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
291 if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
293 GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
297 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
298 // Keep the handle alive until the core events are processed.
299 Dali::BaseHandle sceneHolder(this);
301 // Create send KeyEvent to Core.
302 mScene.QueueEvent(keyEvent);
303 mAdaptor->ProcessCoreEvents();
306 void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
308 mScene.AddFrameRenderedCallback(std::move(callback), frameId);
310 DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
313 void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
315 mScene.AddFramePresentedCallback(std::move(callback), frameId);
317 DALI_LOG_INFO(gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
320 Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
322 SceneHolder* sceneHolderImpl = nullptr;
324 if(Internal::Adaptor::Adaptor::IsAvailable())
326 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
327 sceneHolderImpl = adaptor.GetWindow(actor);
330 return Dali::Integration::SceneHolder(sceneHolderImpl);
333 void SceneHolder::Reset()
337 // Any touch listeners should be told of the interruption.
338 Integration::TouchEvent event;
339 Integration::Point point;
340 point.SetState(PointState::INTERRUPTED);
341 event.AddPoint(point);
343 // First the touch event & related gesture events are queued
344 mScene.QueueEvent(event);
346 // Next the events are processed with a single call into Core
347 mAdaptor->ProcessCoreEvents();
350 void SceneHolder::InitializeDpi()
352 unsigned int dpiHorizontal, dpiVertical;
353 dpiHorizontal = dpiVertical = 0;
355 mSurface->GetDpi(dpiHorizontal, dpiVertical);
356 mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
358 mDpi.SetX(dpiHorizontal);
359 mDpi.SetY(dpiVertical);
362 } // namespace Adaptor
364 } // namespace Internal