blob: b3ece9dcb6a1a998f304740933a1aa7d6835b493 [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"
10
reed@google.coma501a642014-05-12 17:18:45 +000011class PerlinNoiseGM : public skiagm::GM {
sugoi@google.come3b4c502013-04-05 13:47:09 +000012public:
13 PerlinNoiseGM() {
14 this->setBGColor(0xFF000000);
15 fSize = SkISize::Make(80, 80);
16 }
17
18protected:
19 virtual SkString onShortName() {
20 return SkString("perlinnoise");
21 }
22
23 virtual SkISize onISize() {
reed@google.coma501a642014-05-12 17:18:45 +000024 return SkISize::Make(200, 500);
sugoi@google.come3b4c502013-04-05 13:47:09 +000025 }
26
senorblancoce6a3542014-06-12 11:24:19 -070027 void drawRect(SkCanvas* canvas, int x, int y, const SkPaint& paint, const SkISize& size) {
sugoi@google.come3b4c502013-04-05 13:47:09 +000028 canvas->save();
senorblancoce6a3542014-06-12 11:24:19 -070029 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
30 SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
31 SkIntToScalar(size.height()));
sugoi@google.come3b4c502013-04-05 13:47:09 +000032 canvas->drawRect(r, paint);
33 canvas->restore();
34 }
35
36 void test(SkCanvas* canvas, int x, int y, SkPerlinNoiseShader::Type type,
37 float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
sugoi@google.com4775cba2013-04-17 13:46:56 +000038 bool stitchTiles) {
senorblancoce6a3542014-06-12 11:24:19 -070039 SkISize tileSize = SkISize::Make(fSize.width() / 2, fSize.height() / 2);
reedfe630452016-03-25 09:08:00 -070040 sk_sp<SkShader> shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ?
41 SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves,
42 seed, stitchTiles ? &tileSize : nullptr) :
43 SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves,
halcanary96fcdcc2015-08-27 07:41:13 -070044 seed, stitchTiles ? &tileSize : nullptr);
sugoi@google.come3b4c502013-04-05 13:47:09 +000045 SkPaint paint;
reedfe630452016-03-25 09:08:00 -070046 paint.setShader(std::move(shader));
senorblancoce6a3542014-06-12 11:24:19 -070047 if (stitchTiles) {
48 drawRect(canvas, x, y, paint, tileSize);
49 x += tileSize.width();
50 drawRect(canvas, x, y, paint, tileSize);
51 y += tileSize.width();
52 drawRect(canvas, x, y, paint, tileSize);
53 x -= tileSize.width();
54 drawRect(canvas, x, y, paint, tileSize);
55 } else {
56 drawRect(canvas, x, y, paint, fSize);
57 }
sugoi@google.come3b4c502013-04-05 13:47:09 +000058 }
59
60 virtual void onDraw(SkCanvas* canvas) {
senorblanco16b254a2015-04-09 11:13:24 -070061 canvas->clear(SK_ColorBLACK);
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000062 test(canvas, 0, 0, SkPerlinNoiseShader::kFractalNoise_Type,
63 0.1f, 0.1f, 0, 0, false);
64 test(canvas, 100, 0, SkPerlinNoiseShader::kTurbulence_Type,
65 0.1f, 0.1f, 0, 0, false);
skia.committer@gmail.comcff02432013-04-06 07:01:10 +000066
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000067 test(canvas, 0, 100, SkPerlinNoiseShader::kFractalNoise_Type,
sugoi@google.come3b4c502013-04-05 13:47:09 +000068 0.1f, 0.1f, 2, 0, false);
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000069 test(canvas, 100, 100, SkPerlinNoiseShader::kFractalNoise_Type,
senorblancoce6a3542014-06-12 11:24:19 -070070 0.05f, 0.1f, 1, 0, true);
sugoi@google.come3b4c502013-04-05 13:47:09 +000071
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000072 test(canvas, 0, 200, SkPerlinNoiseShader::kTurbulence_Type,
senorblancoce6a3542014-06-12 11:24:19 -070073 0.1f, 0.1f, 1, 0, true);
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000074 test(canvas, 100, 200, SkPerlinNoiseShader::kTurbulence_Type,
sugoi@google.come3b4c502013-04-05 13:47:09 +000075 0.2f, 0.4f, 5, 0, false);
sugoi@google.come3b4c502013-04-05 13:47:09 +000076
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000077 test(canvas, 0, 300, SkPerlinNoiseShader::kFractalNoise_Type,
sugoi@google.come3b4c502013-04-05 13:47:09 +000078 0.1f, 0.1f, 3, 1, false);
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000079 test(canvas, 100, 300, SkPerlinNoiseShader::kFractalNoise_Type,
sugoi@google.come3b4c502013-04-05 13:47:09 +000080 0.1f, 0.1f, 3, 4, false);
sugoi@google.come3b4c502013-04-05 13:47:09 +000081
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000082 canvas->scale(0.75f, 1.0f);
sugoi@google.come3b4c502013-04-05 13:47:09 +000083
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000084 test(canvas, 0, 400, SkPerlinNoiseShader::kFractalNoise_Type,
sugoi@google.come3b4c502013-04-05 13:47:09 +000085 0.1f, 0.1f, 2, 0, false);
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000086 test(canvas, 100, 400, SkPerlinNoiseShader::kFractalNoise_Type,
senorblancoce6a3542014-06-12 11:24:19 -070087 0.1f, 0.05f, 1, 0, true);
sugoi@google.come3b4c502013-04-05 13:47:09 +000088 }
89
90private:
91 typedef GM INHERITED;
92 SkISize fSize;
93};
94
reed@google.coma501a642014-05-12 17:18:45 +000095class PerlinNoiseGM2 : public skiagm::GM {
96public:
97 PerlinNoiseGM2() {
98 fSize = SkISize::Make(80, 80);
99 }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000100
reed@google.coma501a642014-05-12 17:18:45 +0000101protected:
102 virtual SkString onShortName() {
103 return SkString("perlinnoise_localmatrix");
104 }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000105
reed@google.coma501a642014-05-12 17:18:45 +0000106 virtual SkISize onISize() {
107 return SkISize::Make(640, 480);
108 }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000109
reed@google.coma501a642014-05-12 17:18:45 +0000110 void install(SkPaint* paint, SkPerlinNoiseShader::Type type,
111 float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
112 bool stitchTiles) {
reedfe630452016-03-25 09:08:00 -0700113 sk_sp<SkShader> shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ?
114 SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves,
115 seed, stitchTiles ? &fSize : nullptr) :
116 SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves,
117 seed, stitchTiles ? &fSize : nullptr);
118 paint->setShader(std::move(shader));
reed@google.coma501a642014-05-12 17:18:45 +0000119 }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000120
reed@google.coma501a642014-05-12 17:18:45 +0000121 virtual void onDraw(SkCanvas* canvas) {
122 canvas->translate(10, 10);
123
124 SkPaint paint;
125 install(&paint, SkPerlinNoiseShader::kFractalNoise_Type, 0.1f, 0.1f, 2, 0, false);
126
127 const SkScalar w = SkIntToScalar(fSize.width());
128 const SkScalar h = SkIntToScalar(fSize.height());
129
130 SkRect r = SkRect::MakeWH(w, h);
131 canvas->drawRect(r, paint);
132
133 canvas->save();
134 canvas->translate(w * 5/4, 0);
135 canvas->drawRect(r, paint);
136 canvas->restore();
137
138 canvas->save();
139 canvas->translate(0, h + 10);
140 canvas->scale(2, 2);
141 canvas->drawRect(r, paint);
142 canvas->restore();
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000143
reed@google.coma501a642014-05-12 17:18:45 +0000144 canvas->save();
145 canvas->translate(w + 100, h + 10);
146 canvas->scale(2, 2);
147 canvas->drawRect(r, paint);
148 canvas->restore();
149
150 // The next row should draw the same as the previous, even though we are using a local
151 // matrix instead of the canvas.
152
153 canvas->translate(0, h * 2 + 10);
154
155 SkMatrix lm;
156 lm.setScale(2, 2);
reed150835e2016-03-10 06:36:49 -0800157 paint.setShader(paint.getShader()->makeWithLocalMatrix(lm));
reed@google.coma501a642014-05-12 17:18:45 +0000158 r.fRight += r.width();
159 r.fBottom += r.height();
160
161 canvas->save();
162 canvas->translate(0, h + 10);
163 canvas->drawRect(r, paint);
164 canvas->restore();
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000165
reed@google.coma501a642014-05-12 17:18:45 +0000166 canvas->save();
167 canvas->translate(w + 100, h + 10);
168 canvas->drawRect(r, paint);
169 canvas->restore();
170 }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000171
reed@google.coma501a642014-05-12 17:18:45 +0000172private:
173 typedef GM INHERITED;
174 SkISize fSize;
175};
176
sugoi@google.come3b4c502013-04-05 13:47:09 +0000177//////////////////////////////////////////////////////////////////////////////
178
reed@google.coma501a642014-05-12 17:18:45 +0000179DEF_GM( return new PerlinNoiseGM; )
180DEF_GM( return new PerlinNoiseGM2; )