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