blob: ad0c37dc4b994b60e2c6f105864f236add5dedcc [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"
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.
bsalomon29965532016-05-16 09:36:44 -070023 *
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.
bsalomonc67bb572016-05-13 13:48:48 -070026 */
27class BlurCircles2GM : public skiagm::GM {
28public:
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
37protected:
bsalomon29965532016-05-16 09:36:44 -070038 bool runAsBench() const override { return true; }
39
bsalomonc67bb572016-05-13 13:48:48 -070040 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 {
47 static constexpr SkScalar kMaxR = kMaxRadius + kMaxBlurRadius;
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 {
bsalomon29965532016-05-16 09:36:44 -070077 bool benchMode = this->getMode() == kBench_Mode;
bsalomonc67bb572016-05-13 13:48:48 -070078 canvas->save();
79 static constexpr SkScalar kPad = 5;
80 static constexpr SkScalar kRadiusSteps = 5;
81 static constexpr SkScalar kBlurRadiusSteps = 5;
82 canvas->translate(kPad + kMinRadius + kMaxBlurRadius,
83 kPad + kMinRadius + kMaxBlurRadius);
bsalomon29965532016-05-16 09:36:44 -070084 static constexpr SkScalar kDeltaRadius = (kMaxRadius - kMinRadius) / kRadiusSteps;
85 static constexpr SkScalar kDeltaBlurRadius = (kMaxBlurRadius - kMinBlurRadius) /
86 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();)