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