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 | #ifndef SkAndroidCodec_DEFINED |
| 9 | #define SkAndroidCodec_DEFINED |
| 10 | |
| 11 | #include "SkCodec.h" |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 12 | #include "SkEncodedImageFormat.h" |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 13 | #include "SkStream.h" |
| 14 | #include "SkTypes.h" |
| 15 | |
| 16 | /** |
| 17 | * Abstract interface defining image codec functionality that is necessary for |
| 18 | * Android. |
| 19 | */ |
| 20 | class SkAndroidCodec : SkNoncopyable { |
| 21 | public: |
| 22 | /** |
| 23 | * If this stream represents an encoded image that we know how to decode, |
| 24 | * return an SkAndroidCodec that can decode it. Otherwise return NULL. |
| 25 | * |
msarett | 7d5105c | 2015-12-02 07:02:41 -0800 | [diff] [blame] | 26 | * The SkPngChunkReader handles unknown chunks in PNGs. |
| 27 | * See SkCodec.h for more details. |
| 28 | * |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 29 | * If NULL is returned, the stream is deleted immediately. Otherwise, the |
| 30 | * SkCodec takes ownership of it, and will delete it when done with it. |
| 31 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame^] | 32 | static std::unique_ptr<SkAndroidCodec> MakeFromStream(std::unique_ptr<SkStream>, |
| 33 | SkPngChunkReader* = nullptr); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * If this data represents an encoded image that we know how to decode, |
| 37 | * return an SkAndroidCodec that can decode it. Otherwise return NULL. |
| 38 | * |
msarett | 7d5105c | 2015-12-02 07:02:41 -0800 | [diff] [blame] | 39 | * The SkPngChunkReader handles unknown chunks in PNGs. |
| 40 | * See SkCodec.h for more details. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 41 | */ |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame^] | 42 | static std::unique_ptr<SkAndroidCodec> MakeFromData(sk_sp<SkData>, SkPngChunkReader* = nullptr); |
| 43 | |
| 44 | #ifdef SK_SUPPORT_LEGACY_CODEC_NEW |
| 45 | static SkAndroidCodec* NewFromStream(SkStream* stream, SkPngChunkReader* reader) { |
| 46 | return MakeFromStream(std::unique_ptr<SkStream>(stream), reader).release(); |
| 47 | } |
| 48 | static SkAndroidCodec* NewFromData(sk_sp<SkData> data, SkPngChunkReader* reader) { |
| 49 | return MakeFromData(std::move(data), reader).release(); |
| 50 | } |
reed | 42943c8 | 2016-09-12 12:01:44 -0700 | [diff] [blame] | 51 | static SkAndroidCodec* NewFromData(SkData* data, SkPngChunkReader* reader) { |
| 52 | return NewFromData(sk_ref_sp(data), reader); |
| 53 | } |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame^] | 54 | #endif |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 55 | |
| 56 | virtual ~SkAndroidCodec() {} |
| 57 | |
| 58 | |
| 59 | const SkImageInfo& getInfo() const { return fInfo; } |
| 60 | |
| 61 | /** |
| 62 | * Format of the encoded data. |
| 63 | */ |
Hal Canary | 1fcc404 | 2016-11-30 17:07:59 -0500 | [diff] [blame] | 64 | SkEncodedImageFormat getEncodedFormat() const { return fCodec->getEncodedFormat(); } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 67 | * @param requestedColorType Color type requested by the client |
| 68 | * |
Matt Sarett | 8dcc84f | 2016-12-14 10:23:41 -0500 | [diff] [blame] | 69 | * |requestedColorType| may be overriden. We will default to kF16 |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 70 | * for high precision images. |
Matt Sarett | 8dcc84f | 2016-12-14 10:23:41 -0500 | [diff] [blame] | 71 | * |
| 72 | * In the general case, if it is possible to decode to |
| 73 | * |requestedColorType|, this returns |requestedColorType|. |
| 74 | * Otherwise, this returns a color type that is an appropriate |
| 75 | * match for the the encoded data. |
msarett | 9a0e346 | 2015-12-11 07:38:50 -0800 | [diff] [blame] | 76 | */ |
| 77 | SkColorType computeOutputColorType(SkColorType requestedColorType); |
| 78 | |
| 79 | /** |
| 80 | * @param requestedUnpremul Indicates if the client requested |
| 81 | * unpremultiplied output |
| 82 | * |
| 83 | * Returns the appropriate alpha type to decode to. If the image |
| 84 | * has alpha, the value of requestedUnpremul will be honored. |
| 85 | */ |
| 86 | SkAlphaType computeOutputAlphaType(bool requestedUnpremul); |
| 87 | |
| 88 | /** |
Matt Sarett | 68feef4 | 2017-04-11 09:51:32 -0400 | [diff] [blame] | 89 | * @param outputColorType Color type that the client will decode to. |
| 90 | * @param prefColorSpace Preferred color space to decode to. |
| 91 | * This may not return |prefColorSpace| for a couple reasons. |
| 92 | * (1) Android Principles: 565 must be sRGB, F16 must be |
| 93 | * linear sRGB, transfer function must be parametric. |
| 94 | * (2) Codec Limitations: F16 requires a linear color space. |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 95 | * |
| 96 | * Returns the appropriate color space to decode to. |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 97 | */ |
Matt Sarett | 68feef4 | 2017-04-11 09:51:32 -0400 | [diff] [blame] | 98 | sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType, |
| 99 | sk_sp<SkColorSpace> prefColorSpace = nullptr); |
Matt Sarett | 966bb34 | 2016-12-12 16:30:13 -0500 | [diff] [blame] | 100 | |
| 101 | /** |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 102 | * Returns the dimensions of the scaled output image, for an input |
| 103 | * sampleSize. |
| 104 | * |
| 105 | * When the sample size divides evenly into the original dimensions, the |
| 106 | * scaled output dimensions will simply be equal to the original |
| 107 | * dimensions divided by the sample size. |
| 108 | * |
| 109 | * When the sample size does not divide even into the original |
| 110 | * dimensions, the codec may round up or down, depending on what is most |
| 111 | * efficient to decode. |
| 112 | * |
| 113 | * Finally, the codec will always recommend a non-zero output, so the output |
| 114 | * dimension will always be one if the sampleSize is greater than the |
| 115 | * original dimension. |
| 116 | */ |
| 117 | SkISize getSampledDimensions(int sampleSize) const; |
| 118 | |
| 119 | /** |
| 120 | * Return (via desiredSubset) a subset which can decoded from this codec, |
| 121 | * or false if the input subset is invalid. |
| 122 | * |
| 123 | * @param desiredSubset in/out parameter |
| 124 | * As input, a desired subset of the original bounds |
| 125 | * (as specified by getInfo). |
| 126 | * As output, if true is returned, desiredSubset may |
| 127 | * have been modified to a subset which is |
| 128 | * supported. Although a particular change may have |
| 129 | * been made to desiredSubset to create something |
| 130 | * supported, it is possible other changes could |
| 131 | * result in a valid subset. If false is returned, |
| 132 | * desiredSubset's value is undefined. |
| 133 | * @return true If the input desiredSubset is valid. |
| 134 | * desiredSubset may be modified to a subset |
| 135 | * supported by the codec. |
| 136 | * false If desiredSubset is invalid (NULL or not fully |
| 137 | * contained within the image). |
| 138 | */ |
| 139 | bool getSupportedSubset(SkIRect* desiredSubset) const; |
| 140 | // TODO: Rename SkCodec::getValidSubset() to getSupportedSubset() |
| 141 | |
| 142 | /** |
| 143 | * Returns the dimensions of the scaled, partial output image, for an |
| 144 | * input sampleSize and subset. |
| 145 | * |
| 146 | * @param sampleSize Factor to scale down by. |
| 147 | * @param subset Must be a valid subset of the original image |
| 148 | * dimensions and a subset supported by SkAndroidCodec. |
| 149 | * getSubset() can be used to obtain a subset supported |
| 150 | * by SkAndroidCodec. |
| 151 | * @return Size of the scaled partial image. Or zero size |
| 152 | * if either of the inputs is invalid. |
| 153 | */ |
| 154 | SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const; |
| 155 | |
| 156 | /** |
| 157 | * Additional options to pass to getAndroidPixels(). |
| 158 | */ |
| 159 | // FIXME: It's a bit redundant to name these AndroidOptions when this class is already |
| 160 | // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call |
| 161 | // these Options when SkCodec has a slightly different set of Options. Maybe these |
| 162 | // should be DecodeOptions or SamplingOptions? |
| 163 | struct AndroidOptions { |
| 164 | AndroidOptions() |
| 165 | : fZeroInitialized(SkCodec::kNo_ZeroInitialized) |
| 166 | , fSubset(nullptr) |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 167 | , fSampleSize(1) |
| 168 | {} |
| 169 | |
| 170 | /** |
| 171 | * Indicates is destination pixel memory is zero initialized. |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 172 | * |
| 173 | * The default is SkCodec::kNo_ZeroInitialized. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 174 | */ |
| 175 | SkCodec::ZeroInitialized fZeroInitialized; |
| 176 | |
| 177 | /** |
| 178 | * If not NULL, represents a subset of the original image to decode. |
| 179 | * |
| 180 | * Must be within the bounds returned by getInfo(). |
| 181 | * |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 182 | * If the EncodedFormat is SkEncodedImageFormat::kWEBP, the top and left |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 183 | * values must be even. |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 184 | * |
| 185 | * The default is NULL, meaning a decode of the entire image. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 186 | */ |
| 187 | SkIRect* fSubset; |
| 188 | |
| 189 | /** |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 190 | * The client may provide an integer downscale factor for the decode. |
| 191 | * The codec may implement this downscaling by sampling or another |
| 192 | * method if it is more efficient. |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 193 | * |
| 194 | * The default is 1, representing no downscaling. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 195 | */ |
| 196 | int fSampleSize; |
| 197 | }; |
| 198 | |
| 199 | /** |
| 200 | * Decode into the given pixels, a block of memory of size at |
| 201 | * least (info.fHeight - 1) * rowBytes + (info.fWidth * |
| 202 | * bytesPerPixel) |
| 203 | * |
| 204 | * Repeated calls to this function should give the same results, |
| 205 | * allowing the PixelRef to be immutable. |
| 206 | * |
| 207 | * @param info A description of the format (config, size) |
| 208 | * expected by the caller. This can simply be identical |
| 209 | * to the info returned by getInfo(). |
| 210 | * |
| 211 | * This contract also allows the caller to specify |
| 212 | * different output-configs, which the implementation can |
| 213 | * decide to support or not. |
| 214 | * |
| 215 | * A size that does not match getInfo() implies a request |
| 216 | * to scale or subset. If the codec cannot perform this |
| 217 | * scaling or subsetting, it will return an error code. |
| 218 | * |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 219 | * The AndroidOptions object is also used to specify any requested scaling or subsetting |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 220 | * using options->fSampleSize and options->fSubset. If NULL, the defaults (as specified above |
| 221 | * for AndroidOptions) are used. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 222 | * |
| 223 | * @return Result kSuccess, or another value explaining the type of failure. |
| 224 | */ |
| 225 | // FIXME: It's a bit redundant to name this getAndroidPixels() when this class is already |
| 226 | // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call |
| 227 | // this getPixels() when it is a slightly different API than SkCodec's getPixels(). |
| 228 | // Maybe this should be decode() or decodeSubset()? |
| 229 | SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
scroggo | e95a068 | 2015-11-04 04:31:12 -0800 | [diff] [blame] | 230 | const AndroidOptions* options); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 231 | |
| 232 | /** |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 233 | * Simplified version of getAndroidPixels() where we supply the default AndroidOptions as |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 234 | * specified above for AndroidOptions. It will not perform any scaling or subsetting. |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 235 | */ |
| 236 | SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes); |
| 237 | |
scroggo | 7b5e553 | 2016-02-04 06:14:24 -0800 | [diff] [blame] | 238 | SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 239 | return this->getAndroidPixels(info, pixels, rowBytes); |
| 240 | } |
| 241 | |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 242 | protected: |
| 243 | |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 244 | SkAndroidCodec(SkCodec*); |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 245 | |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 246 | SkCodec* codec() const { return fCodec.get(); } |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 247 | |
| 248 | virtual SkISize onGetSampledDimensions(int sampleSize) const = 0; |
| 249 | |
| 250 | virtual bool onGetSupportedSubset(SkIRect* desiredSubset) const = 0; |
| 251 | |
| 252 | virtual SkCodec::Result onGetAndroidPixels(const SkImageInfo& info, void* pixels, |
scroggo | e95a068 | 2015-11-04 04:31:12 -0800 | [diff] [blame] | 253 | size_t rowBytes, const AndroidOptions& options) = 0; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 254 | |
| 255 | private: |
| 256 | |
| 257 | // This will always be a reference to the info that is contained by the |
| 258 | // embedded SkCodec. |
| 259 | const SkImageInfo& fInfo; |
msarett | 90c4d5f | 2015-12-10 13:09:24 -0800 | [diff] [blame] | 260 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 261 | std::unique_ptr<SkCodec> fCodec; |
msarett | 3d9d7a7 | 2015-10-21 10:27:10 -0700 | [diff] [blame] | 262 | }; |
| 263 | #endif // SkAndroidCodec_DEFINED |