2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBlurMask.h"
10 #include "SkBlurMaskFilter.h"
12 #include "SkColorFilter.h"
13 #include "SkLayerDrawLooper.h"
20 #include "SkXfermode.h"
22 // This GM mimics a blurred RR seen in the wild.
23 class BlurRoundRectGM : public skiagm::GM {
25 BlurRoundRectGM(int width, int height, int radius)
26 : fName("blurroundrect")
28 SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
29 fRRect.setRectXY(r, SkIntToScalar(radius), SkIntToScalar(radius));
30 fName.appendf("-WH-%ix%i-corner-%i", width, height, radius);
33 BlurRoundRectGM(int width, int height)
34 : fName("blurroundrect") {
35 fName.appendf("-WH-%ix%i-unevenCorners", width, height);
37 radii[0].set(SkIntToScalar(30), SkIntToScalar(30));
38 radii[1].set(SkIntToScalar(10), SkIntToScalar(10));
39 radii[2].set(SkIntToScalar(30), SkIntToScalar(30));
40 radii[3].set(SkIntToScalar(10), SkIntToScalar(10));
41 SkRect r = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
42 fRRect.setRectRadii(r, radii);
45 SkString onShortName() override {
49 SkISize onISize() override {
50 return SkISize::Make(SkScalarCeilToInt(fRRect.rect().width()),
51 SkScalarCeilToInt(fRRect.rect().height()));
54 void onDraw(SkCanvas* canvas) override {
55 SkLayerDrawLooper::Builder looperBuilder;
57 SkLayerDrawLooper::LayerInfo info;
58 info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
59 | SkLayerDrawLooper::kColorFilter_Bit;
60 info.fColorMode = SkXfermode::kSrc_Mode;
61 info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
62 info.fPostTranslate = false;
63 SkPaint* paint = looperBuilder.addLayerOnTop(info);
64 SkMaskFilter* maskFilter = SkBlurMaskFilter::Create(
66 SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf),
67 SkBlurMaskFilter::kHighQuality_BlurFlag);
68 paint->setMaskFilter(maskFilter)->unref();
69 SkColorFilter* colorFilter = SkColorFilter::CreateModeFilter(SK_ColorLTGRAY,
70 SkXfermode::kSrcIn_Mode);
71 paint->setColorFilter(colorFilter)->unref();
72 paint->setColor(SK_ColorGRAY);
75 SkLayerDrawLooper::LayerInfo info;
76 looperBuilder.addLayerOnTop(info);
79 canvas->drawRect(fRRect.rect(), paint);
81 paint.setLooper(looperBuilder.detachLooper())->unref();
82 paint.setColor(SK_ColorCYAN);
83 paint.setAntiAlias(true);
85 canvas->drawRRect(fRRect, paint);
92 typedef skiagm::GM INHERITED;
95 #include "SkGradientShader.h"
97 * Spits out a dummy gradient to test blur with shader on paint
99 static SkShader* MakeRadial() {
102 { SkIntToScalar(100), SkIntToScalar(100) }
104 SkShader::TileMode tm = SkShader::kClamp_TileMode;
105 const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, };
106 const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
108 scale.setScale(0.5f, 0.5f);
109 scale.postTranslate(5.f, 5.f);
110 SkPoint center0, center1;
111 center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
112 SkScalarAve(pts[0].fY, pts[1].fY));
113 center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
114 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
115 return SkGradientShader::CreateTwoPointRadial(center1, (pts[1].fX - pts[0].fX) / 7,
116 center0, (pts[1].fX - pts[0].fX) / 2,
117 colors, pos, SK_ARRAY_COUNT(colors), tm,
121 // Simpler blurred RR test cases where all the radii are the same.
122 class SimpleBlurRoundRectGM : public skiagm::GM {
124 SimpleBlurRoundRectGM()
125 : fName("simpleblurroundrect") {
130 SkString onShortName() override {
134 SkISize onISize() override {
135 return SkISize::Make(1000, 500);
138 void onDraw(SkCanvas* canvas) override {
139 canvas->scale(1.5f, 1.5f);
140 canvas->translate(50,50);
142 const float blurRadii[] = { 1,5,10,20 };
143 const int cornerRadii[] = { 1,5,10,20 };
144 const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
145 for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
146 SkAutoCanvasRestore autoRestore(canvas, true);
147 canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
148 for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
149 for (int k = 0; k <= 1; k++) {
150 SkMaskFilter* filter = SkBlurMaskFilter::Create(
152 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i])),
153 SkBlurMaskFilter::kHighQuality_BlurFlag);
155 paint.setColor(SK_ColorBLACK);
156 paint.setMaskFilter(filter)->unref();
158 bool useRadial = SkToBool(k);
160 paint.setShader(MakeRadial())->unref();
164 rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]),
165 SkIntToScalar(cornerRadii[j]));
166 canvas->drawRRect(rrect, paint);
167 canvas->translate(r.width() + SkIntToScalar(50), 0);
173 const SkString fName;
175 typedef skiagm::GM INHERITED;
178 // Create one with dimensions/rounded corners based on the skp
180 // TODO(scroggo): Disabled in an attempt to rememdy
181 // https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
182 // ran wrong number of tests')
183 //DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
185 // Rounded rect with two opposite corners with large radii, the other two
187 DEF_GM(return new BlurRoundRectGM(100, 100);)
189 DEF_GM(return new SimpleBlurRoundRectGM();)