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