blob: 98df44929e8ed88c00197fb33b8cfbbfade8ac8b [file] [log] [blame]
reed@google.com05d63ae2011-10-28 18:57:32 +00001/*
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 N = SkBENCHLOOP(10)
30 };
31
32 SkPoint fPts[PTS];
33 SkColor fColors[PTS];
34 SkPoint fTex[PTS];
35 uint16_t fIdx[IDX];
36
37 static void load_2_tris(uint16_t idx[], int x, int y, int rb) {
38 int n = y * rb + x;
39 idx[0] = n; idx[1] = n + 1; idx[2] = rb + n + 1;
40 idx[3] = n; idx[4] = rb + n + 1; idx[5] = n + rb;
41 }
42
43public:
44 VertBench(void* param) : INHERITED(param) {
45 const SkScalar dx = SkIntToScalar(W) / COL;
46 const SkScalar dy = SkIntToScalar(H) / COL;
47
48 SkPoint* pts = fPts;
49 uint16_t* idx = fIdx;
50
51 SkScalar yy = 0;
52 for (int y = 0; y <= ROW; y++) {
53 SkScalar xx = 0;
54 for (int x = 0; x <= COL; ++x) {
55 pts->set(xx, yy);
56 pts += 1;
57 xx += dx;
58
59 if (x < COL && y < ROW) {
60 load_2_tris(idx, x, y, COL + 1);
61 for (int i = 0; i < 6; i++) {
62 SkASSERT(idx[i] < PTS);
63 }
64 idx += 6;
65 }
66 }
67 yy += dy;
68 }
69 SkASSERT(PTS == pts - fPts);
70 SkASSERT(IDX == idx - fIdx);
71
72 SkRandom rand;
73 for (int i = 0; i < PTS; ++i) {
74 fColors[i] = rand.nextU() | (0xFF << 24);
75 }
76
77 fName.set("verts");
78 }
79
80protected:
81 virtual const char* onGetName() { return fName.c_str(); }
82 virtual void onDraw(SkCanvas* canvas) {
83 SkPaint paint;
84 this->setupPaint(&paint);
bsalomon@google.com4a018bb2011-10-28 19:50:21 +000085
reed@google.com05d63ae2011-10-28 18:57:32 +000086 for (int i = 0; i < N; i++) {
87 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, PTS,
88 fPts, NULL, fColors, NULL, fIdx, IDX, paint);
89 }
reed@google.com05d63ae2011-10-28 18:57:32 +000090 }
91private:
92 typedef SkBenchmark INHERITED;
93};
94
95///////////////////////////////////////////////////////////////////////////////
96
97static SkBenchmark* Fact(void* p) { return SkNEW_ARGS(VertBench, (p)); }
98
99static BenchRegistry gReg(Fact);