Rect.cpp revision 5065a55291b67f584d7b0be3fa3cfc4e29a3cd1c
1/*
2 * Copyright (C) 2009 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#include <system/graphics.h>
18#include <ui/Rect.h>
19
20namespace android {
21
22const Rect Rect::INVALID_RECT{0, 0, -1, -1};
23
24static inline int32_t min(int32_t a, int32_t b) {
25    return (a < b) ? a : b;
26}
27
28static inline int32_t max(int32_t a, int32_t b) {
29    return (a > b) ? a : b;
30}
31
32void Rect::makeInvalid() {
33    left = 0;
34    top = 0;
35    right = -1;
36    bottom = -1;
37}
38
39bool Rect::operator <(const Rect& rhs) const {
40    if (top < rhs.top) {
41        return true;
42    } else if (top == rhs.top) {
43        if (left < rhs.left) {
44            return true;
45        } else if (left == rhs.left) {
46            if (bottom < rhs.bottom) {
47                return true;
48            } else if (bottom == rhs.bottom) {
49                if (right < rhs.right) {
50                    return true;
51                }
52            }
53        }
54    }
55    return false;
56}
57
58Rect& Rect::offsetTo(int32_t x, int32_t y) {
59    right -= left - x;
60    bottom -= top - y;
61    left = x;
62    top = y;
63    return *this;
64}
65
66Rect& Rect::offsetBy(int32_t x, int32_t y) {
67    left += x;
68    top += y;
69    right += x;
70    bottom += y;
71    return *this;
72}
73
74const Rect Rect::operator +(const Point& rhs) const {
75    const Rect result(left + rhs.x, top + rhs.y, right + rhs.x, bottom + rhs.y);
76    return result;
77}
78
79const Rect Rect::operator -(const Point& rhs) const {
80    const Rect result(left - rhs.x, top - rhs.y, right - rhs.x, bottom - rhs.y);
81    return result;
82}
83
84bool Rect::intersect(const Rect& with, Rect* result) const {
85    result->left = max(left, with.left);
86    result->top = max(top, with.top);
87    result->right = min(right, with.right);
88    result->bottom = min(bottom, with.bottom);
89    return !(result->isEmpty());
90}
91
92Rect Rect::transform(uint32_t xform, int32_t width, int32_t height) const {
93    Rect result(*this);
94    if (xform & HAL_TRANSFORM_FLIP_H) {
95        result = Rect(width - result.right, result.top, width - result.left,
96                result.bottom);
97    }
98    if (xform & HAL_TRANSFORM_FLIP_V) {
99        result = Rect(result.left, height - result.bottom, result.right,
100                height - result.top);
101    }
102    if (xform & HAL_TRANSFORM_ROT_90) {
103        int left = height - result.bottom;
104        int top = result.left;
105        int right = height - result.top;
106        int bottom = result.right;
107        result = Rect(left, top, right, bottom);
108    }
109    return result;
110}
111
112Rect Rect::reduce(const Rect& exclude) const {
113    Rect result;
114
115    uint32_t mask = 0;
116    mask |= (exclude.left   > left)   ? 1 : 0;
117    mask |= (exclude.top    > top)    ? 2 : 0;
118    mask |= (exclude.right  < right)  ? 4 : 0;
119    mask |= (exclude.bottom < bottom) ? 8 : 0;
120
121    if (mask == 0) {
122        // crop entirely covers us
123        result.clear();
124    } else {
125        result = *this;
126        if (!(mask & (mask - 1))) {
127            // power-of-2, i.e.: just one bit is set
128            if (mask & 1) {
129                result.right = exclude.left;
130            } else if (mask & 2) {
131                result.bottom = exclude.top;
132            } else if (mask & 4) {
133                result.left = exclude.right;
134            } else if (mask & 8) {
135                result.top = exclude.bottom;
136            }
137        }
138    }
139
140    return result;
141}
142
143}; // namespace android
144