2016-05-13 20:48:48 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkBlurTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkMaskFilter.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkPaint.h"
|
2020-08-22 19:08:27 +00:00
|
|
|
#include "include/core/SkPathBuilder.h"
|
2019-05-01 14:59:30 +00:00
|
|
|
#include "include/core/SkRect.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "src/core/SkBlurMask.h"
|
2019-07-11 20:32:53 +00:00
|
|
|
#include "tools/timer/TimeUtils.h"
|
2016-05-13 20:48:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* In GM mode this draws an array of circles with different radii and different blur radii. Below
|
|
|
|
* each circle an almost-circle path is drawn with the same blur filter for comparison.
|
|
|
|
*
|
|
|
|
* In Sample mode this draws a single circle and almost-circle with animating radius and blur
|
|
|
|
* radius.
|
2016-05-16 16:36:44 +00:00
|
|
|
*
|
|
|
|
* Bench mode draws the same as GM mode but without the comparison almost-circle paths. It also
|
|
|
|
* slightly perturbs the blur and circle radii to stress caching of blurred profiles in GPU mode.
|
2016-05-13 20:48:48 +00:00
|
|
|
*/
|
|
|
|
class BlurCircles2GM : public skiagm::GM {
|
|
|
|
public:
|
|
|
|
BlurCircles2GM() {
|
2019-07-11 20:32:53 +00:00
|
|
|
fAnimRadius = TimeUtils::PingPong(
|
2019-03-20 16:55:08 +00:00
|
|
|
0, kRadiusPingPoingPeriod, kRadiusPingPoingShift, kMinRadius, kMaxRadius);
|
2019-07-11 20:32:53 +00:00
|
|
|
fAnimBlurRadius = TimeUtils::PingPong(0,
|
2019-03-20 16:55:08 +00:00
|
|
|
kBlurRadiusPingPoingPeriod,
|
|
|
|
kBlurRadiusPingPoingShift,
|
|
|
|
kMinBlurRadius,
|
|
|
|
kMaxBlurRadius);
|
2016-05-13 20:48:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-05-16 16:36:44 +00:00
|
|
|
bool runAsBench() const override { return true; }
|
|
|
|
|
2016-05-13 20:48:48 +00:00
|
|
|
SkString onShortName() override { return SkString("blurcircles2"); }
|
|
|
|
|
|
|
|
SkISize onISize() override {
|
|
|
|
return SkISize::Make(730, 1350);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kMaxR = kMaxRadius + kMaxBlurRadius;
|
2016-05-13 20:48:48 +00:00
|
|
|
|
2020-08-22 19:08:27 +00:00
|
|
|
auto almostCircleMaker = [] (SkScalar radius) {
|
|
|
|
return SkPathBuilder().addArc(SkRect::MakeXYWH(-radius, -radius, 2 * radius, 2 * radius), 0, 355)
|
|
|
|
.setIsVolatile(true)
|
|
|
|
.close()
|
|
|
|
.detach();
|
2016-05-13 20:48:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto blurMaker = [] (SkScalar radius) ->sk_sp<SkMaskFilter> {
|
2018-03-14 17:01:17 +00:00
|
|
|
return SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
|
|
|
|
SkBlurMask::ConvertRadiusToSigma(radius));
|
2016-05-13 20:48:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
|
|
|
|
if (this->getMode() == kSample_Mode) {
|
|
|
|
paint.setMaskFilter(blurMaker(fAnimBlurRadius));
|
|
|
|
SkISize size = canvas->getBaseLayerSize();
|
2020-08-22 19:08:27 +00:00
|
|
|
SkPath almostCircle = almostCircleMaker(fAnimRadius);
|
2016-05-13 20:48:48 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(size.fWidth / 2.f, size.fHeight / 4.f);
|
|
|
|
canvas->drawCircle(0, 0, fAnimRadius, paint);
|
|
|
|
canvas->translate(0, 2 * kMaxR);
|
|
|
|
canvas->drawPath(almostCircle, paint);
|
|
|
|
canvas->restore();
|
|
|
|
} else {
|
2016-05-16 16:36:44 +00:00
|
|
|
bool benchMode = this->getMode() == kBench_Mode;
|
2016-05-13 20:48:48 +00:00
|
|
|
canvas->save();
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kPad = 5;
|
|
|
|
constexpr SkScalar kRadiusSteps = 5;
|
|
|
|
constexpr SkScalar kBlurRadiusSteps = 5;
|
2016-05-13 20:48:48 +00:00
|
|
|
canvas->translate(kPad + kMinRadius + kMaxBlurRadius,
|
|
|
|
kPad + kMinRadius + kMaxBlurRadius);
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkScalar kDeltaRadius = (kMaxRadius - kMinRadius) / kRadiusSteps;
|
|
|
|
constexpr SkScalar kDeltaBlurRadius = (kMaxBlurRadius - kMinBlurRadius) /
|
2016-05-16 16:36:44 +00:00
|
|
|
kBlurRadiusSteps;
|
2016-05-13 20:48:48 +00:00
|
|
|
SkScalar lineWidth = 0;
|
2016-05-16 16:36:44 +00:00
|
|
|
if (!benchMode) {
|
|
|
|
for (int r = 0; r < kRadiusSteps - 1; ++r) {
|
|
|
|
const SkScalar radius = r * kDeltaRadius + kMinRadius;
|
|
|
|
lineWidth += 2 * (radius + kMaxBlurRadius) + kPad;
|
|
|
|
}
|
2016-05-13 20:48:48 +00:00
|
|
|
}
|
|
|
|
for (int br = 0; br < kBlurRadiusSteps; ++br) {
|
2016-05-16 16:36:44 +00:00
|
|
|
SkScalar blurRadius = br * kDeltaBlurRadius + kMinBlurRadius;
|
|
|
|
if (benchMode) {
|
|
|
|
blurRadius += fRandom.nextSScalar1() * kDeltaBlurRadius;
|
|
|
|
}
|
2016-05-13 20:48:48 +00:00
|
|
|
const SkScalar maxRowR = blurRadius + kMaxRadius;
|
|
|
|
paint.setMaskFilter(blurMaker(blurRadius));
|
|
|
|
canvas->save();
|
|
|
|
for (int r = 0; r < kRadiusSteps; ++r) {
|
2016-05-16 16:36:44 +00:00
|
|
|
SkScalar radius = r * kDeltaRadius + kMinRadius;
|
|
|
|
if (benchMode) {
|
|
|
|
radius += fRandom.nextSScalar1() * kDeltaRadius;
|
|
|
|
}
|
2016-05-13 20:48:48 +00:00
|
|
|
SkPath almostCircle;
|
2016-05-16 16:36:44 +00:00
|
|
|
if (!benchMode) {
|
2020-08-22 19:08:27 +00:00
|
|
|
almostCircle = almostCircleMaker(radius);
|
2016-05-16 16:36:44 +00:00
|
|
|
}
|
2016-05-13 20:48:48 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->drawCircle(0, 0, radius, paint);
|
|
|
|
canvas->translate(0, 2 * maxRowR + kPad);
|
2016-05-16 16:36:44 +00:00
|
|
|
if (!benchMode) {
|
2016-05-13 20:48:48 +00:00
|
|
|
canvas->drawPath(almostCircle, paint);
|
2016-05-16 16:36:44 +00:00
|
|
|
}
|
2016-05-13 20:48:48 +00:00
|
|
|
canvas->restore();
|
|
|
|
const SkScalar maxColR = radius + kMaxBlurRadius;
|
|
|
|
canvas->translate(maxColR * 2 + kPad, 0);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
2016-05-16 16:36:44 +00:00
|
|
|
if (!benchMode) {
|
|
|
|
SkPaint blackPaint;
|
|
|
|
blackPaint.setColor(SK_ColorBLACK);
|
|
|
|
const SkScalar lineY = 3 * maxRowR + 1.5f * kPad;
|
|
|
|
if (br != kBlurRadiusSteps - 1) {
|
|
|
|
canvas->drawLine(0, lineY, lineWidth, lineY, blackPaint);
|
|
|
|
}
|
2016-05-13 20:48:48 +00:00
|
|
|
}
|
|
|
|
canvas->translate(0, maxRowR * 4 + 2 * kPad);
|
|
|
|
}
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 20:32:53 +00:00
|
|
|
bool onAnimate(double nanos) override {
|
|
|
|
fAnimRadius = TimeUtils::PingPong(1e-9 * nanos, kRadiusPingPoingPeriod, kRadiusPingPoingShift, kMinRadius,
|
2016-05-13 20:48:48 +00:00
|
|
|
kMaxRadius);
|
2019-07-11 20:32:53 +00:00
|
|
|
fAnimBlurRadius = TimeUtils::PingPong(1e-9 * nanos, kBlurRadiusPingPoingPeriod, kBlurRadiusPingPoingShift,
|
2016-05-13 20:48:48 +00:00
|
|
|
kMinBlurRadius, kMaxBlurRadius);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr SkScalar kMinRadius = 15;
|
|
|
|
inline static constexpr SkScalar kMaxRadius = 45;
|
|
|
|
inline static constexpr SkScalar kRadiusPingPoingPeriod = 8;
|
|
|
|
inline static constexpr SkScalar kRadiusPingPoingShift = 3;
|
|
|
|
|
|
|
|
inline static constexpr SkScalar kMinBlurRadius = 5;
|
|
|
|
inline static constexpr SkScalar kMaxBlurRadius = 45;
|
|
|
|
inline static constexpr SkScalar kBlurRadiusPingPoingPeriod = 3;
|
|
|
|
inline static constexpr SkScalar kBlurRadiusPingPoingShift = 1.5;
|
2016-05-13 20:48:48 +00:00
|
|
|
|
|
|
|
SkScalar fAnimRadius;
|
|
|
|
SkScalar fAnimBlurRadius;
|
|
|
|
|
2016-05-16 16:36:44 +00:00
|
|
|
SkRandom fRandom;
|
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = skiagm::GM;
|
2016-05-13 20:48:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_GM(return new BlurCircles2GM();)
|