blob: a18b29da7d15458a3156e40b590596027a2a5ab3 [file] [log] [blame]
reed@google.comc56a83c2012-05-07 13:46:32 +00001
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
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000017static int rand_pts(SkRandom& rand, SkPoint pts[4]) {
reed@google.comc56a83c2012-05-07 13:46:32 +000018 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
reed@google.comc56a83c2012-05-07 13:46:32 +000033public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000034 PathIterBench(bool raw) {
reed@google.comc56a83c2012-05-07 13:46:32 +000035 fName.printf("pathiter_%s", raw ? "raw" : "consume");
36 fRaw = raw;
37
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000038 SkRandom rand;
reed@google.comc56a83c2012-05-07 13:46:32 +000039 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 }
robertphillips@google.com433ce5e2012-09-17 10:49:30 +000057
58 fIsRendering = false;
reed@google.comc56a83c2012-05-07 13:46:32 +000059 }
60
61protected:
62 virtual const char* onGetName() SK_OVERRIDE {
63 return fName.c_str();
64 }
65
sugoi@google.com77472f02013-03-05 18:50:01 +000066 virtual void onDraw(SkCanvas*) SK_OVERRIDE {
reed@google.comc56a83c2012-05-07 13:46:32 +000067 if (fRaw) {
mtklein@google.comc2897432013-09-10 19:23:38 +000068 for (int i = 0; i < this->getLoops(); ++i) {
reed@google.comc56a83c2012-05-07 13:46:32 +000069 SkPath::RawIter iter(fPath);
70 SkPath::Verb verb;
71 SkPoint pts[4];
rmistry@google.comfbfcd562012-08-23 18:09:54 +000072
commit-bot@chromium.orgfaa5ae42013-07-23 11:13:56 +000073 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
reed@google.comc56a83c2012-05-07 13:46:32 +000074 }
75 } else {
mtklein@google.comc2897432013-09-10 19:23:38 +000076 for (int i = 0; i < this->getLoops(); ++i) {
reed@google.comc56a83c2012-05-07 13:46:32 +000077 SkPath::Iter iter(fPath, false);
78 SkPath::Verb verb;
79 SkPoint pts[4];
rmistry@google.comfbfcd562012-08-23 18:09:54 +000080
commit-bot@chromium.orgfaa5ae42013-07-23 11:13:56 +000081 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
reed@google.comc56a83c2012-05-07 13:46:32 +000082 }
83 }
84 }
85
86private:
87 typedef SkBenchmark INHERITED;
88};
89
90///////////////////////////////////////////////////////////////////////////////
91
mtklein@google.com410e6e82013-09-13 19:52:27 +000092DEF_BENCH( return new PathIterBench(false); )
93DEF_BENCH( return new PathIterBench(true); )