ClipStrategyBench.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2017 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/SkPath.h"
  10. #include "include/pathops/SkPathOps.h"
  11. class ClipStrategyBench : public Benchmark {
  12. public:
  13. enum class Mode {
  14. kClipPath,
  15. kMask,
  16. };
  17. ClipStrategyBench(Mode mode, size_t count)
  18. : fMode(mode)
  19. , fCount(count)
  20. , fName("clip_strategy_"){
  21. if (fMode == Mode::kClipPath) {
  22. fName.append("path_");
  23. this->forEachClipCircle([&](float x, float y, float r) {
  24. fClipPath.addCircle(x, y, r);
  25. });
  26. } else {
  27. fName.append("mask_");
  28. }
  29. fName.appendf("%zu", count);
  30. }
  31. ~ClipStrategyBench() override = default;
  32. protected:
  33. const char* onGetName() override {
  34. return fName.c_str();
  35. }
  36. void onDraw(int loops, SkCanvas* canvas) override {
  37. SkPaint p, srcIn;
  38. p.setAntiAlias(true);
  39. srcIn.setBlendMode(SkBlendMode::kSrcIn);
  40. for (int i = 0; i < loops; ++i) {
  41. SkAutoCanvasRestore acr(canvas, false);
  42. if (fMode == Mode::kClipPath) {
  43. canvas->save();
  44. canvas->clipPath(fClipPath, true);
  45. } else {
  46. canvas->saveLayer(nullptr, nullptr);
  47. this->forEachClipCircle([&](float x, float y, float r) {
  48. canvas->drawCircle(x, y, r, p);
  49. });
  50. canvas->saveLayer(nullptr, &srcIn);
  51. }
  52. canvas->drawColor(SK_ColorGREEN);
  53. }
  54. }
  55. private:
  56. template <typename Func>
  57. void forEachClipCircle(Func&& func) {
  58. auto q = static_cast<float>(this->getSize().x()) / (fCount + 1);
  59. for (size_t i = 1; i <= fCount; ++i) {
  60. auto x = q * i;
  61. func(x, x, q / 2);
  62. }
  63. }
  64. Mode fMode;
  65. size_t fCount;
  66. SkString fName;
  67. SkPath fClipPath;
  68. typedef Benchmark INHERITED;
  69. };
  70. DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kClipPath, 1 );)
  71. DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kClipPath, 5 );)
  72. DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kClipPath, 10 );)
  73. DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kClipPath, 100);)
  74. DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kMask, 1 );)
  75. DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kMask, 5 );)
  76. DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kMask, 10 );)
  77. DEF_BENCH( return new ClipStrategyBench(ClipStrategyBench::Mode::kMask, 100);)