• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 kRGBA_F32_SkColorType:           return 16;
36         case kR8G8_unorm_SkColorType:         return 2;
37         case kA16_unorm_SkColorType:          return 2;
38         case kR16G16_unorm_SkColorType:       return 4;
39         case kA16_float_SkColorType:          return 2;
40         case kR16G16_float_SkColorType:       return 4;
41         case kR16G16B16A16_unorm_SkColorType: return 8;
42         case kSRGBA_8888_SkColorType:         return 4;
43         case kR8_unorm_SkColorType:           return 1;
44     }
45     SkUNREACHABLE;
46 }
47 
SkColorTypeIsAlwaysOpaque(SkColorType ct)48 bool SkColorTypeIsAlwaysOpaque(SkColorType ct) {
49     return !(SkColorTypeChannelFlags(ct) & kAlpha_SkColorChannelFlag);
50 }
51 
52 ///////////////////////////////////////////////////////////////////////////////////////////////////
53 
54 SkColorInfo::SkColorInfo() = default;
55 SkColorInfo::~SkColorInfo() = default;
56 
SkColorInfo(SkColorType ct,SkAlphaType at,sk_sp<SkColorSpace> cs)57 SkColorInfo::SkColorInfo(SkColorType ct, SkAlphaType at, sk_sp<SkColorSpace> cs)
58             : fColorSpace(std::move(cs)), fColorType(ct), fAlphaType(at) {}
59 
60 SkColorInfo::SkColorInfo(const SkColorInfo&) = default;
61 SkColorInfo::SkColorInfo(SkColorInfo&&) = default;
62 
63 SkColorInfo& SkColorInfo::operator=(const SkColorInfo&) = default;
64 SkColorInfo& SkColorInfo::operator=(SkColorInfo&&) = default;
65 
colorSpace() const66 SkColorSpace* SkColorInfo::colorSpace() const { return fColorSpace.get(); }
refColorSpace() const67 sk_sp<SkColorSpace> SkColorInfo::refColorSpace() const { return fColorSpace; }
68 
operator ==(const SkColorInfo & other) const69 bool SkColorInfo::operator==(const SkColorInfo& other) const {
70     return fColorType == other.fColorType && fAlphaType == other.fAlphaType &&
71            SkColorSpace::Equals(fColorSpace.get(), other.fColorSpace.get());
72 }
73 
operator !=(const SkColorInfo & other) const74 bool SkColorInfo::operator!=(const SkColorInfo& other) const { return !(*this == other); }
75 
makeAlphaType(SkAlphaType newAlphaType) const76 SkColorInfo SkColorInfo::makeAlphaType(SkAlphaType newAlphaType) const {
77     return SkColorInfo(this->colorType(), newAlphaType, this->refColorSpace());
78 }
79 
makeColorType(SkColorType newColorType) const80 SkColorInfo SkColorInfo::makeColorType(SkColorType newColorType) const {
81     return SkColorInfo(newColorType, this->alphaType(), this->refColorSpace());
82 }
83 
makeColorSpace(sk_sp<SkColorSpace> cs) const84 SkColorInfo SkColorInfo::makeColorSpace(sk_sp<SkColorSpace> cs) const {
85     return SkColorInfo(this->colorType(), this->alphaType(), std::move(cs));
86 }
87 
bytesPerPixel() const88 int SkColorInfo::bytesPerPixel() const { return SkColorTypeBytesPerPixel(fColorType); }
89 
gammaCloseToSRGB() const90 bool SkColorInfo::gammaCloseToSRGB() const {
91     return fColorSpace && fColorSpace->gammaCloseToSRGB();
92 }
93 
shiftPerPixel() const94 int SkColorInfo::shiftPerPixel() const { return SkColorTypeShiftPerPixel(fColorType); }
95 
96 ///////////////////////////////////////////////////////////////////////////////////////////////////
97 
computeOffset(int x,int y,size_t rowBytes) const98 size_t SkImageInfo::computeOffset(int x, int y, size_t rowBytes) const {
99     SkASSERT((unsigned)x < (unsigned)this->width());
100     SkASSERT((unsigned)y < (unsigned)this->height());
101     return SkColorTypeComputeOffset(this->colorType(), x, y, rowBytes);
102 }
103 
computeByteSize(size_t rowBytes) const104 size_t SkImageInfo::computeByteSize(size_t rowBytes) const {
105     if (0 == this->height()) {
106         return 0;
107     }
108     SkSafeMath safe;
109     size_t bytes = safe.add(safe.mul(safe.addInt(this->height(), -1), rowBytes),
110                             safe.mul(this->width(), this->bytesPerPixel()));
111 
112     // The CPU backend implements some memory operations on images using instructions that take a
113     // signed 32-bit offset from the base. If we ever make an image larger than that, overflow can
114     // cause us to read/write memory that starts 2GB *before* the buffer. (crbug.com/1264705)
115     constexpr size_t kMaxSigned32BitSize = SK_MaxS32;
116     return (safe.ok() && (bytes <= kMaxSigned32BitSize)) ? bytes : SIZE_MAX;
117 }
118 
colorSpace() const119 SkColorSpace* SkImageInfo::colorSpace() const { return fColorInfo.colorSpace(); }
120 
refColorSpace() const121 sk_sp<SkColorSpace> SkImageInfo::refColorSpace() const { return fColorInfo.refColorSpace(); }
122 
makeColorSpace(sk_sp<SkColorSpace> cs) const123 SkImageInfo SkImageInfo::makeColorSpace(sk_sp<SkColorSpace> cs) const {
124     return Make(fDimensions, fColorInfo.makeColorSpace(std::move(cs)));
125 }
126 
Make(int width,int height,SkColorType ct,SkAlphaType at)127 SkImageInfo SkImageInfo::Make(int width, int height, SkColorType ct, SkAlphaType at) {
128     return Make(width, height, ct, at, nullptr);
129 }
130 
Make(int width,int height,SkColorType ct,SkAlphaType at,sk_sp<SkColorSpace> cs)131 SkImageInfo SkImageInfo::Make(int width, int height, SkColorType ct, SkAlphaType at,
132                               sk_sp<SkColorSpace> cs) {
133     return SkImageInfo({width, height}, {ct, at, std::move(cs)});
134 }
135 
Make(SkISize dimensions,SkColorType ct,SkAlphaType at)136 SkImageInfo SkImageInfo::Make(SkISize dimensions, SkColorType ct, SkAlphaType at) {
137     return Make(dimensions, ct, at, nullptr);
138 }
139 
Make(SkISize dimensions,SkColorType ct,SkAlphaType at,sk_sp<SkColorSpace> cs)140 SkImageInfo SkImageInfo::Make(SkISize dimensions, SkColorType ct, SkAlphaType at,
141                         sk_sp<SkColorSpace> cs) {
142     return SkImageInfo(dimensions, {ct, at, std::move(cs)});
143 }
144 
MakeN32(int width,int height,SkAlphaType at)145 SkImageInfo SkImageInfo::MakeN32(int width, int height, SkAlphaType at) {
146     return MakeN32(width, height, at, nullptr);
147 }
148 
MakeN32(int width,int height,SkAlphaType at,sk_sp<SkColorSpace> cs)149 SkImageInfo SkImageInfo::MakeN32(int width, int height, SkAlphaType at, sk_sp<SkColorSpace> cs) {
150     return Make({width, height}, kN32_SkColorType, at, std::move(cs));
151 }
152 
MakeS32(int width,int height,SkAlphaType at)153 SkImageInfo SkImageInfo::MakeS32(int width, int height, SkAlphaType at) {
154     return SkImageInfo({width, height}, {kN32_SkColorType, at, SkColorSpace::MakeSRGB()});
155 }
156 
MakeN32Premul(int width,int height)157 SkImageInfo SkImageInfo::MakeN32Premul(int width, int height) {
158     return MakeN32Premul(width, height, nullptr);
159 }
160 
MakeN32Premul(int width,int height,sk_sp<SkColorSpace> cs)161 SkImageInfo SkImageInfo::MakeN32Premul(int width, int height, sk_sp<SkColorSpace> cs) {
162     return Make({width, height}, kN32_SkColorType, kPremul_SkAlphaType, std::move(cs));
163 }
164 
MakeN32Premul(SkISize dimensions)165 SkImageInfo SkImageInfo::MakeN32Premul(SkISize dimensions) {
166     return MakeN32Premul(dimensions, nullptr);
167 }
168 
MakeN32Premul(SkISize dimensions,sk_sp<SkColorSpace> cs)169 SkImageInfo SkImageInfo::MakeN32Premul(SkISize dimensions, sk_sp<SkColorSpace> cs) {
170     return Make(dimensions, kN32_SkColorType, kPremul_SkAlphaType, std::move(cs));
171 }
172 
MakeA8(int width,int height)173 SkImageInfo SkImageInfo::MakeA8(int width, int height) {
174     return Make({width, height}, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr);
175 }
176 
MakeA8(SkISize dimensions)177 SkImageInfo SkImageInfo::MakeA8(SkISize dimensions) {
178     return Make(dimensions, kAlpha_8_SkColorType, kPremul_SkAlphaType, nullptr);
179 }
180 
MakeUnknown(int width,int height)181 SkImageInfo SkImageInfo::MakeUnknown(int width, int height) {
182     return Make({width, height}, kUnknown_SkColorType, kUnknown_SkAlphaType, nullptr);
183 }
184 
185 #ifdef SK_DEBUG
validate() const186 void SkImageInfo::validate() const {
187     SkASSERT(fDimensions.width() >= 0);
188     SkASSERT(fDimensions.height() >= 0);
189     SkASSERT(SkColorTypeIsValid(this->colorType()));
190     SkASSERT(SkAlphaTypeIsValid(this->alphaType()));
191 }
192 #endif
193 
SkColorTypeValidateAlphaType(SkColorType colorType,SkAlphaType alphaType,SkAlphaType * canonical)194 bool SkColorTypeValidateAlphaType(SkColorType colorType, SkAlphaType alphaType,
195                                   SkAlphaType* canonical) {
196     switch (colorType) {
197         case kUnknown_SkColorType:
198             alphaType = kUnknown_SkAlphaType;
199             break;
200         case kAlpha_8_SkColorType:         // fall-through
201         case kA16_unorm_SkColorType:       // fall-through
202         case kA16_float_SkColorType:
203             if (kUnpremul_SkAlphaType == alphaType) {
204                 alphaType = kPremul_SkAlphaType;
205             }
206             [[fallthrough]];
207         case kARGB_4444_SkColorType:
208         case kRGBA_8888_SkColorType:
209         case kSRGBA_8888_SkColorType:
210         case kBGRA_8888_SkColorType:
211         case kRGBA_1010102_SkColorType:
212         case kBGRA_1010102_SkColorType:
213         case kRGBA_10x6_SkColorType:
214         case kRGBA_F16Norm_SkColorType:
215         case kRGBA_F16_SkColorType:
216         case kRGBA_F32_SkColorType:
217         case kBGRA_10101010_XR_SkColorType:
218         case kR16G16B16A16_unorm_SkColorType:
219             if (kUnknown_SkAlphaType == alphaType) {
220                 return false;
221             }
222             break;
223         case kGray_8_SkColorType:
224         case kR8G8_unorm_SkColorType:
225         case kR16G16_unorm_SkColorType:
226         case kR16G16_float_SkColorType:
227         case kRGB_565_SkColorType:
228         case kRGB_888x_SkColorType:
229         case kRGB_101010x_SkColorType:
230         case kBGR_101010x_SkColorType:
231         case kBGR_101010x_XR_SkColorType:
232         case kR8_unorm_SkColorType:
233             alphaType = kOpaque_SkAlphaType;
234             break;
235     }
236     if (canonical) {
237         *canonical = alphaType;
238     }
239     return true;
240 }
241