skia2/bench/CanvasSaveRestoreBench.cpp
Brian Osman 310178c7b7 Microbench for canvas save/restore calls
Change-Id: I0dbf1af17c3a7ec70437e0479cd8b6b6269d155c
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/433476
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2021-07-27 21:08:11 +00:00

50 lines
1.5 KiB
C++

/*
* Copyright 2021 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "bench/Benchmark.h"
#include "include/core/SkCanvas.h"
class CanvasSaveRestoreBench : public Benchmark {
public:
CanvasSaveRestoreBench(int depth) : fDepth(depth) {
fName.printf("canvas_save_restore_%d", fDepth);
}
protected:
const char* onGetName() override { return fName.c_str(); }
bool isSuitableFor(Backend backend) override { return backend == kRaster_Backend; }
SkIPoint onGetSize() override { return { 1, 1 }; }
void onDraw(int loops, SkCanvas* canvas) override {
SkM44 m = SkM44::Rotate({0, 0, 1}, 1);
for (int i = 0; i < loops; ++i) {
for (int j = 0; j < fDepth; ++j) {
canvas->save();
canvas->concat(m);
}
canvas->drawColor(SkColors::kCyan);
for (int j = 0; j < fDepth; ++j) {
canvas->restore();
}
}
}
private:
const int fDepth;
SkString fName;
using INHERITED = Benchmark;
};
// Performance remains roughly constant up to 32 (the number of preallocated save records).
// After that, the cost of additional malloc/free calls starts to be measurable.
DEF_BENCH( return new CanvasSaveRestoreBench(8);)
DEF_BENCH( return new CanvasSaveRestoreBench(32);)
DEF_BENCH( return new CanvasSaveRestoreBench(128);)
DEF_BENCH( return new CanvasSaveRestoreBench(512);)