| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkSwizzler.h" | 8 #include "SkSwizzler.h" |
| 9 #include "Test.h" | 9 #include "Test.h" |
| 10 | 10 |
| 11 // These are the values that we will look for to indicate that the fill was succ
essful | 11 // These are the values that we will look for to indicate that the fill was succ
essful |
| 12 static const uint8_t kFillIndex = 0x1; | 12 static const uint8_t kFillIndex = 0x11; |
| 13 static const uint32_t kFillColor = 0x22334455; | 13 static const uint8_t kFillGray = 0x22; |
| 14 static const uint16_t kFill565 = 0x3344; |
| 15 static const uint32_t kFillColor = 0x55667788; |
| 14 | 16 |
| 15 static void check_fill(skiatest::Reporter* r, | 17 static void check_fill(skiatest::Reporter* r, |
| 16 const SkImageInfo& imageInfo, | 18 const SkImageInfo& imageInfo, |
| 17 uint32_t startRow, | 19 uint32_t startRow, |
| 18 uint32_t endRow, | 20 uint32_t endRow, |
| 19 size_t rowBytes, | 21 size_t rowBytes, |
| 20 uint32_t offset, | 22 uint32_t offset, |
| 21 uint32_t colorOrIndex, | 23 uint32_t colorOrIndex) { |
| 22 SkPMColor* colorTable) { | |
| 23 | 24 |
| 24 // Calculate the total size of the image in bytes. Use the smallest possibl
e size. | 25 // Calculate the total size of the image in bytes. Use the smallest possibl
e size. |
| 25 // The offset value tells us to adjust the pointer from the memory we alloca
te in order | 26 // The offset value tells us to adjust the pointer from the memory we alloca
te in order |
| 26 // to test on different memory alignments. If offset is nonzero, we need to
increase the | 27 // to test on different memory alignments. If offset is nonzero, we need to
increase the |
| 27 // size of the memory we allocate in order to make sure that we have enough.
We are | 28 // size of the memory we allocate in order to make sure that we have enough.
We are |
| 28 // still allocating the smallest possible size. | 29 // still allocating the smallest possible size. |
| 29 const size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset; | 30 const size_t totalBytes = imageInfo.getSafeSize(rowBytes) + offset; |
| 30 | 31 |
| 31 // Create fake image data where every byte has a value of 0 | 32 // Create fake image data where every byte has a value of 0 |
| 32 SkAutoTDeleteArray<uint8_t> storage(new uint8_t[totalBytes]); | 33 SkAutoTDeleteArray<uint8_t> storage(new uint8_t[totalBytes]); |
| 33 memset(storage.get(), 0, totalBytes); | 34 memset(storage.get(), 0, totalBytes); |
| 34 // Adjust the pointer in order to test on different memory alignments | 35 // Adjust the pointer in order to test on different memory alignments |
| 35 uint8_t* imageData = storage.get() + offset; | 36 uint8_t* imageData = storage.get() + offset; |
| 36 uint8_t* imageStart = imageData + rowBytes * startRow; | 37 uint8_t* imageStart = imageData + rowBytes * startRow; |
| 37 | 38 |
| 38 // Fill image with the fill value starting at the indicated row | 39 // Fill image with the fill value starting at the indicated row |
| 39 SkSwizzler::Fill(imageStart, imageInfo, rowBytes, endRow - startRow + 1, col
orOrIndex, | 40 const SkImageInfo fillInfo = imageInfo.makeWH(imageInfo.width(), endRow - st
artRow + 1); |
| 40 colorTable, SkCodec::kNo_ZeroInitialized); | 41 SkSwizzler::Fill(imageStart, fillInfo, rowBytes, colorOrIndex, SkCodec::kNo_
ZeroInitialized); |
| 41 | 42 |
| 42 // Ensure that the pixels are filled properly | 43 // Ensure that the pixels are filled properly |
| 43 // The bots should catch any memory corruption | 44 // The bots should catch any memory corruption |
| 44 uint8_t* indexPtr = imageData + startRow * rowBytes; | 45 uint8_t* indexPtr = imageData + startRow * rowBytes; |
| 45 uint8_t* grayPtr = indexPtr; | 46 uint8_t* grayPtr = indexPtr; |
| 46 uint32_t* colorPtr = (uint32_t*) indexPtr; | 47 uint32_t* colorPtr = (uint32_t*) indexPtr; |
| 48 uint16_t* color565Ptr = (uint16_t*) indexPtr; |
| 47 for (uint32_t y = startRow; y <= endRow; y++) { | 49 for (uint32_t y = startRow; y <= endRow; y++) { |
| 48 for (int32_t x = 0; x < imageInfo.width(); x++) { | 50 for (int32_t x = 0; x < imageInfo.width(); x++) { |
| 49 switch (imageInfo.colorType()) { | 51 switch (imageInfo.colorType()) { |
| 50 case kIndex_8_SkColorType: | 52 case kIndex_8_SkColorType: |
| 51 REPORTER_ASSERT(r, kFillIndex == indexPtr[x]); | 53 REPORTER_ASSERT(r, kFillIndex == indexPtr[x]); |
| 52 break; | 54 break; |
| 53 case kN32_SkColorType: | 55 case kN32_SkColorType: |
| 54 REPORTER_ASSERT(r, kFillColor == colorPtr[x]); | 56 REPORTER_ASSERT(r, kFillColor == colorPtr[x]); |
| 55 break; | 57 break; |
| 56 case kGray_8_SkColorType: | 58 case kGray_8_SkColorType: |
| 57 // We always fill kGray with black | 59 REPORTER_ASSERT(r, kFillGray == grayPtr[x]); |
| 58 REPORTER_ASSERT(r, (uint8_t) kFillColor == grayPtr[x]); | 60 break; |
| 61 case kRGB_565_SkColorType: |
| 62 REPORTER_ASSERT(r, kFill565 == color565Ptr[x]); |
| 59 break; | 63 break; |
| 60 default: | 64 default: |
| 61 REPORTER_ASSERT(r, false); | 65 REPORTER_ASSERT(r, false); |
| 62 break; | 66 break; |
| 63 } | 67 } |
| 64 } | 68 } |
| 65 indexPtr += rowBytes; | 69 indexPtr += rowBytes; |
| 66 colorPtr = (uint32_t*) indexPtr; | 70 colorPtr = (uint32_t*) indexPtr; |
| 67 } | 71 } |
| 68 } | 72 } |
| 69 | 73 |
| 70 // Test Fill() with different combinations of dimensions, alignment, and padding | 74 // Test Fill() with different combinations of dimensions, alignment, and padding |
| 71 DEF_TEST(SwizzlerFill, r) { | 75 DEF_TEST(SwizzlerFill, r) { |
| 72 // Set up a color table | |
| 73 SkPMColor colorTable[kFillIndex + 1]; | |
| 74 colorTable[kFillIndex] = kFillColor; | |
| 75 // Apart from the fill index, we will leave the other colors in the color ta
ble uninitialized. | |
| 76 // If we incorrectly try to fill with this uninitialized memory, the bots wi
ll catch it. | |
| 77 | |
| 78 // Test on an invalid width and representative widths | 76 // Test on an invalid width and representative widths |
| 79 const uint32_t widths[] = { 0, 10, 50 }; | 77 const uint32_t widths[] = { 0, 10, 50 }; |
| 80 | 78 |
| 81 // In order to call Fill(), there must be at least one row to fill | 79 // In order to call Fill(), there must be at least one row to fill |
| 82 // Test on the smallest possible height and representative heights | 80 // Test on the smallest possible height and representative heights |
| 83 const uint32_t heights[] = { 1, 5, 10 }; | 81 const uint32_t heights[] = { 1, 5, 10 }; |
| 84 | 82 |
| 85 // Test on interesting possibilities for row padding | 83 // Test on interesting possibilities for row padding |
| 86 const uint32_t paddings[] = { 0, 1, 2, 3, 4 }; | 84 const uint32_t paddings[] = { 0, 1, 2, 3, 4 }; |
| 87 | 85 |
| 88 // Iterate over test dimensions | 86 // Iterate over test dimensions |
| 89 for (uint32_t width : widths) { | 87 for (uint32_t width : widths) { |
| 90 for (uint32_t height : heights) { | 88 for (uint32_t height : heights) { |
| 91 | 89 |
| 92 // Create image info objects | 90 // Create image info objects |
| 93 const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, | 91 const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, kU
nknown_SkAlphaType); |
| 94 kUnknown_SkAlphaType); | 92 const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColor
Type); |
| 95 const SkImageInfo indexInfo = colorInfo.makeColorType(kIndex_8_SkCol
orType); | 93 const SkImageInfo indexInfo = colorInfo.makeColorType(kIndex_8_SkCol
orType); |
| 96 const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColor
Type); | 94 const SkImageInfo color565Info = colorInfo.makeColorType(kRGB_565_Sk
ColorType); |
| 97 | 95 |
| 98 for (uint32_t padding : paddings) { | 96 for (uint32_t padding : paddings) { |
| 99 | 97 |
| 100 // Calculate row bytes | 98 // Calculate row bytes |
| 101 size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType
) * width + | 99 const size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkCol
orType) * width |
| 102 padding; | 100 + padding; |
| 103 size_t indexRowBytes = width + padding; | 101 const size_t indexRowBytes = width + padding; |
| 104 size_t grayRowBytes = indexRowBytes; | 102 const size_t grayRowBytes = indexRowBytes; |
| 103 const size_t color565RowBytes = |
| 104 SkColorTypeBytesPerPixel(kRGB_565_SkColorType) * width +
padding; |
| 105 | 105 |
| 106 // If there is padding, we can invent an offset to change the me
mory alignment | 106 // If there is padding, we can invent an offset to change the me
mory alignment |
| 107 for (uint32_t offset = 0; offset <= padding; offset++) { | 107 for (uint32_t offset = 0; offset <= padding; offset++) { |
| 108 | 108 |
| 109 // Test all possible start rows with all possible end rows | 109 // Test all possible start rows with all possible end rows |
| 110 for (uint32_t startRow = 0; startRow < height; startRow++) { | 110 for (uint32_t startRow = 0; startRow < height; startRow++) { |
| 111 for (uint32_t endRow = startRow; endRow < height; endRow
++) { | 111 for (uint32_t endRow = startRow; endRow < height; endRow
++) { |
| 112 | 112 |
| 113 // Fill with an index that we use to look up a color | 113 // Test fill with each color type |
| 114 check_fill(r, colorInfo, startRow, endRow, colorRowB
ytes, offset, | 114 check_fill(r, colorInfo, startRow, endRow, colorRowB
ytes, offset, |
| 115 kFillIndex, colorTable); | 115 kFillColor); |
| 116 | |
| 117 // Fill with a color | |
| 118 check_fill(r, colorInfo, startRow, endRow, colorRowB
ytes, offset, | |
| 119 kFillColor, nullptr); | |
| 120 | |
| 121 // Fill with an index | |
| 122 check_fill(r, indexInfo, startRow, endRow, indexRowB
ytes, offset, | 116 check_fill(r, indexInfo, startRow, endRow, indexRowB
ytes, offset, |
| 123 kFillIndex, nullptr); | 117 kFillIndex); |
| 124 | |
| 125 // Fill a grayscale image | |
| 126 check_fill(r, grayInfo, startRow, endRow, grayRowByt
es, offset, | 118 check_fill(r, grayInfo, startRow, endRow, grayRowByt
es, offset, |
| 127 kFillColor, nullptr); | 119 kFillGray); |
| 120 check_fill(r, color565Info, startRow, endRow, color5
65RowBytes, offset, |
| 121 kFill565); |
| 128 } | 122 } |
| 129 } | 123 } |
| 130 } | 124 } |
| 131 } | 125 } |
| 132 } | 126 } |
| 133 } | 127 } |
| 134 } | 128 } |
| OLD | NEW |