OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkCodec_DEFINED | 8 #ifndef SkCodec_DEFINED |
9 #define SkCodec_DEFINED | 9 #define SkCodec_DEFINED |
10 | 10 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 */ | 48 */ |
49 const SkImageInfo& getInfo() const { return fSrcInfo; } | 49 const SkImageInfo& getInfo() const { return fSrcInfo; } |
50 | 50 |
51 /** | 51 /** |
52 * Return a size that approximately supports the desired scale factor. | 52 * Return a size that approximately supports the desired scale factor. |
53 * The codec may not be able to scale efficiently to the exact scale | 53 * The codec may not be able to scale efficiently to the exact scale |
54 * factor requested, so return a size that approximates that scale. | 54 * factor requested, so return a size that approximates that scale. |
55 * The returned value is the codec's suggestion for the closest valid | 55 * The returned value is the codec's suggestion for the closest valid |
56 * scale that it can natively support | 56 * scale that it can natively support |
57 */ | 57 */ |
58 SkISize getScaledDimensions(float desiredScale) const { | 58 SkISize getScaledDimensions(float desiredScale) const; |
59 // Negative and zero scales are errors. | |
60 SkASSERT(desiredScale > 0.0f); | |
61 if (desiredScale <= 0.0f) { | |
62 return SkISize::Make(0, 0); | |
63 } | |
64 | |
65 // Upscaling is not supported. Return the original size if the client | |
66 // requests an upscale. | |
67 if (desiredScale >= 1.0f) { | |
68 return this->getInfo().dimensions(); | |
69 } | |
70 return this->onGetScaledDimensions(desiredScale); | |
71 } | |
72 | 59 |
73 /** | 60 /** |
74 * Return (via desiredSubset) a subset which can decoded from this codec, | 61 * Return (via desiredSubset) a subset which can decoded from this codec, |
75 * or false if this codec cannot decode subsets or anything similar to | 62 * or false if this codec cannot decode subsets or anything similar to |
76 * desiredSubset. | 63 * desiredSubset. |
77 * | 64 * |
78 * @param desiredSubset In/out parameter. As input, a desired subset of | 65 * @param desiredSubset In/out parameter. As input, a desired subset of |
79 * the original bounds (as specified by getInfo). If true is returned, | 66 * the original bounds (as specified by getInfo). If true is returned, |
80 * desiredSubset may have been modified to a subset which is | 67 * desiredSubset may have been modified to a subset which is |
81 * supported. Although a particular change may have been made to | 68 * supported. Although a particular change may have been made to |
82 * desiredSubset to create something supported, it is possible other | 69 * desiredSubset to create something supported, it is possible other |
83 * changes could result in a valid subset. | 70 * changes could result in a valid subset. |
84 * If false is returned, desiredSubset's value is undefined. | 71 * If false is returned, desiredSubset's value is undefined. |
85 * @return true if this codec supports decoding desiredSubset (as | 72 * @return true if this codec supports decoding desiredSubset (as |
86 * returned, potentially modified) | 73 * returned, potentially modified) |
87 */ | 74 */ |
88 bool getValidSubset(SkIRect* desiredSubset) const { | 75 bool getValidSubset(SkIRect* desiredSubset) const; |
89 return this->onGetValidSubset(desiredSubset); | |
90 } | |
91 | 76 |
92 /** | 77 /** |
93 * Format of the encoded data. | 78 * Format of the encoded data. |
94 */ | 79 */ |
95 SkEncodedFormat getEncodedFormat() const { return this->onGetEncodedFormat()
; } | 80 SkEncodedFormat getEncodedFormat() const { return this->onGetEncodedFormat()
; } |
96 | 81 |
97 /** | 82 /** |
98 * Used to describe the result of a call to getPixels(). | 83 * Used to describe the result of a call to getPixels(). |
99 * | 84 * |
100 * Result is the union of possible results from subclasses. | 85 * Result is the union of possible results from subclasses. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 kNo_ZeroInitialized, | 141 kNo_ZeroInitialized, |
157 }; | 142 }; |
158 | 143 |
159 /** | 144 /** |
160 * Additional options to pass to getPixels. | 145 * Additional options to pass to getPixels. |
161 */ | 146 */ |
162 struct Options { | 147 struct Options { |
163 Options() | 148 Options() |
164 : fZeroInitialized(kNo_ZeroInitialized) | 149 : fZeroInitialized(kNo_ZeroInitialized) |
165 , fSubset(NULL) | 150 , fSubset(NULL) |
| 151 , fScaledDimensions(SkISize::Make(0, 0)) |
| 152 , fScaledSubset(SkIRect::MakeEmpty()) |
166 {} | 153 {} |
167 | 154 |
168 ZeroInitialized fZeroInitialized; | 155 ZeroInitialized fZeroInitialized; |
| 156 |
169 /** | 157 /** |
170 * If not NULL, represents a subset of the original image to decode. | 158 * fSubset represents a subset of the original image to decode. |
| 159 * It must be within the bounds returned by getInfo(). |
| 160 * If the EncodedFormat is kWEBP_SkEncodedFormat, the top and left |
| 161 * values must be even. |
| 162 * If fSubset is NULL, we are not performing a subset decode. |
171 * | 163 * |
172 * Must be within the bounds returned by getInfo(). | 164 * fScaledDimensions and fScaledSubset are used together to help |
| 165 * specify a scaled subset decode. Both should be specified for a |
| 166 * scaled subset decode, and both should be zeroed otherwise. |
173 * | 167 * |
174 * If the EncodedFormat is kWEBP_SkEncodedFormat (the only one which | 168 * If fScaledDimensions and fScaledSubset are zeroed, we are not |
175 * currently supports subsets), the top and left values must be even. | 169 * performing a scaled subset decode: |
| 170 * (1) If fSubset is non-NULL, we are performing an unscaled |
| 171 * subset decode, and the subset dimensions must match the |
| 172 * dstInfo dimensions. |
| 173 * (2) If fSubset is NULL and the dstInfo dimensions do not |
| 174 * match the original dimensions, we are performing a |
| 175 * scaled decode. |
| 176 * (3) If fSubset is NULL and the dstInfo dimensions match the |
| 177 * original dimensions, we are performing an unscaled, full |
| 178 * image decode. |
| 179 * |
| 180 * If both are non-NULL we are performing a scaled subset decode: |
| 181 * fSubset must be non-NULL. |
| 182 * fScaledSubset must be within the bounds of fScaledDimensions. |
| 183 * The dimensions of fScaledSubset must match the dimensions |
| 184 * specified by dstInfo. |
| 185 * |
| 186 * Usage: |
| 187 * Call getScaledSubsetDimensions() with the desired fSubset of |
| 188 * the original image and the desired scale. The function will |
| 189 * populate the options object with valid values of |
| 190 * fScaledDimensions and fScaledSubset that can be decoded |
| 191 * efficiently. |
176 */ | 192 */ |
177 SkIRect* fSubset; | 193 SkIRect* fSubset; |
| 194 SkISize fScaledDimensions; |
| 195 SkIRect fScaledSubset; |
178 }; | 196 }; |
179 | 197 |
180 /** | 198 /** |
| 199 * @param desiredScale The requested scale factor. |
| 200 * @param options In/out parameter. |
| 201 * options->fSubset (in/out) specifies the |
| 202 * requested subset in terms of the original image |
| 203 * dimensions. This may be modified to a subset |
| 204 * that can be decoded more efficiently. |
| 205 * options->fScaledDimensions is an output and |
| 206 * specifies the best scaled (full image) output |
| 207 * dimensions that can be achieved for the |
| 208 * desiredScale. |
| 209 * options->fScaledSubset is an output and specifies |
| 210 * the fSubset in terms of fScaledDimensions. |
| 211 * |
| 212 * The codec may not be able to scale and subset efficiently to the exact |
| 213 * scale and subset requested, so fScaledDimensions and fScaledSubset may |
| 214 * be approximate. These output values are the codec's suggestion for |
| 215 * the closest valid scaled subset that it can support. |
| 216 * |
| 217 * @return true if fScaledDimensions and fScaledSubset have been |
| 218 * successfully set to values that the codec supports. |
| 219 * false otherwise. |
| 220 */ |
| 221 bool getScaledSubsetDimensions(float desiredScale, Options* options) const; |
| 222 |
| 223 /** |
181 * Decode into the given pixels, a block of memory of size at | 224 * Decode into the given pixels, a block of memory of size at |
182 * least (info.fHeight - 1) * rowBytes + (info.fWidth * | 225 * least (info.fHeight - 1) * rowBytes + (info.fWidth * |
183 * bytesPerPixel) | 226 * bytesPerPixel) |
184 * | 227 * |
185 * Repeated calls to this function should give the same results, | 228 * Repeated calls to this function should give the same results, |
186 * allowing the PixelRef to be immutable. | 229 * allowing the PixelRef to be immutable. |
187 * | 230 * |
188 * @param info A description of the format (config, size) | 231 * @param info A description of the format (config, size) |
189 * expected by the caller. This can simply be identical | 232 * expected by the caller. This can simply be identical |
190 * to the info returned by getInfo(). | 233 * to the info returned by getInfo(). |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 * those of getInfo, this implies a scale. | 292 * those of getInfo, this implies a scale. |
250 * @param options Contains decoding options, including if memory is zero | 293 * @param options Contains decoding options, including if memory is zero |
251 * initialized. | 294 * initialized. |
252 * @param ctable A pointer to a color table. When dstInfo.colorType() is | 295 * @param ctable A pointer to a color table. When dstInfo.colorType() is |
253 * kIndex8, this should be non-NULL and have enough storage for 256 | 296 * kIndex8, this should be non-NULL and have enough storage for 256 |
254 * colors. The color table will be populated after decoding the palett
e. | 297 * colors. The color table will be populated after decoding the palett
e. |
255 * @param ctableCount A pointer to the size of the color table. When | 298 * @param ctableCount A pointer to the size of the color table. When |
256 * dstInfo.colorType() is kIndex8, this should be non-NULL. It will | 299 * dstInfo.colorType() is kIndex8, this should be non-NULL. It will |
257 * be modified to the true size of the color table (<= 256) after | 300 * be modified to the true size of the color table (<= 256) after |
258 * decoding the palette. | 301 * decoding the palette. |
| 302 * @param subsetLeft The left offset at which to begin decoding each |
| 303 * scanline. This enables partial scanline decodes. |
| 304 * @param subsetWidth The number of pixels to decode in each scanline row. |
259 * @return Enum representing success or reason for failure. | 305 * @return Enum representing success or reason for failure. |
260 */ | 306 */ |
261 Result startScanlineDecode(const SkImageInfo& dstInfo, const SkCodec::Option
s* options, | 307 Result startScanlineDecode(const SkImageInfo& dstInfo, const SkCodec::Option
s* options, |
262 SkPMColor ctable[], int* ctableCount); | 308 SkPMColor ctable[], int* ctableCount, int subsetLeft, int subse
tWidth); |
| 309 |
| 310 /** |
| 311 * Simplified version of startScanlineDecode() that does not enable partial |
| 312 * scanline decoding. |
| 313 */ |
| 314 Result startScanlineDecode(const SkImageInfo& dstInfo, const SkCodec::Option
s* options, |
| 315 SkPMColor ctable[], int* ctableCount); |
263 | 316 |
264 /** | 317 /** |
265 * Simplified version of startScanlineDecode() that asserts that info is NO
T | 318 * Simplified version of startScanlineDecode() that asserts that info is NO
T |
266 * kIndex8_SkColorType and uses the default Options. | 319 * kIndex8_SkColorType, uses the default Options, and does not enable |
| 320 * partial scanline decoding. |
267 */ | 321 */ |
268 Result startScanlineDecode(const SkImageInfo& dstInfo); | 322 Result startScanlineDecode(const SkImageInfo& dstInfo); |
269 | 323 |
270 /** | 324 /** |
271 * Write the next countLines scanlines into dst. | 325 * Write the next countLines scanlines into dst. |
272 * | 326 * |
273 * Not valid to call before calling startScanlineDecode(). | 327 * Not valid to call before calling startScanlineDecode(). |
274 * | 328 * |
275 * @param dst Must be non-null, and large enough to hold countLines | 329 * @param dst Must be non-null, and large enough to hold countLines |
276 * scanlines of size rowBytes. | 330 * scanlines of size rowBytes. |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 int outputScanline(int inputScanline) const; | 445 int outputScanline(int inputScanline) const; |
392 | 446 |
393 protected: | 447 protected: |
394 SkCodec(const SkImageInfo&, SkStream*); | 448 SkCodec(const SkImageInfo&, SkStream*); |
395 | 449 |
396 virtual SkISize onGetScaledDimensions(float /* desiredScale */) const { | 450 virtual SkISize onGetScaledDimensions(float /* desiredScale */) const { |
397 // By default, scaling is not supported. | 451 // By default, scaling is not supported. |
398 return this->getInfo().dimensions(); | 452 return this->getInfo().dimensions(); |
399 } | 453 } |
400 | 454 |
| 455 virtual bool onGetValidSubset(SkIRect* /* desiredSubset */) const { |
| 456 // By default, subsets are not supported. |
| 457 return false; |
| 458 } |
| 459 |
| 460 virtual bool onGetScaledSubsetDimensions(float desiredScale, Options* option
s) const { |
| 461 // By default, scaled subsetting is not supported. |
| 462 return false; |
| 463 } |
| 464 |
401 // FIXME: What to do about subsets?? | 465 // FIXME: What to do about subsets?? |
402 /** | 466 /** |
403 * Subclasses should override if they support dimensions other than the | 467 * Subclasses should override if they support dimensions other than the |
404 * srcInfo's. | 468 * srcInfo's. |
405 */ | 469 */ |
406 virtual bool onDimensionsSupported(const SkISize&) { | 470 virtual bool onDimensionsSupported(const SkISize&) { |
407 return false; | 471 return false; |
408 } | 472 } |
409 | 473 |
410 virtual SkEncodedFormat onGetEncodedFormat() const = 0; | 474 virtual SkEncodedFormat onGetEncodedFormat() const = 0; |
411 | 475 |
412 /** | 476 /** |
413 * @param rowsDecoded When the encoded image stream is incomplete, this func
tion | 477 * @param rowsDecoded When the encoded image stream is incomplete, this func
tion |
414 * will return kIncompleteInput and rowsDecoded will be s
et to | 478 * will return kIncompleteInput and rowsDecoded will be s
et to |
415 * the number of scanlines that were successfully decoded
. | 479 * the number of scanlines that were successfully decoded
. |
416 * This will allow getPixels() to fill the uninitialized
memory. | 480 * This will allow getPixels() to fill the uninitialized
memory. |
417 */ | 481 */ |
418 virtual Result onGetPixels(const SkImageInfo& info, | 482 virtual Result onGetPixels(const SkImageInfo& info, |
419 void* pixels, size_t rowBytes, const Options&, | 483 void* pixels, size_t rowBytes, const Options&, |
420 SkPMColor ctable[], int* ctableCount, | 484 SkPMColor ctable[], int* ctableCount, |
421 int* rowsDecoded) = 0; | 485 int* rowsDecoded) = 0; |
422 | 486 |
423 virtual bool onGetValidSubset(SkIRect* /* desiredSubset */) const { | |
424 // By default, subsets are not supported. | |
425 return false; | |
426 } | |
427 | |
428 virtual bool onReallyHasAlpha() const { return false; } | 487 virtual bool onReallyHasAlpha() const { return false; } |
429 | 488 |
430 /** | 489 /** |
431 * If the stream was previously read, attempt to rewind. | 490 * If the stream was previously read, attempt to rewind. |
432 * | 491 * |
433 * If the stream needed to be rewound, call onRewind. | 492 * If the stream needed to be rewound, call onRewind. |
434 * @returns true if the codec is at the right position and can be used. | 493 * @returns true if the codec is at the right position and can be used. |
435 * false if there was a failure to rewind. | 494 * false if there was a failure to rewind. |
436 * | 495 * |
437 * This is called by getPixels() and start(). Subclasses may call if they | 496 * This is called by getPixels() and start(). Subclasses may call if they |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 * on success. | 581 * on success. |
523 * | 582 * |
524 * This must return true for a size returned from getScaledDimensions. | 583 * This must return true for a size returned from getScaledDimensions. |
525 */ | 584 */ |
526 bool dimensionsSupported(const SkISize& dim) { | 585 bool dimensionsSupported(const SkISize& dim) { |
527 return dim == fSrcInfo.dimensions() || this->onDimensionsSupported(dim); | 586 return dim == fSrcInfo.dimensions() || this->onDimensionsSupported(dim); |
528 } | 587 } |
529 | 588 |
530 // Methods for scanline decoding. | 589 // Methods for scanline decoding. |
531 virtual SkCodec::Result onStartScanlineDecode(const SkImageInfo& dstInfo, | 590 virtual SkCodec::Result onStartScanlineDecode(const SkImageInfo& dstInfo, |
532 const SkCodec::Options& options, SkPMColor ctable[], int* ctableCoun
t) { | 591 const SkCodec::Options& options, SkPMColor ctable[], int* ctableCoun
t, int subsetLeft, |
| 592 int subsetWidth) { |
533 return kUnimplemented; | 593 return kUnimplemented; |
534 } | 594 } |
535 | 595 |
536 // Naive default version just calls onGetScanlines on temp memory. | 596 // Naive default version just calls onGetScanlines on temp memory. |
537 virtual bool onSkipScanlines(int countLines) { | 597 virtual bool onSkipScanlines(int countLines) { |
538 // FIXME (msarett): Make this a pure virtual and always override this. | 598 // FIXME (msarett): Make this a pure virtual and always override this. |
539 SkAutoMalloc storage(fDstInfo.minRowBytes()); | 599 SkAutoMalloc storage(fDstInfo.minRowBytes()); |
540 | 600 |
541 // Note that we pass 0 to rowBytes so we continue to use the same memory
. | 601 // Note that we pass 0 to rowBytes so we continue to use the same memory
. |
542 // Also note that while getScanlines checks that rowBytes is big enough, | 602 // Also note that while getScanlines checks that rowBytes is big enough, |
(...skipping 28 matching lines...) Expand all Loading... |
571 * May create a sampler, if one is not currently being used. Otherwise, doe
s | 631 * May create a sampler, if one is not currently being used. Otherwise, doe
s |
572 * not affect ownership. | 632 * not affect ownership. |
573 * | 633 * |
574 * Only valid during scanline decoding. | 634 * Only valid during scanline decoding. |
575 */ | 635 */ |
576 virtual SkSampler* getSampler(bool createIfNecessary) { return nullptr; } | 636 virtual SkSampler* getSampler(bool createIfNecessary) { return nullptr; } |
577 | 637 |
578 friend class SkScaledCodec; | 638 friend class SkScaledCodec; |
579 }; | 639 }; |
580 #endif // SkCodec_DEFINED | 640 #endif // SkCodec_DEFINED |
OLD | NEW |