• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "bench/Benchmark.h"
9 #include "include/core/SkRegion.h"
10 #include "include/core/SkString.h"
11 #include "include/utils/SkRandom.h"
12 
sect_proc(SkRegion & a,SkRegion & b)13 static bool sect_proc(SkRegion& a, SkRegion& b) {
14     SkRegion result;
15     return result.op(a, b, SkRegion::kIntersect_Op);
16 }
17 
18 class RegionContainBench : public Benchmark {
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,
29     };
30 
randrect(SkRandom & rand,int i)31     SkIRect randrect(SkRandom& rand, int i) {
32         int w = rand.nextU() % W;
33         return SkIRect::MakeXYWH(0, i*H/COUNT, w, H/COUNT);
34     }
35 
RegionContainBench(Proc proc,const char name[])36     RegionContainBench(Proc proc, const char name[])  {
37         fProc = proc;
38         fName.printf("region_contains_%s", name);
39 
40         SkRandom rand;
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});
46     }
47 
isSuitableFor(Backend backend)48     bool isSuitableFor(Backend backend) override {
49         return backend == kNonRendering_Backend;
50     }
51 
52 protected:
onGetName()53     const char* onGetName() override { return fName.c_str(); }
54 
onDraw(int loops,SkCanvas *)55     void onDraw(int loops, SkCanvas*) override {
56         Proc proc = fProc;
57 
58         for (int i = 0; i < loops; ++i) {
59            proc(fA, fB);
60         }
61     }
62 
63 private:
64     using INHERITED = Benchmark;
65 };
66 
67 DEF_BENCH(return new RegionContainBench(sect_proc, "sect");)
68