Title: [285644] trunk
Revision
285644
Author
[email protected]
Date
2021-11-11 11:46:00 -0800 (Thu, 11 Nov 2021)

Log Message

REGRESSION (iOS 15, GPU Process): Fails to load texture map
https://bugs.webkit.org/show_bug.cgi?id=231471
<rdar://problem/84328146>

Patch by Kimmo Kinnunen <[email protected]> on 2021-11-11
Reviewed by Tim Horton.

Source/WebCore:

Clear the CGImage copy from the IOSurface CGContext after the IOSurface has
been drawn to a bitmap CGContext. The CGImage stored in the IOSurface CGContext
contains a malloc buffer of the data. This buffer is counted against GPU
process memory consumption, causing GPU process jetsams.

* platform/graphics/cg/ImageBufferIOSurfaceBackend.cpp:
(WebCore::ImageBufferIOSurfaceBackend::draw):
* platform/graphics/cg/ImageBufferIOSurfaceBackend.h:

Source/WebKit:

Use the normal "draw image buffer" call to draw the image buffer
to the shareable bitmap. Before, it was manually re-implementing this
API call via the lower level calls.
The drawImageBuffer call now fixes up the IOSurface -backed source
ImageBuffers to not keep referencing to the CGImage that provided
the data to the draw. Otherwise GPU process jetsams as the CGImage
inside IOSurface is counted against GPU process memory limit.

* GPUProcess/graphics/RemoteRenderingBackend.cpp:
(WebKit::RemoteRenderingBackend::getShareableBitmapForImageBufferWithQualifiedIdentifier):

Tools:

Add a test that tries to assert that GraphicsContext::drawImageBuffer() does not increase
memory footprint of the process. However, leave the test disabled as currently
it seems that the WTF::memoryFootprint() measurement does not see all the
memory releases the CG does.

* TestWebKitAPI/Tests/WebCore/ImageBufferTests.cpp:
(TestWebKitAPI::memoryFootprintChangedBy):
(TestWebKitAPI::imageBufferPixelIs):
(TestWebKitAPI::TEST):

LayoutTests:

Add a test that sometimes jetsams on device.
Jetsam is not deterministic as it is only
triggerd during memory pressure.
macOS does not trigger jetsam due to platform
rules.

* fast/canvas/webgl/gl-teximage-imagebitmap-memory.html: Added.
* fast/canvas/webgl/resources/green-4000x4000.png: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (285643 => 285644)


--- trunk/LayoutTests/ChangeLog	2021-11-11 19:07:12 UTC (rev 285643)
+++ trunk/LayoutTests/ChangeLog	2021-11-11 19:46:00 UTC (rev 285644)
@@ -1,3 +1,20 @@
+2021-11-11  Kimmo Kinnunen  <[email protected]>
+
+        REGRESSION (iOS 15, GPU Process): Fails to load texture map
+        https://bugs.webkit.org/show_bug.cgi?id=231471
+        <rdar://problem/84328146>
+
+        Reviewed by Tim Horton.
+
+        Add a test that sometimes jetsams on device.
+        Jetsam is not deterministic as it is only
+        triggerd during memory pressure.
+        macOS does not trigger jetsam due to platform
+        rules.
+
+        * fast/canvas/webgl/gl-teximage-imagebitmap-memory.html: Added.
+        * fast/canvas/webgl/resources/green-4000x4000.png: Added.
+
 2021-11-11  Alexey Shvayka  <[email protected]>
 
         JSCallbackData should use lexical global object for errors and lifecycle

Added: trunk/LayoutTests/fast/canvas/webgl/gl-teximage-imagebitmap-memory-expected.txt (0 => 285644)


--- trunk/LayoutTests/fast/canvas/webgl/gl-teximage-imagebitmap-memory-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/canvas/webgl/gl-teximage-imagebitmap-memory-expected.txt	2021-11-11 19:46:00 UTC (rev 285644)
@@ -0,0 +1,68 @@
+PASS gpu process restart check canvas should be red
+PASS getError was expected value: NO_ERROR : Should be no errors from setup.
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS getError was expected value: NO_ERROR : Should be no errors from setup
+PASS should be green
+PASS gpu process restart check canvas should be red
+PASS gpu process restart check canvas should be green
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/fast/canvas/webgl/gl-teximage-imagebitmap-memory.html (0 => 285644)


