Add Overlay Layer in window
[platform/core/uifw/dali-adaptor.git] / dali / integration-api / adaptor-framework / scene-holder-impl.cpp
index 5bbe94b..7aa6f29 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <dali/integration-api/adaptor-framework/scene-holder-impl.h>
 
 // EXTERNAL INCLUDES
-#include <sys/time.h>
-#include <dali/public-api/actors/actor.h>
-#include <dali/public-api/actors/layer.h>
 #include <dali/integration-api/debug.h>
+#include <dali/integration-api/events/hover-event-integ.h>
 #include <dali/integration-api/events/key-event-integ.h>
 #include <dali/integration-api/events/touch-event-integ.h>
-#include <dali/integration-api/events/hover-event-integ.h>
 #include <dali/integration-api/events/wheel-event-integ.h>
+#include <dali/public-api/actors/actor.h>
+#include <dali/public-api/actors/layer.h>
+#include <dali/public-api/common/dali-common.h>
 
 // INTERNAL INCLUDES
 #include <dali/internal/adaptor/common/adaptor-impl.h>
 #include <dali/internal/graphics/gles/egl-graphics.h>
 #include <dali/internal/input/common/key-impl.h>
 #include <dali/internal/input/common/physical-keyboard-impl.h>
+#include <dali/internal/system/common/time-service.h>
 
 namespace Dali
 {
-
 namespace Internal
 {
-
 namespace Adaptor
 {
-
 namespace
 {
-
 #if defined(DEBUG_ENABLED)
-Integration::Log::Filter* gTouchEventLogFilter  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_TOUCH");
+Debug::Filter* gSceneHolderLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_SCENE_HOLDER");
 #endif
-
-// Copied from x server
-static uint32_t GetCurrentMilliSeconds(void)
-{
-  struct timeval tv;
-
-  struct timespec tp;
-  static clockid_t clockid;
-
-  if (!clockid)
-  {
-#ifdef CLOCK_MONOTONIC_COARSE
-    if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
-      (tp.tv_nsec / 1000) <= 1000 && clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
-    {
-      clockid = CLOCK_MONOTONIC_COARSE;
-    }
-    else
-#endif
-    if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
-    {
-      clockid = CLOCK_MONOTONIC;
-    }
-    else
-    {
-      clockid = ~0L;
-    }
-  }
-  if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
-  {
-    return static_cast<uint32_t>( (tp.tv_sec * 1000 ) + (tp.tv_nsec / 1000000L) );
-  }
-
-  gettimeofday(&tv, NULL);
-  return static_cast<uint32_t>( (tv.tv_sec * 1000 ) + (tv.tv_usec / 1000) );
-}
-
 } // unnamed namespace
 
 uint32_t SceneHolder::mSceneHolderCounter = 0;
@@ -94,18 +54,14 @@ uint32_t SceneHolder::mSceneHolderCounter = 0;
 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
 {
 public:
-
   SceneHolderLifeCycleObserver(Adaptor*& adaptor)
-  : mAdaptor( adaptor )
-  {
-  };
+  : mAdaptor(adaptor){};
 
 private: // Adaptor::LifeCycleObserver interface
-
-  void OnStart() override {};
-  void OnPause() override {};
-  void OnResume() override {};
-  void OnStop() override {};
+  void OnStart() override{};
+  void OnPause() override{};
+  void OnResume() override{};
+  void OnStop() override{};
   void OnDestroy() override
   {
     mAdaptor = nullptr;
@@ -115,49 +71,49 @@ private:
   Adaptor*& mAdaptor;
 };
 
-
 SceneHolder::SceneHolder()
-: mLifeCycleObserver( new SceneHolderLifeCycleObserver( mAdaptor ) ),
-  mId( mSceneHolderCounter++ ),
-  mSurface( nullptr ),
-  mAdaptor( nullptr ),
-  mIsBeingDeleted( false ),
-  mAdaptorStarted( false ),
-  mVisible( true )
+: mLifeCycleObserver(new SceneHolderLifeCycleObserver(mAdaptor)),
+  mId(mSceneHolderCounter++),
+  mSurface(nullptr),
+  mAdaptor(nullptr),
+  mDpi(),
+  mIsBeingDeleted(false),
+  mAdaptorStarted(false),
+  mVisible(true)
 {
 }
 
 SceneHolder::~SceneHolder()
 {
-  if ( mAdaptor )
+  if(mAdaptor)
   {
-    mAdaptor->RemoveObserver( *mLifeCycleObserver.get() );
-    mAdaptor->RemoveWindow( this );
+    mAdaptor->RemoveObserver(*mLifeCycleObserver.get());
+    mAdaptor->RemoveWindow(this);
 
-    mAdaptor->DeleteSurface( *mSurface.get() );
+    mAdaptor->DeleteSurface(*mSurface.get());
 
     mAdaptor = nullptr;
   }
 
-  if ( mScene )
+  if(mScene)
   {
     mScene.Discard();
   }
 }
 
-void SceneHolder::Add( Dali::Actor actor )
+void SceneHolder::Add(Dali::Actor actor)
 {
-  if ( mScene )
+  if(mScene)
   {
-    mScene.Add( actor );
+    mScene.Add(actor);
   }
 }
 
-void SceneHolder::Remove( Dali::Actor actor )
+void SceneHolder::Remove(Dali::Actor actor)
 {
-  if ( mScene )
+  if(mScene)
   {
-    mScene.Remove( actor );
+    mScene.Remove(actor);
   }
 }
 
@@ -166,6 +122,11 @@ Dali::Layer SceneHolder::GetRootLayer() const
   return mScene ? mScene.GetRootLayer() : Dali::Layer();
 }
 
+Dali::Layer SceneHolder::GetOverlayLayer()
+{
+  return mScene ? mScene.GetOverlayLayer() : Dali::Layer();
+}
+
 uint32_t SceneHolder::GetId() const
 {
   return mId;
@@ -186,36 +147,40 @@ Dali::Integration::Scene SceneHolder::GetScene()
   return mScene;
 }
 
+Uint16Pair SceneHolder::GetDpi() const
+{
+  return mDpi;
+}
+
 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
 {
-  mSurface.reset( surface );
+  mSurface.reset(surface);
 
   mScene.SurfaceReplaced();
 
-  SurfaceResized();
+  PositionSize surfacePositionSize = surface->GetPositionSize();
 
-  unsigned int dpiHorizontal, dpiVertical;
-  dpiHorizontal = dpiVertical = 0;
+  SurfaceResized(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height));
+
+  InitializeDpi();
 
-  mSurface->GetDpi( dpiHorizontal, dpiVertical );
-  mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
+  mSurface->SetAdaptor(*mAdaptor);
+  mSurface->SetScene(mScene);
 
-  mSurface->SetAdaptor( *mAdaptor );
+  // Recreate the render target
+  CreateRenderTarget();
 
-  OnSurfaceSet( surface );
+  OnSurfaceSet(surface);
 }
 
-void SceneHolder::SurfaceResized()
+void SceneHolder::SurfaceResized(float width, float height)
 {
-  PositionSize surfacePositionSize = mSurface->GetPositionSize();
-  mScene.SurfaceResized( static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height ) );
+  mScene.SurfaceResized(width, height);
 
-  GraphicsInterface& graphics = mAdaptor->GetGraphicsInterface();
-  EglGraphics* eglGraphics = static_cast<EglGraphics*>(&graphics);
-  if (eglGraphics)
-  {
-    eglGraphics->SetFullSwapNextFrame();
-  }
+  mSurface->SetFullSwapNextFrame();
+
+  // Recreate the render target
+  CreateRenderTarget();
 }
 
 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
@@ -223,18 +188,13 @@ Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
   return mSurface.get();
 }
 
