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 | |
| 8 | #include "bench/Benchmark.h" |
| 9 | |
Brian Osman | 01e6d17 | 2020-03-30 15:57:14 -0400 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
Robert Phillips | de4456f | 2019-09-12 11:17:16 -0400 | [diff] [blame] | 11 | #include "include/core/SkDeferredDisplayListRecorder.h" |
| 12 | #include "include/core/SkSurfaceCharacterization.h" |
| 13 | |
| 14 | static 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. |
| 41 | class DDLRecorderBench : public Benchmark { |
| 42 | public: |
| 43 | DDLRecorderBench() { } |
| 44 | |
| 45 | protected: |
| 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 | |
| 65 | private: |
| 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; |
| 84 | std::vector<std::unique_ptr<SkDeferredDisplayList>> fDDLs; |
| 85 | |
| 86 | typedef Benchmark INHERITED; |
| 87 | }; |
| 88 | |
| 89 | DEF_BENCH(return new DDLRecorderBench();) |