[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / ui / gfx / screen_win.cc
blobf19117b80f8ffa9c2105880f5b21a1a2b2da3879
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
3 // found in the LICENSE file.
5 #include "ui/gfx/screen_win.h"
7 #include <windows.h>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/hash.h"
12 #include "base/logging.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/win/win_util.h"
15 #include "ui/gfx/display.h"
16 #include "ui/gfx/win/dpi.h"
18 namespace {
20 MONITORINFOEX GetMonitorInfoForMonitor(HMONITOR monitor) {
21 MONITORINFOEX monitor_info;
22 ZeroMemory(&monitor_info, sizeof(MONITORINFOEX));
23 monitor_info.cbSize = sizeof(monitor_info);
24 GetMonitorInfo(monitor, &monitor_info);
25 return monitor_info;
28 gfx::Display GetDisplay(MONITORINFOEX& monitor_info) {
29 int64 id = static_cast<int64>(
30 base::Hash(base::WideToUTF8(monitor_info.szDevice)));
31 gfx::Rect bounds = gfx::Rect(monitor_info.rcMonitor);
32 gfx::Display display(id, bounds);
33 display.set_work_area(gfx::Rect(monitor_info.rcWork));
34 display.SetScaleAndBounds(gfx::GetDPIScale(), bounds);
36 DEVMODE mode;
37 memset(&mode, 0, sizeof(DEVMODE));
38 mode.dmSize = sizeof(DEVMODE);
39 mode.dmDriverExtra = 0;
40 if (EnumDisplaySettings(monitor_info.szDevice,
41 ENUM_CURRENT_SETTINGS,
42 &mode)) {
43 switch (mode.dmDisplayOrientation) {
44 case DMDO_DEFAULT:
45 display.set_rotation(gfx::Display::ROTATE_0);
46 break;
47 case DMDO_90:
48 display.set_rotation(gfx::Display::ROTATE_90);
49 break;
50 case DMDO_180:
51 display.set_rotation(gfx::Display::ROTATE_180);
52 break;
53 case DMDO_270:
54 display.set_rotation(gfx::Display::ROTATE_270);
55 break;
56 default:
57 NOTREACHED();
61 return display;
64 BOOL CALLBACK EnumMonitorCallback(HMONITOR monitor,
65 HDC hdc,
66 LPRECT rect,
67 LPARAM data) {
68 std::vector<gfx::Display>* all_displays =
69 reinterpret_cast<std::vector<gfx::Display>*>(data);
70 DCHECK(all_displays);
72 MONITORINFOEX monitor_info = GetMonitorInfoForMonitor(monitor);
73 gfx::Display display = GetDisplay(monitor_info);
74 all_displays->push_back(display);
75 return TRUE;
78 std::vector<gfx::Display> GetDisplays() {
79 std::vector<gfx::Display> displays;
80 EnumDisplayMonitors(NULL, NULL, EnumMonitorCallback,
81 reinterpret_cast<LPARAM>(&displays));
82 return displays;
85 } // namespace
87 namespace gfx {
89 ScreenWin::ScreenWin()
90 : displays_(GetDisplays()),
91 singleton_hwnd_observer_(new SingletonHwndObserver(
92 base::Bind(&ScreenWin::OnWndProc, base::Unretained(this)))) {}
94 ScreenWin::~ScreenWin() {}
96 gfx::Point ScreenWin::GetCursorScreenPoint() {
97 POINT pt;
98 GetCursorPos(&pt);
99 gfx::Point cursor_pos_pixels(pt);
100 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels);
103 gfx::NativeWindow ScreenWin::GetWindowUnderCursor() {
104 POINT cursor_loc;
105 HWND hwnd = GetCursorPos(&cursor_loc) ? WindowFromPoint(cursor_loc) : NULL;
106 return GetNativeWindowFromHWND(hwnd);
109 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) {
110 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point);
111 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT()));
114 int ScreenWin::GetNumDisplays() const {
115 return GetSystemMetrics(SM_CMONITORS);
118 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const {
119 return displays_;
122 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const {
123 HWND window_hwnd = GetHWNDFromNativeView(window);
124 if (!window_hwnd) {
125 // When |window| isn't rooted to a display, we should just return the
126 // default display so we get some correct display information like the
127 // scaling factor.
128 return GetPrimaryDisplay();
131 MONITORINFOEX monitor_info;
132 monitor_info.cbSize = sizeof(monitor_info);
133 GetMonitorInfo(MonitorFromWindow(window_hwnd, MONITOR_DEFAULTTONEAREST),
134 &monitor_info);
135 return GetDisplay(monitor_info);
138 gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const {
139 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point);
140 POINT initial_loc = { point_in_pixels.x(), point_in_pixels.y() };
141 HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST);
142 MONITORINFOEX mi;
143 ZeroMemory(&mi, sizeof(MONITORINFOEX));
144 mi.cbSize = sizeof(mi);
145 if (monitor && GetMonitorInfo(monitor, &mi)) {
146 return GetDisplay(mi);
148 return gfx::Display();
151 gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const {
152 RECT other_bounds_rect = match_rect.ToRECT();
153 MONITORINFOEX monitor_info = GetMonitorInfoForMonitor(MonitorFromRect(
154 &other_bounds_rect, MONITOR_DEFAULTTONEAREST));
155 return GetDisplay(monitor_info);
158 gfx::Display ScreenWin::GetPrimaryDisplay() const {
159 MONITORINFOEX mi = GetMonitorInfoForMonitor(
160 MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY));
161 gfx::Display display = GetDisplay(mi);
162 // TODO(kevers|girard): Test if these checks can be reintroduced for high-DIP
163 // once more of the app is DIP-aware.
164 if (GetDPIScale() == 1.0) {
165 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width());
166 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height());
168 return display;
171 void ScreenWin::AddObserver(DisplayObserver* observer) {
172 change_notifier_.AddObserver(observer);
175 void ScreenWin::RemoveObserver(DisplayObserver* observer) {
176 change_notifier_.RemoveObserver(observer);
179 void ScreenWin::OnWndProc(HWND hwnd,
180 UINT message,
181 WPARAM wparam,
182 LPARAM lparam) {
183 if (message != WM_DISPLAYCHANGE)
184 return;
186 std::vector<gfx::Display> old_displays = displays_;
187 displays_ = GetDisplays();
189 change_notifier_.NotifyDisplaysChanged(old_displays, displays_);
192 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const {
193 NOTREACHED();
194 return NULL;
197 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const {
198 NOTREACHED();
199 return NULL;
202 } // namespace gfx