blob: d49f0a1d2ac01f5e861aa7b6740d346b1bd93056 [file] [log] [blame]
Brian Salomonf2ebdd92019-09-30 12:15:30 -04001/*
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 Salomon4bc0c1f2019-09-30 15:12:27 -040013#include "src/gpu/GrColorInfo.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040014
15class GrImageInfo {
16public:
17 GrImageInfo() = default;
18
Brian Salomon65f533a2019-10-01 09:59:02 -040019 /* implicit */ GrImageInfo(const SkImageInfo& info)
20 : fColorInfo(info.colorInfo()), fDimensions(info.dimensions()) {}
Brian Salomonf2ebdd92019-09-30 12:15:30 -040021
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 Salomon1a217eb2019-10-24 10:50:36 -040028 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 Salomonf2ebdd92019-09-30 12:15:30 -040034 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 Salomon1a217eb2019-10-24 10:50:36 -040051 const GrColorInfo& colorInfo() const { return fColorInfo; }
52
Brian Salomonf2ebdd92019-09-30 12:15:30 -040053 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
95private:
Brian Salomon4bc0c1f2019-09-30 15:12:27 -040096 GrColorInfo fColorInfo = {};
Brian Salomonf2ebdd92019-09-30 12:15:30 -040097 SkISize fDimensions;
98};
99
100#endif