commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 8 | #include "SkBlitMask.h" |
| 9 | #include "SkColor.h" |
| 10 | #include "SkColorPriv.h" |
mtklein | 4977983 | 2015-08-10 12:58:17 -0700 | [diff] [blame^] | 11 | #include "SkOpts.h" |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 12 | |
tomhudson@google.com | d6770e6 | 2012-02-14 16:01:15 +0000 | [diff] [blame] | 13 | SkBlitMask::BlitLCD16RowProc SkBlitMask::BlitLCD16RowFactory(bool isOpaque) { |
| 14 | BlitLCD16RowProc proc = PlatformBlitRowProcs16(isOpaque); |
| 15 | if (proc) { |
| 16 | return proc; |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 17 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 18 | |
tomhudson@google.com | d6770e6 | 2012-02-14 16:01:15 +0000 | [diff] [blame] | 19 | if (isOpaque) { |
| 20 | return SkBlitLCD16OpaqueRow; |
| 21 | } else { |
| 22 | return SkBlitLCD16Row; |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 23 | } |
| 24 | } |
| 25 | |
| 26 | static void D32_LCD16_Proc(void* SK_RESTRICT dst, size_t dstRB, |
| 27 | const void* SK_RESTRICT mask, size_t maskRB, |
| 28 | SkColor color, int width, int height) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 29 | |
| 30 | SkPMColor* dstRow = (SkPMColor*)dst; |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 31 | const uint16_t* srcRow = (const uint16_t*)mask; |
| 32 | SkPMColor opaqueDst; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 33 | |
tomhudson@google.com | d6770e6 | 2012-02-14 16:01:15 +0000 | [diff] [blame] | 34 | SkBlitMask::BlitLCD16RowProc proc = NULL; |
| 35 | bool isOpaque = (0xFF == SkColorGetA(color)); |
| 36 | proc = SkBlitMask::BlitLCD16RowFactory(isOpaque); |
| 37 | SkASSERT(proc != NULL); |
| 38 | |
| 39 | if (isOpaque) { |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 40 | opaqueDst = SkPreMultiplyColor(color); |
| 41 | } else { |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 42 | opaqueDst = 0; // ignored |
| 43 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 44 | |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 45 | do { |
| 46 | proc(dstRow, srcRow, color, width, opaqueDst); |
| 47 | dstRow = (SkPMColor*)((char*)dstRow + dstRB); |
| 48 | srcRow = (const uint16_t*)((const char*)srcRow + maskRB); |
| 49 | } while (--height != 0); |
| 50 | } |
| 51 | |
| 52 | /////////////////////////////////////////////////////////////////////////////// |
| 53 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 54 | bool SkBlitMask::BlitColor(const SkPixmap& device, const SkMask& mask, |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 55 | const SkIRect& clip, SkColor color) { |
mtklein | 4977983 | 2015-08-10 12:58:17 -0700 | [diff] [blame^] | 56 | int x = clip.fLeft, y = clip.fTop; |
| 57 | |
| 58 | if (device.colorType() == kN32_SkColorType && mask.fFormat == SkMask::kA8_Format) { |
| 59 | SkOpts::blit_mask_d32_a8(device.writable_addr32(x,y), device.rowBytes(), |
| 60 | (const SkAlpha*)mask.getAddr(x,y), mask.fRowBytes, |
| 61 | color, clip.width(), clip.height()); |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 62 | return true; |
| 63 | } |
mtklein | 4977983 | 2015-08-10 12:58:17 -0700 | [diff] [blame^] | 64 | |
| 65 | if (device.colorType() == kN32_SkColorType && mask.fFormat == SkMask::kLCD16_Format) { |
| 66 | // TODO: Is this reachable code? Seems like no. |
| 67 | D32_LCD16_Proc(device.writable_addr32(x,y), device.rowBytes(), |
| 68 | mask.getAddr(x,y), mask.fRowBytes, |
| 69 | color, clip.width(), clip.height()); |
| 70 | return true; |
| 71 | } |
| 72 | |
reed@google.com | edb606c | 2011-10-18 13:56:50 +0000 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 76 | /////////////////////////////////////////////////////////////////////////////// |
| 77 | /////////////////////////////////////////////////////////////////////////////// |
| 78 | |
| 79 | static void BW_RowProc_Blend(SkPMColor* SK_RESTRICT dst, |
| 80 | const uint8_t* SK_RESTRICT mask, |
| 81 | const SkPMColor* SK_RESTRICT src, int count) { |
| 82 | int i, octuple = (count + 7) >> 3; |
| 83 | for (i = 0; i < octuple; ++i) { |
| 84 | int m = *mask++; |
| 85 | if (m & 0x80) { dst[0] = SkPMSrcOver(src[0], dst[0]); } |
| 86 | if (m & 0x40) { dst[1] = SkPMSrcOver(src[1], dst[1]); } |
| 87 | if (m & 0x20) { dst[2] = SkPMSrcOver(src[2], dst[2]); } |
| 88 | if (m & 0x10) { dst[3] = SkPMSrcOver(src[3], dst[3]); } |
| 89 | if (m & 0x08) { dst[4] = SkPMSrcOver(src[4], dst[4]); } |
| 90 | if (m & 0x04) { dst[5] = SkPMSrcOver(src[5], dst[5]); } |
| 91 | if (m & 0x02) { dst[6] = SkPMSrcOver(src[6], dst[6]); } |
| 92 | if (m & 0x01) { dst[7] = SkPMSrcOver(src[7], dst[7]); } |
| 93 | src += 8; |
| 94 | dst += 8; |
| 95 | } |
| 96 | count &= 7; |
| 97 | if (count > 0) { |
| 98 | int m = *mask; |
| 99 | do { |
| 100 | if (m & 0x80) { dst[0] = SkPMSrcOver(src[0], dst[0]); } |
| 101 | m <<= 1; |
| 102 | src += 1; |
| 103 | dst += 1; |
| 104 | } while (--count > 0); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void BW_RowProc_Opaque(SkPMColor* SK_RESTRICT dst, |
| 109 | const uint8_t* SK_RESTRICT mask, |
| 110 | const SkPMColor* SK_RESTRICT src, int count) { |
| 111 | int i, octuple = (count + 7) >> 3; |
| 112 | for (i = 0; i < octuple; ++i) { |
| 113 | int m = *mask++; |
| 114 | if (m & 0x80) { dst[0] = src[0]; } |
| 115 | if (m & 0x40) { dst[1] = src[1]; } |
| 116 | if (m & 0x20) { dst[2] = src[2]; } |
| 117 | if (m & 0x10) { dst[3] = src[3]; } |
| 118 | if (m & 0x08) { dst[4] = src[4]; } |
| 119 | if (m & 0x04) { dst[5] = src[5]; } |
| 120 | if (m & 0x02) { dst[6] = src[6]; } |
| 121 | if (m & 0x01) { dst[7] = src[7]; } |
| 122 | src += 8; |
| 123 | dst += 8; |
| 124 | } |
| 125 | count &= 7; |
| 126 | if (count > 0) { |
| 127 | int m = *mask; |
| 128 | do { |
| 129 | if (m & 0x80) { dst[0] = SkPMSrcOver(src[0], dst[0]); } |
| 130 | m <<= 1; |
| 131 | src += 1; |
| 132 | dst += 1; |
| 133 | } while (--count > 0); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void A8_RowProc_Blend(SkPMColor* SK_RESTRICT dst, |
| 138 | const uint8_t* SK_RESTRICT mask, |
| 139 | const SkPMColor* SK_RESTRICT src, int count) { |
| 140 | for (int i = 0; i < count; ++i) { |
| 141 | if (mask[i]) { |
| 142 | dst[i] = SkBlendARGB32(src[i], dst[i], mask[i]); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // expand the steps that SkAlphaMulQ performs, but this way we can |
| 148 | // exand.. add.. combine |
| 149 | // instead of |
| 150 | // expand..combine add expand..combine |
| 151 | // |
| 152 | #define EXPAND0(v, m, s) ((v) & (m)) * (s) |
| 153 | #define EXPAND1(v, m, s) (((v) >> 8) & (m)) * (s) |
| 154 | #define COMBINE(e0, e1, m) ((((e0) >> 8) & (m)) | ((e1) & ~(m))) |
| 155 | |
| 156 | static void A8_RowProc_Opaque(SkPMColor* SK_RESTRICT dst, |
| 157 | const uint8_t* SK_RESTRICT mask, |
| 158 | const SkPMColor* SK_RESTRICT src, int count) { |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 159 | for (int i = 0; i < count; ++i) { |
| 160 | int m = mask[i]; |
| 161 | if (m) { |
| 162 | m += (m >> 7); |
reed@google.com | 2204862 | 2011-11-18 18:36:07 +0000 | [diff] [blame] | 163 | #if 1 |
| 164 | // this is slightly slower than the expand/combine version, but it |
| 165 | // is much closer to the old results, so we use it for now to reduce |
| 166 | // rebaselining. |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 167 | dst[i] = SkAlphaMulQ(src[i], m) + SkAlphaMulQ(dst[i], 256 - m); |
| 168 | #else |
| 169 | uint32_t v = src[i]; |
| 170 | uint32_t s0 = EXPAND0(v, rbmask, m); |
| 171 | uint32_t s1 = EXPAND1(v, rbmask, m); |
| 172 | v = dst[i]; |
| 173 | uint32_t d0 = EXPAND0(v, rbmask, m); |
| 174 | uint32_t d1 = EXPAND1(v, rbmask, m); |
| 175 | dst[i] = COMBINE(s0 + d0, s1 + d1, rbmask); |
| 176 | #endif |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
reed@google.com | 181f4b3 | 2011-11-30 14:49:24 +0000 | [diff] [blame] | 181 | static int upscale31To255(int value) { |
| 182 | value = (value << 3) | (value >> 2); |
| 183 | return value; |
| 184 | } |
| 185 | |
reed@google.com | 181f4b3 | 2011-11-30 14:49:24 +0000 | [diff] [blame] | 186 | static int src_alpha_blend(int src, int dst, int srcA, int mask) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 187 | |
commit-bot@chromium.org | 12e0cda | 2013-07-15 12:40:54 +0000 | [diff] [blame] | 188 | return dst + SkAlphaMul(src - SkAlphaMul(srcA, dst), mask); |
reed@google.com | 181f4b3 | 2011-11-30 14:49:24 +0000 | [diff] [blame] | 189 | } |
| 190 | |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 191 | static void LCD16_RowProc_Blend(SkPMColor* SK_RESTRICT dst, |
reed@google.com | 5e12770 | 2011-11-28 16:32:01 +0000 | [diff] [blame] | 192 | const uint16_t* SK_RESTRICT mask, |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 193 | const SkPMColor* SK_RESTRICT src, int count) { |
| 194 | for (int i = 0; i < count; ++i) { |
| 195 | uint16_t m = mask[i]; |
| 196 | if (0 == m) { |
| 197 | continue; |
| 198 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 199 | |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 200 | SkPMColor s = src[i]; |
| 201 | SkPMColor d = dst[i]; |
| 202 | |
| 203 | int srcA = SkGetPackedA32(s); |
| 204 | int srcR = SkGetPackedR32(s); |
reed@google.com | 181f4b3 | 2011-11-30 14:49:24 +0000 | [diff] [blame] | 205 | int srcG = SkGetPackedG32(s); |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 206 | int srcB = SkGetPackedB32(s); |
reed@google.com | 181f4b3 | 2011-11-30 14:49:24 +0000 | [diff] [blame] | 207 | |
| 208 | srcA += srcA >> 7; |
| 209 | |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 210 | /* We want all of these in 5bits, hence the shifts in case one of them |
| 211 | * (green) is 6bits. |
| 212 | */ |
| 213 | int maskR = SkGetPackedR16(m) >> (SK_R16_BITS - 5); |
| 214 | int maskG = SkGetPackedG16(m) >> (SK_G16_BITS - 5); |
| 215 | int maskB = SkGetPackedB16(m) >> (SK_B16_BITS - 5); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 216 | |
reed@google.com | 181f4b3 | 2011-11-30 14:49:24 +0000 | [diff] [blame] | 217 | maskR = upscale31To255(maskR); |
| 218 | maskG = upscale31To255(maskG); |
| 219 | maskB = upscale31To255(maskB); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 220 | |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 221 | int dstR = SkGetPackedR32(d); |
| 222 | int dstG = SkGetPackedG32(d); |
| 223 | int dstB = SkGetPackedB32(d); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 224 | |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 225 | // LCD blitting is only supported if the dst is known/required |
| 226 | // to be opaque |
| 227 | dst[i] = SkPackARGB32(0xFF, |
reed@google.com | 181f4b3 | 2011-11-30 14:49:24 +0000 | [diff] [blame] | 228 | src_alpha_blend(srcR, dstR, srcA, maskR), |
| 229 | src_alpha_blend(srcG, dstG, srcA, maskG), |
| 230 | src_alpha_blend(srcB, dstB, srcA, maskB)); |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
| 234 | static void LCD16_RowProc_Opaque(SkPMColor* SK_RESTRICT dst, |
reed@google.com | 5e12770 | 2011-11-28 16:32:01 +0000 | [diff] [blame] | 235 | const uint16_t* SK_RESTRICT mask, |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 236 | const SkPMColor* SK_RESTRICT src, int count) { |
| 237 | for (int i = 0; i < count; ++i) { |
| 238 | uint16_t m = mask[i]; |
| 239 | if (0 == m) { |
| 240 | continue; |
| 241 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 242 | |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 243 | SkPMColor s = src[i]; |
| 244 | SkPMColor d = dst[i]; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 245 | |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 246 | int srcR = SkGetPackedR32(s); |
reed@google.com | 181f4b3 | 2011-11-30 14:49:24 +0000 | [diff] [blame] | 247 | int srcG = SkGetPackedG32(s); |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 248 | int srcB = SkGetPackedB32(s); |
reed@google.com | 181f4b3 | 2011-11-30 14:49:24 +0000 | [diff] [blame] | 249 | |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 250 | /* We want all of these in 5bits, hence the shifts in case one of them |
| 251 | * (green) is 6bits. |
| 252 | */ |
| 253 | int maskR = SkGetPackedR16(m) >> (SK_R16_BITS - 5); |
| 254 | int maskG = SkGetPackedG16(m) >> (SK_G16_BITS - 5); |
| 255 | int maskB = SkGetPackedB16(m) >> (SK_B16_BITS - 5); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 256 | |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 257 | // Now upscale them to 0..32, so we can use blend32 |
tomhudson@google.com | d6770e6 | 2012-02-14 16:01:15 +0000 | [diff] [blame] | 258 | maskR = SkUpscale31To32(maskR); |
| 259 | maskG = SkUpscale31To32(maskG); |
| 260 | maskB = SkUpscale31To32(maskB); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 261 | |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 262 | int dstR = SkGetPackedR32(d); |
| 263 | int dstG = SkGetPackedG32(d); |
| 264 | int dstB = SkGetPackedB32(d); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 265 | |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 266 | // LCD blitting is only supported if the dst is known/required |
| 267 | // to be opaque |
| 268 | dst[i] = SkPackARGB32(0xFF, |
tomhudson@google.com | d6770e6 | 2012-02-14 16:01:15 +0000 | [diff] [blame] | 269 | SkBlend32(srcR, dstR, maskR), |
| 270 | SkBlend32(srcG, dstG, maskG), |
| 271 | SkBlend32(srcB, dstB, maskB)); |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 275 | SkBlitMask::RowProc SkBlitMask::RowFactory(SkColorType ct, |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 276 | SkMask::Format format, |
| 277 | RowFlags flags) { |
reed@google.com | d4780e2 | 2011-11-15 20:49:52 +0000 | [diff] [blame] | 278 | // make this opt-in until chrome can rebaseline |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 279 | RowProc proc = PlatformRowProcs(ct, format, flags); |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 280 | if (proc) { |
| 281 | return proc; |
| 282 | } |
| 283 | |
| 284 | static const RowProc gProcs[] = { |
| 285 | // need X coordinate to handle BW |
caryclark@google.com | 803eceb | 2012-06-06 12:09:34 +0000 | [diff] [blame] | 286 | false ? (RowProc)BW_RowProc_Blend : NULL, // suppress unused warning |
| 287 | false ? (RowProc)BW_RowProc_Opaque : NULL, // suppress unused warning |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 288 | (RowProc)A8_RowProc_Blend, (RowProc)A8_RowProc_Opaque, |
| 289 | (RowProc)LCD16_RowProc_Blend, (RowProc)LCD16_RowProc_Opaque, |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 290 | }; |
| 291 | |
| 292 | int index; |
commit-bot@chromium.org | cba7378 | 2014-05-29 15:57:47 +0000 | [diff] [blame] | 293 | switch (ct) { |
| 294 | case kN32_SkColorType: |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 295 | switch (format) { |
| 296 | case SkMask::kBW_Format: index = 0; break; |
| 297 | case SkMask::kA8_Format: index = 2; break; |
| 298 | case SkMask::kLCD16_Format: index = 4; break; |
reed@google.com | 1750bf1 | 2011-11-15 19:51:02 +0000 | [diff] [blame] | 299 | default: |
| 300 | return NULL; |
| 301 | } |
| 302 | if (flags & kSrcIsOpaque_RowFlag) { |
| 303 | index |= 1; |
| 304 | } |
| 305 | SkASSERT((size_t)index < SK_ARRAY_COUNT(gProcs)); |
| 306 | return gProcs[index]; |
| 307 | default: |
| 308 | break; |
| 309 | } |
| 310 | return NULL; |
| 311 | } |