blob: ae9c7d1440929514cc5d645d03f8f1f83225d255 [file] [log] [blame]
Robert Phillips459b2952019-05-23 09:38:27 -04001/*
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 Klein4b432fa2019-06-06 11:44:05 -05008#include "src/gpu/GrDataUtils.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -04009#include "src/core/SkColorSpaceXformSteps.h"
10#include "src/core/SkTLazy.h"
Brian Salomonc42eb662019-06-24 17:13:00 -040011#include "src/core/SkTraceEvent.h"
Robert Phillipsb5e331a2019-05-28 10:58:09 -040012#include "src/core/SkUtils.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrColor.h"
Robert Phillips459b2952019-05-23 09:38:27 -040014
Robert Phillips8043f322019-05-31 08:11:36 -040015struct ETC1Block {
16 uint32_t fHigh;
17 uint32_t fLow;
18};
19
Robert Phillips459b2952019-05-23 09:38:27 -040020static const int kNumModifierTables = 8;
21static 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
25static 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
36static 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).
43static 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'
57static 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 Phillips8043f322019-05-31 08:11:36 -040098static int num_ETC1_blocks(int w, int h) {
Robert Phillips459b2952019-05-23 09:38:27 -040099 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
Robert Phillips8043f322019-05-31 08:11:36 -0400116size_t GrETC1CompressedDataSize(int width, int height) {
117 int numBlocks = num_ETC1_blocks(width, height);
118
119 return numBlocks * sizeof(ETC1Block);
120}
121
Robert Phillips28a5a432019-06-07 12:46:21 -0400122// Fill in 'dest' with ETC1 blocks derived from 'colorf'
123static void fillin_ETC1_with_color(int width, int height, const SkColor4f& colorf, void* dest) {
Robert Phillips459b2952019-05-23 09:38:27 -0400124 SkColor color = colorf.toSkColor();
125
126 ETC1Block block;
127 create_etc1_block(color, &block);
128
Robert Phillips8043f322019-05-31 08:11:36 -0400129 int numBlocks = num_ETC1_blocks(width, height);
130
Robert Phillips459b2952019-05-23 09:38:27 -0400131 for (int i = 0; i < numBlocks; ++i) {
Robert Phillipse3bd6732019-05-29 14:20:35 -0400132 ((ETC1Block*)dest)[i] = block;
Robert Phillips459b2952019-05-23 09:38:27 -0400133 }
134}
135
Robert Phillips28a5a432019-06-07 12:46:21 -0400136// Fill in the width x height 'dest' with the munged version of 'colorf' that matches 'config'
137static bool fill_buffer_with_color(GrPixelConfig config, int width, int height,
138 const SkColor4f& colorf, void* dest) {
Robert Phillips459b2952019-05-23 09:38:27 -0400139 SkASSERT(kRGB_ETC1_GrPixelConfig != config);
140
141 GrColor color = colorf.toBytes_RGBA();
142
143 uint8_t r = GrColorUnpackR(color);
144 uint8_t g = GrColorUnpackG(color);
145 uint8_t b = GrColorUnpackB(color);
146 uint8_t a = GrColorUnpackA(color);
147
Robert Phillips459b2952019-05-23 09:38:27 -0400148 switch (config) {
149 case kAlpha_8_GrPixelConfig: // fall through
150 case kAlpha_8_as_Alpha_GrPixelConfig: // fall through
151 case kAlpha_8_as_Red_GrPixelConfig: {
152 memset(dest, a, width * height);
153 break;
154 }
155 case kGray_8_GrPixelConfig: // fall through
156 case kGray_8_as_Lum_GrPixelConfig: // fall through
157 case kGray_8_as_Red_GrPixelConfig: {
158 uint8_t gray8 = SkComputeLuminance(r, g, b);
Robert Phillipsb5e331a2019-05-28 10:58:09 -0400159
Robert Phillips459b2952019-05-23 09:38:27 -0400160 memset(dest, gray8, width * height);
161 break;
162 }
163 case kRGB_565_GrPixelConfig: {
Robert Phillips459b2952019-05-23 09:38:27 -0400164 uint16_t rgb565 = SkPack888ToRGB16(r, g, b);
Robert Phillipsb5e331a2019-05-28 10:58:09 -0400165
166 sk_memset16((uint16_t*) dest, rgb565, width * height);
Robert Phillips459b2952019-05-23 09:38:27 -0400167 break;
168 }
169 case kRGBA_4444_GrPixelConfig: {
Robert Phillips459b2952019-05-23 09:38:27 -0400170 uint8_t r4 = (r >> 4) & 0xF;
171 uint8_t g4 = (g >> 4) & 0xF;
172 uint8_t b4 = (b >> 4) & 0xF;
173 uint8_t a4 = (a >> 4) & 0xF;
174
175 uint16_t rgba4444 = r4 << SK_R4444_SHIFT | g4 << SK_G4444_SHIFT |
176 b4 << SK_B4444_SHIFT | a4 << SK_A4444_SHIFT;
177
Robert Phillipsb5e331a2019-05-28 10:58:09 -0400178 sk_memset16((uint16_t*) dest, rgba4444, width * height);
Robert Phillips459b2952019-05-23 09:38:27 -0400179 break;
180 }
181 case kRGBA_8888_GrPixelConfig: {
Robert Phillipsb5e331a2019-05-28 10:58:09 -0400182 sk_memset32((uint32_t *) dest, color, width * height);
Robert Phillips459b2952019-05-23 09:38:27 -0400183 break;
184 }
185 case kRGB_888_GrPixelConfig: {
186 uint8_t* dest8 = (uint8_t*) dest;
187 for (int i = 0; i < width * height; ++i, dest8 += 3) {
188 dest8[0] = r;
189 dest8[1] = g;
190 dest8[2] = b;
191 }
192 break;
193 }
194 case kRGB_888X_GrPixelConfig: {
195 GrColor opaque = GrColorPackRGBA(r, g, b, 0xFF);
196
Robert Phillipsb5e331a2019-05-28 10:58:09 -0400197 sk_memset32((uint32_t *) dest, opaque, width * height);
Robert Phillips459b2952019-05-23 09:38:27 -0400198 break;
199 }
200 case kRG_88_GrPixelConfig: {
Robert Phillipsb5e331a2019-05-28 10:58:09 -0400201 uint16_t rg88 = (r << 8) | g;
202
203 sk_memset16((uint16_t*) dest, rg88, width * height);
Robert Phillips459b2952019-05-23 09:38:27 -0400204 break;
205 }
206 case kBGRA_8888_GrPixelConfig: {
207 GrColor swizzled = GrColorPackRGBA(b, g, r, a);
208
Robert Phillipsb5e331a2019-05-28 10:58:09 -0400209 sk_memset32((uint32_t *) dest, swizzled, width * height);
Robert Phillips459b2952019-05-23 09:38:27 -0400210 break;
211 }
212 case kSRGBA_8888_GrPixelConfig: {
Robert Phillipsb5e331a2019-05-28 10:58:09 -0400213 sk_memset32((uint32_t *) dest, color, width * height);
Robert Phillips459b2952019-05-23 09:38:27 -0400214 break;
215 }
216 case kSBGRA_8888_GrPixelConfig: {
Robert Phillips459b2952019-05-23 09:38:27 -0400217 GrColor swizzled = GrColorPackRGBA(b, g, r, a);
218
Robert Phillipsb5e331a2019-05-28 10:58:09 -0400219 sk_memset32((uint32_t *) dest, swizzled, width * height);
Robert Phillips459b2952019-05-23 09:38:27 -0400220 break;
221 }
222 case kRGBA_1010102_GrPixelConfig: {
223 uint32_t r10 = SkScalarRoundToInt(colorf.fR * 1023.0f);
224 uint32_t g10 = SkScalarRoundToInt(colorf.fG * 1023.0f);
225 uint32_t b10 = SkScalarRoundToInt(colorf.fB * 1023.0f);
226 uint8_t a2 = SkScalarRoundToInt(colorf.fA * 3.0f);
227
228 uint32_t rgba1010102 = a2 << 30 | b10 << 20 | g10 << 10 | r10;
229
Robert Phillipsb5e331a2019-05-28 10:58:09 -0400230 sk_memset32((uint32_t *) dest, rgba1010102, width * height);
Robert Phillips459b2952019-05-23 09:38:27 -0400231 break;
232 }
233 case kRGBA_float_GrPixelConfig: {
234 SkColor4f* destColor = (SkColor4f*) dest;
235 for (int i = 0; i < width * height; ++i) {
236 destColor[i] = colorf;
237 }
238 break;
239 }
240 case kRG_float_GrPixelConfig: {
241 float* destFloat = (float*) dest;
242 for (int i = 0; i < width * height; ++i, destFloat += 2) {
243 destFloat[0] = colorf.fR;
244 destFloat[1] = colorf.fG;
245 }
246 break;
247 }
248 case kAlpha_half_as_Red_GrPixelConfig: // fall through
249 case kAlpha_half_GrPixelConfig: {
Robert Phillips459b2952019-05-23 09:38:27 -0400250 SkHalf alphaHalf = SkFloatToHalf(colorf.fA);
Robert Phillipsb5e331a2019-05-28 10:58:09 -0400251
252 sk_memset16((uint16_t *) dest, alphaHalf, width * height);
Robert Phillips459b2952019-05-23 09:38:27 -0400253 break;
254 }
255 case kRGBA_half_GrPixelConfig: // fall through
256 case kRGBA_half_Clamped_GrPixelConfig: {
Robert Phillipsb5e331a2019-05-28 10:58:09 -0400257 uint64_t rHalf = SkFloatToHalf(colorf.fR);
258 uint64_t gHalf = SkFloatToHalf(colorf.fG);
259 uint64_t bHalf = SkFloatToHalf(colorf.fB);
260 uint64_t aHalf = SkFloatToHalf(colorf.fA);
261
262 uint64_t rgbaHalf = (aHalf << 48) | (bHalf << 32) | (gHalf << 16) | rHalf;
263
264 sk_memset64((uint64_t *) dest, rgbaHalf, width * height);
Robert Phillips459b2952019-05-23 09:38:27 -0400265 break;
266 }
Robert Phillipsfe18de52019-06-06 17:21:50 -0400267 case kR_16_GrPixelConfig: {
268 uint16_t r16 = SkScalarRoundToInt(colorf.fR * 65535.0f);
269 sk_memset16((uint16_t*) dest, r16, width * height);
270 break;
271 }
272 case kRG_1616_GrPixelConfig: {
273 uint16_t r16 = SkScalarRoundToInt(colorf.fR * 65535.0f);
274 uint16_t g16 = SkScalarRoundToInt(colorf.fG * 65535.0f);
275
276 uint32_t rg1616 = r16 << 16 | g16;
277
278 sk_memset32((uint32_t*) dest, rg1616, width * height);
279 break;
280 }
Robert Phillips66a46032019-06-18 08:00:42 -0400281 // Experimental (for Y416 and mutant P016/P010)
282 case kRGBA_16161616_GrPixelConfig: {
283 uint64_t r16 = SkScalarRoundToInt(colorf.fR * 65535.0f);
284 uint64_t g16 = SkScalarRoundToInt(colorf.fG * 65535.0f);
285 uint64_t b16 = SkScalarRoundToInt(colorf.fB * 65535.0f);
286 uint64_t a16 = SkScalarRoundToInt(colorf.fA * 65535.0f);
287
288 uint64_t rgba16161616 = (a16 << 48) | (b16 << 32) | (g16 << 16) | r16;
289 sk_memset64((uint64_t*) dest, rgba16161616, width * height);
290 break;
291 }
292 case kRG_half_GrPixelConfig: {
293 uint32_t rHalf = SkFloatToHalf(colorf.fR);
294 uint32_t gHalf = SkFloatToHalf(colorf.fG);
295
296 uint32_t rgHalf = (rHalf << 16) | gHalf;
297
298 sk_memset32((uint32_t *) dest, rgHalf, width * height);
299 break;
300 }
Robert Phillips459b2952019-05-23 09:38:27 -0400301 default:
302 return false;
303 break;
304 }
305
306 return true;
307}
Robert Phillips28a5a432019-06-07 12:46:21 -0400308
309size_t GrComputeTightCombinedBufferSize(GrCompression compression, size_t bytesPerPixel,
310 int baseWidth, int baseHeight,
311 SkTArray<size_t>* individualMipOffsets,
312 int mipLevelCount) {
313 SkASSERT(individualMipOffsets && !individualMipOffsets->count());
314 SkASSERT(mipLevelCount >= 1);
315
316 individualMipOffsets->push_back(0);
317
318 size_t combinedBufferSize = baseWidth * bytesPerPixel * baseHeight;
319 if (GrCompression::kETC1 == compression) {
320 SkASSERT(0 == bytesPerPixel);
321 bytesPerPixel = 4; // munge Bpp to make the following code work (and not assert)
322 combinedBufferSize = GrETC1CompressedDataSize(baseWidth, baseHeight);
323 }
324
325 int currentWidth = baseWidth;
326 int currentHeight = baseHeight;
327
328 // The Vulkan spec for copying a buffer to an image requires that the alignment must be at
329 // least 4 bytes and a multiple of the bytes per pixel of the image config.
330 SkASSERT(bytesPerPixel == 1 || bytesPerPixel == 2 || bytesPerPixel == 3 ||
331 bytesPerPixel == 4 || bytesPerPixel == 8 || bytesPerPixel == 16);
332 int desiredAlignment = (bytesPerPixel == 3) ? 12 : (bytesPerPixel > 4 ? bytesPerPixel : 4);
333
334 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; ++currentMipLevel) {
335 currentWidth = SkTMax(1, currentWidth / 2);
336 currentHeight = SkTMax(1, currentHeight / 2);
337
338 size_t trimmedSize;
339 if (GrCompression::kETC1 == compression) {
340 trimmedSize = GrETC1CompressedDataSize(currentWidth, currentHeight);
341 } else {
342 trimmedSize = currentWidth * bytesPerPixel * currentHeight;
343 }
344 const size_t alignmentDiff = combinedBufferSize % desiredAlignment;
345 if (alignmentDiff != 0) {
346 combinedBufferSize += desiredAlignment - alignmentDiff;
347 }
348 SkASSERT((0 == combinedBufferSize % 4) && (0 == combinedBufferSize % bytesPerPixel));
349
350 individualMipOffsets->push_back(combinedBufferSize);
351 combinedBufferSize += trimmedSize;
352 }
353
354 SkASSERT(individualMipOffsets->count() == mipLevelCount);
355 return combinedBufferSize;
356}
357
358void GrFillInData(GrCompression compression, GrPixelConfig config,
359 int baseWidth, int baseHeight,
360 const SkTArray<size_t>& individualMipOffsets, char* dstPixels,
361 const SkColor4f& colorf) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400362 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Robert Phillips28a5a432019-06-07 12:46:21 -0400363
364 int mipLevels = individualMipOffsets.count();
365
366 int currentWidth = baseWidth;
367 int currentHeight = baseHeight;
368 for (int currentMipLevel = 0; currentMipLevel < mipLevels; ++currentMipLevel) {
369 size_t offset = individualMipOffsets[currentMipLevel];
370
371 if (GrCompression::kETC1 == compression) {
372 // TODO: compute the ETC1 block for 'colorf' just once
373 fillin_ETC1_with_color(currentWidth, currentHeight, colorf, &(dstPixels[offset]));
374 } else {
375 fill_buffer_with_color(config, currentWidth, currentHeight, colorf,
376 &(dstPixels[offset]));
377 }
378
379 currentWidth = SkTMax(1, currentWidth / 2);
380 currentHeight = SkTMax(1, currentHeight / 2);
381 }
382}
383
Brian Salomonf30b1c12019-06-20 12:25:02 -0400384static GrSwizzle get_load_and_get_swizzle(GrColorType ct, SkRasterPipeline::StockStage* load,
385 bool* isNormalized) {
386 GrSwizzle swizzle("rgba");
387 *isNormalized = true;
388 switch (ct) {
389 case GrColorType::kAlpha_8: *load = SkRasterPipeline::load_a8; break;
390 case GrColorType::kBGR_565: *load = SkRasterPipeline::load_565; break;
391 case GrColorType::kABGR_4444: *load = SkRasterPipeline::load_4444; break;
392 case GrColorType::kRGBA_8888: *load = SkRasterPipeline::load_8888; break;
393 case GrColorType::kRG_88: *load = SkRasterPipeline::load_rg88; break;
394 case GrColorType::kRGBA_1010102: *load = SkRasterPipeline::load_1010102; break;
395 case GrColorType::kAlpha_F16: *load = SkRasterPipeline::load_af16; break;
396 case GrColorType::kRGBA_F16_Clamped: *load = SkRasterPipeline::load_f16; break;
397 case GrColorType::kRG_1616: *load = SkRasterPipeline::load_rg1616; break;
398 case GrColorType::kRGBA_16161616: *load = SkRasterPipeline::load_16161616; break;
399
Brian Salomone14cfbe2019-06-24 15:00:58 -0400400 case GrColorType::kRG_F16: *load = SkRasterPipeline::load_rgf16;
Brian Salomonf30b1c12019-06-20 12:25:02 -0400401 *isNormalized = false;
402 break;
403 case GrColorType::kRGBA_F16: *load = SkRasterPipeline::load_f16;
404 *isNormalized = false;
405 break;
406 case GrColorType::kRG_F32: *load = SkRasterPipeline::load_rgf32;
407 *isNormalized = false;
408 break;
409 case GrColorType::kRGBA_F32: *load = SkRasterPipeline::load_f32;
410 *isNormalized = false;
411 break;
412 case GrColorType::kR_16: *load = SkRasterPipeline::load_a16;
413 swizzle = GrSwizzle("a001");
414 break;
415 case GrColorType::kGray_8: *load = SkRasterPipeline::load_a8;
416 swizzle = GrSwizzle("aaa1");
417 break;
418 case GrColorType::kBGRA_8888: *load = SkRasterPipeline::load_8888;
419 swizzle = GrSwizzle("bgra");
420 break;
421 case GrColorType::kRGB_888x: *load = SkRasterPipeline::load_8888;
422 swizzle = GrSwizzle("rgb1");
423 break;
424
425 case GrColorType::kUnknown:
426 case GrColorType::kRGB_ETC1:
427 SK_ABORT("unexpected CT");
428 }
429 return swizzle;
430}
431
432static GrSwizzle get_dst_swizzle_and_store(GrColorType ct, SkRasterPipeline::StockStage* store,
433 bool* isNormalized) {
434 GrSwizzle swizzle("rgba");
435 *isNormalized = true;
436 switch (ct) {
437 case GrColorType::kAlpha_8: *store = SkRasterPipeline::store_a8; break;
438 case GrColorType::kBGR_565: *store = SkRasterPipeline::store_565; break;
439 case GrColorType::kABGR_4444: *store = SkRasterPipeline::store_4444; break;
440 case GrColorType::kRGBA_8888: *store = SkRasterPipeline::store_8888; break;
441 case GrColorType::kRG_88: *store = SkRasterPipeline::store_rg88; break;
442 case GrColorType::kRGBA_1010102: *store = SkRasterPipeline::store_1010102; break;
443 case GrColorType::kRGBA_F16_Clamped: *store = SkRasterPipeline::store_f16; break;
444 case GrColorType::kRG_1616: *store = SkRasterPipeline::store_rg1616; break;
445 case GrColorType::kRGBA_16161616: *store = SkRasterPipeline::store_16161616; break;
446
Brian Salomone14cfbe2019-06-24 15:00:58 -0400447 case GrColorType::kRG_F16: *store = SkRasterPipeline::store_rgf16;
Brian Salomonf30b1c12019-06-20 12:25:02 -0400448 *isNormalized = false;
449 break;
450 case GrColorType::kAlpha_F16: *store = SkRasterPipeline::store_af16;
451 *isNormalized = false;
452 break;
453 case GrColorType::kRGBA_F16: *store = SkRasterPipeline::store_f16;
454 *isNormalized = false;
455 break;
456 case GrColorType::kRG_F32: *store = SkRasterPipeline::store_rgf32;
457 *isNormalized = false;
458 break;
459 case GrColorType::kRGBA_F32: *store = SkRasterPipeline::store_f32;
460 *isNormalized = false;
461 break;
462 case GrColorType::kR_16: swizzle = GrSwizzle("000r");
463 *store = SkRasterPipeline::store_a16;
464 break;
465 case GrColorType::kBGRA_8888: swizzle = GrSwizzle("bgra");
466 *store = SkRasterPipeline::store_8888;
467 break;
468 case GrColorType::kRGB_888x: swizzle = GrSwizzle("rgb1");
469 *store = SkRasterPipeline::store_8888;
470 break;
471
472 case GrColorType::kGray_8: // not currently supported as output
473 case GrColorType::kUnknown:
474 case GrColorType::kRGB_ETC1:
475 SK_ABORT("unexpected CT");
476 }
477 return swizzle;
478}
479
480static inline void append_clamp_gamut(SkRasterPipeline* pipeline) {
481 // SkRasterPipeline may not know our color type and also doesn't like caller to directly
482 // append clamp_gamut. Fake it out.
483 static SkImageInfo fakeII = SkImageInfo::MakeN32Premul(1, 1);
484 pipeline->append_gamut_clamp_if_normalized(fakeII);
485}
486
487bool GrConvertPixels(const GrPixelInfo& dstInfo, void* dst, const GrPixelInfo& srcInfo,
488 const void* src, GrSwizzle swizzle) {
Brian Salomonc42eb662019-06-24 17:13:00 -0400489 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
490
Brian Salomonf30b1c12019-06-20 12:25:02 -0400491 if (dstInfo.fWidth != srcInfo.fWidth || srcInfo.fHeight != dstInfo.fHeight) {
492 return false;
493 }
494 if (dstInfo.fWidth <= 0 || dstInfo.fHeight <= 0) {
495 return false;
496 }
497 if (GrColorTypeComponentFlags(dstInfo.fColorInfo.fColorType) & kGray_SkColorTypeComponentFlag) {
498 // We don't currently support conversion to Gray.
499 return false;
500 }
501 size_t srcBpp = GrColorTypeBytesPerPixel(srcInfo.fColorInfo.fColorType);
502 size_t dstBpp = GrColorTypeBytesPerPixel(dstInfo.fColorInfo.fColorType);
503 if (!srcBpp || !dstBpp) {
504 // Either src or dst is compressed or kUnknown.
505 return false;
506 }
507 // SkRasterPipeline operates on row-pixels not row-bytes.
508 SkASSERT(dstInfo.fRowBytes % dstBpp == 0);
509 SkASSERT(srcInfo.fRowBytes % srcBpp == 0);
510
511 SkRasterPipeline::StockStage load;
512 bool srcIsNormalized;
513 auto loadSwizzle =
514 get_load_and_get_swizzle(srcInfo.fColorInfo.fColorType, &load, &srcIsNormalized);
515 loadSwizzle = GrSwizzle::Concat(loadSwizzle, swizzle);
516
517 SkRasterPipeline::StockStage store;
518 bool dstIsNormalized;
519 auto storeSwizzle =
520 get_dst_swizzle_and_store(dstInfo.fColorInfo.fColorType, &store, &dstIsNormalized);
521
522 bool alphaOrCSConversion =
523 (srcInfo.fColorInfo.fAlphaType != dstInfo.fColorInfo.fAlphaType &&
524 srcInfo.fColorInfo.fAlphaType != kOpaque_SkAlphaType) ||
525 !SkColorSpace::Equals(srcInfo.fColorInfo.fColorSpace, dstInfo.fColorInfo.fColorSpace);
526
527 bool clampGamut;
528 SkTLazy<SkColorSpaceXformSteps> steps;
529 GrSwizzle loadStoreSwizzle;
530 if (alphaOrCSConversion) {
531 steps.init(srcInfo.fColorInfo.fColorSpace, srcInfo.fColorInfo.fAlphaType,
532 dstInfo.fColorInfo.fColorSpace, dstInfo.fColorInfo.fAlphaType);
533 clampGamut = dstIsNormalized && dstInfo.fColorInfo.fAlphaType == kPremul_SkAlphaType;
534 } else {
535 clampGamut = dstIsNormalized && !srcIsNormalized &&
536 dstInfo.fColorInfo.fAlphaType == kPremul_SkAlphaType;
537 if (!clampGamut) {
538 loadStoreSwizzle = GrSwizzle::Concat(loadSwizzle, storeSwizzle);
539 }
540 }
541 int cnt = 1;
542 int height = srcInfo.fHeight;
543 SkRasterPipeline_MemoryCtx srcCtx{const_cast<void*>(src), SkToInt(srcInfo.fRowBytes / srcBpp)},
544 dstCtx{ dst , SkToInt(dstInfo.fRowBytes / dstBpp)};
545
546 if (srcInfo.fOrigin != dstInfo.fOrigin) {
547 // It *almost* works to point the src at the last row and negate the stride and run the
548 // whole rectangle. However, SkRasterPipeline::run()'s control loop uses size_t loop
549 // variables so it winds up relying on unsigned overflow math. It works out in practice
550 // but UBSAN says "no!" as it's technically undefined and in theory a compiler could emit
551 // code that didn't do what is intended. So we go one row at a time. :(
552 srcCtx.pixels = static_cast<char*>(srcCtx.pixels) + srcInfo.fRowBytes * (height - 1);
553 std::swap(cnt, height);
554 }
555 for (int i = 0; i < cnt; ++i) {
556 SkRasterPipeline_<256> pipeline;
557 pipeline.append(load, &srcCtx);
558
559 if (alphaOrCSConversion) {
560 loadSwizzle.apply(&pipeline);
561 steps->apply(&pipeline, srcIsNormalized);
562 if (clampGamut) {
563 append_clamp_gamut(&pipeline);
564 }
565 storeSwizzle.apply(&pipeline);
566 } else {
567 if (clampGamut) {
568 loadSwizzle.apply(&pipeline);
569 append_clamp_gamut(&pipeline);
570 storeSwizzle.apply(&pipeline);
571 } else {
572 loadStoreSwizzle.apply(&pipeline);
573 }
574 }
575 pipeline.append(store, &dstCtx);
576 pipeline.run(0, 0, srcInfo.fWidth, height);
577 srcCtx.pixels = static_cast<char*>(srcCtx.pixels) - srcInfo.fRowBytes;
578 dstCtx.pixels = static_cast<char*>(dstCtx.pixels) + dstInfo.fRowBytes;
579 }
580 return true;
581}