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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
| 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkPaint.h" |
| 11 | #include "include/core/SkShader.h" |
| 12 | #include "include/core/SkString.h" |
| 13 | #include "include/core/SkVertices.h" |
| 14 | #include "include/utils/SkRandom.h" |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 15 | |
| 16 | enum VertFlags { |
| 17 | kColors_VertFlag, |
| 18 | kTexture_VertFlag, |
| 19 | }; |
| 20 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 21 | class VertBench : public Benchmark { |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 22 | 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.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | SkPoint fPts[PTS]; |
| 33 | SkColor fColors[PTS]; |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 34 | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 41 | |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 42 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 43 | VertBench() { |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 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.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 57 | |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 58 | 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.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 71 | SkRandom rand; |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 72 | for (int i = 0; i < PTS; ++i) { |
| 73 | fColors[i] = rand.nextU() | (0xFF << 24); |
| 74 | } |
| 75 | |
| 76 | fName.set("verts"); |
| 77 | } |
| 78 | |
| 79 | protected: |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 80 | const char* onGetName() override { return fName.c_str(); } |
| 81 | void onDraw(int loops, SkCanvas* canvas) override { |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 82 | SkPaint paint; |
| 83 | this->setupPaint(&paint); |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 84 | |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 85 | auto verts = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, PTS, |
| 86 | fPts, nullptr, fColors, IDX, fIdx); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 87 | for (int i = 0; i < loops; i++) { |
Mike Reed | 887cdf1 | 2017-04-03 11:11:09 -0400 | [diff] [blame] | 88 | canvas->drawVertices(verts, SkBlendMode::kModulate, paint); |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 89 | } |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 90 | } |
| 91 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 92 | typedef Benchmark INHERITED; |
reed@google.com | 05d63ae | 2011-10-28 18:57:32 +0000 | [diff] [blame] | 93 | }; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 94 | DEF_BENCH(return new VertBench();) |
Mike Reed | 1d0e68b | 2019-03-22 15:57:45 -0400 | [diff] [blame] | 95 | |
| 96 | ///////////////////////////////////////////////////////////////////////////////////////////////// |
| 97 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 98 | #include "include/core/SkRSXform.h" |
| 99 | #include "include/utils/SkRandom.h" |
| 100 | #include "tools/Resources.h" |
Mike Reed | 1d0e68b | 2019-03-22 15:57:45 -0400 | [diff] [blame] | 101 | |
| 102 | enum AtlasFlags { |
| 103 | kColors_Flag = 1 << 0, |
| 104 | kVerts_Flag = 1 << 1, |
| 105 | }; |
| 106 | |
| 107 | class AtlasBench : public Benchmark { |
| 108 | unsigned fFlags; |
| 109 | SkString fName; |
| 110 | enum { |
| 111 | W = 640, |
| 112 | H = 480, |
| 113 | N = 10*1000, |
| 114 | }; |
| 115 | |
| 116 | sk_sp<SkImage> fAtlas; |
| 117 | SkRSXform fXforms[N]; |
| 118 | SkRect fRects[N]; |
| 119 | SkColor fColors[N]; |
| 120 | |
| 121 | public: |
| 122 | AtlasBench(unsigned flags) : fFlags(flags) { |
| 123 | fName.printf("drawAtlas_%d", flags); |
| 124 | } |
| 125 | ~AtlasBench() override {} |
| 126 | |
| 127 | protected: |
| 128 | const char* onGetName() override { return fName.c_str(); } |
| 129 | void onDelayedSetup() override { |
| 130 | fAtlas = GetResourceAsImage("images/mandrill_256.png"); |
| 131 | if (fAtlas) { |
| 132 | fAtlas = fAtlas->makeRasterImage(); |
| 133 | } |
| 134 | |
| 135 | const SkScalar imageW = fAtlas->width(); |
| 136 | const SkScalar imageH = fAtlas->height(); |
| 137 | |
| 138 | SkRandom rand; |
| 139 | for (int i = 0; i < N; ++i) { |
| 140 | fRects[i] = SkRect::MakeXYWH(rand.nextF() * (imageW - 8), |
| 141 | rand.nextF() * (imageH - 8), 8, 8); |
| 142 | fColors[i] = rand.nextU(); |
| 143 | fXforms[i] = SkRSXform::Make(1, 0, rand.nextF() * W, rand.nextF() * H); |
| 144 | } |
| 145 | } |
| 146 | void onDraw(int loops, SkCanvas* canvas) override { |
| 147 | const SkRect* cullRect = nullptr; |
| 148 | const SkPaint* paintPtr = nullptr; |
| 149 | const SkColor* colors = nullptr; |
| 150 | const SkImage* atlas = nullptr; |
| 151 | if (fFlags & kColors_Flag) { |
| 152 | colors = fColors; |
| 153 | } |
| 154 | if (fFlags & kVerts_Flag) { |
| 155 | atlas = fAtlas.get(); |
| 156 | } |
| 157 | for (int i = 0; i < loops; i++) { |
| 158 | canvas->drawAtlas(atlas, fXforms, fRects, colors, N, SkBlendMode::kSrcOver, |
| 159 | cullRect, paintPtr); |
| 160 | } |
| 161 | } |
| 162 | private: |
| 163 | typedef Benchmark INHERITED; |
| 164 | }; |
| 165 | //DEF_BENCH(return new AtlasBench(0);) |
| 166 | //DEF_BENCH(return new AtlasBench(kColors_Flag);) |
| 167 | DEF_BENCH(return new AtlasBench(kVerts_Flag);) |
| 168 | DEF_BENCH(return new AtlasBench(kVerts_Flag | kColors_Flag);) |
| 169 | |