blob: 208fb4184abad89e886f1688b414cf49185d1ece [file] [log] [blame]
Robert Phillips6e1fca42016-12-01 12:42:43 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkImageFilter.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkPoint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkRRect.h"
Ben Wagnerd1701ba2019-04-30 13:44:26 -040014#include "include/core/SkRect.h"
15#include "include/core/SkScalar.h"
16#include "include/core/SkSize.h"
17#include "include/core/SkString.h"
18#include "include/core/SkTypes.h"
Michael Ludwig898bbfa2019-08-02 15:21:23 -040019#include "include/effects/SkImageFilters.h"
Mike Klein8aa0edf2020-10-16 11:04:18 -050020#include "include/private/SkTPin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/utils/SkRandom.h"
Hal Canary41248072019-07-11 16:32:53 -040022#include "tools/timer/TimeUtils.h"
Robert Phillips6e1fca42016-12-01 12:42:43 -050023
24static const SkScalar kBlurMax = 7.0f;
25static const int kNumNodes = 30;
26static const int kWidth = 512;
27static const int kHeight = 512;
28static const SkScalar kBlurAnimationDuration = 4.0f; // in secs
29
30// This GM draws a lot of layers with animating BlurImageFilters
31class AnimatedImageBlurs : public skiagm::GM {
32public:
33 AnimatedImageBlurs() : fLastTime(0.0f) {
Mike Kleind46dce32018-08-16 10:17:03 -040034 this->setBGColor(0xFFCCCCCC);
Robert Phillips6e1fca42016-12-01 12:42:43 -050035 }
36
37protected:
38 bool runAsBench() const override { return true; }
39
40 SkString onShortName() override { return SkString("animated-image-blurs"); }
41
42 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
43
44 void onOnceBeforeDraw() override {
45 for (int i = 0; i < kNumNodes; ++i) {
46 fNodes[i].init(&fRand);
47 }
48 }
49
50 void onDraw(SkCanvas* canvas) override {
51 SkPaint paint;
52 paint.setAntiAlias(true);
53
54 for (int i = 0; i < kNumNodes; ++i) {
55 SkPaint layerPaint;
Michael Ludwig898bbfa2019-08-02 15:21:23 -040056 layerPaint.setImageFilter(SkImageFilters::Blur(fNodes[i].sigma(), fNodes[i].sigma(),
57 nullptr));
Robert Phillips6e1fca42016-12-01 12:42:43 -050058
59 canvas->saveLayer(nullptr, &layerPaint);
60 // The rect is outset to block the circle case
61 SkRect rect = SkRect::MakeLTRB(fNodes[i].pos().fX - fNodes[i].size()-0.5f,
62 fNodes[i].pos().fY - fNodes[i].size()-0.5f,
63 fNodes[i].pos().fX + fNodes[i].size()+0.5f,
64 fNodes[i].pos().fY + fNodes[i].size()+0.5f);
65 SkRRect rrect = SkRRect::MakeRectXY(rect, fNodes[i].size(), fNodes[i].size());
66 canvas->drawRRect(rrect, paint);
67 canvas->restore();
68 }
69 }
70
Hal Canary41248072019-07-11 16:32:53 -040071 bool onAnimate(double nanos) override {
Robert Phillips6e1fca42016-12-01 12:42:43 -050072 if (0.0f != fLastTime) {
73 for (int i = 0; i < kNumNodes; ++i) {
Hal Canary41248072019-07-11 16:32:53 -040074 fNodes[i].update(nanos, fLastTime);
Robert Phillips6e1fca42016-12-01 12:42:43 -050075 }
76 }
77
Hal Canary41248072019-07-11 16:32:53 -040078 fLastTime = 1e-9 * nanos;
Robert Phillips6e1fca42016-12-01 12:42:43 -050079 return true;
80 }
81
82private:
83 class Node {
84 public:
85 Node()
86 : fSize(0.0f)
87 , fPos { 0.0f, 0.0f }
88 , fDir { 1.0f, 0.0f }
89 , fBlurOffset(0.0f)
90 , fBlur(fBlurOffset)
91 , fSpeed(0.0f) {
92 }
93
94 void init(SkRandom* rand) {
95 fSize = rand->nextRangeF(10.0f, 60.f);
96 fPos.fX = rand->nextRangeF(fSize, kWidth - fSize);
97 fPos.fY = rand->nextRangeF(fSize, kHeight - fSize);
98 fDir.fX = rand->nextRangeF(-1.0f, 1.0f);
99 fDir.fY = SkScalarSqrt(1.0f - fDir.fX * fDir.fX);
100 if (rand->nextBool()) {
101 fDir.fY = -fDir.fY;
102 }
103 fBlurOffset = rand->nextRangeF(0.0f, kBlurMax);
104 fBlur = fBlurOffset;
105 fSpeed = rand->nextRangeF(20.0f, 60.0f);
106 }
107
Hal Canary41248072019-07-11 16:32:53 -0400108 void update(double nanos, SkScalar lastTime) {
109 SkScalar deltaTime = 1e-9 * nanos - lastTime;
Robert Phillips6e1fca42016-12-01 12:42:43 -0500110
111 fPos.fX += deltaTime * fSpeed * fDir.fX;
112 fPos.fY += deltaTime * fSpeed * fDir.fY;
113 if (fPos.fX >= kWidth || fPos.fX < 0.0f) {
114 fPos.fX = SkTPin<SkScalar>(fPos.fX, 0.0f, kWidth);
115 fDir.fX = -fDir.fX;
116 }
117 if (fPos.fY >= kHeight || fPos.fY < 0.0f) {
118 fPos.fY = SkTPin<SkScalar>(fPos.fY, 0.0f, kHeight);
119 fDir.fY = -fDir.fY;
120 }
121
Hal Canary41248072019-07-11 16:32:53 -0400122 fBlur = TimeUtils::PingPong(1e-9 * nanos, kBlurAnimationDuration, fBlurOffset, 0.0f, kBlurMax);
Robert Phillips6e1fca42016-12-01 12:42:43 -0500123 }
124
125 SkScalar sigma() const { return fBlur; }
126 const SkPoint& pos() const { return fPos; }
127 SkScalar size() const { return fSize; }
128
129 private:
130 SkScalar fSize;
131 SkPoint fPos;
132 SkVector fDir;
133 SkScalar fBlurOffset;
134 SkScalar fBlur;
135 SkScalar fSpeed;
136 };
137
138 Node fNodes[kNumNodes];
139 SkRandom fRand;
140 SkScalar fLastTime;
141
John Stiles7571f9e2020-09-02 22:42:33 -0400142 using INHERITED = GM;
Robert Phillips6e1fca42016-12-01 12:42:43 -0500143};
144
145//////////////////////////////////////////////////////////////////////////////
146
147DEF_GM(return new AnimatedImageBlurs;)