2 * Copyright (c) 2023 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/system/common/system-factory.h>
34 #include <dali/internal/window-system/common/window-base.h>
35 #include <dali/internal/window-system/common/window-factory.h>
36 #include <dali/internal/window-system/common/window-system.h>
46 const int MINIMUM_DIMENSION_CHANGE(1); ///< Minimum change for window to be considered to have moved
47 const float FULL_UPDATE_RATIO(0.8f); ///< Force full update when the dirty area is larget than this ratio
49 #if defined(DEBUG_ENABLED)
50 Debug::Filter* gWindowRenderSurfaceLogFilter = Debug::Filter::New(Debug::Verbose, false, "LOG_WINDOW_RENDER_SURFACE");
53 void InsertRects(WindowRenderSurface::DamagedRectsContainer& damagedRectsList, const Rect<int>& damagedRects)
55 damagedRectsList.insert(damagedRectsList.begin(), damagedRects);
56 if(damagedRectsList.size() > 4) // past triple buffers + current
58 damagedRectsList.pop_back();
62 Rect<int32_t> RecalculateRect0(Rect<int32_t>& rect, const Rect<int32_t>& surfaceSize)
67 Rect<int32_t> RecalculateRect90(Rect<int32_t>& rect, const Rect<int32_t>& surfaceSize)
69 Rect<int32_t> newRect;
70 newRect.x = surfaceSize.height - (rect.y + rect.height);
72 newRect.width = rect.height;
73 newRect.height = rect.width;
77 Rect<int32_t> RecalculateRect180(Rect<int32_t>& rect, const Rect<int32_t>& surfaceSize)
79 Rect<int32_t> newRect;
80 newRect.x = surfaceSize.width - (rect.x + rect.width);
81 newRect.y = surfaceSize.height - (rect.y + rect.height);
82 newRect.width = rect.width;
83 newRect.height = rect.height;
87 Rect<int32_t> RecalculateRect270(Rect<int32_t>& rect, const Rect<int32_t>& surfaceSize)
89 Rect<int32_t> newRect;
91 newRect.y = surfaceSize.width - (rect.x + rect.width);
92 newRect.width = rect.height;
93 newRect.height = rect.width;
97 using RecalculateRectFunction = Rect<int32_t> (*)(Rect<int32_t>&, const Rect<int32_t>&);
99 RecalculateRectFunction RecalculateRect[4] = {RecalculateRect0, RecalculateRect90, RecalculateRect180, RecalculateRect270};
101 void MergeIntersectingRectsAndRotate(Rect<int>& mergingRect, std::vector<Rect<int>>& damagedRects, int orientation, const Rect<int32_t>& surfaceRect)
103 const int n = damagedRects.size();
104 for(int i = 0; i < n - 1; i++)
106 if(damagedRects[i].IsEmpty())
111 for(int j = i + 1; j < n; j++)
113 if(damagedRects[j].IsEmpty())
118 if(damagedRects[i].Intersects(damagedRects[j]))
120 damagedRects[i].Merge(damagedRects[j]);
121 damagedRects[j].width = 0;
122 damagedRects[j].height = 0;
128 for(int i = 0; i < n; i++)
130 if(!damagedRects[i].IsEmpty())
132 // Merge rects before rotate
133 if(mergingRect.IsEmpty())
135 mergingRect = damagedRects[i];
139 mergingRect.Merge(damagedRects[i]);
142 damagedRects[j++] = RecalculateRect[orientation](damagedRects[i], surfaceRect);
148 damagedRects.resize(j);
152 } // unnamed namespace
154 WindowRenderSurface::WindowRenderSurface(Dali::PositionSize positionSize, Any surface, bool isTransparent)
156 mDisplayConnection(nullptr),
157 mPositionSize(positionSize),
159 mThreadSynchronization(nullptr),
160 mRenderNotification(nullptr),
161 mPostRenderTrigger(),
162 mFrameRenderedTrigger(),
164 mEGLSurface(nullptr),
165 mEGLContext(nullptr),
166 mColorDepth(isTransparent ? COLOR_DEPTH_32 : COLOR_DEPTH_24),
167 mOutputTransformedSignal(),
168 mWindowRotationFinishedSignal(),
169 mFrameCallbackInfoContainer(),
170 mBufferDamagedRects(),
172 mWindowRotationAngle(0),
173 mScreenRotationAngle(0),
177 mIsImeWindowSurface(false),
178 mNeedWindowRotationAcknowledgement(false),
179 mIsWindowOrientationChanging(false)
181 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "Creating Window\n");
185 WindowRenderSurface::~WindowRenderSurface()
189 void WindowRenderSurface::Initialize(Any surface)
191 // If width or height are zero, go full screen.
192 if((mPositionSize.width == 0) || (mPositionSize.height == 0))
194 // Default window size == screen size
197 WindowSystem::GetScreenSize(mPositionSize.width, mPositionSize.height);
200 // Create a window base
201 auto windowFactory = Dali::Internal::Adaptor::GetWindowFactory();
202 mWindowBase = windowFactory->CreateWindowBase(mPositionSize, surface, (mColorDepth == COLOR_DEPTH_32 ? true : false));
205 mWindowBase->OutputTransformedSignal().Connect(this, &WindowRenderSurface::OutputTransformed);
207 // Check screen rotation
208 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
209 if(screenRotationAngle != 0)
212 DALI_LOG_RELEASE_INFO("WindowRenderSurface::Initialize, screen rotation is enabled, screen rotation angle:[%d]\n", screenRotationAngle);
216 Any WindowRenderSurface::GetNativeWindow()
218 return mWindowBase->GetNativeWindow();
221 int WindowRenderSurface::GetNativeWindowId()
223 return mWindowBase->GetNativeWindowId();
226 void WindowRenderSurface::Map()
231 void WindowRenderSurface::SetRenderNotification(TriggerEventInterface* renderNotification)
233 mRenderNotification = renderNotification;
236 void WindowRenderSurface::SetTransparency(bool transparent)
238 mWindowBase->SetTransparency(transparent);
241 void WindowRenderSurface::RequestRotation(int angle, PositionSize positionSize)
243 if(!mPostRenderTrigger)
245 mPostRenderTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessPostRender),
246 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
249 mPositionSize.x = positionSize.x;
250 mPositionSize.y = positionSize.y;
252 mWindowBase->SetWindowRotationAngle(angle);
254 DALI_LOG_RELEASE_INFO("start window rotation angle = %d screen rotation = %d\n", angle, mScreenRotationAngle);
257 WindowBase* WindowRenderSurface::GetWindowBase()
259 return mWindowBase.get();
262 WindowBase::OutputSignalType& WindowRenderSurface::OutputTransformedSignal()
264 return mOutputTransformedSignal;
267 WindowRenderSurface::RotationFinishedSignalType& WindowRenderSurface::RotationFinishedSignal()
269 return mWindowRotationFinishedSignal;
272 PositionSize WindowRenderSurface::GetPositionSize() const
274 return mPositionSize;
277 void WindowRenderSurface::GetDpi(unsigned int& dpiHorizontal, unsigned int& dpiVertical)
279 if(mDpiHorizontal == 0 || mDpiVertical == 0)
281 const char* environmentDpiHorizontal = std::getenv(DALI_ENV_DPI_HORIZONTAL);
282 mDpiHorizontal = environmentDpiHorizontal ? std::atoi(environmentDpiHorizontal) : 0;
284 const char* environmentDpiVertical = std::getenv(DALI_ENV_DPI_VERTICAL);
285 mDpiVertical = environmentDpiVertical ? std::atoi(environmentDpiVertical) : 0;
287 if(mDpiHorizontal == 0 || mDpiVertical == 0)
289 mWindowBase->GetDpi(mDpiHorizontal, mDpiVertical);
293 dpiHorizontal = mDpiHorizontal;
294 dpiVertical = mDpiVertical;
297 int WindowRenderSurface::GetSurfaceOrientation() const
299 return mWindowBase->GetWindowRotationAngle();
302 int WindowRenderSurface::GetScreenOrientation() const
304 return mWindowBase->GetScreenRotationAngle();
307 void WindowRenderSurface::InitializeGraphics()
309 if(mEGLContext == NULL)
311 mGraphics = &mAdaptor->GetGraphicsInterface();
313 DALI_ASSERT_ALWAYS(mGraphics && "Graphics interface is not created");
315 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
316 mEGL = &eglGraphics->GetEglInterface();
318 // Create the OpenGL context for this window
319 Internal::Adaptor::EglImplementation& eglImpl = static_cast<Internal::Adaptor::EglImplementation&>(*mEGL);
320 eglImpl.ChooseConfig(true, mColorDepth);
321 eglImpl.CreateWindowContext(mEGLContext);
323 // Create the OpenGL surface
328 void WindowRenderSurface::CreateSurface()
330 DALI_LOG_TRACE_METHOD(gWindowRenderSurfaceLogFilter);
333 if(mScreenRotationAngle == 0 || mScreenRotationAngle == 180)
335 width = mPositionSize.width;
336 height = mPositionSize.height;
340 width = mPositionSize.height;
341 height = mPositionSize.width;
344 // Create the EGL window
345 EGLNativeWindowType window = mWindowBase->CreateEglWindow(width, height);
347 if(mWindowBase->GetType() == WindowType::IME)
349 InitializeImeSurface();
352 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
354 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
355 mEGLSurface = eglImpl.CreateSurfaceWindow(window, mColorDepth);
357 DALI_LOG_RELEASE_INFO("WindowRenderSurface::CreateSurface: WinId (%d), EGLSurface (%p), w = %d h = %d angle = %d screen rotation = %d\n",
358 mWindowBase->GetNativeWindowId(),
361 mPositionSize.height,
362 mWindowRotationAngle,
363 mScreenRotationAngle);
366 void WindowRenderSurface::DestroySurface()
368 DALI_LOG_TRACE_METHOD(gWindowRenderSurfaceLogFilter);
370 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
373 DALI_LOG_RELEASE_INFO("WindowRenderSurface::DestroySurface: WinId (%d)\n", mWindowBase->GetNativeWindowId());
375 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
377 eglImpl.DestroySurface(mEGLSurface);
378 mEGLSurface = nullptr;
380 // Destroy context also
381 eglImpl.DestroyContext(mEGLContext);
382 mEGLContext = nullptr;
384 mWindowBase->DestroyEglWindow();
388 bool WindowRenderSurface::ReplaceGraphicsSurface()
390 DALI_LOG_TRACE_METHOD(gWindowRenderSurfaceLogFilter);
392 // Destroy the old one
393 mWindowBase->DestroyEglWindow();
396 if(mScreenRotationAngle == 0 || mScreenRotationAngle == 180)
398 width = mPositionSize.width;
399 height = mPositionSize.height;
403 width = mPositionSize.height;
404 height = mPositionSize.width;
407 // Create the EGL window
408 EGLNativeWindowType window = mWindowBase->CreateEglWindow(width, height);
410 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
412 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
413 return eglImpl.ReplaceSurfaceWindow(window, mEGLSurface, mEGLContext);
416 void WindowRenderSurface::UpdatePositionSize(Dali::PositionSize positionSize)
419 if((fabs(positionSize.x - mPositionSize.x) >= MINIMUM_DIMENSION_CHANGE) ||
420 (fabs(positionSize.y - mPositionSize.y) >= MINIMUM_DIMENSION_CHANGE))
422 mPositionSize.x = positionSize.x;
423 mPositionSize.y = positionSize.y;
425 DALI_LOG_RELEASE_INFO("Update Position by server (%d, %d)\n", mPositionSize.x, mPositionSize.y);
429 void WindowRenderSurface::Move(Dali::PositionSize positionSize)
431 mPositionSize.x = positionSize.x;
432 mPositionSize.y = positionSize.y;
434 DALI_LOG_RELEASE_INFO("Update Position by client (%d, %d)\n", positionSize.x, positionSize.y);
436 mWindowBase->Move(positionSize);
439 void WindowRenderSurface::MoveResize(Dali::PositionSize positionSize)
441 mPositionSize.x = positionSize.x;
442 mPositionSize.y = positionSize.y;
444 DALI_LOG_RELEASE_INFO("Update Position by client (%d, %d)\n", positionSize.x, positionSize.y);
446 mWindowBase->MoveResize(positionSize);
449 void WindowRenderSurface::StartRender()
453 bool WindowRenderSurface::PreRender(bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
455 InitializeGraphics();
457 Dali::Integration::Scene::FrameCallbackContainer callbacks;
459 Dali::Integration::Scene scene = mScene.GetHandle();
463 bool needFrameRenderedTrigger = false;
465 scene.GetFrameRenderedCallback(callbacks);
466 if(!callbacks.empty())
468 int frameRenderedSync = mWindowBase->CreateFrameRenderedSyncFence();
469 if(frameRenderedSync != -1)
471 Dali::Mutex::ScopedLock lock(mMutex);
473 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence [%d]\n", frameRenderedSync);
475 mFrameCallbackInfoContainer.push_back(std::unique_ptr<FrameCallbackInfo>(new FrameCallbackInfo(callbacks, frameRenderedSync)));
477 needFrameRenderedTrigger = true;
481 DALI_LOG_ERROR("WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence is failed\n");
488 scene.GetFramePresentedCallback(callbacks);
489 if(!callbacks.empty())
491 int framePresentedSync = mWindowBase->CreateFramePresentedSyncFence();
492 if(framePresentedSync != -1)
494 Dali::Mutex::ScopedLock lock(mMutex);
496 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: CreateFramePresentedSyncFence [%d]\n", framePresentedSync);
498 mFrameCallbackInfoContainer.push_back(std::unique_ptr<FrameCallbackInfo>(new FrameCallbackInfo(callbacks, framePresentedSync)));
500 needFrameRenderedTrigger = true;
504 DALI_LOG_ERROR("WindowRenderSurface::PreRender: CreateFramePresentedSyncFence is failed\n");
511 if(needFrameRenderedTrigger)
513 if(!mFrameRenderedTrigger)
515 mFrameRenderedTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessFrameCallback),
516 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
518 mFrameRenderedTrigger->Trigger();
523 * wl_egl_window_tizen_set_rotation(SetEglWindowRotation) -> PreRotation
524 * wl_egl_window_tizen_set_buffer_transform(SetEglWindowBufferTransform) -> Screen Rotation
525 * wl_egl_window_tizen_set_window_transform(SetEglWindowTransform) -> Window Rotation
526 * These function should be called before calling first drawing gl Function.
527 * Notice : PreRotation is not used in the latest tizen,
528 * because output transform event should be occured before egl window is not created.
530 if(scene && resizingSurface)
533 bool isScreenOrientationChanging = false;
535 if(mWindowRotationAngle != scene.GetCurrentSurfaceOrientation())
537 mWindowRotationAngle = scene.GetCurrentSurfaceOrientation();
538 mIsWindowOrientationChanging = true;
541 if(mScreenRotationAngle != scene.GetCurrentScreenOrientation())
543 mScreenRotationAngle = scene.GetCurrentScreenOrientation();
544 isScreenOrientationChanging = true;
546 totalAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
548 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);
550 Rect<int> surfaceSize = scene.GetCurrentSurfaceRect();
551 //update surface size
552 mPositionSize.width = surfaceSize.width;
553 mPositionSize.height = surfaceSize.height;
555 DALI_LOG_RELEASE_INFO("Window is resizing, (%d, %d), [%d x %d], IMEWindow [%d]\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height, mIsImeWindowSurface);
557 // Window rotate or screen rotate
558 if(mIsWindowOrientationChanging || isScreenOrientationChanging)
560 mWindowBase->SetEglWindowBufferTransform(totalAngle);
563 // Only window rotate
564 if(mIsWindowOrientationChanging)
566 mWindowBase->SetEglWindowTransform(mWindowRotationAngle);
570 Dali::PositionSize positionSize;
572 // Some native resize API(wl_egl_window_resize) has the input parameters of x, y, width and height.
573 // So, position data should be set.
574 positionSize.x = mPositionSize.x;
575 positionSize.y = mPositionSize.y;
576 if(totalAngle == 0 || totalAngle == 180)
578 positionSize.width = mPositionSize.width;
579 positionSize.height = mPositionSize.height;
583 positionSize.width = mPositionSize.height;
584 positionSize.height = mPositionSize.width;
587 mWindowBase->ResizeEglWindow(positionSize);
589 SetFullSwapNextFrame();
592 SetBufferDamagedRects(damagedRects, clippingRect);
596 Rect<int> surfaceRect = scene.GetCurrentSurfaceRect();
597 if(clippingRect == surfaceRect)
599 int32_t totalAngle = scene.GetCurrentSurfaceOrientation() + scene.GetCurrentScreenOrientation();
600 if(totalAngle >= 360)
604 mDamagedRects.assign(1, RecalculateRect[std::min(totalAngle / 90, 3)](surfaceRect, surfaceRect));
608 // This is now done when the render pass for the render surface begins
609 // MakeContextCurrent();
614 void WindowRenderSurface::PostRender()
616 // Inform the gl implementation that rendering has finished before informing the surface
617 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
620 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
623 bool needWindowRotationCompleted = false;
625 if(mIsWindowOrientationChanging)
627 if(mNeedWindowRotationAcknowledgement)
629 Dali::Integration::Scene scene = mScene.GetHandle();
632 if(scene.IsRotationCompletedAcknowledgementSet())
634 needWindowRotationCompleted = true;
640 needWindowRotationCompleted = true;
644 if(needWindowRotationCompleted || mIsImeWindowSurface)
646 if(mThreadSynchronization)
648 // Enable PostRender flag
649 mThreadSynchronization->PostRenderStarted();
652 if(mIsWindowOrientationChanging || mIsImeWindowSurface)
654 mPostRenderTrigger->Trigger();
657 if(mThreadSynchronization)
659 // Wait until the event-thread complete the rotation event processing
660 mThreadSynchronization->PostRenderWaitForCompletion();
664 SwapBuffers(mDamagedRects);
666 if(mRenderNotification)
668 mRenderNotification->Trigger();
673 void WindowRenderSurface::StopRender()
677 void WindowRenderSurface::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
679 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n");
681 mThreadSynchronization = &threadSynchronization;
684 void WindowRenderSurface::ReleaseLock()
689 Dali::RenderSurfaceInterface::Type WindowRenderSurface::GetSurfaceType()
691 return Dali::RenderSurfaceInterface::WINDOW_RENDER_SURFACE;
694 void WindowRenderSurface::MakeContextCurrent()
698 mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
702 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
704 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
707 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
709 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
712 void WindowRenderSurface::InitializeImeSurface()
714 if(!mIsImeWindowSurface)
716 mIsImeWindowSurface = true;
717 if(!mPostRenderTrigger)
719 mPostRenderTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessPostRender),
720 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
726 void WindowRenderSurface::SetNeedsRotationCompletedAcknowledgement(bool needAcknowledgement)
728 mNeedWindowRotationAcknowledgement = needAcknowledgement;
731 void WindowRenderSurface::OutputTransformed()
733 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
735 if(mScreenRotationAngle != screenRotationAngle)
737 mOutputTransformedSignal.Emit();
739 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: window = %d new screen angle = %d\n", mWindowRotationAngle, screenRotationAngle);
743 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle);
747 void WindowRenderSurface::ProcessPostRender()
749 if(mIsWindowOrientationChanging)
751 mWindowRotationFinishedSignal.Emit();
752 mWindowBase->WindowRotationCompleted(mWindowRotationAngle, mPositionSize.width, mPositionSize.height);
753 mIsWindowOrientationChanging = false;
754 DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessPostRender: Rotation Done, flag = %d\n", mIsWindowOrientationChanging);
757 if(mIsImeWindowSurface)
759 mWindowBase->ImeWindowReadyToRender();
762 if(mThreadSynchronization)
764 mThreadSynchronization->PostRenderComplete();
768 void WindowRenderSurface::ProcessFrameCallback()
770 Dali::Mutex::ScopedLock lock(mMutex);
772 for(auto&& iter : mFrameCallbackInfoContainer)
774 if(!iter->fileDescriptorMonitor)
776 iter->fileDescriptorMonitor = Dali::Internal::Adaptor::GetSystemFactory()->CreateFileDescriptorMonitor(iter->fileDescriptor, MakeCallback(this, &WindowRenderSurface::OnFileDescriptorEventDispatched), FileDescriptorMonitor::FD_READABLE);
778 DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessFrameCallback: Add handler [%d]\n", iter->fileDescriptor);
783 void WindowRenderSurface::OnFileDescriptorEventDispatched(FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor)
785 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OnFileDescriptorEventDispatched: Frame rendered [%d]\n", fileDescriptor);
787 std::unique_ptr<FrameCallbackInfo> callbackInfo;
789 Dali::Mutex::ScopedLock lock(mMutex);
790 auto frameCallbackInfo = std::find_if(mFrameCallbackInfoContainer.begin(), mFrameCallbackInfoContainer.end(), [fileDescriptor](std::unique_ptr<FrameCallbackInfo>& callbackInfo) {
791 return callbackInfo->fileDescriptor == fileDescriptor;
793 if(frameCallbackInfo != mFrameCallbackInfoContainer.end())
795 callbackInfo = std::move(*frameCallbackInfo);
797 mFrameCallbackInfoContainer.erase(frameCallbackInfo);
801 // Call the connected callback
802 if(callbackInfo && (eventBitMask & FileDescriptorMonitor::FD_READABLE))
804 for(auto&& iter : (callbackInfo)->callbacks)
806 CallbackBase::Execute(*(iter.first), iter.second);
811 void WindowRenderSurface::SetBufferDamagedRects(const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
813 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
816 // If scene is not exist, just use stored mPositionSize.
817 Rect<int> surfaceRect(0, 0, mPositionSize.width, mPositionSize.height);
818 int32_t orientation = 0;
820 Dali::Integration::Scene scene = mScene.GetHandle();
823 surfaceRect = scene.GetCurrentSurfaceRect();
824 int32_t totalAngle = scene.GetCurrentSurfaceOrientation() + scene.GetCurrentScreenOrientation();
825 if(totalAngle >= 360)
829 orientation = std::min(totalAngle / 90, 3);
832 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
833 if(!eglImpl.IsPartialUpdateRequired() || mFullSwapNextFrame)
835 InsertRects(mBufferDamagedRects, surfaceRect);
836 clippingRect = surfaceRect;
840 if(damagedRects.empty())
842 // Empty damaged rect. We don't need rendering
843 clippingRect = Rect<int>();
844 // Clean up current damanged rects.
845 mDamagedRects.clear();
849 mGraphics->ActivateSurfaceContext(this);
851 EGLint bufferAge = eglImpl.GetBufferAge(mEGLSurface);
853 // Buffer age 0 means the back buffer in invalid and requires full swap
856 InsertRects(mBufferDamagedRects, surfaceRect);
857 clippingRect = surfaceRect;
861 mDamagedRects.assign(damagedRects.begin(), damagedRects.end());
863 // Merge intersecting rects, form an array of non intersecting rects to help driver a bit
864 // Could be optional and can be removed, needs to be checked with and without on platform
865 // And then, Make one clipping rect, and rotate rects by orientation.
866 MergeIntersectingRectsAndRotate(clippingRect, mDamagedRects, orientation, surfaceRect);
868 // We push current frame damaged rects here, zero index for current frame
869 InsertRects(mBufferDamagedRects, clippingRect);
871 // Merge damaged rects into clipping rect
872 if(bufferAge <= static_cast<EGLint>(mBufferDamagedRects.size()))
874 // clippingRect is already the current frame's damaged rect. Merge from the second
875 for(int i = 1; i < bufferAge; i++)
877 clippingRect.Merge(mBufferDamagedRects[i]);
882 // The buffer age is too old. Need full update.
883 clippingRect = surfaceRect;
887 if(!clippingRect.Intersect(surfaceRect) || clippingRect.Area() > surfaceRect.Area() * FULL_UPDATE_RATIO)
889 // clipping area too big or doesn't intersect surface rect
890 clippingRect = surfaceRect;
894 if(!clippingRect.IsEmpty())
896 std::vector<Rect<int>> damagedRegion;
899 damagedRegion.push_back(RecalculateRect[orientation](clippingRect, surfaceRect));
903 damagedRegion.push_back(clippingRect);
906 eglImpl.SetDamageRegion(mEGLSurface, damagedRegion);
911 void WindowRenderSurface::SwapBuffers(const std::vector<Rect<int>>& damagedRects)
913 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
916 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
918 if(!eglImpl.IsPartialUpdateRequired() || mFullSwapNextFrame)
920 mFullSwapNextFrame = false;
921 eglImpl.SwapBuffers(mEGLSurface);
925 mFullSwapNextFrame = false;
927 Rect<int32_t> surfaceRect;
928 Dali::Integration::Scene scene = mScene.GetHandle();
931 surfaceRect = scene.GetCurrentSurfaceRect();
934 if(!damagedRects.size() || (damagedRects[0].Area() > surfaceRect.Area() * FULL_UPDATE_RATIO))
936 // In normal cases, WindowRenderSurface::SwapBuffers() will not be called if mergedRects.size() is 0.
937 // For exceptional cases, swap full area.
938 eglImpl.SwapBuffers(mEGLSurface);
942 eglImpl.SwapBuffers(mEGLSurface, damagedRects);
947 } // namespace Adaptor
949 } // namespace Internal