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 "Benchmark.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkGradientShader.h"
12#include "SkPaint.h"
13#include "SkPath.h"
14#include "SkString.h"
15
16enum ColorPattern {
17    kWhite_ColorPattern,
18    kBlue_ColorPattern,
19    kOpaqueBitmap_ColorPattern,
20    kAlphaBitmap_ColorPattern,
21};
22
23static 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
35enum DrawType {
36    kRect_DrawType,
37    kPath_DrawType,
38};
39
40static 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                                                 SkShader::kClamp_TileMode));
57    canvas.drawPaint(paint);
58    paint.setShader(SkGradientShader::MakeLinear(kPts1, kColors1, kPos, SK_ARRAY_COUNT(kColors1),
59                                                 SkShader::kClamp_TileMode));
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 */
77class AlternatingColorPatternBench : public Benchmark {
78public:
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
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
107protected:
108    const char* onGetName() override {
109        return fName.c_str();
110    }
111
112    void onDelayedSetup() override {
113        int w = 40;
114        int h = 40;
115        makebm(&fBmp, w, h);
116        fBmShader = SkShader::MakeBitmapShader(fBmp,
117                                                 SkShader::kRepeat_TileMode,
118                                                 SkShader::kRepeat_TileMode);
119        int offset = 2;
120        int count = 0;
121        for (int j = 0; j < NY; ++j) {
122            for (int i = 0; i < NX; ++i) {
123                int x = (w + offset) * i;
124                int y = (h * offset) * j;
125                if (kRect_DrawType == fDrawType) {
126                    fRects[count].set(SkIntToScalar(x), SkIntToScalar(y),
127                                      SkIntToScalar(x + w), SkIntToScalar(y + h));
128                } else {
129                    fPaths[count].moveTo(SkIntToScalar(x), SkIntToScalar(y));
130                    fPaths[count].rLineTo(SkIntToScalar(w), 0);
131                    fPaths[count].rLineTo(0, SkIntToScalar(h));
132                    fPaths[count].rLineTo(SkIntToScalar(-w + 1), 0);
133                }
134                if (0 == count % 2) {
135                    fColors[count]  = fPattern1.fColor;
136                    fShaders[count] = fPattern1.fIsBitmap ? fBmShader : nullptr;
137                } else {
138                    fColors[count]  = fPattern2.fColor;
139                    fShaders[count] = fPattern2.fIsBitmap ? fBmShader : nullptr;
140                }
141                ++count;
142            }
143        }
144    }
145
146    void onDraw(int loops, SkCanvas* canvas) override {
147        SkPaint paint;
148        paint.setAntiAlias(false);
149        paint.setFilterQuality(kLow_SkFilterQuality);
150
151        for (int i = 0; i < loops; ++i) {
152            for (int j = 0; j < NUM_DRAWS; ++j) {
153                paint.setColor(fColors[j]);
154                paint.setShader(fShaders[j]);
155                if (kRect_DrawType == fDrawType) {
156                    canvas->drawRect(fRects[j], paint);
157                } else {
158                    canvas->drawPath(fPaths[j], paint);
159                }
160            }
161        }
162    }
163
164private:
165    typedef Benchmark INHERITED;
166};
167
168DEF_BENCH(return new AlternatingColorPatternBench(kWhite_ColorPattern,
169                                                  kWhite_ColorPattern,
170                                                  kPath_DrawType);)
171DEF_BENCH(return new AlternatingColorPatternBench(kBlue_ColorPattern,
172                                                  kBlue_ColorPattern,
173                                                  kPath_DrawType);)
174DEF_BENCH(return new AlternatingColorPatternBench(kWhite_ColorPattern,
175                                                  kBlue_ColorPattern,
176                                                  kPath_DrawType);)
177
178DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
179                                                  kOpaqueBitmap_ColorPattern,
180                                                  kPath_DrawType);)
181DEF_BENCH(return new AlternatingColorPatternBench(kAlphaBitmap_ColorPattern,
182                                                  kAlphaBitmap_ColorPattern,
183                                                  kPath_DrawType);)
184DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
185                                                  kAlphaBitmap_ColorPattern,
186                                                  kPath_DrawType);)
187
188DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
189                                                  kOpaqueBitmap_ColorPattern,
190                                                  kRect_DrawType);)
191DEF_BENCH(return new AlternatingColorPatternBench(kAlphaBitmap_ColorPattern,
192                                                  kAlphaBitmap_ColorPattern,
193                                                  kRect_DrawType);)
194DEF_BENCH(return new AlternatingColorPatternBench(kOpaqueBitmap_ColorPattern,
195                                                  kAlphaBitmap_ColorPattern,
196                                                  kRect_DrawType);)
197