Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_RECT_H |
| 18 | #define ANDROID_HWUI_RECT_H |
| 19 | |
| 20 | #include <cmath> |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 21 | #include <SkRect.h> |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 22 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 25 | #include "Vertex.h" |
| 26 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 27 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 28 | namespace uirenderer { |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 29 | |
Chris Craik | 39a908c | 2013-06-13 14:39:01 -0700 | [diff] [blame] | 30 | #define RECT_STRING "%7.2f %7.2f %7.2f %7.2f" |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 31 | #define RECT_ARGS(r) \ |
| 32 | (r).left, (r).top, (r).right, (r).bottom |
| 33 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | // Structs |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 38 | class Rect { |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 39 | public: |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 40 | float left; |
| 41 | float top; |
| 42 | float right; |
| 43 | float bottom; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 44 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 45 | // Used by Region |
| 46 | typedef float value_type; |
| 47 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 48 | // we don't provide copy-ctor and operator= on purpose |
| 49 | // because we want the compiler generated versions |
| 50 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 51 | inline Rect(): |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 52 | left(0), |
| 53 | top(0), |
| 54 | right(0), |
| 55 | bottom(0) { |
| 56 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 57 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 58 | inline Rect(float left, float top, float right, float bottom): |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 59 | left(left), |
| 60 | top(top), |
| 61 | right(right), |
| 62 | bottom(bottom) { |
| 63 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 64 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 65 | inline Rect(float width, float height): |
| 66 | left(0.0f), |
| 67 | top(0.0f), |
| 68 | right(width), |
| 69 | bottom(height) { |
| 70 | } |
| 71 | |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 72 | inline Rect(const SkRect& rect): |
| 73 | left(rect.fLeft), |
| 74 | top(rect.fTop), |
| 75 | right(rect.fRight), |
| 76 | bottom(rect.fBottom) { |
| 77 | } |
| 78 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 79 | friend int operator==(const Rect& a, const Rect& b) { |
| 80 | return !memcmp(&a, &b, sizeof(a)); |
| 81 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 82 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 83 | friend int operator!=(const Rect& a, const Rect& b) { |
| 84 | return memcmp(&a, &b, sizeof(a)); |
| 85 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 86 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 87 | inline void clear() { |
| 88 | left = top = right = bottom = 0.0f; |
| 89 | } |
| 90 | |
| 91 | inline bool isEmpty() const { |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 92 | // this is written in such way this it'll handle NANs to return |
| 93 | // true (empty) |
| 94 | return !((left < right) && (top < bottom)); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 95 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 96 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 97 | inline void setEmpty() { |
| 98 | left = top = right = bottom = 0.0f; |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 99 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 100 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 101 | inline void set(float left, float top, float right, float bottom) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 102 | this->left = left; |
| 103 | this->right = right; |
| 104 | this->top = top; |
| 105 | this->bottom = bottom; |
| 106 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 107 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 108 | inline void set(const Rect& r) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 109 | set(r.left, r.top, r.right, r.bottom); |
| 110 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 111 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 112 | inline float getWidth() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 113 | return right - left; |
| 114 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 115 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 116 | inline float getHeight() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 117 | return bottom - top; |
| 118 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 119 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 120 | bool intersects(float l, float t, float r, float b) const { |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 121 | return !intersectWith(l, t, r, b).isEmpty(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 122 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 123 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 124 | bool intersects(const Rect& r) const { |
| 125 | return intersects(r.left, r.top, r.right, r.bottom); |
| 126 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 127 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 128 | bool intersect(float l, float t, float r, float b) { |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 129 | Rect tmp(l, t, r, b); |
| 130 | intersectWith(tmp); |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 131 | if (!tmp.isEmpty()) { |
| 132 | set(tmp); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | return false; |
| 136 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 137 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 138 | bool intersect(const Rect& r) { |
| 139 | return intersect(r.left, r.top, r.right, r.bottom); |
| 140 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 141 | |
Romain Guy | 2db5e99 | 2013-05-21 15:29:59 -0700 | [diff] [blame] | 142 | inline bool contains(float l, float t, float r, float b) const { |
Romain Guy | ec31f83 | 2011-12-13 18:39:19 -0800 | [diff] [blame] | 143 | return l >= left && t >= top && r <= right && b <= bottom; |
| 144 | } |
| 145 | |
Romain Guy | 2db5e99 | 2013-05-21 15:29:59 -0700 | [diff] [blame] | 146 | inline bool contains(const Rect& r) const { |
Romain Guy | ec31f83 | 2011-12-13 18:39:19 -0800 | [diff] [blame] | 147 | return contains(r.left, r.top, r.right, r.bottom); |
| 148 | } |
| 149 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 150 | 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 Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 169 | void translate(float dx, float dy) { |
| 170 | left += dx; |
| 171 | right += dx; |
| 172 | top += dy; |
| 173 | bottom += dy; |
| 174 | } |
| 175 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 176 | void outset(float delta) { |
| 177 | left -= delta; |
| 178 | top -= delta; |
| 179 | right += delta; |
| 180 | bottom += delta; |
| 181 | } |
| 182 | |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 183 | /** |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 184 | * Similar to snapToPixelBoundaries, but estimates bounds conservatively to handle GL rounding |
| 185 | * errors. |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 186 | * |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 187 | * This function should be used whenever estimating the damage rect of geometry already mapped |
| 188 | * into layer space. |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 189 | */ |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 190 | 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 Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 201 | left = floorf(left + Vertex::GeometryFudgeFactor()); |
| 202 | top = floorf(top + Vertex::GeometryFudgeFactor()); |
| 203 | right = ceilf(right - Vertex::GeometryFudgeFactor()); |
| 204 | bottom = ceilf(bottom - Vertex::GeometryFudgeFactor()); |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 205 | } 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 Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 210 | 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 Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 214 | } |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Romain Guy | bf43411 | 2010-09-16 14:40:17 -0700 | [diff] [blame] | 217 | void snapToPixelBoundaries() { |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 218 | left = floorf(left + 0.5f); |
| 219 | top = floorf(top + 0.5f); |
| 220 | right = floorf(right + 0.5f); |
| 221 | bottom = floorf(bottom + 0.5f); |
Romain Guy | bf43411 | 2010-09-16 14:40:17 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 224 | void roundOut() { |
| 225 | left = floorf(left); |
| 226 | top = floorf(top); |
| 227 | right = ceilf(right); |
| 228 | bottom = ceilf(bottom); |
| 229 | } |
| 230 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 231 | void dump() const { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 232 | ALOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 233 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 234 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 235 | private: |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 236 | void intersectWith(Rect& tmp) const { |
Chris Craik | 2af4635 | 2012-11-26 18:30:17 -0800 | [diff] [blame] | 237 | 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 Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 243 | Rect intersectWith(float l, float t, float r, float b) const { |
| 244 | Rect tmp; |
Chris Craik | 2af4635 | 2012-11-26 18:30:17 -0800 | [diff] [blame] | 245 | tmp.left = fmaxf(left, l); |
| 246 | tmp.top = fmaxf(top, t); |
| 247 | tmp.right = fminf(right, r); |
| 248 | tmp.bottom = fminf(bottom, b); |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 249 | return tmp; |
| 250 | } |
| 251 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 252 | }; // class Rect |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 253 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 254 | }; // namespace uirenderer |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 255 | }; // namespace android |
| 256 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 257 | #endif // ANDROID_HWUI_RECT_H |