BigPathBench.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/SkCanvas.h"
  9. #include "include/core/SkPath.h"
  10. #include "tools/ToolUtils.h"
  11. enum Align {
  12. kLeft_Align,
  13. kMiddle_Align,
  14. kRight_Align
  15. };
  16. const char* gAlignName[] = { "left", "middle", "right" };
  17. // Inspired by crbug.com/455429
  18. class BigPathBench : public Benchmark {
  19. SkPath fPath;
  20. SkString fName;
  21. Align fAlign;
  22. bool fRound;
  23. public:
  24. BigPathBench(Align align, bool round) : fAlign(align), fRound(round) {
  25. fName.printf("bigpath_%s", gAlignName[fAlign]);
  26. if (round) {
  27. fName.append("_round");
  28. }
  29. }
  30. protected:
  31. const char* onGetName() override {
  32. return fName.c_str();
  33. }
  34. SkIPoint onGetSize() override {
  35. return SkIPoint::Make(640, 100);
  36. }
  37. void onDelayedSetup() override { ToolUtils::make_big_path(fPath); }
  38. void onDraw(int loops, SkCanvas* canvas) override {
  39. SkPaint paint;
  40. paint.setAntiAlias(true);
  41. paint.setStyle(SkPaint::kStroke_Style);
  42. paint.setStrokeWidth(2);
  43. if (fRound) {
  44. paint.setStrokeJoin(SkPaint::kRound_Join);
  45. }
  46. this->setupPaint(&paint);
  47. const SkRect r = fPath.getBounds();
  48. switch (fAlign) {
  49. case kLeft_Align:
  50. canvas->translate(-r.left(), 0);
  51. break;
  52. case kMiddle_Align:
  53. break;
  54. case kRight_Align:
  55. canvas->translate(640 - r.right(), 0);
  56. break;
  57. }
  58. for (int i = 0; i < loops; i++) {
  59. canvas->drawPath(fPath, paint);
  60. }
  61. }
  62. private:
  63. typedef Benchmark INHERITED;
  64. };
  65. DEF_BENCH( return new BigPathBench(kLeft_Align, false); )
  66. DEF_BENCH( return new BigPathBench(kMiddle_Align, false); )
  67. DEF_BENCH( return new BigPathBench(kRight_Align, false); )
  68. DEF_BENCH( return new BigPathBench(kLeft_Align, true); )
  69. DEF_BENCH( return new BigPathBench(kMiddle_Align, true); )
  70. DEF_BENCH( return new BigPathBench(kRight_Align, true); )