PathIterBench.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2011 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/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColorPriv.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkPath.h"
  13. #include "include/core/SkShader.h"
  14. #include "include/core/SkString.h"
  15. #include "include/utils/SkRandom.h"
  16. static int rand_pts(SkRandom& rand, SkPoint pts[4]) {
  17. int n = rand.nextU() & 3;
  18. n += 1;
  19. for (int i = 0; i < n; ++i) {
  20. pts[i].fX = rand.nextSScalar1();
  21. pts[i].fY = rand.nextSScalar1();
  22. }
  23. return n;
  24. }
  25. class PathIterBench : public Benchmark {
  26. SkString fName;
  27. SkPath fPath;
  28. bool fRaw;
  29. public:
  30. PathIterBench(bool raw) {
  31. fName.printf("pathiter_%s", raw ? "raw" : "consume");
  32. fRaw = raw;
  33. SkRandom rand;
  34. for (int i = 0; i < 1000; ++i) {
  35. SkPoint pts[4];
  36. int n = rand_pts(rand, pts);
  37. switch (n) {
  38. case 1:
  39. fPath.moveTo(pts[0]);
  40. break;
  41. case 2:
  42. fPath.lineTo(pts[1]);
  43. break;
  44. case 3:
  45. fPath.quadTo(pts[1], pts[2]);
  46. break;
  47. case 4:
  48. fPath.cubicTo(pts[1], pts[2], pts[3]);
  49. break;
  50. }
  51. }
  52. }
  53. bool isSuitableFor(Backend backend) override {
  54. return backend == kNonRendering_Backend;
  55. }
  56. protected:
  57. const char* onGetName() override {
  58. return fName.c_str();
  59. }
  60. void onDraw(int loops, SkCanvas*) override {
  61. if (fRaw) {
  62. for (int i = 0; i < loops; ++i) {
  63. SkPath::RawIter iter(fPath);
  64. SkPath::Verb verb;
  65. SkPoint pts[4];
  66. while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
  67. }
  68. } else {
  69. for (int i = 0; i < loops; ++i) {
  70. SkPath::Iter iter(fPath, false);
  71. SkPath::Verb verb;
  72. SkPoint pts[4];
  73. while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
  74. }
  75. }
  76. }
  77. private:
  78. typedef Benchmark INHERITED;
  79. };
  80. ///////////////////////////////////////////////////////////////////////////////
  81. DEF_BENCH( return new PathIterBench(false); )
  82. DEF_BENCH( return new PathIterBench(true); )