From 06a32062620daee7a93742f553829a8d4f93f720 Mon Sep 17 00:00:00 2001 From: "commit-bot@chromium.org" Date: Mon, 5 May 2014 21:35:09 +0000 Subject: [PATCH] fix TriColorShader to respect the paint's alpha results can be seen in new gm: vertices_80 BUG=skia: R=scroggo@google.com Author: reed@google.com Review URL: https://codereview.chromium.org/270023002 git-svn-id: http://skia.googlecode.com/svn/trunk@14581 2bbb7eff-a529-9590-31e7-b0007b416f81 --- include/core/SkColorPriv.h | 9 +++++++++ src/core/SkDraw.cpp | 12 ++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/include/core/SkColorPriv.h b/include/core/SkColorPriv.h index 9591f2272..d5571dfea 100644 --- a/include/core/SkColorPriv.h +++ b/include/core/SkColorPriv.h @@ -181,6 +181,15 @@ static inline unsigned SkAlpha255To256(U8CPU alpha) { return alpha + 1; } +/** + * Turn a 0..255 value into a 0..256 value, rounding up if the value is >= 0x80. + * This is slightly more accurate than SkAlpha255To256. + */ +static inline unsigned Sk255To256(U8CPU value) { + SkASSERT(SkToU8(value) == value); + return value + (value >> 7); +} + /** Multiplify value by 0..256, and shift the result down 8 (i.e. return (value * alpha256) >> 8) */ diff --git a/src/core/SkDraw.cpp b/src/core/SkDraw.cpp index a74e3c0f7..9347efe47 100644 --- a/src/core/SkDraw.cpp +++ b/src/core/SkDraw.cpp @@ -2432,6 +2432,8 @@ size_t SkTriColorShader::contextSize() const { return sizeof(TriColorShaderContext); } void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor dstC[], int count) { + const int alphaScale = Sk255To256(this->getPaintAlpha()); + SkPoint src; for (int i = 0; i < count; i++) { @@ -2450,9 +2452,15 @@ void SkTriColorShader::TriColorShaderContext::shadeSpan(int x, int y, SkPMColor scale0 = 0; } + if (256 != alphaScale) { + scale0 = SkAlphaMul(scale0, alphaScale); + scale1 = SkAlphaMul(scale1, alphaScale); + scale2 = SkAlphaMul(scale2, alphaScale); + } + dstC[i] = SkAlphaMulQ(fColors[0], scale0) + - SkAlphaMulQ(fColors[1], scale1) + - SkAlphaMulQ(fColors[2], scale2); + SkAlphaMulQ(fColors[1], scale1) + + SkAlphaMulQ(fColors[2], scale2); } }