blob: 1204b946f88c8b80e9b6a82b82db2a18fa851ce0 [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
28static bool contains_proc(SkRegion& a, SkRegion& b) {
29 SkIRect r = b.getBounds();
30 r.inset(r.width()/4, r.height()/4);
31 return a.contains(r);
32}
33
34static bool sects_proc(SkRegion& a, SkRegion& b) {
35 return a.intersects(b);
36}
37
38class RegionBench : public SkBenchmark {
39public:
40 typedef bool (*Proc)(SkRegion& a, SkRegion& b);
41
42 SkRegion fA, fB;
43 Proc fProc;
44 SkString fName;
45
46 enum {
47 W = 1024,
48 H = 768,
49 N = SkBENCHLOOP(5000)
50 };
51
52 SkIRect randrect(SkRandom& rand) {
53 int x = rand.nextU() % W;
54 int y = rand.nextU() % H;
55 int w = rand.nextU() % W;
56 int h = rand.nextU() % H;
57 return SkIRect::MakeXYWH(x, y, w >> 1, h >> 1);
58 }
59
60 RegionBench(void* param, int count, Proc proc, const char name[]) : INHERITED(param) {
61 fProc = proc;
62 fName.printf("Region_%s_%d", name, count);
63
64 SkRandom rand;
65 for (int i = 0; i < N; i++) {
66 fA.op(randrect(rand), SkRegion::kUnion_Op);
67 fB.op(randrect(rand), SkRegion::kUnion_Op);
68 }
69 }
70
71protected:
72 virtual const char* onGetName() { return fName.c_str(); }
73
74 virtual void onDraw(SkCanvas* canvas) {
75 Proc proc = fProc;
76 for (int i = 0; i < N; ++i) {
77 proc(fA, fB);
78 }
79 }
80
81private:
82 typedef SkBenchmark INHERITED;
83};
84
85#define SMALL 64
86
87static SkBenchmark* gF0(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, union_proc, "union")); }
88static SkBenchmark* gF1(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sect_proc, "intersect")); }
89static SkBenchmark* gF2(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, diff_proc, "difference")); }
90static SkBenchmark* gF3(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, contains_proc, "contains")); }
91static SkBenchmark* gF4(void* p) { return SkNEW_ARGS(RegionBench, (p, SMALL, sects_proc, "intersects")); }
92
93static BenchRegistry gR0(gF0);
94static BenchRegistry gR1(gF1);
95static BenchRegistry gR2(gF2);
96static BenchRegistry gR3(gF3);
97static BenchRegistry gR4(gF4);