blob: 2b8b6914de22f51ecd0dd6d239353b64a61297a7 [file] [log] [blame]
Robert Phillips459b2952019-05-23 09:38:27 -04001/*
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 Salomonf30b1c12019-06-20 12:25:02 -040013#include "src/gpu/GrSwizzle.h"
Robert Phillips459b2952019-05-23 09:38:27 -040014
Brian Salomonbb8dde82019-06-27 10:52:13 -040015size_t GrCompressedDataSize(SkImage::CompressionType, int w, int h);
Robert Phillips28a5a432019-06-07 12:46:21 -040016
17// Compute the size of the buffer required to hold all the mipLevels of the specified type
18// of data when all rowBytes are tight.
19// Note there may still be padding between the mipLevels to meet alignment requirements.
Brian Salomonbb8dde82019-06-27 10:52:13 -040020size_t GrComputeTightCombinedBufferSize(size_t bytesPerPixel, int baseWidth, int baseHeight,
21 SkTArray<size_t>* individualMipOffsets, int mipLevelCount);
Robert Phillips28a5a432019-06-07 12:46:21 -040022
Brian Salomonbb8dde82019-06-27 10:52:13 -040023void GrFillInData(GrPixelConfig, int baseWidth, int baseHeight,
24 const SkTArray<size_t>& individualMipOffsets, char* dest, const SkColor4f& color);
25
26void GrFillInCompressedData(SkImage::CompressionType, int width, int height, char* dest,
27 const SkColor4f& color);
Robert Phillips459b2952019-05-23 09:38:27 -040028
Brian Salomon1d435302019-07-01 13:05:28 -040029// TODO: Replace with GrColorSpaceInfo once GrPixelConfig is excised from that type.
30class GrColorInfo {
31public:
32 GrColorInfo() = default;
33
34 GrColorInfo(GrColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
35 : fColorSpace(std::move(cs)), fColorType(ct), fAlphaType(at) {}
36
37 GrColorInfo(const GrColorInfo&) = default;
38 GrColorInfo(GrColorInfo&&) = default;
39 GrColorInfo& operator=(const GrColorInfo&) = default;
40 GrColorInfo& operator=(GrColorInfo&&) = default;
41
42 GrColorType colorType() const { return fColorType; }
43
44 SkAlphaType alphaType() const { return fAlphaType; }
45
46 SkColorSpace* colorSpace() const { return fColorSpace.get(); }
47
48 sk_sp<SkColorSpace> refColorSpace() const { return fColorSpace; }
49
50 bool isValid() const {
51 return fColorType != GrColorType::kUnknown && fAlphaType != kUnknown_SkAlphaType;
52 }
53
54private:
55 sk_sp<SkColorSpace> fColorSpace;
Brian Salomonf30b1c12019-06-20 12:25:02 -040056 GrColorType fColorType = GrColorType::kUnknown;
Brian Salomon1d435302019-07-01 13:05:28 -040057 SkAlphaType fAlphaType = kUnknown_SkAlphaType;
Brian Salomonf30b1c12019-06-20 12:25:02 -040058};
59
Brian Salomon1d435302019-07-01 13:05:28 -040060class GrPixelInfo {
61public:
62 GrPixelInfo() = default;
63
64 // not explicit
65 GrPixelInfo(const SkImageInfo& info)
66 : fColorInfo(SkColorTypeToGrColorType(info.colorType()), info.alphaType(),
67 info.refColorSpace())
68 , fWidth(info.width())
69 , fHeight(info.height()) {}
70
71 GrPixelInfo(GrColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs, int w, int h)
72 : fColorInfo(ct, at, std::move(cs)), fWidth(w), fHeight(h) {}
73
74 GrPixelInfo(const GrPixelInfo&) = default;
75 GrPixelInfo(GrPixelInfo&&) = default;
76 GrPixelInfo& operator=(const GrPixelInfo&) = default;
77 GrPixelInfo& operator=(GrPixelInfo&&) = default;
78
79 GrPixelInfo makeColorType(GrColorType ct) {
80 return {ct, this->alphaType(), this->refColorSpace(), this->width(), this->height()};
81 }
82
83 GrPixelInfo makeAlphaType(SkAlphaType at) {
84 return {this->colorType(), at, this->refColorSpace(), this->width(), this->height()};
85 }
86
87 GrColorType colorType() const { return fColorInfo.colorType(); }
88
89 SkAlphaType alphaType() const { return fColorInfo.alphaType(); }
90
91 SkColorSpace* colorSpace() const { return fColorInfo.colorSpace(); }
92
93 sk_sp<SkColorSpace> refColorSpace() const { return fColorInfo.refColorSpace(); }
94
95 int width() const { return fWidth; }
96
97 int height() const { return fHeight; }
98
99 size_t bpp() const { return GrColorTypeBytesPerPixel(this->colorType()); }
100
101 size_t minRowBytes() const { return this->bpp() * this->width(); }
102
103 /**
104 * Place this pixel rect in a surface of dimensions surfaceWidth x surfaceHeight size offset at
105 * surfacePt and then clip the pixel rectangle to the bounds of the surface. If the pixel rect
106 * does not intersect the rectangle or is empty then return false. If clipped, the input
107 * surfacePt, the width/height of this GrPixelInfo, and the data pointer will be modified to
108 * reflect the clipped rectangle.
109 */
110 template <typename T>
111 bool clip(int surfaceWidth, int surfaceHeight, SkIPoint* surfacePt, T** data, size_t rowBytes) {
112 auto bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
113 auto rect = SkIRect::MakeXYWH(surfacePt->fX, surfacePt->fY, fWidth, fHeight);
114 if (!rect.intersect(bounds)) {
115 return false;
116 }
117 *data = SkTAddOffset<T>(*data, (rect.fTop - surfacePt->fY) * rowBytes +
118 (rect.fLeft - surfacePt->fX) * this->bpp());
119 surfacePt->fX = rect.fLeft;
120 surfacePt->fY = rect.fTop;
121 fWidth = rect.width();
122 fHeight = rect.height();
123 return true;
124 }
125
126 bool isValid() const { return fColorInfo.isValid() && fWidth > 0 && fHeight > 0; }
127
128private:
Brian Salomonf30b1c12019-06-20 12:25:02 -0400129 GrColorInfo fColorInfo = {};
Brian Salomonf30b1c12019-06-20 12:25:02 -0400130 int fWidth = 0;
131 int fHeight = 0;
Brian Salomonf30b1c12019-06-20 12:25:02 -0400132};
133
134// Swizzle param is applied after loading and before converting from srcInfo to dstInfo.
Brian Salomon1d435302019-07-01 13:05:28 -0400135bool GrConvertPixels(const GrPixelInfo& dstInfo, void* dst, size_t dstRB,
136 const GrPixelInfo& srcInfo, const void* src, size_t srcRB,
137 bool flipY = false, GrSwizzle swizzle = GrSwizzle{});
Brian Salomonf30b1c12019-06-20 12:25:02 -0400138
Robert Phillips459b2952019-05-23 09:38:27 -0400139#endif