blob: 16e6249d344a98ac7b876f6fcb3e8b69a7b9ac47 [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"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.h"
reed@google.comc96e5c22011-02-16 21:50:04 +000012
halcanary2a243382015-09-09 08:16:41 -070013DEF_SIMPLE_GM_BG(blurs, canvas, 700, 500, sk_tool_utils::color_to_565(0xFFDDDDDD)) {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000014 SkBlurStyle NONE = SkBlurStyle(-999);
mtkleindbfd7ab2016-09-01 11:24:54 -070015 const struct {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000016 SkBlurStyle fStyle;
17 int fCx, fCy;
reed@google.comc96e5c22011-02-16 21:50:04 +000018 } gRecs[] = {
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000019 { NONE, 0, 0 },
20 { kInner_SkBlurStyle, -1, 0 },
21 { kNormal_SkBlurStyle, 0, 1 },
22 { kSolid_SkBlurStyle, 0, -1 },
23 { kOuter_SkBlurStyle, 1, 0 },
reed@google.comc96e5c22011-02-16 21:50:04 +000024 };
25
26 SkPaint paint;
27 paint.setAntiAlias(true);
caryclark1818acb2015-07-24 12:09:25 -070028 sk_tool_utils::set_portable_typeface(&paint);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000029 paint.setTextSize(SkIntToScalar(25));
30 canvas->translate(SkIntToScalar(-40), SkIntToScalar(0));
reed@google.comc96e5c22011-02-16 21:50:04 +000031
32 SkBlurMaskFilter::BlurFlags flags = SkBlurMaskFilter::kNone_BlurFlag;
33 for (int j = 0; j < 2; j++) {
34 canvas->save();
35 paint.setColor(SK_ColorBLUE);
36 for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) {
37 if (gRecs[i].fStyle != NONE) {
reedefdfd512016-04-04 10:02:58 -070038 paint.setMaskFilter(SkBlurMaskFilter::Make(gRecs[i].fStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000039 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)),
reedefdfd512016-04-04 10:02:58 -070040 flags));
reed@google.comc96e5c22011-02-16 21:50:04 +000041 } else {
halcanary96fcdcc2015-08-27 07:41:13 -070042 paint.setMaskFilter(nullptr);
reed@google.comc96e5c22011-02-16 21:50:04 +000043 }
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000044 canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100),
45 SkIntToScalar(200 + gRecs[i].fCy*100),
46 SkIntToScalar(50),
47 paint);
reed@google.comc96e5c22011-02-16 21:50:04 +000048 }
49 // draw text
50 {
reedefdfd512016-04-04 10:02:58 -070051 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
robertphillips@google.comb7061172013-09-06 14:16:12 +000052 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)),
reedefdfd512016-04-04 10:02:58 -070053 flags));
reed@google.comc96e5c22011-02-16 21:50:04 +000054 SkScalar x = SkIntToScalar(70);
55 SkScalar y = SkIntToScalar(400);
56 paint.setColor(SK_ColorBLACK);
57 canvas->drawText("Hamburgefons Style", 18, x, y, paint);
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000058 canvas->drawText("Hamburgefons Style", 18,
59 x, y + SkIntToScalar(50), paint);
halcanary96fcdcc2015-08-27 07:41:13 -070060 paint.setMaskFilter(nullptr);
reed@google.comc96e5c22011-02-16 21:50:04 +000061 paint.setColor(SK_ColorWHITE);
62 x -= SkIntToScalar(2);
63 y -= SkIntToScalar(2);
64 canvas->drawText("Hamburgefons Style", 18, x, y, paint);
65 }
66 canvas->restore();
reed@google.com11a5ff32011-02-18 20:20:51 +000067 flags = SkBlurMaskFilter::kHighQuality_BlurFlag;
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000068 canvas->translate(SkIntToScalar(350), SkIntToScalar(0));
reed@google.comc96e5c22011-02-16 21:50:04 +000069 }
halcanary2a243382015-09-09 08:16:41 -070070}
reed@google.comc96e5c22011-02-16 21:50:04 +000071
reed36e6e262014-11-14 08:27:40 -080072//////////////////////////////////////////////////////////////////////////////////////////////
reed@google.comc96e5c22011-02-16 21:50:04 +000073
reed36e6e262014-11-14 08:27:40 -080074// exercise a special-case of blurs, which is two nested rects. These are drawn specially,
75// and possibly cached.
76//
77// in particular, we want to notice that the 2nd rect draws slightly differently, since it
78// is translated a fractional amount.
79//
halcanary2a243382015-09-09 08:16:41 -070080DEF_SIMPLE_GM(blur2rects, canvas, 700, 500) {
reed36e6e262014-11-14 08:27:40 -080081 SkPaint paint;
82
reedefdfd512016-04-04 10:02:58 -070083 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, 2.3f));
reed36e6e262014-11-14 08:27:40 -080084
reed468b6b42014-11-17 07:41:35 -080085 SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100);
86 SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80);
reed36e6e262014-11-14 08:27:40 -080087 SkPath path;
88 path.addRect(outer, SkPath::kCW_Direction);
89 path.addRect(inner, SkPath::kCCW_Direction);
90
91 canvas->drawPath(path, paint);
reedae760d52014-11-14 11:59:10 -080092 // important to translate by a factional amount to exercise a different "phase"
reed36e6e262014-11-14 08:27:40 -080093 // of the same path w.r.t. the pixel grid
reed468b6b42014-11-17 07:41:35 -080094 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f;
reedae760d52014-11-14 11:59:10 -080095 canvas->translate(dx, 0);
reed36e6e262014-11-14 08:27:40 -080096 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -070097}
qiankun.miao28828d02014-12-10 07:40:36 -080098
halcanary2a243382015-09-09 08:16:41 -070099DEF_SIMPLE_GM(blur2rectsnonninepatch, canvas, 700, 500) {
qiankun.miao28828d02014-12-10 07:40:36 -0800100 SkPaint paint;
reedefdfd512016-04-04 10:02:58 -0700101 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, 4.3f));
qiankun.miao28828d02014-12-10 07:40:36 -0800102
103 SkRect outer = SkRect::MakeXYWH(10, 110, 100, 100);
104 SkRect inner = SkRect::MakeXYWH(50, 150, 10, 10);
105 SkPath path;
106 path.addRect(outer, SkPath::kCW_Direction);
107 path.addRect(inner, SkPath::kCW_Direction);
108 canvas->drawPath(path, paint);
109
110 SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 40 + 0.25f;
111 canvas->translate(dx, 0);
112 canvas->drawPath(path, paint);
113
114 // Translate to outside of clip bounds.
115 canvas->translate(-dx, 0);
116 canvas->translate(-30, -150);
117 canvas->drawPath(path, paint);
halcanary2a243382015-09-09 08:16:41 -0700118}