dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 "Benchmark.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | #include "SkPaint.h" |
| 12 | #include "SkPatchGrid.h" |
| 13 | #include "SkString.h" |
| 14 | |
| 15 | /** |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 16 | * This bench measures the rendering time of a gridof patches. |
| 17 | * This bench also tests the different combination of optional parameters for the function |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 18 | * (passing texture coordinates and colors, only textures coordinates, only colors or none). |
| 19 | * Finally, it also has 3 possible sizes small, medium and big to test if the size of the patches |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 20 | * in the grid affects time. |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | class PatchGridBench : public Benchmark { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 24 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 25 | public: |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 26 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 27 | enum Size { |
| 28 | kSmall_Size, |
| 29 | kMedium_Size, |
| 30 | kBig_Size |
| 31 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 32 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 33 | enum VertexMode { |
| 34 | kNone_VertexMode, |
| 35 | kColors_VertexMode, |
| 36 | kTexCoords_VertexMode, |
| 37 | kBoth_VertexMode |
| 38 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 39 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 40 | PatchGridBench(Size size, VertexMode vertexMode) |
| 41 | : fVertexMode(vertexMode) |
| 42 | , fSize(size) { } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 43 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 44 | void setScale(SkCanvas* canvas){ |
| 45 | switch (fSize) { |
| 46 | case kSmall_Size: |
| 47 | canvas->scale(0.1f, 0.1f); |
| 48 | break; |
| 49 | case kMedium_Size: |
| 50 | canvas->scale(1.0f, 1.0f); |
| 51 | break; |
| 52 | case kBig_Size: |
| 53 | canvas->scale(3.0f, 3.0f); |
| 54 | break; |
| 55 | } |
| 56 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 57 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 58 | void setGrid() { |
| 59 | SkPoint vertices[4][5] = { |
| 60 | {{50,50}, {150,50}, {250,50},{350,50},{450,50}}, |
| 61 | {{50,150}, {120,120}, {250,150},{350,150},{450,150}}, |
| 62 | {{50,250}, {150,250}, {250,250},{350,250},{450,250}}, |
| 63 | {{100,300}, {150,350}, {250,350},{350,350},{450,350}} |
| 64 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 65 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 66 | SkColor cornerColors[4][5] = { |
| 67 | {SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE}, |
| 68 | {SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED}, |
| 69 | {SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE}, |
| 70 | {SK_ColorRED, SK_ColorBLUE, SK_ColorRED, SK_ColorBLUE, SK_ColorRED}, |
| 71 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 72 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 73 | SkPoint texCoords[4][5] = { |
| 74 | {{0.0f,0.0f}, {1.0f,0.0f}, {2.0f,0.0f}, {3.0f,0.0f}, {4.0f,0.0f}}, |
| 75 | {{0.0f,1.0f}, {1.0f,1.0f}, {2.0f,1.0f}, {3.0f,1.0f}, {4.0f,1.0f}}, |
| 76 | {{0.0f,2.0f}, {1.0f,2.0f}, {2.0f,2.0f}, {3.0f,2.0f}, {4.0f,2.0f}}, |
| 77 | {{0.0f,3.0f}, {1.0f,3.0f}, {2.0f,3.0f}, {3.0f,3.0f}, {4.0f,3.0f}}, |
| 78 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 79 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 80 | SkPoint hrzCtrl[4][8] = { |
| 81 | {{75,30},{125,45},{175,70},{225,20},{275,50},{325,50},{375,5},{425,90}}, |
| 82 | {{75,150},{125,150},{175,150},{225,150},{275,150},{325,150},{375,150},{425,150}}, |
| 83 | {{75,250},{125,250},{175,250},{225,250},{275,200},{325,150},{375,250},{425,250}}, |
| 84 | {{75,350},{125,350},{175,350},{225,350},{275,350},{325,350},{375,350},{425,350}} |
| 85 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 86 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 87 | SkPoint vrtCtrl[6][5] = { |
| 88 | {{50,75},{150,75},{250,75},{350,75},{450,75}}, |
| 89 | {{50,125},{150,125},{250,125},{350,125},{450,125}}, |
| 90 | {{50,175},{150,175},{220,225},{350,175},{470,225}}, |
| 91 | {{50,225},{150,225},{220,175},{350,225},{470,155}}, |
| 92 | {{50,275},{150,275},{250,275},{350,275},{400,305}}, |
| 93 | {{50,325},{150,325},{250,325},{350,325},{450,325}} |
| 94 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 95 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 96 | static const int kRows = 3; |
| 97 | static const int kCols = 4; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 98 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 99 | fGrid.reset(kRows, kCols, SkPatchGrid::kColors_VertexType, nullptr); |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 100 | for (int i = 0; i < kRows; i++) { |
| 101 | for (int j = 0; j < kCols; j++) { |
| 102 | SkPoint points[12]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 103 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 104 | //set corners |
| 105 | points[SkPatchUtils::kTopP0_CubicCtrlPts] = vertices[i][j]; |
| 106 | points[SkPatchUtils::kTopP3_CubicCtrlPts] = vertices[i][j + 1]; |
| 107 | points[SkPatchUtils::kBottomP0_CubicCtrlPts] = vertices[i + 1][j]; |
| 108 | points[SkPatchUtils::kBottomP3_CubicCtrlPts] = vertices[i + 1][j + 1]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 109 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 110 | points[SkPatchUtils::kTopP1_CubicCtrlPts] = hrzCtrl[i][j * 2]; |
| 111 | points[SkPatchUtils::kTopP2_CubicCtrlPts] = hrzCtrl[i][j * 2 + 1]; |
| 112 | points[SkPatchUtils::kBottomP1_CubicCtrlPts] = hrzCtrl[i + 1][j * 2]; |
| 113 | points[SkPatchUtils::kBottomP2_CubicCtrlPts] = hrzCtrl[i + 1][j * 2 + 1]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 114 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 115 | points[SkPatchUtils::kLeftP1_CubicCtrlPts] = vrtCtrl[i * 2][j]; |
| 116 | points[SkPatchUtils::kLeftP2_CubicCtrlPts] = vrtCtrl[i * 2 + 1][j]; |
| 117 | points[SkPatchUtils::kRightP1_CubicCtrlPts] = vrtCtrl[i * 2][j + 1]; |
| 118 | points[SkPatchUtils::kRightP2_CubicCtrlPts] = vrtCtrl[i * 2 + 1][j + 1]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 119 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 120 | SkColor colors[4]; |
| 121 | colors[0] = cornerColors[i][j]; |
| 122 | colors[1] = cornerColors[i][j + 1]; |
| 123 | colors[3] = cornerColors[i + 1][j]; |
| 124 | colors[2] = cornerColors[i + 1][j + 1]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 125 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 126 | SkPoint texs[4]; |
| 127 | texs[0] = texCoords[i][j]; |
| 128 | texs[1] = texCoords[i][j + 1]; |
| 129 | texs[3] = texCoords[i + 1][j]; |
| 130 | texs[2] = texCoords[i + 1][j + 1]; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 131 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 132 | switch (fVertexMode) { |
| 133 | case kNone_VertexMode: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 134 | fGrid.setPatch(j, i, points, nullptr, nullptr); |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 135 | break; |
| 136 | case kColors_VertexMode: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 137 | fGrid.setPatch(j, i, points, colors, nullptr); |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 138 | break; |
| 139 | case kTexCoords_VertexMode: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 140 | fGrid.setPatch(j, i, points, nullptr, texs); |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 141 | break; |
| 142 | case kBoth_VertexMode: |
| 143 | fGrid.setPatch(j, i, points, colors, texs); |
| 144 | break; |
| 145 | default: |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 151 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 152 | // override this method to change the shader |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 153 | sk_sp<SkShader> createShader() { |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 154 | const SkColor colors[] = { |
| 155 | SK_ColorRED, SK_ColorCYAN, SK_ColorGREEN, SK_ColorWHITE, |
| 156 | SK_ColorMAGENTA, SK_ColorBLUE, SK_ColorYELLOW, |
| 157 | }; |
| 158 | const SkPoint pts[] = { { 200.f / 4.f, 0.f }, { 3.f * 200.f / 4, 200.f } }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 159 | |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 160 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 161 | SkShader::kMirror_TileMode); |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 165 | const char* onGetName() override { |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 166 | SkString vertexMode; |
| 167 | switch (fVertexMode) { |
| 168 | case kNone_VertexMode: |
| 169 | vertexMode.set("meshlines"); |
| 170 | break; |
| 171 | case kColors_VertexMode: |
| 172 | vertexMode.set("colors"); |
| 173 | break; |
| 174 | case kTexCoords_VertexMode: |
| 175 | vertexMode.set("texs"); |
| 176 | break; |
| 177 | case kBoth_VertexMode: |
| 178 | vertexMode.set("colors_texs"); |
| 179 | break; |
| 180 | default: |
| 181 | break; |
| 182 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 183 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 184 | SkString size; |
| 185 | switch (fSize) { |
| 186 | case kSmall_Size: |
| 187 | size.set("small"); |
| 188 | break; |
| 189 | case kMedium_Size: |
| 190 | size.set("medium"); |
| 191 | break; |
| 192 | case kBig_Size: |
| 193 | size.set("big"); |
| 194 | break; |
| 195 | default: |
| 196 | break; |
| 197 | } |
| 198 | fName.printf("patch_grid_%s_%s", vertexMode.c_str(), size.c_str()); |
| 199 | return fName.c_str(); |
| 200 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 201 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 202 | void onDelayedSetup() override { |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 203 | this->setGrid(); |
| 204 | switch (fVertexMode) { |
| 205 | case kTexCoords_VertexMode: |
| 206 | case kBoth_VertexMode: |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 207 | fPaint.setShader(createShader()); |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 208 | break; |
| 209 | default: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 210 | fPaint.setShader(nullptr); |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 211 | break; |
| 212 | } |
| 213 | this->setupPaint(&fPaint); |
| 214 | } |
| 215 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 216 | void onDraw(int loops, SkCanvas* canvas) override { |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 217 | this->setScale(canvas); |
| 218 | for (int i = 0; i < loops; i++) { |
| 219 | fGrid.draw(canvas, fPaint); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | SkPaint fPaint; |
| 224 | SkString fName; |
| 225 | SkPatchGrid fGrid; |
| 226 | VertexMode fVertexMode; |
| 227 | Size fSize; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 228 | |
dandov | 7e5598a | 2014-08-15 13:30:47 -0700 | [diff] [blame] | 229 | typedef Benchmark INHERITED; |
| 230 | }; |
| 231 | |
| 232 | |
| 233 | /////////////////////////////////////////////////////////////////////////////// |
| 234 | |
| 235 | DEF_BENCH( return new PatchGridBench(PatchGridBench::kSmall_Size, |
| 236 | PatchGridBench::kNone_VertexMode); ) |
| 237 | DEF_BENCH( return new PatchGridBench(PatchGridBench::kSmall_Size, |
| 238 | PatchGridBench::kColors_VertexMode); ) |
| 239 | DEF_BENCH( return new PatchGridBench(PatchGridBench::kSmall_Size, |
| 240 | PatchGridBench::kTexCoords_VertexMode); ) |
| 241 | DEF_BENCH( return new PatchGridBench(PatchGridBench::kSmall_Size, |
| 242 | PatchGridBench::kBoth_VertexMode); ) |
| 243 | DEF_BENCH( return new PatchGridBench(PatchGridBench::kMedium_Size, |
| 244 | PatchGridBench::kNone_VertexMode); ) |
| 245 | DEF_BENCH( return new PatchGridBench(PatchGridBench::kMedium_Size, |
| 246 | PatchGridBench::kColors_VertexMode); ) |
| 247 | DEF_BENCH( return new PatchGridBench(PatchGridBench::kMedium_Size, |
| 248 | PatchGridBench::kTexCoords_VertexMode); ) |
| 249 | DEF_BENCH( return new PatchGridBench(PatchGridBench::kMedium_Size, |
| 250 | PatchGridBench::kBoth_VertexMode); ) |
| 251 | DEF_BENCH( return new PatchGridBench(PatchGridBench::kBig_Size, |
| 252 | PatchGridBench::kNone_VertexMode); ) |
| 253 | DEF_BENCH( return new PatchGridBench(PatchGridBench::kBig_Size, |
| 254 | PatchGridBench::kColors_VertexMode); ) |
| 255 | DEF_BENCH( return new PatchGridBench(PatchGridBench::kBig_Size, |
| 256 | PatchGridBench::kTexCoords_VertexMode); ) |
| 257 | DEF_BENCH( return new PatchGridBench(PatchGridBench::kBig_Size, |
| 258 | PatchGridBench::kBoth_VertexMode); ) |