1 // Copyright 2014 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_SIMPLE_ENCLOSED_REGION_H_
6 #define CC_BASE_SIMPLE_ENCLOSED_REGION_H_
7 
8 #include <stddef.h>
9 
10 #include <string>
11 
12 #include "cc/base/base_export.h"
13 #include "ui/gfx/geometry/rect.h"
14 
15 namespace cc {
16 
17 class Region;
18 
19 // A constant-sized approximation of a Region. The SimpleEnclosedRegion may
20 // exclude points in its approximation (may have false negatives) but will never
21 // include a point that would not be in the actual Region (no false positives).
22 class CC_BASE_EXPORT SimpleEnclosedRegion {
23  public:
SimpleEnclosedRegion()24   SimpleEnclosedRegion() : rect_() {}
SimpleEnclosedRegion(const SimpleEnclosedRegion & region)25   SimpleEnclosedRegion(const SimpleEnclosedRegion& region)
26       : rect_(region.rect_) {}
SimpleEnclosedRegion(const gfx::Rect & rect)27   explicit SimpleEnclosedRegion(const gfx::Rect& rect) : rect_(rect) {}
SimpleEnclosedRegion(int x,int y,int w,int h)28   SimpleEnclosedRegion(int x, int y, int w, int h) : rect_(x, y, w, h) {}
SimpleEnclosedRegion(int w,int h)29   SimpleEnclosedRegion(int w, int h) : rect_(w, h) {}
30   explicit SimpleEnclosedRegion(const Region& region);
31   ~SimpleEnclosedRegion();
32 
33   const SimpleEnclosedRegion& operator=(const gfx::Rect& rect) {
34     rect_ = rect;
35     return *this;
36   }
37   const SimpleEnclosedRegion& operator=(const SimpleEnclosedRegion& region) {
38     rect_ = region.rect_;
39     return *this;
40   }
41 
IsEmpty()42   bool IsEmpty() const { return rect_.IsEmpty(); }
Clear()43   void Clear() { rect_ = gfx::Rect(); }
GetRegionComplexity()44   size_t GetRegionComplexity() const { return rect_.IsEmpty() ? 0 : 1; }
45 
Contains(const gfx::Point & point)46   bool Contains(const gfx::Point& point) const { return rect_.Contains(point); }
Contains(const gfx::Rect & rect)47   bool Contains(const gfx::Rect& rect) const { return rect_.Contains(rect); }
Contains(const SimpleEnclosedRegion & region)48   bool Contains(const SimpleEnclosedRegion& region) const {
49     return rect_.Contains(region.rect_);
50   }
51 
Intersects(const gfx::Rect & rect)52   bool Intersects(const gfx::Rect& rect) const {
53     return rect_.Intersects(rect);
54   }
Intersects(const SimpleEnclosedRegion & region)55   bool Intersects(const SimpleEnclosedRegion& region) const {
56     return rect_.Intersects(region.rect_);
57   }
58 
59   void Subtract(const gfx::Rect& sub_rect);
Subtract(const SimpleEnclosedRegion & sub_region)60   void Subtract(const SimpleEnclosedRegion& sub_region) {
61     Subtract(sub_region.rect_);
62   }
63   void Union(const gfx::Rect& new_rect);
Union(const SimpleEnclosedRegion & new_region)64   void Union(const SimpleEnclosedRegion& new_region) {
65     Union(new_region.rect_);
66   }
Intersect(const gfx::Rect & in_rect)67   void Intersect(const gfx::Rect& in_rect) { return rect_.Intersect(in_rect); }
Intersect(const SimpleEnclosedRegion & in_region)68   void Intersect(const SimpleEnclosedRegion& in_region) {
69     Intersect(in_region.rect_);
70   }
71 
Equals(const SimpleEnclosedRegion & other)72   bool Equals(const SimpleEnclosedRegion& other) const {
73     bool both_empty = rect_.IsEmpty() && other.rect_.IsEmpty();
74     return both_empty || rect_ == other.rect_;
75   }
76 
bounds()77   gfx::Rect bounds() const { return rect_; }
78 
79   // The value of |i| must be less than GetRegionComplexity().
80   gfx::Rect GetRect(size_t i) const;
81 
ToString()82   std::string ToString() const { return rect_.ToString(); }
83 
84  private:
85   gfx::Rect rect_;
86 };
87 
88 inline bool operator==(const SimpleEnclosedRegion& a,
89                        const SimpleEnclosedRegion& b) {
90   return a.Equals(b);
91 }
92 
93 inline bool operator!=(const SimpleEnclosedRegion& a,
94                        const SimpleEnclosedRegion& b) {
95   return !(a == b);
96 }
97 
SubtractSimpleEnclosedRegions(const SimpleEnclosedRegion & a,const SimpleEnclosedRegion & b)98 inline SimpleEnclosedRegion SubtractSimpleEnclosedRegions(
99     const SimpleEnclosedRegion& a,
100     const SimpleEnclosedRegion& b) {
101   SimpleEnclosedRegion result = a;
102   result.Subtract(b);
103   return result;
104 }
105 
IntersectSimpleEnclosedRegions(const SimpleEnclosedRegion & a,const SimpleEnclosedRegion & b)106 inline SimpleEnclosedRegion IntersectSimpleEnclosedRegions(
107     const SimpleEnclosedRegion& a,
108     const SimpleEnclosedRegion& b) {
109   SimpleEnclosedRegion result = a;
110   result.Intersect(b);
111   return result;
112 }
113 
UnionSimpleEnclosedRegions(const SimpleEnclosedRegion & a,const SimpleEnclosedRegion & b)114 inline SimpleEnclosedRegion UnionSimpleEnclosedRegions(
115     const SimpleEnclosedRegion& a,
116     const SimpleEnclosedRegion& b) {
117   SimpleEnclosedRegion result = a;
118   result.Union(b);
119   return result;
120 }
121 
122 }  // namespace cc
123 
124 #endif  // CC_BASE_SIMPLE_ENCLOSED_REGION_H_
125