| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include "SkRRect.h" | 9 #include "SkRRect.h" |
| 10 #include "SkMatrix.h" | 10 #include "SkMatrix.h" |
| 11 #include "SkScaleToSides.h" |
| 11 | 12 |
| 12 /////////////////////////////////////////////////////////////////////////////// | 13 /////////////////////////////////////////////////////////////////////////////// |
| 13 | 14 |
| 14 void SkRRect::setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad) { | 15 void SkRRect::setRectXY(const SkRect& rect, SkScalar xRad, SkScalar yRad) { |
| 15 fRect = rect; | 16 fRect = rect; |
| 16 fRect.sort(); | 17 fRect.sort(); |
| 17 | 18 |
| 18 if (fRect.isEmpty() || !fRect.isFinite()) { | 19 if (fRect.isEmpty() || !fRect.isFinite()) { |
| 19 this->setEmpty(); | 20 this->setEmpty(); |
| 20 return; | 21 return; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 // These parameters intentionally double. Apropos crbug.com/463920, if one of th
e | 114 // These parameters intentionally double. Apropos crbug.com/463920, if one of th
e |
| 114 // radii is huge while the other is small, single precision math can completely | 115 // radii is huge while the other is small, single precision math can completely |
| 115 // miss the fact that a scale is required. | 116 // miss the fact that a scale is required. |
| 116 static double compute_min_scale(double rad1, double rad2, double limit, double c
urMin) { | 117 static double compute_min_scale(double rad1, double rad2, double limit, double c
urMin) { |
| 117 if ((rad1 + rad2) > limit) { | 118 if ((rad1 + rad2) > limit) { |
| 118 return SkTMin(curMin, limit / (rad1 + rad2)); | 119 return SkTMin(curMin, limit / (rad1 + rad2)); |
| 119 } | 120 } |
| 120 return curMin; | 121 return curMin; |
| 121 } | 122 } |
| 122 | 123 |
| 123 // This code assumes that a and b fit in in a float, and therefore the resulting
smaller value of | |
| 124 // a and b will fit in a float. The side of the rectangle may be larger than a f
loat. | |
| 125 // Scale must be less than or equal to the ratio limit / (*a + *b). | |
| 126 static void adjust_radii(double limit, double scale, float* a, float* b) { | |
| 127 SkASSERT(scale < 1.0 && scale > 0.0); | |
| 128 // This check is conservative. (double)*a + (double)*b >= (double)(*a + *b) | |
| 129 if ((double)*a + (double)*b > limit) { | |
| 130 float* minRadius = a; | |
| 131 float* maxRadius = b; | |
| 132 // Force minRadius to be the smaller of the two. | |
| 133 if (*minRadius > *maxRadius) { | |
| 134 SkTSwap(minRadius, maxRadius); | |
| 135 } | |
| 136 // newMinRadius must be float in order to give the actual value of the r
adius. | |
| 137 // The newMinRadius will always be smaller than limit. The largest that
minRadius can be | |
| 138 // is 1/2 the ratio of minRadius : (minRadius + maxRadius), therefore in
the resulting | |
| 139 // division, minRadius can be no larger than 1/2 limit + ULP. | |
| 140 float newMinRadius = *minRadius * scale; | |
| 141 *minRadius = newMinRadius; | |
| 142 // Because newMaxRadius is the result of a double to float conversion, i
t can be larger | |
| 143 // than limit, but only by one ULP. | |
| 144 float newMaxRadius = (float)(limit - newMinRadius); | |
| 145 // If newMaxRadius is larger than the same value as a double, then it ne
eds to be | |
| 146 // reduced by one ULP to be less than limit - newMinRadius. | |
| 147 // Note: nexttowardf is a c99 call and should be std::nexttoward, but th
is is not | |
| 148 // implemented in the ARM compiler. | |
| 149 if (newMaxRadius > limit - newMinRadius) { | |
| 150 newMaxRadius = nexttowardf(newMaxRadius, limit - newMinRadius); | |
| 151 } | |
| 152 // This handles the case where both sets of radii are larger than a side
by differing | |
| 153 // scale factors. The one that needs the larger scale factor (the radii
with less | |
| 154 // overlap) will produce radii that are short enough just using the smal
ler scale factor | |
| 155 // from the side where the radii overlap is larger. | |
| 156 *maxRadius = SkMinScalar(scale * *maxRadius, newMaxRadius); | |
| 157 } else { | |
| 158 *a *= scale; | |
| 159 *b *= scale; | |
| 160 } | |
| 161 SkASSERT(*a >= 0.0f && *b >= 0.0f); | |
| 162 SkASSERT((*a + *b) <= limit); | |
| 163 } | |
| 164 | |
| 165 void SkRRect::setRectRadii(const SkRect& rect, const SkVector radii[4]) { | 124 void SkRRect::setRectRadii(const SkRect& rect, const SkVector radii[4]) { |
| 166 fRect = rect; | 125 fRect = rect; |
| 167 fRect.sort(); | 126 fRect.sort(); |
| 168 | 127 |
| 169 if (fRect.isEmpty() || !fRect.isFinite()) { | 128 if (fRect.isEmpty() || !fRect.isFinite()) { |
| 170 this->setEmpty(); | 129 this->setEmpty(); |
| 171 return; | 130 return; |
| 172 } | 131 } |
| 173 | 132 |
| 174 if (!SkScalarsAreFinite(&radii[0].fX, 8)) { | 133 if (!SkScalarsAreFinite(&radii[0].fX, 8)) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 172 |
| 214 // The sides of the rectangle may be larger than a float. | 173 // The sides of the rectangle may be larger than a float. |
| 215 double width = (double)fRect.fRight - (double)fRect.fLeft; | 174 double width = (double)fRect.fRight - (double)fRect.fLeft; |
| 216 double height = (double)fRect.fBottom - (double)fRect.fTop; | 175 double height = (double)fRect.fBottom - (double)fRect.fTop; |
| 217 scale = compute_min_scale(fRadii[0].fX, fRadii[1].fX, width, scale); | 176 scale = compute_min_scale(fRadii[0].fX, fRadii[1].fX, width, scale); |
| 218 scale = compute_min_scale(fRadii[1].fY, fRadii[2].fY, height, scale); | 177 scale = compute_min_scale(fRadii[1].fY, fRadii[2].fY, height, scale); |
| 219 scale = compute_min_scale(fRadii[2].fX, fRadii[3].fX, width, scale); | 178 scale = compute_min_scale(fRadii[2].fX, fRadii[3].fX, width, scale); |
| 220 scale = compute_min_scale(fRadii[3].fY, fRadii[0].fY, height, scale); | 179 scale = compute_min_scale(fRadii[3].fY, fRadii[0].fY, height, scale); |
| 221 | 180 |
| 222 if (scale < 1.0) { | 181 if (scale < 1.0) { |
| 223 adjust_radii(width, scale, &fRadii[0].fX, &fRadii[1].fX); | 182 ScaleToSides::AdjustRadii(width, scale, &fRadii[0].fX, &fRadii[1].fX); |
| 224 adjust_radii(height, scale, &fRadii[1].fY, &fRadii[2].fY); | 183 ScaleToSides::AdjustRadii(height, scale, &fRadii[1].fY, &fRadii[2].fY); |
| 225 adjust_radii(width, scale, &fRadii[2].fX, &fRadii[3].fX); | 184 ScaleToSides::AdjustRadii(width, scale, &fRadii[2].fX, &fRadii[3].fX); |
| 226 adjust_radii(height, scale, &fRadii[3].fY, &fRadii[0].fY); | 185 ScaleToSides::AdjustRadii(height, scale, &fRadii[3].fY, &fRadii[0].fY); |
| 227 } | 186 } |
| 228 | 187 |
| 229 // At this point we're either oval, simple, or complex (not empty or rect). | 188 // At this point we're either oval, simple, or complex (not empty or rect). |
| 230 this->computeType(); | 189 this->computeType(); |
| 231 | 190 |
| 232 SkDEBUGCODE(this->validate();) | 191 SkDEBUGCODE(this->validate();) |
| 233 } | 192 } |
| 234 | 193 |
| 235 // This method determines if a point known to be inside the RRect's bounds is | 194 // This method determines if a point known to be inside the RRect's bounds is |
| 236 // inside all the corners. | 195 // inside all the corners. |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 605 } | 564 } |
| 606 | 565 |
| 607 for (int i = 0; i < 4; ++i) { | 566 for (int i = 0; i < 4; ++i) { |
| 608 validate_radius_check_predicates(fRadii[i].fX, fRect.fLeft, fRect.fRight
); | 567 validate_radius_check_predicates(fRadii[i].fX, fRect.fLeft, fRect.fRight
); |
| 609 validate_radius_check_predicates(fRadii[i].fY, fRect.fTop, fRect.fBottom
); | 568 validate_radius_check_predicates(fRadii[i].fY, fRect.fTop, fRect.fBottom
); |
| 610 } | 569 } |
| 611 } | 570 } |
| 612 #endif // SK_DEBUG | 571 #endif // SK_DEBUG |
| 613 | 572 |
| 614 /////////////////////////////////////////////////////////////////////////////// | 573 /////////////////////////////////////////////////////////////////////////////// |
| OLD | NEW |