michael@0: /* michael@0: * Copyright 2011 Google Inc. 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: #include "SkBlitRow.h" michael@0: #include "SkBlitMask.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkUtils.h" michael@0: michael@0: #define UNROLL michael@0: michael@0: SkBlitRow::ColorRectProc PlatformColorRectProcFactory(); michael@0: michael@0: static void S32_Opaque_BlitRow32(SkPMColor* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, michael@0: int count, U8CPU alpha) { michael@0: SkASSERT(255 == alpha); michael@0: memcpy(dst, src, count * sizeof(SkPMColor)); michael@0: } michael@0: michael@0: static void S32_Blend_BlitRow32(SkPMColor* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, michael@0: int count, U8CPU alpha) { michael@0: SkASSERT(alpha <= 255); michael@0: if (count > 0) { michael@0: unsigned src_scale = SkAlpha255To256(alpha); michael@0: unsigned dst_scale = 256 - src_scale; michael@0: michael@0: #ifdef UNROLL michael@0: if (count & 1) { michael@0: *dst = SkAlphaMulQ(*(src++), src_scale) + SkAlphaMulQ(*dst, dst_scale); michael@0: dst += 1; michael@0: count -= 1; michael@0: } michael@0: michael@0: const SkPMColor* SK_RESTRICT srcEnd = src + count; michael@0: while (src != srcEnd) { michael@0: *dst = SkAlphaMulQ(*(src++), src_scale) + SkAlphaMulQ(*dst, dst_scale); michael@0: dst += 1; michael@0: *dst = SkAlphaMulQ(*(src++), src_scale) + SkAlphaMulQ(*dst, dst_scale); michael@0: dst += 1; michael@0: } michael@0: #else michael@0: do { michael@0: *dst = SkAlphaMulQ(*src, src_scale) + SkAlphaMulQ(*dst, dst_scale); michael@0: src += 1; michael@0: dst += 1; michael@0: } while (--count > 0); michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: static void S32A_Opaque_BlitRow32(SkPMColor* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, michael@0: int count, U8CPU alpha) { michael@0: SkASSERT(255 == alpha); michael@0: if (count > 0) { michael@0: #ifdef UNROLL michael@0: if (count & 1) { michael@0: *dst = SkPMSrcOver(*(src++), *dst); michael@0: dst += 1; michael@0: count -= 1; michael@0: } michael@0: michael@0: const SkPMColor* SK_RESTRICT srcEnd = src + count; michael@0: while (src != srcEnd) { michael@0: *dst = SkPMSrcOver(*(src++), *dst); michael@0: dst += 1; michael@0: *dst = SkPMSrcOver(*(src++), *dst); michael@0: dst += 1; michael@0: } michael@0: #else michael@0: do { michael@0: *dst = SkPMSrcOver(*src, *dst); michael@0: src += 1; michael@0: dst += 1; michael@0: } while (--count > 0); michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: static void S32A_Blend_BlitRow32(SkPMColor* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, michael@0: int count, U8CPU alpha) { michael@0: SkASSERT(alpha <= 255); michael@0: if (count > 0) { michael@0: #ifdef UNROLL michael@0: if (count & 1) { michael@0: *dst = SkBlendARGB32(*(src++), *dst, alpha); michael@0: dst += 1; michael@0: count -= 1; michael@0: } michael@0: michael@0: const SkPMColor* SK_RESTRICT srcEnd = src + count; michael@0: while (src != srcEnd) { michael@0: *dst = SkBlendARGB32(*(src++), *dst, alpha); michael@0: dst += 1; michael@0: *dst = SkBlendARGB32(*(src++), *dst, alpha); michael@0: dst += 1; michael@0: } michael@0: #else michael@0: do { michael@0: *dst = SkBlendARGB32(*src, *dst, alpha); michael@0: src += 1; michael@0: dst += 1; michael@0: } while (--count > 0); michael@0: #endif michael@0: } michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static const SkBlitRow::Proc32 gDefault_Procs32[] = { michael@0: S32_Opaque_BlitRow32, michael@0: S32_Blend_BlitRow32, michael@0: S32A_Opaque_BlitRow32, michael@0: S32A_Blend_BlitRow32 michael@0: }; michael@0: michael@0: SkBlitRow::Proc32 SkBlitRow::Factory32(unsigned flags) { michael@0: SkASSERT(flags < SK_ARRAY_COUNT(gDefault_Procs32)); michael@0: // just so we don't crash michael@0: flags &= kFlags32_Mask; michael@0: michael@0: SkBlitRow::Proc32 proc = PlatformProcs32(flags); michael@0: if (NULL == proc) { michael@0: proc = gDefault_Procs32[flags]; michael@0: } michael@0: SkASSERT(proc); michael@0: return proc; michael@0: } michael@0: michael@0: SkBlitRow::Proc32 SkBlitRow::ColorProcFactory() { michael@0: SkBlitRow::ColorProc proc = PlatformColorProc(); michael@0: if (NULL == proc) { michael@0: proc = Color32; michael@0: } michael@0: SkASSERT(proc); michael@0: return proc; michael@0: } michael@0: michael@0: void SkBlitRow::Color32(SkPMColor* SK_RESTRICT dst, michael@0: const SkPMColor* SK_RESTRICT src, michael@0: int count, SkPMColor color) { michael@0: if (count > 0) { michael@0: if (0 == color) { michael@0: if (src != dst) { michael@0: memcpy(dst, src, count * sizeof(SkPMColor)); michael@0: } michael@0: return; michael@0: } michael@0: unsigned colorA = SkGetPackedA32(color); michael@0: if (255 == colorA) { michael@0: sk_memset32(dst, color, count); michael@0: } else { michael@0: unsigned scale = 256 - SkAlpha255To256(colorA); michael@0: do { michael@0: *dst = color + SkAlphaMulQ(*src, scale); michael@0: src += 1; michael@0: dst += 1; michael@0: } while (--count); michael@0: } michael@0: } michael@0: } michael@0: michael@0: template void assignLoop(SkPMColor* dst, SkPMColor color) { michael@0: for (size_t i = 0; i < N; ++i) { michael@0: *dst++ = color; michael@0: } michael@0: } michael@0: michael@0: static inline void assignLoop(SkPMColor dst[], SkPMColor color, int count) { michael@0: while (count >= 4) { michael@0: *dst++ = color; michael@0: *dst++ = color; michael@0: *dst++ = color; michael@0: *dst++ = color; michael@0: count -= 4; michael@0: } michael@0: if (count >= 2) { michael@0: *dst++ = color; michael@0: *dst++ = color; michael@0: count -= 2; michael@0: } michael@0: if (count > 0) { michael@0: *dst++ = color; michael@0: } michael@0: } michael@0: michael@0: void SkBlitRow::ColorRect32(SkPMColor* dst, int width, int height, michael@0: size_t rowBytes, SkPMColor color) { michael@0: if (width <= 0 || height <= 0 || 0 == color) { michael@0: return; michael@0: } michael@0: michael@0: // Just made up this value, since I saw it once in a SSE2 file. michael@0: // We should consider writing some tests to find the optimimal break-point michael@0: // (or query the Platform proc?) michael@0: static const int MIN_WIDTH_FOR_SCANLINE_PROC = 32; michael@0: michael@0: bool isOpaque = (0xFF == SkGetPackedA32(color)); michael@0: michael@0: if (!isOpaque || width >= MIN_WIDTH_FOR_SCANLINE_PROC) { michael@0: SkBlitRow::ColorProc proc = SkBlitRow::ColorProcFactory(); michael@0: while (--height >= 0) { michael@0: (*proc)(dst, dst, width, color); michael@0: dst = (SkPMColor*) ((char*)dst + rowBytes); michael@0: } michael@0: } else { michael@0: switch (width) { michael@0: case 1: michael@0: while (--height >= 0) { michael@0: assignLoop<1>(dst, color); michael@0: dst = (SkPMColor*) ((char*)dst + rowBytes); michael@0: } michael@0: break; michael@0: case 2: michael@0: while (--height >= 0) { michael@0: assignLoop<2>(dst, color); michael@0: dst = (SkPMColor*) ((char*)dst + rowBytes); michael@0: } michael@0: break; michael@0: case 3: michael@0: while (--height >= 0) { michael@0: assignLoop<3>(dst, color); michael@0: dst = (SkPMColor*) ((char*)dst + rowBytes); michael@0: } michael@0: break; michael@0: default: michael@0: while (--height >= 0) { michael@0: assignLoop(dst, color, width); michael@0: dst = (SkPMColor*) ((char*)dst + rowBytes); michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: SkBlitRow::ColorRectProc SkBlitRow::ColorRectProcFactory() { michael@0: SkBlitRow::ColorRectProc proc = PlatformColorRectProcFactory(); michael@0: if (NULL == proc) { michael@0: proc = ColorRect32; michael@0: } michael@0: SkASSERT(proc); michael@0: return proc; michael@0: }