StrokeBench.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright 2015 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/SkPaint.h"
  9. #include "include/core/SkPath.h"
  10. #include "include/core/SkString.h"
  11. #include "include/utils/SkRandom.h"
  12. class StrokeBench : public Benchmark {
  13. public:
  14. StrokeBench(const SkPath& path, const SkPaint& paint, const char pathType[], SkScalar res)
  15. : fPath(path), fPaint(paint), fRes(res)
  16. {
  17. fName.printf("build_stroke_%s_%g_%d_%d",
  18. pathType, paint.getStrokeWidth(), paint.getStrokeJoin(), paint.getStrokeCap());
  19. }
  20. protected:
  21. bool isSuitableFor(Backend backend) override {
  22. return backend == kNonRendering_Backend;
  23. }
  24. const char* onGetName() override { return fName.c_str(); }
  25. void onDraw(int loops, SkCanvas* canvas) override {
  26. SkPaint paint(fPaint);
  27. this->setupPaint(&paint);
  28. for (int outer = 0; outer < 10; ++outer) {
  29. for (int i = 0; i < loops; ++i) {
  30. SkPath result;
  31. paint.getFillPath(fPath, &result, nullptr, fRes);
  32. }
  33. }
  34. }
  35. private:
  36. SkPath fPath;
  37. SkPaint fPaint;
  38. SkString fName;
  39. SkScalar fRes;
  40. typedef Benchmark INHERITED;
  41. };
  42. ///////////////////////////////////////////////////////////////////////////////
  43. static const int N = 100;
  44. static const SkScalar X = 100;
  45. static const SkScalar Y = 100;
  46. static SkPoint rand_pt(SkRandom& rand) {
  47. return SkPoint::Make(rand.nextSScalar1() * X, rand.nextSScalar1() * Y);
  48. }
  49. static SkPath line_path_maker() {
  50. SkPath path;
  51. SkRandom rand;
  52. path.moveTo(rand_pt(rand));
  53. for (int i = 0; i < N; ++i) {
  54. path.lineTo(rand_pt(rand));
  55. }
  56. return path;
  57. }
  58. static SkPath quad_path_maker() {
  59. SkPath path;
  60. SkRandom rand;
  61. path.moveTo(rand_pt(rand));
  62. for (int i = 0; i < N; ++i) {
  63. path.quadTo(rand_pt(rand), rand_pt(rand));
  64. }
  65. return path;
  66. }
  67. static SkPath conic_path_maker() {
  68. SkPath path;
  69. SkRandom rand;
  70. path.moveTo(rand_pt(rand));
  71. for (int i = 0; i < N; ++i) {
  72. path.conicTo(rand_pt(rand), rand_pt(rand), rand.nextUScalar1());
  73. }
  74. return path;
  75. }
  76. static SkPath cubic_path_maker() {
  77. SkPath path;
  78. SkRandom rand;
  79. path.moveTo(rand_pt(rand));
  80. for (int i = 0; i < N; ++i) {
  81. path.cubicTo(rand_pt(rand), rand_pt(rand), rand_pt(rand));
  82. }
  83. return path;
  84. }
  85. static SkPaint paint_maker() {
  86. SkPaint paint;
  87. paint.setStyle(SkPaint::kStroke_Style);
  88. paint.setStrokeWidth(X / 10);
  89. paint.setStrokeJoin(SkPaint::kMiter_Join);
  90. paint.setStrokeCap(SkPaint::kSquare_Cap);
  91. return paint;
  92. }
  93. DEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_1", 1);)
  94. DEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_1", 1);)
  95. DEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_1", 1);)
  96. DEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_1", 1);)
  97. DEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_4", 4);)
  98. DEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_4", 4);)
  99. DEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_4", 4);)
  100. DEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_4", 4);)
  101. DEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_.25", .25f);)
  102. DEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_.25", .25f);)
  103. DEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_.25", .25f);)
  104. DEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_.25", .25f);)