1/*
2 * Copyright 2011 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 "SkBenchmark.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
12#include "SkString.h"
13#include "SkShader.h"
14
15enum VertFlags {
16    kColors_VertFlag,
17    kTexture_VertFlag,
18};
19
20class VertBench : public SkBenchmark {
21    SkString fName;
22    enum {
23        W = 640,
24        H = 480,
25        ROW = 20,
26        COL = 20,
27        PTS = (ROW + 1) * (COL + 1),
28        IDX = ROW * COL * 6,
29    };
30
31    SkPoint fPts[PTS];
32    SkColor fColors[PTS];
33    uint16_t fIdx[IDX];
34
35    static void load_2_tris(uint16_t idx[], int x, int y, int rb) {
36        int n = y * rb + x;
37        idx[0] = n; idx[1] = n + 1; idx[2] = rb + n + 1;
38        idx[3] = n; idx[4] = rb + n + 1; idx[5] = n + rb;
39    }
40
41public:
42    VertBench() {
43        const SkScalar dx = SkIntToScalar(W) / COL;
44        const SkScalar dy = SkIntToScalar(H) / COL;
45
46        SkPoint* pts = fPts;
47        uint16_t* idx = fIdx;
48
49        SkScalar yy = 0;
50        for (int y = 0; y <= ROW; y++) {
51            SkScalar xx = 0;
52            for (int x = 0; x <= COL; ++x) {
53                pts->set(xx, yy);
54                pts += 1;
55                xx += dx;
56
57                if (x < COL && y < ROW) {
58                    load_2_tris(idx, x, y, COL + 1);
59                    for (int i = 0; i < 6; i++) {
60                        SkASSERT(idx[i] < PTS);
61                    }
62                    idx += 6;
63                }
64            }
65            yy += dy;
66        }
67        SkASSERT(PTS == pts - fPts);
68        SkASSERT(IDX == idx - fIdx);
69
70        SkRandom rand;
71        for (int i = 0; i < PTS; ++i) {
72            fColors[i] = rand.nextU() | (0xFF << 24);
73        }
74
75        fName.set("verts");
76    }
77
78protected:
79    virtual const char* onGetName() { return fName.c_str(); }
80    virtual void onDraw(const int loops, SkCanvas* canvas) {
81        SkPaint paint;
82        this->setupPaint(&paint);
83
84        for (int i = 0; i < loops; i++) {
85            canvas->drawVertices(SkCanvas::kTriangles_VertexMode, PTS,
86                                 fPts, NULL, fColors, NULL, fIdx, IDX, paint);
87        }
88    }
89private:
90    typedef SkBenchmark INHERITED;
91};
92
93///////////////////////////////////////////////////////////////////////////////
94
95DEF_BENCH( return SkNEW_ARGS(VertBench, ()); )
96