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