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