Commit 53479ca9 authored by liberato@chromium.org's avatar liberato@chromium.org Committed by Commit Bot

Avoid saturation when computing LayoutSize aspect ratio.

8k video could cause LayoutUnit to saturate multiplication when
computing the correct aspect ratio.  This CL does the calculation in
floating point, instead.

Bug: 728303
Test: LayoutSizeTest
Change-Id: I632777e2f140b8203577b3e5e0761f3e7f891aa2
Reviewed-on: https://chromium-review.googlesource.com/773108Reviewed-by: default avatarPhilip Rogers <pdr@chromium.org>
Reviewed-by: default avatarDale Curtis <dalecurtis@chromium.org>
Commit-Queue: Frank Liberato <liberato@chromium.org>
Cr-Commit-Position: refs/heads/master@{#519784}
parent 20c7806a
...@@ -1802,6 +1802,7 @@ jumbo_source_set("blink_platform_unittests_sources") { ...@@ -1802,6 +1802,7 @@ jumbo_source_set("blink_platform_unittests_sources") {
"geometry/IntRectTest.cpp", "geometry/IntRectTest.cpp",
"geometry/LayoutRectOutsetsTest.cpp", "geometry/LayoutRectOutsetsTest.cpp",
"geometry/LayoutRectTest.cpp", "geometry/LayoutRectTest.cpp",
"geometry/LayoutSizeTest.cpp",
"geometry/RegionTest.cpp", "geometry/RegionTest.cpp",
"graphics/BitmapImageTest.cpp", "graphics/BitmapImageTest.cpp",
"graphics/CompositorElementIdTest.cpp", "graphics/CompositorElementIdTest.cpp",
......
...@@ -133,13 +133,17 @@ class PLATFORM_EXPORT LayoutSize { ...@@ -133,13 +133,17 @@ class PLATFORM_EXPORT LayoutSize {
LayoutSize FitToAspectRatio(const LayoutSize& aspect_ratio, LayoutSize FitToAspectRatio(const LayoutSize& aspect_ratio,
AspectRatioFit fit) const { AspectRatioFit fit) const {
float height_scale = Height().ToFloat() / aspect_ratio.Height().ToFloat(); const float height_float = Height().ToFloat();
float width_scale = Width().ToFloat() / aspect_ratio.Width().ToFloat(); const float width_float = Width().ToFloat();
if ((width_scale > height_scale) != (fit == kAspectRatioFitGrow)) float height_scale = height_float / aspect_ratio.Height().ToFloat();
return LayoutSize(Height() * aspect_ratio.Width() / aspect_ratio.Height(), float width_scale = width_float / aspect_ratio.Width().ToFloat();
Height()); if ((width_scale > height_scale) != (fit == kAspectRatioFitGrow)) {
return LayoutSize(Width(), return LayoutSize(
Width() * aspect_ratio.Height() / aspect_ratio.Width()); height_float * aspect_ratio.Width() / aspect_ratio.Height(),
Height());
}
return LayoutSize(
Width(), width_float * aspect_ratio.Height() / aspect_ratio.Width());
} }
LayoutSize Fraction() const { LayoutSize Fraction() const {
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "platform/geometry/LayoutSize.h"
#include "platform/wtf/text/WTFString.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
TEST(LayoutSizeTest, FitToAspectRatioDoesntOverflow) {
// FitToAspectRatio() shouldn't overflow due to intermediate calculations,
// for both the "constrained by width" and "constrained by height" cases.
LayoutSize aspect_ratio(50000, 50000);
EXPECT_EQ("1000x1000",
LayoutSize(2000, 1000)
.FitToAspectRatio(aspect_ratio, kAspectRatioFitShrink)
.ToString());
LayoutSize size(1000, 2000);
EXPECT_EQ("1000x1000",
LayoutSize(1000, 2000)
.FitToAspectRatio(aspect_ratio, kAspectRatioFitShrink)
.ToString());
}
} // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment