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 |
Chris Craik | 3f085429 | 2014-04-15 16:18:08 -0700 | [diff] [blame^] | 33 | #define SK_RECT_ARGS(r) \ |
| 34 | (r).left(), (r).top(), (r).right(), (r).bottom() |
Chris Craik | 28ce94a | 2013-05-31 11:38:03 -0700 | [diff] [blame] | 35 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | // Structs |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 40 | class Rect { |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 41 | public: |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 42 | float left; |
| 43 | float top; |
| 44 | float right; |
| 45 | float bottom; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 46 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 47 | // Used by Region |
| 48 | typedef float value_type; |
| 49 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 50 | // we don't provide copy-ctor and operator= on purpose |
| 51 | // because we want the compiler generated versions |
| 52 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 53 | inline Rect(): |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 54 | left(0), |
| 55 | top(0), |
| 56 | right(0), |
| 57 | bottom(0) { |
| 58 | } |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 59 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 60 | inline Rect(float left, float top, float right, float bottom): |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 61 | left(left), |
| 62 | top(top), |
| 63 | right(right), |
| 64 | bottom(bottom) { |
| 65 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 66 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 67 | inline Rect(float width, float height): |
| 68 | left(0.0f), |
| 69 | top(0.0f), |
| 70 | right(width), |
| 71 | bottom(height) { |
| 72 | } |
| 73 | |
ztenghui | af6f7ed | 2014-03-18 17:25:49 -0700 | [diff] [blame] | 74 | inline Rect(const SkRect& rect): |
| 75 | left(rect.fLeft), |
| 76 | top(rect.fTop), |
| 77 | right(rect.fRight), |
| 78 | bottom(rect.fBottom) { |
| 79 | } |
| 80 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 81 | friend int operator==(const Rect& a, const Rect& b) { |
| 82 | return !memcmp(&a, &b, sizeof(a)); |
| 83 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 84 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 85 | friend int operator!=(const Rect& a, const Rect& b) { |
| 86 | return memcmp(&a, &b, sizeof(a)); |
| 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 clear() { |
| 90 | left = top = right = bottom = 0.0f; |
| 91 | } |
| 92 | |
| 93 | inline bool isEmpty() const { |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 94 | // this is written in such way this it'll handle NANs to return |
| 95 | // true (empty) |
| 96 | return !((left < right) && (top < bottom)); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 97 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 98 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 99 | inline void setEmpty() { |
| 100 | left = top = right = bottom = 0.0f; |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 101 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 102 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 103 | inline void set(float left, float top, float right, float bottom) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 104 | this->left = left; |
| 105 | this->right = right; |
| 106 | this->top = top; |
| 107 | this->bottom = bottom; |
| 108 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 109 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 110 | inline void set(const Rect& r) { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 111 | set(r.left, r.top, r.right, r.bottom); |
| 112 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 113 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 114 | inline float getWidth() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 115 | return right - left; |
| 116 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 117 | |
Romain Guy | 8aef54f | 2010-09-01 15:13:49 -0700 | [diff] [blame] | 118 | inline float getHeight() const { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 119 | return bottom - top; |
| 120 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 121 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 122 | bool intersects(float l, float t, float r, float b) const { |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 123 | return !intersectWith(l, t, r, b).isEmpty(); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 124 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 125 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 126 | bool intersects(const Rect& r) const { |
| 127 | return intersects(r.left, r.top, r.right, r.bottom); |
| 128 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 129 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 130 | bool intersect(float l, float t, float r, float b) { |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 131 | Rect tmp(l, t, r, b); |
| 132 | intersectWith(tmp); |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 133 | if (!tmp.isEmpty()) { |
| 134 | set(tmp); |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | return false; |
| 138 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 139 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 140 | bool intersect(const Rect& r) { |
| 141 | return intersect(r.left, r.top, r.right, r.bottom); |
| 142 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 143 | |
Romain Guy | 2db5e99 | 2013-05-21 15:29:59 -0700 | [diff] [blame] | 144 | inline bool contains(float l, float t, float r, float b) const { |
Romain Guy | ec31f83 | 2011-12-13 18:39:19 -0800 | [diff] [blame] | 145 | return l >= left && t >= top && r <= right && b <= bottom; |
| 146 | } |
| 147 | |
Romain Guy | 2db5e99 | 2013-05-21 15:29:59 -0700 | [diff] [blame] | 148 | inline bool contains(const Rect& r) const { |
Romain Guy | ec31f83 | 2011-12-13 18:39:19 -0800 | [diff] [blame] | 149 | return contains(r.left, r.top, r.right, r.bottom); |
| 150 | } |
| 151 | |
Romain Guy | 079ba2c | 2010-07-16 14:12:24 -0700 | [diff] [blame] | 152 | bool unionWith(const Rect& r) { |
| 153 | if (r.left < r.right && r.top < r.bottom) { |
| 154 | if (left < right && top < bottom) { |
| 155 | if (left > r.left) left = r.left; |
| 156 | if (top > r.top) top = r.top; |
| 157 | if (right < r.right) right = r.right; |
| 158 | if (bottom < r.bottom) bottom = r.bottom; |
| 159 | return true; |
| 160 | } else { |
| 161 | left = r.left; |
| 162 | top = r.top; |
| 163 | right = r.right; |
| 164 | bottom = r.bottom; |
| 165 | return true; |
| 166 | } |
| 167 | } |
| 168 | return false; |
| 169 | } |
| 170 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 171 | void translate(float dx, float dy) { |
| 172 | left += dx; |
| 173 | right += dx; |
| 174 | top += dy; |
| 175 | bottom += dy; |
| 176 | } |
| 177 | |
Chris Craik | c3566d0 | 2013-02-04 16:16:33 -0800 | [diff] [blame] | 178 | void outset(float delta) { |
| 179 | left -= delta; |
| 180 | top -= delta; |
| 181 | right += delta; |
| 182 | bottom += delta; |
| 183 | } |
| 184 | |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 185 | /** |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 186 | * Similar to snapToPixelBoundaries, but estimates bounds conservatively to handle GL rounding |
| 187 | * errors. |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 188 | * |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 189 | * This function should be used whenever estimating the damage rect of geometry already mapped |
| 190 | * into layer space. |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 191 | */ |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 192 | void snapGeometryToPixelBoundaries(bool snapOut) { |
| 193 | if (snapOut) { |
| 194 | /* For AA geometry with a ramp perimeter, don't snap by rounding - AA geometry will have |
| 195 | * a 0.5 pixel perimeter not accounted for in its bounds. Instead, snap by |
| 196 | * conservatively rounding out the bounds with floor/ceil. |
| 197 | * |
| 198 | * In order to avoid changing integer bounds with floor/ceil due to rounding errors |
| 199 | * inset the bounds first by the fudge factor. Very small fraction-of-a-pixel errors |
| 200 | * from this inset will only incur similarly small errors in output, due to transparency |
| 201 | * in extreme outside of the geometry. |
| 202 | */ |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 203 | left = floorf(left + Vertex::GeometryFudgeFactor()); |
| 204 | top = floorf(top + Vertex::GeometryFudgeFactor()); |
| 205 | right = ceilf(right - Vertex::GeometryFudgeFactor()); |
| 206 | bottom = ceilf(bottom - Vertex::GeometryFudgeFactor()); |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 207 | } else { |
| 208 | /* For other geometry, we do the regular rounding in order to snap, but also outset the |
| 209 | * bounds by a fudge factor. This ensures that ambiguous geometry (e.g. a non-AA Rect |
| 210 | * with top left at (0.5, 0.5)) will err on the side of a larger damage rect. |
| 211 | */ |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 212 | left = floorf(left + 0.5f - Vertex::GeometryFudgeFactor()); |
| 213 | top = floorf(top + 0.5f - Vertex::GeometryFudgeFactor()); |
| 214 | right = floorf(right + 0.5f + Vertex::GeometryFudgeFactor()); |
| 215 | bottom = floorf(bottom + 0.5f + Vertex::GeometryFudgeFactor()); |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 216 | } |
Chris Craik | 5e49b30 | 2013-07-30 19:05:20 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Romain Guy | bf43411 | 2010-09-16 14:40:17 -0700 | [diff] [blame] | 219 | void snapToPixelBoundaries() { |
Romain Guy | ae88e5e | 2010-10-22 17:49:18 -0700 | [diff] [blame] | 220 | left = floorf(left + 0.5f); |
| 221 | top = floorf(top + 0.5f); |
| 222 | right = floorf(right + 0.5f); |
| 223 | bottom = floorf(bottom + 0.5f); |
Romain Guy | bf43411 | 2010-09-16 14:40:17 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Chris Craik | f0a5907 | 2013-11-19 18:00:46 -0800 | [diff] [blame] | 226 | void roundOut() { |
| 227 | left = floorf(left); |
| 228 | top = floorf(top); |
| 229 | right = ceilf(right); |
| 230 | bottom = ceilf(bottom); |
| 231 | } |
| 232 | |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 233 | void dump() const { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 234 | 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] | 235 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 236 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 237 | private: |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 238 | void intersectWith(Rect& tmp) const { |
Chris Craik | 2af4635 | 2012-11-26 18:30:17 -0800 | [diff] [blame] | 239 | tmp.left = fmaxf(left, tmp.left); |
| 240 | tmp.top = fmaxf(top, tmp.top); |
| 241 | tmp.right = fminf(right, tmp.right); |
| 242 | tmp.bottom = fminf(bottom, tmp.bottom); |
Romain Guy | 8f85e80 | 2011-12-14 19:23:32 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 245 | Rect intersectWith(float l, float t, float r, float b) const { |
| 246 | Rect tmp; |
Chris Craik | 2af4635 | 2012-11-26 18:30:17 -0800 | [diff] [blame] | 247 | tmp.left = fmaxf(left, l); |
| 248 | tmp.top = fmaxf(top, t); |
| 249 | tmp.right = fminf(right, r); |
| 250 | tmp.bottom = fminf(bottom, b); |
Romain Guy | a1d3c91 | 2011-12-13 14:55:06 -0800 | [diff] [blame] | 251 | return tmp; |
| 252 | } |
| 253 | |
Mathias Agopian | 83b186a | 2011-09-19 16:00:46 -0700 | [diff] [blame] | 254 | }; // class Rect |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 255 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 256 | }; // namespace uirenderer |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 257 | }; // namespace android |
| 258 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 259 | #endif // ANDROID_HWUI_RECT_H |