Home | History | Annotate | Download | only in bench
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #include "SkBenchmark.h"
      9 #include "SkBitmap.h"
     10 #include "SkCanvas.h"
     11 #include "SkColorPriv.h"
     12 #include "SkPaint.h"
     13 #include "SkRandom.h"
     14 #include "SkShader.h"
     15 #include "SkString.h"
     16 
     17 enum Flags {
     18     kStroke_Flag = 1 << 0,
     19     kBig_Flag    = 1 << 1
     20 };
     21 
     22 #define FLAGS00  Flags(0)
     23 #define FLAGS01  Flags(kStroke_Flag)
     24 #define FLAGS10  Flags(kBig_Flag)
     25 #define FLAGS11  Flags(kStroke_Flag | kBig_Flag)
     26 
     27 class PathBench : public SkBenchmark {
     28     SkPaint     fPaint;
     29     SkString    fName;
     30     Flags       fFlags;
     31     enum { N = SkBENCHLOOP(1000) };
     32 public:
     33     PathBench(void* param, Flags flags) : INHERITED(param), fFlags(flags) {
     34         fPaint.setStyle(flags & kStroke_Flag ? SkPaint::kStroke_Style :
     35                         SkPaint::kFill_Style);
     36         fPaint.setStrokeWidth(SkIntToScalar(5));
     37         fPaint.setStrokeJoin(SkPaint::kBevel_Join);
     38     }
     39 
     40     virtual void appendName(SkString*) = 0;
     41     virtual void makePath(SkPath*) = 0;
     42     virtual int complexity() { return 0; }
     43 
     44 protected:
     45     virtual const char* onGetName() {
     46         fName.printf("path_%s_%s_",
     47                      fFlags & kStroke_Flag ? "stroke" : "fill",
     48                      fFlags & kBig_Flag ? "big" : "small");
     49         this->appendName(&fName);
     50         return fName.c_str();
     51     }
     52 
     53     virtual void onDraw(SkCanvas* canvas) {
     54         SkPaint paint(fPaint);
     55         this->setupPaint(&paint);
     56 
     57         SkPath path;
     58         this->makePath(&path);
     59         if (fFlags & kBig_Flag) {
     60             SkMatrix m;
     61             m.setScale(SkIntToScalar(10), SkIntToScalar(10));
     62             path.transform(m);
     63         }
     64 
     65         int count = N;
     66         if (fFlags & kBig_Flag) {
     67             count >>= 2;
     68         }
     69         count >>= (3 * complexity());
     70 
     71         for (int i = 0; i < count; i++) {
     72             canvas->drawPath(path, paint);
     73         }
     74     }
     75 
     76 private:
     77     typedef SkBenchmark INHERITED;
     78 };
     79 
     80 class TrianglePathBench : public PathBench {
     81 public:
     82     TrianglePathBench(void* param, Flags flags) : INHERITED(param, flags) {}
     83 
     84     virtual void appendName(SkString* name) {
     85         name->append("triangle");
     86     }
     87     virtual void makePath(SkPath* path) {
     88         static const int gCoord[] = {
     89             10, 10, 15, 5, 20, 20
     90         };
     91         path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
     92         path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
     93         path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
     94         path->close();
     95     }
     96 private:
     97     typedef PathBench INHERITED;
     98 };
     99 
    100 class RectPathBench : public PathBench {
    101 public:
    102     RectPathBench(void* param, Flags flags) : INHERITED(param, flags) {}
    103 
    104     virtual void appendName(SkString* name) {
    105         name->append("rect");
    106     }
    107     virtual void makePath(SkPath* path) {
    108         SkRect r = { 10, 10, 20, 20 };
    109         path->addRect(r);
    110     }
    111 private:
    112     typedef PathBench INHERITED;
    113 };
    114 
    115 class OvalPathBench : public PathBench {
    116 public:
    117     OvalPathBench(void* param, Flags flags) : INHERITED(param, flags) {}
    118 
    119     virtual void appendName(SkString* name) {
    120         name->append("oval");
    121     }
    122     virtual void makePath(SkPath* path) {
    123         SkRect r = { 10, 10, 20, 20 };
    124         path->addOval(r);
    125     }
    126 private:
    127     typedef PathBench INHERITED;
    128 };
    129 
    130 class SawToothPathBench : public PathBench {
    131 public:
    132     SawToothPathBench(void* param, Flags flags) : INHERITED(param, flags) {}
    133 
    134     virtual void appendName(SkString* name) {
    135         name->append("sawtooth");
    136     }
    137     virtual void makePath(SkPath* path) {
    138         SkScalar x = SkIntToScalar(20);
    139         SkScalar y = SkIntToScalar(20);
    140         const SkScalar x0 = x;
    141         const SkScalar dx = SK_Scalar1 * 5;
    142         const SkScalar dy = SK_Scalar1 * 10;
    143 
    144         path->moveTo(x, y);
    145         for (int i = 0; i < 32; i++) {
    146             x += dx;
    147             path->lineTo(x, y - dy);
    148             x += dx;
    149             path->lineTo(x, y + dy);
    150         }
    151         path->lineTo(x, y + 2 * dy);
    152         path->lineTo(x0, y + 2 * dy);
    153         path->close();
    154     }
    155     virtual int complexity() { return 1; }
    156 private:
    157     typedef PathBench INHERITED;
    158 };
    159 
    160 class LongCurvedPathBench : public PathBench {
    161 public:
    162     LongCurvedPathBench(void * param, Flags flags)
    163         : INHERITED(param, flags) {
    164     }
    165 
    166     virtual void appendName(SkString* name) {
    167         name->append("long_curved");
    168     }
    169     virtual void makePath(SkPath* path) {
    170         SkRandom rand (12);
    171         int i;
    172         for (i = 0; i < 100; i++) {
    173             path->quadTo(SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
    174                          SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)),
    175                          SkScalarMul(rand.nextUScalar1(), SkIntToScalar(640)),
    176                          SkScalarMul(rand.nextUScalar1(), SkIntToScalar(480)));
    177         }
    178         path->close();
    179     }
    180     virtual int complexity() { return 2; }
    181 private:
    182     typedef PathBench INHERITED;
    183 };
    184 
    185 class LongLinePathBench : public PathBench {
    186 public:
    187     LongLinePathBench(void * param, Flags flags)
    188         : INHERITED(param, flags) {
    189     }
    190 
    191     virtual void appendName(SkString* name) {
    192         name->append("long_line");
    193     }
    194     virtual void makePath(SkPath* path) {
    195         SkRandom rand;
    196         path->moveTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
    197         for (size_t i = 1; i < 100; i++) {
    198             path->lineTo(rand.nextUScalar1() * 640, rand.nextUScalar1() * 480);
    199         }
    200     }
    201     virtual int complexity() { return 2; }
    202 private:
    203     typedef PathBench INHERITED;
    204 };
    205 
    206 
    207 static SkBenchmark* FactT00(void* p) { return new TrianglePathBench(p, FLAGS00); }
    208 static SkBenchmark* FactT01(void* p) { return new TrianglePathBench(p, FLAGS01); }
    209 static SkBenchmark* FactT10(void* p) { return new TrianglePathBench(p, FLAGS10); }
    210 static SkBenchmark* FactT11(void* p) { return new TrianglePathBench(p, FLAGS11); }
    211 
    212 static SkBenchmark* FactR00(void* p) { return new RectPathBench(p, FLAGS00); }
    213 static SkBenchmark* FactR01(void* p) { return new RectPathBench(p, FLAGS01); }
    214 static SkBenchmark* FactR10(void* p) { return new RectPathBench(p, FLAGS10); }
    215 static SkBenchmark* FactR11(void* p) { return new RectPathBench(p, FLAGS11); }
    216 
    217 static SkBenchmark* FactO00(void* p) { return new OvalPathBench(p, FLAGS00); }
    218 static SkBenchmark* FactO01(void* p) { return new OvalPathBench(p, FLAGS01); }
    219 static SkBenchmark* FactO10(void* p) { return new OvalPathBench(p, FLAGS10); }
    220 static SkBenchmark* FactO11(void* p) { return new OvalPathBench(p, FLAGS11); }
    221 
    222 static SkBenchmark* FactS00(void* p) { return new SawToothPathBench(p, FLAGS00); }
    223 static SkBenchmark* FactS01(void* p) { return new SawToothPathBench(p, FLAGS01); }
    224 
    225 static SkBenchmark* FactLC00(void* p) {
    226     return new LongCurvedPathBench(p, FLAGS00);
    227 }
    228 static SkBenchmark* FactLC01(void* p) {
    229     return new LongCurvedPathBench(p, FLAGS01);
    230 }
    231 
    232 static SkBenchmark* FactLL00(void* p) {
    233     return new LongLinePathBench(p, FLAGS00);
    234 }
    235 
    236 static SkBenchmark* FactLL01(void* p) {
    237     return new LongLinePathBench(p, FLAGS01);
    238 }
    239 
    240 static BenchRegistry gRegT00(FactT00);
    241 static BenchRegistry gRegT01(FactT01);
    242 static BenchRegistry gRegT10(FactT10);
    243 static BenchRegistry gRegT11(FactT11);
    244 
    245 static BenchRegistry gRegR00(FactR00);
    246 static BenchRegistry gRegR01(FactR01);
    247 static BenchRegistry gRegR10(FactR10);
    248 static BenchRegistry gRegR11(FactR11);
    249 
    250 static BenchRegistry gRegO00(FactO00);
    251 static BenchRegistry gRegO01(FactO01);
    252 static BenchRegistry gRegO10(FactO10);
    253 static BenchRegistry gRegO11(FactO11);
    254 
    255 static BenchRegistry gRegS00(FactS00);
    256 static BenchRegistry gRegS01(FactS01);
    257 
    258 static BenchRegistry gRegLC00(FactLC00);
    259 static BenchRegistry gRegLC01(FactLC01);
    260 
    261 static BenchRegistry gRegLL00(FactLL00);
    262 static BenchRegistry gRegLL01(FactLL01);
    263 
    264