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

Side by Side Diff: ui/gfx/skia_util.cc

Issue 2231243002: Use CheckedNumeric when converting SkIRect to gfx::Rect. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: handle the negative case; add more comments and unit tests Created 4 years, 4 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 | « cc/output/filter_operations_unittest.cc ('k') | ui/gfx/skrect_conversion_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/skia_util.h" 5 #include "ui/gfx/skia_util.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "base/numerics/safe_conversions.h" 10 #include "base/numerics/safe_conversions.h"
11 #include "base/numerics/safe_math.h"
11 #include "third_party/skia/include/core/SkBitmap.h" 12 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "third_party/skia/include/core/SkColorFilter.h" 13 #include "third_party/skia/include/core/SkColorFilter.h"
13 #include "third_party/skia/include/core/SkColorPriv.h" 14 #include "third_party/skia/include/core/SkColorPriv.h"
14 #include "third_party/skia/include/core/SkUnPreMultiply.h" 15 #include "third_party/skia/include/core/SkUnPreMultiply.h"
15 #include "third_party/skia/include/effects/SkBlurMaskFilter.h" 16 #include "third_party/skia/include/effects/SkBlurMaskFilter.h"
16 #include "third_party/skia/include/effects/SkGradientShader.h" 17 #include "third_party/skia/include/effects/SkGradientShader.h"
17 #include "third_party/skia/include/effects/SkLayerDrawLooper.h" 18 #include "third_party/skia/include/effects/SkLayerDrawLooper.h"
18 #include "ui/gfx/geometry/quad_f.h" 19 #include "ui/gfx/geometry/quad_f.h"
19 #include "ui/gfx/geometry/rect.h" 20 #include "ui/gfx/geometry/rect.h"
20 #include "ui/gfx/geometry/rect_f.h" 21 #include "ui/gfx/geometry/rect_f.h"
(...skipping 18 matching lines...) Expand all
39 SkRect RectToSkRect(const Rect& rect) { 40 SkRect RectToSkRect(const Rect& rect) {
40 return SkRect::MakeXYWH( 41 return SkRect::MakeXYWH(
41 SkIntToScalar(rect.x()), SkIntToScalar(rect.y()), 42 SkIntToScalar(rect.x()), SkIntToScalar(rect.y()),
42 SkIntToScalar(rect.width()), SkIntToScalar(rect.height())); 43 SkIntToScalar(rect.width()), SkIntToScalar(rect.height()));
43 } 44 }
44 45
45 SkIRect RectToSkIRect(const Rect& rect) { 46 SkIRect RectToSkIRect(const Rect& rect) {
46 return SkIRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height()); 47 return SkIRect::MakeXYWH(rect.x(), rect.y(), rect.width(), rect.height());
47 } 48 }
48 49
50 // Produces a non-negative integer for the difference between min and max,
51 // yielding 0 if it would be negative and INT_MAX if it would overflow.
52 // This yields a length such that min+length is in range as well.
53 static int ClampLengthFromRange(int min, int max) {
54 if (min > max)
55 return 0;
56 return (base::CheckedNumeric<int>(max) - min)
57 .ValueOrDefault(std::numeric_limits<int>::max());
58 }
59
49 Rect SkIRectToRect(const SkIRect& rect) { 60 Rect SkIRectToRect(const SkIRect& rect) {
50 return Rect(rect.x(), rect.y(), rect.width(), rect.height()); 61 return Rect(rect.x(), rect.y(),
62 ClampLengthFromRange(rect.left(), rect.right()),
63 ClampLengthFromRange(rect.top(), rect.bottom()));
51 } 64 }
52 65
53 SkRect RectFToSkRect(const RectF& rect) { 66 SkRect RectFToSkRect(const RectF& rect) {
54 return SkRect::MakeXYWH(SkFloatToScalar(rect.x()), 67 return SkRect::MakeXYWH(SkFloatToScalar(rect.x()),
55 SkFloatToScalar(rect.y()), 68 SkFloatToScalar(rect.y()),
56 SkFloatToScalar(rect.width()), 69 SkFloatToScalar(rect.width()),
57 SkFloatToScalar(rect.height())); 70 SkFloatToScalar(rect.height()));
58 } 71 }
59 72
60 RectF SkRectToRectF(const SkRect& rect) { 73 RectF SkRectToRectF(const SkRect& rect) {
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 static const SkScalar kSkToHbRatio = SK_Scalar1 / kHbUnit1; 289 static const SkScalar kSkToHbRatio = SK_Scalar1 / kHbUnit1;
277 return kSkToHbRatio * value; 290 return kSkToHbRatio * value;
278 } 291 }
279 292
280 float HarfBuzzUnitsToFloat(int value) { 293 float HarfBuzzUnitsToFloat(int value) {
281 static const float kFloatToHbRatio = 1.0f / kHbUnit1; 294 static const float kFloatToHbRatio = 1.0f / kHbUnit1;
282 return kFloatToHbRatio * value; 295 return kFloatToHbRatio * value;
283 } 296 }
284 297
285 } // namespace gfx 298 } // namespace gfx
OLDNEW
« no previous file with comments | « cc/output/filter_operations_unittest.cc ('k') | ui/gfx/skrect_conversion_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698