blob: 6e1613b9ff0491e3acaeb318de928cbbee08636b [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"
Mike Reed887cdf12017-04-03 11:11:09 -040014#include "SkVertices.h"
reed@google.com05d63ae2011-10-28 18:57:32 +000015
16enum VertFlags {
17 kColors_VertFlag,
18 kTexture_VertFlag,
19};
20
tfarinaf168b862014-06-19 12:32:29 -070021class VertBench : public Benchmark {
reed@google.com05d63ae2011-10-28 18:57:32 +000022 SkString fName;
23 enum {
24 W = 640,
25 H = 480,
26 ROW = 20,
27 COL = 20,
28 PTS = (ROW + 1) * (COL + 1),
29 IDX = ROW * COL * 6,
reed@google.com05d63ae2011-10-28 18:57:32 +000030 };
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:
mtklein@google.com410e6e82013-09-13 19:52:27 +000043 VertBench() {
reed@google.com05d63ae2011-10-28 18:57:32 +000044 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
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000071 SkRandom rand;
reed@google.com05d63ae2011-10-28 18:57:32 +000072 for (int i = 0; i < PTS; ++i) {
73 fColors[i] = rand.nextU() | (0xFF << 24);
74 }
75
76 fName.set("verts");
77 }
78
79protected:
Mike Reed887cdf12017-04-03 11:11:09 -040080 const char* onGetName() override { return fName.c_str(); }
81 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.com05d63ae2011-10-28 18:57:32 +000082 SkPaint paint;
83 this->setupPaint(&paint);
bsalomon@google.com4a018bb2011-10-28 19:50:21 +000084
Mike Reed887cdf12017-04-03 11:11:09 -040085 auto verts = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, PTS,
86 fPts, nullptr, fColors, IDX, fIdx);
commit-bot@chromium.org33614712013-12-03 18:17:16 +000087 for (int i = 0; i < loops; i++) {
Mike Reed887cdf12017-04-03 11:11:09 -040088 canvas->drawVertices(verts, SkBlendMode::kModulate, paint);
reed@google.com05d63ae2011-10-28 18:57:32 +000089 }
reed@google.com05d63ae2011-10-28 18:57:32 +000090 }
91private:
tfarinaf168b862014-06-19 12:32:29 -070092 typedef Benchmark INHERITED;
reed@google.com05d63ae2011-10-28 18:57:32 +000093};
94
95///////////////////////////////////////////////////////////////////////////////
96
halcanary385fe4d2015-08-26 13:07:48 -070097DEF_BENCH(return new VertBench();)