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