| OLD | NEW |
| (Empty) | |
| 1 |
| 2 /* |
| 3 * Copyright 2016 Google Inc. |
| 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. |
| 7 */ |
| 8 |
| 9 #include "Benchmark.h" |
| 10 #include "SkCanvas.h" |
| 11 #include "SkCommandLineFlags.h" |
| 12 #include "SkPaint.h" |
| 13 #include "SkRandom.h" |
| 14 #include "SkRRect.h" |
| 15 #include "SkString.h" |
| 16 #include <stdio.h> |
| 17 #include <stdlib.h> |
| 18 #include <functional> |
| 19 |
| 20 #define ENABLE_COMMAND_LINE_SHAPES_BENCH 0 |
| 21 |
| 22 #if ENABLE_COMMAND_LINE_SHAPES_BENCH |
| 23 DEFINE_string(shapesType, "mixed", "Type of shape to use in ShapesBench. Must be
one of: " |
| 24 "rect, oval, rrect, mixed."); |
| 25 DEFINE_string(innerShapesType, "none", "Type of inner shape to use in ShapesBenc
h. Must be one of: " |
| 26 "none, rect, oval, rrect, mixed."); |
| 27 DEFINE_int32(numShapes, 10000, "Number of shapes to draw in ShapesBench."); |
| 28 DEFINE_string(shapesSize, "32x32", "Size of shapes to draw in ShapesBench."); |
| 29 DEFINE_bool(shapesPersp, false, "Use slight perspective tilt in ShapesBench?"); |
| 30 #endif |
| 31 |
| 32 class ShapesBench : public Benchmark { |
| 33 public: |
| 34 enum ShapesType { |
| 35 kNone_ShapesType, |
| 36 kRect_ShapesType, |
| 37 kOval_ShapesType, |
| 38 kRRect_ShapesType, |
| 39 kMixed_ShapesType |
| 40 }; |
| 41 |
| 42 ShapesBench(ShapesType shapesType, ShapesType innerShapesType, |
| 43 int numShapes, const SkISize& shapesSize, bool perspective) |
| 44 : fShapesType(shapesType) |
| 45 , fInnerShapesType(innerShapesType) |
| 46 , fNumShapes(numShapes) |
| 47 , fShapesSize(shapesSize) |
| 48 , fPerspective(perspective) { |
| 49 clampShapeSize(); |
| 50 } |
| 51 |
| 52 #if ENABLE_COMMAND_LINE_SHAPES_BENCH |
| 53 ShapesBench() { |
| 54 if (!strcmp(FLAGS_shapesType[0], "rect")) { |
| 55 fShapesType = kRect_ShapesType; |
| 56 } else if (!strcmp(FLAGS_shapesType[0], "oval")) { |
| 57 fShapesType = kOval_ShapesType; |
| 58 } else if (!strcmp(FLAGS_shapesType[0], "rrect")) { |
| 59 fShapesType = kRRect_ShapesType; |
| 60 } else if (!strcmp(FLAGS_shapesType[0], "mixed")) { |
| 61 fShapesType = kMixed_ShapesType; |
| 62 } else { |
| 63 SkDebugf("Invalid shapesType \"%s\". Must be one of: rect, oval, rre
ct, mixed.", |
| 64 FLAGS_shapesType[0]); |
| 65 exit(-1); |
| 66 } |
| 67 if (!strcmp(FLAGS_innerShapesType[0], "none")) { |
| 68 fInnerShapesType = kNone_ShapesType; |
| 69 } else if (!strcmp(FLAGS_innerShapesType[0], "rect")) { |
| 70 fInnerShapesType = kRect_ShapesType; |
| 71 } else if (!strcmp(FLAGS_innerShapesType[0], "oval")) { |
| 72 fInnerShapesType = kOval_ShapesType; |
| 73 } else if (!strcmp(FLAGS_innerShapesType[0], "rrect")) { |
| 74 fInnerShapesType = kRRect_ShapesType; |
| 75 } else if (!strcmp(FLAGS_innerShapesType[0], "mixed")) { |
| 76 fInnerShapesType = kMixed_ShapesType; |
| 77 } else { |
| 78 SkDebugf("Invalid innerShapesType \"%s\". Must be one of: " |
| 79 "none, rect, oval, rrect, mixed.", FLAGS_innerShapesType[0]
); |
| 80 exit(-1); |
| 81 } |
| 82 if (2 != sscanf(FLAGS_shapesSize[0], "%ix%i", &fShapesSize.fWidth, &fSha
pesSize.fHeight)) { |
| 83 SkDebugf("Could not parse shapesSize from \"%s\". Expected \"%%ix%%i
\"\n", |
| 84 FLAGS_shapesSize[0]); |
| 85 exit(-1); |
| 86 } |
| 87 |
| 88 fNumShapes = FLAGS_numShapes; |
| 89 fPerspective = FLAGS_shapesPersp; |
| 90 |
| 91 clampShapeSize(); |
| 92 } |
| 93 #endif |
| 94 |
| 95 bool isVisual() override { return true; } |
| 96 |
| 97 private: |
| 98 void clampShapeSize() { |
| 99 float maxDiagonal = static_cast<float>(SkTMin(kBenchWidth, kBenchHeight)
); |
| 100 float diagonal = sqrtf(static_cast<float>(fShapesSize.width() * fShapesS
ize.width()) + |
| 101 static_cast<float>(fShapesSize.height() * fShapes
Size.height())); |
| 102 if (diagonal > maxDiagonal) { |
| 103 fShapesSize.fWidth = static_cast<int>(fShapesSize.width() * maxDiago
nal / diagonal); |
| 104 fShapesSize.fHeight = static_cast<int>(fShapesSize.height() * maxDia
gonal / diagonal); |
| 105 } |
| 106 } |
| 107 |
| 108 const char* onGetName() override { |
| 109 const char* shapeTypeNames[] = { |
| 110 "none", "rect", "oval", "rrect", "mixed" |
| 111 }; |
| 112 |
| 113 fName.printf("shapes_%s", shapeTypeNames[fShapesType]); |
| 114 |
| 115 if (kNone_ShapesType != fInnerShapesType) { |
| 116 fName.appendf("_%s", shapeTypeNames[fInnerShapesType]); |
| 117 } |
| 118 |
| 119 fName.appendf("_%i_%ix%i", fNumShapes, fShapesSize.width(), fShapesSize.
height()); |
| 120 |
| 121 if (fPerspective) { |
| 122 fName.append("_persp"); |
| 123 } |
| 124 |
| 125 return fName.c_str(); |
| 126 } |
| 127 SkIPoint onGetSize() override { return SkIPoint::Make(kBenchWidth, kBenchHei
ght); } |
| 128 |
| 129 void onDelayedSetup() override { |
| 130 SkScalar w = SkIntToScalar(fShapesSize.width()); |
| 131 SkScalar h = SkIntToScalar(fShapesSize.height()); |
| 132 |
| 133 fRect.setRect(SkRect::MakeXYWH(-w / 2, -h / 2, w, h)); |
| 134 fOval.setOval(fRect.rect()); |
| 135 fRRect.setNinePatch(fRect.rect(), w / 8, h / 13, w / 11, h / 7); |
| 136 |
| 137 if (kNone_ShapesType != fInnerShapesType) { |
| 138 fRect.inset(w / 7, h / 11, &fInnerRect); |
| 139 fInnerRect.offset(w / 28, h / 44); |
| 140 fInnerOval.setOval(fInnerRect.rect()); |
| 141 fInnerRRect.setRectXY(fInnerRect.rect(), w / 13, w / 7); |
| 142 } |
| 143 |
| 144 SkRandom rand; |
| 145 fShapes.push_back_n(fNumShapes); |
| 146 for (int i = 0; i < fNumShapes; i++) { |
| 147 float pad = sqrtf(static_cast<float>(fShapesSize.width() * fShapesSi
ze.width()) + |
| 148 static_cast<float>(fShapesSize.height() * fShapesS
ize.height())); |
| 149 fShapes[i].fMatrix.setTranslate(0.5 * pad + rand.nextF() * (kBenchWi
dth - pad), |
| 150 0.5 * pad + rand.nextF() * (kBenchHe
ight - pad)); |
| 151 fShapes[i].fMatrix.preRotate(rand.nextF() * 360.0f); |
| 152 if (fPerspective) { |
| 153 fShapes[i].fMatrix.setPerspX(0.00015); |
| 154 fShapes[i].fMatrix.setPerspY(-0.00015); |
| 155 } |
| 156 fShapes[i].fColor = rand.nextU() | 0xff808080; |
| 157 } |
| 158 for (int i = 0; i < fNumShapes; i++) { |
| 159 // Do this in a separate loop so mixed shapes get the same random nu
mbers during |
| 160 // placement as non-mixed do. |
| 161 int shapeType = fShapesType; |
| 162 if (kMixed_ShapesType == shapeType) { |
| 163 shapeType = rand.nextRangeU(kRect_ShapesType, kRRect_ShapesType)
; |
| 164 } |
| 165 int innerShapeType = fInnerShapesType; |
| 166 if (kMixed_ShapesType == innerShapeType) { |
| 167 innerShapeType = rand.nextRangeU(kRect_ShapesType, kRRect_Shapes
Type); |
| 168 } |
| 169 if (kNone_ShapesType == innerShapeType) { |
| 170 switch (shapeType) { |
| 171 using namespace std; |
| 172 using namespace std::placeholders; |
| 173 case kRect_ShapesType: |
| 174 fShapes[i].fDraw = bind(&SkCanvas::drawRect, _1, cref(fR
ect.rect()), _2); |
| 175 break; |
| 176 case kOval_ShapesType: |
| 177 fShapes[i].fDraw = bind(&SkCanvas::drawOval, _1, cref(fO
val.rect()), _2); |
| 178 break; |
| 179 case kRRect_ShapesType: |
| 180 fShapes[i].fDraw = bind(&SkCanvas::drawRRect, _1, cref(f
RRect), _2); |
| 181 break; |
| 182 } |
| 183 } else { |
| 184 const SkRRect* outer; |
| 185 switch (shapeType) { |
| 186 case kRect_ShapesType: outer = &fRect; break; |
| 187 case kOval_ShapesType: outer = &fOval; break; |
| 188 case kRRect_ShapesType: outer = &fRRect; break; |
| 189 } |
| 190 const SkRRect* inner; |
| 191 switch (innerShapeType) { |
| 192 case kRect_ShapesType: inner = &fInnerRect; break; |
| 193 case kOval_ShapesType: inner = &fInnerOval; break; |
| 194 case kRRect_ShapesType: inner = &fInnerRRect; break; |
| 195 } |
| 196 fShapes[i].fDraw = std::bind(&SkCanvas::drawDRRect, std::placeho
lders::_1, |
| 197 std::cref(*outer), std::cref(*inner
), |
| 198 std::placeholders::_2); |
| 199 } |
| 200 } |
| 201 } |
| 202 |
| 203 void onDraw(int loops, SkCanvas* canvas) override { |
| 204 SkPaint paint; |
| 205 this->setupPaint(&paint); |
| 206 for (int j = 0; j < loops; j++) { |
| 207 for (int i = 0; i < fNumShapes; i++) { |
| 208 canvas->save(); |
| 209 canvas->setMatrix(fShapes[i].fMatrix); |
| 210 paint.setColor(fShapes[i].fColor); |
| 211 fShapes[i].fDraw(canvas, paint); |
| 212 canvas->restore(); |
| 213 } |
| 214 } |
| 215 } |
| 216 |
| 217 enum { |
| 218 kBenchWidth = 1000, |
| 219 kBenchHeight = 1000 |
| 220 }; |
| 221 |
| 222 struct ShapeInfo { |
| 223 SkMatrix fMatrix; |
| 224 SkColor fColor; |
| 225 std::function<void(SkCanvas*, const SkPaint&)> fDraw; |
| 226 }; |
| 227 |
| 228 ShapesType fShapesType; |
| 229 ShapesType fInnerShapesType; |
| 230 int fNumShapes; |
| 231 SkISize fShapesSize; |
| 232 bool fPerspective; |
| 233 SkString fName; |
| 234 SkRRect fRect; |
| 235 SkRRect fOval; |
| 236 SkRRect fRRect; |
| 237 SkRRect fInnerRect; |
| 238 SkRRect fInnerOval; |
| 239 SkRRect fInnerRRect; |
| 240 SkTArray<ShapeInfo> fShapes; |
| 241 |
| 242 |
| 243 typedef Benchmark INHERITED; |
| 244 }; |
| 245 |
| 246 // Small primitives (CPU bound, in theory): |
| 247 DEF_BENCH(return new ShapesBench(ShapesBench::kRect_ShapesType, ShapesBench::kNo
ne_ShapesType, |
| 248 10000, SkISize::Make(32, 32), false);) |
| 249 DEF_BENCH(return new ShapesBench(ShapesBench::kOval_ShapesType, ShapesBench::kNo
ne_ShapesType, |
| 250 10000, SkISize::Make(32, 32), false);) |
| 251 DEF_BENCH(return new ShapesBench(ShapesBench::kOval_ShapesType, ShapesBench::kNo
ne_ShapesType, |
| 252 10000, SkISize::Make(32, 33), false);) |
| 253 DEF_BENCH(return new ShapesBench(ShapesBench::kRRect_ShapesType, ShapesBench::kN
one_ShapesType, |
| 254 10000, SkISize::Make(32, 32), false);) |
| 255 DEF_BENCH(return new ShapesBench(ShapesBench::kMixed_ShapesType, ShapesBench::kN
one_ShapesType, |
| 256 10000, SkISize::Make(32, 32), false);) |
| 257 |
| 258 // Large primitives (GPU bound, in theory): |
| 259 DEF_BENCH(return new ShapesBench(ShapesBench::kRect_ShapesType, ShapesBench::kNo
ne_ShapesType, |
| 260 1000, SkISize::Make(500, 500), false);) |
| 261 DEF_BENCH(return new ShapesBench(ShapesBench::kOval_ShapesType, ShapesBench::kNo
ne_ShapesType, |
| 262 1000, SkISize::Make(500, 500), false);) |
| 263 DEF_BENCH(return new ShapesBench(ShapesBench::kOval_ShapesType, ShapesBench::kNo
ne_ShapesType, |
| 264 1000, SkISize::Make(500, 501), false);) |
| 265 DEF_BENCH(return new ShapesBench(ShapesBench::kRRect_ShapesType, ShapesBench::kN
one_ShapesType, |
| 266 1000, SkISize::Make(500, 500), false);) |
| 267 DEF_BENCH(return new ShapesBench(ShapesBench::kMixed_ShapesType, ShapesBench::kN
one_ShapesType, |
| 268 1000, SkISize::Make(500, 500), false);) |
| 269 |
| 270 // Donuts (small and large). These fall-back to path rendering due to non-orthog
onal rotation |
| 271 // making them quite slow. Thus, reduce the counts substantially: |
| 272 DEF_BENCH(return new ShapesBench(ShapesBench::kRect_ShapesType, ShapesBench::kRe
ct_ShapesType, |
| 273 2000, SkISize::Make(32, 32), false);) |
| 274 DEF_BENCH(return new ShapesBench(ShapesBench::kRRect_ShapesType, ShapesBench::kR
Rect_ShapesType, |
| 275 2000, SkISize::Make(32, 32), false);) |
| 276 DEF_BENCH(return new ShapesBench(ShapesBench::kRect_ShapesType, ShapesBench::kRe
ct_ShapesType, |
| 277 200, SkISize::Make(500, 500), false);) |
| 278 DEF_BENCH(return new ShapesBench(ShapesBench::kRRect_ShapesType, ShapesBench::kR
Rect_ShapesType, |
| 279 200, SkISize::Make(500, 500), false);) |
| 280 |
| 281 #if ENABLE_COMMAND_LINE_SHAPES_BENCH |
| 282 DEF_BENCH(return new ShapesBench;) |
| 283 #endif |
| OLD | NEW |