blob: dabd8d440e17c62460761f47c485e1ea80a6a039 [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>
Romain Guybb9524b2010-06-22 18:56:38 -070021
Romain Guy5cbbce52010-06-27 22:59:20 -070022#include <utils/Log.h>
23
Chris Craik32f05e32013-09-17 16:20:29 -070024#include "Vertex.h"
25
Romain Guybb9524b2010-06-22 18:56:38 -070026namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070027namespace uirenderer {
Romain Guybb9524b2010-06-22 18:56:38 -070028
Chris Craik39a908c2013-06-13 14:39:01 -070029#define RECT_STRING "%7.2f %7.2f %7.2f %7.2f"
Chris Craik28ce94a2013-05-31 11:38:03 -070030#define RECT_ARGS(r) \
31 (r).left, (r).top, (r).right, (r).bottom
32
Romain Guybb9524b2010-06-22 18:56:38 -070033///////////////////////////////////////////////////////////////////////////////
34// Structs
35///////////////////////////////////////////////////////////////////////////////
36
Mathias Agopian83b186a2011-09-19 16:00:46 -070037class Rect {
Mathias Agopian83b186a2011-09-19 16:00:46 -070038public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070039 float left;
40 float top;
41 float right;
42 float bottom;
Romain Guybb9524b2010-06-22 18:56:38 -070043
Romain Guy5b3b3522010-10-27 18:57:51 -070044 // Used by Region
45 typedef float value_type;
46
Mathias Agopian83b186a2011-09-19 16:00:46 -070047 // we don't provide copy-ctor and operator= on purpose
48 // because we want the compiler generated versions
49
Romain Guy5b3b3522010-10-27 18:57:51 -070050 inline Rect():
Romain Guy7ae7ac42010-06-25 13:46:18 -070051 left(0),
52 top(0),
53 right(0),
54 bottom(0) {
55 }
Romain Guy9d5316e2010-06-24 19:30:36 -070056
Romain Guy5b3b3522010-10-27 18:57:51 -070057 inline Rect(float left, float top, float right, float bottom):
Romain Guy7ae7ac42010-06-25 13:46:18 -070058 left(left),
59 top(top),
60 right(right),
61 bottom(bottom) {
62 }
Romain Guybb9524b2010-06-22 18:56:38 -070063
Romain Guy5b3b3522010-10-27 18:57:51 -070064 inline Rect(float width, float height):
65 left(0.0f),
66 top(0.0f),
67 right(width),
68 bottom(height) {
69 }
70
Romain Guy7ae7ac42010-06-25 13:46:18 -070071 friend int operator==(const Rect& a, const Rect& b) {
72 return !memcmp(&a, &b, sizeof(a));
73 }
Romain Guybb9524b2010-06-22 18:56:38 -070074
Romain Guy7ae7ac42010-06-25 13:46:18 -070075 friend int operator!=(const Rect& a, const Rect& b) {
76 return memcmp(&a, &b, sizeof(a));
77 }
Romain Guybb9524b2010-06-22 18:56:38 -070078
Romain Guy5b3b3522010-10-27 18:57:51 -070079 inline void clear() {
80 left = top = right = bottom = 0.0f;
81 }
82
83 inline bool isEmpty() const {
Mathias Agopian83b186a2011-09-19 16:00:46 -070084 // this is written in such way this it'll handle NANs to return
85 // true (empty)
86 return !((left < right) && (top < bottom));
Romain Guy7ae7ac42010-06-25 13:46:18 -070087 }
Romain Guybb9524b2010-06-22 18:56:38 -070088
Romain Guy5b3b3522010-10-27 18:57:51 -070089 inline void setEmpty() {
90 left = top = right = bottom = 0.0f;
Romain Guy7ae7ac42010-06-25 13:46:18 -070091 }
Romain Guybb9524b2010-06-22 18:56:38 -070092
Romain Guy5b3b3522010-10-27 18:57:51 -070093 inline void set(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070094 this->left = left;
95 this->right = right;
96 this->top = top;
97 this->bottom = bottom;
98 }
Romain Guybb9524b2010-06-22 18:56:38 -070099
Romain Guy5b3b3522010-10-27 18:57:51 -0700100 inline void set(const Rect& r) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700101 set(r.left, r.top, r.right, r.bottom);
102 }
Romain Guybb9524b2010-06-22 18:56:38 -0700103
Romain Guy8aef54f2010-09-01 15:13:49 -0700104 inline float getWidth() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700105 return right - left;
106 }
Romain Guybb9524b2010-06-22 18:56:38 -0700107
Romain Guy8aef54f2010-09-01 15:13:49 -0700108 inline float getHeight() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700109 return bottom - top;
110 }
Romain Guybb9524b2010-06-22 18:56:38 -0700111
Mathias Agopian83b186a2011-09-19 16:00:46 -0700112 bool intersects(float l, float t, float r, float b) const {
Romain Guya1d3c912011-12-13 14:55:06 -0800113 return !intersectWith(l, t, r, b).isEmpty();
Romain Guy7ae7ac42010-06-25 13:46:18 -0700114 }
Romain Guybb9524b2010-06-22 18:56:38 -0700115
Romain Guy7ae7ac42010-06-25 13:46:18 -0700116 bool intersects(const Rect& r) const {
117 return intersects(r.left, r.top, r.right, r.bottom);
118 }
Romain Guybb9524b2010-06-22 18:56:38 -0700119
Mathias Agopian83b186a2011-09-19 16:00:46 -0700120 bool intersect(float l, float t, float r, float b) {
Romain Guy8f85e802011-12-14 19:23:32 -0800121 Rect tmp(l, t, r, b);
122 intersectWith(tmp);
Mathias Agopian83b186a2011-09-19 16:00:46 -0700123 if (!tmp.isEmpty()) {
124 set(tmp);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700125 return true;
126 }
127 return false;
128 }
Romain Guybb9524b2010-06-22 18:56:38 -0700129
Romain Guy7ae7ac42010-06-25 13:46:18 -0700130 bool intersect(const Rect& r) {
131 return intersect(r.left, r.top, r.right, r.bottom);
132 }
Romain Guybb9524b2010-06-22 18:56:38 -0700133
Romain Guy2db5e992013-05-21 15:29:59 -0700134 inline bool contains(float l, float t, float r, float b) const {
Romain Guyec31f832011-12-13 18:39:19 -0800135 return l >= left && t >= top && r <= right && b <= bottom;
136 }
137
Romain Guy2db5e992013-05-21 15:29:59 -0700138 inline bool contains(const Rect& r) const {
Romain Guyec31f832011-12-13 18:39:19 -0800139 return contains(r.left, r.top, r.right, r.bottom);
140 }
141
Romain Guy079ba2c2010-07-16 14:12:24 -0700142 bool unionWith(const Rect& r) {
143 if (r.left < r.right && r.top < r.bottom) {
144 if (left < right && top < bottom) {
145 if (left > r.left) left = r.left;
146 if (top > r.top) top = r.top;
147 if (right < r.right) right = r.right;
148 if (bottom < r.bottom) bottom = r.bottom;
149 return true;
150 } else {
151 left = r.left;
152 top = r.top;
153 right = r.right;
154 bottom = r.bottom;
155 return true;
156 }
157 }
158 return false;
159 }
160
Romain Guy5b3b3522010-10-27 18:57:51 -0700161 void translate(float dx, float dy) {
162 left += dx;
163 right += dx;
164 top += dy;
165 bottom += dy;
166 }
167
Chris Craikc3566d02013-02-04 16:16:33 -0800168 void outset(float delta) {
169 left -= delta;
170 top -= delta;
171 right += delta;
172 bottom += delta;
173 }
174
Chris Craik5e49b302013-07-30 19:05:20 -0700175 /**
Chris Craik32f05e32013-09-17 16:20:29 -0700176 * Similar to snapToPixelBoundaries, but estimates bounds conservatively to handle GL rounding
177 * errors.
Chris Craik5e49b302013-07-30 19:05:20 -0700178 *
Chris Craik32f05e32013-09-17 16:20:29 -0700179 * This function should be used whenever estimating the damage rect of geometry already mapped
180 * into layer space.
Chris Craik5e49b302013-07-30 19:05:20 -0700181 */
Chris Craik32f05e32013-09-17 16:20:29 -0700182 void snapGeometryToPixelBoundaries(bool snapOut) {
183 if (snapOut) {
184 /* For AA geometry with a ramp perimeter, don't snap by rounding - AA geometry will have
185 * a 0.5 pixel perimeter not accounted for in its bounds. Instead, snap by
186 * conservatively rounding out the bounds with floor/ceil.
187 *
188 * In order to avoid changing integer bounds with floor/ceil due to rounding errors
189 * inset the bounds first by the fudge factor. Very small fraction-of-a-pixel errors
190 * from this inset will only incur similarly small errors in output, due to transparency
191 * in extreme outside of the geometry.
192 */
193 left = floorf(left + Vertex::gGeometryFudgeFactor);
194 top = floorf(top + Vertex::gGeometryFudgeFactor);
195 right = ceilf(right - Vertex::gGeometryFudgeFactor);
196 bottom = ceilf(bottom - Vertex::gGeometryFudgeFactor);
197 } else {
198 /* For other geometry, we do the regular rounding in order to snap, but also outset the
199 * bounds by a fudge factor. This ensures that ambiguous geometry (e.g. a non-AA Rect
200 * with top left at (0.5, 0.5)) will err on the side of a larger damage rect.
201 */
202 left = floorf(left + 0.5f - Vertex::gGeometryFudgeFactor);
203 top = floorf(top + 0.5f - Vertex::gGeometryFudgeFactor);
204 right = floorf(right + 0.5f + Vertex::gGeometryFudgeFactor);
205 bottom = floorf(bottom + 0.5f + Vertex::gGeometryFudgeFactor);
206 }
Chris Craik5e49b302013-07-30 19:05:20 -0700207 }
208
Romain Guybf434112010-09-16 14:40:17 -0700209 void snapToPixelBoundaries() {
Romain Guyae88e5e2010-10-22 17:49:18 -0700210 left = floorf(left + 0.5f);
211 top = floorf(top + 0.5f);
212 right = floorf(right + 0.5f);
213 bottom = floorf(bottom + 0.5f);
Romain Guybf434112010-09-16 14:40:17 -0700214 }
215
Romain Guy7ae7ac42010-06-25 13:46:18 -0700216 void dump() const {
Steve Block5baa3a62011-12-20 16:23:08 +0000217 ALOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700218 }
Romain Guybb9524b2010-06-22 18:56:38 -0700219
Romain Guya1d3c912011-12-13 14:55:06 -0800220private:
Romain Guy8f85e802011-12-14 19:23:32 -0800221 void intersectWith(Rect& tmp) const {
Chris Craik2af46352012-11-26 18:30:17 -0800222 tmp.left = fmaxf(left, tmp.left);
223 tmp.top = fmaxf(top, tmp.top);
224 tmp.right = fminf(right, tmp.right);
225 tmp.bottom = fminf(bottom, tmp.bottom);
Romain Guy8f85e802011-12-14 19:23:32 -0800226 }
227
Romain Guya1d3c912011-12-13 14:55:06 -0800228 Rect intersectWith(float l, float t, float r, float b) const {
229 Rect tmp;
Chris Craik2af46352012-11-26 18:30:17 -0800230 tmp.left = fmaxf(left, l);
231 tmp.top = fmaxf(top, t);
232 tmp.right = fminf(right, r);
233 tmp.bottom = fminf(bottom, b);
Romain Guya1d3c912011-12-13 14:55:06 -0800234 return tmp;
235 }
236
Mathias Agopian83b186a2011-09-19 16:00:46 -0700237}; // class Rect
Romain Guybb9524b2010-06-22 18:56:38 -0700238
Romain Guy9d5316e2010-06-24 19:30:36 -0700239}; // namespace uirenderer
Romain Guybb9524b2010-06-22 18:56:38 -0700240}; // namespace android
241
Romain Guy5b3b3522010-10-27 18:57:51 -0700242#endif // ANDROID_HWUI_RECT_H