blob: 961cbe3e4d39bdb87ea2b131a79c75d34403152f [file] [log] [blame]
reed@google.coma3c13482013-01-31 15:23:44 +00001/*
2 * Copyright 2013 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 */
7
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.coma3c13482013-01-31 15:23:44 +00009#include "SkRandom.h"
10#include "SkRegion.h"
11#include "SkString.h"
12
13static bool sect_proc(SkRegion& a, SkRegion& b) {
14 SkRegion result;
15 return result.op(a, b, SkRegion::kIntersect_Op);
16}
17
tfarinaf168b862014-06-19 12:32:29 -070018class RegionContainBench : public Benchmark {
reed@google.coma3c13482013-01-31 15:23:44 +000019public:
20 typedef bool (*Proc)(SkRegion& a, SkRegion& b);
21 SkRegion fA, fB;
22 Proc fProc;
23 SkString fName;
24
25 enum {
26 W = 200,
27 H = 200,
28 COUNT = 10,
reed@google.coma3c13482013-01-31 15:23:44 +000029 };
30
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000031 SkIRect randrect(SkRandom& rand, int i) {
reed@google.coma3c13482013-01-31 15:23:44 +000032 int w = rand.nextU() % W;
33 return SkIRect::MakeXYWH(0, i*H/COUNT, w, H/COUNT);
34 }
35
mtklein@google.com410e6e82013-09-13 19:52:27 +000036 RegionContainBench(Proc proc, const char name[]) {
reed@google.coma3c13482013-01-31 15:23:44 +000037 fProc = proc;
38 fName.printf("region_contains_%s", name);
39
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000040 SkRandom rand;
reed@google.coma3c13482013-01-31 15:23:44 +000041 for (int i = 0; i < COUNT; i++) {
42 fA.op(randrect(rand, i), SkRegion::kXOR_Op);
43 }
44
45 fB.setRect(0, 0, H, W);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000046 }
reed@google.coma3c13482013-01-31 15:23:44 +000047
mtklein36352bf2015-03-25 18:17:31 -070048 bool isSuitableFor(Backend backend) override {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000049 return backend == kNonRendering_Backend;
reed@google.coma3c13482013-01-31 15:23:44 +000050 }
51
52protected:
mtklein36352bf2015-03-25 18:17:31 -070053 const char* onGetName() override { return fName.c_str(); }
reed@google.coma3c13482013-01-31 15:23:44 +000054
mtkleina1ebeb22015-10-01 09:43:39 -070055 void onDraw(int loops, SkCanvas*) override {
reed@google.coma3c13482013-01-31 15:23:44 +000056 Proc proc = fProc;
57
commit-bot@chromium.org33614712013-12-03 18:17:16 +000058 for (int i = 0; i < loops; ++i) {
reed@google.coma3c13482013-01-31 15:23:44 +000059 proc(fA, fB);
60 }
61 }
62
63private:
tfarinaf168b862014-06-19 12:32:29 -070064 typedef Benchmark INHERITED;
reed@google.coma3c13482013-01-31 15:23:44 +000065};
66
halcanary385fe4d2015-08-26 13:07:48 -070067DEF_BENCH(return new RegionContainBench(sect_proc, "sect");)