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 | |
| 8 | #ifndef GrDataUtils_DEFINED |
| 9 | #define GrDataUtils_DEFINED |
| 10 | |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/private/GrTypesPriv.h" |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrColorSpaceInfo.h" |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 14 | #include "src/gpu/GrSwizzle.h" |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 15 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 16 | size_t GrCompressedDataSize(SkImage::CompressionType, int w, int h); |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 17 | |
Jim Van Verth | e367101 | 2019-09-18 09:53:31 -0400 | [diff] [blame] | 18 | // Returns a value that can be used to set rowBytes for a transfer function. |
| 19 | size_t GrCompressedRowBytes(SkImage::CompressionType, int w); |
| 20 | |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 21 | // Compute the size of the buffer required to hold all the mipLevels of the specified type |
| 22 | // of data when all rowBytes are tight. |
| 23 | // Note there may still be padding between the mipLevels to meet alignment requirements. |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 24 | size_t GrComputeTightCombinedBufferSize(size_t bytesPerPixel, int baseWidth, int baseHeight, |
| 25 | SkTArray<size_t>* individualMipOffsets, int mipLevelCount); |
Robert Phillips | 28a5a43 | 2019-06-07 12:46:21 -0400 | [diff] [blame] | 26 | |
Brian Salomon | bb8dde8 | 2019-06-27 10:52:13 -0400 | [diff] [blame] | 27 | void GrFillInData(GrPixelConfig, int baseWidth, int baseHeight, |
| 28 | const SkTArray<size_t>& individualMipOffsets, char* dest, const SkColor4f& color); |
| 29 | |
| 30 | void GrFillInCompressedData(SkImage::CompressionType, int width, int height, char* dest, |
| 31 | const SkColor4f& color); |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 32 | class GrPixelInfo { |
| 33 | public: |
| 34 | GrPixelInfo() = default; |
| 35 | |
| 36 | // not explicit |
| 37 | GrPixelInfo(const SkImageInfo& info) |
| 38 | : fColorInfo(SkColorTypeToGrColorType(info.colorType()), info.alphaType(), |
| 39 | info.refColorSpace()) |
| 40 | , fWidth(info.width()) |
| 41 | , fHeight(info.height()) {} |
| 42 | |
| 43 | GrPixelInfo(GrColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs, int w, int h) |
| 44 | : fColorInfo(ct, at, std::move(cs)), fWidth(w), fHeight(h) {} |
| 45 | |
Brian Salomon | 8660eb0 | 2019-09-20 13:04:13 -0400 | [diff] [blame] | 46 | GrPixelInfo(GrColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs, const SkISize& size) |
| 47 | : fColorInfo(ct, at, std::move(cs)), fWidth(size.fWidth), fHeight(size.fHeight) {} |
| 48 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 49 | GrPixelInfo(const GrPixelInfo&) = default; |
| 50 | GrPixelInfo(GrPixelInfo&&) = default; |
| 51 | GrPixelInfo& operator=(const GrPixelInfo&) = default; |
| 52 | GrPixelInfo& operator=(GrPixelInfo&&) = default; |
| 53 | |
| 54 | GrPixelInfo makeColorType(GrColorType ct) { |
| 55 | return {ct, this->alphaType(), this->refColorSpace(), this->width(), this->height()}; |
| 56 | } |
| 57 | |
| 58 | GrPixelInfo makeAlphaType(SkAlphaType at) { |
| 59 | return {this->colorType(), at, this->refColorSpace(), this->width(), this->height()}; |
| 60 | } |
| 61 | |
Brian Salomon | 6aecd5e | 2019-07-16 09:47:32 -0400 | [diff] [blame] | 62 | GrPixelInfo makeWH(int width, int height) { |
| 63 | return {this->colorType(), this->alphaType(), this->refColorSpace(), width, height}; |
| 64 | } |
| 65 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 66 | GrColorType colorType() const { return fColorInfo.colorType(); } |
| 67 | |
| 68 | SkAlphaType alphaType() const { return fColorInfo.alphaType(); } |
| 69 | |
| 70 | SkColorSpace* colorSpace() const { return fColorInfo.colorSpace(); } |
| 71 | |
| 72 | sk_sp<SkColorSpace> refColorSpace() const { return fColorInfo.refColorSpace(); } |
| 73 | |
| 74 | int width() const { return fWidth; } |
| 75 | |
| 76 | int height() const { return fHeight; } |
| 77 | |
| 78 | size_t bpp() const { return GrColorTypeBytesPerPixel(this->colorType()); } |
| 79 | |
| 80 | size_t minRowBytes() const { return this->bpp() * this->width(); } |
| 81 | |
| 82 | /** |
| 83 | * Place this pixel rect in a surface of dimensions surfaceWidth x surfaceHeight size offset at |
| 84 | * surfacePt and then clip the pixel rectangle to the bounds of the surface. If the pixel rect |
| 85 | * does not intersect the rectangle or is empty then return false. If clipped, the input |
| 86 | * surfacePt, the width/height of this GrPixelInfo, and the data pointer will be modified to |
| 87 | * reflect the clipped rectangle. |
| 88 | */ |
| 89 | template <typename T> |
| 90 | bool clip(int surfaceWidth, int surfaceHeight, SkIPoint* surfacePt, T** data, size_t rowBytes) { |
| 91 | auto bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight); |
| 92 | auto rect = SkIRect::MakeXYWH(surfacePt->fX, surfacePt->fY, fWidth, fHeight); |
| 93 | if (!rect.intersect(bounds)) { |
| 94 | return false; |
| 95 | } |
| 96 | *data = SkTAddOffset<T>(*data, (rect.fTop - surfacePt->fY) * rowBytes + |
| 97 | (rect.fLeft - surfacePt->fX) * this->bpp()); |
| 98 | surfacePt->fX = rect.fLeft; |
| 99 | surfacePt->fY = rect.fTop; |
| 100 | fWidth = rect.width(); |
| 101 | fHeight = rect.height(); |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | bool isValid() const { return fColorInfo.isValid() && fWidth > 0 && fHeight > 0; } |
| 106 | |
| 107 | private: |
Brian Salomon | bd3d8d3 | 2019-07-02 09:16:28 -0400 | [diff] [blame] | 108 | GrColorSpaceInfo fColorInfo = {}; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 109 | int fWidth = 0; |
| 110 | int fHeight = 0; |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | // Swizzle param is applied after loading and before converting from srcInfo to dstInfo. |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 114 | bool GrConvertPixels(const GrPixelInfo& dstInfo, void* dst, size_t dstRB, |
| 115 | const GrPixelInfo& srcInfo, const void* src, size_t srcRB, |
Brian Salomon | 8f8354a | 2019-07-31 20:12:02 -0400 | [diff] [blame] | 116 | bool flipY = false); |
Brian Salomon | f30b1c1 | 2019-06-20 12:25:02 -0400 | [diff] [blame] | 117 | |
Robert Phillips | 459b295 | 2019-05-23 09:38:27 -0400 | [diff] [blame] | 118 | #endif |