scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
| 7 | |
| 8 | #include "gm.h" |
| 9 | #include "SkBlurMask.h" |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkColorFilter.h" |
| 12 | #include "SkLayerDrawLooper.h" |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 13 | #include "SkMaskFilter.h" |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 14 | #include "SkPaint.h" |
| 15 | #include "SkPath.h" |
| 16 | #include "SkPoint.h" |
| 17 | #include "SkRect.h" |
| 18 | #include "SkRRect.h" |
| 19 | #include "SkString.h" |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 20 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 21 | // This GM mimics a blurred RR seen in the wild. |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 22 | class BlurRoundRectGM : public skiagm::GM { |
| 23 | public: |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 24 | BlurRoundRectGM(int width, int height) |
herb | b10fe49 | 2016-01-08 13:48:43 -0800 | [diff] [blame] | 25 | : fName("blurroundrect"), fWidth(width), fHeight(height) { |
scroggo | ef470f1 | 2014-06-03 06:50:26 -0700 | [diff] [blame] | 26 | fName.appendf("-WH-%ix%i-unevenCorners", width, height); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 27 | } |
| 28 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | SkString onShortName() override { |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 30 | return fName; |
| 31 | } |
| 32 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | SkISize onISize() override { |
herb | b10fe49 | 2016-01-08 13:48:43 -0800 | [diff] [blame] | 34 | return SkISize::Make(fWidth, fHeight); |
| 35 | } |
| 36 | |
| 37 | void onOnceBeforeDraw() override { |
| 38 | SkVector radii[4]; |
| 39 | radii[0].set(SkIntToScalar(30), SkIntToScalar(30)); |
| 40 | radii[1].set(SkIntToScalar(10), SkIntToScalar(10)); |
| 41 | radii[2].set(SkIntToScalar(30), SkIntToScalar(30)); |
| 42 | radii[3].set(SkIntToScalar(10), SkIntToScalar(10)); |
| 43 | SkRect r = SkRect::MakeWH(SkIntToScalar(fWidth), SkIntToScalar(fHeight)); |
| 44 | fRRect.setRectRadii(r, radii); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 45 | } |
| 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 48 | SkLayerDrawLooper::Builder looperBuilder; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 49 | { |
| 50 | SkLayerDrawLooper::LayerInfo info; |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 51 | info.fPaintBits = SkLayerDrawLooper::kMaskFilter_Bit |
| 52 | | SkLayerDrawLooper::kColorFilter_Bit; |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 53 | info.fColorMode = SkBlendMode::kSrc; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 54 | info.fOffset = SkPoint::Make(SkIntToScalar(-1), SkIntToScalar(0)); |
| 55 | info.fPostTranslate = false; |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 56 | SkPaint* paint = looperBuilder.addLayerOnTop(info); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 57 | paint->setMaskFilter(SkMaskFilter::MakeBlur( |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 58 | kNormal_SkBlurStyle, |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 59 | SkBlurMask::ConvertRadiusToSigma(SK_ScalarHalf))); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 60 | paint->setColorFilter(SkColorFilter::MakeModeFilter( |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 61 | SK_ColorLTGRAY, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 62 | SkBlendMode::kSrcIn)); |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 63 | paint->setColor(SK_ColorGRAY); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 64 | } |
| 65 | { |
| 66 | SkLayerDrawLooper::LayerInfo info; |
commit-bot@chromium.org | 73cb153 | 2014-04-15 15:48:36 +0000 | [diff] [blame] | 67 | looperBuilder.addLayerOnTop(info); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 68 | } |
| 69 | SkPaint paint; |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 70 | canvas->drawRect(fRRect.rect(), paint); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 71 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 72 | paint.setLooper(looperBuilder.detach()); |
scroggo@google.com | 5fdef98 | 2013-11-05 16:28:38 +0000 | [diff] [blame] | 73 | paint.setColor(SK_ColorCYAN); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 74 | paint.setAntiAlias(true); |
| 75 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 76 | canvas->drawRRect(fRRect, paint); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | private: |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 80 | SkString fName; |
| 81 | SkRRect fRRect; |
herb | b10fe49 | 2016-01-08 13:48:43 -0800 | [diff] [blame] | 82 | int fWidth, fHeight; |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 83 | |
| 84 | typedef skiagm::GM INHERITED; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 87 | #include "SkGradientShader.h" |
| 88 | /* |
| 89 | * Spits out a dummy gradient to test blur with shader on paint |
| 90 | */ |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 91 | static sk_sp<SkShader> MakeRadial() { |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 92 | SkPoint pts[2] = { |
| 93 | { 0, 0 }, |
| 94 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 95 | }; |
| 96 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 97 | const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, }; |
| 98 | const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 99 | SkMatrix scale; |
| 100 | scale.setScale(0.5f, 0.5f); |
| 101 | scale.postTranslate(5.f, 5.f); |
| 102 | SkPoint center0, center1; |
| 103 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 104 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 105 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 106 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 107 | return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, |
| 108 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 109 | colors, pos, SK_ARRAY_COUNT(colors), tm, |
| 110 | 0, &scale); |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 111 | } |
| 112 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 113 | // Simpler blurred RR test cases where all the radii are the same. |
| 114 | class SimpleBlurRoundRectGM : public skiagm::GM { |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 115 | public: |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 116 | SimpleBlurRoundRectGM() |
| 117 | : fName("simpleblurroundrect") { |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | protected: |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 121 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 122 | SkString onShortName() override { |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 123 | return fName; |
| 124 | } |
| 125 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 126 | SkISize onISize() override { |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 127 | return SkISize::Make(1000, 500); |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 128 | } |
| 129 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 130 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 131 | canvas->scale(1.5f, 1.5f); |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 132 | canvas->translate(50,50); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 133 | |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 134 | const float blurRadii[] = { 1,5,10,20 }; |
| 135 | const int cornerRadii[] = { 1,5,10,20 }; |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 136 | const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25)); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 137 | for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) { |
| 138 | SkAutoCanvasRestore autoRestore(canvas, true); |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 139 | canvas->translate(0, (r.height() + SkIntToScalar(50)) * i); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 140 | for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) { |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 141 | for (int k = 0; k <= 1; k++) { |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 142 | SkPaint paint; |
| 143 | paint.setColor(SK_ColorBLACK); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 144 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
| 145 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i])))); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 146 | |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 147 | bool useRadial = SkToBool(k); |
| 148 | if (useRadial) { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 149 | paint.setShader(MakeRadial()); |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | SkRRect rrect; |
| 153 | rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]), |
| 154 | SkIntToScalar(cornerRadii[j])); |
| 155 | canvas->drawRRect(rrect, paint); |
| 156 | canvas->translate(r.width() + SkIntToScalar(50), 0); |
| 157 | } |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 160 | } |
| 161 | private: |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 162 | const SkString fName; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 163 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 164 | typedef skiagm::GM INHERITED; |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | // Create one with dimensions/rounded corners based on the skp |
epoger@google.com | 7e6e80a | 2013-11-05 22:04:53 +0000 | [diff] [blame] | 168 | // |
| 169 | // TODO(scroggo): Disabled in an attempt to rememdy |
| 170 | // https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs: |
| 171 | // ran wrong number of tests') |
| 172 | //DEF_GM(return new BlurRoundRectGM(600, 5514, 6);) |
| 173 | |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 174 | // Rounded rect with two opposite corners with large radii, the other two |
| 175 | // small. |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 176 | DEF_GM(return new BlurRoundRectGM(100, 100);) |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 177 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 178 | DEF_GM(return new SimpleBlurRoundRectGM();) |