blob: a520e4245df2095fe032f73fb79a6526e117999e [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
38static bool sects_proc(SkRegion& a, SkRegion& b) {
39 return a.intersects(b);
40}
41
reed@google.com50129db2012-04-30 12:07:55 +000042static bool containsxy_proc(SkRegion& a, SkRegion& b) {
43 const SkIRect& r = a.getBounds();
44 const int dx = r.width() / 8;
45 const int dy = r.height() / 8;
46 for (int y = r.fTop; y < r.fBottom; y += dy) {
47 for (int x = r.fLeft; x < r.fRight; x += dx) {
48 (void)a.contains(x, y);
49 }
50 }
51 return true;
52}
53
reed@google.com8c2cc1a2012-04-27 19:29:52 +000054class RegionBench : public SkBenchmark {
55public:
56 typedef bool (*Proc)(SkRegion& a, SkRegion& b);
57
58 SkRegion fA, fB;
59 Proc fProc;
60 SkString fName;
reed@google.com46af7ef2012-05-02 16:56:49 +000061 int fLoopMul;
reed@google.com8c2cc1a2012-04-27 19:29:52 +000062
63 enum {
64 W = 1024,
65 H = 768,
reed@google.com50129db2012-04-30 12:07:55 +000066 N = SkBENCHLOOP(2000)
reed@google.com8c2cc1a2012-04-27 19:29:52 +000067 };
68
69 SkIRect randrect(SkRandom& rand) {
70 int x = rand.nextU() % W;
71 int y = rand.nextU() % H;
72 int w = rand.nextU() % W;
73 int h = rand.nextU() % H;
74 return SkIRect::MakeXYWH(x, y, w >> 1, h >> 1);
75 }
76
reed@google.com46af7ef2012-05-02 16:56:49 +000077 RegionBench(void* param, int count, Proc proc, const char name[], int mul = 1) : INHERITED(param) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +000078 fProc = proc;
reed@google.com515d9982012-04-30 14:43:46 +000079 fName.printf("region_%s_%d", name, count);
reed@google.com46af7ef2012-05-02 16:56:49 +000080 fLoopMul = mul;
reed@google.com8c2cc1a2012-04-27 19:29:52 +000081
82 SkRandom rand;
reed@google.com50129db2012-04-30 12:07:55 +000083 for (int i = 0; i < count; i++) {
84 fA.op(randrect(rand), SkRegion::kXOR_Op);
85 fB.op(randrect(rand), SkRegion::kXOR_Op);
reed@google.com8c2cc1a2012-04-27 19:29:52 +000086 }
87 }
88
89protected:
90 virtual const char* onGetName() { return fName.c_str(); }
91
92 virtual void onDraw(SkCanvas* canvas) {
93 Proc proc = fProc;
reed@google.com46af7ef2012-05-02 16:56:49 +000094 int n = fLoopMul * N;
95 for (int i = 0; i < n; ++i) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +000096 proc(fA, fB);
97 }
98 }
99
100private:
101 typedef SkBenchmark INHERITED;
102};
103
reed@google.com50129db2012-04-30 12:07:55 +0000104#define SMALL 16
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000105
106static SkBenchmark* gF0(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, union_proc, "union")); }
107static SkBenchmark* gF1(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sect_proc, "intersect")); }
108static SkBenchmark* gF2(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, diff_proc, "difference")); }
reed@google.com46af7ef2012-05-02 16:56:49 +0000109static SkBenchmark* gF3(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, containsrect_proc, "containsrect", 100)); }
110static SkBenchmark* gF4(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sects_proc, "intersects", 10)); }
reed@google.com50129db2012-04-30 12:07:55 +0000111static SkBenchmark* gF5(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, containsxy_proc, "containsxy")); }
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000112
113static BenchRegistry gR0(gF0);
114static BenchRegistry gR1(gF1);
115static BenchRegistry gR2(gF2);
116static BenchRegistry gR3(gF3);
117static BenchRegistry gR4(gF4);
reed@google.com50129db2012-04-30 12:07:55 +0000118static BenchRegistry gR5(gF5);