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 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 8 | #include "SkImageInfoPriv.h" |
tomhudson | 734351d | 2016-03-23 10:51:20 -0700 | [diff] [blame] | 9 | #include "SkSwizzle.h" |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 10 | #include "SkSwizzler.h" |
| 11 | #include "Test.h" |
mtklein | 55c86ab | 2016-01-08 06:32:52 -0800 | [diff] [blame] | 12 | #include "SkOpts.h" |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 13 | |
| 14 | // 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] | 15 | static const uint8_t kFillGray = 0x22; |
| 16 | static const uint16_t kFill565 = 0x3344; |
| 17 | static const uint32_t kFillColor = 0x55667788; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 18 | |
| 19 | static void check_fill(skiatest::Reporter* r, |
| 20 | const SkImageInfo& imageInfo, |
| 21 | uint32_t startRow, |
msarett | 3c309db | 2015-04-10 14:36:48 -0700 | [diff] [blame] | 22 | uint32_t endRow, |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 23 | size_t rowBytes, |
| 24 | uint32_t offset, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 25 | uint32_t colorOrIndex) { |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 26 | |
| 27 | // Calculate the total size of the image in bytes. Use the smallest possible size. |
| 28 | // The offset value tells us to adjust the pointer from the memory we allocate in order |
| 29 | // to test on different memory alignments. If offset is nonzero, we need to increase the |
| 30 | // size of the memory we allocate in order to make sure that we have enough. We are |
| 31 | // still allocating the smallest possible size. |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 32 | const size_t totalBytes = imageInfo.computeByteSize(rowBytes) + offset; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 33 | |
| 34 | // Create fake image data where every byte has a value of 0 |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 35 | std::unique_ptr<uint8_t[]> storage(new uint8_t[totalBytes]); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 36 | memset(storage.get(), 0, totalBytes); |
| 37 | // Adjust the pointer in order to test on different memory alignments |
| 38 | uint8_t* imageData = storage.get() + offset; |
msarett | 3c309db | 2015-04-10 14:36:48 -0700 | [diff] [blame] | 39 | uint8_t* imageStart = imageData + rowBytes * startRow; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 40 | const SkImageInfo fillInfo = imageInfo.makeWH(imageInfo.width(), endRow - startRow + 1); |
| 41 | SkSampler::Fill(fillInfo, imageStart, rowBytes, colorOrIndex, SkCodec::kNo_ZeroInitialized); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 42 | |
| 43 | // Ensure that the pixels are filled properly |
| 44 | // The bots should catch any memory corruption |
| 45 | uint8_t* indexPtr = imageData + startRow * rowBytes; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 46 | uint8_t* grayPtr = indexPtr; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 47 | uint32_t* colorPtr = (uint32_t*) indexPtr; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 48 | uint16_t* color565Ptr = (uint16_t*) indexPtr; |
msarett | 3c309db | 2015-04-10 14:36:48 -0700 | [diff] [blame] | 49 | for (uint32_t y = startRow; y <= endRow; y++) { |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 50 | for (int32_t x = 0; x < imageInfo.width(); x++) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 51 | switch (imageInfo.colorType()) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 52 | case kN32_SkColorType: |
| 53 | REPORTER_ASSERT(r, kFillColor == colorPtr[x]); |
| 54 | break; |
| 55 | case kGray_8_SkColorType: |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 56 | REPORTER_ASSERT(r, kFillGray == grayPtr[x]); |
| 57 | break; |
| 58 | case kRGB_565_SkColorType: |
| 59 | REPORTER_ASSERT(r, kFill565 == color565Ptr[x]); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 60 | break; |
| 61 | default: |
| 62 | REPORTER_ASSERT(r, false); |
| 63 | break; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | indexPtr += rowBytes; |
| 67 | colorPtr = (uint32_t*) indexPtr; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Test Fill() with different combinations of dimensions, alignment, and padding |
| 72 | DEF_TEST(SwizzlerFill, r) { |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 73 | // Test on an invalid width and representative widths |
| 74 | const uint32_t widths[] = { 0, 10, 50 }; |
| 75 | |
| 76 | // In order to call Fill(), there must be at least one row to fill |
| 77 | // Test on the smallest possible height and representative heights |
| 78 | const uint32_t heights[] = { 1, 5, 10 }; |
| 79 | |
| 80 | // Test on interesting possibilities for row padding |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 81 | const uint32_t paddings[] = { 0, 4 }; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 82 | |
| 83 | // Iterate over test dimensions |
| 84 | for (uint32_t width : widths) { |
| 85 | for (uint32_t height : heights) { |
| 86 | |
| 87 | // Create image info objects |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 88 | const SkImageInfo colorInfo = SkImageInfo::MakeN32(width, height, kUnknown_SkAlphaType); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 89 | const SkImageInfo grayInfo = colorInfo.makeColorType(kGray_8_SkColorType); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 90 | const SkImageInfo color565Info = colorInfo.makeColorType(kRGB_565_SkColorType); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 91 | |
| 92 | for (uint32_t padding : paddings) { |
| 93 | |
| 94 | // Calculate row bytes |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 95 | const size_t colorRowBytes = SkColorTypeBytesPerPixel(kN32_SkColorType) * width |
| 96 | + padding; |
| 97 | const size_t indexRowBytes = width + padding; |
| 98 | const size_t grayRowBytes = indexRowBytes; |
| 99 | const size_t color565RowBytes = |
| 100 | SkColorTypeBytesPerPixel(kRGB_565_SkColorType) * width + padding; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 101 | |
| 102 | // 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] | 103 | for (uint32_t offset = 0; offset <= padding; offset += 4) { |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 104 | |
msarett | 3c309db | 2015-04-10 14:36:48 -0700 | [diff] [blame] | 105 | // Test all possible start rows with all possible end rows |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 106 | for (uint32_t startRow = 0; startRow < height; startRow++) { |
msarett | 3c309db | 2015-04-10 14:36:48 -0700 | [diff] [blame] | 107 | for (uint32_t endRow = startRow; endRow < height; endRow++) { |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 108 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 109 | // Test fill with each color type |
msarett | 3c309db | 2015-04-10 14:36:48 -0700 | [diff] [blame] | 110 | check_fill(r, colorInfo, startRow, endRow, colorRowBytes, offset, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 111 | kFillColor); |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 112 | check_fill(r, grayInfo, startRow, endRow, grayRowBytes, offset, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 113 | kFillGray); |
| 114 | check_fill(r, color565Info, startRow, endRow, color565RowBytes, offset, |
| 115 | kFill565); |
msarett | 3c309db | 2015-04-10 14:36:48 -0700 | [diff] [blame] | 116 | } |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
mtklein | 55c86ab | 2016-01-08 06:32:52 -0800 | [diff] [blame] | 123 | |
| 124 | DEF_TEST(SwizzleOpts, r) { |
| 125 | uint32_t dst, src; |
| 126 | |
| 127 | // forall c, c*255 == c, c*0 == 0 |
| 128 | for (int c = 0; c <= 255; c++) { |
| 129 | src = (255<<24) | c; |
mtklein | 8bf7b79 | 2016-01-22 07:42:53 -0800 | [diff] [blame] | 130 | SkOpts::RGBA_to_rgbA(&dst, &src, 1); |
mtklein | 55c86ab | 2016-01-08 06:32:52 -0800 | [diff] [blame] | 131 | REPORTER_ASSERT(r, dst == src); |
mtklein | 8bf7b79 | 2016-01-22 07:42:53 -0800 | [diff] [blame] | 132 | SkOpts::RGBA_to_bgrA(&dst, &src, 1); |
mtklein | 55c86ab | 2016-01-08 06:32:52 -0800 | [diff] [blame] | 133 | REPORTER_ASSERT(r, dst == (uint32_t)((255<<24) | (c<<16))); |
| 134 | |
| 135 | src = (0<<24) | c; |
mtklein | 8bf7b79 | 2016-01-22 07:42:53 -0800 | [diff] [blame] | 136 | SkOpts::RGBA_to_rgbA(&dst, &src, 1); |
mtklein | 55c86ab | 2016-01-08 06:32:52 -0800 | [diff] [blame] | 137 | REPORTER_ASSERT(r, dst == 0); |
mtklein | 8bf7b79 | 2016-01-22 07:42:53 -0800 | [diff] [blame] | 138 | SkOpts::RGBA_to_bgrA(&dst, &src, 1); |
mtklein | 55c86ab | 2016-01-08 06:32:52 -0800 | [diff] [blame] | 139 | REPORTER_ASSERT(r, dst == 0); |
| 140 | } |
| 141 | |
| 142 | // check a totally arbitrary color |
| 143 | src = 0xFACEB004; |
mtklein | 8bf7b79 | 2016-01-22 07:42:53 -0800 | [diff] [blame] | 144 | SkOpts::RGBA_to_rgbA(&dst, &src, 1); |
mtklein | 55c86ab | 2016-01-08 06:32:52 -0800 | [diff] [blame] | 145 | REPORTER_ASSERT(r, dst == 0xFACAAD04); |
| 146 | |
| 147 | // swap red and blue |
mtklein | 8bf7b79 | 2016-01-22 07:42:53 -0800 | [diff] [blame] | 148 | SkOpts::RGBA_to_BGRA(&dst, &src, 1); |
mtklein | 55c86ab | 2016-01-08 06:32:52 -0800 | [diff] [blame] | 149 | REPORTER_ASSERT(r, dst == 0xFA04B0CE); |
| 150 | |
| 151 | // all together now |
mtklein | 8bf7b79 | 2016-01-22 07:42:53 -0800 | [diff] [blame] | 152 | SkOpts::RGBA_to_bgrA(&dst, &src, 1); |
mtklein | 55c86ab | 2016-01-08 06:32:52 -0800 | [diff] [blame] | 153 | REPORTER_ASSERT(r, dst == 0xFA04ADCA); |
| 154 | } |
tomhudson | 734351d | 2016-03-23 10:51:20 -0700 | [diff] [blame] | 155 | |
| 156 | DEF_TEST(PublicSwizzleOpts, r) { |
| 157 | uint32_t dst, src; |
| 158 | |
| 159 | // check a totally arbitrary color |
| 160 | src = 0xFACEB004; |
| 161 | SkSwapRB(&dst, &src, 1); |
| 162 | REPORTER_ASSERT(r, dst == 0xFA04B0CE); |
| 163 | } |