gfx/skia/trunk/src/core/SkRRect.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2012 Google Inc.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkRRect.h"
michael@0 9 #include "SkMatrix.h"
michael@0 10
michael@0 11 ///////////////////////////////////////////////////////////////////////////////
michael@0 12
michael@0 13 void SkRRect::setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad) {
michael@0 14 if (rect.isEmpty()) {
michael@0 15 this->setEmpty();
michael@0 16 return;
michael@0 17 }
michael@0 18
michael@0 19 if (xRad <= 0 || yRad <= 0) {
michael@0 20 // all corners are square in this case
michael@0 21 this->setRect(rect);
michael@0 22 return;
michael@0 23 }
michael@0 24
michael@0 25 if (rect.width() < xRad+xRad || rect.height() < yRad+yRad) {
michael@0 26 SkScalar scale = SkMinScalar(SkScalarDiv(rect.width(), xRad + xRad),
michael@0 27 SkScalarDiv(rect.height(), yRad + yRad));
michael@0 28 SkASSERT(scale < SK_Scalar1);
michael@0 29 xRad = SkScalarMul(xRad, scale);
michael@0 30 yRad = SkScalarMul(yRad, scale);
michael@0 31 }
michael@0 32
michael@0 33 fRect = rect;
michael@0 34 for (int i = 0; i < 4; ++i) {
michael@0 35 fRadii[i].set(xRad, yRad);
michael@0 36 }
michael@0 37 fType = kSimple_Type;
michael@0 38 if (xRad >= SkScalarHalf(fRect.width()) && yRad >= SkScalarHalf(fRect.height())) {
michael@0 39 fType = kOval_Type;
michael@0 40 // TODO: assert that all the x&y radii are already W/2 & H/2
michael@0 41 }
michael@0 42
michael@0 43 SkDEBUGCODE(this->validate();)
michael@0 44 }
michael@0 45
michael@0 46 void SkRRect::setRectRadii(const SkRect& rect, const SkVector radii[4]) {
michael@0 47 if (rect.isEmpty()) {
michael@0 48 this->setEmpty();
michael@0 49 return;
michael@0 50 }
michael@0 51
michael@0 52 fRect = rect;
michael@0 53 memcpy(fRadii, radii, sizeof(fRadii));
michael@0 54
michael@0 55 bool allCornersSquare = true;
michael@0 56
michael@0 57 // Clamp negative radii to zero
michael@0 58 for (int i = 0; i < 4; ++i) {
michael@0 59 if (fRadii[i].fX <= 0 || fRadii[i].fY <= 0) {
michael@0 60 // In this case we are being a little fast & loose. Since one of
michael@0 61 // the radii is 0 the corner is square. However, the other radii
michael@0 62 // could still be non-zero and play in the global scale factor
michael@0 63 // computation.
michael@0 64 fRadii[i].fX = 0;
michael@0 65 fRadii[i].fY = 0;
michael@0 66 } else {
michael@0 67 allCornersSquare = false;
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 if (allCornersSquare) {
michael@0 72 this->setRect(rect);
michael@0 73 return;
michael@0 74 }
michael@0 75
michael@0 76 // Proportionally scale down all radii to fit. Find the minimum ratio
michael@0 77 // of a side and the radii on that side (for all four sides) and use
michael@0 78 // that to scale down _all_ the radii. This algorithm is from the
michael@0 79 // W3 spec (http://www.w3.org/TR/css3-background/) section 5.5 - Overlapping
michael@0 80 // Curves:
michael@0 81 // "Let f = min(Li/Si), where i is one of { top, right, bottom, left },
michael@0 82 // Si is the sum of the two corresponding radii of the corners on side i,
michael@0 83 // and Ltop = Lbottom = the width of the box,
michael@0 84 // and Lleft = Lright = the height of the box.
michael@0 85 // If f < 1, then all corner radii are reduced by multiplying them by f."
michael@0 86 SkScalar scale = SK_Scalar1;
michael@0 87
michael@0 88 if (fRadii[0].fX + fRadii[1].fX > rect.width()) {
michael@0 89 scale = SkMinScalar(scale,
michael@0 90 SkScalarDiv(rect.width(), fRadii[0].fX + fRadii[1].fX));
michael@0 91 }
michael@0 92 if (fRadii[1].fY + fRadii[2].fY > rect.height()) {
michael@0 93 scale = SkMinScalar(scale,
michael@0 94 SkScalarDiv(rect.height(), fRadii[1].fY + fRadii[2].fY));
michael@0 95 }
michael@0 96 if (fRadii[2].fX + fRadii[3].fX > rect.width()) {
michael@0 97 scale = SkMinScalar(scale,
michael@0 98 SkScalarDiv(rect.width(), fRadii[2].fX + fRadii[3].fX));
michael@0 99 }
michael@0 100 if (fRadii[3].fY + fRadii[0].fY > rect.height()) {
michael@0 101 scale = SkMinScalar(scale,
michael@0 102 SkScalarDiv(rect.height(), fRadii[3].fY + fRadii[0].fY));
michael@0 103 }
michael@0 104
michael@0 105 if (scale < SK_Scalar1) {
michael@0 106 for (int i = 0; i < 4; ++i) {
michael@0 107 fRadii[i].fX = SkScalarMul(fRadii[i].fX, scale);
michael@0 108 fRadii[i].fY = SkScalarMul(fRadii[i].fY, scale);
michael@0 109 }
michael@0 110 }
michael@0 111
michael@0 112 // At this point we're either oval, simple, or complex (not empty or rect)
michael@0 113 // but we lazily resolve the type to avoid the work if the information
michael@0 114 // isn't required.
michael@0 115 fType = (SkRRect::Type) kUnknown_Type;
michael@0 116
michael@0 117 SkDEBUGCODE(this->validate();)
michael@0 118 }
michael@0 119
michael@0 120 // This method determines if a point known to be inside the RRect's bounds is
michael@0 121 // inside all the corners.
michael@0 122 bool SkRRect::checkCornerContainment(SkScalar x, SkScalar y) const {
michael@0 123 SkPoint canonicalPt; // (x,y) translated to one of the quadrants
michael@0 124 int index;
michael@0 125
michael@0 126 if (kOval_Type == this->type()) {
michael@0 127 canonicalPt.set(x - fRect.centerX(), y - fRect.centerY());
michael@0 128 index = kUpperLeft_Corner; // any corner will do in this case
michael@0 129 } else {
michael@0 130 if (x < fRect.fLeft + fRadii[kUpperLeft_Corner].fX &&
michael@0 131 y < fRect.fTop + fRadii[kUpperLeft_Corner].fY) {
michael@0 132 // UL corner
michael@0 133 index = kUpperLeft_Corner;
michael@0 134 canonicalPt.set(x - (fRect.fLeft + fRadii[kUpperLeft_Corner].fX),
michael@0 135 y - (fRect.fTop + fRadii[kUpperLeft_Corner].fY));
michael@0 136 SkASSERT(canonicalPt.fX < 0 && canonicalPt.fY < 0);
michael@0 137 } else if (x < fRect.fLeft + fRadii[kLowerLeft_Corner].fX &&
michael@0 138 y > fRect.fBottom - fRadii[kLowerLeft_Corner].fY) {
michael@0 139 // LL corner
michael@0 140 index = kLowerLeft_Corner;
michael@0 141 canonicalPt.set(x - (fRect.fLeft + fRadii[kLowerLeft_Corner].fX),
michael@0 142 y - (fRect.fBottom - fRadii[kLowerLeft_Corner].fY));
michael@0 143 SkASSERT(canonicalPt.fX < 0 && canonicalPt.fY > 0);
michael@0 144 } else if (x > fRect.fRight - fRadii[kUpperRight_Corner].fX &&
michael@0 145 y < fRect.fTop + fRadii[kUpperRight_Corner].fY) {
michael@0 146 // UR corner
michael@0 147 index = kUpperRight_Corner;
michael@0 148 canonicalPt.set(x - (fRect.fRight - fRadii[kUpperRight_Corner].fX),
michael@0 149 y - (fRect.fTop + fRadii[kUpperRight_Corner].fY));
michael@0 150 SkASSERT(canonicalPt.fX > 0 && canonicalPt.fY < 0);
michael@0 151 } else if (x > fRect.fRight - fRadii[kLowerRight_Corner].fX &&
michael@0 152 y > fRect.fBottom - fRadii[kLowerRight_Corner].fY) {
michael@0 153 // LR corner
michael@0 154 index = kLowerRight_Corner;
michael@0 155 canonicalPt.set(x - (fRect.fRight - fRadii[kLowerRight_Corner].fX),
michael@0 156 y - (fRect.fBottom - fRadii[kLowerRight_Corner].fY));
michael@0 157 SkASSERT(canonicalPt.fX > 0 && canonicalPt.fY > 0);
michael@0 158 } else {
michael@0 159 // not in any of the corners
michael@0 160 return true;
michael@0 161 }
michael@0 162 }
michael@0 163
michael@0 164 // A point is in an ellipse (in standard position) if:
michael@0 165 // x^2 y^2
michael@0 166 // ----- + ----- <= 1
michael@0 167 // a^2 b^2
michael@0 168 // or :
michael@0 169 // b^2*x^2 + a^2*y^2 <= (ab)^2
michael@0 170 SkScalar dist = SkScalarMul(SkScalarSquare(canonicalPt.fX), SkScalarSquare(fRadii[index].fY)) +
michael@0 171 SkScalarMul(SkScalarSquare(canonicalPt.fY), SkScalarSquare(fRadii[index].fX));
michael@0 172 return dist <= SkScalarSquare(SkScalarMul(fRadii[index].fX, fRadii[index].fY));
michael@0 173 }
michael@0 174
michael@0 175 bool SkRRect::allCornersCircular() const {
michael@0 176 return fRadii[0].fX == fRadii[0].fY &&
michael@0 177 fRadii[1].fX == fRadii[1].fY &&
michael@0 178 fRadii[2].fX == fRadii[2].fY &&
michael@0 179 fRadii[3].fX == fRadii[3].fY;
michael@0 180 }
michael@0 181
michael@0 182 bool SkRRect::contains(const SkRect& rect) const {
michael@0 183 if (!this->getBounds().contains(rect)) {
michael@0 184 // If 'rect' isn't contained by the RR's bounds then the
michael@0 185 // RR definitely doesn't contain it
michael@0 186 return false;
michael@0 187 }
michael@0 188
michael@0 189 if (this->isRect()) {
michael@0 190 // the prior test was sufficient
michael@0 191 return true;
michael@0 192 }
michael@0 193
michael@0 194 // At this point we know all four corners of 'rect' are inside the
michael@0 195 // bounds of of this RR. Check to make sure all the corners are inside
michael@0 196 // all the curves
michael@0 197 return this->checkCornerContainment(rect.fLeft, rect.fTop) &&
michael@0 198 this->checkCornerContainment(rect.fRight, rect.fTop) &&
michael@0 199 this->checkCornerContainment(rect.fRight, rect.fBottom) &&
michael@0 200 this->checkCornerContainment(rect.fLeft, rect.fBottom);
michael@0 201 }
michael@0 202
michael@0 203 // There is a simplified version of this method in setRectXY
michael@0 204 void SkRRect::computeType() const {
michael@0 205 SkDEBUGCODE(this->validate();)
michael@0 206
michael@0 207 if (fRect.isEmpty()) {
michael@0 208 fType = kEmpty_Type;
michael@0 209 return;
michael@0 210 }
michael@0 211
michael@0 212 bool allRadiiEqual = true; // are all x radii equal and all y radii?
michael@0 213 bool allCornersSquare = 0 == fRadii[0].fX || 0 == fRadii[0].fY;
michael@0 214
michael@0 215 for (int i = 1; i < 4; ++i) {
michael@0 216 if (0 != fRadii[i].fX && 0 != fRadii[i].fY) {
michael@0 217 // if either radius is zero the corner is square so both have to
michael@0 218 // be non-zero to have a rounded corner
michael@0 219 allCornersSquare = false;
michael@0 220 }
michael@0 221 if (fRadii[i].fX != fRadii[i-1].fX || fRadii[i].fY != fRadii[i-1].fY) {
michael@0 222 allRadiiEqual = false;
michael@0 223 }
michael@0 224 }
michael@0 225
michael@0 226 if (allCornersSquare) {
michael@0 227 fType = kRect_Type;
michael@0 228 return;
michael@0 229 }
michael@0 230
michael@0 231 if (allRadiiEqual) {
michael@0 232 if (fRadii[0].fX >= SkScalarHalf(fRect.width()) &&
michael@0 233 fRadii[0].fY >= SkScalarHalf(fRect.height())) {
michael@0 234 fType = kOval_Type;
michael@0 235 } else {
michael@0 236 fType = kSimple_Type;
michael@0 237 }
michael@0 238 return;
michael@0 239 }
michael@0 240
michael@0 241 fType = kComplex_Type;
michael@0 242 }
michael@0 243
michael@0 244 static bool matrix_only_scale_and_translate(const SkMatrix& matrix) {
michael@0 245 const SkMatrix::TypeMask m = (SkMatrix::TypeMask) (SkMatrix::kAffine_Mask
michael@0 246 | SkMatrix::kPerspective_Mask);
michael@0 247 return (matrix.getType() & m) == 0;
michael@0 248 }
michael@0 249
michael@0 250 bool SkRRect::transform(const SkMatrix& matrix, SkRRect* dst) const {
michael@0 251 if (NULL == dst) {
michael@0 252 return false;
michael@0 253 }
michael@0 254
michael@0 255 // Assert that the caller is not trying to do this in place, which
michael@0 256 // would violate const-ness. Do not return false though, so that
michael@0 257 // if they know what they're doing and want to violate it they can.
michael@0 258 SkASSERT(dst != this);
michael@0 259
michael@0 260 if (matrix.isIdentity()) {
michael@0 261 *dst = *this;
michael@0 262 return true;
michael@0 263 }
michael@0 264
michael@0 265 // If transform supported 90 degree rotations (which it could), we could
michael@0 266 // use SkMatrix::rectStaysRect() to check for a valid transformation.
michael@0 267 if (!matrix_only_scale_and_translate(matrix)) {
michael@0 268 return false;
michael@0 269 }
michael@0 270
michael@0 271 SkRect newRect;
michael@0 272 if (!matrix.mapRect(&newRect, fRect)) {
michael@0 273 return false;
michael@0 274 }
michael@0 275
michael@0 276 // At this point, this is guaranteed to succeed, so we can modify dst.
michael@0 277 dst->fRect = newRect;
michael@0 278
michael@0 279 // Now scale each corner
michael@0 280 SkScalar xScale = matrix.getScaleX();
michael@0 281 const bool flipX = xScale < 0;
michael@0 282 if (flipX) {
michael@0 283 xScale = -xScale;
michael@0 284 }
michael@0 285 SkScalar yScale = matrix.getScaleY();
michael@0 286 const bool flipY = yScale < 0;
michael@0 287 if (flipY) {
michael@0 288 yScale = -yScale;
michael@0 289 }
michael@0 290
michael@0 291 // Scale the radii without respecting the flip.
michael@0 292 for (int i = 0; i < 4; ++i) {
michael@0 293 dst->fRadii[i].fX = SkScalarMul(fRadii[i].fX, xScale);
michael@0 294 dst->fRadii[i].fY = SkScalarMul(fRadii[i].fY, yScale);
michael@0 295 }
michael@0 296
michael@0 297 // Now swap as necessary.
michael@0 298 if (flipX) {
michael@0 299 if (flipY) {
michael@0 300 // Swap with opposite corners
michael@0 301 SkTSwap(dst->fRadii[kUpperLeft_Corner], dst->fRadii[kLowerRight_Corner]);
michael@0 302 SkTSwap(dst->fRadii[kUpperRight_Corner], dst->fRadii[kLowerLeft_Corner]);
michael@0 303 } else {
michael@0 304 // Only swap in x
michael@0 305 SkTSwap(dst->fRadii[kUpperRight_Corner], dst->fRadii[kUpperLeft_Corner]);
michael@0 306 SkTSwap(dst->fRadii[kLowerRight_Corner], dst->fRadii[kLowerLeft_Corner]);
michael@0 307 }
michael@0 308 } else if (flipY) {
michael@0 309 // Only swap in y
michael@0 310 SkTSwap(dst->fRadii[kUpperLeft_Corner], dst->fRadii[kLowerLeft_Corner]);
michael@0 311 SkTSwap(dst->fRadii[kUpperRight_Corner], dst->fRadii[kLowerRight_Corner]);
michael@0 312 }
michael@0 313
michael@0 314 // Since the only transforms that were allowed are scale and translate, the type
michael@0 315 // remains unchanged.
michael@0 316 dst->fType = fType;
michael@0 317
michael@0 318 SkDEBUGCODE(dst->validate();)
michael@0 319
michael@0 320 return true;
michael@0 321 }
michael@0 322
michael@0 323 ///////////////////////////////////////////////////////////////////////////////
michael@0 324
michael@0 325 void SkRRect::inset(SkScalar dx, SkScalar dy, SkRRect* dst) const {
michael@0 326 SkRect r = fRect;
michael@0 327
michael@0 328 r.inset(dx, dy);
michael@0 329 if (r.isEmpty()) {
michael@0 330 dst->setEmpty();
michael@0 331 return;
michael@0 332 }
michael@0 333
michael@0 334 SkVector radii[4];
michael@0 335 memcpy(radii, fRadii, sizeof(radii));
michael@0 336 for (int i = 0; i < 4; ++i) {
michael@0 337 if (radii[i].fX) {
michael@0 338 radii[i].fX -= dx;
michael@0 339 }
michael@0 340 if (radii[i].fY) {
michael@0 341 radii[i].fY -= dy;
michael@0 342 }
michael@0 343 }
michael@0 344 dst->setRectRadii(r, radii);
michael@0 345 }
michael@0 346
michael@0 347 ///////////////////////////////////////////////////////////////////////////////
michael@0 348
michael@0 349 size_t SkRRect::writeToMemory(void* buffer) const {
michael@0 350 SkASSERT(kSizeInMemory == sizeof(SkRect) + sizeof(fRadii));
michael@0 351
michael@0 352 memcpy(buffer, &fRect, sizeof(SkRect));
michael@0 353 memcpy((char*)buffer + sizeof(SkRect), fRadii, sizeof(fRadii));
michael@0 354 return kSizeInMemory;
michael@0 355 }
michael@0 356
michael@0 357 size_t SkRRect::readFromMemory(const void* buffer, size_t length) {
michael@0 358 if (length < kSizeInMemory) {
michael@0 359 return 0;
michael@0 360 }
michael@0 361
michael@0 362 SkScalar storage[12];
michael@0 363 SkASSERT(sizeof(storage) == kSizeInMemory);
michael@0 364
michael@0 365 // we make a local copy, to ensure alignment before we cast
michael@0 366 memcpy(storage, buffer, kSizeInMemory);
michael@0 367
michael@0 368 this->setRectRadii(*(const SkRect*)&storage[0],
michael@0 369 (const SkVector*)&storage[4]);
michael@0 370 return kSizeInMemory;
michael@0 371 }
michael@0 372
michael@0 373 ///////////////////////////////////////////////////////////////////////////////
michael@0 374
michael@0 375 #ifdef SK_DEBUG
michael@0 376 void SkRRect::validate() const {
michael@0 377 bool allRadiiZero = (0 == fRadii[0].fX && 0 == fRadii[0].fY);
michael@0 378 bool allCornersSquare = (0 == fRadii[0].fX || 0 == fRadii[0].fY);
michael@0 379 bool allRadiiSame = true;
michael@0 380
michael@0 381 for (int i = 1; i < 4; ++i) {
michael@0 382 if (0 != fRadii[i].fX || 0 != fRadii[i].fY) {
michael@0 383 allRadiiZero = false;
michael@0 384 }
michael@0 385
michael@0 386 if (fRadii[i].fX != fRadii[i-1].fX || fRadii[i].fY != fRadii[i-1].fY) {
michael@0 387 allRadiiSame = false;
michael@0 388 }
michael@0 389
michael@0 390 if (0 != fRadii[i].fX && 0 != fRadii[i].fY) {
michael@0 391 allCornersSquare = false;
michael@0 392 }
michael@0 393 }
michael@0 394
michael@0 395 switch (fType) {
michael@0 396 case kEmpty_Type:
michael@0 397 SkASSERT(fRect.isEmpty());
michael@0 398 SkASSERT(allRadiiZero && allRadiiSame && allCornersSquare);
michael@0 399
michael@0 400 SkASSERT(0 == fRect.fLeft && 0 == fRect.fTop &&
michael@0 401 0 == fRect.fRight && 0 == fRect.fBottom);
michael@0 402 break;
michael@0 403 case kRect_Type:
michael@0 404 SkASSERT(!fRect.isEmpty());
michael@0 405 SkASSERT(allRadiiZero && allRadiiSame && allCornersSquare);
michael@0 406 break;
michael@0 407 case kOval_Type:
michael@0 408 SkASSERT(!fRect.isEmpty());
michael@0 409 SkASSERT(!allRadiiZero && allRadiiSame && !allCornersSquare);
michael@0 410
michael@0 411 for (int i = 0; i < 4; ++i) {
michael@0 412 SkASSERT(SkScalarNearlyEqual(fRadii[i].fX, SkScalarHalf(fRect.width())));
michael@0 413 SkASSERT(SkScalarNearlyEqual(fRadii[i].fY, SkScalarHalf(fRect.height())));
michael@0 414 }
michael@0 415 break;
michael@0 416 case kSimple_Type:
michael@0 417 SkASSERT(!fRect.isEmpty());
michael@0 418 SkASSERT(!allRadiiZero && allRadiiSame && !allCornersSquare);
michael@0 419 break;
michael@0 420 case kComplex_Type:
michael@0 421 SkASSERT(!fRect.isEmpty());
michael@0 422 SkASSERT(!allRadiiZero && !allRadiiSame && !allCornersSquare);
michael@0 423 break;
michael@0 424 case kUnknown_Type:
michael@0 425 // no limits on this
michael@0 426 break;
michael@0 427 }
michael@0 428 }
michael@0 429 #endif // SK_DEBUG
michael@0 430
michael@0 431 ///////////////////////////////////////////////////////////////////////////////

mercurial