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() { |
| 15 | this->setBGColor(0xFFDDDDDD); |
| 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 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 20 | return kSkipTiled_Flag; |
| 21 | } |
| 22 | |
tfarina | aa458fb | 2015-01-05 17:18:51 -0800 | [diff] [blame^] | 23 | virtual SkString onShortName() SK_OVERRIDE { |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 24 | return SkString("blurs"); |
| 25 | } |
| 26 | |
tfarina | aa458fb | 2015-01-05 17:18:51 -0800 | [diff] [blame^] | 27 | virtual SkISize onISize() SK_OVERRIDE { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 28 | return SkISize::Make(700, 500); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 29 | } |
| 30 | |
tfarina | aa458fb | 2015-01-05 17:18:51 -0800 | [diff] [blame^] | 31 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 32 | SkBlurStyle NONE = SkBlurStyle(-999); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 33 | static const struct { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 34 | SkBlurStyle fStyle; |
| 35 | int fCx, fCy; |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 36 | } gRecs[] = { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 37 | { NONE, 0, 0 }, |
| 38 | { kInner_SkBlurStyle, -1, 0 }, |
| 39 | { kNormal_SkBlurStyle, 0, 1 }, |
| 40 | { kSolid_SkBlurStyle, 0, -1 }, |
| 41 | { kOuter_SkBlurStyle, 1, 0 }, |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | SkPaint paint; |
| 45 | paint.setAntiAlias(true); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 46 | sk_tool_utils::set_portable_typeface(&paint); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 47 | paint.setTextSize(SkIntToScalar(25)); |
| 48 | canvas->translate(SkIntToScalar(-40), SkIntToScalar(0)); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 49 | |
| 50 | SkBlurMaskFilter::BlurFlags flags = SkBlurMaskFilter::kNone_BlurFlag; |
| 51 | for (int j = 0; j < 2; j++) { |
| 52 | canvas->save(); |
| 53 | paint.setColor(SK_ColorBLUE); |
| 54 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRecs); i++) { |
| 55 | if (gRecs[i].fStyle != NONE) { |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 56 | SkMaskFilter* mf = SkBlurMaskFilter::Create(gRecs[i].fStyle, |
| 57 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(20)), |
| 58 | flags); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 59 | paint.setMaskFilter(mf)->unref(); |
| 60 | } else { |
| 61 | paint.setMaskFilter(NULL); |
| 62 | } |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 63 | canvas->drawCircle(SkIntToScalar(200 + gRecs[i].fCx*100), |
| 64 | SkIntToScalar(200 + gRecs[i].fCy*100), |
| 65 | SkIntToScalar(50), |
| 66 | paint); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 67 | } |
| 68 | // draw text |
| 69 | { |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 70 | SkMaskFilter* mf = SkBlurMaskFilter::Create(kNormal_SkBlurStyle, |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 71 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(4)), |
| 72 | flags); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 73 | paint.setMaskFilter(mf)->unref(); |
| 74 | SkScalar x = SkIntToScalar(70); |
| 75 | SkScalar y = SkIntToScalar(400); |
| 76 | paint.setColor(SK_ColorBLACK); |
| 77 | canvas->drawText("Hamburgefons Style", 18, x, y, paint); |
robertphillips@google.com | 7ce661d | 2013-08-27 16:14:03 +0000 | [diff] [blame] | 78 | canvas->drawText("Hamburgefons Style", 18, |
| 79 | x, y + SkIntToScalar(50), paint); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 80 | paint.setMaskFilter(NULL); |
| 81 | paint.setColor(SK_ColorWHITE); |
| 82 | x -= SkIntToScalar(2); |
| 83 | y -= SkIntToScalar(2); |
| 84 | canvas->drawText("Hamburgefons Style", 18, x, y, paint); |
| 85 | } |
| 86 | canvas->restore(); |
reed@google.com | 11a5ff3 | 2011-02-18 20:20:51 +0000 | [diff] [blame] | 87 | flags = SkBlurMaskFilter::kHighQuality_BlurFlag; |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 88 | canvas->translate(SkIntToScalar(350), SkIntToScalar(0)); |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | private: |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 93 | typedef skiagm::GM INHERITED; |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 94 | }; |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 95 | DEF_GM( return new BlursGM; ) |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 96 | |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 97 | ////////////////////////////////////////////////////////////////////////////////////////////// |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 98 | |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 99 | // exercise a special-case of blurs, which is two nested rects. These are drawn specially, |
| 100 | // and possibly cached. |
| 101 | // |
| 102 | // in particular, we want to notice that the 2nd rect draws slightly differently, since it |
| 103 | // is translated a fractional amount. |
| 104 | // |
| 105 | class Blur2RectsGM : public skiagm::GM { |
| 106 | public: |
| 107 | SkString onShortName() SK_OVERRIDE { |
| 108 | return SkString("blur2rects"); |
| 109 | } |
reed@google.com | c96e5c2 | 2011-02-16 21:50:04 +0000 | [diff] [blame] | 110 | |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 111 | SkISize onISize() SK_OVERRIDE { |
| 112 | return SkISize::Make(700, 500); |
| 113 | } |
| 114 | |
| 115 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 116 | SkPaint paint; |
| 117 | |
| 118 | paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle, |
| 119 | 2.3f))->unref(); |
| 120 | |
reed | 468b6b4 | 2014-11-17 07:41:35 -0800 | [diff] [blame] | 121 | SkRect outer = SkRect::MakeXYWH(10.125f, 10.125f, 100.125f, 100); |
| 122 | SkRect inner = SkRect::MakeXYWH(20.25f, 20.125f, 80, 80); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 123 | SkPath path; |
| 124 | path.addRect(outer, SkPath::kCW_Direction); |
| 125 | path.addRect(inner, SkPath::kCCW_Direction); |
| 126 | |
| 127 | canvas->drawPath(path, paint); |
reed | ae760d5 | 2014-11-14 11:59:10 -0800 | [diff] [blame] | 128 | // important to translate by a factional amount to exercise a different "phase" |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 129 | // of the same path w.r.t. the pixel grid |
reed | 468b6b4 | 2014-11-17 07:41:35 -0800 | [diff] [blame] | 130 | SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 14 + 0.25f; |
reed | ae760d5 | 2014-11-14 11:59:10 -0800 | [diff] [blame] | 131 | canvas->translate(dx, 0); |
reed | 36e6e26 | 2014-11-14 08:27:40 -0800 | [diff] [blame] | 132 | canvas->drawPath(path, paint); |
| 133 | } |
| 134 | }; |
| 135 | DEF_GM( return new Blur2RectsGM; ) |
qiankun.miao | 28828d0 | 2014-12-10 07:40:36 -0800 | [diff] [blame] | 136 | |
| 137 | class Blur2RectsNonNinePatchGM : public skiagm::GM { |
| 138 | public: |
| 139 | SkString onShortName() SK_OVERRIDE { |
| 140 | return SkString("blur2rectsnonninepatch"); |
| 141 | } |
| 142 | |
| 143 | SkISize onISize() SK_OVERRIDE { |
| 144 | return SkISize::Make(700, 500); |
| 145 | } |
| 146 | |
| 147 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 148 | SkPaint paint; |
| 149 | paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle, |
| 150 | 4.3f))->unref(); |
| 151 | |
| 152 | SkRect outer = SkRect::MakeXYWH(10, 110, 100, 100); |
| 153 | SkRect inner = SkRect::MakeXYWH(50, 150, 10, 10); |
| 154 | SkPath path; |
| 155 | path.addRect(outer, SkPath::kCW_Direction); |
| 156 | path.addRect(inner, SkPath::kCW_Direction); |
| 157 | canvas->drawPath(path, paint); |
| 158 | |
| 159 | SkScalar dx = SkScalarRoundToScalar(path.getBounds().width()) + 40 + 0.25f; |
| 160 | canvas->translate(dx, 0); |
| 161 | canvas->drawPath(path, paint); |
| 162 | |
| 163 | // Translate to outside of clip bounds. |
| 164 | canvas->translate(-dx, 0); |
| 165 | canvas->translate(-30, -150); |
| 166 | canvas->drawPath(path, paint); |
| 167 | } |
| 168 | }; |
| 169 | DEF_GM( return new Blur2RectsNonNinePatchGM; ) |