blob: bbc3c035e783b246fcc5e01b76205595647ea003 [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 */
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.comc56a83c2012-05-07 13:46:32 +00009#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
tfarinaf168b862014-06-19 12:32:29 -070028class PathIterBench : public Benchmark {
reed@google.comc56a83c2012-05-07 13:46:32 +000029 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 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000057 }
robertphillips@google.com433ce5e2012-09-17 10:49:30 +000058
mtklein36352bf2015-03-25 18:17:31 -070059 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000060 return backend == kNonRendering_Backend;
reed@google.comc56a83c2012-05-07 13:46:32 +000061 }
62
63protected:
mtklein36352bf2015-03-25 18:17:31 -070064 const char* onGetName() override {
reed@google.comc56a83c2012-05-07 13:46:32 +000065 return fName.c_str();
66 }
67
mtklein36352bf2015-03-25 18:17:31 -070068 void onDraw(const int loops, SkCanvas*) override {
reed@google.comc56a83c2012-05-07 13:46:32 +000069 if (fRaw) {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000070 for (int i = 0; i < loops; ++i) {
reed@google.comc56a83c2012-05-07 13:46:32 +000071 SkPath::RawIter iter(fPath);
72 SkPath::Verb verb;
73 SkPoint pts[4];
rmistry@google.comfbfcd562012-08-23 18:09:54 +000074
commit-bot@chromium.orgfaa5ae42013-07-23 11:13:56 +000075 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
reed@google.comc56a83c2012-05-07 13:46:32 +000076 }
77 } else {
commit-bot@chromium.org33614712013-12-03 18:17:16 +000078 for (int i = 0; i < loops; ++i) {
reed@google.comc56a83c2012-05-07 13:46:32 +000079 SkPath::Iter iter(fPath, false);
80 SkPath::Verb verb;
81 SkPoint pts[4];
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082
commit-bot@chromium.orgfaa5ae42013-07-23 11:13:56 +000083 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { }
reed@google.comc56a83c2012-05-07 13:46:32 +000084 }
85 }
86 }
87
88private:
tfarinaf168b862014-06-19 12:32:29 -070089 typedef Benchmark INHERITED;
reed@google.comc56a83c2012-05-07 13:46:32 +000090};
91
92///////////////////////////////////////////////////////////////////////////////
93
mtklein@google.com410e6e82013-09-13 19:52:27 +000094DEF_BENCH( return new PathIterBench(false); )
95DEF_BENCH( return new PathIterBench(true); )