blob: 15c9befe6bc8dafe625f9f3b06f689e44580173b [file] [log] [blame]
reed@google.comc1f90112013-02-04 14:57:28 +00001/*
2 * Copyright 2013 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 "SkBenchmark.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRRect.h"
12#include "SkString.h"
13
14struct RRectRec {
15 SkCanvas* fCanvas;
16 SkRRect fRRect;
17 SkPaint fPaint;
18};
19
20typedef const char* (*DrawProc)(const RRectRec*, int);
21
22static const char* draw_rect(const RRectRec* rec, int count) {
23 if (rec) {
24 const SkRect& r = rec->fRRect.getBounds();
25 for (int i = 0; i < count; ++i) {
26 rec->fCanvas->drawRect(r, rec->fPaint);
27 }
28 }
29 return "rect";
30}
31
32static const char* draw_rrect(const RRectRec* rec, int count) {
33 if (rec) {
34 for (int i = 0; i < count; ++i) {
35 rec->fCanvas->drawRRect(rec->fRRect, rec->fPaint);
36 }
37 }
38 return "rrect";
39}
40
41static const char* draw_oval(const RRectRec* rec, int count) {
42 if (rec) {
43 const SkRect& r = rec->fRRect.getBounds();
44 for (int i = 0; i < count; ++i) {
45 rec->fCanvas->drawOval(r, rec->fPaint);
46 }
47 }
48 return "oval";
49}
50
51// Handles rect, rrect, and oval
52//
53// Test drawing a small stroked version to see the effect of special-casing
54// our stroke code for these convex single-contour shapes.
55//
56class StrokeRRectBench : public SkBenchmark {
57 SkString fName;
58 SkPaint::Join fJoin;
59 RRectRec fRec;
60 DrawProc fProc;
61 enum { N = SkBENCHLOOP(500) };
62public:
63 StrokeRRectBench(void* param, SkPaint::Join j, DrawProc proc) : SkBenchmark(param) {
64 static const char* gJoinName[] = {
65 "miter", "round", "bevel"
66 };
67
68 fJoin = j;
69 fProc = proc;
70 fName.printf("draw_stroke_%s_%s", proc(NULL, 0), gJoinName[j]);
skia.committer@gmail.com0c38ed32013-02-05 07:02:01 +000071
reed@google.comc1f90112013-02-04 14:57:28 +000072 SkRect r = { 20, 20, 40, 40 };
73 SkScalar rad = 4;
74 fRec.fRRect.setRectXY(r, rad, rad);
75 }
76
77protected:
78 virtual const char* onGetName() {
79 return fName.c_str();
80 }
81
82 virtual void onDraw(SkCanvas* canvas) {
83 fRec.fCanvas = canvas;
84 this->setupPaint(&fRec.fPaint);
85 fRec.fPaint.setStyle(SkPaint::kStroke_Style);
86 fRec.fPaint.setStrokeJoin(fJoin);
87 fRec.fPaint.setStrokeWidth(5);
88 fProc(&fRec, N);
89 }
90
91private:
92 typedef SkBenchmark INHERITED;
93};
94
95DEF_BENCH( return new StrokeRRectBench(p, SkPaint::kRound_Join, draw_rect); )
96DEF_BENCH( return new StrokeRRectBench(p, SkPaint::kBevel_Join, draw_rect); )
97DEF_BENCH( return new StrokeRRectBench(p, SkPaint::kMiter_Join, draw_rect); )
98
99DEF_BENCH( return new StrokeRRectBench(p, SkPaint::kRound_Join, draw_rrect); )
100DEF_BENCH( return new StrokeRRectBench(p, SkPaint::kBevel_Join, draw_rrect); )
101DEF_BENCH( return new StrokeRRectBench(p, SkPaint::kMiter_Join, draw_rrect); )
102
103DEF_BENCH( return new StrokeRRectBench(p, SkPaint::kRound_Join, draw_oval); )
104DEF_BENCH( return new StrokeRRectBench(p, SkPaint::kBevel_Join, draw_oval); )
105DEF_BENCH( return new StrokeRRectBench(p, SkPaint::kMiter_Join, draw_oval); )