blob: 0746f066c84583e1ae7cc47f3f1cdf066ec43a4e [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
11#include "SkCodec.h"
Hal Canary1fcc4042016-11-30 17:07:59 -050012#include "SkEncodedImageFormat.h"
msarett3d9d7a72015-10-21 10:27:10 -070013#include "SkStream.h"
14#include "SkTypes.h"
15
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:
22 /**
Leon Scroggins III7397d7a2018-01-04 13:26:30 -050023 * Pass ownership of an SkCodec to a newly-created SkAndroidCodec.
24 */
25 static std::unique_ptr<SkAndroidCodec> MakeFromCodec(std::unique_ptr<SkCodec>);
26
27 /**
msarett3d9d7a72015-10-21 10:27:10 -070028 * If this stream represents an encoded image that we know how to decode,
29 * return an SkAndroidCodec that can decode it. Otherwise return NULL.
30 *
msarett7d5105c2015-12-02 07:02:41 -080031 * The SkPngChunkReader handles unknown chunks in PNGs.
32 * See SkCodec.h for more details.
33 *
msarett3d9d7a72015-10-21 10:27:10 -070034 * If NULL is returned, the stream is deleted immediately. Otherwise, the
35 * SkCodec takes ownership of it, and will delete it when done with it.
36 */
Mike Reedede7bac2017-07-23 15:30:02 -040037 static std::unique_ptr<SkAndroidCodec> MakeFromStream(std::unique_ptr<SkStream>,
38 SkPngChunkReader* = nullptr);
msarett3d9d7a72015-10-21 10:27:10 -070039
40 /**
41 * If this data 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.
msarett3d9d7a72015-10-21 10:27:10 -070046 */
Mike Reedede7bac2017-07-23 15:30:02 -040047 static std::unique_ptr<SkAndroidCodec> MakeFromData(sk_sp<SkData>, SkPngChunkReader* = nullptr);
48
Leon Scroggins III07418182017-08-15 12:24:02 -040049 virtual ~SkAndroidCodec();
msarett3d9d7a72015-10-21 10:27:10 -070050
msarett3d9d7a72015-10-21 10:27:10 -070051 const SkImageInfo& getInfo() const { return fInfo; }
52
53 /**
54 * Format of the encoded data.
55 */
Hal Canary1fcc4042016-11-30 17:07:59 -050056 SkEncodedImageFormat getEncodedFormat() const { return fCodec->getEncodedFormat(); }
msarett3d9d7a72015-10-21 10:27:10 -070057
58 /**
msarett9a0e3462015-12-11 07:38:50 -080059 * @param requestedColorType Color type requested by the client
60 *
Matt Sarett8dcc84f2016-12-14 10:23:41 -050061 * |requestedColorType| may be overriden. We will default to kF16
Leon Scroggins571b30f2017-07-11 17:35:31 +000062 * for high precision images.
Matt Sarett8dcc84f2016-12-14 10:23:41 -050063 *
64 * In the general case, if it is possible to decode to
65 * |requestedColorType|, this returns |requestedColorType|.
66 * Otherwise, this returns a color type that is an appropriate
67 * match for the the encoded data.
msarett9a0e3462015-12-11 07:38:50 -080068 */
69 SkColorType computeOutputColorType(SkColorType requestedColorType);
70
71 /**
72 * @param requestedUnpremul Indicates if the client requested
73 * unpremultiplied output
74 *
75 * Returns the appropriate alpha type to decode to. If the image
76 * has alpha, the value of requestedUnpremul will be honored.
77 */
78 SkAlphaType computeOutputAlphaType(bool requestedUnpremul);
79
80 /**
Matt Sarett68feef42017-04-11 09:51:32 -040081 * @param outputColorType Color type that the client will decode to.
82 * @param prefColorSpace Preferred color space to decode to.
83 * This may not return |prefColorSpace| for a couple reasons.
84 * (1) Android Principles: 565 must be sRGB, F16 must be
85 * linear sRGB, transfer function must be parametric.
86 * (2) Codec Limitations: F16 requires a linear color space.
Matt Sarett966bb342016-12-12 16:30:13 -050087 *
88 * Returns the appropriate color space to decode to.
Matt Sarett966bb342016-12-12 16:30:13 -050089 */
Matt Sarett68feef42017-04-11 09:51:32 -040090 sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
91 sk_sp<SkColorSpace> prefColorSpace = nullptr);
Matt Sarett966bb342016-12-12 16:30:13 -050092
93 /**
Leon Scroggins III07a722c2018-01-16 15:01:17 -050094 * Compute the appropriate sample size to get to |size|.
95 *
96 * @param size As an input parameter, the desired output size of
97 * the decode. As an output parameter, the smallest sampled size
98 * larger than the input.
99 * @return the sample size to set AndroidOptions::fSampleSize to decode
100 * to the output |size|.
101 */
102 int computeSampleSize(SkISize* size) const;
103
104 /**
msarett3d9d7a72015-10-21 10:27:10 -0700105 * Returns the dimensions of the scaled output image, for an input
106 * sampleSize.
107 *
108 * When the sample size divides evenly into the original dimensions, the
109 * scaled output dimensions will simply be equal to the original
110 * dimensions divided by the sample size.
111 *
112 * When the sample size does not divide even into the original
113 * dimensions, the codec may round up or down, depending on what is most
114 * efficient to decode.
115 *
116 * Finally, the codec will always recommend a non-zero output, so the output
117 * dimension will always be one if the sampleSize is greater than the
118 * original dimension.
119 */
120 SkISize getSampledDimensions(int sampleSize) const;
121
122 /**
123 * Return (via desiredSubset) a subset which can decoded from this codec,
124 * or false if the input subset is invalid.
125 *
126 * @param desiredSubset in/out parameter
127 * As input, a desired subset of the original bounds
128 * (as specified by getInfo).
129 * As output, if true is returned, desiredSubset may
130 * have been modified to a subset which is
131 * supported. Although a particular change may have
132 * been made to desiredSubset to create something
133 * supported, it is possible other changes could
134 * result in a valid subset. If false is returned,
135 * desiredSubset's value is undefined.
136 * @return true If the input desiredSubset is valid.
137 * desiredSubset may be modified to a subset
138 * supported by the codec.
139 * false If desiredSubset is invalid (NULL or not fully
140 * contained within the image).
141 */
142 bool getSupportedSubset(SkIRect* desiredSubset) const;
143 // TODO: Rename SkCodec::getValidSubset() to getSupportedSubset()
144
145 /**
146 * Returns the dimensions of the scaled, partial output image, for an
147 * input sampleSize and subset.
148 *
149 * @param sampleSize Factor to scale down by.
150 * @param subset Must be a valid subset of the original image
151 * dimensions and a subset supported by SkAndroidCodec.
152 * getSubset() can be used to obtain a subset supported
153 * by SkAndroidCodec.
154 * @return Size of the scaled partial image. Or zero size
155 * if either of the inputs is invalid.
156 */
157 SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const;
158
159 /**
160 * Additional options to pass to getAndroidPixels().
161 */
162 // FIXME: It's a bit redundant to name these AndroidOptions when this class is already
163 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
164 // these Options when SkCodec has a slightly different set of Options. Maybe these
165 // should be DecodeOptions or SamplingOptions?
166 struct AndroidOptions {
167 AndroidOptions()
168 : fZeroInitialized(SkCodec::kNo_ZeroInitialized)
169 , fSubset(nullptr)
msarett3d9d7a72015-10-21 10:27:10 -0700170 , fSampleSize(1)
171 {}
172
173 /**
174 * Indicates is destination pixel memory is zero initialized.
scroggo7b5e5532016-02-04 06:14:24 -0800175 *
176 * The default is SkCodec::kNo_ZeroInitialized.
msarett3d9d7a72015-10-21 10:27:10 -0700177 */
178 SkCodec::ZeroInitialized fZeroInitialized;
179
180 /**
181 * If not NULL, represents a subset of the original image to decode.
182 *
183 * Must be within the bounds returned by getInfo().
184 *
Hal Canarydb683012016-11-23 08:55:18 -0700185 * If the EncodedFormat is SkEncodedImageFormat::kWEBP, the top and left
msarett3d9d7a72015-10-21 10:27:10 -0700186 * values must be even.
scroggo7b5e5532016-02-04 06:14:24 -0800187 *
188 * The default is NULL, meaning a decode of the entire image.
msarett3d9d7a72015-10-21 10:27:10 -0700189 */
190 SkIRect* fSubset;
191
192 /**
msarett3d9d7a72015-10-21 10:27:10 -0700193 * The client may provide an integer downscale factor for the decode.
194 * The codec may implement this downscaling by sampling or another
195 * method if it is more efficient.
scroggo7b5e5532016-02-04 06:14:24 -0800196 *
197 * The default is 1, representing no downscaling.
msarett3d9d7a72015-10-21 10:27:10 -0700198 */
199 int fSampleSize;
200 };
201
202 /**
203 * Decode into the given pixels, a block of memory of size at
204 * least (info.fHeight - 1) * rowBytes + (info.fWidth *
205 * bytesPerPixel)
206 *
207 * Repeated calls to this function should give the same results,
208 * allowing the PixelRef to be immutable.
209 *
210 * @param info A description of the format (config, size)
211 * expected by the caller. This can simply be identical
212 * to the info returned by getInfo().
213 *
214 * This contract also allows the caller to specify
215 * different output-configs, which the implementation can
216 * decide to support or not.
217 *
218 * A size that does not match getInfo() implies a request
219 * to scale or subset. If the codec cannot perform this
220 * scaling or subsetting, it will return an error code.
221 *
msarett3d9d7a72015-10-21 10:27:10 -0700222 * The AndroidOptions object is also used to specify any requested scaling or subsetting
scroggo7b5e5532016-02-04 06:14:24 -0800223 * using options->fSampleSize and options->fSubset. If NULL, the defaults (as specified above
224 * for AndroidOptions) are used.
msarett3d9d7a72015-10-21 10:27:10 -0700225 *
226 * @return Result kSuccess, or another value explaining the type of failure.
227 */
228 // FIXME: It's a bit redundant to name this getAndroidPixels() when this class is already
229 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
230 // this getPixels() when it is a slightly different API than SkCodec's getPixels().
231 // Maybe this should be decode() or decodeSubset()?
232 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
scroggoe95a0682015-11-04 04:31:12 -0800233 const AndroidOptions* options);
msarett3d9d7a72015-10-21 10:27:10 -0700234
235 /**
scroggo7b5e5532016-02-04 06:14:24 -0800236 * Simplified version of getAndroidPixels() where we supply the default AndroidOptions as
Leon Scroggins571b30f2017-07-11 17:35:31 +0000237 * specified above for AndroidOptions. It will not perform any scaling or subsetting.
msarett3d9d7a72015-10-21 10:27:10 -0700238 */
239 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
240
scroggo7b5e5532016-02-04 06:14:24 -0800241 SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
242 return this->getAndroidPixels(info, pixels, rowBytes);
243 }
244
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500245 SkCodec* codec() const { return fCodec.get(); }
246
msarett3d9d7a72015-10-21 10:27:10 -0700247protected:
248
msarett90c4d5f2015-12-10 13:09:24 -0800249 SkAndroidCodec(SkCodec*);
msarett3d9d7a72015-10-21 10:27:10 -0700250
msarett3d9d7a72015-10-21 10:27:10 -0700251 virtual SkISize onGetSampledDimensions(int sampleSize) const = 0;
252
253 virtual bool onGetSupportedSubset(SkIRect* desiredSubset) const = 0;
254
255 virtual SkCodec::Result onGetAndroidPixels(const SkImageInfo& info, void* pixels,
scroggoe95a0682015-11-04 04:31:12 -0800256 size_t rowBytes, const AndroidOptions& options) = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700257
258private:
259
260 // This will always be a reference to the info that is contained by the
261 // embedded SkCodec.
262 const SkImageInfo& fInfo;
msarett90c4d5f2015-12-10 13:09:24 -0800263
bungeman6bd52842016-10-27 09:30:08 -0700264 std::unique_ptr<SkCodec> fCodec;
msarett3d9d7a72015-10-21 10:27:10 -0700265};
266#endif // SkAndroidCodec_DEFINED