blob: 7ac288619c21690f252fd860b9a077ad7b4b8e85 [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.com50129db2012-04-30 12:07:55 +000028static bool containsrgn_proc(SkRegion& a, SkRegion& b) {
29 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;
61
62 enum {
63 W = 1024,
64 H = 768,
reed@google.com50129db2012-04-30 12:07:55 +000065 N = SkBENCHLOOP(2000)
reed@google.com8c2cc1a2012-04-27 19:29:52 +000066 };
67
68 SkIRect randrect(SkRandom& rand) {
69 int x = rand.nextU() % W;
70 int y = rand.nextU() % H;
71 int w = rand.nextU() % W;
72 int h = rand.nextU() % H;
73 return SkIRect::MakeXYWH(x, y, w >> 1, h >> 1);
74 }
75
76 RegionBench(void* param, int count, Proc proc, const char name[]) : INHERITED(param) {
77 fProc = proc;
78 fName.printf("Region_%s_%d", name, count);
79
80 SkRandom rand;
reed@google.com50129db2012-04-30 12:07:55 +000081 for (int i = 0; i < count; i++) {
82 fA.op(randrect(rand), SkRegion::kXOR_Op);
83 fB.op(randrect(rand), SkRegion::kXOR_Op);
reed@google.com8c2cc1a2012-04-27 19:29:52 +000084 }
85 }
86
87protected:
88 virtual const char* onGetName() { return fName.c_str(); }
89
90 virtual void onDraw(SkCanvas* canvas) {
91 Proc proc = fProc;
92 for (int i = 0; i < N; ++i) {
93 proc(fA, fB);
94 }
95 }
96
97private:
98 typedef SkBenchmark INHERITED;
99};
100
reed@google.com50129db2012-04-30 12:07:55 +0000101#define SMALL 16
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000102
103static SkBenchmark* gF0(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, union_proc, "union")); }
104static SkBenchmark* gF1(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sect_proc, "intersect")); }
105static SkBenchmark* gF2(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, diff_proc, "difference")); }
reed@google.com50129db2012-04-30 12:07:55 +0000106static SkBenchmark* gF3(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, containsrgn_proc, "containsrgb")); }
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000107static SkBenchmark* gF4(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sects_proc, "intersects")); }
reed@google.com50129db2012-04-30 12:07:55 +0000108static SkBenchmark* gF5(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, containsxy_proc, "containsxy")); }
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000109
110static BenchRegistry gR0(gF0);
111static BenchRegistry gR1(gF1);
112static BenchRegistry gR2(gF2);
113static BenchRegistry gR3(gF3);
114static BenchRegistry gR4(gF4);
reed@google.com50129db2012-04-30 12:07:55 +0000115static BenchRegistry gR5(gF5);