--- trunk/LayoutTests/fast/canvas/webgl/gl-teximage-imagebitmap-memory.html	                        (rev 0)
+++ trunk/LayoutTests/fast/canvas/webgl/gl-teximage-imagebitmap-memory.html	2021-11-11 19:46:00 UTC (rev 285644)
@@ -0,0 +1,79 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>texImage2D with large ImageBitmap.</title>
+<script src=""
+<script src="" </script>
+<script src="" </script>
+</head>
+<body>
+<canvas id="example"></canvas>
+<div id="description"></div>
+<script>
+"use strict";
+
+let wtu = WebGLTestUtils;
+
+async function loadImage(url) {
+    const res = await fetch(url, { mode: 'cors' });
+    const blob = await res.blob();
+    const bitmap = await createImageBitmap(blob);
+    return bitmap;
+}
+
+async function test() {
+  let red = [255, 0, 0, 255];
+  let green = [0, 255, 0, 255];
+
+  let ctx2d = document.createElement("canvas").getContext("2d");
+  ctx2d.fillStyle = "#ff0000";
+  ctx2d.fillRect(0, 0, 300, 150);
+  wtu.checkCanvasRect(ctx2d, 0, 0, 300, 150, red, "gpu process restart check canvas should be red", 0);
+
+  let canvas = document.getElementById("example");
+  let gl = wtu.create3DContext(canvas, {'depth': false, 'antialias': false, stencil: 'false'});
+  let program = wtu.setupTexturedQuad(gl);
+  let loc = gl.getUniformLocation(program, "tex");
+  gl.uniform1i(loc, 0);
+
+  gl.disable(gl.BLEND);
+  gl.disable(gl.DEPTH_TEST);
+  gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);
+  let width = canvas.width;
+  let height = canvas.height;
+
+  glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");
+  let bitmapCount = 6;
+  let uploadIterations = 5;
+  let bitmaps = [];
+  for (let i = 0; i < bitmapCount; ++i)
+    bitmaps.push(await loadImage('resources/green-4000x4000.png'));
+  for (let i = 0; i < uploadIterations; ++i) {
+    for (let bitmap of bitmaps) {
+      let tex = gl.createTexture();
+      gl.bindTexture(gl.TEXTURE_2D, tex);
+      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
+      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
+      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
+      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
+      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmap);
+      glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
+      wtu.drawUnitQuad(gl);
+      gl.deleteTexture(tex);
+      wtu.checkCanvasRect(gl, 0, 0, width, height, green, "should be green", 0);
+      gl.clear(gl.COLOR_BUFFER_BIT);
+    }
+  }
+  wtu.checkCanvasRect(ctx2d, 0, 0, 300, 150, red, "gpu process restart check canvas should be red", 0);
+  ctx2d.fillStyle = "#00ff00";
+  ctx2d.fillRect(0, 0, 300, 150);
+  wtu.checkCanvasRect(ctx2d, 0, 0, 300, 150, green, "gpu process restart check canvas should be green", 0);
+}
+
+initNonKhronosFramework(true);
+
+test().then(() => {
+  nonKhronosFrameworkNotifyDone()
+});
+var successfullyParsed = true;
+</script>

Added: trunk/LayoutTests/fast/canvas/webgl/resources/green-4000x4000.png


(Binary files differ)
Index: trunk/LayoutTests/fast/canvas/webgl/resources/green-4000x4000.png =================================================================== --- trunk/LayoutTests/fast/canvas/webgl/resources/green-4000x4000.png 2021-11-11 19:07:12 UTC (rev 285643) +++ trunk/LayoutTests/fast/canvas/webgl/resources/green-4000x4000.png 2021-11-11 19:46:00 UTC (rev 285644) Property changes on: trunk/LayoutTests/fast/canvas/webgl/resources/green-4000x4000.png ___________________________________________________________________

Added: svn:mime-type

+image/png \ No newline at end of property

Modified: trunk/Source/WebCore/ChangeLog (285643 => 285644)


