| reed@android.com | bd700c3 | 2009-01-05 03:34:50 +0000 | [diff] [blame] | 1 | #include "SkBenchmark.h" |
| 2 | #include "SkCanvas.h" |
| 3 | #include "SkPaint.h" |
| 4 | #include "SkRandom.h" |
| 5 | |
| 6 | class RectBench : public SkBenchmark { |
| 7 | public: |
| 8 | enum { |
| 9 | W = 640, |
| 10 | H = 480, |
| 11 | N = 100 |
| 12 | }; |
| 13 | SkRect fRects[N]; |
| 14 | SkColor fColors[N]; |
| 15 | |
| 16 | RectBench() { |
| 17 | SkRandom rand; |
| 18 | for (int i = 0; i < N; i++) { |
| 19 | int x = rand.nextU() % W; |
| 20 | int y = rand.nextU() % H; |
| 21 | int w = rand.nextU() % W; |
| 22 | int h = rand.nextU() % H; |
| 23 | w >>= 1; |
| 24 | h >>= 1; |
| 25 | x -= w/2; |
| 26 | y -= h/2; |
| 27 | fRects[i].set(SkIntToScalar(x), SkIntToScalar(y), |
| 28 | SkIntToScalar(x+w), SkIntToScalar(y+h)); |
| 29 | fColors[i] = rand.nextU() | 0xFF808080; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | protected: |
| 34 | virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) { |
| 35 | c->drawRect(r, p); |
| 36 | } |
| 37 | |
| 38 | virtual const char* onGetName() { return "rectangles"; } |
| 39 | virtual SkIPoint onGetSize() { return SkMakeIPoint(640, 480); } |
| 40 | virtual void onDraw(SkCanvas* canvas) { |
| 41 | SkPaint paint; |
| 42 | for (int i = 0; i < N; i++) { |
| 43 | paint.setColor(fColors[i]); |
| 44 | this->drawThisRect(canvas, fRects[i], paint); |
| 45 | } |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | class OvalBench : public RectBench { |
| 50 | protected: |
| 51 | virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) { |
| 52 | c->drawOval(r, p); |
| 53 | } |
| 54 | virtual const char* onGetName() { return "ovals"; } |
| 55 | }; |
| 56 | |
| 57 | class RRectBench : public RectBench { |
| 58 | protected: |
| 59 | virtual void drawThisRect(SkCanvas* c, const SkRect& r, const SkPaint& p) { |
| 60 | c->drawRoundRect(r, r.width() / 4, r.height() / 4, p); |
| 61 | } |
| 62 | virtual const char* onGetName() { return "roundrects"; } |
| 63 | }; |
| 64 | |
| 65 | class PointsBench : public RectBench { |
| 66 | public: |
| 67 | SkCanvas::PointMode fMode; |
| 68 | const char* fName; |
| 69 | |
| 70 | PointsBench(SkCanvas::PointMode mode, const char* name) : fMode(mode) { |
| 71 | fName = name; |
| 72 | } |
| 73 | |
| 74 | protected: |
| 75 | virtual void onDraw(SkCanvas* canvas) { |
| 76 | static const SkScalar gSizes[] = { |
| 77 | SkIntToScalar(7), 0 |
| 78 | }; |
| 79 | |
| 80 | SkPaint paint; |
| 81 | paint.setAntiAlias(true); |
| 82 | paint.setStrokeCap(SkPaint::kRound_Cap); |
| 83 | |
| 84 | for (size_t i = 0; i < SK_ARRAY_COUNT(gSizes); i++) { |
| 85 | paint.setStrokeWidth(gSizes[i]); |
| 86 | canvas->drawPoints(fMode, N * 2, |
| 87 | reinterpret_cast<const SkPoint*>(fRects), paint); |
| 88 | paint.setColor(fColors[i]); |
| 89 | } |
| 90 | } |
| 91 | virtual const char* onGetName() { return fName; } |
| 92 | }; |
| 93 | |
| 94 | static SkBenchmark* RectFactory() { return SkNEW(RectBench); } |
| 95 | static SkBenchmark* OvalFactory() { return SkNEW(OvalBench); } |
| 96 | static SkBenchmark* RRectFactory() { return SkNEW(RRectBench); } |
| 97 | static SkBenchmark* PointsFactory() { |
| 98 | return SkNEW_ARGS(PointsBench, (SkCanvas::kPoints_PointMode, "points")); |
| 99 | } |
| 100 | static SkBenchmark* LinesFactory() { |
| 101 | return SkNEW_ARGS(PointsBench, (SkCanvas::kLines_PointMode, "lines")); |
| 102 | } |
| 103 | static SkBenchmark* PolygonFactory() { |
| 104 | return SkNEW_ARGS(PointsBench, (SkCanvas::kPolygon_PointMode, "polygon")); |
| 105 | } |
| 106 | |
| 107 | static SkTRegistry<SkBenchmark> gRectReg(RectFactory); |
| 108 | static SkTRegistry<SkBenchmark> gOvalReg(OvalFactory); |
| 109 | static SkTRegistry<SkBenchmark> gRRectReg(RRectFactory); |
| 110 | static SkTRegistry<SkBenchmark> gPointsReg(PointsFactory); |
| 111 | static SkTRegistry<SkBenchmark> gLinesReg(LinesFactory); |
| 112 | static SkTRegistry<SkBenchmark> gPolygonReg(PolygonFactory); |
| 113 | |