Flutter Engine
The Flutter Engine
ClearBench.cpp
Go to the documentation of this file.
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
18#include "include/core/SkRect.h"
23
25 static const SkPoint kPts[] = {{0, 0}, {10, 10}};
26 static const SkColor kColors[] = {SK_ColorBLUE, SK_ColorWHITE};
28}
29
30class ClearBench : public Benchmark {
31public:
32 enum ClearType {
36 };
37
39
40protected:
41 const char* onGetName() override {
42 switch(fType) {
43 case kFull_ClearType:
44 return "Clear-Full";
46 return "Clear-Partial";
48 return "Clear-Complex";
49 }
50 SkASSERT(false);
51 return "Unreachable";
52 }
53
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
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) {
80 canvas->clipRect(kPartialClip);
81 break;
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
100private:
101 ClearType fType;
102};
103
#define DEF_BENCH(code)
Definition: Benchmark.h:20
static sk_sp< SkShader > make_shader()
Definition: ClearBench.cpp:24
#define SkASSERT(cond)
Definition: SkAssert.h:116
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
GLenum type
@ kPartial_ClearType
Definition: ClearBench.cpp:34
@ kComplex_ClearType
Definition: ClearBench.cpp:35
const char * onGetName() override
Definition: ClearBench.cpp:41
void onDraw(int loops, SkCanvas *canvas) override
Definition: ClearBench.cpp:54
ClearBench(ClearType type)
Definition: ClearBench.cpp:38
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
void clear(SkColor color)
Definition: SkCanvas.h:1199
int save()
Definition: SkCanvas.cpp:447
void clipRRect(const SkRRect &rrect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1439
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
void setShader(sk_sp< SkShader > shader)
static SkRRect MakeRectXY(const SkRect &rect, SkScalar xRad, SkScalar yRad)
Definition: SkRRect.h:180
const DlColor kColors[]
SurfaceDrawContext * TopDeviceSurfaceDrawContext(const SkCanvas *canvas)
Definition: GrCanvas.cpp:20
static constexpr SkPoint kPts[kPtsCount]
Definition: emptypath.cpp:123
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646