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