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