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 typedef uint16_t ResultAlpha; |
| 41 static const ResultAlpha kOpaque_ResultAlpha = 0xFFFF; |
| 42 static const ResultAlpha kTransparent_ResultAlpha = 0x0000; |
| 43 |
| 44 /* |
| 45 * |
| 46 * Checks if the result of decoding a row indicates that the row was |
| 47 * transparent. |
| 48 * |
| 49 */ |
| 50 static bool IsTransparent(ResultAlpha r) { |
| 51 return kTransparent_ResultAlpha == r; |
| 52 } |
| 53 |
| 54 /* |
| 55 * |
| 56 * Checks if the result of decoding a row indicates that the row was |
| 57 * opaque. |
| 58 * |
| 59 */ |
| 60 static bool IsOpaque(ResultAlpha r) { |
| 61 return kOpaque_ResultAlpha == r; |
| 62 } |
| 63 |
| 64 /* |
| 65 * |
| 66 * Constructs the proper result code based on accumulated alpha masks |
| 67 * |
| 68 */ |
| 69 static ResultAlpha GetResult(uint8_t zeroAlpha, uint8_t maxAlpha); |
| 70 |
| 71 /* |
| 72 * |
| 73 * Returns bits per pixel for source config |
| 74 * |
| 75 */ |
| 76 static int BitsPerPixel(SrcConfig sc) { |
30 switch (sc) { | 77 switch (sc) { |
| 78 case kIndex1: |
| 79 return 1; |
| 80 case kIndex2: |
| 81 return 2; |
| 82 case kIndex4: |
| 83 return 4; |
31 case kGray: | 84 case kGray: |
32 case kIndex: | 85 case kIndex: |
33 return 1; | 86 return 8; |
| 87 case kRGB_565: |
| 88 return 16; |
34 case kRGB: | 89 case kRGB: |
35 return 3; | 90 case kBGR: |
| 91 return 24; |
36 case kRGBX: | 92 case kRGBX: |
37 case kRGBA: | 93 case kRGBA: |
38 return 4; | 94 case kBGRX: |
39 case kRGB_565: | 95 case kBGRA: |
40 return 2; | 96 return 32; |
41 default: | 97 default: |
42 SkDebugf("invalid source config passed to BytesPerPixel\n"); | 98 SkASSERT(false); |
43 return -1; | 99 return 0; |
44 } | 100 } |
45 } | 101 } |
46 | 102 |
| 103 /* |
| 104 * |
| 105 * Returns bytes per pixel for source config |
| 106 * Raises an error if each pixel is not stored in an even number of bytes |
| 107 * |
| 108 */ |
| 109 static int BytesPerPixel(SrcConfig sc) { |
| 110 SkASSERT(SkIsAlign8(BitsPerPixel(sc))); |
| 111 return BitsPerPixel(sc) >> 3; |
| 112 } |
| 113 |
47 /** | 114 /** |
48 * Create a new SkSwizzler. | 115 * Create a new SkSwizzler. |
49 * @param sc SrcConfig | 116 * @param sc SrcConfig |
50 * @param info dimensions() describe both the src and the dst. | 117 * @param info dimensions() describe both the src and the dst. |
51 * Other fields describe the dst. | 118 * Other fields describe the dst. |
52 * @param dst Destination to write pixels. Must match info and dstRowBytes | 119 * @param dst Destination to write pixels. Must match info and dstRowBytes |
53 * @param dstRowBytes rowBytes for dst. | 120 * @param dstRowBytes rowBytes for dst. |
54 * @param skipZeroes Whether to skip writing zeroes. Useful if dst is | 121 * @param skipZeroes Whether to skip writing zeroes. Useful if dst is |
55 * zero-initialized. The implementation may or may not respect
this. | 122 * zero-initialized. The implementation may or may not respect
this. |
56 * @return A new SkSwizzler or NULL on failure. | 123 * @return A new SkSwizzler or NULL on failure. |
57 */ | 124 */ |
58 static SkSwizzler* CreateSwizzler(SrcConfig sc, const SkPMColor* ctable, | 125 static SkSwizzler* CreateSwizzler(SrcConfig sc, const SkPMColor* ctable, |
59 const SkImageInfo& info, void* dst, | 126 const SkImageInfo& info, void* dst, |
60 size_t dstRowBytes, bool skipZeroes); | 127 size_t dstRowBytes, bool skipZeroes); |
| 128 |
61 /** | 129 /** |
62 * Swizzle the next line. Call height times, once for each row of source. | 130 * Swizzle the next line. Call height times, once for each row of source. |
63 * @param src The next row of the source data. | 131 * @param src The next row of the source data. |
64 * @return Whether the row had non-opaque alpha. | 132 * @return A result code describing if the row was fully opaque, fully |
| 133 * transparent, or neither |
65 */ | 134 */ |
66 bool next(const uint8_t* SK_RESTRICT src); | 135 ResultAlpha next(const uint8_t* SK_RESTRICT src); |
| 136 |
| 137 /** |
| 138 * |
| 139 * Alternate version of next that allows the caller to specify the row. |
| 140 * It is very important to only use one version of next. Since the other |
| 141 * version modifies the dst pointer, it will change the behavior of this |
| 142 * function. We will check this in Debug mode. |
| 143 * |
| 144 */ |
| 145 ResultAlpha next(const uint8_t* SK_RESTRICT src, int y); |
67 private: | 146 private: |
| 147 |
| 148 #ifdef SK_DEBUG |
| 149 /* |
| 150 * |
| 151 * Keep track of which version of next the caller is using |
| 152 * |
| 153 */ |
| 154 enum NextMode { |
| 155 kUninitialized_NextMode, |
| 156 kConsecutive_NextMode, |
| 157 kDesignateRow_NextMode, |
| 158 }; |
| 159 |
| 160 NextMode fNextMode; |
| 161 #endif |
| 162 |
68 /** | 163 /** |
69 * Method for converting raw data to Skia pixels. | 164 * Method for converting raw data to Skia pixels. |
70 * @param dstRow Row in which to write the resulting pixels. | 165 * @param dstRow Row in which to write the resulting pixels. |
71 * @param src Row of src data, in format specified by SrcConfig | 166 * @param src Row of src data, in format specified by SrcConfig |
72 * @param width Width in pixels | 167 * @param width Width in pixels |
73 * @param bpp bytes per pixel of the source. | 168 * @param deltaSrc if bitsPerPixel % 8 == 0, deltaSrc is bytesPerPixel |
| 169 * else, deltaSrc is bitsPerPixel |
74 * @param y Line of source. | 170 * @param y Line of source. |
75 * @param ctable Colors (used for kIndex source). | 171 * @param ctable Colors (used for kIndex source). |
76 */ | 172 */ |
77 typedef bool (*RowProc)(void* SK_RESTRICT dstRow, | 173 typedef ResultAlpha (*RowProc)(void* SK_RESTRICT dstRow, |
78 const uint8_t* SK_RESTRICT src, | 174 const uint8_t* SK_RESTRICT src, |
79 int width, int bpp, int y, | 175 int width, int deltaSrc, int y, |
80 const SkPMColor ctable[]); | 176 const SkPMColor ctable[]); |
81 | 177 |
82 const RowProc fRowProc; | 178 const RowProc fRowProc; |
83 const SkPMColor* fColorTable; // Unowned pointer | 179 const SkPMColor* fColorTable; // Unowned pointer |
84 const int fSrcPixelSize; | 180 const int fDeltaSrc; // if bitsPerPixel % 8 == 0 |
| 181 // deltaSrc is bytesPerPixel |
| 182 // else |
| 183 // deltaSrc is bitsPerPixel |
85 const SkImageInfo fDstInfo; | 184 const SkImageInfo fDstInfo; |
86 void* fDstRow; | 185 void* fDstRow; |
87 const size_t fDstRowBytes; | 186 const size_t fDstRowBytes; |
88 int fCurrY; | 187 int fCurrY; |
89 | 188 |
90 SkSwizzler(RowProc proc, const SkPMColor* ctable, int srcBpp, | 189 SkSwizzler(RowProc proc, const SkPMColor* ctable, int deltaSrc, |
91 const SkImageInfo& info, void* dst, size_t rowBytes); | 190 const SkImageInfo& info, void* dst, size_t rowBytes); |
92 | 191 |
93 }; | 192 }; |
94 #endif // SkSwizzler_DEFINED | 193 #endif // SkSwizzler_DEFINED |
OLD | NEW |