blob: d8a3abcf05294ea81114afaf16bccf3d55422846 [file] [log] [blame]
sugoi@google.come3b4c502013-04-05 13:47:09 +00001/*
2 * Copyright 2013 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 "SkPerlinNoiseShader.h"
Florin Malita14d54c22017-05-18 11:52:59 -040010#include "SkShader.h"
11
12namespace {
13
14enum class Type {
15 kFractalNoise,
16 kTurbulence,
17};
18
19} // anonymous ns
sugoi@google.come3b4c502013-04-05 13:47:09 +000020
reed@google.coma501a642014-05-12 17:18:45 +000021class PerlinNoiseGM : public skiagm::GM {
sugoi@google.come3b4c502013-04-05 13:47:09 +000022public:
23 PerlinNoiseGM() {
24 this->setBGColor(0xFF000000);
25 fSize = SkISize::Make(80, 80);
26 }
27
28protected:
29 virtual SkString onShortName() {
30 return SkString("perlinnoise");
31 }
32
33 virtual SkISize onISize() {
reed@google.coma501a642014-05-12 17:18:45 +000034 return SkISize::Make(200, 500);
sugoi@google.come3b4c502013-04-05 13:47:09 +000035 }
36
senorblancoce6a3542014-06-12 11:24:19 -070037 void drawRect(SkCanvas* canvas, int x, int y, const SkPaint& paint, const SkISize& size) {
sugoi@google.come3b4c502013-04-05 13:47:09 +000038 canvas->save();
senorblancoce6a3542014-06-12 11:24:19 -070039 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
40 SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
41 SkIntToScalar(size.height()));
sugoi@google.come3b4c502013-04-05 13:47:09 +000042 canvas->drawRect(r, paint);
43 canvas->restore();
44 }
45
Florin Malita14d54c22017-05-18 11:52:59 -040046 void test(SkCanvas* canvas, int x, int y, Type type,
sugoi@google.come3b4c502013-04-05 13:47:09 +000047 float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
sugoi@google.com4775cba2013-04-17 13:46:56 +000048 bool stitchTiles) {
senorblancoce6a3542014-06-12 11:24:19 -070049 SkISize tileSize = SkISize::Make(fSize.width() / 2, fSize.height() / 2);
Florin Malita14d54c22017-05-18 11:52:59 -040050 sk_sp<SkShader> shader = (type == Type::kFractalNoise) ?
reedfe630452016-03-25 09:08:00 -070051 SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves,
52 seed, stitchTiles ? &tileSize : nullptr) :
53 SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves,
halcanary96fcdcc2015-08-27 07:41:13 -070054 seed, stitchTiles ? &tileSize : nullptr);
sugoi@google.come3b4c502013-04-05 13:47:09 +000055 SkPaint paint;
reedfe630452016-03-25 09:08:00 -070056 paint.setShader(std::move(shader));
senorblancoce6a3542014-06-12 11:24:19 -070057 if (stitchTiles) {
58 drawRect(canvas, x, y, paint, tileSize);
59 x += tileSize.width();
60 drawRect(canvas, x, y, paint, tileSize);
61 y += tileSize.width();
62 drawRect(canvas, x, y, paint, tileSize);
63 x -= tileSize.width();
64 drawRect(canvas, x, y, paint, tileSize);
65 } else {
66 drawRect(canvas, x, y, paint, fSize);
67 }
sugoi@google.come3b4c502013-04-05 13:47:09 +000068 }
69
70 virtual void onDraw(SkCanvas* canvas) {
senorblanco16b254a2015-04-09 11:13:24 -070071 canvas->clear(SK_ColorBLACK);
Florin Malita14d54c22017-05-18 11:52:59 -040072 test(canvas, 0, 0, Type::kFractalNoise,
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000073 0.1f, 0.1f, 0, 0, false);
Florin Malita14d54c22017-05-18 11:52:59 -040074 test(canvas, 100, 0, Type::kTurbulence,
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000075 0.1f, 0.1f, 0, 0, false);
skia.committer@gmail.comcff02432013-04-06 07:01:10 +000076
Florin Malita14d54c22017-05-18 11:52:59 -040077 test(canvas, 0, 100, Type::kFractalNoise,
sugoi@google.come3b4c502013-04-05 13:47:09 +000078 0.1f, 0.1f, 2, 0, false);
Florin Malita14d54c22017-05-18 11:52:59 -040079 test(canvas, 100, 100, Type::kFractalNoise,
senorblancoce6a3542014-06-12 11:24:19 -070080 0.05f, 0.1f, 1, 0, true);
sugoi@google.come3b4c502013-04-05 13:47:09 +000081
Florin Malita14d54c22017-05-18 11:52:59 -040082 test(canvas, 0, 200, Type::kTurbulence,
senorblancoce6a3542014-06-12 11:24:19 -070083 0.1f, 0.1f, 1, 0, true);
Florin Malita14d54c22017-05-18 11:52:59 -040084 test(canvas, 100, 200, Type::kTurbulence,
sugoi@google.come3b4c502013-04-05 13:47:09 +000085 0.2f, 0.4f, 5, 0, false);
sugoi@google.come3b4c502013-04-05 13:47:09 +000086
Florin Malita14d54c22017-05-18 11:52:59 -040087 test(canvas, 0, 300, Type::kFractalNoise,
sugoi@google.come3b4c502013-04-05 13:47:09 +000088 0.1f, 0.1f, 3, 1, false);
Florin Malita14d54c22017-05-18 11:52:59 -040089 test(canvas, 100, 300, Type::kFractalNoise,
sugoi@google.come3b4c502013-04-05 13:47:09 +000090 0.1f, 0.1f, 3, 4, false);
sugoi@google.come3b4c502013-04-05 13:47:09 +000091
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000092 canvas->scale(0.75f, 1.0f);
sugoi@google.come3b4c502013-04-05 13:47:09 +000093
Florin Malita14d54c22017-05-18 11:52:59 -040094 test(canvas, 0, 400, Type::kFractalNoise,
sugoi@google.come3b4c502013-04-05 13:47:09 +000095 0.1f, 0.1f, 2, 0, false);
Florin Malita14d54c22017-05-18 11:52:59 -040096 test(canvas, 100, 400, Type::kFractalNoise,
senorblancoce6a3542014-06-12 11:24:19 -070097 0.1f, 0.05f, 1, 0, true);
sugoi@google.come3b4c502013-04-05 13:47:09 +000098 }
99
100private:
101 typedef GM INHERITED;
102 SkISize fSize;
103};
104
reed@google.coma501a642014-05-12 17:18:45 +0000105class PerlinNoiseGM2 : public skiagm::GM {
106public:
107 PerlinNoiseGM2() {
108 fSize = SkISize::Make(80, 80);
109 }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000110
reed@google.coma501a642014-05-12 17:18:45 +0000111protected:
112 virtual SkString onShortName() {
113 return SkString("perlinnoise_localmatrix");
114 }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000115
reed@google.coma501a642014-05-12 17:18:45 +0000116 virtual SkISize onISize() {
117 return SkISize::Make(640, 480);
118 }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000119
Florin Malita14d54c22017-05-18 11:52:59 -0400120 void install(SkPaint* paint, Type type,
reed@google.coma501a642014-05-12 17:18:45 +0000121 float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
122 bool stitchTiles) {
Florin Malita14d54c22017-05-18 11:52:59 -0400123 sk_sp<SkShader> shader = (type == Type::kFractalNoise) ?
reedfe630452016-03-25 09:08:00 -0700124 SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves,
125 seed, stitchTiles ? &fSize : nullptr) :
126 SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves,
127 seed, stitchTiles ? &fSize : nullptr);
128 paint->setShader(std::move(shader));
reed@google.coma501a642014-05-12 17:18:45 +0000129 }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000130
reed@google.coma501a642014-05-12 17:18:45 +0000131 virtual void onDraw(SkCanvas* canvas) {
132 canvas->translate(10, 10);
133
134 SkPaint paint;
Robert Phillipsbee27322018-01-23 09:58:18 -0500135 this->install(&paint, Type::kFractalNoise, 0.1f, 0.1f, 2, 0, false);
reed@google.coma501a642014-05-12 17:18:45 +0000136
137 const SkScalar w = SkIntToScalar(fSize.width());
138 const SkScalar h = SkIntToScalar(fSize.height());
139
140 SkRect r = SkRect::MakeWH(w, h);
141 canvas->drawRect(r, paint);
142
143 canvas->save();
144 canvas->translate(w * 5/4, 0);
145 canvas->drawRect(r, paint);
146 canvas->restore();
147
148 canvas->save();
149 canvas->translate(0, h + 10);
150 canvas->scale(2, 2);
151 canvas->drawRect(r, paint);
152 canvas->restore();
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000153
reed@google.coma501a642014-05-12 17:18:45 +0000154 canvas->save();
155 canvas->translate(w + 100, h + 10);
156 canvas->scale(2, 2);
157 canvas->drawRect(r, paint);
158 canvas->restore();
159
160 // The next row should draw the same as the previous, even though we are using a local
161 // matrix instead of the canvas.
162
163 canvas->translate(0, h * 2 + 10);
164
165 SkMatrix lm;
166 lm.setScale(2, 2);
reed150835e2016-03-10 06:36:49 -0800167 paint.setShader(paint.getShader()->makeWithLocalMatrix(lm));
reed@google.coma501a642014-05-12 17:18:45 +0000168 r.fRight += r.width();
169 r.fBottom += r.height();
170
171 canvas->save();
172 canvas->translate(0, h + 10);
173 canvas->drawRect(r, paint);
174 canvas->restore();
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000175
reed@google.coma501a642014-05-12 17:18:45 +0000176 canvas->save();
177 canvas->translate(w + 100, h + 10);
178 canvas->drawRect(r, paint);
179 canvas->restore();
180 }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000181
reed@google.coma501a642014-05-12 17:18:45 +0000182private:
183 typedef GM INHERITED;
184 SkISize fSize;
185};
186
sugoi@google.come3b4c502013-04-05 13:47:09 +0000187//////////////////////////////////////////////////////////////////////////////
188
reed@google.coma501a642014-05-12 17:18:45 +0000189DEF_GM( return new PerlinNoiseGM; )
190DEF_GM( return new PerlinNoiseGM2; )