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