blob: a2bea14d59a823300e4bce071852c7533588d4d0 [file] [log] [blame]
scroggof24f2242015-03-03 08:59:20 -08001/*
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 SkCodec_DEFINED
9#define SkCodec_DEFINED
10
bungemanf3c15b72015-08-19 11:56:48 -070011#include "../private/SkTemplates.h"
scroggoeb602a52015-07-09 08:16:03 -070012#include "SkColor.h"
scroggo1dd3ea92015-03-20 11:55:55 -070013#include "SkEncodedFormat.h"
msarettc30c4182016-04-20 11:53:35 -070014#include "SkEncodedInfo.h"
scroggof24f2242015-03-03 08:59:20 -080015#include "SkImageInfo.h"
16#include "SkSize.h"
scroggofffeede2015-03-18 10:50:37 -070017#include "SkStream.h"
scroggof24f2242015-03-03 08:59:20 -080018#include "SkTypes.h"
msarett4984c3c2016-03-10 05:44:43 -080019#include "SkYUVSizeInfo.h"
scroggof24f2242015-03-03 08:59:20 -080020
msarettad8bcfe2016-03-07 07:09:03 -080021class SkColorSpace;
scroggof24f2242015-03-03 08:59:20 -080022class SkData;
scroggocf98fa92015-11-23 08:14:40 -080023class SkPngChunkReader;
scroggoe7fc14b2015-10-02 13:14:46 -070024class SkSampler;
scroggof24f2242015-03-03 08:59:20 -080025
msarett9876ac52016-06-01 14:47:18 -070026namespace DM {
27class ColorCodecSrc;
28}
29
30
scroggof24f2242015-03-03 08:59:20 -080031/**
32 * Abstraction layer directly on top of an image codec.
33 */
scroggoeb602a52015-07-09 08:16:03 -070034class SkCodec : SkNoncopyable {
scroggof24f2242015-03-03 08:59:20 -080035public:
36 /**
scroggodb30be22015-12-08 18:54:13 -080037 * Minimum number of bytes that must be buffered in SkStream input.
38 *
39 * An SkStream passed to NewFromStream must be able to use this many
40 * bytes to determine the image type. Then the same SkStream must be
41 * passed to the correct decoder to read from the beginning.
42 *
43 * This can be accomplished by implementing peek() to support peeking
44 * this many bytes, or by implementing rewind() to be able to rewind()
45 * after reading this many bytes.
46 */
47 static size_t MinBufferedBytesNeeded();
48
49 /**
scroggof24f2242015-03-03 08:59:20 -080050 * If this stream represents an encoded image that we know how to decode,
51 * return an SkCodec that can decode it. Otherwise return NULL.
52 *
scroggodb30be22015-12-08 18:54:13 -080053 * As stated above, this call must be able to peek or read
54 * MinBufferedBytesNeeded to determine the correct format, and then start
55 * reading from the beginning. First it will attempt to peek, and it
56 * assumes that if less than MinBufferedBytesNeeded bytes (but more than
57 * zero) are returned, this is because the stream is shorter than this,
58 * so falling back to reading would not provide more data. If peek()
59 * returns zero bytes, this call will instead attempt to read(). This
60 * will require that the stream can be rewind()ed.
61 *
62 * If SkPngChunkReader is not NULL, take a ref and pass it to libpng if
63 * the image is a png.
64 *
msarett7d5105c2015-12-02 07:02:41 -080065 * If the SkPngChunkReader is not NULL then:
66 * If the image is not a PNG, the SkPngChunkReader will be ignored.
67 * If the image is a PNG, the SkPngChunkReader will be reffed.
68 * If the PNG has unknown chunks, the SkPngChunkReader will be used
69 * to handle these chunks. SkPngChunkReader will be called to read
70 * any unknown chunk at any point during the creation of the codec
71 * or the decode. Note that if SkPngChunkReader fails to read a
72 * chunk, this could result in a failure to create the codec or a
73 * failure to decode the image.
74 * If the PNG does not contain unknown chunks, the SkPngChunkReader
75 * will not be used or modified.
scroggocf98fa92015-11-23 08:14:40 -080076 *
scroggof24f2242015-03-03 08:59:20 -080077 * If NULL is returned, the stream is deleted immediately. Otherwise, the
78 * SkCodec takes ownership of it, and will delete it when done with it.
79 */
scroggocf98fa92015-11-23 08:14:40 -080080 static SkCodec* NewFromStream(SkStream*, SkPngChunkReader* = NULL);
scroggof24f2242015-03-03 08:59:20 -080081
82 /**
83 * If this data represents an encoded image that we know how to decode,
84 * return an SkCodec that can decode it. Otherwise return NULL.
85 *
msarett7d5105c2015-12-02 07:02:41 -080086 * If the SkPngChunkReader is not NULL then:
87 * If the image is not a PNG, the SkPngChunkReader will be ignored.
88 * If the image is a PNG, the SkPngChunkReader will be reffed.
89 * If the PNG has unknown chunks, the SkPngChunkReader will be used
90 * to handle these chunks. SkPngChunkReader will be called to read
91 * any unknown chunk at any point during the creation of the codec
92 * or the decode. Note that if SkPngChunkReader fails to read a
93 * chunk, this could result in a failure to create the codec or a
94 * failure to decode the image.
95 * If the PNG does not contain unknown chunks, the SkPngChunkReader
96 * will not be used or modified.
scroggocf98fa92015-11-23 08:14:40 -080097 *
scroggof24f2242015-03-03 08:59:20 -080098 * Will take a ref if it returns a codec, else will not affect the data.
99 */
scroggocf98fa92015-11-23 08:14:40 -0800100 static SkCodec* NewFromData(SkData*, SkPngChunkReader* = NULL);
scroggof24f2242015-03-03 08:59:20 -0800101
scroggoeb602a52015-07-09 08:16:03 -0700102 virtual ~SkCodec();
103
104 /**
105 * Return the ImageInfo associated with this codec.
106 */
scroggo46c57472015-09-30 08:57:13 -0700107 const SkImageInfo& getInfo() const { return fSrcInfo; }
scroggoeb602a52015-07-09 08:16:03 -0700108
msarettc30c4182016-04-20 11:53:35 -0700109 const SkEncodedInfo& getEncodedInfo() const { return fEncodedInfo; }
110
scroggof24f2242015-03-03 08:59:20 -0800111 /**
msarett6a738212016-03-04 13:27:35 -0800112 * Returns the color space associated with the codec.
113 * Does not affect ownership.
114 * Might be NULL.
115 */
msarettad8bcfe2016-03-07 07:09:03 -0800116 SkColorSpace* getColorSpace() const { return fColorSpace.get(); }
msarett6a738212016-03-04 13:27:35 -0800117
msarett0e6274f2016-03-21 08:04:40 -0700118 enum Origin {
119 kTopLeft_Origin = 1, // Default
120 kTopRight_Origin = 2, // Reflected across y-axis
121 kBottomRight_Origin = 3, // Rotated 180
122 kBottomLeft_Origin = 4, // Reflected across x-axis
123 kLeftTop_Origin = 5, // Reflected across x-axis, Rotated 90 CCW
124 kRightTop_Origin = 6, // Rotated 90 CW
125 kRightBottom_Origin = 7, // Reflected across x-axis, Rotated 90 CW
126 kLeftBottom_Origin = 8, // Rotated 90 CCW
127 kDefault_Origin = kTopLeft_Origin,
128 kLast_Origin = kLeftBottom_Origin,
129 };
130
131 /**
132 * Returns the image orientation stored in the EXIF data.
133 * If there is no EXIF data, or if we cannot read the EXIF data, returns kTopLeft.
134 */
135 Origin getOrigin() const { return fOrigin; }
136
msarett6a738212016-03-04 13:27:35 -0800137 /**
scroggof24f2242015-03-03 08:59:20 -0800138 * Return a size that approximately supports the desired scale factor.
139 * The codec may not be able to scale efficiently to the exact scale
140 * factor requested, so return a size that approximates that scale.
emmaleer8f4ba762015-08-14 07:44:46 -0700141 * The returned value is the codec's suggestion for the closest valid
142 * scale that it can natively support
scroggof24f2242015-03-03 08:59:20 -0800143 */
scroggofffeede2015-03-18 10:50:37 -0700144 SkISize getScaledDimensions(float desiredScale) const {
msarettb32758a2015-08-18 13:22:46 -0700145 // Negative and zero scales are errors.
146 SkASSERT(desiredScale > 0.0f);
147 if (desiredScale <= 0.0f) {
148 return SkISize::Make(0, 0);
149 }
150
151 // Upscaling is not supported. Return the original size if the client
152 // requests an upscale.
153 if (desiredScale >= 1.0f) {
154 return this->getInfo().dimensions();
155 }
scroggofffeede2015-03-18 10:50:37 -0700156 return this->onGetScaledDimensions(desiredScale);
157 }
scroggof24f2242015-03-03 08:59:20 -0800158
scroggo1dd3ea92015-03-20 11:55:55 -0700159 /**
scroggob636b452015-07-22 07:16:20 -0700160 * Return (via desiredSubset) a subset which can decoded from this codec,
161 * or false if this codec cannot decode subsets or anything similar to
162 * desiredSubset.
163 *
164 * @param desiredSubset In/out parameter. As input, a desired subset of
165 * the original bounds (as specified by getInfo). If true is returned,
166 * desiredSubset may have been modified to a subset which is
167 * supported. Although a particular change may have been made to
168 * desiredSubset to create something supported, it is possible other
169 * changes could result in a valid subset.
170 * If false is returned, desiredSubset's value is undefined.
171 * @return true if this codec supports decoding desiredSubset (as
172 * returned, potentially modified)
173 */
174 bool getValidSubset(SkIRect* desiredSubset) const {
175 return this->onGetValidSubset(desiredSubset);
176 }
177
178 /**
scroggo1dd3ea92015-03-20 11:55:55 -0700179 * Format of the encoded data.
180 */
181 SkEncodedFormat getEncodedFormat() const { return this->onGetEncodedFormat(); }
182
scroggo05245902015-03-25 11:11:52 -0700183 /**
scroggoeb602a52015-07-09 08:16:03 -0700184 * Used to describe the result of a call to getPixels().
185 *
186 * Result is the union of possible results from subclasses.
187 */
188 enum Result {
189 /**
190 * General return value for success.
191 */
192 kSuccess,
193 /**
194 * The input is incomplete. A partial image was generated.
195 */
196 kIncompleteInput,
197 /**
198 * The generator cannot convert to match the request, ignoring
199 * dimensions.
200 */
201 kInvalidConversion,
202 /**
203 * The generator cannot scale to requested size.
204 */
205 kInvalidScale,
206 /**
207 * Parameters (besides info) are invalid. e.g. NULL pixels, rowBytes
208 * too small, etc.
209 */
210 kInvalidParameters,
211 /**
212 * The input did not contain a valid image.
213 */
214 kInvalidInput,
215 /**
216 * Fulfilling this request requires rewinding the input, which is not
217 * supported for this input.
218 */
219 kCouldNotRewind,
220 /**
scroggo46c57472015-09-30 08:57:13 -0700221 * This method is not implemented by this codec.
222 * FIXME: Perhaps this should be kUnsupported?
scroggoeb602a52015-07-09 08:16:03 -0700223 */
224 kUnimplemented,
225 };
226
227 /**
228 * Whether or not the memory passed to getPixels is zero initialized.
229 */
230 enum ZeroInitialized {
231 /**
232 * The memory passed to getPixels is zero initialized. The SkCodec
233 * may take advantage of this by skipping writing zeroes.
234 */
235 kYes_ZeroInitialized,
236 /**
237 * The memory passed to getPixels has not been initialized to zero,
238 * so the SkCodec must write all zeroes to memory.
239 *
240 * This is the default. It will be used if no Options struct is used.
241 */
242 kNo_ZeroInitialized,
243 };
244
245 /**
246 * Additional options to pass to getPixels.
247 */
248 struct Options {
249 Options()
scroggob636b452015-07-22 07:16:20 -0700250 : fZeroInitialized(kNo_ZeroInitialized)
251 , fSubset(NULL)
252 {}
scroggoeb602a52015-07-09 08:16:03 -0700253
254 ZeroInitialized fZeroInitialized;
scroggob636b452015-07-22 07:16:20 -0700255 /**
256 * If not NULL, represents a subset of the original image to decode.
scroggob636b452015-07-22 07:16:20 -0700257 * Must be within the bounds returned by getInfo().
scroggob636b452015-07-22 07:16:20 -0700258 * If the EncodedFormat is kWEBP_SkEncodedFormat (the only one which
259 * currently supports subsets), the top and left values must be even.
msarettfdb47572015-10-13 12:50:14 -0700260 *
scroggo26694c32016-06-01 12:08:23 -0700261 * In getPixels, we will attempt to decode the exact rectangular
262 * subset specified by fSubset.
msarettfdb47572015-10-13 12:50:14 -0700263 *
264 * In a scanline decode, it does not make sense to specify a subset
265 * top or subset height, since the client already controls which rows
266 * to get and which rows to skip. During scanline decodes, we will
267 * require that the subset top be zero and the subset height be equal
268 * to the full height. We will, however, use the values of
269 * subset left and subset width to decode partial scanlines on calls
270 * to getScanlines().
scroggob636b452015-07-22 07:16:20 -0700271 */
272 SkIRect* fSubset;
scroggoeb602a52015-07-09 08:16:03 -0700273 };
274
275 /**
276 * Decode into the given pixels, a block of memory of size at
277 * least (info.fHeight - 1) * rowBytes + (info.fWidth *
278 * bytesPerPixel)
279 *
280 * Repeated calls to this function should give the same results,
281 * allowing the PixelRef to be immutable.
282 *
283 * @param info A description of the format (config, size)
284 * expected by the caller. This can simply be identical
285 * to the info returned by getInfo().
286 *
287 * This contract also allows the caller to specify
288 * different output-configs, which the implementation can
289 * decide to support or not.
290 *
291 * A size that does not match getInfo() implies a request
292 * to scale. If the generator cannot perform this scale,
293 * it will return kInvalidScale.
294 *
295 * If info is kIndex8_SkColorType, then the caller must provide storage for up to 256
296 * SkPMColor values in ctable. On success the generator must copy N colors into that storage,
297 * (where N is the logical number of table entries) and set ctableCount to N.
298 *
299 * If info is not kIndex8_SkColorType, then the last two parameters may be NULL. If ctableCount
300 * is not null, it will be set to 0.
301 *
scroggo46c57472015-09-30 08:57:13 -0700302 * If a scanline decode is in progress, scanline mode will end, requiring the client to call
303 * startScanlineDecode() in order to return to decoding scanlines.
304 *
scroggoeb602a52015-07-09 08:16:03 -0700305 * @return Result kSuccess, or another value explaining the type of failure.
306 */
307 Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, const Options*,
308 SkPMColor ctable[], int* ctableCount);
309
310 /**
311 * Simplified version of getPixels() that asserts that info is NOT kIndex8_SkColorType and
312 * uses the default Options.
313 */
314 Result getPixels(const SkImageInfo& info, void* pixels, size_t rowBytes);
315
msarettb714fb02016-01-22 14:46:42 -0800316 /**
317 * If decoding to YUV is supported, this returns true. Otherwise, this
318 * returns false and does not modify any of the parameters.
319 *
320 * @param sizeInfo Output parameter indicating the sizes and required
321 * allocation widths of the Y, U, and V planes.
322 * @param colorSpace Output parameter. If non-NULL this is set to kJPEG,
323 * otherwise this is ignored.
324 */
msarett4984c3c2016-03-10 05:44:43 -0800325 bool queryYUV8(SkYUVSizeInfo* sizeInfo, SkYUVColorSpace* colorSpace) const {
msarettb714fb02016-01-22 14:46:42 -0800326 if (nullptr == sizeInfo) {
327 return false;
328 }
329
330 return this->onQueryYUV8(sizeInfo, colorSpace);
331 }
332
333 /**
334 * Returns kSuccess, or another value explaining the type of failure.
335 * This always attempts to perform a full decode. If the client only
336 * wants size, it should call queryYUV8().
337 *
338 * @param sizeInfo Needs to exactly match the values returned by the
339 * query, except the WidthBytes may be larger than the
340 * recommendation (but not smaller).
341 * @param planes Memory for each of the Y, U, and V planes.
342 */
msarett4984c3c2016-03-10 05:44:43 -0800343 Result getYUV8Planes(const SkYUVSizeInfo& sizeInfo, void* planes[3]) {
msarettb714fb02016-01-22 14:46:42 -0800344 if (nullptr == planes || nullptr == planes[0] || nullptr == planes[1] ||
345 nullptr == planes[2]) {
346 return kInvalidInput;
347 }
348
349 if (!this->rewindIfNeeded()) {
350 return kCouldNotRewind;
351 }
352
353 return this->onGetYUV8Planes(sizeInfo, planes);
354 }
355
scroggoeb602a52015-07-09 08:16:03 -0700356 /**
scroggo46c57472015-09-30 08:57:13 -0700357 * The remaining functions revolve around decoding scanlines.
358 */
359
360 /**
361 * Prepare for a scanline decode with the specified options.
362 *
363 * After this call, this class will be ready to decode the first scanline.
364 *
365 * This must be called in order to call getScanlines or skipScanlines.
366 *
367 * This may require rewinding the stream.
368 *
369 * Not all SkCodecs support this.
370 *
371 * @param dstInfo Info of the destination. If the dimensions do not match
372 * those of getInfo, this implies a scale.
373 * @param options Contains decoding options, including if memory is zero
374 * initialized.
375 * @param ctable A pointer to a color table. When dstInfo.colorType() is
376 * kIndex8, this should be non-NULL and have enough storage for 256
377 * colors. The color table will be populated after decoding the palette.
378 * @param ctableCount A pointer to the size of the color table. When
379 * dstInfo.colorType() is kIndex8, this should be non-NULL. It will
380 * be modified to the true size of the color table (<= 256) after
381 * decoding the palette.
382 * @return Enum representing success or reason for failure.
383 */
384 Result startScanlineDecode(const SkImageInfo& dstInfo, const SkCodec::Options* options,
msarettfdb47572015-10-13 12:50:14 -0700385 SkPMColor ctable[], int* ctableCount);
scroggo46c57472015-09-30 08:57:13 -0700386
387 /**
388 * Simplified version of startScanlineDecode() that asserts that info is NOT
389 * kIndex8_SkColorType and uses the default Options.
390 */
391 Result startScanlineDecode(const SkImageInfo& dstInfo);
392
393 /**
394 * Write the next countLines scanlines into dst.
395 *
396 * Not valid to call before calling startScanlineDecode().
397 *
398 * @param dst Must be non-null, and large enough to hold countLines
399 * scanlines of size rowBytes.
400 * @param countLines Number of lines to write.
401 * @param rowBytes Number of bytes per row. Must be large enough to hold
402 * a scanline based on the SkImageInfo used to create this object.
msarette6dd0042015-10-09 11:07:34 -0700403 * @return the number of lines successfully decoded. If this value is
404 * less than countLines, this will fill the remaining lines with a
405 * default value.
scroggo46c57472015-09-30 08:57:13 -0700406 */
msarette6dd0042015-10-09 11:07:34 -0700407 int getScanlines(void* dst, int countLines, size_t rowBytes);
scroggo46c57472015-09-30 08:57:13 -0700408
409 /**
410 * Skip count scanlines.
411 *
412 * Not valid to call before calling startScanlineDecode().
413 *
414 * The default version just calls onGetScanlines and discards the dst.
415 * NOTE: If skipped lines are the only lines with alpha, this default
416 * will make reallyHasAlpha return true, when it could have returned
417 * false.
msarette6dd0042015-10-09 11:07:34 -0700418 *
419 * @return true if the scanlines were successfully skipped
420 * false on failure, possible reasons for failure include:
421 * An incomplete input image stream.
422 * Calling this function before calling startScanlineDecode().
423 * If countLines is less than zero or so large that it moves
424 * the current scanline past the end of the image.
scroggo46c57472015-09-30 08:57:13 -0700425 */
msarette6dd0042015-10-09 11:07:34 -0700426 bool skipScanlines(int countLines);
scroggo46c57472015-09-30 08:57:13 -0700427
428 /**
429 * The order in which rows are output from the scanline decoder is not the
430 * same for all variations of all image types. This explains the possible
431 * output row orderings.
432 */
433 enum SkScanlineOrder {
434 /*
435 * By far the most common, this indicates that the image can be decoded
436 * reliably using the scanline decoder, and that rows will be output in
437 * the logical order.
438 */
439 kTopDown_SkScanlineOrder,
440
441 /*
442 * This indicates that the scanline decoder reliably outputs rows, but
443 * they will be returned in reverse order. If the scanline format is
444 * kBottomUp, the nextScanline() API can be used to determine the actual
445 * y-coordinate of the next output row, but the client is not forced
446 * to take advantage of this, given that it's not too tough to keep
447 * track independently.
448 *
449 * For full image decodes, it is safe to get all of the scanlines at
450 * once, since the decoder will handle inverting the rows as it
451 * decodes.
452 *
453 * For subset decodes and sampling, it is simplest to get and skip
454 * scanlines one at a time, using the nextScanline() API. It is
455 * possible to ask for larger chunks at a time, but this should be used
456 * with caution. As with full image decodes, the decoder will handle
457 * inverting the requested rows, but rows will still be delivered
458 * starting from the bottom of the image.
459 *
460 * Upside down bmps are an example.
461 */
462 kBottomUp_SkScanlineOrder,
463
464 /*
465 * This indicates that the scanline decoder reliably outputs rows, but
466 * they will not be in logical order. If the scanline format is
467 * kOutOfOrder, the nextScanline() API should be used to determine the
468 * actual y-coordinate of the next output row.
469 *
470 * For this scanline ordering, it is advisable to get and skip
471 * scanlines one at a time.
472 *
473 * Interlaced gifs are an example.
474 */
475 kOutOfOrder_SkScanlineOrder,
scroggo26694c32016-06-01 12:08:23 -0700476
477 /*
478 * Indicates that the entire image must be decoded in order to output
479 * any amount of scanlines. In this case, it is a REALLY BAD IDEA to
480 * request scanlines 1-by-1 or in small chunks. The client should
481 * determine which scanlines are needed and ask for all of them in
482 * a single call to getScanlines().
483 *
484 * Interlaced pngs are an example.
485 */
486 kNone_SkScanlineOrder,
scroggo46c57472015-09-30 08:57:13 -0700487 };
488
489 /**
490 * An enum representing the order in which scanlines will be returned by
491 * the scanline decoder.
msarettbe8216a2015-12-04 08:00:50 -0800492 *
493 * This is undefined before startScanlineDecode() is called.
scroggo46c57472015-09-30 08:57:13 -0700494 */
495 SkScanlineOrder getScanlineOrder() const { return this->onGetScanlineOrder(); }
496
497 /**
498 * Returns the y-coordinate of the next row to be returned by the scanline
msarette6dd0042015-10-09 11:07:34 -0700499 * decoder.
500 *
501 * This will equal fCurrScanline, except in the case of strangely
502 * encoded image types (bottom-up bmps, interlaced gifs).
scroggo46c57472015-09-30 08:57:13 -0700503 *
504 * Results are undefined when not in scanline decoding mode.
505 */
msarette6dd0042015-10-09 11:07:34 -0700506 int nextScanline() const { return this->outputScanline(fCurrScanline); }
507
508 /**
msarettcb0d5c92015-12-03 12:23:43 -0800509 * Returns the output y-coordinate of the row that corresponds to an input
510 * y-coordinate. The input y-coordinate represents where the scanline
511 * is located in the encoded data.
msarette6dd0042015-10-09 11:07:34 -0700512 *
513 * This will equal inputScanline, except in the case of strangely
514 * encoded image types (bottom-up bmps, interlaced gifs).
515 */
516 int outputScanline(int inputScanline) const;
scroggo46c57472015-09-30 08:57:13 -0700517
scroggof24f2242015-03-03 08:59:20 -0800518protected:
msarett6a738212016-03-04 13:27:35 -0800519 /**
520 * Takes ownership of SkStream*
msarett6a738212016-03-04 13:27:35 -0800521 */
msarettc30c4182016-04-20 11:53:35 -0700522 SkCodec(int width,
523 int height,
524 const SkEncodedInfo&,
msarett0e6274f2016-03-21 08:04:40 -0700525 SkStream*,
526 sk_sp<SkColorSpace> = nullptr,
527 Origin = kTopLeft_Origin);
scroggof24f2242015-03-03 08:59:20 -0800528
msarettb714fb02016-01-22 14:46:42 -0800529 virtual SkISize onGetScaledDimensions(float /*desiredScale*/) const {
scroggof24f2242015-03-03 08:59:20 -0800530 // By default, scaling is not supported.
halcanaryb880d7f2015-03-26 06:29:03 -0700531 return this->getInfo().dimensions();
scroggof24f2242015-03-03 08:59:20 -0800532 }
533
scroggoe7fc14b2015-10-02 13:14:46 -0700534 // FIXME: What to do about subsets??
535 /**
536 * Subclasses should override if they support dimensions other than the
537 * srcInfo's.
538 */
539 virtual bool onDimensionsSupported(const SkISize&) {
540 return false;
541 }
542
scroggo1dd3ea92015-03-20 11:55:55 -0700543 virtual SkEncodedFormat onGetEncodedFormat() const = 0;
544
msarette6dd0042015-10-09 11:07:34 -0700545 /**
546 * @param rowsDecoded When the encoded image stream is incomplete, this function
547 * will return kIncompleteInput and rowsDecoded will be set to
548 * the number of scanlines that were successfully decoded.
549 * This will allow getPixels() to fill the uninitialized memory.
550 */
scroggoeb602a52015-07-09 08:16:03 -0700551 virtual Result onGetPixels(const SkImageInfo& info,
552 void* pixels, size_t rowBytes, const Options&,
msarette6dd0042015-10-09 11:07:34 -0700553 SkPMColor ctable[], int* ctableCount,
554 int* rowsDecoded) = 0;
scroggoeb602a52015-07-09 08:16:03 -0700555
msarett4984c3c2016-03-10 05:44:43 -0800556 virtual bool onQueryYUV8(SkYUVSizeInfo*, SkYUVColorSpace*) const {
msarettb714fb02016-01-22 14:46:42 -0800557 return false;
558 }
559
msarett4984c3c2016-03-10 05:44:43 -0800560 virtual Result onGetYUV8Planes(const SkYUVSizeInfo&, void*[3] /*planes*/) {
msarettb714fb02016-01-22 14:46:42 -0800561 return kUnimplemented;
562 }
563
564 virtual bool onGetValidSubset(SkIRect* /*desiredSubset*/) const {
scroggob636b452015-07-22 07:16:20 -0700565 // By default, subsets are not supported.
566 return false;
567 }
568
msarett90c4d5f2015-12-10 13:09:24 -0800569 /**
scroggof24f2242015-03-03 08:59:20 -0800570 * If the stream was previously read, attempt to rewind.
scroggob427db12015-08-12 07:24:13 -0700571 *
572 * If the stream needed to be rewound, call onRewind.
573 * @returns true if the codec is at the right position and can be used.
574 * false if there was a failure to rewind.
halcanarya096d7a2015-03-27 12:16:53 -0700575 *
scroggo3a7701c2015-09-30 09:15:14 -0700576 * This is called by getPixels() and start(). Subclasses may call if they
577 * need to rewind at another time.
scroggof24f2242015-03-03 08:59:20 -0800578 */
scroggob427db12015-08-12 07:24:13 -0700579 bool SK_WARN_UNUSED_RESULT rewindIfNeeded();
580
581 /**
582 * Called by rewindIfNeeded, if the stream needed to be rewound.
583 *
584 * Subclasses should do any set up needed after a rewind.
585 */
586 virtual bool onRewind() {
587 return true;
588 }
scroggof24f2242015-03-03 08:59:20 -0800589
msarettc0e80c12015-07-01 06:50:35 -0700590 /**
msarette6dd0042015-10-09 11:07:34 -0700591 * On an incomplete input, getPixels() and getScanlines() will fill any uninitialized
592 * scanlines. This allows the subclass to indicate what value to fill with.
593 *
594 * @param colorType Destination color type.
msarette6dd0042015-10-09 11:07:34 -0700595 * @return The value with which to fill uninitialized pixels.
596 *
597 * Note that we can interpret the return value as an SkPMColor, a 16-bit 565 color,
598 * an 8-bit gray color, or an 8-bit index into a color table, depending on the color
599 * type.
600 */
scroggoc5560be2016-02-03 09:42:42 -0800601 uint32_t getFillValue(SkColorType colorType) const {
602 return this->onGetFillValue(colorType);
msarette6dd0042015-10-09 11:07:34 -0700603 }
604
605 /**
606 * Some subclasses will override this function, but this is a useful default for the color
607 * types that we support. Note that for color types that do not use the full 32-bits,
608 * we will simply take the low bits of the fill value.
609 *
scroggoc5560be2016-02-03 09:42:42 -0800610 * kN32_SkColorType: Transparent or Black, depending on the src alpha type
msarette6dd0042015-10-09 11:07:34 -0700611 * kRGB_565_SkColorType: Black
612 * kGray_8_SkColorType: Black
613 * kIndex_8_SkColorType: First color in color table
614 */
scroggoc5560be2016-02-03 09:42:42 -0800615 virtual uint32_t onGetFillValue(SkColorType /*colorType*/) const {
616 return kOpaque_SkAlphaType == fSrcInfo.alphaType() ? SK_ColorBLACK : SK_ColorTRANSPARENT;
msarette6dd0042015-10-09 11:07:34 -0700617 }
618
619 /**
msarett74114382015-03-16 11:55:18 -0700620 * Get method for the input stream
msarett74114382015-03-16 11:55:18 -0700621 */
622 SkStream* stream() {
623 return fStream.get();
624 }
625
scroggo46c57472015-09-30 08:57:13 -0700626 /**
627 * The remaining functions revolve around decoding scanlines.
628 */
629
630 /**
631 * Most images types will be kTopDown and will not need to override this function.
632 */
633 virtual SkScanlineOrder onGetScanlineOrder() const { return kTopDown_SkScanlineOrder; }
634
scroggo26694c32016-06-01 12:08:23 -0700635 /**
636 * Update the current scanline. Used by interlaced png.
637 */
638 void updateCurrScanline(int newY) { fCurrScanline = newY; }
639
scroggo46c57472015-09-30 08:57:13 -0700640 const SkImageInfo& dstInfo() const { return fDstInfo; }
641
642 const SkCodec::Options& options() const { return fOptions; }
643
msarettcb0d5c92015-12-03 12:23:43 -0800644 /**
645 * Returns the number of scanlines that have been decoded so far.
646 * This is unaffected by the SkScanlineOrder.
647 *
648 * Returns -1 if we have not started a scanline decode.
649 */
650 int currScanline() const { return fCurrScanline; }
651
msarette6dd0042015-10-09 11:07:34 -0700652 virtual int onOutputScanline(int inputScanline) const;
653
msarett9876ac52016-06-01 14:47:18 -0700654 /**
655 * Used for testing with qcms.
656 * FIXME: Remove this when we are done comparing with qcms.
657 */
658 virtual sk_sp<SkData> getICCData() const { return nullptr; }
scroggof24f2242015-03-03 08:59:20 -0800659private:
msarettc30c4182016-04-20 11:53:35 -0700660 const SkEncodedInfo fEncodedInfo;
msarett6a738212016-03-04 13:27:35 -0800661 const SkImageInfo fSrcInfo;
662 SkAutoTDelete<SkStream> fStream;
663 bool fNeedsRewind;
msarettad8bcfe2016-03-07 07:09:03 -0800664 sk_sp<SkColorSpace> fColorSpace;
msarett0e6274f2016-03-21 08:04:40 -0700665 const Origin fOrigin;
msarett6a738212016-03-04 13:27:35 -0800666
scroggo46c57472015-09-30 08:57:13 -0700667 // These fields are only meaningful during scanline decodes.
msarett6a738212016-03-04 13:27:35 -0800668 SkImageInfo fDstInfo;
669 SkCodec::Options fOptions;
670 int fCurrScanline;
scroggo46c57472015-09-30 08:57:13 -0700671
scroggoe7fc14b2015-10-02 13:14:46 -0700672 /**
673 * Return whether these dimensions are supported as a scale.
674 *
675 * The codec may choose to cache the information about scale and subset.
676 * Either way, the same information will be passed to onGetPixels/onStart
677 * on success.
678 *
679 * This must return true for a size returned from getScaledDimensions.
680 */
681 bool dimensionsSupported(const SkISize& dim) {
682 return dim == fSrcInfo.dimensions() || this->onDimensionsSupported(dim);
683 }
684
scroggo46c57472015-09-30 08:57:13 -0700685 // Methods for scanline decoding.
msarett33bee092015-11-11 12:43:07 -0800686 virtual SkCodec::Result onStartScanlineDecode(const SkImageInfo& /*dstInfo*/,
687 const SkCodec::Options& /*options*/, SkPMColor* /*ctable*/, int* /*ctableCount*/) {
scroggo46c57472015-09-30 08:57:13 -0700688 return kUnimplemented;
689 }
690
msarett9b9497e2016-02-11 13:29:36 -0800691 virtual bool onSkipScanlines(int /*countLines*/) { return false; }
scroggo46c57472015-09-30 08:57:13 -0700692
msarett33bee092015-11-11 12:43:07 -0800693 virtual int onGetScanlines(void* /*dst*/, int /*countLines*/, size_t /*rowBytes*/) { return 0; }
msarette6dd0042015-10-09 11:07:34 -0700694
695 /**
696 * On an incomplete decode, getPixels() and getScanlines() will call this function
697 * to fill any uinitialized memory.
698 *
699 * @param dstInfo Contains the destination color type
700 * Contains the destination alpha type
701 * Contains the destination width
702 * The height stored in this info is unused
703 * @param dst Pointer to the start of destination pixel memory
704 * @param rowBytes Stride length in destination pixel memory
705 * @param zeroInit Indicates if memory is zero initialized
706 * @param linesRequested Number of lines that the client requested
707 * @param linesDecoded Number of lines that were successfully decoded
708 */
709 void fillIncompleteImage(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
710 ZeroInitialized zeroInit, int linesRequested, int linesDecoded);
scroggo46c57472015-09-30 08:57:13 -0700711
scroggoe7fc14b2015-10-02 13:14:46 -0700712 /**
713 * Return an object which will allow forcing scanline decodes to sample in X.
714 *
715 * May create a sampler, if one is not currently being used. Otherwise, does
716 * not affect ownership.
717 *
718 * Only valid during scanline decoding.
719 */
msarett33bee092015-11-11 12:43:07 -0800720 virtual SkSampler* getSampler(bool /*createIfNecessary*/) { return nullptr; }
scroggoe7fc14b2015-10-02 13:14:46 -0700721
msarett9876ac52016-06-01 14:47:18 -0700722 // For testing with qcms
723 // FIXME: Remove this when we are done comparing with qcms.
724 friend class DM::ColorCodecSrc;
725
msarett3d9d7a72015-10-21 10:27:10 -0700726 friend class SkSampledCodec;
msarettbe8216a2015-12-04 08:00:50 -0800727 friend class SkIcoCodec;
scroggof24f2242015-03-03 08:59:20 -0800728};
729#endif // SkCodec_DEFINED