--- trunk/Source/WebCore/ChangeLog	2021-11-11 19:07:12 UTC (rev 285643)
+++ trunk/Source/WebCore/ChangeLog	2021-11-11 19:46:00 UTC (rev 285644)
@@ -1,3 +1,20 @@
+2021-11-11  Kimmo Kinnunen  <[email protected]>
+
+        REGRESSION (iOS 15, GPU Process): Fails to load texture map
+        https://bugs.webkit.org/show_bug.cgi?id=231471
+        <rdar://problem/84328146>
+
+        Reviewed by Tim Horton.
+
+        Clear the CGImage copy from the IOSurface CGContext after the IOSurface has
+        been drawn to a bitmap CGContext. The CGImage stored in the IOSurface CGContext
+        contains a malloc buffer of the data. This buffer is counted against GPU
+        process memory consumption, causing GPU process jetsams.
+
+        * platform/graphics/cg/ImageBufferIOSurfaceBackend.cpp:
+        (WebCore::ImageBufferIOSurfaceBackend::draw):
+        * platform/graphics/cg/ImageBufferIOSurfaceBackend.h:
+
 2021-11-11  Alan Bujtas  <[email protected]>
 
         [LFC][IFC] Reset InlineTextItem width on bidi split

Modified: trunk/Source/WebCore/platform/graphics/cg/ImageBufferIOSurfaceBackend.cpp (285643 => 285644)


--- trunk/Source/WebCore/platform/graphics/cg/ImageBufferIOSurfaceBackend.cpp	2021-11-11 19:07:12 UTC (rev 285643)
+++ trunk/Source/WebCore/platform/graphics/cg/ImageBufferIOSurfaceBackend.cpp	2021-11-11 19:46:00 UTC (rev 285644)
@@ -175,6 +175,14 @@
     return NativeImage::create(IOSurface::sinkIntoImage(WTFMove(m_surface)));
 }
 
+void ImageBufferIOSurfaceBackend::draw(GraphicsContext& destContext, const FloatRect& destRect, const FloatRect& srcRect, const ImagePaintingOptions& options)
+{
+    ImageBufferCGBackend::draw(destContext, destRect, srcRect, options);
+    // Accelerated to/from unaccelerated image buffers need complex caching. We trust that
+    // this is a one-off draw, and as such we clear the caches of the source image after each draw.
+    if (destContext.renderingMode() != context().renderingMode())
+        invalidateCachedNativeImage();
+}
 void ImageBufferIOSurfaceBackend::drawConsuming(GraphicsContext& destContext, const FloatRect& destRect, const FloatRect& srcRect, const ImagePaintingOptions& options)
 {
     prepareToDrawIntoContext(destContext);

Modified: trunk/Source/WebCore/platform/graphics/cg/ImageBufferIOSurfaceBackend.h (285643 => 285644)


--- trunk/Source/WebCore/platform/graphics/cg/ImageBufferIOSurfaceBackend.h	2021-11-11 19:07:12 UTC (rev 285643)
+++ trunk/Source/WebCore/platform/graphics/cg/ImageBufferIOSurfaceBackend.h	2021-11-11 19:46:00 UTC (rev 285644)
@@ -58,6 +58,7 @@
     RefPtr<NativeImage> copyNativeImage(BackingStoreCopy = CopyBackingStore) const override;
     RefPtr<NativeImage> sinkIntoNativeImage() override;
 
+    void draw(GraphicsContext& destContext, const FloatRect& destRect, const FloatRect& srcRect, const ImagePaintingOptions&) override;
     void drawConsuming(GraphicsContext&, const FloatRect& destRect, const FloatRect& srcRect, const ImagePaintingOptions&) override;
 
     std::optional<PixelBuffer> getPixelBuffer(const PixelBufferFormat& outputFormat, const IntRect&) const override;

Modified: trunk/Source/WebKit/ChangeLog (285643 => 285644)


--- trunk/Source/WebKit/ChangeLog	2021-11-11 19:07:12 UTC (rev 285643)
+++ trunk/Source/WebKit/ChangeLog	2021-11-11 19:46:00 UTC (rev 285644)
@@ -1,3 +1,22 @@
+2021-11-11  Kimmo Kinnunen  <[email protected]>
+
+        REGRESSION (iOS 15, GPU Process): Fails to load texture map
+        https://bugs.webkit.org/show_bug.cgi?id=231471
+        <rdar://problem/84328146>
+
+        Reviewed by Tim Horton.
+
+        Use the normal "draw image buffer" call to draw the image buffer
+        to the shareable bitmap. Before, it was manually re-implementing this
+        API call via the lower level calls.
+        The drawImageBuffer call now fixes up the IOSurface -backed source
+        ImageBuffers to not keep referencing to the CGImage that provided
+        the data to the draw. Otherwise GPU process jetsams as the CGImage
+        inside IOSurface is counted against GPU process memory limit.
+
+        * GPUProcess/graphics/RemoteRenderingBackend.cpp:
+        (WebKit::RemoteRenderingBackend::getShareableBitmapForImageBufferWithQualifiedIdentifier):
+
 2021-11-11  Tomoki Imai  <[email protected]>
 
         [RemoteInspector][Socket] Can't find the target since r277787

Modified: trunk/Source/WebKit/GPUProcess/graphics/RemoteRenderingBackend.cpp (285643 => 285644)


--- trunk/Source/WebKit/GPUProcess/graphics/RemoteRenderingBackend.cpp	2021-11-11 19:07:12 UTC (rev 285643)
+++ trunk/Source/WebKit/GPUProcess/graphics/RemoteRenderingBackend.cpp	2021-11-11 19:46:00 UTC (rev 285644)
@@ -304,9 +304,6 @@
         auto imageBuffer = m_remoteResourceCache.cachedImageBuffer(identifier);
         if (!imageBuffer)
             return;
-        auto image = imageBuffer->copyNativeImage(WebCore::BackingStoreCopy::DontCopyBackingStore);
-        if (!image)
-            return;
         auto backendSize = imageBuffer->backendSize();
         auto resultSize = preserveResolution == WebCore::PreserveResolution::Yes ? backendSize : imageBuffer->truncatedLogicalSize();
         auto bitmap = ShareableBitmap::createShareable(resultSize, { imageBuffer->colorSpace() });
@@ -315,7 +312,7 @@
         auto context = bitmap->createGraphicsContext();
         if (!context)
             return;
-        context->drawNativeImage(*image, resultSize, FloatRect { { }, resultSize }, FloatRect { { }, backendSize }, { WebCore::CompositeOperator::Copy });
+        context->drawImageBuffer(*imageBuffer, FloatRect { { }, resultSize }, FloatRect { { }, backendSize }, { WebCore::CompositeOperator::Copy });
         bitmap->createHandle(handle);
     }();
     completionHandler(WTFMove(handle));

