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 if(mEGLContext == NULL)
277 mGraphics = &mAdaptor->GetGraphicsInterface();
279 DALI_ASSERT_ALWAYS(mGraphics && "Graphics interface is not created");
281 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
282 mEGL = &eglGraphics->GetEglInterface();
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 InitializeGraphics();
433 mDamagedRects.assign(damagedRects.begin(), damagedRects.end());
435 Dali::Integration::Scene::FrameCallbackContainer callbacks;
437 Dali::Integration::Scene scene = mScene.GetHandle();
440 bool needFrameRenderedTrigger = false;
442 scene.GetFrameRenderedCallback(callbacks);
443 if(!callbacks.empty())
445 int frameRenderedSync = mWindowBase->CreateFrameRenderedSyncFence();
446 if(frameRenderedSync != -1)
448 Dali::Mutex::ScopedLock lock(mMutex);
450 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence [%d]\n", frameRenderedSync);
452 mFrameCallbackInfoContainer.push_back(std::unique_ptr<FrameCallbackInfo>(new FrameCallbackInfo(callbacks, frameRenderedSync)));
454 needFrameRenderedTrigger = true;
458 DALI_LOG_ERROR("WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence is failed\n");
465 scene.GetFramePresentedCallback(callbacks);
466 if(!callbacks.empty())
468 int framePresentedSync = mWindowBase->CreateFramePresentedSyncFence();
469 if(framePresentedSync != -1)
471 Dali::Mutex::ScopedLock lock(mMutex);
473 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: CreateFramePresentedSyncFence [%d]\n", framePresentedSync);
475 mFrameCallbackInfoContainer.push_back(std::unique_ptr<FrameCallbackInfo>(new FrameCallbackInfo(callbacks, framePresentedSync)));
477 needFrameRenderedTrigger = true;
481 DALI_LOG_ERROR("WindowRenderSurface::PreRender: CreateFramePresentedSyncFence is failed\n");
488 if(needFrameRenderedTrigger)
490 if(!mFrameRenderedTrigger)
492 mFrameRenderedTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessFrameCallback),
493 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
495 mFrameRenderedTrigger->Trigger();
500 * wl_egl_window_tizen_set_rotation(SetEglWindowRotation) -> PreRotation
501 * wl_egl_window_tizen_set_buffer_transform(SetEglWindowBufferTransform) -> Screen Rotation
502 * wl_egl_window_tizen_set_window_transform(SetEglWindowTransform) -> Window Rotation
503 * These function should be called before calling first drawing gl Function.
504 * Notice : PreRotation is not used in the latest tizen,
505 * because output transform event should be occured before egl window is not created.
508 if(mIsResizing || mDefaultScreenRotationAvailable)
510 int totalAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
512 // Window rotate or screen rotate
513 if(!mWindowRotationFinished || !mScreenRotationFinished)
515 mWindowBase->SetEglWindowBufferTransform(totalAngle);
517 // Reset only screen rotation flag
518 mScreenRotationFinished = true;
520 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mWindowRotationAngle, mScreenRotationAngle);
523 // Only window rotate
524 if(!mWindowRotationFinished)
526 mWindowBase->SetEglWindowTransform(mWindowRotationAngle);
532 Dali::PositionSize positionSize;
533 positionSize.x = mPositionSize.x;
534 positionSize.y = mPositionSize.y;
535 if(totalAngle == 0 || totalAngle == 180)
537 positionSize.width = mPositionSize.width;
538 positionSize.height = mPositionSize.height;
542 positionSize.width = mPositionSize.height;
543 positionSize.height = mPositionSize.width;
545 mWindowBase->ResizeEglWindow(positionSize);
546 mResizeFinished = true;
548 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);
551 SetFullSwapNextFrame();
552 mDefaultScreenRotationAvailable = false;
555 SetBufferDamagedRects(mDamagedRects, clippingRect);
557 if(clippingRect.IsEmpty())
559 mDamagedRects.clear();
562 // This is now done when the render pass for the render surface begins
563 // MakeContextCurrent();
568 void WindowRenderSurface::PostRender()
570 // Inform the gl implementation that rendering has finished before informing the surface
571 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
574 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
577 if((mIsResizing && !mWindowRotationFinished) || mIsImeWindowSurface)
579 if(mThreadSynchronization)
581 // Enable PostRender flag
582 mThreadSynchronization->PostRenderStarted();
585 if(!mWindowRotationFinished || mIsImeWindowSurface)
587 mPostRenderTrigger->Trigger();
590 if(mThreadSynchronization)
592 // Wait until the event-thread complete the rotation event processing
593 mThreadSynchronization->PostRenderWaitForCompletion();
597 SwapBuffers(mDamagedRects);
599 if(mRenderNotification)
601 mRenderNotification->Trigger();
606 void WindowRenderSurface::StopRender()
610 void WindowRenderSurface::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
612 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n");
614 mThreadSynchronization = &threadSynchronization;
617 void WindowRenderSurface::ReleaseLock()
622 Dali::RenderSurfaceInterface::Type WindowRenderSurface::GetSurfaceType()
624 return Dali::RenderSurfaceInterface::WINDOW_RENDER_SURFACE;
627 void WindowRenderSurface::MakeContextCurrent()
631 mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
635 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
637 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
640 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
642 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
645 void WindowRenderSurface::InitializeImeSurface()
647 mIsImeWindowSurface = true;
648 if(!mPostRenderTrigger)
650 mPostRenderTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessPostRender),
651 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
655 void WindowRenderSurface::OutputTransformed()
657 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
659 if(mScreenRotationAngle != screenRotationAngle)
661 mScreenRotationAngle = screenRotationAngle;
662 mScreenRotationFinished = false;
663 mResizeFinished = false;
665 mOutputTransformedSignal.Emit();
667 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: window = %d screen = %d\n", mWindowRotationAngle, mScreenRotationAngle);
671 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle);
675 void WindowRenderSurface::ProcessPostRender()
677 if(!mWindowRotationFinished)
679 mWindowBase->WindowRotationCompleted(mWindowRotationAngle, mPositionSize.width, mPositionSize.height);
680 DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessPostRender: Rotation Done\n");
681 mWindowRotationFinished = true;
684 if(mIsImeWindowSurface)
686 mWindowBase->ImeWindowReadyToRender();
689 if(mThreadSynchronization)
691 mThreadSynchronization->PostRenderComplete();
695 void WindowRenderSurface::ProcessFrameCallback()
697 Dali::Mutex::ScopedLock lock(mMutex);
699 for(auto&& iter : mFrameCallbackInfoContainer)
701 if(!iter->fileDescriptorMonitor)
703 iter->fileDescriptorMonitor = std::unique_ptr<FileDescriptorMonitor>(new FileDescriptorMonitor(iter->fileDescriptor,
704 MakeCallback(this, &WindowRenderSurface::OnFileDescriptorEventDispatched),
705 FileDescriptorMonitor::FD_READABLE));
707 DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessFrameCallback: Add handler [%d]\n", iter->fileDescriptor);
712 void WindowRenderSurface::OnFileDescriptorEventDispatched(FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor)
714 if(!(eventBitMask & FileDescriptorMonitor::FD_READABLE))
716 DALI_LOG_ERROR("WindowRenderSurface::OnFileDescriptorEventDispatched: file descriptor error [%d]\n", eventBitMask);
717 close(fileDescriptor);
721 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OnFileDescriptorEventDispatched: Frame rendered [%d]\n", fileDescriptor);
723 std::unique_ptr<FrameCallbackInfo> callbackInfo;
725 Dali::Mutex::ScopedLock lock(mMutex);
726 auto frameCallbackInfo = std::find_if(mFrameCallbackInfoContainer.begin(), mFrameCallbackInfoContainer.end(), [fileDescriptor](std::unique_ptr<FrameCallbackInfo>& callbackInfo) {
727 return callbackInfo->fileDescriptor == fileDescriptor;
729 if(frameCallbackInfo != mFrameCallbackInfoContainer.end())
731 callbackInfo = std::move(*frameCallbackInfo);
733 mFrameCallbackInfoContainer.erase(frameCallbackInfo);
737 // Call the connected callback
740 for(auto&& iter : (callbackInfo)->callbacks)
742 CallbackBase::Execute(*(iter.first), iter.second);
747 void WindowRenderSurface::SetBufferDamagedRects(const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
749 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
752 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
753 if(!eglImpl.IsPartialUpdateRequired())
758 Rect<int> surfaceRect(0, 0, mPositionSize.width, mPositionSize.height);
760 if(mFullSwapNextFrame)
762 InsertRects(mBufferDamagedRects, std::vector<Rect<int>>(1, surfaceRect));
763 clippingRect = Rect<int>();
767 EGLint bufferAge = eglImpl.GetBufferAge(mEGLSurface);
769 // Buffer age 0 means the back buffer in invalid and requires full swap
770 if(!damagedRects.size() || bufferAge == 0)
772 InsertRects(mBufferDamagedRects, std::vector<Rect<int>>(1, surfaceRect));
773 clippingRect = Rect<int>();
777 // We push current frame damaged rects here, zero index for current frame
778 InsertRects(mBufferDamagedRects, damagedRects);
780 // Merge damaged rects into clipping rect
781 auto bufferDamagedRects = mBufferDamagedRects.begin();
782 while(bufferAge-- >= 0 && bufferDamagedRects != mBufferDamagedRects.end())
784 const std::vector<Rect<int>>& rects = *bufferDamagedRects++;
785 MergeRects(clippingRect, rects);
788 if(!clippingRect.Intersect(surfaceRect) || clippingRect.Area() > surfaceRect.Area() * FULL_UPDATE_RATIO)
790 // clipping area too big or doesn't intersect surface rect
791 clippingRect = Rect<int>();
795 std::vector<Rect<int>> damagedRegion;
796 Dali::Integration::Scene scene = mScene.GetHandle();
799 damagedRegion.push_back(RecalculateRect[std::min(scene.GetCurrentSurfaceOrientation() / 90, 3)](clippingRect, scene.GetCurrentSurfaceRect()));
803 damagedRegion.push_back(clippingRect);
806 eglImpl.SetDamageRegion(mEGLSurface, damagedRegion);
810 void WindowRenderSurface::SwapBuffers(const std::vector<Rect<int>>& damagedRects)
812 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
815 Rect<int32_t> surfaceRect;
816 int32_t orientation = 0;
818 Dali::Integration::Scene scene = mScene.GetHandle();
821 surfaceRect = scene.GetCurrentSurfaceRect();
822 orientation = std::min(scene.GetCurrentSurfaceOrientation() / 90, 3);
825 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
827 if(!eglImpl.IsPartialUpdateRequired() || mFullSwapNextFrame || !damagedRects.size() || (damagedRects[0].Area() > surfaceRect.Area() * FULL_UPDATE_RATIO))
829 mFullSwapNextFrame = false;
830 eglImpl.SwapBuffers(mEGLSurface);
834 mFullSwapNextFrame = false;
836 std::vector<Rect<int>> mergedRects = damagedRects;
838 // Merge intersecting rects, form an array of non intersecting rects to help driver a bit
839 // Could be optional and can be removed, needs to be checked with and without on platform
840 const int n = mergedRects.size();
841 for(int i = 0; i < n - 1; i++)
843 if(mergedRects[i].IsEmpty())
848 for(int j = i + 1; j < n; j++)
850 if(mergedRects[j].IsEmpty())
855 if(mergedRects[i].Intersects(mergedRects[j]))
857 mergedRects[i].Merge(mergedRects[j]);
858 mergedRects[j].width = 0;
859 mergedRects[j].height = 0;
865 for(int i = 0; i < n; i++)
867 if(!mergedRects[i].IsEmpty())
869 mergedRects[j++] = RecalculateRect[orientation](mergedRects[i], surfaceRect);
875 mergedRects.resize(j);
878 if(!mergedRects.size() || (mergedRects[0].Area() > surfaceRect.Area() * FULL_UPDATE_RATIO))
880 eglImpl.SwapBuffers(mEGLSurface);
884 eglImpl.SwapBuffers(mEGLSurface, mergedRects);
889 } // namespace Adaptor
891 } // namespace Internal