Michael Ludwig | 076238f | 2018-10-08 16:37:17 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 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 | // This benchmark attempts to measure the time to do a fullscreen clear, an axis-aligned partial |
| 9 | // clear, and a clear restricted to an axis-aligned rounded rect. The fullscreen and axis-aligned |
| 10 | // partial clears on the GPU should follow a fast path that maps to backend-specialized clear |
| 11 | // operations, whereas the rounded-rect clear cannot be. |
| 12 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "bench/Benchmark.h" |
Michael Ludwig | 1374c85 | 2019-01-16 10:22:50 -0500 | [diff] [blame] | 14 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkCanvas.h" |
| 16 | #include "include/core/SkPaint.h" |
| 17 | #include "include/core/SkRRect.h" |
| 18 | #include "include/core/SkRect.h" |
| 19 | #include "include/effects/SkGradientShader.h" |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 20 | #include "src/core/SkCanvasPriv.h" |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 21 | #include "src/gpu/v1/SurfaceDrawContext_v1.h" |
Michael Ludwig | 1374c85 | 2019-01-16 10:22:50 -0500 | [diff] [blame] | 22 | |
| 23 | static sk_sp<SkShader> make_shader() { |
| 24 | static const SkPoint kPts[] = {{0, 0}, {10, 10}}; |
| 25 | static const SkColor kColors[] = {SK_ColorBLUE, SK_ColorWHITE}; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 26 | return SkGradientShader::MakeLinear(kPts, kColors, nullptr, 2, SkTileMode::kClamp); |
Michael Ludwig | 1374c85 | 2019-01-16 10:22:50 -0500 | [diff] [blame] | 27 | } |
| 28 | |
Michael Ludwig | 076238f | 2018-10-08 16:37:17 -0400 | [diff] [blame] | 29 | class ClearBench : public Benchmark { |
| 30 | public: |
| 31 | enum ClearType { |
| 32 | kFull_ClearType, |
| 33 | kPartial_ClearType, |
| 34 | kComplex_ClearType |
| 35 | }; |
| 36 | |
| 37 | ClearBench(ClearType type) : fType(type) {} |
| 38 | |
| 39 | protected: |
| 40 | const char* onGetName() override { |
| 41 | switch(fType) { |
| 42 | case kFull_ClearType: |
| 43 | return "Clear-Full"; |
| 44 | case kPartial_ClearType: |
| 45 | return "Clear-Partial"; |
| 46 | case kComplex_ClearType: |
| 47 | return "Clear-Complex"; |
| 48 | } |
| 49 | SkASSERT(false); |
| 50 | return "Unreachable"; |
| 51 | } |
| 52 | |
| 53 | void onDraw(int loops, SkCanvas* canvas) override { |
Michael Ludwig | 1374c85 | 2019-01-16 10:22:50 -0500 | [diff] [blame] | 54 | static const SkRect kPartialClip = SkRect::MakeLTRB(50, 50, 400, 400); |
| 55 | static const SkRRect kComplexClip = SkRRect::MakeRectXY(kPartialClip, 15, 15); |
| 56 | // Small to limit fill cost, but intersects the clips to confound batching |
| 57 | static const SkRect kInterruptRect = SkRect::MakeXYWH(200, 200, 3, 3); |
Michael Ludwig | 076238f | 2018-10-08 16:37:17 -0400 | [diff] [blame] | 58 | |
Michael Ludwig | 1374c85 | 2019-01-16 10:22:50 -0500 | [diff] [blame] | 59 | // For the draw that sits between consecutive clears, use a shader that is simple but |
| 60 | // requires local coordinates so that Ganesh does not convert it into a solid color rect, |
| 61 | // which could then turn into a scissored-clear behind the scenes. |
| 62 | SkPaint interruptPaint; |
| 63 | interruptPaint.setShader(make_shader()); |
| 64 | |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 65 | auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas); |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 66 | if (sdc) { |
Robert Phillips | 4dca831 | 2021-07-28 15:13:20 -0400 | [diff] [blame] | 67 | // Tell the skgpu::v1::SurfaceDrawContext to not reset its draw op list on a |
| 68 | // fullscreen clear. |
Chris Dalton | 6b98280 | 2019-06-27 13:53:46 -0600 | [diff] [blame] | 69 | // If we don't do this, fullscreen clear ops would be created and constantly discard the |
| 70 | // previous iteration's op so execution would only invoke one actual clear on the GPU |
| 71 | // (not what we want to measure). |
Brian Salomon | 8f7d953 | 2020-12-23 09:16:59 -0500 | [diff] [blame] | 72 | sdc->testingOnly_SetPreserveOpsOnFullClear(); |
Michael Ludwig | 1374c85 | 2019-01-16 10:22:50 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Michael Ludwig | 076238f | 2018-10-08 16:37:17 -0400 | [diff] [blame] | 75 | for (int i = 0; i < loops; i++) { |
| 76 | canvas->save(); |
| 77 | switch(fType) { |
| 78 | case kPartial_ClearType: |
Michael Ludwig | 1374c85 | 2019-01-16 10:22:50 -0500 | [diff] [blame] | 79 | canvas->clipRect(kPartialClip); |
Michael Ludwig | 076238f | 2018-10-08 16:37:17 -0400 | [diff] [blame] | 80 | break; |
| 81 | case kComplex_ClearType: |
Michael Ludwig | 1374c85 | 2019-01-16 10:22:50 -0500 | [diff] [blame] | 82 | canvas->clipRRect(kComplexClip); |
Michael Ludwig | 076238f | 2018-10-08 16:37:17 -0400 | [diff] [blame] | 83 | break; |
| 84 | case kFull_ClearType: |
| 85 | // Don't add any extra clipping, since it defaults to the entire "device" |
| 86 | break; |
| 87 | } |
| 88 | |
Michael Ludwig | 1374c85 | 2019-01-16 10:22:50 -0500 | [diff] [blame] | 89 | // The clear we care about measuring |
| 90 | canvas->clear(SK_ColorBLUE); |
Michael Ludwig | 076238f | 2018-10-08 16:37:17 -0400 | [diff] [blame] | 91 | canvas->restore(); |
Michael Ludwig | 1374c85 | 2019-01-16 10:22:50 -0500 | [diff] [blame] | 92 | |
| 93 | // Perform as minimal a draw as possible that intersects with the clear region in |
| 94 | // order to prevent the clear ops from being batched together. |
| 95 | canvas->drawRect(kInterruptRect, interruptPaint); |
Michael Ludwig | 076238f | 2018-10-08 16:37:17 -0400 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | ClearType fType; |
| 101 | }; |
| 102 | |
| 103 | DEF_BENCH( return new ClearBench(ClearBench::kFull_ClearType); ) |
| 104 | DEF_BENCH( return new ClearBench(ClearBench::kPartial_ClearType); ) |
| 105 | DEF_BENCH( return new ClearBench(ClearBench::kComplex_ClearType); ) |