RegionBench.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/SkRegion.h"
  9. #include "include/core/SkString.h"
  10. #include "include/utils/SkRandom.h"
  11. static bool union_proc(SkRegion& a, SkRegion& b) {
  12. SkRegion result;
  13. return result.op(a, b, SkRegion::kUnion_Op);
  14. }
  15. static bool sect_proc(SkRegion& a, SkRegion& b) {
  16. SkRegion result;
  17. return result.op(a, b, SkRegion::kIntersect_Op);
  18. }
  19. static bool diff_proc(SkRegion& a, SkRegion& b) {
  20. SkRegion result;
  21. return result.op(a, b, SkRegion::kDifference_Op);
  22. }
  23. static bool diffrect_proc(SkRegion& a, SkRegion& b) {
  24. SkRegion result;
  25. return result.op(a, b.getBounds(), SkRegion::kDifference_Op);
  26. }
  27. static bool diffrectbig_proc(SkRegion& a, SkRegion& b) {
  28. SkRegion result;
  29. return result.op(a, a.getBounds(), SkRegion::kDifference_Op);
  30. }
  31. static bool containsrect_proc(SkRegion& a, SkRegion& b) {
  32. SkIRect r = a.getBounds();
  33. r.inset(r.width()/4, r.height()/4);
  34. (void)a.contains(r);
  35. r = b.getBounds();
  36. r.inset(r.width()/4, r.height()/4);
  37. return b.contains(r);
  38. }
  39. static bool sectsrgn_proc(SkRegion& a, SkRegion& b) {
  40. return a.intersects(b);
  41. }
  42. static bool sectsrect_proc(SkRegion& a, SkRegion& b) {
  43. SkIRect r = a.getBounds();
  44. r.inset(r.width()/4, r.height()/4);
  45. return a.intersects(r);
  46. }
  47. static bool containsxy_proc(SkRegion& a, SkRegion& b) {
  48. const SkIRect& r = a.getBounds();
  49. const int dx = r.width() / 8;
  50. const int dy = r.height() / 8;
  51. for (int y = r.fTop; y < r.fBottom; y += dy) {
  52. for (int x = r.fLeft; x < r.fRight; x += dx) {
  53. (void)a.contains(x, y);
  54. }
  55. }
  56. return true;
  57. }
  58. class RegionBench : public Benchmark {
  59. public:
  60. typedef bool (*Proc)(SkRegion& a, SkRegion& b);
  61. SkRegion fA, fB;
  62. Proc fProc;
  63. SkString fName;
  64. enum {
  65. W = 1024,
  66. H = 768,
  67. };
  68. SkIRect randrect(SkRandom& rand) {
  69. int x = rand.nextU() % W;
  70. int y = rand.nextU() % H;
  71. int w = rand.nextU() % W;
  72. int h = rand.nextU() % H;
  73. return SkIRect::MakeXYWH(x, y, w >> 1, h >> 1);
  74. }
  75. RegionBench(int count, Proc proc, const char name[]) {
  76. fProc = proc;
  77. fName.printf("region_%s_%d", name, count);
  78. SkRandom rand;
  79. for (int i = 0; i < count; i++) {
  80. fA.op(randrect(rand), SkRegion::kXOR_Op);
  81. fB.op(randrect(rand), SkRegion::kXOR_Op);
  82. }
  83. }
  84. bool isSuitableFor(Backend backend) override {
  85. return backend == kNonRendering_Backend;
  86. }
  87. protected:
  88. const char* onGetName() override { return fName.c_str(); }
  89. void onDraw(int loops, SkCanvas* canvas) override {
  90. Proc proc = fProc;
  91. for (int i = 0; i < loops; ++i) {
  92. proc(fA, fB);
  93. }
  94. }
  95. private:
  96. typedef Benchmark INHERITED;
  97. };
  98. ///////////////////////////////////////////////////////////////////////////////
  99. #define SMALL 16
  100. DEF_BENCH(return new RegionBench(SMALL, union_proc, "union");)
  101. DEF_BENCH(return new RegionBench(SMALL, sect_proc, "intersect");)
  102. DEF_BENCH(return new RegionBench(SMALL, diff_proc, "difference");)
  103. DEF_BENCH(return new RegionBench(SMALL, diffrect_proc, "differencerect");)
  104. DEF_BENCH(return new RegionBench(SMALL, diffrectbig_proc, "differencerectbig");)
  105. DEF_BENCH(return new RegionBench(SMALL, containsrect_proc, "containsrect");)
  106. DEF_BENCH(return new RegionBench(SMALL, sectsrgn_proc, "intersectsrgn");)
  107. DEF_BENCH(return new RegionBench(SMALL, sectsrect_proc, "intersectsrect");)
  108. DEF_BENCH(return new RegionBench(SMALL, containsxy_proc, "containsxy");)