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/common/dali-common.h>
24 #include <dali/public-api/actors/actor.h>
25 #include <dali/public-api/actors/layer.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/integration-api/events/key-event-integ.h>
28 #include <dali/integration-api/events/touch-event-integ.h>
29 #include <dali/integration-api/events/hover-event-integ.h>
30 #include <dali/integration-api/events/wheel-event-integ.h>
33 #include <dali/internal/adaptor/common/adaptor-impl.h>
34 #include <dali/internal/adaptor/common/lifecycle-observer.h>
35 #include <dali/internal/graphics/gles/egl-graphics.h>
36 #include <dali/internal/input/common/key-impl.h>
37 #include <dali/internal/input/common/physical-keyboard-impl.h>
51 #if defined(DEBUG_ENABLED)
52 Debug::Filter* gSceneHolderLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_SCENE_HOLDER" );
55 // Copied from x server
56 static uint32_t GetCurrentMilliSeconds(void)
61 static clockid_t clockid;
65 #ifdef CLOCK_MONOTONIC_COARSE
66 if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
67 (tp.tv_nsec / 1000) <= 1000 && clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
69 clockid = CLOCK_MONOTONIC_COARSE;
73 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
75 clockid = CLOCK_MONOTONIC;
82 if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
84 return static_cast<uint32_t>( (tp.tv_sec * 1000 ) + (tp.tv_nsec / 1000000L) );
87 gettimeofday(&tv, NULL);
88 return static_cast<uint32_t>( (tv.tv_sec * 1000 ) + (tv.tv_usec / 1000) );
91 } // unnamed namespace
93 uint32_t SceneHolder::mSceneHolderCounter = 0;
95 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
99 SceneHolderLifeCycleObserver(Adaptor*& adaptor)
100 : mAdaptor( adaptor )
104 private: // Adaptor::LifeCycleObserver interface
106 void OnStart() override {};
107 void OnPause() override {};
108 void OnResume() override {};
109 void OnStop() override {};
110 void OnDestroy() override
120 SceneHolder::SceneHolder()
121 : mLifeCycleObserver( new SceneHolderLifeCycleObserver( mAdaptor ) ),
122 mId( mSceneHolderCounter++ ),
126 mIsBeingDeleted( false ),
127 mAdaptorStarted( false ),
132 SceneHolder::~SceneHolder()
136 mAdaptor->RemoveObserver( *mLifeCycleObserver.get() );
137 mAdaptor->RemoveWindow( this );
139 mAdaptor->DeleteSurface( *mSurface.get() );
150 void SceneHolder::Add( Dali::Actor actor )
158 void SceneHolder::Remove( Dali::Actor actor )
162 mScene.Remove( actor );
166 Dali::Layer SceneHolder::GetRootLayer() const
168 return mScene ? mScene.GetRootLayer() : Dali::Layer();
171 uint32_t SceneHolder::GetId() const
176 std::string SceneHolder::GetName() const
181 bool SceneHolder::IsVisible() const
186 Dali::Integration::Scene SceneHolder::GetScene()
191 Uint16Pair SceneHolder::GetDpi() const
196 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
198 mSurface.reset( surface );
200 mScene.SurfaceReplaced();
202 SurfaceResized( false );
206 mSurface->SetAdaptor( *mAdaptor );
207 mSurface->SetScene( mScene );
209 OnSurfaceSet( surface );
212 void SceneHolder::SurfaceResized( bool forceUpdate )
214 PositionSize surfacePositionSize = mSurface->GetPositionSize();
215 int orientation = mSurface->GetOrientation();
216 mScene.SurfaceResized( static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height ), orientation, forceUpdate );
218 GraphicsInterface& graphics = mAdaptor->GetGraphicsInterface();
219 EglGraphics* eglGraphics = static_cast<EglGraphics*>(&graphics);
222 eglGraphics->SetFullSwapNextFrame();
226 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
228 return mSurface.get();
231 void SceneHolder::SetBackgroundColor( const Vector4& color )
235 mScene.SetBackgroundColor( color );
237 GraphicsInterface& graphics = mAdaptor->GetGraphicsInterface();
238 EglGraphics* eglGraphics = static_cast<EglGraphics*>(&graphics);
241 eglGraphics->SetFullSwapNextFrame();
246 Vector4 SceneHolder::GetBackgroundColor() const
248 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
251 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
253 // Avoid doing this more than once
254 if( mAdaptorStarted )
259 DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
261 mAdaptorStarted = true;
264 PositionSize surfacePositionSize = mSurface->GetPositionSize();
265 mScene = Dali::Integration::Scene::New( Size(static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height )) );
267 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation( adaptor );
268 mAdaptor = &adaptorImpl;
270 // Create an observer for the adaptor lifecycle
271 mAdaptor->AddObserver( *mLifeCycleObserver );
275 mSurface->SetAdaptor( *mAdaptor );
276 mSurface->SetScene( mScene );
278 OnAdaptorSet( adaptor );
281 void SceneHolder::Pause()
288 void SceneHolder::Resume()
295 void SceneHolder::FeedTouchPoint( Dali::Integration::Point& point, int timeStamp )
299 timeStamp = GetCurrentMilliSeconds();
302 RecalculateTouchPosition( point );
304 Integration::TouchEvent touchEvent;
305 Integration::HoverEvent hoverEvent;
306 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
307 if( type != Integration::TouchEventCombiner::DISPATCH_NONE )
309 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 );
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 // First the touch and/or hover event & related gesture events are queued
316 if( type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH )
318 mScene.QueueEvent( touchEvent );
321 if( type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH )
323 mScene.QueueEvent( hoverEvent );
326 // Next the events are processed with a single call into Core
327 mAdaptor->ProcessCoreEvents();
331 void SceneHolder::FeedWheelEvent( Dali::Integration::WheelEvent& wheelEvent )
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 mScene.QueueEvent( wheelEvent );
338 mAdaptor->ProcessCoreEvents();
341 void SceneHolder::FeedKeyEvent( Dali::Integration::KeyEvent& keyEvent )
343 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
344 if( physicalKeyboard )
346 if( ! KeyLookup::IsDeviceButton( keyEvent.keyName.c_str() ) )
348 GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
352 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
353 // Keep the handle alive until the core events are processed.
354 Dali::BaseHandle sceneHolder( this );
356 // Create send KeyEvent to Core.
357 mScene.QueueEvent( keyEvent );
358 mAdaptor->ProcessCoreEvents();
361 void SceneHolder::AddFrameRenderedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
363 mScene.AddFrameRenderedCallback( std::move( callback ), frameId );
365 DALI_LOG_INFO( gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId );
368 void SceneHolder::AddFramePresentedCallback( std::unique_ptr< CallbackBase > callback, int32_t frameId )
370 mScene.AddFramePresentedCallback( std::move( callback ), frameId );
372 DALI_LOG_INFO( gSceneHolderLogFilter, Debug::General, "SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId );
375 Dali::Integration::SceneHolder SceneHolder::Get( Dali::Actor actor )
377 SceneHolder* sceneHolderImpl = nullptr;
379 if ( Internal::Adaptor::Adaptor::IsAvailable() )
381 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation( Internal::Adaptor::Adaptor::Get() );
382 sceneHolderImpl = adaptor.GetWindow( actor );
385 return Dali::Integration::SceneHolder( sceneHolderImpl );
388 void SceneHolder::Reset()
392 // Any touch listeners should be told of the interruption.
393 Integration::TouchEvent event;
394 Integration::Point point;
395 point.SetState( PointState::INTERRUPTED );
396 event.AddPoint( point );
398 // First the touch event & related gesture events are queued
399 mScene.QueueEvent( event );
401 // Next the events are processed with a single call into Core
402 mAdaptor->ProcessCoreEvents();
405 void SceneHolder::InitializeDpi()
407 unsigned int dpiHorizontal, dpiVertical;
408 dpiHorizontal = dpiVertical = 0;
410 mSurface->GetDpi( dpiHorizontal, dpiVertical );
411 mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
413 mDpi.SetX( dpiHorizontal );
414 mDpi.SetY( dpiVertical );