blob: 51d389c751f76f2e44ea905851c052a4a8ddd9ae [file] [log] [blame]
reed@google.com8c2cc1a2012-04-27 19:29:52 +00001
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
13static bool union_proc(SkRegion& a, SkRegion& b) {
14 SkRegion result;
15 return result.op(a, b, SkRegion::kUnion_Op);
16}
17
18static bool sect_proc(SkRegion& a, SkRegion& b) {
19 SkRegion result;
20 return result.op(a, b, SkRegion::kIntersect_Op);
21}
22
23static bool diff_proc(SkRegion& a, SkRegion& b) {
24 SkRegion result;
25 return result.op(a, b, SkRegion::kDifference_Op);
26}
27
reed@google.com01049d52012-05-02 16:45:36 +000028static bool containsrect_proc(SkRegion& a, SkRegion& b) {
reed@google.com50129db2012-04-30 12:07:55 +000029 SkIRect r = a.getBounds();
reed@google.com8c2cc1a2012-04-27 19:29:52 +000030 r.inset(r.width()/4, r.height()/4);
reed@google.com50129db2012-04-30 12:07:55 +000031 (void)a.contains(r);
32
33 r = b.getBounds();
34 r.inset(r.width()/4, r.height()/4);
reed@google.com71937d62012-04-30 13:54:36 +000035 return b.contains(r);
reed@google.com8c2cc1a2012-04-27 19:29:52 +000036}
37
reed@google.com4b4f86d2012-05-02 17:20:02 +000038static bool sectsrgn_proc(SkRegion& a, SkRegion& b) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +000039 return a.intersects(b);
40}
41
reed@google.com4b4f86d2012-05-02 17:20:02 +000042static bool sectsrect_proc(SkRegion& a, SkRegion& b) {
43 SkIRect r = a.getBounds();
44 r.inset(r.width()/4, r.height()/4);
reed@google.com26dc5b62012-05-02 17:41:13 +000045 return a.intersects(r);
reed@google.com4b4f86d2012-05-02 17:20:02 +000046}
47
reed@google.com50129db2012-04-30 12:07:55 +000048static 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.com8c2cc1a2012-04-27 19:29:52 +000060class RegionBench : public SkBenchmark {
61public:
62 typedef bool (*Proc)(SkRegion& a, SkRegion& b);
63
64 SkRegion fA, fB;
65 Proc fProc;
66 SkString fName;
reed@google.com46af7ef2012-05-02 16:56:49 +000067 int fLoopMul;
reed@google.com8c2cc1a2012-04-27 19:29:52 +000068
69 enum {
70 W = 1024,
71 H = 768,
reed@google.com50129db2012-04-30 12:07:55 +000072 N = SkBENCHLOOP(2000)
reed@google.com8c2cc1a2012-04-27 19:29:52 +000073 };
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.com46af7ef2012-05-02 16:56:49 +000083 RegionBench(void* param, int count, Proc proc, const char name[], int mul = 1) : INHERITED(param) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +000084 fProc = proc;
reed@google.com515d9982012-04-30 14:43:46 +000085 fName.printf("region_%s_%d", name, count);
reed@google.com46af7ef2012-05-02 16:56:49 +000086 fLoopMul = mul;
reed@google.com8c2cc1a2012-04-27 19:29:52 +000087
88 SkRandom rand;
reed@google.com50129db2012-04-30 12:07:55 +000089 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.com8c2cc1a2012-04-27 19:29:52 +000092 }
93 }
94
95protected:
96 virtual const char* onGetName() { return fName.c_str(); }
97
98 virtual void onDraw(SkCanvas* canvas) {
99 Proc proc = fProc;
reed@google.com46af7ef2012-05-02 16:56:49 +0000100 int n = fLoopMul * N;
101 for (int i = 0; i < n; ++i) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000102 proc(fA, fB);
103 }
104 }
105
106private:
107 typedef SkBenchmark INHERITED;
108};
109
reed@google.com50129db2012-04-30 12:07:55 +0000110#define SMALL 16
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000111
112static SkBenchmark* gF0(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, union_proc, "union")); }
113static SkBenchmark* gF1(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sect_proc, "intersect")); }
114static SkBenchmark* gF2(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, diff_proc, "difference")); }
reed@google.com46af7ef2012-05-02 16:56:49 +0000115static SkBenchmark* gF3(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, containsrect_proc, "containsrect", 100)); }
reed@google.com4b4f86d2012-05-02 17:20:02 +0000116static SkBenchmark* gF4(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sectsrgn_proc, "intersectsrgn", 10)); }
117static SkBenchmark* gF5(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sectsrect_proc, "intersectsrect", 200)); }
118static SkBenchmark* gF6(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, containsxy_proc, "containsxy")); }
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000119
120static BenchRegistry gR0(gF0);
121static BenchRegistry gR1(gF1);
122static BenchRegistry gR2(gF2);
123static BenchRegistry gR3(gF3);
124static BenchRegistry gR4(gF4);
reed@google.com50129db2012-04-30 12:07:55 +0000125static BenchRegistry gR5(gF5);
reed@google.com4b4f86d2012-05-02 17:20:02 +0000126static BenchRegistry gR6(gF6);