reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame^] | 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 "SkRandom.h" |
| 10 | #include "SkRegion.h" |
| 11 | #include "SkString.h" |
| 12 | |
| 13 | static bool union_proc(SkRegion& a, SkRegion& b) { |
| 14 | SkRegion result; |
| 15 | return result.op(a, b, SkRegion::kUnion_Op); |
| 16 | } |
| 17 | |
| 18 | static bool sect_proc(SkRegion& a, SkRegion& b) { |
| 19 | SkRegion result; |
| 20 | return result.op(a, b, SkRegion::kIntersect_Op); |
| 21 | } |
| 22 | |
| 23 | static bool diff_proc(SkRegion& a, SkRegion& b) { |
| 24 | SkRegion result; |
| 25 | return result.op(a, b, SkRegion::kDifference_Op); |
| 26 | } |
| 27 | |
| 28 | static bool contains_proc(SkRegion& a, SkRegion& b) { |
| 29 | SkIRect r = b.getBounds(); |
| 30 | r.inset(r.width()/4, r.height()/4); |
| 31 | return a.contains(r); |
| 32 | } |
| 33 | |
| 34 | static bool sects_proc(SkRegion& a, SkRegion& b) { |
| 35 | return a.intersects(b); |
| 36 | } |
| 37 | |
| 38 | class RegionBench : public SkBenchmark { |
| 39 | public: |
| 40 | typedef bool (*Proc)(SkRegion& a, SkRegion& b); |
| 41 | |
| 42 | SkRegion fA, fB; |
| 43 | Proc fProc; |
| 44 | SkString fName; |
| 45 | |
| 46 | enum { |
| 47 | W = 1024, |
| 48 | H = 768, |
| 49 | N = SkBENCHLOOP(5000) |
| 50 | }; |
| 51 | |
| 52 | SkIRect randrect(SkRandom& rand) { |
| 53 | int x = rand.nextU() % W; |
| 54 | int y = rand.nextU() % H; |
| 55 | int w = rand.nextU() % W; |
| 56 | int h = rand.nextU() % H; |
| 57 | return SkIRect::MakeXYWH(x, y, w >> 1, h >> 1); |
| 58 | } |
| 59 | |
| 60 | RegionBench(void* param, int count, Proc proc, const char name[]) : INHERITED(param) { |
| 61 | fProc = proc; |
| 62 | fName.printf("Region_%s_%d", name, count); |
| 63 | |
| 64 | SkRandom rand; |
| 65 | for (int i = 0; i < N; i++) { |
| 66 | fA.op(randrect(rand), SkRegion::kUnion_Op); |
| 67 | fB.op(randrect(rand), SkRegion::kUnion_Op); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | protected: |
| 72 | virtual const char* onGetName() { return fName.c_str(); } |
| 73 | |
| 74 | virtual void onDraw(SkCanvas* canvas) { |
| 75 | Proc proc = fProc; |
| 76 | for (int i = 0; i < N; ++i) { |
| 77 | proc(fA, fB); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | typedef SkBenchmark INHERITED; |
| 83 | }; |
| 84 | |
| 85 | #define SMALL 64 |
| 86 | |
| 87 | static SkBenchmark* gF0(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, union_proc, "union")); } |
| 88 | static SkBenchmark* gF1(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sect_proc, "intersect")); } |
| 89 | static SkBenchmark* gF2(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, diff_proc, "difference")); } |
| 90 | static SkBenchmark* gF3(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, contains_proc, "contains")); } |
| 91 | static SkBenchmark* gF4(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sects_proc, "intersects")); } |
| 92 | |
| 93 | static BenchRegistry gR0(gF0); |
| 94 | static BenchRegistry gR1(gF1); |
| 95 | static BenchRegistry gR2(gF2); |
| 96 | static BenchRegistry gR3(gF3); |
| 97 | static BenchRegistry gR4(gF4); |