reed@google.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
reed@google.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 9 | #include "SkRandom.h" |
| 10 | #include "SkRegion.h" |
| 11 | #include "SkString.h" |
| 12 | |
| 13 | static bool sect_proc(SkRegion& a, SkRegion& b) { |
| 14 | SkRegion result; |
| 15 | return result.op(a, b, SkRegion::kIntersect_Op); |
| 16 | } |
| 17 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 18 | class RegionContainBench : public Benchmark { |
reed@google.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 19 | public: |
| 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.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 31 | SkIRect randrect(SkRandom& rand, int i) { |
reed@google.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 32 | int w = rand.nextU() % W; |
| 33 | return SkIRect::MakeXYWH(0, i*H/COUNT, w, H/COUNT); |
| 34 | } |
| 35 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 36 | RegionContainBench(Proc proc, const char name[]) { |
reed@google.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 37 | fProc = proc; |
| 38 | fName.printf("region_contains_%s", name); |
| 39 | |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 40 | SkRandom rand; |
reed@google.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 41 | 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.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 46 | } |
reed@google.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 47 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 48 | bool isSuitableFor(Backend backend) override { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 49 | return backend == kNonRendering_Backend; |
reed@google.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | const char* onGetName() override { return fName.c_str(); } |
reed@google.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 54 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 55 | void onDraw(int loops, SkCanvas*) override { |
reed@google.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 56 | Proc proc = fProc; |
| 57 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 58 | for (int i = 0; i < loops; ++i) { |
reed@google.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 59 | proc(fA, fB); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 64 | typedef Benchmark INHERITED; |
reed@google.com | a3c1348 | 2013-01-31 15:23:44 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 67 | DEF_BENCH(return new RegionContainBench(sect_proc, "sect");) |