Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(298)

Side by Side Diff: src/core/SkScaleToSides.h

Issue 1617763003: Fix bounds of checking if a radii are too long for a side. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Address comment. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fuzz/FuzzScaleToSides.cpp ('k') | tests/ScaleToSidesTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
12 #include "SkScalar.h" 12 #include "SkScalar.h"
13 #include "SkTypes.h" 13 #include "SkTypes.h"
14 14
15 class ScaleToSides { 15 class ScaleToSides {
16 public: 16 public:
17 // This code assumes that a and b fit in in a float, and therefore the resul ting smaller value 17 // This code assumes that a and b fit in a float, and therefore the resultin g smaller value
18 // of a and b will fit in a float. The side of the rectangle may be larger t han a float. 18 // 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). 19 // 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. 20 // This code assumes that NaN and Inf are never passed in.
21 static void AdjustRadii(double limit, double scale, SkScalar* a, SkScalar* b ) { 21 static void AdjustRadii(double limit, double scale, SkScalar* a, SkScalar* b ) {
22 SkASSERTF(scale < 1.0 && scale > 0.0, "scale: %g", scale); 22 SkASSERTF(scale < 1.0 && scale > 0.0, "scale: %g", scale);
23 23
24 *a = (float)((double)*a * scale); 24 *a = (float)((double)*a * scale);
25 *b = (float)((double)*b * scale); 25 *b = (float)((double)*b * scale);
26 26
27 // This check is conservative. (double)*a + (double)*b >= (double)(*a + *b) 27 if (*a + *b > limit) {
28 if ((double)*a + (double)*b > limit) {
29 float* minRadius = a; 28 float* minRadius = a;
30 float* maxRadius = b; 29 float* maxRadius = b;
31 30
32 // Force minRadius to be the smaller of the two. 31 // Force minRadius to be the smaller of the two.
33 if (*minRadius > *maxRadius) { 32 if (*minRadius > *maxRadius) {
34 SkTSwap(minRadius, maxRadius); 33 SkTSwap(minRadius, maxRadius);
35 } 34 }
36 35
37 // newMinRadius must be float in order to give the actual value of t he radius. 36 // 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 37 // 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 38 // 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. 39 // division, minRadius can be no larger than 1/2 limit + ULP.
41 float newMinRadius = *minRadius; 40 float newMinRadius = *minRadius;
42 41
43 // Because newMaxRadius is the result of a double to float conversio n, it can be larger 42 // Because newMaxRadius is the result of a double to float conversio n, it can be larger
44 // than limit, but only by one ULP. 43 // than limit, but only by one ULP.
45 float newMaxRadius = (float)(limit - newMinRadius); 44 float newMaxRadius = (float)(limit - newMinRadius);
46 45
47 // If newMaxRadius forces the total over the limit, then it needs to be 46 // If newMaxRadius forces the total over the limit, then it needs to be
48 // reduced by one ULP to be less than limit - newMinRadius. 47 // 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 48 // Note: nexttowardf is a c99 call and should be std::nexttoward, bu t this is not
50 // implemented in the ARM compiler. 49 // implemented in the ARM compiler.
51 if ((double)newMaxRadius + (double)newMinRadius > limit) { 50 if (newMaxRadius + newMinRadius > limit) {
52 newMaxRadius = nexttowardf(newMaxRadius, 0.0); 51 newMaxRadius = nexttowardf(newMaxRadius, 0.0);
53 } 52 }
54 *maxRadius = newMaxRadius; 53 *maxRadius = newMaxRadius;
55 } 54 }
56 55
57 SkASSERTF(*a >= 0.0f && *b >= 0.0f, "a: %g, b: %g", *a, *b); 56 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); 57 scale);
58 SkASSERTF(*a + *b <= limit, "\nlimit: %.10f, a: %.10f, b: %.10f, scale: %.20f",
59 limit, *a, *b, scale);
59 } 60 }
60 }; 61 };
61 #endif // ScaleToSides_DEFINED 62 #endif // ScaleToSides_DEFINED
OLDNEW
« no previous file with comments | « fuzz/FuzzScaleToSides.cpp ('k') | tests/ScaleToSidesTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698