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 "SkBlitRow.h" michael@0: #include "SkCoreBlitters.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkDither.h" michael@0: #include "SkShader.h" michael@0: #include "SkUtils.h" michael@0: #include "SkXfermode.h" michael@0: michael@0: #if defined(__ARM_HAVE_NEON) && defined(SK_CPU_LENDIAN) michael@0: #define SK_USE_NEON michael@0: #include michael@0: #else michael@0: // if we don't have neon, then our black blitter is worth the extra code michael@0: #define USE_BLACK_BLITTER michael@0: #endif michael@0: michael@0: void sk_dither_memset16(uint16_t dst[], uint16_t value, uint16_t other, michael@0: int count) { michael@0: if (count > 0) { michael@0: // see if we need to write one short before we can cast to an 4byte ptr michael@0: // (we do this subtract rather than (unsigned)dst so we don't get warnings michael@0: // on 64bit machines) michael@0: if (((char*)dst - (char*)0) & 2) { michael@0: *dst++ = value; michael@0: count -= 1; michael@0: SkTSwap(value, other); michael@0: } michael@0: michael@0: // fast way to set [value,other] pairs michael@0: #ifdef SK_CPU_BENDIAN michael@0: sk_memset32((uint32_t*)dst, (value << 16) | other, count >> 1); michael@0: #else michael@0: sk_memset32((uint32_t*)dst, (other << 16) | value, count >> 1); michael@0: #endif michael@0: michael@0: if (count & 1) { michael@0: dst[count - 1] = value; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: class SkRGB16_Blitter : public SkRasterBlitter { michael@0: public: michael@0: SkRGB16_Blitter(const SkBitmap& device, const SkPaint& paint); michael@0: virtual void blitH(int x, int y, int width); michael@0: virtual void blitAntiH(int x, int y, const SkAlpha* antialias, michael@0: const int16_t* runs); michael@0: virtual void blitV(int x, int y, int height, SkAlpha alpha); michael@0: virtual void blitRect(int x, int y, int width, int height); michael@0: virtual void blitMask(const SkMask&, michael@0: const SkIRect&); michael@0: virtual const SkBitmap* justAnOpaqueColor(uint32_t*); michael@0: michael@0: protected: michael@0: SkPMColor fSrcColor32; michael@0: uint32_t fExpandedRaw16; michael@0: unsigned fScale; michael@0: uint16_t fColor16; // already scaled by fScale michael@0: uint16_t fRawColor16; // unscaled michael@0: uint16_t fRawDither16; // unscaled michael@0: SkBool8 fDoDither; michael@0: michael@0: // illegal michael@0: SkRGB16_Blitter& operator=(const SkRGB16_Blitter&); michael@0: michael@0: typedef SkRasterBlitter INHERITED; michael@0: }; michael@0: michael@0: class SkRGB16_Opaque_Blitter : public SkRGB16_Blitter { michael@0: public: michael@0: SkRGB16_Opaque_Blitter(const SkBitmap& device, const SkPaint& paint); michael@0: virtual void blitH(int x, int y, int width); michael@0: virtual void blitAntiH(int x, int y, const SkAlpha* antialias, michael@0: const int16_t* runs); michael@0: virtual void blitV(int x, int y, int height, SkAlpha alpha); michael@0: virtual void blitRect(int x, int y, int width, int height); michael@0: virtual void blitMask(const SkMask&, michael@0: const SkIRect&); michael@0: michael@0: private: michael@0: typedef SkRGB16_Blitter INHERITED; michael@0: }; michael@0: michael@0: #ifdef USE_BLACK_BLITTER michael@0: class SkRGB16_Black_Blitter : public SkRGB16_Opaque_Blitter { michael@0: public: michael@0: SkRGB16_Black_Blitter(const SkBitmap& device, const SkPaint& paint); michael@0: virtual void blitMask(const SkMask&, const SkIRect&); michael@0: virtual void blitAntiH(int x, int y, const SkAlpha* antialias, michael@0: const int16_t* runs); michael@0: michael@0: private: michael@0: typedef SkRGB16_Opaque_Blitter INHERITED; michael@0: }; michael@0: #endif michael@0: michael@0: class SkRGB16_Shader_Blitter : public SkShaderBlitter { michael@0: public: michael@0: SkRGB16_Shader_Blitter(const SkBitmap& device, const SkPaint& paint); michael@0: virtual ~SkRGB16_Shader_Blitter(); michael@0: virtual void blitH(int x, int y, int width); michael@0: virtual void blitAntiH(int x, int y, const SkAlpha* antialias, michael@0: const int16_t* runs); michael@0: virtual void blitRect(int x, int y, int width, int height); michael@0: michael@0: protected: michael@0: SkPMColor* fBuffer; michael@0: SkBlitRow::Proc fOpaqueProc; michael@0: SkBlitRow::Proc fAlphaProc; michael@0: michael@0: private: michael@0: // illegal michael@0: SkRGB16_Shader_Blitter& operator=(const SkRGB16_Shader_Blitter&); michael@0: michael@0: typedef SkShaderBlitter INHERITED; michael@0: }; michael@0: michael@0: // used only if the shader can perform shadSpan16 michael@0: class SkRGB16_Shader16_Blitter : public SkRGB16_Shader_Blitter { michael@0: public: michael@0: SkRGB16_Shader16_Blitter(const SkBitmap& device, const SkPaint& paint); michael@0: virtual void blitH(int x, int y, int width); michael@0: virtual void blitAntiH(int x, int y, const SkAlpha* antialias, michael@0: const int16_t* runs); michael@0: virtual void blitRect(int x, int y, int width, int height); michael@0: michael@0: private: michael@0: typedef SkRGB16_Shader_Blitter INHERITED; michael@0: }; michael@0: michael@0: class SkRGB16_Shader_Xfermode_Blitter : public SkShaderBlitter { michael@0: public: michael@0: SkRGB16_Shader_Xfermode_Blitter(const SkBitmap& device, const SkPaint& paint); michael@0: virtual ~SkRGB16_Shader_Xfermode_Blitter(); michael@0: virtual void blitH(int x, int y, int width); michael@0: virtual void blitAntiH(int x, int y, const SkAlpha* antialias, michael@0: const int16_t* runs); michael@0: michael@0: private: michael@0: SkXfermode* fXfermode; michael@0: SkPMColor* fBuffer; michael@0: uint8_t* fAAExpand; michael@0: michael@0: // illegal michael@0: SkRGB16_Shader_Xfermode_Blitter& operator=(const SkRGB16_Shader_Xfermode_Blitter&); michael@0: michael@0: typedef SkShaderBlitter INHERITED; michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: #ifdef USE_BLACK_BLITTER michael@0: SkRGB16_Black_Blitter::SkRGB16_Black_Blitter(const SkBitmap& device, const SkPaint& paint) michael@0: : INHERITED(device, paint) { michael@0: SkASSERT(paint.getShader() == NULL); michael@0: SkASSERT(paint.getColorFilter() == NULL); michael@0: SkASSERT(paint.getXfermode() == NULL); michael@0: SkASSERT(paint.getColor() == SK_ColorBLACK); michael@0: } michael@0: michael@0: #if 1 michael@0: #define black_8_pixels(mask, dst) \ michael@0: do { \ michael@0: if (mask & 0x80) dst[0] = 0; \ michael@0: if (mask & 0x40) dst[1] = 0; \ michael@0: if (mask & 0x20) dst[2] = 0; \ michael@0: if (mask & 0x10) dst[3] = 0; \ michael@0: if (mask & 0x08) dst[4] = 0; \ michael@0: if (mask & 0x04) dst[5] = 0; \ michael@0: if (mask & 0x02) dst[6] = 0; \ michael@0: if (mask & 0x01) dst[7] = 0; \ michael@0: } while (0) michael@0: #else michael@0: static inline black_8_pixels(U8CPU mask, uint16_t dst[]) michael@0: { michael@0: if (mask & 0x80) dst[0] = 0; michael@0: if (mask & 0x40) dst[1] = 0; michael@0: if (mask & 0x20) dst[2] = 0; michael@0: if (mask & 0x10) dst[3] = 0; michael@0: if (mask & 0x08) dst[4] = 0; michael@0: if (mask & 0x04) dst[5] = 0; michael@0: if (mask & 0x02) dst[6] = 0; michael@0: if (mask & 0x01) dst[7] = 0; michael@0: } michael@0: #endif michael@0: michael@0: #define SK_BLITBWMASK_NAME SkRGB16_Black_BlitBW michael@0: #define SK_BLITBWMASK_ARGS michael@0: #define SK_BLITBWMASK_BLIT8(mask, dst) black_8_pixels(mask, dst) michael@0: #define SK_BLITBWMASK_GETADDR getAddr16 michael@0: #define SK_BLITBWMASK_DEVTYPE uint16_t michael@0: #include "SkBlitBWMaskTemplate.h" michael@0: michael@0: void SkRGB16_Black_Blitter::blitMask(const SkMask& mask, michael@0: const SkIRect& clip) { michael@0: if (mask.fFormat == SkMask::kBW_Format) { michael@0: SkRGB16_Black_BlitBW(fDevice, mask, clip); michael@0: } else { michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(clip.fLeft, clip.fTop); michael@0: const uint8_t* SK_RESTRICT alpha = mask.getAddr8(clip.fLeft, clip.fTop); michael@0: unsigned width = clip.width(); michael@0: unsigned height = clip.height(); michael@0: size_t deviceRB = fDevice.rowBytes() - (width << 1); michael@0: unsigned maskRB = mask.fRowBytes - width; michael@0: michael@0: SkASSERT((int)height > 0); michael@0: SkASSERT((int)width > 0); michael@0: SkASSERT((int)deviceRB >= 0); michael@0: SkASSERT((int)maskRB >= 0); michael@0: michael@0: do { michael@0: unsigned w = width; michael@0: do { michael@0: unsigned aa = *alpha++; michael@0: *device = SkAlphaMulRGB16(*device, SkAlpha255To256(255 - aa)); michael@0: device += 1; michael@0: } while (--w != 0); michael@0: device = (uint16_t*)((char*)device + deviceRB); michael@0: alpha += maskRB; michael@0: } while (--height != 0); michael@0: } michael@0: } michael@0: michael@0: void SkRGB16_Black_Blitter::blitAntiH(int x, int y, michael@0: const SkAlpha* SK_RESTRICT antialias, michael@0: const int16_t* SK_RESTRICT runs) { michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); 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: runs += count; michael@0: michael@0: unsigned aa = antialias[0]; michael@0: antialias += count; michael@0: if (aa) { michael@0: if (aa == 255) { michael@0: memset(device, 0, count << 1); michael@0: } else { michael@0: aa = SkAlpha255To256(255 - aa); michael@0: do { michael@0: *device = SkAlphaMulRGB16(*device, aa); michael@0: device += 1; michael@0: } while (--count != 0); michael@0: continue; michael@0: } michael@0: } michael@0: device += count; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkRGB16_Opaque_Blitter::SkRGB16_Opaque_Blitter(const SkBitmap& device, michael@0: const SkPaint& paint) michael@0: : INHERITED(device, paint) {} michael@0: michael@0: void SkRGB16_Opaque_Blitter::blitH(int x, int y, int width) { michael@0: SkASSERT(width > 0); michael@0: SkASSERT(x + width <= fDevice.width()); michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); michael@0: uint16_t srcColor = fColor16; michael@0: michael@0: SkASSERT(fRawColor16 == srcColor); michael@0: if (fDoDither) { michael@0: uint16_t ditherColor = fRawDither16; michael@0: if ((x ^ y) & 1) { michael@0: SkTSwap(ditherColor, srcColor); michael@0: } michael@0: sk_dither_memset16(device, srcColor, ditherColor, width); michael@0: } else { michael@0: sk_memset16(device, srcColor, width); michael@0: } michael@0: } michael@0: michael@0: // return 1 or 0 from a bool michael@0: static inline int Bool2Int(int value) { michael@0: return !!value; michael@0: } michael@0: michael@0: void SkRGB16_Opaque_Blitter::blitAntiH(int x, int y, michael@0: const SkAlpha* SK_RESTRICT antialias, michael@0: const int16_t* SK_RESTRICT runs) { michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); michael@0: uint16_t srcColor = fRawColor16; michael@0: uint32_t srcExpanded = fExpandedRaw16; michael@0: int ditherInt = Bool2Int(fDoDither); michael@0: uint16_t ditherColor = fRawDither16; michael@0: // if we have no dithering, this will always fail michael@0: if ((x ^ y) & ditherInt) { michael@0: SkTSwap(ditherColor, srcColor); 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: runs += count; michael@0: michael@0: unsigned aa = antialias[0]; michael@0: antialias += count; michael@0: if (aa) { michael@0: if (aa == 255) { michael@0: if (ditherInt) { michael@0: sk_dither_memset16(device, srcColor, michael@0: ditherColor, count); michael@0: } else { michael@0: sk_memset16(device, srcColor, count); michael@0: } michael@0: } else { michael@0: // TODO: respect fDoDither michael@0: unsigned scale5 = SkAlpha255To256(aa) >> 3; michael@0: uint32_t src32 = srcExpanded * scale5; michael@0: scale5 = 32 - scale5; // now we can use it on the device michael@0: int n = count; michael@0: do { michael@0: uint32_t dst32 = SkExpand_rgb_16(*device) * scale5; michael@0: *device++ = SkCompact_rgb_16((src32 + dst32) >> 5); michael@0: } while (--n != 0); michael@0: goto DONE; michael@0: } michael@0: } michael@0: device += count; michael@0: michael@0: DONE: michael@0: // if we have no dithering, this will always fail michael@0: if (count & ditherInt) { michael@0: SkTSwap(ditherColor, srcColor); michael@0: } michael@0: } michael@0: } michael@0: michael@0: #define solid_8_pixels(mask, dst, color) \ michael@0: do { \ michael@0: if (mask & 0x80) dst[0] = color; \ michael@0: if (mask & 0x40) dst[1] = color; \ michael@0: if (mask & 0x20) dst[2] = color; \ michael@0: if (mask & 0x10) dst[3] = color; \ michael@0: if (mask & 0x08) dst[4] = color; \ michael@0: if (mask & 0x04) dst[5] = color; \ michael@0: if (mask & 0x02) dst[6] = color; \ michael@0: if (mask & 0x01) dst[7] = color; \ michael@0: } while (0) michael@0: michael@0: #define SK_BLITBWMASK_NAME SkRGB16_BlitBW michael@0: #define SK_BLITBWMASK_ARGS , uint16_t color michael@0: #define SK_BLITBWMASK_BLIT8(mask, dst) solid_8_pixels(mask, dst, color) michael@0: #define SK_BLITBWMASK_GETADDR getAddr16 michael@0: #define SK_BLITBWMASK_DEVTYPE uint16_t michael@0: #include "SkBlitBWMaskTemplate.h" michael@0: michael@0: static U16CPU blend_compact(uint32_t src32, uint32_t dst32, unsigned scale5) { michael@0: return SkCompact_rgb_16(dst32 + ((src32 - dst32) * scale5 >> 5)); michael@0: } michael@0: michael@0: void SkRGB16_Opaque_Blitter::blitMask(const SkMask& mask, michael@0: const SkIRect& clip) { michael@0: if (mask.fFormat == SkMask::kBW_Format) { michael@0: SkRGB16_BlitBW(fDevice, mask, clip, fColor16); michael@0: return; michael@0: } michael@0: michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(clip.fLeft, clip.fTop); michael@0: const uint8_t* SK_RESTRICT alpha = mask.getAddr8(clip.fLeft, clip.fTop); michael@0: int width = clip.width(); michael@0: int height = clip.height(); michael@0: size_t deviceRB = fDevice.rowBytes() - (width << 1); michael@0: unsigned maskRB = mask.fRowBytes - width; michael@0: uint32_t expanded32 = fExpandedRaw16; michael@0: michael@0: #ifdef SK_USE_NEON michael@0: #define UNROLL 8 michael@0: do { michael@0: int w = width; michael@0: if (w >= UNROLL) { michael@0: uint32x4_t color, dev_lo, dev_hi; michael@0: uint32x4_t wn1, wn2, tmp; michael@0: uint32x4_t vmask_g16, vmask_ng16; michael@0: uint16x8_t valpha, vdev; michael@0: uint16x4_t odev_lo, odev_hi, valpha_lo, valpha_hi; michael@0: michael@0: // prepare constants michael@0: vmask_g16 = vdupq_n_u32(SK_G16_MASK_IN_PLACE); michael@0: vmask_ng16 = vdupq_n_u32(~SK_G16_MASK_IN_PLACE); michael@0: color = vdupq_n_u32(expanded32); michael@0: michael@0: do { michael@0: // alpha is 8x8, widen and split to get a pair of 16x4 michael@0: valpha = vaddw_u8(vdupq_n_u16(1), vld1_u8(alpha)); michael@0: valpha = vshrq_n_u16(valpha, 3); michael@0: valpha_lo = vget_low_u16(valpha); michael@0: valpha_hi = vget_high_u16(valpha); michael@0: michael@0: // load pixels michael@0: vdev = vld1q_u16(device); michael@0: dev_lo = vmovl_u16(vget_low_u16(vdev)); michael@0: dev_hi = vmovl_u16(vget_high_u16(vdev)); michael@0: michael@0: // unpack them in 32 bits michael@0: dev_lo = (dev_lo & vmask_ng16) | vshlq_n_u32(dev_lo & vmask_g16, 16); michael@0: dev_hi = (dev_hi & vmask_ng16) | vshlq_n_u32(dev_hi & vmask_g16, 16); michael@0: michael@0: // blend with color michael@0: tmp = (color - dev_lo) * vmovl_u16(valpha_lo); michael@0: tmp = vshrq_n_u32(tmp, 5); michael@0: dev_lo += tmp; michael@0: michael@0: tmp = vmulq_u32(color - dev_hi, vmovl_u16(valpha_hi)); michael@0: tmp = vshrq_n_u32(tmp, 5); michael@0: dev_hi += tmp; michael@0: michael@0: // re-compact michael@0: wn1 = dev_lo & vmask_ng16; michael@0: wn2 = vshrq_n_u32(dev_lo, 16) & vmask_g16; michael@0: odev_lo = vmovn_u32(wn1 | wn2); michael@0: michael@0: wn1 = dev_hi & vmask_ng16; michael@0: wn2 = vshrq_n_u32(dev_hi, 16) & vmask_g16; michael@0: odev_hi = vmovn_u32(wn1 | wn2); michael@0: michael@0: // store michael@0: vst1q_u16(device, vcombine_u16(odev_lo, odev_hi)); michael@0: michael@0: device += UNROLL; michael@0: alpha += UNROLL; michael@0: w -= UNROLL; michael@0: } while (w >= UNROLL); michael@0: } michael@0: michael@0: // residuals michael@0: while (w > 0) { michael@0: *device = blend_compact(expanded32, SkExpand_rgb_16(*device), michael@0: SkAlpha255To256(*alpha++) >> 3); michael@0: device += 1; michael@0: --w; michael@0: } michael@0: device = (uint16_t*)((char*)device + deviceRB); michael@0: alpha += maskRB; michael@0: } while (--height != 0); michael@0: #undef UNROLL michael@0: #else // non-neon code michael@0: do { michael@0: int w = width; michael@0: do { michael@0: *device = blend_compact(expanded32, SkExpand_rgb_16(*device), michael@0: SkAlpha255To256(*alpha++) >> 3); michael@0: device += 1; michael@0: } while (--w != 0); michael@0: device = (uint16_t*)((char*)device + deviceRB); michael@0: alpha += maskRB; michael@0: } while (--height != 0); michael@0: #endif michael@0: } michael@0: michael@0: void SkRGB16_Opaque_Blitter::blitV(int x, int y, int height, SkAlpha alpha) { michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); michael@0: size_t deviceRB = fDevice.rowBytes(); michael@0: michael@0: // TODO: respect fDoDither michael@0: unsigned scale5 = SkAlpha255To256(alpha) >> 3; michael@0: uint32_t src32 = fExpandedRaw16 * scale5; michael@0: scale5 = 32 - scale5; michael@0: do { michael@0: uint32_t dst32 = SkExpand_rgb_16(*device) * scale5; michael@0: *device = SkCompact_rgb_16((src32 + dst32) >> 5); michael@0: device = (uint16_t*)((char*)device + deviceRB); michael@0: } while (--height != 0); michael@0: } michael@0: michael@0: void SkRGB16_Opaque_Blitter::blitRect(int x, int y, int width, int height) { michael@0: SkASSERT(x + width <= fDevice.width() && y + height <= fDevice.height()); michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); michael@0: size_t deviceRB = fDevice.rowBytes(); michael@0: uint16_t color16 = fColor16; michael@0: michael@0: if (fDoDither) { michael@0: uint16_t ditherColor = fRawDither16; michael@0: if ((x ^ y) & 1) { michael@0: SkTSwap(ditherColor, color16); michael@0: } michael@0: while (--height >= 0) { michael@0: sk_dither_memset16(device, color16, ditherColor, width); michael@0: SkTSwap(ditherColor, color16); michael@0: device = (uint16_t*)((char*)device + deviceRB); michael@0: } michael@0: } else { // no dither michael@0: while (--height >= 0) { michael@0: sk_memset16(device, color16, width); michael@0: device = (uint16_t*)((char*)device + deviceRB); michael@0: } michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkRGB16_Blitter::SkRGB16_Blitter(const SkBitmap& device, const SkPaint& paint) michael@0: : INHERITED(device) { michael@0: SkColor color = paint.getColor(); michael@0: michael@0: fSrcColor32 = SkPreMultiplyColor(color); michael@0: fScale = SkAlpha255To256(SkColorGetA(color)); michael@0: michael@0: int r = SkColorGetR(color); michael@0: int g = SkColorGetG(color); michael@0: int b = SkColorGetB(color); michael@0: michael@0: fRawColor16 = fRawDither16 = SkPack888ToRGB16(r, g, b); michael@0: // if we're dithered, use fRawDither16 to hold that. michael@0: if ((fDoDither = paint.isDither()) != false) { michael@0: fRawDither16 = SkDitherPack888ToRGB16(r, g, b); michael@0: } michael@0: michael@0: fExpandedRaw16 = SkExpand_rgb_16(fRawColor16); michael@0: michael@0: fColor16 = SkPackRGB16( SkAlphaMul(r, fScale) >> (8 - SK_R16_BITS), michael@0: SkAlphaMul(g, fScale) >> (8 - SK_G16_BITS), michael@0: SkAlphaMul(b, fScale) >> (8 - SK_B16_BITS)); michael@0: } michael@0: michael@0: const SkBitmap* SkRGB16_Blitter::justAnOpaqueColor(uint32_t* value) { michael@0: if (!fDoDither && 256 == fScale) { michael@0: *value = fRawColor16; michael@0: return &fDevice; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: static uint32_t pmcolor_to_expand16(SkPMColor c) { michael@0: unsigned r = SkGetPackedR32(c); michael@0: unsigned g = SkGetPackedG32(c); michael@0: unsigned b = SkGetPackedB32(c); michael@0: return (g << 24) | (r << 13) | (b << 2); michael@0: } michael@0: michael@0: static inline void blend32_16_row(SkPMColor src, uint16_t dst[], int count) { michael@0: SkASSERT(count > 0); michael@0: uint32_t src_expand = pmcolor_to_expand16(src); michael@0: unsigned scale = SkAlpha255To256(0xFF - SkGetPackedA32(src)) >> 3; michael@0: do { michael@0: uint32_t dst_expand = SkExpand_rgb_16(*dst) * scale; michael@0: *dst = SkCompact_rgb_16((src_expand + dst_expand) >> 5); michael@0: dst += 1; michael@0: } while (--count != 0); michael@0: } michael@0: michael@0: void SkRGB16_Blitter::blitH(int x, int y, int width) { michael@0: SkASSERT(width > 0); michael@0: SkASSERT(x + width <= fDevice.width()); michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); michael@0: michael@0: // TODO: respect fDoDither michael@0: blend32_16_row(fSrcColor32, device, width); michael@0: } michael@0: michael@0: void SkRGB16_Blitter::blitAntiH(int x, int y, michael@0: const SkAlpha* SK_RESTRICT antialias, michael@0: const int16_t* SK_RESTRICT runs) { michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); michael@0: uint32_t srcExpanded = fExpandedRaw16; michael@0: unsigned scale = fScale; michael@0: michael@0: // TODO: respect fDoDither 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: runs += count; michael@0: michael@0: unsigned aa = antialias[0]; michael@0: antialias += count; michael@0: if (aa) { michael@0: unsigned scale5 = SkAlpha255To256(aa) * scale >> (8 + 3); michael@0: uint32_t src32 = srcExpanded * scale5; michael@0: scale5 = 32 - scale5; michael@0: do { michael@0: uint32_t dst32 = SkExpand_rgb_16(*device) * scale5; michael@0: *device++ = SkCompact_rgb_16((src32 + dst32) >> 5); michael@0: } while (--count != 0); michael@0: continue; michael@0: } michael@0: device += count; michael@0: } michael@0: } michael@0: michael@0: static inline void blend_8_pixels(U8CPU bw, uint16_t dst[], unsigned dst_scale, michael@0: U16CPU srcColor) { michael@0: if (bw & 0x80) dst[0] = srcColor + SkAlphaMulRGB16(dst[0], dst_scale); michael@0: if (bw & 0x40) dst[1] = srcColor + SkAlphaMulRGB16(dst[1], dst_scale); michael@0: if (bw & 0x20) dst[2] = srcColor + SkAlphaMulRGB16(dst[2], dst_scale); michael@0: if (bw & 0x10) dst[3] = srcColor + SkAlphaMulRGB16(dst[3], dst_scale); michael@0: if (bw & 0x08) dst[4] = srcColor + SkAlphaMulRGB16(dst[4], dst_scale); michael@0: if (bw & 0x04) dst[5] = srcColor + SkAlphaMulRGB16(dst[5], dst_scale); michael@0: if (bw & 0x02) dst[6] = srcColor + SkAlphaMulRGB16(dst[6], dst_scale); michael@0: if (bw & 0x01) dst[7] = srcColor + SkAlphaMulRGB16(dst[7], dst_scale); michael@0: } michael@0: michael@0: #define SK_BLITBWMASK_NAME SkRGB16_BlendBW michael@0: #define SK_BLITBWMASK_ARGS , unsigned dst_scale, U16CPU src_color michael@0: #define SK_BLITBWMASK_BLIT8(mask, dst) blend_8_pixels(mask, dst, dst_scale, src_color) michael@0: #define SK_BLITBWMASK_GETADDR getAddr16 michael@0: #define SK_BLITBWMASK_DEVTYPE uint16_t michael@0: #include "SkBlitBWMaskTemplate.h" michael@0: michael@0: void SkRGB16_Blitter::blitMask(const SkMask& mask, michael@0: const SkIRect& clip) { michael@0: if (mask.fFormat == SkMask::kBW_Format) { michael@0: SkRGB16_BlendBW(fDevice, mask, clip, 256 - fScale, fColor16); michael@0: return; michael@0: } michael@0: michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(clip.fLeft, clip.fTop); michael@0: const uint8_t* SK_RESTRICT alpha = mask.getAddr8(clip.fLeft, clip.fTop); michael@0: int width = clip.width(); michael@0: int height = clip.height(); michael@0: size_t deviceRB = fDevice.rowBytes() - (width << 1); michael@0: unsigned maskRB = mask.fRowBytes - width; michael@0: uint32_t color32 = fExpandedRaw16; michael@0: michael@0: unsigned scale256 = fScale; michael@0: do { michael@0: int w = width; michael@0: do { michael@0: unsigned aa = *alpha++; michael@0: unsigned scale = SkAlpha255To256(aa) * scale256 >> (8 + 3); michael@0: uint32_t src32 = color32 * scale; michael@0: uint32_t dst32 = SkExpand_rgb_16(*device) * (32 - scale); michael@0: *device++ = SkCompact_rgb_16((src32 + dst32) >> 5); michael@0: } while (--w != 0); michael@0: device = (uint16_t*)((char*)device + deviceRB); michael@0: alpha += maskRB; michael@0: } while (--height != 0); michael@0: } michael@0: michael@0: void SkRGB16_Blitter::blitV(int x, int y, int height, SkAlpha alpha) { michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); michael@0: size_t deviceRB = fDevice.rowBytes(); michael@0: michael@0: // TODO: respect fDoDither michael@0: unsigned scale5 = SkAlpha255To256(alpha) * fScale >> (8 + 3); michael@0: uint32_t src32 = fExpandedRaw16 * scale5; michael@0: scale5 = 32 - scale5; michael@0: do { michael@0: uint32_t dst32 = SkExpand_rgb_16(*device) * scale5; michael@0: *device = SkCompact_rgb_16((src32 + dst32) >> 5); michael@0: device = (uint16_t*)((char*)device + deviceRB); michael@0: } while (--height != 0); michael@0: } michael@0: michael@0: void SkRGB16_Blitter::blitRect(int x, int y, int width, int height) { michael@0: SkASSERT(x + width <= fDevice.width() && y + height <= fDevice.height()); michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); michael@0: size_t deviceRB = fDevice.rowBytes(); michael@0: SkPMColor src32 = fSrcColor32; michael@0: michael@0: while (--height >= 0) { michael@0: blend32_16_row(src32, device, width); michael@0: device = (uint16_t*)((char*)device + deviceRB); michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkRGB16_Shader16_Blitter::SkRGB16_Shader16_Blitter(const SkBitmap& device, michael@0: const SkPaint& paint) michael@0: : SkRGB16_Shader_Blitter(device, paint) { michael@0: SkASSERT(SkShader::CanCallShadeSpan16(fShaderFlags)); michael@0: } michael@0: michael@0: void SkRGB16_Shader16_Blitter::blitH(int x, int y, int width) { michael@0: SkASSERT(x + width <= fDevice.width()); michael@0: michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); michael@0: SkShader* shader = fShader; michael@0: michael@0: int alpha = shader->getSpan16Alpha(); michael@0: if (0xFF == alpha) { michael@0: shader->shadeSpan16(x, y, device, width); michael@0: } else { michael@0: uint16_t* span16 = (uint16_t*)fBuffer; michael@0: shader->shadeSpan16(x, y, span16, width); michael@0: SkBlendRGB16(span16, device, SkAlpha255To256(alpha), width); michael@0: } michael@0: } michael@0: michael@0: void SkRGB16_Shader16_Blitter::blitRect(int x, int y, int width, int height) { michael@0: SkShader* shader = fShader; michael@0: uint16_t* dst = fDevice.getAddr16(x, y); michael@0: size_t dstRB = fDevice.rowBytes(); michael@0: int alpha = shader->getSpan16Alpha(); michael@0: michael@0: if (0xFF == alpha) { michael@0: if (fShaderFlags & SkShader::kConstInY16_Flag) { michael@0: // have the shader blit directly into the device the first time michael@0: shader->shadeSpan16(x, y, dst, width); michael@0: // and now just memcpy that line on the subsequent lines michael@0: if (--height > 0) { michael@0: const uint16_t* orig = dst; michael@0: do { michael@0: dst = (uint16_t*)((char*)dst + dstRB); michael@0: memcpy(dst, orig, width << 1); michael@0: } while (--height); michael@0: } michael@0: } else { // need to call shadeSpan16 for every line michael@0: do { michael@0: shader->shadeSpan16(x, y, dst, width); michael@0: y += 1; michael@0: dst = (uint16_t*)((char*)dst + dstRB); michael@0: } while (--height); michael@0: } michael@0: } else { michael@0: int scale = SkAlpha255To256(alpha); michael@0: uint16_t* span16 = (uint16_t*)fBuffer; michael@0: if (fShaderFlags & SkShader::kConstInY16_Flag) { michael@0: shader->shadeSpan16(x, y, span16, width); michael@0: do { michael@0: SkBlendRGB16(span16, dst, scale, width); michael@0: dst = (uint16_t*)((char*)dst + dstRB); michael@0: } while (--height); michael@0: } else { michael@0: do { michael@0: shader->shadeSpan16(x, y, span16, width); michael@0: SkBlendRGB16(span16, dst, scale, width); michael@0: y += 1; michael@0: dst = (uint16_t*)((char*)dst + dstRB); michael@0: } while (--height); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkRGB16_Shader16_Blitter::blitAntiH(int x, int y, michael@0: const SkAlpha* SK_RESTRICT antialias, michael@0: const int16_t* SK_RESTRICT runs) { michael@0: SkShader* shader = fShader; michael@0: SkPMColor* SK_RESTRICT span = fBuffer; michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); michael@0: michael@0: int alpha = shader->getSpan16Alpha(); michael@0: uint16_t* span16 = (uint16_t*)span; michael@0: michael@0: if (0xFF == alpha) { michael@0: for (;;) { michael@0: int count = *runs; michael@0: if (count <= 0) { michael@0: break; michael@0: } michael@0: SkASSERT(count <= fDevice.width()); // don't overrun fBuffer michael@0: michael@0: int aa = *antialias; michael@0: if (aa == 255) { michael@0: // go direct to the device! michael@0: shader->shadeSpan16(x, y, device, count); michael@0: } else if (aa) { michael@0: shader->shadeSpan16(x, y, span16, count); michael@0: SkBlendRGB16(span16, device, SkAlpha255To256(aa), count); michael@0: } michael@0: device += count; michael@0: runs += count; michael@0: antialias += count; michael@0: x += count; michael@0: } michael@0: } else { // span alpha is < 255 michael@0: alpha = SkAlpha255To256(alpha); michael@0: for (;;) { michael@0: int count = *runs; michael@0: if (count <= 0) { michael@0: break; michael@0: } michael@0: SkASSERT(count <= fDevice.width()); // don't overrun fBuffer michael@0: michael@0: int aa = SkAlphaMul(*antialias, alpha); michael@0: if (aa) { michael@0: shader->shadeSpan16(x, y, span16, count); michael@0: SkBlendRGB16(span16, device, SkAlpha255To256(aa), count); 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: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkRGB16_Shader_Blitter::SkRGB16_Shader_Blitter(const SkBitmap& device, michael@0: const SkPaint& paint) michael@0: : INHERITED(device, paint) { michael@0: SkASSERT(paint.getXfermode() == NULL); michael@0: michael@0: fBuffer = (SkPMColor*)sk_malloc_throw(device.width() * sizeof(SkPMColor)); michael@0: michael@0: // compute SkBlitRow::Procs michael@0: unsigned flags = 0; michael@0: michael@0: uint32_t shaderFlags = fShaderFlags; michael@0: // shaders take care of global alpha, so we never set it in SkBlitRow michael@0: if (!(shaderFlags & SkShader::kOpaqueAlpha_Flag)) { michael@0: flags |= SkBlitRow::kSrcPixelAlpha_Flag; michael@0: } michael@0: // don't dither if the shader is really 16bit michael@0: if (paint.isDither() && !(shaderFlags & SkShader::kIntrinsicly16_Flag)) { michael@0: flags |= SkBlitRow::kDither_Flag; michael@0: } michael@0: // used when we know our global alpha is 0xFF michael@0: fOpaqueProc = SkBlitRow::Factory(flags, SkBitmap::kRGB_565_Config); michael@0: // used when we know our global alpha is < 0xFF michael@0: fAlphaProc = SkBlitRow::Factory(flags | SkBlitRow::kGlobalAlpha_Flag, michael@0: SkBitmap::kRGB_565_Config); michael@0: } michael@0: michael@0: SkRGB16_Shader_Blitter::~SkRGB16_Shader_Blitter() { michael@0: sk_free(fBuffer); michael@0: } michael@0: michael@0: void SkRGB16_Shader_Blitter::blitH(int x, int y, int width) { michael@0: SkASSERT(x + width <= fDevice.width()); michael@0: michael@0: fShader->shadeSpan(x, y, fBuffer, width); michael@0: // shaders take care of global alpha, so we pass 0xFF (should be ignored) michael@0: fOpaqueProc(fDevice.getAddr16(x, y), fBuffer, width, 0xFF, x, y); michael@0: } michael@0: michael@0: void SkRGB16_Shader_Blitter::blitRect(int x, int y, int width, int height) { michael@0: SkShader* shader = fShader; michael@0: SkBlitRow::Proc proc = fOpaqueProc; michael@0: SkPMColor* buffer = fBuffer; michael@0: uint16_t* dst = fDevice.getAddr16(x, y); michael@0: size_t dstRB = fDevice.rowBytes(); michael@0: michael@0: if (fShaderFlags & SkShader::kConstInY32_Flag) { michael@0: shader->shadeSpan(x, y, buffer, width); michael@0: do { michael@0: proc(dst, buffer, width, 0xFF, x, y); michael@0: y += 1; michael@0: dst = (uint16_t*)((char*)dst + dstRB); michael@0: } while (--height); michael@0: } else { michael@0: do { michael@0: shader->shadeSpan(x, y, buffer, width); michael@0: proc(dst, buffer, width, 0xFF, x, y); michael@0: y += 1; michael@0: dst = (uint16_t*)((char*)dst + dstRB); michael@0: } while (--height); michael@0: } michael@0: } michael@0: michael@0: static inline int count_nonzero_span(const int16_t runs[], const SkAlpha aa[]) { michael@0: int count = 0; michael@0: for (;;) { michael@0: int n = *runs; michael@0: if (n == 0 || *aa == 0) { michael@0: break; michael@0: } michael@0: runs += n; michael@0: aa += n; michael@0: count += n; michael@0: } michael@0: return count; michael@0: } michael@0: michael@0: void SkRGB16_Shader_Blitter::blitAntiH(int x, int y, michael@0: const SkAlpha* SK_RESTRICT antialias, michael@0: const int16_t* SK_RESTRICT runs) { michael@0: SkShader* shader = fShader; michael@0: SkPMColor* SK_RESTRICT span = fBuffer; michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); 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 (0 == aa) { michael@0: device += count; michael@0: runs += count; michael@0: antialias += count; michael@0: x += count; michael@0: continue; michael@0: } michael@0: michael@0: int nonZeroCount = count + count_nonzero_span(runs + count, antialias + count); michael@0: michael@0: SkASSERT(nonZeroCount <= fDevice.width()); // don't overrun fBuffer michael@0: shader->shadeSpan(x, y, span, nonZeroCount); michael@0: michael@0: SkPMColor* localSpan = span; michael@0: for (;;) { michael@0: SkBlitRow::Proc proc = (aa == 0xFF) ? fOpaqueProc : fAlphaProc; michael@0: proc(device, localSpan, count, aa, x, y); michael@0: michael@0: x += count; michael@0: device += count; michael@0: runs += count; michael@0: antialias += count; michael@0: nonZeroCount -= count; michael@0: if (nonZeroCount == 0) { michael@0: break; michael@0: } michael@0: localSpan += count; michael@0: SkASSERT(nonZeroCount > 0); michael@0: count = *runs; michael@0: SkASSERT(count > 0); michael@0: aa = *antialias; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkRGB16_Shader_Xfermode_Blitter::SkRGB16_Shader_Xfermode_Blitter( michael@0: const SkBitmap& device, const SkPaint& paint) michael@0: : INHERITED(device, paint) { michael@0: fXfermode = paint.getXfermode(); michael@0: SkASSERT(fXfermode); michael@0: fXfermode->ref(); michael@0: michael@0: int width = device.width(); michael@0: fBuffer = (SkPMColor*)sk_malloc_throw((width + (SkAlign4(width) >> 2)) * sizeof(SkPMColor)); michael@0: fAAExpand = (uint8_t*)(fBuffer + width); michael@0: } michael@0: michael@0: SkRGB16_Shader_Xfermode_Blitter::~SkRGB16_Shader_Xfermode_Blitter() { michael@0: fXfermode->unref(); michael@0: sk_free(fBuffer); michael@0: } michael@0: michael@0: void SkRGB16_Shader_Xfermode_Blitter::blitH(int x, int y, int width) { michael@0: SkASSERT(x + width <= fDevice.width()); michael@0: michael@0: uint16_t* device = fDevice.getAddr16(x, y); michael@0: SkPMColor* span = fBuffer; michael@0: michael@0: fShader->shadeSpan(x, y, span, width); michael@0: fXfermode->xfer16(device, span, width, NULL); michael@0: } michael@0: michael@0: void SkRGB16_Shader_Xfermode_Blitter::blitAntiH(int x, int y, michael@0: const SkAlpha* SK_RESTRICT antialias, michael@0: const int16_t* SK_RESTRICT runs) { michael@0: SkShader* shader = fShader; michael@0: SkXfermode* mode = fXfermode; michael@0: SkPMColor* SK_RESTRICT span = fBuffer; michael@0: uint8_t* SK_RESTRICT aaExpand = fAAExpand; michael@0: uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y); 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 (0 == aa) { michael@0: device += count; michael@0: runs += count; michael@0: antialias += count; michael@0: x += count; michael@0: continue; michael@0: } michael@0: michael@0: int nonZeroCount = count + count_nonzero_span(runs + count, michael@0: antialias + count); michael@0: michael@0: SkASSERT(nonZeroCount <= fDevice.width()); // don't overrun fBuffer michael@0: shader->shadeSpan(x, y, span, nonZeroCount); michael@0: michael@0: x += nonZeroCount; michael@0: SkPMColor* localSpan = span; michael@0: for (;;) { michael@0: if (aa == 0xFF) { michael@0: mode->xfer16(device, localSpan, count, NULL); michael@0: } else { michael@0: SkASSERT(aa); michael@0: memset(aaExpand, aa, count); michael@0: mode->xfer16(device, localSpan, count, aaExpand); michael@0: } michael@0: device += count; michael@0: runs += count; michael@0: antialias += count; michael@0: nonZeroCount -= count; michael@0: if (nonZeroCount == 0) { michael@0: break; michael@0: } michael@0: localSpan += count; michael@0: SkASSERT(nonZeroCount > 0); michael@0: count = *runs; michael@0: SkASSERT(count > 0); michael@0: aa = *antialias; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkBlitter* SkBlitter_ChooseD565(const SkBitmap& device, const SkPaint& paint, michael@0: SkTBlitterAllocator* allocator) { michael@0: SkASSERT(allocator != NULL); michael@0: michael@0: SkBlitter* blitter; michael@0: SkShader* shader = paint.getShader(); michael@0: SkXfermode* mode = paint.getXfermode(); michael@0: michael@0: // we require a shader if there is an xfermode, handled by our caller michael@0: SkASSERT(NULL == mode || NULL != shader); michael@0: michael@0: if (shader) { michael@0: if (mode) { michael@0: blitter = allocator->createT(device, paint); michael@0: } else if (shader->canCallShadeSpan16()) { michael@0: blitter = allocator->createT(device, paint); michael@0: } else { michael@0: blitter = allocator->createT(device, paint); michael@0: } michael@0: } else { michael@0: // no shader, no xfermode, (and we always ignore colorfilter) michael@0: SkColor color = paint.getColor(); michael@0: if (0 == SkColorGetA(color)) { michael@0: blitter = allocator->createT(); michael@0: #ifdef USE_BLACK_BLITTER michael@0: } else if (SK_ColorBLACK == color) { michael@0: blitter = allocator->createT(device, paint); michael@0: #endif michael@0: } else if (0xFF == SkColorGetA(color)) { michael@0: blitter = allocator->createT(device, paint); michael@0: } else { michael@0: blitter = allocator->createT(device, paint); michael@0: } michael@0: } michael@0: michael@0: return blitter; michael@0: }