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 "Benchmark.h" 9 #include "SkBitmap.h" 10 #include "SkCanvas.h" 11 #include "SkColorPriv.h" 12 #include "SkPaint.h" 13 #include "SkPath.h" 14 #include "SkRandom.h" 15 #include "SkShader.h" 16 #include "SkString.h" 17 18 static int rand_pts(SkRandom& rand, SkPoint pts[4]) { 19 int n = rand.nextU() & 3; 20 n += 1; 21 22 for (int i = 0; i < n; ++i) { 23 pts[i].fX = rand.nextSScalar1(); 24 pts[i].fY = rand.nextSScalar1(); 25 } 26 return n; 27 } 28 29 class PathIterBench : public Benchmark { 30 SkString fName; 31 SkPath fPath; 32 bool fRaw; 33 34 public: 35 PathIterBench(bool raw) { 36 fName.printf("pathiter_%s", raw ? "raw" : "consume"); 37 fRaw = raw; 38 39 SkRandom rand; 40 for (int i = 0; i < 1000; ++i) { 41 SkPoint pts[4]; 42 int n = rand_pts(rand, pts); 43 switch (n) { 44 case 1: 45 fPath.moveTo(pts[0]); 46 break; 47 case 2: 48 fPath.lineTo(pts[1]); 49 break; 50 case 3: 51 fPath.quadTo(pts[1], pts[2]); 52 break; 53 case 4: 54 fPath.cubicTo(pts[1], pts[2], pts[3]); 55 break; 56 } 57 } 58 } 59 60 bool isSuitableFor(Backend backend) override { 61 return backend == kNonRendering_Backend; 62 } 63 64 protected: 65 const char* onGetName() override { 66 return fName.c_str(); 67 } 68 69 void onDraw(int loops, SkCanvas*) override { 70 if (fRaw) { 71 for (int i = 0; i < loops; ++i) { 72 SkPath::RawIter iter(fPath); 73 SkPath::Verb verb; 74 SkPoint pts[4]; 75 76 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { } 77 } 78 } else { 79 for (int i = 0; i < loops; ++i) { 80 SkPath::Iter iter(fPath, false); 81 SkPath::Verb verb; 82 SkPoint pts[4]; 83 84 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) { } 85 } 86 } 87 } 88 89 private: 90 typedef Benchmark INHERITED; 91 }; 92 93 /////////////////////////////////////////////////////////////////////////////// 94 95 DEF_BENCH( return new PathIterBench(false); ) 96 DEF_BENCH( return new PathIterBench(true); ) 97