Port Android relocation packer to chromium build
[chromium-blink-merge.git] / ash / display / display_util.cc
blobd152d13a753dac5e5377599c2173732b35d1006e
1 // Copyright 2014 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 "ash/display/display_util.h"
7 #include <algorithm>
9 #include "ash/display/display_info.h"
11 #if defined(OS_CHROMEOS)
12 #include "base/sys_info.h"
13 #endif
15 namespace ash {
16 namespace {
18 // List of value UI Scale values. Scales for 2x are equivalent to 640,
19 // 800, 1024, 1280, 1440, 1600 and 1920 pixel width respectively on
20 // 2560 pixel width 2x density display. Please see crbug.com/233375
21 // for the full list of resolutions.
22 const float kUIScalesFor2x[] =
23 {0.5f, 0.625f, 0.8f, 1.0f, 1.125f, 1.25f, 1.5f, 2.0f};
24 const float kUIScalesFor1_25x[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.25f };
25 const float kUIScalesFor1280[] = {0.5f, 0.625f, 0.8f, 1.0f, 1.125f };
26 const float kUIScalesFor1366[] = {0.5f, 0.6f, 0.75f, 1.0f, 1.125f };
28 std::vector<float> GetScalesForDisplay(const DisplayMode& native_mode) {
29 #define ASSIGN_ARRAY(v, a) v.assign(a, a + arraysize(a))
31 std::vector<float> ret;
32 if (native_mode.device_scale_factor == 2.0f) {
33 ASSIGN_ARRAY(ret, kUIScalesFor2x);
34 return ret;
35 } else if (native_mode.device_scale_factor == 1.25f) {
36 ASSIGN_ARRAY(ret, kUIScalesFor1_25x);
37 return ret;
39 switch (native_mode.size.width()) {
40 case 1280:
41 ASSIGN_ARRAY(ret, kUIScalesFor1280);
42 break;
43 case 1366:
44 ASSIGN_ARRAY(ret, kUIScalesFor1366);
45 break;
46 default:
47 ASSIGN_ARRAY(ret, kUIScalesFor1280);
48 #if defined(OS_CHROMEOS)
49 if (base::SysInfo::IsRunningOnChromeOS())
50 NOTREACHED() << "Unknown resolution:" << native_mode.size.ToString();
51 #endif
53 return ret;
56 struct ScaleComparator {
57 explicit ScaleComparator(float s) : scale(s) {}
59 bool operator()(const DisplayMode& mode) const {
60 const float kEpsilon = 0.0001f;
61 return std::abs(scale - mode.ui_scale) < kEpsilon;
63 float scale;
66 } // namespace
68 std::vector<DisplayMode> CreateInternalDisplayModeList(
69 const DisplayMode& native_mode) {
70 std::vector<DisplayMode> display_mode_list;
72 float native_ui_scale = (native_mode.device_scale_factor == 1.25f)
73 ? 1.0f
74 : native_mode.device_scale_factor;
75 for (float ui_scale : GetScalesForDisplay(native_mode)) {
76 DisplayMode mode = native_mode;
77 mode.ui_scale = ui_scale;
78 mode.native = (ui_scale == native_ui_scale);
79 display_mode_list.push_back(mode);
81 return display_mode_list;
84 // static
85 float GetNextUIScale(const DisplayInfo& info, bool up) {
86 ScaleComparator comparator(info.configured_ui_scale());
87 const std::vector<DisplayMode>& modes = info.display_modes();
88 for (auto iter = modes.begin(); iter != modes.end(); ++iter) {
89 if (comparator(*iter)) {
90 if (up && (iter + 1) != modes.end())
91 return (iter + 1)->ui_scale;
92 if (!up && iter != modes.begin())
93 return (iter - 1)->ui_scale;
94 return info.configured_ui_scale();
97 // Fallback to 1.0f if the |scale| wasn't in the list.
98 return 1.0f;
101 bool HasDisplayModeForUIScale(const DisplayInfo& info, float ui_scale) {
102 ScaleComparator comparator(ui_scale);
103 const std::vector<DisplayMode>& modes = info.display_modes();
104 return std::find_if(modes.begin(), modes.end(), comparator) != modes.end();
107 } // namespace ash