blob: 0715187e19ea75653a2b1e4a98f684ab498f68f7 [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
John Reck1bcacfd2017-11-03 10:12:19 -070023#include <SkRect.h>
Chris Craik91eff222016-02-22 13:39:33 -080024#include <algorithm>
25#include <cmath>
26#include <iomanip>
27#include <ostream>
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"
John Reck1bcacfd2017-11-03 10:12:19 -070033#define RECT_ARGS(r) (r).left, (r).top, (r).right, (r).bottom
34#define SK_RECT_ARGS(r) (r).left(), (r).top(), (r).right(), (r).bottom()
Chris Craik28ce94a2013-05-31 11:38:03 -070035
Romain Guybb9524b2010-06-22 18:56:38 -070036///////////////////////////////////////////////////////////////////////////////
37// Structs
38///////////////////////////////////////////////////////////////////////////////
39
Mathias Agopian83b186a2011-09-19 16:00:46 -070040class Rect {
Mathias Agopian83b186a2011-09-19 16:00:46 -070041public:
Romain Guy7ae7ac42010-06-25 13:46:18 -070042 float left;
43 float top;
44 float right;
45 float bottom;
Romain Guybb9524b2010-06-22 18:56:38 -070046
Romain Guy5b3b3522010-10-27 18:57:51 -070047 // Used by Region
48 typedef float value_type;
49
Mathias Agopian83b186a2011-09-19 16:00:46 -070050 // we don't provide copy-ctor and operator= on purpose
51 // because we want the compiler generated versions
52
John Reck1bcacfd2017-11-03 10:12:19 -070053 inline Rect() : left(0), top(0), right(0), bottom(0) {}
Romain Guy9d5316e2010-06-24 19:30:36 -070054
John Reck1bcacfd2017-11-03 10:12:19 -070055 inline Rect(float left, float top, float right, float bottom)
56 : left(left), top(top), right(right), bottom(bottom) {}
Romain Guybb9524b2010-06-22 18:56:38 -070057
John Reck1bcacfd2017-11-03 10:12:19 -070058 inline Rect(float width, float height) : left(0.0f), top(0.0f), right(width), bottom(height) {}
Romain Guy5b3b3522010-10-27 18:57:51 -070059
John Reck1bcacfd2017-11-03 10:12:19 -070060 inline Rect(const SkIRect& rect)
61 : // NOLINT, implicit
62 left(rect.fLeft)
63 , top(rect.fTop)
64 , right(rect.fRight)
65 , bottom(rect.fBottom) {}
Chris Craik82457c52016-06-29 16:22:27 -070066
John Reck1bcacfd2017-11-03 10:12:19 -070067 inline Rect(const SkRect& rect)
68 : // NOLINT, implicit
69 left(rect.fLeft)
70 , top(rect.fTop)
71 , right(rect.fRight)
72 , bottom(rect.fBottom) {}
ztenghuiaf6f7ed2014-03-18 17:25:49 -070073
John Reck1bcacfd2017-11-03 10:12:19 -070074 friend int operator==(const Rect& a, const Rect& b) { return !memcmp(&a, &b, sizeof(a)); }
Romain Guybb9524b2010-06-22 18:56:38 -070075
John Reck1bcacfd2017-11-03 10:12:19 -070076 friend int operator!=(const Rect& a, const Rect& b) { return memcmp(&a, &b, sizeof(a)); }
Romain Guybb9524b2010-06-22 18:56:38 -070077
John Reck1bcacfd2017-11-03 10:12:19 -070078 inline void clear() { left = top = right = bottom = 0.0f; }
Romain Guy5b3b3522010-10-27 18:57:51 -070079
80 inline bool isEmpty() const {
Mathias Agopian83b186a2011-09-19 16:00:46 -070081 // this is written in such way this it'll handle NANs to return
82 // true (empty)
83 return !((left < right) && (top < bottom));
Romain Guy7ae7ac42010-06-25 13:46:18 -070084 }
Romain Guybb9524b2010-06-22 18:56:38 -070085
John Reck1bcacfd2017-11-03 10:12:19 -070086 inline void setEmpty() { left = top = right = bottom = 0.0f; }
Romain Guybb9524b2010-06-22 18:56:38 -070087
Romain Guy5b3b3522010-10-27 18:57:51 -070088 inline void set(float left, float top, float right, float bottom) {
Romain Guy7ae7ac42010-06-25 13:46:18 -070089 this->left = left;
90 this->right = right;
91 this->top = top;
92 this->bottom = bottom;
93 }
Romain Guybb9524b2010-06-22 18:56:38 -070094
John Reck1bcacfd2017-11-03 10:12:19 -070095 inline void set(const Rect& r) { set(r.left, r.top, r.right, r.bottom); }
Romain Guybb9524b2010-06-22 18:56:38 -070096
John Reck1bcacfd2017-11-03 10:12:19 -070097 inline void set(const SkIRect& r) { set(r.left(), r.top(), r.right(), r.bottom()); }
Rob Tsuk487a92c2015-01-06 13:22:54 -080098
John Reck1bcacfd2017-11-03 10:12:19 -070099 inline float getWidth() const { return right - left; }
Romain Guybb9524b2010-06-22 18:56:38 -0700100
John Reck1bcacfd2017-11-03 10:12:19 -0700101 inline float getHeight() const { return bottom - top; }
Romain Guybb9524b2010-06-22 18:56:38 -0700102
Mathias Agopian83b186a2011-09-19 16:00:46 -0700103 bool intersects(float l, float t, float r, float b) const {
Chris Craikac02eb92015-10-05 12:23:46 -0700104 float tempLeft = std::max(left, l);
105 float tempTop = std::max(top, t);
106 float tempRight = std::min(right, r);
107 float tempBottom = std::min(bottom, b);
108
John Reck1bcacfd2017-11-03 10:12:19 -0700109 return ((tempLeft < tempRight) && (tempTop < tempBottom)); // !isEmpty
Romain Guy7ae7ac42010-06-25 13:46:18 -0700110 }
Romain Guybb9524b2010-06-22 18:56:38 -0700111
John Reck1bcacfd2017-11-03 10:12:19 -0700112 bool intersects(const Rect& r) const { return intersects(r.left, r.top, r.right, r.bottom); }
Romain Guybb9524b2010-06-22 18:56:38 -0700113
Chris Craikac02eb92015-10-05 12:23:46 -0700114 /**
115 * This method is named 'doIntersect' instead of 'intersect' so as not to be confused with
116 * SkRect::intersect / android.graphics.Rect#intersect behavior, which do not modify the object
117 * if the intersection of the rects would be empty.
118 */
119 void doIntersect(float l, float t, float r, float b) {
120 left = std::max(left, l);
121 top = std::max(top, t);
122 right = std::min(right, r);
123 bottom = std::min(bottom, b);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700124 }
Romain Guybb9524b2010-06-22 18:56:38 -0700125
John Reck1bcacfd2017-11-03 10:12:19 -0700126 void doIntersect(const Rect& r) { doIntersect(r.left, r.top, r.right, r.bottom); }
Romain Guybb9524b2010-06-22 18:56:38 -0700127
Romain Guy2db5e992013-05-21 15:29:59 -0700128 inline bool contains(float l, float t, float r, float b) const {
Romain Guyec31f832011-12-13 18:39:19 -0800129 return l >= left && t >= top && r <= right && b <= bottom;
130 }
131
John Reck1bcacfd2017-11-03 10:12:19 -0700132 inline bool contains(const Rect& r) const { return contains(r.left, r.top, r.right, r.bottom); }
Romain Guyec31f832011-12-13 18:39:19 -0800133
Romain Guy079ba2c2010-07-16 14:12:24 -0700134 bool unionWith(const Rect& r) {
135 if (r.left < r.right && r.top < r.bottom) {
136 if (left < right && top < bottom) {
137 if (left > r.left) left = r.left;
138 if (top > r.top) top = r.top;
139 if (right < r.right) right = r.right;
140 if (bottom < r.bottom) bottom = r.bottom;
141 return true;
142 } else {
143 left = r.left;
144 top = r.top;
145 right = r.right;
146 bottom = r.bottom;
147 return true;
148 }
149 }
150 return false;
151 }
152
Romain Guy5b3b3522010-10-27 18:57:51 -0700153 void translate(float dx, float dy) {
154 left += dx;
155 right += dx;
156 top += dy;
157 bottom += dy;
158 }
159
John Reck1bcacfd2017-11-03 10:12:19 -0700160 void inset(float delta) { outset(-delta); }
Chris Craike4aa95e2014-05-08 13:57:05 -0700161
Chris Craikc3566d02013-02-04 16:16:33 -0800162 void outset(float delta) {
163 left -= delta;
164 top -= delta;
165 right += delta;
166 bottom += delta;
167 }
168
Chris Craik05f3d6e2014-06-02 16:27:04 -0700169 void outset(float xdelta, float ydelta) {
170 left -= xdelta;
171 top -= ydelta;
172 right += xdelta;
173 bottom += ydelta;
174 }
175
Chris Craik5e49b302013-07-30 19:05:20 -0700176 /**
Chris Craik32f05e32013-09-17 16:20:29 -0700177 * Similar to snapToPixelBoundaries, but estimates bounds conservatively to handle GL rounding
178 * errors.
Chris Craik5e49b302013-07-30 19:05:20 -0700179 *
Chris Craik32f05e32013-09-17 16:20:29 -0700180 * This function should be used whenever estimating the damage rect of geometry already mapped
181 * into layer space.
Chris Craik5e49b302013-07-30 19:05:20 -0700182 */
Chris Craik32f05e32013-09-17 16:20:29 -0700183 void snapGeometryToPixelBoundaries(bool snapOut) {
184 if (snapOut) {
185 /* For AA geometry with a ramp perimeter, don't snap by rounding - AA geometry will have
186 * a 0.5 pixel perimeter not accounted for in its bounds. Instead, snap by
187 * conservatively rounding out the bounds with floor/ceil.
188 *
189 * In order to avoid changing integer bounds with floor/ceil due to rounding errors
190 * inset the bounds first by the fudge factor. Very small fraction-of-a-pixel errors
191 * from this inset will only incur similarly small errors in output, due to transparency
192 * in extreme outside of the geometry.
193 */
Chris Craik564acf72014-01-02 16:46:18 -0800194 left = floorf(left + Vertex::GeometryFudgeFactor());
195 top = floorf(top + Vertex::GeometryFudgeFactor());
196 right = ceilf(right - Vertex::GeometryFudgeFactor());
197 bottom = ceilf(bottom - Vertex::GeometryFudgeFactor());
Chris Craik32f05e32013-09-17 16:20:29 -0700198 } else {
199 /* For other geometry, we do the regular rounding in order to snap, but also outset the
200 * bounds by a fudge factor. This ensures that ambiguous geometry (e.g. a non-AA Rect
201 * with top left at (0.5, 0.5)) will err on the side of a larger damage rect.
202 */
Chris Craik564acf72014-01-02 16:46:18 -0800203 left = floorf(left + 0.5f - Vertex::GeometryFudgeFactor());
204 top = floorf(top + 0.5f - Vertex::GeometryFudgeFactor());
205 right = floorf(right + 0.5f + Vertex::GeometryFudgeFactor());
206 bottom = floorf(bottom + 0.5f + Vertex::GeometryFudgeFactor());
Chris Craik32f05e32013-09-17 16:20:29 -0700207 }
Chris Craik5e49b302013-07-30 19:05:20 -0700208 }
209
Romain Guybf434112010-09-16 14:40:17 -0700210 void snapToPixelBoundaries() {
Romain Guyae88e5e2010-10-22 17:49:18 -0700211 left = floorf(left + 0.5f);
212 top = floorf(top + 0.5f);
213 right = floorf(right + 0.5f);
214 bottom = floorf(bottom + 0.5f);
Romain Guybf434112010-09-16 14:40:17 -0700215 }
216
Chris Craikf0a59072013-11-19 18:00:46 -0800217 void roundOut() {
218 left = floorf(left);
219 top = floorf(top);
220 right = ceilf(right);
221 bottom = ceilf(bottom);
222 }
223
Chris Craik15c3f192015-12-03 12:16:56 -0800224 /*
225 * Similar to unionWith, except this makes the assumption that both rects are non-empty
226 * to avoid both emptiness checks.
227 */
228 void expandToCover(const Rect& other) {
229 left = std::min(left, other.left);
230 top = std::min(top, other.top);
231 right = std::max(right, other.right);
232 bottom = std::max(bottom, other.bottom);
233 }
234
235 void expandToCover(float x, float y) {
Chris Craikdf72b632015-06-30 17:56:13 -0700236 left = std::min(left, x);
237 top = std::min(top, y);
238 right = std::max(right, x);
239 bottom = std::max(bottom, y);
Chris Craikc93e45c2014-07-16 10:15:56 -0700240 }
241
John Reck1bcacfd2017-11-03 10:12:19 -0700242 SkRect toSkRect() const { return SkRect::MakeLTRB(left, top, right, bottom); }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800243
John Reck1bcacfd2017-11-03 10:12:19 -0700244 SkIRect toSkIRect() const { return SkIRect::MakeLTRB(left, top, right, bottom); }
Rob Tsuk487a92c2015-01-06 13:22:54 -0800245
Chris Craike84a2082014-12-22 14:28:49 -0800246 void dump(const char* label = nullptr) const {
Chris Craik8ecf41c2015-11-16 10:27:59 -0800247 ALOGD("%s[l=%.2f t=%.2f r=%.2f b=%.2f]", label ? label : "Rect", left, top, right, bottom);
Romain Guy7ae7ac42010-06-25 13:46:18 -0700248 }
Chris Craik91eff222016-02-22 13:39:33 -0800249
250 friend std::ostream& operator<<(std::ostream& os, const Rect& rect) {
251 if (rect.isEmpty()) {
Chris Craik034a10b2016-03-09 16:03:21 -0800252 // Print empty, but continue, since empty rects may still have useful coordinate info
253 os << "(empty)";
Chris Craik91eff222016-02-22 13:39:33 -0800254 }
255
256 if (rect.left == 0 && rect.top == 0) {
257 return os << "[" << rect.right << " x " << rect.bottom << "]";
258 }
259
John Reck1bcacfd2017-11-03 10:12:19 -0700260 return os << "[" << rect.left << " " << rect.top << " " << rect.right << " " << rect.bottom
261 << "]";
Chris Craik91eff222016-02-22 13:39:33 -0800262 }
John Reck1bcacfd2017-11-03 10:12:19 -0700263}; // class Rect
Romain Guybb9524b2010-06-22 18:56:38 -0700264
John Reck1bcacfd2017-11-03 10:12:19 -0700265}; // namespace uirenderer
266}; // namespace android