blob: dfa95ce39741bb30a586515ac09ae5e60a5a24c6 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
reed36e6e262014-11-14 08:27:40 -08007
reed@google.comc96e5c22011-02-16 21:50:04 +00008#include "gm.h"
robertphillips@google.comb7061172013-09-06 14:16:12 +00009#include "SkBlurMask.h"
reed@google.comc96e5c22011-02-16 21:50:04 +000010#include "SkBlurMaskFilter.h"
11
reed36e6e262014-11-14 08:27:40 -080012class BlursGM : public skiagm::GM {
reed@google.comc96e5c22011-02-16 21:50:04 +000013public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000014 BlursGM() {
15 this->setBGColor(0xFFDDDDDD);
16 }
reed@google.comc96e5c22011-02-16 21:50:04 +000017
18protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000019 virtual uint32_t onGetFlags() const SK_OVERRIDE {
20 return kSkipTiled_Flag;
21 }
22
tfarinaaa458fb2015-01-05 17:18:51 -080023 virtual SkString onShortName() SK_OVERRIDE {
reed@google.comc96e5c22011-02-16 21:50:04 +000024 return SkString("blurs");
25 }
26
tfarinaaa458fb2015-01-05 17:18:51 -080027 virtual SkISize onISize() SK_OVERRIDE {
tfarinaf5393182014-06-09 23:59:03 -070028 return SkISize::Make(700, 500);
reed@google.comc96e5c22011-02-16 21:50:04 +000029 }
30
tfarinaaa458fb2015-01-05 17:18:51 -080031 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000032 SkBlurStyle NONE = SkBlurStyle(-999);
reed@google.comc96e5c22011-02-16 21:50:04 +000033 static const struct {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000034 SkBlurStyle fStyle;
35 int fCx, fCy;
reed@google.comc96e5c22011-02-16 21:50:04 +000036 } gRecs[] = {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000037 { NONE, 0, 0 },
38 { kInner_SkBlurStyle, -1, 0 },
39 { kNormal_SkBlurStyle, 0, 1 },
40 { kSolid_SkBlurStyle, 0, -1 },
41 { kOuter_SkBlurStyle, 1, 0 },
reed@google.comc96e5c22011-02-16 21:50:04 +000042 };
43
44 SkPaint paint;
45 paint.setAntiAlias(true);
caryclark5fb6bd42014-06-23 11:25:00 -070046 sk_tool_utils::set_portable_typeface(&paint);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000047 paint.setTextSize(SkIntToScalar(25));
48 canvas->translate(SkIntToScalar(-40), SkIntToScalar(0));
reed@google.comc96e5c22011-02-16 21:50:04 +000049
50 SkBlurMaskFilter::BlurFlags flags = SkBlurMaskFilter::kNone_BlurFlag;
51 for (int j = 0; j < 2; j++) {
52 canvas->save();
53 paint.setColor(SK_ColorBLUE);
54 for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
55 if (gRecs[i].fStyle != NONE) {
robertphillips@google.comb7061172013-09-06 14:16:12 +000056 SkMaskFilter* mf = SkBlurMaskFilter::Create(gRecs[i].fStyle,
57 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)),
58 flags);
reed@google.comc96e5c22011-02-16 21:50:04 +000059 paint.setMaskFilter(mf)->unref();
60 } else {
61 paint.setMaskFilter(NULL);
62 }
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000063 canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100),
64 SkIntToScalar(200 + gRecs[i].fCy*100),
65 SkIntToScalar(50),
66 paint);
reed@google.comc96e5c22011-02-16 21:50:04 +000067 }
68 // draw text
69 {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000070 SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000071 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)),
72 flags);
reed@google.comc96e5c22011-02-16 21:50:04 +000073 paint.setMaskFilter(mf)->unref();
74 SkScalar x = SkIntToScalar(70);
75 SkScalar y = SkIntToScalar(400);
76 paint.setColor(SK_ColorBLACK);
77 canvas->drawText("Hamburgefons Style", 18, x, y, paint);
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000078 canvas->drawText("Hamburgefons Style", 18,
79 x, y + SkIntToScalar(50), paint);
reed@google.comc96e5c22011-02-16 21:50:04 +000080 paint.setMaskFilter(NULL);
81 paint.setColor(SK_ColorWHITE);
82 x -= SkIntToScalar(2);
83 y -= SkIntToScalar(2);
84 canvas->drawText("Hamburgefons Style", 18, x, y, paint);
85 }
86 canvas->restore();
reed@google.com11a5ff32011-02-18 20:20:51 +000087 flags = SkBlurMaskFilter::kHighQuality_BlurFlag;
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000088 canvas->translate(SkIntToScalar(350), SkIntToScalar(0));
reed@google.comc96e5c22011-02-16 21:50:04 +000089 }
90 }
91
92private:
reed36e6e262014-11-14 08:27:40 -080093 typedef skiagm::GM INHERITED;
reed@google.comc96e5c22011-02-16 21:50:04 +000094};
reed36e6e262014-11-14 08:27:40 -080095DEF_GM( return new BlursGM; )
reed@google.comc96e5c22011-02-16 21:50:04 +000096
reed36e6e262014-11-14 08:27:40 -080097//////////////////////////////////////////////////////////////////////////////////////////////
reed@google.comc96e5c22011-02-16 21:50:04 +000098
reed36e6e262014-11-14 08:27:40 -080099// exercise a special-case of blurs, which is two nested rects. These are drawn specially,
100// and possibly cached.
101//
102// in particular, we want to notice that the 2nd rect draws slightly differently, since it
103// is translated a fractional amount.
104//
105class Blur2RectsGM : public skiagm::GM {
106public:
107 SkString onShortName() SK_OVERRIDE {
108 return SkString("blur2rects");
109 }
reed@google.comc96e5c22011-02-16 21:50:04 +0000110
reed36e6e262014-11-14 08:27:40 -0800111 SkISize onISize() SK_OVERRIDE {
112 return SkISize::Make(700, 500);
113 }
114
115 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
116 SkPaint paint;
117
118 paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
119 2.3f))->unref();
120
reed468b6b42014-11-17 07:41:35 -0800121 SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100);
122 SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80);
reed36e6e262014-11-14 08:27:40 -0800123 SkPath path;
124 path.addRect(outer, SkPath::kCW_Direction);
125 path.addRect(inner, SkPath::kCCW_Direction);
126
127 canvas->drawPath(path, paint);
reedae760d52014-11-14 11:59:10 -0800128 // important to translate by a factional amount to exercise a different "phase"
reed36e6e262014-11-14 08:27:40 -0800129 // of the same path w.r.t. the pixel grid
reed468b6b42014-11-17 07:41:35 -0800130 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f;
reedae760d52014-11-14 11:59:10 -0800131 canvas->translate(dx, 0);
reed36e6e262014-11-14 08:27:40 -0800132 canvas->drawPath(path, paint);
133 }
134};
135DEF_GM( return new Blur2RectsGM; )
qiankun.miao28828d02014-12-10 07:40:36 -0800136
137class Blur2RectsNonNinePatchGM : public skiagm::GM {
138public:
139 SkString onShortName() SK_OVERRIDE {
140 return SkString("blur2rectsnonninepatch");
141 }
142
143 SkISize onISize() SK_OVERRIDE {
144 return SkISize::Make(700, 500);
145 }
146
147 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
148 SkPaint paint;
149 paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
150 4.3f))->unref();
151
152 SkRect outer = SkRect::MakeXYWH(10, 110, 100, 100);
153 SkRect inner = SkRect::MakeXYWH(50, 150, 10, 10);
154 SkPath path;
155 path.addRect(outer, SkPath::kCW_Direction);
156 path.addRect(inner, SkPath::kCW_Direction);
157 canvas->drawPath(path, paint);
158
159 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 40 + 0.25f;
160 canvas->translate(dx, 0);
161 canvas->drawPath(path, paint);
162
163 // Translate to outside of clip bounds.
164 canvas->translate(-dx, 0);
165 canvas->translate(-30, -150);
166 canvas->drawPath(path, paint);
167 }
168};
169DEF_GM( return new Blur2RectsNonNinePatchGM; )