Modified: trunk/Tools/ChangeLog (285643 => 285644)


--- trunk/Tools/ChangeLog	2021-11-11 19:07:12 UTC (rev 285643)
+++ trunk/Tools/ChangeLog	2021-11-11 19:46:00 UTC (rev 285644)
@@ -1,3 +1,21 @@
+2021-11-11  Kimmo Kinnunen  <[email protected]>
+
+        REGRESSION (iOS 15, GPU Process): Fails to load texture map
+        https://bugs.webkit.org/show_bug.cgi?id=231471
+        <rdar://problem/84328146>
+
+        Reviewed by Tim Horton.
+
+        Add a test that tries to assert that GraphicsContext::drawImageBuffer() does not increase
+        memory footprint of the process. However, leave the test disabled as currently
+        it seems that the WTF::memoryFootprint() measurement does not see all the
+        memory releases the CG does.
+
+        * TestWebKitAPI/Tests/WebCore/ImageBufferTests.cpp:
+        (TestWebKitAPI::memoryFootprintChangedBy):
+        (TestWebKitAPI::imageBufferPixelIs):
+        (TestWebKitAPI::TEST):
+
 2021-11-11  Jer Noble  <[email protected]>
 
         Infinite recursion under InjectedBundlePage::exitFullScreenForElement()

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/ImageBufferTests.cpp (285643 => 285644)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/ImageBufferTests.cpp	2021-11-11 19:07:12 UTC (rev 285643)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/ImageBufferTests.cpp	2021-11-11 19:46:00 UTC (rev 285644)
@@ -25,12 +25,37 @@
 
 #include "config.h"
 
+#include <WebCore/Color.h>
 #include <WebCore/ImageBuffer.h>
 #include <WebCore/PlatformImageBuffer.h>
