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 | 344cf45 | 2013-06-17 14:19:01 +0000 | [diff] [blame] | 26 | return make_isize(200, 400); |
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); |
skia.committer@gmail.com | cff0243 | 2013-04-06 07:01:10 +0000 | [diff] [blame] | 55 | |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 56 | test(canvas, 0, 0, SkPerlinNoiseShader::kFractalNoise_Type, |
| 57 | 0.1f, 0.1f, 2, 0, false); |
| 58 | test(canvas, 100, 0, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 59 | 0.2f, 0.4f, 5, 0, true); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 60 | |
| 61 | test(canvas, 0, 100, SkPerlinNoiseShader::kTurbulence_Type, |
| 62 | 0.1f, 0.1f, 2, 0, true); |
| 63 | test(canvas, 100, 100, SkPerlinNoiseShader::kTurbulence_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 64 | 0.2f, 0.4f, 5, 0, false); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 65 | |
| 66 | test(canvas, 0, 200, SkPerlinNoiseShader::kFractalNoise_Type, |
| 67 | 0.1f, 0.1f, 3, 1, false); |
| 68 | test(canvas, 100, 200, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 69 | 0.1f, 0.1f, 3, 4, false); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 70 | |
| 71 | canvas->scale(SkFloatToScalar(0.75f), SkFloatToScalar(1.0f)); |
| 72 | |
| 73 | test(canvas, 0, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
| 74 | 0.1f, 0.1f, 2, 0, false); |
| 75 | test(canvas, 100, 300, SkPerlinNoiseShader::kFractalNoise_Type, |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 76 | 0.2f, 0.4f, 5, 0, true); |
sugoi@google.com | e3b4c50 | 2013-04-05 13:47:09 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | private: |
| 80 | typedef GM INHERITED; |
| 81 | SkISize fSize; |
| 82 | }; |
| 83 | |
| 84 | ////////////////////////////////////////////////////////////////////////////// |
| 85 | |
| 86 | static GM* MyFactory(void*) { return new PerlinNoiseGM; } |
| 87 | static GMRegistry reg(MyFactory); |
| 88 | |
| 89 | } |