Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / ozone / common / display_util.cc
blob4ce67c0b840349634ede792d61713b0b9d5f12a1
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 "ui/ozone/common/display_util.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "ui/display/types/display_mode.h"
11 #include "ui/display/types/display_snapshot.h"
12 #include "ui/display/util/edid_parser.h"
13 #include "ui/display/util/edid_parser.h"
14 #include "ui/ozone/public/ozone_switches.h"
16 namespace ui {
18 namespace {
20 const int64_t kDummyDisplayId = 1;
22 } // namespace
24 FindDisplayById::FindDisplayById(int64_t display_id) : display_id_(display_id) {
27 bool FindDisplayById::operator()(const DisplaySnapshot_Params& display) const {
28 return display.display_id == display_id_;
31 DisplayMode_Params GetDisplayModeParams(const DisplayMode& mode) {
32 DisplayMode_Params params;
33 params.size = mode.size();
34 params.is_interlaced = mode.is_interlaced();
35 params.refresh_rate = mode.refresh_rate();
37 return params;
40 DisplaySnapshot_Params GetDisplaySnapshotParams(
41 const DisplaySnapshot& display) {
42 DisplaySnapshot_Params params;
43 params.display_id = display.display_id();
44 params.origin = display.origin();
45 params.physical_size = display.physical_size();
46 params.type = display.type();
47 params.is_aspect_preserving_scaling = display.is_aspect_preserving_scaling();
48 params.has_overscan = display.has_overscan();
49 params.display_name = display.display_name();
50 for (size_t i = 0; i < display.modes().size(); ++i)
51 params.modes.push_back(GetDisplayModeParams(*display.modes()[i]));
53 params.has_current_mode = display.current_mode() != NULL;
54 if (params.has_current_mode)
55 params.current_mode = GetDisplayModeParams(*display.current_mode());
57 params.has_native_mode = display.native_mode() != NULL;
58 if (params.has_native_mode)
59 params.native_mode = GetDisplayModeParams(*display.native_mode());
61 params.string_representation = display.ToString();
63 return params;
66 bool CreateSnapshotFromCommandLine(DisplaySnapshot_Params* snapshot_out) {
67 base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
68 std::string spec =
69 cmd->GetSwitchValueASCII(switches::kOzoneInitialDisplayBounds);
70 std::string physical_spec =
71 cmd->GetSwitchValueASCII(switches::kOzoneInitialDisplayPhysicalSizeMm);
73 int width = 0;
74 int height = 0;
75 if (spec.empty() || sscanf(spec.c_str(), "%dx%d", &width, &height) < 2 ||
76 width == 0 || height == 0) {
77 return false;
80 int physical_width = 0;
81 int physical_height = 0;
82 sscanf(physical_spec.c_str(), "%dx%d", &physical_width, &physical_height);
84 DisplayMode_Params mode_param;
85 mode_param.size = gfx::Size(width, height);
86 mode_param.refresh_rate = 60;
88 snapshot_out->display_id = kDummyDisplayId;
89 snapshot_out->modes.push_back(mode_param);
90 snapshot_out->type = DISPLAY_CONNECTION_TYPE_INTERNAL;
91 snapshot_out->physical_size = gfx::Size(physical_width, physical_height);
92 snapshot_out->has_current_mode = true;
93 snapshot_out->current_mode = mode_param;
94 snapshot_out->has_native_mode = true;
95 snapshot_out->native_mode = mode_param;
96 return true;
99 bool CreateSnapshotFromEDID(bool internal,
100 const std::vector<uint8_t>& edid,
101 DisplaySnapshot_Params* snapshot_out) {
102 uint16_t manufacturer_id = 0;
103 gfx::Size resolution;
105 DisplayMode_Params mode_param;
106 mode_param.refresh_rate = 60.0f;
108 if (!ParseOutputDeviceData(edid, &manufacturer_id,
109 &snapshot_out->display_name, &mode_param.size,
110 &snapshot_out->physical_size) ||
111 !GetDisplayIdFromEDID(edid, 0, &snapshot_out->display_id)) {
112 return false;
114 ParseOutputOverscanFlag(edid, &snapshot_out->has_overscan);
116 snapshot_out->modes.push_back(mode_param);
117 // Use VGA for external display for now.
118 // TODO(oshima): frecon should set this value in the display_info.bin file.
119 snapshot_out->type =
120 internal ? DISPLAY_CONNECTION_TYPE_INTERNAL : DISPLAY_CONNECTION_TYPE_VGA;
121 snapshot_out->has_current_mode = true;
122 snapshot_out->current_mode = mode_param;
123 snapshot_out->has_native_mode = true;
124 snapshot_out->native_mode = mode_param;
125 return true;
128 bool CreateSnapshotFromEDIDFile(const base::FilePath& file,
129 DisplaySnapshot_Params* snapshot_out) {
130 std::string raw_display_info;
131 const int kEDIDMaxSize = 128;
132 if (!base::ReadFileToString(file, &raw_display_info, kEDIDMaxSize + 1) ||
133 raw_display_info.size() < 10) {
134 return false;
136 std::vector<uint8_t> edid;
137 // The head of the file contains one byte flag that indicates the type of
138 // display.
139 bool internal = raw_display_info[0] == 1;
140 edid.assign(raw_display_info.c_str() + 1,
141 raw_display_info.c_str() + raw_display_info.size());
142 return CreateSnapshotFromEDID(internal, edid, snapshot_out);
145 } // namespace ui