epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed@google.com | 58af9a6 | 2011-10-12 13:43:52 +0000 | [diff] [blame] | 7 | |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 8 | #include "SkBlitRow.h" |
reed@google.com | 58af9a6 | 2011-10-12 13:43:52 +0000 | [diff] [blame] | 9 | #include "SkBlitMask.h" |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 10 | #include "SkColorPriv.h" |
| 11 | #include "SkUtils.h" |
| 12 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 13 | #define UNROLL |
| 14 | |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 15 | static void S32_Opaque_BlitRow32(SkPMColor* SK_RESTRICT dst, |
| 16 | const SkPMColor* SK_RESTRICT src, |
| 17 | int count, U8CPU alpha) { |
| 18 | SkASSERT(255 == alpha); |
mtklein | 58fd2c8 | 2015-07-27 11:08:28 -0700 | [diff] [blame] | 19 | memcpy(dst, src, count * 4); |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | static void S32_Blend_BlitRow32(SkPMColor* SK_RESTRICT dst, |
| 23 | const SkPMColor* SK_RESTRICT src, |
| 24 | int count, U8CPU alpha) { |
| 25 | SkASSERT(alpha <= 255); |
| 26 | if (count > 0) { |
| 27 | unsigned src_scale = SkAlpha255To256(alpha); |
| 28 | unsigned dst_scale = 256 - src_scale; |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 29 | |
| 30 | #ifdef UNROLL |
| 31 | if (count & 1) { |
| 32 | *dst = SkAlphaMulQ(*(src++), src_scale) + SkAlphaMulQ(*dst, dst_scale); |
| 33 | dst += 1; |
| 34 | count -= 1; |
| 35 | } |
| 36 | |
| 37 | const SkPMColor* SK_RESTRICT srcEnd = src + count; |
| 38 | while (src != srcEnd) { |
| 39 | *dst = SkAlphaMulQ(*(src++), src_scale) + SkAlphaMulQ(*dst, dst_scale); |
| 40 | dst += 1; |
| 41 | *dst = SkAlphaMulQ(*(src++), src_scale) + SkAlphaMulQ(*dst, dst_scale); |
| 42 | dst += 1; |
| 43 | } |
| 44 | #else |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 45 | do { |
| 46 | *dst = SkAlphaMulQ(*src, src_scale) + SkAlphaMulQ(*dst, dst_scale); |
| 47 | src += 1; |
| 48 | dst += 1; |
| 49 | } while (--count > 0); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 50 | #endif |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 54 | static void S32A_Opaque_BlitRow32(SkPMColor* SK_RESTRICT dst, |
| 55 | const SkPMColor* SK_RESTRICT src, |
| 56 | int count, U8CPU alpha) { |
| 57 | SkASSERT(255 == alpha); |
| 58 | if (count > 0) { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 59 | #ifdef UNROLL |
| 60 | if (count & 1) { |
| 61 | *dst = SkPMSrcOver(*(src++), *dst); |
| 62 | dst += 1; |
| 63 | count -= 1; |
| 64 | } |
| 65 | |
| 66 | const SkPMColor* SK_RESTRICT srcEnd = src + count; |
| 67 | while (src != srcEnd) { |
| 68 | *dst = SkPMSrcOver(*(src++), *dst); |
| 69 | dst += 1; |
| 70 | *dst = SkPMSrcOver(*(src++), *dst); |
| 71 | dst += 1; |
| 72 | } |
| 73 | #else |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 74 | do { |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 75 | *dst = SkPMSrcOver(*src, *dst); |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 76 | src += 1; |
| 77 | dst += 1; |
| 78 | } while (--count > 0); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 79 | #endif |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | static void S32A_Blend_BlitRow32(SkPMColor* SK_RESTRICT dst, |
| 84 | const SkPMColor* SK_RESTRICT src, |
| 85 | int count, U8CPU alpha) { |
| 86 | SkASSERT(alpha <= 255); |
| 87 | if (count > 0) { |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 88 | #ifdef UNROLL |
| 89 | if (count & 1) { |
| 90 | *dst = SkBlendARGB32(*(src++), *dst, alpha); |
| 91 | dst += 1; |
| 92 | count -= 1; |
| 93 | } |
| 94 | |
| 95 | const SkPMColor* SK_RESTRICT srcEnd = src + count; |
| 96 | while (src != srcEnd) { |
| 97 | *dst = SkBlendARGB32(*(src++), *dst, alpha); |
| 98 | dst += 1; |
| 99 | *dst = SkBlendARGB32(*(src++), *dst, alpha); |
| 100 | dst += 1; |
| 101 | } |
| 102 | #else |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 103 | do { |
| 104 | *dst = SkBlendARGB32(*src, *dst, alpha); |
| 105 | src += 1; |
| 106 | dst += 1; |
| 107 | } while (--count > 0); |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 108 | #endif |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | /////////////////////////////////////////////////////////////////////////////// |
| 113 | |
| 114 | static const SkBlitRow::Proc32 gDefault_Procs32[] = { |
| 115 | S32_Opaque_BlitRow32, |
| 116 | S32_Blend_BlitRow32, |
| 117 | S32A_Opaque_BlitRow32, |
| 118 | S32A_Blend_BlitRow32 |
| 119 | }; |
| 120 | |
| 121 | SkBlitRow::Proc32 SkBlitRow::Factory32(unsigned flags) { |
| 122 | SkASSERT(flags < SK_ARRAY_COUNT(gDefault_Procs32)); |
| 123 | // just so we don't crash |
| 124 | flags &= kFlags32_Mask; |
reed@google.com | 981d479 | 2011-03-09 12:55:47 +0000 | [diff] [blame] | 125 | |
senorblanco@chromium.org | 9272761 | 2009-11-04 20:51:06 +0000 | [diff] [blame] | 126 | SkBlitRow::Proc32 proc = PlatformProcs32(flags); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame^] | 127 | if (nullptr == proc) { |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 128 | proc = gDefault_Procs32[flags]; |
| 129 | } |
| 130 | SkASSERT(proc); |
| 131 | return proc; |
| 132 | } |
| 133 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 134 | #include "Sk4px.h" |
| 135 | |
mtklein | 95cc012 | 2015-04-27 15:11:01 -0700 | [diff] [blame] | 136 | // Color32 uses the blend_256_round_alt algorithm from tests/BlendTest.cpp. |
| 137 | // It's not quite perfect, but it's never wrong in the interesting edge cases, |
| 138 | // and it's quite a bit faster than blend_perfect. |
mtklein | a4a0aeb | 2015-04-21 08:09:30 -0700 | [diff] [blame] | 139 | // |
| 140 | // blend_256_round_alt is our currently blessed algorithm. Please use it or an analogous one. |
mtklein | 95cc012 | 2015-04-27 15:11:01 -0700 | [diff] [blame] | 141 | void SkBlitRow::Color32(SkPMColor dst[], const SkPMColor src[], int count, SkPMColor color) { |
mtklein | afe2ffb | 2015-04-17 11:00:54 -0700 | [diff] [blame] | 142 | switch (SkGetPackedA32(color)) { |
| 143 | case 0: memmove(dst, src, count * sizeof(SkPMColor)); return; |
| 144 | case 255: sk_memset32(dst, color, count); return; |
| 145 | } |
| 146 | |
mtklein | 051a51e | 2015-05-22 06:08:29 -0700 | [diff] [blame] | 147 | // This Sk4px impl works great on other platforms or when we have NEON. |
| 148 | #if defined(SK_CPU_ARM32) && !defined(SK_ARM_HAS_NEON) |
| 149 | if (auto proc = PlatformColor32Proc()) { return proc(dst, src, count, color); } |
| 150 | #endif |
| 151 | |
mtklein | afe2ffb | 2015-04-17 11:00:54 -0700 | [diff] [blame] | 152 | unsigned invA = 255 - SkGetPackedA32(color); |
mtklein | a4a0aeb | 2015-04-21 08:09:30 -0700 | [diff] [blame] | 153 | invA += invA >> 7; |
mtklein | 95cc012 | 2015-04-27 15:11:01 -0700 | [diff] [blame] | 154 | SkASSERT(invA < 256); // We've already handled alpha == 0 above. |
| 155 | |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 156 | Sk16h colorHighAndRound = Sk4px::DupPMColor(color).widenHi() + Sk16h(128); |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 157 | Sk16b invA_16x(invA); |
mtklein | 95cc012 | 2015-04-27 15:11:01 -0700 | [diff] [blame] | 158 | |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 159 | Sk4px::MapSrc(count, dst, src, [&](const Sk4px& src4) -> Sk4px { |
mtklein | 059ac00 | 2015-06-22 10:39:38 -0700 | [diff] [blame] | 160 | return (src4 * invA_16x).addNarrowHi(colorHighAndRound); |
mtklein | d2ffd36 | 2015-05-12 06:11:21 -0700 | [diff] [blame] | 161 | }); |
reed@android.com | c4cae85 | 2009-09-23 15:06:10 +0000 | [diff] [blame] | 162 | } |