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