blob: 0083b77777c260858f362fafe33e6a2bfcbf0146 [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 Craik39a908c2013-06-13 14:39:01 -070030#define RECT_STRING "%7.2f %7.2f %7.2f %7.2f"
Chris Craik28ce94a2013-05-31 11:38:03 -070031#define RECT_ARGS(r) \
32 (r).left, (r).top, (r).right, (r).bottom
33
Romain Guybb9524b2010-06-22 18:56:38 -070034///////////////////////////////////////////////////////////////////////////////
35// Structs
36///////////////////////////////////////////////////////////////////////////////
37
Mathias Agopian83b186a2011-09-19 16:00:46 -070038class Rect {
Mathias Agopian83b186a2011-09-19 16:00:46 -070039public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070040 float left;
41 float top;
42 float right;
43 float bottom;
Romain Guybb9524b2010-06-22 18:56:38 -070044
Romain Guy5b3b3522010-10-27 18:57:51 -070045 // Used by Region
46 typedef float value_type;
47
Mathias Agopian83b186a2011-09-19 16:00:46 -070048 // we don't provide copy-ctor and operator= on purpose
49 // because we want the compiler generated versions
50
Romain Guy5b3b3522010-10-27 18:57:51 -070051 inline Rect():
Romain Guy7ae7ac42010-06-25 13:46:18 -070052 left(0),
53 top(0),
54 right(0),
55 bottom(0) {
56 }
Romain Guy9d5316e2010-06-24 19:30:36 -070057
Romain Guy5b3b3522010-10-27 18:57:51 -070058 inline Rect(float left, float top, float right, float bottom):
Romain Guy7ae7ac42010-06-25 13:46:18 -070059 left(left),
60 top(top),
61 right(right),
62 bottom(bottom) {
63 }
Romain Guybb9524b2010-06-22 18:56:38 -070064
Romain Guy5b3b3522010-10-27 18:57:51 -070065 inline Rect(float width, float height):
66 left(0.0f),
67 top(0.0f),
68 right(width),
69 bottom(height) {
70 }
71
ztenghuiaf6f7ed2014-03-18 17:25:49 -070072 inline Rect(const SkRect& rect):
73 left(rect.fLeft),
74 top(rect.fTop),
75 right(rect.fRight),
76 bottom(rect.fBottom) {
77 }
78
Romain Guy7ae7ac42010-06-25 13:46:18 -070079 friend int operator==(const Rect& a, const Rect& b) {
80 return !memcmp(&a, &b, sizeof(a));
81 }
Romain Guybb9524b2010-06-22 18:56:38 -070082
Romain Guy7ae7ac42010-06-25 13:46:18 -070083 friend int operator!=(const Rect& a, const Rect& b) {
84 return memcmp(&a, &b, sizeof(a));
85 }
Romain Guybb9524b2010-06-22 18:56:38 -070086
Romain Guy5b3b3522010-10-27 18:57:51 -070087 inline void clear() {
88 left = top = right = bottom = 0.0f;
89 }
90
91 inline bool isEmpty() const {
Mathias Agopian83b186a2011-09-19 16:00:46 -070092 // this is written in such way this it'll handle NANs to return
93 // true (empty)
94 return !((left < right) && (top < bottom));
Romain Guy7ae7ac42010-06-25 13:46:18 -070095 }
Romain Guybb9524b2010-06-22 18:56:38 -070096
Romain Guy5b3b3522010-10-27 18:57:51 -070097 inline void setEmpty() {
98 left = top = right = bottom = 0.0f;
Romain Guy7ae7ac42010-06-25 13:46:18 -070099 }
Romain Guybb9524b2010-06-22 18:56:38 -0700100
Romain Guy5b3b3522010-10-27 18:57:51 -0700101 inline void set(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700102 this->left = left;
103 this->right = right;
104 this->top = top;
105 this->bottom = bottom;
106 }
Romain Guybb9524b2010-06-22 18:56:38 -0700107
Romain Guy5b3b3522010-10-27 18:57:51 -0700108 inline void set(const Rect& r) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700109 set(r.left, r.top, r.right, r.bottom);
110 }
Romain Guybb9524b2010-06-22 18:56:38 -0700111
Romain Guy8aef54f2010-09-01 15:13:49 -0700112 inline float getWidth() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700113 return right - left;
114 }
Romain Guybb9524b2010-06-22 18:56:38 -0700115
Romain Guy8aef54f2010-09-01 15:13:49 -0700116 inline float getHeight() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700117 return bottom - top;
118 }
Romain Guybb9524b2010-06-22 18:56:38 -0700119
Mathias Agopian83b186a2011-09-19 16:00:46 -0700120 bool intersects(float l, float t, float r, float b) const {
Romain Guya1d3c912011-12-13 14:55:06 -0800121 return !intersectWith(l, t, r, b).isEmpty();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700122 }
Romain Guybb9524b2010-06-22 18:56:38 -0700123
Romain Guy7ae7ac42010-06-25 13:46:18 -0700124 bool intersects(const Rect& r) const {
125 return intersects(r.left, r.top, r.right, r.bottom);
126 }
Romain Guybb9524b2010-06-22 18:56:38 -0700127
Mathias Agopian83b186a2011-09-19 16:00:46 -0700128 bool intersect(float l, float t, float r, float b) {
Romain Guy8f85e802011-12-14 19:23:32 -0800129 Rect tmp(l, t, r, b);
130 intersectWith(tmp);
Mathias Agopian83b186a2011-09-19 16:00:46 -0700131 if (!tmp.isEmpty()) {
132 set(tmp);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700133 return true;
134 }
135 return false;
136 }
Romain Guybb9524b2010-06-22 18:56:38 -0700137
Romain Guy7ae7ac42010-06-25 13:46:18 -0700138 bool intersect(const Rect& r) {
139 return intersect(r.left, r.top, r.right, r.bottom);
140 }
Romain Guybb9524b2010-06-22 18:56:38 -0700141
Romain Guy2db5e992013-05-21 15:29:59 -0700142 inline bool contains(float l, float t, float r, float b) const {
Romain Guyec31f832011-12-13 18:39:19 -0800143 return l >= left && t >= top && r <= right && b <= bottom;
144 }
145
Romain Guy2db5e992013-05-21 15:29:59 -0700146 inline bool contains(const Rect& r) const {
Romain Guyec31f832011-12-13 18:39:19 -0800147 return contains(r.left, r.top, r.right, r.bottom);
148 }
149
Romain Guy079ba2c2010-07-16 14:12:24 -0700150 bool unionWith(const Rect& r) {
151 if (r.left < r.right && r.top < r.bottom) {
152 if (left < right && top < bottom) {
153 if (left > r.left) left = r.left;
154 if (top > r.top) top = r.top;
155 if (right < r.right) right = r.right;
156 if (bottom < r.bottom) bottom = r.bottom;
157 return true;
158 } else {
159 left = r.left;
160 top = r.top;
161 right = r.right;
162 bottom = r.bottom;
163 return true;
164 }
165 }
166 return false;
167 }
168
Romain Guy5b3b3522010-10-27 18:57:51 -0700169 void translate(float dx, float dy) {
170 left += dx;
171 right += dx;
172 top += dy;
173 bottom += dy;
174 }
175
Chris Craikc3566d02013-02-04 16:16:33 -0800176 void outset(float delta) {
177 left -= delta;
178 top -= delta;
179 right += delta;
180 bottom += delta;
181 }
182
Chris Craik5e49b302013-07-30 19:05:20 -0700183 /**
Chris Craik32f05e32013-09-17 16:20:29 -0700184 * Similar to snapToPixelBoundaries, but estimates bounds conservatively to handle GL rounding
185 * errors.
Chris Craik5e49b302013-07-30 19:05:20 -0700186 *
Chris Craik32f05e32013-09-17 16:20:29 -0700187 * This function should be used whenever estimating the damage rect of geometry already mapped
188 * into layer space.
Chris Craik5e49b302013-07-30 19:05:20 -0700189 */
Chris Craik32f05e32013-09-17 16:20:29 -0700190 void snapGeometryToPixelBoundaries(bool snapOut) {
191 if (snapOut) {
192 /* For AA geometry with a ramp perimeter, don't snap by rounding - AA geometry will have
193 * a 0.5 pixel perimeter not accounted for in its bounds. Instead, snap by
194 * conservatively rounding out the bounds with floor/ceil.
195 *
196 * In order to avoid changing integer bounds with floor/ceil due to rounding errors
197 * inset the bounds first by the fudge factor. Very small fraction-of-a-pixel errors
198 * from this inset will only incur similarly small errors in output, due to transparency
199 * in extreme outside of the geometry.
200 */
Chris Craik564acf72014-01-02 16:46:18 -0800201 left = floorf(left + Vertex::GeometryFudgeFactor());
202 top = floorf(top + Vertex::GeometryFudgeFactor());
203 right = ceilf(right - Vertex::GeometryFudgeFactor());
204 bottom = ceilf(bottom - Vertex::GeometryFudgeFactor());
Chris Craik32f05e32013-09-17 16:20:29 -0700205 } else {
206 /* For other geometry, we do the regular rounding in order to snap, but also outset the
207 * bounds by a fudge factor. This ensures that ambiguous geometry (e.g. a non-AA Rect
208 * with top left at (0.5, 0.5)) will err on the side of a larger damage rect.
209 */
Chris Craik564acf72014-01-02 16:46:18 -0800210 left = floorf(left + 0.5f - Vertex::GeometryFudgeFactor());
211 top = floorf(top + 0.5f - Vertex::GeometryFudgeFactor());
212 right = floorf(right + 0.5f + Vertex::GeometryFudgeFactor());
213 bottom = floorf(bottom + 0.5f + Vertex::GeometryFudgeFactor());
Chris Craik32f05e32013-09-17 16:20:29 -0700214 }
Chris Craik5e49b302013-07-30 19:05:20 -0700215 }
216
Romain Guybf434112010-09-16 14:40:17 -0700217 void snapToPixelBoundaries() {
Romain Guyae88e5e2010-10-22 17:49:18 -0700218 left = floorf(left + 0.5f);
219 top = floorf(top + 0.5f);
220 right = floorf(right + 0.5f);
221 bottom = floorf(bottom + 0.5f);
Romain Guybf434112010-09-16 14:40:17 -0700222 }
223
Chris Craikf0a59072013-11-19 18:00:46 -0800224 void roundOut() {
225 left = floorf(left);
226 top = floorf(top);
227 right = ceilf(right);
228 bottom = ceilf(bottom);
229 }
230
Romain Guy7ae7ac42010-06-25 13:46:18 -0700231 void dump() const {
Steve Block5baa3a62011-12-20 16:23:08 +0000232 ALOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700233 }
Romain Guybb9524b2010-06-22 18:56:38 -0700234
Romain Guya1d3c912011-12-13 14:55:06 -0800235private:
Romain Guy8f85e802011-12-14 19:23:32 -0800236 void intersectWith(Rect& tmp) const {
Chris Craik2af46352012-11-26 18:30:17 -0800237 tmp.left = fmaxf(left, tmp.left);
238 tmp.top = fmaxf(top, tmp.top);
239 tmp.right = fminf(right, tmp.right);
240 tmp.bottom = fminf(bottom, tmp.bottom);
Romain Guy8f85e802011-12-14 19:23:32 -0800241 }
242
Romain Guya1d3c912011-12-13 14:55:06 -0800243 Rect intersectWith(float l, float t, float r, float b) const {
244 Rect tmp;
Chris Craik2af46352012-11-26 18:30:17 -0800245 tmp.left = fmaxf(left, l);
246 tmp.top = fmaxf(top, t);
247 tmp.right = fminf(right, r);
248 tmp.bottom = fminf(bottom, b);
Romain Guya1d3c912011-12-13 14:55:06 -0800249 return tmp;
250 }
251
Mathias Agopian83b186a2011-09-19 16:00:46 -0700252}; // class Rect
Romain Guybb9524b2010-06-22 18:56:38 -0700253
Romain Guy9d5316e2010-06-24 19:30:36 -0700254}; // namespace uirenderer
Romain Guybb9524b2010-06-22 18:56:38 -0700255}; // namespace android
256
Romain Guy5b3b3522010-10-27 18:57:51 -0700257#endif // ANDROID_HWUI_RECT_H