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" |
| 14 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 15 | #include "include/core/SkMatrix.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "include/core/SkPaint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkPoint.h" |
| 18 | #include "include/core/SkRRect.h" |
| 19 | #include "include/core/SkRect.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 20 | #include "include/core/SkRefCnt.h" |
| 21 | #include "include/core/SkScalar.h" |
| 22 | #include "include/core/SkShader.h" |
| 23 | #include "include/core/SkSize.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "include/core/SkString.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 25 | #include "include/core/SkTileMode.h" |
| 26 | #include "include/core/SkTypes.h" |
| 27 | #include "include/effects/SkGradientShader.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 28 | #include "src/core/SkBlurMask.h" |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 29 | |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 30 | /* |
Kevin Lubick | be03ef1 | 2021-06-16 15:28:00 -0400 | [diff] [blame] | 31 | * Spits out an arbitrary gradient to test blur with shader on paint |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 32 | */ |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 33 | static sk_sp<SkShader> MakeRadial() { |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 34 | SkPoint pts[2] = { |
| 35 | { 0, 0 }, |
| 36 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 37 | }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 38 | SkTileMode tm = SkTileMode::kClamp; |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 39 | const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, }; |
| 40 | const SkScalar pos[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 41 | SkMatrix scale; |
| 42 | scale.setScale(0.5f, 0.5f); |
| 43 | scale.postTranslate(5.f, 5.f); |
| 44 | SkPoint center0, center1; |
| 45 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 46 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 47 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 48 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 49 | return SkGradientShader::MakeTwoPointConical(center1, (pts[1].fX - pts[0].fX) / 7, |
| 50 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 51 | colors, pos, SK_ARRAY_COUNT(colors), tm, |
| 52 | 0, &scale); |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 53 | } |
| 54 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 55 | // Simpler blurred RR test cases where all the radii are the same. |
| 56 | class SimpleBlurRoundRectGM : public skiagm::GM { |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 57 | SkString onShortName() override { return SkString("simpleblurroundrect"); } |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 58 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 59 | SkISize onISize() override { return {1000, 500}; } |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 60 | |
John Stiles | a2d46a1 | 2020-06-11 14:12:44 -0400 | [diff] [blame] | 61 | bool runAsBench() const override { return true; } |
| 62 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 63 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 64 | canvas->scale(1.5f, 1.5f); |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 65 | canvas->translate(50,50); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 66 | |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 67 | const float blurRadii[] = { 1,5,10,20 }; |
| 68 | const int cornerRadii[] = { 1,5,10,20 }; |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 69 | const SkRect r = SkRect::MakeWH(SkIntToScalar(25), SkIntToScalar(25)); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 70 | for (size_t i = 0; i < SK_ARRAY_COUNT(blurRadii); ++i) { |
| 71 | SkAutoCanvasRestore autoRestore(canvas, true); |
commit-bot@chromium.org | 3d8bf23 | 2014-04-28 19:49:24 +0000 | [diff] [blame] | 72 | canvas->translate(0, (r.height() + SkIntToScalar(50)) * i); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 73 | for (size_t j = 0; j < SK_ARRAY_COUNT(cornerRadii); ++j) { |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 74 | for (int k = 0; k <= 1; k++) { |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 75 | SkPaint paint; |
| 76 | paint.setColor(SK_ColorBLACK); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 77 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
| 78 | SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(blurRadii[i])))); |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 79 | |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 80 | bool useRadial = SkToBool(k); |
| 81 | if (useRadial) { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 82 | paint.setShader(MakeRadial()); |
joshualitt | 341400e | 2014-12-18 11:54:13 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | SkRRect rrect; |
| 86 | rrect.setRectXY(r, SkIntToScalar(cornerRadii[j]), |
| 87 | SkIntToScalar(cornerRadii[j])); |
| 88 | canvas->drawRRect(rrect, paint); |
| 89 | canvas->translate(r.width() + SkIntToScalar(50), 0); |
| 90 | } |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 93 | } |
scroggo@google.com | 7b05659 | 2013-11-05 15:57:21 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | // Create one with dimensions/rounded corners based on the skp |
epoger@google.com | 7e6e80a | 2013-11-05 22:04:53 +0000 | [diff] [blame] | 97 | // |
| 98 | // TODO(scroggo): Disabled in an attempt to rememdy |
| 99 | // https://code.google.com/p/skia/issues/detail?id=1801 ('Win7 Test bots all failing GenerateGMs: |
| 100 | // ran wrong number of tests') |
| 101 | //DEF_GM(return new BlurRoundRectGM(600, 5514, 6);) |
| 102 | |
scroggo@google.com | 8610d2c | 2013-11-05 21:10:58 +0000 | [diff] [blame] | 103 | DEF_GM(return new SimpleBlurRoundRectGM();) |
Brian Salomon | 5e961b0 | 2020-10-21 14:28:47 -0400 | [diff] [blame] | 104 | |
| 105 | // From crbug.com/1138810 |
| 106 | DEF_SIMPLE_GM(blur_large_rrects, canvas, 300, 300) { |
| 107 | SkPaint paint; |
| 108 | paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 20.f)); |
| 109 | |
| 110 | auto rect = SkRect::MakeLTRB(5.f, -20000.f, 240.f, 25.f); |
| 111 | SkRRect rrect = SkRRect::MakeRectXY(rect, 40.f, 40.f); |
| 112 | for (int i = 0; i < 4; ++i) { |
| 113 | SkColor4f color{(i & 1) ? 1.f : 0.f, |
| 114 | (i & 2) ? 1.f : 0.f, |
| 115 | (i < 2) ? 1.f : 0.f, |
| 116 | 1.f}; |
| 117 | paint.setColor(color); |
| 118 | canvas->drawRRect(rrect, paint); |
| 119 | canvas->rotate(90.f, 150.f, 150.f); |
| 120 | } |
| 121 | } |