msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 14 | |
| 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; |
| 18 | } |
| 19 | |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 20 | /** |
| 21 | * Loads the gamut as a set of three points (triangle). |
| 22 | */ |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 23 | static void load_gamut(SkPoint rgb[], const skcms_Matrix3x3& xyz) { |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 24 | // rx = rX / (rX + rY + rZ) |
| 25 | // ry = rY / (rX + rY + rZ) |
| 26 | // gx, gy, bx, and gy are calulcated similarly. |
nagarajan.n | 6e17b6f | 2017-07-10 15:02:00 +0530 | [diff] [blame] | 27 | for (int rgbIdx = 0; rgbIdx < 3; rgbIdx++) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 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; |
nagarajan.n | 6e17b6f | 2017-07-10 15:02:00 +0530 | [diff] [blame] | 31 | } |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Calculates the area of the triangular gamut. |
| 36 | */ |
| 37 | static 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 III | 862c196 | 2017-10-02 16:28:49 -0400 | [diff] [blame] | 44 | static constexpr float kSRGB_D50_GamutArea = 0.084f; |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 45 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 46 | static bool is_wide_gamut(const skcms_ICCProfile& profile) { |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 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. |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 49 | if (profile.has_toXYZD50) { |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 50 | SkPoint rgb[3]; |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 51 | load_gamut(rgb, profile.toXYZD50); |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 52 | return calculate_area(rgb) > kSRGB_D50_GamutArea; |
| 53 | } |
| 54 | |
| 55 | return false; |
| 56 | } |
| 57 | |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 58 | SkAndroidCodec::SkAndroidCodec(SkCodec* codec) |
| 59 | : fInfo(codec->getInfo()) |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 60 | , fCodec(codec) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 61 | {} |
| 62 | |
Leon Scroggins III | 0741818 | 2017-08-15 12:24:02 -0400 | [diff] [blame] | 63 | SkAndroidCodec::~SkAndroidCodec() {} |
| 64 | |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 65 | std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 66 | SkPngChunkReader* chunkReader) { |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 67 | auto codec = SkCodec::MakeFromStream(std::move(stream), nullptr, chunkReader); |
Leon Scroggins III | 7397d7a | 2018-01-04 13:26:30 -0500 | [diff] [blame] | 68 | return MakeFromCodec(std::move(codec)); |
| 69 | } |
| 70 | |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 71 | std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromCodec(std::unique_ptr<SkCodec> codec) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 72 | if (nullptr == codec) { |
| 73 | return nullptr; |
| 74 | } |
| 75 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 76 | switch ((SkEncodedImageFormat)codec->getEncodedFormat()) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 77 | case SkEncodedImageFormat::kPNG: |
| 78 | case SkEncodedImageFormat::kICO: |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 79 | case SkEncodedImageFormat::kJPEG: |
Leon Scroggins III | 70d8f4f | 2019-04-01 12:57:46 -0400 | [diff] [blame] | 80 | #ifndef SK_HAS_WUFFS_LIBRARY |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 81 | case SkEncodedImageFormat::kGIF: |
Leon Scroggins III | 70d8f4f | 2019-04-01 12:57:46 -0400 | [diff] [blame] | 82 | #endif |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 83 | case SkEncodedImageFormat::kBMP: |
| 84 | case SkEncodedImageFormat::kWBMP: |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 85 | case SkEncodedImageFormat::kHEIF: |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 86 | case SkEncodedImageFormat::kAVIF: |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 87 | return std::make_unique<SkSampledCodec>(codec.release()); |
Leon Scroggins III | 70d8f4f | 2019-04-01 12:57:46 -0400 | [diff] [blame] | 88 | #ifdef SK_HAS_WUFFS_LIBRARY |
| 89 | case SkEncodedImageFormat::kGIF: |
| 90 | #endif |
Leon Scroggins III | a77f30c | 2020-03-09 14:23:30 -0400 | [diff] [blame] | 91 | #ifdef SK_CODEC_DECODES_WEBP |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 92 | case SkEncodedImageFormat::kWEBP: |
Mike Klein | 0af9929 | 2019-05-15 22:02:05 +0000 | [diff] [blame] | 93 | #endif |
| 94 | #ifdef SK_CODEC_DECODES_RAW |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 95 | case SkEncodedImageFormat::kDNG: |
Mike Klein | 0af9929 | 2019-05-15 22:02:05 +0000 | [diff] [blame] | 96 | #endif |
Leon Scroggins III | a77f30c | 2020-03-09 14:23:30 -0400 | [diff] [blame] | 97 | #if defined(SK_CODEC_DECODES_WEBP) || defined(SK_CODEC_DECODES_RAW) || defined(SK_HAS_WUFFS_LIBRARY) |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 98 | return std::make_unique<SkAndroidCodecAdapter>(codec.release()); |
Mike Klein | 0af9929 | 2019-05-15 22:02:05 +0000 | [diff] [blame] | 99 | #endif |
Nigel Tao | 612a65d | 2018-11-09 10:10:31 +1100 | [diff] [blame] | 100 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 101 | default: |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 102 | return nullptr; |
| 103 | } |
| 104 | } |
| 105 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 106 | std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<SkData> data, |
| 107 | SkPngChunkReader* chunkReader) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 108 | if (!data) { |
| 109 | return nullptr; |
| 110 | } |
| 111 | |
Mike Reed | 847068c | 2017-07-26 11:35:53 -0400 | [diff] [blame] | 112 | return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 113 | } |
| 114 | |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 115 | SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) { |
Leon Scroggins | 1793e7b | 2017-12-05 15:38:14 +0000 | [diff] [blame] | 116 | bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8; |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 117 | switch (requestedColorType) { |
| 118 | case kARGB_4444_SkColorType: |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 119 | return kN32_SkColorType; |
Matt Sarett | 8dcc84f | 2016-12-14 10:23:41 -0500 | [diff] [blame] | 120 | case kN32_SkColorType: |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 121 | 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 Sarett | 8db74f1 | 2017-06-14 13:02:05 +0000 | [diff] [blame] | 127 | if (kGray_8_SkColorType == this->getInfo().colorType()) { |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 128 | 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 Sarett | 8dcc84f | 2016-12-14 10:23:41 -0500 | [diff] [blame] | 136 | case kRGBA_F16_SkColorType: |
| 137 | return kRGBA_F16_SkColorType; |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 138 | default: |
| 139 | break; |
| 140 | } |
| 141 | |
Leon Scroggins | 1793e7b | 2017-12-05 15:38:14 +0000 | [diff] [blame] | 142 | // F16 is the Android default for high precision images. |
| 143 | return highPrecision ? kRGBA_F16_SkColorType : kN32_SkColorType; |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | SkAlphaType 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 Sarett | 68feef4 | 2017-04-11 09:51:32 -0400 | [diff] [blame] | 153 | sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType, |
| 154 | sk_sp<SkColorSpace> prefColorSpace) { |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 155 | switch (outputColorType) { |
Derek Sollenberger | 1be431f | 2019-01-30 11:21:33 -0500 | [diff] [blame] | 156 | case kRGBA_F16_SkColorType: |
| 157 | case kRGB_565_SkColorType: |
Leon Scroggins III | c20b5f8 | 2017-07-13 08:05:29 -0400 | [diff] [blame] | 158 | case kRGBA_8888_SkColorType: |
| 159 | case kBGRA_8888_SkColorType: { |
Brian Osman | 82ebe04 | 2019-01-04 17:03:00 -0500 | [diff] [blame] | 160 | // If |prefColorSpace| is supplied, choose it. |
| 161 | if (prefColorSpace) { |
Matt Sarett | 68feef4 | 2017-04-11 09:51:32 -0400 | [diff] [blame] | 162 | return prefColorSpace; |
| 163 | } |
| 164 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 165 | const skcms_ICCProfile* encodedProfile = fCodec->getEncodedInfo().profile(); |
Leon Scroggins III | 227d4e1 | 2018-09-27 08:30:22 -0400 | [diff] [blame] | 166 | 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 Sarett | 9341c98 | 2017-02-28 15:36:42 -0500 | [diff] [blame] | 172 | |
Leon Scroggins III | 227d4e1 | 2018-09-27 08:30:22 -0400 | [diff] [blame] | 173 | if (is_wide_gamut(*encodedProfile)) { |
Mike Klein | b147ace | 2020-01-16 11:11:06 -0600 | [diff] [blame] | 174 | return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDisplayP3); |
Leon Scroggins III | 227d4e1 | 2018-09-27 08:30:22 -0400 | [diff] [blame] | 175 | } |
Matt Sarett | 2ae3a2e | 2017-02-15 08:48:02 -0500 | [diff] [blame] | 176 | } |
| 177 | |
Matt Sarett | cf3f234 | 2017-03-23 15:32:25 -0400 | [diff] [blame] | 178 | return SkColorSpace::MakeSRGB(); |
Matt Sarett | 9341c98 | 2017-02-28 15:36:42 -0500 | [diff] [blame] | 179 | } |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 180 | default: |
Matt Sarett | 3725f0a | 2017-03-28 14:34:20 -0400 | [diff] [blame] | 181 | // Color correction not supported for kGray. |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 182 | return nullptr; |
| 183 | } |
| 184 | } |
| 185 | |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 186 | static 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. |
| 194 | static 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. |
| 199 | static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) { |
| 200 | return a.width() > b.width() && a.height() > b.height(); |
| 201 | } |
| 202 | |
| 203 | int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const { |
| 204 | SkASSERT(desiredSize); |
| 205 | |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 206 | const auto origDims = fCodec->dimensions(); |
| 207 | if (!desiredSize || *desiredSize == origDims) { |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 208 | return 1; |
| 209 | } |
| 210 | |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 211 | if (smaller_than(origDims, *desiredSize)) { |
| 212 | *desiredSize = origDims; |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 213 | 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 III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 226 | int sampleX = origDims.width() / desiredSize->width(); |
| 227 | int sampleY = origDims.height() / desiredSize->height(); |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 228 | int sampleSize = std::min(sampleX, sampleY); |
| 229 | auto computedSize = this->getSampledDimensions(sampleSize); |
| 230 | if (computedSize == *desiredSize) { |
| 231 | return sampleSize; |
| 232 | } |
| 233 | |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 234 | if (computedSize == origDims || sampleSize == 1) { |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 235 | // 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 III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 277 | *desiredSize = origDims; |
Leon Scroggins III | 07a722c | 2018-01-16 15:01:17 -0500 | [diff] [blame] | 278 | return 1; |
| 279 | } |
| 280 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 281 | SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const { |
| 282 | if (!is_valid_sample_size(sampleSize)) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 283 | return {0, 0}; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 284 | } |
| 285 | |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 286 | // Fast path for when we are not scaling. |
| 287 | if (1 == sampleSize) { |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 288 | return fCodec->dimensions(); |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 291 | return this->onGetSampledDimensions(sampleSize); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const { |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 295 | if (!desiredSubset || !is_valid_subset(*desiredSubset, fCodec->dimensions())) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | |
| 299 | return this->onGetSupportedSubset(desiredSubset); |
| 300 | } |
| 301 | |
| 302 | SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const { |
| 303 | if (!is_valid_sample_size(sampleSize)) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 304 | return {0, 0}; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 305 | } |
| 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(©Subset) || copySubset != subset) { |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 312 | return {0, 0}; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 313 | } |
| 314 | |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 315 | // If the subset is the entire image, for consistency, use getSampledDimensions(). |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 316 | if (fCodec->dimensions() == subset.size()) { |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 317 | return this->getSampledDimensions(sampleSize); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // This should perhaps call a virtual function, but currently both of our subclasses |
| 321 | // want the same implementation. |
Hal Canary | fafe135 | 2017-04-11 12:12:02 -0400 | [diff] [blame] | 322 | return {get_scaled_dimension(subset.width(), sampleSize), |
| 323 | get_scaled_dimension(subset.height(), sampleSize)}; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 326 | SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo, |
| 327 | void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) { |
| 328 | if (!requestPixels) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 329 | return SkCodec::kInvalidParameters; |
| 330 | } |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 331 | if (requestRowBytes < requestInfo.minRowBytes()) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 332 | return SkCodec::kInvalidParameters; |
| 333 | } |
| 334 | |
| 335 | AndroidOptions defaultOptions; |
| 336 | if (!options) { |
| 337 | options = &defaultOptions; |
Leon Scroggins | 1340dbd | 2020-11-09 14:18:12 -0500 | [diff] [blame] | 338 | } else { |
| 339 | if (options->fSubset) { |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 340 | if (!is_valid_subset(*options->fSubset, fCodec->dimensions())) { |
Leon Scroggins | 1340dbd | 2020-11-09 14:18:12 -0500 | [diff] [blame] | 341 | return SkCodec::kInvalidParameters; |
| 342 | } |
| 343 | |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 344 | if (SkIRect::MakeSize(fCodec->dimensions()) == *options->fSubset) { |
Leon Scroggins | 1340dbd | 2020-11-09 14:18:12 -0500 | [diff] [blame] | 345 | // 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 | } |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 352 | } |
Leon Scroggins | fc4fdc5 | 2020-11-09 14:18:12 -0500 | [diff] [blame] | 353 | } |
| 354 | |
Leon Scroggins | 1340dbd | 2020-11-09 14:18:12 -0500 | [diff] [blame] | 355 | if (auto result = fCodec->handleFrameIndex(requestInfo, requestPixels, requestRowBytes, |
| 356 | *options, this); result != SkCodec::kSuccess) { |
| 357 | return result; |
| 358 | } |
| 359 | |
Leon Scroggins III | add35d9 | 2020-12-15 15:58:53 -0500 | [diff] [blame^] | 360 | return this->onGetAndroidPixels(requestInfo, requestPixels, requestRowBytes, *options); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels, |
| 364 | size_t rowBytes) { |
| 365 | return this->getAndroidPixels(info, pixels, rowBytes, nullptr); |
| 366 | } |