blob: d8702e5755573687bc2eee472cf7a808b68b26c5 [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
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070075const Rect Rect::operator +(const Point& rhs) const {
76 const Rect result(left + rhs.x, top + rhs.y, right + rhs.x, bottom + rhs.y);
Mathias Agopian35801ce2009-05-26 17:44:57 -070077 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078}
79
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070080const Rect Rect::operator -(const Point& rhs) const {
81 const Rect result(left - rhs.x, top - rhs.y, right - rhs.x, bottom - rhs.y);
Mathias Agopian35801ce2009-05-26 17:44:57 -070082 return result;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083}
84
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070085bool Rect::intersect(const Rect& with, Rect* result) const {
86 result->left = max(left, with.left);
87 result->top = max(top, with.top);
88 result->right = min(right, with.right);
89 result->bottom = min(bottom, with.bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 return !(result->isEmpty());
91}
92
Jamie Gennisf15a83f2012-05-10 20:43:55 -070093Rect Rect::transform(uint32_t xform, int32_t width, int32_t height) const {
Jamie Gennis59332802012-05-07 13:49:17 -070094 Rect result(*this);
95 if (xform & HAL_TRANSFORM_FLIP_H) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070096 result = Rect(width - result.right, result.top, width - result.left,
97 result.bottom);
Jamie Gennis59332802012-05-07 13:49:17 -070098 }
99 if (xform & HAL_TRANSFORM_FLIP_V) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700100 result = Rect(result.left, height - result.bottom, result.right,
101 height - result.top);
Jamie Gennis59332802012-05-07 13:49:17 -0700102 }
103 if (xform & HAL_TRANSFORM_ROT_90) {
104 int left = height - result.bottom;
105 int top = result.left;
106 int right = height - result.top;
107 int bottom = result.right;
108 result = Rect(left, top, right, bottom);
109 }
110 return result;
111}
112
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700113Rect Rect::reduce(const Rect& exclude) const {
Pablo Ceballos60d69222015-08-07 14:47:20 -0700114 Rect result(Rect::EMPTY_RECT);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700115
116 uint32_t mask = 0;
117 mask |= (exclude.left > left) ? 1 : 0;
118 mask |= (exclude.top > top) ? 2 : 0;
119 mask |= (exclude.right < right) ? 4 : 0;
120 mask |= (exclude.bottom < bottom) ? 8 : 0;
121
122 if (mask == 0) {
123 // crop entirely covers us
124 result.clear();
125 } else {
126 result = *this;
127 if (!(mask & (mask - 1))) {
128 // power-of-2, i.e.: just one bit is set
129 if (mask & 1) {
Pablo Ceballos19d72c02016-03-09 17:19:22 -0800130 result.right = min(result.right, exclude.left);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700131 } else if (mask & 2) {
Pablo Ceballos19d72c02016-03-09 17:19:22 -0800132 result.bottom = min(result.bottom, exclude.top);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700133 } else if (mask & 4) {
Pablo Ceballos19d72c02016-03-09 17:19:22 -0800134 result.left = max(result.left, exclude.right);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700135 } else if (mask & 8) {
Pablo Ceballos19d72c02016-03-09 17:19:22 -0800136 result.top = max(result.top, exclude.bottom);
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700137 }
138 }
139 }
140
141 return result;
142}
143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144}; // namespace android