| 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 SkScanlineDecoder_DEFINED | 8 #ifndef SkScanlineDecoder_DEFINED |
| 9 #define SkScanlineDecoder_DEFINED | 9 #define SkScanlineDecoder_DEFINED |
| 10 | 10 |
| 11 #include "SkTypes.h" | 11 #include "SkTypes.h" |
| 12 #include "SkTemplates.h" | 12 #include "SkTemplates.h" |
| 13 #include "SkImageGenerator.h" | 13 #include "SkImageGenerator.h" |
| 14 #include "SkImageInfo.h" | 14 #include "SkImageInfo.h" |
| 15 | 15 |
| 16 class SkScanlineDecoder : public SkNoncopyable { | 16 class SkScanlineDecoder : public SkNoncopyable { |
| 17 public: | 17 public: |
| 18 // Note for implementations: An SkScanlineDecoder will be deleted by (and | 18 // Note for implementations: An SkScanlineDecoder will be deleted by (and |
| 19 // therefore *before*) its associated SkCodec, in case the order matters. | 19 // therefore *before*) its associated SkCodec, in case the order matters. |
| 20 virtual ~SkScanlineDecoder() {} | 20 virtual ~SkScanlineDecoder() { |
| 21 this->onFinish(); |
| 22 } |
| 21 | 23 |
| 22 /** | 24 /** |
| 23 * Write the next countLines scanlines into dst. | 25 * Write the next countLines scanlines into dst. |
| 24 * | 26 * |
| 25 * @param dst Must be non-null, and large enough to hold countLines | 27 * @param dst Must be non-null, and large enough to hold countLines |
| 26 * scanlines of size rowBytes. | 28 * scanlines of size rowBytes. |
| 27 * @param countLines Number of lines to write. | 29 * @param countLines Number of lines to write. |
| 28 * @param rowBytes Number of bytes per row. Must be large enough to hold | 30 * @param rowBytes Number of bytes per row. Must be large enough to hold |
| 29 * a scanline based on the SkImageInfo used to create this object. | 31 * a scanline based on the SkImageInfo used to create this object. |
| 30 */ | 32 */ |
| 31 SkImageGenerator::Result getScanlines(void* dst, int countLines, size_t rowB
ytes) { | 33 SkImageGenerator::Result getScanlines(void* dst, int countLines, size_t rowB
ytes) { |
| 32 if ((rowBytes < fDstInfo.minRowBytes() && countLines > 1 ) || countLines
<= 0 | 34 if ((rowBytes < fDstInfo.minRowBytes() && countLines > 1 ) || countLines
<= 0 |
| 33 || fCurrScanline + countLines > fDstInfo.height()) { | 35 || fCurrScanline + countLines > fDstInfo.height()) { |
| 34 return SkImageGenerator::kInvalidParameters; | 36 return SkImageGenerator::kInvalidParameters; |
| 35 } | 37 } |
| 36 const SkImageGenerator::Result result = this->onGetScanlines(dst, countL
ines, rowBytes); | 38 const SkImageGenerator::Result result = this->onGetScanlines(dst, countL
ines, rowBytes); |
| 37 this->checkForFinish(countLines); | 39 fCurrScanline += countLines; |
| 38 return result; | 40 return result; |
| 39 } | 41 } |
| 40 | 42 |
| 41 /** | 43 /** |
| 42 * Skip count scanlines. | 44 * Skip count scanlines. |
| 43 * | 45 * |
| 44 * The default version just calls onGetScanlines and discards the dst. | 46 * The default version just calls onGetScanlines and discards the dst. |
| 45 * NOTE: If skipped lines are the only lines with alpha, this default | 47 * NOTE: If skipped lines are the only lines with alpha, this default |
| 46 * will make reallyHasAlpha return true, when it could have returned | 48 * will make reallyHasAlpha return true, when it could have returned |
| 47 * false. | 49 * false. |
| 48 */ | 50 */ |
| 49 SkImageGenerator::Result skipScanlines(int countLines) { | 51 SkImageGenerator::Result skipScanlines(int countLines) { |
| 50 if (fCurrScanline + countLines > fDstInfo.height()) { | 52 if (fCurrScanline + countLines > fDstInfo.height()) { |
| 51 // Arguably, we could just skip the scanlines which are remaining, | 53 // Arguably, we could just skip the scanlines which are remaining, |
| 52 // and return kSuccess. We choose to return invalid so the client | 54 // and return kSuccess. We choose to return invalid so the client |
| 53 // can catch their bug. | 55 // can catch their bug. |
| 54 return SkImageGenerator::kInvalidParameters; | 56 return SkImageGenerator::kInvalidParameters; |
| 55 } | 57 } |
| 56 const SkImageGenerator::Result result = this->onSkipScanlines(countLines
); | 58 const SkImageGenerator::Result result = this->onSkipScanlines(countLines
); |
| 57 this->checkForFinish(countLines); | 59 fCurrScanline += countLines; |
| 58 return result; | 60 return result; |
| 59 } | 61 } |
| 60 | 62 |
| 61 /** | 63 /** |
| 62 * Some images may initially report that they have alpha due to the format | 64 * Some images may initially report that they have alpha due to the format |
| 63 * of the encoded data, but then never use any colors which have alpha | 65 * of the encoded data, but then never use any colors which have alpha |
| 64 * less than 100%. This function can be called *after* decoding to | 66 * less than 100%. This function can be called *after* decoding to |
| 65 * determine if such an image truly had alpha. Calling it before decoding | 67 * determine if such an image truly had alpha. Calling it before decoding |
| 66 * is undefined. | 68 * is undefined. |
| 67 * FIXME: see skbug.com/3582. | 69 * FIXME: see skbug.com/3582. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 91 // onGetScanlines bypasses that check. | 93 // onGetScanlines bypasses that check. |
| 92 // Calling the virtual method also means we do not double count | 94 // Calling the virtual method also means we do not double count |
| 93 // countLines. | 95 // countLines. |
| 94 return this->onGetScanlines(storage.get(), countLines, 0); | 96 return this->onGetScanlines(storage.get(), countLines, 0); |
| 95 } | 97 } |
| 96 | 98 |
| 97 virtual SkImageGenerator::Result onGetScanlines(void* dst, int countLines, | 99 virtual SkImageGenerator::Result onGetScanlines(void* dst, int countLines, |
| 98 size_t rowBytes) = 0; | 100 size_t rowBytes) = 0; |
| 99 | 101 |
| 100 /** | 102 /** |
| 101 * Called after any set of scanlines read/skipped. Updates fCurrScanline, | 103 * This function will be called after reading/skipping scanlines to do |
| 102 * and, if we are at the end, calls onFinish(). | |
| 103 */ | |
| 104 void checkForFinish(int countLines) { | |
| 105 fCurrScanline += countLines; | |
| 106 if (fCurrScanline >= fDstInfo.height()) { | |
| 107 this->onFinish(); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * This function will be called after reading/skipping all scanlines to do | |
| 113 * any necessary cleanups. | 104 * any necessary cleanups. |
| 105 * It is possible that not all scanlines will have been read/skipped when t
his |
| 106 * function is called. In fact, in the case of subset decodes, it is likel
y |
| 107 * that there will be scanlines at the bottom of the image that have been i
gnored. |
| 114 */ | 108 */ |
| 115 virtual void onFinish() {} // Default does nothing. | 109 virtual void onFinish() {} // Default does nothing. |
| 116 }; | 110 }; |
| 117 #endif // SkScanlineDecoder_DEFINED | 111 #endif // SkScanlineDecoder_DEFINED |
| OLD | NEW |