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