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