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