Revert of Convert Color32 code to perfect blend. (patchset #6 id:100001 of https://codereview.chromium.org/1098913002/)

Reason for revert:
Xfermode_SrcOver not looking encouraging.  Up to 50% regressions.

https://perf.skia.org/#3242

Original issue's description:
> Convert Color32 code to perfect blend.
>
> Before we commit to blend_256_round_alt, let's make sure blend_perfect is
> really slower in practice (i.e. regresses on perf.skia.org).
>
> blend_perfect is really the most desirable algorithm if we can afford it.  Not
> only is it correct, but it's easy to think about and break into correct pieces:
> for instance, its div255() doesn't require any coordination with the multiply.
>
> This looks like a 30% hit according to microbenches.  That said, microbenches
> said my previous change would be a 20-25% perf improvement, but it didn't end
> up showing a significant effect at a high level.
>
> As for correctness, I see a bunch of off-by-1 compared to blend_256_round_alt
> (exactly what we'd expect), and one off-by-3 in a GM that looks like it has a
> bunch of overdraw.
>
> BUG=skia:
>
> Committed: https://skia.googlesource.com/skia/+/61221e7f87a99765b0e034020e06bb018e2a08c2

TBR=reed@google.com,fmalita@chromium.org,mtklein@chromium.org
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:

Review URL: https://codereview.chromium.org/1083923006
This commit is contained in:
mtklein 2015-04-21 08:09:30 -07:00 committed by Commit bot
parent 8672f4dffa
commit a4a0aeb748
3 changed files with 64 additions and 42 deletions

View File

@ -142,8 +142,11 @@ SkBlitRow::Proc32 SkBlitRow::ColorProcFactory() {
#define SK_SUPPORT_LEGACY_COLOR32_MATHx #define SK_SUPPORT_LEGACY_COLOR32_MATHx
// Color32 and its SIMD specializations use the blend_perfect algorithm from tests/BlendTest.cpp. // Color32 and its SIMD specializations use the blend_256_round_alt algorithm
// An acceptable alternative is blend_256_round_alt, which is faster but not quite perfect. // 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, void SkBlitRow::Color32(SkPMColor* SK_RESTRICT dst,
const SkPMColor* SK_RESTRICT src, const SkPMColor* SK_RESTRICT src,
int count, SkPMColor color) { int count, SkPMColor color) {
@ -153,19 +156,19 @@ void SkBlitRow::Color32(SkPMColor* SK_RESTRICT dst,
} }
unsigned invA = 255 - SkGetPackedA32(color); 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) { while (count --> 0) {
// Our math is 16-bit, so we can do a little bit of SIMD in 32-bit registers. // Our math is 16-bit, so we can do a little bit of SIMD in 32-bit registers.
const uint32_t mask = 0x00FF00FF; const uint32_t mask = 0x00FF00FF;
uint32_t rb = (((*src >> 0) & mask) * invA), // r_b_ uint32_t rb = (((*src >> 0) & mask) * invA + round) >> 8, // _r_b
ag = (((*src >> 8) & mask) * invA); // a_g_ ag = (((*src >> 8) & mask) * invA + round) >> 0; // a_g_
#ifndef SK_SUPPORT_LEGACY_COLOR32_MATH *dst = color + ((rb & mask) | (ag & ~mask));
uint32_t round = (128 << 16) + (128 << 0);
rb += round;
ag += round;
rb += (rb & ~mask) >> 8;
ag += (ag & ~mask) >> 8;
#endif
*dst = color + (((rb>>8) & mask) | ((ag>>0) & ~mask));
src++; src++;
dst++; dst++;
} }

View File

@ -234,30 +234,41 @@ void S32A_Blend_BlitRow32_SSE2(SkPMColor* SK_RESTRICT dst,
#define SK_SUPPORT_LEGACY_COLOR32_MATHx #define SK_SUPPORT_LEGACY_COLOR32_MATHx
/* SSE2 version of Color32(), portable version is in core/SkBlitRow_D32.cpp */ /* SSE2 version of Color32()
// Color32 and its SIMD specializations use the blend_perfect algorithm from tests/BlendTest.cpp. * portable version is in core/SkBlitRow_D32.cpp
// An acceptable alternative is blend_256_round_alt, which is faster but not quite perfect. */
// 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 Color32_SSE2(SkPMColor dst[], const SkPMColor src[], int count, SkPMColor color) { void Color32_SSE2(SkPMColor dst[], const SkPMColor src[], int count, SkPMColor color) {
switch (SkGetPackedA32(color)) { switch (SkGetPackedA32(color)) {
case 0: memmove(dst, src, count * sizeof(SkPMColor)); return; case 0: memmove(dst, src, count * sizeof(SkPMColor)); return;
case 255: sk_memset32(dst, color, count); return; case 255: sk_memset32(dst, color, count); return;
} }
__m128i color_2x_high = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_set1_epi32(color)), __m128i colorHigh = _mm_unpacklo_epi8(_mm_setzero_si128(), _mm_set1_epi32(color));
invA_8x = _mm_set1_epi16(255 - SkGetPackedA32(color)); #ifdef SK_SUPPORT_LEGACY_COLOR32_MATH // blend_256_plus1_trunc, busted
__m128i colorAndRound = colorHigh;
#else // blend_256_round_alt, good
__m128i colorAndRound = _mm_add_epi16(colorHigh, _mm_set1_epi16(128));
#endif
unsigned invA = 255 - SkGetPackedA32(color);
#ifdef SK_SUPPORT_LEGACY_COLOR32_MATH // blend_256_plus1_trunc, busted
__m128i invA16 = _mm_set1_epi16(invA);
#else // blend_256_round_alt, good
SkASSERT(invA + (invA >> 7) < 256); // We should still fit in the low byte here.
__m128i invA16 = _mm_set1_epi16(invA + (invA >> 7));
#endif
// Does the core work of blending color onto 4 pixels, returning the resulting 4 pixels. // Does the core work of blending color onto 4 pixels, returning the resulting 4 pixels.
auto kernel = [&](const __m128i& src_4x) -> __m128i { auto kernel = [&](const __m128i& src4) -> __m128i {
__m128i lo = _mm_mullo_epi16(invA_8x, _mm_unpacklo_epi8(src_4x, _mm_setzero_si128())), __m128i lo = _mm_mullo_epi16(invA16, _mm_unpacklo_epi8(src4, _mm_setzero_si128())),
hi = _mm_mullo_epi16(invA_8x, _mm_unpackhi_epi8(src_4x, _mm_setzero_si128())); hi = _mm_mullo_epi16(invA16, _mm_unpackhi_epi8(src4, _mm_setzero_si128()));
#ifndef SK_SUPPORT_LEGACY_COLOR32_MATH return _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(colorAndRound, lo), 8),
lo = _mm_add_epi16(lo, _mm_set1_epi16(128)); _mm_srli_epi16(_mm_add_epi16(colorAndRound, hi), 8));
hi = _mm_add_epi16(hi, _mm_set1_epi16(128));
lo = _mm_add_epi16(lo, _mm_srli_epi16(lo, 8));
hi = _mm_add_epi16(hi, _mm_srli_epi16(hi, 8));
#endif
return _mm_packus_epi16(_mm_srli_epi16(_mm_add_epi16(color_2x_high, lo), 8),
_mm_srli_epi16(_mm_add_epi16(color_2x_high, hi), 8));
}; };
while (count >= 8) { while (count >= 8) {

View File

@ -1681,30 +1681,38 @@ void S32_D565_Opaque_Dither_neon(uint16_t* SK_RESTRICT dst,
#define SK_SUPPORT_LEGACY_COLOR32_MATHx #define SK_SUPPORT_LEGACY_COLOR32_MATHx
/* NEON version of Color32(), portable version is in core/SkBlitRow_D32.cpp */ // Color32 and its SIMD specializations use the blend_256_round_alt algorithm
// Color32 and its SIMD specializations use the blend_perfect algorithm from tests/BlendTest.cpp. // from tests/BlendTest.cpp. It's not quite perfect, but it's never wrong in the
// An acceptable alternative is blend_256_round_alt, which is faster but not quite perfect. // 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 Color32_arm_neon(SkPMColor* dst, const SkPMColor* src, int count, SkPMColor color) { void Color32_arm_neon(SkPMColor* dst, const SkPMColor* src, int count, SkPMColor color) {
switch (SkGetPackedA32(color)) { switch (SkGetPackedA32(color)) {
case 0: memmove(dst, src, count * sizeof(SkPMColor)); return; case 0: memmove(dst, src, count * sizeof(SkPMColor)); return;
case 255: sk_memset32(dst, color, count); return; case 255: sk_memset32(dst, color, count); return;
} }
uint16x8_t color_2x_high = vshll_n_u8((uint8x8_t)vdup_n_u32(color), 8); uint16x8_t colorHigh = vshll_n_u8((uint8x8_t)vdup_n_u32(color), 8);
uint8x8_t invA_8x = vdup_n_u8(255 - SkGetPackedA32(color)); #ifdef SK_SUPPORT_LEGACY_COLOR32_MATH // blend_256_plus1_trunc, busted
uint16x8_t colorAndRound = colorHigh;
#else // blend_256_round_alt, good
uint16x8_t colorAndRound = vaddq_u16(colorHigh, vdupq_n_u16(128));
#endif
unsigned invA = 255 - SkGetPackedA32(color);
#ifdef SK_SUPPORT_LEGACY_COLOR32_MATH // blend_256_plus1_trunc, busted
uint8x8_t invA8 = vdup_n_u8(invA);
#else // blend_256_round_alt, good
SkASSERT(invA + (invA >> 7) < 256); // This next part only works if alpha is not 0.
uint8x8_t invA8 = vdup_n_u8(invA + (invA >> 7));
#endif
// Does the core work of blending color onto 4 pixels, returning the resulting 4 pixels. // Does the core work of blending color onto 4 pixels, returning the resulting 4 pixels.
auto kernel = [&](const uint32x4_t& src4) -> uint32x4_t { auto kernel = [&](const uint32x4_t& src4) -> uint32x4_t {
uint16x8_t lo = vmull_u8(vget_low_u8( (uint8x16_t)src4), invA_8x), uint16x8_t lo = vmull_u8(vget_low_u8( (uint8x16_t)src4), invA8),
hi = vmull_u8(vget_high_u8((uint8x16_t)src4), invA_8x); hi = vmull_u8(vget_high_u8((uint8x16_t)src4), invA8);
#ifndef SK_SUPPORT_LEGACY_COLOR32_MATH
lo = vaddq_u16(lo, vdupq_n_u16(128));
hi = vaddq_u16(hi, vdupq_n_u16(128));
lo = vaddq_u16(lo, vshrq_n_u16(lo, 8));
hi = vaddq_u16(hi, vshrq_n_u16(hi, 8));
#endif
return (uint32x4_t) return (uint32x4_t)
vcombine_u8(vaddhn_u16(color_2x_high, lo), vaddhn_u16(color_2x_high, hi)); vcombine_u8(vaddhn_u16(colorAndRound, lo), vaddhn_u16(colorAndRound, hi));
}; };
while (count >= 8) { while (count >= 8) {