blob: a4f46f939f39f75a405c53a0248fbe2e8765697c [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 /**
msarett3d9d7a72015-10-21 10:27:10 -070075 * Returns the dimensions of the scaled output image, for an input
76 * sampleSize.
77 *
78 * When the sample size divides evenly into the original dimensions, the
79 * scaled output dimensions will simply be equal to the original
80 * dimensions divided by the sample size.
81 *
82 * When the sample size does not divide even into the original
83 * dimensions, the codec may round up or down, depending on what is most
84 * efficient to decode.
85 *
86 * Finally, the codec will always recommend a non-zero output, so the output
87 * dimension will always be one if the sampleSize is greater than the
88 * original dimension.
89 */
90 SkISize getSampledDimensions(int sampleSize) const;
91
92 /**
93 * Return (via desiredSubset) a subset which can decoded from this codec,
94 * or false if the input subset is invalid.
95 *
96 * @param desiredSubset in/out parameter
97 * As input, a desired subset of the original bounds
98 * (as specified by getInfo).
99 * As output, if true is returned, desiredSubset may
100 * have been modified to a subset which is
101 * supported. Although a particular change may have
102 * been made to desiredSubset to create something
103 * supported, it is possible other changes could
104 * result in a valid subset. If false is returned,
105 * desiredSubset's value is undefined.
106 * @return true If the input desiredSubset is valid.
107 * desiredSubset may be modified to a subset
108 * supported by the codec.
109 * false If desiredSubset is invalid (NULL or not fully
110 * contained within the image).
111 */
112 bool getSupportedSubset(SkIRect* desiredSubset) const;
113 // TODO: Rename SkCodec::getValidSubset() to getSupportedSubset()
114
115 /**
116 * Returns the dimensions of the scaled, partial output image, for an
117 * input sampleSize and subset.
118 *
119 * @param sampleSize Factor to scale down by.
120 * @param subset Must be a valid subset of the original image
121 * dimensions and a subset supported by SkAndroidCodec.
122 * getSubset() can be used to obtain a subset supported
123 * by SkAndroidCodec.
124 * @return Size of the scaled partial image. Or zero size
125 * if either of the inputs is invalid.
126 */
127 SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const;
128
129 /**
130 * Additional options to pass to getAndroidPixels().
131 */
132 // FIXME: It's a bit redundant to name these AndroidOptions when this class is already
133 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
134 // these Options when SkCodec has a slightly different set of Options. Maybe these
135 // should be DecodeOptions or SamplingOptions?
136 struct AndroidOptions {
137 AndroidOptions()
138 : fZeroInitialized(SkCodec::kNo_ZeroInitialized)
139 , fSubset(nullptr)
140 , fColorPtr(nullptr)
141 , fColorCount(nullptr)
142 , fSampleSize(1)
143 {}
144
145 /**
146 * Indicates is destination pixel memory is zero initialized.
scroggo7b5e5532016-02-04 06:14:24 -0800147 *
148 * The default is SkCodec::kNo_ZeroInitialized.
msarett3d9d7a72015-10-21 10:27:10 -0700149 */
150 SkCodec::ZeroInitialized fZeroInitialized;
151
152 /**
153 * If not NULL, represents a subset of the original image to decode.
154 *
155 * Must be within the bounds returned by getInfo().
156 *
Hal Canarydb683012016-11-23 08:55:18 -0700157 * If the EncodedFormat is SkEncodedImageFormat::kWEBP, the top and left
msarett3d9d7a72015-10-21 10:27:10 -0700158 * values must be even.
scroggo7b5e5532016-02-04 06:14:24 -0800159 *
160 * The default is NULL, meaning a decode of the entire image.
msarett3d9d7a72015-10-21 10:27:10 -0700161 */
162 SkIRect* fSubset;
163
164 /**
165 * If the client has requested a decode to kIndex8_SkColorType
166 * (specified in the SkImageInfo), then the caller must provide
167 * storage for up to 256 SkPMColor values in fColorPtr. On success,
168 * the codec must copy N colors into that storage, (where N is the
169 * logical number of table entries) and set fColorCount to N.
170 *
171 * If the client does not request kIndex8_SkColorType, then the last
172 * two parameters may be NULL. If fColorCount is not null, it will be
173 * set to 0.
scroggo7b5e5532016-02-04 06:14:24 -0800174 *
175 * The default is NULL for both pointers.
msarett3d9d7a72015-10-21 10:27:10 -0700176 */
177 SkPMColor* fColorPtr;
178 int* fColorCount;
179
180 /**
181 * The client may provide an integer downscale factor for the decode.
182 * The codec may implement this downscaling by sampling or another
183 * method if it is more efficient.
scroggo7b5e5532016-02-04 06:14:24 -0800184 *
185 * The default is 1, representing no downscaling.
msarett3d9d7a72015-10-21 10:27:10 -0700186 */
187 int fSampleSize;
188 };
189
190 /**
191 * Decode into the given pixels, a block of memory of size at
192 * least (info.fHeight - 1) * rowBytes + (info.fWidth *
193 * bytesPerPixel)
194 *
195 * Repeated calls to this function should give the same results,
196 * allowing the PixelRef to be immutable.
197 *
198 * @param info A description of the format (config, size)
199 * expected by the caller. This can simply be identical
200 * to the info returned by getInfo().
201 *
202 * This contract also allows the caller to specify
203 * different output-configs, which the implementation can
204 * decide to support or not.
205 *
206 * A size that does not match getInfo() implies a request
207 * to scale or subset. If the codec cannot perform this
208 * scaling or subsetting, it will return an error code.
209 *
210 * If info is kIndex8_SkColorType, then the caller must provide storage for up to 256
211 * SkPMColor values in options->fColorPtr. On success the codec must copy N colors into
212 * that storage, (where N is the logical number of table entries) and set
213 * options->fColorCount to N.
214 *
215 * If info is not kIndex8_SkColorType, options->fColorPtr and options->fColorCount may
216 * be nullptr.
217 *
218 * The AndroidOptions object is also used to specify any requested scaling or subsetting
scroggo7b5e5532016-02-04 06:14:24 -0800219 * using options->fSampleSize and options->fSubset. If NULL, the defaults (as specified above
220 * for AndroidOptions) are used.
msarett3d9d7a72015-10-21 10:27:10 -0700221 *
222 * @return Result kSuccess, or another value explaining the type of failure.
223 */
224 // FIXME: It's a bit redundant to name this getAndroidPixels() when this class is already
225 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
226 // this getPixels() when it is a slightly different API than SkCodec's getPixels().
227 // Maybe this should be decode() or decodeSubset()?
228 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
scroggoe95a0682015-11-04 04:31:12 -0800229 const AndroidOptions* options);
msarett3d9d7a72015-10-21 10:27:10 -0700230
231 /**
scroggo7b5e5532016-02-04 06:14:24 -0800232 * Simplified version of getAndroidPixels() where we supply the default AndroidOptions as
233 * specified above for AndroidOptions.
msarett3d9d7a72015-10-21 10:27:10 -0700234 *
235 * This will return an error if the info is kIndex_8_SkColorType and also will not perform
236 * any scaling or subsetting.
237 */
238 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
239
scroggo7b5e5532016-02-04 06:14:24 -0800240 SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
241 return this->getAndroidPixels(info, pixels, rowBytes);
242 }
243
msarett3d9d7a72015-10-21 10:27:10 -0700244protected:
245
msarett90c4d5f2015-12-10 13:09:24 -0800246 SkAndroidCodec(SkCodec*);
msarett3d9d7a72015-10-21 10:27:10 -0700247
msarett90c4d5f2015-12-10 13:09:24 -0800248 SkCodec* codec() const { return fCodec.get(); }
msarett3d9d7a72015-10-21 10:27:10 -0700249
250 virtual SkISize onGetSampledDimensions(int sampleSize) const = 0;
251
252 virtual bool onGetSupportedSubset(SkIRect* desiredSubset) const = 0;
253
254 virtual SkCodec::Result onGetAndroidPixels(const SkImageInfo& info, void* pixels,
scroggoe95a0682015-11-04 04:31:12 -0800255 size_t rowBytes, const AndroidOptions& options) = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700256
257private:
258
259 // This will always be a reference to the info that is contained by the
260 // embedded SkCodec.
261 const SkImageInfo& fInfo;
msarett90c4d5f2015-12-10 13:09:24 -0800262
bungeman6bd52842016-10-27 09:30:08 -0700263 std::unique_ptr<SkCodec> fCodec;
msarett3d9d7a72015-10-21 10:27:10 -0700264};
265#endif // SkAndroidCodec_DEFINED