OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 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 #ifndef SkScaleToSides_DEFINED | 8 #ifndef SkScaleToSides_DEFINED |
9 #define SkScaleToSides_DEFINED | 9 #define SkScaleToSides_DEFINED |
10 | 10 |
11 #include <cmath> | 11 #include <cmath> |
robertphillips
2016/01/22 14:42:51
Do we actually need <limits> ?
herb_g
2016/01/22 16:01:05
Done.
| |
12 #include <limits> | |
12 #include "SkScalar.h" | 13 #include "SkScalar.h" |
13 #include "SkTypes.h" | 14 #include "SkTypes.h" |
14 | 15 |
15 class ScaleToSides { | 16 class ScaleToSides { |
16 public: | 17 public: |
robertphillips
2016/01/22 14:42:51
in in -> in ?
herb_g
2016/01/22 16:01:05
Done.
| |
17 // This code assumes that a and b fit in in a float, and therefore the resul ting smaller value | 18 // This code assumes that a and b fit in in a float, and therefore the resul ting smaller value |
18 // of a and b will fit in a float. The side of the rectangle may be larger t han a float. | 19 // of a and b will fit in a float. The side of the rectangle may be larger t han a float. |
19 // Scale must be less than or equal to the ratio limit / (*a + *b). | 20 // Scale must be less than or equal to the ratio limit / (*a + *b). |
20 // This code assumes that NaN and Inf are never passed in. | 21 // This code assumes that NaN and Inf are never passed in. |
21 static void AdjustRadii(double limit, double scale, SkScalar* a, SkScalar* b ) { | 22 static void AdjustRadii(double limit, double scale, SkScalar* a, SkScalar* b ) { |
22 SkASSERTF(scale < 1.0 && scale > 0.0, "scale: %g", scale); | 23 SkASSERTF(scale < 1.0 && scale > 0.0, "scale: %g", scale); |
23 | 24 |
24 *a = (float)((double)*a * scale); | 25 *a = (float)((double)*a * scale); |
25 *b = (float)((double)*b * scale); | 26 *b = (float)((double)*b * scale); |
26 | 27 |
27 // This check is conservative. (double)*a + (double)*b >= (double)(*a + *b) | 28 if (*a + *b > limit) { |
28 if ((double)*a + (double)*b > limit) { | |
29 float* minRadius = a; | 29 float* minRadius = a; |
30 float* maxRadius = b; | 30 float* maxRadius = b; |
31 | 31 |
32 // Force minRadius to be the smaller of the two. | 32 // Force minRadius to be the smaller of the two. |
33 if (*minRadius > *maxRadius) { | 33 if (*minRadius > *maxRadius) { |
34 SkTSwap(minRadius, maxRadius); | 34 SkTSwap(minRadius, maxRadius); |
35 } | 35 } |
36 | 36 |
37 // newMinRadius must be float in order to give the actual value of t he radius. | 37 // newMinRadius must be float in order to give the actual value of t he radius. |
38 // The newMinRadius will always be smaller than limit. The largest t hat minRadius can be | 38 // The newMinRadius will always be smaller than limit. The largest t hat minRadius can be |
39 // is 1/2 the ratio of minRadius : (minRadius + maxRadius), therefor e in the resulting | 39 // is 1/2 the ratio of minRadius : (minRadius + maxRadius), therefor e in the resulting |
40 // division, minRadius can be no larger than 1/2 limit + ULP. | 40 // division, minRadius can be no larger than 1/2 limit + ULP. |
41 float newMinRadius = *minRadius; | 41 float newMinRadius = *minRadius; |
42 | 42 |
43 // Because newMaxRadius is the result of a double to float conversio n, it can be larger | 43 // Because newMaxRadius is the result of a double to float conversio n, it can be larger |
44 // than limit, but only by one ULP. | 44 // than limit, but only by one ULP. |
45 float newMaxRadius = (float)(limit - newMinRadius); | 45 float newMaxRadius = (float)(limit - newMinRadius); |
46 | 46 |
47 // If newMaxRadius forces the total over the limit, then it needs to be | 47 // If newMaxRadius forces the total over the limit, then it needs to be |
48 // reduced by one ULP to be less than limit - newMinRadius. | 48 // reduced by one ULP to be less than limit - newMinRadius. |
49 // Note: nexttowardf is a c99 call and should be std::nexttoward, bu t this is not | 49 // Note: nexttowardf is a c99 call and should be std::nexttoward, bu t this is not |
50 // implemented in the ARM compiler. | 50 // implemented in the ARM compiler. |
51 if ((double)newMaxRadius + (double)newMinRadius > limit) { | 51 if (newMaxRadius + newMinRadius > limit) { |
52 newMaxRadius = nexttowardf(newMaxRadius, 0.0); | 52 newMaxRadius = nexttowardf(newMaxRadius, 0.0); |
53 } | 53 } |
54 *maxRadius = newMaxRadius; | 54 *maxRadius = newMaxRadius; |
55 } | 55 } |
56 | 56 |
57 SkASSERTF(*a >= 0.0f && *b >= 0.0f, "a: %g, b: %g", *a, *b); | 57 SkASSERTF(*a >= 0.0f && *b >= 0.0f, "a: %g, b: %g, limit: %g, scale: %g" , *a, *b, limit, |
58 SkASSERTF((*a + *b) <= limit, "limit: %g, a: %g, b: %g", limit, *a, *b); | 58 scale); |
59 SkASSERTF(*a + *b <= limit, "\nlimit: %.10f, a: %.10f, b: %.10f, scale: %.20f", | |
60 limit, *a, *b, scale); | |
59 } | 61 } |
60 }; | 62 }; |
61 #endif // ScaleToSides_DEFINED | 63 #endif // ScaleToSides_DEFINED |
OLD | NEW |