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),
150 mNeedWindowRotationAcknowledgement(false)
152 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n");
156 WindowRenderSurface::~WindowRenderSurface()
160 void WindowRenderSurface::Initialize(Any surface)
162 // If width or height are zero, go full screen.
163 if((mPositionSize.width == 0) || (mPositionSize.height == 0))
165 // Default window size == screen size
168 WindowSystem::GetScreenSize(mPositionSize.width, mPositionSize.height);
171 // Create a window base
172 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
173 mWindowBase = windowFactory->CreateWindowBase(mPositionSize, surface, (mColorDepth == COLOR_DEPTH_32 ? true : false));
176 mWindowBase->OutputTransformedSignal().Connect(this, &WindowRenderSurface::OutputTransformed);
178 // Check screen rotation
179 mScreenRotationAngle = mWindowBase->GetScreenRotationAngle();
180 if(mScreenRotationAngle != 0)
182 mScreenRotationFinished = false;
183 mResizeFinished = false;
184 mDefaultScreenRotationAvailable = true;
185 DALI_LOG_RELEASE_INFO("WindowRenderSurface::Initialize, screen rotation is enabled, screen rotation angle:[%d]\n", mScreenRotationAngle);
189 Any WindowRenderSurface::GetNativeWindow()
191 return mWindowBase->GetNativeWindow();
194 int WindowRenderSurface::GetNativeWindowId()
196 return mWindowBase->GetNativeWindowId();
199 void WindowRenderSurface::Map()
204 void WindowRenderSurface::SetRenderNotification(TriggerEventInterface* renderNotification)
206 mRenderNotification = renderNotification;
209 void WindowRenderSurface::SetTransparency(bool transparent)
211 mWindowBase->SetTransparency(transparent);
214 void WindowRenderSurface::RequestRotation(int angle, int width, int height)
216 if(!mPostRenderTrigger)
218 mPostRenderTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessPostRender),
219 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
222 mPositionSize.width = width;
223 mPositionSize.height = height;
225 mWindowRotationAngle = angle;
226 mWindowRotationFinished = false;
227 mResizeFinished = false;
229 mWindowBase->SetWindowRotationAngle(mWindowRotationAngle);
231 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::Rotate: angle = %d screen rotation = %d\n", mWindowRotationAngle, mScreenRotationAngle);
234 WindowBase* WindowRenderSurface::GetWindowBase()
236 return mWindowBase.get();
239 WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
241 return mOutputTransformedSignal;
244 PositionSize WindowRenderSurface::GetPositionSize() const
246 return mPositionSize;
249 void WindowRenderSurface::GetDpi(unsigned int& dpiHorizontal, unsigned int& dpiVertical)
251 if(mDpiHorizontal == 0 || mDpiVertical == 0)
253 const char* environmentDpiHorizontal = std::getenv(DALI_ENV_DPI_HORIZONTAL);
254 mDpiHorizontal = environmentDpiHorizontal ? std::atoi(environmentDpiHorizontal) : 0;
256 const char* environmentDpiVertical = std::getenv(DALI_ENV_DPI_VERTICAL);
257 mDpiVertical = environmentDpiVertical ? std::atoi(environmentDpiVertical) : 0;
259 if(mDpiHorizontal == 0 || mDpiVertical == 0)
261 mWindowBase->GetDpi(mDpiHorizontal, mDpiVertical);
265 dpiHorizontal = mDpiHorizontal;
266 dpiVertical = mDpiVertical;
269 int WindowRenderSurface::GetOrientation() const
271 return mWindowBase->GetOrientation();
274 void WindowRenderSurface::InitializeGraphics()
276 if(mEGLContext == NULL)
278 mGraphics = &mAdaptor->GetGraphicsInterface();
280 DALI_ASSERT_ALWAYS(mGraphics && "Graphics interface is not created");
282 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
283 mEGL = &eglGraphics->GetEglInterface();
285 // Create the OpenGL context for this window
286 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
287 eglImpl.ChooseConfig(true, mColorDepth);
288 eglImpl.CreateWindowContext(mEGLContext);
290 // Create the OpenGL surface
295 void WindowRenderSurface::CreateSurface()
297 DALI_LOG_TRACE_METHOD(gWindowRenderSurfaceLogFilter);
300 if(mScreenRotationAngle == 0 || mScreenRotationAngle == 180)
302 width = mPositionSize.width;
303 height = mPositionSize.height;
307 width = mPositionSize.height;
308 height = mPositionSize.width;
311 // Create the EGL window
312 EGLNativeWindowType window = mWindowBase->CreateEglWindow(width, height);
314 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
316 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
317 mEGLSurface = eglImpl.CreateSurfaceWindow(window, mColorDepth);
319 DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: WinId (%d), w = %d h = %d angle = %d screen rotation = %d\n",
320 mWindowBase->GetNativeWindowId(),
322 mPositionSize.height,
323 mWindowRotationAngle,
324 mScreenRotationAngle);
327 void WindowRenderSurface::DestroySurface()
329 DALI_LOG_TRACE_METHOD(gWindowRenderSurfaceLogFilter);
331 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
334 DALI_LOG_RELEASE_INFO("WindowRenderSurface::DestroySurface: WinId (%d)\n", mWindowBase->GetNativeWindowId());
336 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
338 eglImpl.DestroySurface(mEGLSurface);
339 mEGLSurface = nullptr;
341 // Destroy context also
342 eglImpl.DestroyContext(mEGLContext);
343 mEGLContext = nullptr;
345 mWindowBase->DestroyEglWindow();
349 bool WindowRenderSurface::ReplaceGraphicsSurface()
351 DALI_LOG_TRACE_METHOD(gWindowRenderSurfaceLogFilter);
353 // Destroy the old one
354 mWindowBase->DestroyEglWindow();
357 if(mScreenRotationAngle == 0 || mScreenRotationAngle == 180)
359 width = mPositionSize.width;
360 height = mPositionSize.height;
364 width = mPositionSize.height;
365 height = mPositionSize.width;
368 // Create the EGL window
369 EGLNativeWindowType window = mWindowBase->CreateEglWindow(width, height);
371 // Set screen rotation
372 mScreenRotationFinished = false;
374 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
376 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
377 return eglImpl.ReplaceSurfaceWindow(window, mEGLSurface, mEGLContext);
380 void WindowRenderSurface::MoveResize(Dali::PositionSize positionSize)
382 bool needToMove = false;
383 bool needToResize = false;
386 if((fabs(positionSize.x - mPositionSize.x) >= MINIMUM_DIMENSION_CHANGE) ||
387 (fabs(positionSize.y - mPositionSize.y) >= MINIMUM_DIMENSION_CHANGE))
393 if((fabs(positionSize.width - mPositionSize.width) >= MINIMUM_DIMENSION_CHANGE) ||
394 (fabs(positionSize.height - mPositionSize.height) >= MINIMUM_DIMENSION_CHANGE))
403 mWindowBase->MoveResize(positionSize);
407 mWindowBase->Resize(positionSize);
410 mResizeFinished = false;
411 mPositionSize = positionSize;
417 mWindowBase->Move(positionSize);
419 mPositionSize = positionSize;
423 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::MoveResize: %d, %d, %d, %d\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height);
426 void WindowRenderSurface::StartRender()
430 bool WindowRenderSurface::PreRender(bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
432 InitializeGraphics();
434 mDamagedRects.assign(damagedRects.begin(), damagedRects.end());
436 Dali::Integration::Scene::FrameCallbackContainer callbacks;
438 Dali::Integration::Scene scene = mScene.GetHandle();
441 bool needFrameRenderedTrigger = false;
443 scene.GetFrameRenderedCallback(callbacks);
444 if(!callbacks.empty())
446 int frameRenderedSync = mWindowBase->CreateFrameRenderedSyncFence();
447 if(frameRenderedSync != -1)
449 Dali::Mutex::ScopedLock lock(mMutex);
451 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence [%d]\n", frameRenderedSync);
453 mFrameCallbackInfoContainer.push_back(std::unique_ptr<FrameCallbackInfo>(new FrameCallbackInfo(callbacks, frameRenderedSync)));
455 needFrameRenderedTrigger = true;
459 DALI_LOG_ERROR("WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence is failed\n");
466 scene.GetFramePresentedCallback(callbacks);
467 if(!callbacks.empty())
469 int framePresentedSync = mWindowBase->CreateFramePresentedSyncFence();
470 if(framePresentedSync != -1)
472 Dali::Mutex::ScopedLock lock(mMutex);
474 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: CreateFramePresentedSyncFence [%d]\n", framePresentedSync);
476 mFrameCallbackInfoContainer.push_back(std::unique_ptr<FrameCallbackInfo>(new FrameCallbackInfo(callbacks, framePresentedSync)));
478 needFrameRenderedTrigger = true;
482 DALI_LOG_ERROR("WindowRenderSurface::PreRender: CreateFramePresentedSyncFence is failed\n");
489 if(needFrameRenderedTrigger)
491 if(!mFrameRenderedTrigger)
493 mFrameRenderedTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessFrameCallback),
494 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
496 mFrameRenderedTrigger->Trigger();
501 * wl_egl_window_tizen_set_rotation(SetEglWindowRotation) -> PreRotation
502 * wl_egl_window_tizen_set_buffer_transform(SetEglWindowBufferTransform) -> Screen Rotation
503 * wl_egl_window_tizen_set_window_transform(SetEglWindowTransform) -> Window Rotation
504 * These function should be called before calling first drawing gl Function.
505 * Notice : PreRotation is not used in the latest tizen,
506 * because output transform event should be occured before egl window is not created.
509 if(mIsResizing || mDefaultScreenRotationAvailable)
511 int totalAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
513 // Window rotate or screen rotate
514 if(!mWindowRotationFinished || !mScreenRotationFinished)
516 mWindowBase->SetEglWindowBufferTransform(totalAngle);
518 // Reset only screen rotation flag
519 mScreenRotationFinished = true;
521 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: Set rotation [%d] [%d]\n", mWindowRotationAngle, mScreenRotationAngle);
524 // Only window rotate
525 if(!mWindowRotationFinished)
527 mWindowBase->SetEglWindowTransform(mWindowRotationAngle);
533 Dali::PositionSize positionSize;
534 positionSize.x = mPositionSize.x;
535 positionSize.y = mPositionSize.y;
536 if(totalAngle == 0 || totalAngle == 180)
538 positionSize.width = mPositionSize.width;
539 positionSize.height = mPositionSize.height;
543 positionSize.width = mPositionSize.height;
544 positionSize.height = mPositionSize.width;
546 mWindowBase->ResizeEglWindow(positionSize);
547 mResizeFinished = true;
549 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);
552 SetFullSwapNextFrame();
553 mDefaultScreenRotationAvailable = false;
556 SetBufferDamagedRects(mDamagedRects, clippingRect);
560 Rect<int> surfaceRect = scene.GetCurrentSurfaceRect();
561 if(clippingRect == surfaceRect)
563 mDamagedRects.assign(1, surfaceRect);
565 else if(mDamagedRects.empty() && !clippingRect.IsEmpty())
567 // We will render clippingRect area but mDamagedRects is empty.
568 // So make mDamagedRects same with clippingRect to swap buffers.
569 mDamagedRects.assign(1, clippingRect);
573 // This is now done when the render pass for the render surface begins
574 // MakeContextCurrent();
579 void WindowRenderSurface::PostRender()
581 // Inform the gl implementation that rendering has finished before informing the surface
582 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
585 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
588 bool needWindowRotationCompleted = false;
590 if(!mWindowRotationFinished)
592 if(mNeedWindowRotationAcknowledgement)
594 Dali::Integration::Scene scene = mScene.GetHandle();
597 if(scene.IsRotationCompletedAcknowledgementSet())
599 needWindowRotationCompleted = true;
607 needWindowRotationCompleted = true;
612 if(needWindowRotationCompleted || mIsImeWindowSurface)
614 if(mThreadSynchronization)
616 // Enable PostRender flag
617 mThreadSynchronization->PostRenderStarted();
620 if(!mWindowRotationFinished || mIsImeWindowSurface)
622 mPostRenderTrigger->Trigger();
625 if(mThreadSynchronization)
627 // Wait until the event-thread complete the rotation event processing
628 mThreadSynchronization->PostRenderWaitForCompletion();
632 SwapBuffers(mDamagedRects);
634 if(mRenderNotification)
636 mRenderNotification->Trigger();
641 void WindowRenderSurface::StopRender()
645 void WindowRenderSurface::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
647 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n");
649 mThreadSynchronization = &threadSynchronization;
652 void WindowRenderSurface::ReleaseLock()
657 Dali::RenderSurfaceInterface::Type WindowRenderSurface::GetSurfaceType()
659 return Dali::RenderSurfaceInterface::WINDOW_RENDER_SURFACE;
662 void WindowRenderSurface::MakeContextCurrent()
666 mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
670 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
672 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
675 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
677 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
680 void WindowRenderSurface::InitializeImeSurface()
682 mIsImeWindowSurface = true;
683 if(!mPostRenderTrigger)
685 mPostRenderTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessPostRender),
686 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
690 void WindowRenderSurface::SetNeedsRotationCompletedAcknowledgement(bool needAcknowledgement)
692 mNeedWindowRotationAcknowledgement = needAcknowledgement;
695 void WindowRenderSurface::OutputTransformed()
697 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
699 if(mScreenRotationAngle != screenRotationAngle)
701 mScreenRotationAngle = screenRotationAngle;
702 mScreenRotationFinished = false;
703 mResizeFinished = false;
705 mOutputTransformedSignal.Emit();
707 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: window = %d screen = %d\n", mWindowRotationAngle, mScreenRotationAngle);
711 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle);
715 void WindowRenderSurface::ProcessPostRender()
717 if(!mWindowRotationFinished)
719 mWindowBase->WindowRotationCompleted(mWindowRotationAngle, mPositionSize.width, mPositionSize.height);
720 DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessPostRender: Rotation Done\n");
721 mWindowRotationFinished = true;
724 if(mIsImeWindowSurface)
726 mWindowBase->ImeWindowReadyToRender();
729 if(mThreadSynchronization)
731 mThreadSynchronization->PostRenderComplete();
735 void WindowRenderSurface::ProcessFrameCallback()
737 Dali::Mutex::ScopedLock lock(mMutex);
739 for(auto&& iter : mFrameCallbackInfoContainer)
741 if(!iter->fileDescriptorMonitor)
743 iter->fileDescriptorMonitor = std::unique_ptr<FileDescriptorMonitor>(new FileDescriptorMonitor(iter->fileDescriptor,
744 MakeCallback(this, &WindowRenderSurface::OnFileDescriptorEventDispatched),
745 FileDescriptorMonitor::FD_READABLE));
747 DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessFrameCallback: Add handler [%d]\n", iter->fileDescriptor);
752 void WindowRenderSurface::OnFileDescriptorEventDispatched(FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor)
754 if(!(eventBitMask & FileDescriptorMonitor::FD_READABLE))
756 DALI_LOG_ERROR("WindowRenderSurface::OnFileDescriptorEventDispatched: file descriptor error [%d]\n", eventBitMask);
757 close(fileDescriptor);
761 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OnFileDescriptorEventDispatched: Frame rendered [%d]\n", fileDescriptor);
763 std::unique_ptr<FrameCallbackInfo> callbackInfo;
765 Dali::Mutex::ScopedLock lock(mMutex);
766 auto frameCallbackInfo = std::find_if(mFrameCallbackInfoContainer.begin(), mFrameCallbackInfoContainer.end(), [fileDescriptor](std::unique_ptr<FrameCallbackInfo>& callbackInfo) {
767 return callbackInfo->fileDescriptor == fileDescriptor;
769 if(frameCallbackInfo != mFrameCallbackInfoContainer.end())
771 callbackInfo = std::move(*frameCallbackInfo);
773 mFrameCallbackInfoContainer.erase(frameCallbackInfo);
777 // Call the connected callback
780 for(auto&& iter : (callbackInfo)->callbacks)
782 CallbackBase::Execute(*(iter.first), iter.second);
787 void WindowRenderSurface::SetBufferDamagedRects(const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
789 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
792 // If scene is not exist, just use stored mPositionSize.
793 Rect<int> surfaceRect(0, 0, mPositionSize.width, mPositionSize.height);
795 Dali::Integration::Scene scene = mScene.GetHandle();
798 surfaceRect = scene.GetCurrentSurfaceRect();
801 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
802 if(!eglImpl.IsPartialUpdateRequired() || mFullSwapNextFrame)
804 InsertRects(mBufferDamagedRects, std::vector<Rect<int>>(1, surfaceRect));
805 clippingRect = surfaceRect;
809 mGraphics->ActivateSurfaceContext(this);
811 EGLint bufferAge = eglImpl.GetBufferAge(mEGLSurface);
813 // Buffer age 0 means the back buffer in invalid and requires full swap
816 InsertRects(mBufferDamagedRects, std::vector<Rect<int>>(1, surfaceRect));
817 clippingRect = surfaceRect;
821 // We push current frame damaged rects here, zero index for current frame
822 InsertRects(mBufferDamagedRects, damagedRects);
824 // Merge damaged rects into clipping rect
825 auto bufferDamagedRects = mBufferDamagedRects.begin();
826 while(bufferAge-- >= 0 && bufferDamagedRects != mBufferDamagedRects.end())
828 const std::vector<Rect<int>>& rects = *bufferDamagedRects++;
829 MergeRects(clippingRect, rects);
832 if(!clippingRect.Intersect(surfaceRect) || clippingRect.Area() > surfaceRect.Area() * FULL_UPDATE_RATIO)
834 // clipping area too big or doesn't intersect surface rect
835 clippingRect = surfaceRect;
839 if(!clippingRect.IsEmpty())
841 std::vector<Rect<int>> damagedRegion;
844 damagedRegion.push_back(RecalculateRect[std::min(scene.GetCurrentSurfaceOrientation() / 90, 3)](clippingRect, scene.GetCurrentSurfaceRect()));
848 damagedRegion.push_back(clippingRect);
851 eglImpl.SetDamageRegion(mEGLSurface, damagedRegion);
856 void WindowRenderSurface::SwapBuffers(const std::vector<Rect<int>>& damagedRects)
858 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
861 Rect<int32_t> surfaceRect;
862 int32_t orientation = 0;
864 Dali::Integration::Scene scene = mScene.GetHandle();
867 surfaceRect = scene.GetCurrentSurfaceRect();
868 orientation = std::min(scene.GetCurrentSurfaceOrientation() / 90, 3);
871 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
873 if(!eglImpl.IsPartialUpdateRequired() || mFullSwapNextFrame || (damagedRects.size() != 0 && damagedRects[0].Area() > surfaceRect.Area() * FULL_UPDATE_RATIO))
875 mFullSwapNextFrame = false;
876 eglImpl.SwapBuffers(mEGLSurface);
880 mFullSwapNextFrame = false;
882 std::vector<Rect<int>> mergedRects = damagedRects;
884 // Merge intersecting rects, form an array of non intersecting rects to help driver a bit
885 // Could be optional and can be removed, needs to be checked with and without on platform
886 const int n = mergedRects.size();
887 for(int i = 0; i < n - 1; i++)
889 if(mergedRects[i].IsEmpty())
894 for(int j = i + 1; j < n; j++)
896 if(mergedRects[j].IsEmpty())
901 if(mergedRects[i].Intersects(mergedRects[j]))
903 mergedRects[i].Merge(mergedRects[j]);
904 mergedRects[j].width = 0;
905 mergedRects[j].height = 0;
911 for(int i = 0; i < n; i++)
913 if(!mergedRects[i].IsEmpty())
915 mergedRects[j++] = RecalculateRect[orientation](mergedRects[i], surfaceRect);
921 mergedRects.resize(j);
924 if(!mergedRects.size() || (mergedRects[0].Area() > surfaceRect.Area() * FULL_UPDATE_RATIO))
926 // In normal cases, WindowRenderSurface::SwapBuffers() will not be called if mergedRects.size() is 0.
927 // For exceptional cases, swap full area.
928 eglImpl.SwapBuffers(mEGLSurface);
932 eglImpl.SwapBuffers(mEGLSurface, mergedRects);
937 } // namespace Adaptor
939 } // namespace Internal