+#include <cmath>
+#include <wtf/MemoryFootprint.h>
 
 namespace TestWebKitAPI {
 using namespace WebCore;
 
+static ::testing::AssertionResult memoryFootprintChangedBy(size_t& lastFootprint, double expectedChange, double error)
+{
+    WTF::releaseFastMallocFreeMemory();
+    size_t newFootprint = memoryFootprint();
+    size_t oldFootprint = std::exchange(lastFootprint, newFootprint);
+    double change = static_cast<double>(newFootprint) - oldFootprint;
+    if (change - expectedChange > error)
+        return ::testing::AssertionFailure() << "Footprint changed by " << change << ". Expected at most " << expectedChange << "+-" << error;
+    return ::testing::AssertionSuccess();
+}
+
+static ::testing::AssertionResult imageBufferPixelIs(Color expected, ImageBuffer& imageBuffer, int x, int y)
+{
+    PixelBufferFormat format { AlphaPremultiplication::Unpremultiplied, PixelFormat::RGBA8, DestinationColorSpace::SRGB() };
+    auto frontPixelBuffer = imageBuffer.getPixelBuffer(format, { x, y, 1, 1 });
+    auto& data = ""
+    auto got = Color { SRGBA<uint8_t> { data.item(0), data.item(1), data.item(2), data.item(3) } };
+    if (got != expected)
+        return ::testing::AssertionFailure() << "color is not expected."; // FIXME: implement Color <<.
+    return ::testing::AssertionSuccess();
+}
+
 // Tests that the specialized image buffer constructors construct the expected type of object.
 // Test passes if the test compiles, there was a bug where the code wouldn't compile.
 TEST(ImageBufferTests, ImageBufferSubTypeCreateCreatesSubtypes)
@@ -79,26 +104,71 @@
         frontContext.drawImageBuffer(*backImageBuffer, WebCore::FloatPoint { }, { WebCore::CompositeOperator::Copy });
     }
 
-    auto checkGreenPixel = [&](ImageBuffer& imageBuffer, int x, int y) {
-        PixelBufferFormat format { AlphaPremultiplication::Unpremultiplied, PixelFormat::RGBA8, colorSpace };
-        auto frontPixelBuffer = imageBuffer.getPixelBuffer(format, { x, y, 1, 1 });
-        auto& data = ""
+    EXPECT_TRUE(imageBufferPixelIs(Color::green, *frontImageBuffer, fillRect.x() + 1, fillRect.y() + 1));
+    EXPECT_TRUE(imageBufferPixelIs(Color::green, *frontImageBuffer, fillRect.maxX() - 1, fillRect.y() + 1));
+    EXPECT_TRUE(imageBufferPixelIs(Color::green, *frontImageBuffer, fillRect.x() + 1, fillRect.maxY() - 1));
+    EXPECT_TRUE(imageBufferPixelIs(Color::green, *frontImageBuffer, fillRect.maxX() - 1, fillRect.maxY() - 1));
 
-        EXPECT_EQ(data.item(0), 0x00);
-        EXPECT_EQ(data.item(1), 0xff);
-        EXPECT_EQ(data.item(2), 0x00);
-        EXPECT_EQ(data.item(3), 0xff);
-    };
+    EXPECT_TRUE(imageBufferPixelIs(Color::green, *backImageBuffer, fillRect.x() + 1, fillRect.y() + 1));
+    EXPECT_TRUE(imageBufferPixelIs(Color::green, *backImageBuffer, fillRect.maxX() - 1, fillRect.y() + 1));
+    EXPECT_TRUE(imageBufferPixelIs(Color::green, *backImageBuffer, fillRect.x() + 1, fillRect.maxY() - 1));
+    EXPECT_TRUE(imageBufferPixelIs(Color::green, *backImageBuffer, fillRect.maxX() - 1, fillRect.maxY() - 1));
+}
 
