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 | |
reed@google.com | 0d10280 | 2012-05-31 18:28:59 +0000 | [diff] [blame] | 28 | static bool diffrect_proc(SkRegion& a, SkRegion& b) { |
| 29 | SkRegion result; |
| 30 | return result.op(a, b.getBounds(), SkRegion::kDifference_Op); |
| 31 | } |
| 32 | |
| 33 | static bool diffrectbig_proc(SkRegion& a, SkRegion& b) { |
| 34 | SkRegion result; |
| 35 | return result.op(a, a.getBounds(), SkRegion::kDifference_Op); |
| 36 | } |
| 37 | |
reed@google.com | 01049d5 | 2012-05-02 16:45:36 +0000 | [diff] [blame] | 38 | static bool containsrect_proc(SkRegion& a, SkRegion& b) { |
reed@google.com | 50129db | 2012-04-30 12:07:55 +0000 | [diff] [blame] | 39 | SkIRect r = a.getBounds(); |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 40 | r.inset(r.width()/4, r.height()/4); |
reed@google.com | 50129db | 2012-04-30 12:07:55 +0000 | [diff] [blame] | 41 | (void)a.contains(r); |
| 42 | |
| 43 | r = b.getBounds(); |
| 44 | r.inset(r.width()/4, r.height()/4); |
reed@google.com | 71937d6 | 2012-04-30 13:54:36 +0000 | [diff] [blame] | 45 | return b.contains(r); |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 46 | } |
| 47 | |
reed@google.com | 4b4f86d | 2012-05-02 17:20:02 +0000 | [diff] [blame] | 48 | static bool sectsrgn_proc(SkRegion& a, SkRegion& b) { |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 49 | return a.intersects(b); |
| 50 | } |
| 51 | |
reed@google.com | 4b4f86d | 2012-05-02 17:20:02 +0000 | [diff] [blame] | 52 | static bool sectsrect_proc(SkRegion& a, SkRegion& b) { |
| 53 | SkIRect r = a.getBounds(); |
| 54 | r.inset(r.width()/4, r.height()/4); |
reed@google.com | 26dc5b6 | 2012-05-02 17:41:13 +0000 | [diff] [blame] | 55 | return a.intersects(r); |
reed@google.com | 4b4f86d | 2012-05-02 17:20:02 +0000 | [diff] [blame] | 56 | } |
| 57 | |
reed@google.com | 50129db | 2012-04-30 12:07:55 +0000 | [diff] [blame] | 58 | static bool containsxy_proc(SkRegion& a, SkRegion& b) { |
| 59 | const SkIRect& r = a.getBounds(); |
| 60 | const int dx = r.width() / 8; |
| 61 | const int dy = r.height() / 8; |
| 62 | for (int y = r.fTop; y < r.fBottom; y += dy) { |
| 63 | for (int x = r.fLeft; x < r.fRight; x += dx) { |
| 64 | (void)a.contains(x, y); |
| 65 | } |
| 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 70 | class RegionBench : public SkBenchmark { |
| 71 | public: |
| 72 | typedef bool (*Proc)(SkRegion& a, SkRegion& b); |
| 73 | |
| 74 | SkRegion fA, fB; |
| 75 | Proc fProc; |
| 76 | SkString fName; |
reed@google.com | 46af7ef | 2012-05-02 16:56:49 +0000 | [diff] [blame] | 77 | int fLoopMul; |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 78 | |
| 79 | enum { |
| 80 | W = 1024, |
| 81 | H = 768, |
reed@google.com | 50129db | 2012-04-30 12:07:55 +0000 | [diff] [blame] | 82 | N = SkBENCHLOOP(2000) |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | SkIRect randrect(SkRandom& rand) { |
| 86 | int x = rand.nextU() % W; |
| 87 | int y = rand.nextU() % H; |
| 88 | int w = rand.nextU() % W; |
| 89 | int h = rand.nextU() % H; |
| 90 | return SkIRect::MakeXYWH(x, y, w >> 1, h >> 1); |
| 91 | } |
| 92 | |
reed@google.com | 46af7ef | 2012-05-02 16:56:49 +0000 | [diff] [blame] | 93 | RegionBench(void* param, int count, Proc proc, const char name[], int mul = 1) : INHERITED(param) { |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 94 | fProc = proc; |
reed@google.com | 515d998 | 2012-04-30 14:43:46 +0000 | [diff] [blame] | 95 | fName.printf("region_%s_%d", name, count); |
reed@google.com | 46af7ef | 2012-05-02 16:56:49 +0000 | [diff] [blame] | 96 | fLoopMul = mul; |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 97 | |
| 98 | SkRandom rand; |
reed@google.com | 50129db | 2012-04-30 12:07:55 +0000 | [diff] [blame] | 99 | for (int i = 0; i < count; i++) { |
| 100 | fA.op(randrect(rand), SkRegion::kXOR_Op); |
| 101 | fB.op(randrect(rand), SkRegion::kXOR_Op); |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | protected: |
| 106 | virtual const char* onGetName() { return fName.c_str(); } |
| 107 | |
| 108 | virtual void onDraw(SkCanvas* canvas) { |
| 109 | Proc proc = fProc; |
reed@google.com | 46af7ef | 2012-05-02 16:56:49 +0000 | [diff] [blame] | 110 | int n = fLoopMul * N; |
| 111 | for (int i = 0; i < n; ++i) { |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 112 | proc(fA, fB); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | typedef SkBenchmark INHERITED; |
| 118 | }; |
| 119 | |
reed@google.com | 50129db | 2012-04-30 12:07:55 +0000 | [diff] [blame] | 120 | #define SMALL 16 |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 121 | |
| 122 | static SkBenchmark* gF0(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, union_proc, "union")); } |
| 123 | static SkBenchmark* gF1(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sect_proc, "intersect")); } |
| 124 | static SkBenchmark* gF2(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, diff_proc, "difference")); } |
reed@google.com | 0d10280 | 2012-05-31 18:28:59 +0000 | [diff] [blame] | 125 | static SkBenchmark* gF3(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, diffrect_proc, "differencerect")); } |
| 126 | static SkBenchmark* gF4(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, diffrectbig_proc, "differencerectbig")); } |
| 127 | static SkBenchmark* gF5(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, containsrect_proc, "containsrect", 100)); } |
| 128 | static SkBenchmark* gF6(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sectsrgn_proc, "intersectsrgn", 10)); } |
| 129 | static SkBenchmark* gF7(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sectsrect_proc, "intersectsrect", 200)); } |
| 130 | static SkBenchmark* gF8(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, containsxy_proc, "containsxy")); } |
reed@google.com | 8c2cc1a | 2012-04-27 19:29:52 +0000 | [diff] [blame] | 131 | |
| 132 | static BenchRegistry gR0(gF0); |
| 133 | static BenchRegistry gR1(gF1); |
| 134 | static BenchRegistry gR2(gF2); |
| 135 | static BenchRegistry gR3(gF3); |
| 136 | static BenchRegistry gR4(gF4); |
reed@google.com | 50129db | 2012-04-30 12:07:55 +0000 | [diff] [blame] | 137 | static BenchRegistry gR5(gF5); |
reed@google.com | 4b4f86d | 2012-05-02 17:20:02 +0000 | [diff] [blame] | 138 | static BenchRegistry gR6(gF6); |
reed@google.com | 0d10280 | 2012-05-31 18:28:59 +0000 | [diff] [blame] | 139 | static BenchRegistry gR7(gF7); |
| 140 | static BenchRegistry gR8(gF8); |