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" 13 #include "src/gpu/GrColorSpaceInfo.h" 14 #include "src/gpu/GrSwizzle.h" 15 16 size_t GrCompressedDataSize(SkImage::CompressionType, int w, int h); 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. 21 size_t GrComputeTightCombinedBufferSize(size_t bytesPerPixel, int baseWidth, int baseHeight, 22 SkTArray<size_t>* individualMipOffsets, int mipLevelCount); 23 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); 29 class GrPixelInfo { 30 public: 31 GrPixelInfo() = default; 32 33 // not explicit GrPixelInfo(const SkImageInfo & info)34 GrPixelInfo(const SkImageInfo& info) 35 : fColorInfo(SkColorTypeToGrColorType(info.colorType()), info.alphaType(), 36 info.refColorSpace()) 37 , fWidth(info.width()) 38 , fHeight(info.height()) {} 39 GrPixelInfo(GrColorType ct,SkAlphaType at,sk_sp<SkColorSpace> cs,int w,int h)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 makeColorType(GrColorType ct)48 GrPixelInfo makeColorType(GrColorType ct) { 49 return {ct, this->alphaType(), this->refColorSpace(), this->width(), this->height()}; 50 } 51 makeAlphaType(SkAlphaType at)52 GrPixelInfo makeAlphaType(SkAlphaType at) { 53 return {this->colorType(), at, this->refColorSpace(), this->width(), this->height()}; 54 } 55 makeWH(int width,int height)56 GrPixelInfo makeWH(int width, int height) { 57 return {this->colorType(), this->alphaType(), this->refColorSpace(), width, height}; 58 } 59 colorType()60 GrColorType colorType() const { return fColorInfo.colorType(); } 61 alphaType()62 SkAlphaType alphaType() const { return fColorInfo.alphaType(); } 63 colorSpace()64 SkColorSpace* colorSpace() const { return fColorInfo.colorSpace(); } 65 refColorSpace()66 sk_sp<SkColorSpace> refColorSpace() const { return fColorInfo.refColorSpace(); } 67 width()68 int width() const { return fWidth; } 69 height()70 int height() const { return fHeight; } 71 bpp()72 size_t bpp() const { return GrColorTypeBytesPerPixel(this->colorType()); } 73 minRowBytes()74 size_t minRowBytes() const { return this->bpp() * this->width(); } 75 76 /** 77 * Place this pixel rect in a surface of dimensions surfaceWidth x surfaceHeight size offset at 78 * surfacePt and then clip the pixel rectangle to the bounds of the surface. If the pixel rect 79 * does not intersect the rectangle or is empty then return false. If clipped, the input 80 * surfacePt, the width/height of this GrPixelInfo, and the data pointer will be modified to 81 * reflect the clipped rectangle. 82 */ 83 template <typename T> clip(int surfaceWidth,int surfaceHeight,SkIPoint * surfacePt,T ** data,size_t rowBytes)84 bool clip(int surfaceWidth, int surfaceHeight, SkIPoint* surfacePt, T** data, size_t rowBytes) { 85 auto bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight); 86 auto rect = SkIRect::MakeXYWH(surfacePt->fX, surfacePt->fY, fWidth, fHeight); 87 if (!rect.intersect(bounds)) { 88 return false; 89 } 90 *data = SkTAddOffset<T>(*data, (rect.fTop - surfacePt->fY) * rowBytes + 91 (rect.fLeft - surfacePt->fX) * this->bpp()); 92 surfacePt->fX = rect.fLeft; 93 surfacePt->fY = rect.fTop; 94 fWidth = rect.width(); 95 fHeight = rect.height(); 96 return true; 97 } 98 isValid()99 bool isValid() const { return fColorInfo.isValid() && fWidth > 0 && fHeight > 0; } 100 101 private: 102 GrColorSpaceInfo fColorInfo = {}; 103 int fWidth = 0; 104 int fHeight = 0; 105 }; 106 107 // Swizzle param is applied after loading and before converting from srcInfo to dstInfo. 108 bool GrConvertPixels(const GrPixelInfo& dstInfo, void* dst, size_t dstRB, 109 const GrPixelInfo& srcInfo, const void* src, size_t srcRB, 110 bool flipY = false); 111 112 #endif 113