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>
23 #include <dali/public-api/actors/actor.h>
24 #include <dali/public-api/actors/layer.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/events/key-event-integ.h>
27 #include <dali/integration-api/events/touch-event-integ.h>
28 #include <dali/integration-api/events/hover-event-integ.h>
29 #include <dali/integration-api/events/wheel-event-integ.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>
50 #if defined(DEBUG_ENABLED)
51 Debug::Filter* gSceneHolderLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_SCENE_HOLDER" );
54 // Copied from x server
55 static uint32_t GetCurrentMilliSeconds(void)
60 static clockid_t clockid;
64 #ifdef CLOCK_MONOTONIC_COARSE
65 if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
66 (tp.tv_nsec / 1000) <= 1000 && clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
68 clockid = CLOCK_MONOTONIC_COARSE;
72 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
74 clockid = CLOCK_MONOTONIC;
81 if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
83 return static_cast<uint32_t>( (tp.tv_sec * 1000 ) + (tp.tv_nsec / 1000000L) );
86 gettimeofday(&tv, NULL);
87 return static_cast<uint32_t>( (tv.tv_sec * 1000 ) + (tv.tv_usec / 1000) );
90 } // unnamed namespace
92 uint32_t SceneHolder::mSceneHolderCounter = 0;
94 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
98 SceneHolderLifeCycleObserver(Adaptor*& adaptor)
103 private: // Adaptor::LifeCycleObserver interface
105 void OnStart() override {};
106 void OnPause() override {};
107 void OnResume() override {};
108 void OnStop() override {};
109 void OnDestroy() override
119 SceneHolder::SceneHolder()
120 : mLifeCycleObserver( new SceneHolderLifeCycleObserver( mAdaptor ) ),
121 mId( mSceneHolderCounter++ ),
124 mIsBeingDeleted( false ),
125 mAdaptorStarted( false ),
130 SceneHolder::~SceneHolder()
134 mAdaptor->RemoveObserver( *mLifeCycleObserver.get() );
135 mAdaptor->RemoveWindow( this );
137 mAdaptor->DeleteSurface( *mSurface.get() );
148 void SceneHolder::Add( Dali::Actor actor )
156 void SceneHolder::Remove( Dali::Actor actor )
160 mScene.Remove( actor );
164 Dali::Layer SceneHolder::GetRootLayer() const
166 return mScene ? mScene.GetRootLayer() : Dali::Layer();
169 uint32_t SceneHolder::GetId() const
174 std::string SceneHolder::GetName() const
179 bool SceneHolder::IsVisible() const
184 Dali::Integration::Scene SceneHolder::GetScene()
189 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
191 mSurface.reset( surface );
193 mScene.SurfaceReplaced();
197 unsigned int dpiHorizontal, dpiVertical;
198 dpiHorizontal = dpiVertical = 0;
200 mSurface->GetDpi( dpiHorizontal, dpiVertical );
201 mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
203 mSurface->SetAdaptor( *mAdaptor );
204 mSurface->SetScene( mScene );
206 OnSurfaceSet( surface );
209 void SceneHolder::SurfaceResized()
211 PositionSize surfacePositionSize = mSurface->GetPositionSize();
212 mScene.SurfaceResized( static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height ) );
214 GraphicsInterface& graphics = mAdaptor->GetGraphicsInterface();
215 EglGraphics* eglGraphics = static_cast<EglGraphics*>(&graphics);
218 eglGraphics->SetFullSwapNextFrame();
222 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
224 return mSurface.get();
227 void SceneHolder::SetBackgroundColor( const Vector4& color )
231 mScene.SetBackgroundColor( color );
233 GraphicsInterface& graphics = mAdaptor->GetGraphicsInterface();
234 EglGraphics* eglGraphics = static_cast<EglGraphics*>(&graphics);
237 eglGraphics->SetFullSwapNextFrame();
242 Vector4 SceneHolder::GetBackgroundColor() const
244 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
247 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
249 // Avoid doing this more than once
250 if( mAdaptorStarted )
255 mAdaptorStarted = true;
258 PositionSize surfacePositionSize = mSurface->GetPositionSize();
259 mScene = Dali::Integration::Scene::New( Size(static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height )) );
261 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation( adaptor );
262 mAdaptor = &adaptorImpl;
264 // Create an observer for the adaptor lifecycle
265 mAdaptor->AddObserver( *mLifeCycleObserver );
269 unsigned int dpiHorizontal, dpiVertical;
270 dpiHorizontal = dpiVertical = 0;
272 mSurface->GetDpi( dpiHorizontal, dpiVertical );
273 mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
275 mSurface->SetAdaptor( *mAdaptor );
276 mSurface->SetScene( mScene );
279 OnAdaptorSet( adaptor );
282 void SceneHolder::Pause()
289 void SceneHolder::Resume()
296 void SceneHolder::FeedTouchPoint( Dali::Integration::Point& point, int timeStamp )
300 timeStamp = GetCurrentMilliSeconds();
303 RecalculateTouchPosition( point );
305 Integration::TouchEvent touchEvent;
306 Integration::HoverEvent hoverEvent;
307 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
308 if( type != Integration::TouchEventCombiner::DispatchNone )
310 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 );
312 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
313 // Keep the handle alive until the core events are processed.
314 Dali::BaseHandle sceneHolder( this );
316 // First the touch and/or hover event & related gesture events are queued
317 if( type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth )
319 mScene.QueueEvent( touchEvent );
322 if( type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth )
324 mScene.QueueEvent( hoverEvent );
327 // Next the events are processed with a single call into Core
328 mAdaptor->ProcessCoreEvents();
332 void SceneHolder::FeedWheelEvent( Dali::Integration::WheelEvent& wheelEvent )
334 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
335 // Keep the handle alive until the core events are processed.
336 Dali::BaseHandle sceneHolder( this );
338 mScene.QueueEvent( wheelEvent );
339 mAdaptor->ProcessCoreEvents();
342 void SceneHolder::FeedKeyEvent( Dali::Integration::KeyEvent& keyEvent )
344 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
345 if( physicalKeyboard )
347 if( ! KeyLookup::IsDeviceButton( keyEvent.keyName.c_str() ) )
349 GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
353 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
354 // Keep the handle alive until the core events are processed.
355 Dali::BaseHandle sceneHolder( this );
357 // Create send KeyEvent to Core.
358 mScene.QueueEvent( keyEvent );
359 mAdaptor->ProcessCoreEvents();
362 void SceneHolder::AddFrameRenderedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
364 mScene.AddFrameRenderedCallback( std::move( callback ), frameId );
366 DALI_LOG_INFO( gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId );
369 void SceneHolder::AddFramePresentedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
371 mScene.AddFramePresentedCallback( std::move( callback ), frameId );
373 DALI_LOG_INFO( gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId );
376 Dali::Integration::SceneHolder SceneHolder::Get( Dali::Actor actor )
378 SceneHolder* sceneHolderImpl = nullptr;
380 if ( Internal::Adaptor::Adaptor::IsAvailable() )
382 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation( Internal::Adaptor::Adaptor::Get() );
383 sceneHolderImpl = adaptor.GetWindow( actor );
386 return Dali::Integration::SceneHolder( sceneHolderImpl );
389 void SceneHolder::Reset()
393 // Any touch listeners should be told of the interruption.
394 Integration::TouchEvent event;
395 Integration::Point point;
396 point.SetState( PointState::INTERRUPTED );
397 event.AddPoint( point );
399 // First the touch event & related gesture events are queued
400 mScene.QueueEvent( event );
402 // Next the events are processed with a single call into Core
403 mAdaptor->ProcessCoreEvents();