blob: e7b66d52656a9b20567fe471ca6e1318fdc7cb04 [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() {
caryclarkbe7f7682015-06-15 05:09:59 -070015 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000016 }
reed@google.comc96e5c22011-02-16 21:50:04 +000017
18protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000019
mtklein36352bf2015-03-25 18:17:31 -070020 SkString onShortName() override {
reed@google.comc96e5c22011-02-16 21:50:04 +000021 return SkString("blurs");
22 }
23
mtklein36352bf2015-03-25 18:17:31 -070024 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070025 return SkISize::Make(700, 500);
reed@google.comc96e5c22011-02-16 21:50:04 +000026 }
27
mtklein36352bf2015-03-25 18:17:31 -070028 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000029 SkBlurStyle NONE = SkBlurStyle(-999);
reed@google.comc96e5c22011-02-16 21:50:04 +000030 static const struct {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000031 SkBlurStyle fStyle;
32 int fCx, fCy;
reed@google.comc96e5c22011-02-16 21:50:04 +000033 } gRecs[] = {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000034 { NONE, 0, 0 },
35 { kInner_SkBlurStyle, -1, 0 },
36 { kNormal_SkBlurStyle, 0, 1 },
37 { kSolid_SkBlurStyle, 0, -1 },
38 { kOuter_SkBlurStyle, 1, 0 },
reed@google.comc96e5c22011-02-16 21:50:04 +000039 };
40
41 SkPaint paint;
42 paint.setAntiAlias(true);
caryclarkceadfef2015-06-12 05:42:58 -070043 sk_tool_utils::set_portable_typeface_always(&paint);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000044 paint.setTextSize(SkIntToScalar(25));
45 canvas->translate(SkIntToScalar(-40), SkIntToScalar(0));
reed@google.comc96e5c22011-02-16 21:50:04 +000046
47 SkBlurMaskFilter::BlurFlags flags = SkBlurMaskFilter::kNone_BlurFlag;
48 for (int j = 0; j < 2; j++) {
49 canvas->save();
50 paint.setColor(SK_ColorBLUE);
51 for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
52 if (gRecs[i].fStyle != NONE) {
robertphillips@google.comb7061172013-09-06 14:16:12 +000053 SkMaskFilter* mf = SkBlurMaskFilter::Create(gRecs[i].fStyle,
54 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)),
55 flags);
reed@google.comc96e5c22011-02-16 21:50:04 +000056 paint.setMaskFilter(mf)->unref();
57 } else {
58 paint.setMaskFilter(NULL);
59 }
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000060 canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100),
61 SkIntToScalar(200 + gRecs[i].fCy*100),
62 SkIntToScalar(50),
63 paint);
reed@google.comc96e5c22011-02-16 21:50:04 +000064 }
65 // draw text
66 {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000067 SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000068 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)),
69 flags);
reed@google.comc96e5c22011-02-16 21:50:04 +000070 paint.setMaskFilter(mf)->unref();
71 SkScalar x = SkIntToScalar(70);
72 SkScalar y = SkIntToScalar(400);
73 paint.setColor(SK_ColorBLACK);
74 canvas->drawText("Hamburgefons Style", 18, x, y, paint);
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000075 canvas->drawText("Hamburgefons Style", 18,
76 x, y + SkIntToScalar(50), paint);
reed@google.comc96e5c22011-02-16 21:50:04 +000077 paint.setMaskFilter(NULL);
78 paint.setColor(SK_ColorWHITE);
79 x -= SkIntToScalar(2);
80 y -= SkIntToScalar(2);
81 canvas->drawText("Hamburgefons Style", 18, x, y, paint);
82 }
83 canvas->restore();
reed@google.com11a5ff32011-02-18 20:20:51 +000084 flags = SkBlurMaskFilter::kHighQuality_BlurFlag;
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000085 canvas->translate(SkIntToScalar(350), SkIntToScalar(0));
reed@google.comc96e5c22011-02-16 21:50:04 +000086 }
87 }
88
89private:
reed36e6e262014-11-14 08:27:40 -080090 typedef skiagm::GM INHERITED;
reed@google.comc96e5c22011-02-16 21:50:04 +000091};
reed36e6e262014-11-14 08:27:40 -080092DEF_GM( return new BlursGM; )
reed@google.comc96e5c22011-02-16 21:50:04 +000093
reed36e6e262014-11-14 08:27:40 -080094//////////////////////////////////////////////////////////////////////////////////////////////
reed@google.comc96e5c22011-02-16 21:50:04 +000095
reed36e6e262014-11-14 08:27:40 -080096// exercise a special-case of blurs, which is two nested rects. These are drawn specially,
97// and possibly cached.
98//
99// in particular, we want to notice that the 2nd rect draws slightly differently, since it
100// is translated a fractional amount.
101//
102class Blur2RectsGM : public skiagm::GM {
103public:
mtklein36352bf2015-03-25 18:17:31 -0700104 SkString onShortName() override {
reed36e6e262014-11-14 08:27:40 -0800105 return SkString("blur2rects");
106 }
reed@google.comc96e5c22011-02-16 21:50:04 +0000107
mtklein36352bf2015-03-25 18:17:31 -0700108 SkISize onISize() override {
reed36e6e262014-11-14 08:27:40 -0800109 return SkISize::Make(700, 500);
110 }
111
mtklein36352bf2015-03-25 18:17:31 -0700112 void onDraw(SkCanvas* canvas) override {
reed36e6e262014-11-14 08:27:40 -0800113 SkPaint paint;
114
115 paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
116 2.3f))->unref();
117
reed468b6b42014-11-17 07:41:35 -0800118 SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100);
119 SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80);
reed36e6e262014-11-14 08:27:40 -0800120 SkPath path;
121 path.addRect(outer, SkPath::kCW_Direction);
122 path.addRect(inner, SkPath::kCCW_Direction);
123
124 canvas->drawPath(path, paint);
reedae760d52014-11-14 11:59:10 -0800125 // important to translate by a factional amount to exercise a different "phase"
reed36e6e262014-11-14 08:27:40 -0800126 // of the same path w.r.t. the pixel grid
reed468b6b42014-11-17 07:41:35 -0800127 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f;
reedae760d52014-11-14 11:59:10 -0800128 canvas->translate(dx, 0);
reed36e6e262014-11-14 08:27:40 -0800129 canvas->drawPath(path, paint);
130 }
131};
132DEF_GM( return new Blur2RectsGM; )
qiankun.miao28828d02014-12-10 07:40:36 -0800133
134class Blur2RectsNonNinePatchGM : public skiagm::GM {
135public:
mtklein36352bf2015-03-25 18:17:31 -0700136 SkString onShortName() override {
qiankun.miao28828d02014-12-10 07:40:36 -0800137 return SkString("blur2rectsnonninepatch");
138 }
139
mtklein36352bf2015-03-25 18:17:31 -0700140 SkISize onISize() override {
qiankun.miao28828d02014-12-10 07:40:36 -0800141 return SkISize::Make(700, 500);
142 }
143
mtklein36352bf2015-03-25 18:17:31 -0700144 void onDraw(SkCanvas* canvas) override {
qiankun.miao28828d02014-12-10 07:40:36 -0800145 SkPaint paint;
146 paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle,
147 4.3f))->unref();
148
149 SkRect outer = SkRect::MakeXYWH(10, 110, 100, 100);
150 SkRect inner = SkRect::MakeXYWH(50, 150, 10, 10);
151 SkPath path;
152 path.addRect(outer, SkPath::kCW_Direction);
153 path.addRect(inner, SkPath::kCW_Direction);
154 canvas->drawPath(path, paint);
155
156 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 40 + 0.25f;
157 canvas->translate(dx, 0);
158 canvas->drawPath(path, paint);
159
160 // Translate to outside of clip bounds.
161 canvas->translate(-dx, 0);
162 canvas->translate(-30, -150);
163 canvas->drawPath(path, paint);
164 }
165};
166DEF_GM( return new Blur2RectsNonNinePatchGM; )