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