blob: 5786668addc57f062fb9d2885cdc6c210488ab65 [file] [log] [blame]
Romain Guybb9524b2010-06-22 18:56:38 -07001/*
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 Craik91eff222016-02-22 13:39:33 -080017#pragma once
Romain Guy5b3b3522010-10-27 18:57:51 -070018
Chris Craik91eff222016-02-22 13:39:33 -080019#include "Vertex.h"
Romain Guybb9524b2010-06-22 18:56:38 -070020
Romain Guy5cbbce52010-06-27 22:59:20 -070021#include <utils/Log.h>
22
Chris Craik91eff222016-02-22 13:39:33 -080023#include <algorithm>
24#include <cmath>
25#include <iomanip>
26#include <ostream>
27#include <SkRect.h>
Chris Craik32f05e32013-09-17 16:20:29 -070028
Romain Guybb9524b2010-06-22 18:56:38 -070029namespace android {
Romain Guy9d5316e2010-06-24 19:30:36 -070030namespace uirenderer {
Romain Guybb9524b2010-06-22 18:56:38 -070031
Chris Craik62d307c2014-07-29 10:35:13 -070032#define RECT_STRING "%5.2f %5.2f %5.2f %5.2f"
Chris Craik28ce94a2013-05-31 11:38:03 -070033#define RECT_ARGS(r) \
34 (r).left, (r).top, (r).right, (r).bottom
Chris Craik3f0854292014-04-15 16:18:08 -070035#define SK_RECT_ARGS(r) \
36 (r).left(), (r).top(), (r).right(), (r).bottom()
Chris Craik28ce94a2013-05-31 11:38:03 -070037
Romain Guybb9524b2010-06-22 18:56:38 -070038///////////////////////////////////////////////////////////////////////////////
39// Structs
40///////////////////////////////////////////////////////////////////////////////
41
Mathias Agopian83b186a2011-09-19 16:00:46 -070042class Rect {
Mathias Agopian83b186a2011-09-19 16:00:46 -070043public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070044 float left;
45 float top;
46 float right;
47 float bottom;
Romain Guybb9524b2010-06-22 18:56:38 -070048
Romain Guy5b3b3522010-10-27 18:57:51 -070049 // Used by Region
50 typedef float value_type;
51
Mathias Agopian83b186a2011-09-19 16:00:46 -070052 // we don't provide copy-ctor and operator= on purpose
53 // because we want the compiler generated versions
54
Romain Guy5b3b3522010-10-27 18:57:51 -070055 inline Rect():
Romain Guy7ae7ac42010-06-25 13:46:18 -070056 left(0),
57 top(0),
58 right(0),
59 bottom(0) {
60 }
Romain Guy9d5316e2010-06-24 19:30:36 -070061
Romain Guy5b3b3522010-10-27 18:57:51 -070062 inline Rect(float left, float top, float right, float bottom):
Romain Guy7ae7ac42010-06-25 13:46:18 -070063 left(left),
64 top(top),
65 right(right),
66 bottom(bottom) {
67 }
Romain Guybb9524b2010-06-22 18:56:38 -070068
Romain Guy5b3b3522010-10-27 18:57:51 -070069 inline Rect(float width, float height):
70 left(0.0f),
71 top(0.0f),
72 right(width),
73 bottom(height) {
74 }
75
Chris Craik82457c52016-06-29 16:22:27 -070076 inline Rect(const SkIRect& rect):
77 left(rect.fLeft),
78 top(rect.fTop),
79 right(rect.fRight),
80 bottom(rect.fBottom) {
81 }
82
ztenghuiaf6f7ed2014-03-18 17:25:49 -070083 inline Rect(const SkRect& rect):
84 left(rect.fLeft),
85 top(rect.fTop),
86 right(rect.fRight),
87 bottom(rect.fBottom) {
88 }
89
Romain Guy7ae7ac42010-06-25 13:46:18 -070090 friend int operator==(const Rect& a, const Rect& b) {
91 return !memcmp(&a, &b, sizeof(a));
92 }
Romain Guybb9524b2010-06-22 18:56:38 -070093
Romain Guy7ae7ac42010-06-25 13:46:18 -070094 friend int operator!=(const Rect& a, const Rect& b) {
95 return memcmp(&a, &b, sizeof(a));
96 }
Romain Guybb9524b2010-06-22 18:56:38 -070097
Romain Guy5b3b3522010-10-27 18:57:51 -070098 inline void clear() {
99 left = top = right = bottom = 0.0f;
100 }
101
102 inline bool isEmpty() const {
Mathias Agopian83b186a2011-09-19 16:00:46 -0700103 // this is written in such way this it'll handle NANs to return
104 // true (empty)
105 return !((left < right) && (top < bottom));
Romain Guy7ae7ac42010-06-25 13:46:18 -0700106 }
Romain Guybb9524b2010-06-22 18:56:38 -0700107
Romain Guy5b3b3522010-10-27 18:57:51 -0700108 inline void setEmpty() {
109 left = top = right = bottom = 0.0f;
Romain Guy7ae7ac42010-06-25 13:46:18 -0700110 }
Romain Guybb9524b2010-06-22 18:56:38 -0700111
Romain Guy5b3b3522010-10-27 18:57:51 -0700112 inline void set(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700113 this->left = left;
114 this->right = right;
115 this->top = top;
116 this->bottom = bottom;
117 }
Romain Guybb9524b2010-06-22 18:56:38 -0700118
Romain Guy5b3b3522010-10-27 18:57:51 -0700119 inline void set(const Rect& r) {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700120 set(r.left, r.top, r.right, r.bottom);
121 }
Romain Guybb9524b2010-06-22 18:56:38 -0700122
Rob Tsuk487a92c2015-01-06 13:22:54 -0800123 inline void set(const SkIRect& r) {
124 set(r.left(), r.top(), r.right(), r.bottom());
125 }
126
Romain Guy8aef54f2010-09-01 15:13:49 -0700127 inline float getWidth() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700128 return right - left;
129 }
Romain Guybb9524b2010-06-22 18:56:38 -0700130
Romain Guy8aef54f2010-09-01 15:13:49 -0700131 inline float getHeight() const {
Romain Guy7ae7ac42010-06-25 13:46:18 -0700132 return bottom - top;
133 }
Romain Guybb9524b2010-06-22 18:56:38 -0700134
Mathias Agopian83b186a2011-09-19 16:00:46 -0700135 bool intersects(float l, float t, float r, float b) const {
Chris Craikac02eb92015-10-05 12:23:46 -0700136 float tempLeft = std::max(left, l);
137 float tempTop = std::max(top, t);
138 float tempRight = std::min(right, r);
139 float tempBottom = std::min(bottom, b);
140
141 return ((tempLeft < tempRight) && (tempTop < tempBottom)); // !isEmpty
Romain Guy7ae7ac42010-06-25 13:46:18 -0700142 }
Romain Guybb9524b2010-06-22 18:56:38 -0700143
Romain Guy7ae7ac42010-06-25 13:46:18 -0700144 bool intersects(const Rect& r) const {
145 return intersects(r.left, r.top, r.right, r.bottom);
146 }
Romain Guybb9524b2010-06-22 18:56:38 -0700147
Chris Craikac02eb92015-10-05 12:23:46 -0700148 /**
149 * This method is named 'doIntersect' instead of 'intersect' so as not to be confused with
150 * SkRect::intersect / android.graphics.Rect#intersect behavior, which do not modify the object
151 * if the intersection of the rects would be empty.
152 */
153 void doIntersect(float l, float t, float r, float b) {
154 left = std::max(left, l);
155 top = std::max(top, t);
156 right = std::min(right, r);
157 bottom = std::min(bottom, b);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700158 }
Romain Guybb9524b2010-06-22 18:56:38 -0700159
Chris Craikac02eb92015-10-05 12:23:46 -0700160 void doIntersect(const Rect& r) {
161 doIntersect(r.left, r.top, r.right, r.bottom);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700162 }
Romain Guybb9524b2010-06-22 18:56:38 -0700163
Romain Guy2db5e992013-05-21 15:29:59 -0700164 inline bool contains(float l, float t, float r, float b) const {
Romain Guyec31f832011-12-13 18:39:19 -0800165 return l >= left && t >= top && r <= right && b <= bottom;
166 }
167
Romain Guy2db5e992013-05-21 15:29:59 -0700168 inline bool contains(const Rect& r) const {
Romain Guyec31f832011-12-13 18:39:19 -0800169 return contains(r.left, r.top, r.right, r.bottom);
170 }
171
Romain Guy079ba2c2010-07-16 14:12:24 -0700172 bool unionWith(const Rect& r) {
173 if (r.left < r.right && r.top < r.bottom) {
174 if (left < right && top < bottom) {
175 if (left > r.left) left = r.left;
176 if (top > r.top) top = r.top;
177 if (right < r.right) right = r.right;
178 if (bottom < r.bottom) bottom = r.bottom;
179 return true;
180 } else {
181 left = r.left;
182 top = r.top;
183 right = r.right;
184 bottom = r.bottom;
185 return true;
186 }
187 }
188 return false;
189 }
190
Romain Guy5b3b3522010-10-27 18:57:51 -0700191 void translate(float dx, float dy) {
192 left += dx;
193 right += dx;
194 top += dy;
195 bottom += dy;
196 }
197
Chris Craike4aa95e2014-05-08 13:57:05 -0700198 void inset(float delta) {
199 outset(-delta);
200 }
201
Chris Craikc3566d02013-02-04 16:16:33 -0800202 void outset(float delta) {
203 left -= delta;
204 top -= delta;
205 right += delta;
206 bottom += delta;
207 }
208
Chris Craik05f3d6e2014-06-02 16:27:04 -0700209 void outset(float xdelta, float ydelta) {
210 left -= xdelta;
211 top -= ydelta;
212 right += xdelta;
213 bottom += ydelta;
214 }
215
Chris Craik5e49b302013-07-30 19:05:20 -0700216 /**
Chris Craik32f05e32013-09-17 16:20:29 -0700217 * Similar to snapToPixelBoundaries, but estimates bounds conservatively to handle GL rounding
218 * errors.
Chris Craik5e49b302013-07-30 19:05:20 -0700219 *
Chris Craik32f05e32013-09-17 16:20:29 -0700220 * This function should be used whenever estimating the damage rect of geometry already mapped
221 * into layer space.
Chris Craik5e49b302013-07-30 19:05:20 -0700222 */
Chris Craik32f05e32013-09-17 16:20:29 -0700223 void snapGeometryToPixelBoundaries(bool snapOut) {
224 if (snapOut) {
225 /* For AA geometry with a ramp perimeter, don't snap by rounding - AA geometry will have
226 * a 0.5 pixel perimeter not accounted for in its bounds. Instead, snap by
227 * conservatively rounding out the bounds with floor/ceil.
228 *
229 * In order to avoid changing integer bounds with floor/ceil due to rounding errors
230 * inset the bounds first by the fudge factor. Very small fraction-of-a-pixel errors
231 * from this inset will only incur similarly small errors in output, due to transparency
232 * in extreme outside of the geometry.
233 */
Chris Craik564acf72014-01-02 16:46:18 -0800234 left = floorf(left + Vertex::GeometryFudgeFactor());
235 top = floorf(top + Vertex::GeometryFudgeFactor());
236 right = ceilf(right - Vertex::GeometryFudgeFactor());
237 bottom = ceilf(bottom - Vertex::GeometryFudgeFactor());
Chris Craik32f05e32013-09-17 16:20:29 -0700238 } else {
239 /* For other geometry, we do the regular rounding in order to snap, but also outset the
240 * bounds by a fudge factor. This ensures that ambiguous geometry (e.g. a non-AA Rect
241 * with top left at (0.5, 0.5)) will err on the side of a larger damage rect.
242 */
Chris Craik564acf72014-01-02 16:46:18 -0800243 left = floorf(left + 0.5f - Vertex::GeometryFudgeFactor());
244 top = floorf(top + 0.5f - Vertex::GeometryFudgeFactor());
245 right = floorf(right + 0.5f + Vertex::GeometryFudgeFactor());
246 bottom = floorf(bottom + 0.5f + Vertex::GeometryFudgeFactor());
Chris Craik32f05e32013-09-17 16:20:29 -0700247 }
Chris Craik5e49b302013-07-30 19:05:20 -0700248 }
249
Romain Guybf434112010-09-16 14:40:17 -0700250 void snapToPixelBoundaries() {
Romain Guyae88e5e2010-10-22 17:49:18 -0700251 left = floorf(left + 0.5f);
252 top = floorf(top + 0.5f);
253 right = floorf(right + 0.5f);
254 bottom = floorf(bottom + 0.5f);
Romain Guybf434112010-09-16 14:40:17 -0700255 }
256
Chris Craikf0a59072013-11-19 18:00:46 -0800257 void roundOut() {
258 left = floorf(left);
259 top = floorf(top);
260 right = ceilf(right);
261 bottom = ceilf(bottom);
262 }
263
Chris Craik15c3f192015-12-03 12:16:56 -0800264 /*
265 * Similar to unionWith, except this makes the assumption that both rects are non-empty
266 * to avoid both emptiness checks.
267 */
268 void expandToCover(const Rect& other) {
269 left = std::min(left, other.left);
270 top = std::min(top, other.top);
271 right = std::max(right, other.right);
272 bottom = std::max(bottom, other.bottom);
273 }
274
275 void expandToCover(float x, float y) {
Chris Craikdf72b632015-06-30 17:56:13 -0700276 left = std::min(left, x);
277 top = std::min(top, y);
278 right = std::max(right, x);
279 bottom = std::max(bottom, y);
Chris Craikc93e45c2014-07-16 10:15:56 -0700280 }
281
Rob Tsuk487a92c2015-01-06 13:22:54 -0800282 SkRect toSkRect() const {
283 return SkRect::MakeLTRB(left, top, right, bottom);
284 }
285
286 SkIRect toSkIRect() const {
287 return SkIRect::MakeLTRB(left, top, right, bottom);
288 }
289
Chris Craike84a2082014-12-22 14:28:49 -0800290 void dump(const char* label = nullptr) const {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800291 ALOGD("%s[l=%.2f t=%.2f r=%.2f b=%.2f]", label ? label : "Rect", left, top, right, bottom);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700292 }
Chris Craik91eff222016-02-22 13:39:33 -0800293
294 friend std::ostream& operator<<(std::ostream& os, const Rect& rect) {
295 if (rect.isEmpty()) {
Chris Craik034a10b2016-03-09 16:03:21 -0800296 // Print empty, but continue, since empty rects may still have useful coordinate info
297 os << "(empty)";
Chris Craik91eff222016-02-22 13:39:33 -0800298 }
299
300 if (rect.left == 0 && rect.top == 0) {
301 return os << "[" << rect.right << " x " << rect.bottom << "]";
302 }
303
304 return os << "[" << rect.left
305 << " " << rect.top
306 << " " << rect.right
307 << " " << rect.bottom << "]";
308 }
Mathias Agopian83b186a2011-09-19 16:00:46 -0700309}; // class Rect
Romain Guybb9524b2010-06-22 18:56:38 -0700310
Romain Guy9d5316e2010-06-24 19:30:36 -0700311}; // namespace uirenderer
Romain Guybb9524b2010-06-22 18:56:38 -0700312}; // namespace android
313