-void SceneHolder::SetBackgroundColor( const Vector4& color )
+void SceneHolder::SetBackgroundColor(const Vector4& color)
 {
-  if( mScene )
+  if(mScene)
   {
-    mScene.SetBackgroundColor( color );
+    mScene.SetBackgroundColor(color);
 
-    GraphicsInterface& graphics = mAdaptor->GetGraphicsInterface();
-    EglGraphics* eglGraphics = static_cast<EglGraphics*>(&graphics);
-    if (eglGraphics)
-    {
-      eglGraphics->SetFullSwapNextFrame();
-    }
+    mSurface->SetFullSwapNextFrame();
   }
 }
 
@@ -246,35 +206,48 @@ Vector4 SceneHolder::GetBackgroundColor() const
 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
 {
   // Avoid doing this more than once
-  if( mAdaptorStarted )
+  if(mAdaptorStarted)
   {
     return;
   }
 
+  DALI_ASSERT_DEBUG(mSurface && "Surface needs to be set before calling this method\n");
+
   mAdaptorStarted = true;
 
   // Create the scene
   PositionSize surfacePositionSize = mSurface->GetPositionSize();
-  mScene = Dali::Integration::Scene::New( Size(static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height )) );
+  int          windowOrientation   = mSurface->GetSurfaceOrientation();
+  int          screenOrientation   = mSurface->GetScreenOrientation();
 
-  Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation( adaptor );
-  mAdaptor = &adaptorImpl;
+  mScene = Dali::Integration::Scene::New(Size(static_cast<float>(surfacePositionSize.width), static_cast<float>(surfacePositionSize.height)), windowOrientation, screenOrientation);
+
+  Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
+  mAdaptor                                = &adaptorImpl;
 
   // Create an observer for the adaptor lifecycle
-  mAdaptor->AddObserver( *mLifeCycleObserver );
+  mAdaptor->AddObserver(*mLifeCycleObserver);
 
-  if ( mSurface )
-  {
-    unsigned int dpiHorizontal, dpiVertical;
-    dpiHorizontal = dpiVertical = 0;
+  InitializeDpi();
 
-    mSurface->GetDpi( dpiHorizontal, dpiVertical );
-    mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
+  mSurface->SetAdaptor(*mAdaptor);
+  mSurface->SetScene(mScene);
 
-    mSurface->SetAdaptor( *mAdaptor );
-  }
+  // Create the render target
+  CreateRenderTarget();
 
-  OnAdaptorSet( adaptor );
+  OnAdaptorSet(adaptor);
+}
+
+void SceneHolder::CreateRenderTarget()
+{
+  Graphics::RenderTargetCreateInfo rtInfo{};
+  rtInfo
+    .SetSurface(mSurface.get())
+    .SetExtent({static_cast<uint32_t>(mSurface->GetPositionSize().width), static_cast<uint32_t>(mSurface->GetPositionSize().height)})
+    .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
+
+  mScene.SetSurfaceRenderTarget(rtInfo);
 }
 
 void SceneHolder::Pause()
@@ -291,35 +264,46 @@ void SceneHolder::Resume()
   OnResume();
 }
 
