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