• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkString.h"
14 #include "include/effects/SkGradientShader.h"
15 
16 enum ColorPattern {
17     kWhite_ColorPattern,
18     kBlue_ColorPattern,
19     kOpaqueBitmap_ColorPattern,
20     kAlphaBitmap_ColorPattern,
21 };
22 
23 static const struct ColorPatternData{
24     SkColor         fColor;
25     bool            fIsBitmap;
26     const char*     fName;
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
33 };
34 
35 enum DrawType {
36     kRect_DrawType,
37     kPath_DrawType,
38 };
39 
makebm(SkBitmap * bm,int w,int h)40 static void makebm(SkBitmap* bm, int w, int h) {
41     bm->allocN32Pixels(w, h);
42     bm->eraseColor(SK_ColorTRANSPARENT);
43 
44     SkCanvas    canvas(*bm);
45     SkScalar    s = SkIntToScalar(SkMin32(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 };
51 
52 
53     SkPaint     paint;
54 
55     paint.setShader(SkGradientShader::MakeLinear(kPts0, kColors0, kPos, SK_ARRAY_COUNT(kColors0),
56                                                  SkTileMode::kClamp));
57     canvas.drawPaint(paint);
58     paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos, SK_ARRAY_COUNT(kColors1),
59                                                  SkTileMode::kClamp));
60     canvas.drawPaint(paint);
61 }
62 
63 /**
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
67  *   2) solid blue color
68  *   3) opaque bitmap
69  *   4) partial alpha bitmap
70  * The same color pattern can be set for both arguments to create a uniform pattern on all draws.
71  *
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.
76  */
77 class AlternatingColorPatternBench : public Benchmark {
78 public:
79     enum {
80         NX = 5,
81         NY = 5,
82         NUM_DRAWS = NX * NY,
83     };
84     sk_sp<SkShader> fBmShader;
85 
86     SkPath  fPaths[NUM_DRAWS];
87     SkRect  fRects[NUM_DRAWS];
88     SkColor fColors[NUM_DRAWS];
89     sk_sp<SkShader> fShaders[NUM_DRAWS];
90 
91     SkString        fName;
92     ColorPatternData    fPattern1;
93     ColorPatternData    fPattern2;
94     DrawType fDrawType;
95     SkBitmap fBmp;
96 
97 
AlternatingColorPatternBench(ColorPattern pattern1,ColorPattern pattern2,DrawType drawType)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;
105     }
106 
107 protected:
onGetName()108     const char* onGetName() override {
109         return fName.c_str();
110     }
111 
onDelayedSetup()112     void onDelayedSetup() override {
113         int w = 40;
114         int h = 40;
115         makebm(&fBmp, w, h);
116         fBmShader = fBmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat);
117         int offset = 2;
118         int count = 0;
119         for (int j = 0; j < NY; ++j) {
120             for (int i = 0; i < NX; ++i) {
121                 int x = (w + offset) * i;
122                 int y = (h * offset) * j;
123                 if (kRect_DrawType == fDrawType) {
124                     fRects[count].set(SkIntToScalar(x), SkIntToScalar(y),
125                                       SkIntToScalar(x + w), SkIntToScalar(y + h));
126                 } else {
127                     fPaths[count].moveTo(SkIntToScalar(x), SkIntToScalar(y));
128                     fPaths[count].rLineTo(SkIntToScalar(w), 0);
129                     fPaths[count].rLineTo(0, SkIntToScalar(h));
130                     fPaths[count].rLineTo(SkIntToScalar(-w + 1), 0);
131                 }
132                 if (0 == count % 2) {
133                     fColors[count]  = fPattern1.fColor;
134                     fShaders[count] = fPattern1.fIsBitmap ? fBmShader : nullptr;
135                 } else {
136                     fColors[count]  = fPattern2.fColor;
137                     fShaders[count] = fPattern2.fIsBitmap ? fBmShader : nullptr;
138                 }
139                 ++count;
140             }
141         }
142     }
143 
onDraw(int loops,SkCanvas * canvas)144     void onDraw(int loops, SkCanvas* canvas) override {
145         SkPaint paint;
146         paint.setAntiAlias(false);
147         paint.setFilterQuality(kLow_SkFilterQuality);
148 
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);
155                 } else {
156                     canvas->drawPath(fPaths[j], paint);
157                 }
158             }
159         }
160     }
161 
162 private:
163     typedef Benchmark INHERITED;
164 };
165 
166 DEF_BENCH(return new AlternatingColorPatternBench(kWhite_ColorPattern,
167                                                   kWhite_ColorPattern,
168                                                   kPath_DrawType);)
169 DEF_BENCH(return new AlternatingColorPatternBench(kBlue_ColorPattern,
170                                                   kBlue_ColorPattern,
171                                                   kPath_DrawType);)
172 DEF_BENCH(return new AlternatingColorPatternBench(kWhite_ColorPattern,
173                                                   kBlue_ColorPattern,
174                                                   kPath_DrawType);)
175 
176 DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
177                                                   kOpaqueBitmap_ColorPattern,
178                                                   kPath_DrawType);)
179 DEF_BENCH(return new AlternatingColorPatternBench(kAlphaBitmap_ColorPattern,
180                                                   kAlphaBitmap_ColorPattern,
181                                                   kPath_DrawType);)
182 DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
183                                                   kAlphaBitmap_ColorPattern,
184                                                   kPath_DrawType);)
185 
186 DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
187                                                   kOpaqueBitmap_ColorPattern,
188                                                   kRect_DrawType);)
189 DEF_BENCH(return new AlternatingColorPatternBench(kAlphaBitmap_ColorPattern,
190                                                   kAlphaBitmap_ColorPattern,
191                                                   kRect_DrawType);)
192 DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
193                                                   kAlphaBitmap_ColorPattern,
194                                                   kRect_DrawType);)
195