| 1 |
/* |
| 2 |
* Copyright 2012 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/core/SkRRect.h" |
| 9 |
|
| 10 |
#include "include/core/SkMatrix.h" |
| 11 |
#include "include/core/SkPoint.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkScalar.h" |
| 14 |
#include "include/core/SkString.h" |
| 15 |
#include "include/core/SkTypes.h" |
| 16 |
#include "include/private/base/SkDebug.h" |
| 17 |
#include "include/private/base/SkFloatingPoint.h" |
| 18 |
#include "src/base/SkBuffer.h" |
| 19 |
#include "src/core/SkPathPriv.h" |
| 20 |
#include "src/core/SkPathRawShapes.h" |
| 21 |
#include "src/core/SkRRectPriv.h" |
| 22 |
#include "src/core/SkRectPriv.h" |
| 23 |
#include "src/core/SkScaleToSides.h" |
| 24 |
#include "src/core/SkStringUtils.h" |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
#include <cstring> |
| 28 |
#include <iterator> |
| 29 |
|
| 30 |
/////////////////////////////////////////////////////////////////////////////// |
| 31 |
|
| 32 |
void SkRRect::setOval(const SkRect& oval) { |
| 33 |
if (!this->initializeRect(oval)) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
SkScalar xRad = SkRectPriv::HalfWidth(fRect); |
| 38 |
SkScalar yRad = SkRectPriv::HalfHeight(fRect); |
| 39 |
|
| 40 |
if (xRad == 0.0f || yRad == 0.0f) { |
| 41 |
// All the corners will be square |
| 42 |
memset(fRadii, 0, sizeof(fRadii)); |
| 43 |
fType = kRect_Type; |
| 44 |
} else { |
| 45 |
for (int i = 0; i < 4; ++i) { |
| 46 |
fRadii[i].set(xRad, yRad); |
| 47 |
} |
| 48 |
fType = kOval_Type; |
| 49 |
} |
| 50 |
|
| 51 |
SkASSERT(this->isValid()); |
| 52 |
} |
| 53 |
|
| 54 |
void SkRRect::setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad) { |
| 55 |
if (!this->initializeRect(rect)) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
if (!SkIsFinite(xRad, yRad)) { |
| 60 |
xRad = yRad = 0; // devolve into a simple rect |
| 61 |
} |
| 62 |
|
| 63 |
if (fRect.width() < xRad+xRad || fRect.height() < yRad+yRad) { |
| 64 |
// At most one of these two divides will be by zero, and neither numerator is zero. |
| 65 |
SkScalar scale = std::min(sk_ieee_float_divide(fRect. width(), xRad + xRad), |
| 66 |
sk_ieee_float_divide(fRect.height(), yRad + yRad)); |
| 67 |
SkASSERT(scale < SK_Scalar1); |
| 68 |
xRad *= scale; |
| 69 |
yRad *= scale; |
| 70 |
} |
| 71 |
|
| 72 |
if (xRad <= 0 || yRad <= 0) { |
| 73 |
// all corners are square in this case |
| 74 |
this->setRect(rect); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
for (int i = 0; i < 4; ++i) { |
| 79 |
fRadii[i].set(xRad, yRad); |
| 80 |
} |
| 81 |
fType = kSimple_Type; |
| 82 |
if (xRad >= SkScalarHalf(fRect.width()) && yRad >= SkScalarHalf(fRect.height())) { |
| 83 |
fType = kOval_Type; |
| 84 |
// TODO: assert that all the x&y radii are already W/2 & H/2 |
| 85 |
} |
| 86 |
|
| 87 |
SkASSERT(this->isValid()); |
| 88 |
} |
| 89 |
|
| 90 |
static bool clamp_to_zero(SkVector radii[4]) { |
| 91 |
bool allCornersSquare = true; |
| 92 |
|
| 93 |
// Clamp negative radii to zero |
| 94 |
for (int i = 0; i < 4; ++i) { |
| 95 |
if (radii[i].fX <= 0 || radii[i].fY <= 0) { |
| 96 |
// In this case we are being a little fast & loose. Since one of |
| 97 |
// the radii is 0 the corner is square. However, the other radii |
| 98 |
// could still be non-zero and play in the global scale factor |
| 99 |
// computation. |
| 100 |
radii[i].fX = 0; |
| 101 |
radii[i].fY = 0; |
| 102 |
} else { |
| 103 |
allCornersSquare = false; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return allCornersSquare; |
| 108 |
} |
| 109 |
|
| 110 |
static bool radii_are_nine_patch(const SkVector radii[4]) { |
| 111 |
return radii[SkRRect::kUpperLeft_Corner].fX == radii[SkRRect::kLowerLeft_Corner].fX && |
| 112 |
radii[SkRRect::kUpperLeft_Corner].fY == radii[SkRRect::kUpperRight_Corner].fY && |
| 113 |
radii[SkRRect::kUpperRight_Corner].fX == radii[SkRRect::kLowerRight_Corner].fX && |
| 114 |
radii[SkRRect::kLowerLeft_Corner].fY == radii[SkRRect::kLowerRight_Corner].fY; |
| 115 |
} |
| 116 |
|
| 117 |
void SkRRect::setNinePatch(const SkRect& rect, SkScalar leftRad, SkScalar topRad, |
| 118 |
SkScalar rightRad, SkScalar bottomRad) { |
| 119 |
if (!this->initializeRect(rect)) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
if (!SkIsFinite(leftRad, topRad, rightRad, bottomRad)) { |
| 124 |
this->setRect(rect); // devolve into a simple rect |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
leftRad = std::max(leftRad, 0.0f); |
| 129 |
topRad = std::max(topRad, 0.0f); |
| 130 |
rightRad = std::max(rightRad, 0.0f); |
| 131 |
bottomRad = std::max(bottomRad, 0.0f); |
| 132 |
|
| 133 |
SkScalar scale = SK_Scalar1; |
| 134 |
if (leftRad + rightRad > fRect.width()) { |
| 135 |
scale = fRect.width() / (leftRad + rightRad); |
| 136 |
} |
| 137 |
if (topRad + bottomRad > fRect.height()) { |
| 138 |
scale = std::min(scale, fRect.height() / (topRad + bottomRad)); |
| 139 |
} |
| 140 |
|
| 141 |
if (scale < SK_Scalar1) { |
| 142 |
leftRad *= scale; |
| 143 |
topRad *= scale; |
| 144 |
rightRad *= scale; |
| 145 |
bottomRad *= scale; |
| 146 |
} |
| 147 |
|
| 148 |
if (leftRad == rightRad && topRad == bottomRad) { |
| 149 |
if (leftRad >= SkScalarHalf(fRect.width()) && topRad >= SkScalarHalf(fRect.height())) { |
| 150 |
fType = kOval_Type; |
| 151 |
} else if (0 == leftRad || 0 == topRad) { |
| 152 |
// If the left and (by equality check above) right radii are zero then it is a rect. |
| 153 |
// Same goes for top/bottom. |
| 154 |
fType = kRect_Type; |
| 155 |
leftRad = 0; |
| 156 |
topRad = 0; |
| 157 |
rightRad = 0; |
| 158 |
bottomRad = 0; |
| 159 |
} else { |
| 160 |
fType = kSimple_Type; |
| 161 |
} |
| 162 |
} else { |
| 163 |
fType = kNinePatch_Type; |
| 164 |
} |
| 165 |
|
| 166 |
fRadii[kUpperLeft_Corner].set(leftRad, topRad); |
| 167 |
fRadii[kUpperRight_Corner].set(rightRad, topRad); |
| 168 |
fRadii[kLowerRight_Corner].set(rightRad, bottomRad); |
| 169 |
fRadii[kLowerLeft_Corner].set(leftRad, bottomRad); |
| 170 |
if (clamp_to_zero(fRadii)) { |
| 171 |
this->setRect(rect); // devolve into a simple rect |
| 172 |
return; |
| 173 |
} |
| 174 |
if (fType == kNinePatch_Type && !radii_are_nine_patch(fRadii)) { |
| 175 |
fType = kComplex_Type; |
| 176 |
} |
| 177 |
|
| 178 |
SkASSERT(this->isValid()); |
| 179 |
} |
| 180 |
|
| 181 |
// These parameters intentionally double. Apropos crbug.com/463920, if one of the |
| 182 |
// radii is huge while the other is small, single precision math can completely |
| 183 |
// miss the fact that a scale is required. |
| 184 |
static double compute_min_scale(double rad1, double rad2, double limit, double curMin) { |
| 185 |
if ((rad1 + rad2) > limit) { |
| 186 |
return std::min(curMin, limit / (rad1 + rad2)); |
| 187 |
} |
| 188 |
return curMin; |
| 189 |
} |
| 190 |
|
| 191 |
void SkRRect::setRectRadii(const SkRect& rect, const SkVector radii[4]) { |
| 192 |
if (!this->initializeRect(rect)) { |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
if (!SkIsFinite(&radii[0].fX, 8)) { |
| 197 |
this->setRect(rect); // devolve into a simple rect |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
memcpy(fRadii, radii, sizeof(fRadii)); |
| 202 |
|
| 203 |
if (clamp_to_zero(fRadii)) { |
| 204 |
this->setRect(rect); |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
this->scaleRadii(); |
| 209 |
|
| 210 |
if (!this->isValid()) { |
| 211 |
this->setRect(rect); |
| 212 |
return; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
bool SkRRect::initializeRect(const SkRect& rect) { |
| 217 |
// Check this before sorting because sorting can hide nans. |
| 218 |
if (!rect.isFinite()) { |
| 219 |
*this = SkRRect(); |
| 220 |
return false; |
| 221 |
} |
| 222 |
fRect = rect.makeSorted(); |
| 223 |
if (fRect.isEmpty()) { |
| 224 |
memset(fRadii, 0, sizeof(fRadii)); |
| 225 |
fType = kEmpty_Type; |
| 226 |
return false; |
| 227 |
} |
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
// If we can't distinguish one of the radii relative to the other, force it to zero so it |
| 232 |
// doesn't confuse us later. See crbug.com/850350 |
| 233 |
// |
| 234 |
static void flush_to_zero(SkScalar& a, SkScalar& b) { |
| 235 |
SkASSERT(a >= 0); |
| 236 |
SkASSERT(b >= 0); |
| 237 |
if (a + b == a) { |
| 238 |
b = 0; |
| 239 |
} else if (a + b == b) { |
| 240 |
a = 0; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
bool SkRRect::scaleRadii() { |
| 245 |
// Proportionally scale down all radii to fit. Find the minimum ratio |
| 246 |
// of a side and the radii on that side (for all four sides) and use |
| 247 |
// that to scale down _all_ the radii. This algorithm is from the |
| 248 |
// W3 spec (http://www.w3.org/TR/css3-background/) section 5.5 - Overlapping |
| 249 |
// Curves: |
| 250 |
// "Let f = min(Li/Si), where i is one of { top, right, bottom, left }, |
| 251 |
// Si is the sum of the two corresponding radii of the corners on side i, |
| 252 |
// and Ltop = Lbottom = the width of the box, |
| 253 |
// and Lleft = Lright = the height of the box. |
| 254 |
// If f < 1, then all corner radii are reduced by multiplying them by f." |
| 255 |
double scale = 1.0; |
| 256 |
|
| 257 |
// The sides of the rectangle may be larger than a float. |
| 258 |
double width = (double)fRect.fRight - (double)fRect.fLeft; |
| 259 |
double height = (double)fRect.fBottom - (double)fRect.fTop; |
| 260 |
scale = compute_min_scale(fRadii[0].fX, fRadii[1].fX, width, scale); |
| 261 |
scale = compute_min_scale(fRadii[1].fY, fRadii[2].fY, height, scale); |
| 262 |
scale = compute_min_scale(fRadii[2].fX, fRadii[3].fX, width, scale); |
| 263 |
scale = compute_min_scale(fRadii[3].fY, fRadii[0].fY, height, scale); |
| 264 |
|
| 265 |
flush_to_zero(fRadii[0].fX, fRadii[1].fX); |
| 266 |
flush_to_zero(fRadii[1].fY, fRadii[2].fY); |
| 267 |
flush_to_zero(fRadii[2].fX, fRadii[3].fX); |
| 268 |
flush_to_zero(fRadii[3].fY, fRadii[0].fY); |
| 269 |
|
| 270 |
if (scale < 1.0) { |
| 271 |
SkScaleToSides::AdjustRadii(width, scale, &fRadii[0].fX, &fRadii[1].fX); |
| 272 |
SkScaleToSides::AdjustRadii(height, scale, &fRadii[1].fY, &fRadii[2].fY); |
| 273 |
SkScaleToSides::AdjustRadii(width, scale, &fRadii[2].fX, &fRadii[3].fX); |
| 274 |
SkScaleToSides::AdjustRadii(height, scale, &fRadii[3].fY, &fRadii[0].fY); |
| 275 |
} |
| 276 |
|
| 277 |
// adjust radii may set x or y to zero; set companion to zero as well |
| 278 |
clamp_to_zero(fRadii); |
| 279 |
|
| 280 |
// May be simple, oval, or complex, or become a rect/empty if the radii adjustment made them 0 |
| 281 |
this->computeType(); |
| 282 |
|
| 283 |
// TODO: Why can't we assert this here? |
| 284 |
//SkASSERT(this->isValid()); |
| 285 |
|
| 286 |
return scale < 1.0; |
| 287 |
} |
| 288 |
|
| 289 |
// This method determines if a point known to be inside the RRect's bounds is |
| 290 |
// inside all the corners. |
| 291 |
bool SkRRect::checkCornerContainment(SkScalar x, SkScalar y) const { |
| 292 |
SkPoint canonicalPt; // (x,y) translated to one of the quadrants |
| 293 |
int index; |
| 294 |
|
| 295 |
if (kOval_Type == this->type()) { |
| 296 |
canonicalPt.set(x - fRect.centerX(), y - fRect.centerY()); |
| 297 |
index = kUpperLeft_Corner; // any corner will do in this case |
| 298 |
} else { |
| 299 |
if (x < fRect.fLeft + fRadii[kUpperLeft_Corner].fX && |
| 300 |
y < fRect.fTop + fRadii[kUpperLeft_Corner].fY) { |
| 301 |
// UL corner |
| 302 |
index = kUpperLeft_Corner; |
| 303 |
canonicalPt.set(x - (fRect.fLeft + fRadii[kUpperLeft_Corner].fX), |
| 304 |
y - (fRect.fTop + fRadii[kUpperLeft_Corner].fY)); |
| 305 |
SkASSERT(canonicalPt.fX < 0 && canonicalPt.fY < 0); |
| 306 |
} else if (x < fRect.fLeft + fRadii[kLowerLeft_Corner].fX && |
| 307 |
y > fRect.fBottom - fRadii[kLowerLeft_Corner].fY) { |
| 308 |
// LL corner |
| 309 |
index = kLowerLeft_Corner; |
| 310 |
canonicalPt.set(x - (fRect.fLeft + fRadii[kLowerLeft_Corner].fX), |
| 311 |
y - (fRect.fBottom - fRadii[kLowerLeft_Corner].fY)); |
| 312 |
SkASSERT(canonicalPt.fX < 0 && canonicalPt.fY > 0); |
| 313 |
} else if (x > fRect.fRight - fRadii[kUpperRight_Corner].fX && |
| 314 |
y < fRect.fTop + fRadii[kUpperRight_Corner].fY) { |
| 315 |
// UR corner |
| 316 |
index = kUpperRight_Corner; |
| 317 |
canonicalPt.set(x - (fRect.fRight - fRadii[kUpperRight_Corner].fX), |
| 318 |
y - (fRect.fTop + fRadii[kUpperRight_Corner].fY)); |
| 319 |
SkASSERT(canonicalPt.fX > 0 && canonicalPt.fY < 0); |
| 320 |
} else if (x > fRect.fRight - fRadii[kLowerRight_Corner].fX && |
| 321 |
y > fRect.fBottom - fRadii[kLowerRight_Corner].fY) { |
| 322 |
// LR corner |
| 323 |
index = kLowerRight_Corner; |
| 324 |
canonicalPt.set(x - (fRect.fRight - fRadii[kLowerRight_Corner].fX), |
| 325 |
y - (fRect.fBottom - fRadii[kLowerRight_Corner].fY)); |
| 326 |
SkASSERT(canonicalPt.fX > 0 && canonicalPt.fY > 0); |
| 327 |
} else { |
| 328 |
// not in any of the corners |
| 329 |
return true; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
// A point is in an ellipse (in standard position) if: |
| 334 |
// x^2 y^2 |
| 335 |
// ----- + ----- <= 1 |
| 336 |
// a^2 b^2 |
| 337 |
// or : |
| 338 |
// b^2*x^2 + a^2*y^2 <= (ab)^2 |
| 339 |
float dist = SkScalarSquare(canonicalPt.fX) * SkScalarSquare(fRadii[index].fY) + |
| 340 |
SkScalarSquare(canonicalPt.fY) * SkScalarSquare(fRadii[index].fX); |
| 341 |
return dist <= SkScalarSquare(fRadii[index].fX * fRadii[index].fY); |
| 342 |
} |
| 343 |
|
| 344 |
bool SkRRectPriv::IsNearlySimpleCircular(const SkRRect& rr, float tolerance) { |
| 345 |
const float simpleRadius = rr.fRadii[0].fX; |
| 346 |
return SkScalarNearlyEqual(simpleRadius, rr.fRadii[0].fY, tolerance) && |
| 347 |
SkScalarNearlyEqual(simpleRadius, rr.fRadii[1].fX, tolerance) && |
| 348 |
SkScalarNearlyEqual(simpleRadius, rr.fRadii[1].fY, tolerance) && |
| 349 |
SkScalarNearlyEqual(simpleRadius, rr.fRadii[2].fX, tolerance) && |
| 350 |
SkScalarNearlyEqual(simpleRadius, rr.fRadii[2].fY, tolerance) && |
| 351 |
SkScalarNearlyEqual(simpleRadius, rr.fRadii[3].fX, tolerance) && |
| 352 |
SkScalarNearlyEqual(simpleRadius, rr.fRadii[3].fY, tolerance); |
| 353 |
} |
| 354 |
|
| 355 |
bool SkRRectPriv::AllCornersCircular(const SkRRect& rr, float tolerance) { |
| 356 |
return SkScalarNearlyEqual(rr.fRadii[0].fX, rr.fRadii[0].fY, tolerance) && |
| 357 |
SkScalarNearlyEqual(rr.fRadii[1].fX, rr.fRadii[1].fY, tolerance) && |
| 358 |
SkScalarNearlyEqual(rr.fRadii[2].fX, rr.fRadii[2].fY, tolerance) && |
| 359 |
SkScalarNearlyEqual(rr.fRadii[3].fX, rr.fRadii[3].fY, tolerance); |
| 360 |
} |
| 361 |
|
| 362 |
bool SkRRectPriv::IsRelativelyCircular(float rx, float ry, float tolerance) { |
| 363 |
// The ellipse is considered relatively circular if either `rx/ry` or `ry/rx` is within |
| 364 |
// `tolerance` of 1.0, but this is equivalent to comparing the absolute difference between |
| 365 |
// `rx` and `ry` to `tolerance` multiplied by the largest radii. |
| 366 |
return std::abs(rx - ry) <= tolerance * std::max(rx, ry); |
| 367 |
} |
| 368 |
|
| 369 |
bool SkRRectPriv::AllCornersRelativelyCircular(const SkRRect &rr, float tolerance) { |
| 370 |
return IsRelativelyCircular(rr.fRadii[0].fX, rr.fRadii[0].fY, tolerance) && |
| 371 |
IsRelativelyCircular(rr.fRadii[1].fX, rr.fRadii[1].fY, tolerance) && |
| 372 |
IsRelativelyCircular(rr.fRadii[2].fX, rr.fRadii[2].fY, tolerance) && |
| 373 |
IsRelativelyCircular(rr.fRadii[3].fX, rr.fRadii[3].fY, tolerance); |
| 374 |
} |
| 375 |
|
| 376 |
bool SkRRect::contains(const SkRect& rect) const { |
| 377 |
if (!this->getBounds().contains(rect)) { |
| 378 |
// If 'rect' isn't contained by the RR's bounds then the |
| 379 |
// RR definitely doesn't contain it |
| 380 |
return false; |
| 381 |
} |
| 382 |
|
| 383 |
if (this->isRect()) { |
| 384 |
// the prior test was sufficient |
| 385 |
return true; |
| 386 |
} |
| 387 |
|
| 388 |
// At this point we know all four corners of 'rect' are inside the |
| 389 |
// bounds of of this RR. Check to make sure all the corners are inside |
| 390 |
// all the curves |
| 391 |
return this->checkCornerContainment(rect.fLeft, rect.fTop) && |
| 392 |
this->checkCornerContainment(rect.fRight, rect.fTop) && |
| 393 |
this->checkCornerContainment(rect.fRight, rect.fBottom) && |
| 394 |
this->checkCornerContainment(rect.fLeft, rect.fBottom); |
| 395 |
} |
| 396 |
|
| 397 |
// There is a simplified version of this method in setRectXY |
| 398 |
void SkRRect::computeType() { |
| 399 |
if (fRect.isEmpty()) { |
| 400 |
SkASSERT(fRect.isSorted()); |
| 401 |
for (size_t i = 0; i < std::size(fRadii); ++i) { |
| 402 |
SkASSERT((fRadii[i] == SkVector{0, 0})); |
| 403 |
} |
| 404 |
fType = kEmpty_Type; |
| 405 |
SkASSERT(this->isValid()); |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
bool allRadiiEqual = true; // are all x radii equal and all y radii? |
| 410 |
bool allCornersSquare = 0 == fRadii[0].fX || 0 == fRadii[0].fY; |
| 411 |
|
| 412 |
for (int i = 1; i < 4; ++i) { |
| 413 |
if (0 != fRadii[i].fX && 0 != fRadii[i].fY) { |
| 414 |
// if either radius is zero the corner is square so both have to |
| 415 |
// be non-zero to have a rounded corner |
| 416 |
allCornersSquare = false; |
| 417 |
} |
| 418 |
if (fRadii[i].fX != fRadii[i-1].fX || fRadii[i].fY != fRadii[i-1].fY) { |
| 419 |
allRadiiEqual = false; |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
if (allCornersSquare) { |
| 424 |
fType = kRect_Type; |
| 425 |
SkASSERT(this->isValid()); |
| 426 |
return; |
| 427 |
} |
| 428 |
|
| 429 |
if (allRadiiEqual) { |
| 430 |
if (fRadii[0].fX >= SkScalarHalf(fRect.width()) && |
| 431 |
fRadii[0].fY >= SkScalarHalf(fRect.height())) { |
| 432 |
fType = kOval_Type; |
| 433 |
} else { |
| 434 |
fType = kSimple_Type; |
| 435 |
} |
| 436 |
SkASSERT(this->isValid()); |
| 437 |
return; |
| 438 |
} |
| 439 |
|
| 440 |
if (radii_are_nine_patch(fRadii)) { |
| 441 |
fType = kNinePatch_Type; |
| 442 |
} else { |
| 443 |
fType = kComplex_Type; |
| 444 |
} |
| 445 |
|
| 446 |
if (!this->isValid()) { |
| 447 |
this->setRect(this->rect()); |
| 448 |
SkASSERT(this->isValid()); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
std::optional<SkRRect> SkRRect::transform(const SkMatrix& matrix) const { |
| 453 |
// TODO(b/441005851): Resolve Android RenderEngine's shader prewarming regressions before removing. |
| 454 |
#ifdef SK_SUPPORT_LEGACY_RRECT_TRANSFORM |
| 455 |
SkRRect newrr; |
| 456 |
if (this->transform(matrix, &newrr)) { |
| 457 |
return newrr; |
| 458 |
} |
| 459 |
return {}; |
| 460 |
#else |
| 461 |
if (matrix.isIdentity()) { |
| 462 |
return *this; |
| 463 |
} |
| 464 |
|
| 465 |
if (!matrix.preservesAxisAlignment()) { |
| 466 |
return {}; |
| 467 |
} |
| 468 |
|
| 469 |
const SkRect newRect = matrix.mapRect(fRect); |
| 470 |
if (!newRect.isFinite()) { |
| 471 |
return {}; |
| 472 |
} |
| 473 |
|
| 474 |
switch (this->getType()) { |
| 475 |
case kEmpty_Type: return MakeEmpty(); |
| 476 |
case kRect_Type: return MakeRect(newRect); |
| 477 |
case kOval_Type: return MakeOval(newRect); |
| 478 |
default: |
| 479 |
break; |
| 480 |
} |
| 481 |
|
| 482 |
SkPathRawShapes::RRect raw(*this); |
| 483 |
matrix.mapPoints(raw.fStorage); |
| 484 |
return SkPathPriv::DeduceRRectFromContour(newRect, raw.fPoints, raw.fVerbs); |
| 485 |
#endif |
| 486 |
} |
| 487 |
|
| 488 |
bool SkRRect::transform(const SkMatrix& matrix, SkRRect* dst) const { |
| 489 |
// TODO(b/441005851): Resolve Android RenderEngine's shader prewarming regressions before removing. |
| 490 |
#ifdef SK_SUPPORT_LEGACY_RRECT_TRANSFORM |
| 491 |
if (nullptr == dst) { |
| 492 |
return false; |
| 493 |
} |
| 494 |
|
| 495 |
// Assert that the caller is not trying to do this in place, which |
| 496 |
// would violate const-ness. Do not return false though, so that |
| 497 |
// if they know what they're doing and want to violate it they can. |
| 498 |
SkASSERT(dst != this); |
| 499 |
|
| 500 |
if (matrix.isIdentity()) { |
| 501 |
*dst = *this; |
| 502 |
return true; |
| 503 |
} |
| 504 |
|
| 505 |
if (!matrix.preservesAxisAlignment()) { |
| 506 |
return false; |
| 507 |
} |
| 508 |
|
| 509 |
SkRect newRect; |
| 510 |
if (!matrix.mapRect(&newRect, fRect)) { |
| 511 |
return false; |
| 512 |
} |
| 513 |
|
| 514 |
// The matrix may have scaled us to zero (or due to float madness, we now have collapsed |
| 515 |
// some dimension of the rect, so we need to check for that. Note that matrix must be |
| 516 |
// scale and translate and mapRect() produces a sorted rect. So an empty rect indicates |
| 517 |
// loss of precision. |
| 518 |
if (!newRect.isFinite() || newRect.isEmpty()) { |
| 519 |
return false; |
| 520 |
} |
| 521 |
|
| 522 |
// At this point, this is guaranteed to succeed, so we can modify dst. |
| 523 |
dst->fRect = newRect; |
| 524 |
|
| 525 |
// Since the only transforms that were allowed are axis aligned, the type |
| 526 |
// remains unchanged. |
| 527 |
dst->fType = fType; |
| 528 |
|
| 529 |
if (kRect_Type == fType) { |
| 530 |
SkASSERT(dst->isValid()); |
| 531 |
return true; |
| 532 |
} |
| 533 |
if (kOval_Type == fType) { |
| 534 |
for (int i = 0; i < 4; ++i) { |
| 535 |
dst->fRadii[i].fX = SkScalarHalf(newRect.width()); |
| 536 |
dst->fRadii[i].fY = SkScalarHalf(newRect.height()); |
| 537 |
} |
| 538 |
SkASSERT(dst->isValid()); |
| 539 |
return true; |
| 540 |
} |
| 541 |
|
| 542 |
// Now scale each corner |
| 543 |
SkScalar xScale = matrix.getScaleX(); |
| 544 |
SkScalar yScale = matrix.getScaleY(); |
| 545 |
|
| 546 |
// There is a rotation of 90 (Clockwise 90) or 270 (Counter clockwise 90). |
| 547 |
// 180 degrees rotations are simply flipX with a flipY and would come under |
| 548 |
// a scale transform. |
| 549 |
if (!matrix.isScaleTranslate()) { |
| 550 |
// If we got here, the matrix preserves axis alignment (earlier return check) but isn't |
| 551 |
// a regular scale matrix. To confirm that it's a 90/270 rotation, the scale components are |
| 552 |
// 0s and the skew components are non-zero (+/-1 if there is no other scale factor). |
| 553 |
SkASSERT(matrix.getScaleX() == 0.f && matrix.getScaleY() == 0.f && |
| 554 |
matrix.getSkewX() != 0.f && matrix.getSkewY() != 0.f); |
| 555 |
const bool isClockwise = matrix.getSkewX() < 0; |
| 556 |
|
| 557 |
// The matrix location for scale changes if there is a rotation. |
| 558 |
// xScale and yScale represent scales applied to the dst radii, so we store the src x scale |
| 559 |
// in yScale and vice versa. |
| 560 |
yScale = matrix.getSkewY() * (isClockwise ? 1 : -1); |
| 561 |
xScale = matrix.getSkewX() * (isClockwise ? -1 : 1); |
| 562 |
|
| 563 |
const int dir = isClockwise ? 3 : 1; |
| 564 |
for (int i = 0; i < 4; ++i) { |
| 565 |
const int src = (i + dir) >= 4 ? (i + dir) % 4 : (i + dir); |
| 566 |
// Swap X and Y axis for the radii. |
| 567 |
dst->fRadii[i].fX = fRadii[src].fY; |
| 568 |
dst->fRadii[i].fY = fRadii[src].fX; |
| 569 |
} |
| 570 |
} else { |
| 571 |
for (int i = 0; i < 4; ++i) { |
| 572 |
dst->fRadii[i].fX = fRadii[i].fX; |
| 573 |
dst->fRadii[i].fY = fRadii[i].fY; |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
const bool flipX = xScale < 0; |
| 578 |
if (flipX) { |
| 579 |
xScale = -xScale; |
| 580 |
} |
| 581 |
|
| 582 |
const bool flipY = yScale < 0; |
| 583 |
if (flipY) { |
| 584 |
yScale = -yScale; |
| 585 |
} |
| 586 |
|
| 587 |
// Scale the radii without respecting the flip. |
| 588 |
for (int i = 0; i < 4; ++i) { |
| 589 |
dst->fRadii[i].fX *= xScale; |
| 590 |
dst->fRadii[i].fY *= yScale; |
| 591 |
} |
| 592 |
|
| 593 |
// Now swap as necessary. |
| 594 |
using std::swap; |
| 595 |
if (flipX) { |
| 596 |
if (flipY) { |
| 597 |
// Swap with opposite corners |
| 598 |
swap(dst->fRadii[kUpperLeft_Corner], dst->fRadii[kLowerRight_Corner]); |
| 599 |
swap(dst->fRadii[kUpperRight_Corner], dst->fRadii[kLowerLeft_Corner]); |
| 600 |
} else { |
| 601 |
// Only swap in x |
| 602 |
swap(dst->fRadii[kUpperRight_Corner], dst->fRadii[kUpperLeft_Corner]); |
| 603 |
swap(dst->fRadii[kLowerRight_Corner], dst->fRadii[kLowerLeft_Corner]); |
| 604 |
} |
| 605 |
} else if (flipY) { |
| 606 |
// Only swap in y |
| 607 |
swap(dst->fRadii[kUpperLeft_Corner], dst->fRadii[kLowerLeft_Corner]); |
| 608 |
swap(dst->fRadii[kUpperRight_Corner], dst->fRadii[kLowerRight_Corner]); |
| 609 |
} |
| 610 |
|
| 611 |
dst->scaleRadii(); |
| 612 |
|
| 613 |
if (!AreRectAndRadiiValid(dst->fRect, dst->fRadii)) { |
| 614 |
return false; |
| 615 |
} |
| 616 |
|
| 617 |
SkASSERT(dst->isValid()); |
| 618 |
return true; |
| 619 |
#else |
| 620 |
if (auto rr = this->transform(matrix)) { |
| 621 |
if (dst) { |
| 622 |
*dst = *rr; |
| 623 |
} |
| 624 |
return true; |
| 625 |
} |
| 626 |
return false; |
| 627 |
#endif |
| 628 |
} |
| 629 |
|
| 630 |
/////////////////////////////////////////////////////////////////////////////// |
| 631 |
|
| 632 |
void SkRRect::inset(SkScalar dx, SkScalar dy, SkRRect* dst) const { |
| 633 |
SkRect r = fRect.makeInset(dx, dy); |
| 634 |
bool degenerate = false; |
| 635 |
if (r.fRight <= r.fLeft) { |
| 636 |
degenerate = true; |
| 637 |
r.fLeft = r.fRight = sk_float_midpoint(r.fLeft, r.fRight); |
| 638 |
} |
| 639 |
if (r.fBottom <= r.fTop) { |
| 640 |
degenerate = true; |
| 641 |
r.fTop = r.fBottom = sk_float_midpoint(r.fTop, r.fBottom); |
| 642 |
} |
| 643 |
if (degenerate) { |
| 644 |
dst->fRect = r; |
| 645 |
memset(dst->fRadii, 0, sizeof(dst->fRadii)); |
| 646 |
dst->fType = kEmpty_Type; |
| 647 |
return; |
| 648 |
} |
| 649 |
if (!r.isFinite()) { |
| 650 |
*dst = SkRRect(); |
| 651 |
return; |
| 652 |
} |
| 653 |
|
| 654 |
SkVector radii[4]; |
| 655 |
memcpy(radii, fRadii, sizeof(radii)); |
| 656 |
for (int i = 0; i < 4; ++i) { |
| 657 |
if (radii[i].fX) { |
| 658 |
radii[i].fX -= dx; |
| 659 |
} |
| 660 |
if (radii[i].fY) { |
| 661 |
radii[i].fY -= dy; |
| 662 |
} |
| 663 |
} |
| 664 |
dst->setRectRadii(r, radii); |
| 665 |
} |
| 666 |
|
| 667 |
/////////////////////////////////////////////////////////////////////////////// |
| 668 |
|
| 669 |
size_t SkRRect::writeToMemory(void* buffer) const { |
| 670 |
// Serialize only the rect and corners, but not the derived type tag. |
| 671 |
memcpy(buffer, this, kSizeInMemory); |
| 672 |
return kSizeInMemory; |
| 673 |
} |
| 674 |
|
| 675 |
void SkRRectPriv::WriteToBuffer(const SkRRect& rr, SkWBuffer* buffer) { |
| 676 |
// Serialize only the rect and corners, but not the derived type tag. |
| 677 |
buffer->write(&rr, SkRRect::kSizeInMemory); |
| 678 |
} |
| 679 |
|
| 680 |
size_t SkRRect::readFromMemory(const void* buffer, size_t length) { |
| 681 |
if (length < kSizeInMemory) { |
| 682 |
return 0; |
| 683 |
} |
| 684 |
|
| 685 |
// The extra (void*) tells GCC not to worry that kSizeInMemory < sizeof(SkRRect). |
| 686 |
|
| 687 |
SkRRect raw; |
| 688 |
memcpy((void*)&raw, buffer, kSizeInMemory); |
| 689 |
this->setRectRadii(raw.fRect, raw.fRadii); |
| 690 |
return kSizeInMemory; |
| 691 |
} |
| 692 |
|
| 693 |
bool SkRRectPriv::ReadFromBuffer(SkRBuffer* buffer, SkRRect* rr) { |
| 694 |
if (buffer->available() < SkRRect::kSizeInMemory) { |
| 695 |
return false; |
| 696 |
} |
| 697 |
SkRRect storage; |
| 698 |
return buffer->read(&storage, SkRRect::kSizeInMemory) && |
| 699 |
(rr->readFromMemory(&storage, SkRRect::kSizeInMemory) == SkRRect::kSizeInMemory); |
| 700 |
} |
| 701 |
|
| 702 |
SkString SkRRect::dumpToString(bool asHex) const { |
| 703 |
SkScalarAsStringType asType = asHex ? kHex_SkScalarAsStringType : kDec_SkScalarAsStringType; |
| 704 |
|
| 705 |
SkString line = fRect.dumpToString(asHex); |
| 706 |
line.appendf("\nconst SkPoint corners[] = {\n"); |
| 707 |
for (int i = 0; i < 4; ++i) { |
| 708 |
SkString strX, strY; |
| 709 |
SkAppendScalar(&strX, fRadii[i].x(), asType); |
| 710 |
SkAppendScalar(&strY, fRadii[i].y(), asType); |
| 711 |
line.appendf(" { %s, %s },", strX.c_str(), strY.c_str()); |
| 712 |
if (asHex) { |
| 713 |
line.appendf(" /* %f %f */", fRadii[i].x(), fRadii[i].y()); |
| 714 |
} |
| 715 |
line.append("\n"); |
| 716 |
} |
| 717 |
line.append("};"); |
| 718 |
return line; |
| 719 |
} |
| 720 |
|
| 721 |
void SkRRect::dump(bool asHex) const { SkDebugf("%s\n", this->dumpToString(asHex).c_str()); } |
| 722 |
|
| 723 |
/////////////////////////////////////////////////////////////////////////////// |
| 724 |
|
| 725 |
/** |
| 726 |
* We need all combinations of predicates to be true to have a "safe" radius value. |
| 727 |
*/ |
| 728 |
static bool are_radius_check_predicates_valid(SkScalar rad, SkScalar min, SkScalar max) { |
| 729 |
return (min <= max) && (rad <= max - min) && (min + rad <= max) && (max - rad >= min) && |
| 730 |
rad >= 0; |
| 731 |
} |
| 732 |
|
| 733 |
bool SkRRect::isValid() const { |
| 734 |
if (!AreRectAndRadiiValid(fRect, fRadii)) { |
| 735 |
return false; |
| 736 |
} |
| 737 |
|
| 738 |
bool allRadiiZero = (0 == fRadii[0].fX && 0 == fRadii[0].fY); |
| 739 |
bool allCornersSquare = (0 == fRadii[0].fX || 0 == fRadii[0].fY); |
| 740 |
bool allRadiiSame = true; |
| 741 |
|
| 742 |
for (int i = 1; i < 4; ++i) { |
| 743 |
if (0 != fRadii[i].fX || 0 != fRadii[i].fY) { |
| 744 |
allRadiiZero = false; |
| 745 |
} |
| 746 |
|
| 747 |
if (fRadii[i].fX != fRadii[i-1].fX || fRadii[i].fY != fRadii[i-1].fY) { |
| 748 |
allRadiiSame = false; |
| 749 |
} |
| 750 |
|
| 751 |
if (0 != fRadii[i].fX && 0 != fRadii[i].fY) { |
| 752 |
allCornersSquare = false; |
| 753 |
} |
| 754 |
} |
| 755 |
bool patchesOfNine = radii_are_nine_patch(fRadii); |
| 756 |
|
| 757 |
if (fType < 0 || fType > kLastType) { |
| 758 |
return false; |
| 759 |
} |
| 760 |
|
| 761 |
switch (fType) { |
| 762 |
case kEmpty_Type: |
| 763 |
if (!fRect.isEmpty() || !allRadiiZero || !allRadiiSame || !allCornersSquare) { |
| 764 |
return false; |
| 765 |
} |
| 766 |
break; |
| 767 |
case kRect_Type: |
| 768 |
if (fRect.isEmpty() || !allRadiiZero || !allRadiiSame || !allCornersSquare) { |
| 769 |
return false; |
| 770 |
} |
| 771 |
break; |
| 772 |
case kOval_Type: |
| 773 |
if (fRect.isEmpty() || allRadiiZero || !allRadiiSame || allCornersSquare) { |
| 774 |
return false; |
| 775 |
} |
| 776 |
|
| 777 |
for (int i = 0; i < 4; ++i) { |
| 778 |
if (!SkScalarNearlyEqual(fRadii[i].fX, SkRectPriv::HalfWidth(fRect)) || |
| 779 |
!SkScalarNearlyEqual(fRadii[i].fY, SkRectPriv::HalfHeight(fRect))) { |
| 780 |
return false; |
| 781 |
} |
| 782 |
} |
| 783 |
break; |
| 784 |
case kSimple_Type: |
| 785 |
if (fRect.isEmpty() || allRadiiZero || !allRadiiSame || allCornersSquare) { |
| 786 |
return false; |
| 787 |
} |
| 788 |
break; |
| 789 |
case kNinePatch_Type: |
| 790 |
if (fRect.isEmpty() || allRadiiZero || allRadiiSame || allCornersSquare || |
| 791 |
!patchesOfNine) { |
| 792 |
return false; |
| 793 |
} |
| 794 |
break; |
| 795 |
case kComplex_Type: |
| 796 |
if (fRect.isEmpty() || allRadiiZero || allRadiiSame || allCornersSquare || |
| 797 |
patchesOfNine) { |
| 798 |
return false; |
| 799 |
} |
| 800 |
break; |
| 801 |
} |
| 802 |
|
| 803 |
return true; |
| 804 |
} |
| 805 |
|
| 806 |
bool SkRRect::AreRectAndRadiiValid(const SkRect& rect, const SkVector radii[4]) { |
| 807 |
if (!rect.isFinite() || !rect.isSorted()) { |
| 808 |
return false; |
| 809 |
} |
| 810 |
for (int i = 0; i < 4; ++i) { |
| 811 |
if (!are_radius_check_predicates_valid(radii[i].fX, rect.fLeft, rect.fRight) || |
| 812 |
!are_radius_check_predicates_valid(radii[i].fY, rect.fTop, rect.fBottom)) { |
| 813 |
return false; |
| 814 |
} |
| 815 |
} |
| 816 |
return true; |
| 817 |
} |
| 818 |
/////////////////////////////////////////////////////////////////////////////// |
| 819 |
|
| 820 |
SkRect SkRRectPriv::InnerBounds(const SkRRect& rr) { |
| 821 |
if (rr.isEmpty() || rr.isRect()) { |
| 822 |
return rr.rect(); |
| 823 |
} |
| 824 |
|
| 825 |
// We start with the outer bounds of the round rect and consider three subsets and take the |
| 826 |
// one with maximum area. The first two are the horizontal and vertical rects inset from the |
| 827 |
// corners, the third is the rect inscribed at the corner curves' maximal point. This forms |
| 828 |
// the exact solution when all corners have the same radii (the radii do not have to be |
| 829 |
// circular). |
| 830 |
SkRect innerBounds = rr.getBounds(); |
| 831 |
SkVector tl = rr.radii(SkRRect::kUpperLeft_Corner); |
| 832 |
SkVector tr = rr.radii(SkRRect::kUpperRight_Corner); |
| 833 |
SkVector bl = rr.radii(SkRRect::kLowerLeft_Corner); |
| 834 |
SkVector br = rr.radii(SkRRect::kLowerRight_Corner); |
| 835 |
|
| 836 |
// Select maximum inset per edge, which may move an adjacent corner of the inscribed |
| 837 |
// rectangle off of the rounded-rect path, but that is acceptable given that the general |
| 838 |
// equation for inscribed area is non-trivial to evaluate. |
| 839 |
SkScalar leftShift = std::max(tl.fX, bl.fX); |
| 840 |
SkScalar topShift = std::max(tl.fY, tr.fY); |
| 841 |
SkScalar rightShift = std::max(tr.fX, br.fX); |
| 842 |
SkScalar bottomShift = std::max(bl.fY, br.fY); |
| 843 |
|
| 844 |
SkScalar dw = leftShift + rightShift; |
| 845 |
SkScalar dh = topShift + bottomShift; |
| 846 |
|
| 847 |
// Area removed by shifting left/right |
| 848 |
SkScalar horizArea = (innerBounds.width() - dw) * innerBounds.height(); |
| 849 |
// And by shifting top/bottom |
| 850 |
SkScalar vertArea = (innerBounds.height() - dh) * innerBounds.width(); |
| 851 |
// And by shifting all edges: just considering a corner ellipse, the maximum inscribed rect has |
| 852 |
// a corner at sqrt(2)/2 * (rX, rY), so scale all corner shifts by (1 - sqrt(2)/2) to get the |
| 853 |
// safe shift per edge (since the shifts already are the max radius for that edge). |
| 854 |
// - We actually scale by a value slightly increased to make it so that the shifted corners are |
| 855 |
// safely inside the curves, otherwise numerical stability can cause it to fail contains(). |
| 856 |
static constexpr SkScalar kScale = (1.f - SK_ScalarRoot2Over2) + 1e-5f; |
| 857 |
SkScalar innerArea = (innerBounds.width() - kScale * dw) * (innerBounds.height() - kScale * dh); |
| 858 |
|
| 859 |
if (horizArea > vertArea && horizArea > innerArea) { |
| 860 |
// Cut off corners by insetting left and right |
| 861 |
innerBounds.fLeft += leftShift; |
| 862 |
innerBounds.fRight -= rightShift; |
| 863 |
} else if (vertArea > innerArea) { |
| 864 |
// Cut off corners by insetting top and bottom |
| 865 |
innerBounds.fTop += topShift; |
| 866 |
innerBounds.fBottom -= bottomShift; |
| 867 |
} else if (innerArea > 0.f) { |
| 868 |
// Inset on all sides, scaled to touch |
| 869 |
innerBounds.fLeft += kScale * leftShift; |
| 870 |
innerBounds.fRight -= kScale * rightShift; |
| 871 |
innerBounds.fTop += kScale * topShift; |
| 872 |
innerBounds.fBottom -= kScale * bottomShift; |
| 873 |
} else { |
| 874 |
// Inner region would collapse to empty |
| 875 |
return SkRect::MakeEmpty(); |
| 876 |
} |
| 877 |
|
| 878 |
SkASSERT(innerBounds.isSorted() && !innerBounds.isEmpty()); |
| 879 |
return innerBounds; |
| 880 |
} |
| 881 |
|
| 882 |
SkRRect SkRRectPriv::ConservativeIntersect(const SkRRect& a, const SkRRect& b) { |
| 883 |
// Returns the coordinate of the rect matching the corner enum. |
| 884 |
auto getCorner = [](const SkRect& r, SkRRect::Corner corner) -> SkPoint { |
| 885 |
switch(corner) { |
| 886 |
case SkRRect::kUpperLeft_Corner: return {r.fLeft, r.fTop}; |
| 887 |
case SkRRect::kUpperRight_Corner: return {r.fRight, r.fTop}; |
| 888 |
case SkRRect::kLowerLeft_Corner: return {r.fLeft, r.fBottom}; |
| 889 |
case SkRRect::kLowerRight_Corner: return {r.fRight, r.fBottom}; |
| 890 |
default: SkUNREACHABLE; |
| 891 |
} |
| 892 |
}; |
| 893 |
// Returns true if shape A's extreme point is contained within shape B's extreme point, relative |
| 894 |
// to the 'corner' location. If the two shapes' corners have the same ellipse radii, this |
| 895 |
// is sufficient for A's ellipse arc to be contained by B's ellipse arc. |
| 896 |
auto insideCorner = [](SkRRect::Corner corner, const SkPoint& a, const SkPoint& b) { |
| 897 |
switch(corner) { |
| 898 |
case SkRRect::kUpperLeft_Corner: return a.fX >= b.fX && a.fY >= b.fY; |
| 899 |
case SkRRect::kUpperRight_Corner: return a.fX <= b.fX && a.fY >= b.fY; |
| 900 |
case SkRRect::kLowerRight_Corner: return a.fX <= b.fX && a.fY <= b.fY; |
| 901 |
case SkRRect::kLowerLeft_Corner: return a.fX >= b.fX && a.fY <= b.fY; |
| 902 |
default: SkUNREACHABLE; |
| 903 |
} |
| 904 |
}; |
| 905 |
|
| 906 |
auto getIntersectionRadii = [&](const SkRect& r, SkRRect::Corner corner, SkVector* radii) { |
| 907 |
SkPoint test = getCorner(r, corner); |
| 908 |
SkPoint aCorner = getCorner(a.rect(), corner); |
| 909 |
SkPoint bCorner = getCorner(b.rect(), corner); |
| 910 |
|
| 911 |
if (test == aCorner && test == bCorner) { |
| 912 |
// The round rects share a corner anchor, so pick A or B such that its X and Y radii |
| 913 |
// are both larger than the other rrect's, or return false if neither A or B has the max |
| 914 |
// corner radii (this is more permissive than the single corner tests below). |
| 915 |
SkVector aRadii = a.radii(corner); |
| 916 |
SkVector bRadii = b.radii(corner); |
| 917 |
if (aRadii.fX >= bRadii.fX && aRadii.fY >= bRadii.fY) { |
| 918 |
*radii = aRadii; |
| 919 |
return true; |
| 920 |
} else if (bRadii.fX >= aRadii.fX && bRadii.fY >= aRadii.fY) { |
| 921 |
*radii = bRadii; |
| 922 |
return true; |
| 923 |
} else { |
| 924 |
return false; |
| 925 |
} |
| 926 |
} else if (test == aCorner) { |
| 927 |
// Test that A's ellipse is contained by B. This is a non-trivial function to evaluate |
| 928 |
// so we resrict it to when the corners have the same radii. If not, we use the more |
| 929 |
// conservative test that the extreme point of A's bounding box is contained in B. |
| 930 |
*radii = a.radii(corner); |
| 931 |
if (*radii == b.radii(corner)) { |
| 932 |
return insideCorner(corner, aCorner, bCorner); // A inside B |
| 933 |
} else { |
| 934 |
return b.checkCornerContainment(aCorner.fX, aCorner.fY); |
| 935 |
} |
| 936 |
} else if (test == bCorner) { |
| 937 |
// Mirror of the above |
| 938 |
*radii = b.radii(corner); |
| 939 |
if (*radii == a.radii(corner)) { |
| 940 |
return insideCorner(corner, bCorner, aCorner); // B inside A |
| 941 |
} else { |
| 942 |
return a.checkCornerContainment(bCorner.fX, bCorner.fY); |
| 943 |
} |
| 944 |
} else { |
| 945 |
// This is a corner formed by two straight edges of A and B, so confirm that it is |
| 946 |
// contained in both (if not, then the intersection can't be a round rect). |
| 947 |
*radii = {0.f, 0.f}; |
| 948 |
return a.checkCornerContainment(test.fX, test.fY) && |
| 949 |
b.checkCornerContainment(test.fX, test.fY); |
| 950 |
} |
| 951 |
}; |
| 952 |
|
| 953 |
// We fill in the SkRRect directly. Since the rect and radii are either 0s or determined by |
| 954 |
// valid existing SkRRects, we know we are finite. |
| 955 |
SkRRect intersection; |
| 956 |
if (!intersection.fRect.intersect(a.rect(), b.rect())) { |
| 957 |
// Definitely no intersection |
| 958 |
return SkRRect::MakeEmpty(); |
| 959 |
} |
| 960 |
|
| 961 |
const SkRRect::Corner corners[] = { |
| 962 |
SkRRect::kUpperLeft_Corner, |
| 963 |
SkRRect::kUpperRight_Corner, |
| 964 |
SkRRect::kLowerRight_Corner, |
| 965 |
SkRRect::kLowerLeft_Corner |
| 966 |
}; |
| 967 |
// By definition, edges is contained in the bounds of 'a' and 'b', but now we need to consider |
| 968 |
// the corners. If the bound's corner point is in both rrects, the corner radii will be 0s. |
| 969 |
// If the bound's corner point matches a's edges and is inside 'b', we use a's radii. |
| 970 |
// Same for b's radii. If any corner fails these conditions, we reject the intersection as an |
| 971 |
// rrect. If after determining radii for all 4 corners, they would overlap, we also reject the |
| 972 |
// intersection shape. |
| 973 |
for (auto c : corners) { |
| 974 |
if (!getIntersectionRadii(intersection.fRect, c, &intersection.fRadii[c])) { |
| 975 |
return SkRRect::MakeEmpty(); // Resulting intersection is not a rrect |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
// Check for radius overlap along the four edges, since the earlier evaluation was only a |
| 980 |
// one-sided corner check. If they aren't valid, a corner's radii doesn't fit within the rect. |
| 981 |
// If the radii are scaled, the combination of radii from two adjacent corners doesn't fit. |
| 982 |
// Normally for a regularly constructed SkRRect, we want this scaling, but in this case it means |
| 983 |
// the intersection shape is definitively not a round rect. |
| 984 |
if (!SkRRect::AreRectAndRadiiValid(intersection.fRect, intersection.fRadii) || |
| 985 |
intersection.scaleRadii()) { |
| 986 |
return SkRRect::MakeEmpty(); |
| 987 |
} |
| 988 |
|
| 989 |
// The intersection is an rrect of the given radii. Potentially all 4 corners could have |
| 990 |
// been simplified to (0,0) radii, making the intersection a rectangle. |
| 991 |
intersection.computeType(); |
| 992 |
return intersection; |
| 993 |
} |