From c94b8e1712e84802bd7b7cb9c8f00306daafc55f Mon Sep 17 00:00:00 2001 From: reuk Date: Mon, 12 Aug 2024 20:15:06 +0100 Subject: [PATCH] Direct2D: Refactor paintAreas handling in graphics contexts --- .../juce_Direct2DGraphicsContext_windows.cpp | 20 +++++++------------ .../juce_Direct2DHwndContext_windows.cpp | 14 ++++++------- .../juce_Direct2DImageContext_windows.cpp | 10 +++++----- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/modules/juce_graphics/native/juce_Direct2DGraphicsContext_windows.cpp b/modules/juce_graphics/native/juce_Direct2DGraphicsContext_windows.cpp index 3937626665..089df2a950 100644 --- a/modules/juce_graphics/native/juce_Direct2DGraphicsContext_windows.cpp +++ b/modules/juce_graphics/native/juce_Direct2DGraphicsContext_windows.cpp @@ -547,7 +547,6 @@ protected: Direct2DGraphicsContext& owner; SharedResourcePointer directX; SharedResourcePointer directWrite; - RectangleList paintAreas; std::optional deviceResources; @@ -568,8 +567,6 @@ protected: virtual ComSmartPtr getDeviceContextTarget() const = 0; - virtual void updatePaintAreas() = 0; - virtual bool checkPaintReady() { return deviceResources.has_value(); @@ -602,8 +599,8 @@ public: prepare(); // Anything to paint? - updatePaintAreas(); - auto paintBounds = paintAreas.getBounds(); + const auto paintAreas = getPaintAreas(); + const auto paintBounds = paintAreas.getBounds(); if (! getFrameSize().intersects (paintBounds) || paintBounds.isEmpty()) return nullptr; @@ -705,12 +702,8 @@ public: return deviceResources->deviceContext; } - const auto& getPaintAreas() const noexcept - { - return paintAreas; - } - - virtual Rectangle getFrameSize() = 0; + virtual RectangleList getPaintAreas() const = 0; + virtual Rectangle getFrameSize() const = 0; void setDeviceContextTransform (AffineTransform transform) { @@ -893,7 +886,8 @@ Direct2DGraphicsContext::~Direct2DGraphicsContext() = default; bool Direct2DGraphicsContext::startFrame (float dpiScale) { - auto pimpl = getPimpl(); + const auto pimpl = getPimpl(); + const auto paintAreas = pimpl->getPaintAreas(); currentState = pimpl->startFrame (dpiScale); if (currentState == nullptr) @@ -903,7 +897,7 @@ bool Direct2DGraphicsContext::startFrame (float dpiScale) { resetPendingClipList(); - clipToRectangleList (pimpl->getPaintAreas()); + clipToRectangleList (paintAreas); // Clear the buffer *after* setting the clip region clearTargetBuffer(); diff --git a/modules/juce_graphics/native/juce_Direct2DHwndContext_windows.cpp b/modules/juce_graphics/native/juce_Direct2DHwndContext_windows.cpp index 776f7a7d90..0bc5a604bc 100644 --- a/modules/juce_graphics/native/juce_Direct2DHwndContext_windows.cpp +++ b/modules/juce_graphics/native/juce_Direct2DHwndContext_windows.cpp @@ -371,18 +371,18 @@ private: Pimpl::teardown(); } - void updatePaintAreas() override + RectangleList getPaintAreas() const override { // Does the entire buffer need to be filled? if (swap.state == SwapChain::State::bufferAllocated || resizing) - deferredRepaints = swap.getSize(); + return swap.getSize(); // If the window alpha is less than 1.0, clip to the union of the // deferred repaints so the device context Clear() works correctly if (targetAlpha < 1.0f || ! opaque) - paintAreas = deferredRepaints.getBounds(); - else - paintAreas = deferredRepaints; + return deferredRepaints.getBounds(); + + return deferredRepaints; } bool checkPaintReady() override @@ -436,7 +436,7 @@ public: return Rectangle::leftTopRightBottom (clientRect.left, clientRect.top, clientRect.right, clientRect.bottom); } - Rectangle getFrameSize() override + Rectangle getFrameSize() const override { return getClientRect(); } @@ -523,7 +523,7 @@ public: // next frame JUCE_TRACE_LOG_D2D_PAINT_CALL (etw::direct2dHwndPaintStart, owner.getFrameId()); - presentation.getPresentation()->setPaintAreas (paintAreas); + presentation.getPresentation()->setPaintAreas (getPaintAreas()); deferredRepaints.clear(); diff --git a/modules/juce_graphics/native/juce_Direct2DImageContext_windows.cpp b/modules/juce_graphics/native/juce_Direct2DImageContext_windows.cpp index baa8a4fa05..e6d9ec3dca 100644 --- a/modules/juce_graphics/native/juce_Direct2DImageContext_windows.cpp +++ b/modules/juce_graphics/native/juce_Direct2DImageContext_windows.cpp @@ -46,15 +46,15 @@ public: { } - Rectangle getFrameSize() override + Rectangle getFrameSize() const override { const auto targetBitmap = getBitmap(); if (targetBitmap == nullptr) return {}; - auto size = targetBitmap->GetSize(); - return Rectangle { size.width, size.height }.getSmallestIntegerContainer(); + const auto size = targetBitmap->GetSize(); + return { (int) size.width, (int) size.height }; } ComSmartPtr getDeviceContextTarget() const override @@ -81,9 +81,9 @@ private: return target->getAdapterD2D1Bitmap(); } - void updatePaintAreas() override + RectangleList getPaintAreas() const override { - paintAreas = getFrameSize(); + return getFrameSize(); } Direct2DPixelData::Ptr target;