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" |
13 | #include "src/gpu/GrColorInfo.h" |
14 | |
15 | class GrImageInfo { |
16 | public: |
17 | GrImageInfo() = default; |
18 | |
19 | /* implicit */ GrImageInfo(const SkImageInfo& info) |
20 | : fColorInfo(info.colorInfo()), fDimensions(info.dimensions()) {} |
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 | |
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 | |
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) const { |
40 | return {ct, this->alphaType(), this->refColorSpace(), this->width(), this->height()}; |
41 | } |
42 | |
43 | GrImageInfo makeAlphaType(SkAlphaType at) const { |
44 | return {this->colorType(), at, this->refColorSpace(), this->width(), this->height()}; |
45 | } |
46 | |
47 | GrImageInfo makeWH(int width, int height) const { |
48 | return {this->colorType(), this->alphaType(), this->refColorSpace(), width, height}; |
49 | } |
50 | |
51 | const GrColorInfo& colorInfo() const { return fColorInfo; } |
52 | |
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: |
96 | GrColorInfo fColorInfo = {}; |
97 | SkISize fDimensions; |
98 | }; |
99 | |
100 | #endif |
101 | |