sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 1 | /* |
| 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 Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 10 | #include "SkShader.h" |
| 11 | |
| 12 | namespace { |
| 13 | |
| 14 | enum class Type { |
| 15 | kFractalNoise, |
| 16 | kTurbulence, |
| 17 | }; |
| 18 | |
| 19 | } // anonymous ns |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 20 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 21 | class PerlinNoiseGM : public skiagm::GM { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 22 | public: |
| 23 | PerlinNoiseGM() { |
| 24 | this->setBGColor(0xFF000000); |
| 25 | fSize = SkISize::Make(80, 80); |
| 26 | } |
| 27 | |
| 28 | protected: |
| 29 | virtual SkString onShortName() { |
| 30 | return SkString("perlinnoise"); |
| 31 | } |
| 32 | |
| 33 | virtual SkISize onISize() { |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 34 | return SkISize::Make(200, 500); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 35 | } |
| 36 | |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 37 | void drawRect(SkCanvas* canvas, int x, int y, const SkPaint& paint, const SkISize& size) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 38 | canvas->save(); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 39 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 40 | SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()), |
| 41 | SkIntToScalar(size.height())); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 42 | canvas->drawRect(r, paint); |
| 43 | canvas->restore(); |
| 44 | } |
| 45 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 46 | void test(SkCanvas* canvas, int x, int y, Type type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 47 | float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed, |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 48 | bool stitchTiles) { |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 49 | SkISize tileSize = SkISize::Make(fSize.width() / 2, fSize.height() / 2); |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 50 | sk_sp<SkShader> shader = (type == Type::kFractalNoise) ? |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 51 | SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, |
| 52 | seed, stitchTiles ? &tileSize : nullptr) : |
| 53 | SkPerlinNoiseShader::MakeTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | seed, stitchTiles ? &tileSize : nullptr); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 55 | SkPaint paint; |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 56 | paint.setShader(std::move(shader)); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 57 | 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.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | virtual void onDraw(SkCanvas* canvas) { |
senorblanco | 16b254a | 2015-04-09 11:13:24 -0700 | [diff] [blame] | 71 | canvas->clear(SK_ColorBLACK); |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 72 | test(canvas, 0, 0, Type::kFractalNoise, |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 73 | 0.1f, 0.1f, 0, 0, false); |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 74 | test(canvas, 100, 0, Type::kTurbulence, |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 75 | 0.1f, 0.1f, 0, 0, false); |
skia.committer@gmail.com | cff0243 | 2013-04-06 07:01:10 +0000 | [diff] [blame] | 76 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 77 | test(canvas, 0, 100, Type::kFractalNoise, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 78 | 0.1f, 0.1f, 2, 0, false); |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 79 | test(canvas, 100, 100, Type::kFractalNoise, |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 80 | 0.05f, 0.1f, 1, 0, true); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 81 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 82 | test(canvas, 0, 200, Type::kTurbulence, |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 83 | 0.1f, 0.1f, 1, 0, true); |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 84 | test(canvas, 100, 200, Type::kTurbulence, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 85 | 0.2f, 0.4f, 5, 0, false); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 86 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 87 | test(canvas, 0, 300, Type::kFractalNoise, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 88 | 0.1f, 0.1f, 3, 1, false); |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 89 | test(canvas, 100, 300, Type::kFractalNoise, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 90 | 0.1f, 0.1f, 3, 4, false); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 91 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 92 | canvas->scale(0.75f, 1.0f); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 93 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 94 | test(canvas, 0, 400, Type::kFractalNoise, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 95 | 0.1f, 0.1f, 2, 0, false); |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 96 | test(canvas, 100, 400, Type::kFractalNoise, |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 97 | 0.1f, 0.05f, 1, 0, true); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | private: |
| 101 | typedef GM INHERITED; |
| 102 | SkISize fSize; |
| 103 | }; |
| 104 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 105 | class PerlinNoiseGM2 : public skiagm::GM { |
| 106 | public: |
| 107 | PerlinNoiseGM2() { |
| 108 | fSize = SkISize::Make(80, 80); |
| 109 | } |
skia.committer@gmail.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 110 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 111 | protected: |
| 112 | virtual SkString onShortName() { |
| 113 | return SkString("perlinnoise_localmatrix"); |
| 114 | } |
skia.committer@gmail.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 115 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 116 | virtual SkISize onISize() { |
| 117 | return SkISize::Make(640, 480); |
| 118 | } |
skia.committer@gmail.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 119 | |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 120 | void install(SkPaint* paint, Type type, |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 121 | float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed, |
| 122 | bool stitchTiles) { |
Florin Malita | 14d54c2 | 2017-05-18 11:52:59 -0400 | [diff] [blame] | 123 | sk_sp<SkShader> shader = (type == Type::kFractalNoise) ? |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 124 | 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.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 129 | } |
skia.committer@gmail.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 130 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 131 | virtual void onDraw(SkCanvas* canvas) { |
| 132 | canvas->translate(10, 10); |
| 133 | |
| 134 | SkPaint paint; |
Robert Phillips | bee2732 | 2018-01-23 09:58:18 -0500 | [diff] [blame] | 135 | this->install(&paint, Type::kFractalNoise, 0.1f, 0.1f, 2, 0, false); |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 136 | |
| 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.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 153 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 154 | 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); |
reed | 150835e | 2016-03-10 06:36:49 -0800 | [diff] [blame] | 167 | paint.setShader(paint.getShader()->makeWithLocalMatrix(lm)); |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 168 | 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.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 175 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 176 | canvas->save(); |
| 177 | canvas->translate(w + 100, h + 10); |
| 178 | canvas->drawRect(r, paint); |
| 179 | canvas->restore(); |
| 180 | } |
skia.committer@gmail.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 181 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 182 | private: |
| 183 | typedef GM INHERITED; |
| 184 | SkISize fSize; |
| 185 | }; |
| 186 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 187 | ////////////////////////////////////////////////////////////////////////////// |
| 188 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 189 | DEF_GM( return new PerlinNoiseGM; ) |
| 190 | DEF_GM( return new PerlinNoiseGM2; ) |