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