mtklein | 4a37d08 | 2015-09-10 10:38:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 7 | |
| 8 | #ifndef SkBlitRow_opts_DEFINED |
| 9 | #define SkBlitRow_opts_DEFINED |
| 10 | |
Mike Klein | c33e6dc | 2019-04-10 11:44:42 -0500 | [diff] [blame] | 11 | #include "SkVx.h" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 12 | #include "SkColorData.h" |
mtklein | b4a7dc9 | 2016-03-23 06:29:12 -0700 | [diff] [blame] | 13 | #include "SkMSAN.h" |
| 14 | |
| 15 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
Herbert Derby | d8e2b13 | 2017-11-29 11:02:07 -0500 | [diff] [blame] | 16 | #include <immintrin.h> |
Mike Klein | f3086f0 | 2018-12-04 15:14:28 -0500 | [diff] [blame] | 17 | |
| 18 | static inline __m128i SkPMSrcOver_SSE2(const __m128i& src, const __m128i& dst) { |
| 19 | auto SkAlphaMulQ_SSE2 = [](const __m128i& c, const __m128i& scale) { |
| 20 | const __m128i mask = _mm_set1_epi32(0xFF00FF); |
| 21 | __m128i s = _mm_or_si128(_mm_slli_epi32(scale, 16), scale); |
| 22 | |
| 23 | // uint32_t rb = ((c & mask) * scale) >> 8 |
| 24 | __m128i rb = _mm_and_si128(mask, c); |
| 25 | rb = _mm_mullo_epi16(rb, s); |
| 26 | rb = _mm_srli_epi16(rb, 8); |
| 27 | |
| 28 | // uint32_t ag = ((c >> 8) & mask) * scale |
| 29 | __m128i ag = _mm_srli_epi16(c, 8); |
| 30 | ag = _mm_mullo_epi16(ag, s); |
| 31 | |
| 32 | // (rb & mask) | (ag & ~mask) |
| 33 | ag = _mm_andnot_si128(mask, ag); |
| 34 | return _mm_or_si128(rb, ag); |
| 35 | }; |
| 36 | return _mm_add_epi32(src, |
| 37 | SkAlphaMulQ_SSE2(dst, _mm_sub_epi32(_mm_set1_epi32(256), |
| 38 | _mm_srli_epi32(src, 24)))); |
| 39 | } |
mtklein | b4a7dc9 | 2016-03-23 06:29:12 -0700 | [diff] [blame] | 40 | #endif |
mtklein | 4a37d08 | 2015-09-10 10:38:02 -0700 | [diff] [blame] | 41 | |
| 42 | namespace SK_OPTS_NS { |
| 43 | |
Mike Klein | 3d50730 | 2019-04-15 08:56:06 -0500 | [diff] [blame^] | 44 | #if defined(SK_ARM_HAS_NEON) |
| 45 | // With NEON we can do eight u8*u8 -> u16 in one instruction, vmull_u8 (read, mul-long). |
| 46 | // TODO(mtklein): I wish I could make this a bit prettier and still get ideal codegen. |
| 47 | static inline skvx::Vec<4,uint16_t> mull(skvx::Vec<4,uint8_t> x, skvx::Vec<4,uint8_t> y) { |
| 48 | return skvx::to_vec<8,uint16_t>( vmull_u8(skvx::to_vext(skvx::join(x,x)), |
| 49 | skvx::to_vext(skvx::join(y,y))) ) |
| 50 | .lo; |
| 51 | } |
| 52 | static inline skvx::Vec<16,uint16_t> mull(skvx::Vec<16,uint8_t> x, skvx::Vec<16,uint8_t> y) { |
| 53 | uint16x8_t lo = vmull_u8( skvx::to_vext(x.lo), skvx::to_vext(y.lo) ), |
| 54 | hi = vmull_u8( skvx::to_vext(x.hi), skvx::to_vext(y.hi) ); |
| 55 | // TODO: why can't I get skvx::join() to generate the same code as this? |
| 56 | skvx::Vec<16,uint16_t> r; |
| 57 | memcpy(&r.lo, &lo, sizeof(lo)); |
| 58 | memcpy(&r.hi, &hi, sizeof(hi)); |
| 59 | return r; |
| 60 | } |
| 61 | #else |
| 62 | // Nothing special when we don't have NEON... just cast up to 16-bit and multiply. |
| 63 | template <int N> |
| 64 | static inline skvx::Vec<N,uint16_t> mull(skvx::Vec<N,uint8_t> x, skvx::Vec<N,uint8_t> y) { |
| 65 | return skvx::cast<uint16_t>(x) |
| 66 | * skvx::cast<uint16_t>(y); |
| 67 | } |
| 68 | #endif |
| 69 | |
Mike Klein | c33e6dc | 2019-04-10 11:44:42 -0500 | [diff] [blame] | 70 | // Blend constant color over count src pixels, writing into dst. |
| 71 | inline void blit_row_color32(SkPMColor* dst, const SkPMColor* src, int count, SkPMColor color) { |
Mike Klein | 3d50730 | 2019-04-15 08:56:06 -0500 | [diff] [blame^] | 72 | constexpr int N = 4; // 8, 16 also reasonable choices |
Mike Klein | c33e6dc | 2019-04-10 11:44:42 -0500 | [diff] [blame] | 73 | using U32 = skvx::Vec< N, uint32_t>; |
| 74 | using U16 = skvx::Vec<4*N, uint16_t>; |
| 75 | using U8 = skvx::Vec<4*N, uint8_t>; |
| 76 | |
| 77 | auto kernel = [color](U32 src) { |
| 78 | unsigned invA = 255 - SkGetPackedA32(color); |
| 79 | invA += invA >> 7; |
| 80 | SkASSERT(0 < invA && invA < 256); // We handle alpha == 0 or alpha == 255 specially. |
| 81 | |
| 82 | // (src * invA + (color << 8) + 128) >> 8 |
| 83 | // Should all fit in 16 bits. |
Mike Klein | 3d50730 | 2019-04-15 08:56:06 -0500 | [diff] [blame^] | 84 | U8 s = skvx::bit_pun<U8>(src), |
| 85 | a = U8(invA); |
| 86 | U16 c = skvx::cast<uint16_t>(skvx::bit_pun<U8>(U32(color))), |
| 87 | d = (mull(s,a) + (c << 8) + 128)>>8; |
Mike Klein | c33e6dc | 2019-04-10 11:44:42 -0500 | [diff] [blame] | 88 | return skvx::bit_pun<U32>(skvx::cast<uint8_t>(d)); |
| 89 | }; |
| 90 | |
| 91 | while (count >= N) { |
| 92 | kernel(U32::Load(src)).store(dst); |
| 93 | src += N; |
| 94 | dst += N; |
| 95 | count -= N; |
| 96 | } |
| 97 | while (count --> 0) { |
| 98 | *dst++ = kernel(U32{*src++})[0]; |
| 99 | } |
| 100 | } |
| 101 | |
Matteo Franchin | a132c38 | 2017-05-26 18:56:51 +0100 | [diff] [blame] | 102 | #if defined(SK_ARM_HAS_NEON) |
| 103 | |
| 104 | // Return a uint8x8_t value, r, computed as r[i] = SkMulDiv255Round(x[i], y[i]), where r[i], x[i], |
| 105 | // y[i] are the i-th lanes of the corresponding NEON vectors. |
| 106 | static inline uint8x8_t SkMulDiv255Round_neon8(uint8x8_t x, uint8x8_t y) { |
| 107 | uint16x8_t prod = vmull_u8(x, y); |
| 108 | return vraddhn_u16(prod, vrshrq_n_u16(prod, 8)); |
| 109 | } |
| 110 | |
| 111 | // The implementations of SkPMSrcOver below perform alpha blending consistently with |
| 112 | // SkMulDiv255Round. They compute the color components (numbers in the interval [0, 255]) as: |
| 113 | // |
| 114 | // result_i = src_i + rint(g(src_alpha, dst_i)) |
| 115 | // |
| 116 | // where g(x, y) = ((255.0 - x) * y) / 255.0 and rint rounds to the nearest integer. |
| 117 | |
| 118 | // In this variant of SkPMSrcOver each NEON register, dst.val[i], src.val[i], contains the value |
| 119 | // of the same color component for 8 consecutive pixels. The result of this function follows the |
| 120 | // same convention. |
| 121 | static inline uint8x8x4_t SkPMSrcOver_neon8(uint8x8x4_t dst, uint8x8x4_t src) { |
| 122 | uint8x8_t nalphas = vmvn_u8(src.val[3]); |
| 123 | uint8x8x4_t result; |
| 124 | result.val[0] = vadd_u8(src.val[0], SkMulDiv255Round_neon8(nalphas, dst.val[0])); |
| 125 | result.val[1] = vadd_u8(src.val[1], SkMulDiv255Round_neon8(nalphas, dst.val[1])); |
| 126 | result.val[2] = vadd_u8(src.val[2], SkMulDiv255Round_neon8(nalphas, dst.val[2])); |
| 127 | result.val[3] = vadd_u8(src.val[3], SkMulDiv255Round_neon8(nalphas, dst.val[3])); |
| 128 | return result; |
| 129 | } |
| 130 | |
| 131 | // In this variant of SkPMSrcOver dst and src contain the color components of two consecutive |
| 132 | // pixels. The return value follows the same convention. |
| 133 | static inline uint8x8_t SkPMSrcOver_neon2(uint8x8_t dst, uint8x8_t src) { |
| 134 | const uint8x8_t alpha_indices = vcreate_u8(0x0707070703030303); |
| 135 | uint8x8_t nalphas = vmvn_u8(vtbl1_u8(src, alpha_indices)); |
| 136 | return vadd_u8(src, SkMulDiv255Round_neon8(nalphas, dst)); |
| 137 | } |
| 138 | |
| 139 | #endif |
| 140 | |
Mike Klein | cd71f11 | 2017-08-23 11:11:55 -0400 | [diff] [blame] | 141 | /*not static*/ inline |
mtklein | b4a7dc9 | 2016-03-23 06:29:12 -0700 | [diff] [blame] | 142 | void blit_row_s32a_opaque(SkPMColor* dst, const SkPMColor* src, int len, U8CPU alpha) { |
| 143 | SkASSERT(alpha == 0xFF); |
| 144 | sk_msan_assert_initialized(src, src+len); |
| 145 | |
| 146 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41 |
| 147 | while (len >= 16) { |
| 148 | // Load 16 source pixels. |
| 149 | auto s0 = _mm_loadu_si128((const __m128i*)(src) + 0), |
| 150 | s1 = _mm_loadu_si128((const __m128i*)(src) + 1), |
| 151 | s2 = _mm_loadu_si128((const __m128i*)(src) + 2), |
| 152 | s3 = _mm_loadu_si128((const __m128i*)(src) + 3); |
| 153 | |
| 154 | const auto alphaMask = _mm_set1_epi32(0xFF000000); |
| 155 | |
| 156 | auto ORed = _mm_or_si128(s3, _mm_or_si128(s2, _mm_or_si128(s1, s0))); |
| 157 | if (_mm_testz_si128(ORed, alphaMask)) { |
| 158 | // All 16 source pixels are transparent. Nothing to do. |
| 159 | src += 16; |
| 160 | dst += 16; |
| 161 | len -= 16; |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | auto d0 = (__m128i*)(dst) + 0, |
| 166 | d1 = (__m128i*)(dst) + 1, |
| 167 | d2 = (__m128i*)(dst) + 2, |
| 168 | d3 = (__m128i*)(dst) + 3; |
| 169 | |
| 170 | auto ANDed = _mm_and_si128(s3, _mm_and_si128(s2, _mm_and_si128(s1, s0))); |
| 171 | if (_mm_testc_si128(ANDed, alphaMask)) { |
| 172 | // All 16 source pixels are opaque. SrcOver becomes Src. |
| 173 | _mm_storeu_si128(d0, s0); |
| 174 | _mm_storeu_si128(d1, s1); |
| 175 | _mm_storeu_si128(d2, s2); |
| 176 | _mm_storeu_si128(d3, s3); |
| 177 | src += 16; |
| 178 | dst += 16; |
| 179 | len -= 16; |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | // TODO: This math is wrong. |
| 184 | // Do SrcOver. |
| 185 | _mm_storeu_si128(d0, SkPMSrcOver_SSE2(s0, _mm_loadu_si128(d0))); |
| 186 | _mm_storeu_si128(d1, SkPMSrcOver_SSE2(s1, _mm_loadu_si128(d1))); |
| 187 | _mm_storeu_si128(d2, SkPMSrcOver_SSE2(s2, _mm_loadu_si128(d2))); |
| 188 | _mm_storeu_si128(d3, SkPMSrcOver_SSE2(s3, _mm_loadu_si128(d3))); |
| 189 | src += 16; |
| 190 | dst += 16; |
| 191 | len -= 16; |
| 192 | } |
| 193 | |
| 194 | #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
| 195 | while (len >= 16) { |
| 196 | // Load 16 source pixels. |
| 197 | auto s0 = _mm_loadu_si128((const __m128i*)(src) + 0), |
| 198 | s1 = _mm_loadu_si128((const __m128i*)(src) + 1), |
| 199 | s2 = _mm_loadu_si128((const __m128i*)(src) + 2), |
| 200 | s3 = _mm_loadu_si128((const __m128i*)(src) + 3); |
| 201 | |
| 202 | const auto alphaMask = _mm_set1_epi32(0xFF000000); |
| 203 | |
| 204 | auto ORed = _mm_or_si128(s3, _mm_or_si128(s2, _mm_or_si128(s1, s0))); |
| 205 | if (0xffff == _mm_movemask_epi8(_mm_cmpeq_epi8(_mm_and_si128(ORed, alphaMask), |
| 206 | _mm_setzero_si128()))) { |
| 207 | // All 16 source pixels are transparent. Nothing to do. |
| 208 | src += 16; |
| 209 | dst += 16; |
| 210 | len -= 16; |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | auto d0 = (__m128i*)(dst) + 0, |
| 215 | d1 = (__m128i*)(dst) + 1, |
| 216 | d2 = (__m128i*)(dst) + 2, |
| 217 | d3 = (__m128i*)(dst) + 3; |
| 218 | |
| 219 | auto ANDed = _mm_and_si128(s3, _mm_and_si128(s2, _mm_and_si128(s1, s0))); |
| 220 | if (0xffff == _mm_movemask_epi8(_mm_cmpeq_epi8(_mm_and_si128(ANDed, alphaMask), |
| 221 | alphaMask))) { |
| 222 | // All 16 source pixels are opaque. SrcOver becomes Src. |
| 223 | _mm_storeu_si128(d0, s0); |
| 224 | _mm_storeu_si128(d1, s1); |
| 225 | _mm_storeu_si128(d2, s2); |
| 226 | _mm_storeu_si128(d3, s3); |
| 227 | src += 16; |
| 228 | dst += 16; |
| 229 | len -= 16; |
| 230 | continue; |
| 231 | } |
| 232 | |
| 233 | // TODO: This math is wrong. |
| 234 | // Do SrcOver. |
| 235 | _mm_storeu_si128(d0, SkPMSrcOver_SSE2(s0, _mm_loadu_si128(d0))); |
| 236 | _mm_storeu_si128(d1, SkPMSrcOver_SSE2(s1, _mm_loadu_si128(d1))); |
| 237 | _mm_storeu_si128(d2, SkPMSrcOver_SSE2(s2, _mm_loadu_si128(d2))); |
| 238 | _mm_storeu_si128(d3, SkPMSrcOver_SSE2(s3, _mm_loadu_si128(d3))); |
| 239 | |
| 240 | src += 16; |
| 241 | dst += 16; |
| 242 | len -= 16; |
| 243 | } |
| 244 | |
| 245 | #elif defined(SK_ARM_HAS_NEON) |
Matteo Franchin | a132c38 | 2017-05-26 18:56:51 +0100 | [diff] [blame] | 246 | // Do 8-pixels at a time. A 16-pixels at a time version of this code was also tested, but it |
| 247 | // underperformed on some of the platforms under test for inputs with frequent transitions of |
| 248 | // alpha (corresponding to changes of the conditions [~]alpha_u64 == 0 below). It may be worth |
| 249 | // revisiting the situation in the future. |
| 250 | while (len >= 8) { |
| 251 | // Load 8 pixels in 4 NEON registers. src_col.val[i] will contain the same color component |
| 252 | // for 8 consecutive pixels (e.g. src_col.val[3] will contain all alpha components of 8 |
| 253 | // pixels). |
| 254 | uint8x8x4_t src_col = vld4_u8(reinterpret_cast<const uint8_t*>(src)); |
| 255 | src += 8; |
| 256 | len -= 8; |
| 257 | |
| 258 | // We now detect 2 special cases: the first occurs when all alphas are zero (the 8 pixels |
| 259 | // are all transparent), the second when all alphas are fully set (they are all opaque). |
| 260 | uint8x8_t alphas = src_col.val[3]; |
| 261 | uint64_t alphas_u64 = vget_lane_u64(vreinterpret_u64_u8(alphas), 0); |
| 262 | if (alphas_u64 == 0) { |
| 263 | // All pixels transparent. |
| 264 | dst += 8; |
mtklein | b4a7dc9 | 2016-03-23 06:29:12 -0700 | [diff] [blame] | 265 | continue; |
| 266 | } |
| 267 | |
Matteo Franchin | a132c38 | 2017-05-26 18:56:51 +0100 | [diff] [blame] | 268 | if (~alphas_u64 == 0) { |
| 269 | // All pixels opaque. |
| 270 | vst4_u8(reinterpret_cast<uint8_t*>(dst), src_col); |
| 271 | dst += 8; |
mtklein | b4a7dc9 | 2016-03-23 06:29:12 -0700 | [diff] [blame] | 272 | continue; |
| 273 | } |
| 274 | |
Matteo Franchin | a132c38 | 2017-05-26 18:56:51 +0100 | [diff] [blame] | 275 | uint8x8x4_t dst_col = vld4_u8(reinterpret_cast<uint8_t*>(dst)); |
| 276 | vst4_u8(reinterpret_cast<uint8_t*>(dst), SkPMSrcOver_neon8(dst_col, src_col)); |
| 277 | dst += 8; |
mtklein | b4a7dc9 | 2016-03-23 06:29:12 -0700 | [diff] [blame] | 278 | } |
Matteo Franchin | a132c38 | 2017-05-26 18:56:51 +0100 | [diff] [blame] | 279 | |
| 280 | // Deal with leftover pixels. |
| 281 | for (; len >= 2; len -= 2, src += 2, dst += 2) { |
| 282 | uint8x8_t src2 = vld1_u8(reinterpret_cast<const uint8_t*>(src)); |
| 283 | uint8x8_t dst2 = vld1_u8(reinterpret_cast<const uint8_t*>(dst)); |
| 284 | vst1_u8(reinterpret_cast<uint8_t*>(dst), SkPMSrcOver_neon2(dst2, src2)); |
| 285 | } |
| 286 | |
| 287 | if (len != 0) { |
| 288 | uint8x8_t result = SkPMSrcOver_neon2(vcreate_u8(*dst), vcreate_u8(*src)); |
| 289 | vst1_lane_u32(dst, vreinterpret_u32_u8(result), 0); |
| 290 | } |
| 291 | return; |
mtklein | b4a7dc9 | 2016-03-23 06:29:12 -0700 | [diff] [blame] | 292 | #endif |
| 293 | |
| 294 | while (len-- > 0) { |
mtklein | 3e31812 | 2016-06-17 13:47:53 -0700 | [diff] [blame] | 295 | // This 0xFF000000 is not semantically necessary, but for compatibility |
| 296 | // with chromium:611002 we need to keep it until we figure out where |
| 297 | // the non-premultiplied src values (like 0x00FFFFFF) are coming from. |
| 298 | // TODO(mtklein): sort this out and assert *src is premul here. |
| 299 | if (*src & 0xFF000000) { |
mtklein | b4a7dc9 | 2016-03-23 06:29:12 -0700 | [diff] [blame] | 300 | *dst = (*src >= 0xFF000000) ? *src : SkPMSrcOver(*src, *dst); |
| 301 | } |
| 302 | src++; |
| 303 | dst++; |
| 304 | } |
| 305 | } |
| 306 | |
mtklein | 4a37d08 | 2015-09-10 10:38:02 -0700 | [diff] [blame] | 307 | } // SK_OPTS_NS |
| 308 | |
| 309 | #endif//SkBlitRow_opts_DEFINED |