1 /*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 
8 #include "gm.h"
9 #include "SkBlurMask.h"
10 #include "SkCanvas.h"
11 #include "SkColorFilter.h"
12 #include "SkLayerDrawLooper.h"
13 #include "SkMaskFilter.h"
14 #include "SkPaint.h"
15 #include "SkPath.h"
16 #include "SkPoint.h"
17 #include "SkRect.h"
18 #include "SkRRect.h"
19 #include "SkString.h"
20 
21 // This GM mimics a blurred RR seen in the wild.
22 class BlurRoundRectGM : public skiagm::GM {
23 public:
BlurRoundRectGM(int width,int height)24     BlurRoundRectGM(int width, int height)
25         : fName("blurroundrect"), fWidth(width), fHeight(height) {
26         fName.appendf("-WH-%ix%i-unevenCorners", width,  height);
27     }
28 
onShortName()29     SkString onShortName() override {
30         return fName;
31     }
32 
onISize()33     SkISize onISize() override {
34         return SkISize::Make(fWidth, fHeight);
35     }
36 
onOnceBeforeDraw()37     void onOnceBeforeDraw() override {
38         SkVector radii[4];
39         radii[0].set(SkIntToScalar(30), SkIntToScalar(30));
40         radii[1].set(SkIntToScalar(10), SkIntToScalar(10));
41         radii[2].set(SkIntToScalar(30), SkIntToScalar(30));
42         radii[3].set(SkIntToScalar(10), SkIntToScalar(10));
43         SkRect r = SkRect::MakeWH(SkIntToScalar(fWidth), SkIntToScalar(fHeight));
44         fRRect.setRectRadii(r, radii);
45     }
46 
onDraw(SkCanvas * canvas)47     void onDraw(SkCanvas* canvas) override {
48         SkLayerDrawLooper::Builder looperBuilder;
49         {
50             SkLayerDrawLooper::LayerInfo info;
51             info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit
52                               | SkLayerDrawLooper::kColorFilter_Bit;
53             info.fColorMode = SkBlendMode::kSrc;
54             info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0));
55             info.fPostTranslate = false;
56             SkPaint* paint = looperBuilder.addLayerOnTop(info);
57             paint->setMaskFilter(SkMaskFilter::MakeBlur(
58                     kNormal_SkBlurStyle,
59                     SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf)));
60             paint->setColorFilter(SkColorFilter::MakeModeFilter(
61                     SK_ColorLTGRAY,
62                     SkBlendMode::kSrcIn));
63             paint->setColor(SK_ColorGRAY);
64         }
65         {
66             SkLayerDrawLooper::LayerInfo info;
67             looperBuilder.addLayerOnTop(info);
68         }
69         SkPaint paint;
70         canvas->drawRect(fRRect.rect(), paint);
71 
72         paint.setLooper(looperBuilder.detach());
73         paint.setColor(SK_ColorCYAN);
74         paint.setAntiAlias(true);
75 
76         canvas->drawRRect(fRRect, paint);
77     }
78 
79 private:
80     SkString        fName;
81     SkRRect         fRRect;
82     int             fWidth, fHeight;
83 
84     typedef skiagm::GM INHERITED;
85 };
86 
87 #include "SkGradientShader.h"
88 /*
89  * Spits out a dummy gradient to test blur with shader on paint
90  */
MakeRadial()91 static sk_sp<SkShader> MakeRadial() {
92     SkPoint pts[2] = {
93         { 0, 0 },
94         { SkIntToScalar(100), SkIntToScalar(100) }
95     };
96     SkShader::TileMode tm = SkShader::kClamp_TileMode;
97     const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, };
98     const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 };
99     SkMatrix scale;
100     scale.setScale(0.5f, 0.5f);
101     scale.postTranslate(5.f, 5.f);
102     SkPoint center0, center1;
103     center0.set(SkScalarAve(pts[0].fX, pts[1].fX),
104                 SkScalarAve(pts[0].fY, pts[1].fY));
105     center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5),
106                 SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4));
107     return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7,
108                                                  center0, (pts[1].fX - pts[0].fX) / 2,
109                                                  colors, pos, SK_ARRAY_COUNT(colors), tm,
110                                                  0, &scale);
111 }
112 
113 // Simpler blurred RR test cases where all the radii are the same.
114 class SimpleBlurRoundRectGM : public skiagm::GM {
115 public:
SimpleBlurRoundRectGM()116     SimpleBlurRoundRectGM()
117         : fName("simpleblurroundrect") {
118     }
119 
120 protected:
121 
onShortName()122     SkString onShortName() override {
123         return fName;
124     }
125 
onISize()126     SkISize onISize() override {
127         return SkISize::Make(1000, 500);
128     }
129 
onDraw(SkCanvas * canvas)130     void onDraw(SkCanvas* canvas) override {
131         canvas->scale(1.5f, 1.5f);
132         canvas->translate(50,50);
133 
134         const float blurRadii[] = { 1,5,10,20 };
135         const int cornerRadii[] = { 1,5,10,20 };
136         const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25));
137         for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) {
138             SkAutoCanvasRestore autoRestore(canvas, true);
139             canvas->translate(0, (r.height() + SkIntToScalar(50)) * i);
140             for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) {
141                 for (int k = 0; k <= 1; k++) {
142                     SkPaint paint;
143                     paint.setColor(SK_ColorBLACK);
144                     paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
145                                    SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i]))));
146 
147                     bool useRadial = SkToBool(k);
148                     if (useRadial) {
149                         paint.setShader(MakeRadial());
150                     }
151 
152                     SkRRect rrect;
153                     rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]),
154                                     SkIntToScalar(cornerRadii[j]));
155                     canvas->drawRRect(rrect, paint);
156                     canvas->translate(r.width() + SkIntToScalar(50), 0);
157                 }
158             }
159         }
160     }
161 private:
162     const SkString  fName;
163 
164     typedef         skiagm::GM INHERITED;
165 };
166 
167 // Create one with dimensions/rounded corners based on the skp
168 //
169 // TODO(scroggo): Disabled in an attempt to rememdy
170 // https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs:
171 // ran wrong number of tests')
172 //DEF_GM(return new BlurRoundRectGM(600, 5514, 6);)
173 
174 // Rounded rect with two opposite corners with large radii, the other two
175 // small.
176 DEF_GM(return new BlurRoundRectGM(100, 100);)
177 
178 DEF_GM(return new SimpleBlurRoundRectGM();)
179