| 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 |
|
| 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 |
|
| 34 |
SkAndroidCodec::SkAndroidCodec(SkCodec* codec) |
| 35 |
: fInfo(codec->getInfo()) |
| 36 |
, fCodec(codec) |
| 37 |
{} |
| 38 |
|
| 39 |
SkAndroidCodec::~SkAndroidCodec() {} |
| 40 |
|
| 41 |
std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 42 |
SkPngChunkReader* chunkReader) { |
| 43 |
auto codec = SkCodec::MakeFromStream(std::move(stream), nullptr, chunkReader); |
| 44 |
return MakeFromCodec(std::move(codec)); |
| 45 |
} |
| 46 |
|
| 47 |
std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromCodec(std::unique_ptr<SkCodec> codec) { |
| 48 |
if (nullptr == codec) { |
| 49 |
return nullptr; |
| 50 |
} |
| 51 |
|
| 52 |
switch (codec->getEncodedFormat()) { |
| 53 |
case SkEncodedImageFormat::kPNG: |
| 54 |
case SkEncodedImageFormat::kICO: |
| 55 |
case SkEncodedImageFormat::kJPEG: |
| 56 |
case SkEncodedImageFormat::kBMP: |
| 57 |
case SkEncodedImageFormat::kWBMP: |
| 58 |
return std::make_unique<SkSampledCodec>(codec.release()); |
| 59 |
case SkEncodedImageFormat::kGIF: |
| 60 |
case SkEncodedImageFormat::kWEBP: |
| 61 |
case SkEncodedImageFormat::kDNG: |
| 62 |
// On the Android framework, both HEIF and AVIF are handled by |
| 63 |
// SkCrabbyAvifCodec. It can handle scaling internally. So we can use |
| 64 |
// SkAndroidCodecAdapter for both these formats. |
| 65 |
case SkEncodedImageFormat::kAVIF: |
| 66 |
case SkEncodedImageFormat::kHEIF: |
| 67 |
return std::make_unique<SkAndroidCodecAdapter>(codec.release()); |
| 68 |
case SkEncodedImageFormat::kPKM: |
| 69 |
case SkEncodedImageFormat::kKTX: |
| 70 |
case SkEncodedImageFormat::kASTC: |
| 71 |
case SkEncodedImageFormat::kJPEGXL: |
| 72 |
return nullptr; |
| 73 |
} |
| 74 |
SkUNREACHABLE; |
| 75 |
} |
| 76 |
|
| 77 |
std::unique_ptr<SkAndroidCodec> SkAndroidCodec::MakeFromData(sk_sp<const SkData> data, |
| 78 |
SkPngChunkReader* chunkReader) { |
| 79 |
if (!data) { |
| 80 |
return nullptr; |
| 81 |
} |
| 82 |
|
| 83 |
return MakeFromStream(SkMemoryStream::Make(std::move(data)), chunkReader); |
| 84 |
} |
| 85 |
|
| 86 |
SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) { |
| 87 |
bool highPrecision = fCodec->getEncodedInfo().bitsPerComponent() > 8; |
| 88 |
uint8_t colorDepth = fCodec->getEncodedInfo().getColorDepth(); |
| 89 |
switch (requestedColorType) { |
| 90 |
case kARGB_4444_SkColorType: |
| 91 |
return kN32_SkColorType; |
| 92 |
case kN32_SkColorType: |
| 93 |
break; |
| 94 |
case kAlpha_8_SkColorType: |
| 95 |
// Fall through to kGray_8. Before kGray_8_SkColorType existed, |
| 96 |
// we allowed clients to request kAlpha_8 when they wanted a |
| 97 |
// grayscale decode. |
| 98 |
case kGray_8_SkColorType: |
| 99 |
if (kGray_8_SkColorType == this->getInfo().colorType()) { |
| 100 |
return kGray_8_SkColorType; |
| 101 |
} |
| 102 |
break; |
| 103 |
case kRGB_565_SkColorType: |
| 104 |
if (kOpaque_SkAlphaType == this->getInfo().alphaType()) { |
| 105 |
return kRGB_565_SkColorType; |
| 106 |
} |
| 107 |
break; |
| 108 |
case kRGBA_1010102_SkColorType: |
| 109 |
if (colorDepth == 10) { |
| 110 |
return kRGBA_1010102_SkColorType; |
| 111 |
} |
| 112 |
break; |
| 113 |
case kRGBA_F16_SkColorType: |
| 114 |
return kRGBA_F16_SkColorType; |
| 115 |
default: |
| 116 |
break; |
| 117 |
} |
| 118 |
|
| 119 |
// F16 is the Android default for high precision images. |
| 120 |
return highPrecision ? kRGBA_F16_SkColorType : |
| 121 |
(colorDepth == 10 ? kRGBA_1010102_SkColorType : kN32_SkColorType); |
| 122 |
} |
| 123 |
|
| 124 |
SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) { |
| 125 |
if (kOpaque_SkAlphaType == this->getInfo().alphaType()) { |
| 126 |
return kOpaque_SkAlphaType; |
| 127 |
} |
| 128 |
return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType; |
| 129 |
} |
| 130 |
|
| 131 |
sk_sp<SkColorSpace> SkAndroidCodec::computeOutputColorSpace(SkColorType outputColorType, |
| 132 |
sk_sp<SkColorSpace> prefColorSpace) { |
| 133 |
switch (outputColorType) { |
| 134 |
case kRGBA_F16_SkColorType: |
| 135 |
case kRGB_565_SkColorType: |
| 136 |
case kRGBA_8888_SkColorType: |
| 137 |
case kBGRA_8888_SkColorType: |
| 138 |
case kRGBA_1010102_SkColorType: { |
| 139 |
// If |prefColorSpace| is supplied, choose it. |
| 140 |
if (prefColorSpace) { |
| 141 |
return prefColorSpace; |
| 142 |
} |
| 143 |
if (const auto* colorProfile = fCodec->getEncodedInfo().colorProfile()) { |
| 144 |
return colorProfile->getAndroidOutputColorSpace(); |
| 145 |
} |
| 146 |
return SkColorSpace::MakeSRGB(); |
| 147 |
} |
| 148 |
default: |
| 149 |
// Color correction not supported for kGray. |
| 150 |
return nullptr; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
static bool is_webp(const SkCodec* codec) { |
| 155 |
return codec->getEncodedFormat() == SkEncodedImageFormat::kWEBP; |
| 156 |
} |
| 157 |
|
| 158 |
// There are a variety of ways two SkISizes could be compared. This method |
| 159 |
// returns true if either dimensions of a is < that of b. |
| 160 |
// computeSampleSize also uses the opposite, which means that both |
| 161 |
// dimensions of a >= b. |
| 162 |
static inline bool smaller_than(const SkISize& a, const SkISize& b) { |
| 163 |
return a.width() < b.width() || a.height() < b.height(); |
| 164 |
} |
| 165 |
|
| 166 |
// Both dimensions of a > that of b. |
| 167 |
static inline bool strictly_bigger_than(const SkISize& a, const SkISize& b) { |
| 168 |
return a.width() > b.width() && a.height() > b.height(); |
| 169 |
} |
| 170 |
|
| 171 |
int SkAndroidCodec::computeSampleSize(SkISize* desiredSize) const { |
| 172 |
SkASSERT(desiredSize); |
| 173 |
|
| 174 |
const auto origDims = fCodec->dimensions(); |
| 175 |
if (!desiredSize || *desiredSize == origDims) { |
| 176 |
return 1; |
| 177 |
} |
| 178 |
|
| 179 |
if (smaller_than(origDims, *desiredSize)) { |
| 180 |
*desiredSize = origDims; |
| 181 |
return 1; |
| 182 |
} |
| 183 |
|
| 184 |
// Handle bad input: |
| 185 |
if (desiredSize->width() < 1 || desiredSize->height() < 1) { |
| 186 |
*desiredSize = SkISize::Make(std::max(1, desiredSize->width()), |
| 187 |
std::max(1, desiredSize->height())); |
| 188 |
} |
| 189 |
|
| 190 |
if (is_webp(fCodec.get())) { |
| 191 |
if (fCodec->getFrameCount() > 1) { |
| 192 |
// Cannot downscale animated webp |
| 193 |
*desiredSize = origDims; |
| 194 |
} |
| 195 |
return 1; |
| 196 |
} |
| 197 |
|
| 198 |
int sampleX = origDims.width() / desiredSize->width(); |
| 199 |
int sampleY = origDims.height() / desiredSize->height(); |
| 200 |
int sampleSize = std::min(sampleX, sampleY); |
| 201 |
auto computedSize = this->getSampledDimensions(sampleSize); |
| 202 |
if (computedSize == *desiredSize) { |
| 203 |
return sampleSize; |
| 204 |
} |
| 205 |
|
| 206 |
if (computedSize == origDims || sampleSize == 1) { |
| 207 |
// Cannot downscale |
| 208 |
*desiredSize = computedSize; |
| 209 |
return 1; |
| 210 |
} |
| 211 |
|
| 212 |
if (strictly_bigger_than(computedSize, *desiredSize)) { |
| 213 |
// See if there is a tighter fit. |
| 214 |
while (true) { |
| 215 |
auto smaller = this->getSampledDimensions(sampleSize + 1); |
| 216 |
if (smaller == *desiredSize) { |
| 217 |
return sampleSize + 1; |
| 218 |
} |
| 219 |
if (smaller == computedSize || smaller_than(smaller, *desiredSize)) { |
| 220 |
// Cannot get any smaller without being smaller than desired. |
| 221 |
*desiredSize = computedSize; |
| 222 |
return sampleSize; |
| 223 |
} |
| 224 |
|
| 225 |
sampleSize++; |
| 226 |
computedSize = smaller; |
| 227 |
} |
| 228 |
|
| 229 |
SkASSERT(false); |
| 230 |
} |
| 231 |
|
| 232 |
if (!smaller_than(computedSize, *desiredSize)) { |
| 233 |
// This means one of the computed dimensions is equal to desired, and |
| 234 |
// the other is bigger. This is as close as we can get. |
| 235 |
*desiredSize = computedSize; |
| 236 |
return sampleSize; |
| 237 |
} |
| 238 |
|
| 239 |
// computedSize is too small. Make it larger. |
| 240 |
while (sampleSize > 2) { |
| 241 |
auto bigger = this->getSampledDimensions(sampleSize - 1); |
| 242 |
if (bigger == *desiredSize || !smaller_than(bigger, *desiredSize)) { |
| 243 |
*desiredSize = bigger; |
| 244 |
return sampleSize - 1; |
| 245 |
} |
| 246 |
sampleSize--; |
| 247 |
} |
| 248 |
|
| 249 |
*desiredSize = origDims; |
| 250 |
return 1; |
| 251 |
} |
| 252 |
|
| 253 |
SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const { |
| 254 |
if (!is_valid_sample_size(sampleSize)) { |
| 255 |
return {0, 0}; |
| 256 |
} |
| 257 |
|
| 258 |
// Fast path for when we are not scaling. |
| 259 |
if (1 == sampleSize) { |
| 260 |
return fCodec->dimensions(); |
| 261 |
} |
| 262 |
|
| 263 |
return this->onGetSampledDimensions(sampleSize); |
| 264 |
} |
| 265 |
|
| 266 |
bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const { |
| 267 |
if (!desiredSubset || !SkCodecPriv::IsValidSubset(*desiredSubset, fCodec->dimensions())) { |
| 268 |
return false; |
| 269 |
} |
| 270 |
|
| 271 |
return this->onGetSupportedSubset(desiredSubset); |
| 272 |
} |
| 273 |
|
| 274 |
SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const { |
| 275 |
if (!is_valid_sample_size(sampleSize)) { |
| 276 |
return {0, 0}; |
| 277 |
} |
| 278 |
|
| 279 |
// We require that the input subset is a subset that is supported by SkAndroidCodec. |
| 280 |
// We test this by calling getSupportedSubset() and verifying that no modifications |
| 281 |
// are made to the subset. |
| 282 |
SkIRect copySubset = subset; |
| 283 |
if (!this->getSupportedSubset(©Subset) || copySubset != subset) { |
| 284 |
return {0, 0}; |
| 285 |
} |
| 286 |
|
| 287 |
// If the subset is the entire image, for consistency, use getSampledDimensions(). |
| 288 |
if (fCodec->dimensions() == subset.size()) { |
| 289 |
return this->getSampledDimensions(sampleSize); |
| 290 |
} |
| 291 |
|
| 292 |
// This should perhaps call a virtual function, but currently both of our subclasses |
| 293 |
// want the same implementation. |
| 294 |
return {SkCodecPriv::GetSampledDimension(subset.width(), sampleSize), |
| 295 |
SkCodecPriv::GetSampledDimension(subset.height(), sampleSize)}; |
| 296 |
} |
| 297 |
|
| 298 |
SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& requestInfo, |
| 299 |
void* requestPixels, size_t requestRowBytes, const AndroidOptions* options) { |
| 300 |
if (!requestPixels) { |
| 301 |
return SkCodec::kInvalidParameters; |
| 302 |
} |
| 303 |
if (requestRowBytes < requestInfo.minRowBytes()) { |
| 304 |
return SkCodec::kInvalidParameters; |
| 305 |
} |
| 306 |
|
| 307 |
AndroidOptions defaultOptions; |
| 308 |
if (!options) { |
| 309 |
options = &defaultOptions; |
| 310 |
} else { |
| 311 |
if (options->fSubset) { |
| 312 |
if (!SkCodecPriv::IsValidSubset(*options->fSubset, fCodec->dimensions())) { |
| 313 |
return SkCodec::kInvalidParameters; |
| 314 |
} |
| 315 |
|
| 316 |
if (SkIRect::MakeSize(fCodec->dimensions()) == *options->fSubset) { |
| 317 |
// The caller wants the whole thing, rather than a subset. Modify |
| 318 |
// the AndroidOptions passed to onGetAndroidPixels to not specify |
| 319 |
// a subset. |
| 320 |
defaultOptions = *options; |
| 321 |
defaultOptions.fSubset = nullptr; |
| 322 |
options = &defaultOptions; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
// We may need to have handleFrameIndex recursively call this method |
| 328 |
// to resolve one frame depending on another. The recursion stops |
| 329 |
// when we find a frame which does not require an earlier frame |
| 330 |
// e.g. frame->getRequiredFrame() returns kNoFrame |
| 331 |
auto getPixelsFn = [&](const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 332 |
const SkCodec::Options& opts, int requiredFrame |
| 333 |
) -> SkCodec::Result { |
| 334 |
SkAndroidCodec::AndroidOptions prevFrameOptions( |
| 335 |
reinterpret_cast<const SkAndroidCodec::AndroidOptions&>(opts)); |
| 336 |
prevFrameOptions.fFrameIndex = requiredFrame; |
| 337 |
return this->getAndroidPixels(info, pixels, rowBytes, &prevFrameOptions); |
| 338 |
}; |
| 339 |
if (auto result = fCodec->handleFrameIndex(requestInfo, requestPixels, requestRowBytes, |
| 340 |
*options, getPixelsFn); result != SkCodec::kSuccess) { |
| 341 |
return result; |
| 342 |
} |
| 343 |
|
| 344 |
return this->onGetAndroidPixels(requestInfo, requestPixels, requestRowBytes, *options); |
| 345 |
} |
| 346 |
|
| 347 |
SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels, |
| 348 |
size_t rowBytes) { |
| 349 |
return this->getAndroidPixels(info, pixels, rowBytes, nullptr); |
| 350 |
} |
| 351 |
|
| 352 |
bool SkAndroidCodec::getGainmapAndroidCodec(SkGainmapInfo* info, |
| 353 |
std::unique_ptr<SkAndroidCodec>* outCodec) { |
| 354 |
if (outCodec) { |
| 355 |
std::unique_ptr<SkCodec> gainmapCodec; |
| 356 |
if (!fCodec->onGetGainmapCodec(info, &gainmapCodec)) { |
| 357 |
return false; |
| 358 |
} |
| 359 |
*outCodec = MakeFromCodec(std::move(gainmapCodec)); |
| 360 |
return true; |
| 361 |
} |
| 362 |
return fCodec->onGetGainmapCodec(info, nullptr); |
| 363 |
} |
| 364 |
|
| 365 |
bool SkAndroidCodec::getAndroidGainmap(SkGainmapInfo* info, |
| 366 |
std::unique_ptr<SkStream>* outGainmapImageStream) { |
| 367 |
return fCodec->onGetGainmapInfo(info, outGainmapImageStream); |
| 368 |
} |