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