2 * Copyright (c) 2022 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 InsertRects(WindowRenderSurface::DamagedRectsContainer& damagedRectsList, const Rect<int>& damagedRects)
54 damagedRectsList.insert(damagedRectsList.begin(), damagedRects);
55 if(damagedRectsList.size() > 4) // past triple buffers + current
57 damagedRectsList.pop_back();
61 Rect<int32_t> RecalculateRect0(Rect<int32_t>& rect, const Rect<int32_t>& surfaceSize)
66 Rect<int32_t> RecalculateRect90(Rect<int32_t>& rect, const Rect<int32_t>& surfaceSize)
68 Rect<int32_t> newRect;
69 newRect.x = surfaceSize.height - (rect.y + rect.height);
71 newRect.width = rect.height;
72 newRect.height = rect.width;
76 Rect<int32_t> RecalculateRect180(Rect<int32_t>& rect, const Rect<int32_t>& surfaceSize)
78 Rect<int32_t> newRect;
79 newRect.x = surfaceSize.width - (rect.x + rect.width);
80 newRect.y = surfaceSize.height - (rect.y + rect.height);
81 newRect.width = rect.width;
82 newRect.height = rect.height;
86 Rect<int32_t> RecalculateRect270(Rect<int32_t>& rect, const Rect<int32_t>& surfaceSize)
88 Rect<int32_t> newRect;
90 newRect.y = surfaceSize.width - (rect.x + rect.width);
91 newRect.width = rect.height;
92 newRect.height = rect.width;
96 using RecalculateRectFunction = Rect<int32_t> (*)(Rect<int32_t>&, const Rect<int32_t>&);
98 RecalculateRectFunction RecalculateRect[4] = {RecalculateRect0, RecalculateRect90, RecalculateRect180, RecalculateRect270};
100 void MergeIntersectingRectsAndRotate(Rect<int>& mergingRect, std::vector<Rect<int>>& damagedRects, int orientation, const Rect<int32_t>& surfaceRect)
102 const int n = damagedRects.size();
103 for(int i = 0; i < n - 1; i++)
105 if(damagedRects[i].IsEmpty())
110 for(int j = i + 1; j < n; j++)
112 if(damagedRects[j].IsEmpty())
117 if(damagedRects[i].Intersects(damagedRects[j]))
119 damagedRects[i].Merge(damagedRects[j]);
120 damagedRects[j].width = 0;
121 damagedRects[j].height = 0;
127 for(int i = 0; i < n; i++)
129 if(!damagedRects[i].IsEmpty())
131 // Merge rects before rotate
132 if(mergingRect.IsEmpty())
134 mergingRect = damagedRects[i];
138 mergingRect.Merge(damagedRects[i]);
141 damagedRects[j++] = RecalculateRect[orientation](damagedRects[i], surfaceRect);
147 damagedRects.resize(j);
151 } // unnamed namespace
153 WindowRenderSurface::WindowRenderSurface(Dali::PositionSize positionSize, Any surface, bool isTransparent)
155 mDisplayConnection(nullptr),
156 mPositionSize(positionSize),
158 mThreadSynchronization(nullptr),
159 mRenderNotification(nullptr),
160 mPostRenderTrigger(),
161 mFrameRenderedTrigger(),
163 mEGLSurface(nullptr),
164 mEGLContext(nullptr),
165 mColorDepth(isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24),
166 mOutputTransformedSignal(),
167 mWindowRotationFinishedSignal(),
168 mFrameCallbackInfoContainer(),
169 mBufferDamagedRects(),
171 mWindowRotationAngle(0),
172 mScreenRotationAngle(0),
176 mIsImeWindowSurface(false),
177 mNeedWindowRotationAcknowledgement(false),
178 mIsWindowOrientationChanging(false)
180 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n");
184 WindowRenderSurface::~WindowRenderSurface()
188 void WindowRenderSurface::Initialize(Any surface)
190 // If width or height are zero, go full screen.
191 if((mPositionSize.width == 0) || (mPositionSize.height == 0))
193 // Default window size == screen size
196 WindowSystem::GetScreenSize(mPositionSize.width, mPositionSize.height);
199 // Create a window base
200 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
201 mWindowBase = windowFactory->CreateWindowBase(mPositionSize, surface, (mColorDepth == COLOR_DEPTH_32 ? true : false));
204 mWindowBase->OutputTransformedSignal().Connect(this, &WindowRenderSurface::OutputTransformed);
206 // Check screen rotation
207 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
208 if(screenRotationAngle != 0)
211 DALI_LOG_RELEASE_INFO("WindowRenderSurface::Initialize, screen rotation is enabled, screen rotation angle:[%d]\n", screenRotationAngle);
215 Any WindowRenderSurface::GetNativeWindow()
217 return mWindowBase->GetNativeWindow();
220 int WindowRenderSurface::GetNativeWindowId()
222 return mWindowBase->GetNativeWindowId();
225 void WindowRenderSurface::Map()
230 void WindowRenderSurface::SetRenderNotification(TriggerEventInterface* renderNotification)
232 mRenderNotification = renderNotification;
235 void WindowRenderSurface::SetTransparency(bool transparent)
237 mWindowBase->SetTransparency(transparent);
240 void WindowRenderSurface::RequestRotation(int angle, PositionSize positionSize)
242 if(!mPostRenderTrigger)
244 mPostRenderTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessPostRender),
245 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
248 mPositionSize.x = positionSize.x;
249 mPositionSize.y = positionSize.y;
251 mWindowBase->SetWindowRotationAngle(angle);
253 DALI_LOG_RELEASE_INFO("start window rotation angle = %d screen rotation = %d\n", angle, mScreenRotationAngle);
256 WindowBase* WindowRenderSurface::GetWindowBase()
258 return mWindowBase.get();
261 WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
263 return mOutputTransformedSignal;
266 WindowRenderSurface::RotationFinishedSignalType& WindowRenderSurface::RotationFinishedSignal()
268 return mWindowRotationFinishedSignal;
271 PositionSize WindowRenderSurface::GetPositionSize() const
273 return mPositionSize;
276 void WindowRenderSurface::GetDpi(unsigned int& dpiHorizontal, unsigned int& dpiVertical)
278 if(mDpiHorizontal == 0 || mDpiVertical == 0)
280 const char* environmentDpiHorizontal = std::getenv(DALI_ENV_DPI_HORIZONTAL);
281 mDpiHorizontal = environmentDpiHorizontal ? std::atoi(environmentDpiHorizontal) : 0;
283 const char* environmentDpiVertical = std::getenv(DALI_ENV_DPI_VERTICAL);
284 mDpiVertical = environmentDpiVertical ? std::atoi(environmentDpiVertical) : 0;
286 if(mDpiHorizontal == 0 || mDpiVertical == 0)
288 mWindowBase->GetDpi(mDpiHorizontal, mDpiVertical);
292 dpiHorizontal = mDpiHorizontal;
293 dpiVertical = mDpiVertical;
296 int WindowRenderSurface::GetSurfaceOrientation() const
298 return mWindowBase->GetWindowRotationAngle();
301 int WindowRenderSurface::GetScreenOrientation() const
303 return mWindowBase->GetScreenRotationAngle();
306 void WindowRenderSurface::InitializeGraphics()
308 if(mEGLContext == NULL)
310 mGraphics = &mAdaptor->GetGraphicsInterface();
312 DALI_ASSERT_ALWAYS(mGraphics && "Graphics interface is not created");
314 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
315 mEGL = &eglGraphics->GetEglInterface();
317 // Create the OpenGL context for this window
318 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
319 eglImpl.ChooseConfig(true, mColorDepth);
320 eglImpl.CreateWindowContext(mEGLContext);
322 // Create the OpenGL surface
327 void WindowRenderSurface::CreateSurface()
329 DALI_LOG_TRACE_METHOD(gWindowRenderSurfaceLogFilter);
332 if(mScreenRotationAngle == 0 || mScreenRotationAngle == 180)
334 width = mPositionSize.width;
335 height = mPositionSize.height;
339 width = mPositionSize.height;
340 height = mPositionSize.width;
343 // Create the EGL window
344 EGLNativeWindowType window = mWindowBase->CreateEglWindow(width, height);
346 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
348 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
349 mEGLSurface = eglImpl.CreateSurfaceWindow(window, mColorDepth);
351 DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: WinId (%d), EGLSurface (%p), w = %d h = %d angle = %d screen rotation = %d\n",
352 mWindowBase->GetNativeWindowId(),
355 mPositionSize.height,
356 mWindowRotationAngle,
357 mScreenRotationAngle);
360 void WindowRenderSurface::DestroySurface()
362 DALI_LOG_TRACE_METHOD(gWindowRenderSurfaceLogFilter);
364 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
367 DALI_LOG_RELEASE_INFO("WindowRenderSurface::DestroySurface: WinId (%d)\n", mWindowBase->GetNativeWindowId());
369 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
371 eglImpl.DestroySurface(mEGLSurface);
372 mEGLSurface = nullptr;
374 // Destroy context also
375 eglImpl.DestroyContext(mEGLContext);
376 mEGLContext = nullptr;
378 mWindowBase->DestroyEglWindow();
382 bool WindowRenderSurface::ReplaceGraphicsSurface()
384 DALI_LOG_TRACE_METHOD(gWindowRenderSurfaceLogFilter);
386 // Destroy the old one
387 mWindowBase->DestroyEglWindow();
390 if(mScreenRotationAngle == 0 || mScreenRotationAngle == 180)
392 width = mPositionSize.width;
393 height = mPositionSize.height;
397 width = mPositionSize.height;
398 height = mPositionSize.width;
401 // Create the EGL window
402 EGLNativeWindowType window = mWindowBase->CreateEglWindow(width, height);
404 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
406 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
407 return eglImpl.ReplaceSurfaceWindow(window, mEGLSurface, mEGLContext);
410 void WindowRenderSurface::UpdatePositionSize(Dali::PositionSize positionSize)
413 if((fabs(positionSize.x - mPositionSize.x) >= MINIMUM_DIMENSION_CHANGE) ||
414 (fabs(positionSize.y - mPositionSize.y) >= MINIMUM_DIMENSION_CHANGE))
416 mPositionSize.x = positionSize.x;
417 mPositionSize.y = positionSize.y;
419 DALI_LOG_RELEASE_INFO("Update Position by server (%d, %d)\n", mPositionSize.x, mPositionSize.y);
423 void WindowRenderSurface::MoveResize(Dali::PositionSize positionSize)
425 mPositionSize.x = positionSize.x;
426 mPositionSize.y = positionSize.y;
428 DALI_LOG_RELEASE_INFO("Update Position by client (%d, %d)\n", positionSize.x, positionSize.y);
430 mWindowBase->MoveResize(positionSize);
433 void WindowRenderSurface::StartRender()
437 bool WindowRenderSurface::PreRender(bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
439 InitializeGraphics();
441 Dali::Integration::Scene::FrameCallbackContainer callbacks;
443 Dali::Integration::Scene scene = mScene.GetHandle();
447 bool needFrameRenderedTrigger = false;
449 scene.GetFrameRenderedCallback(callbacks);
450 if(!callbacks.empty())
452 int frameRenderedSync = mWindowBase->CreateFrameRenderedSyncFence();
453 if(frameRenderedSync != -1)
455 Dali::Mutex::ScopedLock lock(mMutex);
457 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence [%d]\n", frameRenderedSync);
459 mFrameCallbackInfoContainer.push_back(std::unique_ptr<FrameCallbackInfo>(new FrameCallbackInfo(callbacks, frameRenderedSync)));
461 needFrameRenderedTrigger = true;
465 DALI_LOG_ERROR("WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence is failed\n");
472 scene.GetFramePresentedCallback(callbacks);
473 if(!callbacks.empty())
475 int framePresentedSync = mWindowBase->CreateFramePresentedSyncFence();
476 if(framePresentedSync != -1)
478 Dali::Mutex::ScopedLock lock(mMutex);
480 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: CreateFramePresentedSyncFence [%d]\n", framePresentedSync);
482 mFrameCallbackInfoContainer.push_back(std::unique_ptr<FrameCallbackInfo>(new FrameCallbackInfo(callbacks, framePresentedSync)));
484 needFrameRenderedTrigger = true;
488 DALI_LOG_ERROR("WindowRenderSurface::PreRender: CreateFramePresentedSyncFence is failed\n");
495 if(needFrameRenderedTrigger)
497 if(!mFrameRenderedTrigger)
499 mFrameRenderedTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessFrameCallback),
500 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
502 mFrameRenderedTrigger->Trigger();
507 * wl_egl_window_tizen_set_rotation(SetEglWindowRotation) -> PreRotation
508 * wl_egl_window_tizen_set_buffer_transform(SetEglWindowBufferTransform) -> Screen Rotation
509 * wl_egl_window_tizen_set_window_transform(SetEglWindowTransform) -> Window Rotation
510 * These function should be called before calling first drawing gl Function.
511 * Notice : PreRotation is not used in the latest tizen,
512 * because output transform event should be occured before egl window is not created.
514 if(scene && resizingSurface)
517 bool isScreenOrientationChanging = false;
519 if(mWindowRotationAngle != scene.GetCurrentSurfaceOrientation())
521 mWindowRotationAngle = scene.GetCurrentSurfaceOrientation();
522 mIsWindowOrientationChanging = true;
525 if(mScreenRotationAngle != scene.GetCurrentScreenOrientation())
527 mScreenRotationAngle = scene.GetCurrentScreenOrientation();
528 isScreenOrientationChanging = true;
530 totalAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
532 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);
534 Rect<int> surfaceSize = scene.GetCurrentSurfaceRect();
535 //update surface size
536 mPositionSize.width = surfaceSize.width;
537 mPositionSize.height = surfaceSize.height;
539 DALI_LOG_RELEASE_INFO("Window is resizing, (%d, %d), [%d x %d]\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height);
541 // Window rotate or screen rotate
542 if(mIsWindowOrientationChanging || isScreenOrientationChanging)
544 mWindowBase->SetEglWindowBufferTransform(totalAngle);
547 // Only window rotate
548 if(mIsWindowOrientationChanging)
550 mWindowBase->SetEglWindowTransform(mWindowRotationAngle);
554 Dali::PositionSize positionSize;
556 // Some native resize API(wl_egl_window_resize) has the input parameters of x, y, width and height.
557 // So, position data should be set.
558 positionSize.x = mPositionSize.x;
559 positionSize.y = mPositionSize.y;
560 if(totalAngle == 0 || totalAngle == 180)
562 positionSize.width = mPositionSize.width;
563 positionSize.height = mPositionSize.height;
567 positionSize.width = mPositionSize.height;
568 positionSize.height = mPositionSize.width;
571 mWindowBase->ResizeEglWindow(positionSize);
573 SetFullSwapNextFrame();
576 SetBufferDamagedRects(damagedRects, clippingRect);
580 Rect<int> surfaceRect = scene.GetCurrentSurfaceRect();
581 if(clippingRect == surfaceRect)
583 int32_t totalAngle = scene.GetCurrentSurfaceOrientation() + scene.GetCurrentScreenOrientation();
584 if(totalAngle >= 360)
588 mDamagedRects.assign(1, RecalculateRect[std::min(totalAngle / 90, 3)](surfaceRect, surfaceRect));
592 // This is now done when the render pass for the render surface begins
593 // MakeContextCurrent();
598 void WindowRenderSurface::PostRender()
600 // Inform the gl implementation that rendering has finished before informing the surface
601 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
604 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
607 bool needWindowRotationCompleted = false;
609 if(mIsWindowOrientationChanging)
611 if(mNeedWindowRotationAcknowledgement)
613 Dali::Integration::Scene scene = mScene.GetHandle();
616 if(scene.IsRotationCompletedAcknowledgementSet())
618 needWindowRotationCompleted = true;
624 needWindowRotationCompleted = true;
628 if(needWindowRotationCompleted || mIsImeWindowSurface)
630 if(mThreadSynchronization)
632 // Enable PostRender flag
633 mThreadSynchronization->PostRenderStarted();
636 if(mIsWindowOrientationChanging || mIsImeWindowSurface)
638 mPostRenderTrigger->Trigger();
641 if(mThreadSynchronization)
643 // Wait until the event-thread complete the rotation event processing
644 mThreadSynchronization->PostRenderWaitForCompletion();
648 SwapBuffers(mDamagedRects);
650 if(mRenderNotification)
652 mRenderNotification->Trigger();
657 void WindowRenderSurface::StopRender()
661 void WindowRenderSurface::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
663 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n");
665 mThreadSynchronization = &threadSynchronization;
668 void WindowRenderSurface::ReleaseLock()
673 Dali::RenderSurfaceInterface::Type WindowRenderSurface::GetSurfaceType()
675 return Dali::RenderSurfaceInterface::WINDOW_RENDER_SURFACE;
678 void WindowRenderSurface::MakeContextCurrent()
682 mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
686 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
688 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
691 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
693 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
696 void WindowRenderSurface::InitializeImeSurface()
698 mIsImeWindowSurface = true;
699 if(!mPostRenderTrigger)
701 mPostRenderTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessPostRender),
702 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
706 void WindowRenderSurface::SetNeedsRotationCompletedAcknowledgement(bool needAcknowledgement)
708 mNeedWindowRotationAcknowledgement = needAcknowledgement;
711 void WindowRenderSurface::OutputTransformed()
713 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
715 if(mScreenRotationAngle != screenRotationAngle)
717 mOutputTransformedSignal.Emit();
719 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: window = %d new screen angle = %d\n", mWindowRotationAngle, screenRotationAngle);
723 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle);
727 void WindowRenderSurface::ProcessPostRender()
729 if(mIsWindowOrientationChanging)
731 mWindowRotationFinishedSignal.Emit();
732 mWindowBase->WindowRotationCompleted(mWindowRotationAngle, mPositionSize.width, mPositionSize.height);
733 mIsWindowOrientationChanging = false;
734 DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessPostRender: Rotation Done, flag = %d\n", mIsWindowOrientationChanging);
737 if(mIsImeWindowSurface)
739 mWindowBase->ImeWindowReadyToRender();
742 if(mThreadSynchronization)
744 mThreadSynchronization->PostRenderComplete();
748 void WindowRenderSurface::ProcessFrameCallback()
750 Dali::Mutex::ScopedLock lock(mMutex);
752 for(auto&& iter : mFrameCallbackInfoContainer)
754 if(!iter->fileDescriptorMonitor)
756 iter->fileDescriptorMonitor = std::unique_ptr<FileDescriptorMonitor>(new FileDescriptorMonitor(iter->fileDescriptor,
757 MakeCallback(this, &WindowRenderSurface::OnFileDescriptorEventDispatched),
758 FileDescriptorMonitor::FD_READABLE));
760 DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessFrameCallback: Add handler [%d]\n", iter->fileDescriptor);
765 void WindowRenderSurface::OnFileDescriptorEventDispatched(FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor)
767 if(!(eventBitMask & FileDescriptorMonitor::FD_READABLE))
769 DALI_LOG_ERROR("WindowRenderSurface::OnFileDescriptorEventDispatched: file descriptor error [%d]\n", eventBitMask);
770 close(fileDescriptor);
774 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OnFileDescriptorEventDispatched: Frame rendered [%d]\n", fileDescriptor);
776 std::unique_ptr<FrameCallbackInfo> callbackInfo;
778 Dali::Mutex::ScopedLock lock(mMutex);
779 auto frameCallbackInfo = std::find_if(mFrameCallbackInfoContainer.begin(), mFrameCallbackInfoContainer.end(), [fileDescriptor](std::unique_ptr<FrameCallbackInfo>& callbackInfo) {
780 return callbackInfo->fileDescriptor == fileDescriptor;
782 if(frameCallbackInfo != mFrameCallbackInfoContainer.end())
784 callbackInfo = std::move(*frameCallbackInfo);
786 mFrameCallbackInfoContainer.erase(frameCallbackInfo);
790 // Call the connected callback
793 for(auto&& iter : (callbackInfo)->callbacks)
795 CallbackBase::Execute(*(iter.first), iter.second);
800 void WindowRenderSurface::SetBufferDamagedRects(const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
802 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
805 // If scene is not exist, just use stored mPositionSize.
806 Rect<int> surfaceRect(0, 0, mPositionSize.width, mPositionSize.height);
807 int32_t orientation = 0;
809 Dali::Integration::Scene scene = mScene.GetHandle();
812 surfaceRect = scene.GetCurrentSurfaceRect();
813 int32_t totalAngle = scene.GetCurrentSurfaceOrientation() + scene.GetCurrentScreenOrientation();
814 if(totalAngle >= 360)
818 orientation = std::min(totalAngle / 90, 3);
821 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
822 if(!eglImpl.IsPartialUpdateRequired() || mFullSwapNextFrame)
824 InsertRects(mBufferDamagedRects, surfaceRect);
825 clippingRect = surfaceRect;
829 if(damagedRects.empty())
831 // Empty damaged rect. We don't need rendering
832 clippingRect = Rect<int>();
833 // Clean up current damanged rects.
834 mDamagedRects.clear();
838 mGraphics->ActivateSurfaceContext(this);
840 EGLint bufferAge = eglImpl.GetBufferAge(mEGLSurface);
842 // Buffer age 0 means the back buffer in invalid and requires full swap
845 InsertRects(mBufferDamagedRects, surfaceRect);
846 clippingRect = surfaceRect;
850 mDamagedRects.assign(damagedRects.begin(), damagedRects.end());
852 // Merge intersecting rects, form an array of non intersecting rects to help driver a bit
853 // Could be optional and can be removed, needs to be checked with and without on platform
854 // And then, Make one clipping rect, and rotate rects by orientation.
855 MergeIntersectingRectsAndRotate(clippingRect, mDamagedRects, orientation, surfaceRect);
857 // We push current frame damaged rects here, zero index for current frame
858 InsertRects(mBufferDamagedRects, clippingRect);
860 // Merge damaged rects into clipping rect
861 if(bufferAge <= static_cast<EGLint>(mBufferDamagedRects.size()))
863 // clippingRect is already the current frame's damaged rect. Merge from the second
864 for(int i = 1; i < bufferAge; i++)
866 clippingRect.Merge(mBufferDamagedRects[i]);
871 // The buffer age is too old. Need full update.
872 clippingRect = surfaceRect;
876 if(!clippingRect.Intersect(surfaceRect) || clippingRect.Area() > surfaceRect.Area() * FULL_UPDATE_RATIO)
878 // clipping area too big or doesn't intersect surface rect
879 clippingRect = surfaceRect;
883 if(!clippingRect.IsEmpty())
885 std::vector<Rect<int>> damagedRegion;
888 damagedRegion.push_back(RecalculateRect[orientation](clippingRect, surfaceRect));
892 damagedRegion.push_back(clippingRect);
895 eglImpl.SetDamageRegion(mEGLSurface, damagedRegion);
900 void WindowRenderSurface::SwapBuffers(const std::vector<Rect<int>>& damagedRects)
902 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
905 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
907 if(!eglImpl.IsPartialUpdateRequired() || mFullSwapNextFrame)
909 mFullSwapNextFrame = false;
910 eglImpl.SwapBuffers(mEGLSurface);
914 mFullSwapNextFrame = false;
916 Rect<int32_t> surfaceRect;
917 Dali::Integration::Scene scene = mScene.GetHandle();
920 surfaceRect = scene.GetCurrentSurfaceRect();
923 if(!damagedRects.size() || (damagedRects[0].Area() > surfaceRect.Area() * FULL_UPDATE_RATIO))
925 // In normal cases, WindowRenderSurface::SwapBuffers() will not be called if mergedRects.size() is 0.
926 // For exceptional cases, swap full area.
927 eglImpl.SwapBuffers(mEGLSurface);
931 eglImpl.SwapBuffers(mEGLSurface, damagedRects);
936 } // namespace Adaptor
938 } // namespace Internal