bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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/SkCanvas.h" |
| 10 | #include "include/core/SkImage.h" |
| 11 | #include "include/core/SkSurface.h" |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 12 | #include "include/gpu/GrDirectContext.h" |
Adlai Holler | a069304 | 2020-10-14 11:23:11 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "tools/ToolUtils.h" |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 15 | |
brianosman | 9f1f6e2 | 2016-09-15 08:33:02 -0700 | [diff] [blame] | 16 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 17 | #include <utility> |
| 18 | |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 19 | /** These benchmarks were designed to measure changes to GrResourceCache's replacement policy */ |
| 20 | |
| 21 | ////////////////////////////////////////////////////////////////////////////// |
| 22 | |
| 23 | // The width/height of the images to draw. The small size underestimates the value of a good |
| 24 | // replacement strategy since the texture uploads are quite small. However, the effects are still |
| 25 | // significant and this lets the benchmarks complete a lot faster, especially on mobile. |
| 26 | static constexpr int kS = 25; |
| 27 | |
| 28 | static void make_images(sk_sp<SkImage> imgs[], int cnt) { |
| 29 | for (int i = 0; i < cnt; ++i) { |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 30 | imgs[i] = ToolUtils::create_checkerboard_image(kS, kS, SK_ColorBLACK, SK_ColorCYAN, 10); |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 31 | } |
| 32 | } |
| 33 | |
| 34 | static void draw_image(SkCanvas* canvas, SkImage* img) { |
| 35 | // Make the paint transparent to avoid any issues of deferred tiler blending |
| 36 | // optmizations |
| 37 | SkPaint paint; |
| 38 | paint.setAlpha(0x10); |
| 39 | canvas->drawImage(img, 0, 0, &paint); |
| 40 | } |
| 41 | |
| 42 | void set_cache_budget(SkCanvas* canvas, int approxImagesInBudget) { |
| 43 | // This is inexact but we attempt to figure out a baseline number of resources GrContext needs |
| 44 | // to render an SkImage and add one additional resource for each image we'd like to fit. |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 45 | auto context = canvas->recordingContext()->asDirectContext(); |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 46 | SkASSERT(context); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 47 | context->flushAndSubmit(); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 48 | context->priv().testingOnly_purgeAllUnlockedResources(); |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 49 | sk_sp<SkImage> image; |
| 50 | make_images(&image, 1); |
| 51 | draw_image(canvas, image.get()); |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 52 | context->flushAndSubmit(); |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 53 | int baselineCount; |
| 54 | context->getResourceCacheUsage(&baselineCount, nullptr); |
| 55 | baselineCount -= 1; // for the image's textures. |
| 56 | context->setResourceCacheLimits(baselineCount + approxImagesInBudget, 1 << 30); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 57 | context->priv().testingOnly_purgeAllUnlockedResources(); |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | ////////////////////////////////////////////////////////////////////////////// |
| 61 | |
| 62 | /** |
| 63 | * Tests repeatedly drawing the same set of images in each frame. Different instances of the bench |
| 64 | * run with different cache sizes and either repeat the image order each frame or use a random |
| 65 | * order. Every variation of this bench draws the same image set, only the budget and order of |
| 66 | * images differs. Since the total fill is the same they can be cross-compared. |
| 67 | */ |
| 68 | class ImageCacheBudgetBench : public Benchmark { |
| 69 | public: |
| 70 | /** budgetSize is the number of images that can fit in the cache. 100 images will be drawn. */ |
| 71 | ImageCacheBudgetBench(int budgetSize, bool shuffle) |
| 72 | : fBudgetSize(budgetSize) |
| 73 | , fShuffle(shuffle) |
| 74 | , fIndices(nullptr) { |
| 75 | float imagesOverBudget = float(kImagesToDraw) / budgetSize; |
| 76 | // Make the benchmark name contain the percentage of the budget that is used in each |
| 77 | // simulated frame. |
| 78 | fName.printf("image_cache_budget_%.0f%s", imagesOverBudget * 100, |
| 79 | (shuffle ? "_shuffle" : "")); |
| 80 | } |
| 81 | |
| 82 | bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; } |
| 83 | |
| 84 | protected: |
| 85 | const char* onGetName() override { |
| 86 | return fName.c_str(); |
| 87 | } |
| 88 | |
| 89 | void onPerCanvasPreDraw(SkCanvas* canvas) override { |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 90 | auto context = canvas->recordingContext()->asDirectContext(); |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 91 | SkASSERT(context); |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 92 | fOldBytes = context->getResourceCacheLimit(); |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 93 | set_cache_budget(canvas, fBudgetSize); |
| 94 | make_images(fImages, kImagesToDraw); |
| 95 | if (fShuffle) { |
| 96 | SkRandom random; |
| 97 | fIndices.reset(new int[kSimulatedFrames * kImagesToDraw]); |
| 98 | for (int frame = 0; frame < kSimulatedFrames; ++frame) { |
| 99 | int* base = fIndices.get() + frame * kImagesToDraw; |
| 100 | for (int i = 0; i < kImagesToDraw; ++i) { |
| 101 | base[i] = i; |
| 102 | } |
| 103 | for (int i = 0; i < kImagesToDraw - 1; ++i) { |
| 104 | int other = random.nextULessThan(kImagesToDraw - i) + i; |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 105 | using std::swap; |
| 106 | swap(base[i], base[other]); |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void onPerCanvasPostDraw(SkCanvas* canvas) override { |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 113 | auto context = canvas->recordingContext()->asDirectContext(); |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 114 | SkASSERT(context); |
Robert Phillips | cf39f37 | 2019-09-03 10:29:20 -0400 | [diff] [blame] | 115 | context->setResourceCacheLimit(fOldBytes); |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 116 | for (int i = 0; i < kImagesToDraw; ++i) { |
| 117 | fImages[i].reset(); |
| 118 | } |
| 119 | fIndices.reset(nullptr); |
| 120 | } |
| 121 | |
| 122 | void onDraw(int loops, SkCanvas* canvas) override { |
Robert Phillips | 2c21a11 | 2020-11-20 13:49:37 -0500 | [diff] [blame] | 123 | auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 124 | |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 125 | for (int i = 0; i < loops; ++i) { |
| 126 | for (int frame = 0; frame < kSimulatedFrames; ++frame) { |
| 127 | for (int j = 0; j < kImagesToDraw; ++j) { |
| 128 | int idx; |
| 129 | if (fShuffle) { |
| 130 | idx = fIndices[frame * kImagesToDraw + j]; |
| 131 | } else { |
| 132 | idx = j; |
| 133 | } |
| 134 | draw_image(canvas, fImages[idx].get()); |
| 135 | } |
| 136 | // Simulate a frame boundary by flushing. This should notify GrResourceCache. |
Robert Phillips | 2c21a11 | 2020-11-20 13:49:37 -0500 | [diff] [blame] | 137 | if (dContext) { |
| 138 | dContext->flush(); |
| 139 | } |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private: |
| 145 | static constexpr int kImagesToDraw = 100; |
| 146 | static constexpr int kSimulatedFrames = 5; |
| 147 | |
| 148 | int fBudgetSize; |
| 149 | bool fShuffle; |
| 150 | SkString fName; |
| 151 | sk_sp<SkImage> fImages[kImagesToDraw]; |
Ben Wagner | 7ecc596 | 2016-11-02 17:07:33 -0400 | [diff] [blame] | 152 | std::unique_ptr<int[]> fIndices; |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 153 | size_t fOldBytes; |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 154 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 155 | using INHERITED = Benchmark; |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | DEF_BENCH( return new ImageCacheBudgetBench(105, false); ) |
| 159 | |
| 160 | DEF_BENCH( return new ImageCacheBudgetBench(90, false); ) |
| 161 | |
| 162 | DEF_BENCH( return new ImageCacheBudgetBench(80, false); ) |
| 163 | |
| 164 | DEF_BENCH( return new ImageCacheBudgetBench(50, false); ) |
| 165 | |
| 166 | DEF_BENCH( return new ImageCacheBudgetBench(105, true); ) |
| 167 | |
| 168 | DEF_BENCH( return new ImageCacheBudgetBench(90, true); ) |
| 169 | |
| 170 | DEF_BENCH( return new ImageCacheBudgetBench(80, true); ) |
| 171 | |
| 172 | DEF_BENCH( return new ImageCacheBudgetBench(50, true); ) |
| 173 | |
| 174 | ////////////////////////////////////////////////////////////////////////////// |
| 175 | |
| 176 | /** |
| 177 | * Similar to above but changes between being over and under budget by varying the number of images |
| 178 | * rendered. This is not directly comparable to the non-dynamic benchmarks. |
| 179 | */ |
| 180 | class ImageCacheBudgetDynamicBench : public Benchmark { |
| 181 | public: |
| 182 | enum class Mode { |
| 183 | // Increase from min to max images drawn gradually over simulated frames and then back. |
| 184 | kPingPong, |
| 185 | // Alternate between under and over budget every other simulated frame. |
| 186 | kFlipFlop |
| 187 | }; |
| 188 | |
| 189 | ImageCacheBudgetDynamicBench(Mode mode) : fMode(mode) {} |
| 190 | |
| 191 | bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; } |
| 192 | |
| 193 | protected: |
| 194 | const char* onGetName() override { |
| 195 | switch (fMode) { |
| 196 | case Mode::kPingPong: |
| 197 | return "image_cache_budget_dynamic_ping_pong"; |
| 198 | case Mode::kFlipFlop: |
| 199 | return "image_cache_budget_dynamic_flip_flop"; |
| 200 | } |
| 201 | return ""; |
| 202 | } |
| 203 | |
| 204 | void onPerCanvasPreDraw(SkCanvas* canvas) override { |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 205 | auto context = canvas->recordingContext()->asDirectContext(); |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 206 | SkASSERT(context); |
| 207 | context->getResourceCacheLimits(&fOldCount, &fOldBytes); |
| 208 | make_images(fImages, kMaxImagesToDraw); |
| 209 | set_cache_budget(canvas, kImagesInBudget); |
| 210 | } |
| 211 | |
| 212 | void onPerCanvasPostDraw(SkCanvas* canvas) override { |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 213 | auto context = canvas->recordingContext()->asDirectContext(); |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 214 | SkASSERT(context); |
| 215 | context->setResourceCacheLimits(fOldCount, fOldBytes); |
| 216 | for (int i = 0; i < kMaxImagesToDraw; ++i) { |
| 217 | fImages[i].reset(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | void onDraw(int loops, SkCanvas* canvas) override { |
Robert Phillips | 2c21a11 | 2020-11-20 13:49:37 -0500 | [diff] [blame] | 222 | auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 223 | |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 224 | int delta = 0; |
| 225 | switch (fMode) { |
| 226 | case Mode::kPingPong: |
| 227 | delta = 1; |
| 228 | break; |
| 229 | case Mode::kFlipFlop: |
| 230 | delta = kMaxImagesToDraw - kMinImagesToDraw; |
| 231 | break; |
| 232 | } |
| 233 | for (int i = 0; i < loops; ++i) { |
| 234 | int imgsToDraw = kMinImagesToDraw; |
| 235 | for (int frame = 0; frame < kSimulatedFrames; ++frame) { |
| 236 | for (int j = 0; j < imgsToDraw; ++j) { |
| 237 | draw_image(canvas, fImages[j].get()); |
| 238 | } |
| 239 | imgsToDraw += delta; |
| 240 | if (imgsToDraw > kMaxImagesToDraw || imgsToDraw < kMinImagesToDraw) { |
| 241 | delta = -delta; |
| 242 | imgsToDraw += 2 * delta; |
| 243 | } |
| 244 | // Simulate a frame boundary by flushing. This should notify GrResourceCache. |
Robert Phillips | 2c21a11 | 2020-11-20 13:49:37 -0500 | [diff] [blame] | 245 | if (dContext) { |
| 246 | dContext->flush(); |
| 247 | } |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | private: |
| 253 | static constexpr int kImagesInBudget = 25; |
| 254 | static constexpr int kMinImagesToDraw = 15; |
| 255 | static constexpr int kMaxImagesToDraw = 35; |
| 256 | static constexpr int kSimulatedFrames = 80; |
| 257 | |
| 258 | Mode fMode; |
| 259 | sk_sp<SkImage> fImages[kMaxImagesToDraw]; |
| 260 | size_t fOldBytes; |
| 261 | int fOldCount; |
| 262 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 263 | using INHERITED = Benchmark; |
bsalomon | b3cb214 | 2016-09-08 12:35:32 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | DEF_BENCH( return new ImageCacheBudgetDynamicBench(ImageCacheBudgetDynamicBench::Mode::kPingPong); ) |
| 267 | DEF_BENCH( return new ImageCacheBudgetDynamicBench(ImageCacheBudgetDynamicBench::Mode::kFlipFlop); ) |