blob: 802c5be021f17b10b7f3caa051d151f629586540 [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"
Florin Malita14d54c22017-05-18 11:52:59 -040010#include "SkShader.h"
sugoi@google.com4775cba2013-04-17 13:46:56 +000011
tfarinaf168b862014-06-19 12:32:29 -070012class PerlinNoiseBench : public Benchmark {
sugoi@google.com4775cba2013-04-17 13:46:56 +000013 SkISize fSize;
14
15public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000016 PerlinNoiseBench() {
sugoi@google.com4775cba2013-04-17 13:46:56 +000017 fSize = SkISize::Make(80, 80);
18 }
19
20protected:
mtklein36352bf2015-03-25 18:17:31 -070021 const char* onGetName() override {
sugoi@google.com4775cba2013-04-17 13:46:56 +000022 return "perlinnoise";
23 }
24
mtkleina1ebeb22015-10-01 09:43:39 -070025 void onDraw(int loops, SkCanvas* canvas) override {
Florin Malita14d54c22017-05-18 11:52:59 -040026 this->test(loops, canvas, 0, 0, 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
Florin Malita14d54c22017-05-18 11:52:59 -040041 void test(int loops, SkCanvas* canvas, int x, int y,
sugoi@google.com4775cba2013-04-17 13:46:56 +000042 float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
43 bool stitchTiles) {
sugoi@google.com4775cba2013-04-17 13:46:56 +000044 SkPaint paint;
Florin Malita14d54c22017-05-18 11:52:59 -040045 paint.setShader(SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
46 numOctaves, seed,
47 stitchTiles ? &fSize : nullptr));
commit-bot@chromium.org33614712013-12-03 18:17:16 +000048 for (int i = 0; i < loops; i++) {
mtklein@google.comc2897432013-09-10 19:23:38 +000049 this->drawClippedRect(canvas, x, y, paint);
50 }
sugoi@google.com4775cba2013-04-17 13:46:56 +000051 }
52
tfarinaf168b862014-06-19 12:32:29 -070053 typedef Benchmark INHERITED;
sugoi@google.com4775cba2013-04-17 13:46:56 +000054};
55
56///////////////////////////////////////////////////////////////////////////////
57
mtklein@google.com410e6e82013-09-13 19:52:27 +000058DEF_BENCH( return new PerlinNoiseBench(); )