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 | |
| 8 | #include "SkAndroidCodec.h" |
| 9 | #include "SkCodec.h" |
| 10 | #include "SkCodecPriv.h" |
| 11 | #include "SkSampledCodec.h" |
| 12 | #include "SkWebpAdapterCodec.h" |
| 13 | |
| 14 | static bool is_valid_sample_size(int sampleSize) { |
| 15 | // FIXME: As Leon has mentioned elsewhere, surely there is also a maximum sampleSize? |
| 16 | return sampleSize > 0; |
| 17 | } |
| 18 | |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 19 | SkAndroidCodec::SkAndroidCodec(SkCodec* codec) |
| 20 | : fInfo(codec->getInfo()) |
| 21 | , fCodec(codec) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 22 | {} |
| 23 | |
msarett | 7d5105c | 2015-12-02 07:02:41 -0800 | [diff] [blame] | 24 | SkAndroidCodec* SkAndroidCodec::NewFromStream(SkStream* stream, SkPngChunkReader* chunkReader) { |
| 25 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream, chunkReader)); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 26 | if (nullptr == codec) { |
| 27 | return nullptr; |
| 28 | } |
| 29 | |
| 30 | switch (codec->getEncodedFormat()) { |
| 31 | case kWEBP_SkEncodedFormat: |
| 32 | return new SkWebpAdapterCodec((SkWebpCodec*) codec.detach()); |
| 33 | case kPNG_SkEncodedFormat: |
| 34 | case kJPEG_SkEncodedFormat: |
msarett | 33c7623 | 2015-11-16 13:43:40 -0800 | [diff] [blame] | 35 | case kWBMP_SkEncodedFormat: |
msarett | 887e18e | 2015-11-17 08:46:02 -0800 | [diff] [blame] | 36 | case kBMP_SkEncodedFormat: |
msarett | 5af4e0b | 2015-11-17 11:18:03 -0800 | [diff] [blame] | 37 | case kGIF_SkEncodedFormat: |
msarett | be8216a | 2015-12-04 08:00:50 -0800 | [diff] [blame] | 38 | case kICO_SkEncodedFormat: |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 39 | return new SkSampledCodec(codec.detach()); |
| 40 | default: |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 41 | return nullptr; |
| 42 | } |
| 43 | } |
| 44 | |
msarett | 7d5105c | 2015-12-02 07:02:41 -0800 | [diff] [blame] | 45 | SkAndroidCodec* SkAndroidCodec::NewFromData(SkData* data, SkPngChunkReader* chunkReader) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 46 | if (!data) { |
| 47 | return nullptr; |
| 48 | } |
| 49 | |
msarett | 7d5105c | 2015-12-02 07:02:41 -0800 | [diff] [blame] | 50 | return NewFromStream(new SkMemoryStream(data), chunkReader); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 51 | } |
| 52 | |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 53 | SkColorType SkAndroidCodec::computeOutputColorType(SkColorType requestedColorType) { |
| 54 | SkColorType suggestedColorType = this->getInfo().colorType(); |
| 55 | switch (requestedColorType) { |
| 56 | case kARGB_4444_SkColorType: |
| 57 | case kN32_SkColorType: |
| 58 | return kN32_SkColorType; |
| 59 | case kIndex_8_SkColorType: |
| 60 | if (kIndex_8_SkColorType == suggestedColorType) { |
| 61 | return kIndex_8_SkColorType; |
| 62 | } |
| 63 | break; |
| 64 | case kAlpha_8_SkColorType: |
| 65 | // Fall through to kGray_8. Before kGray_8_SkColorType existed, |
| 66 | // we allowed clients to request kAlpha_8 when they wanted a |
| 67 | // grayscale decode. |
| 68 | case kGray_8_SkColorType: |
| 69 | if (kGray_8_SkColorType == suggestedColorType) { |
| 70 | return kGray_8_SkColorType; |
| 71 | } |
| 72 | break; |
| 73 | case kRGB_565_SkColorType: |
| 74 | if (kOpaque_SkAlphaType == this->getInfo().alphaType()) { |
| 75 | return kRGB_565_SkColorType; |
| 76 | } |
| 77 | break; |
| 78 | default: |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | return suggestedColorType; |
| 83 | } |
| 84 | |
| 85 | SkAlphaType SkAndroidCodec::computeOutputAlphaType(bool requestedUnpremul) { |
| 86 | if (kOpaque_SkAlphaType == this->getInfo().alphaType()) { |
| 87 | return kOpaque_SkAlphaType; |
| 88 | } |
| 89 | return requestedUnpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType; |
| 90 | } |
| 91 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 92 | SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const { |
| 93 | if (!is_valid_sample_size(sampleSize)) { |
| 94 | return SkISize::Make(0, 0); |
| 95 | } |
| 96 | |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 97 | // Fast path for when we are not scaling. |
| 98 | if (1 == sampleSize) { |
| 99 | return fInfo.dimensions(); |
| 100 | } |
| 101 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 102 | return this->onGetSampledDimensions(sampleSize); |
| 103 | } |
| 104 | |
| 105 | bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const { |
| 106 | if (!desiredSubset || !is_valid_subset(*desiredSubset, fInfo.dimensions())) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | return this->onGetSupportedSubset(desiredSubset); |
| 111 | } |
| 112 | |
| 113 | SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const { |
| 114 | if (!is_valid_sample_size(sampleSize)) { |
| 115 | return SkISize::Make(0, 0); |
| 116 | } |
| 117 | |
| 118 | // We require that the input subset is a subset that is supported by SkAndroidCodec. |
| 119 | // We test this by calling getSupportedSubset() and verifying that no modifications |
| 120 | // are made to the subset. |
| 121 | SkIRect copySubset = subset; |
| 122 | if (!this->getSupportedSubset(©Subset) || copySubset != subset) { |
| 123 | return SkISize::Make(0, 0); |
| 124 | } |
| 125 | |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 126 | // If the subset is the entire image, for consistency, use getSampledDimensions(). |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 127 | if (fInfo.dimensions() == subset.size()) { |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 128 | return this->getSampledDimensions(sampleSize); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // This should perhaps call a virtual function, but currently both of our subclasses |
| 132 | // want the same implementation. |
| 133 | return SkISize::Make(get_scaled_dimension(subset.width(), sampleSize), |
| 134 | get_scaled_dimension(subset.height(), sampleSize)); |
| 135 | } |
| 136 | |
| 137 | SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels, |
scroggo | e95a068 | 2015-11-04 04:31:12 -0800 | [diff] [blame] | 138 | size_t rowBytes, const AndroidOptions* options) { |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 139 | if (!pixels) { |
| 140 | return SkCodec::kInvalidParameters; |
| 141 | } |
| 142 | if (rowBytes < info.minRowBytes()) { |
| 143 | return SkCodec::kInvalidParameters; |
| 144 | } |
| 145 | |
| 146 | AndroidOptions defaultOptions; |
| 147 | if (!options) { |
| 148 | options = &defaultOptions; |
| 149 | } else if (options->fSubset) { |
| 150 | if (!is_valid_subset(*options->fSubset, fInfo.dimensions())) { |
| 151 | return SkCodec::kInvalidParameters; |
| 152 | } |
scroggo | 501b734 | 2015-11-03 07:55:11 -0800 | [diff] [blame] | 153 | |
| 154 | if (SkIRect::MakeSize(fInfo.dimensions()) == *options->fSubset) { |
| 155 | // The caller wants the whole thing, rather than a subset. Modify |
| 156 | // the AndroidOptions passed to onGetAndroidPixels to not specify |
| 157 | // a subset. |
| 158 | defaultOptions = *options; |
| 159 | defaultOptions.fSubset = nullptr; |
| 160 | options = &defaultOptions; |
| 161 | } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | return this->onGetAndroidPixels(info, pixels, rowBytes, *options); |
| 165 | } |
| 166 | |
| 167 | SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void* pixels, |
| 168 | size_t rowBytes) { |
| 169 | return this->getAndroidPixels(info, pixels, rowBytes, nullptr); |
| 170 | } |