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