• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "Benchmark.h"
8 #include "SkCanvas.h"
9 #include "SkPerlinNoiseShader.h"
10 #include "SkShader.h"
11 
12 class PerlinNoiseBench : public Benchmark {
13     SkISize fSize;
14 
15 public:
PerlinNoiseBench()16     PerlinNoiseBench()  {
17         fSize = SkISize::Make(80, 80);
18     }
19 
20 protected:
onGetName()21     const char* onGetName() override {
22         return "perlinnoise";
23     }
24 
onDraw(int loops,SkCanvas * canvas)25     void onDraw(int loops, SkCanvas* canvas) override {
26         this->test(loops, canvas, 0, 0, 0.1f, 0.1f, 3, 0, false);
27     }
28 
29 private:
drawClippedRect(SkCanvas * canvas,int x,int y,const SkPaint & paint)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 
test(int loops,SkCanvas * canvas,int x,int y,float baseFrequencyX,float baseFrequencyY,int numOctaves,float seed,bool stitchTiles)41     void test(int loops, SkCanvas* canvas, int x, int y,
42               float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
43               bool stitchTiles) {
44         SkPaint paint;
45         paint.setShader(SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
46                                                               numOctaves, seed,
47                                                               stitchTiles ? &fSize : nullptr));
48         for (int i = 0; i < loops; i++) {
49             this->drawClippedRect(canvas, x, y, paint);
50         }
51     }
52 
53     typedef Benchmark INHERITED;
54 };
55 
56 ///////////////////////////////////////////////////////////////////////////////
57 
58 DEF_BENCH( return new PerlinNoiseBench(); )
59