Update V8 to version 4.7.21.
[chromium-blink-merge.git] / cc / base / region.cc
blobc048c89e5710ba0412910008c9eabb2fdaa37abc
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.
5 #include "cc/base/region.h"
7 #include "base/trace_event/trace_event_argument.h"
8 #include "base/values.h"
9 #include "cc/base/simple_enclosed_region.h"
11 namespace cc {
13 Region::Region() {
16 Region::Region(const Region& region)
17 : skregion_(region.skregion_) {
20 Region::Region(const gfx::Rect& rect)
21 : skregion_(gfx::RectToSkIRect(rect)) {
24 Region::~Region() {
27 const Region& Region::operator=(const gfx::Rect& rect) {
28 skregion_ = SkRegion(gfx::RectToSkIRect(rect));
29 return *this;
32 const Region& Region::operator=(const Region& region) {
33 skregion_ = region.skregion_;
34 return *this;
37 void Region::Swap(Region* region) {
38 region->skregion_.swap(skregion_);
41 void Region::Clear() {
42 skregion_.setEmpty();
45 bool Region::IsEmpty() const {
46 return skregion_.isEmpty();
49 int Region::GetRegionComplexity() const {
50 return skregion_.computeRegionComplexity();
53 bool Region::Contains(const gfx::Point& point) const {
54 return skregion_.contains(point.x(), point.y());
57 bool Region::Contains(const gfx::Rect& rect) const {
58 if (rect.IsEmpty())
59 return true;
60 return skregion_.contains(gfx::RectToSkIRect(rect));
63 bool Region::Contains(const Region& region) const {
64 if (region.IsEmpty())
65 return true;
66 return skregion_.contains(region.skregion_);
69 bool Region::Intersects(const gfx::Rect& rect) const {
70 return skregion_.intersects(gfx::RectToSkIRect(rect));
73 bool Region::Intersects(const Region& region) const {
74 return skregion_.intersects(region.skregion_);
77 void Region::Subtract(const gfx::Rect& rect) {
78 skregion_.op(gfx::RectToSkIRect(rect), SkRegion::kDifference_Op);
81 void Region::Subtract(const Region& region) {
82 skregion_.op(region.skregion_, SkRegion::kDifference_Op);
85 void Region::Subtract(const SimpleEnclosedRegion& region) {
86 for (size_t i = 0; i < region.GetRegionComplexity(); ++i) {
87 skregion_.op(gfx::RectToSkIRect(region.GetRect(i)),
88 SkRegion::kDifference_Op);
92 void Region::Union(const gfx::Rect& rect) {
93 skregion_.op(gfx::RectToSkIRect(rect), SkRegion::kUnion_Op);
96 void Region::Union(const Region& region) {
97 skregion_.op(region.skregion_, SkRegion::kUnion_Op);
100 void Region::Intersect(const gfx::Rect& rect) {
101 skregion_.op(gfx::RectToSkIRect(rect), SkRegion::kIntersect_Op);
104 void Region::Intersect(const Region& region) {
105 skregion_.op(region.skregion_, SkRegion::kIntersect_Op);
108 std::string Region::ToString() const {
109 if (IsEmpty())
110 return gfx::Rect().ToString();
112 std::string result;
113 for (Iterator it(*this); it.has_rect(); it.next()) {
114 if (!result.empty())
115 result += " | ";
116 result += it.rect().ToString();
118 return result;
121 scoped_ptr<base::Value> Region::AsValue() const {
122 scoped_ptr<base::ListValue> result(new base::ListValue());
123 for (Iterator it(*this); it.has_rect(); it.next()) {
124 gfx::Rect rect(it.rect());
125 result->AppendInteger(rect.x());
126 result->AppendInteger(rect.y());
127 result->AppendInteger(rect.width());
128 result->AppendInteger(rect.height());
130 return result.Pass();
133 void Region::AsValueInto(base::trace_event::TracedValue* result) const {
134 for (Iterator it(*this); it.has_rect(); it.next()) {
135 gfx::Rect rect(it.rect());
136 result->AppendInteger(rect.x());
137 result->AppendInteger(rect.y());
138 result->AppendInteger(rect.width());
139 result->AppendInteger(rect.height());
143 Region::Iterator::Iterator() {
146 Region::Iterator::Iterator(const Region& region)
147 : it_(region.skregion_) {
150 Region::Iterator::~Iterator() {
153 } // namespace cc