OLD | NEW |
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/win/dpi.h" | 5 #include "ui/gfx/win/dpi.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/win/scoped_hdc.h" | 9 #include "base/win/scoped_hdc.h" |
10 #include "base/win/windows_version.h" | 10 #include "base/win/windows_version.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 return is_process_dpi_aware_func(); | 31 return is_process_dpi_aware_func(); |
32 return FALSE; | 32 return FALSE; |
33 } | 33 } |
34 | 34 |
35 float g_device_scale_factor = 0.0f; | 35 float g_device_scale_factor = 0.0f; |
36 | 36 |
37 float GetUnforcedDeviceScaleFactor() { | 37 float GetUnforcedDeviceScaleFactor() { |
38 // If the global device scale factor is initialized use it. This is to ensure | 38 // If the global device scale factor is initialized use it. This is to ensure |
39 // we use the same scale factor across all callsites. We don't use the | 39 // we use the same scale factor across all callsites. We don't use the |
40 // GetDeviceScaleFactor function here because it fires a DCHECK if the | 40 // GetDeviceScaleFactor function here because it fires a DCHECK if the |
41 // g_device_scale_factor global is 0. | 41 // g_device_scale_factor global is 0. |
42 if (g_device_scale_factor) | 42 if (g_device_scale_factor) |
43 return g_device_scale_factor; | 43 return g_device_scale_factor; |
44 return static_cast<float>(gfx::GetDPI().width()) / | 44 return static_cast<float>(gfx::GetDPI().width()) / |
45 static_cast<float>(kDefaultDPIX); | 45 static_cast<float>(kDefaultDPIX); |
46 } | 46 } |
47 | 47 |
48 // Duplicated from Win8.1 SDK ShellScalingApi.h | 48 // Duplicated from Win8.1 SDK ShellScalingApi.h |
49 typedef enum PROCESS_DPI_AWARENESS { | 49 typedef enum PROCESS_DPI_AWARENESS { |
50 PROCESS_DPI_UNAWARE = 0, | 50 PROCESS_DPI_UNAWARE = 0, |
51 PROCESS_SYSTEM_DPI_AWARE = 1, | 51 PROCESS_SYSTEM_DPI_AWARE = 1, |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 L"Software\\Google\\Chrome\\Profile"; | 177 L"Software\\Google\\Chrome\\Profile"; |
178 GFX_EXPORT const wchar_t kHighDPISupportW[] = L"high-dpi-support"; | 178 GFX_EXPORT const wchar_t kHighDPISupportW[] = L"high-dpi-support"; |
179 | 179 |
180 float GetDeviceScaleFactor() { | 180 float GetDeviceScaleFactor() { |
181 DCHECK_NE(0.0f, g_device_scale_factor); | 181 DCHECK_NE(0.0f, g_device_scale_factor); |
182 return g_device_scale_factor; | 182 return g_device_scale_factor; |
183 } | 183 } |
184 | 184 |
185 Point ScreenToDIPPoint(const Point& pixel_point) { | 185 Point ScreenToDIPPoint(const Point& pixel_point) { |
186 return ToFlooredPoint(ScalePoint(pixel_point, | 186 return ToFlooredPoint(ScalePoint(pixel_point, |
187 1.0f / GetDeviceScaleFactor())); | 187 1.0f / GetDeviceScaleFactor())); |
188 } | 188 } |
189 | 189 |
190 Point DIPToScreenPoint(const Point& dip_point) { | 190 Point DIPToScreenPoint(const Point& dip_point) { |
191 return ToFlooredPoint(ScalePoint(dip_point, GetDeviceScaleFactor())); | 191 return ToFlooredPoint(ScalePoint(dip_point, GetDeviceScaleFactor())); |
192 } | 192 } |
193 | 193 |
194 Rect ScreenToDIPRect(const Rect& pixel_bounds) { | 194 Rect ScreenToDIPRect(const Rect& pixel_bounds) { |
195 // TODO(kevers): Switch to non-deprecated method for float to int conversions. | 195 // It's important we scale the origin and size separately. If we instead |
196 return ToFlooredRectDeprecated( | 196 // calculated the size from the floored origin and ceiled right the size could |
197 ScaleRect(pixel_bounds, 1.0f / GetDeviceScaleFactor())); | 197 // vary depending upon where the two points land. That would cause problems |
| 198 // for the places this code is used (in particular mapping from native window |
| 199 // bounds to DIPs). |
| 200 return Rect(ScreenToDIPPoint(pixel_bounds.origin()), |
| 201 ScreenToDIPSize(pixel_bounds.size())); |
198 } | 202 } |
199 | 203 |
200 Rect DIPToScreenRect(const Rect& dip_bounds) { | 204 Rect DIPToScreenRect(const Rect& dip_bounds) { |
201 // We scale the origin by the scale factor and round up via ceil. This | 205 // See comment in ScreenToDIPRect for why we calculate size like this. |
202 // ensures that we get the original logical origin back when we scale down. | 206 return Rect(DIPToScreenPoint(dip_bounds.origin()), |
203 // We round the size down after scaling. It may be better to round this up | 207 DIPToScreenSize(dip_bounds.size())); |
204 // on the same lines as the origin. | |
205 // TODO(ananta) | |
206 // Investigate if rounding size up on the same lines as origin is workable. | |
207 return gfx::Rect( | |
208 gfx::ToCeiledPoint(gfx::ScalePoint( | |
209 dip_bounds.origin(), GetDeviceScaleFactor())), | |
210 gfx::ToFlooredSize(gfx::ScaleSize( | |
211 dip_bounds.size(), GetDeviceScaleFactor()))); | |
212 } | 208 } |
213 | 209 |
214 Size ScreenToDIPSize(const Size& size_in_pixels) { | 210 Size ScreenToDIPSize(const Size& size_in_pixels) { |
215 return ToFlooredSize( | 211 // Always ceil sizes. Otherwise we may be leaving off part of the bounds. |
| 212 return ToCeiledSize( |
216 ScaleSize(size_in_pixels, 1.0f / GetDeviceScaleFactor())); | 213 ScaleSize(size_in_pixels, 1.0f / GetDeviceScaleFactor())); |
217 } | 214 } |
218 | 215 |
219 Size DIPToScreenSize(const Size& dip_size) { | 216 Size DIPToScreenSize(const Size& dip_size) { |
220 return ToFlooredSize(ScaleSize(dip_size, GetDeviceScaleFactor())); | 217 // Always ceil sizes. Otherwise we may be leaving off part of the bounds. |
| 218 return ToCeiledSize(ScaleSize(dip_size, GetDeviceScaleFactor())); |
221 } | 219 } |
222 | 220 |
223 int GetSystemMetricsInDIP(int metric) { | 221 int GetSystemMetricsInDIP(int metric) { |
224 return static_cast<int>(GetSystemMetrics(metric) / | 222 return static_cast<int>(GetSystemMetrics(metric) / |
225 GetDeviceScaleFactor() + 0.5); | 223 GetDeviceScaleFactor() + 0.5); |
226 } | 224 } |
227 | 225 |
228 bool IsDeviceScaleFactorSet() { | 226 bool IsDeviceScaleFactorSet() { |
229 return g_device_scale_factor != 0.0f; | 227 return g_device_scale_factor != 0.0f; |
230 } | 228 } |
231 | 229 |
232 } // namespace win | 230 } // namespace win |
233 } // namespace gfx | 231 } // namespace gfx |
OLD | NEW |