2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
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/SkString.h"
14 #include "include/effects/SkGradientShader.h"
19 kOpaqueBitmap_ColorPattern,
20 kAlphaBitmap_ColorPattern,
23 static const struct ColorPatternData{
27 } gColorPatterns[] = {
28 // Keep this in same order as ColorPattern enum
29 { SK_ColorWHITE, false, "white" }, // kWhite_ColorPattern
30 { SK_ColorBLUE, false, "blue" }, // kBlue_ColorPattern
31 { SK_ColorWHITE, true, "obaqueBitMap" }, // kOpaqueBitmap_ColorPattern
32 { 0x10000000, true, "alphaBitmap" }, // kAlphaBitmap_ColorPattern
40 static void makebm(SkBitmap* bm, int w, int h) {
41 bm->allocN32Pixels(w, h);
42 bm->eraseColor(SK_ColorTRANSPARENT);
45 SkScalar s = SkIntToScalar(std::min(w, h));
46 static const SkPoint kPts0[] = { { 0, 0 }, { s, s } };
47 static const SkPoint kPts1[] = { { s/2, 0 }, { s/2, s } };
48 static const SkScalar kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
49 static const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
50 static const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
55 paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos, SK_ARRAY_COUNT(kColors0),
57 canvas.drawPaint(paint);
58 paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos, SK_ARRAY_COUNT(kColors1),
60 canvas.drawPaint(paint);
64 * This bench draws a grid of either rects or filled paths, with two alternating color patterns.
65 * This color patterns are passed in as enums to the class. The options are:
66 * 1) solid white color
69 * 4) partial alpha bitmap
70 * The same color pattern can be set for both arguments to create a uniform pattern on all draws.
72 * The bench is used to test a few things. First it can test any optimizations made for a specific
73 * color pattern (for example drawing an opaque bitmap versus one with partial alpha). Also it can
74 * be used to test the cost of program switching and/or GrDrawOp combining when alternating between
75 * different patterns when on the gpu.
77 class AlternatingColorPatternBench : public Benchmark {
84 sk_sp<SkShader> fBmShader;
86 SkPath fPaths[NUM_DRAWS];
87 SkRect fRects[NUM_DRAWS];
88 SkColor fColors[NUM_DRAWS];
89 sk_sp<SkShader> fShaders[NUM_DRAWS];
92 ColorPatternData fPattern1;
93 ColorPatternData fPattern2;
98 AlternatingColorPatternBench(ColorPattern pattern1, ColorPattern pattern2, DrawType drawType) {
99 fPattern1 = gColorPatterns[pattern1];
100 fPattern2 = gColorPatterns[pattern2];
101 fName.printf("colorPattern_%s_%s_%s",
102 fPattern1.fName, fPattern2.fName,
103 kRect_DrawType == drawType ? "rect" : "path");
104 fDrawType = drawType;
108 const char* onGetName() override {
109 return fName.c_str();
112 void onDelayedSetup() override {
116 fBmShader = fBmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
117 SkSamplingOptions(SkFilterMode::kLinear));
120 for (int j = 0; j < NY; ++j) {
121 for (int i = 0; i < NX; ++i) {
122 int x = (w + offset) * i;
123 int y = (h * offset) * j;
124 if (kRect_DrawType == fDrawType) {
125 fRects[count].setXYWH(SkIntToScalar(x), SkIntToScalar(y),
126 SkIntToScalar(w), SkIntToScalar(h));
128 fPaths[count].moveTo(SkIntToScalar(x), SkIntToScalar(y));
129 fPaths[count].rLineTo(SkIntToScalar(w), 0);
130 fPaths[count].rLineTo(0, SkIntToScalar(h));
131 fPaths[count].rLineTo(SkIntToScalar(-w + 1), 0);
133 if (0 == count % 2) {
134 fColors[count] = fPattern1.fColor;
135 fShaders[count] = fPattern1.fIsBitmap ? fBmShader : nullptr;
137 fColors[count] = fPattern2.fColor;
138 fShaders[count] = fPattern2.fIsBitmap ? fBmShader : nullptr;
145 void onDraw(int loops, SkCanvas* canvas) override {
147 paint.setAntiAlias(false);
149 for (int i = 0; i < loops; ++i) {
150 for (int j = 0; j < NUM_DRAWS; ++j) {
151 paint.setColor(fColors[j]);
152 paint.setShader(fShaders[j]);
153 if (kRect_DrawType == fDrawType) {
154 canvas->drawRect(fRects[j], paint);
156 canvas->drawPath(fPaths[j], paint);
163 using INHERITED = Benchmark;
166 DEF_BENCH(return new AlternatingColorPatternBench(kWhite_ColorPattern,
169 DEF_BENCH(return new AlternatingColorPatternBench(kBlue_ColorPattern,
172 DEF_BENCH(return new AlternatingColorPatternBench(kWhite_ColorPattern,
176 DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
177 kOpaqueBitmap_ColorPattern,
179 DEF_BENCH(return new AlternatingColorPatternBench(kAlphaBitmap_ColorPattern,
180 kAlphaBitmap_ColorPattern,
182 DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
183 kAlphaBitmap_ColorPattern,
186 DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
187 kOpaqueBitmap_ColorPattern,
189 DEF_BENCH(return new AlternatingColorPatternBench(kAlphaBitmap_ColorPattern,
190 kAlphaBitmap_ColorPattern,
192 DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
193 kAlphaBitmap_ColorPattern,