michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkCoreBlitters.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkShader.h" michael@0: #include "SkXfermode.h" michael@0: michael@0: SkA8_Blitter::SkA8_Blitter(const SkBitmap& device, const SkPaint& paint) michael@0: : INHERITED(device) { michael@0: fSrcA = paint.getAlpha(); michael@0: } michael@0: michael@0: const SkBitmap* SkA8_Blitter::justAnOpaqueColor(uint32_t* value) { michael@0: if (255 == fSrcA) { michael@0: *value = 255; michael@0: return &fDevice; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: void SkA8_Blitter::blitH(int x, int y, int width) { michael@0: SkASSERT(x >= 0 && y >= 0 && michael@0: (unsigned)(x + width) <= (unsigned)fDevice.width()); michael@0: michael@0: if (fSrcA == 0) { michael@0: return; michael@0: } michael@0: michael@0: uint8_t* device = fDevice.getAddr8(x, y); michael@0: michael@0: if (fSrcA == 255) { michael@0: memset(device, 0xFF, width); michael@0: } else { michael@0: unsigned scale = 256 - SkAlpha255To256(fSrcA); michael@0: unsigned srcA = fSrcA; michael@0: michael@0: for (int i = 0; i < width; i++) { michael@0: device[i] = SkToU8(srcA + SkAlphaMul(device[i], scale)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkA8_Blitter::blitAntiH(int x, int y, const SkAlpha antialias[], michael@0: const int16_t runs[]) { michael@0: if (fSrcA == 0) { michael@0: return; michael@0: } michael@0: michael@0: uint8_t* device = fDevice.getAddr8(x, y); michael@0: unsigned srcA = fSrcA; michael@0: michael@0: for (;;) { michael@0: int count = runs[0]; michael@0: SkASSERT(count >= 0); michael@0: if (count == 0) { michael@0: return; michael@0: } michael@0: unsigned aa = antialias[0]; michael@0: michael@0: if (aa == 255 && srcA == 255) { michael@0: memset(device, 0xFF, count); michael@0: } else { michael@0: unsigned sa = SkAlphaMul(srcA, SkAlpha255To256(aa)); michael@0: unsigned scale = 256 - sa; michael@0: michael@0: for (int i = 0; i < count; i++) { michael@0: device[i] = SkToU8(sa + SkAlphaMul(device[i], scale)); michael@0: } michael@0: } michael@0: runs += count; michael@0: antialias += count; michael@0: device += count; michael@0: } michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #define solid_8_pixels(mask, dst) \ michael@0: do { \ michael@0: if (mask & 0x80) dst[0] = 0xFF; \ michael@0: if (mask & 0x40) dst[1] = 0xFF; \ michael@0: if (mask & 0x20) dst[2] = 0xFF; \ michael@0: if (mask & 0x10) dst[3] = 0xFF; \ michael@0: if (mask & 0x08) dst[4] = 0xFF; \ michael@0: if (mask & 0x04) dst[5] = 0xFF; \ michael@0: if (mask & 0x02) dst[6] = 0xFF; \ michael@0: if (mask & 0x01) dst[7] = 0xFF; \ michael@0: } while (0) michael@0: michael@0: #define SK_BLITBWMASK_NAME SkA8_BlitBW michael@0: #define SK_BLITBWMASK_ARGS michael@0: #define SK_BLITBWMASK_BLIT8(mask, dst) solid_8_pixels(mask, dst) michael@0: #define SK_BLITBWMASK_GETADDR getAddr8 michael@0: #define SK_BLITBWMASK_DEVTYPE uint8_t michael@0: #include "SkBlitBWMaskTemplate.h" michael@0: michael@0: static inline void blend_8_pixels(U8CPU bw, uint8_t dst[], U8CPU sa, michael@0: unsigned dst_scale) { michael@0: if (bw & 0x80) dst[0] = SkToU8(sa + SkAlphaMul(dst[0], dst_scale)); michael@0: if (bw & 0x40) dst[1] = SkToU8(sa + SkAlphaMul(dst[1], dst_scale)); michael@0: if (bw & 0x20) dst[2] = SkToU8(sa + SkAlphaMul(dst[2], dst_scale)); michael@0: if (bw & 0x10) dst[3] = SkToU8(sa + SkAlphaMul(dst[3], dst_scale)); michael@0: if (bw & 0x08) dst[4] = SkToU8(sa + SkAlphaMul(dst[4], dst_scale)); michael@0: if (bw & 0x04) dst[5] = SkToU8(sa + SkAlphaMul(dst[5], dst_scale)); michael@0: if (bw & 0x02) dst[6] = SkToU8(sa + SkAlphaMul(dst[6], dst_scale)); michael@0: if (bw & 0x01) dst[7] = SkToU8(sa + SkAlphaMul(dst[7], dst_scale)); michael@0: } michael@0: michael@0: #define SK_BLITBWMASK_NAME SkA8_BlendBW michael@0: #define SK_BLITBWMASK_ARGS , U8CPU sa, unsigned dst_scale michael@0: #define SK_BLITBWMASK_BLIT8(mask, dst) blend_8_pixels(mask, dst, sa, dst_scale) michael@0: #define SK_BLITBWMASK_GETADDR getAddr8 michael@0: #define SK_BLITBWMASK_DEVTYPE uint8_t michael@0: #include "SkBlitBWMaskTemplate.h" michael@0: michael@0: void SkA8_Blitter::blitMask(const SkMask& mask, const SkIRect& clip) { michael@0: if (fSrcA == 0) { michael@0: return; michael@0: } michael@0: michael@0: if (mask.fFormat == SkMask::kBW_Format) { michael@0: if (fSrcA == 0xFF) { michael@0: SkA8_BlitBW(fDevice, mask, clip); michael@0: } else { michael@0: SkA8_BlendBW(fDevice, mask, clip, fSrcA, michael@0: SkAlpha255To256(255 - fSrcA)); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: int x = clip.fLeft; michael@0: int y = clip.fTop; michael@0: int width = clip.width(); michael@0: int height = clip.height(); michael@0: uint8_t* device = fDevice.getAddr8(x, y); michael@0: const uint8_t* alpha = mask.getAddr8(x, y); michael@0: unsigned srcA = fSrcA; michael@0: michael@0: while (--height >= 0) { michael@0: for (int i = width - 1; i >= 0; --i) { michael@0: unsigned sa; michael@0: // scale our src by the alpha value michael@0: { michael@0: int aa = alpha[i]; michael@0: if (aa == 0) { michael@0: continue; michael@0: } michael@0: if (aa == 255) { michael@0: if (srcA == 255) { michael@0: device[i] = 0xFF; michael@0: continue; michael@0: } michael@0: sa = srcA; michael@0: } else { michael@0: sa = SkAlphaMul(srcA, SkAlpha255To256(aa)); michael@0: } michael@0: } michael@0: michael@0: int scale = 256 - SkAlpha255To256(sa); michael@0: device[i] = SkToU8(sa + SkAlphaMul(device[i], scale)); michael@0: } michael@0: device += fDevice.rowBytes(); michael@0: alpha += mask.fRowBytes; michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void SkA8_Blitter::blitV(int x, int y, int height, SkAlpha alpha) { michael@0: if (fSrcA == 0) { michael@0: return; michael@0: } michael@0: michael@0: unsigned sa = SkAlphaMul(fSrcA, SkAlpha255To256(alpha)); michael@0: uint8_t* device = fDevice.getAddr8(x, y); michael@0: size_t rowBytes = fDevice.rowBytes(); michael@0: michael@0: if (sa == 0xFF) { michael@0: for (int i = 0; i < height; i++) { michael@0: *device = SkToU8(sa); michael@0: device += rowBytes; michael@0: } michael@0: } else { michael@0: unsigned scale = 256 - SkAlpha255To256(sa); michael@0: michael@0: for (int i = 0; i < height; i++) { michael@0: *device = SkToU8(sa + SkAlphaMul(*device, scale)); michael@0: device += rowBytes; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkA8_Blitter::blitRect(int x, int y, int width, int height) { michael@0: SkASSERT(x >= 0 && y >= 0 && michael@0: (unsigned)(x + width) <= (unsigned)fDevice.width() && michael@0: (unsigned)(y + height) <= (unsigned)fDevice.height()); michael@0: michael@0: if (fSrcA == 0) { michael@0: return; michael@0: } michael@0: michael@0: uint8_t* device = fDevice.getAddr8(x, y); michael@0: unsigned srcA = fSrcA; michael@0: michael@0: if (srcA == 255) { michael@0: while (--height >= 0) { michael@0: memset(device, 0xFF, width); michael@0: device += fDevice.rowBytes(); michael@0: } michael@0: } else { michael@0: unsigned scale = 256 - SkAlpha255To256(srcA); michael@0: michael@0: while (--height >= 0) { michael@0: for (int i = 0; i < width; i++) { michael@0: device[i] = SkToU8(srcA + SkAlphaMul(device[i], scale)); michael@0: } michael@0: device += fDevice.rowBytes(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkA8_Shader_Blitter::SkA8_Shader_Blitter(const SkBitmap& device, const SkPaint& paint) michael@0: : INHERITED(device, paint) { michael@0: if ((fXfermode = paint.getXfermode()) != NULL) { michael@0: fXfermode->ref(); michael@0: SkASSERT(fShader); michael@0: } michael@0: michael@0: int width = device.width(); michael@0: fBuffer = (SkPMColor*)sk_malloc_throw(sizeof(SkPMColor) * (width + (SkAlign4(width) >> 2))); michael@0: fAAExpand = (uint8_t*)(fBuffer + width); michael@0: } michael@0: michael@0: SkA8_Shader_Blitter::~SkA8_Shader_Blitter() { michael@0: if (fXfermode) SkSafeUnref(fXfermode); michael@0: sk_free(fBuffer); michael@0: } michael@0: michael@0: void SkA8_Shader_Blitter::blitH(int x, int y, int width) { michael@0: SkASSERT(x >= 0 && y >= 0 && michael@0: (unsigned)(x + width) <= (unsigned)fDevice.width()); michael@0: michael@0: uint8_t* device = fDevice.getAddr8(x, y); michael@0: michael@0: if ((fShader->getFlags() & SkShader::kOpaqueAlpha_Flag) && !fXfermode) { michael@0: memset(device, 0xFF, width); michael@0: } else { michael@0: SkPMColor* span = fBuffer; michael@0: michael@0: fShader->shadeSpan(x, y, span, width); michael@0: if (fXfermode) { michael@0: fXfermode->xferA8(device, span, width, NULL); michael@0: } else { michael@0: for (int i = width - 1; i >= 0; --i) { michael@0: unsigned srcA = SkGetPackedA32(span[i]); michael@0: unsigned scale = 256 - SkAlpha255To256(srcA); michael@0: michael@0: device[i] = SkToU8(srcA + SkAlphaMul(device[i], scale)); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: static inline uint8_t aa_blend8(SkPMColor src, U8CPU da, int aa) { michael@0: SkASSERT((unsigned)aa <= 255); michael@0: michael@0: int src_scale = SkAlpha255To256(aa); michael@0: int sa = SkGetPackedA32(src); michael@0: int dst_scale = 256 - SkAlphaMul(sa, src_scale); michael@0: michael@0: return SkToU8((sa * src_scale + da * dst_scale) >> 8); michael@0: } michael@0: michael@0: void SkA8_Shader_Blitter::blitAntiH(int x, int y, const SkAlpha antialias[], michael@0: const int16_t runs[]) { michael@0: SkShader* shader = fShader; michael@0: SkXfermode* mode = fXfermode; michael@0: uint8_t* aaExpand = fAAExpand; michael@0: SkPMColor* span = fBuffer; michael@0: uint8_t* device = fDevice.getAddr8(x, y); michael@0: int opaque = fShader->getFlags() & SkShader::kOpaqueAlpha_Flag; michael@0: michael@0: for (;;) { michael@0: int count = *runs; michael@0: if (count == 0) { michael@0: break; michael@0: } michael@0: int aa = *antialias; michael@0: if (aa) { michael@0: if (opaque && aa == 255 && mode == NULL) { michael@0: memset(device, 0xFF, count); michael@0: } else { michael@0: shader->shadeSpan(x, y, span, count); michael@0: if (mode) { michael@0: memset(aaExpand, aa, count); michael@0: mode->xferA8(device, span, count, aaExpand); michael@0: } else { michael@0: for (int i = count - 1; i >= 0; --i) { michael@0: device[i] = aa_blend8(span[i], device[i], aa); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: device += count; michael@0: runs += count; michael@0: antialias += count; michael@0: x += count; michael@0: } michael@0: } michael@0: michael@0: void SkA8_Shader_Blitter::blitMask(const SkMask& mask, const SkIRect& clip) { michael@0: if (mask.fFormat == SkMask::kBW_Format) { michael@0: this->INHERITED::blitMask(mask, clip); michael@0: return; michael@0: } michael@0: michael@0: int x = clip.fLeft; michael@0: int y = clip.fTop; michael@0: int width = clip.width(); michael@0: int height = clip.height(); michael@0: uint8_t* device = fDevice.getAddr8(x, y); michael@0: const uint8_t* alpha = mask.getAddr8(x, y); michael@0: michael@0: SkPMColor* span = fBuffer; michael@0: michael@0: while (--height >= 0) { michael@0: fShader->shadeSpan(x, y, span, width); michael@0: if (fXfermode) { michael@0: fXfermode->xferA8(device, span, width, alpha); michael@0: } else { michael@0: for (int i = width - 1; i >= 0; --i) { michael@0: device[i] = aa_blend8(span[i], device[i], alpha[i]); michael@0: } michael@0: } michael@0: michael@0: y += 1; michael@0: device += fDevice.rowBytes(); michael@0: alpha += mask.fRowBytes; michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkA8_Coverage_Blitter::SkA8_Coverage_Blitter(const SkBitmap& device, michael@0: const SkPaint& paint) : SkRasterBlitter(device) { michael@0: SkASSERT(NULL == paint.getShader()); michael@0: SkASSERT(NULL == paint.getXfermode()); michael@0: SkASSERT(NULL == paint.getColorFilter()); michael@0: } michael@0: michael@0: void SkA8_Coverage_Blitter::blitAntiH(int x, int y, const SkAlpha antialias[], michael@0: const int16_t runs[]) { michael@0: uint8_t* device = fDevice.getAddr8(x, y); michael@0: SkDEBUGCODE(int totalCount = 0;) michael@0: michael@0: for (;;) { michael@0: int count = runs[0]; michael@0: SkASSERT(count >= 0); michael@0: if (count == 0) { michael@0: return; michael@0: } michael@0: if (antialias[0]) { michael@0: memset(device, antialias[0], count); michael@0: } michael@0: runs += count; michael@0: antialias += count; michael@0: device += count; michael@0: michael@0: SkDEBUGCODE(totalCount += count;) michael@0: } michael@0: SkASSERT(fDevice.width() == totalCount); michael@0: } michael@0: michael@0: void SkA8_Coverage_Blitter::blitH(int x, int y, int width) { michael@0: memset(fDevice.getAddr8(x, y), 0xFF, width); michael@0: } michael@0: michael@0: void SkA8_Coverage_Blitter::blitV(int x, int y, int height, SkAlpha alpha) { michael@0: if (0 == alpha) { michael@0: return; michael@0: } michael@0: michael@0: uint8_t* dst = fDevice.getAddr8(x, y); michael@0: const size_t dstRB = fDevice.rowBytes(); michael@0: while (--height >= 0) { michael@0: *dst = alpha; michael@0: dst += dstRB; michael@0: } michael@0: } michael@0: michael@0: void SkA8_Coverage_Blitter::blitRect(int x, int y, int width, int height) { michael@0: uint8_t* dst = fDevice.getAddr8(x, y); michael@0: const size_t dstRB = fDevice.rowBytes(); michael@0: while (--height >= 0) { michael@0: memset(dst, 0xFF, width); michael@0: dst += dstRB; michael@0: } michael@0: } michael@0: michael@0: void SkA8_Coverage_Blitter::blitMask(const SkMask& mask, const SkIRect& clip) { michael@0: SkASSERT(SkMask::kA8_Format == mask.fFormat); michael@0: michael@0: int x = clip.fLeft; michael@0: int y = clip.fTop; michael@0: int width = clip.width(); michael@0: int height = clip.height(); michael@0: michael@0: uint8_t* dst = fDevice.getAddr8(x, y); michael@0: const uint8_t* src = mask.getAddr8(x, y); michael@0: const size_t srcRB = mask.fRowBytes; michael@0: const size_t dstRB = fDevice.rowBytes(); michael@0: michael@0: while (--height >= 0) { michael@0: memcpy(dst, src, width); michael@0: dst += dstRB; michael@0: src += srcRB; michael@0: } michael@0: } michael@0: michael@0: const SkBitmap* SkA8_Coverage_Blitter::justAnOpaqueColor(uint32_t*) { michael@0: return NULL; michael@0: }