| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
| Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 2 | * Copyright (C) 2009 The Android Open Source Project | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3 | * | 
| Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 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 | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7 | * | 
| Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 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. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 15 | */ | 
|  | 16 |  | 
| Jamie Gennis | 5933280 | 2012-05-07 13:49:17 -0700 | [diff] [blame] | 17 | #include <system/graphics.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | #include <ui/Rect.h> | 
|  | 19 |  | 
|  | 20 | namespace android { | 
|  | 21 |  | 
| Dianne Hackborn | 9147d11 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 22 | static inline int32_t min(int32_t a, int32_t b) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | return (a<b) ? a : b; | 
|  | 24 | } | 
|  | 25 |  | 
| Dianne Hackborn | 9147d11 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 26 | static inline int32_t max(int32_t a, int32_t b) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | { | 
|  | 39 | if (top<rhs.top) { | 
|  | 40 | return true; | 
|  | 41 | } else if (top == rhs.top) { | 
|  | 42 | if (left < rhs.left) { | 
|  | 43 | return true; | 
|  | 44 | } else if (left == rhs.left) { | 
|  | 45 | if (bottom<rhs.bottom) { | 
|  | 46 | return true; | 
|  | 47 | } else if (bottom == rhs.bottom) { | 
|  | 48 | if (right<rhs.right) { | 
|  | 49 | return true; | 
|  | 50 | } | 
|  | 51 | } | 
|  | 52 | } | 
|  | 53 | } | 
|  | 54 | return false; | 
|  | 55 | } | 
|  | 56 |  | 
| Dianne Hackborn | 9147d11 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 57 | Rect& Rect::offsetTo(int32_t x, int32_t y) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | { | 
|  | 59 | right -= left - x; | 
|  | 60 | bottom -= top - y; | 
|  | 61 | left = x; | 
|  | 62 | top = y; | 
|  | 63 | return *this; | 
|  | 64 | } | 
|  | 65 |  | 
| Dianne Hackborn | 9147d11 | 2010-07-09 11:44:11 -0700 | [diff] [blame] | 66 | Rect& Rect::offsetBy(int32_t x, int32_t y) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | { | 
|  | 68 | left += x; | 
|  | 69 | top  += y; | 
|  | 70 | right+= x; | 
|  | 71 | bottom+=y; | 
|  | 72 | return *this; | 
|  | 73 | } | 
|  | 74 |  | 
| Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 75 | const Rect Rect::operator + (const Point& rhs) const | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | { | 
| Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 77 | const Rect result(left+rhs.x, top+rhs.y, right+rhs.x, bottom+rhs.y); | 
|  | 78 | return result; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | } | 
|  | 80 |  | 
| Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 81 | const Rect Rect::operator - (const Point& rhs) const | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | { | 
| Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 83 | const Rect result(left-rhs.x, top-rhs.y, right-rhs.x, bottom-rhs.y); | 
|  | 84 | return result; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
|  | 87 | bool Rect::intersect(const Rect& with, Rect* result) const | 
|  | 88 | { | 
|  | 89 | result->left    = max(left, with.left); | 
|  | 90 | result->top     = max(top, with.top); | 
|  | 91 | result->right   = min(right, with.right); | 
|  | 92 | result->bottom  = min(bottom, with.bottom); | 
|  | 93 | return !(result->isEmpty()); | 
|  | 94 | } | 
|  | 95 |  | 
| Jamie Gennis | f15a83f | 2012-05-10 20:43:55 -0700 | [diff] [blame] | 96 | Rect Rect::transform(uint32_t xform, int32_t width, int32_t height) const { | 
| Jamie Gennis | 5933280 | 2012-05-07 13:49:17 -0700 | [diff] [blame] | 97 | Rect result(*this); | 
|  | 98 | if (xform & HAL_TRANSFORM_FLIP_H) { | 
|  | 99 | result = Rect(width - result.right, result.top, | 
|  | 100 | width - result.left, result.bottom); | 
|  | 101 | } | 
|  | 102 | if (xform & HAL_TRANSFORM_FLIP_V) { | 
|  | 103 | result = Rect(result.left, height - result.bottom, | 
|  | 104 | result.right, height - result.top); | 
|  | 105 | } | 
|  | 106 | if (xform & HAL_TRANSFORM_ROT_90) { | 
|  | 107 | int left = height - result.bottom; | 
|  | 108 | int top = result.left; | 
|  | 109 | int right = height - result.top; | 
|  | 110 | int bottom = result.right; | 
|  | 111 | result = Rect(left, top, right, bottom); | 
|  | 112 | } | 
|  | 113 | return result; | 
|  | 114 | } | 
|  | 115 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | }; // namespace android |