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

Unified Diff: ui/gfx/dpi.cc

Issue 659883002: Enable hidpi on Linux, refactor a bit on Windows to share Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: resource bundle loading Created 6 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: ui/gfx/dpi.cc
diff --git a/ui/gfx/win/dpi.cc b/ui/gfx/dpi.cc
similarity index 88%
rename from ui/gfx/win/dpi.cc
rename to ui/gfx/dpi.cc
index 2777f7cb01235bb838de9c8e7b42343a1f5f2499..38a740b4604c7cf18467560c592d11c0ddc414fc 100644
--- a/ui/gfx/win/dpi.cc
+++ b/ui/gfx/dpi.cc
@@ -2,33 +2,28 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "ui/gfx/win/dpi.h"
+#include "ui/gfx/dpi.h"
-#include <windows.h>
-#include "base/win/scoped_hdc.h"
-#include "base/win/windows_version.h"
-#include "base/win/registry.h"
+#include "base/logging.h"
#include "ui/gfx/display.h"
#include "ui/gfx/point_conversions.h"
#include "ui/gfx/rect_conversions.h"
#include "ui/gfx/size_conversions.h"
-namespace {
+#if defined(OS_WIN)
+#include <windows.h>
+#include "base/win/registry.h"
+#include "base/win/scoped_hdc.h"
+#include "base/win/windows_version.h"
+#endif
-int kDefaultDPIX = 96;
-int kDefaultDPIY = 96;
+#if defined(OS_LINUX)
+#include "ui/gfx/linux_font_delegate.h"
+#endif
-bool force_highdpi_for_testing = false;
+namespace {
-BOOL IsProcessDPIAwareWrapper() {
- typedef BOOL(WINAPI *IsProcessDPIAwarePtr)(VOID);
- IsProcessDPIAwarePtr is_process_dpi_aware_func =
- reinterpret_cast<IsProcessDPIAwarePtr>(
- GetProcAddress(GetModuleHandleA("user32.dll"), "IsProcessDPIAware"));
- if (is_process_dpi_aware_func)
- return is_process_dpi_aware_func();
- return FALSE;
-}
+int kDefaultDPI = 96;
float g_device_scale_factor = 0.0f;
@@ -40,55 +35,10 @@ float GetUnforcedDeviceScaleFactor() {
if (g_device_scale_factor)
return g_device_scale_factor;
return static_cast<float>(gfx::GetDPI().width()) /
- static_cast<float>(kDefaultDPIX);
-}
-
-// Duplicated from Win8.1 SDK ShellScalingApi.h
-typedef enum PROCESS_DPI_AWARENESS {
- PROCESS_DPI_UNAWARE = 0,
- PROCESS_SYSTEM_DPI_AWARE = 1,
- PROCESS_PER_MONITOR_DPI_AWARE = 2
-} PROCESS_DPI_AWARENESS;
-
-typedef enum MONITOR_DPI_TYPE {
- MDT_EFFECTIVE_DPI = 0,
- MDT_ANGULAR_DPI = 1,
- MDT_RAW_DPI = 2,
- MDT_DEFAULT = MDT_EFFECTIVE_DPI
-} MONITOR_DPI_TYPE;
-
-// Win8.1 supports monitor-specific DPI scaling.
-bool SetProcessDpiAwarenessWrapper(PROCESS_DPI_AWARENESS value) {
- typedef BOOL(WINAPI *SetProcessDpiAwarenessPtr)(PROCESS_DPI_AWARENESS);
- SetProcessDpiAwarenessPtr set_process_dpi_awareness_func =
- reinterpret_cast<SetProcessDpiAwarenessPtr>(
- GetProcAddress(GetModuleHandleA("user32.dll"),
- "SetProcessDpiAwarenessInternal"));
- if (set_process_dpi_awareness_func) {
- HRESULT hr = set_process_dpi_awareness_func(value);
- if (SUCCEEDED(hr)) {
- VLOG(1) << "SetProcessDpiAwareness succeeded.";
- return true;
- } else if (hr == E_ACCESSDENIED) {
- LOG(ERROR) << "Access denied error from SetProcessDpiAwareness. "
- "Function called twice, or manifest was used.";
- }
- }
- return false;
-}
-
-// This function works for Windows Vista through Win8. Win8.1 must use
-// SetProcessDpiAwareness[Wrapper]
-BOOL SetProcessDPIAwareWrapper() {
- typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID);
- SetProcessDPIAwarePtr set_process_dpi_aware_func =
- reinterpret_cast<SetProcessDPIAwarePtr>(
- GetProcAddress(GetModuleHandleA("user32.dll"),
- "SetProcessDPIAware"));
- return set_process_dpi_aware_func &&
- set_process_dpi_aware_func();
+ static_cast<float>(kDefaultDPI);
}
+#if defined(OS_WIN)
DWORD ReadRegistryValue(HKEY root,
const wchar_t* base_key,
const wchar_t* value_name,
@@ -103,9 +53,11 @@ DWORD ReadRegistryValue(HKEY root,
}
return default_value;
}
+#endif
} // namespace
+
namespace gfx {
void InitDeviceScaleFactor(float scale) {
@@ -113,6 +65,23 @@ void InitDeviceScaleFactor(float scale) {
g_device_scale_factor = scale;
}
+float GetDeviceScaleFactor() {
+ DCHECK_NE(0.0f, g_device_scale_factor);
+ return g_device_scale_factor;
+}
+
+bool IsDeviceScaleFactorSet() {
+ return g_device_scale_factor != 0.0f;
+}
+
+void ForceHighDPISupportForTesting(float scale) {
+ g_device_scale_factor = scale;
+}
+
+bool IsInHighDPIMode() {
+ return GetDPIScale() > 1.0;
+}
+
Size GetDPI() {
static int dpi_x = 0;
static int dpi_y = 0;
@@ -120,12 +89,21 @@ Size GetDPI() {
if (should_initialize) {
should_initialize = false;
+#if defined(OS_WIN)
base::win::ScopedGetDC screen_dc(NULL);
// This value is safe to cache for the life time of the app since the
// user must logout to change the DPI setting. This value also applies
// to all screens.
dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX);
dpi_y = GetDeviceCaps(screen_dc, LOGPIXELSY);
+#else
+ const gfx::LinuxFontDelegate* delegate = gfx::LinuxFontDelegate::instance();
+ if (delegate)
+ dpi_x = delegate->GetFontDPI();
+ if (dpi_x <= 0)
+ dpi_x = 96;
+ dpi_y = dpi_x;
+#endif
}
return Size(dpi_x, dpi_y);
}
@@ -145,42 +123,6 @@ float GetDPIScale() {
return 1.0;
}
-void ForceHighDPISupportForTesting(float scale) {
- g_device_scale_factor = scale;
-}
-
-bool IsHighDPIEnabled() {
- // Flag stored in HKEY_CURRENT_USER\SOFTWARE\\Google\\Chrome\\Profile,
- // under the DWORD value high-dpi-support.
- // Default is disabled.
- static DWORD value = ReadRegistryValue(
- HKEY_CURRENT_USER, gfx::win::kRegistryProfilePath,
- gfx::win::kHighDPISupportW, TRUE);
- return value != 0;
-}
-
-bool IsInHighDPIMode() {
- return GetDPIScale() > 1.0;
-}
-
-void EnableHighDPISupport() {
- if (IsHighDPIEnabled() &&
- !SetProcessDpiAwarenessWrapper(PROCESS_SYSTEM_DPI_AWARE)) {
- SetProcessDPIAwareWrapper();
- }
-}
-
-namespace win {
-
-GFX_EXPORT const wchar_t kRegistryProfilePath[] =
- L"Software\\Google\\Chrome\\Profile";
-GFX_EXPORT const wchar_t kHighDPISupportW[] = L"high-dpi-support";
-
-float GetDeviceScaleFactor() {
- DCHECK_NE(0.0f, g_device_scale_factor);
- return g_device_scale_factor;
-}
-
Point ScreenToDIPPoint(const Point& pixel_point) {
return ToFlooredPoint(ScalePoint(pixel_point,
1.0f / GetDeviceScaleFactor()));
@@ -217,14 +159,92 @@ Size DIPToScreenSize(const Size& dip_size) {
return ToCeiledSize(ScaleSize(dip_size, GetDeviceScaleFactor()));
}
+bool IsHighDPIEnabled() {
+#if defined(OS_WIN)
+ static const wchar_t kRegistryProfilePath[] =
+ L"Software\\Google\\Chrome\\Profile";
+ static const wchar_t kHighDPISupportW[] = L"high-dpi-support";
+
+ // Flag stored in HKEY_CURRENT_USER\SOFTWARE\\Google\\Chrome\\Profile,
+ // under the DWORD value high-dpi-support.
+ // Default is disabled.
+ static DWORD value = ReadRegistryValue(
+ HKEY_CURRENT_USER, kRegistryProfilePath, kHighDPISupportW, TRUE);
+ return value != 0;
+#elif defined(OS_LINUX)
+ return true;
+#endif
+}
+
+
+#if defined(OS_WIN)
+
+namespace win {
+
+namespace {
+
+// Duplicated from Win8.1 SDK ShellScalingApi.h
+typedef enum PROCESS_DPI_AWARENESS {
+ PROCESS_DPI_UNAWARE = 0,
+ PROCESS_SYSTEM_DPI_AWARE = 1,
+ PROCESS_PER_MONITOR_DPI_AWARE = 2
+} PROCESS_DPI_AWARENESS;
+
+typedef enum MONITOR_DPI_TYPE {
+ MDT_EFFECTIVE_DPI = 0,
+ MDT_ANGULAR_DPI = 1,
+ MDT_RAW_DPI = 2,
+ MDT_DEFAULT = MDT_EFFECTIVE_DPI
+} MONITOR_DPI_TYPE;
+
+// Win8.1 supports monitor-specific DPI scaling.
+bool SetProcessDpiAwarenessWrapper(PROCESS_DPI_AWARENESS value) {
+ typedef BOOL(WINAPI *SetProcessDpiAwarenessPtr)(PROCESS_DPI_AWARENESS);
+ SetProcessDpiAwarenessPtr set_process_dpi_awareness_func =
+ reinterpret_cast<SetProcessDpiAwarenessPtr>(
+ GetProcAddress(GetModuleHandleA("user32.dll"),
+ "SetProcessDpiAwarenessInternal"));
+ if (set_process_dpi_awareness_func) {
+ HRESULT hr = set_process_dpi_awareness_func(value);
+ if (SUCCEEDED(hr)) {
+ VLOG(1) << "SetProcessDpiAwareness succeeded.";
+ return true;
+ } else if (hr == E_ACCESSDENIED) {
+ LOG(ERROR) << "Access denied error from SetProcessDpiAwareness. "
+ "Function called twice, or manifest was used.";
+ }
+ }
+ return false;
+}
+
+// This function works for Windows Vista through Win8. Win8.1 must use
+// SetProcessDpiAwareness[Wrapper]
+BOOL SetProcessDPIAwareWrapper() {
+ typedef BOOL(WINAPI *SetProcessDPIAwarePtr)(VOID);
+ SetProcessDPIAwarePtr set_process_dpi_aware_func =
+ reinterpret_cast<SetProcessDPIAwarePtr>(
+ GetProcAddress(GetModuleHandleA("user32.dll"),
+ "SetProcessDPIAware"));
+ return set_process_dpi_aware_func &&
+ set_process_dpi_aware_func();
+}
+
+} // namespace
+
+void EnableHighDPISupport() {
+ if (IsHighDPIEnabled() &&
+ !SetProcessDpiAwarenessWrapper(PROCESS_SYSTEM_DPI_AWARE)) {
+ SetProcessDPIAwareWrapper();
+ }
+}
+
int GetSystemMetricsInDIP(int metric) {
return static_cast<int>(GetSystemMetrics(metric) /
GetDeviceScaleFactor() + 0.5);
}
-bool IsDeviceScaleFactorSet() {
- return g_device_scale_factor != 0.0f;
-}
-
} // namespace win
+
+#endif // OS_WIN
oshima 2014/10/20 21:11:59 consider moving win impl to dip_win.cc ?
scottmg 2014/10/22 18:54:14 Done.
+
} // namespace gfx
« no previous file with comments | « ui/gfx/dpi.h ('k') | ui/gfx/gfx.gyp » ('j') | ui/views/widget/desktop_aura/desktop_screen_x11.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698