Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
GrDataUtils.h File Reference
#include "include/core/SkColor.h"
#include "include/private/base/SkTArray.h"
#include <array>
#include <cstddef>

Go to the source code of this file.

Functions

size_t GrComputeTightCombinedBufferSize (size_t bytesPerPixel, SkISize baseDimensions, skia_private::TArray< size_t > *individualMipOffsets, int mipLevelCount)
 
bool GrConvertPixels (const GrPixmap &dst, const GrCPixmap &src, bool flipY=false)
 
bool GrClearImage (const GrImageInfo &dstInfo, void *dst, size_t dstRB, std::array< float, 4 > color)
 

Function Documentation

◆ GrClearImage()

bool GrClearImage ( const GrImageInfo dstInfo,
void *  dst,
size_t  dstRB,
std::array< float, 4 >  color 
)

Clears the dst image to a constant color.

Definition at line 507 of file GrDataUtils.cpp.

507 {
508 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
509
510 if (!dstInfo.isValid()) {
511 return false;
512 }
513 if (!dst) {
514 return false;
515 }
516 if (dstRB < dstInfo.minRowBytes()) {
517 return false;
518 }
519 if (dstInfo.colorType() == GrColorType::kRGB_888) {
520 // SkRasterPipeline doesn't handle writing to RGB_888. So we handle that specially here.
521 uint32_t rgba = SkColor4f{color[0], color[1], color[2], color[3]}.toBytes_RGBA();
522 for (int y = 0; y < dstInfo.height(); ++y) {
523 char* d = static_cast<char*>(dst) + y * dstRB;
524 for (int x = 0; x < dstInfo.width(); ++x, d += 3) {
525 memcpy(d, &rgba, 3);
526 }
527 }
528 return true;
529 }
530
531 LumMode lumMode;
532 bool isNormalized;
533 bool dstIsSRGB;
535 skgpu::Swizzle storeSwizzle = get_dst_swizzle_and_store(dstInfo.colorType(), &store, &lumMode,
536 &isNormalized, &dstIsSRGB);
537 char block[64];
538 SkArenaAlloc alloc(block, sizeof(block), 1024);
539 SkRasterPipeline_<256> pipeline;
540 pipeline.appendConstantColor(&alloc, color.data());
541 switch (lumMode) {
542 case LumMode::kNone:
543 break;
544 case LumMode::kToRGB:
545 pipeline.append(SkRasterPipelineOp::bt709_luminance_or_luma_to_rgb);
546 break;
548 pipeline.append(SkRasterPipelineOp::bt709_luminance_or_luma_to_alpha);
549 // If we ever need to store srgb-encoded gray (e.g. GL_SLUMINANCE8) then we should use
550 // ToRGB and then a swizzle stage rather than ToAlpha. The subsequent transfer function
551 // stage ignores the alpha channel (where we just stashed the gray).
552 SkASSERT(!dstIsSRGB);
553 break;
554 }
555 if (dstIsSRGB) {
557 }
558 storeSwizzle.apply(&pipeline);
559 SkRasterPipeline_MemoryCtx dstCtx{dst, SkToInt(dstRB/dstInfo.bpp())};
560 pipeline.append(store, &dstCtx);
561 pipeline.run(0, 0, dstInfo.width(), dstInfo.height());
562
563 return true;
564}
LumMode
static skgpu::Swizzle get_dst_swizzle_and_store(GrColorType ct, SkRasterPipelineOp *store, LumMode *lumMode, bool *isNormalized, bool *isSRGB)
SkColor4f color
static const uint32_t rgba[kNumPixels]
#define SkASSERT(cond)
Definition SkAssert.h:116
constexpr int SkToInt(S x)
Definition SkTo.h:29
#define TRACE_FUNC
SI void store(P *ptr, const T &val)
int width() const
Definition GrImageInfo.h:54
GrColorType colorType() const
Definition GrImageInfo.h:44
size_t bpp() const
Definition GrImageInfo.h:58
int height() const
Definition GrImageInfo.h:56
bool isValid() const
Definition GrImageInfo.h:62
size_t minRowBytes() const
Definition GrImageInfo.h:60
void run(size_t x, size_t y, size_t w, size_t h) const
void appendTransferFunction(const skcms_TransferFunction &)
void append(SkRasterPipelineOp, void *=nullptr)
void appendConstantColor(SkArenaAlloc *, const float rgba[4])
void apply(SkRasterPipeline *) const
Definition Swizzle.cpp:17
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
double y
double x
dst
Definition cp.py:12
const skcms_TransferFunction * skcms_sRGB_Inverse_TransferFunction()
Definition skcms.cc:1591
#define TRACE_EVENT0(category_group, name)

