blob: c2739a948ca6d2dda0dc53e52ba95e568db2f743 [file] [log] [blame]
bsalomonc67bb572016-05-13 13:48:48 -07001/*
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"
Mike Klein33d20552017-03-22 13:47:51 -040016#include "SkRandom.h"
bsalomonc67bb572016-05-13 13:48:48 -070017
18/**
19 * In GM mode this draws an array of circles with different radii and different blur radii. Below
20 * each circle an almost-circle path is drawn with the same blur filter for comparison.
21 *
22 * In Sample mode this draws a single circle and almost-circle with animating radius and blur
23 * radius.
bsalomon29965532016-05-16 09:36:44 -070024 *
25 * Bench mode draws the same as GM mode but without the comparison almost-circle paths. It also
26 * slightly perturbs the blur and circle radii to stress caching of blurred profiles in GPU mode.
bsalomonc67bb572016-05-13 13:48:48 -070027 */
28class BlurCircles2GM : public skiagm::GM {
29public:
30 BlurCircles2GM() {
31 fAnimRadius = SkAnimTimer::PingPong(0, kRadiusPingPoingPeriod, kRadiusPingPoingShift,
32 kMinRadius, kMaxRadius);
33 fAnimBlurRadius = SkAnimTimer::PingPong(0, kBlurRadiusPingPoingPeriod,
34 kBlurRadiusPingPoingShift, kMinBlurRadius,
35 kMaxBlurRadius);
36 }
37
38protected:
bsalomon29965532016-05-16 09:36:44 -070039 bool runAsBench() const override { return true; }
40
bsalomonc67bb572016-05-13 13:48:48 -070041 SkString onShortName() override { return SkString("blurcircles2"); }
42
43 SkISize onISize() override {
44 return SkISize::Make(730, 1350);
45 }
46
47 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -070048 constexpr SkScalar kMaxR = kMaxRadius + kMaxBlurRadius;
bsalomonc67bb572016-05-13 13:48:48 -070049
50 auto almostCircleMaker = [] (SkScalar radius, SkPath* dst) {
51 dst->reset();
52 dst->addArc(SkRect::MakeXYWH(-radius, -radius, 2 * radius, 2 * radius), 0, 355);
53 dst->setIsVolatile(true);
54 dst->close();
55 };
56
57 auto blurMaker = [] (SkScalar radius) ->sk_sp<SkMaskFilter> {
Mike Reed1be1f8d2018-03-14 13:01:17 -040058 return SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
59 SkBlurMask::ConvertRadiusToSigma(radius));
bsalomonc67bb572016-05-13 13:48:48 -070060 };
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 {
bsalomon29965532016-05-16 09:36:44 -070077 bool benchMode = this->getMode() == kBench_Mode;
bsalomonc67bb572016-05-13 13:48:48 -070078 canvas->save();
mtkleindbfd7ab2016-09-01 11:24:54 -070079 constexpr SkScalar kPad = 5;
80 constexpr SkScalar kRadiusSteps = 5;
81 constexpr SkScalar kBlurRadiusSteps = 5;
bsalomonc67bb572016-05-13 13:48:48 -070082 canvas->translate(kPad + kMinRadius + kMaxBlurRadius,
83 kPad + kMinRadius + kMaxBlurRadius);
mtkleindbfd7ab2016-09-01 11:24:54 -070084 constexpr SkScalar kDeltaRadius = (kMaxRadius - kMinRadius) / kRadiusSteps;
85 constexpr SkScalar kDeltaBlurRadius = (kMaxBlurRadius - kMinBlurRadius) /
bsalomon29965532016-05-16 09:36:44 -070086 kBlurRadiusSteps;
bsalomonc67bb572016-05-13 13:48:48 -070087 SkScalar lineWidth = 0;
bsalomon29965532016-05-16 09:36:44 -070088 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 }
bsalomonc67bb572016-05-13 13:48:48 -070093 }
94 for (int br = 0; br < kBlurRadiusSteps; ++br) {
bsalomon29965532016-05-16 09:36:44 -070095 SkScalar blurRadius = br * kDeltaBlurRadius + kMinBlurRadius;
96 if (benchMode) {
97 blurRadius += fRandom.nextSScalar1() * kDeltaBlurRadius;
98 }
bsalomonc67bb572016-05-13 13:48:48 -070099 const SkScalar maxRowR = blurRadius + kMaxRadius;
100 paint.setMaskFilter(blurMaker(blurRadius));
101 canvas->save();
102 for (int r = 0; r < kRadiusSteps; ++r) {
bsalomon29965532016-05-16 09:36:44 -0700103 SkScalar radius = r * kDeltaRadius + kMinRadius;
104 if (benchMode) {
105 radius += fRandom.nextSScalar1() * kDeltaRadius;
106 }
bsalomonc67bb572016-05-13 13:48:48 -0700107 SkPath almostCircle;
bsalomon29965532016-05-16 09:36:44 -0700108 if (!benchMode) {
109 almostCircleMaker(radius, &almostCircle);
110 }
bsalomonc67bb572016-05-13 13:48:48 -0700111 canvas->save();
112 canvas->drawCircle(0, 0, radius, paint);
113 canvas->translate(0, 2 * maxRowR + kPad);
bsalomon29965532016-05-16 09:36:44 -0700114 if (!benchMode) {
bsalomonc67bb572016-05-13 13:48:48 -0700115 canvas->drawPath(almostCircle, paint);
bsalomon29965532016-05-16 09:36:44 -0700116 }
bsalomonc67bb572016-05-13 13:48:48 -0700117 canvas->restore();
118 const SkScalar maxColR = radius + kMaxBlurRadius;
119 canvas->translate(maxColR * 2 + kPad, 0);
120 }
121 canvas->restore();
bsalomon29965532016-05-16 09:36:44 -0700122 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 }
bsalomonc67bb572016-05-13 13:48:48 -0700129 }
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
144private:
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
bsalomon29965532016-05-16 09:36:44 -0700158 SkRandom fRandom;
159
bsalomonc67bb572016-05-13 13:48:48 -0700160 typedef skiagm::GM INHERITED;
161};
162
163DEF_GM(return new BlurCircles2GM();)