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::Move(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->Move(positionSize);
433 void WindowRenderSurface::MoveResize(Dali::PositionSize positionSize)
435 mPositionSize.x = positionSize.x;
436 mPositionSize.y = positionSize.y;
438 DALI_LOG_RELEASE_INFO("Update Position by client (%d, %d)\n", positionSize.x, positionSize.y);
440 mWindowBase->MoveResize(positionSize);
443 void WindowRenderSurface::StartRender()
447 bool WindowRenderSurface::PreRender(bool resizingSurface, const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
449 InitializeGraphics();
451 Dali::Integration::Scene::FrameCallbackContainer callbacks;
453 Dali::Integration::Scene scene = mScene.GetHandle();
457 bool needFrameRenderedTrigger = false;
459 scene.GetFrameRenderedCallback(callbacks);
460 if(!callbacks.empty())
462 int frameRenderedSync = mWindowBase->CreateFrameRenderedSyncFence();
463 if(frameRenderedSync != -1)
465 Dali::Mutex::ScopedLock lock(mMutex);
467 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence [%d]\n", frameRenderedSync);
469 mFrameCallbackInfoContainer.push_back(std::unique_ptr<FrameCallbackInfo>(new FrameCallbackInfo(callbacks, frameRenderedSync)));
471 needFrameRenderedTrigger = true;
475 DALI_LOG_ERROR("WindowRenderSurface::PreRender: CreateFrameRenderedSyncFence is failed\n");
482 scene.GetFramePresentedCallback(callbacks);
483 if(!callbacks.empty())
485 int framePresentedSync = mWindowBase->CreateFramePresentedSyncFence();
486 if(framePresentedSync != -1)
488 Dali::Mutex::ScopedLock lock(mMutex);
490 DALI_LOG_RELEASE_INFO("WindowRenderSurface::PreRender: CreateFramePresentedSyncFence [%d]\n", framePresentedSync);
492 mFrameCallbackInfoContainer.push_back(std::unique_ptr<FrameCallbackInfo>(new FrameCallbackInfo(callbacks, framePresentedSync)));
494 needFrameRenderedTrigger = true;
498 DALI_LOG_ERROR("WindowRenderSurface::PreRender: CreateFramePresentedSyncFence is failed\n");
505 if(needFrameRenderedTrigger)
507 if(!mFrameRenderedTrigger)
509 mFrameRenderedTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessFrameCallback),
510 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
512 mFrameRenderedTrigger->Trigger();
517 * wl_egl_window_tizen_set_rotation(SetEglWindowRotation) -> PreRotation
518 * wl_egl_window_tizen_set_buffer_transform(SetEglWindowBufferTransform) -> Screen Rotation
519 * wl_egl_window_tizen_set_window_transform(SetEglWindowTransform) -> Window Rotation
520 * These function should be called before calling first drawing gl Function.
521 * Notice : PreRotation is not used in the latest tizen,
522 * because output transform event should be occured before egl window is not created.
524 if(scene && resizingSurface)
527 bool isScreenOrientationChanging = false;
529 if(mWindowRotationAngle != scene.GetCurrentSurfaceOrientation())
531 mWindowRotationAngle = scene.GetCurrentSurfaceOrientation();
532 mIsWindowOrientationChanging = true;
535 if(mScreenRotationAngle != scene.GetCurrentScreenOrientation())
537 mScreenRotationAngle = scene.GetCurrentScreenOrientation();
538 isScreenOrientationChanging = true;
540 totalAngle = (mWindowRotationAngle + mScreenRotationAngle) % 360;
542 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);
544 Rect<int> surfaceSize = scene.GetCurrentSurfaceRect();
545 //update surface size
546 mPositionSize.width = surfaceSize.width;
547 mPositionSize.height = surfaceSize.height;
549 DALI_LOG_RELEASE_INFO("Window is resizing, (%d, %d), [%d x %d]\n", mPositionSize.x, mPositionSize.y, mPositionSize.width, mPositionSize.height);
551 // Window rotate or screen rotate
552 if(mIsWindowOrientationChanging || isScreenOrientationChanging)
554 mWindowBase->SetEglWindowBufferTransform(totalAngle);
557 // Only window rotate
558 if(mIsWindowOrientationChanging)
560 mWindowBase->SetEglWindowTransform(mWindowRotationAngle);
564 Dali::PositionSize positionSize;
566 // Some native resize API(wl_egl_window_resize) has the input parameters of x, y, width and height.
567 // So, position data should be set.
568 positionSize.x = mPositionSize.x;
569 positionSize.y = mPositionSize.y;
570 if(totalAngle == 0 || totalAngle == 180)
572 positionSize.width = mPositionSize.width;
573 positionSize.height = mPositionSize.height;
577 positionSize.width = mPositionSize.height;
578 positionSize.height = mPositionSize.width;
581 mWindowBase->ResizeEglWindow(positionSize);
583 SetFullSwapNextFrame();
586 SetBufferDamagedRects(damagedRects, clippingRect);
590 Rect<int> surfaceRect = scene.GetCurrentSurfaceRect();
591 if(clippingRect == surfaceRect)
593 int32_t totalAngle = scene.GetCurrentSurfaceOrientation() + scene.GetCurrentScreenOrientation();
594 if(totalAngle >= 360)
598 mDamagedRects.assign(1, RecalculateRect[std::min(totalAngle / 90, 3)](surfaceRect, surfaceRect));
602 // This is now done when the render pass for the render surface begins
603 // MakeContextCurrent();
608 void WindowRenderSurface::PostRender()
610 // Inform the gl implementation that rendering has finished before informing the surface
611 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
614 GlImplementation& mGLES = eglGraphics->GetGlesInterface();
617 bool needWindowRotationCompleted = false;
619 if(mIsWindowOrientationChanging)
621 if(mNeedWindowRotationAcknowledgement)
623 Dali::Integration::Scene scene = mScene.GetHandle();
626 if(scene.IsRotationCompletedAcknowledgementSet())
628 needWindowRotationCompleted = true;
634 needWindowRotationCompleted = true;
638 if(needWindowRotationCompleted || mIsImeWindowSurface)
640 if(mThreadSynchronization)
642 // Enable PostRender flag
643 mThreadSynchronization->PostRenderStarted();
646 if(mIsWindowOrientationChanging || mIsImeWindowSurface)
648 mPostRenderTrigger->Trigger();
651 if(mThreadSynchronization)
653 // Wait until the event-thread complete the rotation event processing
654 mThreadSynchronization->PostRenderWaitForCompletion();
658 SwapBuffers(mDamagedRects);
660 if(mRenderNotification)
662 mRenderNotification->Trigger();
667 void WindowRenderSurface::StopRender()
671 void WindowRenderSurface::SetThreadSynchronization(ThreadSynchronizationInterface& threadSynchronization)
673 DALI_LOG_INFO(gWindowRenderSurfaceLogFilter, Debug::Verbose, "WindowRenderSurface::SetThreadSynchronization: called\n");
675 mThreadSynchronization = &threadSynchronization;
678 void WindowRenderSurface::ReleaseLock()
683 Dali::RenderSurfaceInterface::Type WindowRenderSurface::GetSurfaceType()
685 return Dali::RenderSurfaceInterface::WINDOW_RENDER_SURFACE;
688 void WindowRenderSurface::MakeContextCurrent()
692 mEGL->MakeContextCurrent(mEGLSurface, mEGLContext);
696 Integration::DepthBufferAvailable WindowRenderSurface::GetDepthBufferRequired()
698 return mGraphics ? mGraphics->GetDepthBufferRequired() : Integration::DepthBufferAvailable::FALSE;
701 Integration::StencilBufferAvailable WindowRenderSurface::GetStencilBufferRequired()
703 return mGraphics ? mGraphics->GetStencilBufferRequired() : Integration::StencilBufferAvailable::FALSE;
706 void WindowRenderSurface::InitializeImeSurface()
708 mIsImeWindowSurface = true;
709 if(!mPostRenderTrigger)
711 mPostRenderTrigger = std::unique_ptr<TriggerEventInterface>(TriggerEventFactory::CreateTriggerEvent(MakeCallback(this, &WindowRenderSurface::ProcessPostRender),
712 TriggerEventInterface::KEEP_ALIVE_AFTER_TRIGGER));
716 void WindowRenderSurface::SetNeedsRotationCompletedAcknowledgement(bool needAcknowledgement)
718 mNeedWindowRotationAcknowledgement = needAcknowledgement;
721 void WindowRenderSurface::OutputTransformed()
723 int screenRotationAngle = mWindowBase->GetScreenRotationAngle();
725 if(mScreenRotationAngle != screenRotationAngle)
727 mOutputTransformedSignal.Emit();
729 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: window = %d new screen angle = %d\n", mWindowRotationAngle, screenRotationAngle);
733 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OutputTransformed: Ignore output transform [%d]\n", mScreenRotationAngle);
737 void WindowRenderSurface::ProcessPostRender()
739 if(mIsWindowOrientationChanging)
741 mWindowRotationFinishedSignal.Emit();
742 mWindowBase->WindowRotationCompleted(mWindowRotationAngle, mPositionSize.width, mPositionSize.height);
743 mIsWindowOrientationChanging = false;
744 DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessPostRender: Rotation Done, flag = %d\n", mIsWindowOrientationChanging);
747 if(mIsImeWindowSurface)
749 mWindowBase->ImeWindowReadyToRender();
752 if(mThreadSynchronization)
754 mThreadSynchronization->PostRenderComplete();
758 void WindowRenderSurface::ProcessFrameCallback()
760 Dali::Mutex::ScopedLock lock(mMutex);
762 for(auto&& iter : mFrameCallbackInfoContainer)
764 if(!iter->fileDescriptorMonitor)
766 iter->fileDescriptorMonitor = std::unique_ptr<FileDescriptorMonitor>(new FileDescriptorMonitor(iter->fileDescriptor,
767 MakeCallback(this, &WindowRenderSurface::OnFileDescriptorEventDispatched),
768 FileDescriptorMonitor::FD_READABLE));
770 DALI_LOG_RELEASE_INFO("WindowRenderSurface::ProcessFrameCallback: Add handler [%d]\n", iter->fileDescriptor);
775 void WindowRenderSurface::OnFileDescriptorEventDispatched(FileDescriptorMonitor::EventType eventBitMask, int fileDescriptor)
777 if(!(eventBitMask & FileDescriptorMonitor::FD_READABLE))
779 DALI_LOG_ERROR("WindowRenderSurface::OnFileDescriptorEventDispatched: file descriptor error [%d]\n", eventBitMask);
780 close(fileDescriptor);
784 DALI_LOG_RELEASE_INFO("WindowRenderSurface::OnFileDescriptorEventDispatched: Frame rendered [%d]\n", fileDescriptor);
786 std::unique_ptr<FrameCallbackInfo> callbackInfo;
788 Dali::Mutex::ScopedLock lock(mMutex);
789 auto frameCallbackInfo = std::find_if(mFrameCallbackInfoContainer.begin(), mFrameCallbackInfoContainer.end(), [fileDescriptor](std::unique_ptr<FrameCallbackInfo>& callbackInfo) {
790 return callbackInfo->fileDescriptor == fileDescriptor;
792 if(frameCallbackInfo != mFrameCallbackInfoContainer.end())
794 callbackInfo = std::move(*frameCallbackInfo);
796 mFrameCallbackInfoContainer.erase(frameCallbackInfo);
800 // Call the connected callback
803 for(auto&& iter : (callbackInfo)->callbacks)
805 CallbackBase::Execute(*(iter.first), iter.second);
810 void WindowRenderSurface::SetBufferDamagedRects(const std::vector<Rect<int>>& damagedRects, Rect<int>& clippingRect)
812 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
815 // If scene is not exist, just use stored mPositionSize.
816 Rect<int> surfaceRect(0, 0, mPositionSize.width, mPositionSize.height);
817 int32_t orientation = 0;
819 Dali::Integration::Scene scene = mScene.GetHandle();
822 surfaceRect = scene.GetCurrentSurfaceRect();
823 int32_t totalAngle = scene.GetCurrentSurfaceOrientation() + scene.GetCurrentScreenOrientation();
824 if(totalAngle >= 360)
828 orientation = std::min(totalAngle / 90, 3);
831 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
832 if(!eglImpl.IsPartialUpdateRequired() || mFullSwapNextFrame)
834 InsertRects(mBufferDamagedRects, surfaceRect);
835 clippingRect = surfaceRect;
839 if(damagedRects.empty())
841 // Empty damaged rect. We don't need rendering
842 clippingRect = Rect<int>();
843 // Clean up current damanged rects.
844 mDamagedRects.clear();
848 mGraphics->ActivateSurfaceContext(this);
850 EGLint bufferAge = eglImpl.GetBufferAge(mEGLSurface);
852 // Buffer age 0 means the back buffer in invalid and requires full swap
855 InsertRects(mBufferDamagedRects, surfaceRect);
856 clippingRect = surfaceRect;
860 mDamagedRects.assign(damagedRects.begin(), damagedRects.end());
862 // Merge intersecting rects, form an array of non intersecting rects to help driver a bit
863 // Could be optional and can be removed, needs to be checked with and without on platform
864 // And then, Make one clipping rect, and rotate rects by orientation.
865 MergeIntersectingRectsAndRotate(clippingRect, mDamagedRects, orientation, surfaceRect);
867 // We push current frame damaged rects here, zero index for current frame
868 InsertRects(mBufferDamagedRects, clippingRect);
870 // Merge damaged rects into clipping rect
871 if(bufferAge <= static_cast<EGLint>(mBufferDamagedRects.size()))
873 // clippingRect is already the current frame's damaged rect. Merge from the second
874 for(int i = 1; i < bufferAge; i++)
876 clippingRect.Merge(mBufferDamagedRects[i]);
881 // The buffer age is too old. Need full update.
882 clippingRect = surfaceRect;
886 if(!clippingRect.Intersect(surfaceRect) || clippingRect.Area() > surfaceRect.Area() * FULL_UPDATE_RATIO)
888 // clipping area too big or doesn't intersect surface rect
889 clippingRect = surfaceRect;
893 if(!clippingRect.IsEmpty())
895 std::vector<Rect<int>> damagedRegion;
898 damagedRegion.push_back(RecalculateRect[orientation](clippingRect, surfaceRect));
902 damagedRegion.push_back(clippingRect);
905 eglImpl.SetDamageRegion(mEGLSurface, damagedRegion);
910 void WindowRenderSurface::SwapBuffers(const std::vector<Rect<int>>& damagedRects)
912 auto eglGraphics = static_cast<EglGraphics*>(mGraphics);
915 Internal::Adaptor::EglImplementation& eglImpl = eglGraphics->GetEglImplementation();
917 if(!eglImpl.IsPartialUpdateRequired() || mFullSwapNextFrame)
919 mFullSwapNextFrame = false;
920 eglImpl.SwapBuffers(mEGLSurface);
924 mFullSwapNextFrame = false;
926 Rect<int32_t> surfaceRect;
927 Dali::Integration::Scene scene = mScene.GetHandle();
930 surfaceRect = scene.GetCurrentSurfaceRect();
933 if(!damagedRects.size() || (damagedRects[0].Area() > surfaceRect.Area() * FULL_UPDATE_RATIO))
935 // In normal cases, WindowRenderSurface::SwapBuffers() will not be called if mergedRects.size() is 0.
936 // For exceptional cases, swap full area.
937 eglImpl.SwapBuffers(mEGLSurface);
941 eglImpl.SwapBuffers(mEGLSurface, damagedRects);
946 } // namespace Adaptor
948 } // namespace Internal