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