blob: fd61c13a62109bf5cf4a648b7e30917ef8dfa0ae [file] [log] [blame]
reed@google.comc56a83c2012-05-07 13:46:32 +00001/*
2 * Copyright 2011 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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "bench/Benchmark.h"
8#include "include/core/SkBitmap.h"
9#include "include/core/SkCanvas.h"
10#include "include/core/SkColorPriv.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkPath.h"
13#include "include/core/SkShader.h"
14#include "include/core/SkString.h"
15#include "include/utils/SkRandom.h"
Mike Reed5f152f02019-08-19 14:40:16 -040016#include "src/core/SkPathPriv.h"
17
18enum class PathIterType {
19 kIter,
20 kRaw,
21 kEdge,
22};
23const char* gPathIterNames[] = {
24 "iter", "raw", "edge"
25};
reed@google.comc56a83c2012-05-07 13:46:32 +000026
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000027static int rand_pts(SkRandom& rand, SkPoint pts[4]) {
reed@google.comc56a83c2012-05-07 13:46:32 +000028 int n = rand.nextU() & 3;
29 n += 1;
30
31 for (int i = 0; i < n; ++i) {
32 pts[i].fX = rand.nextSScalar1();
33 pts[i].fY = rand.nextSScalar1();
34 }
35 return n;
36}
37
tfarinaf168b862014-06-19 12:32:29 -070038class PathIterBench : public Benchmark {
Mike Reed5f152f02019-08-19 14:40:16 -040039 SkString fName;
40 SkPath fPath;
41 PathIterType fType;
reed@google.comc56a83c2012-05-07 13:46:32 +000042
Mike Reed8f3bd772019-08-20 12:52:13 -040043 int fVerbInc = 0;
44 SkScalar fXInc = 0, fYInc = 0;
45
reed@google.comc56a83c2012-05-07 13:46:32 +000046public:
Mike Reed5f152f02019-08-19 14:40:16 -040047 PathIterBench(PathIterType t) : fType(t) {
48 fName.printf("pathiter_%s", gPathIterNames[static_cast<unsigned>(t)]);
reed@google.comc56a83c2012-05-07 13:46:32 +000049
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000050 SkRandom rand;
reed@google.comc56a83c2012-05-07 13:46:32 +000051 for (int i = 0; i < 1000; ++i) {
52 SkPoint pts[4];
53 int n = rand_pts(rand, pts);
54 switch (n) {
55 case 1:
56 fPath.moveTo(pts[0]);
57 break;
58 case 2:
59 fPath.lineTo(pts[1]);
60 break;
61 case 3:
62 fPath.quadTo(pts[1], pts[2]);
63 break;
64 case 4:
65 fPath.cubicTo(pts[1], pts[2], pts[3]);
66 break;
67 }
68 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000069 }
robertphillips@google.com433ce5e2012-09-17 10:49:30 +000070
mtklein36352bf2015-03-25 18:17:31 -070071 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000072 return backend == kNonRendering_Backend;
reed@google.comc56a83c2012-05-07 13:46:32 +000073 }
74
75protected:
mtklein36352bf2015-03-25 18:17:31 -070076 const char* onGetName() override {
reed@google.comc56a83c2012-05-07 13:46:32 +000077 return fName.c_str();
78 }
79
mtkleina1ebeb22015-10-01 09:43:39 -070080 void onDraw(int loops, SkCanvas*) override {
Mike Reed8f3bd772019-08-20 12:52:13 -040081 // Need to do *something* with the results, so the compile doesn't elide
82 // away the code we want to time.
83 auto handle = [this](int verb, const SkPoint pts[]) {
84 fVerbInc += verb;
85 fXInc += pts[0].fX;
86 fYInc += pts[0].fY;
87 };
88
Mike Reed5f152f02019-08-19 14:40:16 -040089 SkPath::Verb verb;
90 SkPoint pts[4];
91 switch (fType) {
92 case PathIterType::kIter:
93 for (int i = 0; i < loops; ++i) {
94 SkPath::Iter iter(fPath, true);
Mike Reed8f3bd772019-08-20 12:52:13 -040095 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
96 handle(verb, pts);
97 }
Mike Reed5f152f02019-08-19 14:40:16 -040098 }
99 break;
100 case PathIterType::kRaw:
101 for (int i = 0; i < loops; ++i) {
Chris Dalton8650d332020-05-05 16:21:19 -0600102 for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) {
103 handle((SkPath::Verb)verb, pts);
Mike Reed8f3bd772019-08-20 12:52:13 -0400104 }
Mike Reed5f152f02019-08-19 14:40:16 -0400105 }
106 break;
107 case PathIterType::kEdge:
108 for (int i = 0; i < loops; ++i) {
109 SkPathEdgeIter iter(fPath);
Mike Reed8f3bd772019-08-20 12:52:13 -0400110 while (auto r = iter.next()) {
111 handle((int)r.fEdge, r.fPts);
112 }
Mike Reed5f152f02019-08-19 14:40:16 -0400113 }
114 break;
reed@google.comc56a83c2012-05-07 13:46:32 +0000115 }
116 }
117
118private:
tfarinaf168b862014-06-19 12:32:29 -0700119 typedef Benchmark INHERITED;
reed@google.comc56a83c2012-05-07 13:46:32 +0000120};
121
122///////////////////////////////////////////////////////////////////////////////
123
Mike Reed5f152f02019-08-19 14:40:16 -0400124DEF_BENCH( return new PathIterBench(PathIterType::kIter); )
125DEF_BENCH( return new PathIterBench(PathIterType::kRaw); )
126DEF_BENCH( return new PathIterBench(PathIterType::kEdge); )