Commit e9bc87f3 authored by sky's avatar sky Committed by Commit bot

Changes dip conversions to ceil size and floor origin

This way the size should always remain the same regardless of the
origin. Also, it's important that we ceil here otherwise a round trip
for DIP to screen to DIP could result in a smaller value resulting in
all sorts of wierdness.

BUG=417392
TEST=see bug
R=ananta@chromium.org

Review URL: https://codereview.chromium.org/600103002

Cr-Commit-Position: refs/heads/master@{#296559}
parent 5ae838bb
......@@ -38,7 +38,7 @@ float GetUnforcedDeviceScaleFactor() {
// If the global device scale factor is initialized use it. This is to ensure
// we use the same scale factor across all callsites. We don't use the
// GetDeviceScaleFactor function here because it fires a DCHECK if the
// g_device_scale_factor global is 0.
// g_device_scale_factor global is 0.
if (g_device_scale_factor)
return g_device_scale_factor;
return static_cast<float>(gfx::GetDPI().width()) /
......@@ -184,7 +184,7 @@ float GetDeviceScaleFactor() {
Point ScreenToDIPPoint(const Point& pixel_point) {
return ToFlooredPoint(ScalePoint(pixel_point,
1.0f / GetDeviceScaleFactor()));
1.0f / GetDeviceScaleFactor()));
}
Point DIPToScreenPoint(const Point& dip_point) {
......@@ -192,32 +192,30 @@ Point DIPToScreenPoint(const Point& dip_point) {
}
Rect ScreenToDIPRect(const Rect& pixel_bounds) {
// TODO(kevers): Switch to non-deprecated method for float to int conversions.
return ToFlooredRectDeprecated(
ScaleRect(pixel_bounds, 1.0f / GetDeviceScaleFactor()));
// It's important we scale the origin and size separately. If we instead
// calculated the size from the floored origin and ceiled right the size could
// vary depending upon where the two points land. That would cause problems
// for the places this code is used (in particular mapping from native window
// bounds to DIPs).
return Rect(ScreenToDIPPoint(pixel_bounds.origin()),
ScreenToDIPSize(pixel_bounds.size()));
}
Rect DIPToScreenRect(const Rect& dip_bounds) {
// We scale the origin by the scale factor and round up via ceil. This
// ensures that we get the original logical origin back when we scale down.
// We round the size down after scaling. It may be better to round this up
// on the same lines as the origin.
// TODO(ananta)
// Investigate if rounding size up on the same lines as origin is workable.
return gfx::Rect(
gfx::ToCeiledPoint(gfx::ScalePoint(
dip_bounds.origin(), GetDeviceScaleFactor())),
gfx::ToFlooredSize(gfx::ScaleSize(
dip_bounds.size(), GetDeviceScaleFactor())));
// See comment in ScreenToDIPRect for why we calculate size like this.
return Rect(DIPToScreenPoint(dip_bounds.origin()),
DIPToScreenSize(dip_bounds.size()));
}
Size ScreenToDIPSize(const Size& size_in_pixels) {
return ToFlooredSize(
// Always ceil sizes. Otherwise we may be leaving off part of the bounds.
return ToCeiledSize(
ScaleSize(size_in_pixels, 1.0f / GetDeviceScaleFactor()));
}
Size DIPToScreenSize(const Size& dip_size) {
return ToFlooredSize(ScaleSize(dip_size, GetDeviceScaleFactor()));
// Always ceil sizes. Otherwise we may be leaving off part of the bounds.
return ToCeiledSize(ScaleSize(dip_size, GetDeviceScaleFactor()));
}
int GetSystemMetricsInDIP(int metric) {
......
......@@ -22,7 +22,7 @@ GFX_EXPORT void InitDeviceScaleFactor(float scale);
GFX_EXPORT Size GetDPI();
// Gets the scale factor of the display. For example, if the display DPI is
// 96 then the scale factor is 1.0.
// 96 then the scale factor is 1.0.
GFX_EXPORT float GetDPIScale();
// Tests to see if the command line flag "--high-dpi-support" is set.
......@@ -44,12 +44,14 @@ GFX_EXPORT Point ScreenToDIPPoint(const Point& pixel_point);
GFX_EXPORT Point DIPToScreenPoint(const Point& dip_point);
// WARNING: there is no right way to scale sizes and rects. The implementation
// of these strives to maintain a constant size by scaling the size independent
// of the origin. An alternative is to get the enclosing rect, which is the
// right way for some situations. Understand which you need before blindly
// assuming this is the right way.
GFX_EXPORT Rect ScreenToDIPRect(const Rect& pixel_bounds);
GFX_EXPORT Rect DIPToScreenRect(const Rect& dip_bounds);
GFX_EXPORT Size ScreenToDIPSize(const Size& size_in_pixels);
GFX_EXPORT Size DIPToScreenSize(const Size& dip_size);
// Win32's GetSystemMetrics uses pixel measures. This function calls
......
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