[Tizen] Remove list item in error case
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / window-render-surface.cpp
index 22ac0f3..ed66eb5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 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.
@@ -49,27 +49,6 @@ const float FULL_UPDATE_RATIO(0.8f);     ///< Force full update when the dirty a
 Debug::Filter* gWindowRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_WINDOW_RENDER_SURFACE");
 #endif
 
-void MergeRects(Rect<int>& mergingRect, const std::vector<Rect<int>>& rects)
-{
-  uint32_t i = 0;
-  if(mergingRect.IsEmpty())
-  {
-    for(; i < rects.size(); i++)
-    {
-      if(!rects[i].IsEmpty())
-      {
-        mergingRect = rects[i];
-        break;
-      }
-    }
-  }
-
-  for(; i < rects.size(); i++)
-  {
-    mergingRect.Merge(rects[i]);
-  }
-}
-
 void InsertRects(WindowRenderSurface::DamagedRectsContainer& damagedRectsList, const Rect<int>& damagedRects)
 {
   damagedRectsList.insert(damagedRectsList.begin(), damagedRects);
@@ -118,7 +97,7 @@ using RecalculateRectFunction = Rect<int32_t> (*)(Rect<int32_t>&, const Rect<int
 
 RecalculateRectFunction RecalculateRect[4] = {RecalculateRect0, RecalculateRect90, RecalculateRect180, RecalculateRect270};
 
-void MergeIntersectingRects(std::vector<Rect<int>>& damagedRects, int orientation, const Rect<int32_t>& surfaceRect)
+void MergeIntersectingRectsAndRotate(Rect<int>& mergingRect, std::vector<Rect<int>>& damagedRects, int orientation, const Rect<int32_t>& surfaceRect)
 {
   const int n = damagedRects.size();
   for(int i = 0; i < n - 1; i++)
@@ -149,6 +128,16 @@ void MergeIntersectingRects(std::vector<Rect<int>>& damagedRects, int orientatio
   {
     if(!damagedRects[i].IsEmpty())
     {
+      // Merge rects before rotate
+      if(mergingRect.IsEmpty())
+      {
+        mergingRect = damagedRects[i];
+      }
+      else
+      {
+        mergingRect.Merge(damagedRects[i]);
+      }
+
       damagedRects[j++] = RecalculateRect[orientation](damagedRects[i], surfaceRect);
     }
   }
@@ -175,6 +164,7 @@ WindowRenderSurface::WindowRenderSurface(Dali::PositionSize positionSize, Any su
   mEGLContext(nullptr),
   mColorDepth(isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24),
   mOutputTransformedSignal(),
+  mWindowRotationFinishedSignal(),
   mFrameCallbackInfoContainer(),
   mBufferDamagedRects(),
   mMutex(),
@@ -183,12 +173,9 @@ WindowRenderSurface::WindowRenderSurface(Dali::PositionSize positionSize, Any su
   mDpiHorizontal(0),
   mDpiVertical(0),
   mOwnSurface(false),
-  mWindowRotationFinished(true),
-  mScreenRotationFinished(true),
-  mResizeFinished(true),
-  mDefaultScreenRotationAvailable(false),
   mIsImeWindowSurface(false),
-  mNeedWindowRotationAcknowledgement(false)
+  mNeedWindowRotationAcknowledgement(false),
+  mIsWindowOrientationChanging(false)
 {
   DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n");
   Initialize(surface);
@@ -217,13 +204,11 @@ void WindowRenderSurface::Initialize(Any surface)
   mWindowBase->OutputTransformedSignal().Connect(this, &WindowRenderSurface::OutputTransformed);
 
   // Check screen rotation
-  mScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
-  if(mScreenRotationAngle != 0)
+  int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
+  if(screenRotationAngle != 0)
   {
-    mScreenRotationFinished         = false;
-    mResizeFinished                 = false;
-    mDefaultScreenRotationAvailable = true;
-    DALI_LOG_RELEASE_INFO("WindowRenderSurface::Initialize, screen rotation is enabled, screen rotation angle:[%d]\n", mScreenRotationAngle);
+    OutputTransformed();
+    DALI_LOG_RELEASE_INFO("WindowRenderSurface::Initialize, screen rotation is enabled, screen rotation angle:[%d]\n", screenRotationAngle);
   }
 }
 
@@ -260,15 +245,12 @@ void WindowRenderSurface::RequestRotation(int angle, PositionSize positionSize)
                                                                                                         TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
   }
 
-  mPositionSize = positionSize;
-
-  mWindowRotationAngle    = angle;
-  mWindowRotationFinished = false;
-  mResizeFinished         = false;
+  mPositionSize.x = positionSize.x;
+  mPositionSize.y = positionSize.y;
 
-  mWindowBase->SetWindowRotationAngle(mWindowRotationAngle);
+  mWindowBase->SetWindowRotationAngle(angle);
 
-  DALI_LOG_RELEASE_INFO("angle = %d screen rotation = %d, flag = %d\n", mWindowRotationAngle, mScreenRotationAngle, mWindowRotationFinished);
+  DALI_LOG_RELEASE_INFO("start window rotation angle = %d screen rotation = %d\n", angle, mScreenRotationAngle);
 }
 
 WindowBase* WindowRenderSurface::GetWindowBase()
@@ -281,6 +263,11 @@ WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
   return mOutputTransformedSignal;
 }
 
+WindowRenderSurface::RotationFinishedSignalType& WindowRenderSurface::RotationFinishedSignal()
+{
+  return mWindowRotationFinishedSignal;
+}
+
 PositionSize WindowRenderSurface::GetPositionSize() const
 {
   return mPositionSize;
@@ -306,9 +293,14 @@ void WindowRenderSurface::GetDpi(unsigned int& dpiHorizontal, unsigned int& dpiV
   dpiVertical   = mDpiVertical;
 }
 
-int WindowRenderSurface::GetOrientation() const
+int WindowRenderSurface::GetSurfaceOrientation() const
+{
+  return mWindowBase->GetWindowRotationAngle();
+}
+
+int WindowRenderSurface::GetScreenOrientation() const
 {
-  return mWindowBase->GetOrientation();
+  return mWindowBase->GetScreenRotationAngle();
 }
 
 void WindowRenderSurface::InitializeGraphics()
@@ -409,9 +401,6 @@ bool WindowRenderSurface::ReplaceGraphicsSurface()
   // Create the EGL window
   EGLNativeWindowType window = mWindowBase->CreateEglWindow(width, height);
 
-  // Set screen rotation
-  mScreenRotationFinished = false;
-
   auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
 
   Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
@@ -420,83 +409,25 @@ bool WindowRenderSurface::ReplaceGraphicsSurface()
 
 void WindowRenderSurface::UpdatePositionSize(Dali::PositionSize positionSize)
 {
-  bool needToMove   = false;
-  bool needToResize = false;
-
   // Check moving
   if((fabs(positionSize.x - mPositionSize.x) >= MINIMUM_DIMENSION_CHANGE) ||
      (fabs(positionSize.y - mPositionSize.y) >= MINIMUM_DIMENSION_CHANGE))
   {
-    needToMove = true;
-  }
+    mPositionSize.x = positionSize.x;
+    mPositionSize.y = positionSize.y;
 
-  // Check resizing
-  if((fabs(positionSize.width - mPositionSize.width) >= MINIMUM_DIMENSION_CHANGE) ||
-     (fabs(positionSize.height - mPositionSize.height) >= MINIMUM_DIMENSION_CHANGE))
-  {
-    needToResize = true;
-  }
-
-  if(needToResize)
-  {
-    mResizeFinished = false;
-    mPositionSize   = positionSize;
-  }
-  else
-  {
-    if(needToMove)
-    {
-      mPositionSize = positionSize;
-    }
+    DALI_LOG_RELEASE_INFO("Update Position by server (%d, %d)\n", mPositionSize.x, mPositionSize.y);
   }
-
-  DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height);
 }
 
 void WindowRenderSurface::MoveResize(Dali::PositionSize positionSize)
 {
-  bool needToMove   = false;
-  bool needToResize = false;
+  mPositionSize.x = positionSize.x;
+  mPositionSize.y = positionSize.y;
 
-  // Check moving
-  if((fabs(positionSize.x - mPositionSize.x) >= MINIMUM_DIMENSION_CHANGE) ||
-     (fabs(positionSize.y - mPositionSize.y) >= MINIMUM_DIMENSION_CHANGE))
-  {
-    needToMove = true;
-  }
-
-  // Check resizing
-  if((fabs(positionSize.width - mPositionSize.width) >= MINIMUM_DIMENSION_CHANGE) ||
-     (fabs(positionSize.height - mPositionSize.height) >= MINIMUM_DIMENSION_CHANGE))
-  {
-    needToResize = true;
-  }
-
-  if(needToResize)
-  {
-    if(needToMove)
-    {
-      mWindowBase->MoveResize(positionSize);
-    }
-    else
-    {
-      mWindowBase->Resize(positionSize);
-    }
+  DALI_LOG_RELEASE_INFO("Update Position by client (%d, %d)\n", positionSize.x, positionSize.y);
 
-    mResizeFinished = false;
-    mPositionSize   = positionSize;
-  }
-  else
-  {
-    if(needToMove)
-    {
-      mWindowBase->Move(positionSize);
-
-      mPositionSize = positionSize;
-    }
-  }
-
-  DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height);
+  mWindowBase->MoveResize(positionSize);
 }
 
 void WindowRenderSurface::StartRender()
@@ -510,6 +441,7 @@ bool WindowRenderSurface::PreRender(bool resizingSurface, const std::vector<Rect
   Dali::Integration::Scene::FrameCallbackContainer callbacks;
 
   Dali::Integration::Scene scene = mScene.GetHandle();
+
   if(scene)
   {
     bool needFrameRenderedTrigger = false;
@@ -579,52 +511,66 @@ bool WindowRenderSurface::PreRender(bool resizingSurface, const std::vector<Rect
     * Notice : PreRotation is not used in the latest tizen,
     *          because output transform event should be occured before egl window is not created.
     */
-
-  if(mIsResizing || mDefaultScreenRotationAvailable)
+  if(scene && resizingSurface)
   {
-    int totalAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
+    int  totalAngle                  = 0;
+    bool isScreenOrientationChanging = false;
 
-    // Window rotate or screen rotate
-    if(!mWindowRotationFinished || !mScreenRotationFinished)
+    if(mWindowRotationAngle != scene.GetCurrentSurfaceOrientation())
     {
-      mWindowBase->SetEglWindowBufferTransform(totalAngle);
+      mWindowRotationAngle         = scene.GetCurrentSurfaceOrientation();
+      mIsWindowOrientationChanging = true;
+    }
 
-      // Reset only screen rotation flag
-      mScreenRotationFinished = true;
+    if(mScreenRotationAngle != scene.GetCurrentScreenOrientation())
+    {
+      mScreenRotationAngle        = scene.GetCurrentScreenOrientation();
+      isScreenOrientationChanging = true;
+    }
+    totalAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
+
+    DALI_LOG_RELEASE_INFO("Window/Screen orientation ard changed, WinOrientation[%d],flag[%d], ScreenOrientation[%d],flag[%d], total[%d]\n", mWindowRotationAngle, mIsWindowOrientationChanging, mScreenRotationAngle, isScreenOrientationChanging, totalAngle);
+
+    Rect<int> surfaceSize = scene.GetCurrentSurfaceRect();
+    //update surface size
+    mPositionSize.width  = surfaceSize.width;
+    mPositionSize.height = surfaceSize.height;
+
+    DALI_LOG_RELEASE_INFO("Window is resizing, (%d, %d), [%d x %d]\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height);
 
-      DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mWindowRotationAngle, mScreenRotationAngle);
+    // Window rotate or screen rotate
+    if(mIsWindowOrientationChanging || isScreenOrientationChanging)
+    {
+      mWindowBase->SetEglWindowBufferTransform(totalAngle);
     }
 
     // Only window rotate
-    if(!mWindowRotationFinished)
+    if(mIsWindowOrientationChanging)
     {
       mWindowBase->SetEglWindowTransform(mWindowRotationAngle);
     }
 
     // Resize case
-    if(!mResizeFinished)
-    {
-      Dali::PositionSize positionSize;
-      positionSize.x = mPositionSize.x;
-      positionSize.y = mPositionSize.y;
-      if(totalAngle == 0 || totalAngle == 180)
-      {
-        positionSize.width  = mPositionSize.width;
-        positionSize.height = mPositionSize.height;
-      }
-      else
-      {
-        positionSize.width  = mPositionSize.height;
-        positionSize.height = mPositionSize.width;
-      }
-      mWindowBase->ResizeEglWindow(positionSize);
-      mResizeFinished = true;
+    Dali::PositionSize positionSize;
 
-      DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: Set resize, totalAngle: %d, x: %d, y: %d, w: %d, h:%d\n", totalAngle, positionSize.x, positionSize.y, positionSize.width, positionSize.height);
+    // Some native resize API(wl_egl_window_resize) has the input parameters of x, y, width and height.
+    // So, position data should be set.
+    positionSize.x = mPositionSize.x;
+    positionSize.y = mPositionSize.y;
+    if(totalAngle == 0 || totalAngle == 180)
+    {
+      positionSize.width  = mPositionSize.width;
+      positionSize.height = mPositionSize.height;
+    }
+    else
+    {
+      positionSize.width  = mPositionSize.height;
+      positionSize.height = mPositionSize.width;
     }
 
+    mWindowBase->ResizeEglWindow(positionSize);
+
     SetFullSwapNextFrame();
-    mDefaultScreenRotationAvailable = false;
   }
 
   SetBufferDamagedRects(damagedRects, clippingRect);
@@ -634,13 +580,12 @@ bool WindowRenderSurface::PreRender(bool resizingSurface, const std::vector<Rect
     Rect<int> surfaceRect = scene.GetCurrentSurfaceRect();
     if(clippingRect == surfaceRect)
     {
-      mDamagedRects.assign(1, surfaceRect);
-    }
-    else if(mDamagedRects.empty() && !clippingRect.IsEmpty())
-    {
-      // We will render clippingRect area but mDamagedRects is empty.
-      // So make mDamagedRects same with clippingRect to swap buffers.
-      mDamagedRects.assign(1, clippingRect);
+      int32_t totalAngle = scene.GetCurrentSurfaceOrientation() + scene.GetCurrentScreenOrientation();
+      if(totalAngle >= 360)
+      {
+        totalAngle -= 360;
+      }
+      mDamagedRects.assign(1, RecalculateRect[std::min(totalAngle / 90, 3)](surfaceRect, surfaceRect));
     }
   }
 
@@ -661,7 +606,7 @@ void WindowRenderSurface::PostRender()
 
     bool needWindowRotationCompleted = false;
 
-    if(!mWindowRotationFinished)
+    if(mIsWindowOrientationChanging)
     {
       if(mNeedWindowRotationAcknowledgement)
       {
@@ -676,10 +621,7 @@ void WindowRenderSurface::PostRender()
       }
       else
       {
-        if(mIsResizing)
-        {
-          needWindowRotationCompleted = true;
-        }
+        needWindowRotationCompleted = true;
       }
     }
 
@@ -691,7 +633,7 @@ void WindowRenderSurface::PostRender()
         mThreadSynchronization->PostRenderStarted();
       }
 
-      if(!mWindowRotationFinished || mIsImeWindowSurface)
+      if(mIsWindowOrientationChanging || mIsImeWindowSurface)
       {
         mPostRenderTrigger->Trigger();
       }
@@ -772,13 +714,9 @@ void WindowRenderSurface::OutputTransformed()
 
   if(mScreenRotationAngle != screenRotationAngle)
   {
-    mScreenRotationAngle    = screenRotationAngle;
-    mScreenRotationFinished = false;
-    mResizeFinished         = false;
-
     mOutputTransformedSignal.Emit();
 
-    DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: window = %d screen = %d\n", mWindowRotationAngle, mScreenRotationAngle);
+    DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: window = %d new screen angle = %d\n", mWindowRotationAngle, screenRotationAngle);
   }
   else
   {
@@ -786,18 +724,14 @@ void WindowRenderSurface::OutputTransformed()
   }
 }
 
-bool WindowRenderSurface::IsWindowRotating() const
-{
-  return !(mWindowRotationFinished);
-}
-
 void WindowRenderSurface::ProcessPostRender()
 {
-  if(!mWindowRotationFinished)
+  if(mIsWindowOrientationChanging)
   {
+    mWindowRotationFinishedSignal.Emit();
     mWindowBase->WindowRotationCompleted(mWindowRotationAngle, mPositionSize.width, mPositionSize.height);
-    mWindowRotationFinished = true;
-    DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessPostRender: Rotation Done, flag = %d\n", mWindowRotationFinished);
+    mIsWindowOrientationChanging = false;
+    DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessPostRender: Rotation Done, flag = %d\n", mIsWindowOrientationChanging);
   }
 
   if(mIsImeWindowSurface)
@@ -830,13 +764,6 @@ void WindowRenderSurface::ProcessFrameCallback()
 
 void WindowRenderSurface::OnFileDescriptorEventDispatched(FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor)
 {
-  if(!(eventBitMask & FileDescriptorMonitor::FD_READABLE))
-  {
-    DALI_LOG_ERROR("WindowRenderSurface::OnFileDescriptorEventDispatched: file descriptor error [%d]\n", eventBitMask);
-    close(fileDescriptor);
-    return;
-  }
-
   DALI_LOG_RELEASE_INFO("WindowRenderSurface::OnFileDescriptorEventDispatched: Frame rendered [%d]\n", fileDescriptor);
 
   std::unique_ptr<FrameCallbackInfo> callbackInfo;
@@ -854,7 +781,7 @@ void WindowRenderSurface::OnFileDescriptorEventDispatched(FileDescriptorMonitor:
   }
 
   // Call the connected callback
-  if(callbackInfo)
+  if(callbackInfo && (eventBitMask & FileDescriptorMonitor::FD_READABLE))
   {
     for(auto&& iter : (callbackInfo)->callbacks)
     {
@@ -875,8 +802,13 @@ void WindowRenderSurface::SetBufferDamagedRects(const std::vector<Rect<int>>& da
     Dali::Integration::Scene scene = mScene.GetHandle();
     if(scene)
     {
-      surfaceRect = scene.GetCurrentSurfaceRect();
-      orientation = std::min(scene.GetCurrentSurfaceOrientation() / 90, 3);
+      surfaceRect        = scene.GetCurrentSurfaceRect();
+      int32_t totalAngle = scene.GetCurrentSurfaceOrientation() + scene.GetCurrentScreenOrientation();
+      if(totalAngle >= 360)
+      {
+        totalAngle -= 360;
+      }
+      orientation = std::min(totalAngle / 90, 3);
     }
 
     Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
@@ -887,6 +819,15 @@ void WindowRenderSurface::SetBufferDamagedRects(const std::vector<Rect<int>>& da
       return;
     }
 
+    if(damagedRects.empty())
+    {
+      // Empty damaged rect. We don't need rendering
+      clippingRect = Rect<int>();
+      // Clean up current damanged rects.
+      mDamagedRects.clear();
+      return;
+    }
+
     mGraphics->ActivateSurfaceContext(this);
 
     EGLint bufferAge = eglImpl.GetBufferAge(mEGLSurface);
@@ -903,10 +844,8 @@ void WindowRenderSurface::SetBufferDamagedRects(const std::vector<Rect<int>>& da
 
     // Merge intersecting rects, form an array of non intersecting rects to help driver a bit
     // Could be optional and can be removed, needs to be checked with and without on platform
-    MergeIntersectingRects(mDamagedRects, orientation, surfaceRect);
-
-    // Make one clipping rect
-    MergeRects(clippingRect, mDamagedRects);
+    // And then, Make one clipping rect, and rotate rects by orientation.
+    MergeIntersectingRectsAndRotate(clippingRect, mDamagedRects, orientation, surfaceRect);
 
     // We push current frame damaged rects here, zero index for current frame
     InsertRects(mBufferDamagedRects, clippingRect);
@@ -939,7 +878,7 @@ void WindowRenderSurface::SetBufferDamagedRects(const std::vector<Rect<int>>& da
       std::vector<Rect<int>> damagedRegion;
       if(scene)
       {
-        damagedRegion.push_back(RecalculateRect[std::min(scene.GetCurrentSurfaceOrientation() / 90, 3)](clippingRect, scene.GetCurrentSurfaceRect()));
+        damagedRegion.push_back(RecalculateRect[orientation](clippingRect, surfaceRect));
       }
       else
       {