epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 7 | |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 8 | #include "gm.h" |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 9 | #include "SkBlurMask.h" |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 10 | #include "SkBlurMaskFilter.h" |
| 11 | |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 12 | class BlursGM : public skiagm::GM { |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 13 | public: |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 14 | BlursGM() { |
caryclark | be7f768 | 2015-06-15 05:09:59 -0700 | [diff] [blame^] | 15 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 16 | } |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 17 | |
| 18 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 19 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 20 | SkString onShortName() override { |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 21 | return SkString("blurs"); |
| 22 | } |
| 23 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 24 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 25 | return SkISize::Make(700, 500); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 26 | } |
| 27 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 28 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 29 | SkBlurStyle NONE = SkBlurStyle(-999); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 30 | static const struct { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 31 | SkBlurStyle fStyle; |
| 32 | int fCx, fCy; |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 33 | } gRecs[] = { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 34 | { 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.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | SkPaint paint; |
| 42 | paint.setAntiAlias(true); |
caryclark | ceadfef | 2015-06-12 05:42:58 -0700 | [diff] [blame] | 43 | sk_tool_utils::set_portable_typeface_always(&paint); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 44 | paint.setTextSize(SkIntToScalar(25)); |
| 45 | canvas->translate(SkIntToScalar(-40), SkIntToScalar(0)); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 46 | |
| 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.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 53 | SkMaskFilter* mf = SkBlurMaskFilter::Create(gRecs[i].fStyle, |
| 54 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)), |
| 55 | flags); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 56 | paint.setMaskFilter(mf)->unref(); |
| 57 | } else { |
| 58 | paint.setMaskFilter(NULL); |
| 59 | } |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 60 | canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100), |
| 61 | SkIntToScalar(200 + gRecs[i].fCy*100), |
| 62 | SkIntToScalar(50), |
| 63 | paint); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 64 | } |
| 65 | // draw text |
| 66 | { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 67 | SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 68 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)), |
| 69 | flags); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 70 | 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.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 75 | canvas->drawText("Hamburgefons Style", 18, |
| 76 | x, y + SkIntToScalar(50), paint); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 77 | 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.com | 11a5ff3 | 2011-02-18 20:20:51 +0000 | [diff] [blame] | 84 | flags = SkBlurMaskFilter::kHighQuality_BlurFlag; |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 85 | canvas->translate(SkIntToScalar(350), SkIntToScalar(0)); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | private: |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 90 | typedef skiagm::GM INHERITED; |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 91 | }; |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 92 | DEF_GM( return new BlursGM; ) |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 93 | |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 94 | ////////////////////////////////////////////////////////////////////////////////////////////// |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 95 | |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 96 | // 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 | // |
| 102 | class Blur2RectsGM : public skiagm::GM { |
| 103 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 104 | SkString onShortName() override { |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 105 | return SkString("blur2rects"); |
| 106 | } |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 107 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 108 | SkISize onISize() override { |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 109 | return SkISize::Make(700, 500); |
| 110 | } |
| 111 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 112 | void onDraw(SkCanvas* canvas) override { |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 113 | SkPaint paint; |
| 114 | |
| 115 | paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle, |
| 116 | 2.3f))->unref(); |
| 117 | |
reed | 468b6b4 | 2014-11-17 07:41:35 -0800 | [diff] [blame] | 118 | SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100); |
| 119 | SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 120 | SkPath path; |
| 121 | path.addRect(outer, SkPath::kCW_Direction); |
| 122 | path.addRect(inner, SkPath::kCCW_Direction); |
| 123 | |
| 124 | canvas->drawPath(path, paint); |
reed | ae760d5 | 2014-11-14 11:59:10 -0800 | [diff] [blame] | 125 | // important to translate by a factional amount to exercise a different "phase" |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 126 | // of the same path w.r.t. the pixel grid |
reed | 468b6b4 | 2014-11-17 07:41:35 -0800 | [diff] [blame] | 127 | SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f; |
reed | ae760d5 | 2014-11-14 11:59:10 -0800 | [diff] [blame] | 128 | canvas->translate(dx, 0); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 129 | canvas->drawPath(path, paint); |
| 130 | } |
| 131 | }; |
| 132 | DEF_GM( return new Blur2RectsGM; ) |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 133 | |
| 134 | class Blur2RectsNonNinePatchGM : public skiagm::GM { |
| 135 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 136 | SkString onShortName() override { |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 137 | return SkString("blur2rectsnonninepatch"); |
| 138 | } |
| 139 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 140 | SkISize onISize() override { |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 141 | return SkISize::Make(700, 500); |
| 142 | } |
| 143 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 144 | void onDraw(SkCanvas* canvas) override { |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 145 | 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 | }; |
| 166 | DEF_GM( return new Blur2RectsNonNinePatchGM; ) |