Chromium Code Reviews| 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/win/scoped_hdc.h" | 8 #include "base/win/scoped_hdc.h" |
| 9 #include "base/win/windows_version.h" | 9 #include "base/win/windows_version.h" |
| 10 #include "base/win/registry.h" | 10 #include "base/win/registry.h" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 IsProcessDPIAwarePtr is_process_dpi_aware_func = | 25 IsProcessDPIAwarePtr is_process_dpi_aware_func = |
| 26 reinterpret_cast<IsProcessDPIAwarePtr>( | 26 reinterpret_cast<IsProcessDPIAwarePtr>( |
| 27 GetProcAddress(GetModuleHandleA("user32.dll"), "IsProcessDPIAware")); | 27 GetProcAddress(GetModuleHandleA("user32.dll"), "IsProcessDPIAware")); |
| 28 if (is_process_dpi_aware_func) | 28 if (is_process_dpi_aware_func) |
| 29 return is_process_dpi_aware_func(); | 29 return is_process_dpi_aware_func(); |
| 30 return FALSE; | 30 return FALSE; |
| 31 } | 31 } |
| 32 | 32 |
| 33 float g_device_scale_factor = 0.0f; | 33 float g_device_scale_factor = 0.0f; |
| 34 | 34 |
| 35 float GetUnforcedDeviceScaleFactor() { | 35 float GetUnforcedDeviceScaleFactor() { |
|
sky
2014/10/23 16:51:51
Can you reanme devicescalefactor to dpiscale every
| |
| 36 // If the global device scale factor is initialized use it. This is to ensure | 36 // If the global device scale factor is initialized use it. This is to ensure |
| 37 // we use the same scale factor across all callsites. We don't use the | 37 // we use the same scale factor across all callsites. |
| 38 // GetDeviceScaleFactor function here because it fires a DCHECK if the | |
| 39 // g_device_scale_factor global is 0. | |
| 40 if (g_device_scale_factor) | 38 if (g_device_scale_factor) |
| 41 return g_device_scale_factor; | 39 return g_device_scale_factor; |
| 42 return static_cast<float>(gfx::GetDPI().width()) / | 40 return static_cast<float>(gfx::GetDPI().width()) / |
| 43 static_cast<float>(kDefaultDPIX); | 41 static_cast<float>(kDefaultDPIX); |
| 44 } | 42 } |
| 45 | 43 |
| 46 // Duplicated from Win8.1 SDK ShellScalingApi.h | 44 // Duplicated from Win8.1 SDK ShellScalingApi.h |
| 47 typedef enum PROCESS_DPI_AWARENESS { | 45 typedef enum PROCESS_DPI_AWARENESS { |
| 48 PROCESS_DPI_UNAWARE = 0, | 46 PROCESS_DPI_UNAWARE = 0, |
| 49 PROCESS_SYSTEM_DPI_AWARE = 1, | 47 PROCESS_SYSTEM_DPI_AWARE = 1, |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 SetProcessDPIAwareWrapper(); | 163 SetProcessDPIAwareWrapper(); |
| 166 } | 164 } |
| 167 } | 165 } |
| 168 | 166 |
| 169 namespace win { | 167 namespace win { |
| 170 | 168 |
| 171 GFX_EXPORT const wchar_t kRegistryProfilePath[] = | 169 GFX_EXPORT const wchar_t kRegistryProfilePath[] = |
| 172 L"Software\\Google\\Chrome\\Profile"; | 170 L"Software\\Google\\Chrome\\Profile"; |
| 173 GFX_EXPORT const wchar_t kHighDPISupportW[] = L"high-dpi-support"; | 171 GFX_EXPORT const wchar_t kHighDPISupportW[] = L"high-dpi-support"; |
| 174 | 172 |
| 175 float GetDeviceScaleFactor() { | |
| 176 DCHECK_NE(0.0f, g_device_scale_factor); | |
| 177 return g_device_scale_factor; | |
| 178 } | |
| 179 | |
| 180 Point ScreenToDIPPoint(const Point& pixel_point) { | 173 Point ScreenToDIPPoint(const Point& pixel_point) { |
| 181 return ToFlooredPoint(ScalePoint(pixel_point, | 174 return ToFlooredPoint(ScalePoint(pixel_point, 1.0f / GetDPIScale())); |
| 182 1.0f / GetDeviceScaleFactor())); | |
| 183 } | 175 } |
| 184 | 176 |
| 185 Point DIPToScreenPoint(const Point& dip_point) { | 177 Point DIPToScreenPoint(const Point& dip_point) { |
| 186 return ToFlooredPoint(ScalePoint(dip_point, GetDeviceScaleFactor())); | 178 return ToFlooredPoint(ScalePoint(dip_point, GetDPIScale())); |
| 187 } | 179 } |
| 188 | 180 |
| 189 Rect ScreenToDIPRect(const Rect& pixel_bounds) { | 181 Rect ScreenToDIPRect(const Rect& pixel_bounds) { |
| 190 // It's important we scale the origin and size separately. If we instead | 182 // It's important we scale the origin and size separately. If we instead |
| 191 // calculated the size from the floored origin and ceiled right the size could | 183 // calculated the size from the floored origin and ceiled right the size could |
| 192 // vary depending upon where the two points land. That would cause problems | 184 // vary depending upon where the two points land. That would cause problems |
| 193 // for the places this code is used (in particular mapping from native window | 185 // for the places this code is used (in particular mapping from native window |
| 194 // bounds to DIPs). | 186 // bounds to DIPs). |
| 195 return Rect(ScreenToDIPPoint(pixel_bounds.origin()), | 187 return Rect(ScreenToDIPPoint(pixel_bounds.origin()), |
| 196 ScreenToDIPSize(pixel_bounds.size())); | 188 ScreenToDIPSize(pixel_bounds.size())); |
| 197 } | 189 } |
| 198 | 190 |
| 199 Rect DIPToScreenRect(const Rect& dip_bounds) { | 191 Rect DIPToScreenRect(const Rect& dip_bounds) { |
| 200 // See comment in ScreenToDIPRect for why we calculate size like this. | 192 // See comment in ScreenToDIPRect for why we calculate size like this. |
| 201 return Rect(DIPToScreenPoint(dip_bounds.origin()), | 193 return Rect(DIPToScreenPoint(dip_bounds.origin()), |
| 202 DIPToScreenSize(dip_bounds.size())); | 194 DIPToScreenSize(dip_bounds.size())); |
| 203 } | 195 } |
| 204 | 196 |
| 205 Size ScreenToDIPSize(const Size& size_in_pixels) { | 197 Size ScreenToDIPSize(const Size& size_in_pixels) { |
| 206 // Always ceil sizes. Otherwise we may be leaving off part of the bounds. | 198 // Always ceil sizes. Otherwise we may be leaving off part of the bounds. |
| 207 return ToCeiledSize( | 199 return ToCeiledSize(ScaleSize(size_in_pixels, 1.0f / GetDPIScale())); |
| 208 ScaleSize(size_in_pixels, 1.0f / GetDeviceScaleFactor())); | |
| 209 } | 200 } |
| 210 | 201 |
| 211 Size DIPToScreenSize(const Size& dip_size) { | 202 Size DIPToScreenSize(const Size& dip_size) { |
| 212 // Always ceil sizes. Otherwise we may be leaving off part of the bounds. | 203 // Always ceil sizes. Otherwise we may be leaving off part of the bounds. |
| 213 return ToCeiledSize(ScaleSize(dip_size, GetDeviceScaleFactor())); | 204 return ToCeiledSize(ScaleSize(dip_size, GetDPIScale())); |
| 214 } | 205 } |
| 215 | 206 |
| 216 int GetSystemMetricsInDIP(int metric) { | 207 int GetSystemMetricsInDIP(int metric) { |
| 217 return static_cast<int>(GetSystemMetrics(metric) / | 208 return static_cast<int>(GetSystemMetrics(metric) / GetDPIScale() + 0.5); |
| 218 GetDeviceScaleFactor() + 0.5); | |
| 219 } | 209 } |
| 220 | 210 |
| 221 bool IsDeviceScaleFactorSet() { | 211 bool IsDeviceScaleFactorSet() { |
| 222 return g_device_scale_factor != 0.0f; | 212 return g_device_scale_factor != 0.0f; |
| 223 } | 213 } |
| 224 | 214 |
| 225 } // namespace win | 215 } // namespace win |
| 226 } // namespace gfx | 216 } // namespace gfx |
| OLD | NEW |