Chromium Code Reviews| 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 SkSwizzler_DEFINED | 8 #ifndef SkSwizzler_DEFINED |
| 9 #define SkSwizzler_DEFINED | 9 #define SkSwizzler_DEFINED |
| 10 | 10 |
| 11 #include "SkTypes.h" | 11 #include "SkTypes.h" |
| 12 #include "SkColor.h" | 12 #include "SkColor.h" |
| 13 #include "SkImageInfo.h" | 13 #include "SkImageInfo.h" |
| 14 | 14 |
| 15 class SkSwizzler : public SkNoncopyable { | 15 class SkSwizzler : public SkNoncopyable { |
| 16 public: | 16 public: |
| 17 /** | 17 /** |
| 18 * Enum describing the config of the source data. | 18 * Enum describing the config of the source data. |
| 19 */ | 19 */ |
| 20 enum SrcConfig { | 20 enum SrcConfig { |
| 21 kGray, // 1 byte per pixel | 21 kGray, |
| 22 kIndex, // 1 byte per pixel | 22 kIndex1, |
| 23 kRGB, // 3 bytes per pixel | 23 kIndex2, |
| 24 kRGBX, // 4 byes per pixel (ignore 4th) | 24 kIndex4, |
| 25 kRGBA, // 4 bytes per pixel | 25 kIndex, |
| 26 kRGB_565 // 2 bytes per pixel | 26 kRGB, |
| 27 kBGR, | |
| 28 kRGBX, | |
| 29 kBGRX, | |
| 30 kRGBA, | |
| 31 kBGRA, | |
| 32 kRGB_565, | |
| 27 }; | 33 }; |
| 28 | 34 |
| 29 static int BytesPerPixel(SrcConfig sc) { | 35 /* |
| 36 * | |
| 37 * Result code for the alpha components of a row. | |
| 38 * | |
| 39 */ | |
| 40 enum ResultAlpha { | |
| 41 kOpaque_ResultAlpha, // All pixels in a row have max alpha | |
| 42 kTransparent_ResultAlpha, // All pixels in a row have zero alpha | |
| 43 kNeither_ResultAlpha // Alpha is not completely maxed or zeroed | |
| 44 }; | |
| 45 | |
| 46 static int BitsPerPixel(SrcConfig sc) { | |
| 30 switch (sc) { | 47 switch (sc) { |
| 48 case kIndex1: | |
| 49 return 1; | |
| 50 case kIndex2: | |
| 51 return 2; | |
| 52 case kIndex4: | |
| 53 return 4; | |
| 31 case kGray: | 54 case kGray: |
| 32 case kIndex: | 55 case kIndex: |
| 33 return 1; | 56 return 8; |
| 57 case kRGB_565: | |
| 58 return 16; | |
| 34 case kRGB: | 59 case kRGB: |
| 35 return 3; | 60 case kBGR: |
| 61 return 24; | |
| 36 case kRGBX: | 62 case kRGBX: |
| 37 case kRGBA: | 63 case kRGBA: |
| 38 return 4; | 64 case kBGRX: |
| 39 case kRGB_565: | 65 case kBGRA: |
| 40 return 2; | 66 return 32; |
| 41 default: | 67 default: |
| 42 SkDebugf("invalid source config passed to BytesPerPixel\n"); | 68 SkASSERT(false); |
| 43 return -1; | 69 return 0; |
| 44 } | 70 } |
| 45 } | 71 } |
| 46 | 72 |
| 73 static int BytesPerPixel(SrcConfig sc) { | |
| 74 SkASSERT(SkIsAlign4(BitsPerPixel(sc))); | |
| 75 return (BitsPerPixel(sc)) >> 3; | |
| 76 } | |
| 77 | |
| 47 /** | 78 /** |
| 48 * Create a new SkSwizzler. | 79 * Create a new SkSwizzler. |
| 49 * @param sc SrcConfig | 80 * @param sc SrcConfig |
| 50 * @param info dimensions() describe both the src and the dst. | 81 * @param info dimensions() describe both the src and the dst. |
| 51 * Other fields describe the dst. | 82 * Other fields describe the dst. |
| 52 * @param dst Destination to write pixels. Must match info and dstRowBytes | 83 * @param dst Destination to write pixels. Must match info and dstRowBytes |
| 53 * @param dstRowBytes rowBytes for dst. | 84 * @param dstRowBytes rowBytes for dst. |
| 54 * @param skipZeroes Whether to skip writing zeroes. Useful if dst is | 85 * @param skipZeroes Whether to skip writing zeroes. Useful if dst is |
| 55 * zero-initialized. The implementation may or may not respect this. | 86 * zero-initialized. The implementation may or may not respect this. |
| 56 * @return A new SkSwizzler or NULL on failure. | 87 * @return A new SkSwizzler or NULL on failure. |
| 57 */ | 88 */ |
| 58 static SkSwizzler* CreateSwizzler(SrcConfig sc, const SkPMColor* ctable, | 89 static SkSwizzler* CreateSwizzler( |
| 59 const SkImageInfo& info, void* dst, | 90 SrcConfig sc, const SkPMColor* ctable, const SkImageInfo& info, |
| 60 size_t dstRowBytes, bool skipZeroes); | 91 void* dst, size_t dstRowBytes, bool skipZeroes); |
| 92 | |
| 93 /** | |
| 94 * | |
| 95 * Chooses the proper result alpha enum based on accumulated alpha masks | |
| 96 * | |
| 97 */ | |
| 98 static ResultAlpha GetResult(uint8_t zeroAlpha, uint8_t maxAlpha); | |
| 99 | |
| 61 /** | 100 /** |
| 62 * Swizzle the next line. Call height times, once for each row of source. | 101 * Swizzle the next line. Call height times, once for each row of source. |
| 63 * @param src The next row of the source data. | 102 * @param src The next row of the source data. |
| 64 * @return Whether the row had non-opaque alpha. | 103 * @return Whether the row had non-opaque alpha. |
| 65 */ | 104 */ |
| 66 bool next(const uint8_t* SK_RESTRICT src); | 105 ResultAlpha next(const uint8_t* SK_RESTRICT src); |
| 106 | |
| 107 /** | |
| 108 * | |
| 109 * Alternate version of next that allows the caller to specify the row. | |
| 110 * It is very important to only use one version of next. Since the other | |
| 111 * version modifies the dst pointer, it will change the behavior of this | |
| 112 * function. We will check this in Debug mode. | |
| 113 * | |
| 114 */ | |
| 115 ResultAlpha next(const uint8_t* SK_RESTRICT src, int y); | |
| 67 private: | 116 private: |
| 117 | |
| 118 #ifdef SK_DEBUG | |
| 119 /* | |
| 120 * | |
| 121 * Keep track of which version of next the caller is using | |
| 122 * | |
| 123 */ | |
| 124 enum SwizzleMode { | |
|
scroggo
2015/03/11 19:22:45
I think this name is not very informative. How abo
msarett
2015/03/11 20:03:11
Almost went with NextMode, and then I tried to be
| |
| 125 kUninitialized_SwizzleMode, | |
| 126 kConsecutive_SwizzleMode, | |
| 127 kDesignateRow_SwizzleMode, | |
| 128 }; | |
| 129 | |
| 130 SwizzleMode fSwizzleMode; | |
| 131 #endif | |
| 132 | |
| 68 /** | 133 /** |
| 69 * Method for converting raw data to Skia pixels. | 134 * Method for converting raw data to Skia pixels. |
| 70 * @param dstRow Row in which to write the resulting pixels. | 135 * @param dstRow Row in which to write the resulting pixels. |
| 71 * @param src Row of src data, in format specified by SrcConfig | 136 * @param src Row of src data, in format specified by SrcConfig |
| 72 * @param width Width in pixels | 137 * @param width Width in pixels |
| 73 * @param bpp bytes per pixel of the source. | 138 * @param bpp bits per pixel of the source. |
| 74 * @param y Line of source. | 139 * @param y Line of source. |
| 75 * @param ctable Colors (used for kIndex source). | 140 * @param ctable Colors (used for kIndex source). |
| 76 */ | 141 */ |
| 77 typedef bool (*RowProc)(void* SK_RESTRICT dstRow, | 142 typedef ResultAlpha (*RowProc)(void* SK_RESTRICT dstRow, |
| 78 const uint8_t* SK_RESTRICT src, | 143 const uint8_t* SK_RESTRICT src, |
| 79 int width, int bpp, int y, | 144 int width, int bitsPerPixel, int y, |
| 80 const SkPMColor ctable[]); | 145 const SkPMColor ctable[]); |
| 81 | 146 |
| 82 const RowProc fRowProc; | 147 const RowProc fRowProc; |
| 83 const SkPMColor* fColorTable; // Unowned pointer | 148 const SkPMColor* fColorTable; // Unowned pointer |
| 84 const int fSrcPixelSize; | 149 const int fSrcBitsPerPixel; |
| 85 const SkImageInfo fDstInfo; | 150 const SkImageInfo fDstInfo; |
| 86 void* fDstRow; | 151 void* fDstRow; |
| 87 const size_t fDstRowBytes; | 152 const size_t fDstRowBytes; |
| 88 int fCurrY; | 153 int fCurrY; |
| 89 | 154 |
| 90 SkSwizzler(RowProc proc, const SkPMColor* ctable, int srcBpp, | 155 SkSwizzler(RowProc proc, const SkPMColor* ctable, int srcBPP, |
| 91 const SkImageInfo& info, void* dst, size_t rowBytes); | 156 const SkImageInfo& info, void* dst, size_t rowBytes); |
| 92 | 157 |
| 93 }; | 158 }; |
| 94 #endif // SkSwizzler_DEFINED | 159 #endif // SkSwizzler_DEFINED |
| OLD | NEW |