|
Lines 25-36
a/Tools/TestWebKitAPI/Tests/WebCore/ImageBufferTests.cpp_sec1
|
| 25 |
|
25 |
|
| 26 |
#include "config.h" |
26 |
#include "config.h" |
| 27 |
|
27 |
|
|
|
28 |
#include <WebCore/Color.h> |
| 28 |
#include <WebCore/ImageBuffer.h> |
29 |
#include <WebCore/ImageBuffer.h> |
| 29 |
#include <WebCore/PlatformImageBuffer.h> |
30 |
#include <WebCore/PlatformImageBuffer.h> |
|
|
31 |
#include <cmath> |
| 32 |
#include <wtf/MemoryFootprint.h> |
| 30 |
|
33 |
|
| 31 |
namespace TestWebKitAPI { |
34 |
namespace TestWebKitAPI { |
| 32 |
using namespace WebCore; |
35 |
using namespace WebCore; |
| 33 |
|
36 |
|
|
|
37 |
static ::testing::AssertionResult memoryFootprintChangedBy(size_t& lastFootprint, double expectedChange, double error) |
| 38 |
{ |
| 39 |
WTF::releaseFastMallocFreeMemory(); |
| 40 |
size_t newFootprint = memoryFootprint(); |
| 41 |
size_t oldFootprint = std::exchange(lastFootprint, newFootprint); |
| 42 |
double change = static_cast<double>(newFootprint) - oldFootprint; |
| 43 |
if (change - expectedChange > error) |
| 44 |
return ::testing::AssertionFailure() << "Footprint changed by " << change << ". Expected at most " << expectedChange << "+-" << error; |
| 45 |
return ::testing::AssertionSuccess(); |
| 46 |
} |
| 47 |
|
| 48 |
static ::testing::AssertionResult imageBufferPixelIs(Color expected, ImageBuffer& imageBuffer, int x, int y) |
| 49 |
{ |
| 50 |
PixelBufferFormat format { AlphaPremultiplication::Unpremultiplied, PixelFormat::RGBA8, DestinationColorSpace::SRGB() }; |
| 51 |
auto frontPixelBuffer = imageBuffer.getPixelBuffer(format, { x, y, 1, 1 }); |
| 52 |
auto& data = frontPixelBuffer->data(); |
| 53 |
auto got = Color { SRGBA<uint8_t> { data.item(0), data.item(1), data.item(2), data.item(3) } }; |
| 54 |
if (got != expected) |
| 55 |
return ::testing::AssertionFailure() << "color is not expected."; // FIXME: implement Color <<. |
| 56 |
return ::testing::AssertionSuccess(); |
| 57 |
} |
| 58 |
|
| 34 |
// Tests that the specialized image buffer constructors construct the expected type of object. |
59 |
// Tests that the specialized image buffer constructors construct the expected type of object. |
| 35 |
// Test passes if the test compiles, there was a bug where the code wouldn't compile. |
60 |
// Test passes if the test compiles, there was a bug where the code wouldn't compile. |
| 36 |
TEST(ImageBufferTests, ImageBufferSubTypeCreateCreatesSubtypes) |
61 |
TEST(ImageBufferTests, ImageBufferSubTypeCreateCreatesSubtypes) |
|
Lines 79-104
TEST(ImageBufferTests, ImageBufferSubPixelDrawing)
a/Tools/TestWebKitAPI/Tests/WebCore/ImageBufferTests.cpp_sec2
|
| 79 |
frontContext.drawImageBuffer(*backImageBuffer, WebCore::FloatPoint { }, { WebCore::CompositeOperator::Copy }); |
104 |
frontContext.drawImageBuffer(*backImageBuffer, WebCore::FloatPoint { }, { WebCore::CompositeOperator::Copy }); |
| 80 |
} |
105 |
} |
| 81 |
|
106 |
|
| 82 |
auto checkGreenPixel = [&](ImageBuffer& imageBuffer, int x, int y) { |
107 |
EXPECT_TRUE(imageBufferPixelIs(Color::green, *frontImageBuffer, fillRect.x() + 1, fillRect.y() + 1)); |
| 83 |
PixelBufferFormat format { AlphaPremultiplication::Unpremultiplied, PixelFormat::RGBA8, colorSpace }; |
108 |
EXPECT_TRUE(imageBufferPixelIs(Color::green, *frontImageBuffer, fillRect.maxX() - 1, fillRect.y() + 1)); |
| 84 |
auto frontPixelBuffer = imageBuffer.getPixelBuffer(format, { x, y, 1, 1 }); |
109 |
EXPECT_TRUE(imageBufferPixelIs(Color::green, *frontImageBuffer, fillRect.x() + 1, fillRect.maxY() - 1)); |
| 85 |
auto& data = frontPixelBuffer->data(); |
110 |
EXPECT_TRUE(imageBufferPixelIs(Color::green, *frontImageBuffer, fillRect.maxX() - 1, fillRect.maxY() - 1)); |
| 86 |
|
111 |
|
| 87 |
EXPECT_EQ(data.item(0), 0x00); |
112 |
EXPECT_TRUE(imageBufferPixelIs(Color::green, *backImageBuffer, fillRect.x() + 1, fillRect.y() + 1)); |
| 88 |
EXPECT_EQ(data.item(1), 0xff); |
113 |
EXPECT_TRUE(imageBufferPixelIs(Color::green, *backImageBuffer, fillRect.maxX() - 1, fillRect.y() + 1)); |
| 89 |
EXPECT_EQ(data.item(2), 0x00); |
114 |
EXPECT_TRUE(imageBufferPixelIs(Color::green, *backImageBuffer, fillRect.x() + 1, fillRect.maxY() - 1)); |
| 90 |
EXPECT_EQ(data.item(3), 0xff); |
115 |
EXPECT_TRUE(imageBufferPixelIs(Color::green, *backImageBuffer, fillRect.maxX() - 1, fillRect.maxY() - 1)); |
| 91 |
}; |
|
|
| 92 |
|
| 93 |
checkGreenPixel(*frontImageBuffer, fillRect.x() + 1, fillRect.y() + 1); |
| 94 |
checkGreenPixel(*frontImageBuffer, fillRect.maxX() - 1, fillRect.y() + 1); |
| 95 |
checkGreenPixel(*frontImageBuffer, fillRect.x() + 1, fillRect.maxY() - 1); |
| 96 |
checkGreenPixel(*frontImageBuffer, fillRect.maxX() - 1, fillRect.maxY() - 1); |
| 97 |
|
| 98 |
checkGreenPixel(*backImageBuffer, fillRect.x() + 1, fillRect.y() + 1); |
| 99 |
checkGreenPixel(*backImageBuffer, fillRect.maxX() - 1, fillRect.y() + 1); |
| 100 |
checkGreenPixel(*backImageBuffer, fillRect.x() + 1, fillRect.maxY() - 1); |
| 101 |
checkGreenPixel(*backImageBuffer, fillRect.maxX() - 1, fillRect.maxY() - 1); |
| 102 |
} |
116 |
} |
| 103 |
|
117 |
|
|
|
118 |
// Test that drawing an accelerated ImageBuffer to an unaccelerated does not store extra |
| 119 |
// memory to the accelerated ImageBuffer. |
| 120 |
// FIXME: The test is disabled as it appears that WTF::memoryFootprint() is not exact enough to |
| 121 |
// test that GraphicsContext::drawImageBitmap() does not keep extra memory around. |
| 122 |
// However, if the test is paused at the memory measurement location and the process is inspected |
| 123 |
// manually with the memory tools, the footprint is as expected, e.g. drawBitmapImage does not |
| 124 |
// persist additional memory. |
| 125 |
TEST(ImageBufferTests, DISABLED_DrawImageBufferDoesNotReferenceExtraMemory) |
| 126 |
{ |
| 127 |
auto colorSpace = DestinationColorSpace::SRGB(); |
| 128 |
auto pixelFormat = PixelFormat::BGRA8; |
| 129 |
FloatSize logicalSize { 4096, 4096 }; |
| 130 |
float scale = 1; |
| 131 |
size_t footprintError = 1024 * 1024; |
| 132 |
size_t logicalSizeBytes = logicalSize.width() * logicalSize.height() * 4; |
| 133 |
// FIXME: Logically this fuzz amount should not exist. |
| 134 |
// WTF::memoryFootprint() does not return the same amount of memory as |
| 135 |
// the `footprint` command or the leak tools. |
| 136 |
// At the time of writing, the bug case would report drawImageBitmap footprint |
| 137 |
// as ~130mb, and fixed case would report ~67mb. |
| 138 |
size_t drawImageBitmapUnaccountedFootprint = logicalSizeBytes + 3 * 1024 * 1024; |
| 139 |
|
| 140 |
{ |
| 141 |
// Make potential accelerated drawing backend instantiate roughly the global structures needed for this test. |
| 142 |
auto accelerated = ImageBuffer::create(logicalSize, RenderingMode::Accelerated, scale, colorSpace, pixelFormat, nullptr); |
| 143 |
auto fillRect = FloatRect { { }, logicalSize }; |
| 144 |
accelerated->context().fillRect(fillRect, Color::green); |
| 145 |
EXPECT_TRUE(imageBufferPixelIs(Color::green, *accelerated, fillRect.maxX() - 1, fillRect.maxY() - 1)); |
| 146 |
} |
| 147 |
WTF::releaseFastMallocFreeMemory(); |
| 148 |
auto initialFootprint = memoryFootprint(); |
| 149 |
auto lastFootprint = initialFootprint; |
| 150 |
EXPECT_GT(lastFootprint, 0u); |
| 151 |
|
| 152 |
auto accelerated = ImageBuffer::create(logicalSize, RenderingMode::Accelerated, scale, colorSpace, pixelFormat, nullptr); |
| 153 |
auto fillRect = FloatRect { { }, logicalSize }; |
| 154 |
accelerated->context().fillRect(fillRect, Color::green); |
| 155 |
accelerated->flushContext(); |
| 156 |
EXPECT_TRUE(memoryFootprintChangedBy(lastFootprint, logicalSizeBytes, footprintError)); |
| 157 |
|
| 158 |
auto unaccelerated = ImageBuffer::create(logicalSize, RenderingMode::Unaccelerated, scale, colorSpace, pixelFormat, nullptr); |
| 159 |
unaccelerated->context().fillRect(fillRect, Color::yellow); |
| 160 |
EXPECT_TRUE(imageBufferPixelIs(Color::yellow, *unaccelerated, fillRect.maxX() - 1, fillRect.maxY() - 1)); |
| 161 |
EXPECT_TRUE(memoryFootprintChangedBy(lastFootprint, logicalSizeBytes, footprintError)); |
| 162 |
|
| 163 |
// The purpose of the whole test is to test that drawImageBuffer does not increase |
| 164 |
// memory footprint. |
| 165 |
unaccelerated->context().drawImageBuffer(*accelerated, FloatRect { { }, logicalSize }, FloatRect { { }, logicalSize }, { WebCore::CompositeOperator::Copy }); |
| 166 |
EXPECT_TRUE(imageBufferPixelIs(Color::green, *unaccelerated, fillRect.maxX() - 1, fillRect.maxY() - 1)); |
| 167 |
EXPECT_TRUE(memoryFootprintChangedBy(lastFootprint, 0 + drawImageBitmapUnaccountedFootprint, footprintError)); |
| 168 |
// sleep(10000); // Enable this to inspect the process manually. |
| 169 |
accelerated = nullptr; |
| 170 |
unaccelerated = nullptr; |
| 171 |
lastFootprint = initialFootprint; |
| 172 |
EXPECT_TRUE(memoryFootprintChangedBy(lastFootprint, 0, footprintError)); |
| 173 |
} |
| 104 |
} |
174 |
} |