xref: /aosp_15_r20/external/skia/bench/ClearBench.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
13 #include "bench/Benchmark.h"
14 
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"
20 #include "src/core/SkCanvasPriv.h"
21 #include "src/gpu/ganesh/GrCanvas.h"
22 #include "src/gpu/ganesh/SurfaceDrawContext.h"
23 
make_shader()24 static sk_sp<SkShader> make_shader() {
25     static const SkPoint kPts[] = {{0, 0}, {10, 10}};
26     static const SkColor kColors[] = {SK_ColorBLUE, SK_ColorWHITE};
27     return SkGradientShader::MakeLinear(kPts, kColors, nullptr, 2, SkTileMode::kClamp);
28 }
29 
30 class ClearBench : public Benchmark {
31 public:
32     enum ClearType {
33         kFull_ClearType,
34         kPartial_ClearType,
35         kComplex_ClearType
36     };
37 
ClearBench(ClearType type)38     ClearBench(ClearType type) : fType(type) {}
39 
40 protected:
onGetName()41     const char* onGetName() override {
42         switch(fType) {
43         case kFull_ClearType:
44             return "Clear-Full";
45         case kPartial_ClearType:
46             return "Clear-Partial";
47         case kComplex_ClearType:
48             return "Clear-Complex";
49         }
50         SkASSERT(false);
51         return "Unreachable";
52     }
53 
onDraw(int loops,SkCanvas * canvas)54     void onDraw(int loops, SkCanvas* canvas) override {
55         static const SkRect kPartialClip = SkRect::MakeLTRB(50, 50, 400, 400);
56         static const SkRRect kComplexClip = SkRRect::MakeRectXY(kPartialClip, 15, 15);
57         // Small to limit fill cost, but intersects the clips to confound batching
58         static const SkRect kInterruptRect = SkRect::MakeXYWH(200, 200, 3, 3);
59 
60         // For the draw that sits between consecutive clears, use a shader that is simple but
61         // requires local coordinates so that Ganesh does not convert it into a solid color rect,
62         // which could then turn into a scissored-clear behind the scenes.
63         SkPaint interruptPaint;
64         interruptPaint.setShader(make_shader());
65 
66         auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas);
67         if (sdc) {
68             // Tell the skgpu::ganesh::SurfaceDrawContext to not reset its draw op list on a
69             // fullscreen clear.
70             // If we don't do this, fullscreen clear ops would be created and constantly discard the
71             // previous iteration's op so execution would only invoke one actual clear on the GPU
72             // (not what we want to measure).
73             sdc->testingOnly_SetPreserveOpsOnFullClear();
74         }
75 
76         for (int i = 0; i < loops; i++) {
77             canvas->save();
78             switch(fType) {
79                 case kPartial_ClearType:
80                     canvas->clipRect(kPartialClip);
81                     break;
82                 case kComplex_ClearType:
83                     canvas->clipRRect(kComplexClip);
84                     break;
85                 case kFull_ClearType:
86                     // Don't add any extra clipping, since it defaults to the entire "device"
87                     break;
88             }
89 
90             // The clear we care about measuring
91             canvas->clear(SK_ColorBLUE);
92             canvas->restore();
93 
94             // Perform as minimal a draw as possible that intersects with the clear region in
95             // order to prevent the clear ops from being batched together.
96             canvas->drawRect(kInterruptRect, interruptPaint);
97         }
98     }
99 
100 private:
101     ClearType fType;
102 };
103 
104 DEF_BENCH( return new ClearBench(ClearBench::kFull_ClearType); )
105 DEF_BENCH( return new ClearBench(ClearBench::kPartial_ClearType); )
106 DEF_BENCH( return new ClearBench(ClearBench::kComplex_ClearType); )
107