Chromium Code Reviews| Index: tests/SwizzlerTest.cpp |
| diff --git a/tests/SwizzlerTest.cpp b/tests/SwizzlerTest.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e55904a28b00aed29eebf8f52623f702ade9d0b3 |
| --- /dev/null |
| +++ b/tests/SwizzlerTest.cpp |
| @@ -0,0 +1,105 @@ |
| +/* |
| + * Copyright 2015 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +#include "SkSwizzler.h" |
| +#include "Test.h" |
| + |
| +// These are the values that we will look for to indicate that the fill was successful |
| +static const uint8_t fillIndex = 0x1; |
|
scroggo
2015/04/09 14:53:48
nit: Typically we name our constants:
kFillIndex
msarett
2015/04/09 16:26:32
Done.
|
| +static const uint32_t fillColor = 0x22334455; |
| + |
| +static void check_fill(skiatest::Reporter* r, |
| + const SkImageInfo& imageInfo, |
| + uint32_t startRow, |
| + size_t rowBytes, |
| + uint32_t offset, |
| + uint32_t colorOrIndex, |
| + SkPMColor* colorTable) { |
| + |
| + // Calculate the total size of the image in bytes. Use the smallest possible size. |
|
scroggo
2015/04/09 14:53:48
nit: Might be nice to explain the offset. (We're s
msarett
2015/04/09 16:26:32
Yes that would be helpful.
|
| + size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset; |
|
scroggo
2015/04/09 14:53:48
nit: could be const.
msarett
2015/04/09 16:26:32
Done.
|
| + |
| + // Create fake image data where every byte has a value of 0 |
| + SkAutoTDelete<uint8_t> storage((uint8_t*) sk_malloc_throw(totalBytes)); |
| + memset(storage.get(), 0, totalBytes); |
| + uint8_t* imageData = storage.get() + offset; |
| + |
| + // Fill image with the fill value starting at the indicated row |
| + SkSwizzler::Fill(imageData, imageInfo, rowBytes, startRow, colorOrIndex, colorTable); |
| + |
| + // Ensure that the pixels are filled properly |
| + // The bots should catch any memory corruption |
| + uint8_t* indexPtr = imageData + startRow * rowBytes; |
| + uint32_t* colorPtr = (uint32_t*) indexPtr; |
| + for (int32_t y = startRow; y < imageInfo.height(); y++) { |
| + for (int32_t x = 0; x < imageInfo.width(); x++) { |
| + if (kIndex_8_SkColorType == imageInfo.colorType()) { |
| + REPORTER_ASSERT(r, fillIndex == indexPtr[x]); |
| + } else { |
| + REPORTER_ASSERT(r, fillColor == colorPtr[x]); |
| + } |
| + } |
| + indexPtr += rowBytes; |
| + colorPtr = (uint32_t*) indexPtr; |
| + } |
| +} |
| + |
| +// Test Fill() with different combinations of dimensions, alignment, and padding |
| +DEF_TEST(SwizzlerFill, r) { |
| + // Set up a color table |
| + SkPMColor colorTable[fillIndex + 1]; |
| + colorTable[fillIndex] = fillColor; |
| + |
| + // Test on an invalid width and representative widths |
| + const uint32_t widths[] = { 0, 10, 50 }; |
| + |
| + // In order to call Fill(), there must be at least one row to fill |
| + // Test on the smallest possible height and representative heights |
| + const uint32_t heights[] = { 1, 5, 10 }; |
| + |
| + // Test on interesting possibilities for row padding |
| + const uint32_t paddings[] = { 0, 1, 2, 3, 4 }; |
| + |
| + // Iterate over test dimensions |
| + for (uint32_t width : widths) { |
| + for (uint32_t height : heights) { |
| + |
| + // Create image info objects |
| + const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, |
| + kUnknown_SkAlphaType); |
| + const SkImageInfo indexInfo = colorInfo.makeColorType(kIndex_8_SkColorType); |
| + |
| + for (uint32_t padding : paddings) { |
| + |
| + // Calculate row bytes |
| + size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width + |
| + padding; |
| + size_t indexRowBytes = width + padding; |
| + |
| + // If there is padding, we can invent an offset to change the memory alignment |
| + for (uint32_t offset = 0; offset <= padding; offset++) { |
| + |
| + // Test all possible start rows |
| + for (uint32_t startRow = 0; startRow < height; startRow++) { |
| + |
| + // Fill with an index that we use to look up a color |
| + check_fill(r, colorInfo, startRow, colorRowBytes, offset, fillIndex, |
| + colorTable); |
| + |
| + // Fill with a color |
| + check_fill(r, colorInfo, startRow, colorRowBytes, offset, fillColor, |
| + NULL); |
| + |
| + // Fill with an index |
| + check_fill(r, indexInfo, startRow, indexRowBytes, offset, fillIndex, |
| + NULL); |
| + } |
| + } |
| + } |
| + } |
| + } |
| +} |