diff options
author | robertphillips <robertphillips@google.com> | 2015-12-10 13:29:14 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-12-10 13:29:14 -0800 |
commit | 4e56772432825e174b8f9a3370235381482a8f51 (patch) | |
tree | 517fa5d131c543257612b36ea5ebadbfbee324fe | |
parent | 0a42e6827b178468105343ffdf40b011b7ba283c (diff) |
Address precision issue in circle blur effect
BUG=560651
Review URL: https://codereview.chromium.org/1504333003
-rw-r--r-- | gm/blurredclippedcircle.cpp | 92 | ||||
-rw-r--r-- | src/effects/GrCircleBlurFragmentProcessor.cpp | 17 |
2 files changed, 103 insertions, 6 deletions
diff --git a/gm/blurredclippedcircle.cpp b/gm/blurredclippedcircle.cpp new file mode 100644 index 0000000000..d1d374c7d4 --- /dev/null +++ b/gm/blurredclippedcircle.cpp @@ -0,0 +1,92 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "gm.h" +#include "SkBlurMaskFilter.h" +#include "SkColorFilter.h" +#include "SkPaint.h" +#include "SkRRect.h" + +namespace skiagm { + +// This GM reproduces the precision artifacts seen in crbug.com/560651. +// It draws a largish blurred circle with its center clipped out. +class BlurredClippedCircleGM : public GM { +public: + BlurredClippedCircleGM() { + this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC)); + } + +protected: + + SkString onShortName() override { + return SkString("blurredclippedcircle"); + } + + SkISize onISize() override { + return SkISize::Make(kWidth, kHeight); + } + + void onDraw(SkCanvas* canvas) override { + SkPaint whitePaint; + whitePaint.setColor(SK_ColorWHITE); + whitePaint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode))->unref(); + whitePaint.setAntiAlias(true); + + // This scale exercises precision limits in the circle blur effect (crbug.com/560651) + static const float kScale = 2.0f; + canvas->scale(kScale, kScale); + + canvas->save(); + SkRect clipRect1 = SkRect::MakeLTRB(0, 0, + SkIntToScalar(kWidth), SkIntToScalar(kHeight)); + + canvas->clipRect(clipRect1, SkRegion::kIntersect_Op, false); + + canvas->save(); + + canvas->clipRect(clipRect1, SkRegion::kIntersect_Op, false); + canvas->drawRect(clipRect1, whitePaint); + + canvas->save(); + + SkRect clipRect2 = SkRect::MakeLTRB(8, 8, 288, 288); + SkRRect clipRRect = SkRRect::MakeOval(clipRect2); + canvas->clipRRect(clipRRect, SkRegion::kDifference_Op, true); + + SkRect r = SkRect::MakeLTRB(4, 4, 292, 292); + SkRRect rr = SkRRect::MakeOval(r); + + SkPaint paint; + + paint.setMaskFilter(SkBlurMaskFilter::Create( + kNormal_SkBlurStyle, + 1.366025f, + SkBlurMaskFilter::kHighQuality_BlurFlag))->unref(); + paint.setColorFilter(SkColorFilter::CreateModeFilter( + SK_ColorRED, + SkXfermode::kSrcIn_Mode))->unref(); + paint.setAntiAlias(true); + + canvas->drawRRect(rr, paint); + + canvas->restore(); + canvas->restore(); + canvas->restore(); + } + +private: + static const int kWidth = 1164; + static const int kHeight = 802; + + typedef GM INHERITED; +}; + +////////////////////////////////////////////////////////////////////////////// + +DEF_GM(return new BlurredClippedCircleGM;) +} diff --git a/src/effects/GrCircleBlurFragmentProcessor.cpp b/src/effects/GrCircleBlurFragmentProcessor.cpp index 88109921f7..83faa86fa2 100644 --- a/src/effects/GrCircleBlurFragmentProcessor.cpp +++ b/src/effects/GrCircleBlurFragmentProcessor.cpp @@ -40,7 +40,7 @@ void GrGLCircleBlurFragmentProcessor::emitCode(EmitArgs& args) { // The data is formatted as: // x,y - the center of the circle // z - the distance at which the intensity starts falling off (e.g., the start of the table) - // w - the size of the profile texture + // w - the inverse of the profile texture size fDataUniform = args.fUniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility, kVec4f_GrSLType, kDefault_GrSLPrecision, @@ -56,8 +56,13 @@ void GrGLCircleBlurFragmentProcessor::emitCode(EmitArgs& args) { fragBuilder->codeAppendf("vec4 src=vec4(1);"); } - fragBuilder->codeAppendf("vec2 vec = %s.xy - %s.xy;", fragmentPos, dataName); - fragBuilder->codeAppendf("float dist = (length(vec) - %s.z + 0.5) / %s.w;", dataName, dataName); + // We just want to compute "length(vec) - %s.z + 0.5) * %s.w" but need to rearrange + // for precision + fragBuilder->codeAppendf("vec2 vec = vec2( (%s.x - %s.x) * %s.w , (%s.y - %s.y) * %s.w );", + fragmentPos, dataName, dataName, + fragmentPos, dataName, dataName); + fragBuilder->codeAppendf("float dist = length(vec) + ( 0.5 - %s.z ) * %s.w;", + dataName, dataName); fragBuilder->codeAppendf("float intensity = "); fragBuilder->appendTextureLookup(args.fSamplers[0], "vec2(dist, 0.5)"); @@ -74,9 +79,9 @@ void GrGLCircleBlurFragmentProcessor::onSetData(const GrGLSLProgramDataManager& // The data is formatted as: // x,y - the center of the circle // z - the distance at which the intensity starts falling off (e.g., the start of the table) - // w - the size of the profile texture + // w - the inverse of the profile texture size pdman.set4f(fDataUniform, circle.centerX(), circle.centerY(), cbfp.offset(), - SkIntToScalar(cbfp.profileSize())); + 1.0f / cbfp.profileSize()); } /////////////////////////////////////////////////////////////////////////////// @@ -181,7 +186,7 @@ static inline void compute_profile_offset_and_size(float halfWH, float sigma, // The circle is bigger than the Gaussian. In this case we know the interior of the // blurred circle is solid. *offset = halfWH - 3 * sigma; // This location maps to 0.5f in the weights texture. - // It should always be 255. + // It should always be 255. *size = SkScalarCeilToInt(6*sigma); } else { // The Gaussian is bigger than the circle. |