blob: c361f8ac50ebef0abfd232e2bbf3abf9531bb513 [file] [log] [blame]
msarett3d9d7a72015-10-21 10:27:10 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/codec/SkCodec.h"
12#include "include/core/SkEncodedImageFormat.h"
13#include "include/core/SkStream.h"
14#include "include/core/SkTypes.h"
msarett3d9d7a72015-10-21 10:27:10 -070015
16/**
17 * Abstract interface defining image codec functionality that is necessary for
18 * Android.
19 */
Derek Sollenberger2fbf1bc2017-09-20 15:51:08 -040020class SK_API SkAndroidCodec : SkNoncopyable {
msarett3d9d7a72015-10-21 10:27:10 -070021public:
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050022 enum class ExifOrientationBehavior {
23 /**
24 * Ignore any exif orientation markers in the data.
25 *
26 * getInfo's width and height will match the header of the image, and
27 * no processing will be done to match the marker.
28 */
29 kIgnore,
30
31 /**
32 * Respect the exif orientation marker.
33 *
34 * getInfo's width and height will represent what they should be after
35 * applying the orientation. For example, if the marker specifies a
36 * rotation by 90 degrees, they will be swapped relative to the header.
37 * getAndroidPixels will apply the orientation as well.
38 */
39 kRespect,
40 };
41
msarett3d9d7a72015-10-21 10:27:10 -070042 /**
Leon Scroggins III7397d7a2018-01-04 13:26:30 -050043 * Pass ownership of an SkCodec to a newly-created SkAndroidCodec.
44 */
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050045 static std::unique_ptr<SkAndroidCodec> MakeFromCodec(std::unique_ptr<SkCodec>,
46 ExifOrientationBehavior = ExifOrientationBehavior::kIgnore);
Leon Scroggins III7397d7a2018-01-04 13:26:30 -050047
48 /**
msarett3d9d7a72015-10-21 10:27:10 -070049 * If this stream represents an encoded image that we know how to decode,
50 * return an SkAndroidCodec that can decode it. Otherwise return NULL.
51 *
msarett7d5105c2015-12-02 07:02:41 -080052 * The SkPngChunkReader handles unknown chunks in PNGs.
53 * See SkCodec.h for more details.
54 *
msarett3d9d7a72015-10-21 10:27:10 -070055 * If NULL is returned, the stream is deleted immediately. Otherwise, the
56 * SkCodec takes ownership of it, and will delete it when done with it.
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050057 *
58 * ExifOrientationBehavior is set to kIgnore.
msarett3d9d7a72015-10-21 10:27:10 -070059 */
Mike Reedede7bac2017-07-23 15:30:02 -040060 static std::unique_ptr<SkAndroidCodec> MakeFromStream(std::unique_ptr<SkStream>,
61 SkPngChunkReader* = nullptr);
msarett3d9d7a72015-10-21 10:27:10 -070062
63 /**
64 * If this data represents an encoded image that we know how to decode,
65 * return an SkAndroidCodec that can decode it. Otherwise return NULL.
66 *
msarett7d5105c2015-12-02 07:02:41 -080067 * The SkPngChunkReader handles unknown chunks in PNGs.
68 * See SkCodec.h for more details.
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050069 *
70 * ExifOrientationBehavior is set to kIgnore.
msarett3d9d7a72015-10-21 10:27:10 -070071 */
Mike Reedede7bac2017-07-23 15:30:02 -040072 static std::unique_ptr<SkAndroidCodec> MakeFromData(sk_sp<SkData>, SkPngChunkReader* = nullptr);
73
Leon Scroggins III07418182017-08-15 12:24:02 -040074 virtual ~SkAndroidCodec();
msarett3d9d7a72015-10-21 10:27:10 -070075
msarett3d9d7a72015-10-21 10:27:10 -070076 const SkImageInfo& getInfo() const { return fInfo; }
77
78 /**
Leon Scroggins III22d08b52020-01-30 16:03:28 -050079 * Return the ICC profile of the encoded data.
80 */
81 const skcms_ICCProfile* getICCProfile() const {
82 return fCodec->getEncodedInfo().profile();
83 }
84
85 /**
msarett3d9d7a72015-10-21 10:27:10 -070086 * Format of the encoded data.
87 */
Hal Canary1fcc4042016-11-30 17:07:59 -050088 SkEncodedImageFormat getEncodedFormat() const { return fCodec->getEncodedFormat(); }
msarett3d9d7a72015-10-21 10:27:10 -070089
90 /**
msarett9a0e3462015-12-11 07:38:50 -080091 * @param requestedColorType Color type requested by the client
92 *
Matt Sarett8dcc84f2016-12-14 10:23:41 -050093 * |requestedColorType| may be overriden. We will default to kF16
Leon Scroggins571b30f2017-07-11 17:35:31 +000094 * for high precision images.
Matt Sarett8dcc84f2016-12-14 10:23:41 -050095 *
96 * In the general case, if it is possible to decode to
97 * |requestedColorType|, this returns |requestedColorType|.
98 * Otherwise, this returns a color type that is an appropriate
99 * match for the the encoded data.
msarett9a0e3462015-12-11 07:38:50 -0800100 */
101 SkColorType computeOutputColorType(SkColorType requestedColorType);
102
103 /**
104 * @param requestedUnpremul Indicates if the client requested
105 * unpremultiplied output
106 *
107 * Returns the appropriate alpha type to decode to. If the image
108 * has alpha, the value of requestedUnpremul will be honored.
109 */
110 SkAlphaType computeOutputAlphaType(bool requestedUnpremul);
111
112 /**
Matt Sarett68feef42017-04-11 09:51:32 -0400113 * @param outputColorType Color type that the client will decode to.
114 * @param prefColorSpace Preferred color space to decode to.
115 * This may not return |prefColorSpace| for a couple reasons.
116 * (1) Android Principles: 565 must be sRGB, F16 must be
117 * linear sRGB, transfer function must be parametric.
118 * (2) Codec Limitations: F16 requires a linear color space.
Matt Sarett966bb342016-12-12 16:30:13 -0500119 *
120 * Returns the appropriate color space to decode to.
Matt Sarett966bb342016-12-12 16:30:13 -0500121 */
Matt Sarett68feef42017-04-11 09:51:32 -0400122 sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
123 sk_sp<SkColorSpace> prefColorSpace = nullptr);
Matt Sarett966bb342016-12-12 16:30:13 -0500124
125 /**
Leon Scroggins III07a722c2018-01-16 15:01:17 -0500126 * Compute the appropriate sample size to get to |size|.
127 *
128 * @param size As an input parameter, the desired output size of
129 * the decode. As an output parameter, the smallest sampled size
130 * larger than the input.
131 * @return the sample size to set AndroidOptions::fSampleSize to decode
132 * to the output |size|.
133 */
134 int computeSampleSize(SkISize* size) const;
135
136 /**
msarett3d9d7a72015-10-21 10:27:10 -0700137 * Returns the dimensions of the scaled output image, for an input
138 * sampleSize.
139 *
140 * When the sample size divides evenly into the original dimensions, the
141 * scaled output dimensions will simply be equal to the original
142 * dimensions divided by the sample size.
143 *
144 * When the sample size does not divide even into the original
145 * dimensions, the codec may round up or down, depending on what is most
146 * efficient to decode.
147 *
148 * Finally, the codec will always recommend a non-zero output, so the output
149 * dimension will always be one if the sampleSize is greater than the
150 * original dimension.
151 */
152 SkISize getSampledDimensions(int sampleSize) const;
153
154 /**
155 * Return (via desiredSubset) a subset which can decoded from this codec,
156 * or false if the input subset is invalid.
157 *
158 * @param desiredSubset in/out parameter
159 * As input, a desired subset of the original bounds
160 * (as specified by getInfo).
161 * As output, if true is returned, desiredSubset may
162 * have been modified to a subset which is
163 * supported. Although a particular change may have
164 * been made to desiredSubset to create something
165 * supported, it is possible other changes could
166 * result in a valid subset. If false is returned,
167 * desiredSubset's value is undefined.
168 * @return true If the input desiredSubset is valid.
169 * desiredSubset may be modified to a subset
170 * supported by the codec.
171 * false If desiredSubset is invalid (NULL or not fully
172 * contained within the image).
173 */
174 bool getSupportedSubset(SkIRect* desiredSubset) const;
175 // TODO: Rename SkCodec::getValidSubset() to getSupportedSubset()
176
177 /**
178 * Returns the dimensions of the scaled, partial output image, for an
179 * input sampleSize and subset.
180 *
181 * @param sampleSize Factor to scale down by.
182 * @param subset Must be a valid subset of the original image
183 * dimensions and a subset supported by SkAndroidCodec.
184 * getSubset() can be used to obtain a subset supported
185 * by SkAndroidCodec.
186 * @return Size of the scaled partial image. Or zero size
187 * if either of the inputs is invalid.
188 */
189 SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const;
190
191 /**
192 * Additional options to pass to getAndroidPixels().
193 */
194 // FIXME: It's a bit redundant to name these AndroidOptions when this class is already
195 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
196 // these Options when SkCodec has a slightly different set of Options. Maybe these
197 // should be DecodeOptions or SamplingOptions?
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500198 struct AndroidOptions : public SkCodec::Options {
msarett3d9d7a72015-10-21 10:27:10 -0700199 AndroidOptions()
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500200 : SkCodec::Options()
msarett3d9d7a72015-10-21 10:27:10 -0700201 , fSampleSize(1)
202 {}
203
204 /**
msarett3d9d7a72015-10-21 10:27:10 -0700205 * The client may provide an integer downscale factor for the decode.
206 * The codec may implement this downscaling by sampling or another
207 * method if it is more efficient.
scroggo7b5e5532016-02-04 06:14:24 -0800208 *
209 * The default is 1, representing no downscaling.
msarett3d9d7a72015-10-21 10:27:10 -0700210 */
211 int fSampleSize;
212 };
213
214 /**
215 * Decode into the given pixels, a block of memory of size at
216 * least (info.fHeight - 1) * rowBytes + (info.fWidth *
217 * bytesPerPixel)
218 *
219 * Repeated calls to this function should give the same results,
220 * allowing the PixelRef to be immutable.
221 *
222 * @param info A description of the format (config, size)
223 * expected by the caller. This can simply be identical
224 * to the info returned by getInfo().
225 *
226 * This contract also allows the caller to specify
227 * different output-configs, which the implementation can
228 * decide to support or not.
229 *
230 * A size that does not match getInfo() implies a request
231 * to scale or subset. If the codec cannot perform this
232 * scaling or subsetting, it will return an error code.
233 *
msarett3d9d7a72015-10-21 10:27:10 -0700234 * The AndroidOptions object is also used to specify any requested scaling or subsetting
scroggo7b5e5532016-02-04 06:14:24 -0800235 * using options->fSampleSize and options->fSubset. If NULL, the defaults (as specified above
236 * for AndroidOptions) are used.
msarett3d9d7a72015-10-21 10:27:10 -0700237 *
238 * @return Result kSuccess, or another value explaining the type of failure.
239 */
240 // FIXME: It's a bit redundant to name this getAndroidPixels() when this class is already
241 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
242 // this getPixels() when it is a slightly different API than SkCodec's getPixels().
243 // Maybe this should be decode() or decodeSubset()?
244 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
scroggoe95a0682015-11-04 04:31:12 -0800245 const AndroidOptions* options);
msarett3d9d7a72015-10-21 10:27:10 -0700246
247 /**
scroggo7b5e5532016-02-04 06:14:24 -0800248 * Simplified version of getAndroidPixels() where we supply the default AndroidOptions as
Leon Scroggins571b30f2017-07-11 17:35:31 +0000249 * specified above for AndroidOptions. It will not perform any scaling or subsetting.
msarett3d9d7a72015-10-21 10:27:10 -0700250 */
251 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
252
scroggo7b5e5532016-02-04 06:14:24 -0800253 SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
254 return this->getAndroidPixels(info, pixels, rowBytes);
255 }
256
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500257 SkCodec* codec() const { return fCodec.get(); }
258
msarett3d9d7a72015-10-21 10:27:10 -0700259protected:
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500260 SkAndroidCodec(SkCodec*, ExifOrientationBehavior = ExifOrientationBehavior::kIgnore);
msarett3d9d7a72015-10-21 10:27:10 -0700261
msarett3d9d7a72015-10-21 10:27:10 -0700262 virtual SkISize onGetSampledDimensions(int sampleSize) const = 0;
263
264 virtual bool onGetSupportedSubset(SkIRect* desiredSubset) const = 0;
265
266 virtual SkCodec::Result onGetAndroidPixels(const SkImageInfo& info, void* pixels,
scroggoe95a0682015-11-04 04:31:12 -0800267 size_t rowBytes, const AndroidOptions& options) = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700268
269private:
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500270 const SkImageInfo fInfo;
271 const ExifOrientationBehavior fOrientationBehavior;
272 std::unique_ptr<SkCodec> fCodec;
Leon Scroggins7d1153f2020-11-06 17:05:36 -0500273
274 friend class SkAnimatedImage;
msarett3d9d7a72015-10-21 10:27:10 -0700275};
276#endif // SkAndroidCodec_DEFINED