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 
inset(int32_t _left,int32_t _top,int32_t _right,int32_t _bottom)75 Rect& Rect::inset(int32_t _left, int32_t _top, int32_t _right, int32_t _bottom) {
76     this->left += _left;
77     this->top += _top;
78     this->right -= _right;
79     this->bottom -= _bottom;
80     return *this;
81 }
82 
operator +(const Point & rhs) const83 const Rect Rect::operator +(const Point& rhs) const {
84     const Rect result(left + rhs.x, top + rhs.y, right + rhs.x, bottom + rhs.y);
85     return result;
86 }
87 
operator -(const Point & rhs) const88 const Rect Rect::operator -(const Point& rhs) const {
89     const Rect result(left - rhs.x, top - rhs.y, right - rhs.x, bottom - rhs.y);
90     return result;
91 }
92 
intersect(const Rect & with,Rect * result) const93 bool Rect::intersect(const Rect& with, Rect* result) const {
94     result->left = max(left, with.left);
95     result->top = max(top, with.top);
96     result->right = min(right, with.right);
97     result->bottom = min(bottom, with.bottom);
98     return !(result->isEmpty());
99 }
100 
transform(uint32_t xform,int32_t width,int32_t height) const101 Rect Rect::transform(uint32_t xform, int32_t width, int32_t height) const {
102     Rect result(*this);
103     if (xform & HAL_TRANSFORM_FLIP_H) {
104         result = Rect(width - result.right, result.top, width - result.left,
105                 result.bottom);
106     }
107     if (xform & HAL_TRANSFORM_FLIP_V) {
108         result = Rect(result.left, height - result.bottom, result.right,
109                 height - result.top);
110     }
111     if (xform & HAL_TRANSFORM_ROT_90) {
112         int left = height - result.bottom;
113         int top = result.left;
114         int right = height - result.top;
115         int bottom = result.right;
116         result = Rect(left, top, right, bottom);
117     }
118     return result;
119 }
120 
reduce(const Rect & exclude) const121 Rect Rect::reduce(const Rect& exclude) const {
122     Rect result(Rect::EMPTY_RECT);
123 
124     uint32_t mask = 0;
125     mask |= (exclude.left   > left)   ? 1 : 0;
126     mask |= (exclude.top    > top)    ? 2 : 0;
127     mask |= (exclude.right  < right)  ? 4 : 0;
128     mask |= (exclude.bottom < bottom) ? 8 : 0;
129 
130     if (mask == 0) {
131         // crop entirely covers us
132         result.clear();
133     } else {
134         result = *this;
135         if (!(mask & (mask - 1))) {
136             // power-of-2, i.e.: just one bit is set
137             if (mask & 1) {
138                 result.right = min(result.right, exclude.left);
139             } else if (mask & 2) {
140                 result.bottom = min(result.bottom, exclude.top);
141             } else if (mask & 4) {
142                 result.left = max(result.left, exclude.right);
143             } else if (mask & 8) {
144                 result.top = max(result.top, exclude.bottom);
145             }
146         }
147     }
148 
149     return result;
150 }
151 
152 }; // namespace android
153