forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_util.cc
353 lines (316 loc) · 13.1 KB
/
display_util.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/display/display_util.h"
#include <algorithm>
#include "ash/display/display_info.h"
#include "ash/display/display_manager.h"
#include "ash/host/ash_window_tree_host.h"
#include "ash/shell.h"
#include "ui/aura/env.h"
#include "ui/aura/window_tree_host.h"
#include "ui/gfx/display.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/wm/core/coordinate_conversion.h"
#if defined(OS_CHROMEOS)
#include "base/sys_info.h"
#endif
namespace ash {
namespace {
// List of value UI Scale values. Scales for 2x are equivalent to 640,
// 800, 1024, 1280, 1440, 1600 and 1920 pixel width respectively on
// 2560 pixel width 2x density display. Please see crbug.com/233375
// for the full list of resolutions.
const float kUIScalesFor2x[] =
{0.5f, 0.625f, 0.8f, 1.0f, 1.125f, 1.25f, 1.5f, 2.0f};
const float kUIScalesFor1_25x[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.25f };
const float kUIScalesFor1280[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.125f };
const float kUIScalesFor1366[] = {0.5f, 0.6f, 0.75f, 1.0f, 1.125f };
std::vector<float> GetScalesForDisplay(const DisplayMode& native_mode) {
#define ASSIGN_ARRAY(v, a) v.assign(a, a + arraysize(a))
std::vector<float> ret;
if (native_mode.device_scale_factor == 2.0f) {
ASSIGN_ARRAY(ret, kUIScalesFor2x);
return ret;
} else if (native_mode.device_scale_factor == 1.25f) {
ASSIGN_ARRAY(ret, kUIScalesFor1_25x);
return ret;
}
switch (native_mode.size.width()) {
case 1280:
ASSIGN_ARRAY(ret, kUIScalesFor1280);
break;
case 1366:
ASSIGN_ARRAY(ret, kUIScalesFor1366);
break;
default:
ASSIGN_ARRAY(ret, kUIScalesFor1280);
#if defined(OS_CHROMEOS)
if (base::SysInfo::IsRunningOnChromeOS())
NOTREACHED() << "Unknown resolution:" << native_mode.size.ToString();
#endif
}
return ret;
}
struct ScaleComparator {
explicit ScaleComparator(float s) : scale(s) {}
bool operator()(const DisplayMode& mode) const {
const float kEpsilon = 0.0001f;
return std::abs(scale - mode.ui_scale) < kEpsilon;
}
float scale;
};
void ConvertPointFromScreenToNative(aura::WindowTreeHost* host,
gfx::Point* point) {
::wm::ConvertPointFromScreen(host->window(), point);
host->ConvertPointToNativeScreen(point);
}
bool GetDisplayModeForUIScale(const DisplayInfo& info,
float ui_scale,
DisplayMode* out) {
const std::vector<DisplayMode>& modes = info.display_modes();
auto iter = std::find_if(modes.begin(), modes.end(),
[ui_scale](const DisplayMode& mode) {
return mode.ui_scale == ui_scale;
});
if (iter == modes.end())
return false;
*out = *iter;
return true;
}
void FindNextMode(std::vector<DisplayMode>::const_iterator& iter,
const std::vector<DisplayMode>& modes,
bool up,
DisplayMode* out) {
DCHECK(iter != modes.end());
if (up && (iter + 1) != modes.end())
*out = *(iter + 1);
else if (!up && iter != modes.begin())
*out = *(iter - 1);
else
*out = *iter;
}
} // namespace
std::vector<DisplayMode> CreateInternalDisplayModeList(
const DisplayMode& native_mode) {
std::vector<DisplayMode> display_mode_list;
float native_ui_scale = (native_mode.device_scale_factor == 1.25f)
? 1.0f
: native_mode.device_scale_factor;
for (float ui_scale : GetScalesForDisplay(native_mode)) {
DisplayMode mode = native_mode;
mode.ui_scale = ui_scale;
mode.native = (ui_scale == native_ui_scale);
display_mode_list.push_back(mode);
}
return display_mode_list;
}
std::vector<DisplayMode> CreateUnifiedDisplayModeList(
const DisplayMode& native_mode,
const std::set<std::pair<float, float>>& dsf_scale_list) {
std::vector<DisplayMode> display_mode_list;
for (auto& pair : dsf_scale_list) {
DisplayMode mode = native_mode;
mode.device_scale_factor = pair.first;
gfx::SizeF scaled_size(native_mode.size);
scaled_size.Scale(pair.second);
mode.size = gfx::ToFlooredSize(scaled_size);
mode.native = false;
display_mode_list.push_back(mode);
}
// Sort the mode by the size in DIP.
std::sort(display_mode_list.begin(), display_mode_list.end(),
[](const DisplayMode& a, const DisplayMode& b) {
return a.GetSizeInDIP(false).GetArea() <
b.GetSizeInDIP(false).GetArea();
});
return display_mode_list;
}
bool GetDisplayModeForResolution(const DisplayInfo& info,
const gfx::Size& resolution,
DisplayMode* out) {
if (gfx::Display::IsInternalDisplayId(info.id()))
return false;
const std::vector<DisplayMode>& modes = info.display_modes();
DCHECK_NE(0u, modes.size());
DisplayMode target_mode;
target_mode.size = resolution;
std::vector<DisplayMode>::const_iterator iter = std::find_if(
modes.begin(), modes.end(), [resolution](const DisplayMode& mode) {
return mode.size == resolution;
});
if (iter == modes.end()) {
LOG(WARNING) << "Unsupported resolution was requested:"
<< resolution.ToString();
return false;
}
*out = *iter;
return true;
}
bool GetDisplayModeForNextUIScale(const DisplayInfo& info,
bool up,
DisplayMode* out) {
if (!gfx::Display::IsInternalDisplayId(info.id()))
return false;
const std::vector<DisplayMode>& modes = info.display_modes();
ScaleComparator comparator(info.configured_ui_scale());
auto iter = std::find_if(modes.begin(), modes.end(), comparator);
FindNextMode(iter, modes, up, out);
return true;
}
bool GetDisplayModeForNextResolution(const DisplayInfo& info,
bool up,
DisplayMode* out) {
if (gfx::Display::IsInternalDisplayId(info.id()))
return false;
const std::vector<DisplayMode>& modes = info.display_modes();
DisplayMode tmp(info.size_in_pixel(), 0.0f, false, false);
tmp.device_scale_factor = info.device_scale_factor();
gfx::Size resolution = tmp.GetSizeInDIP(false);
auto iter = std::find_if(modes.begin(), modes.end(),
[resolution](const DisplayMode& mode) {
return mode.GetSizeInDIP(false) == resolution;
});
FindNextMode(iter, modes, up, out);
return true;
}
bool SetDisplayUIScale(int64 id, float ui_scale) {
DisplayManager* display_manager = Shell::GetInstance()->display_manager();
const DisplayInfo& info = display_manager->GetDisplayInfo(id);
DisplayMode mode;
if (!GetDisplayModeForUIScale(info, ui_scale, &mode))
return false;
return display_manager->SetDisplayMode(id, mode);
}
bool HasDisplayModeForUIScale(const DisplayInfo& info, float ui_scale) {
ScaleComparator comparator(ui_scale);
const std::vector<DisplayMode>& modes = info.display_modes();
return std::find_if(modes.begin(), modes.end(), comparator) != modes.end();
}
void ComputeBoundary(const gfx::Display& primary_display,
const gfx::Display& secondary_display,
DisplayLayout::Position position,
gfx::Rect* primary_edge_in_screen,
gfx::Rect* secondary_edge_in_screen) {
const gfx::Rect& primary = primary_display.bounds();
const gfx::Rect& secondary = secondary_display.bounds();
switch (position) {
case DisplayLayout::TOP:
case DisplayLayout::BOTTOM: {
int left = std::max(primary.x(), secondary.x());
int right = std::min(primary.right(), secondary.right());
if (position == DisplayLayout::TOP) {
primary_edge_in_screen->SetRect(left, primary.y(), right - left, 1);
secondary_edge_in_screen->SetRect(left, secondary.bottom() - 1,
right - left, 1);
} else {
primary_edge_in_screen->SetRect(left, primary.bottom() - 1,
right - left, 1);
secondary_edge_in_screen->SetRect(left, secondary.y(), right - left, 1);
}
break;
}
case DisplayLayout::LEFT:
case DisplayLayout::RIGHT: {
int top = std::max(primary.y(), secondary.y());
int bottom = std::min(primary.bottom(), secondary.bottom());
if (position == DisplayLayout::LEFT) {
primary_edge_in_screen->SetRect(primary.x(), top, 1, bottom - top);
secondary_edge_in_screen->SetRect(secondary.right() - 1, top, 1,
bottom - top);
} else {
primary_edge_in_screen->SetRect(primary.right() - 1, top, 1,
bottom - top);
secondary_edge_in_screen->SetRect(secondary.y(), top, 1, bottom - top);
}
break;
}
}
}
gfx::Rect GetNativeEdgeBounds(AshWindowTreeHost* ash_host,
const gfx::Rect& bounds_in_screen) {
aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
gfx::Rect native_bounds = host->GetBounds();
native_bounds.Inset(ash_host->GetHostInsets());
gfx::Point start_in_native = bounds_in_screen.origin();
gfx::Point end_in_native = bounds_in_screen.bottom_right();
ConvertPointFromScreenToNative(host, &start_in_native);
ConvertPointFromScreenToNative(host, &end_in_native);
if (std::abs(start_in_native.x() - end_in_native.x()) <
std::abs(start_in_native.y() - end_in_native.y())) {
// vertical in native
int x = std::abs(native_bounds.x() - start_in_native.x()) <
std::abs(native_bounds.right() - start_in_native.x())
? native_bounds.x()
: native_bounds.right() - 1;
return gfx::Rect(x, std::min(start_in_native.y(), end_in_native.y()), 1,
std::abs(end_in_native.y() - start_in_native.y()));
} else {
// horizontal in native
int y = std::abs(native_bounds.y() - start_in_native.y()) <
std::abs(native_bounds.bottom() - start_in_native.y())
? native_bounds.y()
: native_bounds.bottom() - 1;
return gfx::Rect(std::min(start_in_native.x(), end_in_native.x()), y,
std::abs(end_in_native.x() - start_in_native.x()), 1);
}
}
// Moves the cursor to the point inside the root that is closest to
// the point_in_screen, which is outside of the root window.
void MoveCursorTo(AshWindowTreeHost* ash_host,
const gfx::Point& point_in_screen,
bool update_last_location_now) {
aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
gfx::Point point_in_native = point_in_screen;
::wm::ConvertPointFromScreen(host->window(), &point_in_native);
host->ConvertPointToNativeScreen(&point_in_native);
// now fit the point inside the native bounds.
gfx::Rect native_bounds = host->GetBounds();
gfx::Point native_origin = native_bounds.origin();
native_bounds.Inset(ash_host->GetHostInsets());
// Shrink further so that the mouse doesn't warp on the
// edge. The right/bottom needs to be shrink by 2 to subtract
// the 1 px from width/height value.
native_bounds.Inset(1, 1, 2, 2);
// Ensure that |point_in_native| is inside the |native_bounds|.
point_in_native.SetToMax(native_bounds.origin());
point_in_native.SetToMin(native_bounds.bottom_right());
gfx::Point point_in_host = point_in_native;
point_in_host.Offset(-native_origin.x(), -native_origin.y());
host->MoveCursorToHostLocation(point_in_host);
if (update_last_location_now) {
gfx::Point new_point_in_screen = point_in_native;
if (Shell::GetInstance()->display_manager()->IsInUnifiedMode()) {
// TODO(oshima): Do not use ConvertPointFromNativeScreen because
// the mirroring display has a transform that should not be applied here.
gfx::Point origin = host->GetBounds().origin();
new_point_in_screen.Offset(-origin.x(), -origin.y());
} else {
host->ConvertPointFromNativeScreen(&new_point_in_screen);
}
::wm::ConvertPointToScreen(host->window(), &new_point_in_screen);
aura::Env::GetInstance()->set_last_mouse_location(new_point_in_screen);
}
}
int FindDisplayIndexContainingPoint(const std::vector<gfx::Display>& displays,
const gfx::Point& point_in_screen) {
auto iter = std::find_if(displays.begin(), displays.end(),
[point_in_screen](const gfx::Display& display) {
return display.bounds().Contains(point_in_screen);
});
return iter == displays.end() ? -1 : (iter - displays.begin());
}
DisplayIdPair CreateDisplayIdPair(int64 id1, int64 id2) {
DCHECK_NE(id1, id2);
// Output index is stored in the first 8 bits. See GetDisplayIdFromEDID
// in edid_parser.cc.
int index_1 = id1 & 0xFF;
int index_2 = id2 & 0xFF;
if (gfx::Display::IsInternalDisplayId(id2) || index_2 < index_1)
return std::make_pair(id2, id1);
else
return std::make_pair(id1, id2);
}
} // namespace ash