1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2015 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "bench/Benchmark.h"
9cb93a386Sopenharmony_ci#include "include/core/SkPaint.h"
10cb93a386Sopenharmony_ci#include "include/core/SkPath.h"
11cb93a386Sopenharmony_ci#include "include/core/SkString.h"
12cb93a386Sopenharmony_ci#include "include/utils/SkRandom.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ciclass StrokeBench : public Benchmark {
15cb93a386Sopenharmony_cipublic:
16cb93a386Sopenharmony_ci    StrokeBench(const SkPath& path, const SkPaint& paint, const char pathType[], SkScalar res)
17cb93a386Sopenharmony_ci        : fPath(path), fPaint(paint), fRes(res)
18cb93a386Sopenharmony_ci    {
19cb93a386Sopenharmony_ci        fName.printf("build_stroke_%s_%g_%d_%d",
20cb93a386Sopenharmony_ci                     pathType, paint.getStrokeWidth(), paint.getStrokeJoin(), paint.getStrokeCap());
21cb93a386Sopenharmony_ci    }
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_ciprotected:
24cb93a386Sopenharmony_ci    bool isSuitableFor(Backend backend) override {
25cb93a386Sopenharmony_ci        return backend == kNonRendering_Backend;
26cb93a386Sopenharmony_ci    }
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ci    const char* onGetName() override { return fName.c_str(); }
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_ci    void onDraw(int loops, SkCanvas* canvas) override {
31cb93a386Sopenharmony_ci        SkPaint paint(fPaint);
32cb93a386Sopenharmony_ci        this->setupPaint(&paint);
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ci        for (int outer = 0; outer < 10; ++outer) {
35cb93a386Sopenharmony_ci            for (int i = 0; i < loops; ++i) {
36cb93a386Sopenharmony_ci                SkPath result;
37cb93a386Sopenharmony_ci                paint.getFillPath(fPath, &result, nullptr, fRes);
38cb93a386Sopenharmony_ci            }
39cb93a386Sopenharmony_ci        }
40cb93a386Sopenharmony_ci    }
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ciprivate:
43cb93a386Sopenharmony_ci    SkPath      fPath;
44cb93a386Sopenharmony_ci    SkPaint     fPaint;
45cb93a386Sopenharmony_ci    SkString    fName;
46cb93a386Sopenharmony_ci    SkScalar    fRes;
47cb93a386Sopenharmony_ci    using INHERITED = Benchmark;
48cb93a386Sopenharmony_ci};
49cb93a386Sopenharmony_ci
50cb93a386Sopenharmony_ci///////////////////////////////////////////////////////////////////////////////
51cb93a386Sopenharmony_ci
52cb93a386Sopenharmony_cistatic const int N = 100;
53cb93a386Sopenharmony_cistatic const SkScalar X = 100;
54cb93a386Sopenharmony_cistatic const SkScalar Y = 100;
55cb93a386Sopenharmony_ci
56cb93a386Sopenharmony_cistatic SkPoint rand_pt(SkRandom& rand) {
57cb93a386Sopenharmony_ci    return SkPoint::Make(rand.nextSScalar1() * X, rand.nextSScalar1() * Y);
58cb93a386Sopenharmony_ci}
59cb93a386Sopenharmony_ci
60cb93a386Sopenharmony_cistatic SkPath line_path_maker() {
61cb93a386Sopenharmony_ci    SkPath path;
62cb93a386Sopenharmony_ci    SkRandom rand;
63cb93a386Sopenharmony_ci    path.moveTo(rand_pt(rand));
64cb93a386Sopenharmony_ci    for (int i = 0; i < N; ++i) {
65cb93a386Sopenharmony_ci        path.lineTo(rand_pt(rand));
66cb93a386Sopenharmony_ci    }
67cb93a386Sopenharmony_ci    return path;
68cb93a386Sopenharmony_ci}
69cb93a386Sopenharmony_cistatic SkPath quad_path_maker() {
70cb93a386Sopenharmony_ci    SkPath path;
71cb93a386Sopenharmony_ci    SkRandom rand;
72cb93a386Sopenharmony_ci    path.moveTo(rand_pt(rand));
73cb93a386Sopenharmony_ci    for (int i = 0; i < N; ++i) {
74cb93a386Sopenharmony_ci        path.quadTo(rand_pt(rand), rand_pt(rand));
75cb93a386Sopenharmony_ci    }
76cb93a386Sopenharmony_ci    return path;
77cb93a386Sopenharmony_ci}
78cb93a386Sopenharmony_cistatic SkPath conic_path_maker() {
79cb93a386Sopenharmony_ci    SkPath path;
80cb93a386Sopenharmony_ci    SkRandom rand;
81cb93a386Sopenharmony_ci    path.moveTo(rand_pt(rand));
82cb93a386Sopenharmony_ci    for (int i = 0; i < N; ++i) {
83cb93a386Sopenharmony_ci        path.conicTo(rand_pt(rand), rand_pt(rand), rand.nextUScalar1());
84cb93a386Sopenharmony_ci    }
85cb93a386Sopenharmony_ci    return path;
86cb93a386Sopenharmony_ci}
87cb93a386Sopenharmony_cistatic SkPath cubic_path_maker() {
88cb93a386Sopenharmony_ci    SkPath path;
89cb93a386Sopenharmony_ci    SkRandom rand;
90cb93a386Sopenharmony_ci    path.moveTo(rand_pt(rand));
91cb93a386Sopenharmony_ci    for (int i = 0; i < N; ++i) {
92cb93a386Sopenharmony_ci        path.cubicTo(rand_pt(rand), rand_pt(rand), rand_pt(rand));
93cb93a386Sopenharmony_ci    }
94cb93a386Sopenharmony_ci    return path;
95cb93a386Sopenharmony_ci}
96cb93a386Sopenharmony_ci
97cb93a386Sopenharmony_cistatic SkPaint paint_maker() {
98cb93a386Sopenharmony_ci    SkPaint paint;
99cb93a386Sopenharmony_ci    paint.setStyle(SkPaint::kStroke_Style);
100cb93a386Sopenharmony_ci    paint.setStrokeWidth(X / 10);
101cb93a386Sopenharmony_ci    paint.setStrokeJoin(SkPaint::kMiter_Join);
102cb93a386Sopenharmony_ci    paint.setStrokeCap(SkPaint::kSquare_Cap);
103cb93a386Sopenharmony_ci    return paint;
104cb93a386Sopenharmony_ci}
105cb93a386Sopenharmony_ci
106cb93a386Sopenharmony_ciDEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_1", 1);)
107cb93a386Sopenharmony_ciDEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_1", 1);)
108cb93a386Sopenharmony_ciDEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_1", 1);)
109cb93a386Sopenharmony_ciDEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_1", 1);)
110cb93a386Sopenharmony_ci
111cb93a386Sopenharmony_ciDEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_4", 4);)
112cb93a386Sopenharmony_ciDEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_4", 4);)
113cb93a386Sopenharmony_ciDEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_4", 4);)
114cb93a386Sopenharmony_ciDEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_4", 4);)
115cb93a386Sopenharmony_ci
116cb93a386Sopenharmony_ciDEF_BENCH(return new StrokeBench(line_path_maker(), paint_maker(), "line_.25", .25f);)
117cb93a386Sopenharmony_ciDEF_BENCH(return new StrokeBench(quad_path_maker(), paint_maker(), "quad_.25", .25f);)
118cb93a386Sopenharmony_ciDEF_BENCH(return new StrokeBench(conic_path_maker(), paint_maker(), "conic_.25", .25f);)
119cb93a386Sopenharmony_ciDEF_BENCH(return new StrokeBench(cubic_path_maker(), paint_maker(), "cubic_.25", .25f);)
120