2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/codec/SkAndroidCodec.h"
9 #include "include/codec/SkCodec.h"
10 #include "include/core/SkPixmap.h"
11 #include "src/codec/SkAndroidCodecAdapter.h"
12 #include "src/codec/SkCodecPriv.h"
13 #include "src/codec/SkSampledCodec.h"
15 static bool is_valid_sample_size(int sampleSize) {
16 // FIXME: As Leon has mentioned elsewhere, surely there is also a maximum sampleSize?
17 return sampleSize > 0;
21 * Loads the gamut as a set of three points (triangle).
23 static void load_gamut(SkPoint rgb[], const skcms_Matrix3x3& xyz) {
24 // rx = rX / (rX + rY + rZ)
25 // ry = rY / (rX + rY + rZ)
26 // gx, gy, bx, and gy are calulcated similarly.
27 for (int rgbIdx = 0; rgbIdx < 3; rgbIdx++) {
28 float sum = xyz.vals[rgbIdx][0] + xyz.vals[rgbIdx][1] + xyz.vals[rgbIdx][2];
29 rgb[rgbIdx].fX = xyz.vals[rgbIdx][0] / sum;
30 rgb[rgbIdx].fY = xyz.vals[rgbIdx][1] / sum;
35 * Calculates the area of the triangular gamut.
37 static float calculate_area(SkPoint abc[]) {
41 return 0.5f * SkTAbs(a.fX*b.fY + b.fX*c.fY - a.fX*c.fY - c.fX*b.fY - b.fX*a.fY);
44 static constexpr float kSRGB_D50_GamutArea = 0.084f;
46 static bool is_wide_gamut(const skcms_ICCProfile& profile) {
47 // Determine if the source image has a gamut that is wider than sRGB. If so, we
48 // will use P3 as the output color space to avoid clipping the gamut.
49 if (profile.has_toXYZD50) {
51 load_gamut(rgb, profile.toXYZD50);
52 return calculate_area(rgb) > kSRGB_D50_GamutArea;
58 SkAndroidCodec::SkAndroidCodec(SkCodec* codec)
59 : fInfo(codec->getInfo())
63 SkAndroidCodec::~SkAndroidCodec() {}
65 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
66 SkPngChunkReader* chunkReader) {
67 auto codec = SkCodec::MakeFromStream(std::move(stream), nullptr, chunkReader);
68 return MakeFromCodec(std::move(codec));
71 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromCodec(std::unique_ptr<SkCodec> codec) {
72 if (nullptr == codec) {
76 switch ((SkEncodedImageFormat)codec->getEncodedFormat()) {
77 case SkEncodedImageFormat::kPNG:
78 case SkEncodedImageFormat::kICO:
79 case SkEncodedImageFormat::kJPEG:
80 #ifndef SK_HAS_WUFFS_LIBRARY
81 case SkEncodedImageFormat::kGIF:
83 case SkEncodedImageFormat::kBMP:
84 case SkEncodedImageFormat::kWBMP:
85 case SkEncodedImageFormat::kHEIF:
86 case SkEncodedImageFormat::kAVIF:
87 return std::make_unique<SkSampledCodec>(codec.release());
88 #ifdef SK_HAS_WUFFS_LIBRARY
89 case SkEncodedImageFormat::kGIF:
91 #ifdef SK_CODEC_DECODES_WEBP
92 case SkEncodedImageFormat::kWEBP:
94 #ifdef SK_CODEC_DECODES_RAW
95 case SkEncodedImageFormat::kDNG:
97 #if defined(SK_CODEC_DECODES_WEBP) || defined(SK_CODEC_DECODES_RAW) || defined(SK_HAS_WUFFS_LIBRARY)
98 return std::make_unique<SkAndroidCodecAdapter>(codec.release());
106 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data,
107 SkPngChunkReader* chunkReader) {
112 return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader);
115 SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
116 bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8;
117 uint8_t colorDepth = fCodec->getEncodedInfo().getColorDepth();
118 switch (requestedColorType) {
119 case kARGB_4444_SkColorType:
120 return kN32_SkColorType;
121 case kN32_SkColorType:
123 case kAlpha_8_SkColorType:
124 // Fall through to kGray_8. Before kGray_8_SkColorType existed,
125 // we allowed clients to request kAlpha_8 when they wanted a
127 case kGray_8_SkColorType:
128 if (kGray_8_SkColorType == this->getInfo().colorType()) {
129 return kGray_8_SkColorType;
132 case kRGB_565_SkColorType:
133 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
134 return kRGB_565_SkColorType;
137 case kRGBA_1010102_SkColorType:
138 if (colorDepth == 10) {
139 return kRGBA_1010102_SkColorType;
142 case kRGBA_F16_SkColorType:
143 return kRGBA_F16_SkColorType;
148 // F16 is the Android default for high precision images.
149 return highPrecision ? kRGBA_F16_SkColorType :
150 (colorDepth == 10 ? kRGBA_1010102_SkColorType : kN32_SkColorType);
153 SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) {
154 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
155 return kOpaque_SkAlphaType;
157 return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
160 sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType,
161 sk_sp<SkColorSpace> prefColorSpace) {
162 switch (outputColorType) {
163 case kRGBA_F16_SkColorType:
164 case kRGB_565_SkColorType:
165 case kRGBA_8888_SkColorType:
166 case kBGRA_8888_SkColorType:
167 case kRGBA_1010102_SkColorType: {
168 // If |prefColorSpace| is supplied, choose it.
169 if (prefColorSpace) {
170 return prefColorSpace;
173 const skcms_ICCProfile* encodedProfile = fCodec->getEncodedInfo().profile();
174 if (encodedProfile) {
175 if (auto encodedSpace = SkColorSpace::Make(*encodedProfile)) {
176 // Leave the pixels in the encoded color space. Color space conversion
177 // will be handled after decode time.
181 if (is_wide_gamut(*encodedProfile)) {
182 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
186 return SkColorSpace::MakeSRGB();
189 // Color correction not supported for kGray.
194 static bool supports_any_down_scale(const SkCodec* codec) {
195 return codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP;
198 // There are a variety of ways two SkISizes could be compared. This method
199 // returns true if either dimensions of a is < that of b.
200 // computeSampleSize also uses the opposite, which means that both
201 // dimensions of a >= b.
202 static inline bool smaller_than(const SkISize& a, const SkISize& b) {
203 return a.width() < b.width() || a.height() < b.height();
206 // Both dimensions of a > that of b.
207 static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) {
208 return a.width() > b.width() && a.height() > b.height();
211 int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const {
212 SkASSERT(desiredSize);
214 const auto origDims = fCodec->dimensions();
215 if (!desiredSize || *desiredSize == origDims) {
219 if (smaller_than(origDims, *desiredSize)) {
220 *desiredSize = origDims;
225 if (desiredSize->width() < 1 || desiredSize->height() < 1) {
226 *desiredSize = SkISize::Make(std::max(1, desiredSize->width()),
227 std::max(1, desiredSize->height()));
230 if (supports_any_down_scale(fCodec.get())) {
234 int sampleX = origDims.width() / desiredSize->width();
235 int sampleY = origDims.height() / desiredSize->height();
236 int sampleSize = std::min(sampleX, sampleY);
237 auto computedSize = this->getSampledDimensions(sampleSize);
238 if (computedSize == *desiredSize) {
242 if (computedSize == origDims || sampleSize == 1) {
244 *desiredSize = computedSize;
248 if (strictly_bigger_than(computedSize, *desiredSize)) {
249 // See if there is a tighter fit.
251 auto smaller = this->getSampledDimensions(sampleSize + 1);
252 if (smaller == *desiredSize) {
253 return sampleSize + 1;
255 if (smaller == computedSize || smaller_than(smaller, *desiredSize)) {
256 // Cannot get any smaller without being smaller than desired.
257 *desiredSize = computedSize;
262 computedSize = smaller;
268 if (!smaller_than(computedSize, *desiredSize)) {
269 // This means one of the computed dimensions is equal to desired, and
270 // the other is bigger. This is as close as we can get.
271 *desiredSize = computedSize;
275 // computedSize is too small. Make it larger.
276 while (sampleSize > 2) {
277 auto bigger = this->getSampledDimensions(sampleSize - 1);
278 if (bigger == *desiredSize || !smaller_than(bigger, *desiredSize)) {
279 *desiredSize = bigger;
280 return sampleSize - 1;
285 *desiredSize = origDims;
289 SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const {
290 if (!is_valid_sample_size(sampleSize)) {
294 // Fast path for when we are not scaling.
295 if (1 == sampleSize) {
296 return fCodec->dimensions();
299 return this->onGetSampledDimensions(sampleSize);
302 bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const {
303 if (!desiredSubset || !is_valid_subset(*desiredSubset, fCodec->dimensions())) {
307 return this->onGetSupportedSubset(desiredSubset);
310 SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const {
311 if (!is_valid_sample_size(sampleSize)) {
315 // We require that the input subset is a subset that is supported by SkAndroidCodec.
316 // We test this by calling getSupportedSubset() and verifying that no modifications
317 // are made to the subset.
318 SkIRect copySubset = subset;
319 if (!this->getSupportedSubset(©Subset) || copySubset != subset) {
323 // If the subset is the entire image, for consistency, use getSampledDimensions().
324 if (fCodec->dimensions() == subset.size()) {
325 return this->getSampledDimensions(sampleSize);
328 // This should perhaps call a virtual function, but currently both of our subclasses
329 // want the same implementation.
330 return {get_scaled_dimension(subset.width(), sampleSize),
331 get_scaled_dimension(subset.height(), sampleSize)};
334 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo,
335 void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) {
336 if (!requestPixels) {
337 return SkCodec::kInvalidParameters;
339 if (requestRowBytes < requestInfo.minRowBytes()) {
340 return SkCodec::kInvalidParameters;
343 AndroidOptions defaultOptions;
345 options = &defaultOptions;
347 if (options->fSubset) {
348 if (!is_valid_subset(*options->fSubset, fCodec->dimensions())) {
349 return SkCodec::kInvalidParameters;
352 if (SkIRect::MakeSize(fCodec->dimensions()) == *options->fSubset) {
353 // The caller wants the whole thing, rather than a subset. Modify
354 // the AndroidOptions passed to onGetAndroidPixels to not specify
356 defaultOptions = *options;
357 defaultOptions.fSubset = nullptr;
358 options = &defaultOptions;
363 if (auto result = fCodec->handleFrameIndex(requestInfo, requestPixels, requestRowBytes,
364 *options, this); result != SkCodec::kSuccess) {
368 return this->onGetAndroidPixels(requestInfo, requestPixels, requestRowBytes, *options);
371 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
373 return this->getAndroidPixels(info, pixels, rowBytes, nullptr);