-    checkGreenPixel(*frontImageBuffer, fillRect.x()    + 1, fillRect.y()    + 1);
-    checkGreenPixel(*frontImageBuffer, fillRect.maxX() - 1, fillRect.y()    + 1);
-    checkGreenPixel(*frontImageBuffer, fillRect.x()    + 1, fillRect.maxY() - 1);
-    checkGreenPixel(*frontImageBuffer, fillRect.maxX() - 1, fillRect.maxY() - 1);
+// Test that drawing an accelerated ImageBuffer to an unaccelerated does not store extra
+// memory to the accelerated ImageBuffer.
+// FIXME: The test is disabled as it appears that WTF::memoryFootprint() is not exact enough to
+// test that GraphicsContext::drawImageBitmap() does not keep extra memory around.
+// However, if the test is paused at the memory measurement location and the process is inspected
+// manually with the memory tools, the footprint is as expected, e.g. drawBitmapImage does not
+// persist additional memory.
+TEST(ImageBufferTests, DISABLED_DrawImageBufferDoesNotReferenceExtraMemory)
+{
+    auto colorSpace = DestinationColorSpace::SRGB();
+    auto pixelFormat = PixelFormat::BGRA8;
+    FloatSize logicalSize { 4096, 4096 };
+    float scale = 1;
+    size_t footprintError = 1024 * 1024;
+    size_t logicalSizeBytes = logicalSize.width() * logicalSize.height() * 4;
+    // FIXME: Logically this fuzz amount  should not exist.
+    // WTF::memoryFootprint() does not return the same amount of memory as
+    // the `footprint` command or the leak tools.
+    // At the time of writing, the bug case would report drawImageBitmap footprint
+    // as ~130mb, and fixed case would report ~67mb.
+    size_t drawImageBitmapUnaccountedFootprint = logicalSizeBytes + 3 * 1024 * 1024;
 
-    checkGreenPixel(*backImageBuffer, fillRect.x()    + 1, fillRect.y()    + 1);
-    checkGreenPixel(*backImageBuffer, fillRect.maxX() - 1, fillRect.y()    + 1);
-    checkGreenPixel(*backImageBuffer, fillRect.x()    + 1, fillRect.maxY() - 1);
-    checkGreenPixel(*backImageBuffer, fillRect.maxX() - 1, fillRect.maxY() - 1);
-}
+    {
+        // Make potential accelerated drawing backend instantiate roughly the global structures needed for this test.
+        auto accelerated = ImageBuffer::create(logicalSize, RenderingMode::Accelerated, scale, colorSpace, pixelFormat, nullptr);
+        auto fillRect = FloatRect { { }, logicalSize };
+        accelerated->context().fillRect(fillRect, Color::green);
+        EXPECT_TRUE(imageBufferPixelIs(Color::green, *accelerated, fillRect.maxX() - 1, fillRect.maxY() - 1));
+    }
+    WTF::releaseFastMallocFreeMemory();
+    auto initialFootprint = memoryFootprint();
+    auto lastFootprint = initialFootprint;
+    EXPECT_GT(lastFootprint, 0u);
 
+    auto accelerated = ImageBuffer::create(logicalSize, RenderingMode::Accelerated, scale, colorSpace, pixelFormat, nullptr);
+    auto fillRect = FloatRect { { }, logicalSize };
+    accelerated->context().fillRect(fillRect, Color::green);
+    accelerated->flushContext();
+    EXPECT_TRUE(memoryFootprintChangedBy(lastFootprint, logicalSizeBytes, footprintError));
+
+    auto unaccelerated = ImageBuffer::create(logicalSize, RenderingMode::Unaccelerated, scale, colorSpace, pixelFormat, nullptr);
+    unaccelerated->context().fillRect(fillRect, Color::yellow);
+    EXPECT_TRUE(imageBufferPixelIs(Color::yellow, *unaccelerated, fillRect.maxX() - 1, fillRect.maxY() - 1));
+    EXPECT_TRUE(memoryFootprintChangedBy(lastFootprint, logicalSizeBytes, footprintError));
+
+    // The purpose of the whole test is to test that drawImageBuffer does not increase
+    // memory footprint.
+    unaccelerated->context().drawImageBuffer(*accelerated, FloatRect { { }, logicalSize }, FloatRect { { }, logicalSize }, { WebCore::CompositeOperator::Copy });
+    EXPECT_TRUE(imageBufferPixelIs(Color::green, *unaccelerated, fillRect.maxX() - 1, fillRect.maxY() - 1));
+    EXPECT_TRUE(memoryFootprintChangedBy(lastFootprint, 0 + drawImageBitmapUnaccountedFootprint, footprintError));
+    // sleep(10000); // Enable this to inspect the process manually.
+    accelerated = nullptr;
+    unaccelerated = nullptr;
+    lastFootprint = initialFootprint;
+    EXPECT_TRUE(memoryFootprintChangedBy(lastFootprint, 0, footprintError));
 }
+}
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to