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