OLD | NEW |
(Empty) | |
| 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 sa
mpleSize? |
| 16 return sampleSize > 0; |
| 17 } |
| 18 |
| 19 SkAndroidCodec::SkAndroidCodec(const SkImageInfo& info) |
| 20 : fInfo(info) |
| 21 {} |
| 22 |
| 23 SkAndroidCodec* SkAndroidCodec::NewFromStream(SkStream* stream) { |
| 24 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream)); |
| 25 if (nullptr == codec) { |
| 26 return nullptr; |
| 27 } |
| 28 |
| 29 switch (codec->getEncodedFormat()) { |
| 30 case kWEBP_SkEncodedFormat: |
| 31 return new SkWebpAdapterCodec((SkWebpCodec*) codec.detach()); |
| 32 case kPNG_SkEncodedFormat: |
| 33 case kJPEG_SkEncodedFormat: |
| 34 return new SkSampledCodec(codec.detach()); |
| 35 default: |
| 36 // FIXME: SkSampledCodec is temporarily disabled for other formats |
| 37 // while focusing on the formats that are supported by |
| 38 // BitmapRegionDecoder. |
| 39 return nullptr; |
| 40 } |
| 41 } |
| 42 |
| 43 SkAndroidCodec* SkAndroidCodec::NewFromData(SkData* data) { |
| 44 if (!data) { |
| 45 return nullptr; |
| 46 } |
| 47 |
| 48 return NewFromStream(new SkMemoryStream(data)); |
| 49 } |
| 50 |
| 51 SkISize SkAndroidCodec::getSampledDimensions(int sampleSize) const { |
| 52 if (!is_valid_sample_size(sampleSize)) { |
| 53 return SkISize::Make(0, 0); |
| 54 } |
| 55 |
| 56 return this->onGetSampledDimensions(sampleSize); |
| 57 } |
| 58 |
| 59 bool SkAndroidCodec::getSupportedSubset(SkIRect* desiredSubset) const { |
| 60 if (!desiredSubset || !is_valid_subset(*desiredSubset, fInfo.dimensions()))
{ |
| 61 return false; |
| 62 } |
| 63 |
| 64 return this->onGetSupportedSubset(desiredSubset); |
| 65 } |
| 66 |
| 67 SkISize SkAndroidCodec::getSampledSubsetDimensions(int sampleSize, const SkIRect
& subset) const { |
| 68 if (!is_valid_sample_size(sampleSize)) { |
| 69 return SkISize::Make(0, 0); |
| 70 } |
| 71 |
| 72 // We require that the input subset is a subset that is supported by SkAndro
idCodec. |
| 73 // We test this by calling getSupportedSubset() and verifying that no modifi
cations |
| 74 // are made to the subset. |
| 75 SkIRect copySubset = subset; |
| 76 if (!this->getSupportedSubset(©Subset) || copySubset != subset) { |
| 77 return SkISize::Make(0, 0); |
| 78 } |
| 79 |
| 80 // If the subset is the entire image, for consistency, use onGetSampledDimen
sions(). |
| 81 if (fInfo.dimensions() == subset.size()) { |
| 82 return onGetSampledDimensions(sampleSize); |
| 83 } |
| 84 |
| 85 // This should perhaps call a virtual function, but currently both of our su
bclasses |
| 86 // want the same implementation. |
| 87 return SkISize::Make(get_scaled_dimension(subset.width(), sampleSize), |
| 88 get_scaled_dimension(subset.height(), sampleSize)); |
| 89 } |
| 90 |
| 91 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void*
pixels, |
| 92 size_t rowBytes, AndroidOptions* options) { |
| 93 if (!pixels) { |
| 94 return SkCodec::kInvalidParameters; |
| 95 } |
| 96 if (rowBytes < info.minRowBytes()) { |
| 97 return SkCodec::kInvalidParameters; |
| 98 } |
| 99 |
| 100 AndroidOptions defaultOptions; |
| 101 if (!options) { |
| 102 options = &defaultOptions; |
| 103 } else if (options->fSubset) { |
| 104 if (!is_valid_subset(*options->fSubset, fInfo.dimensions())) { |
| 105 return SkCodec::kInvalidParameters; |
| 106 } |
| 107 } |
| 108 |
| 109 return this->onGetAndroidPixels(info, pixels, rowBytes, *options); |
| 110 } |
| 111 |
| 112 SkCodec::Result SkAndroidCodec::getAndroidPixels(const SkImageInfo& info, void*
pixels, |
| 113 size_t rowBytes) { |
| 114 return this->getAndroidPixels(info, pixels, rowBytes, nullptr); |
| 115 } |
OLD | NEW |