blob: 0328a1f72bb2680fa96a182fcab0e77cb472e8c8 [file] [log] [blame]
msarett3d9d7a72015-10-21 10:27:10 -07001/*
2 * Copyright 2015 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
msarett3d9d7a72015-10-21 10:27:10 -070014
15static 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;
18}
19
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050020/**
21 * Loads the gamut as a set of three points (triangle).
22 */
Leon Scroggins III36f7e322018-08-27 11:55:46 -040023static void load_gamut(SkPoint rgb[], const skcms_Matrix3x3& xyz) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050024 // rx = rX / (rX + rY + rZ)
25 // ry = rY / (rX + rY + rZ)
26 // gx, gy, bx, and gy are calulcated similarly.
nagarajan.n6e17b6f2017-07-10 15:02:00 +053027 for (int rgbIdx = 0; rgbIdx < 3; rgbIdx++) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -040028 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;
nagarajan.n6e17b6f2017-07-10 15:02:00 +053031 }
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050032}
33
34/**
35 * Calculates the area of the triangular gamut.
36 */
37static float calculate_area(SkPoint abc[]) {
38 SkPoint a = abc[0];
39 SkPoint b = abc[1];
40 SkPoint c = abc[2];
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);
42}
43
Leon Scroggins III862c1962017-10-02 16:28:49 -040044static constexpr float kSRGB_D50_GamutArea = 0.084f;
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050045
Leon Scroggins III36f7e322018-08-27 11:55:46 -040046static bool is_wide_gamut(const skcms_ICCProfile& profile) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050047 // 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.
Leon Scroggins III36f7e322018-08-27 11:55:46 -040049 if (profile.has_toXYZD50) {
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050050 SkPoint rgb[3];
Leon Scroggins III36f7e322018-08-27 11:55:46 -040051 load_gamut(rgb, profile.toXYZD50);
Matt Sarett2ae3a2e2017-02-15 08:48:02 -050052 return calculate_area(rgb) > kSRGB_D50_GamutArea;
53 }
54
55 return false;
56}
57
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -050058SkAndroidCodec::SkAndroidCodec(SkCodec* codec)
59 : fInfo(codec->getInfo())
msarett90c4d5f2015-12-10 13:09:24 -080060 , fCodec(codec)
msarett3d9d7a72015-10-21 10:27:10 -070061{}
62
Leon Scroggins III07418182017-08-15 12:24:02 -040063SkAndroidCodec::~SkAndroidCodec() {}
64
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050065std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
66 SkPngChunkReader* chunkReader) {
Mike Reedede7bac2017-07-23 15:30:02 -040067 auto codec = SkCodec::MakeFromStream(std::move(stream), nullptr, chunkReader);
Leon Scroggins III7397d7a2018-01-04 13:26:30 -050068 return MakeFromCodec(std::move(codec));
69}
70
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -050071std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromCodec(std::unique_ptr<SkCodec> codec) {
msarett3d9d7a72015-10-21 10:27:10 -070072 if (nullptr == codec) {
73 return nullptr;
74 }
75
Hal Canarydb683012016-11-23 08:55:18 -070076 switch ((SkEncodedImageFormat)codec->getEncodedFormat()) {
Hal Canarydb683012016-11-23 08:55:18 -070077 case SkEncodedImageFormat::kPNG:
78 case SkEncodedImageFormat::kICO:
Hal Canarydb683012016-11-23 08:55:18 -070079 case SkEncodedImageFormat::kJPEG:
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040080#ifndef SK_HAS_WUFFS_LIBRARY
Hal Canarydb683012016-11-23 08:55:18 -070081 case SkEncodedImageFormat::kGIF:
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040082#endif
Hal Canarydb683012016-11-23 08:55:18 -070083 case SkEncodedImageFormat::kBMP:
84 case SkEncodedImageFormat::kWBMP:
Leon Scroggins III04be2b52017-08-17 15:13:20 -040085 case SkEncodedImageFormat::kHEIF:
Vignesh Venkatasubramanianeb7f9602020-11-04 14:13:39 -080086 case SkEncodedImageFormat::kAVIF:
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -050087 return std::make_unique<SkSampledCodec>(codec.release());
Leon Scroggins III70d8f4f2019-04-01 12:57:46 -040088#ifdef SK_HAS_WUFFS_LIBRARY
89 case SkEncodedImageFormat::kGIF:
90#endif
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040091#ifdef SK_CODEC_DECODES_WEBP
Hal Canarydb683012016-11-23 08:55:18 -070092 case SkEncodedImageFormat::kWEBP:
Mike Klein0af99292019-05-15 22:02:05 +000093#endif
94#ifdef SK_CODEC_DECODES_RAW
Hal Canarydb683012016-11-23 08:55:18 -070095 case SkEncodedImageFormat::kDNG:
Mike Klein0af99292019-05-15 22:02:05 +000096#endif
Leon Scroggins IIIa77f30c2020-03-09 14:23:30 -040097#if defined(SK_CODEC_DECODES_WEBP) || defined(SK_CODEC_DECODES_RAW) || defined(SK_HAS_WUFFS_LIBRARY)
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -050098 return std::make_unique<SkAndroidCodecAdapter>(codec.release());
Mike Klein0af99292019-05-15 22:02:05 +000099#endif
Nigel Tao612a65d2018-11-09 10:10:31 +1100100
msarett3d9d7a72015-10-21 10:27:10 -0700101 default:
msarett3d9d7a72015-10-21 10:27:10 -0700102 return nullptr;
103 }
104}
105
Mike Reedede7bac2017-07-23 15:30:02 -0400106std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data,
107 SkPngChunkReader* chunkReader) {
msarett3d9d7a72015-10-21 10:27:10 -0700108 if (!data) {
109 return nullptr;
110 }
111
Mike Reed847068c2017-07-26 11:35:53 -0400112 return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader);
msarett3d9d7a72015-10-21 10:27:10 -0700113}
114
msarett9a0e3462015-12-11 07:38:50 -0800115SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
Leon Scroggins1793e7b2017-12-05 15:38:14 +0000116 bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8;
msarett9a0e3462015-12-11 07:38:50 -0800117 switch (requestedColorType) {
118 case kARGB_4444_SkColorType:
msarett9a0e3462015-12-11 07:38:50 -0800119 return kN32_SkColorType;
Matt Sarett8dcc84f2016-12-14 10:23:41 -0500120 case kN32_SkColorType:
msarett9a0e3462015-12-11 07:38:50 -0800121 break;
122 case kAlpha_8_SkColorType:
123 // Fall through to kGray_8. Before kGray_8_SkColorType existed,
124 // we allowed clients to request kAlpha_8 when they wanted a
125 // grayscale decode.
126 case kGray_8_SkColorType:
Matt Sarett8db74f12017-06-14 13:02:05 +0000127 if (kGray_8_SkColorType == this->getInfo().colorType()) {
msarett9a0e3462015-12-11 07:38:50 -0800128 return kGray_8_SkColorType;
129 }
130 break;
131 case kRGB_565_SkColorType:
132 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
133 return kRGB_565_SkColorType;
134 }
135 break;
Matt Sarett8dcc84f2016-12-14 10:23:41 -0500136 case kRGBA_F16_SkColorType:
137 return kRGBA_F16_SkColorType;
msarett9a0e3462015-12-11 07:38:50 -0800138 default:
139 break;
140 }
141
Leon Scroggins1793e7b2017-12-05 15:38:14 +0000142 // F16 is the Android default for high precision images.
143 return highPrecision ? kRGBA_F16_SkColorType : kN32_SkColorType;
msarett9a0e3462015-12-11 07:38:50 -0800144}
145
146SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) {
147 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
148 return kOpaque_SkAlphaType;
149 }
150 return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
151}
152
Matt Sarett68feef42017-04-11 09:51:32 -0400153sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType,
154 sk_sp<SkColorSpace> prefColorSpace) {
Matt Sarett966bb342016-12-12 16:30:13 -0500155 switch (outputColorType) {
Derek Sollenberger1be431f2019-01-30 11:21:33 -0500156 case kRGBA_F16_SkColorType:
157 case kRGB_565_SkColorType:
Leon Scroggins IIIc20b5f82017-07-13 08:05:29 -0400158 case kRGBA_8888_SkColorType:
159 case kBGRA_8888_SkColorType: {
Brian Osman82ebe042019-01-04 17:03:00 -0500160 // If |prefColorSpace| is supplied, choose it.
161 if (prefColorSpace) {
Matt Sarett68feef42017-04-11 09:51:32 -0400162 return prefColorSpace;
163 }
164
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400165 const skcms_ICCProfile* encodedProfile = fCodec->getEncodedInfo().profile();
Leon Scroggins III227d4e12018-09-27 08:30:22 -0400166 if (encodedProfile) {
167 if (auto encodedSpace = SkColorSpace::Make(*encodedProfile)) {
168 // Leave the pixels in the encoded color space. Color space conversion
169 // will be handled after decode time.
170 return encodedSpace;
171 }
Matt Sarett9341c982017-02-28 15:36:42 -0500172
Leon Scroggins III227d4e12018-09-27 08:30:22 -0400173 if (is_wide_gamut(*encodedProfile)) {
Mike Kleinb147ace2020-01-16 11:11:06 -0600174 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3);
Leon Scroggins III227d4e12018-09-27 08:30:22 -0400175 }
Matt Sarett2ae3a2e2017-02-15 08:48:02 -0500176 }
177
Matt Sarettcf3f2342017-03-23 15:32:25 -0400178 return SkColorSpace::MakeSRGB();
Matt Sarett9341c982017-02-28 15:36:42 -0500179 }
Matt Sarett966bb342016-12-12 16:30:13 -0500180 default:
Matt Sarett3725f0a2017-03-28 14:34:20 -0400181 // Color correction not supported for kGray.
Matt Sarett966bb342016-12-12 16:30:13 -0500182 return nullptr;
183 }
184}
185
Leon Scroggins III07a722c2018-01-16 15:01:17 -0500186static bool supports_any_down_scale(const SkCodec* codec) {
187 return codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP;
188}
189
190// There are a variety of ways two SkISizes could be compared. This method
191// returns true if either dimensions of a is < that of b.
192// computeSampleSize also uses the opposite, which means that both
193// dimensions of a >= b.
194static inline bool smaller_than(const SkISize& a, const SkISize& b) {
195 return a.width() < b.width() || a.height() < b.height();
196}
197
198// Both dimensions of a > that of b.
199static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) {
200 return a.width() > b.width() && a.height() > b.height();
201}
202
203int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const {
204 SkASSERT(desiredSize);
205
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500206 const auto origDims = fCodec->dimensions();
207 if (!desiredSize || *desiredSize == origDims) {
Leon Scroggins III07a722c2018-01-16 15:01:17 -0500208 return 1;
209 }
210
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500211 if (smaller_than(origDims, *desiredSize)) {
212 *desiredSize = origDims;
Leon Scroggins III07a722c2018-01-16 15:01:17 -0500213 return 1;
214 }
215
216 // Handle bad input:
217 if (desiredSize->width() < 1 || desiredSize->height() < 1) {
218 *desiredSize = SkISize::Make(std::max(1, desiredSize->width()),
219 std::max(1, desiredSize->height()));
220 }
221
222 if (supports_any_down_scale(fCodec.get())) {
223 return 1;
224 }
225
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500226 int sampleX = origDims.width() / desiredSize->width();
227 int sampleY = origDims.height() / desiredSize->height();
Leon Scroggins III07a722c2018-01-16 15:01:17 -0500228 int sampleSize = std::min(sampleX, sampleY);
229 auto computedSize = this->getSampledDimensions(sampleSize);
230 if (computedSize == *desiredSize) {
231 return sampleSize;
232 }
233
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500234 if (computedSize == origDims || sampleSize == 1) {
Leon Scroggins III07a722c2018-01-16 15:01:17 -0500235 // Cannot downscale
236 *desiredSize = computedSize;
237 return 1;
238 }
239
240 if (strictly_bigger_than(computedSize, *desiredSize)) {
241 // See if there is a tighter fit.
242 while (true) {
243 auto smaller = this->getSampledDimensions(sampleSize + 1);
244 if (smaller == *desiredSize) {
245 return sampleSize + 1;
246 }
247 if (smaller == computedSize || smaller_than(smaller, *desiredSize)) {
248 // Cannot get any smaller without being smaller than desired.
249 *desiredSize = computedSize;
250 return sampleSize;
251 }
252
253 sampleSize++;
254 computedSize = smaller;
255 }
256
257 SkASSERT(false);
258 }
259
260 if (!smaller_than(computedSize, *desiredSize)) {
261 // This means one of the computed dimensions is equal to desired, and
262 // the other is bigger. This is as close as we can get.
263 *desiredSize = computedSize;
264 return sampleSize;
265 }
266
267 // computedSize is too small. Make it larger.
268 while (sampleSize > 2) {
269 auto bigger = this->getSampledDimensions(sampleSize - 1);
270 if (bigger == *desiredSize || !smaller_than(bigger, *desiredSize)) {
271 *desiredSize = bigger;
272 return sampleSize - 1;
273 }
274 sampleSize--;
275 }
276
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500277 *desiredSize = origDims;
Leon Scroggins III07a722c2018-01-16 15:01:17 -0500278 return 1;
279}
280
msarett3d9d7a72015-10-21 10:27:10 -0700281SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const {
282 if (!is_valid_sample_size(sampleSize)) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400283 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700284 }
285
scroggo501b7342015-11-03 07:55:11 -0800286 // Fast path for when we are not scaling.
287 if (1 == sampleSize) {
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500288 return fCodec->dimensions();
scroggo501b7342015-11-03 07:55:11 -0800289 }
290
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500291 return this->onGetSampledDimensions(sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700292}
293
294bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const {
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500295 if (!desiredSubset || !is_valid_subset(*desiredSubset, fCodec->dimensions())) {
msarett3d9d7a72015-10-21 10:27:10 -0700296 return false;
297 }
298
299 return this->onGetSupportedSubset(desiredSubset);
300}
301
302SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const {
303 if (!is_valid_sample_size(sampleSize)) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400304 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700305 }
306
307 // We require that the input subset is a subset that is supported by SkAndroidCodec.
308 // We test this by calling getSupportedSubset() and verifying that no modifications
309 // are made to the subset.
310 SkIRect copySubset = subset;
311 if (!this->getSupportedSubset(&copySubset) || copySubset != subset) {
Hal Canaryfafe1352017-04-11 12:12:02 -0400312 return {0, 0};
msarett3d9d7a72015-10-21 10:27:10 -0700313 }
314
scroggo501b7342015-11-03 07:55:11 -0800315 // If the subset is the entire image, for consistency, use getSampledDimensions().
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500316 if (fCodec->dimensions() == subset.size()) {
scroggo501b7342015-11-03 07:55:11 -0800317 return this->getSampledDimensions(sampleSize);
msarett3d9d7a72015-10-21 10:27:10 -0700318 }
319
320 // This should perhaps call a virtual function, but currently both of our subclasses
321 // want the same implementation.
Hal Canaryfafe1352017-04-11 12:12:02 -0400322 return {get_scaled_dimension(subset.width(), sampleSize),
323 get_scaled_dimension(subset.height(), sampleSize)};
msarett3d9d7a72015-10-21 10:27:10 -0700324}
325
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500326SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo,
327 void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) {
328 if (!requestPixels) {
msarett3d9d7a72015-10-21 10:27:10 -0700329 return SkCodec::kInvalidParameters;
330 }
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500331 if (requestRowBytes < requestInfo.minRowBytes()) {
msarett3d9d7a72015-10-21 10:27:10 -0700332 return SkCodec::kInvalidParameters;
333 }
334
335 AndroidOptions defaultOptions;
336 if (!options) {
337 options = &defaultOptions;
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500338 } else {
339 if (options->fSubset) {
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500340 if (!is_valid_subset(*options->fSubset, fCodec->dimensions())) {
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500341 return SkCodec::kInvalidParameters;
342 }
343
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500344 if (SkIRect::MakeSize(fCodec->dimensions()) == *options->fSubset) {
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500345 // The caller wants the whole thing, rather than a subset. Modify
346 // the AndroidOptions passed to onGetAndroidPixels to not specify
347 // a subset.
348 defaultOptions = *options;
349 defaultOptions.fSubset = nullptr;
350 options = &defaultOptions;
351 }
scroggo501b7342015-11-03 07:55:11 -0800352 }
Leon Scrogginsfc4fdc52020-11-09 14:18:12 -0500353 }
354
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500355 if (auto result = fCodec->handleFrameIndex(requestInfo, requestPixels, requestRowBytes,
356 *options, this); result != SkCodec::kSuccess) {
357 return result;
358 }
359
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500360 return this->onGetAndroidPixels(requestInfo, requestPixels, requestRowBytes, *options);
msarett3d9d7a72015-10-21 10:27:10 -0700361}
362
363SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
364 size_t rowBytes) {
365 return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
366}