Debian Codegraph @

include/codec/SkAndroidCodec.h

raw
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 "include/codec/SkCodec.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkImageInfo.h"
14 #include "include/core/SkRefCnt.h"
15 #include "include/core/SkSize.h"
16 #include "include/core/SkTypes.h"
17 #include "include/private/SkEncodedInfo.h"
18 #include "include/private/base/SkNoncopyable.h"
19 #include "modules/skcms/skcms.h"
20
21 // TODO(kjlubick, bungeman) Replace these includes with forward declares
22 #include "include/codec/SkEncodedImageFormat.h" // IWYU pragma: keep
23 #include "include/core/SkAlphaType.h" // IWYU pragma: keep
24 #include "include/core/SkColorType.h" // IWYU pragma: keep
25
26 #include <cstddef>
27 #include <memory>
28
29 class SkData;
30 class SkPngChunkReader;
31 class SkStream;
32 struct SkGainmapInfo;
33 struct SkIRect;
34
35 /**
36 * Abstract interface defining image codec functionality that is necessary for
37 * Android.
38 */
39 class SK_API SkAndroidCodec : SkNoncopyable {
40 public:
41 /**
42 * Deprecated.
43 *
44 * Now that SkAndroidCodec supports multiframe images, there are multiple
45 * ways to handle compositing an oriented frame on top of an oriented frame
46 * with different tradeoffs. SkAndroidCodec now ignores the orientation and
47 * forces the client to handle it.
48 */
49 enum class ExifOrientationBehavior {
50 kIgnore,
51 kRespect,
52 };
53
54 /**
55 * Pass ownership of an SkCodec to a newly-created SkAndroidCodec.
56 */
57 static std::unique_ptr<SkAndroidCodec> MakeFromCodec(std::unique_ptr<SkCodec>);
58
59 /**
60 * If this stream represents an encoded image that we know how to decode,
61 * return an SkAndroidCodec that can decode it. Otherwise return NULL.
62 *
63 * The SkPngChunkReader handles unknown chunks in PNGs.
64 * See SkCodec.h for more details.
65 *
66 * If NULL is returned, the stream is deleted immediately. Otherwise, the
67 * SkCodec takes ownership of it, and will delete it when done with it.
68 */
69 static std::unique_ptr<SkAndroidCodec> MakeFromStream(std::unique_ptr<SkStream>,
70 SkPngChunkReader* = nullptr);
71
72 /**
73 * If this data represents an encoded image that we know how to decode,
74 * return an SkAndroidCodec that can decode it. Otherwise return NULL.
75 *
76 * The SkPngChunkReader handles unknown chunks in PNGs.
77 * See SkCodec.h for more details.
78 */
79 static std::unique_ptr<SkAndroidCodec> MakeFromData(sk_sp<const SkData>,
80 SkPngChunkReader* = nullptr);
81
82 virtual ~SkAndroidCodec();
83
84 // TODO: fInfo is now just a cache of SkCodec's SkImageInfo. No need to
85 // cache and return a reference here, once Android call-sites are updated.
86 const SkImageInfo& getInfo() const { return fInfo; }
87
88 /**
89 * Return the ICC profile of the encoded data.
90 */
91 const skcms_ICCProfile* getICCProfile() const {
92 return fCodec->getEncodedInfo().profile();
93 }
94
95 /**
96 * Format of the encoded data.
97 */
98 SkEncodedImageFormat getEncodedFormat() const { return fCodec->getEncodedFormat(); }
99
100 /**
101 * @param requestedColorType Color type requested by the client
102 *
103 * |requestedColorType| may be overriden. We will default to kF16
104 * for high precision images.
105 *
106 * In the general case, if it is possible to decode to
107 * |requestedColorType|, this returns |requestedColorType|.
108 * Otherwise, this returns a color type that is an appropriate
109 * match for the the encoded data.
110 */
111 SkColorType computeOutputColorType(SkColorType requestedColorType);
112
113 /**
114 * @param requestedUnpremul Indicates if the client requested
115 * unpremultiplied output
116 *
117 * Returns the appropriate alpha type to decode to. If the image
118 * has alpha, the value of requestedUnpremul will be honored.
119 */
120 SkAlphaType computeOutputAlphaType(bool requestedUnpremul);
121
122 /**
123 * @param outputColorType Color type that the client will decode to.
124 * @param prefColorSpace Preferred color space to decode to.
125 * This may not return |prefColorSpace| for
126 * specific color types.
127 *
128 * Returns the appropriate color space to decode to.
129 */
130 sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
131 sk_sp<SkColorSpace> prefColorSpace = nullptr);
132
133 /**
134 * Compute the appropriate sample size to get to |size|.
135 *
136 * @param size As an input parameter, the desired output size of
137 * the decode. As an output parameter, the smallest sampled size
138 * larger than the input.
139 * @return the sample size to set AndroidOptions::fSampleSize to decode
140 * to the output |size|.
141 */
142 int computeSampleSize(SkISize* size) const;
143
144 /**
145 * Returns the dimensions of the scaled output image, for an input
146 * sampleSize.
147 *
148 * When the sample size divides evenly into the original dimensions, the
149 * scaled output dimensions will simply be equal to the original
150 * dimensions divided by the sample size.
151 *
152 * When the sample size does not divide even into the original
153 * dimensions, the codec may round up or down, depending on what is most
154 * efficient to decode.
155 *
156 * Finally, the codec will always recommend a non-zero output, so the output
157 * dimension will always be one if the sampleSize is greater than the
158 * original dimension.
159 */
160 SkISize getSampledDimensions(int sampleSize) const;
161
162 /**
163 * Return (via desiredSubset) a subset which can decoded from this codec,
164 * or false if the input subset is invalid.
165 *
166 * @param desiredSubset in/out parameter
167 * As input, a desired subset of the original bounds
168 * (as specified by getInfo).
169 * As output, if true is returned, desiredSubset may
170 * have been modified to a subset which is
171 * supported. Although a particular change may have
172 * been made to desiredSubset to create something
173 * supported, it is possible other changes could
174 * result in a valid subset. If false is returned,
175 * desiredSubset's value is undefined.
176 * @return true If the input desiredSubset is valid.
177 * desiredSubset may be modified to a subset
178 * supported by the codec.
179 * false If desiredSubset is invalid (NULL or not fully
180 * contained within the image).
181 */
182 bool getSupportedSubset(SkIRect* desiredSubset) const;
183 // TODO: Rename SkCodec::getValidSubset() to getSupportedSubset()
184
185 /**
186 * Returns the dimensions of the scaled, partial output image, for an
187 * input sampleSize and subset.
188 *
189 * @param sampleSize Factor to scale down by.
190 * @param subset Must be a valid subset of the original image
191 * dimensions and a subset supported by SkAndroidCodec.
192 * getSubset() can be used to obtain a subset supported
193 * by SkAndroidCodec.
194 * @return Size of the scaled partial image. Or zero size
195 * if either of the inputs is invalid.
196 */
197 SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const;
198
199 /**
200 * Additional options to pass to getAndroidPixels().
201 */
202 // FIXME: It's a bit redundant to name these AndroidOptions when this class is already
203 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
204 // these Options when SkCodec has a slightly different set of Options. Maybe these
205 // should be DecodeOptions or SamplingOptions?
206 struct AndroidOptions : public SkCodec::Options {
207 AndroidOptions()
208 : SkCodec::Options()
209 , fSampleSize(1)
210 {}
211
212 /**
213 * The client may provide an integer downscale factor for the decode.
214 * The codec may implement this downscaling by sampling or another
215 * method if it is more efficient.
216 *
217 * The default is 1, representing no downscaling.
218 */
219 int fSampleSize;
220 };
221
222 /**
223 * Decode into the given pixels, a block of memory of size at
224 * least (info.fHeight - 1) * rowBytes + (info.fWidth *
225 * bytesPerPixel)
226 *
227 * Repeated calls to this function should give the same results,
228 * allowing the PixelRef to be immutable.
229 *
230 * @param info A description of the format (config, size)
231 * expected by the caller. This can simply be identical
232 * to the info returned by getInfo().
233 *
234 * This contract also allows the caller to specify
235 * different output-configs, which the implementation can
236 * decide to support or not.
237 *
238 * A size that does not match getInfo() implies a request
239 * to scale or subset. If the codec cannot perform this
240 * scaling or subsetting, it will return an error code.
241 *
242 * The AndroidOptions object is also used to specify any requested scaling or subsetting
243 * using options->fSampleSize and options->fSubset. If NULL, the defaults (as specified above
244 * for AndroidOptions) are used.
245 *
246 * @return Result kSuccess, or another value explaining the type of failure.
247 */
248 // FIXME: It's a bit redundant to name this getAndroidPixels() when this class is already
249 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
250 // this getPixels() when it is a slightly different API than SkCodec's getPixels().
251 // Maybe this should be decode() or decodeSubset()?
252 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
253 const AndroidOptions* options);
254
255 /**
256 * Simplified version of getAndroidPixels() where we supply the default AndroidOptions as
257 * specified above for AndroidOptions. It will not perform any scaling or subsetting.
258 */
259 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
260
261 SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
262 return this->getAndroidPixels(info, pixels, rowBytes);
263 }
264
265 SkCodec* codec() const { return fCodec.get(); }
266
267 /**
268 * Retrieve the gainmap codec for an image.
269 *
270 * @param outInfo On success, this is populated with the parameters for
271 * rendering this gainmap. This parameter must be non-nullptr.
272 *
273 * @param outGainmapCodec On success, this is populated with a codec from which the
274 * gainmap image may be decoded. This parameter is optional, and
275 * may be set to nullptr.
276 *
277 * @return If this has a gainmap image and that gainmap image was
278 * successfully extracted then return true. Otherwise return
279 * false.
280 */
281 bool getGainmapAndroidCodec(SkGainmapInfo* outInfo, std::unique_ptr<SkAndroidCodec>* outCodec);
282
283 // TODO(issues.skia.org/363544350): This API only works for JPEG images. Remove this API once
284 // it is no longer used.
285 bool getAndroidGainmap(SkGainmapInfo* outInfo,
286 std::unique_ptr<SkStream>* outGainmapImageStream);
287
288 protected:
289 explicit SkAndroidCodec(SkCodec*);
290
291 virtual SkISize onGetSampledDimensions(int sampleSize) const = 0;
292
293 virtual bool onGetSupportedSubset(SkIRect* desiredSubset) const = 0;
294
295 virtual SkCodec::Result onGetAndroidPixels(const SkImageInfo& info, void* pixels,
296 size_t rowBytes, const AndroidOptions& options) = 0;
297
298 private:
299 const SkImageInfo fInfo;
300 std::unique_ptr<SkCodec> fCodec;
301 };
302 #endif // SkAndroidCodec_DEFINED