◆ GrComputeTightCombinedBufferSize()

size_t GrComputeTightCombinedBufferSize ( size_t  bytesPerPixel,
SkISize  baseDimensions,
skia_private::TArray< size_t > *  individualMipOffsets,
int  mipLevelCount 
)

Definition at line 124 of file GrDataUtils.cpp.

125 {
126 SkASSERT(individualMipOffsets && individualMipOffsets->empty());
127 SkASSERT(mipLevelCount >= 1);
128
129 individualMipOffsets->push_back(0);
130
131 size_t combinedBufferSize = baseDimensions.width() * bytesPerPixel * baseDimensions.height();
132 SkISize levelDimensions = baseDimensions;
133
134 // The Vulkan spec for copying a buffer to an image requires that the alignment must be at
135 // least 4 bytes and a multiple of the bytes per pixel of the image config.
136 SkASSERT(bytesPerPixel == 1 || bytesPerPixel == 2 || bytesPerPixel == 3 ||
137 bytesPerPixel == 4 || bytesPerPixel == 8 || bytesPerPixel == 16);
138 int desiredAlignment = (bytesPerPixel == 3) ? 12 : (bytesPerPixel > 4 ? bytesPerPixel : 4);
139
140 for (int currentMipLevel = 1; currentMipLevel < mipLevelCount; ++currentMipLevel) {
141 levelDimensions = {std::max(1, levelDimensions.width() /2),
142 std::max(1, levelDimensions.height()/2)};
143
144 size_t trimmedSize = levelDimensions.area() * bytesPerPixel;
145 const size_t alignmentDiff = combinedBufferSize % desiredAlignment;
146 if (alignmentDiff != 0) {
147 combinedBufferSize += desiredAlignment - alignmentDiff;
148 }
149 SkASSERT((0 == combinedBufferSize % 4) && (0 == combinedBufferSize % bytesPerPixel));
150
151 individualMipOffsets->push_back(combinedBufferSize);
152 combinedBufferSize += trimmedSize;
153 }
154
155 SkASSERT(individualMipOffsets->size() == mipLevelCount);
156 return combinedBufferSize;
157}
bool empty() const
Definition SkTArray.h:194
int size() const
Definition SkTArray.h:416
constexpr int32_t width() const
Definition SkSize.h:36
constexpr int32_t height() const
Definition SkSize.h:37
constexpr int64_t area() const
Definition SkSize.h:39

◆ GrConvertPixels()

bool GrConvertPixels ( const GrPixmap dst,
const GrCPixmap src,
bool  flipY = false 
)

Definition at line 335 of file GrDataUtils.cpp.

