blob: 56077e1a491d71398a161eb00a8a35696ae617ff [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
John Stilesfbd050b2020-08-03 13:21:46 -04008#include <memory>
9
Robert Phillipsde4456f2019-09-12 11:17:16 -040010#include "bench/Benchmark.h"
11
Brian Osman01e6d172020-03-30 15:57:14 -040012#include "include/core/SkCanvas.h"
Robert Phillipsde4456f2019-09-12 11:17:16 -040013#include "include/core/SkDeferredDisplayListRecorder.h"
14#include "include/core/SkSurfaceCharacterization.h"
Robert Phillipsf0288102020-07-06 13:45:34 -040015#include "include/gpu/GrDirectContext.h"
Robert Phillipsde4456f2019-09-12 11:17:16 -040016
Robert Phillipsf0288102020-07-06 13:45:34 -040017static SkSurfaceCharacterization create_characterization(GrDirectContext* direct) {
18 size_t maxResourceBytes = direct->getResourceCacheLimit();
Robert Phillipsde4456f2019-09-12 11:17:16 -040019
Robert Phillipsf0288102020-07-06 13:45:34 -040020 if (!direct->colorTypeSupportedAsSurface(kRGBA_8888_SkColorType)) {
Robert Phillipsde4456f2019-09-12 11:17:16 -040021 return SkSurfaceCharacterization();
22 }
23
24 SkImageInfo ii = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType,
25 kPremul_SkAlphaType, nullptr);
26
Robert Phillipsf0288102020-07-06 13:45:34 -040027 GrBackendFormat backendFormat = direct->defaultBackendFormat(kRGBA_8888_SkColorType,
28 GrRenderable::kYes);
Robert Phillipsde4456f2019-09-12 11:17:16 -040029 if (!backendFormat.isValid()) {
30 return SkSurfaceCharacterization();
31 }
32
33 SkSurfaceProps props(0x0, kUnknown_SkPixelGeometry);
34
Robert Phillipsf0288102020-07-06 13:45:34 -040035 SkSurfaceCharacterization c = direct->threadSafeProxy()->createCharacterization(
Robert Phillipsde4456f2019-09-12 11:17:16 -040036 maxResourceBytes, ii, backendFormat, 1,
37 kTopLeft_GrSurfaceOrigin, props, false);
38 return c;
39}
40
41// This benchmark tries to simulate how Viz is using SkDDLRecorders.
42// For each config it will create a single DDLRecorder which it reuses for all the runs
43// For each run it creates a DDL and stores it for later deletion.
44class DDLRecorderBench : public Benchmark {
45public:
46 DDLRecorderBench() { }
47
48protected:
Robert Phillipsf0288102020-07-06 13:45:34 -040049 bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; }
50
Robert Phillipsde4456f2019-09-12 11:17:16 -040051 const char* onGetName() override { return "DDLRecorder"; }
52
53 void onDraw(int loops, SkCanvas* origCanvas) override {
54 if (!fRecorder) {
55 return;
56 }
57
58 SkASSERT(!fDDLs.size());
59 fDDLs.reserve(loops);
60
61 for (int i = 0; i < loops; ++i) {
62 SkCanvas* recordingCanvas = fRecorder->getCanvas();
63
64 recordingCanvas->drawRect(SkRect::MakeWH(32, 32), SkPaint());
65
66 fDDLs.emplace_back(fRecorder->detach());
67 }
68 }
69
70private:
71 // We create one DDLRecorder for all the timing runs and just keep reusing it
72 void onPerCanvasPreDraw(SkCanvas* origCanvas) override {
Robert Phillipsf0288102020-07-06 13:45:34 -040073 auto context = origCanvas->recordingContext()->asDirectContext();
Robert Phillipsde4456f2019-09-12 11:17:16 -040074 if (!context) {
75 return;
76 }
77
78 SkSurfaceCharacterization c = create_characterization(context);
79
John Stilesfbd050b2020-08-03 13:21:46 -040080 fRecorder = std::make_unique<SkDeferredDisplayListRecorder>(c);
Robert Phillipsde4456f2019-09-12 11:17:16 -040081 }
82
83 // We defer the clean up of the DDLs so it is done outside of the timing loop
84 void onPostDraw(SkCanvas*) override {
85 fDDLs.clear();
86 }
87
88 std::unique_ptr<SkDeferredDisplayListRecorder> fRecorder = nullptr;
Adlai Hollerf19bbb52020-06-29 10:00:08 -040089 std::vector<sk_sp<SkDeferredDisplayList>> fDDLs;
Robert Phillipsde4456f2019-09-12 11:17:16 -040090
John Stiles7571f9e2020-09-02 22:42:33 -040091 using INHERITED = Benchmark;
Robert Phillipsde4456f2019-09-12 11:17:16 -040092};
93
94DEF_BENCH(return new DDLRecorderBench();)