1 /*
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
8 #include "include/codec/SkAndroidCodec.h"
9
10 #include "include/codec/SkCodec.h"
11 #include "include/codec/SkEncodedImageFormat.h"
12 #include "include/core/SkAlphaType.h"
13 #include "include/core/SkColorType.h"
14 #include "include/core/SkData.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkStream.h"
17 #include "modules/skcms/skcms.h"
18 #include "src/codec/SkAndroidCodecAdapter.h"
19 #include "src/codec/SkCodecPriv.h"
20 #include "src/codec/SkSampledCodec.h"
21
22 #include <algorithm>
23 #include <cstdint>
24 #include <functional>
25 #include <utility>
26
27 class SkPngChunkReader;
28
is_valid_sample_size(int sampleSize)29 static bool is_valid_sample_size(int sampleSize) {
30 // FIXME: As Leon has mentioned elsewhere, surely there is also a maximum sampleSize?
31 return sampleSize > 0;
32 }
33
cicp_get_primaries(uint8_t primaries,skcms_Matrix3x3 * sk_primaries)34 static bool cicp_get_primaries(uint8_t primaries, skcms_Matrix3x3* sk_primaries) {
35 // Rec. ITU-T H.273, Table 2.
36 switch (primaries) {
37 case 0:
38 // Reserved.
39 break;
40 case 1:
41 *sk_primaries = SkNamedGamut::kSRGB;
42 return true;
43 case 2:
44 // Unspecified.
45 break;
46 case 3:
47 // Reserved.
48 break;
49 case 4:
50 return skcms_PrimariesToXYZD50(
51 0.67f, 0.33f, 0.21f, 0.71f, 0.14f, 0.08f, 0.31f, 0.316f, sk_primaries);
52 case 5:
53 return skcms_PrimariesToXYZD50(
54 0.64f, 0.33f, 0.29f, 0.60f, 0.15f, 0.06f, 0.3127f, 0.3290f, sk_primaries);
55 case 6:
56 return skcms_PrimariesToXYZD50(
57 0.630f, 0.340f, 0.310f, 0.595f, 0.155f, 0.070f, 0.3127f, 0.3290f, sk_primaries);
58 case 7:
59 return skcms_PrimariesToXYZD50(
60 0.630f, 0.340f, 0.310f, 0.595f, 0.155f, 0.070f, 0.3127f, 0.3290f, sk_primaries);
61 case 8:
62 return skcms_PrimariesToXYZD50(
63 0.681f, 0.319f, 0.243f, 0.692f, 0.145f, 0.049f, 0.310f, 0.316f, sk_primaries);
64 case 9:
65 *sk_primaries = SkNamedGamut::kRec2020;
66 return true;
67 case 10:
68 return skcms_PrimariesToXYZD50(
69 1.f, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f / 3.f, 1.f / 3.f, sk_primaries);
70 case 11:
71 return skcms_PrimariesToXYZD50(
72 0.680f, 0.320f, 0.265f, 0.690f, 0.150f, 0.060f, 0.314f, 0.351f, sk_primaries);
73 case 12:
74 return skcms_PrimariesToXYZD50(
75 0.680f, 0.320f, 0.265f, 0.690f, 0.150f, 0.060f, 0.3127f, 0.3290f, sk_primaries);
76 case 22:
77 return skcms_PrimariesToXYZD50(
78 0.630f, 0.340f, 0.295f, 0.605f, 0.155f, 0.077f, 0.3127f, 0.3290f, sk_primaries);
79 default:
80 // Reserved.
81 break;
82 }
83 *sk_primaries = SkNamedGamut::kSRGB;
84 return false;
85 }
86
cicp_get_transfer_fn(uint8_t transfer_characteristics,skcms_TransferFunction * trfn)87 static bool cicp_get_transfer_fn(uint8_t transfer_characteristics, skcms_TransferFunction* trfn) {
88 // Rec. ITU-T H.273, Table 3.
89 switch (transfer_characteristics) {
90 case 0:
91 // Reserved.
92 break;
93 case 1:
94 *trfn = SkNamedTransferFn::kRec2020;
95 return true;
96 case 2:
97 // Unspecified.
98 break;
99 case 3:
100 // Reserved.
101 break;
102 case 4:
103 *trfn = {2.2f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
104 return true;
105 case 5:
106 *trfn = {2.8f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
107 return true;
108 case 6:
109 *trfn = SkNamedTransferFn::kRec2020;
110 return true;
111 case 7:
112 *trfn = {2.222222222222f,
113 0.899626676224f,
114 0.100373323776f,
115 0.25f,
116 0.091286342118f,
117 0.f,
118 0.f};
119 return true;
120 case 8:
121 *trfn = SkNamedTransferFn::kLinear;
122 return true;
123 case 9:
124 // Logarithmic transfer characteristic (100:1 range).
125 // Not supported by skcms
126 break;
127 case 10:
128 // Logarithmic transfer characteristic (100 * Sqrt( 10 ) : 1 range).
129 // Not supported by skcms
130 break;
131 case 11:
132 *trfn = SkNamedTransferFn::kSRGB;
133 break;
134 case 12:
135 // Rec. ITU-R BT.1361-0 extended colour gamut system (historical).
136 // Same as kRec709 on positive values, differs on negative values.
137 // Not supported by skcms
138 break;
139 case 13:
140 *trfn = SkNamedTransferFn::kSRGB;
141 return true;
142 case 14:
143 *trfn = SkNamedTransferFn::kRec2020;
144 return true;
145 case 15:
146 *trfn = SkNamedTransferFn::kRec2020;
147 return true;
148 case 16:
149 // Android expects PQ to match 203 nits to SDR white
150 *trfn = {-2.f,
151 -1.55522297832f,
152 1.86045365631f,
153 32 / 2523.0f,
154 2413 / 128.0f,
155 -2392 / 128.0f,
156 8192 / 1305.0f};
157 return true;
158 case 17:
159 *trfn = {2.6f, 1.034080527699f, 0.f, 0.f, 0.f, 0.f, 0.f};
160 return true;
161 case 18:
162 // Android expects HLG to match 203 nits to SDR white
163 if (skcms_TransferFunction_makeScaledHLGish(trfn,
164 0.314509843f,
165 2.f,
166 2.f,
167 1.f / 0.17883277f,
168 0.28466892f,
169 0.55991073f)) {
170 return true;
171 }
172 break;
173 default:
174 // 19-255 Reserved.
175 break;
176 }
177
178 *trfn = SkNamedTransferFn::kSRGB;
179 return false;
180 }
181
cicp_get_sk_color_space(uint8_t color_primaries,uint8_t transfer_characteristics,uint8_t matrix_coefficients,uint8_t full_range_flag)182 static sk_sp<SkColorSpace> cicp_get_sk_color_space(uint8_t color_primaries,
183 uint8_t transfer_characteristics,
184 uint8_t matrix_coefficients,
185 uint8_t full_range_flag) {
186 if (matrix_coefficients != 0) return nullptr;
187
188 if (full_range_flag != 1) return nullptr;
189
190 skcms_TransferFunction trfn;
191 if (!cicp_get_transfer_fn(transfer_characteristics, &trfn)) return nullptr;
192
193 skcms_Matrix3x3 primaries;
194 if (!cicp_get_primaries(color_primaries, &primaries)) return nullptr;
195
196 return SkColorSpace::MakeRGB(trfn, primaries);
197 }
198
SkAndroidCodec(SkCodec * codec)199 SkAndroidCodec::SkAndroidCodec(SkCodec* codec)
200 : fInfo(codec->getInfo())
201 , fCodec(codec)
202 {}
203
~SkAndroidCodec()204 SkAndroidCodec::~SkAndroidCodec() {}
205
MakeFromStream(std::unique_ptr<SkStream> stream,SkPngChunkReader * chunkReader)206 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
207 SkPngChunkReader* chunkReader) {
208 auto codec = SkCodec::MakeFromStream(std::move(stream), nullptr, chunkReader);
209 return MakeFromCodec(std::move(codec));
210 }
211
MakeFromCodec(std::unique_ptr<SkCodec> codec)212 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromCodec(std::unique_ptr<SkCodec> codec) {
213 if (nullptr == codec) {
214 return nullptr;
215 }
216
217 switch (codec->getEncodedFormat()) {
218 case SkEncodedImageFormat::kPNG:
219 case SkEncodedImageFormat::kICO:
220 case SkEncodedImageFormat::kJPEG:
221 case SkEncodedImageFormat::kBMP:
222 case SkEncodedImageFormat::kWBMP:
223 return std::make_unique<SkSampledCodec>(codec.release());
224 case SkEncodedImageFormat::kGIF:
225 case SkEncodedImageFormat::kWEBP:
226 case SkEncodedImageFormat::kDNG:
227 // On the Android framework, both HEIF and AVIF are handled by
228 // SkCrabbyAvifCodec. It can handle scaling internally. So we can use
229 // SkAndroidCodecAdapter for both these formats.
230 case SkEncodedImageFormat::kAVIF:
231 case SkEncodedImageFormat::kHEIF:
232 return std::make_unique<SkAndroidCodecAdapter>(codec.release());
233 case SkEncodedImageFormat::kPKM:
234 case SkEncodedImageFormat::kKTX:
235 case SkEncodedImageFormat::kASTC:
236 case SkEncodedImageFormat::kJPEGXL:
237 return nullptr;
238 }
239 SkUNREACHABLE;
240 }
241
MakeFromData(sk_sp<SkData> data,SkPngChunkReader * chunkReader)242 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data,
243 SkPngChunkReader* chunkReader) {
244 if (!data) {
245 return nullptr;
246 }
247
248 return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader);
249 }
250
computeOutputColorType(SkColorType requestedColorType)251 SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
252 bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8;
253 uint8_t colorDepth = fCodec->getEncodedInfo().getColorDepth();
254 switch (requestedColorType) {
255 case kARGB_4444_SkColorType:
256 return kN32_SkColorType;
257 case kN32_SkColorType:
258 break;
259 case kAlpha_8_SkColorType:
260 // Fall through to kGray_8. Before kGray_8_SkColorType existed,
261 // we allowed clients to request kAlpha_8 when they wanted a
262 // grayscale decode.
263 case kGray_8_SkColorType:
264 if (kGray_8_SkColorType == this->getInfo().colorType()) {
265 return kGray_8_SkColorType;
266 }
267 break;
268 case kRGB_565_SkColorType:
269 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
270 return kRGB_565_SkColorType;
271 }
272 break;
273 case kRGBA_1010102_SkColorType:
274 if (colorDepth == 10) {
275 return kRGBA_1010102_SkColorType;
276 }
277 break;
278 case kRGBA_F16_SkColorType:
279 return kRGBA_F16_SkColorType;
280 default:
281 break;
282 }
283
284 // F16 is the Android default for high precision images.
285 return highPrecision ? kRGBA_F16_SkColorType :
286 (colorDepth == 10 ? kRGBA_1010102_SkColorType : kN32_SkColorType);
287 }
288
computeOutputAlphaType(bool requestedUnpremul)289 SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) {
290 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
291 return kOpaque_SkAlphaType;
292 }
293 return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
294 }
295
computeOutputColorSpace(SkColorType outputColorType,sk_sp<SkColorSpace> prefColorSpace)296 sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType,
297 sk_sp<SkColorSpace> prefColorSpace) {
298 switch (outputColorType) {
299 case kRGBA_F16_SkColorType:
300 case kRGB_565_SkColorType:
301 case kRGBA_8888_SkColorType:
302 case kBGRA_8888_SkColorType:
303 case kRGBA_1010102_SkColorType: {
304 // If |prefColorSpace| is supplied, choose it.
305 if (prefColorSpace) {
306 return prefColorSpace;
307 }
308
309 const skcms_ICCProfile* encodedProfile = fCodec->getEncodedInfo().profile();
310 if (encodedProfile) {
311 // Prefer CICP information if it exists.
312 if (encodedProfile->has_CICP) {
313 const auto cicpColorSpace =
314 cicp_get_sk_color_space(encodedProfile->CICP.color_primaries,
315 encodedProfile->CICP.transfer_characteristics,
316 encodedProfile->CICP.matrix_coefficients,
317 encodedProfile->CICP.video_full_range_flag);
318 if (cicpColorSpace) {
319 return cicpColorSpace;
320 }
321 }
322 if (auto encodedSpace = SkColorSpace::Make(*encodedProfile)) {
323 // Leave the pixels in the encoded color space. Color space conversion
324 // will be handled after decode time.
325 return encodedSpace;
326 }
327
328 if (encodedProfile->has_toXYZD50) {
329 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
330 encodedProfile->toXYZD50);
331 }
332 }
333
334 return SkColorSpace::MakeSRGB();
335 }
336 default:
337 // Color correction not supported for kGray.
338 return nullptr;
339 }
340 }
341
is_webp(const SkCodec * codec)342 static bool is_webp(const SkCodec* codec) {
343 return codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP;
344 }
345
346 // There are a variety of ways two SkISizes could be compared. This method
347 // returns true if either dimensions of a is < that of b.
348 // computeSampleSize also uses the opposite, which means that both
349 // dimensions of a >= b.
smaller_than(const SkISize & a,const SkISize & b)350 static inline bool smaller_than(const SkISize& a, const SkISize& b) {
351 return a.width() < b.width() || a.height() < b.height();
352 }
353
354 // Both dimensions of a > that of b.
strictly_bigger_than(const SkISize & a,const SkISize & b)355 static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) {
356 return a.width() > b.width() && a.height() > b.height();
357 }
358
computeSampleSize(SkISize * desiredSize) const359 int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const {
360 SkASSERT(desiredSize);
361
362 const auto origDims = fCodec->dimensions();
363 if (!desiredSize || *desiredSize == origDims) {
364 return 1;
365 }
366
367 if (smaller_than(origDims, *desiredSize)) {
368 *desiredSize = origDims;
369 return 1;
370 }
371
372 // Handle bad input:
373 if (desiredSize->width() < 1 || desiredSize->height() < 1) {
374 *desiredSize = SkISize::Make(std::max(1, desiredSize->width()),
375 std::max(1, desiredSize->height()));
376 }
377
378 if (is_webp(fCodec.get())) {
379 if (fCodec->getFrameCount() > 1) {
380 // Cannot downscale animated webp
381 *desiredSize = origDims;
382 }
383 return 1;
384 }
385
386 int sampleX = origDims.width() / desiredSize->width();
387 int sampleY = origDims.height() / desiredSize->height();
388 int sampleSize = std::min(sampleX, sampleY);
389 auto computedSize = this->getSampledDimensions(sampleSize);
390 if (computedSize == *desiredSize) {
391 return sampleSize;
392 }
393
394 if (computedSize == origDims || sampleSize == 1) {
395 // Cannot downscale
396 *desiredSize = computedSize;
397 return 1;
398 }
399
400 if (strictly_bigger_than(computedSize, *desiredSize)) {
401 // See if there is a tighter fit.
402 while (true) {
403 auto smaller = this->getSampledDimensions(sampleSize + 1);
404 if (smaller == *desiredSize) {
405 return sampleSize + 1;
406 }
407 if (smaller == computedSize || smaller_than(smaller, *desiredSize)) {
408 // Cannot get any smaller without being smaller than desired.
409 *desiredSize = computedSize;
410 return sampleSize;
411 }
412
413 sampleSize++;
414 computedSize = smaller;
415 }
416
417 SkASSERT(false);
418 }
419
420 if (!smaller_than(computedSize, *desiredSize)) {
421 // This means one of the computed dimensions is equal to desired, and
422 // the other is bigger. This is as close as we can get.
423 *desiredSize = computedSize;
424 return sampleSize;
425 }
426
427 // computedSize is too small. Make it larger.
428 while (sampleSize > 2) {
429 auto bigger = this->getSampledDimensions(sampleSize - 1);
430 if (bigger == *desiredSize || !smaller_than(bigger, *desiredSize)) {
431 *desiredSize = bigger;
432 return sampleSize - 1;
433 }
434 sampleSize--;
435 }
436
437 *desiredSize = origDims;
438 return 1;
439 }
440
getSampledDimensions(int sampleSize) const441 SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const {
442 if (!is_valid_sample_size(sampleSize)) {
443 return {0, 0};
444 }
445
446 // Fast path for when we are not scaling.
447 if (1 == sampleSize) {
448 return fCodec->dimensions();
449 }
450
451 return this->onGetSampledDimensions(sampleSize);
452 }
453
getSupportedSubset(SkIRect * desiredSubset) const454 bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const {
455 if (!desiredSubset || !SkCodecPriv::IsValidSubset(*desiredSubset, fCodec->dimensions())) {
456 return false;
457 }
458
459 return this->onGetSupportedSubset(desiredSubset);
460 }
461
getSampledSubsetDimensions(int sampleSize,const SkIRect & subset) const462 SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const {
463 if (!is_valid_sample_size(sampleSize)) {
464 return {0, 0};
465 }
466
467 // We require that the input subset is a subset that is supported by SkAndroidCodec.
468 // We test this by calling getSupportedSubset() and verifying that no modifications
469 // are made to the subset.
470 SkIRect copySubset = subset;
471 if (!this->getSupportedSubset(©Subset) || copySubset != subset) {
472 return {0, 0};
473 }
474
475 // If the subset is the entire image, for consistency, use getSampledDimensions().
476 if (fCodec->dimensions() == subset.size()) {
477 return this->getSampledDimensions(sampleSize);
478 }
479
480 // This should perhaps call a virtual function, but currently both of our subclasses
481 // want the same implementation.
482 return {SkCodecPriv::GetSampledDimension(subset.width(), sampleSize),
483 SkCodecPriv::GetSampledDimension(subset.height(), sampleSize)};
484 }
485
getAndroidPixels(const SkImageInfo & requestInfo,void * requestPixels,size_t requestRowBytes,const AndroidOptions * options)486 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo,
487 void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) {
488 if (!requestPixels) {
489 return SkCodec::kInvalidParameters;
490 }
491 if (requestRowBytes < requestInfo.minRowBytes()) {
492 return SkCodec::kInvalidParameters;
493 }
494
495 AndroidOptions defaultOptions;
496 if (!options) {
497 options = &defaultOptions;
498 } else {
499 if (options->fSubset) {
500 if (!SkCodecPriv::IsValidSubset(*options->fSubset, fCodec->dimensions())) {
501 return SkCodec::kInvalidParameters;
502 }
503
504 if (SkIRect::MakeSize(fCodec->dimensions()) == *options->fSubset) {
505 // The caller wants the whole thing, rather than a subset. Modify
506 // the AndroidOptions passed to onGetAndroidPixels to not specify
507 // a subset.
508 defaultOptions = *options;
509 defaultOptions.fSubset = nullptr;
510 options = &defaultOptions;
511 }
512 }
513 }
514
515 // We may need to have handleFrameIndex recursively call this method
516 // to resolve one frame depending on another. The recursion stops
517 // when we find a frame which does not require an earlier frame
518 // e.g. frame->getRequiredFrame() returns kNoFrame
519 auto getPixelsFn = [&](const SkImageInfo& info, void* pixels, size_t rowBytes,
520 const SkCodec::Options& opts, int requiredFrame
521 ) -> SkCodec::Result {
522 SkAndroidCodec::AndroidOptions prevFrameOptions(
523 reinterpret_cast<const SkAndroidCodec::AndroidOptions&>(opts));
524 prevFrameOptions.fFrameIndex = requiredFrame;
525 return this->getAndroidPixels(info, pixels, rowBytes, &prevFrameOptions);
526 };
527 if (auto result = fCodec->handleFrameIndex(requestInfo, requestPixels, requestRowBytes,
528 *options, getPixelsFn); result != SkCodec::kSuccess) {
529 return result;
530 }
531
532 return this->onGetAndroidPixels(requestInfo, requestPixels, requestRowBytes, *options);
533 }
534
getAndroidPixels(const SkImageInfo & info,void * pixels,size_t rowBytes)535 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
536 size_t rowBytes) {
537 return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
538 }
539
getGainmapAndroidCodec(SkGainmapInfo * info,std::unique_ptr<SkAndroidCodec> * outCodec)540 bool SkAndroidCodec::getGainmapAndroidCodec(SkGainmapInfo* info,
541 std::unique_ptr<SkAndroidCodec>* outCodec) {
542 if (outCodec) {
543 std::unique_ptr<SkCodec> gainmapCodec;
544 if (!fCodec->onGetGainmapCodec(info, &gainmapCodec)) {
545 return false;
546 }
547 *outCodec = MakeFromCodec(std::move(gainmapCodec));
548 return true;
549 }
550 return fCodec->onGetGainmapCodec(info, nullptr);
551 }
552
getAndroidGainmap(SkGainmapInfo * info,std::unique_ptr<SkStream> * outGainmapImageStream)553 bool SkAndroidCodec::getAndroidGainmap(SkGainmapInfo* info,
554 std::unique_ptr<SkStream>* outGainmapImageStream) {
555 return fCodec->onGetGainmapInfo(info, outGainmapImageStream);
556 }
557