2 * Copyright (c) 2021 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/internal/window-system/common/window-render-surface.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/integration-api/gl-abstraction.h>
26 #include <dali/integration-api/adaptor-framework/thread-synchronization-interface.h>
27 #include <dali/integration-api/adaptor-framework/trigger-event-factory.h>
28 #include <dali/internal/adaptor/common/adaptor-impl.h>
29 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
30 #include <dali/internal/graphics/gles/egl-graphics.h>
31 #include <dali/internal/graphics/gles/egl-implementation.h>
32 #include <dali/internal/system/common/environment-variables.h>
33 #include <dali/internal/window-system/common/window-base.h>
34 #include <dali/internal/window-system/common/window-factory.h>
35 #include <dali/internal/window-system/common/window-system.h>
45 const int MINIMUM_DIMENSION_CHANGE(1); ///< Minimum change for window to be considered to have moved
46 const float FULL_UPDATE_RATIO(0.8f); ///< Force full update when the dirty area is larget than this ratio
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gWindowRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_WINDOW_RENDER_SURFACE");
52 void MergeRects(Rect<int>& mergingRect, const std::vector<Rect<int>>& rects)
55 if(mergingRect.IsEmpty())
57 for(; i < rects.size(); i++)
59 if(!rects[i].IsEmpty())
61 mergingRect = rects[i];
67 for(; i < rects.size(); i++)
69 mergingRect.Merge(rects[i]);
73 void InsertRects(WindowRenderSurface::DamagedRectsContainer& damagedRectsList, const std::vector<Rect<int>>& damagedRects)
75 damagedRectsList.push_front(damagedRects);
76 if(damagedRectsList.size() > 4) // past triple buffers + current
78 damagedRectsList.pop_back();
82 Rect<int32_t> RecalculateRect0(Rect<int32_t>& rect, const Rect<int32_t>& surfaceSize)
87 Rect<int32_t> RecalculateRect90(Rect<int32_t>& rect, const Rect<int32_t>& surfaceSize)
89 Rect<int32_t> newRect;
90 newRect.x = surfaceSize.height - (rect.y + rect.height);
92 newRect.width = rect.height;
93 newRect.height = rect.width;
97 Rect<int32_t> RecalculateRect180(Rect<int32_t>& rect, const Rect<int32_t>& surfaceSize)
99 Rect<int32_t> newRect;
100 newRect.x = surfaceSize.width - (rect.x + rect.width);
101 newRect.y = surfaceSize.height - (rect.y + rect.height);
102 newRect.width = rect.width;
103 newRect.height = rect.height;
107 Rect<int32_t> RecalculateRect270(Rect<int32_t>& rect, const Rect<int32_t>& surfaceSize)
109 Rect<int32_t> newRect;
111 newRect.y = surfaceSize.width - (rect.x + rect.width);
112 newRect.width = rect.height;
113 newRect.height = rect.width;
117 using RecalculateRectFunction = Rect<int32_t> (*)(Rect<int32_t>&, const Rect<int32_t>&);
119 RecalculateRectFunction RecalculateRect[4] = {RecalculateRect0, RecalculateRect90, RecalculateRect180, RecalculateRect270};
121 } // unnamed namespace
123 WindowRenderSurface::WindowRenderSurface(Dali::PositionSize positionSize, Any surface, bool isTransparent)
125 mDisplayConnection(nullptr),
126 mPositionSize(positionSize),
128 mThreadSynchronization(nullptr),
129 mRenderNotification(nullptr),
130 mPostRenderTrigger(),
131 mFrameRenderedTrigger(),
133 mEGLSurface(nullptr),
134 mEGLContext(nullptr),
135 mColorDepth(isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24),
136 mOutputTransformedSignal(),
137 mFrameCallbackInfoContainer(),
138 mBufferDamagedRects(),
140 mWindowRotationAngle(0),
141 mScreenRotationAngle(0),
145 mWindowRotationFinished(true),
146 mScreenRotationFinished(true),
147 mResizeFinished(true),
148 mDefaultScreenRotationAvailable(false),
149 mIsImeWindowSurface(false)
151 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n");
155 WindowRenderSurface::~WindowRenderSurface()
159 void WindowRenderSurface::Initialize(Any surface)
161 // If width or height are zero, go full screen.
162 if((mPositionSize.width == 0) || (mPositionSize.height == 0))
164 // Default window size == screen size
167 WindowSystem::GetScreenSize(mPositionSize.width, mPositionSize.height);
170 // Create a window base
171 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
172 mWindowBase = windowFactory->CreateWindowBase(mPositionSize, surface, (mColorDepth == COLOR_DEPTH_32 ? true : false));
175 mWindowBase->OutputTransformedSignal().Connect(this, &WindowRenderSurface::OutputTransformed);
177 // Check screen rotation
178 mScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
179 if(mScreenRotationAngle != 0)
181 mScreenRotationFinished = false;
182 mResizeFinished = false;
183 mDefaultScreenRotationAvailable = true;
184 DALI_LOG_RELEASE_INFO("WindowRenderSurface::Initialize, screen rotation is enabled, screen rotation angle:[%d]\n", mScreenRotationAngle);
188 Any WindowRenderSurface::GetNativeWindow()
190 return mWindowBase->GetNativeWindow();
193 int WindowRenderSurface::GetNativeWindowId()
195 return mWindowBase->GetNativeWindowId();
198 void WindowRenderSurface::Map()
203 void WindowRenderSurface::SetRenderNotification(TriggerEventInterface* renderNotification)
205 mRenderNotification = renderNotification;
208 void WindowRenderSurface::SetTransparency(bool transparent)
210 mWindowBase->SetTransparency(transparent);
213 void WindowRenderSurface::RequestRotation(int angle, int width, int height)
215 if(!mPostRenderTrigger)
217 mPostRenderTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessPostRender),
218 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
221 mPositionSize.width = width;
222 mPositionSize.height = height;
224 mWindowRotationAngle = angle;
225 mWindowRotationFinished = false;
226 mResizeFinished = false;
228 mWindowBase->SetWindowRotationAngle(mWindowRotationAngle);
230 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mWindowRotationAngle, mScreenRotationAngle);
233 WindowBase* WindowRenderSurface::GetWindowBase()
235 return mWindowBase.get();
238 WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
240 return mOutputTransformedSignal;
243 PositionSize WindowRenderSurface::GetPositionSize() const
245 return mPositionSize;
248 void WindowRenderSurface::GetDpi(unsigned int& dpiHorizontal, unsigned int& dpiVertical)
250 if(mDpiHorizontal == 0 || mDpiVertical == 0)
252 const char* environmentDpiHorizontal = std::getenv(DALI_ENV_DPI_HORIZONTAL);
253 mDpiHorizontal = environmentDpiHorizontal ? std::atoi(environmentDpiHorizontal) : 0;
255 const char* environmentDpiVertical = std::getenv(DALI_ENV_DPI_VERTICAL);
256 mDpiVertical = environmentDpiVertical ? std::atoi(environmentDpiVertical) : 0;
258 if(mDpiHorizontal == 0 || mDpiVertical == 0)
260 mWindowBase->GetDpi(mDpiHorizontal, mDpiVertical);
264 dpiHorizontal = mDpiHorizontal;
265 dpiVertical = mDpiVertical;
268 int WindowRenderSurface::GetOrientation() const
270 return mWindowBase->GetOrientation();
273 void WindowRenderSurface::InitializeGraphics()
275 mGraphics = &mAdaptor->GetGraphicsInterface();
277 DALI_ASSERT_ALWAYS(mGraphics && "Graphics interface is not created");
279 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
280 mEGL = &eglGraphics->GetEglInterface();
282 if(mEGLContext == NULL)
284 // Create the OpenGL context for this window
285 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
286 eglImpl.ChooseConfig(true, mColorDepth);
287 eglImpl.CreateWindowContext(mEGLContext);
289 // Create the OpenGL surface
294 void WindowRenderSurface::CreateSurface()
296 DALI_LOG_TRACE_METHOD(gWindowRenderSurfaceLogFilter);
299 if(mScreenRotationAngle == 0 || mScreenRotationAngle == 180)
301 width = mPositionSize.width;
302 height = mPositionSize.height;
306 width = mPositionSize.height;
307 height = mPositionSize.width;
310 // Create the EGL window
311 EGLNativeWindowType window = mWindowBase->CreateEglWindow(width, height);
313 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
315 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
316 mEGLSurface = eglImpl.CreateSurfaceWindow(window, mColorDepth);
318 DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: WinId (%d), w = %d h = %d angle = %d screen rotation = %d\n",
319 mWindowBase->GetNativeWindowId(),
321 mPositionSize.height,
322 mWindowRotationAngle,
323 mScreenRotationAngle);
326 void WindowRenderSurface::DestroySurface()
328 DALI_LOG_TRACE_METHOD(gWindowRenderSurfaceLogFilter);
330 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
333 DALI_LOG_RELEASE_INFO("WindowRenderSurface::DestroySurface: WinId (%d)\n", mWindowBase->GetNativeWindowId());
335 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
337 eglImpl.DestroySurface(mEGLSurface);
338 mEGLSurface = nullptr;
340 // Destroy context also
341 eglImpl.DestroyContext(mEGLContext);
342 mEGLContext = nullptr;
344 mWindowBase->DestroyEglWindow();
348 bool WindowRenderSurface::ReplaceGraphicsSurface()
350 DALI_LOG_TRACE_METHOD(gWindowRenderSurfaceLogFilter);
352 // Destroy the old one
353 mWindowBase->DestroyEglWindow();
356 if(mScreenRotationAngle == 0 || mScreenRotationAngle == 180)
358 width = mPositionSize.width;
359 height = mPositionSize.height;
363 width = mPositionSize.height;
364 height = mPositionSize.width;
367 // Create the EGL window
368 EGLNativeWindowType window = mWindowBase->CreateEglWindow(width, height);
370 // Set screen rotation
371 mScreenRotationFinished = false;
373 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
375 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
376 return eglImpl.ReplaceSurfaceWindow(window, mEGLSurface, mEGLContext);
379 void WindowRenderSurface::MoveResize(Dali::PositionSize positionSize)
381 bool needToMove = false;
382 bool needToResize = false;
385 if((fabs(positionSize.x - mPositionSize.x) >= MINIMUM_DIMENSION_CHANGE) ||
386 (fabs(positionSize.y - mPositionSize.y) >= MINIMUM_DIMENSION_CHANGE))
392 if((fabs(positionSize.width - mPositionSize.width) >= MINIMUM_DIMENSION_CHANGE) ||
393 (fabs(positionSize.height - mPositionSize.height) >= MINIMUM_DIMENSION_CHANGE))
402 mWindowBase->MoveResize(positionSize);
406 mWindowBase->Resize(positionSize);
409 mResizeFinished = false;
410 mPositionSize = positionSize;
416 mWindowBase->Move(positionSize);
418 mPositionSize = positionSize;
422 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height);
425 void WindowRenderSurface::StartRender()
429 bool WindowRenderSurface::PreRender(bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
431 mDamagedRects.assign(damagedRects.begin(), damagedRects.end());
433 Dali::Integration::Scene::FrameCallbackContainer callbacks;
435 Dali::Integration::Scene scene = mScene.GetHandle();
438 bool needFrameRenderedTrigger = false;
440 scene.GetFrameRenderedCallback(callbacks);
441 if(!callbacks.empty())
443 int frameRenderedSync = mWindowBase->CreateFrameRenderedSyncFence();
444 if(frameRenderedSync != -1)
446 Dali::Mutex::ScopedLock lock(mMutex);
448 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence [%d]\n", frameRenderedSync);
450 mFrameCallbackInfoContainer.push_back(std::unique_ptr<FrameCallbackInfo>(new FrameCallbackInfo(callbacks, frameRenderedSync)));
452 needFrameRenderedTrigger = true;
456 DALI_LOG_ERROR("WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence is failed\n");
463 scene.GetFramePresentedCallback(callbacks);
464 if(!callbacks.empty())
466 int framePresentedSync = mWindowBase->CreateFramePresentedSyncFence();
467 if(framePresentedSync != -1)
469 Dali::Mutex::ScopedLock lock(mMutex);
471 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: CreateFramePresentedSyncFence [%d]\n", framePresentedSync);
473 mFrameCallbackInfoContainer.push_back(std::unique_ptr<FrameCallbackInfo>(new FrameCallbackInfo(callbacks, framePresentedSync)));
475 needFrameRenderedTrigger = true;
479 DALI_LOG_ERROR("WindowRenderSurface::PreRender: CreateFramePresentedSyncFence is failed\n");
486 if(needFrameRenderedTrigger)
488 if(!mFrameRenderedTrigger)
490 mFrameRenderedTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessFrameCallback),
491 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
493 mFrameRenderedTrigger->Trigger();
498 * wl_egl_window_tizen_set_rotation(SetEglWindowRotation) -> PreRotation
499 * wl_egl_window_tizen_set_buffer_transform(SetEglWindowBufferTransform) -> Screen Rotation
500 * wl_egl_window_tizen_set_window_transform(SetEglWindowTransform) -> Window Rotation
501 * These function should be called before calling first drawing gl Function.
502 * Notice : PreRotation is not used in the latest tizen,
503 * because output transform event should be occured before egl window is not created.
506 if(mIsResizing || mDefaultScreenRotationAvailable)
508 int totalAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
510 // Window rotate or screen rotate
511 if(!mWindowRotationFinished || !mScreenRotationFinished)
513 mWindowBase->SetEglWindowBufferTransform(totalAngle);
515 // Reset only screen rotation flag
516 mScreenRotationFinished = true;
518 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mWindowRotationAngle, mScreenRotationAngle);
521 // Only window rotate
522 if(!mWindowRotationFinished)
524 mWindowBase->SetEglWindowTransform(mWindowRotationAngle);
530 Dali::PositionSize positionSize;
531 positionSize.x = mPositionSize.x;
532 positionSize.y = mPositionSize.y;
533 if(totalAngle == 0 || totalAngle == 180)
535 positionSize.width = mPositionSize.width;
536 positionSize.height = mPositionSize.height;
540 positionSize.width = mPositionSize.height;
541 positionSize.height = mPositionSize.width;
543 mWindowBase->ResizeEglWindow(positionSize);
544 mResizeFinished = true;
546 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: Set resize, x: %d, y: %d, w: %d, h:%d\n", positionSize.x, positionSize.y, positionSize.width, positionSize.height);
549 SetFullSwapNextFrame();
550 mDefaultScreenRotationAvailable = false;
553 SetBufferDamagedRects(mDamagedRects, clippingRect);
555 if(clippingRect.IsEmpty())
557 mDamagedRects.clear();
560 // This is now done when the render pass for the render surface begins
561 // MakeContextCurrent();
566 void WindowRenderSurface::PostRender()
568 // Inform the gl implementation that rendering has finished before informing the surface
569 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
572 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
575 if((mIsResizing && !mWindowRotationFinished) || mIsImeWindowSurface)
577 if(mThreadSynchronization)
579 // Enable PostRender flag
580 mThreadSynchronization->PostRenderStarted();
583 if(!mWindowRotationFinished || mIsImeWindowSurface)
585 mPostRenderTrigger->Trigger();
588 if(mThreadSynchronization)
590 // Wait until the event-thread complete the rotation event processing
591 mThreadSynchronization->PostRenderWaitForCompletion();
595 SwapBuffers(mDamagedRects);
597 if(mRenderNotification)
599 mRenderNotification->Trigger();
604 void WindowRenderSurface::StopRender()
608 void WindowRenderSurface::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
610 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n");
612 mThreadSynchronization = &threadSynchronization;
615 void WindowRenderSurface::ReleaseLock()
620 Dali::RenderSurfaceInterface::Type WindowRenderSurface::GetSurfaceType()
622 return Dali::RenderSurfaceInterface::WINDOW_RENDER_SURFACE;
625 void WindowRenderSurface::MakeContextCurrent()
629 mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
633 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
635 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
638 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
640 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
643 void WindowRenderSurface::InitializeImeSurface()
645 mIsImeWindowSurface = true;
646 if(!mPostRenderTrigger)
648 mPostRenderTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessPostRender),
649 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
653 void WindowRenderSurface::OutputTransformed()
655 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
657 if(mScreenRotationAngle != screenRotationAngle)
659 mScreenRotationAngle = screenRotationAngle;
660 mScreenRotationFinished = false;
661 mResizeFinished = false;
663 mOutputTransformedSignal.Emit();
665 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: window = %d screen = %d\n", mWindowRotationAngle, mScreenRotationAngle);
669 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle);
673 void WindowRenderSurface::ProcessPostRender()
675 if(!mWindowRotationFinished)
677 mWindowBase->WindowRotationCompleted(mWindowRotationAngle, mPositionSize.width, mPositionSize.height);
678 DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessPostRender: Rotation Done\n");
679 mWindowRotationFinished = true;
682 if(mIsImeWindowSurface)
684 mWindowBase->ImeWindowReadyToRender();
687 if(mThreadSynchronization)
689 mThreadSynchronization->PostRenderComplete();
693 void WindowRenderSurface::ProcessFrameCallback()
695 Dali::Mutex::ScopedLock lock(mMutex);
697 for(auto&& iter : mFrameCallbackInfoContainer)
699 if(!iter->fileDescriptorMonitor)
701 iter->fileDescriptorMonitor = std::unique_ptr<FileDescriptorMonitor>(new FileDescriptorMonitor(iter->fileDescriptor,
702 MakeCallback(this, &WindowRenderSurface::OnFileDescriptorEventDispatched),
703 FileDescriptorMonitor::FD_READABLE));
705 DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessFrameCallback: Add handler [%d]\n", iter->fileDescriptor);
710 void WindowRenderSurface::OnFileDescriptorEventDispatched(FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor)
712 if(!(eventBitMask & FileDescriptorMonitor::FD_READABLE))
714 DALI_LOG_ERROR("WindowRenderSurface::OnFileDescriptorEventDispatched: file descriptor error [%d]\n", eventBitMask);
715 close(fileDescriptor);
719 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OnFileDescriptorEventDispatched: Frame rendered [%d]\n", fileDescriptor);
721 std::unique_ptr<FrameCallbackInfo> callbackInfo;
723 Dali::Mutex::ScopedLock lock(mMutex);
724 auto frameCallbackInfo = std::find_if(mFrameCallbackInfoContainer.begin(), mFrameCallbackInfoContainer.end(), [fileDescriptor](std::unique_ptr<FrameCallbackInfo>& callbackInfo) {
725 return callbackInfo->fileDescriptor == fileDescriptor;
727 if(frameCallbackInfo != mFrameCallbackInfoContainer.end())
729 callbackInfo = std::move(*frameCallbackInfo);
731 mFrameCallbackInfoContainer.erase(frameCallbackInfo);
735 // Call the connected callback
738 for(auto&& iter : (callbackInfo)->callbacks)
740 CallbackBase::Execute(*(iter.first), iter.second);
745 void WindowRenderSurface::SetBufferDamagedRects(const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
747 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
750 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
751 if(!eglImpl.IsPartialUpdateRequired())
756 Rect<int> surfaceRect(0, 0, mPositionSize.width, mPositionSize.height);
758 if(mFullSwapNextFrame)
760 InsertRects(mBufferDamagedRects, std::vector<Rect<int>>(1, surfaceRect));
761 clippingRect = Rect<int>();
765 EGLint bufferAge = eglImpl.GetBufferAge(mEGLSurface);
767 // Buffer age 0 means the back buffer in invalid and requires full swap
768 if(!damagedRects.size() || bufferAge == 0)
770 InsertRects(mBufferDamagedRects, std::vector<Rect<int>>(1, surfaceRect));
771 clippingRect = Rect<int>();
775 // We push current frame damaged rects here, zero index for current frame
776 InsertRects(mBufferDamagedRects, damagedRects);
778 // Merge damaged rects into clipping rect
779 auto bufferDamagedRects = mBufferDamagedRects.begin();
780 while(bufferAge-- >= 0 && bufferDamagedRects != mBufferDamagedRects.end())
782 const std::vector<Rect<int>>& rects = *bufferDamagedRects++;
783 MergeRects(clippingRect, rects);
786 if(!clippingRect.Intersect(surfaceRect) || clippingRect.Area() > surfaceRect.Area() * FULL_UPDATE_RATIO)
788 // clipping area too big or doesn't intersect surface rect
789 clippingRect = Rect<int>();
793 std::vector<Rect<int>> damagedRegion;
794 Dali::Integration::Scene scene = mScene.GetHandle();
797 damagedRegion.push_back(RecalculateRect[std::min(scene.GetCurrentSurfaceOrientation() / 90, 3)](clippingRect, scene.GetCurrentSurfaceRect()));
801 damagedRegion.push_back(clippingRect);
804 eglImpl.SetDamageRegion(mEGLSurface, damagedRegion);
808 void WindowRenderSurface::SwapBuffers(const std::vector<Rect<int>>& damagedRects)
810 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
813 Rect<int32_t> surfaceRect;
814 int32_t orientation = 0;
816 Dali::Integration::Scene scene = mScene.GetHandle();
819 surfaceRect = scene.GetCurrentSurfaceRect();
820 orientation = std::min(scene.GetCurrentSurfaceOrientation() / 90, 3);
823 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
825 if(!eglImpl.IsPartialUpdateRequired() || mFullSwapNextFrame || !damagedRects.size() || (damagedRects[0].Area() > surfaceRect.Area() * FULL_UPDATE_RATIO))
827 mFullSwapNextFrame = false;
828 eglImpl.SwapBuffers(mEGLSurface);
832 mFullSwapNextFrame = false;
834 std::vector<Rect<int>> mergedRects = damagedRects;
836 // Merge intersecting rects, form an array of non intersecting rects to help driver a bit
837 // Could be optional and can be removed, needs to be checked with and without on platform
838 const int n = mergedRects.size();
839 for(int i = 0; i < n - 1; i++)
841 if(mergedRects[i].IsEmpty())
846 for(int j = i + 1; j < n; j++)
848 if(mergedRects[j].IsEmpty())
853 if(mergedRects[i].Intersects(mergedRects[j]))
855 mergedRects[i].Merge(mergedRects[j]);
856 mergedRects[j].width = 0;
857 mergedRects[j].height = 0;
863 for(int i = 0; i < n; i++)
865 if(!mergedRects[i].IsEmpty())
867 mergedRects[j++] = RecalculateRect[orientation](mergedRects[i], surfaceRect);
873 mergedRects.resize(j);
876 if(!mergedRects.size() || (mergedRects[0].Area() > surfaceRect.Area() * FULL_UPDATE_RATIO))
878 eglImpl.SwapBuffers(mEGLSurface);
882 eglImpl.SwapBuffers(mEGLSurface, mergedRects);
887 } // namespace Adaptor
889 } // namespace Internal