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/memory/scoped_ptr.h"
11#include "cc/base/cc_export.h"
12#include "third_party/skia/include/core/SkRegion.h"
13#include "ui/gfx/rect.h"
14#include "ui/gfx/skia_util.h"
15
16namespace base {
17class Value;
18namespace debug {
19class TracedValue;
20}
21}
22
23namespace cc {
24class SimpleEnclosedRegion;
25
26class CC_EXPORT Region {
27 public:
28  Region();
29  Region(const Region& region);
30  Region(const gfx::Rect& rect);  // NOLINT(runtime/explicit)
31  ~Region();
32
33  const Region& operator=(const gfx::Rect& rect);
34  const Region& operator=(const Region& region);
35
36  void Swap(Region* region);
37  void Clear();
38  bool IsEmpty() const;
39  int GetRegionComplexity() const;
40
41  bool Contains(const gfx::Point& point) const;
42  bool Contains(const gfx::Rect& rect) const;
43  bool Contains(const Region& region) const;
44
45  bool Intersects(const gfx::Rect& rect) const;
46  bool Intersects(const Region& region) const;
47
48  void Subtract(const gfx::Rect& rect);
49  void Subtract(const Region& region);
50  void Subtract(const SimpleEnclosedRegion& region);
51  void Union(const gfx::Rect& rect);
52  void Union(const Region& region);
53  void Intersect(const gfx::Rect& rect);
54  void Intersect(const Region& region);
55
56  bool Equals(const Region& other) const {
57    return skregion_ == other.skregion_;
58  }
59
60  gfx::Rect bounds() const {
61    return gfx::SkIRectToRect(skregion_.getBounds());
62  }
63
64  std::string ToString() const;
65  scoped_ptr<base::Value> AsValue() const;
66  void AsValueInto(base::debug::TracedValue* array) const;
67
68  class CC_EXPORT Iterator {
69   public:
70    Iterator();
71    explicit Iterator(const Region& region);
72    ~Iterator();
73
74    gfx::Rect rect() const {
75      return gfx::SkIRectToRect(it_.rect());
76    }
77
78    void next() {
79      it_.next();
80    }
81
82    bool has_rect() const {
83      return !it_.done();
84    }
85
86   private:
87    SkRegion::Iterator it_;
88  };
89
90 private:
91  SkRegion skregion_;
92};
93
94inline bool operator==(const Region& a, const Region& b) {
95  return a.Equals(b);
96}
97
98inline bool operator!=(const Region& a, const Region& b) {
99  return !(a == b);
100}
101
102inline Region SubtractRegions(const Region& a, const Region& b) {
103  Region result = a;
104  result.Subtract(b);
105  return result;
106}
107
108inline Region SubtractRegions(const Region& a, const gfx::Rect& b) {
109  Region result = a;
110  result.Subtract(b);
111  return result;
112}
113
114inline Region IntersectRegions(const Region& a, const Region& b) {
115  Region result = a;
116  result.Intersect(b);
117  return result;
118}
119
120inline Region IntersectRegions(const Region& a, const gfx::Rect& b) {
121  Region result = a;
122  result.Intersect(b);
123  return result;
124}
125
126inline Region UnionRegions(const Region& a, const Region& b) {
127  Region result = a;
128  result.Union(b);
129  return result;
130}
131
132inline Region UnionRegions(const Region& a, const gfx::Rect& b) {
133  Region result = a;
134  result.Union(b);
135  return result;
136}
137
138}  // namespace cc
139
140#endif  // CC_BASE_REGION_H_
141