blob: 11157429fda7e7fbf46b9ab36ca003d3e9d1ea9e [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 IIIadd35d92020-12-15 15:58:53 -050022 /**
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 IIIda3e9ad2018-01-26 15:48:26 -050030 enum class ExifOrientationBehavior {
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050031 kIgnore,
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050032 kRespect,
33 };
34
msarett3d9d7a72015-10-21 10:27:10 -070035 /**
Leon Scroggins III7397d7a2018-01-04 13:26:30 -050036 * Pass ownership of an SkCodec to a newly-created SkAndroidCodec.
37 */
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -050038 static std::unique_ptr<SkAndroidCodec> MakeFromCodec(std::unique_ptr<SkCodec>);
Leon Scroggins III7397d7a2018-01-04 13:26:30 -050039
40 /**
msarett3d9d7a72015-10-21 10:27:10 -070041 * 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 *
msarett7d5105c2015-12-02 07:02:41 -080044 * The SkPngChunkReader handles unknown chunks in PNGs.
45 * See SkCodec.h for more details.
46 *
msarett3d9d7a72015-10-21 10:27:10 -070047 * 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 Reedede7bac2017-07-23 15:30:02 -040050 static std::unique_ptr<SkAndroidCodec> MakeFromStream(std::unique_ptr<SkStream>,
51 SkPngChunkReader* = nullptr);
msarett3d9d7a72015-10-21 10:27:10 -070052
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 *
msarett7d5105c2015-12-02 07:02:41 -080057 * The SkPngChunkReader handles unknown chunks in PNGs.
58 * See SkCodec.h for more details.
msarett3d9d7a72015-10-21 10:27:10 -070059 */
Mike Reedede7bac2017-07-23 15:30:02 -040060 static std::unique_ptr<SkAndroidCodec> MakeFromData(sk_sp<SkData>, SkPngChunkReader* = nullptr);
61
Leon Scroggins III07418182017-08-15 12:24:02 -040062 virtual ~SkAndroidCodec();
msarett3d9d7a72015-10-21 10:27:10 -070063
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -050064 // 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.
msarett3d9d7a72015-10-21 10:27:10 -070066 const SkImageInfo& getInfo() const { return fInfo; }
67
68 /**
Leon Scroggins III22d08b52020-01-30 16:03:28 -050069 * Return the ICC profile of the encoded data.
70 */
71 const skcms_ICCProfile* getICCProfile() const {
72 return fCodec->getEncodedInfo().profile();
73 }
74
75 /**
msarett3d9d7a72015-10-21 10:27:10 -070076 * Format of the encoded data.
77 */
Hal Canary1fcc4042016-11-30 17:07:59 -050078 SkEncodedImageFormat getEncodedFormat() const { return fCodec->getEncodedFormat(); }
msarett3d9d7a72015-10-21 10:27:10 -070079
80 /**
msarett9a0e3462015-12-11 07:38:50 -080081 * @param requestedColorType Color type requested by the client
82 *
Matt Sarett8dcc84f2016-12-14 10:23:41 -050083 * |requestedColorType| may be overriden. We will default to kF16
Leon Scroggins571b30f2017-07-11 17:35:31 +000084 * for high precision images.
Matt Sarett8dcc84f2016-12-14 10:23:41 -050085 *
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.
msarett9a0e3462015-12-11 07:38:50 -080090 */
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 Sarett68feef42017-04-11 09:51:32 -0400103 * @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 Sarett966bb342016-12-12 16:30:13 -0500109 *
110 * Returns the appropriate color space to decode to.
Matt Sarett966bb342016-12-12 16:30:13 -0500111 */
Matt Sarett68feef42017-04-11 09:51:32 -0400112 sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
113 sk_sp<SkColorSpace> prefColorSpace = nullptr);
Matt Sarett966bb342016-12-12 16:30:13 -0500114
115 /**
Leon Scroggins III07a722c2018-01-16 15:01:17 -0500116 * 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 /**
msarett3d9d7a72015-10-21 10:27:10 -0700127 * 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 Scroggins1340dbd2020-11-09 14:18:12 -0500188 struct AndroidOptions : public SkCodec::Options {
msarett3d9d7a72015-10-21 10:27:10 -0700189 AndroidOptions()
Leon Scroggins1340dbd2020-11-09 14:18:12 -0500190 : SkCodec::Options()
msarett3d9d7a72015-10-21 10:27:10 -0700191 , fSampleSize(1)
192 {}
193
194 /**
msarett3d9d7a72015-10-21 10:27:10 -0700195 * 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.
scroggo7b5e5532016-02-04 06:14:24 -0800198 *
199 * The default is 1, representing no downscaling.
msarett3d9d7a72015-10-21 10:27:10 -0700200 */
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 *
msarett3d9d7a72015-10-21 10:27:10 -0700224 * The AndroidOptions object is also used to specify any requested scaling or subsetting
scroggo7b5e5532016-02-04 06:14:24 -0800225 * using options->fSampleSize and options->fSubset. If NULL, the defaults (as specified above
226 * for AndroidOptions) are used.
msarett3d9d7a72015-10-21 10:27:10 -0700227 *
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,
scroggoe95a0682015-11-04 04:31:12 -0800235 const AndroidOptions* options);
msarett3d9d7a72015-10-21 10:27:10 -0700236
237 /**
scroggo7b5e5532016-02-04 06:14:24 -0800238 * Simplified version of getAndroidPixels() where we supply the default AndroidOptions as
Leon Scroggins571b30f2017-07-11 17:35:31 +0000239 * specified above for AndroidOptions. It will not perform any scaling or subsetting.
msarett3d9d7a72015-10-21 10:27:10 -0700240 */
241 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
242
scroggo7b5e5532016-02-04 06:14:24 -0800243 SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
244 return this->getAndroidPixels(info, pixels, rowBytes);
245 }
246
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500247 SkCodec* codec() const { return fCodec.get(); }
248
msarett3d9d7a72015-10-21 10:27:10 -0700249protected:
Leon Scroggins IIIadd35d92020-12-15 15:58:53 -0500250 SkAndroidCodec(SkCodec*);
msarett3d9d7a72015-10-21 10:27:10 -0700251
msarett3d9d7a72015-10-21 10:27:10 -0700252 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,
scroggoe95a0682015-11-04 04:31:12 -0800257 size_t rowBytes, const AndroidOptions& options) = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700258
259private:
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500260 const SkImageInfo fInfo;
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500261 std::unique_ptr<SkCodec> fCodec;
msarett3d9d7a72015-10-21 10:27:10 -0700262};
263#endif // SkAndroidCodec_DEFINED