GrDataUtils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef GrDataUtils_DEFINED
  8. #define GrDataUtils_DEFINED
  9. #include "include/core/SkColor.h"
  10. #include "include/private/GrTypesPriv.h"
  11. #include "src/gpu/GrColorSpaceInfo.h"
  12. #include "src/gpu/GrSwizzle.h"
  13. size_t GrCompressedDataSize(SkImage::CompressionType, int w, int h);
  14. // Compute the size of the buffer required to hold all the mipLevels of the specified type
  15. // of data when all rowBytes are tight.
  16. // Note there may still be padding between the mipLevels to meet alignment requirements.
  17. size_t GrComputeTightCombinedBufferSize(size_t bytesPerPixel, int baseWidth, int baseHeight,
  18. SkTArray<size_t>* individualMipOffsets, int mipLevelCount);
  19. void GrFillInData(GrPixelConfig, int baseWidth, int baseHeight,
  20. const SkTArray<size_t>& individualMipOffsets, char* dest, const SkColor4f& color);
  21. void GrFillInCompressedData(SkImage::CompressionType, int width, int height, char* dest,
  22. const SkColor4f& color);
  23. class GrPixelInfo {
  24. public:
  25. GrPixelInfo() = default;
  26. // not explicit
  27. GrPixelInfo(const SkImageInfo& info)
  28. : fColorInfo(SkColorTypeToGrColorType(info.colorType()), info.alphaType(),
  29. info.refColorSpace())
  30. , fWidth(info.width())
  31. , fHeight(info.height()) {}
  32. GrPixelInfo(GrColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs, int w, int h)
  33. : fColorInfo(ct, at, std::move(cs)), fWidth(w), fHeight(h) {}
  34. GrPixelInfo(const GrPixelInfo&) = default;
  35. GrPixelInfo(GrPixelInfo&&) = default;
  36. GrPixelInfo& operator=(const GrPixelInfo&) = default;
  37. GrPixelInfo& operator=(GrPixelInfo&&) = default;
  38. GrPixelInfo makeColorType(GrColorType ct) {
  39. return {ct, this->alphaType(), this->refColorSpace(), this->width(), this->height()};
  40. }
  41. GrPixelInfo makeAlphaType(SkAlphaType at) {
  42. return {this->colorType(), at, this->refColorSpace(), this->width(), this->height()};
  43. }
  44. GrPixelInfo makeWH(int width, int height) {
  45. return {this->colorType(), this->alphaType(), this->refColorSpace(), width, height};
  46. }
  47. GrColorType colorType() const { return fColorInfo.colorType(); }
  48. SkAlphaType alphaType() const { return fColorInfo.alphaType(); }
  49. SkColorSpace* colorSpace() const { return fColorInfo.colorSpace(); }
  50. sk_sp<SkColorSpace> refColorSpace() const { return fColorInfo.refColorSpace(); }
  51. int width() const { return fWidth; }
  52. int height() const { return fHeight; }
  53. size_t bpp() const { return GrColorTypeBytesPerPixel(this->colorType()); }
  54. size_t minRowBytes() const { return this->bpp() * this->width(); }
  55. /**
  56. * Place this pixel rect in a surface of dimensions surfaceWidth x surfaceHeight size offset at
  57. * surfacePt and then clip the pixel rectangle to the bounds of the surface. If the pixel rect
  58. * does not intersect the rectangle or is empty then return false. If clipped, the input
  59. * surfacePt, the width/height of this GrPixelInfo, and the data pointer will be modified to
  60. * reflect the clipped rectangle.
  61. */
  62. template <typename T>
  63. bool clip(int surfaceWidth, int surfaceHeight, SkIPoint* surfacePt, T** data, size_t rowBytes) {
  64. auto bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
  65. auto rect = SkIRect::MakeXYWH(surfacePt->fX, surfacePt->fY, fWidth, fHeight);
  66. if (!rect.intersect(bounds)) {
  67. return false;
  68. }
  69. *data = SkTAddOffset<T>(*data, (rect.fTop - surfacePt->fY) * rowBytes +
  70. (rect.fLeft - surfacePt->fX) * this->bpp());
  71. surfacePt->fX = rect.fLeft;
  72. surfacePt->fY = rect.fTop;
  73. fWidth = rect.width();
  74. fHeight = rect.height();
  75. return true;
  76. }
  77. bool isValid() const { return fColorInfo.isValid() && fWidth > 0 && fHeight > 0; }
  78. private:
  79. GrColorSpaceInfo fColorInfo = {};
  80. int fWidth = 0;
  81. int fHeight = 0;
  82. };
  83. // Swizzle param is applied after loading and before converting from srcInfo to dstInfo.
  84. bool GrConvertPixels(const GrPixelInfo& dstInfo, void* dst, size_t dstRB,
  85. const GrPixelInfo& srcInfo, const void* src, size_t srcRB,
  86. bool flipY = false, GrSwizzle swizzle = GrSwizzle{});
  87. #endif