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 | |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrDataUtils.h" |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 9 | #include "src/core/SkColorSpaceXformSteps.h" |
| 10 | #include "src/core/SkTLazy.h" |
Brian Salomon | c42eb66 | 2019-06-24 17:13:00 -0400 | [diff] [blame] | 11 | #include "src/core/SkTraceEvent.h" |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 12 | #include "src/core/SkUtils.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrColor.h" |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 14 | |
Robert Phillips | 8043f32 | 2019-05-31 08:11:36 -0400 | [diff] [blame] | 15 | struct ETC1Block { |
| 16 | uint32_t fHigh; |
| 17 | uint32_t fLow; |
| 18 | }; |
| 19 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 20 | static const int kNumModifierTables = 8; |
| 21 | static const int kNumPixelIndices = 4; |
| 22 | |
| 23 | // The index of each row in this table is the ETC1 table codeword |
| 24 | // The index of each column in this table is the ETC1 pixel index value |
| 25 | static const int kModifierTables[kNumModifierTables][kNumPixelIndices] = { |
| 26 | /* 0 */ { 2, 8, -2, -8 }, |
| 27 | /* 1 */ { 5, 17, -5, -17 }, |
| 28 | /* 2 */ { 9, 29, -9, -29 }, |
| 29 | /* 3 */ { 13, 42, -13, -42 }, |
| 30 | /* 4 */ { 18, 60, -18, -60 }, |
| 31 | /* 5 */ { 24, 80, -24, -80 }, |
| 32 | /* 6 */ { 33, 106, -33, -106 }, |
| 33 | /* 7 */ { 47, 183, -47, -183 } |
| 34 | }; |
| 35 | |
| 36 | static inline int convert_5To8(int b) { |
| 37 | int c = b & 0x1f; |
| 38 | return (c << 3) | (c >> 2); |
| 39 | } |
| 40 | |
| 41 | // Evaluate one of the entries in 'kModifierTables' to see how close it can get (r8,g8,b8) to |
| 42 | // the original color (rOrig, gOrib, bOrig). |
| 43 | static int test_table_entry(int rOrig, int gOrig, int bOrig, |
| 44 | int r8, int g8, int b8, |
| 45 | int table, int offset) { |
| 46 | SkASSERT(0 <= table && table < 8); |
| 47 | SkASSERT(0 <= offset && offset < 4); |
| 48 | |
| 49 | r8 = SkTPin<uint8_t>(r8 + kModifierTables[table][offset], 0, 255); |
| 50 | g8 = SkTPin<uint8_t>(g8 + kModifierTables[table][offset], 0, 255); |
| 51 | b8 = SkTPin<uint8_t>(b8 + kModifierTables[table][offset], 0, 255); |
| 52 | |
| 53 | return SkTAbs(rOrig - r8) + SkTAbs(gOrig - g8) + SkTAbs(bOrig - b8); |
| 54 | } |
| 55 | |
| 56 | // Create an ETC1 compressed block that is filled with 'col' |
| 57 | static void create_etc1_block(SkColor col, ETC1Block* block) { |
| 58 | block->fHigh = 0; |
| 59 | block->fLow = 0; |
| 60 | |
| 61 | int rOrig = SkColorGetR(col); |
| 62 | int gOrig = SkColorGetG(col); |
| 63 | int bOrig = SkColorGetB(col); |
| 64 | |
| 65 | int r5 = SkMulDiv255Round(31, rOrig); |
| 66 | int g5 = SkMulDiv255Round(31, gOrig); |
| 67 | int b5 = SkMulDiv255Round(31, bOrig); |
| 68 | |
| 69 | int r8 = convert_5To8(r5); |
| 70 | int g8 = convert_5To8(g5); |
| 71 | int b8 = convert_5To8(b5); |
| 72 | |
| 73 | // We always encode solid color textures as 555 + zero diffs |
| 74 | block->fHigh |= (r5 << 27) | (g5 << 19) | (b5 << 11) | 0x2; |
| 75 | |
| 76 | int bestTableIndex = 0, bestPixelIndex = 0; |
| 77 | int bestSoFar = 1024; |
| 78 | for (int tableIndex = 0; tableIndex < kNumModifierTables; ++tableIndex) { |
| 79 | for (int pixelIndex = 0; pixelIndex < kNumPixelIndices; ++pixelIndex) { |
| 80 | int score = test_table_entry(rOrig, gOrig, bOrig, r8, g8, b8, |
| 81 | tableIndex, pixelIndex); |
| 82 | |
| 83 | if (bestSoFar > score) { |
| 84 | bestSoFar = score; |
| 85 | bestTableIndex = tableIndex; |
| 86 | bestPixelIndex = pixelIndex; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | block->fHigh |= (bestTableIndex << 5) | (bestTableIndex << 2); |
| 92 | |
| 93 | for (int i = 0; i < 16; ++i) { |
| 94 | block->fLow |= bestPixelIndex << 2*i; |
| 95 | } |
| 96 | } |
| 97 | |
Robert Phillips | 8043f32 | 2019-05-31 08:11:36 -0400 | [diff] [blame] | 98 | static int num_ETC1_blocks(int w, int h) { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 99 | if (w < 4) { |
| 100 | w = 1; |
| 101 | } else { |
| 102 | SkASSERT((w & 3) == 0); |
| 103 | w >>= 2; |
| 104 | } |
| 105 | |
| 106 | if (h < 4) { |
| 107 | h = 1; |
| 108 | } else { |
| 109 | SkASSERT((h & 3) == 0); |
| 110 | h >>= 2; |
| 111 | } |
| 112 | |
| 113 | return w * h; |
| 114 | } |
| 115 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 116 | size_t GrCompressedDataSize(SkImage::CompressionType type, int width, int height) { |
| 117 | switch (type) { |
| 118 | case SkImage::kETC1_CompressionType: |
| 119 | int numBlocks = num_ETC1_blocks(width, height); |
| 120 | return numBlocks * sizeof(ETC1Block); |
| 121 | } |
| 122 | SK_ABORT("Unexpected compression type"); |
| 123 | return 0; |
Robert Phillips | 8043f32 | 2019-05-31 08:11:36 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 126 | // Fill in 'dest' with ETC1 blocks derived from 'colorf' |
| 127 | static void fillin_ETC1_with_color(int width, int height, const SkColor4f& colorf, void* dest) { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 128 | SkColor color = colorf.toSkColor(); |
| 129 | |
| 130 | ETC1Block block; |
| 131 | create_etc1_block(color, &block); |
| 132 | |
Robert Phillips | 8043f32 | 2019-05-31 08:11:36 -0400 | [diff] [blame] | 133 | int numBlocks = num_ETC1_blocks(width, height); |
| 134 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 135 | for (int i = 0; i < numBlocks; ++i) { |
Robert Phillips | e3bd673 | 2019-05-29 14:20:35 -0400 | [diff] [blame] | 136 | ((ETC1Block*)dest)[i] = block; |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 140 | // Fill in the width x height 'dest' with the munged version of 'colorf' that matches 'config' |
| 141 | static bool fill_buffer_with_color(GrPixelConfig config, int width, int height, |
| 142 | const SkColor4f& colorf, void* dest) { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 143 | SkASSERT(kRGB_ETC1_GrPixelConfig != config); |
| 144 | |
| 145 | GrColor color = colorf.toBytes_RGBA(); |
| 146 | |
| 147 | uint8_t r = GrColorUnpackR(color); |
| 148 | uint8_t g = GrColorUnpackG(color); |
| 149 | uint8_t b = GrColorUnpackB(color); |
| 150 | uint8_t a = GrColorUnpackA(color); |
| 151 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 152 | switch (config) { |
| 153 | case kAlpha_8_GrPixelConfig: // fall through |
| 154 | case kAlpha_8_as_Alpha_GrPixelConfig: // fall through |
| 155 | case kAlpha_8_as_Red_GrPixelConfig: { |
| 156 | memset(dest, a, width * height); |
| 157 | break; |
| 158 | } |
| 159 | case kGray_8_GrPixelConfig: // fall through |
| 160 | case kGray_8_as_Lum_GrPixelConfig: // fall through |
| 161 | case kGray_8_as_Red_GrPixelConfig: { |
| 162 | uint8_t gray8 = SkComputeLuminance(r, g, b); |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 163 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 164 | memset(dest, gray8, width * height); |
| 165 | break; |
| 166 | } |
| 167 | case kRGB_565_GrPixelConfig: { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 168 | uint16_t rgb565 = SkPack888ToRGB16(r, g, b); |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 169 | |
| 170 | sk_memset16((uint16_t*) dest, rgb565, width * height); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 171 | break; |
| 172 | } |
| 173 | case kRGBA_4444_GrPixelConfig: { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 174 | uint8_t r4 = (r >> 4) & 0xF; |
| 175 | uint8_t g4 = (g >> 4) & 0xF; |
| 176 | uint8_t b4 = (b >> 4) & 0xF; |
| 177 | uint8_t a4 = (a >> 4) & 0xF; |
| 178 | |
| 179 | uint16_t rgba4444 = r4 << SK_R4444_SHIFT | g4 << SK_G4444_SHIFT | |
| 180 | b4 << SK_B4444_SHIFT | a4 << SK_A4444_SHIFT; |
| 181 | |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 182 | sk_memset16((uint16_t*) dest, rgba4444, width * height); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 183 | break; |
| 184 | } |
| 185 | case kRGBA_8888_GrPixelConfig: { |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 186 | sk_memset32((uint32_t *) dest, color, width * height); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 187 | break; |
| 188 | } |
| 189 | case kRGB_888_GrPixelConfig: { |
| 190 | uint8_t* dest8 = (uint8_t*) dest; |
| 191 | for (int i = 0; i < width * height; ++i, dest8 += 3) { |
| 192 | dest8[0] = r; |
| 193 | dest8[1] = g; |
| 194 | dest8[2] = b; |
| 195 | } |
| 196 | break; |
| 197 | } |
| 198 | case kRGB_888X_GrPixelConfig: { |
| 199 | GrColor opaque = GrColorPackRGBA(r, g, b, 0xFF); |
| 200 | |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 201 | sk_memset32((uint32_t *) dest, opaque, width * height); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | case kRG_88_GrPixelConfig: { |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 205 | uint16_t rg88 = (r << 8) | g; |
| 206 | |
| 207 | sk_memset16((uint16_t*) dest, rg88, width * height); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 208 | break; |
| 209 | } |
| 210 | case kBGRA_8888_GrPixelConfig: { |
| 211 | GrColor swizzled = GrColorPackRGBA(b, g, r, a); |
| 212 | |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 213 | sk_memset32((uint32_t *) dest, swizzled, width * height); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | case kSRGBA_8888_GrPixelConfig: { |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 217 | sk_memset32((uint32_t *) dest, color, width * height); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 218 | break; |
| 219 | } |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 220 | case kRGBA_1010102_GrPixelConfig: { |
| 221 | uint32_t r10 = SkScalarRoundToInt(colorf.fR * 1023.0f); |
| 222 | uint32_t g10 = SkScalarRoundToInt(colorf.fG * 1023.0f); |
| 223 | uint32_t b10 = SkScalarRoundToInt(colorf.fB * 1023.0f); |
| 224 | uint8_t a2 = SkScalarRoundToInt(colorf.fA * 3.0f); |
| 225 | |
| 226 | uint32_t rgba1010102 = a2 << 30 | b10 << 20 | g10 << 10 | r10; |
| 227 | |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 228 | sk_memset32((uint32_t *) dest, rgba1010102, width * height); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | case kRGBA_float_GrPixelConfig: { |
| 232 | SkColor4f* destColor = (SkColor4f*) dest; |
| 233 | for (int i = 0; i < width * height; ++i) { |
| 234 | destColor[i] = colorf; |
| 235 | } |
| 236 | break; |
| 237 | } |
Robert Phillips | ebab03f | 2019-07-22 08:48:18 -0400 | [diff] [blame^] | 238 | case kAlpha_half_as_Lum_GrPixelConfig: // fall through |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 239 | case kAlpha_half_as_Red_GrPixelConfig: // fall through |
| 240 | case kAlpha_half_GrPixelConfig: { |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 241 | SkHalf alphaHalf = SkFloatToHalf(colorf.fA); |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 242 | |
| 243 | sk_memset16((uint16_t *) dest, alphaHalf, width * height); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 244 | break; |
| 245 | } |
| 246 | case kRGBA_half_GrPixelConfig: // fall through |
| 247 | case kRGBA_half_Clamped_GrPixelConfig: { |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 248 | uint64_t rHalf = SkFloatToHalf(colorf.fR); |
| 249 | uint64_t gHalf = SkFloatToHalf(colorf.fG); |
| 250 | uint64_t bHalf = SkFloatToHalf(colorf.fB); |
| 251 | uint64_t aHalf = SkFloatToHalf(colorf.fA); |
| 252 | |
| 253 | uint64_t rgbaHalf = (aHalf << 48) | (bHalf << 32) | (gHalf << 16) | rHalf; |
| 254 | |
| 255 | sk_memset64((uint64_t *) dest, rgbaHalf, width * height); |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 256 | break; |
| 257 | } |
Robert Phillips | fe18de5 | 2019-06-06 17:21:50 -0400 | [diff] [blame] | 258 | case kR_16_GrPixelConfig: { |
| 259 | uint16_t r16 = SkScalarRoundToInt(colorf.fR * 65535.0f); |
| 260 | sk_memset16((uint16_t*) dest, r16, width * height); |
| 261 | break; |
| 262 | } |
| 263 | case kRG_1616_GrPixelConfig: { |
| 264 | uint16_t r16 = SkScalarRoundToInt(colorf.fR * 65535.0f); |
| 265 | uint16_t g16 = SkScalarRoundToInt(colorf.fG * 65535.0f); |
| 266 | |
| 267 | uint32_t rg1616 = r16 << 16 | g16; |
| 268 | |
| 269 | sk_memset32((uint32_t*) dest, rg1616, width * height); |
| 270 | break; |
| 271 | } |
Robert Phillips | 66a4603 | 2019-06-18 08:00:42 -0400 | [diff] [blame] | 272 | // Experimental (for Y416 and mutant P016/P010) |
| 273 | case kRGBA_16161616_GrPixelConfig: { |
| 274 | uint64_t r16 = SkScalarRoundToInt(colorf.fR * 65535.0f); |
| 275 | uint64_t g16 = SkScalarRoundToInt(colorf.fG * 65535.0f); |
| 276 | uint64_t b16 = SkScalarRoundToInt(colorf.fB * 65535.0f); |
| 277 | uint64_t a16 = SkScalarRoundToInt(colorf.fA * 65535.0f); |
| 278 | |
| 279 | uint64_t rgba16161616 = (a16 << 48) | (b16 << 32) | (g16 << 16) | r16; |
| 280 | sk_memset64((uint64_t*) dest, rgba16161616, width * height); |
| 281 | break; |
| 282 | } |
| 283 | case kRG_half_GrPixelConfig: { |
| 284 | uint32_t rHalf = SkFloatToHalf(colorf.fR); |
| 285 | uint32_t gHalf = SkFloatToHalf(colorf.fG); |
| 286 | |
| 287 | uint32_t rgHalf = (rHalf << 16) | gHalf; |
| 288 | |
| 289 | sk_memset32((uint32_t *) dest, rgHalf, width * height); |
| 290 | break; |
| 291 | } |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 292 | default: |
| 293 | return false; |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | return true; |
| 298 | } |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 299 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 300 | size_t GrComputeTightCombinedBufferSize(size_t bytesPerPixel, int baseWidth, int baseHeight, |
| 301 | SkTArray<size_t>* individualMipOffsets, int mipLevelCount) { |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 302 | SkASSERT(individualMipOffsets && !individualMipOffsets->count()); |
| 303 | SkASSERT(mipLevelCount >= 1); |
| 304 | |
| 305 | individualMipOffsets->push_back(0); |
| 306 | |
| 307 | size_t combinedBufferSize = baseWidth * bytesPerPixel * baseHeight; |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 308 | int currentWidth = baseWidth; |
| 309 | int currentHeight = baseHeight; |
| 310 | |
| 311 | // The Vulkan spec for copying a buffer to an image requires that the alignment must be at |
| 312 | // least 4 bytes and a multiple of the bytes per pixel of the image config. |
| 313 | SkASSERT(bytesPerPixel == 1 || bytesPerPixel == 2 || bytesPerPixel == 3 || |
| 314 | bytesPerPixel == 4 || bytesPerPixel == 8 || bytesPerPixel == 16); |
| 315 | int desiredAlignment = (bytesPerPixel == 3) ? 12 : (bytesPerPixel > 4 ? bytesPerPixel : 4); |
| 316 | |
| 317 | for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; ++currentMipLevel) { |
| 318 | currentWidth = SkTMax(1, currentWidth / 2); |
| 319 | currentHeight = SkTMax(1, currentHeight / 2); |
| 320 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 321 | size_t trimmedSize = currentWidth * bytesPerPixel * currentHeight; |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 322 | const size_t alignmentDiff = combinedBufferSize % desiredAlignment; |
| 323 | if (alignmentDiff != 0) { |
| 324 | combinedBufferSize += desiredAlignment - alignmentDiff; |
| 325 | } |
| 326 | SkASSERT((0 == combinedBufferSize % 4) && (0 == combinedBufferSize % bytesPerPixel)); |
| 327 | |
| 328 | individualMipOffsets->push_back(combinedBufferSize); |
| 329 | combinedBufferSize += trimmedSize; |
| 330 | } |
| 331 | |
| 332 | SkASSERT(individualMipOffsets->count() == mipLevelCount); |
| 333 | return combinedBufferSize; |
| 334 | } |
| 335 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 336 | void GrFillInData(GrPixelConfig config, int baseWidth, int baseHeight, |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 337 | const SkTArray<size_t>& individualMipOffsets, char* dstPixels, |
| 338 | const SkColor4f& colorf) { |
Brian Salomon | c42eb66 | 2019-06-24 17:13:00 -0400 | [diff] [blame] | 339 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 340 | SkASSERT(!GrPixelConfigIsCompressed(config)); |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 341 | int mipLevels = individualMipOffsets.count(); |
| 342 | |
| 343 | int currentWidth = baseWidth; |
| 344 | int currentHeight = baseHeight; |
| 345 | for (int currentMipLevel = 0; currentMipLevel < mipLevels; ++currentMipLevel) { |
| 346 | size_t offset = individualMipOffsets[currentMipLevel]; |
| 347 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 348 | fill_buffer_with_color(config, currentWidth, currentHeight, colorf, &(dstPixels[offset])); |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 349 | currentWidth = SkTMax(1, currentWidth / 2); |
| 350 | currentHeight = SkTMax(1, currentHeight / 2); |
| 351 | } |
| 352 | } |
| 353 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 354 | void GrFillInCompressedData(SkImage::CompressionType type, int baseWidth, int baseHeight, |
| 355 | char* dstPixels, const SkColor4f& colorf) { |
| 356 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
| 357 | int currentWidth = baseWidth; |
| 358 | int currentHeight = baseHeight; |
| 359 | if (SkImage::kETC1_CompressionType == type) { |
| 360 | fillin_ETC1_with_color(currentWidth, currentHeight, colorf, dstPixels); |
| 361 | } |
| 362 | } |
| 363 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 364 | static GrSwizzle get_load_and_get_swizzle(GrColorType ct, SkRasterPipeline::StockStage* load, |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 365 | bool* isNormalized, bool* isSRGB) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 366 | GrSwizzle swizzle("rgba"); |
| 367 | *isNormalized = true; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 368 | *isSRGB = false; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 369 | switch (ct) { |
| 370 | case GrColorType::kAlpha_8: *load = SkRasterPipeline::load_a8; break; |
| 371 | case GrColorType::kBGR_565: *load = SkRasterPipeline::load_565; break; |
| 372 | case GrColorType::kABGR_4444: *load = SkRasterPipeline::load_4444; break; |
| 373 | case GrColorType::kRGBA_8888: *load = SkRasterPipeline::load_8888; break; |
| 374 | case GrColorType::kRG_88: *load = SkRasterPipeline::load_rg88; break; |
| 375 | case GrColorType::kRGBA_1010102: *load = SkRasterPipeline::load_1010102; break; |
| 376 | case GrColorType::kAlpha_F16: *load = SkRasterPipeline::load_af16; break; |
| 377 | case GrColorType::kRGBA_F16_Clamped: *load = SkRasterPipeline::load_f16; break; |
| 378 | case GrColorType::kRG_1616: *load = SkRasterPipeline::load_rg1616; break; |
| 379 | case GrColorType::kRGBA_16161616: *load = SkRasterPipeline::load_16161616; break; |
| 380 | |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 381 | case GrColorType::kRGBA_8888_SRGB: *load = SkRasterPipeline::load_8888; |
| 382 | *isSRGB = true; |
| 383 | break; |
Brian Salomon | e14cfbe | 2019-06-24 15:00:58 -0400 | [diff] [blame] | 384 | case GrColorType::kRG_F16: *load = SkRasterPipeline::load_rgf16; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 385 | *isNormalized = false; |
| 386 | break; |
| 387 | case GrColorType::kRGBA_F16: *load = SkRasterPipeline::load_f16; |
| 388 | *isNormalized = false; |
| 389 | break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 390 | case GrColorType::kRGBA_F32: *load = SkRasterPipeline::load_f32; |
| 391 | *isNormalized = false; |
| 392 | break; |
| 393 | case GrColorType::kR_16: *load = SkRasterPipeline::load_a16; |
| 394 | swizzle = GrSwizzle("a001"); |
| 395 | break; |
| 396 | case GrColorType::kGray_8: *load = SkRasterPipeline::load_a8; |
| 397 | swizzle = GrSwizzle("aaa1"); |
| 398 | break; |
| 399 | case GrColorType::kBGRA_8888: *load = SkRasterPipeline::load_8888; |
| 400 | swizzle = GrSwizzle("bgra"); |
| 401 | break; |
| 402 | case GrColorType::kRGB_888x: *load = SkRasterPipeline::load_8888; |
| 403 | swizzle = GrSwizzle("rgb1"); |
| 404 | break; |
| 405 | |
| 406 | case GrColorType::kUnknown: |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 407 | SK_ABORT("unexpected CT"); |
| 408 | } |
| 409 | return swizzle; |
| 410 | } |
| 411 | |
| 412 | static GrSwizzle get_dst_swizzle_and_store(GrColorType ct, SkRasterPipeline::StockStage* store, |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 413 | bool* isNormalized, bool* isSRGB) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 414 | GrSwizzle swizzle("rgba"); |
| 415 | *isNormalized = true; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 416 | *isSRGB = false; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 417 | switch (ct) { |
| 418 | case GrColorType::kAlpha_8: *store = SkRasterPipeline::store_a8; break; |
| 419 | case GrColorType::kBGR_565: *store = SkRasterPipeline::store_565; break; |
| 420 | case GrColorType::kABGR_4444: *store = SkRasterPipeline::store_4444; break; |
| 421 | case GrColorType::kRGBA_8888: *store = SkRasterPipeline::store_8888; break; |
| 422 | case GrColorType::kRG_88: *store = SkRasterPipeline::store_rg88; break; |
| 423 | case GrColorType::kRGBA_1010102: *store = SkRasterPipeline::store_1010102; break; |
| 424 | case GrColorType::kRGBA_F16_Clamped: *store = SkRasterPipeline::store_f16; break; |
| 425 | case GrColorType::kRG_1616: *store = SkRasterPipeline::store_rg1616; break; |
| 426 | case GrColorType::kRGBA_16161616: *store = SkRasterPipeline::store_16161616; break; |
| 427 | |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 428 | case GrColorType::kRGBA_8888_SRGB: *store = SkRasterPipeline::store_8888; |
| 429 | *isSRGB = true; |
| 430 | break; |
Brian Salomon | e14cfbe | 2019-06-24 15:00:58 -0400 | [diff] [blame] | 431 | case GrColorType::kRG_F16: *store = SkRasterPipeline::store_rgf16; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 432 | *isNormalized = false; |
| 433 | break; |
| 434 | case GrColorType::kAlpha_F16: *store = SkRasterPipeline::store_af16; |
| 435 | *isNormalized = false; |
| 436 | break; |
| 437 | case GrColorType::kRGBA_F16: *store = SkRasterPipeline::store_f16; |
| 438 | *isNormalized = false; |
| 439 | break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 440 | case GrColorType::kRGBA_F32: *store = SkRasterPipeline::store_f32; |
| 441 | *isNormalized = false; |
| 442 | break; |
| 443 | case GrColorType::kR_16: swizzle = GrSwizzle("000r"); |
| 444 | *store = SkRasterPipeline::store_a16; |
| 445 | break; |
| 446 | case GrColorType::kBGRA_8888: swizzle = GrSwizzle("bgra"); |
| 447 | *store = SkRasterPipeline::store_8888; |
| 448 | break; |
| 449 | case GrColorType::kRGB_888x: swizzle = GrSwizzle("rgb1"); |
| 450 | *store = SkRasterPipeline::store_8888; |
| 451 | break; |
| 452 | |
| 453 | case GrColorType::kGray_8: // not currently supported as output |
| 454 | case GrColorType::kUnknown: |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 455 | SK_ABORT("unexpected CT"); |
| 456 | } |
| 457 | return swizzle; |
| 458 | } |
| 459 | |
| 460 | static inline void append_clamp_gamut(SkRasterPipeline* pipeline) { |
| 461 | // SkRasterPipeline may not know our color type and also doesn't like caller to directly |
| 462 | // append clamp_gamut. Fake it out. |
| 463 | static SkImageInfo fakeII = SkImageInfo::MakeN32Premul(1, 1); |
| 464 | pipeline->append_gamut_clamp_if_normalized(fakeII); |
| 465 | } |
| 466 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 467 | bool GrConvertPixels(const GrPixelInfo& dstInfo, void* dst, size_t dstRB, |
| 468 | const GrPixelInfo& srcInfo, const void* src, size_t srcRB, |
| 469 | bool flipY, GrSwizzle swizzle) { |
Brian Salomon | c42eb66 | 2019-06-24 17:13:00 -0400 | [diff] [blame] | 470 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 471 | if (!srcInfo.isValid() || !dstInfo.isValid()) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 472 | return false; |
| 473 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 474 | if (!src || !dst) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 475 | return false; |
| 476 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 477 | if (dstInfo.width() != srcInfo.width() || srcInfo.height() != dstInfo.height()) { |
| 478 | return false; |
| 479 | } |
| 480 | if (GrColorTypeComponentFlags(dstInfo.colorType()) & kGray_SkColorTypeComponentFlag) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 481 | // We don't currently support conversion to Gray. |
| 482 | return false; |
| 483 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 484 | if (dstRB < dstInfo.minRowBytes() || srcRB < srcInfo.minRowBytes()) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 485 | return false; |
| 486 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 487 | |
| 488 | size_t srcBpp = srcInfo.bpp(); |
| 489 | size_t dstBpp = dstInfo.bpp(); |
| 490 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 491 | // SkRasterPipeline operates on row-pixels not row-bytes. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 492 | SkASSERT(dstRB % dstBpp == 0); |
| 493 | SkASSERT(srcRB % srcBpp == 0); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 494 | |
| 495 | SkRasterPipeline::StockStage load; |
| 496 | bool srcIsNormalized; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 497 | bool srcIsSRGB; |
| 498 | auto loadSwizzle = get_load_and_get_swizzle(srcInfo.colorType(), &load, &srcIsNormalized, |
| 499 | &srcIsSRGB); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 500 | loadSwizzle = GrSwizzle::Concat(loadSwizzle, swizzle); |
| 501 | |
| 502 | SkRasterPipeline::StockStage store; |
| 503 | bool dstIsNormalized; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 504 | bool dstIsSRGB; |
| 505 | auto storeSwizzle = get_dst_swizzle_and_store(dstInfo.colorType(), &store, &dstIsNormalized, |
| 506 | &dstIsSRGB); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 507 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 508 | bool premul = srcInfo.alphaType() == kUnpremul_SkAlphaType && |
| 509 | dstInfo.alphaType() == kPremul_SkAlphaType; |
| 510 | bool unpremul = srcInfo.alphaType() == kPremul_SkAlphaType && |
| 511 | dstInfo.alphaType() == kUnpremul_SkAlphaType; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 512 | bool alphaOrCSConversion = |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 513 | premul || unpremul || !SkColorSpace::Equals(srcInfo.colorSpace(), dstInfo.colorSpace()); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 514 | |
| 515 | bool clampGamut; |
| 516 | SkTLazy<SkColorSpaceXformSteps> steps; |
| 517 | GrSwizzle loadStoreSwizzle; |
| 518 | if (alphaOrCSConversion) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 519 | steps.init(srcInfo.colorSpace(), srcInfo.alphaType(), |
| 520 | dstInfo.colorSpace(), dstInfo.alphaType()); |
| 521 | clampGamut = dstIsNormalized && dstInfo.alphaType() == kPremul_SkAlphaType; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 522 | } else { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 523 | clampGamut = |
| 524 | dstIsNormalized && !srcIsNormalized && dstInfo.alphaType() == kPremul_SkAlphaType; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 525 | if (!clampGamut) { |
| 526 | loadStoreSwizzle = GrSwizzle::Concat(loadSwizzle, storeSwizzle); |
| 527 | } |
| 528 | } |
| 529 | int cnt = 1; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 530 | int height = srcInfo.height(); |
| 531 | SkRasterPipeline_MemoryCtx srcCtx{const_cast<void*>(src), SkToInt(srcRB / srcBpp)}, |
| 532 | dstCtx{ dst , SkToInt(dstRB / dstBpp)}; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 533 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 534 | if (flipY) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 535 | // It *almost* works to point the src at the last row and negate the stride and run the |
| 536 | // whole rectangle. However, SkRasterPipeline::run()'s control loop uses size_t loop |
| 537 | // variables so it winds up relying on unsigned overflow math. It works out in practice |
| 538 | // but UBSAN says "no!" as it's technically undefined and in theory a compiler could emit |
| 539 | // 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] | 540 | srcCtx.pixels = static_cast<char*>(srcCtx.pixels) + srcRB * (height - 1); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 541 | std::swap(cnt, height); |
| 542 | } |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 543 | |
| 544 | bool hasConversion = alphaOrCSConversion || clampGamut; |
| 545 | |
| 546 | if (srcIsSRGB && dstIsSRGB && !hasConversion) { |
| 547 | // No need to convert from srgb if we are just going to immediately convert it back. |
| 548 | srcIsSRGB = dstIsSRGB = false; |
| 549 | } |
| 550 | |
| 551 | hasConversion = hasConversion || srcIsSRGB || dstIsSRGB; |
| 552 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 553 | for (int i = 0; i < cnt; ++i) { |
| 554 | SkRasterPipeline_<256> pipeline; |
| 555 | pipeline.append(load, &srcCtx); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 556 | if (hasConversion) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 557 | loadSwizzle.apply(&pipeline); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 558 | if (srcIsSRGB) { |
| 559 | pipeline.append(SkRasterPipeline::from_srgb); |
| 560 | } |
| 561 | if (alphaOrCSConversion) { |
| 562 | steps->apply(&pipeline, srcIsNormalized); |
| 563 | } |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 564 | if (clampGamut) { |
| 565 | append_clamp_gamut(&pipeline); |
| 566 | } |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 567 | // If we add support for storing to Gray we would add a luminance to alpha conversion |
| 568 | // here. We also wouldn't then need a to_srgb stage after since it would have not effect |
| 569 | // on the alpha channel. It would also mean we have an SRGB Gray color type which |
| 570 | // doesn't exist currently. |
| 571 | if (dstIsSRGB) { |
| 572 | pipeline.append(SkRasterPipeline::to_srgb); |
| 573 | } |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 574 | storeSwizzle.apply(&pipeline); |
| 575 | } else { |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 576 | loadStoreSwizzle.apply(&pipeline); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 577 | } |
| 578 | pipeline.append(store, &dstCtx); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 579 | pipeline.run(0, 0, srcInfo.width(), height); |
| 580 | srcCtx.pixels = static_cast<char*>(srcCtx.pixels) - srcRB; |
| 581 | dstCtx.pixels = static_cast<char*>(dstCtx.pixels) + dstRB; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 582 | } |
| 583 | return true; |
| 584 | } |