blob: 045db31087e6c2234090b90584968a829619e21f [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
Jamie Gennis59332802012-05-07 13:49:17 -070017#include <system/graphics.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018#include <ui/Rect.h>
19
20namespace android {
21
Dan Stoza5065a552015-03-17 16:23:42 -070022const Rect Rect::INVALID_RECT{0, 0, -1, -1};
Pablo Ceballos60d69222015-08-07 14:47:20 -070023const Rect Rect::EMPTY_RECT{0, 0, 0, 0};
Dan Stoza5065a552015-03-17 16:23:42 -070024
Dianne Hackborn9147d112010-07-09 11:44:11 -070025static inline int32_t min(int32_t a, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070026 return (a < b) ? a : b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027}
28
Dianne Hackborn9147d112010-07-09 11:44:11 -070029static inline int32_t max(int32_t a, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070030 return (a > b) ? a : b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031}
32
33void Rect::makeInvalid() {
34 left = 0;
35 top = 0;
36 right = -1;
37 bottom = -1;
38}
39
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070040bool Rect::operator <(const Rect& rhs) const {
41 if (top < rhs.top) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042 return true;
43 } else if (top == rhs.top) {
44 if (left < rhs.left) {
45 return true;
46 } else if (left == rhs.left) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070047 if (bottom < rhs.bottom) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048 return true;
49 } else if (bottom == rhs.bottom) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070050 if (right < rhs.right) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 return true;
52 }
53 }
54 }
55 }
56 return false;
57}
58
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070059Rect& Rect::offsetTo(int32_t x, int32_t y) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060 right -= left - x;
61 bottom -= top - y;
62 left = x;
63 top = y;
64 return *this;
65}
66
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070067Rect& Rect::offsetBy(int32_t x, int32_t y) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068 left += x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070069 top += y;
70 right += x;
71 bottom += y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 return *this;
73}
74
Vishnu Nair8033a492018-12-05 07:27:23 -080075Rect& Rect::inset(int32_t left, int32_t top, int32_t right, int32_t bottom) {
76 this->left += left;
77 this->top += top;
78 this->right -= right;
79 this->bottom -= bottom;
80 return *this;
81}
82
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070083const Rect Rect::operator +(const Point& rhs) const {
84 const Rect result(left + rhs.x, top + rhs.y, right + rhs.x, bottom + rhs.y);
Mathias Agopian35801ce2009-05-26 17:44:57 -070085 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086}
87
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070088const Rect Rect::operator -(const Point& rhs) const {
89 const Rect result(left - rhs.x, top - rhs.y, right - rhs.x, bottom - rhs.y);
Mathias Agopian35801ce2009-05-26 17:44:57 -070090 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091}
92
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070093bool Rect::intersect(const Rect& with, Rect* result) const {
94 result->left = max(left, with.left);
95 result->top = max(top, with.top);
96 result->right = min(right, with.right);
97 result->bottom = min(bottom, with.bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080098 return !(result->isEmpty());
99}
100
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700101Rect Rect::transform(uint32_t xform, int32_t width, int32_t height) const {
Jamie Gennis59332802012-05-07 13:49:17 -0700102 Rect result(*this);
103 if (xform & HAL_TRANSFORM_FLIP_H) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700104 result = Rect(width - result.right, result.top, width - result.left,
105 result.bottom);
Jamie Gennis59332802012-05-07 13:49:17 -0700106 }
107 if (xform & HAL_TRANSFORM_FLIP_V) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700108 result = Rect(result.left, height - result.bottom, result.right,
109 height - result.top);
Jamie Gennis59332802012-05-07 13:49:17 -0700110 }
111 if (xform & HAL_TRANSFORM_ROT_90) {
112 int left = height - result.bottom;
113 int top = result.left;
114 int right = height - result.top;
115 int bottom = result.right;
116 result = Rect(left, top, right, bottom);
117 }
118 return result;
119}
120
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700121Rect Rect::reduce(const Rect& exclude) const {
Pablo Ceballos60d69222015-08-07 14:47:20 -0700122 Rect result(Rect::EMPTY_RECT);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700123
124 uint32_t mask = 0;
125 mask |= (exclude.left > left) ? 1 : 0;
126 mask |= (exclude.top > top) ? 2 : 0;
127 mask |= (exclude.right < right) ? 4 : 0;
128 mask |= (exclude.bottom < bottom) ? 8 : 0;
129
130 if (mask == 0) {
131 // crop entirely covers us
132 result.clear();
133 } else {
134 result = *this;
135 if (!(mask & (mask - 1))) {
136 // power-of-2, i.e.: just one bit is set
137 if (mask & 1) {
Pablo Ceballos19d72c02016-03-09 17:19:22 -0800138 result.right = min(result.right, exclude.left);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700139 } else if (mask & 2) {
Pablo Ceballos19d72c02016-03-09 17:19:22 -0800140 result.bottom = min(result.bottom, exclude.top);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700141 } else if (mask & 4) {
Pablo Ceballos19d72c02016-03-09 17:19:22 -0800142 result.left = max(result.left, exclude.right);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700143 } else if (mask & 8) {
Pablo Ceballos19d72c02016-03-09 17:19:22 -0800144 result.top = max(result.top, exclude.bottom);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700145 }
146 }
147 }
148
149 return result;
150}
151
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152}; // namespace android