Skip to content

Commit 33bdc3c

Browse files
committed
Reuse WebGL drawing buffers once the compositor releases them
https://bugs.webkit.org/show_bug.cgi?id=327571 rdar://190042989 Reviewed by Kimmo Kinnunen. For a WebGL context we keep a ring of three drawing buffers: front (display), back (drawing), and a secondary back buffer. Each frame the ring moves on to the buffer sent two frames earlier, and if it is still in use, destroys it and allocates a new one. On Apple platforms the compositor keeps a displayed buffer for about 4 frames at both 60 and 120 FPS. So if a canvas is rendering every frame a buffer gets released and a new one allocated every frame. This is quite expensive since the kernel needs to allocate memory and map it in to the GPU process and then destroy an old one on the next frame. Keep two extra buffers as a spares instead. Once a spare is no longer in use it becomes available and marked volatile. The next time the ring needs a buffer it takes the earliest unused spare, and only allocates if there are none. So that the last few buffers are not left non-volatile once a canvas stops drawing, RemoteGraphicsContextGLCocoa schedules a check 200 ms after a frame that leaves a spare in-use. If no frame came in the mean time, it makes the released spares volatile and drops any that are still in-use to save memory on a static page. Measured on an iPhone 16 Pro with one 1902x1440 WebGL2 canvas, while animating the allocation churn drops from one per frame to a total of 5 at 60Hz and 6 at 120Hz over the course of about 75 seconds. GPU process CPU drops by about 43% at 60 Hz and about 45% at 120 Hz. The Web process's peak footprint goes down by 21 MB at 60 Hz and 13 MB at 120 Hz, and once the canvas stops drawing its footprint is the same as before this patch. Tests: TestWebKitAPI.GraphicsContextGLCocoaTest/AnyContextAttributeTest.DisplayBuffersInUseAreReusedOnceReleased TestWebKitAPI.GraphicsContextGLCocoaTest/AnyContextAttributeTest.IdleSpareDrawingBuffersAreReleased TestWebKitAPI.GraphicsContextGLCocoaTest/AnyContextAttributeTest.SpareDrawingBuffersAreDroppedOnReshapeAndColorSpaceChange * Source/WTF/wtf/spi/cocoa/IOSurfaceSPI.h: Add declaration needed for tests. * Source/WebCore/platform/graphics/cocoa/GraphicsContextGLCocoa.h: * Source/WebCore/platform/graphics/cocoa/GraphicsContextGLCocoa.mm: (WebCore::GraphicsContextGLCocoa::copySurfaceBuffer): (WebCore::GraphicsContextGLCocoa::createDrawingBuffer): (WebCore::GraphicsContextGLCocoa::createPbuffer): (WebCore::GraphicsContextGLCocoa::bindNextDrawingBuffer): (WebCore::GraphicsContextGLCocoa::freeDrawingBuffers): (WebCore::GraphicsContextGLCocoa::retireDrawingBuffer): (WebCore::GraphicsContextGLCocoa::takeSpareDrawingBuffer): (WebCore::GraphicsContextGLCocoa::updateSpareDrawingBuffers): (WebCore::GraphicsContextGLCocoa::hasSpareDrawingBuffersInUse const): (WebCore::GraphicsContextGLCocoa::releaseIdleSpareDrawingBuffers): * Source/WebCore/platform/graphics/cocoa/IOSurfaceDrawingBuffer.h: (WebCore::IOSurfaceDrawingBuffer::takeSurface): * Source/WebKit/GPUProcess/graphics/RemoteGraphicsContextGLCocoa.cpp: (WebKit::RemoteGraphicsContextGLCocoa::prepareForDisplay): (WebKit::RemoteGraphicsContextGLCocoa::scheduleIdleSpareDrawingBufferRelease): (WebKit::RemoteGraphicsContextGLCocoa::idleSpareDrawingBufferReleaseTimerFired): * Tools/TestWebKitAPI/Tests/WebCore/cocoa/TestGraphicsContextGLCocoa.mm: (TestWebKitAPI::WebCore::TestedGraphicsContextGLCocoa::spareDrawingBufferCount const): (TestWebKitAPI::WebCore::TestedGraphicsContextGLCocoa::volatileSpareDrawingBufferCount const): (TestWebKitAPI::TEST_P): (TestWebKitAPI::displayFramesKeepingBuffersInUse): (TestWebKitAPI::releaseBuffers): * WebKitLibraries/SDKs/watchos26.0-additions.sdk/System/Library/Frameworks/IOSurface.framework/IOSurface.tbd: * WebKitLibraries/SDKs/watchos27.0-additions.sdk/System/Library/Frameworks/IOSurface.framework/IOSurface.tbd: * WebKitLibraries/SDKs/watchsimulator26.0-additions.sdk/System/Library/Frameworks/IOSurface.framework/IOSurface.tbd: watchOS public SDK does not come with IOSurfaceDecrementUseCount which is needed for the new tests. Canonical link: https://commits.webkit.org/323882@main
1 parent ed8a3f8 commit 33bdc3c

