blob: ecf0a08c15d61b67d4a8c26726e4ac9c00573b02 [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 */
20class SkAndroidCodec : SkNoncopyable {
21public:
22 /**
23 * If this stream represents an encoded image that we know how to decode,
24 * return an SkAndroidCodec that can decode it. Otherwise return NULL.
25 *
msarett7d5105c2015-12-02 07:02:41 -080026 * The SkPngChunkReader handles unknown chunks in PNGs.
27 * See SkCodec.h for more details.
28 *
msarett3d9d7a72015-10-21 10:27:10 -070029 * If NULL is returned, the stream is deleted immediately. Otherwise, the
30 * SkCodec takes ownership of it, and will delete it when done with it.
31 */
msarett7d5105c2015-12-02 07:02:41 -080032 static SkAndroidCodec* NewFromStream(SkStream*, SkPngChunkReader* = NULL);
msarett3d9d7a72015-10-21 10:27:10 -070033
34 /**
35 * If this data represents an encoded image that we know how to decode,
36 * return an SkAndroidCodec that can decode it. Otherwise return NULL.
37 *
msarett7d5105c2015-12-02 07:02:41 -080038 * The SkPngChunkReader handles unknown chunks in PNGs.
39 * See SkCodec.h for more details.
msarett3d9d7a72015-10-21 10:27:10 -070040 */
reed42943c82016-09-12 12:01:44 -070041 static SkAndroidCodec* NewFromData(sk_sp<SkData>, SkPngChunkReader* = NULL);
42 static SkAndroidCodec* NewFromData(SkData* data, SkPngChunkReader* reader) {
43 return NewFromData(sk_ref_sp(data), reader);
44 }
msarett3d9d7a72015-10-21 10:27:10 -070045
46 virtual ~SkAndroidCodec() {}
47
48
49 const SkImageInfo& getInfo() const { return fInfo; }
50
51 /**
52 * Format of the encoded data.
53 */
Hal Canary1fcc4042016-11-30 17:07:59 -050054 SkEncodedImageFormat getEncodedFormat() const { return fCodec->getEncodedFormat(); }
msarett3d9d7a72015-10-21 10:27:10 -070055
56 /**
msarett9a0e3462015-12-11 07:38:50 -080057 * @param requestedColorType Color type requested by the client
58 *
59 * If it is possible to decode to requestedColorType, this returns
60 * requestedColorType. Otherwise, this returns whichever color type
61 * is suggested by the codec as the best match for the encoded data.
62 */
63 SkColorType computeOutputColorType(SkColorType requestedColorType);
64
65 /**
66 * @param requestedUnpremul Indicates if the client requested
67 * unpremultiplied output
68 *
69 * Returns the appropriate alpha type to decode to. If the image
70 * has alpha, the value of requestedUnpremul will be honored.
71 */
72 SkAlphaType computeOutputAlphaType(bool requestedUnpremul);
73
74 /**
Matt Sarett966bb342016-12-12 16:30:13 -050075 * @param outputColorType Color type that the client will decode to
76 *
77 * Returns the appropriate color space to decode to.
78 *
79 * For now, this just returns a default. This could be updated to take
80 * requests for wide gamut modes or specific output spaces.
81 */
82 sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType);
83
84 /**
msarett3d9d7a72015-10-21 10:27:10 -070085 * Returns the dimensions of the scaled output image, for an input
86 * sampleSize.
87 *
88 * When the sample size divides evenly into the original dimensions, the
89 * scaled output dimensions will simply be equal to the original
90 * dimensions divided by the sample size.
91 *
92 * When the sample size does not divide even into the original
93 * dimensions, the codec may round up or down, depending on what is most
94 * efficient to decode.
95 *
96 * Finally, the codec will always recommend a non-zero output, so the output
97 * dimension will always be one if the sampleSize is greater than the
98 * original dimension.
99 */
100 SkISize getSampledDimensions(int sampleSize) const;
101
102 /**
103 * Return (via desiredSubset) a subset which can decoded from this codec,
104 * or false if the input subset is invalid.
105 *
106 * @param desiredSubset in/out parameter
107 * As input, a desired subset of the original bounds
108 * (as specified by getInfo).
109 * As output, if true is returned, desiredSubset may
110 * have been modified to a subset which is
111 * supported. Although a particular change may have
112 * been made to desiredSubset to create something
113 * supported, it is possible other changes could
114 * result in a valid subset. If false is returned,
115 * desiredSubset's value is undefined.
116 * @return true If the input desiredSubset is valid.
117 * desiredSubset may be modified to a subset
118 * supported by the codec.
119 * false If desiredSubset is invalid (NULL or not fully
120 * contained within the image).
121 */
122 bool getSupportedSubset(SkIRect* desiredSubset) const;
123 // TODO: Rename SkCodec::getValidSubset() to getSupportedSubset()
124
125 /**
126 * Returns the dimensions of the scaled, partial output image, for an
127 * input sampleSize and subset.
128 *
129 * @param sampleSize Factor to scale down by.
130 * @param subset Must be a valid subset of the original image
131 * dimensions and a subset supported by SkAndroidCodec.
132 * getSubset() can be used to obtain a subset supported
133 * by SkAndroidCodec.
134 * @return Size of the scaled partial image. Or zero size
135 * if either of the inputs is invalid.
136 */
137 SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const;
138
139 /**
140 * Additional options to pass to getAndroidPixels().
141 */
142 // FIXME: It's a bit redundant to name these AndroidOptions when this class is already
143 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
144 // these Options when SkCodec has a slightly different set of Options. Maybe these
145 // should be DecodeOptions or SamplingOptions?
146 struct AndroidOptions {
147 AndroidOptions()
148 : fZeroInitialized(SkCodec::kNo_ZeroInitialized)
149 , fSubset(nullptr)
150 , fColorPtr(nullptr)
151 , fColorCount(nullptr)
152 , fSampleSize(1)
153 {}
154
155 /**
156 * Indicates is destination pixel memory is zero initialized.
scroggo7b5e5532016-02-04 06:14:24 -0800157 *
158 * The default is SkCodec::kNo_ZeroInitialized.
msarett3d9d7a72015-10-21 10:27:10 -0700159 */
160 SkCodec::ZeroInitialized fZeroInitialized;
161
162 /**
163 * If not NULL, represents a subset of the original image to decode.
164 *
165 * Must be within the bounds returned by getInfo().
166 *
Hal Canarydb683012016-11-23 08:55:18 -0700167 * If the EncodedFormat is SkEncodedImageFormat::kWEBP, the top and left
msarett3d9d7a72015-10-21 10:27:10 -0700168 * values must be even.
scroggo7b5e5532016-02-04 06:14:24 -0800169 *
170 * The default is NULL, meaning a decode of the entire image.
msarett3d9d7a72015-10-21 10:27:10 -0700171 */
172 SkIRect* fSubset;
173
174 /**
175 * If the client has requested a decode to kIndex8_SkColorType
176 * (specified in the SkImageInfo), then the caller must provide
177 * storage for up to 256 SkPMColor values in fColorPtr. On success,
178 * the codec must copy N colors into that storage, (where N is the
179 * logical number of table entries) and set fColorCount to N.
180 *
181 * If the client does not request kIndex8_SkColorType, then the last
182 * two parameters may be NULL. If fColorCount is not null, it will be
183 * set to 0.
scroggo7b5e5532016-02-04 06:14:24 -0800184 *
185 * The default is NULL for both pointers.
msarett3d9d7a72015-10-21 10:27:10 -0700186 */
187 SkPMColor* fColorPtr;
188 int* fColorCount;
189
190 /**
191 * The client may provide an integer downscale factor for the decode.
192 * The codec may implement this downscaling by sampling or another
193 * method if it is more efficient.
scroggo7b5e5532016-02-04 06:14:24 -0800194 *
195 * The default is 1, representing no downscaling.
msarett3d9d7a72015-10-21 10:27:10 -0700196 */
197 int fSampleSize;
198 };
199
200 /**
201 * Decode into the given pixels, a block of memory of size at
202 * least (info.fHeight - 1) * rowBytes + (info.fWidth *
203 * bytesPerPixel)
204 *
205 * Repeated calls to this function should give the same results,
206 * allowing the PixelRef to be immutable.
207 *
208 * @param info A description of the format (config, size)
209 * expected by the caller. This can simply be identical
210 * to the info returned by getInfo().
211 *
212 * This contract also allows the caller to specify
213 * different output-configs, which the implementation can
214 * decide to support or not.
215 *
216 * A size that does not match getInfo() implies a request
217 * to scale or subset. If the codec cannot perform this
218 * scaling or subsetting, it will return an error code.
219 *
220 * If info is kIndex8_SkColorType, then the caller must provide storage for up to 256
221 * SkPMColor values in options->fColorPtr. On success the codec must copy N colors into
222 * that storage, (where N is the logical number of table entries) and set
223 * options->fColorCount to N.
224 *
225 * If info is not kIndex8_SkColorType, options->fColorPtr and options->fColorCount may
226 * be nullptr.
227 *
228 * The AndroidOptions object is also used to specify any requested scaling or subsetting
scroggo7b5e5532016-02-04 06:14:24 -0800229 * using options->fSampleSize and options->fSubset. If NULL, the defaults (as specified above
230 * for AndroidOptions) are used.
msarett3d9d7a72015-10-21 10:27:10 -0700231 *
232 * @return Result kSuccess, or another value explaining the type of failure.
233 */
234 // FIXME: It's a bit redundant to name this getAndroidPixels() when this class is already
235 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
236 // this getPixels() when it is a slightly different API than SkCodec's getPixels().
237 // Maybe this should be decode() or decodeSubset()?
238 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
scroggoe95a0682015-11-04 04:31:12 -0800239 const AndroidOptions* options);
msarett3d9d7a72015-10-21 10:27:10 -0700240
241 /**
scroggo7b5e5532016-02-04 06:14:24 -0800242 * Simplified version of getAndroidPixels() where we supply the default AndroidOptions as
243 * specified above for AndroidOptions.
msarett3d9d7a72015-10-21 10:27:10 -0700244 *
245 * This will return an error if the info is kIndex_8_SkColorType and also will not perform
246 * any scaling or subsetting.
247 */
248 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
249
scroggo7b5e5532016-02-04 06:14:24 -0800250 SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
251 return this->getAndroidPixels(info, pixels, rowBytes);
252 }
253
msarett3d9d7a72015-10-21 10:27:10 -0700254protected:
255
msarett90c4d5f2015-12-10 13:09:24 -0800256 SkAndroidCodec(SkCodec*);
msarett3d9d7a72015-10-21 10:27:10 -0700257
msarett90c4d5f2015-12-10 13:09:24 -0800258 SkCodec* codec() const { return fCodec.get(); }
msarett3d9d7a72015-10-21 10:27:10 -0700259
260 virtual SkISize onGetSampledDimensions(int sampleSize) const = 0;
261
262 virtual bool onGetSupportedSubset(SkIRect* desiredSubset) const = 0;
263
264 virtual SkCodec::Result onGetAndroidPixels(const SkImageInfo& info, void* pixels,
scroggoe95a0682015-11-04 04:31:12 -0800265 size_t rowBytes, const AndroidOptions& options) = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700266
267private:
268
269 // This will always be a reference to the info that is contained by the
270 // embedded SkCodec.
271 const SkImageInfo& fInfo;
msarett90c4d5f2015-12-10 13:09:24 -0800272
bungeman6bd52842016-10-27 09:30:08 -0700273 std::unique_ptr<SkCodec> fCodec;
msarett3d9d7a72015-10-21 10:27:10 -0700274};
275#endif // SkAndroidCodec_DEFINED