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" |
| 10 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 11 | class PerlinNoiseGM : public skiagm::GM { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 12 | public: |
| 13 | PerlinNoiseGM() { |
| 14 | this->setBGColor(0xFF000000); |
| 15 | fSize = SkISize::Make(80, 80); |
| 16 | } |
| 17 | |
| 18 | protected: |
| 19 | virtual SkString onShortName() { |
| 20 | return SkString("perlinnoise"); |
| 21 | } |
| 22 | |
| 23 | virtual SkISize onISize() { |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 24 | return SkISize::Make(200, 500); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 25 | } |
| 26 | |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 27 | 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] | 28 | canvas->save(); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 29 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 30 | SkRect r = SkRect::MakeWH(SkIntToScalar(size.width()), |
| 31 | SkIntToScalar(size.height())); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 32 | 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.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 38 | bool stitchTiles) { |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 39 | SkISize tileSize = SkISize::Make(fSize.width() / 2, fSize.height() / 2); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 40 | SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ? |
| 41 | SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 42 | seed, stitchTiles ? &tileSize : NULL) : |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame] | 43 | SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 44 | seed, stitchTiles ? &tileSize : NULL); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 45 | SkPaint paint; |
| 46 | paint.setShader(shader)->unref(); |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 47 | 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.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | virtual void onDraw(SkCanvas* canvas) { |
senorblanco | 16b254a | 2015-04-09 11:13:24 -0700 | [diff] [blame^] | 61 | canvas->clear(SK_ColorBLACK); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 62 | 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.com | cff0243 | 2013-04-06 07:01:10 +0000 | [diff] [blame] | 66 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 67 | test(canvas, 0, 100, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 68 | 0.1f, 0.1f, 2, 0, false); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 69 | test(canvas, 100, 100, SkPerlinNoiseShader::kFractalNoise_Type, |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 70 | 0.05f, 0.1f, 1, 0, true); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 71 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 72 | test(canvas, 0, 200, SkPerlinNoiseShader::kTurbulence_Type, |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 73 | 0.1f, 0.1f, 1, 0, true); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 74 | test(canvas, 100, 200, SkPerlinNoiseShader::kTurbulence_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 75 | 0.2f, 0.4f, 5, 0, false); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 76 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 77 | test(canvas, 0, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 78 | 0.1f, 0.1f, 3, 1, false); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 79 | test(canvas, 100, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 80 | 0.1f, 0.1f, 3, 4, false); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 81 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 82 | canvas->scale(0.75f, 1.0f); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 83 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 84 | test(canvas, 0, 400, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 85 | 0.1f, 0.1f, 2, 0, false); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 86 | test(canvas, 100, 400, SkPerlinNoiseShader::kFractalNoise_Type, |
senorblanco | ce6a354 | 2014-06-12 11:24:19 -0700 | [diff] [blame] | 87 | 0.1f, 0.05f, 1, 0, true); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private: |
| 91 | typedef GM INHERITED; |
| 92 | SkISize fSize; |
| 93 | }; |
| 94 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 95 | class PerlinNoiseGM2 : public skiagm::GM { |
| 96 | public: |
| 97 | PerlinNoiseGM2() { |
| 98 | fSize = SkISize::Make(80, 80); |
| 99 | } |
skia.committer@gmail.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 100 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 101 | protected: |
| 102 | virtual SkString onShortName() { |
| 103 | return SkString("perlinnoise_localmatrix"); |
| 104 | } |
skia.committer@gmail.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 105 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 106 | virtual SkISize onISize() { |
| 107 | return SkISize::Make(640, 480); |
| 108 | } |
skia.committer@gmail.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 109 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 110 | void install(SkPaint* paint, SkPerlinNoiseShader::Type type, |
| 111 | float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed, |
| 112 | bool stitchTiles) { |
| 113 | SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ? |
| 114 | SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, |
| 115 | seed, stitchTiles ? &fSize : NULL) : |
| 116 | SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, |
| 117 | seed, stitchTiles ? &fSize : NULL); |
| 118 | paint->setShader(shader)->unref(); |
| 119 | } |
skia.committer@gmail.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 120 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 121 | 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.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 143 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 144 | 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); |
commit-bot@chromium.org | 5970f62 | 2014-05-12 20:42:21 +0000 | [diff] [blame] | 157 | paint.setShader(SkShader::CreateLocalMatrixShader(paint.getShader(), lm))->unref(); |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 158 | 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.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 165 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 166 | canvas->save(); |
| 167 | canvas->translate(w + 100, h + 10); |
| 168 | canvas->drawRect(r, paint); |
| 169 | canvas->restore(); |
| 170 | } |
skia.committer@gmail.com | e92c68f | 2014-05-13 03:06:05 +0000 | [diff] [blame] | 171 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 172 | private: |
| 173 | typedef GM INHERITED; |
| 174 | SkISize fSize; |
| 175 | }; |
| 176 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 177 | ////////////////////////////////////////////////////////////////////////////// |
| 178 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 179 | DEF_GM( return new PerlinNoiseGM; ) |
| 180 | DEF_GM( return new PerlinNoiseGM2; ) |