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