From 55853db3cce9539746fe202519a534c85ecdf62c Mon Sep 17 00:00:00 2001 From: "reed@google.com" Date: Fri, 1 Feb 2013 19:34:59 +0000 Subject: [PATCH] Move dither toggle logic into common helper functions, to make it easier to change their behavior later. No functional change. Review URL: https://codereview.appspot.com/7241063 git-svn-id: http://skia.googlecode.com/svn/trunk@7525 2bbb7eff-a529-9590-31e7-b0007b416f81 --- src/effects/gradients/SkGradientShaderPriv.h | 16 +++++++++++ src/effects/gradients/SkLinearGradient.cpp | 30 ++++++++++---------- src/effects/gradients/SkRadialGradient.cpp | 26 ++++++++--------- src/effects/gradients/SkSweepGradient.cpp | 6 ++-- 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/effects/gradients/SkGradientShaderPriv.h b/src/effects/gradients/SkGradientShaderPriv.h index d35fb1c596..4d14dc6a07 100644 --- a/src/effects/gradients/SkGradientShaderPriv.h +++ b/src/effects/gradients/SkGradientShaderPriv.h @@ -175,6 +175,22 @@ private: typedef SkShader INHERITED; }; +static inline int init_dither_toggle(int x, int y) { + return ((x ^ y) & 1) * SkGradientShaderBase::kDitherStride32; +} + +static inline int next_dither_toggle(int toggle) { + return toggle ^ SkGradientShaderBase::kDitherStride32; +} + +static inline int init_dither_toggle16(int x, int y) { + return ((x ^ y) & 1) * SkGradientShaderBase::kDitherStride16; +} + +static inline int next_dither_toggle16(int toggle) { + return toggle ^ SkGradientShaderBase::kDitherStride16; +} + /////////////////////////////////////////////////////////////////////////////// #if SK_SUPPORT_GPU diff --git a/src/effects/gradients/SkLinearGradient.cpp b/src/effects/gradients/SkLinearGradient.cpp index b194d50a84..0d3cc7e0be 100644 --- a/src/effects/gradients/SkLinearGradient.cpp +++ b/src/effects/gradients/SkLinearGradient.cpp @@ -111,7 +111,7 @@ bool SkLinearGradient::setContext(const SkBitmap& device, const SkPaint& paint, SkASSERT(fi <= 0xFF); \ fx += dx; \ *dstC++ = cache[toggle + fi]; \ - toggle ^= SkGradientShaderBase::kDitherStride32; \ + toggle = next_dither_toggle(toggle); \ } while (0) namespace { @@ -156,7 +156,7 @@ void shadeSpan_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx, if ((count = range.fCount0) > 0) { sk_memset32_dither(dstC, cache[toggle + range.fV0], - cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + range.fV0], + cache[next_dither_toggle(toggle) + range.fV0], count); dstC += count; } @@ -178,7 +178,7 @@ void shadeSpan_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx, if ((count = range.fCount2) > 0) { sk_memset32_dither(dstC, cache[toggle + range.fV1], - cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + range.fV1], + cache[next_dither_toggle(toggle) + range.fV1], count); } } @@ -192,7 +192,7 @@ void shadeSpan_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx, SkASSERT(fi <= 0xFF); fx += dx; *dstC++ = cache[toggle + fi]; - toggle ^= SkGradientShaderBase::kDitherStride32; + toggle = next_dither_toggle(toggle); } while (--count != 0); } @@ -205,7 +205,7 @@ void shadeSpan_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx, SkASSERT(fi <= 0xFF); fx += dx; *dstC++ = cache[toggle + fi]; - toggle ^= SkGradientShaderBase::kDitherStride32; + toggle = next_dither_toggle(toggle); } while (--count != 0); } @@ -220,7 +220,7 @@ void SkLinearGradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC, TileProc proc = fTileProc; const SkPMColor* SK_RESTRICT cache = this->getCache32(); #ifdef USE_DITHER_32BIT_GRADIENT - int toggle = ((x ^ y) & 1) * kDitherStride32; + int toggle = init_dither_toggle(x, y); #else int toggle = 0; #endif @@ -258,7 +258,7 @@ void SkLinearGradient::shadeSpan(int x, int y, SkPMColor* SK_RESTRICT dstC, unsigned fi = proc(SkScalarToFixed(srcPt.fX)); SkASSERT(fi <= 0xFFFF); *dstC++ = cache[toggle + (fi >> kCache32Shift)]; - toggle ^= SkGradientShaderBase::kDitherStride32; + toggle = next_dither_toggle(toggle); dstX += SK_Scalar1; } while (--count != 0); } @@ -310,7 +310,7 @@ static void dither_memset16(uint16_t dst[], uint16_t value, uint16_t other, SkASSERT(fi < SkGradientShaderBase::kCache16Count); \ fx += dx; \ *dstC++ = cache[toggle + fi]; \ - toggle ^= SkGradientShaderBase::kDitherStride16; \ + toggle = next_dither_toggle16(toggle); \ } while (0) namespace { @@ -327,7 +327,7 @@ void shadeSpan16_linear_vertical(TileProc proc, SkFixed dx, SkFixed fx, unsigned fi = proc(fx) >> SkGradientShaderBase::kCache16Shift; SkASSERT(fi < SkGradientShaderBase::kCache16Count); dither_memset16(dstC, cache[toggle + fi], - cache[(toggle ^ SkGradientShaderBase::kDitherStride16) + fi], count); + cache[next_dither_toggle16(toggle) + fi], count); } @@ -341,7 +341,7 @@ void shadeSpan16_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx, if ((count = range.fCount0) > 0) { dither_memset16(dstC, cache[toggle + range.fV0], - cache[(toggle ^ SkGradientShaderBase::kDitherStride16) + range.fV0], + cache[next_dither_toggle16(toggle) + range.fV0], count); dstC += count; } @@ -363,7 +363,7 @@ void shadeSpan16_linear_clamp(TileProc proc, SkFixed dx, SkFixed fx, if ((count = range.fCount2) > 0) { dither_memset16(dstC, cache[toggle + range.fV1], - cache[(toggle ^ SkGradientShaderBase::kDitherStride16) + range.fV1], + cache[next_dither_toggle16(toggle) + range.fV1], count); } } @@ -378,7 +378,7 @@ void shadeSpan16_linear_mirror(TileProc proc, SkFixed dx, SkFixed fx, SkASSERT(fi < SkGradientShaderBase::kCache16Count); fx += dx; *dstC++ = cache[toggle + fi]; - toggle ^= SkGradientShaderBase::kDitherStride16; + toggle = next_dither_toggle16(toggle); } while (--count != 0); } @@ -392,7 +392,7 @@ void shadeSpan16_linear_repeat(TileProc proc, SkFixed dx, SkFixed fx, SkASSERT(fi < SkGradientShaderBase::kCache16Count); fx += dx; *dstC++ = cache[toggle + fi]; - toggle ^= SkGradientShaderBase::kDitherStride16; + toggle = next_dither_toggle16(toggle); } while (--count != 0); } } @@ -405,7 +405,7 @@ void SkLinearGradient::shadeSpan16(int x, int y, SkMatrix::MapXYProc dstProc = fDstToIndexProc; TileProc proc = fTileProc; const uint16_t* SK_RESTRICT cache = this->getCache16(); - int toggle = ((x ^ y) & 1) * kDitherStride16; + int toggle = init_dither_toggle16(x, y); if (fDstToIndexClass != kPerspective_MatrixClass) { dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, @@ -442,7 +442,7 @@ void SkLinearGradient::shadeSpan16(int x, int y, int index = fi >> kCache16Shift; *dstC++ = cache[toggle + index]; - toggle ^= SkGradientShaderBase::kDitherStride16; + toggle = next_dither_toggle16(toggle); dstX += SK_Scalar1; } while (--count != 0); diff --git a/src/effects/gradients/SkRadialGradient.cpp b/src/effects/gradients/SkRadialGradient.cpp index 6945d5a8cc..328fe76e51 100644 --- a/src/effects/gradients/SkRadialGradient.cpp +++ b/src/effects/gradients/SkRadialGradient.cpp @@ -88,7 +88,7 @@ void shadeSpan16_radial_clamp(SkScalar sfx, SkScalar sdx, fx += dx; *dstC++ = cache[toggle + (sqrt_table[fi] >> SkGradientShaderBase::kSqrt16Shift)]; - toggle ^= SkGradientShaderBase::kDitherStride16; + toggle = next_dither_toggle16(toggle); } while (--count != 0); } else { do { @@ -100,7 +100,7 @@ void shadeSpan16_radial_clamp(SkScalar sfx, SkScalar sdx, fy += dy; *dstC++ = cache[toggle + (sqrt_table[fi] >> SkGradientShaderBase::kSqrt16Shift)]; - toggle ^= SkGradientShaderBase::kDitherStride16; + toggle = next_dither_toggle16(toggle); } while (--count != 0); } } @@ -123,7 +123,7 @@ void shadeSpan16_radial_mirror(SkScalar sfx, SkScalar sdx, unsigned fi = mirror_tileproc(dist); SkASSERT(fi <= 0xFFFF); *dstC++ = cache[toggle + (fi >> SkGradientShaderBase::kCache16Shift)]; - toggle ^= SkGradientShaderBase::kDitherStride16; + toggle = next_dither_toggle16(toggle); sfx += sdx; sfy += sdy; } while (--count != 0); @@ -144,7 +144,7 @@ void shadeSpan16_radial_repeat(SkScalar sfx, SkScalar sdx, fx += dx; fy += dy; *dstC++ = cache[toggle + (fi >> SkGradientShaderBase::kCache16Shift)]; - toggle ^= SkGradientShaderBase::kDitherStride16; + toggle = next_dither_toggle16(toggle); } while (--count != 0); } @@ -175,7 +175,7 @@ void SkRadialGradient::shadeSpan16(int x, int y, uint16_t* dstCParam, SkMatrix::MapXYProc dstProc = fDstToIndexProc; TileProc proc = fTileProc; const uint16_t* SK_RESTRICT cache = this->getCache16(); - int toggle = ((x ^ y) & 1) * kDitherStride16; + int toggle = init_dither_toggle16(x, y); if (fDstToIndexClass != kPerspective_MatrixClass) { dstProc(fDstToIndex, SkIntToScalar(x) + SK_ScalarHalf, @@ -214,7 +214,7 @@ void SkRadialGradient::shadeSpan16(int x, int y, uint16_t* dstCParam, int index = fi >> (16 - kCache16Bits); *dstC++ = cache[toggle + index]; - toggle ^= kDitherStride16; + toggle = next_dither_toggle16(toggle); dstX += SK_Scalar1; } while (--count != 0); @@ -296,7 +296,7 @@ inline bool no_need_for_radial_pin(int fx, int dx, fi = (fx * fx + fy * fy) >> (14 + 16 - kSQRT_TABLE_BITS); \ *dstC++ = cache[toggle + \ (sqrt_table[fi] >> SkGradientShaderBase::kSqrt32Shift)]; \ - toggle ^= SkGradientShaderBase::kDitherStride32; \ + toggle = next_dither_toggle(toggle); \ fx += dx; \ fy += dy; @@ -321,7 +321,7 @@ void shadeSpan_radial_clamp(SkScalar sfx, SkScalar sdx, unsigned fi = SkGradientShaderBase::kCache32Count - 1; sk_memset32_dither(dstC, cache[toggle + fi], - cache[(toggle ^ SkGradientShaderBase::kDitherStride32) + fi], + cache[next_dither_toggle(toggle) + fi], count); } else if ((count > 4) && no_need_for_radial_pin(fx, dx, fy, dy, count)) { @@ -347,7 +347,7 @@ void shadeSpan_radial_clamp(SkScalar sfx, SkScalar sdx, fi = SkFastMin32(fi, 0xFFFF >> (16 - kSQRT_TABLE_BITS)); *dstC++ = cache[toggle + (sqrt_table[fi] >> SkGradientShaderBase::kSqrt32Shift)]; - toggle ^= SkGradientShaderBase::kDitherStride32; + toggle = next_dither_toggle(toggle); fx += dx; } while (--count != 0); } else { @@ -358,7 +358,7 @@ void shadeSpan_radial_clamp(SkScalar sfx, SkScalar sdx, fi = SkFastMin32(fi, 0xFFFF >> (16 - kSQRT_TABLE_BITS)); *dstC++ = cache[toggle + (sqrt_table[fi] >> SkGradientShaderBase::kSqrt32Shift)]; - toggle ^= SkGradientShaderBase::kDitherStride32; + toggle = next_dither_toggle(toggle); fx += dx; fy += dy; } while (--count != 0); @@ -387,7 +387,7 @@ void shadeSpan_radial_mirror(SkScalar sfx, SkScalar sdx, unsigned fi = mirror_tileproc(dist); SkASSERT(fi <= 0xFFFF); *dstC++ = cache[toggle + (fi >> SkGradientShaderBase::kCache32Shift)]; - toggle ^= SkGradientShaderBase::kDitherStride32; + toggle = next_dither_toggle(toggle); sfx += sdx; sfy += sdy; } while (--count != 0); @@ -410,7 +410,7 @@ void shadeSpan_radial_repeat(SkScalar sfx, SkScalar sdx, unsigned fi = repeat_tileproc(dist); SkASSERT(fi <= 0xFFFF); *dstC++ = cache[toggle + (fi >> SkGradientShaderBase::kCache32Shift)]; - toggle ^= SkGradientShaderBase::kDitherStride32; + toggle = next_dither_toggle(toggle); fx += dx; fy += dy; } while (--count != 0); @@ -426,7 +426,7 @@ void SkRadialGradient::shadeSpan(int x, int y, TileProc proc = fTileProc; const SkPMColor* SK_RESTRICT cache = this->getCache32(); #ifdef USE_DITHER_32BIT_GRADIENT - int toggle = ((x ^ y) & 1) * SkGradientShaderBase::kDitherStride32; + int toggle = init_dither_toggle(x, y); #else int toggle = 0; #endif diff --git a/src/effects/gradients/SkSweepGradient.cpp b/src/effects/gradients/SkSweepGradient.cpp index 3cdac8577b..db18521066 100644 --- a/src/effects/gradients/SkSweepGradient.cpp +++ b/src/effects/gradients/SkSweepGradient.cpp @@ -337,7 +337,7 @@ void SkSweepGradient::shadeSpan16(int x, int y, uint16_t* SK_RESTRICT dstC, SkMatrix::MapXYProc proc = fDstToIndexProc; const SkMatrix& matrix = fDstToIndex; const uint16_t* SK_RESTRICT cache = this->getCache16(); - int toggle = ((x ^ y) & 1) * kDitherStride16; + int toggle = init_dither_toggle16(x, y); SkPoint srcPt; if (fDstToIndexClass != kPerspective_MatrixClass) { @@ -361,7 +361,7 @@ void SkSweepGradient::shadeSpan16(int x, int y, uint16_t* SK_RESTRICT dstC, for (; count > 0; --count) { int index = SkATan2_255(fy, fx) >> (8 - kCache16Bits); *dstC++ = cache[toggle + index]; - toggle ^= kDitherStride16; + toggle = next_dither_toggle16(toggle); fx += dx; fy += dy; } @@ -373,7 +373,7 @@ void SkSweepGradient::shadeSpan16(int x, int y, uint16_t* SK_RESTRICT dstC, int index = SkATan2_255(srcPt.fY, srcPt.fX); index >>= (8 - kCache16Bits); *dstC++ = cache[toggle + index]; - toggle ^= kDitherStride16; + toggle = next_dither_toggle16(toggle); } } }