1 /*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <android-base/stringprintf.h>
18 #include <system/graphics.h>
19 #include <ui/Rect.h>
20
21 namespace android {
22
23 const Rect Rect::INVALID_RECT{0, 0, -1, -1};
24 const Rect Rect::EMPTY_RECT{0, 0, 0, 0};
25
min(int32_t a,int32_t b)26 static inline int32_t min(int32_t a, int32_t b) {
27 return (a < b) ? a : b;
28 }
29
max(int32_t a,int32_t b)30 static inline int32_t max(int32_t a, int32_t b) {
31 return (a > b) ? a : b;
32 }
33
makeInvalid()34 void Rect::makeInvalid() {
35 left = 0;
36 top = 0;
37 right = -1;
38 bottom = -1;
39 }
40
operator <(const Rect & rhs) const41 bool Rect::operator <(const Rect& rhs) const {
42 if (top < rhs.top) {
43 return true;
44 } else if (top == rhs.top) {
45 if (left < rhs.left) {
46 return true;
47 } else if (left == rhs.left) {
48 if (bottom < rhs.bottom) {
49 return true;
50 } else if (bottom == rhs.bottom) {
51 if (right < rhs.right) {
52 return true;
53 }
54 }
55 }
56 }
57 return false;
58 }
59
offsetTo(int32_t x,int32_t y)60 Rect& Rect::offsetTo(int32_t x, int32_t y) {
61 right -= left - x;
62 bottom -= top - y;
63 left = x;
64 top = y;
65 return *this;
66 }
67
offsetBy(int32_t x,int32_t y)68 Rect& Rect::offsetBy(int32_t x, int32_t y) {
69 left += x;
70 top += y;
71 right += x;
72 bottom += y;
73 return *this;
74 }
75
inset(int32_t _left,int32_t _top,int32_t _right,int32_t _bottom)76 Rect& Rect::inset(int32_t _left, int32_t _top, int32_t _right, int32_t _bottom) {
77 this->left += _left;
78 this->top += _top;
79 this->right -= _right;
80 this->bottom -= _bottom;
81 return *this;
82 }
83
operator +(const Point & rhs) const84 const Rect Rect::operator +(const Point& rhs) const {
85 const Rect result(left + rhs.x, top + rhs.y, right + rhs.x, bottom + rhs.y);
86 return result;
87 }
88
operator -(const Point & rhs) const89 const Rect Rect::operator -(const Point& rhs) const {
90 const Rect result(left - rhs.x, top - rhs.y, right - rhs.x, bottom - rhs.y);
91 return result;
92 }
93
intersect(const Rect & with,Rect * result) const94 bool Rect::intersect(const Rect& with, Rect* result) const {
95 result->left = max(left, with.left);
96 result->top = max(top, with.top);
97 result->right = min(right, with.right);
98 result->bottom = min(bottom, with.bottom);
99 return !(result->isEmpty());
100 }
101
transform(uint32_t xform,int32_t width,int32_t height) const102 Rect Rect::transform(uint32_t xform, int32_t width, int32_t height) const {
103 Rect result(*this);
104 if (xform & HAL_TRANSFORM_FLIP_H) {
105 result = Rect(width - result.right, result.top, width - result.left,
106 result.bottom);
107 }
108 if (xform & HAL_TRANSFORM_FLIP_V) {
109 result = Rect(result.left, height - result.bottom, result.right,
110 height - result.top);
111 }
112 if (xform & HAL_TRANSFORM_ROT_90) {
113 int left = height - result.bottom;
114 int top = result.left;
115 int right = height - result.top;
116 int bottom = result.right;
117 result = Rect(left, top, right, bottom);
118 }
119 return result;
120 }
121
reduce(const Rect & exclude) const122 Rect Rect::reduce(const Rect& exclude) const {
123 Rect result(Rect::EMPTY_RECT);
124
125 uint32_t mask = 0;
126 mask |= (exclude.left > left) ? 1 : 0;
127 mask |= (exclude.top > top) ? 2 : 0;
128 mask |= (exclude.right < right) ? 4 : 0;
129 mask |= (exclude.bottom < bottom) ? 8 : 0;
130
131 if (mask == 0) {
132 // crop entirely covers us
133 result.clear();
134 } else {
135 result = *this;
136 if (!(mask & (mask - 1))) {
137 // power-of-2, i.e.: just one bit is set
138 if (mask & 1) {
139 result.right = min(result.right, exclude.left);
140 } else if (mask & 2) {
141 result.bottom = min(result.bottom, exclude.top);
142 } else if (mask & 4) {
143 result.left = max(result.left, exclude.right);
144 } else if (mask & 8) {
145 result.top = max(result.top, exclude.bottom);
146 }
147 }
148 }
149
150 return result;
151 }
152
to_string(const android::Rect & rect)153 std::string to_string(const android::Rect& rect) {
154 return android::base::StringPrintf("Rect(%d, %d, %d, %d)", rect.left, rect.top, rect.right,
155 rect.bottom);
156 }
157
PrintTo(const Rect & rect,::std::ostream * os)158 void PrintTo(const Rect& rect, ::std::ostream* os) {
159 *os << to_string(rect);
160 }
161
162 }; // namespace android
163