blob: e785411ea9ba6688f80709dbb5242e8b002340ae [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 */
Mike Reedede7bac2017-07-23 15:30:02 -040032 static std::unique_ptr<SkAndroidCodec> MakeFromStream(std::unique_ptr<SkStream>,
33 SkPngChunkReader* = nullptr);
msarett3d9d7a72015-10-21 10:27:10 -070034
35 /**
36 * If this data represents an encoded image that we know how to decode,
37 * return an SkAndroidCodec that can decode it. Otherwise return NULL.
38 *
msarett7d5105c2015-12-02 07:02:41 -080039 * The SkPngChunkReader handles unknown chunks in PNGs.
40 * See SkCodec.h for more details.
msarett3d9d7a72015-10-21 10:27:10 -070041 */
Mike Reedede7bac2017-07-23 15:30:02 -040042 static std::unique_ptr<SkAndroidCodec> MakeFromData(sk_sp<SkData>, SkPngChunkReader* = nullptr);
43
44#ifdef SK_SUPPORT_LEGACY_CODEC_NEW
45 static SkAndroidCodec* NewFromStream(SkStream* stream, SkPngChunkReader* reader) {
46 return MakeFromStream(std::unique_ptr<SkStream>(stream), reader).release();
47 }
48 static SkAndroidCodec* NewFromData(sk_sp<SkData> data, SkPngChunkReader* reader) {
49 return MakeFromData(std::move(data), reader).release();
50 }
reed42943c82016-09-12 12:01:44 -070051 static SkAndroidCodec* NewFromData(SkData* data, SkPngChunkReader* reader) {
52 return NewFromData(sk_ref_sp(data), reader);
53 }
Mike Reedede7bac2017-07-23 15:30:02 -040054#endif
msarett3d9d7a72015-10-21 10:27:10 -070055
56 virtual ~SkAndroidCodec() {}
57
58
59 const SkImageInfo& getInfo() const { return fInfo; }
60
61 /**
62 * Format of the encoded data.
63 */
Hal Canary1fcc4042016-11-30 17:07:59 -050064 SkEncodedImageFormat getEncodedFormat() const { return fCodec->getEncodedFormat(); }
msarett3d9d7a72015-10-21 10:27:10 -070065
66 /**
msarett9a0e3462015-12-11 07:38:50 -080067 * @param requestedColorType Color type requested by the client
68 *
Matt Sarett8dcc84f2016-12-14 10:23:41 -050069 * |requestedColorType| may be overriden. We will default to kF16
Leon Scroggins571b30f2017-07-11 17:35:31 +000070 * for high precision images.
Matt Sarett8dcc84f2016-12-14 10:23:41 -050071 *
72 * In the general case, if it is possible to decode to
73 * |requestedColorType|, this returns |requestedColorType|.
74 * Otherwise, this returns a color type that is an appropriate
75 * match for the the encoded data.
msarett9a0e3462015-12-11 07:38:50 -080076 */
77 SkColorType computeOutputColorType(SkColorType requestedColorType);
78
79 /**
80 * @param requestedUnpremul Indicates if the client requested
81 * unpremultiplied output
82 *
83 * Returns the appropriate alpha type to decode to. If the image
84 * has alpha, the value of requestedUnpremul will be honored.
85 */
86 SkAlphaType computeOutputAlphaType(bool requestedUnpremul);
87
88 /**
Matt Sarett68feef42017-04-11 09:51:32 -040089 * @param outputColorType Color type that the client will decode to.
90 * @param prefColorSpace Preferred color space to decode to.
91 * This may not return |prefColorSpace| for a couple reasons.
92 * (1) Android Principles: 565 must be sRGB, F16 must be
93 * linear sRGB, transfer function must be parametric.
94 * (2) Codec Limitations: F16 requires a linear color space.
Matt Sarett966bb342016-12-12 16:30:13 -050095 *
96 * Returns the appropriate color space to decode to.
Matt Sarett966bb342016-12-12 16:30:13 -050097 */
Matt Sarett68feef42017-04-11 09:51:32 -040098 sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
99 sk_sp<SkColorSpace> prefColorSpace = nullptr);
Matt Sarett966bb342016-12-12 16:30:13 -0500100
101 /**
msarett3d9d7a72015-10-21 10:27:10 -0700102 * Returns the dimensions of the scaled output image, for an input
103 * sampleSize.
104 *
105 * When the sample size divides evenly into the original dimensions, the
106 * scaled output dimensions will simply be equal to the original
107 * dimensions divided by the sample size.
108 *
109 * When the sample size does not divide even into the original
110 * dimensions, the codec may round up or down, depending on what is most
111 * efficient to decode.
112 *
113 * Finally, the codec will always recommend a non-zero output, so the output
114 * dimension will always be one if the sampleSize is greater than the
115 * original dimension.
116 */
117 SkISize getSampledDimensions(int sampleSize) const;
118
119 /**
120 * Return (via desiredSubset) a subset which can decoded from this codec,
121 * or false if the input subset is invalid.
122 *
123 * @param desiredSubset in/out parameter
124 * As input, a desired subset of the original bounds
125 * (as specified by getInfo).
126 * As output, if true is returned, desiredSubset may
127 * have been modified to a subset which is
128 * supported. Although a particular change may have
129 * been made to desiredSubset to create something
130 * supported, it is possible other changes could
131 * result in a valid subset. If false is returned,
132 * desiredSubset's value is undefined.
133 * @return true If the input desiredSubset is valid.
134 * desiredSubset may be modified to a subset
135 * supported by the codec.
136 * false If desiredSubset is invalid (NULL or not fully
137 * contained within the image).
138 */
139 bool getSupportedSubset(SkIRect* desiredSubset) const;
140 // TODO: Rename SkCodec::getValidSubset() to getSupportedSubset()
141
142 /**
143 * Returns the dimensions of the scaled, partial output image, for an
144 * input sampleSize and subset.
145 *
146 * @param sampleSize Factor to scale down by.
147 * @param subset Must be a valid subset of the original image
148 * dimensions and a subset supported by SkAndroidCodec.
149 * getSubset() can be used to obtain a subset supported
150 * by SkAndroidCodec.
151 * @return Size of the scaled partial image. Or zero size
152 * if either of the inputs is invalid.
153 */
154 SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const;
155
156 /**
157 * Additional options to pass to getAndroidPixels().
158 */
159 // FIXME: It's a bit redundant to name these AndroidOptions when this class is already
160 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
161 // these Options when SkCodec has a slightly different set of Options. Maybe these
162 // should be DecodeOptions or SamplingOptions?
163 struct AndroidOptions {
164 AndroidOptions()
165 : fZeroInitialized(SkCodec::kNo_ZeroInitialized)
166 , fSubset(nullptr)
msarett3d9d7a72015-10-21 10:27:10 -0700167 , fSampleSize(1)
168 {}
169
170 /**
171 * Indicates is destination pixel memory is zero initialized.
scroggo7b5e5532016-02-04 06:14:24 -0800172 *
173 * The default is SkCodec::kNo_ZeroInitialized.
msarett3d9d7a72015-10-21 10:27:10 -0700174 */
175 SkCodec::ZeroInitialized fZeroInitialized;
176
177 /**
178 * If not NULL, represents a subset of the original image to decode.
179 *
180 * Must be within the bounds returned by getInfo().
181 *
Hal Canarydb683012016-11-23 08:55:18 -0700182 * If the EncodedFormat is SkEncodedImageFormat::kWEBP, the top and left
msarett3d9d7a72015-10-21 10:27:10 -0700183 * values must be even.
scroggo7b5e5532016-02-04 06:14:24 -0800184 *
185 * The default is NULL, meaning a decode of the entire image.
msarett3d9d7a72015-10-21 10:27:10 -0700186 */
187 SkIRect* fSubset;
188
189 /**
msarett3d9d7a72015-10-21 10:27:10 -0700190 * The client may provide an integer downscale factor for the decode.
191 * The codec may implement this downscaling by sampling or another
192 * method if it is more efficient.
scroggo7b5e5532016-02-04 06:14:24 -0800193 *
194 * The default is 1, representing no downscaling.
msarett3d9d7a72015-10-21 10:27:10 -0700195 */
196 int fSampleSize;
197 };
198
199 /**
200 * Decode into the given pixels, a block of memory of size at
201 * least (info.fHeight - 1) * rowBytes + (info.fWidth *
202 * bytesPerPixel)
203 *
204 * Repeated calls to this function should give the same results,
205 * allowing the PixelRef to be immutable.
206 *
207 * @param info A description of the format (config, size)
208 * expected by the caller. This can simply be identical
209 * to the info returned by getInfo().
210 *
211 * This contract also allows the caller to specify
212 * different output-configs, which the implementation can
213 * decide to support or not.
214 *
215 * A size that does not match getInfo() implies a request
216 * to scale or subset. If the codec cannot perform this
217 * scaling or subsetting, it will return an error code.
218 *
msarett3d9d7a72015-10-21 10:27:10 -0700219 * The AndroidOptions object is also used to specify any requested scaling or subsetting
scroggo7b5e5532016-02-04 06:14:24 -0800220 * using options->fSampleSize and options->fSubset. If NULL, the defaults (as specified above
221 * for AndroidOptions) are used.
msarett3d9d7a72015-10-21 10:27:10 -0700222 *
223 * @return Result kSuccess, or another value explaining the type of failure.
224 */
225 // FIXME: It's a bit redundant to name this getAndroidPixels() when this class is already
226 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
227 // this getPixels() when it is a slightly different API than SkCodec's getPixels().
228 // Maybe this should be decode() or decodeSubset()?
229 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
scroggoe95a0682015-11-04 04:31:12 -0800230 const AndroidOptions* options);
msarett3d9d7a72015-10-21 10:27:10 -0700231
232 /**
scroggo7b5e5532016-02-04 06:14:24 -0800233 * Simplified version of getAndroidPixels() where we supply the default AndroidOptions as
Leon Scroggins571b30f2017-07-11 17:35:31 +0000234 * specified above for AndroidOptions. It will not perform any scaling or subsetting.
msarett3d9d7a72015-10-21 10:27:10 -0700235 */
236 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
237
scroggo7b5e5532016-02-04 06:14:24 -0800238 SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
239 return this->getAndroidPixels(info, pixels, rowBytes);
240 }
241
msarett3d9d7a72015-10-21 10:27:10 -0700242protected:
243
msarett90c4d5f2015-12-10 13:09:24 -0800244 SkAndroidCodec(SkCodec*);
msarett3d9d7a72015-10-21 10:27:10 -0700245
msarett90c4d5f2015-12-10 13:09:24 -0800246 SkCodec* codec() const { return fCodec.get(); }
msarett3d9d7a72015-10-21 10:27:10 -0700247
248 virtual SkISize onGetSampledDimensions(int sampleSize) const = 0;
249
250 virtual bool onGetSupportedSubset(SkIRect* desiredSubset) const = 0;
251
252 virtual SkCodec::Result onGetAndroidPixels(const SkImageInfo& info, void* pixels,
scroggoe95a0682015-11-04 04:31:12 -0800253 size_t rowBytes, const AndroidOptions& options) = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700254
255private:
256
257 // This will always be a reference to the info that is contained by the
258 // embedded SkCodec.
259 const SkImageInfo& fInfo;
msarett90c4d5f2015-12-10 13:09:24 -0800260
bungeman6bd52842016-10-27 09:30:08 -0700261 std::unique_ptr<SkCodec> fCodec;
msarett3d9d7a72015-10-21 10:27:10 -0700262};
263#endif // SkAndroidCodec_DEFINED