Greg Daniel | 2cb7a1a | 2017-05-30 11:15:07 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "gm.h" |
| 9 | #include "sk_tool_utils.h" |
| 10 | |
| 11 | #include "SkBlurMask.h" |
Greg Daniel | 2cb7a1a | 2017-05-30 11:15:07 -0400 | [diff] [blame] | 12 | #include "SkCanvas.h" |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 13 | #include "SkMaskFilter.h" |
Greg Daniel | 2cb7a1a | 2017-05-30 11:15:07 -0400 | [diff] [blame] | 14 | #include "SkPaint.h" |
| 15 | |
| 16 | // This GM tests out the SkBlurMaskFilter's kIgnoreTransform flag. That flag causes the blur mask |
| 17 | // filter to not apply the CTM to the blur's radius. |
| 18 | class BlurIgnoreXformGM : public skiagm::GM { |
| 19 | public: |
| 20 | enum class DrawType { |
| 21 | kCircle, |
| 22 | kRect, |
| 23 | kRRect, |
| 24 | }; |
| 25 | |
| 26 | BlurIgnoreXformGM(DrawType drawType) : fDrawType(drawType) { } |
| 27 | |
| 28 | protected: |
| 29 | bool runAsBench() const override { return true; } |
| 30 | |
| 31 | SkString onShortName() override { |
| 32 | SkString name; |
| 33 | name.printf("blur_ignore_xform_%s", |
| 34 | DrawType::kCircle == fDrawType ? "circle" |
| 35 | : DrawType::kRect == fDrawType ? "rect" : "rrect"); |
| 36 | return name; |
| 37 | } |
| 38 | |
| 39 | SkISize onISize() override { |
| 40 | return SkISize::Make(375, 475); |
| 41 | } |
| 42 | |
| 43 | void onOnceBeforeDraw() override { |
| 44 | for (int i = 0; i < kNumBlurs; ++i) { |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 45 | fBlurFilters[i] = SkMaskFilter::MakeBlur( |
Greg Daniel | 2cb7a1a | 2017-05-30 11:15:07 -0400 | [diff] [blame] | 46 | kNormal_SkBlurStyle, |
| 47 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)), |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 48 | kBlurFlags[i].fRespectCTM); |
Greg Daniel | 2cb7a1a | 2017-05-30 11:15:07 -0400 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | void onDraw(SkCanvas* canvas) override { |
| 53 | SkPaint paint; |
| 54 | paint.setColor(SK_ColorBLACK); |
| 55 | paint.setAntiAlias(true); |
| 56 | |
| 57 | canvas->translate(10, 25); |
| 58 | canvas->save(); |
| 59 | canvas->translate(80, 0); |
| 60 | for (size_t i = 0; i < kNumBlurs; ++i) { |
| 61 | SkAutoCanvasRestore autoRestore(canvas, true); |
| 62 | canvas->translate(SkIntToScalar(i * 150), 0); |
| 63 | for (auto scale : kMatrixScales) { |
| 64 | canvas->save(); |
| 65 | canvas->scale(scale.fScale, scale.fScale); |
| 66 | static const SkScalar kRadius = 20.0f; |
| 67 | SkScalar coord = 50.0f * 1.0f / scale.fScale; |
| 68 | SkRect rect = SkRect::MakeXYWH(coord - kRadius , coord - kRadius, |
| 69 | 2 * kRadius, 2 * kRadius); |
| 70 | SkRRect rrect = SkRRect::MakeRectXY(rect, kRadius/2.0f, kRadius/2.0f); |
| 71 | |
| 72 | paint.setMaskFilter(fBlurFilters[i]); |
| 73 | for (int j = 0; j < 2; ++j) { |
| 74 | canvas->save(); |
| 75 | canvas->translate(10 * (1 - j), 10 * (1 - j)); |
| 76 | if (DrawType::kCircle == fDrawType) { |
| 77 | canvas->drawCircle(coord, coord, kRadius, paint); |
| 78 | } else if (DrawType::kRect == fDrawType) { |
| 79 | canvas->drawRect(rect, paint); |
| 80 | } else { |
| 81 | canvas->drawRRect(rrect, paint); |
| 82 | } |
| 83 | paint.setMaskFilter(nullptr); |
| 84 | canvas->restore(); |
| 85 | } |
| 86 | canvas->restore(); |
| 87 | canvas->translate(0, SkIntToScalar(150)); |
| 88 | } |
| 89 | } |
| 90 | canvas->restore(); |
| 91 | if (kBench_Mode != this->getMode()) { |
| 92 | this->drawOverlay(canvas); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void drawOverlay(SkCanvas* canvas) { |
| 97 | canvas->translate(10, 0); |
| 98 | SkPaint textPaint; |
| 99 | sk_tool_utils::set_portable_typeface(&textPaint); |
| 100 | textPaint.setAntiAlias(true); |
| 101 | canvas->save(); |
| 102 | for (int i = 0; i < kNumBlurs; ++i) { |
| 103 | canvas->drawString(kBlurFlags[i].fName, 100, 0, textPaint); |
| 104 | canvas->translate(SkIntToScalar(130), 0); |
| 105 | } |
| 106 | canvas->restore(); |
| 107 | for (auto scale : kMatrixScales) { |
| 108 | canvas->drawString(scale.fName, 0, 50, textPaint); |
| 109 | canvas->translate(0, SkIntToScalar(150)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | private: |
| 114 | static constexpr int kNumBlurs = 2; |
| 115 | |
| 116 | static const struct BlurFlags { |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 117 | bool fRespectCTM; |
Greg Daniel | 2cb7a1a | 2017-05-30 11:15:07 -0400 | [diff] [blame] | 118 | const char* fName; |
| 119 | } kBlurFlags[kNumBlurs]; |
| 120 | |
| 121 | static const struct MatrixScale { |
| 122 | float fScale; |
| 123 | const char* fName; |
| 124 | } kMatrixScales[3]; |
| 125 | |
| 126 | DrawType fDrawType; |
| 127 | sk_sp<SkMaskFilter> fBlurFilters[kNumBlurs]; |
| 128 | |
| 129 | typedef skiagm::GM INHERITED; |
| 130 | }; |
| 131 | |
| 132 | const BlurIgnoreXformGM::BlurFlags BlurIgnoreXformGM::kBlurFlags[] = { |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 133 | {true, "none"}, |
| 134 | {false, "IgnoreTransform"} |
Greg Daniel | 2cb7a1a | 2017-05-30 11:15:07 -0400 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | const BlurIgnoreXformGM::MatrixScale BlurIgnoreXformGM::kMatrixScales[] = { |
| 138 | {1.0f, "Identity"}, |
| 139 | {0.5f, "Scale = 0.5"}, |
| 140 | {2.0f, "Scale = 2.0"} |
| 141 | }; |
| 142 | |
| 143 | DEF_GM(return new BlurIgnoreXformGM(BlurIgnoreXformGM::DrawType::kCircle);) |
| 144 | DEF_GM(return new BlurIgnoreXformGM(BlurIgnoreXformGM::DrawType::kRect);) |
| 145 | DEF_GM(return new BlurIgnoreXformGM(BlurIgnoreXformGM::DrawType::kRRect);) |
| 146 | |
| 147 | |
| 148 | |