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