blob: c8a40c9a062c2f6db426e1797d34136a7520958d [file] [log] [blame]
Greg Daniel2cb7a1a2017-05-30 11:15:07 -04001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -04009#include "include/core/SkBlurTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040011#include "include/core/SkColor.h"
12#include "include/core/SkFont.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkMaskFilter.h"
14#include "include/core/SkPaint.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040015#include "include/core/SkRRect.h"
16#include "include/core/SkRect.h"
17#include "include/core/SkRefCnt.h"
18#include "include/core/SkScalar.h"
19#include "include/core/SkSize.h"
20#include "include/core/SkString.h"
21#include "include/core/SkTypeface.h"
22#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/core/SkBlurMask.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040024#include "tools/ToolUtils.h"
Greg Daniel2cb7a1a2017-05-30 11:15:07 -040025
26// This GM tests out the SkBlurMaskFilter's kIgnoreTransform flag. That flag causes the blur mask
27// filter to not apply the CTM to the blur's radius.
28class BlurIgnoreXformGM : public skiagm::GM {
29public:
30 enum class DrawType {
31 kCircle,
32 kRect,
33 kRRect,
34 };
35
36 BlurIgnoreXformGM(DrawType drawType) : fDrawType(drawType) { }
37
38protected:
39 bool runAsBench() const override { return true; }
40
41 SkString onShortName() override {
42 SkString name;
43 name.printf("blur_ignore_xform_%s",
44 DrawType::kCircle == fDrawType ? "circle"
45 : DrawType::kRect == fDrawType ? "rect" : "rrect");
46 return name;
47 }
48
49 SkISize onISize() override {
50 return SkISize::Make(375, 475);
51 }
52
53 void onOnceBeforeDraw() override {
54 for (int i = 0; i < kNumBlurs; ++i) {
Mike Reed1be1f8d2018-03-14 13:01:17 -040055 fBlurFilters[i] = SkMaskFilter::MakeBlur(
Greg Daniel2cb7a1a2017-05-30 11:15:07 -040056 kNormal_SkBlurStyle,
57 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)),
Mike Reed1be1f8d2018-03-14 13:01:17 -040058 kBlurFlags[i].fRespectCTM);
Greg Daniel2cb7a1a2017-05-30 11:15:07 -040059 }
60 }
61
62 void onDraw(SkCanvas* canvas) override {
63 SkPaint paint;
64 paint.setColor(SK_ColorBLACK);
65 paint.setAntiAlias(true);
66
67 canvas->translate(10, 25);
68 canvas->save();
69 canvas->translate(80, 0);
70 for (size_t i = 0; i < kNumBlurs; ++i) {
71 SkAutoCanvasRestore autoRestore(canvas, true);
72 canvas->translate(SkIntToScalar(i * 150), 0);
73 for (auto scale : kMatrixScales) {
74 canvas->save();
75 canvas->scale(scale.fScale, scale.fScale);
76 static const SkScalar kRadius = 20.0f;
77 SkScalar coord = 50.0f * 1.0f / scale.fScale;
78 SkRect rect = SkRect::MakeXYWH(coord - kRadius , coord - kRadius,
79 2 * kRadius, 2 * kRadius);
80 SkRRect rrect = SkRRect::MakeRectXY(rect, kRadius/2.0f, kRadius/2.0f);
81
82 paint.setMaskFilter(fBlurFilters[i]);
83 for (int j = 0; j < 2; ++j) {
84 canvas->save();
85 canvas->translate(10 * (1 - j), 10 * (1 - j));
86 if (DrawType::kCircle == fDrawType) {
87 canvas->drawCircle(coord, coord, kRadius, paint);
88 } else if (DrawType::kRect == fDrawType) {
89 canvas->drawRect(rect, paint);
90 } else {
91 canvas->drawRRect(rrect, paint);
92 }
93 paint.setMaskFilter(nullptr);
94 canvas->restore();
95 }
96 canvas->restore();
97 canvas->translate(0, SkIntToScalar(150));
98 }
99 }
100 canvas->restore();
101 if (kBench_Mode != this->getMode()) {
102 this->drawOverlay(canvas);
103 }
104 }
105
106 void drawOverlay(SkCanvas* canvas) {
107 canvas->translate(10, 0);
Mike Kleinea3f0142019-03-20 11:12:10 -0500108 SkFont font(ToolUtils::create_portable_typeface());
Greg Daniel2cb7a1a2017-05-30 11:15:07 -0400109 canvas->save();
110 for (int i = 0; i < kNumBlurs; ++i) {
Mike Reedc4745d62019-01-07 09:31:58 -0500111 canvas->drawString(kBlurFlags[i].fName, 100, 0, font, SkPaint());
Greg Daniel2cb7a1a2017-05-30 11:15:07 -0400112 canvas->translate(SkIntToScalar(130), 0);
113 }
114 canvas->restore();
115 for (auto scale : kMatrixScales) {
Mike Reedc4745d62019-01-07 09:31:58 -0500116 canvas->drawString(scale.fName, 0, 50, font, SkPaint());
Greg Daniel2cb7a1a2017-05-30 11:15:07 -0400117 canvas->translate(0, SkIntToScalar(150));
118 }
119 }
120
121private:
122 static constexpr int kNumBlurs = 2;
123
124 static const struct BlurFlags {
Mike Reed1be1f8d2018-03-14 13:01:17 -0400125 bool fRespectCTM;
Greg Daniel2cb7a1a2017-05-30 11:15:07 -0400126 const char* fName;
127 } kBlurFlags[kNumBlurs];
128
129 static const struct MatrixScale {
130 float fScale;
131 const char* fName;
132 } kMatrixScales[3];
133
134 DrawType fDrawType;
135 sk_sp<SkMaskFilter> fBlurFilters[kNumBlurs];
136
John Stiles7571f9e2020-09-02 22:42:33 -0400137 using INHERITED = skiagm::GM;
Greg Daniel2cb7a1a2017-05-30 11:15:07 -0400138};
139
140const BlurIgnoreXformGM::BlurFlags BlurIgnoreXformGM::kBlurFlags[] = {
Mike Reed1be1f8d2018-03-14 13:01:17 -0400141 {true, "none"},
142 {false, "IgnoreTransform"}
Greg Daniel2cb7a1a2017-05-30 11:15:07 -0400143};
144
145const BlurIgnoreXformGM::MatrixScale BlurIgnoreXformGM::kMatrixScales[] = {
146 {1.0f, "Identity"},
147 {0.5f, "Scale = 0.5"},
148 {2.0f, "Scale = 2.0"}
149};
150
151DEF_GM(return new BlurIgnoreXformGM(BlurIgnoreXformGM::DrawType::kCircle);)
152DEF_GM(return new BlurIgnoreXformGM(BlurIgnoreXformGM::DrawType::kRect);)
153DEF_GM(return new BlurIgnoreXformGM(BlurIgnoreXformGM::DrawType::kRRect);)
154
155
156