blob: b8237c065555e18b37af159ac02497fb76c29ac8 [file] [log] [blame]
sugoi@google.com4775cba2013-04-17 13:46:56 +00001/*
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 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
sugoi@google.com4775cba2013-04-17 13:46:56 +00008#include "SkCanvas.h"
9#include "SkPerlinNoiseShader.h"
10
tfarinaf168b862014-06-19 12:32:29 -070011class PerlinNoiseBench : public Benchmark {
sugoi@google.com4775cba2013-04-17 13:46:56 +000012 SkISize fSize;
13
14public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000015 PerlinNoiseBench() {
sugoi@google.com4775cba2013-04-17 13:46:56 +000016 fSize = SkISize::Make(80, 80);
17 }
18
19protected:
mtklein36352bf2015-03-25 18:17:31 -070020 const char* onGetName() override {
sugoi@google.com4775cba2013-04-17 13:46:56 +000021 return "perlinnoise";
22 }
23
mtklein36352bf2015-03-25 18:17:31 -070024 void onDraw(const int loops, SkCanvas* canvas) override {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000025 this->test(loops, canvas, 0, 0, SkPerlinNoiseShader::kFractalNoise_Type,
26 0.1f, 0.1f, 3, 0, false);
sugoi@google.com4775cba2013-04-17 13:46:56 +000027 }
28
29private:
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.org33614712013-12-03 18:17:16 +000041 void test(const int loops, SkCanvas* canvas, int x, int y, SkPerlinNoiseShader::Type type,
sugoi@google.com4775cba2013-04-17 13:46:56 +000042 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.org9fbbcca2014-04-01 16:09:37 +000047 SkPerlinNoiseShader::CreateTurbulence(baseFrequencyX, baseFrequencyY, numOctaves,
sugoi@google.com4775cba2013-04-17 13:46:56 +000048 seed, stitchTiles ? &fSize : NULL);
49 SkPaint paint;
50 paint.setShader(shader)->unref();
mtklein@google.comc2897432013-09-10 19:23:38 +000051
commit-bot@chromium.org33614712013-12-03 18:17:16 +000052 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000053 this->drawClippedRect(canvas, x, y, paint);
54 }
sugoi@google.com4775cba2013-04-17 13:46:56 +000055 }
56
tfarinaf168b862014-06-19 12:32:29 -070057 typedef Benchmark INHERITED;
sugoi@google.com4775cba2013-04-17 13:46:56 +000058};
59
60///////////////////////////////////////////////////////////////////////////////
61
mtklein@google.com410e6e82013-09-13 19:52:27 +000062DEF_BENCH( return new PerlinNoiseBench(); )