| reed@google.com | 57c4957 | 2011-10-31 14:33:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 "SkBenchmark.h" |
| 9 | #include "SkAAClip.h" |
| 10 | #include "SkPath.h" |
| 11 | #include "SkRegion.h" |
| 12 | #include "SkString.h" |
| robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 13 | #include "SkCanvas.h" |
| reed@google.com | 57c4957 | 2011-10-31 14:33:35 +0000 | [diff] [blame] | 14 | |
| robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 15 | |
| 16 | //////////////////////////////////////////////////////////////////////////////// |
| 17 | // This bench tests out AA/BW clipping via canvas' clipPath and clipRect calls |
| 18 | class AAClipBench : public SkBenchmark { |
| 19 | SkString fName; |
| 20 | SkPath fClipPath; |
| 21 | SkRect fClipRect; |
| 22 | SkRect fDrawRect; |
| 23 | bool fDoPath; |
| 24 | bool fDoAA; |
| 25 | |
| 26 | enum { |
| 27 | N = SkBENCHLOOP(200), |
| 28 | }; |
| 29 | |
| 30 | public: |
| 31 | AAClipBench(void* param, bool doPath, bool doAA) |
| 32 | : INHERITED(param) { |
| 33 | fDoPath = doPath; |
| 34 | fDoAA = doAA; |
| 35 | |
| 36 | fName.printf("aaclip_%s_%s", |
| 37 | doPath ? "path" : "rect", |
| 38 | doAA ? "AA" : "BW"); |
| 39 | |
| 40 | fClipRect.set(SkFloatToScalar(10.5), SkFloatToScalar(10.5), |
| 41 | SkFloatToScalar(50.5), SkFloatToScalar(50.5)); |
| 42 | fClipPath.addRoundRect(fClipRect, SkIntToScalar(10), SkIntToScalar(10)); |
| 43 | fDrawRect.set(SkIntToScalar(0), SkIntToScalar(0), |
| 44 | SkIntToScalar(100), SkIntToScalar(100)); |
| 45 | |
| 46 | SkASSERT(fClipPath.isConvex()); |
| 47 | } |
| 48 | |
| 49 | protected: |
| 50 | virtual const char* onGetName() { return fName.c_str(); } |
| 51 | virtual void onDraw(SkCanvas* canvas) { |
| 52 | |
| 53 | SkPaint paint; |
| 54 | this->setupPaint(&paint); |
| 55 | |
| 56 | for (int i = 0; i < N; ++i) { |
| 57 | // jostle the clip regions each time to prevent caching |
| 58 | fClipRect.offset((i % 2) == 0 ? SkIntToScalar(10) : SkIntToScalar(-10), 0); |
| 59 | fClipPath.reset(); |
| 60 | fClipPath.addRoundRect(fClipRect, |
| 61 | SkIntToScalar(5), SkIntToScalar(5)); |
| 62 | SkASSERT(fClipPath.isConvex()); |
| 63 | |
| 64 | canvas->save(); |
| 65 | #if 1 |
| 66 | if (fDoPath) { |
| 67 | canvas->clipPath(fClipPath, SkRegion::kReplace_Op, fDoAA); |
| 68 | } else { |
| 69 | canvas->clipRect(fClipRect, SkRegion::kReplace_Op, fDoAA); |
| 70 | } |
| 71 | |
| 72 | canvas->drawRect(fDrawRect, paint); |
| 73 | #else |
| 74 | // this path tests out directly draw the clip primitive |
| 75 | // use it to comparing just drawing the clip vs. drawing using |
| 76 | // the clip |
| 77 | if (fDoPath) { |
| 78 | canvas->drawPath(fClipPath, paint); |
| 79 | } else { |
| 80 | canvas->drawRect(fClipRect, paint); |
| 81 | } |
| 82 | #endif |
| 83 | canvas->restore(); |
| 84 | } |
| 85 | } |
| 86 | private: |
| 87 | typedef SkBenchmark INHERITED; |
| 88 | }; |
| 89 | |
| 90 | //////////////////////////////////////////////////////////////////////////////// |
| reed@google.com | 57c4957 | 2011-10-31 14:33:35 +0000 | [diff] [blame] | 91 | class AAClipBuilderBench : public SkBenchmark { |
| 92 | SkString fName; |
| 93 | SkPath fPath; |
| 94 | SkRect fRect; |
| 95 | SkRegion fRegion; |
| 96 | bool fDoPath; |
| 97 | bool fDoAA; |
| 98 | |
| 99 | enum { |
| 100 | N = SkBENCHLOOP(200), |
| 101 | }; |
| 102 | |
| 103 | public: |
| 104 | AAClipBuilderBench(void* param, bool doPath, bool doAA) : INHERITED(param) { |
| 105 | fDoPath = doPath; |
| 106 | fDoAA = doAA; |
| 107 | |
| 108 | fName.printf("aaclip_build_%s_%s", doPath ? "path" : "rect", |
| 109 | doAA ? "AA" : "BW"); |
| 110 | |
| 111 | fRegion.setRect(0, 0, 640, 480); |
| 112 | fRect.set(fRegion.getBounds()); |
| 113 | fRect.inset(SK_Scalar1/4, SK_Scalar1/4); |
| 114 | fPath.addRoundRect(fRect, SkIntToScalar(20), SkIntToScalar(20)); |
| 115 | } |
| 116 | |
| 117 | protected: |
| 118 | virtual const char* onGetName() { return fName.c_str(); } |
| 119 | virtual void onDraw(SkCanvas* canvas) { |
| 120 | SkPaint paint; |
| 121 | this->setupPaint(&paint); |
| 122 | |
| 123 | for (int i = 0; i < N; ++i) { |
| 124 | SkAAClip clip; |
| 125 | if (fDoPath) { |
| 126 | clip.setPath(fPath, &fRegion, fDoAA); |
| 127 | } else { |
| 128 | clip.setRect(fRect, fDoAA); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | private: |
| 133 | typedef SkBenchmark INHERITED; |
| 134 | }; |
| 135 | |
| robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 136 | //////////////////////////////////////////////////////////////////////////////// |
| reed@google.com | a069c8f | 2011-11-28 19:54:56 +0000 | [diff] [blame] | 137 | class AAClipRegionBench : public SkBenchmark { |
| 138 | public: |
| 139 | AAClipRegionBench(void* param) : INHERITED(param) { |
| 140 | SkPath path; |
| 141 | // test conversion of a complex clip to a aaclip |
| 142 | path.addCircle(0, 0, SkIntToScalar(200)); |
| 143 | path.addCircle(0, 0, SkIntToScalar(180)); |
| 144 | // evenodd means we've constructed basically a stroked circle |
| 145 | path.setFillType(SkPath::kEvenOdd_FillType); |
| 146 | |
| 147 | SkIRect bounds; |
| 148 | path.getBounds().roundOut(&bounds); |
| 149 | fRegion.setPath(path, SkRegion(bounds)); |
| 150 | } |
| 151 | |
| 152 | protected: |
| 153 | virtual const char* onGetName() { return "aaclip_setregion"; } |
| 154 | virtual void onDraw(SkCanvas* canvas) { |
| 155 | for (int i = 0; i < N; ++i) { |
| 156 | SkAAClip clip; |
| 157 | clip.setRegion(fRegion); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private: |
| 162 | enum { |
| 163 | N = SkBENCHLOOP(400), |
| 164 | }; |
| 165 | SkRegion fRegion; |
| 166 | typedef SkBenchmark INHERITED; |
| 167 | }; |
| 168 | |
| robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 169 | //////////////////////////////////////////////////////////////////////////////// |
| reed@google.com | 57c4957 | 2011-10-31 14:33:35 +0000 | [diff] [blame] | 170 | |
| 171 | static SkBenchmark* Fact0(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, false, false)); } |
| 172 | static SkBenchmark* Fact1(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, false, true)); } |
| 173 | static SkBenchmark* Fact2(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, true, false)); } |
| 174 | static SkBenchmark* Fact3(void* p) { return SkNEW_ARGS(AAClipBuilderBench, (p, true, true)); } |
| 175 | |
| 176 | static BenchRegistry gReg0(Fact0); |
| 177 | static BenchRegistry gReg1(Fact1); |
| 178 | static BenchRegistry gReg2(Fact2); |
| 179 | static BenchRegistry gReg3(Fact3); |
| reed@google.com | a069c8f | 2011-11-28 19:54:56 +0000 | [diff] [blame] | 180 | |
| 181 | static SkBenchmark* Fact01(void* p) { return SkNEW_ARGS(AAClipRegionBench, (p)); } |
| 182 | static BenchRegistry gReg01(Fact01); |
| robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 183 | |
| 184 | static SkBenchmark* Fact000(void* p) { return SkNEW_ARGS(AAClipBench, (p, false, false)); } |
| 185 | static SkBenchmark* Fact001(void* p) { return SkNEW_ARGS(AAClipBench, (p, false, true)); } |
| 186 | static SkBenchmark* Fact002(void* p) { return SkNEW_ARGS(AAClipBench, (p, true, false)); } |
| 187 | static SkBenchmark* Fact003(void* p) { return SkNEW_ARGS(AAClipBench, (p, true, true)); } |
| 188 | |
| 189 | static BenchRegistry gReg000(Fact000); |
| 190 | static BenchRegistry gReg001(Fact001); |
| 191 | static BenchRegistry gReg002(Fact002); |
| 192 | static BenchRegistry gReg003(Fact003); |
| 193 | |