blob: 1c5aa7dfa8d12aa77ed7ab825536586e1e2ada54 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkMatrix.h"
12#include "include/core/SkPaint.h"
13#include "include/core/SkRect.h"
14#include "include/core/SkRefCnt.h"
15#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#include "include/core/SkSize.h"
18#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/effects/SkPerlinNoiseShader.h"
Florin Malita14d54c22017-05-18 11:52:59 -040020
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include <utility>
22
Florin Malita14d54c22017-05-18 11:52:59 -040023namespace {
24
25enum class Type {
26 kFractalNoise,
27 kTurbulence,
28};
29
reed@google.coma501a642014-05-12 17:18:45 +000030class PerlinNoiseGM : public skiagm::GM {
Hal Canarybd865e22019-07-18 11:51:19 -040031 SkISize fSize = {80, 80};
sugoi@google.come3b4c502013-04-05 13:47:09 +000032
Hal Canarybd865e22019-07-18 11:51:19 -040033 void onOnceBeforeDraw() override { this->setBGColor(0xFF000000); }
sugoi@google.come3b4c502013-04-05 13:47:09 +000034
Hal Canarybd865e22019-07-18 11:51:19 -040035 SkString onShortName() override { return SkString("perlinnoise"); }
36
37 SkISize onISize() override { return {200, 500}; }
sugoi@google.come3b4c502013-04-05 13:47:09 +000038
senorblancoce6a3542014-06-12 11:24:19 -070039 void drawRect(SkCanvas* canvas, int x, int y, const SkPaint& paint, const SkISize& size) {
sugoi@google.come3b4c502013-04-05 13:47:09 +000040 canvas->save();
senorblancoce6a3542014-06-12 11:24:19 -070041 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
42 SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()),
43 SkIntToScalar(size.height()));
sugoi@google.come3b4c502013-04-05 13:47:09 +000044 canvas->drawRect(r, paint);
45 canvas->restore();
46 }
47
Florin Malita14d54c22017-05-18 11:52:59 -040048 void test(SkCanvas* canvas, int x, int y, Type type,
sugoi@google.come3b4c502013-04-05 13:47:09 +000049 float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
sugoi@google.com4775cba2013-04-17 13:46:56 +000050 bool stitchTiles) {
senorblancoce6a3542014-06-12 11:24:19 -070051 SkISize tileSize = SkISize::Make(fSize.width() / 2, fSize.height() / 2);
Florin Malita14d54c22017-05-18 11:52:59 -040052 sk_sp<SkShader> shader = (type == Type::kFractalNoise) ?
reedfe630452016-03-25 09:08:00 -070053 SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves,
54 seed, stitchTiles ? &tileSize : nullptr) :
55 SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves,
halcanary96fcdcc2015-08-27 07:41:13 -070056 seed, stitchTiles ? &tileSize : nullptr);
sugoi@google.come3b4c502013-04-05 13:47:09 +000057 SkPaint paint;
reedfe630452016-03-25 09:08:00 -070058 paint.setShader(std::move(shader));
senorblancoce6a3542014-06-12 11:24:19 -070059 if (stitchTiles) {
60 drawRect(canvas, x, y, paint, tileSize);
61 x += tileSize.width();
62 drawRect(canvas, x, y, paint, tileSize);
63 y += tileSize.width();
64 drawRect(canvas, x, y, paint, tileSize);
65 x -= tileSize.width();
66 drawRect(canvas, x, y, paint, tileSize);
67 } else {
68 drawRect(canvas, x, y, paint, fSize);
69 }
sugoi@google.come3b4c502013-04-05 13:47:09 +000070 }
71
Hal Canarybd865e22019-07-18 11:51:19 -040072 void onDraw(SkCanvas* canvas) override {
senorblanco16b254a2015-04-09 11:13:24 -070073 canvas->clear(SK_ColorBLACK);
Florin Malita14d54c22017-05-18 11:52:59 -040074 test(canvas, 0, 0, Type::kFractalNoise,
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000075 0.1f, 0.1f, 0, 0, false);
Florin Malita14d54c22017-05-18 11:52:59 -040076 test(canvas, 100, 0, Type::kTurbulence,
commit-bot@chromium.orgc2a0ea62013-11-06 10:08:38 +000077 0.1f, 0.1f, 0, 0, false);
skia.committer@gmail.comcff02432013-04-06 07:01:10 +000078
Florin Malita14d54c22017-05-18 11:52:59 -040079 test(canvas, 0, 100, Type::kFractalNoise,
sugoi@google.come3b4c502013-04-05 13:47:09 +000080 0.1f, 0.1f, 2, 0, false);
Florin Malita14d54c22017-05-18 11:52:59 -040081 test(canvas, 100, 100, Type::kFractalNoise,
senorblancoce6a3542014-06-12 11:24:19 -070082 0.05f, 0.1f, 1, 0, true);
sugoi@google.come3b4c502013-04-05 13:47:09 +000083
Florin Malita14d54c22017-05-18 11:52:59 -040084 test(canvas, 0, 200, Type::kTurbulence,
senorblancoce6a3542014-06-12 11:24:19 -070085 0.1f, 0.1f, 1, 0, true);
Florin Malita14d54c22017-05-18 11:52:59 -040086 test(canvas, 100, 200, Type::kTurbulence,
sugoi@google.come3b4c502013-04-05 13:47:09 +000087 0.2f, 0.4f, 5, 0, false);
sugoi@google.come3b4c502013-04-05 13:47:09 +000088
Florin Malita14d54c22017-05-18 11:52:59 -040089 test(canvas, 0, 300, Type::kFractalNoise,
sugoi@google.come3b4c502013-04-05 13:47:09 +000090 0.1f, 0.1f, 3, 1, false);
Florin Malita14d54c22017-05-18 11:52:59 -040091 test(canvas, 100, 300, Type::kFractalNoise,
sugoi@google.come3b4c502013-04-05 13:47:09 +000092 0.1f, 0.1f, 3, 4, false);
sugoi@google.come3b4c502013-04-05 13:47:09 +000093
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000094 canvas->scale(0.75f, 1.0f);
sugoi@google.come3b4c502013-04-05 13:47:09 +000095
Florin Malita14d54c22017-05-18 11:52:59 -040096 test(canvas, 0, 400, Type::kFractalNoise,
sugoi@google.come3b4c502013-04-05 13:47:09 +000097 0.1f, 0.1f, 2, 0, false);
Florin Malita14d54c22017-05-18 11:52:59 -040098 test(canvas, 100, 400, Type::kFractalNoise,
senorblancoce6a3542014-06-12 11:24:19 -070099 0.1f, 0.05f, 1, 0, true);
sugoi@google.come3b4c502013-04-05 13:47:09 +0000100 }
101
102private:
103 typedef GM INHERITED;
sugoi@google.come3b4c502013-04-05 13:47:09 +0000104};
105
reed@google.coma501a642014-05-12 17:18:45 +0000106class PerlinNoiseGM2 : public skiagm::GM {
Hal Canarybd865e22019-07-18 11:51:19 -0400107 SkISize fSize = {80, 80};
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000108
Hal Canarybd865e22019-07-18 11:51:19 -0400109 SkString onShortName() override { return SkString("perlinnoise_localmatrix"); }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000110
Hal Canarybd865e22019-07-18 11:51:19 -0400111 SkISize onISize() override { return {640, 480}; }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000112
Florin Malita14d54c22017-05-18 11:52:59 -0400113 void install(SkPaint* paint, Type type,
reed@google.coma501a642014-05-12 17:18:45 +0000114 float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
115 bool stitchTiles) {
Florin Malita14d54c22017-05-18 11:52:59 -0400116 sk_sp<SkShader> shader = (type == Type::kFractalNoise) ?
reedfe630452016-03-25 09:08:00 -0700117 SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves,
118 seed, stitchTiles ? &fSize : nullptr) :
119 SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves,
120 seed, stitchTiles ? &fSize : nullptr);
121 paint->setShader(std::move(shader));
reed@google.coma501a642014-05-12 17:18:45 +0000122 }
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000123
Hal Canarybd865e22019-07-18 11:51:19 -0400124 void onDraw(SkCanvas* canvas) override {
reed@google.coma501a642014-05-12 17:18:45 +0000125 canvas->translate(10, 10);
126
127 SkPaint paint;
Robert Phillipsbee27322018-01-23 09:58:18 -0500128 this->install(&paint, Type::kFractalNoise, 0.1f, 0.1f, 2, 0, false);
reed@google.coma501a642014-05-12 17:18:45 +0000129
130 const SkScalar w = SkIntToScalar(fSize.width());
131 const SkScalar h = SkIntToScalar(fSize.height());
132
133 SkRect r = SkRect::MakeWH(w, h);
134 canvas->drawRect(r, paint);
135
136 canvas->save();
137 canvas->translate(w * 5/4, 0);
138 canvas->drawRect(r, paint);
139 canvas->restore();
140
141 canvas->save();
142 canvas->translate(0, h + 10);
143 canvas->scale(2, 2);
144 canvas->drawRect(r, paint);
145 canvas->restore();
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000146
reed@google.coma501a642014-05-12 17:18:45 +0000147 canvas->save();
148 canvas->translate(w + 100, h + 10);
149 canvas->scale(2, 2);
150 canvas->drawRect(r, paint);
151 canvas->restore();
152
153 // The next row should draw the same as the previous, even though we are using a local
154 // matrix instead of the canvas.
155
156 canvas->translate(0, h * 2 + 10);
157
158 SkMatrix lm;
159 lm.setScale(2, 2);
reed150835e2016-03-10 06:36:49 -0800160 paint.setShader(paint.getShader()->makeWithLocalMatrix(lm));
reed@google.coma501a642014-05-12 17:18:45 +0000161 r.fRight += r.width();
162 r.fBottom += r.height();
163
164 canvas->save();
165 canvas->translate(0, h + 10);
166 canvas->drawRect(r, paint);
167 canvas->restore();
skia.committer@gmail.come92c68f2014-05-13 03:06:05 +0000168
reed@google.coma501a642014-05-12 17:18:45 +0000169 canvas->save();
170 canvas->translate(w + 100, h + 10);
171 canvas->drawRect(r, paint);
172 canvas->restore();
173 }
reed@google.coma501a642014-05-12 17:18:45 +0000174};
175
Hal Canarybd865e22019-07-18 11:51:19 -0400176} // namespace
sugoi@google.come3b4c502013-04-05 13:47:09 +0000177
reed@google.coma501a642014-05-12 17:18:45 +0000178DEF_GM( return new PerlinNoiseGM; )
179DEF_GM( return new PerlinNoiseGM2; )