9 files changed

Lines changed: 248 additions & 10 deletions

File tree

‎Source/WTF/wtf/spi/cocoa/IOSurfaceSPI.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ extern const CFStringRef kIOSurfaceContentHeadroom;
8080
size_t IOSurfaceAlignProperty(CFStringRef property, size_t value);
8181
IOSurfaceRef IOSurfaceCreate(CFDictionaryRef properties);
8282
mach_port_t IOSurfaceCreateMachPort(IOSurfaceRef buffer);
83+
void IOSurfaceDecrementUseCount(IOSurfaceRef buffer);
8384
size_t IOSurfaceGetAllocSize(IOSurfaceRef buffer);
8485
void *IOSurfaceGetBaseAddress(IOSurfaceRef buffer);
8586
size_t IOSurfaceGetBytesPerRow(IOSurfaceRef buffer);

‎Source/WebCore/platform/graphics/cocoa/GraphicsContextGLCocoa.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <array>
3434
#include <memory>
3535
#include <wtf/CheckedPtr.h>
36+
#include <wtf/Vector.h>
3637

3738
#if ENABLE(WEBXR)
3839
#include <WebCore/PlatformXR.h>
@@ -115,6 +116,9 @@ class WEBCORE_EXPORT GraphicsContextGLCocoa : public GraphicsContextGLANGLE, pub
115116
// Prepares current frame for display. The `finishedSignal` will be invoked once the frame has finished rendering.
116117
void prepareForDisplayWithFinishedSignal(Function<void()> finishedSignal);
117118

119+
bool hasSpareDrawingBuffersInUse() const;
120+
void releaseIdleSpareDrawingBuffers();
121+
118122
protected:
119123
GraphicsContextGLCocoa(WebCore::GraphicsContextGLAttributes&&, ProcessIdentity&& resourceOwner);
120124

@@ -131,7 +135,11 @@ class WEBCORE_EXPORT GraphicsContextGLCocoa : public GraphicsContextGLANGLE, pub
131135
IOSurfacePbuffer& LEGACY_NODELETE surfaceBuffer(SurfaceBuffer);
132136
bool bindNextDrawingBuffer();
133137
IOSurfacePbuffer createDrawingBuffer();
138+
IOSurfacePbuffer createPbuffer(std::unique_ptr<IOSurface>&&);
134139
void freeDrawingBuffers();
140+
void retireDrawingBuffer(IOSurfacePbuffer&);
141+
std::unique_ptr<IOSurface> takeSpareDrawingBuffer();
142+
void updateSpareDrawingBuffers();
135143

