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