blob: 0f0bf216d7ec5b4a56ff2e025e793088abd65707 [file] [log] [blame]
Robert Phillipsde4456f2019-09-12 11:17:16 -04001/*
2 * Copyright 2019 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
Brian Osman01e6d172020-03-30 15:57:14 -040010#include "include/core/SkCanvas.h"
Robert Phillipsde4456f2019-09-12 11:17:16 -040011#include "include/core/SkDeferredDisplayListRecorder.h"
12#include "include/core/SkSurfaceCharacterization.h"
13
14static SkSurfaceCharacterization create_characterization(GrContext* context) {
15 size_t maxResourceBytes = context->getResourceCacheLimit();
16
17 if (!context->colorTypeSupportedAsSurface(kRGBA_8888_SkColorType)) {
18 return SkSurfaceCharacterization();
19 }
20
21 SkImageInfo ii = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType,
22 kPremul_SkAlphaType, nullptr);
23
24 GrBackendFormat backendFormat = context->defaultBackendFormat(kRGBA_8888_SkColorType,
25 GrRenderable::kYes);
26 if (!backendFormat.isValid()) {
27 return SkSurfaceCharacterization();
28 }
29
30 SkSurfaceProps props(0x0, kUnknown_SkPixelGeometry);
31
32 SkSurfaceCharacterization c = context->threadSafeProxy()->createCharacterization(
33 maxResourceBytes, ii, backendFormat, 1,
34 kTopLeft_GrSurfaceOrigin, props, false);
35 return c;
36}
37
38// This benchmark tries to simulate how Viz is using SkDDLRecorders.
39// For each config it will create a single DDLRecorder which it reuses for all the runs
40// For each run it creates a DDL and stores it for later deletion.
41class DDLRecorderBench : public Benchmark {
42public:
43 DDLRecorderBench() { }
44
45protected:
46 const char* onGetName() override { return "DDLRecorder"; }
47
48 void onDraw(int loops, SkCanvas* origCanvas) override {
49 if (!fRecorder) {
50 return;
51 }
52
53 SkASSERT(!fDDLs.size());
54 fDDLs.reserve(loops);
55
56 for (int i = 0; i < loops; ++i) {
57 SkCanvas* recordingCanvas = fRecorder->getCanvas();
58
59 recordingCanvas->drawRect(SkRect::MakeWH(32, 32), SkPaint());
60
61 fDDLs.emplace_back(fRecorder->detach());
62 }
63 }
64
65private:
66 // We create one DDLRecorder for all the timing runs and just keep reusing it
67 void onPerCanvasPreDraw(SkCanvas* origCanvas) override {
68 GrContext* context = origCanvas->getGrContext();
69 if (!context) {
70 return;
71 }
72
73 SkSurfaceCharacterization c = create_characterization(context);
74
75 fRecorder.reset(new SkDeferredDisplayListRecorder(c));
76 }
77
78 // We defer the clean up of the DDLs so it is done outside of the timing loop
79 void onPostDraw(SkCanvas*) override {
80 fDDLs.clear();
81 }
82
83 std::unique_ptr<SkDeferredDisplayListRecorder> fRecorder = nullptr;
Adlai Hollerf19bbb52020-06-29 10:00:08 -040084 std::vector<sk_sp<SkDeferredDisplayList>> fDDLs;
Robert Phillipsde4456f2019-09-12 11:17:16 -040085
86 typedef Benchmark INHERITED;
87};
88
89DEF_BENCH(return new DDLRecorderBench();)