1 /* 2 * Copyright (C) 2006 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 #ifndef ANDROID_UI_RECT 17 #define ANDROID_UI_RECT 18 #include <utils/Flattenable.h> 19 #include <utils/Log.h> 20 #include <utils/TypeHelpers.h> 21 #include <log/log.h> 22 #include <ui/FloatRect.h> 23 #include <ui/Point.h> 24 #include <android/rect.h> 25 namespace android { 26 class Rect : public ARect, public LightFlattenablePod<Rect> 27 { 28 public: 29 typedef ARect::value_type value_type; 30 static const Rect INVALID_RECT; 31 static const Rect EMPTY_RECT; 32 // we don't provide copy-ctor and operator= on purpose 33 // because we want the compiler generated versions Rect()34 inline Rect() : Rect(INVALID_RECT) {} 35 template <typename T> Rect(T w,T h)36 inline Rect(T w, T h) { 37 if (w > INT32_MAX) { 38 w = INT32_MAX; 39 } 40 if (h > INT32_MAX) { 41 h = INT32_MAX; 42 } 43 left = top = 0; 44 right = static_cast<int32_t>(w); 45 bottom = static_cast<int32_t>(h); 46 } Rect(int32_t l,int32_t t,int32_t r,int32_t b)47 inline Rect(int32_t l, int32_t t, int32_t r, int32_t b) { 48 left = l; 49 top = t; 50 right = r; 51 bottom = b; 52 } Rect(const Point & lt,const Point & rb)53 inline Rect(const Point& lt, const Point& rb) { 54 left = lt.x; 55 top = lt.y; 56 right = rb.x; 57 bottom = rb.y; 58 } Rect(const FloatRect & floatRect)59 inline explicit Rect(const FloatRect& floatRect) { 60 // Ideally we would use std::round, but we don't want to add an STL 61 // dependency here, so we use an approximation 62 left = static_cast<int32_t>(floatRect.left + 0.5f); 63 top = static_cast<int32_t>(floatRect.top + 0.5f); 64 right = static_cast<int32_t>(floatRect.right + 0.5f); 65 bottom = static_cast<int32_t>(floatRect.bottom + 0.5f); 66 } 67 void makeInvalid(); clear()68 inline void clear() { 69 left = top = right = bottom = 0; 70 } 71 // a valid rectangle has a non negative width and height isValid()72 inline bool isValid() const { 73 return (getWidth() >= 0) && (getHeight() >= 0); 74 } 75 // an empty rect has a zero width or height, or is invalid isEmpty()76 inline bool isEmpty() const { 77 return (getWidth() <= 0) || (getHeight() <= 0); 78 } 79 // rectangle's width 80 __attribute__((no_sanitize("signed-integer-overflow"))) getWidth()81 inline int32_t getWidth() const { 82 return right - left; 83 } 84 // rectangle's height 85 __attribute__((no_sanitize("signed-integer-overflow"))) getHeight()86 inline int32_t getHeight() const { 87 return bottom - top; 88 } 89 __attribute__((no_sanitize("signed-integer-overflow"))) getBounds()90 inline Rect getBounds() const { 91 return Rect(right - left, bottom - top); 92 } setLeftTop(const Point & lt)93 void setLeftTop(const Point& lt) { 94 left = lt.x; 95 top = lt.y; 96 } setRightBottom(const Point & rb)97 void setRightBottom(const Point& rb) { 98 right = rb.x; 99 bottom = rb.y; 100 } 101 102 // the following 4 functions return the 4 corners of the rect as Point leftTop()103 Point leftTop() const { 104 return Point(left, top); 105 } rightBottom()106 Point rightBottom() const { 107 return Point(right, bottom); 108 } rightTop()109 Point rightTop() const { 110 return Point(right, top); 111 } leftBottom()112 Point leftBottom() const { 113 return Point(left, bottom); 114 } 115 // comparisons 116 inline bool operator == (const Rect& rhs) const { 117 return (left == rhs.left) && (top == rhs.top) && 118 (right == rhs.right) && (bottom == rhs.bottom); 119 } 120 inline bool operator != (const Rect& rhs) const { 121 return !operator == (rhs); 122 } 123 // operator < defines an order which allows to use rectangles in sorted 124 // vectors. 125 bool operator < (const Rect& rhs) const; 126 const Rect operator + (const Point& rhs) const; 127 const Rect operator - (const Point& rhs) const; 128 Rect& operator += (const Point& rhs) { 129 return offsetBy(rhs.x, rhs.y); 130 } 131 Rect& operator -= (const Point& rhs) { 132 return offsetBy(-rhs.x, -rhs.y); 133 } offsetToOrigin()134 Rect& offsetToOrigin() { 135 right -= left; 136 bottom -= top; 137 left = top = 0; 138 return *this; 139 } offsetTo(const Point & p)140 Rect& offsetTo(const Point& p) { 141 return offsetTo(p.x, p.y); 142 } offsetBy(const Point & dp)143 Rect& offsetBy(const Point& dp) { 144 return offsetBy(dp.x, dp.y); 145 } 146 Rect& offsetTo(int32_t x, int32_t y); 147 Rect& offsetBy(int32_t x, int32_t y); 148 bool intersect(const Rect& with, Rect* result) const; 149 // Create a new Rect by transforming this one using a graphics HAL 150 // transform. This rectangle is defined in a coordinate space starting at 151 // the origin and extending to (width, height). If the transform includes 152 // a ROT90 then the output rectangle is defined in a space extending to 153 // (height, width). Otherwise the output rectangle is in the same space as 154 // the input. 155 Rect transform(uint32_t xform, int32_t width, int32_t height) const; 156 // this calculates (Region(*this) - exclude).bounds() efficiently 157 Rect reduce(const Rect& exclude) const; 158 // for backward compatibility width()159 inline int32_t width() const { return getWidth(); } height()160 inline int32_t height() const { return getHeight(); } set(const Rect & rhs)161 inline void set(const Rect& rhs) { operator = (rhs); } toFloatRect()162 FloatRect toFloatRect() const { 163 return {static_cast<float>(left), static_cast<float>(top), 164 static_cast<float>(right), static_cast<float>(bottom)}; 165 } 166 }; 167 168 ANDROID_BASIC_TYPES_TRAITS(Rect) 169 }; // namespace android 170 #endif // ANDROID_UI_RECT 171