Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkAndroidCodec.cpp
Go to the documentation of this file.
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
9
14#include "include/core/SkData.h"
15#include "include/core/SkRect.h"
17#include "modules/skcms/skcms.h"
21
22#include <algorithm>
23#include <cstdint>
24#include <functional>
25#include <utility>
26
28
29static 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
34static 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:
51 0.67f, 0.33f, 0.21f, 0.71f, 0.14f, 0.08f, 0.31f, 0.316f, sk_primaries);
52 case 5:
54 0.64f, 0.33f, 0.29f, 0.60f, 0.15f, 0.06f, 0.3127f, 0.3290f, sk_primaries);
55 case 6:
57 0.630f, 0.340f, 0.310f, 0.595f, 0.155f, 0.070f, 0.3127f, 0.3290f, sk_primaries);
58 case 7:
60 0.630f, 0.340f, 0.310f, 0.595f, 0.155f, 0.070f, 0.3127f, 0.3290f, sk_primaries);
61 case 8:
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:
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:
72 0.680f, 0.320f, 0.265f, 0.690f, 0.150f, 0.060f, 0.314f, 0.351f, sk_primaries);
73 case 12:
75 0.680f, 0.320f, 0.265f, 0.690f, 0.150f, 0.060f, 0.3127f, 0.3290f, sk_primaries);
76 case 22:
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
87static 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:
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:
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:
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:
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:
141 return true;
142 case 14:
144 return true;
145 case 15:
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
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
179 return false;
180}
181
182static 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
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
200 : fInfo(codec->getInfo())
201 , fCodec(codec)
202{}
203
205
206std::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
212std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromCodec(std::unique_ptr<SkCodec> codec) {
213 if (nullptr == codec) {
214 return nullptr;
215 }
216
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) {
235 return std::make_unique<SkSampledCodec>(codec.release());
239 return std::make_unique<SkAndroidCodecAdapter>(codec.release());
240 case SkEncodedImageFormat::kAVIF: // Handled above
245 return nullptr;
246 }
248}
249
250std::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
260 bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8;
261 uint8_t colorDepth = fCodec->getEncodedInfo().getColorDepth();
262 switch (requestedColorType) {
264 return kN32_SkColorType;
265 case kN32_SkColorType:
266 break;
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.
272 if (kGray_8_SkColorType == this->getInfo().colorType()) {
273 return kGray_8_SkColorType;
274 }
275 break;
277 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
279 }
280 break;
282 if (colorDepth == 10) {
284 }
285 break;
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
298 if (kOpaque_SkAlphaType == this->getInfo().alphaType()) {
299 return kOpaque_SkAlphaType;
300 }
301 return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
302}
303
305 sk_sp<SkColorSpace> prefColorSpace) {
306 switch (outputColorType) {
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 =
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) {
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
350static bool supports_any_down_scale(const SkCodec* codec) {
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.
358static 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.
363static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) {
364 return a.width() > b.width() && a.height() > b.height();
365}
366
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
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
458bool 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
466SkISize 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
491 void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) {
492 if (!requestPixels) {
494 }
495 if (requestRowBytes < requestInfo.minRowBytes()) {
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())) {
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
540 size_t rowBytes) {
541 return this->getAndroidPixels(info, pixels, rowBytes, nullptr);
542}
543
545 std::unique_ptr<SkStream>* outGainmapImageStream) {
546 return fCodec->onGetGainmapInfo(info, outGainmapImageStream);
547}
const char * options
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
kUnpremul_SkAlphaType
SkAlphaType
Definition SkAlphaType.h:26
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
@ kPremul_SkAlphaType
pixel components are premultiplied by alpha
Definition SkAlphaType.h:29
static bool cicp_get_primaries(uint8_t primaries, skcms_Matrix3x3 *sk_primaries)
static bool smaller_than(const SkISize &a, const SkISize &b)
static sk_sp< SkColorSpace > cicp_get_sk_color_space(uint8_t color_primaries, uint8_t transfer_characteristics, uint8_t matrix_coefficients, uint8_t full_range_flag)
static bool strictly_bigger_than(const SkISize &a, const SkISize &b)
static bool supports_any_down_scale(const SkCodec *codec)
static bool cicp_get_transfer_fn(uint8_t transfer_characteristics, skcms_TransferFunction *trfn)
static bool is_valid_sample_size(int sampleSize)
#define SkUNREACHABLE
Definition SkAssert.h:135
#define SkASSERT(cond)
Definition SkAssert.h:116
static bool is_valid_subset(const SkIRect &subset, const SkISize &imageDims)
Definition SkCodecPriv.h:35
static int get_scaled_dimension(int srcDimension, int sampleSize)
Definition SkCodecPriv.h:44
SkColorType
Definition SkColorType.h:19
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition SkColorType.h:23
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition SkColorType.h:21
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition SkColorType.h:22
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
@ kRGBA_1010102_SkColorType
10 bits for red, green, blue; 2 bits for alpha; in 32-bit word
Definition SkColorType.h:27
SkEncodedImageFormat
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
bool getSupportedSubset(SkIRect *desiredSubset) const
virtual SkCodec::Result onGetAndroidPixels(const SkImageInfo &info, void *pixels, size_t rowBytes, const AndroidOptions &options)=0
static std::unique_ptr< SkAndroidCodec > MakeFromData(sk_sp< SkData >, SkPngChunkReader *=nullptr)
SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect &subset) const
const SkImageInfo & getInfo() const
virtual ~SkAndroidCodec()
SkCodec::Result getAndroidPixels(const SkImageInfo &info, void *pixels, size_t rowBytes, const AndroidOptions *options)
static std::unique_ptr< SkAndroidCodec > MakeFromCodec(std::unique_ptr< SkCodec >)
virtual SkISize onGetSampledDimensions(int sampleSize) const =0
SkISize getSampledDimensions(int sampleSize) const
SkCodec * codec() const
bool getAndroidGainmap(SkGainmapInfo *outInfo, std::unique_ptr< SkStream > *outGainmapImageStream)
SkColorType computeOutputColorType(SkColorType requestedColorType)
SkAndroidCodec(SkCodec *)
SkAlphaType computeOutputAlphaType(bool requestedUnpremul)
int computeSampleSize(SkISize *size) const
virtual bool onGetSupportedSubset(SkIRect *desiredSubset) const =0
sk_sp< SkColorSpace > computeOutputColorSpace(SkColorType outputColorType, sk_sp< SkColorSpace > prefColorSpace=nullptr)
static std::unique_ptr< SkAndroidCodec > MakeFromStream(std::unique_ptr< SkStream >, SkPngChunkReader *=nullptr)
static std::unique_ptr< SkCodec > MakeFromStream(std::unique_ptr< SkStream >, SkSpan< const SkCodecs::Decoder > decoders, Result *=nullptr, SkPngChunkReader *=nullptr, SelectionPolicy selectionPolicy=SelectionPolicy::kPreferStillImage)
Definition SkCodec.cpp:163
SkEncodedImageFormat getEncodedFormat() const
Definition SkCodec.h:292
@ kInvalidParameters
Definition SkCodec.h:105
@ kSuccess
Definition SkCodec.h:80
static sk_sp< SkColorSpace > MakeSRGB()
static sk_sp< SkColorSpace > MakeRGB(const skcms_TransferFunction &transferFn, const skcms_Matrix3x3 &toXYZ)
static sk_sp< SkColorSpace > Make(const skcms_ICCProfile &)
static std::unique_ptr< SkMemoryStream > Make(sk_sp< SkData > data)
Definition SkStream.cpp:314
static bool b
struct MyStruct a[10]
GAsyncResult * result
uint32_t uint32_t * format
bool HasDecoder(std::string_view id)
Definition SkCodec.cpp:146
static constexpr skcms_Matrix3x3 kSRGB
static constexpr skcms_Matrix3x3 kRec2020
static constexpr skcms_TransferFunction kRec2020
static constexpr skcms_TransferFunction kSRGB
static constexpr skcms_TransferFunction kLinear
bool skcms_PrimariesToXYZD50(float rx, float ry, float gx, float gy, float bx, float by, float wx, float wy, skcms_Matrix3x3 *toXYZD50)
Definition skcms.cc:1747
bool skcms_TransferFunction_makeScaledHLGish(skcms_TransferFunction *tf, float K, float R, float G, float a, float b, float c)
Definition skcms.cc:204
const SkIRect * fSubset
Definition SkCodec.h:347
constexpr SkISize size() const
Definition SkRect.h:172
constexpr int32_t height() const
Definition SkRect.h:165
static constexpr SkIRect MakeSize(const SkISize &size)
Definition SkRect.h:66
constexpr int32_t width() const
Definition SkRect.h:158
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
constexpr int32_t width() const
Definition SkSize.h:36
constexpr int32_t height() const
Definition SkSize.h:37
size_t minRowBytes() const
uint8_t color_primaries
uint8_t matrix_coefficients
uint8_t video_full_range_flag
uint8_t transfer_characteristics
skcms_Matrix3x3 toXYZD50