| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#pragma once |
| 18 |
|
| 19 |
#include "Vertex.h" |
| 20 |
|
| 21 |
#include <utils/Log.h> |
| 22 |
|
| 23 |
#include <SkRect.h> |
| 24 |
#include <algorithm> |
| 25 |
#include <cmath> |
| 26 |
#include <iomanip> |
| 27 |
#include <ostream> |
| 28 |
|
| 29 |
namespace android { |
| 30 |
namespace uirenderer { |
| 31 |
|
| 32 |
#define RECT_STRING "%5.2f %5.2f %5.2f %5.2f" |
| 33 |
#define RECT_ARGS(r) (r).left, (r).top, (r).right, (r).bottom |
| 34 |
#define SK_RECT_ARGS(r) (r).left(), (r).top(), (r).right(), (r).bottom() |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class Rect { |
| 41 |
public: |
| 42 |
float left; |
| 43 |
float top; |
| 44 |
float right; |
| 45 |
float bottom; |
| 46 |
|
| 47 |
|
| 48 |
typedef float value_type; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
inline Rect() : left(0), top(0), right(0), bottom(0) {} |
| 54 |
|
| 55 |
inline Rect(float left, float top, float right, float bottom) |
| 56 |
: left(left), top(top), right(right), bottom(bottom) {} |
| 57 |
|
| 58 |
inline Rect(float width, float height) : left(0.0f), top(0.0f), right(width), bottom(height) {} |
| 59 |
|
| 60 |
inline Rect(const SkIRect& rect) |
| 61 |
: |
| 62 |
left(rect.fLeft) |
| 63 |
, top(rect.fTop) |
| 64 |
, right(rect.fRight) |
| 65 |
, bottom(rect.fBottom) {} |
| 66 |
|
| 67 |
inline Rect(const SkRect& rect) |
| 68 |
: |
| 69 |
left(rect.fLeft) |
| 70 |
, top(rect.fTop) |
| 71 |
, right(rect.fRight) |
| 72 |
, bottom(rect.fBottom) {} |
| 73 |
|
| 74 |
friend int operator==(const Rect& a, const Rect& b) { return !memcmp(&a, &b, sizeof(a)); } |
| 75 |
|
| 76 |
friend int operator!=(const Rect& a, const Rect& b) { return memcmp(&a, &b, sizeof(a)); } |
| 77 |
|
| 78 |
inline void clear() { left = top = right = bottom = 0.0f; } |
| 79 |
|
| 80 |
inline bool isEmpty() const { |
| 81 |
|
| 82 |
|
| 83 |
return !((left < right) && (top < bottom)); |
| 84 |
} |
| 85 |
|
| 86 |
inline void setEmpty() { left = top = right = bottom = 0.0f; } |
| 87 |
|
| 88 |
inline void set(float left, float top, float right, float bottom) { |
| 89 |
this->left = left; |
| 90 |
this->right = right; |
| 91 |
this->top = top; |
| 92 |
this->bottom = bottom; |
| 93 |
} |
| 94 |
|
| 95 |
inline void set(const Rect& r) { set(r.left, r.top, r.right, r.bottom); } |
| 96 |
|
| 97 |
inline void set(const SkIRect& r) { set(r.left(), r.top(), r.right(), r.bottom()); } |
| 98 |
|
| 99 |
inline float getWidth() const { return right - left; } |
| 100 |
|
| 101 |
inline float getHeight() const { return bottom - top; } |
| 102 |
|
| 103 |
bool intersects(float l, float t, float r, float b) const { |
| 104 |
float tempLeft = std::max(left, l); |
| 105 |
float tempTop = std::max(top, t); |
| 106 |
float tempRight = std::min(right, r); |
| 107 |
float tempBottom = std::min(bottom, b); |
| 108 |
|
| 109 |
return ((tempLeft < tempRight) && (tempTop < tempBottom)); |
| 110 |
} |
| 111 |
|
| 112 |
bool intersects(const Rect& r) const { return intersects(r.left, r.top, r.right, r.bottom); } |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
void doIntersect(float l, float t, float r, float b) { |
| 120 |
left = std::max(left, l); |
| 121 |
top = std::max(top, t); |
| 122 |
right = std::min(right, r); |
| 123 |
bottom = std::min(bottom, b); |
| 124 |
} |
| 125 |
|
| 126 |
void doIntersect(const Rect& r) { doIntersect(r.left, r.top, r.right, r.bottom); } |
| 127 |
|
| 128 |
inline bool contains(float l, float t, float r, float b) const { |
| 129 |
return l >= left && t >= top && r <= right && b <= bottom; |
| 130 |
} |
| 131 |
|
| 132 |
inline bool contains(const Rect& r) const { return contains(r.left, r.top, r.right, r.bottom); } |
| 133 |
|
| 134 |
bool unionWith(const Rect& r) { |
| 135 |
if (r.left < r.right && r.top < r.bottom) { |
| 136 |
if (left < right && top < bottom) { |
| 137 |
if (left > r.left) left = r.left; |
| 138 |
if (top > r.top) top = r.top; |
| 139 |
if (right < r.right) right = r.right; |
| 140 |
if (bottom < r.bottom) bottom = r.bottom; |
| 141 |
return true; |
| 142 |
} else { |
| 143 |
left = r.left; |
| 144 |
top = r.top; |
| 145 |
right = r.right; |
| 146 |
bottom = r.bottom; |
| 147 |
return true; |
| 148 |
} |
| 149 |
} |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
void translate(float dx, float dy) { |
| 154 |
left += dx; |
| 155 |
right += dx; |
| 156 |
top += dy; |
| 157 |
bottom += dy; |
| 158 |
} |
| 159 |
|
| 160 |
void inset(float delta) { outset(-delta); } |
| 161 |
|
| 162 |
void outset(float delta) { |
| 163 |
left -= delta; |
| 164 |
top -= delta; |
| 165 |
right += delta; |
| 166 |
bottom += delta; |
| 167 |
} |
| 168 |
|
| 169 |
void outset(float xdelta, float ydelta) { |
| 170 |
left -= xdelta; |
| 171 |
top -= ydelta; |
| 172 |
right += xdelta; |
| 173 |
bottom += ydelta; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
void snapGeometryToPixelBoundaries(bool snapOut) { |
| 184 |
if (snapOut) { |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
left = floorf(left + Vertex::GeometryFudgeFactor()); |
| 195 |
top = floorf(top + Vertex::GeometryFudgeFactor()); |
| 196 |
right = ceilf(right - Vertex::GeometryFudgeFactor()); |
| 197 |
bottom = ceilf(bottom - Vertex::GeometryFudgeFactor()); |
| 198 |
} else { |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
left = floorf(left + 0.5f - Vertex::GeometryFudgeFactor()); |
| 204 |
top = floorf(top + 0.5f - Vertex::GeometryFudgeFactor()); |
| 205 |
right = floorf(right + 0.5f + Vertex::GeometryFudgeFactor()); |
| 206 |
bottom = floorf(bottom + 0.5f + Vertex::GeometryFudgeFactor()); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
void snapToPixelBoundaries() { |
| 211 |
left = floorf(left + 0.5f); |
| 212 |
top = floorf(top + 0.5f); |
| 213 |
right = floorf(right + 0.5f); |
| 214 |
bottom = floorf(bottom + 0.5f); |
| 215 |
} |
| 216 |
|
| 217 |
void roundOut() { |
| 218 |
left = floorf(left); |
| 219 |
top = floorf(top); |
| 220 |
right = ceilf(right); |
| 221 |
bottom = ceilf(bottom); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
void expandToCover(const Rect& other) { |
| 229 |
left = std::min(left, other.left); |
| 230 |
top = std::min(top, other.top); |
| 231 |
right = std::max(right, other.right); |
| 232 |
bottom = std::max(bottom, other.bottom); |
| 233 |
} |
| 234 |
|
| 235 |
void expandToCover(float x, float y) { |
| 236 |
left = std::min(left, x); |
| 237 |
top = std::min(top, y); |
| 238 |
right = std::max(right, x); |
| 239 |
bottom = std::max(bottom, y); |
| 240 |
} |
| 241 |
|
| 242 |
SkRect toSkRect() const { return SkRect::MakeLTRB(left, top, right, bottom); } |
| 243 |
|
| 244 |
SkIRect toSkIRect() const { return SkIRect::MakeLTRB(left, top, right, bottom); } |
| 245 |
|
| 246 |
void dump(const char* label = nullptr) const { |
| 247 |
ALOGD("%s[l=%.2f t=%.2f r=%.2f b=%.2f]", label ? label : "Rect", left, top, right, bottom); |
| 248 |
} |
| 249 |
|
| 250 |
friend std::ostream& operator<<(std::ostream& os, const Rect& rect) { |
| 251 |
if (rect.isEmpty()) { |
| 252 |
|
| 253 |
os << "(empty)"; |
| 254 |
} |
| 255 |
|
| 256 |
if (rect.left == 0 && rect.top == 0) { |
| 257 |
return os << "[" << rect.right << " x " << rect.bottom << "]"; |
| 258 |
} |
| 259 |
|
| 260 |
return os << "[" << rect.left << " " << rect.top << " " << rect.right << " " << rect.bottom |
| 261 |
<< "]"; |
| 262 |
} |
| 263 |
}; |
| 264 |
|
| 265 |
} |
| 266 |
} |