136144
// Inserts new fence that will invoke `signal` from a background thread when completed.
137145
// If not possible, calls the `signal`.
@@ -160,6 +168,15 @@ class WEBCORE_EXPORT GraphicsContextGLCocoa : public GraphicsContextGLANGLE, pub
160168
static constexpr size_t maxReusedDrawingBuffers { 3 };
161169
size_t m_currentDrawingBufferIndex { 0 };
162170
std::array<IOSurfacePbuffer, maxReusedDrawingBuffers> m_drawingBuffers;
171+
172+
struct SpareDrawingBuffer {
173+
std::unique_ptr<IOSurface> surface;
174+
bool isAvailable { false };
175+
};
176+
Vector<SpareDrawingBuffer> m_spareDrawingBuffers;
177+
// On iOS CoreAnimation keeps a displayed buffer for about 4 frames; one more for headroom.
178+
static constexpr size_t maxSpareDrawingBuffers { 5 };
179+
163180
friend class GraphicsContextGLCVCocoa;
164181
};
165182

‎Source/WebCore/platform/graphics/cocoa/GraphicsContextGLCocoa.mm‎

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,9 @@ static EGLDisplay initializeEGLDisplay(const GraphicsContextGLAttributes& attrs)
371371
return nullptr;
372372

373373
auto& destination = copyBuffer();
374-
if (destination && destination.isInUse()) {
375-
EGL_DestroySurface(m_displayObj, destination.pbuffer());
376-
destination = { };
377-
}
374+
updateSpareDrawingBuffers();
375+
if (destination.isInUse())
376+
retireDrawingBuffer(destination);
378377
if (!destination) {
379378
destination = createDrawingBuffer();
380379
if (!destination)
@@ -474,6 +473,9 @@ static EGLDisplay initializeEGLDisplay(const GraphicsContextGLAttributes& attrs)
474473

475474
IOSurfacePbuffer GraphicsContextGLCocoa::createDrawingBuffer()
476475
{
476+
if (auto spare = takeSpareDrawingBuffer())
477+
return createPbuffer(WTF::move(spare));
478+
477479
const auto size = getInternalFramebufferSize();
478480
IOSurface::IOSurfaceOptions options;
479481
#if HAVE(IOSURFACE_ALPHA_CHANNEL_MODE)
@@ -486,6 +488,12 @@ static EGLDisplay initializeEGLDisplay(const GraphicsContextGLAttributes& attrs)
486488
if (m_resourceOwner)
487489
surface->setOwnershipIdentity(m_resourceOwner);
488490

491+
return createPbuffer(WTF::move(surface));
492+
}
493+
494+
IOSurfacePbuffer GraphicsContextGLCocoa::createPbuffer(std::unique_ptr<IOSurface>&& surface)
495+
{
496+
const auto size = getInternalFramebufferSize();
489497
const bool usingAlpha = contextAttributes().alpha;
490498
const EGLint surfaceAttributes[] = {
491499
EGL_WIDTH, size.width(),
@@ -508,13 +516,16 @@ static EGLDisplay initializeEGLDisplay(const GraphicsContextGLAttributes& attrs)
508516
bool GraphicsContextGLCocoa::bindNextDrawingBuffer()
509517
{
510518
ASSERT(!getInternalFramebufferSize().isEmpty());
519+
updateSpareDrawingBuffers();
511520
if (auto& buffer = drawingBuffer())
512521
EGL_ReleaseTexImage(m_displayObj, buffer.pbuffer(), EGL_BACK_BUFFER);
513522

514523
m_currentDrawingBufferIndex++;
515524
auto& buffer = drawingBuffer();
516525

517-
if (buffer && (buffer.isInUse() || m_failNextDrawingBufferAllocation)) {
526+
if (buffer.isInUse())
527+
retireDrawingBuffer(buffer);
528+
else if (buffer && m_failNextDrawingBufferAllocation) {
518529
EGL_DestroySurface(m_displayObj, buffer.pbuffer());
519530
buffer = { };
520531
}
@@ -547,6 +558,59 @@ static EGLDisplay initializeEGLDisplay(const GraphicsContextGLAttributes& attrs)
547558
buffer = { };
548559
}
549560
}
561+
m_spareDrawingBuffers.clear();
562+
}
563+
564+
void GraphicsContextGLCocoa::retireDrawingBuffer(IOSurfacePbuffer& buffer)
565+
{
566+
EGL_DestroySurface(m_displayObj, buffer.pbuffer());
567+
auto surface = buffer.takeSurface();
568+
buffer = { };
569+
if (!surface)
570+
return;
571+
m_spareDrawingBuffers.append({ WTF::move(surface) });
572+
if (m_spareDrawingBuffers.size() > maxSpareDrawingBuffers)
573+
m_spareDrawingBuffers.removeAt(0);
574+
}
575+
576+
std::unique_ptr<IOSurface> GraphicsContextGLCocoa::takeSpareDrawingBuffer()
577+
{
578+
auto index = m_spareDrawingBuffers.findIf([](auto& spare) {
579+
return spare.isAvailable;
580+
});
581+
if (index == notFound)
582+
return nullptr;
583+
auto surface = WTF::move(m_spareDrawingBuffers[index].surface);
584+
m_spareDrawingBuffers.removeAt(index);
585+
ASSERT(surface->size() == getInternalFramebufferSize());
586+
// The contents do not matter, so neither does whether they were purged.
587+
surface->setVolatile(false);
588+
return surface;
589+
}
590+
591+
void GraphicsContextGLCocoa::updateSpareDrawingBuffers()
592+
{
593+
for (auto& spare : m_spareDrawingBuffers) {
594+
if (spare.isAvailable || spare.surface->isInUse())
595+
continue;
596+
spare.isAvailable = true;
597+
spare.surface->setVolatile(true);
598+
}
599+
}
600+
601+
bool GraphicsContextGLCocoa::hasSpareDrawingBuffersInUse() const
602+
{
603+
return m_spareDrawingBuffers.containsIf([](auto& spare) {
604+
return !spare.isAvailable;
605+
});
606+
}
607+
608+
void GraphicsContextGLCocoa::releaseIdleSpareDrawingBuffers()
609+
{
610+
updateSpareDrawingBuffers();
611+
m_spareDrawingBuffers.removeAllMatching([](auto& spare) {
612+
return !spare.isAvailable;
613+
});
550614
}
551615

552616
bool GraphicsContextGLANGLE::makeCurrent(GCGLDisplay display, GCGLContext context)

‎Source/WebCore/platform/graphics/cocoa/IOSurfaceDrawingBuffer.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class IOSurfaceDrawingBuffer {
5252
// cross-process use, and copy-on-write would not work.
5353
bool isInUse() const;
5454

55+
std::unique_ptr<IOSurface> takeSurface() { return WTF::move(m_surface); }
56+
5557
private:
5658
std::unique_ptr<IOSurface> m_surface;
5759
};

‎Source/WebKit/GPUProcess/graphics/RemoteGraphicsContextGLCocoa.cpp‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
#include "GPUConnectionToWebProcess.h"
3232
#include "IPCUtilities.h"
3333
#include "RemoteSharedResourceCache.h"
34+
#include "StreamConnectionWorkQueue.h"
3435
#include <WebCore/ProcessIdentity.h>
3536
#include <WebCore/SharedMemory.h>
3637
#include <wtf/MachSendRight.h>
38+
#include <wtf/RunLoop.h>
3739
#include <wtf/TZoneMalloc.h>
3840
#include <wtf/TZoneMallocInlines.h>
3941

@@ -90,6 +92,11 @@ class RemoteGraphicsContextGLCocoa final : public RemoteGraphicsContextGL {
9092
void platformWorkQueueInitialize(WebCore::GraphicsContextGLAttributes&&) final;
9193
void prepareForDisplay(IPC::Semaphore&&, CompletionHandler<void(WTF::MachSendRight&&)>&&) final;
9294
private:
95+
void scheduleIdleSpareDrawingBufferRelease();
96+
void idleSpareDrawingBufferReleaseTimerFired();
97+
98+
bool m_idleSpareDrawingBufferReleaseScheduled WTF_GUARDED_BY_CAPABILITY(workQueue()) { false };
99+
bool m_didDisplaySinceIdleSpareDrawingBufferReleaseScheduled WTF_GUARDED_BY_CAPABILITY(workQueue()) { false };
93100
};
94101

95102
WTF_MAKE_TZONE_ALLOCATED_IMPL(RemoteGraphicsContextGLCocoa);
@@ -126,6 +133,41 @@ void RemoteGraphicsContextGLCocoa::prepareForDisplay(IPC::Semaphore&& finishedSe
126133
if (WebCore::IOSurface* surface = context->displayBufferSurface())
127134
sendRight = surface->createSendRight();
128135
completionHandler(WTF::move(sendRight));
136+
m_didDisplaySinceIdleSpareDrawingBufferReleaseScheduled = true;
137+
if (context->hasSpareDrawingBuffersInUse())
138+
scheduleIdleSpareDrawingBufferRelease();
139+
}
140+
141+
static constexpr Seconds idleSpareDrawingBufferReleaseDelay = 200_ms;
142+
143+
void RemoteGraphicsContextGLCocoa::scheduleIdleSpareDrawingBufferRelease()
144+
{
145+
assertIsCurrent(workQueue());
146+
if (m_idleSpareDrawingBufferReleaseScheduled)
147+
return;
148+
m_idleSpareDrawingBufferReleaseScheduled = true;
149+
m_didDisplaySinceIdleSpareDrawingBufferReleaseScheduled = false;
150+
RunLoop::mainSingleton().dispatchAfter(idleSpareDrawingBufferReleaseDelay, [protectedThis = protect(*this)] mutable {
151+
Ref workQueue = protectedThis->workQueue();
152+
workQueue->dispatch([protectedThis = WTF::move(protectedThis)] {
153+
protectedThis->idleSpareDrawingBufferReleaseTimerFired();
154+
});
155+
});
156+
}
157+
158+
void RemoteGraphicsContextGLCocoa::idleSpareDrawingBufferReleaseTimerFired()
159+
{
160+
assertIsCurrent(workQueue());
161+
m_idleSpareDrawingBufferReleaseScheduled = false;
162+
RefPtr context = m_context;
163+
if (!context)
164+
return;
165+
if (m_didDisplaySinceIdleSpareDrawingBufferReleaseScheduled) {
166+
if (context->hasSpareDrawingBuffersInUse())
167+
scheduleIdleSpareDrawingBufferRelease();
168+
return;
169+
}
170+
context->releaseIdleSpareDrawingBuffers();
129171
}
130172

131173
}

‎Tools/TestWebKitAPI/Tests/WebCore/cocoa/TestGraphicsContextGLCocoa.mm‎

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#import <atomic>
3838
#import <limits>
3939
#import <optional>
40+
#import <wtf/Deque.h>
4041
#import <wtf/HashSet.h>
4142
#import <wtf/MemoryFootprint.h>
4243
#import <wtf/StdLibExtras.h>
@@ -71,6 +72,21 @@ void didChangeMemoryCost() final { }
7172
return context;
7273
}
7374
RefPtr<GraphicsLayerContentsDisplayDelegate> layerContentsDisplayDelegate() final { return nullptr; }
75+
76+
using GraphicsContextGLCocoa::maxSpareDrawingBuffers;
77+
78+
size_t spareDrawingBufferCount() const { return m_spareDrawingBuffers.size(); }
79+
// As the kernel has it, rather than as WebCore::IOSurface last set it.
80+
size_t volatileSpareDrawingBufferCount() const
81+
{
82+
size_t count = 0;
83+
for (auto& spare : m_spareDrawingBuffers) {
84+
uint32_t state = 0;
85+
IOSurfaceSetPurgeable(spare.surface->surface(), kIOSurfacePurgeableKeepCurrent, &state);
86+
count += state == kIOSurfacePurgeableVolatile || state == kIOSurfacePurgeableEmpty;
87+
}
88+
return count;
89+
}
7490
private:
7591
TestedGraphicsContextGLCocoa(GraphicsContextGLAttributes attributes)
7692
: GraphicsContextGLCocoa(WTF::move(attributes), { })
@@ -635,10 +651,105 @@ static void clearBottomHalf(GraphicsContextGL& gl, IntSize size, Color color, Al
635651
seenSurfaceRefs.add(surfaceRef);
636652

637653
IOSurfaceIncrementUseCount(surfaceRef);
654+
EXPECT_LE(context->spareDrawingBufferCount(), TestedGraphicsContextGLCocoa::maxSpareDrawingBuffers);
638655
}
639656
ASSERT_EQ(seenSurfaceRefs.size(), 50u);
640657
}
641658

