blob: 0d6cc185d944b1a33385a0b2e4aaa8b2dcf2ac3e [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 /**
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
Leon Scroggins III07418182017-08-15 12:24:02 -040044 virtual ~SkAndroidCodec();
msarett3d9d7a72015-10-21 10:27:10 -070045
msarett3d9d7a72015-10-21 10:27:10 -070046 const SkImageInfo& getInfo() const { return fInfo; }
47
48 /**
49 * Format of the encoded data.
50 */
Hal Canary1fcc4042016-11-30 17:07:59 -050051 SkEncodedImageFormat getEncodedFormat() const { return fCodec->getEncodedFormat(); }
msarett3d9d7a72015-10-21 10:27:10 -070052
53 /**
msarett9a0e3462015-12-11 07:38:50 -080054 * @param requestedColorType Color type requested by the client
55 *
Matt Sarett8dcc84f2016-12-14 10:23:41 -050056 * |requestedColorType| may be overriden. We will default to kF16
Leon Scroggins571b30f2017-07-11 17:35:31 +000057 * for high precision images.
Matt Sarett8dcc84f2016-12-14 10:23:41 -050058 *
59 * In the general case, if it is possible to decode to
60 * |requestedColorType|, this returns |requestedColorType|.
61 * Otherwise, this returns a color type that is an appropriate
62 * match for the the encoded data.
msarett9a0e3462015-12-11 07:38:50 -080063 */
64 SkColorType computeOutputColorType(SkColorType requestedColorType);
65
66 /**
67 * @param requestedUnpremul Indicates if the client requested
68 * unpremultiplied output
69 *
70 * Returns the appropriate alpha type to decode to. If the image
71 * has alpha, the value of requestedUnpremul will be honored.
72 */
73 SkAlphaType computeOutputAlphaType(bool requestedUnpremul);
74
75 /**
Matt Sarett68feef42017-04-11 09:51:32 -040076 * @param outputColorType Color type that the client will decode to.
77 * @param prefColorSpace Preferred color space to decode to.
78 * This may not return |prefColorSpace| for a couple reasons.
79 * (1) Android Principles: 565 must be sRGB, F16 must be
80 * linear sRGB, transfer function must be parametric.
81 * (2) Codec Limitations: F16 requires a linear color space.
Matt Sarett966bb342016-12-12 16:30:13 -050082 *
83 * Returns the appropriate color space to decode to.
Matt Sarett966bb342016-12-12 16:30:13 -050084 */
Matt Sarett68feef42017-04-11 09:51:32 -040085 sk_sp<SkColorSpace> computeOutputColorSpace(SkColorType outputColorType,
86 sk_sp<SkColorSpace> prefColorSpace = nullptr);
Matt Sarett966bb342016-12-12 16:30:13 -050087
88 /**
msarett3d9d7a72015-10-21 10:27:10 -070089 * Returns the dimensions of the scaled output image, for an input
90 * sampleSize.
91 *
92 * When the sample size divides evenly into the original dimensions, the
93 * scaled output dimensions will simply be equal to the original
94 * dimensions divided by the sample size.
95 *
96 * When the sample size does not divide even into the original
97 * dimensions, the codec may round up or down, depending on what is most
98 * efficient to decode.
99 *
100 * Finally, the codec will always recommend a non-zero output, so the output
101 * dimension will always be one if the sampleSize is greater than the
102 * original dimension.
103 */
104 SkISize getSampledDimensions(int sampleSize) const;
105
106 /**
107 * Return (via desiredSubset) a subset which can decoded from this codec,
108 * or false if the input subset is invalid.
109 *
110 * @param desiredSubset in/out parameter
111 * As input, a desired subset of the original bounds
112 * (as specified by getInfo).
113 * As output, if true is returned, desiredSubset may
114 * have been modified to a subset which is
115 * supported. Although a particular change may have
116 * been made to desiredSubset to create something
117 * supported, it is possible other changes could
118 * result in a valid subset. If false is returned,
119 * desiredSubset's value is undefined.
120 * @return true If the input desiredSubset is valid.
121 * desiredSubset may be modified to a subset
122 * supported by the codec.
123 * false If desiredSubset is invalid (NULL or not fully
124 * contained within the image).
125 */
126 bool getSupportedSubset(SkIRect* desiredSubset) const;
127 // TODO: Rename SkCodec::getValidSubset() to getSupportedSubset()
128
129 /**
130 * Returns the dimensions of the scaled, partial output image, for an
131 * input sampleSize and subset.
132 *
133 * @param sampleSize Factor to scale down by.
134 * @param subset Must be a valid subset of the original image
135 * dimensions and a subset supported by SkAndroidCodec.
136 * getSubset() can be used to obtain a subset supported
137 * by SkAndroidCodec.
138 * @return Size of the scaled partial image. Or zero size
139 * if either of the inputs is invalid.
140 */
141 SkISize getSampledSubsetDimensions(int sampleSize, const SkIRect& subset) const;
142
143 /**
144 * Additional options to pass to getAndroidPixels().
145 */
146 // FIXME: It's a bit redundant to name these AndroidOptions when this class is already
147 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
148 // these Options when SkCodec has a slightly different set of Options. Maybe these
149 // should be DecodeOptions or SamplingOptions?
150 struct AndroidOptions {
151 AndroidOptions()
152 : fZeroInitialized(SkCodec::kNo_ZeroInitialized)
153 , fSubset(nullptr)
msarett3d9d7a72015-10-21 10:27:10 -0700154 , fSampleSize(1)
155 {}
156
157 /**
158 * Indicates is destination pixel memory is zero initialized.
scroggo7b5e5532016-02-04 06:14:24 -0800159 *
160 * The default is SkCodec::kNo_ZeroInitialized.
msarett3d9d7a72015-10-21 10:27:10 -0700161 */
162 SkCodec::ZeroInitialized fZeroInitialized;
163
164 /**
165 * If not NULL, represents a subset of the original image to decode.
166 *
167 * Must be within the bounds returned by getInfo().
168 *
Hal Canarydb683012016-11-23 08:55:18 -0700169 * If the EncodedFormat is SkEncodedImageFormat::kWEBP, the top and left
msarett3d9d7a72015-10-21 10:27:10 -0700170 * values must be even.
scroggo7b5e5532016-02-04 06:14:24 -0800171 *
172 * The default is NULL, meaning a decode of the entire image.
msarett3d9d7a72015-10-21 10:27:10 -0700173 */
174 SkIRect* fSubset;
175
176 /**
msarett3d9d7a72015-10-21 10:27:10 -0700177 * The client may provide an integer downscale factor for the decode.
178 * The codec may implement this downscaling by sampling or another
179 * method if it is more efficient.
scroggo7b5e5532016-02-04 06:14:24 -0800180 *
181 * The default is 1, representing no downscaling.
msarett3d9d7a72015-10-21 10:27:10 -0700182 */
183 int fSampleSize;
184 };
185
186 /**
187 * Decode into the given pixels, a block of memory of size at
188 * least (info.fHeight - 1) * rowBytes + (info.fWidth *
189 * bytesPerPixel)
190 *
191 * Repeated calls to this function should give the same results,
192 * allowing the PixelRef to be immutable.
193 *
194 * @param info A description of the format (config, size)
195 * expected by the caller. This can simply be identical
196 * to the info returned by getInfo().
197 *
198 * This contract also allows the caller to specify
199 * different output-configs, which the implementation can
200 * decide to support or not.
201 *
202 * A size that does not match getInfo() implies a request
203 * to scale or subset. If the codec cannot perform this
204 * scaling or subsetting, it will return an error code.
205 *
msarett3d9d7a72015-10-21 10:27:10 -0700206 * The AndroidOptions object is also used to specify any requested scaling or subsetting
scroggo7b5e5532016-02-04 06:14:24 -0800207 * using options->fSampleSize and options->fSubset. If NULL, the defaults (as specified above
208 * for AndroidOptions) are used.
msarett3d9d7a72015-10-21 10:27:10 -0700209 *
210 * @return Result kSuccess, or another value explaining the type of failure.
211 */
212 // FIXME: It's a bit redundant to name this getAndroidPixels() when this class is already
213 // called SkAndroidCodec. On the other hand, it's may be a bit confusing to call
214 // this getPixels() when it is a slightly different API than SkCodec's getPixels().
215 // Maybe this should be decode() or decodeSubset()?
216 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
scroggoe95a0682015-11-04 04:31:12 -0800217 const AndroidOptions* options);
msarett3d9d7a72015-10-21 10:27:10 -0700218
219 /**
scroggo7b5e5532016-02-04 06:14:24 -0800220 * Simplified version of getAndroidPixels() where we supply the default AndroidOptions as
Leon Scroggins571b30f2017-07-11 17:35:31 +0000221 * specified above for AndroidOptions. It will not perform any scaling or subsetting.
msarett3d9d7a72015-10-21 10:27:10 -0700222 */
223 SkCodec::Result getAndroidPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
224
scroggo7b5e5532016-02-04 06:14:24 -0800225 SkCodec::Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes) {
226 return this->getAndroidPixels(info, pixels, rowBytes);
227 }
228
msarett3d9d7a72015-10-21 10:27:10 -0700229protected:
230
msarett90c4d5f2015-12-10 13:09:24 -0800231 SkAndroidCodec(SkCodec*);
msarett3d9d7a72015-10-21 10:27:10 -0700232
msarett90c4d5f2015-12-10 13:09:24 -0800233 SkCodec* codec() const { return fCodec.get(); }
msarett3d9d7a72015-10-21 10:27:10 -0700234
235 virtual SkISize onGetSampledDimensions(int sampleSize) const = 0;
236
237 virtual bool onGetSupportedSubset(SkIRect* desiredSubset) const = 0;
238
239 virtual SkCodec::Result onGetAndroidPixels(const SkImageInfo& info, void* pixels,
scroggoe95a0682015-11-04 04:31:12 -0800240 size_t rowBytes, const AndroidOptions& options) = 0;
msarett3d9d7a72015-10-21 10:27:10 -0700241
242private:
243
244 // This will always be a reference to the info that is contained by the
245 // embedded SkCodec.
246 const SkImageInfo& fInfo;
msarett90c4d5f2015-12-10 13:09:24 -0800247
bungeman6bd52842016-10-27 09:30:08 -0700248 std::unique_ptr<SkCodec> fCodec;
msarett3d9d7a72015-10-21 10:27:10 -0700249};
250#endif // SkAndroidCodec_DEFINED