PathIterBench.cpp revision 410e6e80f00a6c660675c80904807a041c7b7d2a
1
2/*
3 * Copyright 2011 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 "SkBenchmark.h"
9#include "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkColorPriv.h"
12#include "SkPaint.h"
13#include "SkRandom.h"
14#include "SkShader.h"
15#include "SkString.h"
16
17static int rand_pts(SkRandom& rand, SkPoint pts[4]) {
18    int n = rand.nextU() & 3;
19    n += 1;
20
21    for (int i = 0; i < n; ++i) {
22        pts[i].fX = rand.nextSScalar1();
23        pts[i].fY = rand.nextSScalar1();
24    }
25    return n;
26}
27
28class PathIterBench : public SkBenchmark {
29    SkString    fName;
30    SkPath      fPath;
31    bool        fRaw;
32
33public:
34    PathIterBench(bool raw)  {
35        fName.printf("pathiter_%s", raw ? "raw" : "consume");
36        fRaw = raw;
37
38        SkRandom rand;
39        for (int i = 0; i < 1000; ++i) {
40            SkPoint pts[4];
41            int n = rand_pts(rand, pts);
42            switch (n) {
43                case 1:
44                    fPath.moveTo(pts[0]);
45                    break;
46                case 2:
47                    fPath.lineTo(pts[1]);
48                    break;
49                case 3:
50                    fPath.quadTo(pts[1], pts[2]);
51                    break;
52                case 4:
53                    fPath.cubicTo(pts[1], pts[2], pts[3]);
54                    break;
55            }
56        }
57
58        fIsRendering = false;
59    }
60
61protected:
62    virtual const char* onGetName() SK_OVERRIDE {
63        return fName.c_str();
64    }
65
66    virtual void onDraw(SkCanvas*) SK_OVERRIDE {
67        if (fRaw) {
68            for (int i = 0; i < this->getLoops(); ++i) {
69                SkPath::RawIter iter(fPath);
70                SkPath::Verb verb;
71                SkPoint      pts[4];
72
73                while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
74            }
75        } else {
76            for (int i = 0; i < this->getLoops(); ++i) {
77                SkPath::Iter iter(fPath, false);
78                SkPath::Verb verb;
79                SkPoint      pts[4];
80
81                while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
82            }
83        }
84    }
85
86private:
87    typedef SkBenchmark INHERITED;
88};
89
90///////////////////////////////////////////////////////////////////////////////
91
92DEF_BENCH( return new PathIterBench(false); )
93DEF_BENCH( return new PathIterBench(true); )
94