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 "Benchmark.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 Benchmark {
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
59    virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
60        return backend == kNonRendering_Backend;
61    }
62
63protected:
64    virtual const char* onGetName() SK_OVERRIDE {
65        return fName.c_str();
66    }
67
68    virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
69        if (fRaw) {
70            for (int i = 0; i < loops; ++i) {
71                SkPath::RawIter iter(fPath);
72                SkPath::Verb verb;
73                SkPoint      pts[4];
74
75                while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
76            }
77        } else {
78            for (int i = 0; i < loops; ++i) {
79                SkPath::Iter iter(fPath, false);
80                SkPath::Verb verb;
81                SkPoint      pts[4];
82
83                while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
84            }
85        }
86    }
87
88private:
89    typedef Benchmark INHERITED;
90};
91
92///////////////////////////////////////////////////////////////////////////////
93
94DEF_BENCH( return new PathIterBench(false); )
95DEF_BENCH( return new PathIterBench(true); )
96