sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +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 | */ |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 7 | #include "Benchmark.h" |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 8 | #include "SkCanvas.h" |
| 9 | #include "SkPerlinNoiseShader.h" |
| 10 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 11 | class PerlinNoiseBench : public Benchmark { |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 12 | SkISize fSize; |
| 13 | |
| 14 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 15 | PerlinNoiseBench() { |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 16 | fSize = SkISize::Make(80, 80); |
| 17 | } |
| 18 | |
| 19 | protected: |
| 20 | virtual const char* onGetName() SK_OVERRIDE { |
| 21 | return "perlinnoise"; |
| 22 | } |
| 23 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 24 | virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
| 25 | this->test(loops, canvas, 0, 0, SkPerlinNoiseShader::kFractalNoise_Type, |
| 26 | 0.1f, 0.1f, 3, 0, false); |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | private: |
| 30 | void drawClippedRect(SkCanvas* canvas, int x, int y, const SkPaint& paint) { |
| 31 | canvas->save(); |
| 32 | canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 33 | SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height()))); |
| 34 | SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y), |
| 35 | SkIntToScalar(fSize.width()), |
| 36 | SkIntToScalar(fSize.height())); |
| 37 | canvas->drawRect(r, paint); |
| 38 | canvas->restore(); |
| 39 | } |
| 40 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 41 | void test(const int loops, SkCanvas* canvas, int x, int y, SkPerlinNoiseShader::Type type, |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 42 | float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed, |
| 43 | bool stitchTiles) { |
| 44 | SkShader* shader = (type == SkPerlinNoiseShader::kFractalNoise_Type) ? |
| 45 | SkPerlinNoiseShader::CreateFractalNoise(baseFrequencyX, baseFrequencyY, numOctaves, |
| 46 | seed, stitchTiles ? &fSize : NULL) : |
commit-bot@chromium.org | 9fbbcca | 2014-04-01 16:09:37 +0000 | [diff] [blame] | 47 | SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves, |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 48 | seed, stitchTiles ? &fSize : NULL); |
| 49 | SkPaint paint; |
| 50 | paint.setShader(shader)->unref(); |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 51 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 52 | for (int i = 0; i < loops; i++) { |
mtklein@google.com | c289743 | 2013-09-10 19:23:38 +0000 | [diff] [blame] | 53 | this->drawClippedRect(canvas, x, y, paint); |
| 54 | } |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 55 | } |
| 56 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 57 | typedef Benchmark INHERITED; |
sugoi@google.com | 4775cba | 2013-04-17 13:46:56 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 62 | DEF_BENCH( return new PerlinNoiseBench(); ) |