blob: 20e4d417e9fa3daa445f284923c95030ed783af4 [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"
Robert Phillipsf0288102020-07-06 13:45:34 -040013#include "include/gpu/GrDirectContext.h"
Robert Phillipsde4456f2019-09-12 11:17:16 -040014
Robert Phillipsf0288102020-07-06 13:45:34 -040015static SkSurfaceCharacterization create_characterization(GrDirectContext* direct) {
16 size_t maxResourceBytes = direct->getResourceCacheLimit();
Robert Phillipsde4456f2019-09-12 11:17:16 -040017
Robert Phillipsf0288102020-07-06 13:45:34 -040018 if (!direct->colorTypeSupportedAsSurface(kRGBA_8888_SkColorType)) {
Robert Phillipsde4456f2019-09-12 11:17:16 -040019 return SkSurfaceCharacterization();
20 }
21
22 SkImageInfo ii = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType,
23 kPremul_SkAlphaType, nullptr);
24
Robert Phillipsf0288102020-07-06 13:45:34 -040025 GrBackendFormat backendFormat = direct->defaultBackendFormat(kRGBA_8888_SkColorType,
26 GrRenderable::kYes);
Robert Phillipsde4456f2019-09-12 11:17:16 -040027 if (!backendFormat.isValid()) {
28 return SkSurfaceCharacterization();
29 }
30
31 SkSurfaceProps props(0x0, kUnknown_SkPixelGeometry);
32
Robert Phillipsf0288102020-07-06 13:45:34 -040033 SkSurfaceCharacterization c = direct->threadSafeProxy()->createCharacterization(
Robert Phillipsde4456f2019-09-12 11:17:16 -040034 maxResourceBytes, ii, backendFormat, 1,
35 kTopLeft_GrSurfaceOrigin, props, false);
36 return c;
37}
38
39// This benchmark tries to simulate how Viz is using SkDDLRecorders.
40// For each config it will create a single DDLRecorder which it reuses for all the runs
41// For each run it creates a DDL and stores it for later deletion.
42class DDLRecorderBench : public Benchmark {
43public:
44 DDLRecorderBench() { }
45
46protected:
Robert Phillipsf0288102020-07-06 13:45:34 -040047 bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; }
48
Robert Phillipsde4456f2019-09-12 11:17:16 -040049 const char* onGetName() override { return "DDLRecorder"; }
50
51 void onDraw(int loops, SkCanvas* origCanvas) override {
52 if (!fRecorder) {
53 return;
54 }
55
56 SkASSERT(!fDDLs.size());
57 fDDLs.reserve(loops);
58
59 for (int i = 0; i < loops; ++i) {
60 SkCanvas* recordingCanvas = fRecorder->getCanvas();
61
62 recordingCanvas->drawRect(SkRect::MakeWH(32, 32), SkPaint());
63
64 fDDLs.emplace_back(fRecorder->detach());
65 }
66 }
67
68private:
69 // We create one DDLRecorder for all the timing runs and just keep reusing it
70 void onPerCanvasPreDraw(SkCanvas* origCanvas) override {
Robert Phillipsf0288102020-07-06 13:45:34 -040071 auto context = origCanvas->recordingContext()->asDirectContext();
Robert Phillipsde4456f2019-09-12 11:17:16 -040072 if (!context) {
73 return;
74 }
75
76 SkSurfaceCharacterization c = create_characterization(context);
77
78 fRecorder.reset(new SkDeferredDisplayListRecorder(c));
79 }
80
81 // We defer the clean up of the DDLs so it is done outside of the timing loop
82 void onPostDraw(SkCanvas*) override {
83 fDDLs.clear();
84 }
85
86 std::unique_ptr<SkDeferredDisplayListRecorder> fRecorder = nullptr;
Adlai Hollerf19bbb52020-06-29 10:00:08 -040087 std::vector<sk_sp<SkDeferredDisplayList>> fDDLs;
Robert Phillipsde4456f2019-09-12 11:17:16 -040088
89 typedef Benchmark INHERITED;
90};
91
92DEF_BENCH(return new DDLRecorderBench();)