blob: d2e5269c52cb8170b8b674fcdbdfe76750cbeadf [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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.com05d63ae2011-10-28 18:57:32 +00009#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
reed@google.com05d63ae2011-10-28 18:57:32 +000012#include "SkShader.h"
tfarinaf168b862014-06-19 12:32:29 -070013#include "SkString.h"
reed@google.com05d63ae2011-10-28 18:57:32 +000014
15enum VertFlags {
16 kColors_VertFlag,
17 kTexture_VertFlag,
18};
19
tfarinaf168b862014-06-19 12:32:29 -070020class VertBench : public Benchmark {
reed@google.com05d63ae2011-10-28 18:57:32 +000021 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,
reed@google.com05d63ae2011-10-28 18:57:32 +000029 };
30
31 SkPoint fPts[PTS];
32 SkColor fColors[PTS];
reed@google.com05d63ae2011-10-28 18:57:32 +000033 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 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000040
reed@google.com05d63ae2011-10-28 18:57:32 +000041public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000042 VertBench() {
reed@google.com05d63ae2011-10-28 18:57:32 +000043 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;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000056
reed@google.com05d63ae2011-10-28 18:57:32 +000057 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
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000070 SkRandom rand;
reed@google.com05d63ae2011-10-28 18:57:32 +000071 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(); }
commit-bot@chromium.org33614712013-12-03 18:17:16 +000080 virtual void onDraw(const int loops, SkCanvas* canvas) {
reed@google.com05d63ae2011-10-28 18:57:32 +000081 SkPaint paint;
82 this->setupPaint(&paint);
bsalomon@google.com4a018bb2011-10-28 19:50:21 +000083
commit-bot@chromium.org33614712013-12-03 18:17:16 +000084 for (int i = 0; i < loops; i++) {
reed@google.com05d63ae2011-10-28 18:57:32 +000085 canvas->drawVertices(SkCanvas::kTriangles_VertexMode, PTS,
halcanary96fcdcc2015-08-27 07:41:13 -070086 fPts, nullptr, fColors, nullptr, fIdx, IDX, paint);
reed@google.com05d63ae2011-10-28 18:57:32 +000087 }
reed@google.com05d63ae2011-10-28 18:57:32 +000088 }
89private:
tfarinaf168b862014-06-19 12:32:29 -070090 typedef Benchmark INHERITED;
reed@google.com05d63ae2011-10-28 18:57:32 +000091};
92
93///////////////////////////////////////////////////////////////////////////////
94
halcanary385fe4d2015-08-26 13:07:48 -070095DEF_BENCH(return new VertBench();)