1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2014 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/SkCanvas.h"
10cb93a386Sopenharmony_ci#include "include/core/SkPaint.h"
11cb93a386Sopenharmony_ci#include "include/core/SkPath.h"
12cb93a386Sopenharmony_ci#include "include/core/SkString.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_cistruct BezierRec {
15cb93a386Sopenharmony_ci    SkCanvas*   fCanvas;
16cb93a386Sopenharmony_ci    SkPaint     fPaint;
17cb93a386Sopenharmony_ci    SkPath      fQuad;
18cb93a386Sopenharmony_ci    SkPath      fCubic;
19cb93a386Sopenharmony_ci};
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_citypedef const char* (*DrawProc)(const BezierRec*, int);
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_cistatic const char* draw_quad(const BezierRec* rec, int count) {
24cb93a386Sopenharmony_ci    if (rec) {
25cb93a386Sopenharmony_ci        SkCanvas* canvas = rec->fCanvas;
26cb93a386Sopenharmony_ci        const SkPaint& paint = rec->fPaint;
27cb93a386Sopenharmony_ci        const SkPath& path = rec->fQuad;
28cb93a386Sopenharmony_ci        for (int i = 0; i < count; ++i) {
29cb93a386Sopenharmony_ci            canvas->drawPath(path, paint);
30cb93a386Sopenharmony_ci        }
31cb93a386Sopenharmony_ci    }
32cb93a386Sopenharmony_ci    return "quad";
33cb93a386Sopenharmony_ci}
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_cistatic const char* draw_cubic(const BezierRec* rec, int count) {
36cb93a386Sopenharmony_ci    if (rec) {
37cb93a386Sopenharmony_ci        SkCanvas* canvas = rec->fCanvas;
38cb93a386Sopenharmony_ci        const SkPaint& paint = rec->fPaint;
39cb93a386Sopenharmony_ci        const SkPath& path = rec->fCubic;
40cb93a386Sopenharmony_ci        for (int i = 0; i < count; ++i) {
41cb93a386Sopenharmony_ci            canvas->drawPath(path, paint);
42cb93a386Sopenharmony_ci        }
43cb93a386Sopenharmony_ci    }
44cb93a386Sopenharmony_ci    return "cubic";
45cb93a386Sopenharmony_ci}
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ciclass BezierBench : public Benchmark {
48cb93a386Sopenharmony_ci    SkString fName;
49cb93a386Sopenharmony_ci    SkPaint::Cap fCap;
50cb93a386Sopenharmony_ci    SkPaint::Join fJoin;
51cb93a386Sopenharmony_ci    BezierRec fRec;
52cb93a386Sopenharmony_ci    DrawProc fProc;
53cb93a386Sopenharmony_ci    SkScalar fWidth;
54cb93a386Sopenharmony_cipublic:
55cb93a386Sopenharmony_ci    BezierBench(SkPaint::Cap c, SkPaint::Join j, SkScalar w, DrawProc proc) {
56cb93a386Sopenharmony_ci        static const char* gCapName[] = {
57cb93a386Sopenharmony_ci            "butt", "round", "square"
58cb93a386Sopenharmony_ci        };
59cb93a386Sopenharmony_ci        static const char* gJoinName[] = {
60cb93a386Sopenharmony_ci            "miter", "round", "bevel"
61cb93a386Sopenharmony_ci        };
62cb93a386Sopenharmony_ci
63cb93a386Sopenharmony_ci        fCap = c;
64cb93a386Sopenharmony_ci        fJoin = j;
65cb93a386Sopenharmony_ci        fProc = proc;
66cb93a386Sopenharmony_ci        fWidth = SkIntToScalar(w);
67cb93a386Sopenharmony_ci        fName.printf("draw_stroke_bezier_%s_%s_%s_%g", proc(nullptr, 0), gCapName[c], gJoinName[j], w);
68cb93a386Sopenharmony_ci
69cb93a386Sopenharmony_ci        fRec.fQuad.moveTo(20, 20);
70cb93a386Sopenharmony_ci        fRec.fQuad.quadTo(60, 20, 60, 60);
71cb93a386Sopenharmony_ci        fRec.fQuad.quadTo(20, 60, 20, 100);
72cb93a386Sopenharmony_ci        fRec.fCubic.moveTo(20, 20);
73cb93a386Sopenharmony_ci        fRec.fCubic.cubicTo(40, 20, 60, 40, 60, 60);
74cb93a386Sopenharmony_ci        fRec.fCubic.cubicTo(40, 60, 20, 80, 20, 100);
75cb93a386Sopenharmony_ci    }
76cb93a386Sopenharmony_ci
77cb93a386Sopenharmony_ciprotected:
78cb93a386Sopenharmony_ci    const char* onGetName() override {
79cb93a386Sopenharmony_ci        return fName.c_str();
80cb93a386Sopenharmony_ci    }
81cb93a386Sopenharmony_ci
82cb93a386Sopenharmony_ci    void onDraw(int loops, SkCanvas* canvas) override {
83cb93a386Sopenharmony_ci        fRec.fCanvas = canvas;
84cb93a386Sopenharmony_ci        this->setupPaint(&fRec.fPaint);
85cb93a386Sopenharmony_ci        fRec.fPaint.setStyle(SkPaint::kStroke_Style);
86cb93a386Sopenharmony_ci        fRec.fPaint.setStrokeCap(fCap);
87cb93a386Sopenharmony_ci        fRec.fPaint.setStrokeJoin(fJoin);
88cb93a386Sopenharmony_ci        fRec.fPaint.setStrokeWidth(fWidth);
89cb93a386Sopenharmony_ci        fProc(&fRec, loops);
90cb93a386Sopenharmony_ci    }
91cb93a386Sopenharmony_ci
92cb93a386Sopenharmony_ciprivate:
93cb93a386Sopenharmony_ci    using INHERITED = Benchmark;
94cb93a386Sopenharmony_ci};
95cb93a386Sopenharmony_ci
96cb93a386Sopenharmony_ciDEF_BENCH( return new BezierBench(SkPaint::kButt_Cap, SkPaint::kRound_Join, 2, draw_quad); )
97cb93a386Sopenharmony_ciDEF_BENCH( return new BezierBench(SkPaint::kSquare_Cap, SkPaint::kBevel_Join, 10, draw_quad); )
98cb93a386Sopenharmony_ciDEF_BENCH( return new BezierBench(SkPaint::kRound_Cap, SkPaint::kMiter_Join, 50, draw_quad); )
99cb93a386Sopenharmony_ci
100cb93a386Sopenharmony_ciDEF_BENCH( return new BezierBench(SkPaint::kButt_Cap, SkPaint::kRound_Join, 2, draw_cubic); )
101cb93a386Sopenharmony_ciDEF_BENCH( return new BezierBench(SkPaint::kSquare_Cap, SkPaint::kBevel_Join, 10, draw_cubic); )
102cb93a386Sopenharmony_ciDEF_BENCH( return new BezierBench(SkPaint::kRound_Cap, SkPaint::kMiter_Join, 50, draw_cubic); )
103