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 | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 8 | #include "src/core/SkColorSpaceXformSteps.h" |
| 9 | #include "src/core/SkTLazy.h" |
Brian Salomon | c42eb66 | 2019-06-24 17:13:00 -0400 | [diff] [blame] | 10 | #include "src/core/SkTraceEvent.h" |
Robert Phillips | b5e331a | 2019-05-28 10:58:09 -0400 | [diff] [blame] | 11 | #include "src/core/SkUtils.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrColor.h" |
Mike Klein | 52337de | 2019-07-25 09:00:52 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrDataUtils.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; |
Brian Salomon | 1cec69a | 2019-07-31 10:24:26 -0400 | [diff] [blame] | 393 | case GrColorType::kAlpha_8xxx: *load = SkRasterPipeline::load_8888; |
| 394 | swizzle = GrSwizzle("000r"); |
| 395 | break; |
| 396 | case GrColorType::kAlpha_F32xxx: *load = SkRasterPipeline::load_f32; |
| 397 | swizzle = GrSwizzle("000r"); |
| 398 | break; |
| 399 | case GrColorType::kGray_8xxx: *load = SkRasterPipeline::load_8888; |
| 400 | swizzle = GrSwizzle("rrr1"); |
| 401 | break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 402 | case GrColorType::kR_16: *load = SkRasterPipeline::load_a16; |
| 403 | swizzle = GrSwizzle("a001"); |
| 404 | break; |
| 405 | case GrColorType::kGray_8: *load = SkRasterPipeline::load_a8; |
| 406 | swizzle = GrSwizzle("aaa1"); |
| 407 | break; |
| 408 | case GrColorType::kBGRA_8888: *load = SkRasterPipeline::load_8888; |
| 409 | swizzle = GrSwizzle("bgra"); |
| 410 | break; |
| 411 | case GrColorType::kRGB_888x: *load = SkRasterPipeline::load_8888; |
| 412 | swizzle = GrSwizzle("rgb1"); |
| 413 | break; |
| 414 | |
| 415 | case GrColorType::kUnknown: |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 416 | SK_ABORT("unexpected CT"); |
| 417 | } |
| 418 | return swizzle; |
| 419 | } |
| 420 | |
| 421 | static GrSwizzle get_dst_swizzle_and_store(GrColorType ct, SkRasterPipeline::StockStage* store, |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 422 | bool* isNormalized, bool* isSRGB) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 423 | GrSwizzle swizzle("rgba"); |
| 424 | *isNormalized = true; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 425 | *isSRGB = false; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 426 | switch (ct) { |
| 427 | case GrColorType::kAlpha_8: *store = SkRasterPipeline::store_a8; break; |
| 428 | case GrColorType::kBGR_565: *store = SkRasterPipeline::store_565; break; |
| 429 | case GrColorType::kABGR_4444: *store = SkRasterPipeline::store_4444; break; |
| 430 | case GrColorType::kRGBA_8888: *store = SkRasterPipeline::store_8888; break; |
| 431 | case GrColorType::kRG_88: *store = SkRasterPipeline::store_rg88; break; |
| 432 | case GrColorType::kRGBA_1010102: *store = SkRasterPipeline::store_1010102; break; |
| 433 | case GrColorType::kRGBA_F16_Clamped: *store = SkRasterPipeline::store_f16; break; |
| 434 | case GrColorType::kRG_1616: *store = SkRasterPipeline::store_rg1616; break; |
| 435 | case GrColorType::kRGBA_16161616: *store = SkRasterPipeline::store_16161616; break; |
| 436 | |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 437 | case GrColorType::kRGBA_8888_SRGB: *store = SkRasterPipeline::store_8888; |
| 438 | *isSRGB = true; |
| 439 | break; |
Brian Salomon | e14cfbe | 2019-06-24 15:00:58 -0400 | [diff] [blame] | 440 | case GrColorType::kRG_F16: *store = SkRasterPipeline::store_rgf16; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 441 | *isNormalized = false; |
| 442 | break; |
| 443 | case GrColorType::kAlpha_F16: *store = SkRasterPipeline::store_af16; |
| 444 | *isNormalized = false; |
| 445 | break; |
| 446 | case GrColorType::kRGBA_F16: *store = SkRasterPipeline::store_f16; |
| 447 | *isNormalized = false; |
| 448 | break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 449 | case GrColorType::kRGBA_F32: *store = SkRasterPipeline::store_f32; |
| 450 | *isNormalized = false; |
| 451 | break; |
Brian Salomon | 1cec69a | 2019-07-31 10:24:26 -0400 | [diff] [blame] | 452 | case GrColorType::kAlpha_8xxx: *store = SkRasterPipeline::store_8888; |
| 453 | swizzle = GrSwizzle("a000"); |
| 454 | break; |
| 455 | case GrColorType::kAlpha_F32xxx: *store = SkRasterPipeline::store_f32; |
| 456 | swizzle = GrSwizzle("a000"); |
| 457 | break; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 458 | case GrColorType::kR_16: swizzle = GrSwizzle("000r"); |
| 459 | *store = SkRasterPipeline::store_a16; |
| 460 | break; |
| 461 | case GrColorType::kBGRA_8888: swizzle = GrSwizzle("bgra"); |
| 462 | *store = SkRasterPipeline::store_8888; |
| 463 | break; |
| 464 | case GrColorType::kRGB_888x: swizzle = GrSwizzle("rgb1"); |
| 465 | *store = SkRasterPipeline::store_8888; |
| 466 | break; |
| 467 | |
| 468 | case GrColorType::kGray_8: // not currently supported as output |
Brian Salomon | 1cec69a | 2019-07-31 10:24:26 -0400 | [diff] [blame] | 469 | case GrColorType::kGray_8xxx: // not currently supported as output |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 470 | case GrColorType::kUnknown: |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 471 | SK_ABORT("unexpected CT"); |
| 472 | } |
| 473 | return swizzle; |
| 474 | } |
| 475 | |
| 476 | static inline void append_clamp_gamut(SkRasterPipeline* pipeline) { |
| 477 | // SkRasterPipeline may not know our color type and also doesn't like caller to directly |
| 478 | // append clamp_gamut. Fake it out. |
| 479 | static SkImageInfo fakeII = SkImageInfo::MakeN32Premul(1, 1); |
| 480 | pipeline->append_gamut_clamp_if_normalized(fakeII); |
| 481 | } |
| 482 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 483 | bool GrConvertPixels(const GrPixelInfo& dstInfo, void* dst, size_t dstRB, |
| 484 | const GrPixelInfo& srcInfo, const void* src, size_t srcRB, |
Brian Salomon | 1cec69a | 2019-07-31 10:24:26 -0400 | [diff] [blame] | 485 | bool flipY) { |
Brian Salomon | c42eb66 | 2019-06-24 17:13:00 -0400 | [diff] [blame] | 486 | TRACE_EVENT0("skia.gpu", TRACE_FUNC); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 487 | if (!srcInfo.isValid() || !dstInfo.isValid()) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 488 | return false; |
| 489 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 490 | if (!src || !dst) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 491 | return false; |
| 492 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 493 | if (dstInfo.width() != srcInfo.width() || srcInfo.height() != dstInfo.height()) { |
| 494 | return false; |
| 495 | } |
| 496 | if (GrColorTypeComponentFlags(dstInfo.colorType()) & kGray_SkColorTypeComponentFlag) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 497 | // We don't currently support conversion to Gray. |
| 498 | return false; |
| 499 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 500 | if (dstRB < dstInfo.minRowBytes() || srcRB < srcInfo.minRowBytes()) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 501 | return false; |
| 502 | } |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 503 | |
| 504 | size_t srcBpp = srcInfo.bpp(); |
| 505 | size_t dstBpp = dstInfo.bpp(); |
| 506 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 507 | // SkRasterPipeline operates on row-pixels not row-bytes. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 508 | SkASSERT(dstRB % dstBpp == 0); |
| 509 | SkASSERT(srcRB % srcBpp == 0); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 510 | |
| 511 | SkRasterPipeline::StockStage load; |
| 512 | bool srcIsNormalized; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 513 | bool srcIsSRGB; |
| 514 | auto loadSwizzle = get_load_and_get_swizzle(srcInfo.colorType(), &load, &srcIsNormalized, |
| 515 | &srcIsSRGB); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 516 | |
| 517 | SkRasterPipeline::StockStage store; |
| 518 | bool dstIsNormalized; |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 519 | bool dstIsSRGB; |
| 520 | auto storeSwizzle = get_dst_swizzle_and_store(dstInfo.colorType(), &store, &dstIsNormalized, |
| 521 | &dstIsSRGB); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 522 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 523 | bool premul = srcInfo.alphaType() == kUnpremul_SkAlphaType && |
| 524 | dstInfo.alphaType() == kPremul_SkAlphaType; |
| 525 | bool unpremul = srcInfo.alphaType() == kPremul_SkAlphaType && |
| 526 | dstInfo.alphaType() == kUnpremul_SkAlphaType; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 527 | bool alphaOrCSConversion = |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 528 | premul || unpremul || !SkColorSpace::Equals(srcInfo.colorSpace(), dstInfo.colorSpace()); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 529 | |
| 530 | bool clampGamut; |
| 531 | SkTLazy<SkColorSpaceXformSteps> steps; |
| 532 | GrSwizzle loadStoreSwizzle; |
| 533 | if (alphaOrCSConversion) { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 534 | steps.init(srcInfo.colorSpace(), srcInfo.alphaType(), |
| 535 | dstInfo.colorSpace(), dstInfo.alphaType()); |
| 536 | clampGamut = dstIsNormalized && dstInfo.alphaType() == kPremul_SkAlphaType; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 537 | } else { |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 538 | clampGamut = |
| 539 | dstIsNormalized && !srcIsNormalized && dstInfo.alphaType() == kPremul_SkAlphaType; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 540 | if (!clampGamut) { |
| 541 | loadStoreSwizzle = GrSwizzle::Concat(loadSwizzle, storeSwizzle); |
| 542 | } |
| 543 | } |
| 544 | int cnt = 1; |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 545 | int height = srcInfo.height(); |
| 546 | SkRasterPipeline_MemoryCtx srcCtx{const_cast<void*>(src), SkToInt(srcRB / srcBpp)}, |
| 547 | dstCtx{ dst , SkToInt(dstRB / dstBpp)}; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 548 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 549 | if (flipY) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 550 | // It *almost* works to point the src at the last row and negate the stride and run the |
| 551 | // whole rectangle. However, SkRasterPipeline::run()'s control loop uses size_t loop |
| 552 | // variables so it winds up relying on unsigned overflow math. It works out in practice |
| 553 | // but UBSAN says "no!" as it's technically undefined and in theory a compiler could emit |
| 554 | // 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] | 555 | srcCtx.pixels = static_cast<char*>(srcCtx.pixels) + srcRB * (height - 1); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 556 | std::swap(cnt, height); |
| 557 | } |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 558 | |
| 559 | bool hasConversion = alphaOrCSConversion || clampGamut; |
| 560 | |
| 561 | if (srcIsSRGB && dstIsSRGB && !hasConversion) { |
| 562 | // No need to convert from srgb if we are just going to immediately convert it back. |
| 563 | srcIsSRGB = dstIsSRGB = false; |
| 564 | } |
| 565 | |
| 566 | hasConversion = hasConversion || srcIsSRGB || dstIsSRGB; |
| 567 | |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 568 | for (int i = 0; i < cnt; ++i) { |
| 569 | SkRasterPipeline_<256> pipeline; |
| 570 | pipeline.append(load, &srcCtx); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 571 | if (hasConversion) { |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 572 | loadSwizzle.apply(&pipeline); |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 573 | if (srcIsSRGB) { |
| 574 | pipeline.append(SkRasterPipeline::from_srgb); |
| 575 | } |
| 576 | if (alphaOrCSConversion) { |
| 577 | steps->apply(&pipeline, srcIsNormalized); |
| 578 | } |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 579 | if (clampGamut) { |
| 580 | append_clamp_gamut(&pipeline); |
| 581 | } |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 582 | // If we add support for storing to Gray we would add a luminance to alpha conversion |
| 583 | // here. We also wouldn't then need a to_srgb stage after since it would have not effect |
| 584 | // on the alpha channel. It would also mean we have an SRGB Gray color type which |
| 585 | // doesn't exist currently. |
| 586 | if (dstIsSRGB) { |
| 587 | pipeline.append(SkRasterPipeline::to_srgb); |
| 588 | } |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 589 | storeSwizzle.apply(&pipeline); |
| 590 | } else { |
Greg Daniel | e877dce | 2019-07-11 10:52:43 -0400 | [diff] [blame] | 591 | loadStoreSwizzle.apply(&pipeline); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 592 | } |
| 593 | pipeline.append(store, &dstCtx); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 594 | pipeline.run(0, 0, srcInfo.width(), height); |
| 595 | srcCtx.pixels = static_cast<char*>(srcCtx.pixels) - srcRB; |
| 596 | dstCtx.pixels = static_cast<char*>(dstCtx.pixels) + dstRB; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 597 | } |
| 598 | return true; |
| 599 | } |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 600 | |
Robert Phillips | c80b0e9 | 2019-07-23 10:27:09 -0400 | [diff] [blame] | 601 | GrColorType SkColorTypeAndFormatToGrColorType(const GrCaps* caps, |
| 602 | SkColorType skCT, |
| 603 | const GrBackendFormat& format) { |
| 604 | GrColorType grCT = SkColorTypeToGrColorType(skCT); |
| 605 | // Until we support SRGB in the SkColorType we have to do this manual check here to make sure |
| 606 | // we use the correct GrColorType. |
| 607 | if (caps->isFormatSRGB(format)) { |
| 608 | if (grCT != GrColorType::kRGBA_8888) { |
| 609 | return GrColorType::kUnknown; |
| 610 | } |
| 611 | grCT = GrColorType::kRGBA_8888_SRGB; |
| 612 | } |
| 613 | return grCT; |
| 614 | } |