Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 1 | /* |
| 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 Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 10 | #include "bench/Benchmark.h" |
| 11 | |
Brian Osman | 01e6d17 | 2020-03-30 15:57:14 -0400 | [diff] [blame] | 12 | #include "include/core/SkCanvas.h" |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 13 | #include "include/core/SkDeferredDisplayListRecorder.h" |
| 14 | #include "include/core/SkSurfaceCharacterization.h" |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 15 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 16 | |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 17 | static SkSurfaceCharacterization create_characterization(GrDirectContext* direct) { |
| 18 | size_t maxResourceBytes = direct->getResourceCacheLimit(); |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 19 | |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 20 | if (!direct->colorTypeSupportedAsSurface(kRGBA_8888_SkColorType)) { |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 21 | return SkSurfaceCharacterization(); |
| 22 | } |
| 23 | |
| 24 | SkImageInfo ii = SkImageInfo::Make(32, 32, kRGBA_8888_SkColorType, |
| 25 | kPremul_SkAlphaType, nullptr); |
| 26 | |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 27 | GrBackendFormat backendFormat = direct->defaultBackendFormat(kRGBA_8888_SkColorType, |
| 28 | GrRenderable::kYes); |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 29 | if (!backendFormat.isValid()) { |
| 30 | return SkSurfaceCharacterization(); |
| 31 | } |
| 32 | |
| 33 | SkSurfaceProps props(0x0, kUnknown_SkPixelGeometry); |
| 34 | |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 35 | SkSurfaceCharacterization c = direct->threadSafeProxy()->createCharacterization( |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 36 | 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. |
| 44 | class DDLRecorderBench : public Benchmark { |
| 45 | public: |
| 46 | DDLRecorderBench() { } |
| 47 | |
| 48 | protected: |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 49 | bool isSuitableFor(Backend backend) override { return kGPU_Backend == backend; } |
| 50 | |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 51 | 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 | |
| 70 | private: |
| 71 | // We create one DDLRecorder for all the timing runs and just keep reusing it |
| 72 | void onPerCanvasPreDraw(SkCanvas* origCanvas) override { |
Robert Phillips | f028810 | 2020-07-06 13:45:34 -0400 | [diff] [blame] | 73 | auto context = origCanvas->recordingContext()->asDirectContext(); |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 74 | if (!context) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | SkSurfaceCharacterization c = create_characterization(context); |
| 79 | |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 80 | fRecorder = std::make_unique<SkDeferredDisplayListRecorder>(c); |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 81 | } |
| 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 Holler | f19bbb5 | 2020-06-29 10:00:08 -0400 | [diff] [blame] | 89 | std::vector<sk_sp<SkDeferredDisplayList>> fDDLs; |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 90 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 91 | using INHERITED = Benchmark; |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | DEF_BENCH(return new DDLRecorderBench();) |