blob: 1716cf0ccb911fffa0b37498966ed00c4623745a [file] [log] [blame]
Romain Guybb9524b2010-06-22 18:56:38 -07001/*
2 * Copyright (C) 2010 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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_RECT_H
18#define ANDROID_HWUI_RECT_H
19
20#include <cmath>
ztenghuiaf6f7ed2014-03-18 17:25:49 -070021#include <SkRect.h>
Romain Guybb9524b2010-06-22 18:56:38 -070022
Romain Guy5cbbce52010-06-27 22:59:20 -070023#include <utils/Log.h>
24
Chris Craik32f05e32013-09-17 16:20:29 -070025#include "Vertex.h"
26
Romain Guybb9524b2010-06-22 18:56:38 -070027namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070028namespace uirenderer {
Romain Guybb9524b2010-06-22 18:56:38 -070029
Chris Craik62d307c2014-07-29 10:35:13 -070030#define RECT_STRING "%5.2f %5.2f %5.2f %5.2f"
Chris Craik28ce94a2013-05-31 11:38:03 -070031#define RECT_ARGS(r) \
32 (r).left, (r).top, (r).right, (r).bottom
Chris Craik3f0854292014-04-15 16:18:08 -070033#define SK_RECT_ARGS(r) \
34 (r).left(), (r).top(), (r).right(), (r).bottom()
Chris Craik28ce94a2013-05-31 11:38:03 -070035
Romain Guybb9524b2010-06-22 18:56:38 -070036///////////////////////////////////////////////////////////////////////////////
37// Structs
38///////////////////////////////////////////////////////////////////////////////
39
Mathias Agopian83b186a2011-09-19 16:00:46 -070040class Rect {
Mathias Agopian83b186a2011-09-19 16:00:46 -070041public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070042 float left;
43 float top;
44 float right;
45 float bottom;
Romain Guybb9524b2010-06-22 18:56:38 -070046
Romain Guy5b3b3522010-10-27 18:57:51 -070047 // Used by Region
48 typedef float value_type;
49
Mathias Agopian83b186a2011-09-19 16:00:46 -070050 // we don't provide copy-ctor and operator= on purpose
51 // because we want the compiler generated versions
52
Romain Guy5b3b3522010-10-27 18:57:51 -070053 inline Rect():
Romain Guy7ae7ac42010-06-25 13:46:18 -070054 left(0),
55 top(0),
56 right(0),
57 bottom(0) {
58 }
Romain Guy9d5316e2010-06-24 19:30:36 -070059
Romain Guy5b3b3522010-10-27 18:57:51 -070060 inline Rect(float left, float top, float right, float bottom):
Romain Guy7ae7ac42010-06-25 13:46:18 -070061 left(left),
62 top(top),
63 right(right),
64 bottom(bottom) {
65 }
Romain Guybb9524b2010-06-22 18:56:38 -070066
Romain Guy5b3b3522010-10-27 18:57:51 -070067 inline Rect(float width, float height):
68 left(0.0f),
69 top(0.0f),
70 right(width),
71 bottom(height) {
72 }
73
ztenghuiaf6f7ed2014-03-18 17:25:49 -070074 inline Rect(const SkRect& rect):
75 left(rect.fLeft),
76 top(rect.fTop),
77 right(rect.fRight),
78 bottom(rect.fBottom) {
79 }
80
Romain Guy7ae7ac42010-06-25 13:46:18 -070081 friend int operator==(const Rect& a, const Rect& b) {
82 return !memcmp(&a, &b, sizeof(a));
83 }
Romain Guybb9524b2010-06-22 18:56:38 -070084
Romain Guy7ae7ac42010-06-25 13:46:18 -070085 friend int operator!=(const Rect& a, const Rect& b) {
86 return memcmp(&a, &b, sizeof(a));
87 }
Romain Guybb9524b2010-06-22 18:56:38 -070088
Romain Guy5b3b3522010-10-27 18:57:51 -070089 inline void clear() {
90 left = top = right = bottom = 0.0f;
91 }
92
93 inline bool isEmpty() const {
Mathias Agopian83b186a2011-09-19 16:00:46 -070094 // this is written in such way this it'll handle NANs to return
95 // true (empty)
96 return !((left < right) && (top < bottom));
Romain Guy7ae7ac42010-06-25 13:46:18 -070097 }
Romain Guybb9524b2010-06-22 18:56:38 -070098
Romain Guy5b3b3522010-10-27 18:57:51 -070099 inline void setEmpty() {
100 left = top = right = bottom = 0.0f;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700101 }
Romain Guybb9524b2010-06-22 18:56:38 -0700102
Romain Guy5b3b3522010-10-27 18:57:51 -0700103 inline void set(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700104 this->left = left;
105 this->right = right;
106 this->top = top;
107 this->bottom = bottom;
108 }
Romain Guybb9524b2010-06-22 18:56:38 -0700109
Romain Guy5b3b3522010-10-27 18:57:51 -0700110 inline void set(const Rect& r) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700111 set(r.left, r.top, r.right, r.bottom);
112 }
Romain Guybb9524b2010-06-22 18:56:38 -0700113
Rob Tsuk487a92c2015-01-06 13:22:54 -0800114 inline void set(const SkIRect& r) {
115 set(r.left(), r.top(), r.right(), r.bottom());
116 }
117
Romain Guy8aef54f2010-09-01 15:13:49 -0700118 inline float getWidth() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700119 return right - left;
120 }
Romain Guybb9524b2010-06-22 18:56:38 -0700121
Romain Guy8aef54f2010-09-01 15:13:49 -0700122 inline float getHeight() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700123 return bottom - top;
124 }
Romain Guybb9524b2010-06-22 18:56:38 -0700125
Mathias Agopian83b186a2011-09-19 16:00:46 -0700126 bool intersects(float l, float t, float r, float b) const {
Romain Guya1d3c912011-12-13 14:55:06 -0800127 return !intersectWith(l, t, r, b).isEmpty();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700128 }
Romain Guybb9524b2010-06-22 18:56:38 -0700129
Romain Guy7ae7ac42010-06-25 13:46:18 -0700130 bool intersects(const Rect& r) const {
131 return intersects(r.left, r.top, r.right, r.bottom);
132 }
Romain Guybb9524b2010-06-22 18:56:38 -0700133
Mathias Agopian83b186a2011-09-19 16:00:46 -0700134 bool intersect(float l, float t, float r, float b) {
Romain Guy8f85e802011-12-14 19:23:32 -0800135 Rect tmp(l, t, r, b);
136 intersectWith(tmp);
Mathias Agopian83b186a2011-09-19 16:00:46 -0700137 if (!tmp.isEmpty()) {
138 set(tmp);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700139 return true;
140 }
141 return false;
142 }
Romain Guybb9524b2010-06-22 18:56:38 -0700143
Romain Guy7ae7ac42010-06-25 13:46:18 -0700144 bool intersect(const Rect& r) {
145 return intersect(r.left, r.top, r.right, r.bottom);
146 }
Romain Guybb9524b2010-06-22 18:56:38 -0700147
Romain Guy2db5e992013-05-21 15:29:59 -0700148 inline bool contains(float l, float t, float r, float b) const {
Romain Guyec31f832011-12-13 18:39:19 -0800149 return l >= left && t >= top && r <= right && b <= bottom;
150 }
151
Romain Guy2db5e992013-05-21 15:29:59 -0700152 inline bool contains(const Rect& r) const {
Romain Guyec31f832011-12-13 18:39:19 -0800153 return contains(r.left, r.top, r.right, r.bottom);
154 }
155
Romain Guy079ba2c2010-07-16 14:12:24 -0700156 bool unionWith(const Rect& r) {
157 if (r.left < r.right && r.top < r.bottom) {
158 if (left < right && top < bottom) {
159 if (left > r.left) left = r.left;
160 if (top > r.top) top = r.top;
161 if (right < r.right) right = r.right;
162 if (bottom < r.bottom) bottom = r.bottom;
163 return true;
164 } else {
165 left = r.left;
166 top = r.top;
167 right = r.right;
168 bottom = r.bottom;
169 return true;
170 }
171 }
172 return false;
173 }
174
Romain Guy5b3b3522010-10-27 18:57:51 -0700175 void translate(float dx, float dy) {
176 left += dx;
177 right += dx;
178 top += dy;
179 bottom += dy;
180 }
181
Chris Craike4aa95e2014-05-08 13:57:05 -0700182 void inset(float delta) {
183 outset(-delta);
184 }
185
Chris Craikc3566d02013-02-04 16:16:33 -0800186 void outset(float delta) {
187 left -= delta;
188 top -= delta;
189 right += delta;
190 bottom += delta;
191 }
192
Chris Craik05f3d6e2014-06-02 16:27:04 -0700193 void outset(float xdelta, float ydelta) {
194 left -= xdelta;
195 top -= ydelta;
196 right += xdelta;
197 bottom += ydelta;
198 }
199
Chris Craik5e49b302013-07-30 19:05:20 -0700200 /**
Chris Craik32f05e32013-09-17 16:20:29 -0700201 * Similar to snapToPixelBoundaries, but estimates bounds conservatively to handle GL rounding
202 * errors.
Chris Craik5e49b302013-07-30 19:05:20 -0700203 *
Chris Craik32f05e32013-09-17 16:20:29 -0700204 * This function should be used whenever estimating the damage rect of geometry already mapped
205 * into layer space.
Chris Craik5e49b302013-07-30 19:05:20 -0700206 */
Chris Craik32f05e32013-09-17 16:20:29 -0700207 void snapGeometryToPixelBoundaries(bool snapOut) {
208 if (snapOut) {
209 /* For AA geometry with a ramp perimeter, don't snap by rounding - AA geometry will have
210 * a 0.5 pixel perimeter not accounted for in its bounds. Instead, snap by
211 * conservatively rounding out the bounds with floor/ceil.
212 *
213 * In order to avoid changing integer bounds with floor/ceil due to rounding errors
214 * inset the bounds first by the fudge factor. Very small fraction-of-a-pixel errors
215 * from this inset will only incur similarly small errors in output, due to transparency
216 * in extreme outside of the geometry.
217 */
Chris Craik564acf72014-01-02 16:46:18 -0800218 left = floorf(left + Vertex::GeometryFudgeFactor());
219 top = floorf(top + Vertex::GeometryFudgeFactor());
220 right = ceilf(right - Vertex::GeometryFudgeFactor());
221 bottom = ceilf(bottom - Vertex::GeometryFudgeFactor());
Chris Craik32f05e32013-09-17 16:20:29 -0700222 } else {
223 /* For other geometry, we do the regular rounding in order to snap, but also outset the
224 * bounds by a fudge factor. This ensures that ambiguous geometry (e.g. a non-AA Rect
225 * with top left at (0.5, 0.5)) will err on the side of a larger damage rect.
226 */
Chris Craik564acf72014-01-02 16:46:18 -0800227 left = floorf(left + 0.5f - Vertex::GeometryFudgeFactor());
228 top = floorf(top + 0.5f - Vertex::GeometryFudgeFactor());
229 right = floorf(right + 0.5f + Vertex::GeometryFudgeFactor());
230 bottom = floorf(bottom + 0.5f + Vertex::GeometryFudgeFactor());
Chris Craik32f05e32013-09-17 16:20:29 -0700231 }
Chris Craik5e49b302013-07-30 19:05:20 -0700232 }
233
Romain Guybf434112010-09-16 14:40:17 -0700234 void snapToPixelBoundaries() {
Romain Guyae88e5e2010-10-22 17:49:18 -0700235 left = floorf(left + 0.5f);
236 top = floorf(top + 0.5f);
237 right = floorf(right + 0.5f);
238 bottom = floorf(bottom + 0.5f);
Romain Guybf434112010-09-16 14:40:17 -0700239 }
240
Chris Craikf0a59072013-11-19 18:00:46 -0800241 void roundOut() {
242 left = floorf(left);
243 top = floorf(top);
244 right = ceilf(right);
245 bottom = ceilf(bottom);
246 }
247
Chris Craikc93e45c2014-07-16 10:15:56 -0700248 void expandToCoverVertex(float x, float y) {
249 left = fminf(left, x);
250 top = fminf(top, y);
251 right = fmaxf(right, x);
252 bottom = fmaxf(bottom, y);
253 }
254
Rob Tsuk487a92c2015-01-06 13:22:54 -0800255 SkRect toSkRect() const {
256 return SkRect::MakeLTRB(left, top, right, bottom);
257 }
258
259 SkIRect toSkIRect() const {
260 return SkIRect::MakeLTRB(left, top, right, bottom);
261 }
262
Chris Craike84a2082014-12-22 14:28:49 -0800263 void dump(const char* label = nullptr) const {
Chris Craike4aa95e2014-05-08 13:57:05 -0700264 ALOGD("%s[l=%f t=%f r=%f b=%f]", label ? label : "Rect", left, top, right, bottom);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700265 }
Romain Guybb9524b2010-06-22 18:56:38 -0700266
Romain Guya1d3c912011-12-13 14:55:06 -0800267private:
Romain Guy8f85e802011-12-14 19:23:32 -0800268 void intersectWith(Rect& tmp) const {
Chris Craik2af46352012-11-26 18:30:17 -0800269 tmp.left = fmaxf(left, tmp.left);
270 tmp.top = fmaxf(top, tmp.top);
271 tmp.right = fminf(right, tmp.right);
272 tmp.bottom = fminf(bottom, tmp.bottom);
Romain Guy8f85e802011-12-14 19:23:32 -0800273 }
274
Romain Guya1d3c912011-12-13 14:55:06 -0800275 Rect intersectWith(float l, float t, float r, float b) const {
276 Rect tmp;
Chris Craik2af46352012-11-26 18:30:17 -0800277 tmp.left = fmaxf(left, l);
278 tmp.top = fmaxf(top, t);
279 tmp.right = fminf(right, r);
280 tmp.bottom = fminf(bottom, b);
Romain Guya1d3c912011-12-13 14:55:06 -0800281 return tmp;
282 }
283
Mathias Agopian83b186a2011-09-19 16:00:46 -0700284}; // class Rect
Romain Guybb9524b2010-06-22 18:56:38 -0700285
Romain Guy9d5316e2010-06-24 19:30:36 -0700286}; // namespace uirenderer
Romain Guybb9524b2010-06-22 18:56:38 -0700287}; // namespace android
288
Romain Guy5b3b3522010-10-27 18:57:51 -0700289#endif // ANDROID_HWUI_RECT_H