xref: /aosp_15_r20/external/skia/src/codec/SkAndroidCodec.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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     const SkEncodedImageFormat format = codec->getEncodedFormat();
218     if (format == SkEncodedImageFormat::kAVIF) {
219         if (SkCodecs::HasDecoder("avif")) {
220             // If a dedicated AVIF decoder has been registered, SkAvifCodec can
221             // handle scaling internally.
222             return std::make_unique<SkAndroidCodecAdapter>(codec.release());
223         }
224         // This will fallback to SkHeifCodec, which needs sampling.
225         return std::make_unique<SkSampledCodec>(codec.release());
226     }
227 
228     switch (format) {
229         case SkEncodedImageFormat::kPNG:
230         case SkEncodedImageFormat::kICO:
231         case SkEncodedImageFormat::kJPEG:
232         case SkEncodedImageFormat::kBMP:
233         case SkEncodedImageFormat::kWBMP:
234         case SkEncodedImageFormat::kHEIF:
235             return std::make_unique<SkSampledCodec>(codec.release());
236         case SkEncodedImageFormat::kGIF:
237         case SkEncodedImageFormat::kWEBP:
238         case SkEncodedImageFormat::kDNG:
239             return std::make_unique<SkAndroidCodecAdapter>(codec.release());
240         case SkEncodedImageFormat::kAVIF: // Handled above
241         case SkEncodedImageFormat::kPKM:
242         case SkEncodedImageFormat::kKTX:
243         case SkEncodedImageFormat::kASTC:
244         case SkEncodedImageFormat::kJPEGXL:
245             return nullptr;
246     }
247     SkUNREACHABLE;
248 }
249 
MakeFromData(sk_sp<SkData> data,SkPngChunkReader * chunkReader)250 std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data,
251                                                              SkPngChunkReader* chunkReader) {
252     if (!data) {
253         return nullptr;
254     }
255 
256     return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader);
257 }
258 
computeOutputColorType(SkColorType requestedColorType)259 SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) {
260     bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8;
261     uint8_t colorDepth = fCodec->getEncodedInfo().getColorDepth();
262     switch (requestedColorType) {
263         case kARGB_4444_SkColorType:
264             return kN32_SkColorType;
265         case kN32_SkColorType:
266             break;
267         case kAlpha_8_SkColorType:
268             // Fall through to kGray_8.  Before kGray_8_SkColorType existed,
269             // we allowed clients to request kAlpha_8 when they wanted a
270             // grayscale decode.
271         case kGray_8_SkColorType:
272             if (kGray_8_SkColorType == this->getInfo().colorType()) {
273                 return kGray_8_SkColorType;
274             }
275             break;
276         case kRGB_565_SkColorType:
277             if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
278                 return kRGB_565_SkColorType;
279             }
280             break;
281         case kRGBA_1010102_SkColorType:
282             if (colorDepth == 10) {
283               return kRGBA_1010102_SkColorType;
284             }
285             break;
286         case kRGBA_F16_SkColorType:
287             return kRGBA_F16_SkColorType;
288         default:
289             break;
290     }
291 
292     // F16 is the Android default for high precision images.
293     return highPrecision ? kRGBA_F16_SkColorType :
294         (colorDepth == 10 ? kRGBA_1010102_SkColorType : kN32_SkColorType);
295 }
296 
computeOutputAlphaType(bool requestedUnpremul)297 SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) {
298     if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
299         return kOpaque_SkAlphaType;
300     }
301     return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
302 }
303 
computeOutputColorSpace(SkColorType outputColorType,sk_sp<SkColorSpace> prefColorSpace)304 sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType,
305                                                             sk_sp<SkColorSpace> prefColorSpace) {
306     switch (outputColorType) {
307         case kRGBA_F16_SkColorType:
308         case kRGB_565_SkColorType:
309         case kRGBA_8888_SkColorType:
310         case kBGRA_8888_SkColorType:
311         case kRGBA_1010102_SkColorType: {
312             // If |prefColorSpace| is supplied, choose it.
313             if (prefColorSpace) {
314                 return prefColorSpace;
315             }
316 
317             const skcms_ICCProfile* encodedProfile = fCodec->getEncodedInfo().profile();
318             if (encodedProfile) {
319                 // Prefer CICP information if it exists.
320                 if (encodedProfile->has_CICP) {
321                     const auto cicpColorSpace =
322                             cicp_get_sk_color_space(encodedProfile->CICP.color_primaries,
323                                                     encodedProfile->CICP.transfer_characteristics,
324                                                     encodedProfile->CICP.matrix_coefficients,
325                                                     encodedProfile->CICP.video_full_range_flag);
326                     if (cicpColorSpace) {
327                         return cicpColorSpace;
328                     }
329                 }
330                 if (auto encodedSpace = SkColorSpace::Make(*encodedProfile)) {
331                     // Leave the pixels in the encoded color space.  Color space conversion
332                     // will be handled after decode time.
333                     return encodedSpace;
334                 }
335 
336                 if (encodedProfile->has_toXYZD50) {
337                     return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB,
338                                                  encodedProfile->toXYZD50);
339                 }
340             }
341 
342             return SkColorSpace::MakeSRGB();
343         }
344         default:
345             // Color correction not supported for kGray.
346             return nullptr;
347     }
348 }
349 
supports_any_down_scale(const SkCodec * codec)350 static bool supports_any_down_scale(const SkCodec* codec) {
351     return codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP;
352 }
353 
354 // There are a variety of ways two SkISizes could be compared. This method
355 // returns true if either dimensions of a is < that of b.
356 // computeSampleSize also uses the opposite, which means that both
357 // dimensions of a >= b.
smaller_than(const SkISize & a,const SkISize & b)358 static inline bool smaller_than(const SkISize& a, const SkISize& b) {
359     return a.width() < b.width() || a.height() < b.height();
360 }
361 
362 // Both dimensions of a > that of b.
strictly_bigger_than(const SkISize & a,const SkISize & b)363 static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) {
364     return a.width() > b.width() && a.height() > b.height();
365 }
366 
computeSampleSize(SkISize * desiredSize) const367 int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const {
368     SkASSERT(desiredSize);
369 
370     const auto origDims = fCodec->dimensions();
371     if (!desiredSize || *desiredSize == origDims) {
372         return 1;
373     }
374 
375     if (smaller_than(origDims, *desiredSize)) {
376         *desiredSize = origDims;
377         return 1;
378     }
379 
380     // Handle bad input:
381     if (desiredSize->width() < 1 || desiredSize->height() < 1) {
382         *desiredSize = SkISize::Make(std::max(1, desiredSize->width()),
383                                      std::max(1, desiredSize->height()));
384     }
385 
386     if (supports_any_down_scale(fCodec.get())) {
387         return 1;
388     }
389 
390     int sampleX = origDims.width()  / desiredSize->width();
391     int sampleY = origDims.height() / desiredSize->height();
392     int sampleSize = std::min(sampleX, sampleY);
393     auto computedSize = this->getSampledDimensions(sampleSize);
394     if (computedSize == *desiredSize) {
395         return sampleSize;
396     }
397 
398     if (computedSize == origDims || sampleSize == 1) {
399         // Cannot downscale
400         *desiredSize = computedSize;
401         return 1;
402     }
403 
404     if (strictly_bigger_than(computedSize, *desiredSize)) {
405         // See if there is a tighter fit.
406         while (true) {
407             auto smaller = this->getSampledDimensions(sampleSize + 1);
408             if (smaller == *desiredSize) {
409                 return sampleSize + 1;
410             }
411             if (smaller == computedSize || smaller_than(smaller, *desiredSize)) {
412                 // Cannot get any smaller without being smaller than desired.
413                 *desiredSize = computedSize;
414                 return sampleSize;
415             }
416 
417             sampleSize++;
418             computedSize = smaller;
419         }
420 
421         SkASSERT(false);
422     }
423 
424     if (!smaller_than(computedSize, *desiredSize)) {
425         // This means one of the computed dimensions is equal to desired, and
426         // the other is bigger. This is as close as we can get.
427         *desiredSize = computedSize;
428         return sampleSize;
429     }
430 
431     // computedSize is too small. Make it larger.
432     while (sampleSize > 2) {
433         auto bigger = this->getSampledDimensions(sampleSize - 1);
434         if (bigger == *desiredSize || !smaller_than(bigger, *desiredSize)) {
435             *desiredSize = bigger;
436             return sampleSize - 1;
437         }
438         sampleSize--;
439     }
440 
441     *desiredSize = origDims;
442     return 1;
443 }
444 
getSampledDimensions(int sampleSize) const445 SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const {
446     if (!is_valid_sample_size(sampleSize)) {
447         return {0, 0};
448     }
449 
450     // Fast path for when we are not scaling.
451     if (1 == sampleSize) {
452         return fCodec->dimensions();
453     }
454 
455     return this->onGetSampledDimensions(sampleSize);
456 }
457 
getSupportedSubset(SkIRect * desiredSubset) const458 bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const {
459     if (!desiredSubset || !is_valid_subset(*desiredSubset, fCodec->dimensions())) {
460         return false;
461     }
462 
463     return this->onGetSupportedSubset(desiredSubset);
464 }
465 
getSampledSubsetDimensions(int sampleSize,const SkIRect & subset) const466 SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const {
467     if (!is_valid_sample_size(sampleSize)) {
468         return {0, 0};
469     }
470 
471     // We require that the input subset is a subset that is supported by SkAndroidCodec.
472     // We test this by calling getSupportedSubset() and verifying that no modifications
473     // are made to the subset.
474     SkIRect copySubset = subset;
475     if (!this->getSupportedSubset(&copySubset) || copySubset != subset) {
476         return {0, 0};
477     }
478 
479     // If the subset is the entire image, for consistency, use getSampledDimensions().
480     if (fCodec->dimensions() == subset.size()) {
481         return this->getSampledDimensions(sampleSize);
482     }
483 
484     // This should perhaps call a virtual function, but currently both of our subclasses
485     // want the same implementation.
486     return {get_scaled_dimension(subset.width(), sampleSize),
487             get_scaled_dimension(subset.height(), sampleSize)};
488 }
489 
getAndroidPixels(const SkImageInfo & requestInfo,void * requestPixels,size_t requestRowBytes,const AndroidOptions * options)490 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo,
491         void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) {
492     if (!requestPixels) {
493         return SkCodec::kInvalidParameters;
494     }
495     if (requestRowBytes < requestInfo.minRowBytes()) {
496         return SkCodec::kInvalidParameters;
497     }
498 
499     AndroidOptions defaultOptions;
500     if (!options) {
501         options = &defaultOptions;
502     } else {
503         if (options->fSubset) {
504             if (!is_valid_subset(*options->fSubset, fCodec->dimensions())) {
505                 return SkCodec::kInvalidParameters;
506             }
507 
508             if (SkIRect::MakeSize(fCodec->dimensions()) == *options->fSubset) {
509                 // The caller wants the whole thing, rather than a subset. Modify
510                 // the AndroidOptions passed to onGetAndroidPixels to not specify
511                 // a subset.
512                 defaultOptions = *options;
513                 defaultOptions.fSubset = nullptr;
514                 options = &defaultOptions;
515             }
516         }
517     }
518 
519     // We may need to have handleFrameIndex recursively call this method
520     // to resolve one frame depending on another. The recursion stops
521     // when we find a frame which does not require an earlier frame
522     // e.g. frame->getRequiredFrame() returns kNoFrame
523     auto getPixelsFn = [&](const SkImageInfo& info, void* pixels, size_t rowBytes,
524                            const SkCodec::Options& opts, int requiredFrame
525                            ) -> SkCodec::Result {
526         SkAndroidCodec::AndroidOptions prevFrameOptions(
527                         reinterpret_cast<const SkAndroidCodec::AndroidOptions&>(opts));
528         prevFrameOptions.fFrameIndex = requiredFrame;
529         return this->getAndroidPixels(info, pixels, rowBytes, &prevFrameOptions);
530     };
531     if (auto result = fCodec->handleFrameIndex(requestInfo, requestPixels, requestRowBytes,
532             *options, getPixelsFn); result != SkCodec::kSuccess) {
533         return result;
534     }
535 
536     return this->onGetAndroidPixels(requestInfo, requestPixels, requestRowBytes, *options);
537 }
538 
getAndroidPixels(const SkImageInfo & info,void * pixels,size_t rowBytes)539 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels,
540         size_t rowBytes) {
541     return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
542 }
543 
getGainmapAndroidCodec(SkGainmapInfo * info,std::unique_ptr<SkAndroidCodec> * outCodec)544 bool SkAndroidCodec::getGainmapAndroidCodec(SkGainmapInfo* info,
545                                             std::unique_ptr<SkAndroidCodec>* outCodec) {
546     if (outCodec) {
547         std::unique_ptr<SkCodec> gainmapCodec;
548         if (!fCodec->onGetGainmapCodec(info, &gainmapCodec)) {
549             return false;
550         }
551         *outCodec = MakeFromCodec(std::move(gainmapCodec));
552         return true;
553     }
554     return fCodec->onGetGainmapCodec(info, nullptr);
555 }
556 
getAndroidGainmap(SkGainmapInfo * info,std::unique_ptr<SkStream> * outGainmapImageStream)557 bool SkAndroidCodec::getAndroidGainmap(SkGainmapInfo* info,
558                                        std::unique_ptr<SkStream>* outGainmapImageStream) {
559     return fCodec->onGetGainmapInfo(info, outGainmapImageStream);
560 }
561