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 | |
| 27 | void drawClippedRect(SkCanvas* canvas, int x, int y, const SkPaint& paint) { |
| 28 | canvas->save(); |
| 29 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 30 | SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height()))); |
| 31 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 32 | SkIntToScalar(fSize.width()), |
| 33 | SkIntToScalar(fSize.height())); |
| 34 | canvas->drawRect(r, paint); |
| 35 | canvas->restore(); |
| 36 | } |
| 37 | |
| 38 | void test(SkCanvas* canvas, int x, int y, SkPerlinNoiseShader::Type type, |
| 39 | float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed, |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 40 | bool stitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 41 | SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ? |
| 42 | SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, |
| 43 | seed, stitchTiles ? &fSize : NULL) : |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame] | 44 | SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 45 | seed, stitchTiles ? &fSize : NULL); |
| 46 | SkPaint paint; |
| 47 | paint.setShader(shader)->unref(); |
| 48 | drawClippedRect(canvas, x, y, paint); |
| 49 | } |
| 50 | |
| 51 | virtual void onDraw(SkCanvas* canvas) { |
| 52 | canvas->clear(0x00000000); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 53 | test(canvas, 0, 0, SkPerlinNoiseShader::kFractalNoise_Type, |
| 54 | 0.1f, 0.1f, 0, 0, false); |
| 55 | test(canvas, 100, 0, SkPerlinNoiseShader::kTurbulence_Type, |
| 56 | 0.1f, 0.1f, 0, 0, false); |
skia.committer@gmail.com | cff0243 | 2013-04-06 07:01:10 +0000 | [diff] [blame] | 57 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 58 | test(canvas, 0, 100, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 59 | 0.1f, 0.1f, 2, 0, false); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 60 | test(canvas, 100, 100, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 61 | 0.2f, 0.4f, 5, 0, true); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 62 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 63 | test(canvas, 0, 200, SkPerlinNoiseShader::kTurbulence_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 64 | 0.1f, 0.1f, 2, 0, true); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 65 | test(canvas, 100, 200, SkPerlinNoiseShader::kTurbulence_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 66 | 0.2f, 0.4f, 5, 0, false); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 67 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 68 | test(canvas, 0, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 69 | 0.1f, 0.1f, 3, 1, false); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 70 | test(canvas, 100, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 71 | 0.1f, 0.1f, 3, 4, false); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 72 | |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 73 | canvas->scale(0.75f, 1.0f); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 74 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 75 | test(canvas, 0, 400, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 76 | 0.1f, 0.1f, 2, 0, false); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 77 | test(canvas, 100, 400, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 78 | 0.2f, 0.4f, 5, 0, true); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | private: |
| 82 | typedef GM INHERITED; |
| 83 | SkISize fSize; |
| 84 | }; |
| 85 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 86 | class PerlinNoiseGM2 : public skiagm::GM { |
| 87 | public: |
| 88 | PerlinNoiseGM2() { |
| 89 | fSize = SkISize::Make(80, 80); |
| 90 | } |
| 91 | |
| 92 | protected: |
| 93 | virtual SkString onShortName() { |
| 94 | return SkString("perlinnoise_localmatrix"); |
| 95 | } |
| 96 | |
| 97 | virtual SkISize onISize() { |
| 98 | return SkISize::Make(640, 480); |
| 99 | } |
| 100 | |
| 101 | void install(SkPaint* paint, SkPerlinNoiseShader::Type type, |
| 102 | float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed, |
| 103 | bool stitchTiles) { |
| 104 | SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ? |
| 105 | SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, |
| 106 | seed, stitchTiles ? &fSize : NULL) : |
| 107 | SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, |
| 108 | seed, stitchTiles ? &fSize : NULL); |
| 109 | paint->setShader(shader)->unref(); |
| 110 | } |
| 111 | |
| 112 | virtual void onDraw(SkCanvas* canvas) { |
| 113 | canvas->translate(10, 10); |
| 114 | |
| 115 | SkPaint paint; |
| 116 | install(&paint, SkPerlinNoiseShader::kFractalNoise_Type, 0.1f, 0.1f, 2, 0, false); |
| 117 | |
| 118 | const SkScalar w = SkIntToScalar(fSize.width()); |
| 119 | const SkScalar h = SkIntToScalar(fSize.height()); |
| 120 | |
| 121 | SkRect r = SkRect::MakeWH(w, h); |
| 122 | canvas->drawRect(r, paint); |
| 123 | |
| 124 | canvas->save(); |
| 125 | canvas->translate(w * 5/4, 0); |
| 126 | canvas->drawRect(r, paint); |
| 127 | canvas->restore(); |
| 128 | |
| 129 | canvas->save(); |
| 130 | canvas->translate(0, h + 10); |
| 131 | canvas->scale(2, 2); |
| 132 | canvas->drawRect(r, paint); |
| 133 | canvas->restore(); |
| 134 | |
| 135 | canvas->save(); |
| 136 | canvas->translate(w + 100, h + 10); |
| 137 | canvas->scale(2, 2); |
| 138 | canvas->drawRect(r, paint); |
| 139 | canvas->restore(); |
| 140 | |
| 141 | // The next row should draw the same as the previous, even though we are using a local |
| 142 | // matrix instead of the canvas. |
| 143 | |
| 144 | canvas->translate(0, h * 2 + 10); |
| 145 | |
| 146 | SkMatrix lm; |
| 147 | lm.setScale(2, 2); |
commit-bot@chromium.org | 5970f62 | 2014-05-12 20:42:21 +0000 | [diff] [blame^] | 148 | paint.setShader(SkShader::CreateLocalMatrixShader(paint.getShader(), lm))->unref(); |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 149 | r.fRight += r.width(); |
| 150 | r.fBottom += r.height(); |
| 151 | |
| 152 | canvas->save(); |
| 153 | canvas->translate(0, h + 10); |
| 154 | canvas->drawRect(r, paint); |
| 155 | canvas->restore(); |
| 156 | |
| 157 | canvas->save(); |
| 158 | canvas->translate(w + 100, h + 10); |
| 159 | canvas->drawRect(r, paint); |
| 160 | canvas->restore(); |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | typedef GM INHERITED; |
| 165 | SkISize fSize; |
| 166 | }; |
| 167 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 168 | ////////////////////////////////////////////////////////////////////////////// |
| 169 | |
reed@google.com | a501a64 | 2014-05-12 17:18:45 +0000 | [diff] [blame] | 170 | DEF_GM( return new PerlinNoiseGM; ) |
| 171 | DEF_GM( return new PerlinNoiseGM2; ) |