blob: 5b548c2fbbaae29cf511c70f7414396c6691feef [file] [log] [blame]
Michael Ludwig076238f2018-10-08 16:37:17 -04001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -050013#include "bench/Benchmark.h"
Michael Ludwig1374c852019-01-16 10:22:50 -050014
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#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"
Michael Ludwig076238f2018-10-08 16:37:17 -040020
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrRenderTargetContext.h"
Michael Ludwig1374c852019-01-16 10:22:50 -050022
23static sk_sp<SkShader> make_shader() {
24 static const SkPoint kPts[] = {{0, 0}, {10, 10}};
25 static const SkColor kColors[] = {SK_ColorBLUE, SK_ColorWHITE};
Mike Reedfae8fce2019-04-03 10:27:45 -040026 return SkGradientShader::MakeLinear(kPts, kColors, nullptr, 2, SkTileMode::kClamp);
Michael Ludwig1374c852019-01-16 10:22:50 -050027}
28
Michael Ludwig076238f2018-10-08 16:37:17 -040029class ClearBench : public Benchmark {
30public:
31 enum ClearType {
32 kFull_ClearType,
33 kPartial_ClearType,
34 kComplex_ClearType
35 };
36
37 ClearBench(ClearType type) : fType(type) {}
38
39protected:
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 Ludwig1374c852019-01-16 10:22:50 -050054 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 Ludwig076238f2018-10-08 16:37:17 -040058
Michael Ludwig1374c852019-01-16 10:22:50 -050059 // 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
65 GrRenderTargetContext* rtc = canvas->internal_private_accessTopLayerRenderTargetContext();
66 if (rtc) {
Chris Dalton6b982802019-06-27 13:53:46 -060067 // Tell the GrRenderTargetContext to not reset its draw op list on a fullscreen clear.
68 // If we don't do this, fullscreen clear ops would be created and constantly discard the
69 // previous iteration's op so execution would only invoke one actual clear on the GPU
70 // (not what we want to measure).
71 rtc->testingOnly_SetPreserveOpsOnFullClear();
Michael Ludwig1374c852019-01-16 10:22:50 -050072 }
73
Michael Ludwig076238f2018-10-08 16:37:17 -040074 for (int i = 0; i < loops; i++) {
75 canvas->save();
76 switch(fType) {
77 case kPartial_ClearType:
Michael Ludwig1374c852019-01-16 10:22:50 -050078 canvas->clipRect(kPartialClip);
Michael Ludwig076238f2018-10-08 16:37:17 -040079 break;
80 case kComplex_ClearType:
Michael Ludwig1374c852019-01-16 10:22:50 -050081 canvas->clipRRect(kComplexClip);
Michael Ludwig076238f2018-10-08 16:37:17 -040082 break;
83 case kFull_ClearType:
84 // Don't add any extra clipping, since it defaults to the entire "device"
85 break;
86 }
87
Michael Ludwig1374c852019-01-16 10:22:50 -050088 // The clear we care about measuring
89 canvas->clear(SK_ColorBLUE);
Michael Ludwig076238f2018-10-08 16:37:17 -040090 canvas->restore();
Michael Ludwig1374c852019-01-16 10:22:50 -050091
92 // Perform as minimal a draw as possible that intersects with the clear region in
93 // order to prevent the clear ops from being batched together.
94 canvas->drawRect(kInterruptRect, interruptPaint);
Michael Ludwig076238f2018-10-08 16:37:17 -040095 }
96 }
97
98private:
99 ClearType fType;
100};
101
102DEF_BENCH( return new ClearBench(ClearBench::kFull_ClearType); )
103DEF_BENCH( return new ClearBench(ClearBench::kPartial_ClearType); )
104DEF_BENCH( return new ClearBench(ClearBench::kComplex_ClearType); )