blob: a8d6285169901cb0a91712f9f0e92d50cbe41b62 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
Mathias Agopian35801ce2009-05-26 17:44:57 -07002 * Copyright (C) 2009 The Android Open Source Project
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08003 *
Mathias Agopian35801ce2009-05-26 17:44:57 -07004 * 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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08007 *
Mathias Agopian35801ce2009-05-26 17:44:57 -07008 * 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.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080015 */
16
Marin Shalamanov6ad317c2020-07-29 23:34:07 +020017#include <android-base/stringprintf.h>
Jamie Gennis59332802012-05-07 13:49:17 -070018#include <system/graphics.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080019#include <ui/Rect.h>
20
21namespace android {
22
Dan Stoza5065a552015-03-17 16:23:42 -070023const Rect Rect::INVALID_RECT{0, 0, -1, -1};
Pablo Ceballos60d69222015-08-07 14:47:20 -070024const Rect Rect::EMPTY_RECT{0, 0, 0, 0};
Dan Stoza5065a552015-03-17 16:23:42 -070025
Dianne Hackborn9147d112010-07-09 11:44:11 -070026static inline int32_t min(int32_t a, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070027 return (a < b) ? a : b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028}
29
Dianne Hackborn9147d112010-07-09 11:44:11 -070030static inline int32_t max(int32_t a, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070031 return (a > b) ? a : b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032}
33
34void Rect::makeInvalid() {
35 left = 0;
36 top = 0;
37 right = -1;
38 bottom = -1;
39}
40
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070041bool Rect::operator <(const Rect& rhs) const {
42 if (top < rhs.top) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080043 return true;
44 } else if (top == rhs.top) {
45 if (left < rhs.left) {
46 return true;
47 } else if (left == rhs.left) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070048 if (bottom < rhs.bottom) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049 return true;
50 } else if (bottom == rhs.bottom) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070051 if (right < rhs.right) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052 return true;
53 }
54 }
55 }
56 }
57 return false;
58}
59
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070060Rect& Rect::offsetTo(int32_t x, int32_t y) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061 right -= left - x;
62 bottom -= top - y;
63 left = x;
64 top = y;
65 return *this;
66}
67
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070068Rect& Rect::offsetBy(int32_t x, int32_t y) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069 left += x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070070 top += y;
71 right += x;
72 bottom += y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 return *this;
74}
75
Chih-Hung Hsieh91f56832018-12-05 12:13:34 -080076Rect& 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;
Vishnu Nair8033a492018-12-05 07:27:23 -080081 return *this;
82}
83
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070084const Rect Rect::operator +(const Point& rhs) const {
85 const Rect result(left + rhs.x, top + rhs.y, right + rhs.x, bottom + rhs.y);
Mathias Agopian35801ce2009-05-26 17:44:57 -070086 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087}
88
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070089const Rect Rect::operator -(const Point& rhs) const {
90 const Rect result(left - rhs.x, top - rhs.y, right - rhs.x, bottom - rhs.y);
Mathias Agopian35801ce2009-05-26 17:44:57 -070091 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092}
93
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070094bool 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);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 return !(result->isEmpty());
100}
101
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700102Rect Rect::transform(uint32_t xform, int32_t width, int32_t height) const {
Jamie Gennis59332802012-05-07 13:49:17 -0700103 Rect result(*this);
104 if (xform & HAL_TRANSFORM_FLIP_H) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700105 result = Rect(width - result.right, result.top, width - result.left,
106 result.bottom);
Jamie Gennis59332802012-05-07 13:49:17 -0700107 }
108 if (xform & HAL_TRANSFORM_FLIP_V) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700109 result = Rect(result.left, height - result.bottom, result.right,
110 height - result.top);
Jamie Gennis59332802012-05-07 13:49:17 -0700111 }
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
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700122Rect Rect::reduce(const Rect& exclude) const {
Pablo Ceballos60d69222015-08-07 14:47:20 -0700123 Rect result(Rect::EMPTY_RECT);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700124
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) {
Pablo Ceballos19d72c02016-03-09 17:19:22 -0800139 result.right = min(result.right, exclude.left);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700140 } else if (mask & 2) {
Pablo Ceballos19d72c02016-03-09 17:19:22 -0800141 result.bottom = min(result.bottom, exclude.top);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700142 } else if (mask & 4) {
Pablo Ceballos19d72c02016-03-09 17:19:22 -0800143 result.left = max(result.left, exclude.right);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700144 } else if (mask & 8) {
Pablo Ceballos19d72c02016-03-09 17:19:22 -0800145 result.top = max(result.top, exclude.bottom);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700146 }
147 }
148 }
149
150 return result;
151}
152
Marin Shalamanov6ad317c2020-07-29 23:34:07 +0200153std::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
158void PrintTo(const Rect& rect, ::std::ostream* os) {
159 *os << to_string(rect);
160}
161
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162}; // namespace android