Debian Codegraph @

bench/AlternatingColorPatternBench.cpp

raw
1 /*
2 * Copyright 2014 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 #include "bench/Benchmark.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkPathBuilder.h"
14 #include "include/core/SkString.h"
15 #include "include/effects/SkGradient.h"
16
17 enum ColorPattern {
18 kWhite_ColorPattern,
19 kBlue_ColorPattern,
20 kOpaqueBitmap_ColorPattern,
21 kAlphaBitmap_ColorPattern,
22 };
23
24 static const struct ColorPatternData{
25 SkColor fColor;
26 bool fIsBitmap;
27 const char* fName;
28 } gColorPatterns[] = {
29 // Keep this in same order as ColorPattern enum
30 { SK_ColorWHITE, false, "white" }, // kWhite_ColorPattern
31 { SK_ColorBLUE, false, "blue" }, // kBlue_ColorPattern
32 { SK_ColorWHITE, true, "obaqueBitMap" }, // kOpaqueBitmap_ColorPattern
33 { 0x10000000, true, "alphaBitmap" }, // kAlphaBitmap_ColorPattern
34 };
35
36 enum DrawType {
37 kRect_DrawType,
38 kPath_DrawType,
39 };
40
41 static void makebm(SkBitmap* bm, int w, int h) {
42 bm->allocN32Pixels(w, h);
43 bm->eraseColor(SK_ColorTRANSPARENT);
44
45 SkCanvas canvas(*bm);
46 SkScalar s = SkIntToScalar(std::min(w, h));
47 static const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
48 static const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
49 static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
50 static const SkColor4f kColors0[] = { SkColor4f::FromColor(0x80F00080),
51 SkColor4f::FromColor(0xF0F08000),
52 SkColor4f::FromColor(0x800080F0) };
53 static const SkColor4f kColors1[] = { SkColor4f::FromColor(0xF08000F0),
54 SkColor4f::FromColor(0x8080F000),
55 SkColor4f::FromColor(0xF000F080) };
56
57
58 SkPaint paint;
59
60 paint.setShader(SkShaders::LinearGradient(kPts0, {{kColors0, kPos, SkTileMode::kClamp}, {}}));
61 canvas.drawPaint(paint);
62 paint.setShader(SkShaders::LinearGradient(kPts1, {{kColors1, kPos, SkTileMode::kClamp}, {}}));
63 canvas.drawPaint(paint);
64 }
65
66 /**
67 * This bench draws a grid of either rects or filled paths, with two alternating color patterns.
68 * This color patterns are passed in as enums to the class. The options are:
69 * 1) solid white color
70 * 2) solid blue color
71 * 3) opaque bitmap
72 * 4) partial alpha bitmap
73 * The same color pattern can be set for both arguments to create a uniform pattern on all draws.
74 *
75 * The bench is used to test a few things. First it can test any optimizations made for a specific
76 * color pattern (for example drawing an opaque bitmap versus one with partial alpha). Also it can
77 * be used to test the cost of program switching and/or GrDrawOp combining when alternating between
78 * different patterns when on the gpu.
79 */
80 class AlternatingColorPatternBench : public Benchmark {
81 public:
82 enum {
83 NX = 5,
84 NY = 5,
85 NUM_DRAWS = NX * NY,
86 };
87 sk_sp<SkShader> fBmShader;
88
89 SkPath fPaths[NUM_DRAWS];
90 SkRect fRects[NUM_DRAWS];
91 SkColor fColors[NUM_DRAWS];
92 sk_sp<SkShader> fShaders[NUM_DRAWS];
93
94 SkString fName;
95 ColorPatternData fPattern1;
96 ColorPatternData fPattern2;
97 DrawType fDrawType;
98 SkBitmap fBmp;
99
100
101 AlternatingColorPatternBench(ColorPattern pattern1, ColorPattern pattern2, DrawType drawType) {
102 fPattern1 = gColorPatterns[pattern1];
103 fPattern2 = gColorPatterns[pattern2];
104 fName.printf("colorPattern_%s_%s_%s",
105 fPattern1.fName, fPattern2.fName,
106 kRect_DrawType == drawType ? "rect" : "path");
107 fDrawType = drawType;
108 }
109
110 protected:
111 const char* onGetName() override {
112 return fName.c_str();
113 }
114
115 void onDelayedSetup() override {
116 int w = 40;
117 int h = 40;
118 makebm(&fBmp, w, h);
119 fBmShader = fBmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
120 SkSamplingOptions(SkFilterMode::kLinear));
121 int offset = 2;
122 int count = 0;
123 for (int j = 0; j < NY; ++j) {
124 for (int i = 0; i < NX; ++i) {
125 int x = (w + offset) * i;
126 int y = (h * offset) * j;
127 if (kRect_DrawType == fDrawType) {
128 fRects[count].setXYWH(SkIntToScalar(x), SkIntToScalar(y),
129 SkIntToScalar(w), SkIntToScalar(h));
130 } else {
131 SkPathBuilder builder;
132 builder.moveTo(SkIntToScalar(x), SkIntToScalar(y));
133 builder.rLineTo(SkIntToScalar(w), 0);
134 builder.rLineTo(0, SkIntToScalar(h));
135 builder.rLineTo(SkIntToScalar(-w + 1), 0);
136 fPaths[count] = builder.detach();
137 }
138 if (0 == count % 2) {
139 fColors[count] = fPattern1.fColor;
140 fShaders[count] = fPattern1.fIsBitmap ? fBmShader : nullptr;
141 } else {
142 fColors[count] = fPattern2.fColor;
143 fShaders[count] = fPattern2.fIsBitmap ? fBmShader : nullptr;
144 }
145 ++count;
146 }
147 }
148 }
149
150 void onDraw(int loops, SkCanvas* canvas) override {
151 SkPaint paint;
152 paint.setAntiAlias(false);
153
154 for (int i = 0; i < loops; ++i) {
155 for (int j = 0; j < NUM_DRAWS; ++j) {
156 paint.setColor(fColors[j]);
157 paint.setShader(fShaders[j]);
158 if (kRect_DrawType == fDrawType) {
159 canvas->drawRect(fRects[j], paint);
160 } else {
161 canvas->drawPath(fPaths[j], paint);
162 }
163 }
164 }
165 }
166
167 private:
168 using INHERITED = Benchmark;
169 };
170
171 DEF_BENCH(return new AlternatingColorPatternBench(kWhite_ColorPattern,
172 kWhite_ColorPattern,
173 kPath_DrawType);)
174 DEF_BENCH(return new AlternatingColorPatternBench(kBlue_ColorPattern,
175 kBlue_ColorPattern,
176 kPath_DrawType);)
177 DEF_BENCH(return new AlternatingColorPatternBench(kWhite_ColorPattern,
178 kBlue_ColorPattern,
179 kPath_DrawType);)
180
181 DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
182 kOpaqueBitmap_ColorPattern,
183 kPath_DrawType);)
184 DEF_BENCH(return new AlternatingColorPatternBench(kAlphaBitmap_ColorPattern,
185 kAlphaBitmap_ColorPattern,
186 kPath_DrawType);)
187 DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
188 kAlphaBitmap_ColorPattern,
189 kPath_DrawType);)
190
191 DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
192 kOpaqueBitmap_ColorPattern,
193 kRect_DrawType);)
194 DEF_BENCH(return new AlternatingColorPatternBench(kAlphaBitmap_ColorPattern,
195 kAlphaBitmap_ColorPattern,
196 kRect_DrawType);)
197 DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
198 kAlphaBitmap_ColorPattern,
199 kRect_DrawType);)