Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Brian Salomon | 77a684f | 2019-08-01 14:38:04 -0400 | [diff] [blame] | 8 | #include "src/gpu/GrDataUtils.h" |
| 9 | |
Mike Klein | 0498431 | 2020-05-19 12:41:05 -0500 | [diff] [blame] | 10 | #include "include/third_party/skcms/skcms.h" |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 11 | #include "src/core/SkColorSpaceXformSteps.h" |
Robert Phillips | 99dead9 | 2020-01-27 16:11:57 -0500 | [diff] [blame] | 12 | #include "src/core/SkCompressedDataUtils.h" |
Brian Salomon | 77a684f | 2019-08-01 14:38:04 -0400 | [diff] [blame] | 13 | #include "src/core/SkConvertPixels.h" |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 14 | #include "src/core/SkMipmap.h" |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 15 | #include "src/core/SkTLazy.h" |
Brian Salomon | c42eb66 | 2019-06-24 17:13:00 -0400 | [diff] [blame] | 16 | #include "src/core/SkTraceEvent.h" |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 17 | #include "src/core/SkUtils.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrColor.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 19 | #include "src/gpu/GrImageInfo.h" |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 20 | |
Robert Phillips | 8043f32 | 2019-05-31 08:11:36 -0400 | [diff] [blame] | 21 | struct ETC1Block { |
| 22 | uint32_t fHigh; |
| 23 | uint32_t fLow; |
| 24 | }; |
| 25 | |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 26 | constexpr uint32_t kDiffBit = 0x2; // set -> differential; not-set -> individual |
| 27 | |
| 28 | static inline int extend_5To8bits(int b) { |
| 29 | int c = b & 0x1f; |
| 30 | return (c << 3) | (c >> 2); |
| 31 | } |
| 32 | |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 33 | static const int kNumETC1ModifierTables = 8; |
| 34 | static const int kNumETC1PixelIndices = 4; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 35 | |
| 36 | // The index of each row in this table is the ETC1 table codeword |
| 37 | // The index of each column in this table is the ETC1 pixel index value |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 38 | static const int kETC1ModifierTables[kNumETC1ModifierTables][kNumETC1PixelIndices] = { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 39 | /* 0 */ { 2, 8, -2, -8 }, |
| 40 | /* 1 */ { 5, 17, -5, -17 }, |
| 41 | /* 2 */ { 9, 29, -9, -29 }, |
| 42 | /* 3 */ { 13, 42, -13, -42 }, |
| 43 | /* 4 */ { 18, 60, -18, -60 }, |
| 44 | /* 5 */ { 24, 80, -24, -80 }, |
| 45 | /* 6 */ { 33, 106, -33, -106 }, |
| 46 | /* 7 */ { 47, 183, -47, -183 } |
| 47 | }; |
| 48 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 49 | // Evaluate one of the entries in 'kModifierTables' to see how close it can get (r8,g8,b8) to |
| 50 | // the original color (rOrig, gOrib, bOrig). |
| 51 | static int test_table_entry(int rOrig, int gOrig, int bOrig, |
| 52 | int r8, int g8, int b8, |
| 53 | int table, int offset) { |
| 54 | SkASSERT(0 <= table && table < 8); |
| 55 | SkASSERT(0 <= offset && offset < 4); |
| 56 | |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 57 | r8 = SkTPin<int>(r8 + kETC1ModifierTables[table][offset], 0, 255); |
| 58 | g8 = SkTPin<int>(g8 + kETC1ModifierTables[table][offset], 0, 255); |
| 59 | b8 = SkTPin<int>(b8 + kETC1ModifierTables[table][offset], 0, 255); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 60 | |
| 61 | return SkTAbs(rOrig - r8) + SkTAbs(gOrig - g8) + SkTAbs(bOrig - b8); |
| 62 | } |
| 63 | |
| 64 | // Create an ETC1 compressed block that is filled with 'col' |
| 65 | static void create_etc1_block(SkColor col, ETC1Block* block) { |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 66 | uint32_t high = 0; |
| 67 | uint32_t low = 0; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 68 | |
| 69 | int rOrig = SkColorGetR(col); |
| 70 | int gOrig = SkColorGetG(col); |
| 71 | int bOrig = SkColorGetB(col); |
| 72 | |
| 73 | int r5 = SkMulDiv255Round(31, rOrig); |
| 74 | int g5 = SkMulDiv255Round(31, gOrig); |
| 75 | int b5 = SkMulDiv255Round(31, bOrig); |
| 76 | |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 77 | int r8 = extend_5To8bits(r5); |
| 78 | int g8 = extend_5To8bits(g5); |
| 79 | int b8 = extend_5To8bits(b5); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 80 | |
Robert Phillips | d4f6831 | 2020-01-31 10:15:05 -0500 | [diff] [blame] | 81 | // We always encode solid color textures in differential mode (i.e., with a 555 base color) but |
| 82 | // with zero diffs (i.e., bits 26-24, 18-16 and 10-8 are left 0). |
| 83 | high |= (r5 << 27) | (g5 << 19) | (b5 << 11) | kDiffBit; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 84 | |
| 85 | int bestTableIndex = 0, bestPixelIndex = 0; |
| 86 | int bestSoFar = 1024; |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 87 | for (int tableIndex = 0; tableIndex < kNumETC1ModifierTables; ++tableIndex) { |
| 88 | for (int pixelIndex = 0; pixelIndex < kNumETC1PixelIndices; ++pixelIndex) { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 89 | int score = test_table_entry(rOrig, gOrig, bOrig, r8, g8, b8, |
| 90 | tableIndex, pixelIndex); |
| 91 | |
| 92 | if (bestSoFar > score) { |
| 93 | bestSoFar = score; |
| 94 | bestTableIndex = tableIndex; |
| 95 | bestPixelIndex = pixelIndex; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 100 | high |= (bestTableIndex << 5) | (bestTableIndex << 2); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 101 | |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 102 | if (bestPixelIndex & 0x1) { |
| 103 | low |= 0xFFFF; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 104 | } |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 105 | if (bestPixelIndex & 0x2) { |
| 106 | low |= 0xFFFF0000; |
| 107 | } |
| 108 | |
| 109 | block->fHigh = SkBSwap32(high); |
| 110 | block->fLow = SkBSwap32(low); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Robert Phillips | 162e04b | 2020-01-28 14:22:43 -0500 | [diff] [blame] | 113 | static int num_4x4_blocks(int size) { |
| 114 | return ((size + 3) & ~3) >> 2; |
Jim Van Verth | e367101 | 2019-09-18 09:53:31 -0400 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int num_ETC1_blocks(int w, int h) { |
Robert Phillips | 162e04b | 2020-01-28 14:22:43 -0500 | [diff] [blame] | 118 | w = num_4x4_blocks(w); |
| 119 | h = num_4x4_blocks(h); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 120 | |
| 121 | return w * h; |
| 122 | } |
| 123 | |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 124 | struct BC1Block { |
| 125 | uint16_t fColor0; |
| 126 | uint16_t fColor1; |
| 127 | uint32_t fIndices; |
| 128 | }; |
| 129 | |
Robert Phillips | ac90802 | 2020-01-14 16:54:17 -0500 | [diff] [blame] | 130 | static uint16_t to565(SkColor col) { |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 131 | int r5 = SkMulDiv255Round(31, SkColorGetR(col)); |
| 132 | int g6 = SkMulDiv255Round(63, SkColorGetG(col)); |
| 133 | int b5 = SkMulDiv255Round(31, SkColorGetB(col)); |
| 134 | |
Robert Phillips | ac90802 | 2020-01-14 16:54:17 -0500 | [diff] [blame] | 135 | return (r5 << 11) | (g6 << 5) | b5; |
| 136 | } |
| 137 | |
| 138 | // Create a BC1 compressed block that has two colors but is initialized to 'col0' |
| 139 | static void create_BC1_block(SkColor col0, SkColor col1, BC1Block* block) { |
| 140 | block->fColor0 = to565(col0); |
| 141 | block->fColor1 = to565(col1); |
Robert Phillips | d095b9f | 2020-02-03 16:12:51 -0500 | [diff] [blame] | 142 | SkASSERT(block->fColor0 <= block->fColor1); // we always assume transparent blocks |
| 143 | |
Robert Phillips | b085527 | 2020-01-15 12:56:52 -0500 | [diff] [blame] | 144 | if (col0 == SK_ColorTRANSPARENT) { |
| 145 | // This sets all 16 pixels to just use color3 (under the assumption |
| 146 | // that this is a kBC1_RGBA8_UNORM texture. Note that in this case |
| 147 | // fColor0 will be opaque black. |
| 148 | block->fIndices = 0xFFFFFFFF; |
| 149 | } else { |
| 150 | // This sets all 16 pixels to just use 'fColor0' |
| 151 | block->fIndices = 0; |
| 152 | } |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 153 | } |
| 154 | |
Jim Van Verth | e367101 | 2019-09-18 09:53:31 -0400 | [diff] [blame] | 155 | size_t GrCompressedRowBytes(SkImage::CompressionType type, int width) { |
| 156 | switch (type) { |
Robert Phillips | ab2b722 | 2019-12-10 10:05:06 -0500 | [diff] [blame] | 157 | case SkImage::CompressionType::kNone: |
| 158 | return 0; |
Robert Phillips | c558f72 | 2020-01-13 13:02:26 -0500 | [diff] [blame] | 159 | case SkImage::CompressionType::kETC2_RGB8_UNORM: |
Robert Phillips | b085527 | 2020-01-15 12:56:52 -0500 | [diff] [blame] | 160 | case SkImage::CompressionType::kBC1_RGB8_UNORM: |
| 161 | case SkImage::CompressionType::kBC1_RGBA8_UNORM: { |
Robert Phillips | 162e04b | 2020-01-28 14:22:43 -0500 | [diff] [blame] | 162 | int numBlocksWidth = num_4x4_blocks(width); |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 163 | |
| 164 | static_assert(sizeof(ETC1Block) == sizeof(BC1Block)); |
Jim Van Verth | e367101 | 2019-09-18 09:53:31 -0400 | [diff] [blame] | 165 | return numBlocksWidth * sizeof(ETC1Block); |
Robert Phillips | b085527 | 2020-01-15 12:56:52 -0500 | [diff] [blame] | 166 | } |
Jim Van Verth | e367101 | 2019-09-18 09:53:31 -0400 | [diff] [blame] | 167 | } |
Robert Phillips | ab2b722 | 2019-12-10 10:05:06 -0500 | [diff] [blame] | 168 | SkUNREACHABLE; |
Jim Van Verth | e367101 | 2019-09-18 09:53:31 -0400 | [diff] [blame] | 169 | } |
| 170 | |
Robert Phillips | 41acc0e | 2020-01-06 13:29:53 -0500 | [diff] [blame] | 171 | SkISize GrCompressedDimensions(SkImage::CompressionType type, SkISize baseDimensions) { |
| 172 | switch (type) { |
| 173 | case SkImage::CompressionType::kNone: |
| 174 | return baseDimensions; |
Robert Phillips | c558f72 | 2020-01-13 13:02:26 -0500 | [diff] [blame] | 175 | case SkImage::CompressionType::kETC2_RGB8_UNORM: |
Robert Phillips | b085527 | 2020-01-15 12:56:52 -0500 | [diff] [blame] | 176 | case SkImage::CompressionType::kBC1_RGB8_UNORM: |
| 177 | case SkImage::CompressionType::kBC1_RGBA8_UNORM: { |
Robert Phillips | 162e04b | 2020-01-28 14:22:43 -0500 | [diff] [blame] | 178 | int numBlocksWidth = num_4x4_blocks(baseDimensions.width()); |
| 179 | int numBlocksHeight = num_4x4_blocks(baseDimensions.height()); |
Robert Phillips | 41acc0e | 2020-01-06 13:29:53 -0500 | [diff] [blame] | 180 | |
| 181 | // Each BC1_RGB8_UNORM and ETC1 block has 16 pixels |
| 182 | return { 4 * numBlocksWidth, 4 * numBlocksHeight }; |
Robert Phillips | b085527 | 2020-01-15 12:56:52 -0500 | [diff] [blame] | 183 | } |
Robert Phillips | 41acc0e | 2020-01-06 13:29:53 -0500 | [diff] [blame] | 184 | } |
| 185 | SkUNREACHABLE; |
| 186 | } |
| 187 | |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 188 | // Fill in 'dest' with ETC1 blocks derived from 'colorf' |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 189 | static void fillin_ETC1_with_color(SkISize dimensions, const SkColor4f& colorf, char* dest) { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 190 | SkColor color = colorf.toSkColor(); |
| 191 | |
| 192 | ETC1Block block; |
| 193 | create_etc1_block(color, &block); |
| 194 | |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 195 | int numBlocks = num_ETC1_blocks(dimensions.width(), dimensions.height()); |
Robert Phillips | 8043f32 | 2019-05-31 08:11:36 -0400 | [diff] [blame] | 196 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 197 | for (int i = 0; i < numBlocks; ++i) { |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 198 | memcpy(dest, &block, sizeof(ETC1Block)); |
| 199 | dest += sizeof(ETC1Block); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 203 | // Fill in 'dest' with BC1 blocks derived from 'colorf' |
| 204 | static void fillin_BC1_with_color(SkISize dimensions, const SkColor4f& colorf, char* dest) { |
| 205 | SkColor color = colorf.toSkColor(); |
| 206 | |
| 207 | BC1Block block; |
Robert Phillips | ac90802 | 2020-01-14 16:54:17 -0500 | [diff] [blame] | 208 | create_BC1_block(color, color, &block); |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 209 | |
| 210 | int numBlocks = num_ETC1_blocks(dimensions.width(), dimensions.height()); |
| 211 | |
| 212 | for (int i = 0; i < numBlocks; ++i) { |
| 213 | memcpy(dest, &block, sizeof(BC1Block)); |
| 214 | dest += sizeof(BC1Block); |
| 215 | } |
| 216 | } |
| 217 | |
Robert Phillips | e073552 | 2020-01-31 11:03:32 -0500 | [diff] [blame] | 218 | #if GR_TEST_UTILS |
| 219 | |
Robert Phillips | ac90802 | 2020-01-14 16:54:17 -0500 | [diff] [blame] | 220 | // Fill in 'dstPixels' with BC1 blocks derived from the 'pixmap'. |
| 221 | void GrTwoColorBC1Compress(const SkPixmap& pixmap, SkColor otherColor, char* dstPixels) { |
Robert Phillips | 99dead9 | 2020-01-27 16:11:57 -0500 | [diff] [blame] | 222 | BC1Block* dstBlocks = reinterpret_cast<BC1Block*>(dstPixels); |
Robert Phillips | ac90802 | 2020-01-14 16:54:17 -0500 | [diff] [blame] | 223 | SkASSERT(pixmap.colorType() == SkColorType::kRGBA_8888_SkColorType); |
| 224 | |
| 225 | BC1Block block; |
| 226 | |
| 227 | // black -> fColor0, otherColor -> fColor1 |
| 228 | create_BC1_block(SK_ColorBLACK, otherColor, &block); |
| 229 | |
Robert Phillips | 162e04b | 2020-01-28 14:22:43 -0500 | [diff] [blame] | 230 | int numXBlocks = num_4x4_blocks(pixmap.width()); |
| 231 | int numYBlocks = num_4x4_blocks(pixmap.height()); |
Robert Phillips | ac90802 | 2020-01-14 16:54:17 -0500 | [diff] [blame] | 232 | |
| 233 | for (int y = 0; y < numYBlocks; ++y) { |
| 234 | for (int x = 0; x < numXBlocks; ++x) { |
| 235 | int shift = 0; |
| 236 | int offsetX = 4 * x, offsetY = 4 * y; |
Robert Phillips | 162e04b | 2020-01-28 14:22:43 -0500 | [diff] [blame] | 237 | block.fIndices = 0; // init all the pixels to color0 (i.e., opaque black) |
Robert Phillips | ac90802 | 2020-01-14 16:54:17 -0500 | [diff] [blame] | 238 | for (int i = 0; i < 4; ++i) { |
Robert Phillips | 162e04b | 2020-01-28 14:22:43 -0500 | [diff] [blame] | 239 | for (int j = 0; j < 4; ++j, shift += 2) { |
Robert Phillips | ac90802 | 2020-01-14 16:54:17 -0500 | [diff] [blame] | 240 | if (offsetX + j >= pixmap.width() || offsetY + i >= pixmap.height()) { |
Robert Phillips | 162e04b | 2020-01-28 14:22:43 -0500 | [diff] [blame] | 241 | // This can happen for the topmost levels of a mipmap and for |
| 242 | // non-multiple of 4 textures |
Robert Phillips | ac90802 | 2020-01-14 16:54:17 -0500 | [diff] [blame] | 243 | continue; |
| 244 | } |
| 245 | |
| 246 | SkColor tmp = pixmap.getColor(offsetX + j, offsetY + i); |
| 247 | if (tmp == SK_ColorTRANSPARENT) { |
| 248 | // For RGBA BC1 images color3 is set to transparent black |
| 249 | block.fIndices |= 3 << shift; |
| 250 | } else if (tmp != SK_ColorBLACK) { |
| 251 | block.fIndices |= 1 << shift; // color1 |
| 252 | } |
Robert Phillips | ac90802 | 2020-01-14 16:54:17 -0500 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | dstBlocks[y*numXBlocks + x] = block; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
Robert Phillips | e073552 | 2020-01-31 11:03:32 -0500 | [diff] [blame] | 261 | #endif |
| 262 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 263 | size_t GrComputeTightCombinedBufferSize(size_t bytesPerPixel, SkISize baseDimensions, |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 264 | SkTArray<size_t>* individualMipOffsets, int mipLevelCount) { |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 265 | SkASSERT(individualMipOffsets && !individualMipOffsets->count()); |
| 266 | SkASSERT(mipLevelCount >= 1); |
| 267 | |
| 268 | individualMipOffsets->push_back(0); |
| 269 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 270 | size_t combinedBufferSize = baseDimensions.width() * bytesPerPixel * baseDimensions.height(); |
| 271 | SkISize levelDimensions = baseDimensions; |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 272 | |
| 273 | // The Vulkan spec for copying a buffer to an image requires that the alignment must be at |
| 274 | // least 4 bytes and a multiple of the bytes per pixel of the image config. |
| 275 | SkASSERT(bytesPerPixel == 1 || bytesPerPixel == 2 || bytesPerPixel == 3 || |
| 276 | bytesPerPixel == 4 || bytesPerPixel == 8 || bytesPerPixel == 16); |
| 277 | int desiredAlignment = (bytesPerPixel == 3) ? 12 : (bytesPerPixel > 4 ? bytesPerPixel : 4); |
| 278 | |
| 279 | for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; ++currentMipLevel) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 280 | levelDimensions = {std::max(1, levelDimensions.width() /2), |
| 281 | std::max(1, levelDimensions.height()/2)}; |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 282 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 283 | size_t trimmedSize = levelDimensions.area() * bytesPerPixel; |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 284 | const size_t alignmentDiff = combinedBufferSize % desiredAlignment; |
| 285 | if (alignmentDiff != 0) { |
| 286 | combinedBufferSize += desiredAlignment - alignmentDiff; |
| 287 | } |
| 288 | SkASSERT((0 == combinedBufferSize % 4) && (0 == combinedBufferSize % bytesPerPixel)); |
| 289 | |
| 290 | individualMipOffsets->push_back(combinedBufferSize); |
| 291 | combinedBufferSize += trimmedSize; |
| 292 | } |
| 293 | |
| 294 | SkASSERT(individualMipOffsets->count() == mipLevelCount); |
| 295 | return combinedBufferSize; |
| 296 | } |
| 297 | |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 298 | void GrFillInCompressedData(SkImage::CompressionType type, SkISize dimensions, |
| 299 | GrMipMapped mipMapped, char* dstPixels, const SkColor4f& colorf) { |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 300 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 301 | |
| 302 | int numMipLevels = 1; |
| 303 | if (mipMapped == GrMipMapped::kYes) { |
Mike Reed | 13711eb | 2020-07-14 17:16:32 -0400 | [diff] [blame] | 304 | numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1; |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 307 | size_t offset = 0; |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 308 | |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 309 | for (int i = 0; i < numMipLevels; ++i) { |
Robert Phillips | 99dead9 | 2020-01-27 16:11:57 -0500 | [diff] [blame] | 310 | size_t levelSize = SkCompressedDataSize(type, dimensions, nullptr, false); |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 311 | |
Robert Phillips | c558f72 | 2020-01-13 13:02:26 -0500 | [diff] [blame] | 312 | if (SkImage::CompressionType::kETC2_RGB8_UNORM == type) { |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 313 | fillin_ETC1_with_color(dimensions, colorf, &dstPixels[offset]); |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 314 | } else { |
Robert Phillips | b085527 | 2020-01-15 12:56:52 -0500 | [diff] [blame] | 315 | SkASSERT(type == SkImage::CompressionType::kBC1_RGB8_UNORM || |
| 316 | type == SkImage::CompressionType::kBC1_RGBA8_UNORM); |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 317 | fillin_BC1_with_color(dimensions, colorf, &dstPixels[offset]); |
Robert Phillips | 48257d7 | 2019-12-16 14:20:53 -0500 | [diff] [blame] | 318 | } |
Robert Phillips | 8f259a0 | 2019-12-20 11:32:27 -0500 | [diff] [blame] | 319 | |
| 320 | offset += levelSize; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 321 | dimensions = {std::max(1, dimensions.width()/2), std::max(1, dimensions.height()/2)}; |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 325 | static GrSwizzle get_load_and_src_swizzle(GrColorType ct, SkRasterPipeline::StockStage* load, |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 326 | bool* isNormalized, bool* isSRGB) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 327 | GrSwizzle swizzle("rgba"); |
| 328 | *isNormalized = true; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 329 | *isSRGB = false; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 330 | switch (ct) { |
| 331 | case GrColorType::kAlpha_8: *load = SkRasterPipeline::load_a8; break; |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 332 | case GrColorType::kAlpha_16: *load = SkRasterPipeline::load_a16; break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 333 | case GrColorType::kBGR_565: *load = SkRasterPipeline::load_565; break; |
| 334 | case GrColorType::kABGR_4444: *load = SkRasterPipeline::load_4444; break; |
Greg Daniel | 746460e | 2020-06-30 13:13:53 -0400 | [diff] [blame] | 335 | case GrColorType::kARGB_4444: swizzle = GrSwizzle("bgra"); |
| 336 | *load = SkRasterPipeline::load_4444; break; |
| 337 | case GrColorType::kBGRA_4444: swizzle = GrSwizzle("gbar"); |
| 338 | *load = SkRasterPipeline::load_4444; break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 339 | case GrColorType::kRGBA_8888: *load = SkRasterPipeline::load_8888; break; |
| 340 | case GrColorType::kRG_88: *load = SkRasterPipeline::load_rg88; break; |
| 341 | case GrColorType::kRGBA_1010102: *load = SkRasterPipeline::load_1010102; break; |
Robert Phillips | 9a30ee0 | 2020-04-29 08:58:39 -0400 | [diff] [blame] | 342 | case GrColorType::kBGRA_1010102: *load = SkRasterPipeline::load_1010102; |
| 343 | swizzle = GrSwizzle("bgra"); |
| 344 | break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 345 | case GrColorType::kAlpha_F16: *load = SkRasterPipeline::load_af16; break; |
| 346 | case GrColorType::kRGBA_F16_Clamped: *load = SkRasterPipeline::load_f16; break; |
| 347 | case GrColorType::kRG_1616: *load = SkRasterPipeline::load_rg1616; break; |
| 348 | case GrColorType::kRGBA_16161616: *load = SkRasterPipeline::load_16161616; break; |
| 349 | |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 350 | case GrColorType::kRGBA_8888_SRGB: *load = SkRasterPipeline::load_8888; |
| 351 | *isSRGB = true; |
| 352 | break; |
Brian Salomon | e14cfbe | 2019-06-24 15:00:58 -0400 | [diff] [blame] | 353 | case GrColorType::kRG_F16: *load = SkRasterPipeline::load_rgf16; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 354 | *isNormalized = false; |
| 355 | break; |
| 356 | case GrColorType::kRGBA_F16: *load = SkRasterPipeline::load_f16; |
| 357 | *isNormalized = false; |
| 358 | break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 359 | case GrColorType::kRGBA_F32: *load = SkRasterPipeline::load_f32; |
| 360 | *isNormalized = false; |
| 361 | break; |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 362 | case GrColorType::kAlpha_8xxx: *load = SkRasterPipeline::load_8888; |
| 363 | swizzle = GrSwizzle("000r"); |
| 364 | break; |
| 365 | case GrColorType::kAlpha_F32xxx: *load = SkRasterPipeline::load_f32; |
| 366 | swizzle = GrSwizzle("000r"); |
| 367 | break; |
| 368 | case GrColorType::kGray_8xxx: *load = SkRasterPipeline::load_8888; |
| 369 | swizzle = GrSwizzle("rrr1"); |
| 370 | break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 371 | case GrColorType::kGray_8: *load = SkRasterPipeline::load_a8; |
| 372 | swizzle = GrSwizzle("aaa1"); |
| 373 | break; |
| 374 | case GrColorType::kBGRA_8888: *load = SkRasterPipeline::load_8888; |
| 375 | swizzle = GrSwizzle("bgra"); |
| 376 | break; |
| 377 | case GrColorType::kRGB_888x: *load = SkRasterPipeline::load_8888; |
| 378 | swizzle = GrSwizzle("rgb1"); |
| 379 | break; |
| 380 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 381 | // These are color types we don't expect to ever have to load. |
| 382 | case GrColorType::kRGB_888: |
| 383 | case GrColorType::kR_8: |
| 384 | case GrColorType::kR_16: |
| 385 | case GrColorType::kR_F16: |
| 386 | case GrColorType::kGray_F16: |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 387 | case GrColorType::kUnknown: |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 388 | SK_ABORT("unexpected CT"); |
| 389 | } |
| 390 | return swizzle; |
| 391 | } |
| 392 | |
| 393 | static GrSwizzle get_dst_swizzle_and_store(GrColorType ct, SkRasterPipeline::StockStage* store, |
Brian Salomon | b3bd864 | 2019-11-04 16:59:29 -0500 | [diff] [blame] | 394 | bool* doLumToAlpha, bool* isNormalized, bool* isSRGB) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 395 | GrSwizzle swizzle("rgba"); |
| 396 | *isNormalized = true; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 397 | *isSRGB = false; |
Brian Salomon | 998937c | 2019-10-29 09:34:52 -0400 | [diff] [blame] | 398 | *doLumToAlpha = false; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 399 | switch (ct) { |
| 400 | case GrColorType::kAlpha_8: *store = SkRasterPipeline::store_a8; break; |
Robert Phillips | 429f0d3 | 2019-09-11 17:03:28 -0400 | [diff] [blame] | 401 | case GrColorType::kAlpha_16: *store = SkRasterPipeline::store_a16; break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 402 | case GrColorType::kBGR_565: *store = SkRasterPipeline::store_565; break; |
| 403 | case GrColorType::kABGR_4444: *store = SkRasterPipeline::store_4444; break; |
Greg Daniel | 746460e | 2020-06-30 13:13:53 -0400 | [diff] [blame] | 404 | case GrColorType::kARGB_4444: swizzle = GrSwizzle("bgra"); |
| 405 | *store = SkRasterPipeline::store_4444; break; |
| 406 | case GrColorType::kBGRA_4444: swizzle = GrSwizzle("argb"); |
| 407 | *store = SkRasterPipeline::store_4444; break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 408 | case GrColorType::kRGBA_8888: *store = SkRasterPipeline::store_8888; break; |
| 409 | case GrColorType::kRG_88: *store = SkRasterPipeline::store_rg88; break; |
| 410 | case GrColorType::kRGBA_1010102: *store = SkRasterPipeline::store_1010102; break; |
Robert Phillips | 9a30ee0 | 2020-04-29 08:58:39 -0400 | [diff] [blame] | 411 | case GrColorType::kBGRA_1010102: swizzle = GrSwizzle("bgra"); |
| 412 | *store = SkRasterPipeline::store_1010102; |
| 413 | break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 414 | case GrColorType::kRGBA_F16_Clamped: *store = SkRasterPipeline::store_f16; break; |
| 415 | case GrColorType::kRG_1616: *store = SkRasterPipeline::store_rg1616; break; |
| 416 | case GrColorType::kRGBA_16161616: *store = SkRasterPipeline::store_16161616; break; |
| 417 | |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 418 | case GrColorType::kRGBA_8888_SRGB: *store = SkRasterPipeline::store_8888; |
| 419 | *isSRGB = true; |
| 420 | break; |
Brian Salomon | e14cfbe | 2019-06-24 15:00:58 -0400 | [diff] [blame] | 421 | case GrColorType::kRG_F16: *store = SkRasterPipeline::store_rgf16; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 422 | *isNormalized = false; |
| 423 | break; |
| 424 | case GrColorType::kAlpha_F16: *store = SkRasterPipeline::store_af16; |
| 425 | *isNormalized = false; |
| 426 | break; |
| 427 | case GrColorType::kRGBA_F16: *store = SkRasterPipeline::store_f16; |
| 428 | *isNormalized = false; |
| 429 | break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 430 | case GrColorType::kRGBA_F32: *store = SkRasterPipeline::store_f32; |
| 431 | *isNormalized = false; |
| 432 | break; |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 433 | case GrColorType::kAlpha_8xxx: *store = SkRasterPipeline::store_8888; |
| 434 | swizzle = GrSwizzle("a000"); |
| 435 | break; |
| 436 | case GrColorType::kAlpha_F32xxx: *store = SkRasterPipeline::store_f32; |
| 437 | swizzle = GrSwizzle("a000"); |
| 438 | break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 439 | case GrColorType::kBGRA_8888: swizzle = GrSwizzle("bgra"); |
| 440 | *store = SkRasterPipeline::store_8888; |
| 441 | break; |
| 442 | case GrColorType::kRGB_888x: swizzle = GrSwizzle("rgb1"); |
| 443 | *store = SkRasterPipeline::store_8888; |
| 444 | break; |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 445 | case GrColorType::kR_8: swizzle = GrSwizzle("agbr"); |
| 446 | *store = SkRasterPipeline::store_a8; |
| 447 | break; |
| 448 | case GrColorType::kR_16: swizzle = GrSwizzle("agbr"); |
| 449 | *store = SkRasterPipeline::store_a16; |
| 450 | break; |
| 451 | case GrColorType::kR_F16: swizzle = GrSwizzle("agbr"); |
| 452 | *store = SkRasterPipeline::store_af16; |
| 453 | break; |
| 454 | case GrColorType::kGray_F16: *doLumToAlpha = true; |
| 455 | *store = SkRasterPipeline::store_af16; |
| 456 | break; |
Brian Salomon | 998937c | 2019-10-29 09:34:52 -0400 | [diff] [blame] | 457 | case GrColorType::kGray_8: *doLumToAlpha = true; |
| 458 | *store = SkRasterPipeline::store_a8; |
| 459 | break; |
| 460 | case GrColorType::kGray_8xxx: *doLumToAlpha = true; |
| 461 | *store = SkRasterPipeline::store_8888; |
| 462 | swizzle = GrSwizzle("a000"); |
| 463 | break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 464 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 465 | // These are color types we don't expect to ever have to store. |
| 466 | case GrColorType::kRGB_888: // This is handled specially in GrConvertPixels. |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 467 | case GrColorType::kUnknown: |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 468 | SK_ABORT("unexpected CT"); |
| 469 | } |
| 470 | return swizzle; |
| 471 | } |
| 472 | |
| 473 | static inline void append_clamp_gamut(SkRasterPipeline* pipeline) { |
| 474 | // SkRasterPipeline may not know our color type and also doesn't like caller to directly |
| 475 | // append clamp_gamut. Fake it out. |
| 476 | static SkImageInfo fakeII = SkImageInfo::MakeN32Premul(1, 1); |
| 477 | pipeline->append_gamut_clamp_if_normalized(fakeII); |
| 478 | } |
| 479 | |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 480 | bool GrConvertPixels(const GrImageInfo& dstInfo, void* dst, size_t dstRB, |
| 481 | const GrImageInfo& srcInfo, const void* src, size_t srcRB, |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 482 | bool flipY) { |
Brian Salomon | c42eb66 | 2019-06-24 17:13:00 -0400 | [diff] [blame] | 483 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 484 | if (srcInfo.colorType() == GrColorType::kRGB_888) { |
| 485 | // We don't expect to have to convert from this format. |
| 486 | return false; |
| 487 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 488 | if (!srcInfo.isValid() || !dstInfo.isValid()) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 489 | return false; |
| 490 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 491 | if (!src || !dst) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 492 | return false; |
| 493 | } |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 494 | if (dstInfo.dimensions() != srcInfo.dimensions()) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 495 | return false; |
| 496 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 497 | if (dstRB < dstInfo.minRowBytes() || srcRB < srcInfo.minRowBytes()) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 498 | return false; |
| 499 | } |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 500 | if (dstInfo.colorType() == GrColorType::kRGB_888) { |
| 501 | // SkRasterPipeline doesn't handle writing to RGB_888. So we have it write to RGB_888x and |
| 502 | // then do another conversion that does the 24bit packing. |
| 503 | auto tempDstInfo = dstInfo.makeColorType(GrColorType::kRGB_888x); |
| 504 | auto tempRB = tempDstInfo.minRowBytes(); |
| 505 | std::unique_ptr<char[]> tempDst(new char[tempRB * tempDstInfo.height()]); |
| 506 | if (!GrConvertPixels(tempDstInfo, tempDst.get(), tempRB, srcInfo, src, srcRB, flipY)) { |
| 507 | return false; |
| 508 | } |
| 509 | auto* tRow = reinterpret_cast<const char*>(tempDst.get()); |
| 510 | auto* dRow = reinterpret_cast<char*>(dst); |
| 511 | for (int y = 0; y < dstInfo.height(); ++y, tRow += tempRB, dRow += dstRB) { |
| 512 | for (int x = 0; x < dstInfo.width(); ++x) { |
| 513 | auto t = reinterpret_cast<const uint32_t*>(tRow + x * sizeof(uint32_t)); |
| 514 | auto d = reinterpret_cast<uint32_t*>(dRow + x * 3); |
| 515 | memcpy(d, t, 3); |
| 516 | } |
| 517 | } |
| 518 | return true; |
| 519 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 520 | |
| 521 | size_t srcBpp = srcInfo.bpp(); |
| 522 | size_t dstBpp = dstInfo.bpp(); |
| 523 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 524 | // SkRasterPipeline operates on row-pixels not row-bytes. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 525 | SkASSERT(dstRB % dstBpp == 0); |
| 526 | SkASSERT(srcRB % srcBpp == 0); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 527 | |
Brian Salomon | 77a684f | 2019-08-01 14:38:04 -0400 | [diff] [blame] | 528 | bool premul = srcInfo.alphaType() == kUnpremul_SkAlphaType && |
| 529 | dstInfo.alphaType() == kPremul_SkAlphaType; |
| 530 | bool unpremul = srcInfo.alphaType() == kPremul_SkAlphaType && |
| 531 | dstInfo.alphaType() == kUnpremul_SkAlphaType; |
| 532 | bool alphaOrCSConversion = |
| 533 | premul || unpremul || !SkColorSpace::Equals(srcInfo.colorSpace(), dstInfo.colorSpace()); |
| 534 | |
| 535 | if (srcInfo.colorType() == dstInfo.colorType() && !alphaOrCSConversion) { |
| 536 | size_t tightRB = dstBpp * dstInfo.width(); |
| 537 | if (flipY) { |
| 538 | dst = static_cast<char*>(dst) + dstRB * (dstInfo.height() - 1); |
| 539 | for (int y = 0; y < dstInfo.height(); ++y) { |
| 540 | memcpy(dst, src, tightRB); |
| 541 | src = static_cast<const char*>(src) + srcRB; |
| 542 | dst = static_cast< char*>(dst) - dstRB; |
| 543 | } |
| 544 | } else { |
| 545 | SkRectMemcpy(dst, dstRB, src, srcRB, tightRB, srcInfo.height()); |
| 546 | } |
| 547 | return true; |
| 548 | } |
| 549 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 550 | SkRasterPipeline::StockStage load; |
| 551 | bool srcIsNormalized; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 552 | bool srcIsSRGB; |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 553 | auto loadSwizzle = |
| 554 | get_load_and_src_swizzle(srcInfo.colorType(), &load, &srcIsNormalized, &srcIsSRGB); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 555 | |
| 556 | SkRasterPipeline::StockStage store; |
Brian Salomon | 998937c | 2019-10-29 09:34:52 -0400 | [diff] [blame] | 557 | bool doLumToAlpha; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 558 | bool dstIsNormalized; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 559 | bool dstIsSRGB; |
Brian Salomon | 998937c | 2019-10-29 09:34:52 -0400 | [diff] [blame] | 560 | auto storeSwizzle = get_dst_swizzle_and_store(dstInfo.colorType(), &store, &doLumToAlpha, |
| 561 | &dstIsNormalized, &dstIsSRGB); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 562 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 563 | bool clampGamut; |
| 564 | SkTLazy<SkColorSpaceXformSteps> steps; |
| 565 | GrSwizzle loadStoreSwizzle; |
| 566 | if (alphaOrCSConversion) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 567 | steps.init(srcInfo.colorSpace(), srcInfo.alphaType(), |
| 568 | dstInfo.colorSpace(), dstInfo.alphaType()); |
| 569 | clampGamut = dstIsNormalized && dstInfo.alphaType() == kPremul_SkAlphaType; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 570 | } else { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 571 | clampGamut = |
| 572 | dstIsNormalized && !srcIsNormalized && dstInfo.alphaType() == kPremul_SkAlphaType; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 573 | if (!clampGamut) { |
| 574 | loadStoreSwizzle = GrSwizzle::Concat(loadSwizzle, storeSwizzle); |
| 575 | } |
| 576 | } |
| 577 | int cnt = 1; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 578 | int height = srcInfo.height(); |
| 579 | SkRasterPipeline_MemoryCtx srcCtx{const_cast<void*>(src), SkToInt(srcRB / srcBpp)}, |
| 580 | dstCtx{ dst , SkToInt(dstRB / dstBpp)}; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 581 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 582 | if (flipY) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 583 | // It *almost* works to point the src at the last row and negate the stride and run the |
| 584 | // whole rectangle. However, SkRasterPipeline::run()'s control loop uses size_t loop |
| 585 | // variables so it winds up relying on unsigned overflow math. It works out in practice |
| 586 | // but UBSAN says "no!" as it's technically undefined and in theory a compiler could emit |
| 587 | // code that didn't do what is intended. So we go one row at a time. :( |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 588 | srcCtx.pixels = static_cast<char*>(srcCtx.pixels) + srcRB * (height - 1); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 589 | std::swap(cnt, height); |
| 590 | } |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 591 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 592 | bool hasConversion = alphaOrCSConversion || clampGamut || doLumToAlpha; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 593 | |
| 594 | if (srcIsSRGB && dstIsSRGB && !hasConversion) { |
| 595 | // No need to convert from srgb if we are just going to immediately convert it back. |
| 596 | srcIsSRGB = dstIsSRGB = false; |
| 597 | } |
| 598 | |
| 599 | hasConversion = hasConversion || srcIsSRGB || dstIsSRGB; |
| 600 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 601 | for (int i = 0; i < cnt; ++i) { |
| 602 | SkRasterPipeline_<256> pipeline; |
| 603 | pipeline.append(load, &srcCtx); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 604 | if (hasConversion) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 605 | loadSwizzle.apply(&pipeline); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 606 | if (srcIsSRGB) { |
Mike Klein | 0498431 | 2020-05-19 12:41:05 -0500 | [diff] [blame] | 607 | pipeline.append_transfer_function(*skcms_sRGB_TransferFunction()); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 608 | } |
| 609 | if (alphaOrCSConversion) { |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 610 | steps->apply(&pipeline); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 611 | } |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 612 | if (clampGamut) { |
| 613 | append_clamp_gamut(&pipeline); |
| 614 | } |
Brian Salomon | 998937c | 2019-10-29 09:34:52 -0400 | [diff] [blame] | 615 | if (doLumToAlpha) { |
| 616 | pipeline.append(SkRasterPipeline::StockStage::bt709_luminance_or_luma_to_alpha); |
| 617 | // If we ever needed to convert from linear-encoded gray to sRGB-encoded |
Mike Klein | 0498431 | 2020-05-19 12:41:05 -0500 | [diff] [blame] | 618 | // gray we'd have a problem here because the subsequent transfer function stage |
Brian Salomon | 998937c | 2019-10-29 09:34:52 -0400 | [diff] [blame] | 619 | // ignores the alpha channel (where we just stashed the gray). There are |
| 620 | // several ways that could be fixed but given our current set of color types |
| 621 | // this should never happen. |
| 622 | SkASSERT(!dstIsSRGB); |
| 623 | } |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 624 | if (dstIsSRGB) { |
Mike Klein | 0498431 | 2020-05-19 12:41:05 -0500 | [diff] [blame] | 625 | pipeline.append_transfer_function(*skcms_sRGB_Inverse_TransferFunction()); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 626 | } |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 627 | storeSwizzle.apply(&pipeline); |
| 628 | } else { |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 629 | loadStoreSwizzle.apply(&pipeline); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 630 | } |
| 631 | pipeline.append(store, &dstCtx); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 632 | pipeline.run(0, 0, srcInfo.width(), height); |
| 633 | srcCtx.pixels = static_cast<char*>(srcCtx.pixels) - srcRB; |
| 634 | dstCtx.pixels = static_cast<char*>(dstCtx.pixels) + dstRB; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 635 | } |
| 636 | return true; |
| 637 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 638 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 639 | bool GrClearImage(const GrImageInfo& dstInfo, void* dst, size_t dstRB, SkColor4f color) { |
Brian Salomon | 998937c | 2019-10-29 09:34:52 -0400 | [diff] [blame] | 640 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | 998937c | 2019-10-29 09:34:52 -0400 | [diff] [blame] | 641 | |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 642 | if (!dstInfo.isValid()) { |
| 643 | return false; |
Brian Salomon | 998937c | 2019-10-29 09:34:52 -0400 | [diff] [blame] | 644 | } |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 645 | if (!dst) { |
| 646 | return false; |
| 647 | } |
| 648 | if (dstRB < dstInfo.minRowBytes()) { |
| 649 | return false; |
| 650 | } |
| 651 | if (dstInfo.colorType() == GrColorType::kRGB_888) { |
| 652 | // SkRasterPipeline doesn't handle writing to RGB_888. So we handle that specially here. |
| 653 | uint32_t rgba = color.toBytes_RGBA(); |
| 654 | for (int y = 0; y < dstInfo.height(); ++y) { |
| 655 | char* d = static_cast<char*>(dst) + y * dstRB; |
| 656 | for (int x = 0; x < dstInfo.width(); ++x, d += 3) { |
| 657 | memcpy(d, &rgba, 3); |
| 658 | } |
| 659 | } |
| 660 | return true; |
| 661 | } |
| 662 | |
| 663 | bool doLumToAlpha; |
| 664 | bool isNormalized; |
| 665 | bool dstIsSRGB; |
| 666 | SkRasterPipeline::StockStage store; |
| 667 | GrSwizzle storeSwizzle = get_dst_swizzle_and_store(dstInfo.colorType(), &store, &doLumToAlpha, |
| 668 | &isNormalized, &dstIsSRGB); |
| 669 | char block[64]; |
| 670 | SkArenaAlloc alloc(block, sizeof(block), 1024); |
| 671 | SkRasterPipeline_<256> pipeline; |
| 672 | pipeline.append_constant_color(&alloc, color); |
| 673 | if (doLumToAlpha) { |
| 674 | pipeline.append(SkRasterPipeline::StockStage::bt709_luminance_or_luma_to_alpha); |
| 675 | // If we ever needed to convert from linear-encoded gray to sRGB-encoded |
Mike Klein | 0498431 | 2020-05-19 12:41:05 -0500 | [diff] [blame] | 676 | // gray we'd have a problem here because the subsequent transfer function stage |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 677 | // ignores the alpha channel (where we just stashed the gray). There are |
| 678 | // several ways that could be fixed but given our current set of color types |
| 679 | // this should never happen. |
| 680 | SkASSERT(!dstIsSRGB); |
| 681 | } |
| 682 | if (dstIsSRGB) { |
Mike Klein | 0498431 | 2020-05-19 12:41:05 -0500 | [diff] [blame] | 683 | pipeline.append_transfer_function(*skcms_sRGB_Inverse_TransferFunction()); |
Brian Salomon | 85c3d68 | 2019-11-04 15:04:54 -0500 | [diff] [blame] | 684 | } |
| 685 | storeSwizzle.apply(&pipeline); |
| 686 | SkRasterPipeline_MemoryCtx dstCtx{dst, SkToInt(dstRB/dstInfo.bpp())}; |
| 687 | pipeline.append(store, &dstCtx); |
| 688 | pipeline.run(0, 0, dstInfo.width(), dstInfo.height()); |
| 689 | |
| 690 | return true; |
Brian Salomon | 998937c | 2019-10-29 09:34:52 -0400 | [diff] [blame] | 691 | } |
| 692 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 693 | GrColorType SkColorTypeAndFormatToGrColorType(const GrCaps* caps, |
| 694 | SkColorType skCT, |
| 695 | const GrBackendFormat& format) { |
| 696 | GrColorType grCT = SkColorTypeToGrColorType(skCT); |
| 697 | // Until we support SRGB in the SkColorType we have to do this manual check here to make sure |
| 698 | // we use the correct GrColorType. |
| 699 | if (caps->isFormatSRGB(format)) { |
| 700 | if (grCT != GrColorType::kRGBA_8888) { |
| 701 | return GrColorType::kUnknown; |
| 702 | } |
| 703 | grCT = GrColorType::kRGBA_8888_SRGB; |
| 704 | } |
| 705 | return grCT; |
| 706 | } |