bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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/SkBlurTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkMaskFilter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkPaint.h" |
Mike Reed | d16d654 | 2020-08-22 15:08:27 -0400 | [diff] [blame] | 14 | #include "include/core/SkPathBuilder.h" |
Ben Wagner | 6a34f3a | 2019-05-01 10:59:30 -0400 | [diff] [blame] | 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkSize.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/utils/SkRandom.h" |
| 21 | #include "src/core/SkBlurMask.h" |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 22 | #include "tools/timer/TimeUtils.h" |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * In GM mode this draws an array of circles with different radii and different blur radii. Below |
| 26 | * each circle an almost-circle path is drawn with the same blur filter for comparison. |
| 27 | * |
| 28 | * In Sample mode this draws a single circle and almost-circle with animating radius and blur |
| 29 | * radius. |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 30 | * |
| 31 | * Bench mode draws the same as GM mode but without the comparison almost-circle paths. It also |
| 32 | * slightly perturbs the blur and circle radii to stress caching of blurred profiles in GPU mode. |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 33 | */ |
| 34 | class BlurCircles2GM : public skiagm::GM { |
| 35 | public: |
| 36 | BlurCircles2GM() { |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 37 | fAnimRadius = TimeUtils::PingPong( |
Mike Klein | cd5104e | 2019-03-20 11:55:08 -0500 | [diff] [blame] | 38 | 0, kRadiusPingPoingPeriod, kRadiusPingPoingShift, kMinRadius, kMaxRadius); |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 39 | fAnimBlurRadius = TimeUtils::PingPong(0, |
Mike Klein | cd5104e | 2019-03-20 11:55:08 -0500 | [diff] [blame] | 40 | kBlurRadiusPingPoingPeriod, |
| 41 | kBlurRadiusPingPoingShift, |
| 42 | kMinBlurRadius, |
| 43 | kMaxBlurRadius); |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | protected: |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 47 | bool runAsBench() const override { return true; } |
| 48 | |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 49 | SkString onShortName() override { return SkString("blurcircles2"); } |
| 50 | |
| 51 | SkISize onISize() override { |
| 52 | return SkISize::Make(730, 1350); |
| 53 | } |
| 54 | |
| 55 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 56 | constexpr SkScalar kMaxR = kMaxRadius + kMaxBlurRadius; |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 57 | |
Mike Reed | d16d654 | 2020-08-22 15:08:27 -0400 | [diff] [blame] | 58 | auto almostCircleMaker = [] (SkScalar radius) { |
| 59 | return SkPathBuilder().addArc(SkRect::MakeXYWH(-radius, -radius, 2 * radius, 2 * radius), 0, 355) |
| 60 | .setIsVolatile(true) |
| 61 | .close() |
| 62 | .detach(); |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | auto blurMaker = [] (SkScalar radius) ->sk_sp<SkMaskFilter> { |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 66 | return SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
| 67 | SkBlurMask::ConvertRadiusToSigma(radius)); |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | SkPaint paint; |
| 71 | paint.setColor(SK_ColorBLACK); |
| 72 | |
| 73 | if (this->getMode() == kSample_Mode) { |
| 74 | paint.setMaskFilter(blurMaker(fAnimBlurRadius)); |
| 75 | SkISize size = canvas->getBaseLayerSize(); |
Mike Reed | d16d654 | 2020-08-22 15:08:27 -0400 | [diff] [blame] | 76 | SkPath almostCircle = almostCircleMaker(fAnimRadius); |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 77 | canvas->save(); |
| 78 | canvas->translate(size.fWidth / 2.f, size.fHeight / 4.f); |
| 79 | canvas->drawCircle(0, 0, fAnimRadius, paint); |
| 80 | canvas->translate(0, 2 * kMaxR); |
| 81 | canvas->drawPath(almostCircle, paint); |
| 82 | canvas->restore(); |
| 83 | } else { |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 84 | bool benchMode = this->getMode() == kBench_Mode; |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 85 | canvas->save(); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 86 | constexpr SkScalar kPad = 5; |
| 87 | constexpr SkScalar kRadiusSteps = 5; |
| 88 | constexpr SkScalar kBlurRadiusSteps = 5; |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 89 | canvas->translate(kPad + kMinRadius + kMaxBlurRadius, |
| 90 | kPad + kMinRadius + kMaxBlurRadius); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 91 | constexpr SkScalar kDeltaRadius = (kMaxRadius - kMinRadius) / kRadiusSteps; |
| 92 | constexpr SkScalar kDeltaBlurRadius = (kMaxBlurRadius - kMinBlurRadius) / |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 93 | kBlurRadiusSteps; |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 94 | SkScalar lineWidth = 0; |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 95 | if (!benchMode) { |
| 96 | for (int r = 0; r < kRadiusSteps - 1; ++r) { |
| 97 | const SkScalar radius = r * kDeltaRadius + kMinRadius; |
| 98 | lineWidth += 2 * (radius + kMaxBlurRadius) + kPad; |
| 99 | } |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 100 | } |
| 101 | for (int br = 0; br < kBlurRadiusSteps; ++br) { |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 102 | SkScalar blurRadius = br * kDeltaBlurRadius + kMinBlurRadius; |
| 103 | if (benchMode) { |
| 104 | blurRadius += fRandom.nextSScalar1() * kDeltaBlurRadius; |
| 105 | } |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 106 | const SkScalar maxRowR = blurRadius + kMaxRadius; |
| 107 | paint.setMaskFilter(blurMaker(blurRadius)); |
| 108 | canvas->save(); |
| 109 | for (int r = 0; r < kRadiusSteps; ++r) { |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 110 | SkScalar radius = r * kDeltaRadius + kMinRadius; |
| 111 | if (benchMode) { |
| 112 | radius += fRandom.nextSScalar1() * kDeltaRadius; |
| 113 | } |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 114 | SkPath almostCircle; |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 115 | if (!benchMode) { |
Mike Reed | d16d654 | 2020-08-22 15:08:27 -0400 | [diff] [blame] | 116 | almostCircle = almostCircleMaker(radius); |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 117 | } |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 118 | canvas->save(); |
| 119 | canvas->drawCircle(0, 0, radius, paint); |
| 120 | canvas->translate(0, 2 * maxRowR + kPad); |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 121 | if (!benchMode) { |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 122 | canvas->drawPath(almostCircle, paint); |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 123 | } |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 124 | canvas->restore(); |
| 125 | const SkScalar maxColR = radius + kMaxBlurRadius; |
| 126 | canvas->translate(maxColR * 2 + kPad, 0); |
| 127 | } |
| 128 | canvas->restore(); |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 129 | if (!benchMode) { |
| 130 | SkPaint blackPaint; |
| 131 | blackPaint.setColor(SK_ColorBLACK); |
| 132 | const SkScalar lineY = 3 * maxRowR + 1.5f * kPad; |
| 133 | if (br != kBlurRadiusSteps - 1) { |
| 134 | canvas->drawLine(0, lineY, lineWidth, lineY, blackPaint); |
| 135 | } |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 136 | } |
| 137 | canvas->translate(0, maxRowR * 4 + 2 * kPad); |
| 138 | } |
| 139 | canvas->restore(); |
| 140 | } |
| 141 | } |
| 142 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 143 | bool onAnimate(double nanos) override { |
| 144 | fAnimRadius = TimeUtils::PingPong(1e-9 * nanos, kRadiusPingPoingPeriod, kRadiusPingPoingShift, kMinRadius, |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 145 | kMaxRadius); |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 146 | fAnimBlurRadius = TimeUtils::PingPong(1e-9 * nanos, kBlurRadiusPingPoingPeriod, kBlurRadiusPingPoingShift, |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 147 | kMinBlurRadius, kMaxBlurRadius); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | private: |
| 152 | static constexpr SkScalar kMinRadius = 15; |
| 153 | static constexpr SkScalar kMaxRadius = 45; |
| 154 | static constexpr SkScalar kRadiusPingPoingPeriod = 8; |
| 155 | static constexpr SkScalar kRadiusPingPoingShift = 3; |
| 156 | |
| 157 | static constexpr SkScalar kMinBlurRadius = 5; |
| 158 | static constexpr SkScalar kMaxBlurRadius = 45; |
| 159 | static constexpr SkScalar kBlurRadiusPingPoingPeriod = 3; |
| 160 | static constexpr SkScalar kBlurRadiusPingPoingShift = 1.5; |
| 161 | |
| 162 | SkScalar fAnimRadius; |
| 163 | SkScalar fAnimBlurRadius; |
| 164 | |
bsalomon | 2996553 | 2016-05-16 09:36:44 -0700 | [diff] [blame] | 165 | SkRandom fRandom; |
| 166 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 167 | using INHERITED = skiagm::GM; |
bsalomon | c67bb57 | 2016-05-13 13:48:48 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | DEF_GM(return new BlurCircles2GM();) |