xref: /aosp_15_r20/external/skia/src/core/SkImageInfo.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2010 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 #include "include/core/SkImageInfo.h"
9 
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/private/base/SkAssert.h"
13 #include "src/base/SkSafeMath.h"
14 #include "src/core/SkImageInfoPriv.h"
15 
SkColorTypeBytesPerPixel(SkColorType ct)16 int SkColorTypeBytesPerPixel(SkColorType ct) {
17     switch (ct) {
18         case kUnknown_SkColorType:            return 0;
19         case kAlpha_8_SkColorType:            return 1;
20         case kRGB_565_SkColorType:            return 2;
21         case kARGB_4444_SkColorType:          return 2;
22         case kRGBA_8888_SkColorType:          return 4;
23         case kBGRA_8888_SkColorType:          return 4;
24         case kRGB_888x_SkColorType:           return 4;
25         case kRGBA_1010102_SkColorType:       return 4;
26         case kRGB_101010x_SkColorType:        return 4;
27         case kBGRA_1010102_SkColorType:       return 4;
28         case kBGR_101010x_SkColorType:        return 4;
29         case kBGR_101010x_XR_SkColorType:     return 4;
30         case kBGRA_10101010_XR_SkColorType:   return 8;
31         case kRGBA_10x6_SkColorType:          return 8;
32         case kGray_8_SkColorType:             return 1;
33         case kRGBA_F16Norm_SkColorType:       return 8;
34         case kRGBA_F16_SkColorType:           return 8;
35         case kRGB_F16F16F16x_SkColorType:     return 8;
36         case kRGBA_F32_SkColorType:           return 16;
37         case kR8G8_unorm_SkColorType:         return 2;
38         case kA16_unorm_SkColorType:          return 2;
39         case kR16G16_unorm_SkColorType:       return 4;
40         case kA16_float_SkColorType:          return 2;
41         case kR16G16_float_SkColorType:       return 4;
42         case kR16G16B16A16_unorm_SkColorType: return 8;
43         case kSRGBA_8888_SkColorType:         return 4;
44         case kR8_unorm_SkColorType:           return 1;
45     }
46     SkUNREACHABLE;
47 }
48 
SkColorTypeIsAlwaysOpaque(SkColorType ct)49 bool SkColorTypeIsAlwaysOpaque(SkColorType ct) {
50     return !(SkColorTypeChannelFlags(ct) & kAlpha_SkColorChannelFlag);
51 }
52 
53 ///////////////////////////////////////////////////////////////////////////////////////////////////
54 
SkYUVColorSpaceIsLimitedRange(SkYUVColorSpace cs)55 bool SkYUVColorSpaceIsLimitedRange(SkYUVColorSpace cs) {
56     switch (cs) {
57         case kRec601_Limited_SkYUVColorSpace:
58         case kRec709_Limited_SkYUVColorSpace:
59         case kBT2020_8bit_Limited_SkYUVColorSpace:
60         case kBT2020_10bit_Limited_SkYUVColorSpace:
61         case kBT2020_12bit_Limited_SkYUVColorSpace:
62         case kBT2020_16bit_Limited_SkYUVColorSpace:
63         case kFCC_Limited_SkYUVColorSpace:
64         case kSMPTE240_Limited_SkYUVColorSpace:
65         case kYDZDX_Limited_SkYUVColorSpace:
66         case kGBR_Limited_SkYUVColorSpace:
67         case kYCgCo_8bit_Limited_SkYUVColorSpace:
68         case kYCgCo_10bit_Limited_SkYUVColorSpace:
69         case kYCgCo_12bit_Limited_SkYUVColorSpace:
70         case kYCgCo_16bit_Limited_SkYUVColorSpace:
71             return true;
72 
73         case kJPEG_Full_SkYUVColorSpace:
74         case kRec709_Full_SkYUVColorSpace:
75         case kBT2020_8bit_Full_SkYUVColorSpace:
76         case kBT2020_10bit_Full_SkYUVColorSpace:
77         case kBT2020_12bit_Full_SkYUVColorSpace:
78         case kBT2020_16bit_Full_SkYUVColorSpace:
79         case kFCC_Full_SkYUVColorSpace:
80         case kSMPTE240_Full_SkYUVColorSpace:
81         case kYDZDX_Full_SkYUVColorSpace:
82         case kGBR_Full_SkYUVColorSpace:
83         case kYCgCo_8bit_Full_SkYUVColorSpace:
84         case kYCgCo_10bit_Full_SkYUVColorSpace:
85         case kYCgCo_12bit_Full_SkYUVColorSpace:
86         case kYCgCo_16bit_Full_SkYUVColorSpace:
87         case kIdentity_SkYUVColorSpace:
88             return false;
89 
90         default:
91             SkUNREACHABLE;
92     }
93 }
94 
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 
97 SkColorInfo::SkColorInfo() = default;
98 SkColorInfo::~SkColorInfo() = default;
99 
SkColorInfo(SkColorType ct,SkAlphaType at,sk_sp<SkColorSpace> cs)100 SkColorInfo::SkColorInfo(SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
101             : fColorSpace(std::move(cs)), fColorType(ct), fAlphaType(at) {}
102 
103 SkColorInfo::SkColorInfo(const SkColorInfo&) = default;
104 SkColorInfo::SkColorInfo(SkColorInfo&&) = default;
105 
106 SkColorInfo& SkColorInfo::operator=(const SkColorInfo&) = default;
107 SkColorInfo& SkColorInfo::operator=(SkColorInfo&&) = default;
108 
colorSpace() const109 SkColorSpace* SkColorInfo::colorSpace() const { return fColorSpace.get(); }
refColorSpace() const110 sk_sp<SkColorSpace> SkColorInfo::refColorSpace() const { return fColorSpace; }
111 
operator ==(const SkColorInfo & other) const112 bool SkColorInfo::operator==(const SkColorInfo& other) const {
113     return fColorType == other.fColorType && fAlphaType == other.fAlphaType &&
114            SkColorSpace::Equals(fColorSpace.get(), other.fColorSpace.get());
115 }
116 
operator !=(const SkColorInfo & other) const117 bool SkColorInfo::operator!=(const SkColorInfo& other) const { return !(*this == other); }
118 
makeAlphaType(SkAlphaType newAlphaType) const119 SkColorInfo SkColorInfo::makeAlphaType(SkAlphaType newAlphaType) const {
120     return SkColorInfo(this->colorType(), newAlphaType, this->refColorSpace());
121 }
122 
makeColorType(SkColorType newColorType) const123 SkColorInfo SkColorInfo::makeColorType(SkColorType newColorType) const {
124     return SkColorInfo(newColorType, this->alphaType(), this->refColorSpace());
125 }
126 
makeColorSpace(sk_sp<SkColorSpace> cs) const127 SkColorInfo SkColorInfo::makeColorSpace(sk_sp<SkColorSpace> cs) const {
128     return SkColorInfo(this->colorType(), this->alphaType(), std::move(cs));
129 }
130 
bytesPerPixel() const131 int SkColorInfo::bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
132 
gammaCloseToSRGB() const133 bool SkColorInfo::gammaCloseToSRGB() const {
134     return fColorSpace && fColorSpace->gammaCloseToSRGB();
135 }
136 
shiftPerPixel() const137 int SkColorInfo::shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
138 
139 ///////////////////////////////////////////////////////////////////////////////////////////////////
140 
computeOffset(int x,int y,size_t rowBytes) const141 size_t SkImageInfo::computeOffset(int x, int y, size_t rowBytes) const {
142     SkASSERT((unsigned)x < (unsigned)this->width());
143     SkASSERT((unsigned)y < (unsigned)this->height());
144     return SkColorTypeComputeOffset(this->colorType(), x, y, rowBytes);
145 }
146 
computeByteSize(size_t rowBytes) const147 size_t SkImageInfo::computeByteSize(size_t rowBytes) const {
148     if (0 == this->height()) {
149         return 0;
150     }
151     SkSafeMath safe;
152     size_t bytes = safe.add(safe.mul(safe.addInt(this->height(), -1), rowBytes),
153                             safe.mul(this->width(), this->bytesPerPixel()));
154 
155     // The CPU backend implements some memory operations on images using instructions that take a
156     // signed 32-bit offset from the base. If we ever make an image larger than that, overflow can
157     // cause us to read/write memory that starts 2GB *before* the buffer. (crbug.com/1264705)
158     constexpr size_t kMaxSigned32BitSize = SK_MaxS32;
159     return (safe.ok() && (bytes <= kMaxSigned32BitSize)) ? bytes : SIZE_MAX;
160 }
161 
colorSpace() const162 SkColorSpace* SkImageInfo::colorSpace() const { return fColorInfo.colorSpace(); }
163 
refColorSpace() const164 sk_sp<SkColorSpace> SkImageInfo::refColorSpace() const { return fColorInfo.refColorSpace(); }
165 
makeColorSpace(sk_sp<SkColorSpace> cs) const166 SkImageInfo SkImageInfo::makeColorSpace(sk_sp<SkColorSpace> cs) const {
167     return Make(fDimensions, fColorInfo.makeColorSpace(std::move(cs)));
168 }
169 
Make(int width,int height,SkColorType ct,SkAlphaType at)170 SkImageInfo SkImageInfo::Make(int width, int height, SkColorType ct, SkAlphaType at) {
171     return Make(width, height, ct, at, nullptr);
172 }
173 
Make(int width,int height,SkColorType ct,SkAlphaType at,sk_sp<SkColorSpace> cs)174 SkImageInfo SkImageInfo::Make(int width, int height, SkColorType ct, SkAlphaType at,
175                               sk_sp<SkColorSpace> cs) {
176     return SkImageInfo({width, height}, {ct, at, std::move(cs)});
177 }
178 
Make(SkISize dimensions,SkColorType ct,SkAlphaType at)179 SkImageInfo SkImageInfo::Make(SkISize dimensions, SkColorType ct, SkAlphaType at) {
180     return Make(dimensions, ct, at, nullptr);
181 }
182 
Make(SkISize dimensions,SkColorType ct,SkAlphaType at,sk_sp<SkColorSpace> cs)183 SkImageInfo SkImageInfo::Make(SkISize dimensions, SkColorType ct, SkAlphaType at,
184                         sk_sp<SkColorSpace> cs) {
185     return SkImageInfo(dimensions, {ct, at, std::move(cs)});
186 }
187 
MakeN32(int width,int height,SkAlphaType at)188 SkImageInfo SkImageInfo::MakeN32(int width, int height, SkAlphaType at) {
189     return MakeN32(width, height, at, nullptr);
190 }
191 
MakeN32(int width,int height,SkAlphaType at,sk_sp<SkColorSpace> cs)192 SkImageInfo SkImageInfo::MakeN32(int width, int height, SkAlphaType at, sk_sp<SkColorSpace> cs) {
193     return Make({width, height}, kN32_SkColorType, at, std::move(cs));
194 }
195 
MakeS32(int width,int height,SkAlphaType at)196 SkImageInfo SkImageInfo::MakeS32(int width, int height, SkAlphaType at) {
197     return SkImageInfo({width, height}, {kN32_SkColorType, at, SkColorSpace::MakeSRGB()});
198 }
199 
MakeN32Premul(int width,int height)200 SkImageInfo SkImageInfo::MakeN32Premul(int width, int height) {
201     return MakeN32Premul(width, height, nullptr);
202 }
203 
MakeN32Premul(int width,int height,sk_sp<SkColorSpace> cs)204 SkImageInfo SkImageInfo::MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs) {
205     return Make({width, height}, kN32_SkColorType, kPremul_SkAlphaType, std::move(cs));
206 }
207 
MakeN32Premul(SkISize dimensions)208 SkImageInfo SkImageInfo::MakeN32Premul(SkISize dimensions) {
209     return MakeN32Premul(dimensions, nullptr);
210 }
211 
MakeN32Premul(SkISize dimensions,sk_sp<SkColorSpace> cs)212 SkImageInfo SkImageInfo::MakeN32Premul(SkISize dimensions, sk_sp<SkColorSpace> cs) {
213     return Make(dimensions, kN32_SkColorType, kPremul_SkAlphaType, std::move(cs));
214 }
215 
MakeA8(int width,int height)216 SkImageInfo SkImageInfo::MakeA8(int width, int height) {
217     return Make({width, height}, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr);
218 }
219 
MakeA8(SkISize dimensions)220 SkImageInfo SkImageInfo::MakeA8(SkISize dimensions) {
221     return Make(dimensions, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr);
222 }
223 
MakeUnknown(int width,int height)224 SkImageInfo SkImageInfo::MakeUnknown(int width, int height) {
225     return Make({width, height}, kUnknown_SkColorType, kUnknown_SkAlphaType, nullptr);
226 }
227 
228 #ifdef SK_DEBUG
validate() const229 void SkImageInfo::validate() const {
230     SkASSERT(fDimensions.width() >= 0);
231     SkASSERT(fDimensions.height() >= 0);
232     SkASSERT(SkColorTypeIsValid(this->colorType()));
233     SkASSERT(SkAlphaTypeIsValid(this->alphaType()));
234 }
235 #endif
236 
SkColorTypeValidateAlphaType(SkColorType colorType,SkAlphaType alphaType,SkAlphaType * canonical)237 bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
238                                   SkAlphaType* canonical) {
239     switch (colorType) {
240         case kUnknown_SkColorType:
241             alphaType = kUnknown_SkAlphaType;
242             break;
243         case kAlpha_8_SkColorType:         // fall-through
244         case kA16_unorm_SkColorType:       // fall-through
245         case kA16_float_SkColorType:
246             if (kUnpremul_SkAlphaType == alphaType) {
247                 alphaType = kPremul_SkAlphaType;
248             }
249             [[fallthrough]];
250         case kARGB_4444_SkColorType:
251         case kRGBA_8888_SkColorType:
252         case kSRGBA_8888_SkColorType:
253         case kBGRA_8888_SkColorType:
254         case kRGBA_1010102_SkColorType:
255         case kBGRA_1010102_SkColorType:
256         case kRGBA_10x6_SkColorType:
257         case kRGBA_F16Norm_SkColorType:
258         case kRGBA_F16_SkColorType:
259         case kRGBA_F32_SkColorType:
260         case kBGRA_10101010_XR_SkColorType:
261         case kR16G16B16A16_unorm_SkColorType:
262             if (kUnknown_SkAlphaType == alphaType) {
263                 return false;
264             }
265             break;
266         case kGray_8_SkColorType:
267         case kR8G8_unorm_SkColorType:
268         case kR16G16_unorm_SkColorType:
269         case kR16G16_float_SkColorType:
270         case kRGB_565_SkColorType:
271         case kRGB_888x_SkColorType:
272         case kRGB_101010x_SkColorType:
273         case kBGR_101010x_SkColorType:
274         case kBGR_101010x_XR_SkColorType:
275         case kRGB_F16F16F16x_SkColorType:
276         case kR8_unorm_SkColorType:
277             alphaType = kOpaque_SkAlphaType;
278             break;
279     }
280     if (canonical) {
281         *canonical = alphaType;
282     }
283     return true;
284 }
285