blob: 84e8d1b057c9a50375ceafeb147b22a45abf8137 [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 */
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.com8c2cc1a2012-04-27 19:29:52 +00009#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.com0d102802012-05-31 18:28:59 +000028static bool diffrect_proc(SkRegion& a, SkRegion& b) {
29 SkRegion result;
30 return result.op(a, b.getBounds(), SkRegion::kDifference_Op);
31}
32
33static bool diffrectbig_proc(SkRegion& a, SkRegion& b) {
34 SkRegion result;
35 return result.op(a, a.getBounds(), SkRegion::kDifference_Op);
36}
37
reed@google.com01049d52012-05-02 16:45:36 +000038static bool containsrect_proc(SkRegion& a, SkRegion& b) {
reed@google.com50129db2012-04-30 12:07:55 +000039 SkIRect r = a.getBounds();
reed@google.com8c2cc1a2012-04-27 19:29:52 +000040 r.inset(r.width()/4, r.height()/4);
reed@google.com50129db2012-04-30 12:07:55 +000041 (void)a.contains(r);
42
43 r = b.getBounds();
44 r.inset(r.width()/4, r.height()/4);
reed@google.com71937d62012-04-30 13:54:36 +000045 return b.contains(r);
reed@google.com8c2cc1a2012-04-27 19:29:52 +000046}
47
reed@google.com4b4f86d2012-05-02 17:20:02 +000048static bool sectsrgn_proc(SkRegion& a, SkRegion& b) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +000049 return a.intersects(b);
50}
51
reed@google.com4b4f86d2012-05-02 17:20:02 +000052static bool sectsrect_proc(SkRegion& a, SkRegion& b) {
53 SkIRect r = a.getBounds();
54 r.inset(r.width()/4, r.height()/4);
reed@google.com26dc5b62012-05-02 17:41:13 +000055 return a.intersects(r);
reed@google.com4b4f86d2012-05-02 17:20:02 +000056}
57
reed@google.com50129db2012-04-30 12:07:55 +000058static bool containsxy_proc(SkRegion& a, SkRegion& b) {
59 const SkIRect& r = a.getBounds();
60 const int dx = r.width() / 8;
61 const int dy = r.height() / 8;
62 for (int y = r.fTop; y < r.fBottom; y += dy) {
63 for (int x = r.fLeft; x < r.fRight; x += dx) {
64 (void)a.contains(x, y);
65 }
66 }
67 return true;
68}
69
tfarinaf168b862014-06-19 12:32:29 -070070class RegionBench : public Benchmark {
reed@google.com8c2cc1a2012-04-27 19:29:52 +000071public:
72 typedef bool (*Proc)(SkRegion& a, SkRegion& b);
73
74 SkRegion fA, fB;
75 Proc fProc;
76 SkString fName;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000077
reed@google.com8c2cc1a2012-04-27 19:29:52 +000078 enum {
79 W = 1024,
80 H = 768,
reed@google.com8c2cc1a2012-04-27 19:29:52 +000081 };
82
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000083 SkIRect randrect(SkRandom& rand) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +000084 int x = rand.nextU() % W;
85 int y = rand.nextU() % H;
86 int w = rand.nextU() % W;
87 int h = rand.nextU() % H;
88 return SkIRect::MakeXYWH(x, y, w >> 1, h >> 1);
89 }
90
mtklein@google.com410e6e82013-09-13 19:52:27 +000091 RegionBench(int count, Proc proc, const char name[]) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +000092 fProc = proc;
reed@google.com515d9982012-04-30 14:43:46 +000093 fName.printf("region_%s_%d", name, count);
reed@google.com8c2cc1a2012-04-27 19:29:52 +000094
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000095 SkRandom rand;
reed@google.com50129db2012-04-30 12:07:55 +000096 for (int i = 0; i < count; i++) {
97 fA.op(randrect(rand), SkRegion::kXOR_Op);
98 fB.op(randrect(rand), SkRegion::kXOR_Op);
reed@google.com8c2cc1a2012-04-27 19:29:52 +000099 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000100 }
101
mtklein36352bf2015-03-25 18:17:31 -0700102 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000103 return backend == kNonRendering_Backend;
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000104 }
105
106protected:
107 virtual const char* onGetName() { return fName.c_str(); }
108
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000109 virtual void onDraw(const int loops, SkCanvas* canvas) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000110 Proc proc = fProc;
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000111 for (int i = 0; i < loops; ++i) {
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000112 proc(fA, fB);
113 }
114 }
115
116private:
tfarinaf168b862014-06-19 12:32:29 -0700117 typedef Benchmark INHERITED;
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000118};
119
mike@reedtribe.orgae8f9522014-01-01 20:32:45 +0000120///////////////////////////////////////////////////////////////////////////////
121
reed@google.com50129db2012-04-30 12:07:55 +0000122#define SMALL 16
reed@google.com8c2cc1a2012-04-27 19:29:52 +0000123
mtklein@google.com410e6e82013-09-13 19:52:27 +0000124DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, union_proc, "union")); )
125DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, sect_proc, "intersect")); )
126DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, diff_proc, "difference")); )
127DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, diffrect_proc, "differencerect")); )
128DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, diffrectbig_proc, "differencerectbig")); )
129DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, containsrect_proc, "containsrect")); )
130DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, sectsrgn_proc, "intersectsrgn")); )
131DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, sectsrect_proc, "intersectsrect")); )
132DEF_BENCH( return SkNEW_ARGS(RegionBench, (SMALL, containsxy_proc, "containsxy")); )