Rect.h revision af6f7ed8dd4288a41d0a07a1f0f0be7d6d035b33
1/* 2 * Copyright (C) 2010 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#ifndef ANDROID_HWUI_RECT_H 18#define ANDROID_HWUI_RECT_H 19 20#include <cmath> 21#include <SkRect.h> 22 23#include <utils/Log.h> 24 25#include "Vertex.h" 26 27namespace android { 28namespace uirenderer { 29 30#define RECT_STRING "%7.2f %7.2f %7.2f %7.2f" 31#define RECT_ARGS(r) \ 32 (r).left, (r).top, (r).right, (r).bottom 33 34/////////////////////////////////////////////////////////////////////////////// 35// Structs 36/////////////////////////////////////////////////////////////////////////////// 37 38class Rect { 39public: 40 float left; 41 float top; 42 float right; 43 float bottom; 44 45 // Used by Region 46 typedef float value_type; 47 48 // we don't provide copy-ctor and operator= on purpose 49 // because we want the compiler generated versions 50 51 inline Rect(): 52 left(0), 53 top(0), 54 right(0), 55 bottom(0) { 56 } 57 58 inline Rect(float left, float top, float right, float bottom): 59 left(left), 60 top(top), 61 right(right), 62 bottom(bottom) { 63 } 64 65 inline Rect(float width, float height): 66 left(0.0f), 67 top(0.0f), 68 right(width), 69 bottom(height) { 70 } 71 72 inline Rect(const SkRect& rect): 73 left(rect.fLeft), 74 top(rect.fTop), 75 right(rect.fRight), 76 bottom(rect.fBottom) { 77 } 78 79 friend int operator==(const Rect& a, const Rect& b) { 80 return !memcmp(&a, &b, sizeof(a)); 81 } 82 83 friend int operator!=(const Rect& a, const Rect& b) { 84 return memcmp(&a, &b, sizeof(a)); 85 } 86 87 inline void clear() { 88 left = top = right = bottom = 0.0f; 89 } 90 91 inline bool isEmpty() const { 92 // this is written in such way this it'll handle NANs to return 93 // true (empty) 94 return !((left < right) && (top < bottom)); 95 } 96 97 inline void setEmpty() { 98 left = top = right = bottom = 0.0f; 99 } 100 101 inline void set(float left, float top, float right, float bottom) { 102 this->left = left; 103 this->right = right; 104 this->top = top; 105 this->bottom = bottom; 106 } 107 108 inline void set(const Rect& r) { 109 set(r.left, r.top, r.right, r.bottom); 110 } 111 112 inline float getWidth() const { 113 return right - left; 114 } 115 116 inline float getHeight() const { 117 return bottom - top; 118 } 119 120 bool intersects(float l, float t, float r, float b) const { 121 return !intersectWith(l, t, r, b).isEmpty(); 122 } 123 124 bool intersects(const Rect& r) const { 125 return intersects(r.left, r.top, r.right, r.bottom); 126 } 127 128 bool intersect(float l, float t, float r, float b) { 129 Rect tmp(l, t, r, b); 130 intersectWith(tmp); 131 if (!tmp.isEmpty()) { 132 set(tmp); 133 return true; 134 } 135 return false; 136 } 137 138 bool intersect(const Rect& r) { 139 return intersect(r.left, r.top, r.right, r.bottom); 140 } 141 142 inline bool contains(float l, float t, float r, float b) const { 143 return l >= left && t >= top && r <= right && b <= bottom; 144 } 145 146 inline bool contains(const Rect& r) const { 147 return contains(r.left, r.top, r.right, r.bottom); 148 } 149 150 bool unionWith(const Rect& r) { 151 if (r.left < r.right && r.top < r.bottom) { 152 if (left < right && top < bottom) { 153 if (left > r.left) left = r.left; 154 if (top > r.top) top = r.top; 155 if (right < r.right) right = r.right; 156 if (bottom < r.bottom) bottom = r.bottom; 157 return true; 158 } else { 159 left = r.left; 160 top = r.top; 161 right = r.right; 162 bottom = r.bottom; 163 return true; 164 } 165 } 166 return false; 167 } 168 169 void translate(float dx, float dy) { 170 left += dx; 171 right += dx; 172 top += dy; 173 bottom += dy; 174 } 175 176 void outset(float delta) { 177 left -= delta; 178 top -= delta; 179 right += delta; 180 bottom += delta; 181 } 182 183 /** 184 * Similar to snapToPixelBoundaries, but estimates bounds conservatively to handle GL rounding 185 * errors. 186 * 187 * This function should be used whenever estimating the damage rect of geometry already mapped 188 * into layer space. 189 */ 190 void snapGeometryToPixelBoundaries(bool snapOut) { 191 if (snapOut) { 192 /* For AA geometry with a ramp perimeter, don't snap by rounding - AA geometry will have 193 * a 0.5 pixel perimeter not accounted for in its bounds. Instead, snap by 194 * conservatively rounding out the bounds with floor/ceil. 195 * 196 * In order to avoid changing integer bounds with floor/ceil due to rounding errors 197 * inset the bounds first by the fudge factor. Very small fraction-of-a-pixel errors 198 * from this inset will only incur similarly small errors in output, due to transparency 199 * in extreme outside of the geometry. 200 */ 201 left = floorf(left + Vertex::GeometryFudgeFactor()); 202 top = floorf(top + Vertex::GeometryFudgeFactor()); 203 right = ceilf(right - Vertex::GeometryFudgeFactor()); 204 bottom = ceilf(bottom - Vertex::GeometryFudgeFactor()); 205 } else { 206 /* For other geometry, we do the regular rounding in order to snap, but also outset the 207 * bounds by a fudge factor. This ensures that ambiguous geometry (e.g. a non-AA Rect 208 * with top left at (0.5, 0.5)) will err on the side of a larger damage rect. 209 */ 210 left = floorf(left + 0.5f - Vertex::GeometryFudgeFactor()); 211 top = floorf(top + 0.5f - Vertex::GeometryFudgeFactor()); 212 right = floorf(right + 0.5f + Vertex::GeometryFudgeFactor()); 213 bottom = floorf(bottom + 0.5f + Vertex::GeometryFudgeFactor()); 214 } 215 } 216 217 void snapToPixelBoundaries() { 218 left = floorf(left + 0.5f); 219 top = floorf(top + 0.5f); 220 right = floorf(right + 0.5f); 221 bottom = floorf(bottom + 0.5f); 222 } 223 224 void roundOut() { 225 left = floorf(left); 226 top = floorf(top); 227 right = ceilf(right); 228 bottom = ceilf(bottom); 229 } 230 231 void dump() const { 232 ALOGD("Rect[l=%f t=%f r=%f b=%f]", left, top, right, bottom); 233 } 234 235private: 236 void intersectWith(Rect& tmp) const { 237 tmp.left = fmaxf(left, tmp.left); 238 tmp.top = fmaxf(top, tmp.top); 239 tmp.right = fminf(right, tmp.right); 240 tmp.bottom = fminf(bottom, tmp.bottom); 241 } 242 243 Rect intersectWith(float l, float t, float r, float b) const { 244 Rect tmp; 245 tmp.left = fmaxf(left, l); 246 tmp.top = fmaxf(top, t); 247 tmp.right = fminf(right, r); 248 tmp.bottom = fminf(bottom, b); 249 return tmp; 250 } 251 252}; // class Rect 253 254}; // namespace uirenderer 255}; // namespace android 256 257#endif // ANDROID_HWUI_RECT_H 258