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