blob: f73a88889fb29258d038ce916784365545a77da9 [file] [log] [blame]
reed@google.com8c2cc1a2012-04-27 19:29:52 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
tfarinaf168b862014-06-19 12:32:29 -07007#include "Benchmark.h"
reed@google.com8c2cc1a2012-04-27 19:29:52 +00008#include "SkRandom.h"
9#include "SkRegion.h"
10#include "SkString.h"
11
12static bool union_proc(SkRegion& a, SkRegion& b) {
13 SkRegion result;
14 return result.op(a, b, SkRegion::kUnion_Op);
15}
16
17static bool sect_proc(SkRegion& a, SkRegion& b) {
18 SkRegion result;
19 return result.op(a, b, SkRegion::kIntersect_Op);
20}
21
22static bool diff_proc(SkRegion& a, SkRegion& b) {
23 SkRegion result;
24 return result.op(a, b, SkRegion::kDifference_Op);
25}
26
reed@google.com0d102802012-05-31 18:28:59 +000027static bool diffrect_proc(SkRegion& a, SkRegion& b) {
28 SkRegion result;
29 return result.op(a, b.getBounds(), SkRegion::kDifference_Op);
30}
31
32static bool diffrectbig_proc(SkRegion& a, SkRegion& b) {
33 SkRegion result;
34 return result.op(a, a.getBounds(), SkRegion::kDifference_Op);
35}
36
reed@google.com01049d52012-05-02 16:45:36 +000037static bool containsrect_proc(SkRegion& a, SkRegion& b) {
reed@google.com50129db2012-04-30 12:07:55 +000038 SkIRect r = a.getBounds();
reed@google.com8c2cc1a2012-04-27 19:29:52 +000039 r.inset(r.width()/4, r.height()/4);
reed@google.com50129db2012-04-30 12:07:55 +000040 (void)a.contains(r);
41
42 r = b.getBounds();
43 r.inset(r.width()/4, r.height()/4);
reed@google.com71937d62012-04-30 13:54:36 +000044 return b.contains(r);
reed@google.com8c2cc1a2012-04-27 19:29:52 +000045}
46
reed@google.com4b4f86d2012-05-02 17:20:02 +000047static bool sectsrgn_proc(SkRegion& a, SkRegion& b) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +000048 return a.intersects(b);
49}
50
reed@google.com4b4f86d2012-05-02 17:20:02 +000051static bool sectsrect_proc(SkRegion& a, SkRegion& b) {
52 SkIRect r = a.getBounds();
53 r.inset(r.width()/4, r.height()/4);
reed@google.com26dc5b62012-05-02 17:41:13 +000054 return a.intersects(r);
reed@google.com4b4f86d2012-05-02 17:20:02 +000055}
56
reed@google.com50129db2012-04-30 12:07:55 +000057static bool containsxy_proc(SkRegion& a, SkRegion& b) {
58 const SkIRect& r = a.getBounds();
59 const int dx = r.width() / 8;
60 const int dy = r.height() / 8;
61 for (int y = r.fTop; y < r.fBottom; y += dy) {
62 for (int x = r.fLeft; x < r.fRight; x += dx) {
63 (void)a.contains(x, y);
64 }
65 }
66 return true;
67}
68
tfarinaf168b862014-06-19 12:32:29 -070069class RegionBench : public Benchmark {
reed@google.com8c2cc1a2012-04-27 19:29:52 +000070public:
71 typedef bool (*Proc)(SkRegion& a, SkRegion& b);
72
73 SkRegion fA, fB;
74 Proc fProc;
75 SkString fName;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000076
reed@google.com8c2cc1a2012-04-27 19:29:52 +000077 enum {
78 W = 1024,
79 H = 768,
reed@google.com8c2cc1a2012-04-27 19:29:52 +000080 };
81
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000082 SkIRect randrect(SkRandom& rand) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +000083 int x = rand.nextU() % W;
84 int y = rand.nextU() % H;
85 int w = rand.nextU() % W;
86 int h = rand.nextU() % H;
87 return SkIRect::MakeXYWH(x, y, w >> 1, h >> 1);
88 }
89
mtklein@google.com410e6e82013-09-13 19:52:27 +000090 RegionBench(int count, Proc proc, const char name[]) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +000091 fProc = proc;
reed@google.com515d9982012-04-30 14:43:46 +000092 fName.printf("region_%s_%d", name, count);
reed@google.com8c2cc1a2012-04-27 19:29:52 +000093
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000094 SkRandom rand;
reed@google.com50129db2012-04-30 12:07:55 +000095 for (int i = 0; i < count; i++) {
96 fA.op(randrect(rand), SkRegion::kXOR_Op);
97 fB.op(randrect(rand), SkRegion::kXOR_Op);
reed@google.com8c2cc1a2012-04-27 19:29:52 +000098 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000099 }
100
mtklein36352bf2015-03-25 18:17:31 -0700101 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000102 return backend == kNonRendering_Backend;
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000103 }
104
105protected:
mtkleinf0599002015-07-13 06:18:39 -0700106 const char* onGetName() override { return fName.c_str(); }
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000107
mtkleina1ebeb22015-10-01 09:43:39 -0700108 void onDraw(int loops, SkCanvas* canvas) override {
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000109 Proc proc = fProc;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000110 for (int i = 0; i < loops; ++i) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000111 proc(fA, fB);
112 }
113 }
114
115private:
tfarinaf168b862014-06-19 12:32:29 -0700116 typedef Benchmark INHERITED;
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000117};
118
mike@reedtribe.orgae8f9522014-01-01 20:32:45 +0000119///////////////////////////////////////////////////////////////////////////////
120
reed@google.com50129db2012-04-30 12:07:55 +0000121#define SMALL 16
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000122
halcanary385fe4d2015-08-26 13:07:48 -0700123DEF_BENCH(return new RegionBench(SMALL, union_proc, "union");)
124DEF_BENCH(return new RegionBench(SMALL, sect_proc, "intersect");)
125DEF_BENCH(return new RegionBench(SMALL, diff_proc, "difference");)
126DEF_BENCH(return new RegionBench(SMALL, diffrect_proc, "differencerect");)
127DEF_BENCH(return new RegionBench(SMALL, diffrectbig_proc, "differencerectbig");)
128DEF_BENCH(return new RegionBench(SMALL, containsrect_proc, "containsrect");)
129DEF_BENCH(return new RegionBench(SMALL, sectsrgn_proc, "intersectsrgn");)
130DEF_BENCH(return new RegionBench(SMALL, sectsrect_proc, "intersectsrect");)
131DEF_BENCH(return new RegionBench(SMALL, containsxy_proc, "containsxy");)