RectanizerBench.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2014 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/SkSize.h"
  9. #include "include/private/SkTDArray.h"
  10. #include "include/utils/SkRandom.h"
  11. #include "src/gpu/GrRectanizer_pow2.h"
  12. #include "src/gpu/GrRectanizer_skyline.h"
  13. /**
  14. * This bench exercises Ganesh' GrRectanizer classes. It exercises the following
  15. * rectanizers:
  16. * Pow2 Rectanizer
  17. * Skyline Rectanizer
  18. * in the following cases:
  19. * random rects (e.g., pull-save-layers forward use case)
  20. * random power of two rects
  21. * small constant sized power of 2 rects (e.g., glyph cache use case)
  22. */
  23. class RectanizerBench : public Benchmark {
  24. public:
  25. static const int kWidth = 1024;
  26. static const int kHeight = 1024;
  27. enum RectanizerType {
  28. kPow2_RectanizerType,
  29. kSkyline_RectanizerType,
  30. };
  31. enum RectType {
  32. kRand_RectType,
  33. kRandPow2_RectType,
  34. kSmallPow2_RectType
  35. };
  36. RectanizerBench(RectanizerType rectanizerType, RectType rectType)
  37. : fName("rectanizer_")
  38. , fRectanizerType(rectanizerType)
  39. , fRectType(rectType) {
  40. if (kPow2_RectanizerType == fRectanizerType) {
  41. fName.append("pow2_");
  42. } else {
  43. SkASSERT(kSkyline_RectanizerType == fRectanizerType);
  44. fName.append("skyline_");
  45. }
  46. if (kRand_RectType == fRectType) {
  47. fName.append("rand");
  48. } else if (kRandPow2_RectType == fRectType) {
  49. fName.append("rand2");
  50. } else {
  51. SkASSERT(kSmallPow2_RectType == fRectType);
  52. fName.append("sm2");
  53. }
  54. }
  55. protected:
  56. bool isSuitableFor(Backend backend) override {
  57. return kNonRendering_Backend == backend;
  58. }
  59. const char* onGetName() override {
  60. return fName.c_str();
  61. }
  62. void onDelayedSetup() override {
  63. SkASSERT(nullptr == fRectanizer.get());
  64. if (kPow2_RectanizerType == fRectanizerType) {
  65. fRectanizer.reset(new GrRectanizerPow2(kWidth, kHeight));
  66. } else {
  67. SkASSERT(kSkyline_RectanizerType == fRectanizerType);
  68. fRectanizer.reset(new GrRectanizerSkyline(kWidth, kHeight));
  69. }
  70. }
  71. void onDraw(int loops, SkCanvas* canvas) override {
  72. SkRandom rand;
  73. SkIPoint16 loc;
  74. SkISize size;
  75. for (int i = 0; i < loops; ++i) {
  76. if (kRand_RectType == fRectType) {
  77. size = SkISize::Make(rand.nextRangeU(1, kWidth / 2),
  78. rand.nextRangeU(1, kHeight / 2));
  79. } else if (kRandPow2_RectType == fRectType) {
  80. size = SkISize::Make(GrNextPow2(rand.nextRangeU(1, kWidth / 2)),
  81. GrNextPow2(rand.nextRangeU(1, kHeight / 2)));
  82. } else {
  83. SkASSERT(kSmallPow2_RectType == fRectType);
  84. size = SkISize::Make(128, 128);
  85. }
  86. if (!fRectanizer->addRect(size.fWidth, size.fHeight, &loc)) {
  87. // insert failed so clear out the rectanizer and give the
  88. // current rect another try
  89. fRectanizer->reset();
  90. i--;
  91. }
  92. }
  93. fRectanizer->reset();
  94. }
  95. private:
  96. SkString fName;
  97. RectanizerType fRectanizerType;
  98. RectType fRectType;
  99. std::unique_ptr<GrRectanizer> fRectanizer;
  100. typedef Benchmark INHERITED;
  101. };
  102. //////////////////////////////////////////////////////////////////////////////
  103. DEF_BENCH(return new RectanizerBench(RectanizerBench::kPow2_RectanizerType,
  104. RectanizerBench::kRand_RectType);)
  105. DEF_BENCH(return new RectanizerBench(RectanizerBench::kPow2_RectanizerType,
  106. RectanizerBench::kRandPow2_RectType);)
  107. DEF_BENCH(return new RectanizerBench(RectanizerBench::kPow2_RectanizerType,
  108. RectanizerBench::kSmallPow2_RectType);)
  109. DEF_BENCH(return new RectanizerBench(RectanizerBench::kSkyline_RectanizerType,
  110. RectanizerBench::kRand_RectType);)
  111. DEF_BENCH(return new RectanizerBench(RectanizerBench::kSkyline_RectanizerType,
  112. RectanizerBench::kRandPow2_RectType);)
  113. DEF_BENCH(return new RectanizerBench(RectanizerBench::kSkyline_RectanizerType,
  114. RectanizerBench::kSmallPow2_RectType);)