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 "Benchmark.h"
9#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
18class RegionContainBench : public Benchmark {
19public:
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
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
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
48    virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
49        return backend == kNonRendering_Backend;
50    }
51
52protected:
53    virtual const char* onGetName() { return fName.c_str(); }
54
55    virtual void onDraw(const int loops, SkCanvas*) {
56        Proc proc = fProc;
57
58        for (int i = 0; i < loops; ++i) {
59           proc(fA, fB);
60        }
61    }
62
63private:
64    typedef Benchmark INHERITED;
65};
66
67DEF_BENCH( return SkNEW_ARGS(RegionContainBench, (sect_proc, "sect")); )
68