| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 | 6 |
| 7 #include "cc/region.h" | 7 #include "cc/region.h" |
| 8 | 8 |
| 9 namespace cc { | 9 namespace cc { |
| 10 | 10 |
| 11 // TODO(danakj) Use method from ui/gfx/skia_utils.h when it exists. | 11 // TODO(danakj) Use method from ui/gfx/skia_utils.h when it exists. |
| 12 static inline SkIRect ToSkIRect(gfx::Rect rect) | 12 static inline SkIRect ToSkIRect(gfx::Rect rect) |
| 13 { | 13 { |
| 14 return SkIRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height()); | 14 return SkIRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height()); |
| 15 } | 15 } |
| 16 | 16 |
| 17 Region::Region() { | 17 Region::Region() { |
| 18 } | 18 } |
| 19 | 19 |
| 20 Region::Region(const Region& region) | 20 Region::Region(const Region& region) |
| 21 #ifdef USE_ANDROID_REGION |
| 22 : droidregion_(region.droidregion_) { |
| 23 #else |
| 21 : skregion_(region.skregion_) { | 24 : skregion_(region.skregion_) { |
| 25 #endif |
| 22 } | 26 } |
| 23 | 27 |
| 24 Region::Region(gfx::Rect rect) | 28 Region::Region(gfx::Rect rect) |
| 29 #ifdef USE_ANDROID_REGION |
| 30 : droidregion_(ToSkIRect(rect)) { |
| 31 #else |
| 25 : skregion_(ToSkIRect(rect)) { | 32 : skregion_(ToSkIRect(rect)) { |
| 33 #endif |
| 26 } | 34 } |
| 27 | 35 |
| 28 Region::~Region() { | 36 Region::~Region() { |
| 29 } | 37 } |
| 30 | 38 |
| 31 const Region& Region::operator=(gfx::Rect rect) { | 39 const Region& Region::operator=(gfx::Rect rect) { |
| 40 #ifdef USE_ANDROID_REGION |
| 41 droidregion_.set(ToSkIRect(rect)); |
| 42 #else |
| 32 skregion_ = SkRegion(ToSkIRect(rect)); | 43 skregion_ = SkRegion(ToSkIRect(rect)); |
| 44 #endif |
| 33 return *this; | 45 return *this; |
| 34 } | 46 } |
| 35 | 47 |
| 36 const Region& Region::operator=(const Region& region) { | 48 const Region& Region::operator=(const Region& region) { |
| 49 #ifdef USE_ANDROID_REGION |
| 50 droidregion_ = region.droidregion_; |
| 51 #else |
| 37 skregion_ = region.skregion_; | 52 skregion_ = region.skregion_; |
| 53 #endif |
| 38 return *this; | 54 return *this; |
| 39 } | 55 } |
| 40 | 56 |
| 41 bool Region::IsEmpty() const { | 57 bool Region::IsEmpty() const { |
| 58 #ifdef USE_ANDROID_REGION |
| 59 return droidregion_.isEmpty(); |
| 60 #else |
| 42 return skregion_.isEmpty(); | 61 return skregion_.isEmpty(); |
| 62 #endif |
| 43 } | 63 } |
| 44 | 64 |
| 45 bool Region::Contains(gfx::Point point) const { | 65 bool Region::Contains(gfx::Point point) const { |
| 66 #ifdef USE_ANDROID_REGION |
| 67 return !droidregion_.intersect(ToSkIRect(gfx::Rect(point, gfx::Size(1, 1)))).i
sEmpty(); |
| 68 #else |
| 46 return skregion_.contains(point.x(), point.y()); | 69 return skregion_.contains(point.x(), point.y()); |
| 70 #endif |
| 47 } | 71 } |
| 48 | 72 |
| 49 bool Region::Contains(gfx::Rect rect) const { | 73 bool Region::Contains(gfx::Rect rect) const { |
| 74 #ifdef USE_ANDROID_REGION |
| 75 if (rect.IsEmpty()) |
| 76 return false; |
| 77 android::Region result = droidregion_.intersect(ToSkIRect(rect)); |
| 78 return result.isRect() && result.bounds() == ToSkIRect(rect); |
| 79 #else |
| 50 return skregion_.contains(ToSkIRect(rect)); | 80 return skregion_.contains(ToSkIRect(rect)); |
| 81 #endif |
| 51 } | 82 } |
| 52 | 83 |
| 53 bool Region::Contains(const Region& region) const { | 84 bool Region::Contains(const Region& region) const { |
| 85 #ifdef USE_ANDROID_REGION |
| 86 if (region.IsEmpty()) |
| 87 return false; |
| 88 android::Region result = region.droidregion_.subtract(droidregion_); |
| 89 return result.isEmpty(); |
| 90 #else |
| 54 return skregion_.contains(region.skregion_); | 91 return skregion_.contains(region.skregion_); |
| 92 #endif |
| 55 } | 93 } |
| 56 | 94 |
| 57 bool Region::Intersects(gfx::Rect rect) const { | 95 bool Region::Intersects(gfx::Rect rect) const { |
| 96 #ifdef USE_ANDROID_REGION |
| 97 return !droidregion_.intersect(ToSkIRect(rect)).isEmpty(); |
| 98 #else |
| 58 return skregion_.intersects(ToSkIRect(rect)); | 99 return skregion_.intersects(ToSkIRect(rect)); |
| 100 #endif |
| 59 } | 101 } |
| 60 | 102 |
| 61 bool Region::Intersects(const Region& region) const { | 103 bool Region::Intersects(const Region& region) const { |
| 104 #ifdef USE_ANDROID_REGION |
| 105 return !droidregion_.intersect(region.droidregion_).isEmpty(); |
| 106 #else |
| 62 return skregion_.intersects(region.skregion_); | 107 return skregion_.intersects(region.skregion_); |
| 108 #endif |
| 63 } | 109 } |
| 64 | 110 |
| 65 void Region::Subtract(gfx::Rect rect) { | 111 void Region::Subtract(gfx::Rect rect) { |
| 112 #ifdef USE_ANDROID_REGION |
| 113 droidregion_.subtractSelf(ToSkIRect(rect)); |
| 114 #else |
| 66 skregion_.op(ToSkIRect(rect), SkRegion::kDifference_Op); | 115 skregion_.op(ToSkIRect(rect), SkRegion::kDifference_Op); |
| 116 #endif |
| 67 } | 117 } |
| 68 | 118 |
| 69 void Region::Subtract(const Region& region) { | 119 void Region::Subtract(const Region& region) { |
| 120 #ifdef USE_ANDROID_REGION |
| 121 droidregion_.subtractSelf(region.droidregion_); |
| 122 #else |
| 70 skregion_.op(region.skregion_, SkRegion::kDifference_Op); | 123 skregion_.op(region.skregion_, SkRegion::kDifference_Op); |
| 124 #endif |
| 71 } | 125 } |
| 72 | 126 |
| 73 void Region::Union(gfx::Rect rect) { | 127 void Region::Union(gfx::Rect rect) { |
| 128 #ifdef USE_ANDROID_REGION |
| 129 droidregion_.orSelf(ToSkIRect(rect)); |
| 130 #else |
| 74 skregion_.op(ToSkIRect(rect), SkRegion::kUnion_Op); | 131 skregion_.op(ToSkIRect(rect), SkRegion::kUnion_Op); |
| 132 #endif |
| 75 } | 133 } |
| 76 | 134 |
| 77 void Region::Union(const Region& region) { | 135 void Region::Union(const Region& region) { |
| 136 #ifdef USE_ANDROID_REGION |
| 137 droidregion_.orSelf(region.droidregion_); |
| 138 #else |
| 78 skregion_.op(region.skregion_, SkRegion::kUnion_Op); | 139 skregion_.op(region.skregion_, SkRegion::kUnion_Op); |
| 140 #endif |
| 79 } | 141 } |
| 80 | 142 |
| 81 void Region::Intersect(gfx::Rect rect) { | 143 void Region::Intersect(gfx::Rect rect) { |
| 144 #ifdef USE_ANDROID_REGION |
| 145 droidregion_.andSelf(ToSkIRect(rect)); |
| 146 #else |
| 82 skregion_.op(ToSkIRect(rect), SkRegion::kIntersect_Op); | 147 skregion_.op(ToSkIRect(rect), SkRegion::kIntersect_Op); |
| 148 #endif |
| 83 } | 149 } |
| 84 | 150 |
| 85 void Region::Intersect(const Region& region) { | 151 void Region::Intersect(const Region& region) { |
| 152 #ifdef USE_ANDROID_REGION |
| 153 droidregion_.andSelf(region.droidregion_); |
| 154 #else |
| 86 skregion_.op(region.skregion_, SkRegion::kIntersect_Op); | 155 skregion_.op(region.skregion_, SkRegion::kIntersect_Op); |
| 156 #endif |
| 157 } |
| 158 |
| 159 bool Region::Equals(const Region& other) const { |
| 160 #ifdef USE_ANDROID_REGION |
| 161 if (droidregion_.bounds() != other.droidregion_.bounds()) |
| 162 return false; |
| 163 android::Region::const_iterator it_me = droidregion_.begin(); |
| 164 android::Region::const_iterator it_them = other.droidregion_.begin(); |
| 165 for (; it_me != droidregion_.end() && it_them != droidregion_.end(); ++it_me,
++it_them) { |
| 166 if (*it_me != *it_them) |
| 167 return false; |
| 168 } |
| 169 if (it_me != droidregion_.end()) |
| 170 return false; |
| 171 if (it_them != other.droidregion_.end()) |
| 172 return false; |
| 173 return true; |
| 174 #else |
| 175 return skregion_ == other.skregion_; |
| 176 #endif |
| 87 } | 177 } |
| 88 | 178 |
| 89 std::string Region::ToString() const { | 179 std::string Region::ToString() const { |
| 90 if (IsEmpty()) | 180 if (IsEmpty()) |
| 91 return gfx::Rect().ToString(); | 181 return gfx::Rect().ToString(); |
| 92 | 182 |
| 93 std::string result; | 183 std::string result; |
| 184 #ifdef USE_ANDROID_REGION |
| 185 for (android::Region::const_iterator it = droidregion_.begin(); it != droidreg
ion_.end(); ++it) { |
| 186 #else |
| 94 for (Iterator it(*this); it.has_rect(); it.next()) { | 187 for (Iterator it(*this); it.has_rect(); it.next()) { |
| 188 #endif |
| 95 if (!result.empty()) | 189 if (!result.empty()) |
| 96 result += " | "; | 190 result += " | "; |
| 191 #ifdef USE_ANDROID_REGION |
| 192 result += gfx::Rect(it->x(), it->y(), it->width(), it->height()).ToString(); |
| 193 #else |
| 97 result += it.rect().ToString(); | 194 result += it.rect().ToString(); |
| 195 #endif |
| 98 } | 196 } |
| 99 return result; | 197 return result; |
| 100 } | 198 } |
| 101 | 199 |
| 102 Region::Iterator::Iterator(const Region& region) | 200 Region::Iterator::Iterator(const Region& region) |
| 201 #ifdef USE_ANDROID_REGION |
| 202 : droidregion_(region.droidregion_), |
| 203 it_(droidregion_.begin()) { |
| 204 #else |
| 103 : it_(region.skregion_) { | 205 : it_(region.skregion_) { |
| 206 #endif |
| 104 } | 207 } |
| 105 | 208 |
| 106 Region::Iterator::~Iterator() { | 209 Region::Iterator::~Iterator() { |
| 107 } | 210 } |
| 108 | 211 |
| 109 } // namespace cc | 212 } // namespace cc |
| OLD | NEW |