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 | |
| 11 | namespace skiagm { |
| 12 | |
| 13 | class PerlinNoiseGM : public GM { |
| 14 | public: |
| 15 | PerlinNoiseGM() { |
| 16 | this->setBGColor(0xFF000000); |
| 17 | fSize = SkISize::Make(80, 80); |
| 18 | } |
| 19 | |
| 20 | protected: |
| 21 | virtual SkString onShortName() { |
| 22 | return SkString("perlinnoise"); |
| 23 | } |
| 24 | |
| 25 | virtual SkISize onISize() { |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 26 | return make_isize(200, 500); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void drawClippedRect(SkCanvas* canvas, int x, int y, const SkPaint& paint) { |
| 30 | canvas->save(); |
| 31 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 32 | SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height()))); |
| 33 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 34 | SkIntToScalar(fSize.width()), |
| 35 | SkIntToScalar(fSize.height())); |
| 36 | canvas->drawRect(r, paint); |
| 37 | canvas->restore(); |
| 38 | } |
| 39 | |
| 40 | void test(SkCanvas* canvas, int x, int y, SkPerlinNoiseShader::Type type, |
| 41 | float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed, |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 42 | bool stitchTiles) { |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 43 | SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ? |
| 44 | SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, |
| 45 | seed, stitchTiles ? &fSize : NULL) : |
| 46 | SkPerlinNoiseShader::CreateTubulence(baseFrequencyX, baseFrequencyY, numOctaves, |
| 47 | seed, stitchTiles ? &fSize : NULL); |
| 48 | SkPaint paint; |
| 49 | paint.setShader(shader)->unref(); |
| 50 | drawClippedRect(canvas, x, y, paint); |
| 51 | } |
| 52 | |
| 53 | virtual void onDraw(SkCanvas* canvas) { |
| 54 | canvas->clear(0x00000000); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 55 | test(canvas, 0, 0, SkPerlinNoiseShader::kFractalNoise_Type, |
| 56 | 0.1f, 0.1f, 0, 0, false); |
| 57 | test(canvas, 100, 0, SkPerlinNoiseShader::kTurbulence_Type, |
| 58 | 0.1f, 0.1f, 0, 0, false); |
skia.committer@gmail.com | cff0243 | 2013-04-06 07:01:10 +0000 | [diff] [blame] | 59 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 60 | test(canvas, 0, 100, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 61 | 0.1f, 0.1f, 2, 0, false); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 62 | test(canvas, 100, 100, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 63 | 0.2f, 0.4f, 5, 0, true); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 64 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 65 | test(canvas, 0, 200, SkPerlinNoiseShader::kTurbulence_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 66 | 0.1f, 0.1f, 2, 0, true); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 67 | test(canvas, 100, 200, SkPerlinNoiseShader::kTurbulence_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 68 | 0.2f, 0.4f, 5, 0, false); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 69 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 70 | test(canvas, 0, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 71 | 0.1f, 0.1f, 3, 1, false); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 72 | test(canvas, 100, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 73 | 0.1f, 0.1f, 3, 4, false); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 74 | |
| 75 | canvas->scale(SkFloatToScalar(0.75f), SkFloatToScalar(1.0f)); |
| 76 | |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 77 | test(canvas, 0, 400, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 78 | 0.1f, 0.1f, 2, 0, false); |
commit-bot@chromium.org | c2a0ea6 | 2013-11-06 10:08:38 +0000 | [diff] [blame] | 79 | test(canvas, 100, 400, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 80 | 0.2f, 0.4f, 5, 0, true); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | private: |
| 84 | typedef GM INHERITED; |
| 85 | SkISize fSize; |
| 86 | }; |
| 87 | |
| 88 | ////////////////////////////////////////////////////////////////////////////// |
| 89 | |
| 90 | static GM* MyFactory(void*) { return new PerlinNoiseGM; } |
| 91 | static GMRegistry reg(MyFactory); |
| 92 | |
| 93 | } |