reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkRandom.h" |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 12 | #include "SkShader.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 13 | #include "SkString.h" |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 14 | |
| 15 | enum VertFlags { |
| 16 | kColors_VertFlag, |
| 17 | kTexture_VertFlag, |
| 18 | }; |
| 19 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 20 | class VertBench : public Benchmark { |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 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, |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | SkPoint fPts[PTS]; |
| 32 | SkColor fColors[PTS]; |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 40 | |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 41 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 42 | VertBench() { |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 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; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 56 | |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 70 | SkRandom rand; |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 71 | for (int i = 0; i < PTS; ++i) { |
| 72 | fColors[i] = rand.nextU() | (0xFF << 24); |
| 73 | } |
| 74 | |
| 75 | fName.set("verts"); |
| 76 | } |
| 77 | |
| 78 | protected: |
| 79 | virtual const char* onGetName() { return fName.c_str(); } |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 80 | virtual void onDraw(const int loops, SkCanvas* canvas) { |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 81 | SkPaint paint; |
| 82 | this->setupPaint(&paint); |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 83 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 84 | for (int i = 0; i < loops; i++) { |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 85 | canvas->drawVertices(SkCanvas::kTriangles_VertexMode, PTS, |
| 86 | fPts, NULL, fColors, NULL, fIdx, IDX, paint); |
| 87 | } |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 88 | } |
| 89 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 90 | typedef Benchmark INHERITED; |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 95 | DEF_BENCH( return SkNEW_ARGS(VertBench, ()); ) |