Address precision issue in circle blur effect
BUG=560651 Review URL: https://codereview.chromium.org/1504333003
This commit is contained in:
parent
0a42e6827b
commit
4e56772432
92
gm/blurredclippedcircle.cpp
Normal file
92
gm/blurredclippedcircle.cpp
Normal file
@ -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;)
|
||||
}
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user