blob: dcce21f15a77913c40bd8629c0669b03b2a9ab3a [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};
23
Dianne Hackborn9147d112010-07-09 11:44:11 -070024static inline int32_t min(int32_t a, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070025 return (a < b) ? a : b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026}
27
Dianne Hackborn9147d112010-07-09 11:44:11 -070028static inline int32_t max(int32_t a, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070029 return (a > b) ? a : b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030}
31
32void Rect::makeInvalid() {
33 left = 0;
34 top = 0;
35 right = -1;
36 bottom = -1;
37}
38
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070039bool Rect::operator <(const Rect& rhs) const {
40 if (top < rhs.top) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041 return true;
42 } else if (top == rhs.top) {
43 if (left < rhs.left) {
44 return true;
45 } else if (left == rhs.left) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070046 if (bottom < rhs.bottom) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047 return true;
48 } else if (bottom == rhs.bottom) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070049 if (right < rhs.right) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050 return true;
51 }
52 }
53 }
54 }
55 return false;
56}
57
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070058Rect& Rect::offsetTo(int32_t x, int32_t y) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 right -= left - x;
60 bottom -= top - y;
61 left = x;
62 top = y;
63 return *this;
64}
65
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070066Rect& Rect::offsetBy(int32_t x, int32_t y) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067 left += x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070068 top += y;
69 right += x;
70 bottom += y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071 return *this;
72}
73
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070074const Rect Rect::operator +(const Point& rhs) const {
75 const Rect result(left + rhs.x, top + rhs.y, right + rhs.x, bottom + rhs.y);
Mathias Agopian35801ce2009-05-26 17:44:57 -070076 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077}
78
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070079const Rect Rect::operator -(const Point& rhs) const {
80 const Rect result(left - rhs.x, top - rhs.y, right - rhs.x, bottom - rhs.y);
Mathias Agopian35801ce2009-05-26 17:44:57 -070081 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082}
83
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070084bool Rect::intersect(const Rect& with, Rect* result) const {
85 result->left = max(left, with.left);
86 result->top = max(top, with.top);
87 result->right = min(right, with.right);
88 result->bottom = min(bottom, with.bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 return !(result->isEmpty());
90}
91
Jamie Gennisf15a83f2012-05-10 20:43:55 -070092Rect Rect::transform(uint32_t xform, int32_t width, int32_t height) const {
Jamie Gennis59332802012-05-07 13:49:17 -070093 Rect result(*this);
94 if (xform & HAL_TRANSFORM_FLIP_H) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070095 result = Rect(width - result.right, result.top, width - result.left,
96 result.bottom);
Jamie Gennis59332802012-05-07 13:49:17 -070097 }
98 if (xform & HAL_TRANSFORM_FLIP_V) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070099 result = Rect(result.left, height - result.bottom, result.right,
100 height - result.top);
Jamie Gennis59332802012-05-07 13:49:17 -0700101 }
102 if (xform & HAL_TRANSFORM_ROT_90) {
103 int left = height - result.bottom;
104 int top = result.left;
105 int right = height - result.top;
106 int bottom = result.right;
107 result = Rect(left, top, right, bottom);
108 }
109 return result;
110}
111
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700112Rect Rect::reduce(const Rect& exclude) const {
113 Rect result;
114
115 uint32_t mask = 0;
116 mask |= (exclude.left > left) ? 1 : 0;
117 mask |= (exclude.top > top) ? 2 : 0;
118 mask |= (exclude.right < right) ? 4 : 0;
119 mask |= (exclude.bottom < bottom) ? 8 : 0;
120
121 if (mask == 0) {
122 // crop entirely covers us
123 result.clear();
124 } else {
125 result = *this;
126 if (!(mask & (mask - 1))) {
127 // power-of-2, i.e.: just one bit is set
128 if (mask & 1) {
129 result.right = exclude.left;
130 } else if (mask & 2) {
131 result.bottom = exclude.top;
132 } else if (mask & 4) {
133 result.left = exclude.right;
134 } else if (mask & 8) {
135 result.top = exclude.bottom;
136 }
137 }
138 }
139
140 return result;
141}
142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143}; // namespace android