Debian Codegraph @

src/gpu/ganesh/GrDataUtils.cpp

raw
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/ganesh/GrDataUtils.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkColorType.h"
13 #include "include/core/SkPixmap.h"
14 #include "include/core/SkSize.h"
15 #include "include/private/base/SkAssert.h"
16 #include "include/private/base/SkMath.h"
17 #include "include/private/base/SkTemplates.h"
18 #include "include/private/base/SkTo.h"
19 #include "include/private/gpu/ganesh/GrTypesPriv.h"
20 #include "modules/skcms/skcms.h"
21 #include "src/base/SkArenaAlloc.h"
22 #include "src/base/SkRectMemcpy.h"
23 #include "src/core/SkColorSpaceXformSteps.h"
24 #include "src/core/SkRasterPipeline.h"
25 #include "src/core/SkRasterPipelineOpContexts.h"
26 #include "src/core/SkRasterPipelineOpList.h"
27 #include "src/core/SkTraceEvent.h"
28 #include "src/gpu/Swizzle.h"
29 #include "src/gpu/ganesh/GrImageInfo.h"
30 #include "src/gpu/ganesh/GrPixmap.h"
31
32 #include <algorithm>
33 #include <cstdint>
34 #include <cstring>
35 #include <functional>
36
37 using namespace skia_private;
38
39 size_t GrComputeTightCombinedBufferSize(size_t bytesPerPixel, SkISize baseDimensions,
40 TArray<size_t>* individualMipOffsets, int mipLevelCount) {
41 SkASSERT(individualMipOffsets && individualMipOffsets->empty());
42 SkASSERT(mipLevelCount >= 1);
43
44 individualMipOffsets->push_back(0);
45
46 size_t combinedBufferSize = baseDimensions.width() * bytesPerPixel * baseDimensions.height();
47 SkISize levelDimensions = baseDimensions;
48
49 // The Vulkan spec for copying a buffer to an image requires that the alignment must be at
50 // least 4 bytes and a multiple of the bytes per pixel of the image config.
51 SkASSERT(bytesPerPixel == 1 || bytesPerPixel == 2 || bytesPerPixel == 3 ||
52 bytesPerPixel == 4 || bytesPerPixel == 8 || bytesPerPixel == 16);
53 int desiredAlignment = (bytesPerPixel == 3) ? 12 : (bytesPerPixel > 4 ? bytesPerPixel : 4);
54
55 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; ++currentMipLevel) {
56 levelDimensions = {std::max(1, levelDimensions.width() /2),
57 std::max(1, levelDimensions.height()/2)};
58
59 size_t trimmedSize = levelDimensions.area() * bytesPerPixel;
60 const size_t alignmentDiff = combinedBufferSize % desiredAlignment;
61 if (alignmentDiff != 0) {
62 combinedBufferSize += desiredAlignment - alignmentDiff;
63 }
64 SkASSERT((0 == combinedBufferSize % 4) && (0 == combinedBufferSize % bytesPerPixel));
65
66 individualMipOffsets->push_back(combinedBufferSize);
67 combinedBufferSize += trimmedSize;
68 }
69
70 SkASSERT(individualMipOffsets->size() == mipLevelCount);
71 return combinedBufferSize;
72 }
73
74 static skgpu::Swizzle get_load_and_src_swizzle(GrColorType ct, SkRasterPipelineOp* load,
75 bool* isNormalized, bool* isSRGB) {
76 skgpu::Swizzle swizzle("rgba");
77 *isNormalized = true;
78 *isSRGB = false;
79 switch (ct) {
80 case GrColorType::kAlpha_8: *load = SkRasterPipelineOp::load_a8; break;
81 case GrColorType::kAlpha_16: *load = SkRasterPipelineOp::load_a16; break;
82 case GrColorType::kBGR_565: *load = SkRasterPipelineOp::load_565; break;
83 case GrColorType::kRGB_565: swizzle = skgpu::Swizzle("bgr1");
84 *load = SkRasterPipelineOp::load_565; break;
85 case GrColorType::kABGR_4444: *load = SkRasterPipelineOp::load_4444; break;
86 case GrColorType::kARGB_4444: swizzle = skgpu::Swizzle("bgra");
87 *load = SkRasterPipelineOp::load_4444; break;
88 case GrColorType::kBGRA_4444: swizzle = skgpu::Swizzle("gbar");
89 *load = SkRasterPipelineOp::load_4444; break;
90 case GrColorType::kRGBA_8888: *load = SkRasterPipelineOp::load_8888; break;
91 case GrColorType::kRG_88: *load = SkRasterPipelineOp::load_rg88; break;
92 case GrColorType::kRGBA_1010102: *load = SkRasterPipelineOp::load_1010102; break;
93 case GrColorType::kBGRA_1010102: *load = SkRasterPipelineOp::load_1010102;
94 swizzle = skgpu::Swizzle("bgra");
95 break;
96 case GrColorType::kRGB_101010x: *load = SkRasterPipelineOp::load_1010102;
97 swizzle = skgpu::Swizzle("rgb1");
98 break;
99 case GrColorType::kRGBA_10x6: *load = SkRasterPipelineOp::load_10x6; break;
100 case GrColorType::kAlpha_F16: *load = SkRasterPipelineOp::load_af16; break;
101 case GrColorType::kRGBA_F16_Clamped: *load = SkRasterPipelineOp::load_f16; break;
102 case GrColorType::kRG_1616: *load = SkRasterPipelineOp::load_rg1616; break;
103 case GrColorType::kRGBA_16161616: *load = SkRasterPipelineOp::load_16161616; break;
104
105 case GrColorType::kRGBA_8888_SRGB: *load = SkRasterPipelineOp::load_8888;
106 *isSRGB = true;
107 break;
108 case GrColorType::kRG_F16: *load = SkRasterPipelineOp::load_rgf16;
109 *isNormalized = false;
110 break;
111 case GrColorType::kRGBA_F16: *load = SkRasterPipelineOp::load_f16;
112 *isNormalized = false;
113 break;
114 case GrColorType::kRGB_F16F16F16x: *load = SkRasterPipelineOp::load_f16;
115 *isNormalized = false;
116 swizzle = skgpu::Swizzle("rgb1");
117 break;
118 case GrColorType::kRGBA_F32: *load = SkRasterPipelineOp::load_f32;
119 *isNormalized = false;
120 break;
121 case GrColorType::kAlpha_8xxx: *load = SkRasterPipelineOp::load_8888;
122 swizzle = skgpu::Swizzle("000r");
123 break;
124 case GrColorType::kAlpha_F32xxx: *load = SkRasterPipelineOp::load_f32;
125 swizzle = skgpu::Swizzle("000r");
126 break;
127 case GrColorType::kGray_8xxx: *load = SkRasterPipelineOp::load_8888;
128 swizzle = skgpu::Swizzle("rrr1");
129 break;
130 case GrColorType::kGray_8: *load = SkRasterPipelineOp::load_a8;
131 swizzle = skgpu::Swizzle("aaa1");
132 break;
133 case GrColorType::kR_8xxx: *load = SkRasterPipelineOp::load_8888;
134 swizzle = skgpu::Swizzle("r001");
135 break;
136 case GrColorType::kR_8: *load = SkRasterPipelineOp::load_a8;
137 swizzle = skgpu::Swizzle("a001");
138 break;
139 case GrColorType::kGrayAlpha_88: *load = SkRasterPipelineOp::load_rg88;
140 swizzle = skgpu::Swizzle("rrrg");
141 break;
142 case GrColorType::kBGRA_8888: *load = SkRasterPipelineOp::load_8888;
143 swizzle = skgpu::Swizzle("bgra");
144 break;
145 case GrColorType::kRGB_888x: *load = SkRasterPipelineOp::load_8888;
146 swizzle = skgpu::Swizzle("rgb1");
147 break;
148 case GrColorType::kR_16: *load = SkRasterPipelineOp::load_r16;
149 swizzle = skgpu::Swizzle("r001");
150 break;
151 // These are color types we don't expect to ever have to load.
152 case GrColorType::kRGB_888:
153 case GrColorType::kR_F16:
154 case GrColorType::kGray_F16:
155 case GrColorType::kUnknown:
156 SK_ABORT("unexpected CT");
157 }
158 return swizzle;
159 }
160
161 enum class LumMode {
162 kNone,
163 kToRGB,
164 kToAlpha
165 };
166
167 static skgpu::Swizzle get_dst_swizzle_and_store(GrColorType ct, SkRasterPipelineOp* store,
168 LumMode* lumMode, bool* isNormalized,
169 bool* isSRGB) {
170 skgpu::Swizzle swizzle("rgba");
171 *isNormalized = true;
172 *isSRGB = false;
173 *lumMode = LumMode::kNone;
174 switch (ct) {
175 case GrColorType::kAlpha_8: *store = SkRasterPipelineOp::store_a8; break;
176 case GrColorType::kAlpha_16: *store = SkRasterPipelineOp::store_a16; break;
177 case GrColorType::kBGR_565: *store = SkRasterPipelineOp::store_565; break;
178 case GrColorType::kRGB_565: swizzle = skgpu::Swizzle("bgr1");
179 *store = SkRasterPipelineOp::store_565; break;
180 case GrColorType::kABGR_4444: *store = SkRasterPipelineOp::store_4444; break;
181 case GrColorType::kARGB_4444: swizzle = skgpu::Swizzle("bgra");
182 *store = SkRasterPipelineOp::store_4444; break;
183 case GrColorType::kBGRA_4444: swizzle = skgpu::Swizzle("argb");
184 *store = SkRasterPipelineOp::store_4444; break;
185 case GrColorType::kRGBA_8888: *store = SkRasterPipelineOp::store_8888; break;
186 case GrColorType::kRG_88: *store = SkRasterPipelineOp::store_rg88; break;
187 case GrColorType::kRGBA_1010102: *store = SkRasterPipelineOp::store_1010102; break;
188 case GrColorType::kBGRA_1010102: swizzle = skgpu::Swizzle("bgra");
189 *store = SkRasterPipelineOp::store_1010102;
190 break;
191 case GrColorType::kRGB_101010x: swizzle = skgpu::Swizzle("rgb1");
192 *store = SkRasterPipelineOp::store_1010102;
193 break;
194 case GrColorType::kRGBA_10x6: *store = SkRasterPipelineOp::store_10x6; break;
195 case GrColorType::kRGBA_F16_Clamped: *store = SkRasterPipelineOp::store_f16; break;
196 case GrColorType::kRG_1616: *store = SkRasterPipelineOp::store_rg1616; break;
197 case GrColorType::kRGBA_16161616: *store = SkRasterPipelineOp::store_16161616; break;
198
199 case GrColorType::kRGBA_8888_SRGB: *store = SkRasterPipelineOp::store_8888;
200 *isSRGB = true;
201 break;
202 case GrColorType::kRG_F16: *store = SkRasterPipelineOp::store_rgf16;
203 *isNormalized = false;
204 break;
205 case GrColorType::kAlpha_F16: *store = SkRasterPipelineOp::store_af16;
206 *isNormalized = false;
207 break;
208 case GrColorType::kRGBA_F16: *store = SkRasterPipelineOp::store_f16;
209 *isNormalized = false;
210 break;
211 case GrColorType::kRGB_F16F16F16x: swizzle = skgpu::Swizzle("rgb1");
212 *store = SkRasterPipelineOp::store_f16;
213 *isNormalized = false;
214 break;
215 case GrColorType::kRGBA_F32: *store = SkRasterPipelineOp::store_f32;
216 *isNormalized = false;
217 break;
218 case GrColorType::kAlpha_8xxx: *store = SkRasterPipelineOp::store_8888;
219 swizzle = skgpu::Swizzle("a000");
220 break;
221 case GrColorType::kAlpha_F32xxx: *store = SkRasterPipelineOp::store_f32;
222 swizzle = skgpu::Swizzle("a000");
223 break;
224 case GrColorType::kBGRA_8888: swizzle = skgpu::Swizzle("bgra");
225 *store = SkRasterPipelineOp::store_8888;
226 break;
227 case GrColorType::kRGB_888x: swizzle = skgpu::Swizzle("rgb1");
228 *store = SkRasterPipelineOp::store_8888;
229 break;
230 case GrColorType::kR_8xxx: swizzle = skgpu::Swizzle("r001");
231 *store = SkRasterPipelineOp::store_8888;
232 break;
233 case GrColorType::kR_8: swizzle = skgpu::Swizzle("agbr");
234 *store = SkRasterPipelineOp::store_a8;
235 break;
236 case GrColorType::kR_16: swizzle = skgpu::Swizzle("r001");
237 *store = SkRasterPipelineOp::store_r16;
238 break;
239 case GrColorType::kR_F16: swizzle = skgpu::Swizzle("agbr");
240 *store = SkRasterPipelineOp::store_af16;
241 break;
242 case GrColorType::kGray_F16: *lumMode = LumMode::kToAlpha;
243 *store = SkRasterPipelineOp::store_af16;
244 break;
245 case GrColorType::kGray_8: *lumMode = LumMode::kToAlpha;
246 *store = SkRasterPipelineOp::store_a8;
247 break;
248 case GrColorType::kGrayAlpha_88: *lumMode = LumMode::kToRGB;
249 swizzle = skgpu::Swizzle("ragb");
250 *store = SkRasterPipelineOp::store_rg88;
251 break;
252 case GrColorType::kGray_8xxx: *lumMode = LumMode::kToRGB;
253 *store = SkRasterPipelineOp::store_8888;
254 swizzle = skgpu::Swizzle("r000");
255 break;
256
257 // These are color types we don't expect to ever have to store.
258 case GrColorType::kRGB_888: // This is handled specially in GrConvertPixels.
259 case GrColorType::kUnknown:
260 SK_ABORT("unexpected CT");
261 }
262 return swizzle;
263 }
264
265 bool GrConvertPixels(const GrPixmap& dst, const GrCPixmap& src, bool flipY) {
266 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
267 if (src.dimensions().isEmpty() || dst.dimensions().isEmpty()) {
268 return false;
269 }
270 if (src.colorType() == GrColorType::kUnknown || dst.colorType() == GrColorType::kUnknown) {
271 return false;
272 }
273 if (!src.hasPixels() || !dst.hasPixels()) {
274 return false;
275 }
276 if (dst.dimensions() != src.dimensions()) {
277 return false;
278 }
279 if (dst.colorType() == GrColorType::kRGB_888) {
280 // SkRasterPipeline doesn't handle writing to RGB_888. So we have it write to RGB_888x and
281 // then do another conversion that does the 24bit packing. We could be cleverer and skip the
282 // temp pixmap if this is the only conversion but this is rare so keeping it simple.
283 GrPixmap temp = GrPixmap::Allocate(dst.info().makeColorType(GrColorType::kRGB_888x));
284 if (!GrConvertPixels(temp, src, flipY)) {
285 return false;
286 }
287 auto* tRow = reinterpret_cast<const char*>(temp.addr());
288 auto* dRow = reinterpret_cast<char*>(dst.addr());
289 for (int y = 0; y < dst.height(); ++y, tRow += temp.rowBytes(), dRow += dst.rowBytes()) {
290 for (int x = 0; x < dst.width(); ++x) {
291 auto t = tRow + x*sizeof(uint32_t);
292 auto d = dRow + x*3;
293 memcpy(d, t, 3);
294 }
295 }
296 return true;
297 } else if (src.colorType() == GrColorType::kRGB_888) {
298 // SkRasterPipeline doesn't handle reading from RGB_888. So convert it to RGB_888x and then
299 // do a recursive call if there is any remaining conversion.
300 GrPixmap temp = GrPixmap::Allocate(src.info().makeColorType(GrColorType::kRGB_888x));
301 auto* sRow = reinterpret_cast<const char*>(src.addr());
302 auto* tRow = reinterpret_cast<char*>(temp.addr());
303 for (int y = 0; y < src.height(); ++y, sRow += src.rowBytes(), tRow += temp.rowBytes()) {
304 for (int x = 0; x < src.width(); ++x) {
305 auto s = sRow + x*3;
306 auto t = tRow + x*sizeof(uint32_t);
307 memcpy(t, s, 3);
308 t[3] = static_cast<char>(0xFF);
309 }
310 }
311 return GrConvertPixels(dst, temp, flipY);
312 }
313
314 size_t srcBpp = src.info().bpp();
315 size_t dstBpp = dst.info().bpp();
316
317 // SkRasterPipeline operates on row-pixels not row-bytes.
318 SkASSERT(dst.rowBytes() % dstBpp == 0);
319 SkASSERT(src.rowBytes() % srcBpp == 0);
320
321 bool premul = src.alphaType() == kUnpremul_SkAlphaType &&
322 dst.alphaType() == kPremul_SkAlphaType;
323 bool unpremul = src.alphaType() == kPremul_SkAlphaType &&
324 dst.alphaType() == kUnpremul_SkAlphaType;
325 bool alphaOrCSConversion =
326 premul || unpremul || !SkColorSpace::Equals(src.colorSpace(), dst.colorSpace());
327
328 if (src.colorType() == dst.colorType() && !alphaOrCSConversion) {
329 size_t tightRB = dstBpp * dst.width();
330 if (flipY) {
331 auto s = static_cast<const char*>(src.addr());
332 auto d = SkTAddOffset<char>(dst.addr(), dst.rowBytes()*(dst.height() - 1));
333 for (int y = 0; y < dst.height(); ++y, d -= dst.rowBytes(), s += src.rowBytes()) {
334 memcpy(d, s, tightRB);
335 }
336 } else {
337 SkRectMemcpy(dst.addr(), dst.rowBytes(),
338 src.addr(), src.rowBytes(),
339 tightRB, src.height());
340 }
341 return true;
342 }
343
344 SkRasterPipelineOp load;
345 bool srcIsNormalized;
346 bool srcIsSRGB;
347 auto loadSwizzle = get_load_and_src_swizzle(src.colorType(),
348 &load,
349 &srcIsNormalized,
350 &srcIsSRGB);
351
352 SkRasterPipelineOp store;
353 LumMode lumMode;
354 bool dstIsNormalized;
355 bool dstIsSRGB;
356 auto storeSwizzle = get_dst_swizzle_and_store(dst.colorType(),
357 &store,
358 &lumMode,
359 &dstIsNormalized,
360 &dstIsSRGB);
361
362 std::optional<SkColorSpaceXformSteps> steps;
363 skgpu::Swizzle loadStoreSwizzle;
364 if (alphaOrCSConversion) {
365 steps.emplace(src.colorSpace(), src.alphaType(), dst.colorSpace(), dst.alphaType());
366 } else {
367 loadStoreSwizzle = skgpu::Swizzle::Concat(loadSwizzle, storeSwizzle);
368 }
369 int cnt = 1;
370 int height = src.height();
371 SkRasterPipelineContexts::MemoryCtx
372 srcCtx{const_cast<void*>(src.addr()), SkToInt(src.rowBytes()/srcBpp)},
373 dstCtx{ dst.addr(), SkToInt(dst.rowBytes()/dstBpp)};
374
375 if (flipY) {
376 // It *almost* works to point the src at the last row and negate the stride and run the
377 // whole rectangle. However, SkRasterPipeline::run()'s control loop uses size_t loop
378 // variables so it winds up relying on unsigned overflow math. It works out in practice
379 // but UBSAN says "no!" as it's technically undefined and in theory a compiler could emit
380 // code that didn't do what is intended. So we go one row at a time. :(
381 srcCtx.pixels = static_cast<char*>(srcCtx.pixels) + src.rowBytes()*(height - 1);
382 std::swap(cnt, height);
383 }
384
385 bool hasConversion = alphaOrCSConversion || lumMode != LumMode::kNone;
386
387 if (srcIsSRGB && dstIsSRGB && !hasConversion) {
388 // No need to convert from srgb if we are just going to immediately convert it back.
389 srcIsSRGB = dstIsSRGB = false;
390 }
391
392 hasConversion = hasConversion || srcIsSRGB || dstIsSRGB;
393
394 SkRasterPipeline_<256> pipeline;
395 pipeline.append(load, &srcCtx);
396 if (hasConversion) {
397 loadSwizzle.apply(&pipeline);
398 if (srcIsSRGB) {
399 pipeline.appendTransferFunction(*skcms_sRGB_TransferFunction());
400 }
401 if (alphaOrCSConversion) {
402 steps->apply(&pipeline);
403 }
404 switch (lumMode) {
405 case LumMode::kNone:
406 break;
407 case LumMode::kToRGB:
408 pipeline.append(SkRasterPipelineOp::bt709_luminance_or_luma_to_rgb);
409 break;
410 case LumMode::kToAlpha:
411 pipeline.append(SkRasterPipelineOp::bt709_luminance_or_luma_to_alpha);
412 // If we ever need to store srgb-encoded gray (e.g. GL_SLUMINANCE8) then we
413 // should use ToRGB and then a swizzle stage rather than ToAlpha. The subsequent
414 // transfer function stage ignores the alpha channel (where we just stashed the
415 // gray).
416 SkASSERT(!dstIsSRGB);
417 break;
418 }
419 if (dstIsSRGB) {
420 pipeline.appendTransferFunction(*skcms_sRGB_Inverse_TransferFunction());
421 }
422 storeSwizzle.apply(&pipeline);
423 } else {
424 loadStoreSwizzle.apply(&pipeline);
425 }
426 pipeline.append(store, &dstCtx);
427 auto pipelineFn = pipeline.compile();
428 for (int i = 0; i < cnt; ++i) {
429 pipelineFn(0, 0, src.width(), height);
430 srcCtx.pixels = static_cast<char*>(srcCtx.pixels) - src.rowBytes();
431 dstCtx.pixels = static_cast<char*>(dstCtx.pixels) + dst.rowBytes();
432 }
433
434 return true;
435 }
436
437 bool GrClearImage(const GrImageInfo& dstInfo, void* dst, size_t dstRB, std::array<float, 4> color) {
438 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
439
440 if (!dstInfo.isValid()) {
441 return false;
442 }
443 if (!dst) {
444 return false;
445 }
446 if (dstRB < dstInfo.minRowBytes()) {
447 return false;
448 }
449 if (dstInfo.colorType() == GrColorType::kRGB_888) {
450 // SkRasterPipeline doesn't handle writing to RGB_888. So we handle that specially here.
451 uint32_t rgba = SkColor4f{color[0], color[1], color[2], color[3]}.toBytes_RGBA();
452 for (int y = 0; y < dstInfo.height(); ++y) {
453 char* d = static_cast<char*>(dst) + y * dstRB;
454 for (int x = 0; x < dstInfo.width(); ++x, d += 3) {
455 memcpy(d, &rgba, 3);
456 }
457 }
458 return true;
459 }
460
461 LumMode lumMode;
462 bool isNormalized;
463 bool dstIsSRGB;
464 SkRasterPipelineOp store;
465 skgpu::Swizzle storeSwizzle = get_dst_swizzle_and_store(dstInfo.colorType(), &store, &lumMode,
466 &isNormalized, &dstIsSRGB);
467 char block[64];
468 SkArenaAlloc alloc(block, sizeof(block), 1024);
469 SkRasterPipeline_<256> pipeline;
470 pipeline.appendConstantColor(&alloc, color.data());
471 switch (lumMode) {
472 case LumMode::kNone:
473 break;
474 case LumMode::kToRGB:
475 pipeline.append(SkRasterPipelineOp::bt709_luminance_or_luma_to_rgb);
476 break;
477 case LumMode::kToAlpha:
478 pipeline.append(SkRasterPipelineOp::bt709_luminance_or_luma_to_alpha);
479 // If we ever need to store srgb-encoded gray (e.g. GL_SLUMINANCE8) then we should use
480 // ToRGB and then a swizzle stage rather than ToAlpha. The subsequent transfer function
481 // stage ignores the alpha channel (where we just stashed the gray).
482 SkASSERT(!dstIsSRGB);
483 break;
484 }
485 if (dstIsSRGB) {
486 pipeline.appendTransferFunction(*skcms_sRGB_Inverse_TransferFunction());
487 }
488 storeSwizzle.apply(&pipeline);
489 SkRasterPipelineContexts::MemoryCtx dstCtx{dst, SkToInt(dstRB / dstInfo.bpp())};
490 pipeline.append(store, &dstCtx);
491 pipeline.run(0, 0, dstInfo.width(), dstInfo.height());
492
493 return true;
494 }