335 {
336 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
337 if (src.dimensions().isEmpty() || dst.dimensions().isEmpty()) {
338 return false;
339 }
340 if (src.colorType() == GrColorType::kUnknown || dst.colorType() == GrColorType::kUnknown) {
341 return false;
342 }
343 if (!src.hasPixels() || !dst.hasPixels()) {
344 return false;
345 }
346 if (dst.dimensions() != src.dimensions()) {
347 return false;
348 }
349 if (dst.colorType() == GrColorType::kRGB_888) {
350 // SkRasterPipeline doesn't handle writing to RGB_888. So we have it write to RGB_888x and
351 // then do another conversion that does the 24bit packing. We could be cleverer and skip the
352 // temp pixmap if this is the only conversion but this is rare so keeping it simple.
353 GrPixmap temp = GrPixmap::Allocate(dst.info().makeColorType(GrColorType::kRGB_888x));
354 if (!GrConvertPixels(temp, src, flipY)) {
355 return false;
356 }
357 auto* tRow = reinterpret_cast<const char*>(temp.addr());
358 auto* dRow = reinterpret_cast<char*>(dst.addr());
359 for (int y = 0; y < dst.height(); ++y, tRow += temp.rowBytes(), dRow += dst.rowBytes()) {
360 for (int x = 0; x < dst.width(); ++x) {
361 auto t = tRow + x*sizeof(uint32_t);
362 auto d = dRow + x*3;
363 memcpy(d, t, 3);
364 }
365 }
366 return true;
367 } else if (src.colorType() == GrColorType::kRGB_888) {
368 // SkRasterPipeline doesn't handle reading from RGB_888. So convert it to RGB_888x and then
369 // do a recursive call if there is any remaining conversion.
370 GrPixmap temp = GrPixmap::Allocate(src.info().makeColorType(GrColorType::kRGB_888x));
371 auto* sRow = reinterpret_cast<const char*>(src.addr());
372 auto* tRow = reinterpret_cast<char*>(temp.addr());
373 for (int y = 0; y < src.height(); ++y, sRow += src.rowBytes(), tRow += temp.rowBytes()) {
374 for (int x = 0; x < src.width(); ++x) {
375 auto s = sRow + x*3;
376 auto t = tRow + x*sizeof(uint32_t);
377 memcpy(t, s, 3);
378 t[3] = static_cast<char>(0xFF);
379 }
380 }
381 return GrConvertPixels(dst, temp, flipY);
382 }
383
384 size_t srcBpp = src.info().bpp();
385 size_t dstBpp = dst.info().bpp();
386
387 // SkRasterPipeline operates on row-pixels not row-bytes.
388 SkASSERT(dst.rowBytes() % dstBpp == 0);
389 SkASSERT(src.rowBytes() % srcBpp == 0);
390
391 bool premul = src.alphaType() == kUnpremul_SkAlphaType &&
392 dst.alphaType() == kPremul_SkAlphaType;
393 bool unpremul = src.alphaType() == kPremul_SkAlphaType &&
394 dst.alphaType() == kUnpremul_SkAlphaType;
395 bool alphaOrCSConversion =
396 premul || unpremul || !SkColorSpace::Equals(src.colorSpace(), dst.colorSpace());
397
398 if (src.colorType() == dst.colorType() && !alphaOrCSConversion) {
399 size_t tightRB = dstBpp * dst.width();
400 if (flipY) {
401 auto s = static_cast<const char*>(src.addr());
402 auto d = SkTAddOffset<char>(dst.addr(), dst.rowBytes()*(dst.height() - 1));
403 for (int y = 0; y < dst.height(); ++y, d -= dst.rowBytes(), s += src.rowBytes()) {
404 memcpy(d, s, tightRB);
405 }
406 } else {
407 SkRectMemcpy(dst.addr(), dst.rowBytes(),
408 src.addr(), src.rowBytes(),
409 tightRB, src.height());
410 }
411 return true;
412 }
413
415 bool srcIsNormalized;
416 bool srcIsSRGB;
417 auto loadSwizzle = get_load_and_src_swizzle(src.colorType(),
418 &load,
419 &srcIsNormalized,
420 &srcIsSRGB);
421
423 LumMode lumMode;
424 bool dstIsNormalized;
425 bool dstIsSRGB;
426 auto storeSwizzle = get_dst_swizzle_and_store(dst.colorType(),
427 &store,
428 &lumMode,
429 &dstIsNormalized,
430 &dstIsSRGB);
431
433 skgpu::Swizzle loadStoreSwizzle;
434 if (alphaOrCSConversion) {
435 steps.init(src.colorSpace(), src.alphaType(), dst.colorSpace(), dst.alphaType());
436 } else {
437 loadStoreSwizzle = skgpu::Swizzle::Concat(loadSwizzle, storeSwizzle);
438 }
439 int cnt = 1;
440 int height = src.height();
442 srcCtx{const_cast<void*>(src.addr()), SkToInt(src.rowBytes()/srcBpp)},
443 dstCtx{ dst.addr(), SkToInt(dst.rowBytes()/dstBpp)};
444
445 if (flipY) {
446 // It *almost* works to point the src at the last row and negate the stride and run the
447 // whole rectangle. However, SkRasterPipeline::run()'s control loop uses size_t loop
448 // variables so it winds up relying on unsigned overflow math. It works out in practice
449 // but UBSAN says "no!" as it's technically undefined and in theory a compiler could emit
450 // code that didn't do what is intended. So we go one row at a time. :(
451 srcCtx.pixels = static_cast<char*>(srcCtx.pixels) + src.rowBytes()*(height - 1);
452 std::swap(cnt, height);
453 }
454
455 bool hasConversion = alphaOrCSConversion || lumMode != LumMode::kNone;
456
457 if (srcIsSRGB && dstIsSRGB && !hasConversion) {
458 // No need to convert from srgb if we are just going to immediately convert it back.
459 srcIsSRGB = dstIsSRGB = false;
460 }
461
462 hasConversion = hasConversion || srcIsSRGB || dstIsSRGB;
463
464 SkRasterPipeline_<256> pipeline;
465 pipeline.append(load, &srcCtx);
466 if (hasConversion) {
467 loadSwizzle.apply(&pipeline);
468 if (srcIsSRGB) {
470 }
471 if (alphaOrCSConversion) {
472 steps->apply(&pipeline);
473 }
474 switch (lumMode) {
475 case LumMode::kNone:
476 break;
477 case LumMode::kToRGB:
478 pipeline.append(SkRasterPipelineOp::bt709_luminance_or_luma_to_rgb);
479 break;
481 pipeline.append(SkRasterPipelineOp::bt709_luminance_or_luma_to_alpha);
482 // If we ever need to store srgb-encoded gray (e.g. GL_SLUMINANCE8) then we
483 // should use ToRGB and then a swizzle stage rather than ToAlpha. The subsequent
484 // transfer function stage ignores the alpha channel (where we just stashed the
485 // gray).
486 SkASSERT(!dstIsSRGB);
487 break;
488 }
489 if (dstIsSRGB) {
491 }
492 storeSwizzle.apply(&pipeline);
493 } else {
494 loadStoreSwizzle.apply(&pipeline);
495 }
496 pipeline.append(store, &dstCtx);
497 auto pipelineFn = pipeline.compile();
498 for (int i = 0; i < cnt; ++i) {
499 pipelineFn(0, 0, src.width(), height);
500 srcCtx.pixels = static_cast<char*>(srcCtx.pixels) - src.rowBytes();
501 dstCtx.pixels = static_cast<char*>(dstCtx.pixels) + dst.rowBytes();
502 }
503
504 return true;
505}
bool GrConvertPixels(const GrPixmap &dst, const GrCPixmap &src, bool flipY)
static skgpu::Swizzle get_load_and_src_swizzle(GrColorType ct, SkRasterPipelineOp *load, bool *isNormalized, bool *isSRGB)
kUnpremul_SkAlphaType
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
static void SkRectMemcpy(void *dst, size_t dstRB, const void *src, size_t srcRB, size_t trimRowBytes, int rowCount)
SI T load(const P *ptr)
static uint32_t premul(uint32_t color)
size_t rowBytes() const
Definition GrPixmap.h:21
T * addr() const
Definition GrPixmap.h:20
static GrPixmap Allocate(const GrImageInfo &info)
Definition GrPixmap.h:101
static bool Equals(const SkColorSpace *, const SkColorSpace *)
std::function< void(size_t, size_t, size_t, size_t)> compile() const
T * init(Args &&... args)
Definition SkTLazy.h:45
static constexpr Swizzle Concat(const Swizzle &a, const Swizzle &b)
Definition Swizzle.h:156
struct MyStruct s
int32_t height
const skcms_TransferFunction * skcms_sRGB_TransferFunction()
Definition skcms.cc:1587