2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 #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"
16 #include "src/core/SkPathPriv.h"
18 enum class PathIterType {
23 const char* gPathIterNames[] = {
27 static int rand_pts(SkRandom& rand, SkPoint pts[4]) {
28 int n = rand.nextU() & 3;
31 for (int i = 0; i < n; ++i) {
32 pts[i].fX = rand.nextSScalar1();
33 pts[i].fY = rand.nextSScalar1();
38 class PathIterBench : public Benchmark {
44 SkScalar fXInc = 0, fYInc = 0;
47 PathIterBench(PathIterType t) : fType(t) {
48 fName.printf("pathiter_%s", gPathIterNames[static_cast<unsigned>(t)]);
51 for (int i = 0; i < 1000; ++i) {
53 int n = rand_pts(rand, pts);
62 fPath.quadTo(pts[1], pts[2]);
65 fPath.cubicTo(pts[1], pts[2], pts[3]);
71 bool isSuitableFor(Backend backend) override {
72 return backend == kNonRendering_Backend;
76 const char* onGetName() override {
80 void onDraw(int loops, SkCanvas*) override {
81 // 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[]) {
90 case PathIterType::kIter:
91 for (int i = 0; i < loops; ++i) {
92 SkPath::Iter iter(fPath, true);
95 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
100 case PathIterType::kRaw:
101 for (int i = 0; i < loops; ++i) {
102 for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) {
103 handle((SkPath::Verb)verb, pts);
107 case PathIterType::kEdge:
108 for (int i = 0; i < loops; ++i) {
109 SkPathEdgeIter iter(fPath);
110 while (auto r = iter.next()) {
111 handle((int)r.fEdge, r.fPts);
119 using INHERITED = Benchmark;
122 ///////////////////////////////////////////////////////////////////////////////
124 DEF_BENCH( return new PathIterBench(PathIterType::kIter); )
125 DEF_BENCH( return new PathIterBench(PathIterType::kRaw); )
126 DEF_BENCH( return new PathIterBench(PathIterType::kEdge); )