blob: 0bec0b7f788794da180c683adc80b990dd31af6d [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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#ifndef ANDROID_UI_RECT
18#define ANDROID_UI_RECT
19
Mathias Agopian8683fca2012-08-12 19:37:16 -070020#include <utils/Flattenable.h>
Dan Stozadd883c02014-11-18 10:24:03 -080021#include <utils/Log.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <utils/TypeHelpers.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070023#include <log/log.h>
24
Dan Stoza5a423ea2017-02-16 14:10:39 -080025#include <ui/FloatRect.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026#include <ui/Point.h>
27
Dianne Hackborn9147d112010-07-09 11:44:11 -070028#include <android/rect.h>
29
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030namespace android {
31
Mathias Agopian8683fca2012-08-12 19:37:16 -070032class Rect : public ARect, public LightFlattenablePod<Rect>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033{
34public:
Mathias Agopian000e95e2011-09-19 16:00:46 -070035 typedef ARect::value_type value_type;
Mathias Agopian4b8160f2009-05-27 15:02:35 -070036
Dan Stoza5065a552015-03-17 16:23:42 -070037 static const Rect INVALID_RECT;
Pablo Ceballos60d69222015-08-07 14:47:20 -070038 static const Rect EMPTY_RECT;
Dan Stoza5065a552015-03-17 16:23:42 -070039
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040 // we don't provide copy-ctor and operator= on purpose
41 // because we want the compiler generated versions
42
Pablo Ceballos60d69222015-08-07 14:47:20 -070043 inline Rect() : Rect(INVALID_RECT) {}
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070044
Dan Stoza4e643af2016-01-08 13:22:49 -080045 template <typename T>
46 inline Rect(T w, T h) {
Dan Stozadd883c02014-11-18 10:24:03 -080047 if (w > INT32_MAX) {
Dan Stozadd883c02014-11-18 10:24:03 -080048 w = INT32_MAX;
49 }
50 if (h > INT32_MAX) {
Dan Stozadd883c02014-11-18 10:24:03 -080051 h = INT32_MAX;
52 }
53 left = top = 0;
Dan Stoza4e643af2016-01-08 13:22:49 -080054 right = static_cast<int32_t>(w);
55 bottom = static_cast<int32_t>(h);
Dan Stozadd883c02014-11-18 10:24:03 -080056 }
57
Dianne Hackborn9147d112010-07-09 11:44:11 -070058 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070059 left = l;
60 top = t;
61 right = r;
62 bottom = b;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070064
Dianne Hackborn9147d112010-07-09 11:44:11 -070065 inline Rect(const Point& lt, const Point& rb) {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070066 left = lt.x;
67 top = lt.y;
68 right = rb.x;
69 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 }
71
Dan Stoza80d61162017-12-20 15:57:52 -080072 inline explicit Rect(const FloatRect& floatRect) {
73 // Ideally we would use std::round, but we don't want to add an STL
74 // dependency here, so we use an approximation
75 left = static_cast<int32_t>(floatRect.left + 0.5f);
76 top = static_cast<int32_t>(floatRect.top + 0.5f);
77 right = static_cast<int32_t>(floatRect.right + 0.5f);
78 bottom = static_cast<int32_t>(floatRect.bottom + 0.5f);
79 }
80
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081 void makeInvalid();
Mathias Agopian4b8160f2009-05-27 15:02:35 -070082
83 inline void clear() {
84 left = top = right = bottom = 0;
85 }
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 // a valid rectangle has a non negative width and height
88 inline bool isValid() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070089 return (getWidth() >= 0) && (getHeight() >= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 }
91
92 // an empty rect has a zero width or height, or is invalid
93 inline bool isEmpty() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -070094 return (getWidth() <= 0) || (getHeight() <= 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080095 }
96
97 // rectangle's width
Ivan Lozanoabf347c2017-12-12 15:59:01 -080098 __attribute__((no_sanitize("signed-integer-overflow")))
Mathias Agopianb82203a2012-05-13 20:02:04 -070099 inline int32_t getWidth() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700100 return right - left;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 }
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700102
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 // rectangle's height
Ivan Lozanoabf347c2017-12-12 15:59:01 -0800104 __attribute__((no_sanitize("signed-integer-overflow")))
Mathias Agopianb82203a2012-05-13 20:02:04 -0700105 inline int32_t getHeight() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700106 return bottom - top;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 }
108
Ivan Lozanoabf347c2017-12-12 15:59:01 -0800109 __attribute__((no_sanitize("signed-integer-overflow")))
Mathias Agopianb82203a2012-05-13 20:02:04 -0700110 inline Rect getBounds() const {
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700111 return Rect(right - left, bottom - top);
Mathias Agopianb82203a2012-05-13 20:02:04 -0700112 }
113
Mathias Agopian35801ce2009-05-26 17:44:57 -0700114 void setLeftTop(const Point& lt) {
115 left = lt.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700116 top = lt.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700118
119 void setRightBottom(const Point& rb) {
120 right = rb.x;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700121 bottom = rb.y;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 }
123
124 // the following 4 functions return the 4 corners of the rect as Point
Mathias Agopian35801ce2009-05-26 17:44:57 -0700125 Point leftTop() const {
126 return Point(left, top);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127 }
Mathias Agopian35801ce2009-05-26 17:44:57 -0700128 Point rightBottom() const {
129 return Point(right, bottom);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 }
131 Point rightTop() const {
132 return Point(right, top);
133 }
134 Point leftBottom() const {
135 return Point(left, bottom);
136 }
137
138 // comparisons
139 inline bool operator == (const Rect& rhs) const {
140 return (left == rhs.left) && (top == rhs.top) &&
141 (right == rhs.right) && (bottom == rhs.bottom);
142 }
143
144 inline bool operator != (const Rect& rhs) const {
145 return !operator == (rhs);
146 }
147
148 // operator < defines an order which allows to use rectangles in sorted
149 // vectors.
150 bool operator < (const Rect& rhs) const;
151
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700152 const Rect operator + (const Point& rhs) const;
153 const Rect operator - (const Point& rhs) const;
154
155 Rect& operator += (const Point& rhs) {
156 return offsetBy(rhs.x, rhs.y);
157 }
158 Rect& operator -= (const Point& rhs) {
159 return offsetBy(-rhs.x, -rhs.y);
160 }
161
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 Rect& offsetToOrigin() {
163 right -= left;
164 bottom -= top;
165 left = top = 0;
166 return *this;
167 }
168 Rect& offsetTo(const Point& p) {
169 return offsetTo(p.x, p.y);
170 }
171 Rect& offsetBy(const Point& dp) {
172 return offsetBy(dp.x, dp.y);
173 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700175 Rect& offsetTo(int32_t x, int32_t y);
176 Rect& offsetBy(int32_t x, int32_t y);
Jamie Gennis59332802012-05-07 13:49:17 -0700177
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700178 bool intersect(const Rect& with, Rect* result) const;
Jamie Gennis59332802012-05-07 13:49:17 -0700179
180 // Create a new Rect by transforming this one using a graphics HAL
181 // transform. This rectangle is defined in a coordinate space starting at
182 // the origin and extending to (width, height). If the transform includes
183 // a ROT90 then the output rectangle is defined in a space extending to
184 // (height, width). Otherwise the output rectangle is in the same space as
185 // the input.
Jamie Gennisf15a83f2012-05-10 20:43:55 -0700186 Rect transform(uint32_t xform, int32_t width, int32_t height) const;
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700187
Mathias Agopianf3e85d42013-05-10 18:01:12 -0700188 // this calculates (Region(*this) - exclude).bounds() efficiently
189 Rect reduce(const Rect& exclude) const;
190
Mathias Agopian6c7f25a2013-05-09 20:37:10 -0700191 // for backward compatibility
192 inline int32_t width() const { return getWidth(); }
193 inline int32_t height() const { return getHeight(); }
194 inline void set(const Rect& rhs) { operator = (rhs); }
Dan Stoza71bded52016-10-19 11:10:33 -0700195
Dan Stoza5a423ea2017-02-16 14:10:39 -0800196 FloatRect toFloatRect() const {
Dan Stoza71bded52016-10-19 11:10:33 -0700197 return {static_cast<float>(left), static_cast<float>(top),
198 static_cast<float>(right), static_cast<float>(bottom)};
199 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200};
201
202ANDROID_BASIC_TYPES_TRAITS(Rect)
203
204}; // namespace android
205
206#endif // ANDROID_UI_RECT