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