scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 8 | #include "SkCodecPriv.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 9 | #include "SkColorPriv.h" |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 10 | #include "SkScaledCodec.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 11 | #include "SkSwizzler.h" |
| 12 | #include "SkTemplates.h" |
| 13 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 14 | SkSwizzler::ResultAlpha SkSwizzler::GetResult(uint8_t zeroAlpha, |
| 15 | uint8_t maxAlpha) { |
| 16 | // In the transparent case, this returns 0x0000 |
| 17 | // In the opaque case, this returns 0xFFFF |
| 18 | // If the row is neither transparent nor opaque, returns something else |
| 19 | return (((uint16_t) maxAlpha) << 8) | zeroAlpha; |
msarett | 3675874 | 2015-03-16 08:27:53 -0700 | [diff] [blame] | 20 | } |
| 21 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 22 | // samples the row. Does not do anything else but sampling |
| 23 | static SkSwizzler::ResultAlpha sample565(void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 24 | int width, int bpp, int deltaSrc, int offset, const SkPMColor ctable[]){ |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 25 | |
| 26 | src += offset; |
| 27 | uint16_t* SK_RESTRICT dst = (uint16_t*) dstRow; |
| 28 | for (int x = 0; x < width; x++) { |
| 29 | dst[x] = src[1] << 8 | src[0]; |
| 30 | src += deltaSrc; |
| 31 | } |
| 32 | // 565 is always opaque |
| 33 | return SkSwizzler::kOpaque_ResultAlpha; |
| 34 | } |
| 35 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 36 | // TODO (msarett): Investigate SIMD optimizations for swizzle routines. |
| 37 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 38 | // kBit |
| 39 | // These routines exclusively choose between white and black |
| 40 | |
| 41 | #define GRAYSCALE_BLACK 0 |
| 42 | #define GRAYSCALE_WHITE 0xFF |
| 43 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 44 | |
| 45 | // same as swizzle_bit_to_index and swizzle_bit_to_n32 except for value assigned to dst[x] |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 46 | static SkSwizzler::ResultAlpha swizzle_bit_to_grayscale( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 47 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 48 | int bpp, int deltaSrc, int offset, const SkPMColor* /*ctable*/) { |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 49 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 50 | uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow; |
| 51 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 52 | // increment src by byte offset and bitIndex by bit offset |
| 53 | src += offset / 8; |
| 54 | int bitIndex = offset % 8; |
| 55 | uint8_t currByte = *src; |
| 56 | |
| 57 | dst[0] = ((currByte >> (7-bitIndex)) & 1) ? GRAYSCALE_WHITE : GRAYSCALE_BLACK; |
| 58 | |
| 59 | for (int x = 1; x < dstWidth; x++) { |
| 60 | int bitOffset = bitIndex + deltaSrc; |
| 61 | bitIndex = bitOffset % 8; |
| 62 | currByte = *(src += bitOffset / 8); |
| 63 | dst[x] = ((currByte >> (7-bitIndex)) & 1) ? GRAYSCALE_WHITE : GRAYSCALE_BLACK; |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 64 | } |
| 65 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 66 | return SkSwizzler::kOpaque_ResultAlpha; |
| 67 | } |
| 68 | |
| 69 | #undef GRAYSCALE_BLACK |
| 70 | #undef GRAYSCALE_WHITE |
| 71 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 72 | // same as swizzle_bit_to_grayscale and swizzle_bit_to_n32 except for value assigned to dst[x] |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 73 | static SkSwizzler::ResultAlpha swizzle_bit_to_index( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 74 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 75 | int bpp, int deltaSrc, int offset, const SkPMColor* /*ctable*/) { |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 76 | uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow; |
| 77 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 78 | // increment src by byte offset and bitIndex by bit offset |
| 79 | src += offset / 8; |
| 80 | int bitIndex = offset % 8; |
| 81 | uint8_t currByte = *src; |
| 82 | |
| 83 | dst[0] = ((currByte >> (7-bitIndex)) & 1); |
| 84 | |
| 85 | for (int x = 1; x < dstWidth; x++) { |
| 86 | int bitOffset = bitIndex + deltaSrc; |
| 87 | bitIndex = bitOffset % 8; |
| 88 | currByte = *(src += bitOffset / 8); |
| 89 | dst[x] = ((currByte >> (7-bitIndex)) & 1); |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 90 | } |
| 91 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 92 | return SkSwizzler::kOpaque_ResultAlpha; |
| 93 | } |
| 94 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 95 | // same as swizzle_bit_to_grayscale and swizzle_bit_to_index except for value assigned to dst[x] |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 96 | static SkSwizzler::ResultAlpha swizzle_bit_to_n32( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 97 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 98 | int bpp, int deltaSrc, int offset, const SkPMColor* /*ctable*/) { |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 99 | SkPMColor* SK_RESTRICT dst = (SkPMColor*) dstRow; |
| 100 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 101 | // increment src by byte offset and bitIndex by bit offset |
| 102 | src += offset / 8; |
| 103 | int bitIndex = offset % 8; |
| 104 | uint8_t currByte = *src; |
| 105 | |
| 106 | dst[0] = ((currByte >> (7 - bitIndex)) & 1) ? SK_ColorWHITE : SK_ColorBLACK; |
| 107 | |
| 108 | for (int x = 1; x < dstWidth; x++) { |
| 109 | int bitOffset = bitIndex + deltaSrc; |
| 110 | bitIndex = bitOffset % 8; |
| 111 | currByte = *(src += bitOffset / 8); |
| 112 | dst[x] = ((currByte >> (7 - bitIndex)) & 1) ? SK_ColorWHITE : SK_ColorBLACK; |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 113 | } |
| 114 | |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 115 | return SkSwizzler::kOpaque_ResultAlpha; |
| 116 | } |
| 117 | |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 118 | #define RGB565_BLACK 0 |
| 119 | #define RGB565_WHITE 0xFFFF |
| 120 | |
| 121 | static SkSwizzler::ResultAlpha swizzle_bit_to_565( |
| 122 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 123 | int bpp, int deltaSrc, int offset, const SkPMColor* /*ctable*/) { |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 124 | uint16_t* SK_RESTRICT dst = (uint16_t*) dstRow; |
| 125 | |
| 126 | // increment src by byte offset and bitIndex by bit offset |
| 127 | src += offset / 8; |
| 128 | int bitIndex = offset % 8; |
| 129 | uint8_t currByte = *src; |
| 130 | |
| 131 | dst[0] = ((currByte >> (7 - bitIndex)) & 1) ? RGB565_WHITE : RGB565_BLACK; |
| 132 | |
| 133 | for (int x = 1; x < dstWidth; x++) { |
| 134 | int bitOffset = bitIndex + deltaSrc; |
| 135 | bitIndex = bitOffset % 8; |
| 136 | currByte = *(src += bitOffset / 8); |
| 137 | dst[x] = ((currByte >> (7 - bitIndex)) & 1) ? RGB565_WHITE : RGB565_BLACK; |
| 138 | } |
| 139 | |
| 140 | return SkSwizzler::kOpaque_ResultAlpha; |
| 141 | } |
| 142 | |
| 143 | #undef RGB565_BLACK |
| 144 | #undef RGB565_WHITE |
| 145 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 146 | // kIndex1, kIndex2, kIndex4 |
| 147 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 148 | static SkSwizzler::ResultAlpha swizzle_small_index_to_index( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 149 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 150 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 151 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 152 | uint8_t* dst = (uint8_t*) dstRow; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 153 | INIT_RESULT_ALPHA; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 154 | src += offset / 8; |
| 155 | int bitIndex = offset % 8; |
| 156 | uint8_t currByte = *src; |
| 157 | const uint8_t mask = (1 << bpp) - 1; |
| 158 | uint8_t index = (currByte >> (8 - bpp - bitIndex)) & mask; |
| 159 | dst[0] = index; |
| 160 | UPDATE_RESULT_ALPHA(ctable[index] >> SK_A32_SHIFT); |
| 161 | |
| 162 | for (int x = 1; x < dstWidth; x++) { |
| 163 | int bitOffset = bitIndex + deltaSrc; |
| 164 | bitIndex = bitOffset % 8; |
| 165 | currByte = *(src += bitOffset / 8); |
| 166 | index = (currByte >> (8 - bpp - bitIndex)) & mask; |
| 167 | dst[x] = index; |
| 168 | UPDATE_RESULT_ALPHA(ctable[index] >> SK_A32_SHIFT); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 169 | } |
| 170 | return COMPUTE_RESULT_ALPHA; |
| 171 | } |
| 172 | |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 173 | static SkSwizzler::ResultAlpha swizzle_small_index_to_565( |
| 174 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 175 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 176 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 177 | uint16_t* dst = (uint16_t*) dstRow; |
| 178 | src += offset / 8; |
| 179 | int bitIndex = offset % 8; |
| 180 | uint8_t currByte = *src; |
| 181 | const uint8_t mask = (1 << bpp) - 1; |
| 182 | uint8_t index = (currByte >> (8 - bpp - bitIndex)) & mask; |
| 183 | dst[0] = SkPixel32ToPixel16(ctable[index]); |
| 184 | |
| 185 | for (int x = 1; x < dstWidth; x++) { |
| 186 | int bitOffset = bitIndex + deltaSrc; |
| 187 | bitIndex = bitOffset % 8; |
| 188 | currByte = *(src += bitOffset / 8); |
| 189 | index = (currByte >> (8 - bpp - bitIndex)) & mask; |
| 190 | dst[x] = SkPixel32ToPixel16(ctable[index]); |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 191 | } |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 192 | return SkAlphaType::kOpaque_SkAlphaType; |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 193 | } |
| 194 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 195 | static SkSwizzler::ResultAlpha swizzle_small_index_to_n32( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 196 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 197 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 198 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 199 | SkPMColor* dst = (SkPMColor*) dstRow; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 200 | INIT_RESULT_ALPHA; |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 201 | src += offset / 8; |
| 202 | int bitIndex = offset % 8; |
| 203 | uint8_t currByte = *src; |
| 204 | const uint8_t mask = (1 << bpp) - 1; |
| 205 | uint8_t index = (currByte >> (8 - bpp - bitIndex)) & mask; |
| 206 | dst[0] = ctable[index]; |
| 207 | UPDATE_RESULT_ALPHA(ctable[index] >> SK_A32_SHIFT); |
| 208 | |
| 209 | for (int x = 1; x < dstWidth; x++) { |
| 210 | int bitOffset = bitIndex + deltaSrc; |
| 211 | bitIndex = bitOffset % 8; |
| 212 | currByte = *(src += bitOffset / 8); |
| 213 | index = (currByte >> (8 - bpp - bitIndex)) & mask; |
| 214 | dst[x] = ctable[index]; |
| 215 | UPDATE_RESULT_ALPHA(ctable[index] >> SK_A32_SHIFT); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 216 | } |
| 217 | return COMPUTE_RESULT_ALPHA; |
| 218 | } |
| 219 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 220 | // kIndex |
| 221 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 222 | static SkSwizzler::ResultAlpha swizzle_index_to_index( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 223 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 224 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 225 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 226 | src += offset; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 227 | uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 228 | INIT_RESULT_ALPHA; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 229 | // TODO (msarett): Should we skip the loop here and guess that the row is opaque/not opaque? |
| 230 | // SkScaledBitmap sampler just guesses that it is opaque. This is dangerous |
| 231 | // and probably wrong since gif and bmp (rarely) may have alpha. |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 232 | if (1 == deltaSrc) { |
| 233 | // A non-zero offset is only used when sampling, meaning that deltaSrc will be |
| 234 | // greater than 1. The below loop relies on the fact that src remains unchanged. |
| 235 | SkASSERT(0 == offset); |
| 236 | memcpy(dst, src, dstWidth); |
| 237 | for (int x = 0; x < dstWidth; x++) { |
| 238 | UPDATE_RESULT_ALPHA(ctable[src[x]] >> SK_A32_SHIFT); |
| 239 | } |
| 240 | } else { |
| 241 | for (int x = 0; x < dstWidth; x++) { |
| 242 | dst[x] = *src; |
| 243 | UPDATE_RESULT_ALPHA(ctable[*src] >> SK_A32_SHIFT); |
| 244 | src += deltaSrc; |
emmaleer | b157917 | 2015-08-14 05:46:08 -0700 | [diff] [blame] | 245 | } |
emmaleer | b157917 | 2015-08-14 05:46:08 -0700 | [diff] [blame] | 246 | } |
| 247 | return COMPUTE_RESULT_ALPHA; |
| 248 | } |
| 249 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 250 | static SkSwizzler::ResultAlpha swizzle_index_to_n32( |
| 251 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 252 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 253 | |
| 254 | src += offset; |
| 255 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 256 | INIT_RESULT_ALPHA; |
| 257 | for (int x = 0; x < dstWidth; x++) { |
| 258 | SkPMColor c = ctable[*src]; |
| 259 | UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); |
| 260 | dst[x] = c; |
| 261 | src += deltaSrc; |
| 262 | } |
| 263 | return COMPUTE_RESULT_ALPHA; |
| 264 | } |
| 265 | |
| 266 | static SkSwizzler::ResultAlpha swizzle_index_to_n32_skipZ( |
| 267 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 268 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 269 | |
| 270 | src += offset; |
| 271 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 272 | INIT_RESULT_ALPHA; |
| 273 | for (int x = 0; x < dstWidth; x++) { |
| 274 | SkPMColor c = ctable[*src]; |
| 275 | UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); |
| 276 | if (c != 0) { |
| 277 | dst[x] = c; |
| 278 | } |
| 279 | src += deltaSrc; |
| 280 | } |
| 281 | return COMPUTE_RESULT_ALPHA; |
| 282 | } |
| 283 | |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 284 | static SkSwizzler::ResultAlpha swizzle_index_to_565( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 285 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 286 | int bytesPerPixel, int deltaSrc, int offset, const SkPMColor ctable[]) { |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 287 | // FIXME: Support dithering? Requires knowing y, which I think is a bigger |
| 288 | // change. |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 289 | src += offset; |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 290 | uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 291 | for (int x = 0; x < dstWidth; x++) { |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 292 | dst[x] = SkPixel32ToPixel16(ctable[*src]); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 293 | src += deltaSrc; |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 294 | } |
| 295 | return SkSwizzler::kOpaque_ResultAlpha; |
| 296 | } |
| 297 | |
| 298 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 299 | #undef A32_MASK_IN_PLACE |
| 300 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 301 | // kGray |
| 302 | |
| 303 | static SkSwizzler::ResultAlpha swizzle_gray_to_n32( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 304 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 305 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 306 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 307 | src += offset; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 308 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 309 | for (int x = 0; x < dstWidth; x++) { |
| 310 | dst[x] = SkPackARGB32NoCheck(0xFF, *src, *src, *src); |
| 311 | src += deltaSrc; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 312 | } |
| 313 | return SkSwizzler::kOpaque_ResultAlpha; |
| 314 | } |
| 315 | |
| 316 | static SkSwizzler::ResultAlpha swizzle_gray_to_gray( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 317 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 318 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 319 | |
| 320 | src += offset; |
| 321 | uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow; |
| 322 | if (1 == deltaSrc) { |
| 323 | memcpy(dstRow, src, dstWidth); |
| 324 | } else { |
| 325 | for (int x = 0; x < dstWidth; x++) { |
| 326 | dst[x] = src[0]; |
| 327 | src += deltaSrc; |
| 328 | } |
| 329 | } |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 330 | return SkSwizzler::kOpaque_ResultAlpha; |
| 331 | } |
| 332 | |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 333 | static SkSwizzler::ResultAlpha swizzle_gray_to_565( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 334 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 335 | int bytesPerPixel, int deltaSrc, int offset, const SkPMColor ctable[]) { |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 336 | // FIXME: Support dithering? |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 337 | src += offset; |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 338 | uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 339 | for (int x = 0; x < dstWidth; x++) { |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 340 | dst[x] = SkPack888ToRGB16(src[0], src[0], src[0]); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 341 | src += deltaSrc; |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 342 | } |
| 343 | return SkSwizzler::kOpaque_ResultAlpha; |
| 344 | } |
| 345 | |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 346 | // kBGRX |
| 347 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 348 | static SkSwizzler::ResultAlpha swizzle_bgrx_to_n32( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 349 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 350 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 351 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 352 | src += offset; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 353 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 354 | for (int x = 0; x < dstWidth; x++) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 355 | dst[x] = SkPackARGB32NoCheck(0xFF, src[2], src[1], src[0]); |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 356 | src += deltaSrc; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 357 | } |
| 358 | return SkSwizzler::kOpaque_ResultAlpha; |
| 359 | } |
| 360 | |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 361 | static SkSwizzler::ResultAlpha swizzle_bgrx_to_565( |
| 362 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 363 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 364 | // FIXME: Support dithering? |
| 365 | src += offset; |
| 366 | uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; |
| 367 | for (int x = 0; x < dstWidth; x++) { |
| 368 | dst[x] = SkPack888ToRGB16(src[2], src[1], src[0]); |
| 369 | src += deltaSrc; |
| 370 | } |
| 371 | return SkSwizzler::kOpaque_ResultAlpha; |
| 372 | } |
| 373 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 374 | // kBGRA |
| 375 | |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 376 | static SkSwizzler::ResultAlpha swizzle_bgra_to_n32_unpremul( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 377 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 378 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 379 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 380 | src += offset; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 381 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 382 | INIT_RESULT_ALPHA; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 383 | for (int x = 0; x < dstWidth; x++) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 384 | uint8_t alpha = src[3]; |
| 385 | UPDATE_RESULT_ALPHA(alpha); |
| 386 | dst[x] = SkPackARGB32NoCheck(alpha, src[2], src[1], src[0]); |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 387 | src += deltaSrc; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 388 | } |
| 389 | return COMPUTE_RESULT_ALPHA; |
| 390 | } |
| 391 | |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 392 | static SkSwizzler::ResultAlpha swizzle_bgra_to_n32_premul( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 393 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 394 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 395 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 396 | src += offset; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 397 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 398 | INIT_RESULT_ALPHA; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 399 | for (int x = 0; x < dstWidth; x++) { |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 400 | uint8_t alpha = src[3]; |
| 401 | UPDATE_RESULT_ALPHA(alpha); |
| 402 | dst[x] = SkPreMultiplyARGB(alpha, src[2], src[1], src[0]); |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 403 | src += deltaSrc; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 404 | } |
| 405 | return COMPUTE_RESULT_ALPHA; |
| 406 | } |
| 407 | |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 408 | // kRGBX |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 409 | static SkSwizzler::ResultAlpha swizzle_rgbx_to_n32( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 410 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 411 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 412 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 413 | src += offset; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 414 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 415 | for (int x = 0; x < dstWidth; x++) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 416 | dst[x] = SkPackARGB32(0xFF, src[0], src[1], src[2]); |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 417 | src += deltaSrc; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 418 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 419 | return SkSwizzler::kOpaque_ResultAlpha; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 420 | } |
| 421 | |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 422 | static SkSwizzler::ResultAlpha swizzle_rgbx_to_565( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 423 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 424 | int bytesPerPixel, int deltaSrc, int offset, const SkPMColor ctable[]) { |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 425 | // FIXME: Support dithering? |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 426 | src += offset; |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 427 | uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 428 | for (int x = 0; x < dstWidth; x++) { |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 429 | dst[x] = SkPack888ToRGB16(src[0], src[1], src[2]); |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 430 | src += deltaSrc; |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 431 | } |
| 432 | return SkSwizzler::kOpaque_ResultAlpha; |
| 433 | } |
| 434 | |
| 435 | |
| 436 | // kRGBA |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 437 | static SkSwizzler::ResultAlpha swizzle_rgba_to_n32_premul( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 438 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 439 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 440 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 441 | src += offset; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 442 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 443 | INIT_RESULT_ALPHA; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 444 | for (int x = 0; x < dstWidth; x++) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 445 | unsigned alpha = src[3]; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 446 | UPDATE_RESULT_ALPHA(alpha); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 447 | dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 448 | src += deltaSrc; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 449 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 450 | return COMPUTE_RESULT_ALPHA; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 451 | } |
| 452 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 453 | static SkSwizzler::ResultAlpha swizzle_rgba_to_n32_unpremul( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 454 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 455 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 456 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 457 | src += offset; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 458 | uint32_t* SK_RESTRICT dst = reinterpret_cast<uint32_t*>(dstRow); |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 459 | INIT_RESULT_ALPHA; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 460 | for (int x = 0; x < dstWidth; x++) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 461 | unsigned alpha = src[3]; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 462 | UPDATE_RESULT_ALPHA(alpha); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 463 | dst[x] = SkPackARGB32NoCheck(alpha, src[0], src[1], src[2]); |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 464 | src += deltaSrc; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 465 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 466 | return COMPUTE_RESULT_ALPHA; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 467 | } |
| 468 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 469 | static SkSwizzler::ResultAlpha swizzle_rgba_to_n32_premul_skipZ( |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 470 | void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int dstWidth, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 471 | int bpp, int deltaSrc, int offset, const SkPMColor ctable[]) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 472 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 473 | src += offset; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 474 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 475 | INIT_RESULT_ALPHA; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 476 | for (int x = 0; x < dstWidth; x++) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 477 | unsigned alpha = src[3]; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 478 | UPDATE_RESULT_ALPHA(alpha); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 479 | if (0 != alpha) { |
| 480 | dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); |
| 481 | } |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 482 | src += deltaSrc; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 483 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 484 | return COMPUTE_RESULT_ALPHA; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /** |
| 488 | FIXME: This was my idea to cheat in order to continue taking advantage of skipping zeroes. |
| 489 | This would be fine for drawing normally, but not for drawing with transfer modes. Being |
| 490 | honest means we can draw correctly with transfer modes, with the cost of not being able |
| 491 | to take advantage of Android's free unwritten pages. Something to keep in mind when we |
| 492 | decide whether to switch to unpremul default. |
| 493 | static bool swizzle_rgba_to_n32_unpremul_skipZ(void* SK_RESTRICT dstRow, |
| 494 | const uint8_t* SK_RESTRICT src, |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 495 | int dstWidth, int bitsPerPixel, int offset, |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 496 | const SkPMColor[]) { |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 497 | src += offset; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 498 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 499 | unsigned alphaMask = 0xFF; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 500 | for (int x = 0; x < dstWidth; x++) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 501 | unsigned alpha = src[3]; |
| 502 | // NOTE: We cheat here. The caller requested unpremul and skip zeroes. It's possible |
| 503 | // the color components are not zero, but we skip them anyway, meaning they'll remain |
| 504 | // zero (implied by the request to skip zeroes). |
| 505 | if (0 != alpha) { |
| 506 | dst[x] = SkPackARGB32NoCheck(alpha, src[0], src[1], src[2]); |
| 507 | } |
| 508 | src += deltaSrc; |
| 509 | alphaMask &= alpha; |
| 510 | } |
| 511 | return alphaMask != 0xFF; |
| 512 | } |
| 513 | */ |
| 514 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 515 | SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, |
| 516 | const SkPMColor* ctable, |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 517 | const SkImageInfo& dstInfo, |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 518 | SkCodec::ZeroInitialized zeroInit) { |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 519 | if (dstInfo.colorType() == kUnknown_SkColorType || kUnknown == sc) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 520 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 521 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 522 | if ((kIndex == sc || kIndex4 == sc || kIndex2 == sc || kIndex1 == sc) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 523 | && nullptr == ctable) { |
| 524 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 525 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 526 | RowProc proc = nullptr; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 527 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 528 | switch (sc) { |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 529 | case kBit: |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 530 | switch (dstInfo.colorType()) { |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 531 | case kN32_SkColorType: |
| 532 | proc = &swizzle_bit_to_n32; |
| 533 | break; |
| 534 | case kIndex_8_SkColorType: |
| 535 | proc = &swizzle_bit_to_index; |
| 536 | break; |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 537 | case kRGB_565_SkColorType: |
| 538 | proc = &swizzle_bit_to_565; |
| 539 | break; |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 540 | case kGray_8_SkColorType: |
| 541 | proc = &swizzle_bit_to_grayscale; |
| 542 | break; |
| 543 | default: |
| 544 | break; |
| 545 | } |
| 546 | break; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 547 | case kIndex1: |
| 548 | case kIndex2: |
| 549 | case kIndex4: |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 550 | switch (dstInfo.colorType()) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 551 | case kN32_SkColorType: |
| 552 | proc = &swizzle_small_index_to_n32; |
| 553 | break; |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 554 | case kRGB_565_SkColorType: |
| 555 | proc = &swizzle_small_index_to_565; |
| 556 | break; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 557 | case kIndex_8_SkColorType: |
| 558 | proc = &swizzle_small_index_to_index; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 559 | break; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 560 | default: |
| 561 | break; |
| 562 | } |
| 563 | break; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 564 | case kIndex: |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 565 | switch (dstInfo.colorType()) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 566 | case kN32_SkColorType: |
scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 567 | // We assume the color premultiplied ctable (or not) as desired. |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 568 | if (SkCodec::kYes_ZeroInitialized == zeroInit) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 569 | proc = &swizzle_index_to_n32_skipZ; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 570 | break; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 571 | } else { |
| 572 | proc = &swizzle_index_to_n32; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 573 | break; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 574 | } |
| 575 | break; |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 576 | case kRGB_565_SkColorType: |
| 577 | proc = &swizzle_index_to_565; |
| 578 | break; |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 579 | case kIndex_8_SkColorType: |
| 580 | proc = &swizzle_index_to_index; |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 581 | break; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 582 | default: |
| 583 | break; |
| 584 | } |
| 585 | break; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 586 | case kGray: |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 587 | switch (dstInfo.colorType()) { |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 588 | case kN32_SkColorType: |
| 589 | proc = &swizzle_gray_to_n32; |
| 590 | break; |
| 591 | case kGray_8_SkColorType: |
| 592 | proc = &swizzle_gray_to_gray; |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 593 | break; |
| 594 | case kRGB_565_SkColorType: |
| 595 | proc = &swizzle_gray_to_565; |
| 596 | break; |
msarett | e16b04a | 2015-04-15 07:32:19 -0700 | [diff] [blame] | 597 | default: |
| 598 | break; |
| 599 | } |
| 600 | break; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 601 | case kBGR: |
| 602 | case kBGRX: |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 603 | switch (dstInfo.colorType()) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 604 | case kN32_SkColorType: |
| 605 | proc = &swizzle_bgrx_to_n32; |
| 606 | break; |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 607 | case kRGB_565_SkColorType: |
| 608 | proc = &swizzle_bgrx_to_565; |
| 609 | break; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 610 | default: |
| 611 | break; |
| 612 | } |
| 613 | break; |
| 614 | case kBGRA: |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 615 | switch (dstInfo.colorType()) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 616 | case kN32_SkColorType: |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 617 | switch (dstInfo.alphaType()) { |
msarett | eed039b | 2015-03-18 11:11:19 -0700 | [diff] [blame] | 618 | case kUnpremul_SkAlphaType: |
| 619 | proc = &swizzle_bgra_to_n32_unpremul; |
| 620 | break; |
| 621 | case kPremul_SkAlphaType: |
| 622 | proc = &swizzle_bgra_to_n32_premul; |
| 623 | break; |
| 624 | default: |
| 625 | break; |
| 626 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 627 | break; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 628 | default: |
| 629 | break; |
| 630 | } |
| 631 | break; |
| 632 | case kRGBX: |
| 633 | // TODO: Support other swizzles. |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 634 | switch (dstInfo.colorType()) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 635 | case kN32_SkColorType: |
| 636 | proc = &swizzle_rgbx_to_n32; |
| 637 | break; |
scroggo | ab60c5b | 2015-08-06 06:08:18 -0700 | [diff] [blame] | 638 | case kRGB_565_SkColorType: |
| 639 | proc = &swizzle_rgbx_to_565; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 640 | default: |
| 641 | break; |
| 642 | } |
| 643 | break; |
| 644 | case kRGBA: |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 645 | switch (dstInfo.colorType()) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 646 | case kN32_SkColorType: |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 647 | if (dstInfo.alphaType() == kUnpremul_SkAlphaType) { |
scroggo | 9552662 | 2015-03-17 05:02:17 -0700 | [diff] [blame] | 648 | // Respect zeroInit? |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 649 | proc = &swizzle_rgba_to_n32_unpremul; |
| 650 | } else { |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 651 | if (SkCodec::kYes_ZeroInitialized == zeroInit) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 652 | proc = &swizzle_rgba_to_n32_premul_skipZ; |
| 653 | } else { |
| 654 | proc = &swizzle_rgba_to_n32_premul; |
| 655 | } |
| 656 | } |
| 657 | break; |
| 658 | default: |
| 659 | break; |
| 660 | } |
| 661 | break; |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 662 | case kRGB: |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 663 | switch (dstInfo.colorType()) { |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 664 | case kN32_SkColorType: |
| 665 | proc = &swizzle_rgbx_to_n32; |
| 666 | break; |
| 667 | default: |
| 668 | break; |
| 669 | } |
| 670 | break; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 671 | case kRGB_565: |
| 672 | switch (dstInfo.colorType()) { |
| 673 | case kRGB_565_SkColorType: |
| 674 | proc = &sample565; |
| 675 | break; |
| 676 | default: |
| 677 | break; |
| 678 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 679 | default: |
| 680 | break; |
| 681 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 682 | if (nullptr == proc) { |
| 683 | return nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 684 | } |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 685 | |
| 686 | // Store deltaSrc in bytes if it is an even multiple, otherwise use bits |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 687 | int deltaSrc = SkIsAlign8(BitsPerPixel(sc)) ? BytesPerPixel(sc) : BitsPerPixel(sc); |
| 688 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 689 | return new SkSwizzler(proc, ctable, deltaSrc, dstInfo.width()); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 690 | } |
| 691 | |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 692 | SkSwizzler::SkSwizzler(RowProc proc, const SkPMColor* ctable, |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 693 | int deltaSrc, int srcWidth) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 694 | : fRowProc(proc) |
| 695 | , fColorTable(ctable) |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 696 | , fDeltaSrc(deltaSrc) |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 697 | , fSrcWidth(srcWidth) |
| 698 | , fDstWidth(srcWidth) |
| 699 | , fSampleX(1) |
| 700 | , fX0(0) |
| 701 | {} |
| 702 | |
| 703 | int SkSwizzler::onSetSampleX(int sampleX) { |
| 704 | SkASSERT(sampleX > 0); // Surely there is an upper limit? Should there be |
| 705 | // way to report failure? |
| 706 | fSampleX = sampleX; |
| 707 | fX0 = get_start_coord(sampleX); |
| 708 | fDstWidth = get_scaled_dimension(fSrcWidth, sampleX); |
| 709 | |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 710 | // check that fX0 is less than original width |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 711 | SkASSERT(fX0 >= 0 && fX0 < fSrcWidth); |
| 712 | return fDstWidth; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 713 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 714 | |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 715 | SkSwizzler::ResultAlpha SkSwizzler::swizzle(void* dst, const uint8_t* SK_RESTRICT src) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 716 | SkASSERT(nullptr != dst && nullptr != src); |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 717 | return fRowProc(dst, src, fDstWidth, fDeltaSrc, fSampleX * fDeltaSrc, |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 718 | fX0 * fDeltaSrc, fColorTable); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 719 | } |