-void SceneHolder::FeedTouchPoint( Dali::Integration::Point& point, int timeStamp )
+void SceneHolder::SurfaceRotated(float width, float height, int32_t windowOrientation, int32_t screenOrientation)
+{
+  mScene.SurfaceRotated(width, height, windowOrientation, screenOrientation);
+}
+
+void SceneHolder::SetRotationCompletedAcknowledgement()
 {
-  if( timeStamp < 1 )
+  mScene.SetRotationCompletedAcknowledgement();
+}
+
+void SceneHolder::FeedTouchPoint(Dali::Integration::Point& point, int timeStamp)
+{
+  if(timeStamp < 1)
   {
-    timeStamp = GetCurrentMilliSeconds();
+    timeStamp = TimeService::GetMilliSeconds();
   }
 
-  RecalculateTouchPosition( point );
+  Vector2 convertedPosition = RecalculatePosition(point.GetScreenPosition());
+  point.SetScreenPosition(convertedPosition);
 
-  Integration::TouchEvent touchEvent;
-  Integration::HoverEvent hoverEvent;
+  Integration::TouchEvent                            touchEvent;
+  Integration::HoverEvent                            hoverEvent;
   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
-  if( type != Integration::TouchEventCombiner::DispatchNone )
+  if(type != Integration::TouchEventCombiner::DISPATCH_NONE)
   {
-    DALI_LOG_INFO( gTouchEventLogFilter, Debug::General, "%d: Device %d: Button state %d (%.2f, %.2f)\n", timeStamp, point.GetDeviceId(), point.GetState(), point.GetScreenPosition().x, point.GetScreenPosition().y );
+    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);
 
     // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
     // Keep the handle alive until the core events are processed.
-    Dali::BaseHandle sceneHolder( this );
+    Dali::BaseHandle sceneHolder(this);
 
     // First the touch and/or hover event & related gesture events are queued
-    if( type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth )
+    if(type == Integration::TouchEventCombiner::DISPATCH_TOUCH || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
     {
-      mScene.QueueEvent( touchEvent );
+      mScene.QueueEvent(touchEvent);
     }
 
-    if( type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth )
+    if(type == Integration::TouchEventCombiner::DISPATCH_HOVER || type == Integration::TouchEventCombiner::DISPATCH_BOTH)
     {
-      mScene.QueueEvent( hoverEvent );
+      mScene.QueueEvent(hoverEvent);
     }
 
     // Next the events are processed with a single call into Core
@@ -327,47 +311,64 @@ void SceneHolder::FeedTouchPoint( Dali::Integration::Point& point, int timeStamp
   }
 }
 
-void SceneHolder::FeedWheelEvent( Dali::Integration::WheelEvent& wheelEvent )
+void SceneHolder::FeedWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
 {
   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
   // Keep the handle alive until the core events are processed.
-  Dali::BaseHandle sceneHolder( this );
+  Dali::BaseHandle sceneHolder(this);
+
+  Vector2 convertedPosition = RecalculatePosition(wheelEvent.point);
+  wheelEvent.point          = convertedPosition;
 
-  mScene.QueueEvent( wheelEvent );
+  mScene.QueueEvent(wheelEvent);
   mAdaptor->ProcessCoreEvents();
 }
 
-void SceneHolder::FeedKeyEvent( Dali::Integration::KeyEvent& keyEvent )
+void SceneHolder::FeedKeyEvent(Dali::Integration::KeyEvent& keyEvent)
 {
   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
-  if( physicalKeyboard )
+  if(physicalKeyboard)
   {
-    if( ! KeyLookup::IsDeviceButton( keyEvent.keyName.c_str() ) )
+    if(!KeyLookup::IsDeviceButton(keyEvent.keyName.c_str()))
     {
-      GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
+      GetImplementation(physicalKeyboard).KeyReceived(keyEvent.time > 1);
     }
   }
 
   // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
   // Keep the handle alive until the core events are processed.
-  Dali::BaseHandle sceneHolder( this );
+  Dali::BaseHandle sceneHolder(this);
 
   // Create send KeyEvent to Core.
-  mScene.QueueEvent( keyEvent );
+  mScene.QueueEvent(keyEvent);
   mAdaptor->ProcessCoreEvents();
 }
 
-Dali::Integration::SceneHolder SceneHolder::Get( Dali::Actor actor )
+void SceneHolder::AddFrameRenderedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
+{
+  mScene.AddFrameRenderedCallback(std::move(callback), frameId);
+
+  DALI_LOG_RELEASE_INFO("SceneHolder::AddFrameRenderedCallback:: Added [%d]\n", frameId);
+}
+
+void SceneHolder::AddFramePresentedCallback(std::unique_ptr<CallbackBase> callback, int32_t frameId)
+{
+  mScene.AddFramePresentedCallback(std::move(callback), frameId);
+
+  DALI_LOG_RELEASE_INFO("SceneHolder::AddFramePresentedCallback:: Added [%d]\n", frameId);
+}
+
+Dali::Integration::SceneHolder SceneHolder::Get(Dali::Actor actor)
 {
   SceneHolder* sceneHolderImpl = nullptr;
 
-  if ( Internal::Adaptor::Adaptor::IsAvailable() )
+  if(Internal::Adaptor::Adaptor::IsAvailable())
   {
-    Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation( Internal::Adaptor::Adaptor::Get() );
-    sceneHolderImpl = adaptor.GetWindow( actor );
+    Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
+    sceneHolderImpl                           = adaptor.GetWindow(actor);
   }
 
-  return Dali::Integration::SceneHolder( sceneHolderImpl );
+  return Dali::Integration::SceneHolder(sceneHolderImpl);
 }
 
 void SceneHolder::Reset()
@@ -376,20 +377,31 @@ void SceneHolder::Reset()
 
   // Any touch listeners should be told of the interruption.
   Integration::TouchEvent event;
-  Integration::Point point;
-  point.SetState( PointState::INTERRUPTED );
-  event.AddPoint( point );
+  Integration::Point      point;
+  point.SetState(PointState::INTERRUPTED);
+  event.AddPoint(point);
 
   // First the touch event & related gesture events are queued
-  mScene.QueueEvent( event );
+  mScene.QueueEvent(event);
 
   // Next the events are processed with a single call into Core
   mAdaptor->ProcessCoreEvents();
 }
 
+void SceneHolder::InitializeDpi()
+{
+  unsigned int dpiHorizontal, dpiVertical;
+  dpiHorizontal = dpiVertical = 0;
+
+  mSurface->GetDpi(dpiHorizontal, dpiVertical);
+  mScene.SetDpi(Vector2(static_cast<float>(dpiHorizontal), static_cast<float>(dpiVertical)));
+
+  mDpi.SetX(dpiHorizontal);
+  mDpi.SetY(dpiVertical);
+}
 
-}// Adaptor
+} // namespace Adaptor
 
-}// Internal
+} // namespace Internal
 
-} // Dali
+} // namespace Dali