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