Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 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 GrImageInfo_DEFINED |
| 9 | #define GrImageInfo_DEFINED |
| 10 | |
| 11 | #include "include/core/SkImageInfo.h" |
| 12 | #include "include/private/GrTypesPriv.h" |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrColorInfo.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 14 | |
| 15 | class GrImageInfo { |
| 16 | public: |
| 17 | GrImageInfo() = default; |
| 18 | |
Brian Salomon | 65f533a | 2019-10-01 09:59:02 -0400 | [diff] [blame] | 19 | /* implicit */ GrImageInfo(const SkImageInfo& info) |
| 20 | : fColorInfo(info.colorInfo()), fDimensions(info.dimensions()) {} |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 21 | |
| 22 | GrImageInfo(GrColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs, int w, int h) |
| 23 | : fColorInfo(ct, at, std::move(cs)), fDimensions{w,h} {} |
| 24 | |
| 25 | GrImageInfo(GrColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs, const SkISize& dimensions) |
| 26 | : fColorInfo(ct, at, std::move(cs)), fDimensions(dimensions) {} |
| 27 | |
Brian Salomon | 1a217eb | 2019-10-24 10:50:36 -0400 | [diff] [blame] | 28 | GrImageInfo(const GrColorInfo& info, const SkISize& dimensions) |
| 29 | : fColorInfo(info), fDimensions(dimensions) {} |
| 30 | |
| 31 | GrImageInfo(GrColorInfo&& info, const SkISize& dimensions) |
| 32 | : fColorInfo(std::move(info)), fDimensions(dimensions) {} |
| 33 | |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 34 | GrImageInfo(const GrImageInfo&) = default; |
| 35 | GrImageInfo(GrImageInfo&&) = default; |
| 36 | GrImageInfo& operator=(const GrImageInfo&) = default; |
| 37 | GrImageInfo& operator=(GrImageInfo&&) = default; |
| 38 | |
| 39 | GrImageInfo makeColorType(GrColorType ct) { |
| 40 | return {ct, this->alphaType(), this->refColorSpace(), this->width(), this->height()}; |
| 41 | } |
| 42 | |
| 43 | GrImageInfo makeAlphaType(SkAlphaType at) { |
| 44 | return {this->colorType(), at, this->refColorSpace(), this->width(), this->height()}; |
| 45 | } |
| 46 | |
| 47 | GrImageInfo makeWH(int width, int height) { |
| 48 | return {this->colorType(), this->alphaType(), this->refColorSpace(), width, height}; |
| 49 | } |
| 50 | |
Brian Salomon | 1a217eb | 2019-10-24 10:50:36 -0400 | [diff] [blame] | 51 | const GrColorInfo& colorInfo() const { return fColorInfo; } |
| 52 | |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 53 | GrColorType colorType() const { return fColorInfo.colorType(); } |
| 54 | |
| 55 | SkAlphaType alphaType() const { return fColorInfo.alphaType(); } |
| 56 | |
| 57 | SkColorSpace* colorSpace() const { return fColorInfo.colorSpace(); } |
| 58 | |
| 59 | sk_sp<SkColorSpace> refColorSpace() const { return fColorInfo.refColorSpace(); } |
| 60 | |
| 61 | SkISize dimensions() const { return fDimensions; } |
| 62 | |
| 63 | int width() const { return fDimensions.width(); } |
| 64 | |
| 65 | int height() const { return fDimensions.height(); } |
| 66 | |
| 67 | size_t bpp() const { return GrColorTypeBytesPerPixel(this->colorType()); } |
| 68 | |
| 69 | size_t minRowBytes() const { return this->bpp() * this->width(); } |
| 70 | |
| 71 | /** |
| 72 | * Place this image rect in a surface of dimensions surfaceWidth x surfaceHeight size offset at |
| 73 | * surfacePt and then clip the pixel rectangle to the bounds of the surface. If the pixel rect |
| 74 | * does not intersect the rectangle or is empty then return false. If clipped, the input |
| 75 | * surfacePt, the width/height of this GrImageInfo, and the data pointer will be modified to |
| 76 | * reflect the clipped rectangle. |
| 77 | */ |
| 78 | template <typename T> |
| 79 | bool clip(int surfaceWidth, int surfaceHeight, SkIPoint* surfacePt, T** data, size_t rowBytes) { |
| 80 | auto bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight); |
| 81 | auto rect = SkIRect::MakeXYWH(surfacePt->fX, surfacePt->fY, this->width(), this->height()); |
| 82 | if (!rect.intersect(bounds)) { |
| 83 | return false; |
| 84 | } |
| 85 | *data = SkTAddOffset<T>(*data, (rect.fTop - surfacePt->fY) * rowBytes + |
| 86 | (rect.fLeft - surfacePt->fX) * this->bpp()); |
| 87 | surfacePt->fX = rect.fLeft; |
| 88 | surfacePt->fY = rect.fTop; |
| 89 | fDimensions = rect.size(); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | bool isValid() const { return fColorInfo.isValid() && this->width() > 0 && this->height() > 0; } |
| 94 | |
| 95 | private: |
Brian Salomon | 4bc0c1f | 2019-09-30 15:12:27 -0400 | [diff] [blame] | 96 | GrColorInfo fColorInfo = {}; |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 97 | SkISize fDimensions; |
| 98 | }; |
| 99 | |
| 100 | #endif |