1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/core/SkBlitter_A8.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,431 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2006 The Android Open Source Project 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#include "SkCoreBlitters.h" 1.14 +#include "SkColorPriv.h" 1.15 +#include "SkShader.h" 1.16 +#include "SkXfermode.h" 1.17 + 1.18 +SkA8_Blitter::SkA8_Blitter(const SkBitmap& device, const SkPaint& paint) 1.19 + : INHERITED(device) { 1.20 + fSrcA = paint.getAlpha(); 1.21 +} 1.22 + 1.23 +const SkBitmap* SkA8_Blitter::justAnOpaqueColor(uint32_t* value) { 1.24 + if (255 == fSrcA) { 1.25 + *value = 255; 1.26 + return &fDevice; 1.27 + } 1.28 + return NULL; 1.29 +} 1.30 + 1.31 +void SkA8_Blitter::blitH(int x, int y, int width) { 1.32 + SkASSERT(x >= 0 && y >= 0 && 1.33 + (unsigned)(x + width) <= (unsigned)fDevice.width()); 1.34 + 1.35 + if (fSrcA == 0) { 1.36 + return; 1.37 + } 1.38 + 1.39 + uint8_t* device = fDevice.getAddr8(x, y); 1.40 + 1.41 + if (fSrcA == 255) { 1.42 + memset(device, 0xFF, width); 1.43 + } else { 1.44 + unsigned scale = 256 - SkAlpha255To256(fSrcA); 1.45 + unsigned srcA = fSrcA; 1.46 + 1.47 + for (int i = 0; i < width; i++) { 1.48 + device[i] = SkToU8(srcA + SkAlphaMul(device[i], scale)); 1.49 + } 1.50 + } 1.51 +} 1.52 + 1.53 +void SkA8_Blitter::blitAntiH(int x, int y, const SkAlpha antialias[], 1.54 + const int16_t runs[]) { 1.55 + if (fSrcA == 0) { 1.56 + return; 1.57 + } 1.58 + 1.59 + uint8_t* device = fDevice.getAddr8(x, y); 1.60 + unsigned srcA = fSrcA; 1.61 + 1.62 + for (;;) { 1.63 + int count = runs[0]; 1.64 + SkASSERT(count >= 0); 1.65 + if (count == 0) { 1.66 + return; 1.67 + } 1.68 + unsigned aa = antialias[0]; 1.69 + 1.70 + if (aa == 255 && srcA == 255) { 1.71 + memset(device, 0xFF, count); 1.72 + } else { 1.73 + unsigned sa = SkAlphaMul(srcA, SkAlpha255To256(aa)); 1.74 + unsigned scale = 256 - sa; 1.75 + 1.76 + for (int i = 0; i < count; i++) { 1.77 + device[i] = SkToU8(sa + SkAlphaMul(device[i], scale)); 1.78 + } 1.79 + } 1.80 + runs += count; 1.81 + antialias += count; 1.82 + device += count; 1.83 + } 1.84 +} 1.85 + 1.86 +///////////////////////////////////////////////////////////////////////////////////// 1.87 + 1.88 +#define solid_8_pixels(mask, dst) \ 1.89 + do { \ 1.90 + if (mask & 0x80) dst[0] = 0xFF; \ 1.91 + if (mask & 0x40) dst[1] = 0xFF; \ 1.92 + if (mask & 0x20) dst[2] = 0xFF; \ 1.93 + if (mask & 0x10) dst[3] = 0xFF; \ 1.94 + if (mask & 0x08) dst[4] = 0xFF; \ 1.95 + if (mask & 0x04) dst[5] = 0xFF; \ 1.96 + if (mask & 0x02) dst[6] = 0xFF; \ 1.97 + if (mask & 0x01) dst[7] = 0xFF; \ 1.98 + } while (0) 1.99 + 1.100 +#define SK_BLITBWMASK_NAME SkA8_BlitBW 1.101 +#define SK_BLITBWMASK_ARGS 1.102 +#define SK_BLITBWMASK_BLIT8(mask, dst) solid_8_pixels(mask, dst) 1.103 +#define SK_BLITBWMASK_GETADDR getAddr8 1.104 +#define SK_BLITBWMASK_DEVTYPE uint8_t 1.105 +#include "SkBlitBWMaskTemplate.h" 1.106 + 1.107 +static inline void blend_8_pixels(U8CPU bw, uint8_t dst[], U8CPU sa, 1.108 + unsigned dst_scale) { 1.109 + if (bw & 0x80) dst[0] = SkToU8(sa + SkAlphaMul(dst[0], dst_scale)); 1.110 + if (bw & 0x40) dst[1] = SkToU8(sa + SkAlphaMul(dst[1], dst_scale)); 1.111 + if (bw & 0x20) dst[2] = SkToU8(sa + SkAlphaMul(dst[2], dst_scale)); 1.112 + if (bw & 0x10) dst[3] = SkToU8(sa + SkAlphaMul(dst[3], dst_scale)); 1.113 + if (bw & 0x08) dst[4] = SkToU8(sa + SkAlphaMul(dst[4], dst_scale)); 1.114 + if (bw & 0x04) dst[5] = SkToU8(sa + SkAlphaMul(dst[5], dst_scale)); 1.115 + if (bw & 0x02) dst[6] = SkToU8(sa + SkAlphaMul(dst[6], dst_scale)); 1.116 + if (bw & 0x01) dst[7] = SkToU8(sa + SkAlphaMul(dst[7], dst_scale)); 1.117 +} 1.118 + 1.119 +#define SK_BLITBWMASK_NAME SkA8_BlendBW 1.120 +#define SK_BLITBWMASK_ARGS , U8CPU sa, unsigned dst_scale 1.121 +#define SK_BLITBWMASK_BLIT8(mask, dst) blend_8_pixels(mask, dst, sa, dst_scale) 1.122 +#define SK_BLITBWMASK_GETADDR getAddr8 1.123 +#define SK_BLITBWMASK_DEVTYPE uint8_t 1.124 +#include "SkBlitBWMaskTemplate.h" 1.125 + 1.126 +void SkA8_Blitter::blitMask(const SkMask& mask, const SkIRect& clip) { 1.127 + if (fSrcA == 0) { 1.128 + return; 1.129 + } 1.130 + 1.131 + if (mask.fFormat == SkMask::kBW_Format) { 1.132 + if (fSrcA == 0xFF) { 1.133 + SkA8_BlitBW(fDevice, mask, clip); 1.134 + } else { 1.135 + SkA8_BlendBW(fDevice, mask, clip, fSrcA, 1.136 + SkAlpha255To256(255 - fSrcA)); 1.137 + } 1.138 + return; 1.139 + } 1.140 + 1.141 + int x = clip.fLeft; 1.142 + int y = clip.fTop; 1.143 + int width = clip.width(); 1.144 + int height = clip.height(); 1.145 + uint8_t* device = fDevice.getAddr8(x, y); 1.146 + const uint8_t* alpha = mask.getAddr8(x, y); 1.147 + unsigned srcA = fSrcA; 1.148 + 1.149 + while (--height >= 0) { 1.150 + for (int i = width - 1; i >= 0; --i) { 1.151 + unsigned sa; 1.152 + // scale our src by the alpha value 1.153 + { 1.154 + int aa = alpha[i]; 1.155 + if (aa == 0) { 1.156 + continue; 1.157 + } 1.158 + if (aa == 255) { 1.159 + if (srcA == 255) { 1.160 + device[i] = 0xFF; 1.161 + continue; 1.162 + } 1.163 + sa = srcA; 1.164 + } else { 1.165 + sa = SkAlphaMul(srcA, SkAlpha255To256(aa)); 1.166 + } 1.167 + } 1.168 + 1.169 + int scale = 256 - SkAlpha255To256(sa); 1.170 + device[i] = SkToU8(sa + SkAlphaMul(device[i], scale)); 1.171 + } 1.172 + device += fDevice.rowBytes(); 1.173 + alpha += mask.fRowBytes; 1.174 + } 1.175 +} 1.176 + 1.177 +/////////////////////////////////////////////////////////////////////////////// 1.178 + 1.179 +void SkA8_Blitter::blitV(int x, int y, int height, SkAlpha alpha) { 1.180 + if (fSrcA == 0) { 1.181 + return; 1.182 + } 1.183 + 1.184 + unsigned sa = SkAlphaMul(fSrcA, SkAlpha255To256(alpha)); 1.185 + uint8_t* device = fDevice.getAddr8(x, y); 1.186 + size_t rowBytes = fDevice.rowBytes(); 1.187 + 1.188 + if (sa == 0xFF) { 1.189 + for (int i = 0; i < height; i++) { 1.190 + *device = SkToU8(sa); 1.191 + device += rowBytes; 1.192 + } 1.193 + } else { 1.194 + unsigned scale = 256 - SkAlpha255To256(sa); 1.195 + 1.196 + for (int i = 0; i < height; i++) { 1.197 + *device = SkToU8(sa + SkAlphaMul(*device, scale)); 1.198 + device += rowBytes; 1.199 + } 1.200 + } 1.201 +} 1.202 + 1.203 +void SkA8_Blitter::blitRect(int x, int y, int width, int height) { 1.204 + SkASSERT(x >= 0 && y >= 0 && 1.205 + (unsigned)(x + width) <= (unsigned)fDevice.width() && 1.206 + (unsigned)(y + height) <= (unsigned)fDevice.height()); 1.207 + 1.208 + if (fSrcA == 0) { 1.209 + return; 1.210 + } 1.211 + 1.212 + uint8_t* device = fDevice.getAddr8(x, y); 1.213 + unsigned srcA = fSrcA; 1.214 + 1.215 + if (srcA == 255) { 1.216 + while (--height >= 0) { 1.217 + memset(device, 0xFF, width); 1.218 + device += fDevice.rowBytes(); 1.219 + } 1.220 + } else { 1.221 + unsigned scale = 256 - SkAlpha255To256(srcA); 1.222 + 1.223 + while (--height >= 0) { 1.224 + for (int i = 0; i < width; i++) { 1.225 + device[i] = SkToU8(srcA + SkAlphaMul(device[i], scale)); 1.226 + } 1.227 + device += fDevice.rowBytes(); 1.228 + } 1.229 + } 1.230 +} 1.231 + 1.232 +/////////////////////////////////////////////////////////////////////// 1.233 + 1.234 +SkA8_Shader_Blitter::SkA8_Shader_Blitter(const SkBitmap& device, const SkPaint& paint) 1.235 + : INHERITED(device, paint) { 1.236 + if ((fXfermode = paint.getXfermode()) != NULL) { 1.237 + fXfermode->ref(); 1.238 + SkASSERT(fShader); 1.239 + } 1.240 + 1.241 + int width = device.width(); 1.242 + fBuffer = (SkPMColor*)sk_malloc_throw(sizeof(SkPMColor) * (width + (SkAlign4(width) >> 2))); 1.243 + fAAExpand = (uint8_t*)(fBuffer + width); 1.244 +} 1.245 + 1.246 +SkA8_Shader_Blitter::~SkA8_Shader_Blitter() { 1.247 + if (fXfermode) SkSafeUnref(fXfermode); 1.248 + sk_free(fBuffer); 1.249 +} 1.250 + 1.251 +void SkA8_Shader_Blitter::blitH(int x, int y, int width) { 1.252 + SkASSERT(x >= 0 && y >= 0 && 1.253 + (unsigned)(x + width) <= (unsigned)fDevice.width()); 1.254 + 1.255 + uint8_t* device = fDevice.getAddr8(x, y); 1.256 + 1.257 + if ((fShader->getFlags() & SkShader::kOpaqueAlpha_Flag) && !fXfermode) { 1.258 + memset(device, 0xFF, width); 1.259 + } else { 1.260 + SkPMColor* span = fBuffer; 1.261 + 1.262 + fShader->shadeSpan(x, y, span, width); 1.263 + if (fXfermode) { 1.264 + fXfermode->xferA8(device, span, width, NULL); 1.265 + } else { 1.266 + for (int i = width - 1; i >= 0; --i) { 1.267 + unsigned srcA = SkGetPackedA32(span[i]); 1.268 + unsigned scale = 256 - SkAlpha255To256(srcA); 1.269 + 1.270 + device[i] = SkToU8(srcA + SkAlphaMul(device[i], scale)); 1.271 + } 1.272 + } 1.273 + } 1.274 +} 1.275 + 1.276 +static inline uint8_t aa_blend8(SkPMColor src, U8CPU da, int aa) { 1.277 + SkASSERT((unsigned)aa <= 255); 1.278 + 1.279 + int src_scale = SkAlpha255To256(aa); 1.280 + int sa = SkGetPackedA32(src); 1.281 + int dst_scale = 256 - SkAlphaMul(sa, src_scale); 1.282 + 1.283 + return SkToU8((sa * src_scale + da * dst_scale) >> 8); 1.284 +} 1.285 + 1.286 +void SkA8_Shader_Blitter::blitAntiH(int x, int y, const SkAlpha antialias[], 1.287 + const int16_t runs[]) { 1.288 + SkShader* shader = fShader; 1.289 + SkXfermode* mode = fXfermode; 1.290 + uint8_t* aaExpand = fAAExpand; 1.291 + SkPMColor* span = fBuffer; 1.292 + uint8_t* device = fDevice.getAddr8(x, y); 1.293 + int opaque = fShader->getFlags() & SkShader::kOpaqueAlpha_Flag; 1.294 + 1.295 + for (;;) { 1.296 + int count = *runs; 1.297 + if (count == 0) { 1.298 + break; 1.299 + } 1.300 + int aa = *antialias; 1.301 + if (aa) { 1.302 + if (opaque && aa == 255 && mode == NULL) { 1.303 + memset(device, 0xFF, count); 1.304 + } else { 1.305 + shader->shadeSpan(x, y, span, count); 1.306 + if (mode) { 1.307 + memset(aaExpand, aa, count); 1.308 + mode->xferA8(device, span, count, aaExpand); 1.309 + } else { 1.310 + for (int i = count - 1; i >= 0; --i) { 1.311 + device[i] = aa_blend8(span[i], device[i], aa); 1.312 + } 1.313 + } 1.314 + } 1.315 + } 1.316 + device += count; 1.317 + runs += count; 1.318 + antialias += count; 1.319 + x += count; 1.320 + } 1.321 +} 1.322 + 1.323 +void SkA8_Shader_Blitter::blitMask(const SkMask& mask, const SkIRect& clip) { 1.324 + if (mask.fFormat == SkMask::kBW_Format) { 1.325 + this->INHERITED::blitMask(mask, clip); 1.326 + return; 1.327 + } 1.328 + 1.329 + int x = clip.fLeft; 1.330 + int y = clip.fTop; 1.331 + int width = clip.width(); 1.332 + int height = clip.height(); 1.333 + uint8_t* device = fDevice.getAddr8(x, y); 1.334 + const uint8_t* alpha = mask.getAddr8(x, y); 1.335 + 1.336 + SkPMColor* span = fBuffer; 1.337 + 1.338 + while (--height >= 0) { 1.339 + fShader->shadeSpan(x, y, span, width); 1.340 + if (fXfermode) { 1.341 + fXfermode->xferA8(device, span, width, alpha); 1.342 + } else { 1.343 + for (int i = width - 1; i >= 0; --i) { 1.344 + device[i] = aa_blend8(span[i], device[i], alpha[i]); 1.345 + } 1.346 + } 1.347 + 1.348 + y += 1; 1.349 + device += fDevice.rowBytes(); 1.350 + alpha += mask.fRowBytes; 1.351 + } 1.352 +} 1.353 + 1.354 +/////////////////////////////////////////////////////////////////////////////// 1.355 + 1.356 +SkA8_Coverage_Blitter::SkA8_Coverage_Blitter(const SkBitmap& device, 1.357 + const SkPaint& paint) : SkRasterBlitter(device) { 1.358 + SkASSERT(NULL == paint.getShader()); 1.359 + SkASSERT(NULL == paint.getXfermode()); 1.360 + SkASSERT(NULL == paint.getColorFilter()); 1.361 +} 1.362 + 1.363 +void SkA8_Coverage_Blitter::blitAntiH(int x, int y, const SkAlpha antialias[], 1.364 + const int16_t runs[]) { 1.365 + uint8_t* device = fDevice.getAddr8(x, y); 1.366 + SkDEBUGCODE(int totalCount = 0;) 1.367 + 1.368 + for (;;) { 1.369 + int count = runs[0]; 1.370 + SkASSERT(count >= 0); 1.371 + if (count == 0) { 1.372 + return; 1.373 + } 1.374 + if (antialias[0]) { 1.375 + memset(device, antialias[0], count); 1.376 + } 1.377 + runs += count; 1.378 + antialias += count; 1.379 + device += count; 1.380 + 1.381 + SkDEBUGCODE(totalCount += count;) 1.382 + } 1.383 + SkASSERT(fDevice.width() == totalCount); 1.384 +} 1.385 + 1.386 +void SkA8_Coverage_Blitter::blitH(int x, int y, int width) { 1.387 + memset(fDevice.getAddr8(x, y), 0xFF, width); 1.388 +} 1.389 + 1.390 +void SkA8_Coverage_Blitter::blitV(int x, int y, int height, SkAlpha alpha) { 1.391 + if (0 == alpha) { 1.392 + return; 1.393 + } 1.394 + 1.395 + uint8_t* dst = fDevice.getAddr8(x, y); 1.396 + const size_t dstRB = fDevice.rowBytes(); 1.397 + while (--height >= 0) { 1.398 + *dst = alpha; 1.399 + dst += dstRB; 1.400 + } 1.401 +} 1.402 + 1.403 +void SkA8_Coverage_Blitter::blitRect(int x, int y, int width, int height) { 1.404 + uint8_t* dst = fDevice.getAddr8(x, y); 1.405 + const size_t dstRB = fDevice.rowBytes(); 1.406 + while (--height >= 0) { 1.407 + memset(dst, 0xFF, width); 1.408 + dst += dstRB; 1.409 + } 1.410 +} 1.411 + 1.412 +void SkA8_Coverage_Blitter::blitMask(const SkMask& mask, const SkIRect& clip) { 1.413 + SkASSERT(SkMask::kA8_Format == mask.fFormat); 1.414 + 1.415 + int x = clip.fLeft; 1.416 + int y = clip.fTop; 1.417 + int width = clip.width(); 1.418 + int height = clip.height(); 1.419 + 1.420 + uint8_t* dst = fDevice.getAddr8(x, y); 1.421 + const uint8_t* src = mask.getAddr8(x, y); 1.422 + const size_t srcRB = mask.fRowBytes; 1.423 + const size_t dstRB = fDevice.rowBytes(); 1.424 + 1.425 + while (--height >= 0) { 1.426 + memcpy(dst, src, width); 1.427 + dst += dstRB; 1.428 + src += srcRB; 1.429 + } 1.430 +} 1.431 + 1.432 +const SkBitmap* SkA8_Coverage_Blitter::justAnOpaqueColor(uint32_t*) { 1.433 + return NULL; 1.434 +}