1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CC_BASE_REGION_H_
6#define CC_BASE_REGION_H_
7
8#include <string>
9
10#include "base/logging.h"
11#include "base/memory/scoped_ptr.h"
12#include "cc/base/cc_export.h"
13#include "third_party/skia/include/core/SkRegion.h"
14#include "ui/gfx/rect.h"
15#include "ui/gfx/skia_util.h"
16
17namespace base {
18class Value;
19}
20
21namespace cc {
22
23class CC_EXPORT Region {
24 public:
25  Region();
26  Region(const Region& region);
27  Region(gfx::Rect rect);  // NOLINT(runtime/explicit)
28  ~Region();
29
30  const Region& operator=(gfx::Rect rect);
31  const Region& operator=(const Region& region);
32
33  void Swap(Region* region);
34  void Clear();
35  bool IsEmpty() const;
36  int GetRegionComplexity() const;
37
38  bool Contains(gfx::Point point) const;
39  bool Contains(gfx::Rect rect) const;
40  bool Contains(const Region& region) const;
41
42  bool Intersects(gfx::Rect rect) const;
43  bool Intersects(const Region& region) const;
44
45  void Subtract(gfx::Rect rect);
46  void Subtract(const Region& region);
47  void Union(gfx::Rect rect);
48  void Union(const Region& region);
49  void Intersect(gfx::Rect rect);
50  void Intersect(const Region& region);
51
52  bool Equals(const Region& other) const {
53    return skregion_ == other.skregion_;
54  }
55
56  gfx::Rect bounds() const {
57    return gfx::SkIRectToRect(skregion_.getBounds());
58  }
59
60  std::string ToString() const;
61  scoped_ptr<base::Value> AsValue() const;
62
63  class CC_EXPORT Iterator {
64   public:
65    Iterator();
66    explicit Iterator(const Region& region);
67    ~Iterator();
68
69    gfx::Rect rect() const {
70      return gfx::SkIRectToRect(it_.rect());
71    }
72
73    void next() {
74      it_.next();
75    }
76
77    bool has_rect() const {
78      return !it_.done();
79    }
80
81   private:
82    SkRegion::Iterator it_;
83  };
84
85 private:
86  SkRegion skregion_;
87};
88
89inline bool operator==(const Region& a, const Region& b) {
90  return a.Equals(b);
91}
92
93inline bool operator!=(const Region& a, const Region& b) {
94  return !(a == b);
95}
96
97inline Region SubtractRegions(const Region& a, const Region& b) {
98  Region result = a;
99  result.Subtract(b);
100  return result;
101}
102
103inline Region SubtractRegions(const Region& a, gfx::Rect b) {
104  Region result = a;
105  result.Subtract(b);
106  return result;
107}
108
109inline Region IntersectRegions(const Region& a, const Region& b) {
110  Region result = a;
111  result.Intersect(b);
112  return result;
113}
114
115inline Region IntersectRegions(const Region& a, gfx::Rect b) {
116  Region result = a;
117  result.Intersect(b);
118  return result;
119}
120
121inline Region UnionRegions(const Region& a, const Region& b) {
122  Region result = a;
123  result.Union(b);
124  return result;
125}
126
127inline Region UnionRegions(const Region& a, gfx::Rect b) {
128  Region result = a;
129  result.Union(b);
130  return result;
131}
132
133}  // namespace cc
134
135#endif  // CC_BASE_REGION_H_
136