2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <utils/Log.h>
30 namespace uirenderer {
32 #define RECT_STRING "%5.2f %5.2f %5.2f %5.2f"
33 #define RECT_ARGS(r) \
34 (r).left, (r).top, (r).right, (r).bottom
35 #define SK_RECT_ARGS(r) \
36 (r).left(), (r).top(), (r).right(), (r).bottom()
38 ///////////////////////////////////////////////////////////////////////////////
40 ///////////////////////////////////////////////////////////////////////////////
50 typedef float value_type;
52 // we don't provide copy-ctor and operator= on purpose
53 // because we want the compiler generated versions
62 inline Rect(float left, float top, float right, float bottom):
69 inline Rect(float width, float height):
76 inline Rect(const SkIRect& rect):
80 bottom(rect.fBottom) {
83 inline Rect(const SkRect& rect):
87 bottom(rect.fBottom) {
90 friend int operator==(const Rect& a, const Rect& b) {
91 return !memcmp(&a, &b, sizeof(a));
94 friend int operator!=(const Rect& a, const Rect& b) {
95 return memcmp(&a, &b, sizeof(a));
99 left = top = right = bottom = 0.0f;
102 inline bool isEmpty() const {
103 // this is written in such way this it'll handle NANs to return
105 return !((left < right) && (top < bottom));
108 inline void setEmpty() {
109 left = top = right = bottom = 0.0f;
112 inline void set(float left, float top, float right, float bottom) {
116 this->bottom = bottom;
119 inline void set(const Rect& r) {
120 set(r.left, r.top, r.right, r.bottom);
123 inline void set(const SkIRect& r) {
124 set(r.left(), r.top(), r.right(), r.bottom());
127 inline float getWidth() const {
131 inline float getHeight() const {
135 bool intersects(float l, float t, float r, float b) const {
136 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);
141 return ((tempLeft < tempRight) && (tempTop < tempBottom)); // !isEmpty
144 bool intersects(const Rect& r) const {
145 return intersects(r.left, r.top, r.right, r.bottom);
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.
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);
160 void doIntersect(const Rect& r) {
161 doIntersect(r.left, r.top, r.right, r.bottom);
164 inline bool contains(float l, float t, float r, float b) const {
165 return l >= left && t >= top && r <= right && b <= bottom;
168 inline bool contains(const Rect& r) const {
169 return contains(r.left, r.top, r.right, r.bottom);
172 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;
191 void translate(float dx, float dy) {
198 void inset(float delta) {
202 void outset(float delta) {
209 void outset(float xdelta, float ydelta) {
217 * Similar to snapToPixelBoundaries, but estimates bounds conservatively to handle GL rounding
220 * This function should be used whenever estimating the damage rect of geometry already mapped
223 void snapGeometryToPixelBoundaries(bool 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.
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.
234 left = floorf(left + Vertex::GeometryFudgeFactor());
235 top = floorf(top + Vertex::GeometryFudgeFactor());
236 right = ceilf(right - Vertex::GeometryFudgeFactor());
237 bottom = ceilf(bottom - Vertex::GeometryFudgeFactor());
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.
243 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());
250 void snapToPixelBoundaries() {
251 left = floorf(left + 0.5f);
252 top = floorf(top + 0.5f);
253 right = floorf(right + 0.5f);
254 bottom = floorf(bottom + 0.5f);
260 right = ceilf(right);
261 bottom = ceilf(bottom);
265 * Similar to unionWith, except this makes the assumption that both rects are non-empty
266 * to avoid both emptiness checks.
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);
275 void expandToCover(float x, float y) {
276 left = std::min(left, x);
277 top = std::min(top, y);
278 right = std::max(right, x);
279 bottom = std::max(bottom, y);
282 SkRect toSkRect() const {
283 return SkRect::MakeLTRB(left, top, right, bottom);
286 SkIRect toSkIRect() const {
287 return SkIRect::MakeLTRB(left, top, right, bottom);
290 void dump(const char* label = nullptr) const {
291 ALOGD("%s[l=%.2f t=%.2f r=%.2f b=%.2f]", label ? label : "Rect", left, top, right, bottom);
294 friend std::ostream& operator<<(std::ostream& os, const Rect& rect) {
295 if (rect.isEmpty()) {
296 // Print empty, but continue, since empty rects may still have useful coordinate info
300 if (rect.left == 0 && rect.top == 0) {
301 return os << "[" << rect.right << " x " << rect.bottom << "]";
304 return os << "[" << rect.left
307 << " " << rect.bottom << "]";
311 }; // namespace uirenderer
312 }; // namespace android