blob: 3874987ea98afaf85cfb7172433782e9cfe238d6 [file] [log] [blame]
Brian Osman310178c2021-07-27 13:16:09 -04001/*
2 * Copyright 2021 Google LLC
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
11class CanvasSaveRestoreBench : public Benchmark {
12public:
13 CanvasSaveRestoreBench(int depth) : fDepth(depth) {
14 fName.printf("canvas_save_restore_%d", fDepth);
15 }
16
17protected:
18 const char* onGetName() override { return fName.c_str(); }
19 bool isSuitableFor(Backend backend) override { return backend == kRaster_Backend; }
20 SkIPoint onGetSize() override { return { 1, 1 }; }
21
22 void onDraw(int loops, SkCanvas* canvas) override {
23 SkM44 m = SkM44::Rotate({0, 0, 1}, 1);
24
25 for (int i = 0; i < loops; ++i) {
26 for (int j = 0; j < fDepth; ++j) {
27 canvas->save();
28 canvas->concat(m);
29 }
30 canvas->drawColor(SkColors::kCyan);
31 for (int j = 0; j < fDepth; ++j) {
32 canvas->restore();
33 }
34 }
35 }
36
37private:
38 const int fDepth;
39 SkString fName;
40
41 using INHERITED = Benchmark;
42};
43
44// Performance remains roughly constant up to 32 (the number of preallocated save records).
45// After that, the cost of additional malloc/free calls starts to be measurable.
46DEF_BENCH( return new CanvasSaveRestoreBench(8);)
47DEF_BENCH( return new CanvasSaveRestoreBench(32);)
48DEF_BENCH( return new CanvasSaveRestoreBench(128);)
49DEF_BENCH( return new CanvasSaveRestoreBench(512);)