| /* |
| * Copyright 2011 Google Inc. |
| * |
| * Use of this source code is governed by a BSD-style license that can be |
| * found in the LICENSE file. |
| */ |
| |
| #include "SkBlitRow.h" |
| #include "SkBlitMask.h" |
| #include "SkColorPriv.h" |
| #include "SkUtils.h" |
| |
| #define UNROLL |
| |
| static void S32_Opaque_BlitRow32(SkPMColor* SK_RESTRICT dst, |
| const SkPMColor* SK_RESTRICT src, |
| int count, U8CPU alpha) { |
| SkASSERT(255 == alpha); |
| sk_memcpy32(dst, src, count); |
| } |
| |
| static void S32_Blend_BlitRow32(SkPMColor* SK_RESTRICT dst, |
| const SkPMColor* SK_RESTRICT src, |
| int count, U8CPU alpha) { |
| SkASSERT(alpha <= 255); |
| if (count > 0) { |
| unsigned src_scale = SkAlpha255To256(alpha); |
| unsigned dst_scale = 256 - src_scale; |
| |
| #ifdef UNROLL |
| if (count & 1) { |
| *dst = SkAlphaMulQ(*(src++), src_scale) + SkAlphaMulQ(*dst, dst_scale); |
| dst += 1; |
| count -= 1; |
| } |
| |
| const SkPMColor* SK_RESTRICT srcEnd = src + count; |
| while (src != srcEnd) { |
| *dst = SkAlphaMulQ(*(src++), src_scale) + SkAlphaMulQ(*dst, dst_scale); |
| dst += 1; |
| *dst = SkAlphaMulQ(*(src++), src_scale) + SkAlphaMulQ(*dst, dst_scale); |
| dst += 1; |
| } |
| #else |
| do { |
| *dst = SkAlphaMulQ(*src, src_scale) + SkAlphaMulQ(*dst, dst_scale); |
| src += 1; |
| dst += 1; |
| } while (--count > 0); |
| #endif |
| } |
| } |
| |
| static void S32A_Opaque_BlitRow32(SkPMColor* SK_RESTRICT dst, |
| const SkPMColor* SK_RESTRICT src, |
| int count, U8CPU alpha) { |
| SkASSERT(255 == alpha); |
| if (count > 0) { |
| #ifdef UNROLL |
| if (count & 1) { |
| *dst = SkPMSrcOver(*(src++), *dst); |
| dst += 1; |
| count -= 1; |
| } |
| |
| const SkPMColor* SK_RESTRICT srcEnd = src + count; |
| while (src != srcEnd) { |
| *dst = SkPMSrcOver(*(src++), *dst); |
| dst += 1; |
| *dst = SkPMSrcOver(*(src++), *dst); |
| dst += 1; |
| } |
| #else |
| do { |
| *dst = SkPMSrcOver(*src, *dst); |
| src += 1; |
| dst += 1; |
| } while (--count > 0); |
| #endif |
| } |
| } |
| |
| static void S32A_Blend_BlitRow32(SkPMColor* SK_RESTRICT dst, |
| const SkPMColor* SK_RESTRICT src, |
| int count, U8CPU alpha) { |
| SkASSERT(alpha <= 255); |
| if (count > 0) { |
| #ifdef UNROLL |
| if (count & 1) { |
| *dst = SkBlendARGB32(*(src++), *dst, alpha); |
| dst += 1; |
| count -= 1; |
| } |
| |
| const SkPMColor* SK_RESTRICT srcEnd = src + count; |
| while (src != srcEnd) { |
| *dst = SkBlendARGB32(*(src++), *dst, alpha); |
| dst += 1; |
| *dst = SkBlendARGB32(*(src++), *dst, alpha); |
| dst += 1; |
| } |
| #else |
| do { |
| *dst = SkBlendARGB32(*src, *dst, alpha); |
| src += 1; |
| dst += 1; |
| } while (--count > 0); |
| #endif |
| } |
| } |
| |
| /////////////////////////////////////////////////////////////////////////////// |
| |
| static const SkBlitRow::Proc32 gDefault_Procs32[] = { |
| S32_Opaque_BlitRow32, |
| S32_Blend_BlitRow32, |
| S32A_Opaque_BlitRow32, |
| S32A_Blend_BlitRow32 |
| }; |
| |
| SkBlitRow::Proc32 SkBlitRow::Factory32(unsigned flags) { |
| SkASSERT(flags < SK_ARRAY_COUNT(gDefault_Procs32)); |
| // just so we don't crash |
| flags &= kFlags32_Mask; |
| |
| SkBlitRow::Proc32 proc = PlatformProcs32(flags); |
| if (NULL == proc) { |
| proc = gDefault_Procs32[flags]; |
| } |
| SkASSERT(proc); |
| return proc; |
| } |
| |
| SkBlitRow::Proc32 SkBlitRow::ColorProcFactory() { |
| SkBlitRow::ColorProc proc = PlatformColorProc(); |
| if (NULL == proc) { |
| proc = Color32; |
| } |
| SkASSERT(proc); |
| return proc; |
| } |
| |
| #define SK_SUPPORT_LEGACY_COLOR32_MATHx |
| |
| // Color32 and its SIMD specializations use the blend_256_round_alt algorithm |
| // from tests/BlendTest.cpp. It's not quite perfect, but it's never wrong in the |
| // interesting edge cases, and it's quite a bit faster than blend_perfect. |
| // |
| // blend_256_round_alt is our currently blessed algorithm. Please use it or an analogous one. |
| void SkBlitRow::Color32(SkPMColor* SK_RESTRICT dst, |
| const SkPMColor* SK_RESTRICT src, |
| int count, SkPMColor color) { |
| switch (SkGetPackedA32(color)) { |
| case 0: memmove(dst, src, count * sizeof(SkPMColor)); return; |
| case 255: sk_memset32(dst, color, count); return; |
| } |
| |
| unsigned invA = 255 - SkGetPackedA32(color); |
| #ifdef SK_SUPPORT_LEGACY_COLOR32_MATH // blend_256_plus1_trunc, busted |
| unsigned round = 0; |
| #else // blend_256_round_alt, good |
| invA += invA >> 7; |
| unsigned round = (128 << 16) + (128 << 0); |
| #endif |
| |
| while (count --> 0) { |
| // Our math is 16-bit, so we can do a little bit of SIMD in 32-bit registers. |
| const uint32_t mask = 0x00FF00FF; |
| uint32_t rb = (((*src >> 0) & mask) * invA + round) >> 8, // _r_b |
| ag = (((*src >> 8) & mask) * invA + round) >> 0; // a_g_ |
| *dst = color + ((rb & mask) | (ag & ~mask)); |
| src++; |
| dst++; |
| } |
| } |
| |