PathOpsBench.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2018 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/SkPath.h"
  9. #include "include/core/SkShader.h"
  10. #include "include/core/SkString.h"
  11. #include "include/pathops/SkPathOps.h"
  12. #include "include/private/SkTArray.h"
  13. #include "include/utils/SkRandom.h"
  14. class PathOpsBench : public Benchmark {
  15. SkString fName;
  16. SkPath fPath1, fPath2;
  17. SkPathOp fOp;
  18. public:
  19. PathOpsBench(const char suffix[], SkPathOp op) : fOp(op) {
  20. fName.printf("pathops_%s", suffix);
  21. fPath1.addOval({-10, -20, 10, 20});
  22. fPath2.addOval({-20, -10, 20, 10});
  23. }
  24. bool isSuitableFor(Backend backend) override {
  25. return backend == kNonRendering_Backend;
  26. }
  27. protected:
  28. const char* onGetName() override {
  29. return fName.c_str();
  30. }
  31. void onDraw(int loops, SkCanvas* canvas) override {
  32. for (int i = 0; i < loops; i++) {
  33. for (int j = 0; j < 1000; ++j) {
  34. SkPath result;
  35. Op(fPath1, fPath2, fOp, &result);
  36. }
  37. }
  38. }
  39. private:
  40. typedef Benchmark INHERITED;
  41. };
  42. class PathOpsSimplifyBench : public Benchmark {
  43. SkString fName;
  44. SkPath fPath;
  45. public:
  46. PathOpsSimplifyBench(const char suffix[], const SkPath& path) : fPath(path) {
  47. fName.printf("pathops_simplify_%s", suffix);
  48. }
  49. bool isSuitableFor(Backend backend) override {
  50. return backend == kNonRendering_Backend;
  51. }
  52. protected:
  53. const char* onGetName() override {
  54. return fName.c_str();
  55. }
  56. void onDraw(int loops, SkCanvas* canvas) override {
  57. for (int i = 0; i < loops; i++) {
  58. for (int j = 0; j < 100; ++j) {
  59. SkPath result;
  60. Simplify(fPath, &result);
  61. }
  62. }
  63. }
  64. private:
  65. typedef Benchmark INHERITED;
  66. };
  67. DEF_BENCH( return new PathOpsBench("sect", kIntersect_SkPathOp); )
  68. DEF_BENCH( return new PathOpsBench("join", kUnion_SkPathOp); )
  69. static SkPath makerects() {
  70. SkRandom rand;
  71. SkPath path;
  72. SkScalar scale = 100;
  73. for (int i = 0; i < 20; ++i) {
  74. SkScalar x = rand.nextUScalar1() * scale;
  75. SkScalar y = rand.nextUScalar1() * scale;
  76. path.addRect({x, y, x + scale, y + scale});
  77. }
  78. return path;
  79. }
  80. DEF_BENCH( return new PathOpsSimplifyBench("rects", makerects()); )