659+
TEST_P(AnyContextAttributeTest, DisplayBuffersInUseAreReusedOnceReleased)
660+
{
661+
auto context = createTestContext({ 20, 20 });
662+
ASSERT_NE(context, nullptr);
663+
constexpr size_t framesHeld = 4;
664+
constexpr IntPoint samplePoint { 10, 10 };
665+
Deque<RetainPtr<IOSurfaceRef>> heldSurfaceRefs;
666+
HashSet<RetainPtr<IOSurfaceRef>> seenSurfaceRefs;
667+
size_t surfacesSeenHalfway = 0;
668+
for (int i = 0; i < 50; ++i) {
669+
EXPECT_TRUE(changeContextContents(*context, i));
670+
context->prepareForDisplay();
671+
auto* surface = context->displayBufferSurface();
672+
ASSERT_NE(surface, nullptr);
673+
RefPtr image = context->copyNativeImage(GraphicsContextGL::SurfaceBuffer::DisplayBuffer);
674+
ASSERT_NE(image, nullptr);
675+
EXPECT_TRUE(nativeImagePixelsIs(i % 2 ? Color::green : Color::yellow, *image, samplePoint)) << "frame " << i;
676+
677+
RetainPtr surfaceRef = surface->surface();
678+
seenSurfaceRefs.add(surfaceRef);
679+
IOSurfaceIncrementUseCount(surfaceRef.get());
680+
heldSurfaceRefs.append(WTF::move(surfaceRef));
681+
if (heldSurfaceRefs.size() > framesHeld)
682+
IOSurfaceDecrementUseCount(heldSurfaceRefs.takeFirst().get());
683+
if (i == 24)
684+
surfacesSeenHalfway = seenSurfaceRefs.size();
685+
}
686+
for (auto& surfaceRef : heldSurfaceRefs)
687+
IOSurfaceDecrementUseCount(surfaceRef.get());
688+
689+
EXPECT_EQ(seenSurfaceRefs.size(), surfacesSeenHalfway);
690+
EXPECT_LE(seenSurfaceRefs.size(), expectedDisplayBufferPoolSize + TestedGraphicsContextGLCocoa::maxSpareDrawingBuffers);
691+
}
692+
693+
static void displayFramesKeepingBuffersInUse(TestedGraphicsContextGLCocoa& context, int frames, Vector<RetainPtr<IOSurfaceRef>>& heldSurfaceRefs)
694+
{
695+
for (int i = 0; i < frames; ++i) {
696+
EXPECT_TRUE(changeContextContents(context, i));
697+
context.prepareForDisplay();
698+
auto* surface = context.displayBufferSurface();
699+
ASSERT_NE(surface, nullptr);
700+
RetainPtr surfaceRef = surface->surface();
701+
IOSurfaceIncrementUseCount(surfaceRef.get());
702+
heldSurfaceRefs.append(WTF::move(surfaceRef));
703+
}
704+
}
705+
706+
static void releaseBuffers(Vector<RetainPtr<IOSurfaceRef>>& heldSurfaceRefs)
707+
{
708+
for (auto& surfaceRef : heldSurfaceRefs)
709+
IOSurfaceDecrementUseCount(surfaceRef.get());
710+
heldSurfaceRefs.clear();
711+
}
712+
713+
TEST_P(AnyContextAttributeTest, IdleSpareDrawingBuffersAreReleased)
714+
{
715+
auto context = createTestContext({ 20, 20 });
716+
ASSERT_NE(context, nullptr);
717+
Vector<RetainPtr<IOSurfaceRef>> heldSurfaceRefs;
718+
displayFramesKeepingBuffersInUse(*context, 6, heldSurfaceRefs);
719+
auto sparesInUse = context->spareDrawingBufferCount();
720+
EXPECT_GT(sparesInUse, 1u);
721+
EXPECT_TRUE(context->hasSpareDrawingBuffersInUse());
722+
EXPECT_EQ(context->volatileSpareDrawingBufferCount(), 0u);
723+
724+
// The spares are the earliest frames' buffers. Release the first one, and keep the others in use.
725+
IOSurfaceDecrementUseCount(heldSurfaceRefs[0].get());
726+
context->releaseIdleSpareDrawingBuffers();
727+
EXPECT_EQ(context->spareDrawingBufferCount(), 1u);
728+
EXPECT_EQ(context->volatileSpareDrawingBufferCount(), 1u);
729+
EXPECT_FALSE(context->hasSpareDrawingBuffersInUse());
730+
731+
heldSurfaceRefs.removeAt(0);
732+
releaseBuffers(heldSurfaceRefs);
733+
}
734+
735+
TEST_P(AnyContextAttributeTest, SpareDrawingBuffersAreDroppedOnReshapeAndColorSpaceChange)
736+
{
737+
auto context = createTestContext({ 20, 20 });
738+
ASSERT_NE(context, nullptr);
739+
Vector<RetainPtr<IOSurfaceRef>> heldSurfaceRefs;
740+
displayFramesKeepingBuffersInUse(*context, 6, heldSurfaceRefs);
741+
EXPECT_GT(context->spareDrawingBufferCount(), 0u);
742+
context->reshape(30, 30);
743+
EXPECT_EQ(context->spareDrawingBufferCount(), 0u);
744+
releaseBuffers(heldSurfaceRefs);
745+
746+
displayFramesKeepingBuffersInUse(*context, 6, heldSurfaceRefs);
747+
EXPECT_GT(context->spareDrawingBufferCount(), 0u);
748+
context->setDrawingBufferColorSpace(ColorSpace::DisplayP3());
749+
EXPECT_EQ(context->spareDrawingBufferCount(), 0u);
750+
releaseBuffers(heldSurfaceRefs);
751+
}
752+
642753
// Test that drawing to GraphicsContextGL and marking the display buffer in use does not leak big
643754
// amounts of memory for each displayed buffer.
644755
// FIXME when rdar://164264557 is resolved.
@@ -652,9 +763,10 @@ static void clearBottomHalf(GraphicsContextGL& gl, IntSize size, Color color, Al
652763
// memory (130mb) stays resident, presumably by intention of IOKit. The test would originally leak
653764
// 2.7gb so the intended bug would be detected with 150mb error range.
654765
size_t footprintError = 150 * 1024 * 1024;
655-
size_t footprintChange = 0;
766+
constexpr IntSize size { 2048, 2048 };
767+
size_t footprintChange = TestedGraphicsContextGLCocoa::maxSpareDrawingBuffers * size.width() * size.height() * 4;
656768

657-
auto context = createTestContext({ 2048, 2048 });
769+
auto context = createTestContext(size);
658770
ASSERT_NE(context, nullptr);
659771

660772
WTF::releaseFastMallocFreeMemory();

0 commit comments

Comments
 (0)