PerlinNoiseBench.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 "bench/Benchmark.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkShader.h"
  10. #include "include/effects/SkPerlinNoiseShader.h"
  11. class PerlinNoiseBench : public Benchmark {
  12. SkISize fSize;
  13. public:
  14. PerlinNoiseBench() {
  15. fSize = SkISize::Make(80, 80);
  16. }
  17. protected:
  18. const char* onGetName() override {
  19. return "perlinnoise";
  20. }
  21. void onDraw(int loops, SkCanvas* canvas) override {
  22. this->test(loops, canvas, 0, 0, 0.1f, 0.1f, 3, 0, false);
  23. }
  24. private:
  25. void drawClippedRect(SkCanvas* canvas, int x, int y, const SkPaint& paint) {
  26. canvas->save();
  27. canvas->clipRect(SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
  28. SkIntToScalar(fSize.width()), SkIntToScalar(fSize.height())));
  29. SkRect r = SkRect::MakeXYWH(SkIntToScalar(x), SkIntToScalar(y),
  30. SkIntToScalar(fSize.width()),
  31. SkIntToScalar(fSize.height()));
  32. canvas->drawRect(r, paint);
  33. canvas->restore();
  34. }
  35. void test(int loops, SkCanvas* canvas, int x, int y,
  36. float baseFrequencyX, float baseFrequencyY, int numOctaves, float seed,
  37. bool stitchTiles) {
  38. SkPaint paint;
  39. paint.setShader(SkPerlinNoiseShader::MakeFractalNoise(baseFrequencyX, baseFrequencyY,
  40. numOctaves, seed,
  41. stitchTiles ? &fSize : nullptr));
  42. for (int i = 0; i < loops; i++) {
  43. this->drawClippedRect(canvas, x, y, paint);
  44. }
  45. }
  46. typedef Benchmark INHERITED;
  47. };
  48. ///////////////////////////////////////////////////////////////////////////////
  49. DEF_BENCH( return new PerlinNoiseBench(); )