blob: 819189fd6f3b591446afca4a70c127ea261dcf32 [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 Salomonbd3d8d32019-07-02 09:16:28 -040013#include "src/gpu/GrColorSpaceInfo.h"
Brian Salomonf30b1c12019-06-20 12:25:02 -040014#include "src/gpu/GrSwizzle.h"
Robert Phillips459b2952019-05-23 09:38:27 -040015
Brian Salomonbb8dde82019-06-27 10:52:13 -040016size_t GrCompressedDataSize(SkImage::CompressionType, int w, int h);
Robert Phillips28a5a432019-06-07 12:46:21 -040017
Jim Van Verthe3671012019-09-18 09:53:31 -040018// Returns a value that can be used to set rowBytes for a transfer function.
19size_t GrCompressedRowBytes(SkImage::CompressionType, int w);
20
Robert Phillips28a5a432019-06-07 12:46:21 -040021// Compute the size of the buffer required to hold all the mipLevels of the specified type
22// of data when all rowBytes are tight.
23// Note there may still be padding between the mipLevels to meet alignment requirements.
Brian Salomonbb8dde82019-06-27 10:52:13 -040024size_t GrComputeTightCombinedBufferSize(size_t bytesPerPixel, int baseWidth, int baseHeight,
25 SkTArray<size_t>* individualMipOffsets, int mipLevelCount);
Robert Phillips28a5a432019-06-07 12:46:21 -040026
Brian Salomonbb8dde82019-06-27 10:52:13 -040027void GrFillInData(GrPixelConfig, int baseWidth, int baseHeight,
28 const SkTArray<size_t>& individualMipOffsets, char* dest, const SkColor4f& color);
29
30void GrFillInCompressedData(SkImage::CompressionType, int width, int height, char* dest,
31 const SkColor4f& color);
Brian Salomon1d435302019-07-01 13:05:28 -040032class GrPixelInfo {
33public:
34 GrPixelInfo() = default;
35
36 // not explicit
37 GrPixelInfo(const SkImageInfo& info)
38 : fColorInfo(SkColorTypeToGrColorType(info.colorType()), info.alphaType(),
39 info.refColorSpace())
40 , fWidth(info.width())
41 , fHeight(info.height()) {}
42
43 GrPixelInfo(GrColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs, int w, int h)
44 : fColorInfo(ct, at, std::move(cs)), fWidth(w), fHeight(h) {}
45
46 GrPixelInfo(const GrPixelInfo&) = default;
47 GrPixelInfo(GrPixelInfo&&) = default;
48 GrPixelInfo& operator=(const GrPixelInfo&) = default;
49 GrPixelInfo& operator=(GrPixelInfo&&) = default;
50
51 GrPixelInfo makeColorType(GrColorType ct) {
52 return {ct, this->alphaType(), this->refColorSpace(), this->width(), this->height()};
53 }
54
55 GrPixelInfo makeAlphaType(SkAlphaType at) {
56 return {this->colorType(), at, this->refColorSpace(), this->width(), this->height()};
57 }
58
Brian Salomon6aecd5e2019-07-16 09:47:32 -040059 GrPixelInfo makeWH(int width, int height) {
60 return {this->colorType(), this->alphaType(), this->refColorSpace(), width, height};
61 }
62
Brian Salomon1d435302019-07-01 13:05:28 -040063 GrColorType colorType() const { return fColorInfo.colorType(); }
64
65 SkAlphaType alphaType() const { return fColorInfo.alphaType(); }
66
67 SkColorSpace* colorSpace() const { return fColorInfo.colorSpace(); }
68
69 sk_sp<SkColorSpace> refColorSpace() const { return fColorInfo.refColorSpace(); }
70
71 int width() const { return fWidth; }
72
73 int height() const { return fHeight; }
74
75 size_t bpp() const { return GrColorTypeBytesPerPixel(this->colorType()); }
76
77 size_t minRowBytes() const { return this->bpp() * this->width(); }
78
79 /**
80 * Place this pixel rect in a surface of dimensions surfaceWidth x surfaceHeight size offset at
81 * surfacePt and then clip the pixel rectangle to the bounds of the surface. If the pixel rect
82 * does not intersect the rectangle or is empty then return false. If clipped, the input
83 * surfacePt, the width/height of this GrPixelInfo, and the data pointer will be modified to
84 * reflect the clipped rectangle.
85 */
86 template <typename T>
87 bool clip(int surfaceWidth, int surfaceHeight, SkIPoint* surfacePt, T** data, size_t rowBytes) {
88 auto bounds = SkIRect::MakeWH(surfaceWidth, surfaceHeight);
89 auto rect = SkIRect::MakeXYWH(surfacePt->fX, surfacePt->fY, fWidth, fHeight);
90 if (!rect.intersect(bounds)) {
91 return false;
92 }
93 *data = SkTAddOffset<T>(*data, (rect.fTop - surfacePt->fY) * rowBytes +
94 (rect.fLeft - surfacePt->fX) * this->bpp());
95 surfacePt->fX = rect.fLeft;
96 surfacePt->fY = rect.fTop;
97 fWidth = rect.width();
98 fHeight = rect.height();
99 return true;
100 }
101
102 bool isValid() const { return fColorInfo.isValid() && fWidth > 0 && fHeight > 0; }
103
104private:
Brian Salomonbd3d8d32019-07-02 09:16:28 -0400105 GrColorSpaceInfo fColorInfo = {};
Brian Salomonf30b1c12019-06-20 12:25:02 -0400106 int fWidth = 0;
107 int fHeight = 0;
Brian Salomonf30b1c12019-06-20 12:25:02 -0400108};
109
110// Swizzle param is applied after loading and before converting from srcInfo to dstInfo.
Brian Salomon1d435302019-07-01 13:05:28 -0400111bool GrConvertPixels(const GrPixelInfo& dstInfo, void* dst, size_t dstRB,
112 const GrPixelInfo& srcInfo, const void* src, size_t srcRB,
Brian Salomon8f8354a2019-07-31 20:12:02 -0400113 bool flipY = false);
Brian Salomonf30b1c12019-06-20 12:25:02 -0400114
Robert Phillips459b2952019-05-23 09:38:27 -0400115#endif