| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/display/output_util.h" | 5 #include "base/x11/edid_parser_x11.h" |
| 6 | 6 |
| 7 #include <X11/Xlib.h> | |
| 8 #include <X11/extensions/Xrandr.h> | 7 #include <X11/extensions/Xrandr.h> |
| 9 #include <X11/Xatom.h> | 8 #include <X11/Xatom.h> |
| 9 #include <X11/Xlib.h> |
| 10 | 10 |
| 11 #include "base/hash.h" | 11 #include "base/hash.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/sys_byteorder.h" | 14 #include "base/sys_byteorder.h" |
| 15 | 15 |
| 16 namespace chromeos { | |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 // Prefixes for the built-in displays. | |
| 20 const char kInternal_LVDS[] = "LVDS"; | |
| 21 const char kInternal_eDP[] = "eDP"; | |
| 22 const char kInternal_DSI[] = "DSI"; | |
| 23 | |
| 24 // Returns 64-bit persistent ID for the specified manufacturer's ID and | 18 // Returns 64-bit persistent ID for the specified manufacturer's ID and |
| 25 // product_code_hash, and the index of the output it is connected to. | 19 // product_code_hash, and the index of the output it is connected to. |
| 26 // |output_index| is used to distinguish the displays of the same type. For | 20 // |output_index| is used to distinguish the displays of the same type. For |
| 27 // example, swapping two identical display between two outputs will not be | 21 // example, swapping two identical display between two outputs will not be |
| 28 // treated as swap. The 'serial number' field in EDID isn't used here because | 22 // treated as swap. The 'serial number' field in EDID isn't used here because |
| 29 // it is not guaranteed to have unique number and it may have the same fixed | 23 // it is not guaranteed to have unique number and it may have the same fixed |
| 30 // value (like 0). | 24 // value (like 0). |
| 31 int64 GetID(uint16 manufacturer_id, | 25 int64 GetID(uint16 manufacturer_id, |
| 32 uint32 product_code_hash, | 26 uint32 product_code_hash, |
| 33 uint8 output_index) { | 27 uint8 output_index) { |
| 34 return ((static_cast<int64>(manufacturer_id) << 40) | | 28 return ((static_cast<int64>(manufacturer_id) << 40) | |
| 35 (static_cast<int64>(product_code_hash) << 8) | output_index); | 29 (static_cast<int64>(product_code_hash) << 8) | output_index); |
| 36 } | 30 } |
| 37 | 31 |
| 38 bool IsRandRAvailable() { | 32 bool IsRandRAvailable() { |
| 39 int randr_version_major = 0; | 33 int randr_version_major = 0; |
| 40 int randr_version_minor = 0; | 34 int randr_version_minor = 0; |
| 41 static bool is_randr_available = XRRQueryVersion( | 35 static bool is_randr_available = XRRQueryVersion( |
| 42 base::MessagePumpX11::GetDefaultXDisplay(), | 36 base::MessagePumpX11::GetDefaultXDisplay(), |
| 43 &randr_version_major, &randr_version_minor); | 37 &randr_version_major, &randr_version_minor); |
| 44 return is_randr_available; | 38 return is_randr_available; |
| 45 } | 39 } |
| 46 | 40 |
| 47 // Get the EDID data from the |output| and stores to |prop|. |nitem| will store | 41 } // namespace |
| 48 // the number of characters |prop| will have. It doesn't take the ownership of | 42 |
| 49 // |prop|, so caller must release it by XFree(). | 43 namespace base { |
| 50 // Returns true if EDID property is successfully obtained. Otherwise returns | 44 |
| 51 // false and does not touch |prop| and |nitems|. | |
| 52 bool GetEDIDProperty(XID output, unsigned long* nitems, unsigned char** prop) { | 45 bool GetEDIDProperty(XID output, unsigned long* nitems, unsigned char** prop) { |
| 53 if (!IsRandRAvailable()) | 46 if (!IsRandRAvailable()) |
| 54 return false; | 47 return false; |
| 55 | 48 |
| 56 Display* display = base::MessagePumpX11::GetDefaultXDisplay(); | 49 Display* display = base::MessagePumpX11::GetDefaultXDisplay(); |
| 57 | 50 |
| 58 static Atom edid_property = XInternAtom( | 51 static Atom edid_property = XInternAtom( |
| 59 base::MessagePumpX11::GetDefaultXDisplay(), | 52 base::MessagePumpX11::GetDefaultXDisplay(), |
| 60 RR_PROPERTY_RANDR_EDID, false); | 53 RR_PROPERTY_RANDR_EDID, false); |
| 61 | 54 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 86 &actual_type, | 79 &actual_type, |
| 87 &actual_format, | 80 &actual_format, |
| 88 nitems, | 81 nitems, |
| 89 &bytes_after, | 82 &bytes_after, |
| 90 prop); | 83 prop); |
| 91 DCHECK_EQ(XA_INTEGER, actual_type); | 84 DCHECK_EQ(XA_INTEGER, actual_type); |
| 92 DCHECK_EQ(8, actual_format); | 85 DCHECK_EQ(8, actual_format); |
| 93 return true; | 86 return true; |
| 94 } | 87 } |
| 95 | 88 |
| 96 // Gets some useful data from the specified output device, such like | |
| 97 // manufacturer's ID, product code, and human readable name. Returns false if it | |
| 98 // fails to get those data and doesn't touch manufacturer ID/product code/name. | |
| 99 // NULL can be passed for unwanted output parameters. | |
| 100 bool GetOutputDeviceData(XID output, | |
| 101 uint16* manufacturer_id, | |
| 102 std::string* human_readable_name) { | |
| 103 unsigned long nitems = 0; | |
| 104 unsigned char *prop = NULL; | |
| 105 if (!GetEDIDProperty(output, &nitems, &prop)) | |
| 106 return false; | |
| 107 | |
| 108 bool result = ParseOutputDeviceData( | |
| 109 prop, nitems, manufacturer_id, human_readable_name); | |
| 110 XFree(prop); | |
| 111 return result; | |
| 112 } | |
| 113 | |
| 114 } // namespace | |
| 115 | |
| 116 std::string GetDisplayName(XID output_id) { | |
| 117 std::string display_name; | |
| 118 GetOutputDeviceData(output_id, NULL, &display_name); | |
| 119 return display_name; | |
| 120 } | |
| 121 | |
| 122 bool GetDisplayId(XID output_id, size_t output_index, int64* display_id_out) { | 89 bool GetDisplayId(XID output_id, size_t output_index, int64* display_id_out) { |
| 123 unsigned long nitems = 0; | 90 unsigned long nitems = 0; |
| 124 unsigned char* prop = NULL; | 91 unsigned char* prop = NULL; |
| 125 if (!GetEDIDProperty(output_id, &nitems, &prop)) | 92 if (!GetEDIDProperty(output_id, &nitems, &prop)) |
| 126 return false; | 93 return false; |
| 127 | 94 |
| 128 bool result = | 95 bool result = |
| 129 GetDisplayIdFromEDID(prop, nitems, output_index, display_id_out); | 96 GetDisplayIdFromEDID(prop, nitems, output_index, display_id_out); |
| 130 XFree(prop); | 97 XFree(prop); |
| 131 return result; | 98 return result; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 if (!isascii(c) || !isprint(c)) { | 186 if (!isascii(c) || !isprint(c)) { |
| 220 human_readable_name->clear(); | 187 human_readable_name->clear(); |
| 221 LOG(ERROR) << "invalid EDID: human unreadable char in name"; | 188 LOG(ERROR) << "invalid EDID: human unreadable char in name"; |
| 222 return false; | 189 return false; |
| 223 } | 190 } |
| 224 } | 191 } |
| 225 | 192 |
| 226 return true; | 193 return true; |
| 227 } | 194 } |
| 228 | 195 |
| 229 bool GetOutputOverscanFlag(XID output, bool* flag) { | 196 } // namespace base |
| 230 unsigned long nitems = 0; | |
| 231 unsigned char *prop = NULL; | |
| 232 if (!GetEDIDProperty(output, &nitems, &prop)) | |
| 233 return false; | |
| 234 | |
| 235 bool found = ParseOutputOverscanFlag(prop, nitems, flag); | |
| 236 XFree(prop); | |
| 237 return found; | |
| 238 } | |
| 239 | |
| 240 bool ParseOutputOverscanFlag(const unsigned char* prop, | |
| 241 unsigned long nitems, | |
| 242 bool *flag) { | |
| 243 // See http://en.wikipedia.org/wiki/Extended_display_identification_data | |
| 244 // for the extension format of EDID. Also see EIA/CEA-861 spec for | |
| 245 // the format of the extensions and how video capability is encoded. | |
| 246 // - byte 0: tag. should be 02h. | |
| 247 // - byte 1: revision. only cares revision 3 (03h). | |
| 248 // - byte 4-: data block. | |
| 249 const unsigned int kExtensionBase = 128; | |
| 250 const unsigned int kExtensionSize = 128; | |
| 251 const unsigned int kNumExtensionsOffset = 126; | |
| 252 const unsigned int kDataBlockOffset = 4; | |
| 253 const unsigned char kCEAExtensionTag = '\x02'; | |
| 254 const unsigned char kExpectedExtensionRevision = '\x03'; | |
| 255 const unsigned char kExtendedTag = 7; | |
| 256 const unsigned char kExtendedVideoCapabilityTag = 0; | |
| 257 const unsigned int kPTOverscan = 4; | |
| 258 const unsigned int kITOverscan = 2; | |
| 259 const unsigned int kCEOverscan = 0; | |
| 260 | |
| 261 if (nitems <= kNumExtensionsOffset) | |
| 262 return false; | |
| 263 | |
| 264 unsigned char num_extensions = prop[kNumExtensionsOffset]; | |
| 265 | |
| 266 for (size_t i = 0; i < num_extensions; ++i) { | |
| 267 // Skip parsing the whole extension if size is not enough. | |
| 268 if (nitems < kExtensionBase + (i + 1) * kExtensionSize) | |
| 269 break; | |
| 270 | |
| 271 const unsigned char* extension = prop + kExtensionBase + i * kExtensionSize; | |
| 272 unsigned char tag = extension[0]; | |
| 273 unsigned char revision = extension[1]; | |
| 274 if (tag != kCEAExtensionTag || revision != kExpectedExtensionRevision) | |
| 275 continue; | |
| 276 | |
| 277 unsigned char timing_descriptors_start = | |
| 278 std::min(extension[2], static_cast<unsigned char>(kExtensionSize)); | |
| 279 const unsigned char* data_block = extension + kDataBlockOffset; | |
| 280 while (data_block < extension + timing_descriptors_start) { | |
| 281 // A data block is encoded as: | |
| 282 // - byte 1 high 3 bits: tag. '07' for extended tags. | |
| 283 // - byte 1 remaining bits: the length of data block. | |
| 284 // - byte 2: the extended tag. '0' for video capability. | |
| 285 // - byte 3: the capability. | |
| 286 unsigned char tag = data_block[0] >> 5; | |
| 287 unsigned char payload_length = data_block[0] & 0x1f; | |
| 288 if (static_cast<unsigned long>(data_block + payload_length - prop) > | |
| 289 nitems) | |
| 290 break; | |
| 291 | |
| 292 if (tag != kExtendedTag || payload_length < 2) { | |
| 293 data_block += payload_length + 1; | |
| 294 continue; | |
| 295 } | |
| 296 | |
| 297 unsigned char extended_tag_code = data_block[1]; | |
| 298 if (extended_tag_code != kExtendedVideoCapabilityTag) { | |
| 299 data_block += payload_length + 1; | |
| 300 continue; | |
| 301 } | |
| 302 | |
| 303 // The difference between preferred, IT, and CE video formats | |
| 304 // doesn't matter. Sets |flag| to true if any of these flags are true. | |
| 305 if ((data_block[2] & (1 << kPTOverscan)) || | |
| 306 (data_block[2] & (1 << kITOverscan)) || | |
| 307 (data_block[2] & (1 << kCEOverscan))) { | |
| 308 *flag = true; | |
| 309 } else { | |
| 310 *flag = false; | |
| 311 } | |
| 312 return true; | |
| 313 } | |
| 314 } | |
| 315 | |
| 316 return false; | |
| 317 } | |
| 318 | |
| 319 bool IsInternalOutputName(const std::string& name) { | |
| 320 return name.find(kInternal_LVDS) == 0 || name.find(kInternal_eDP) == 0 || | |
| 321 name.find(kInternal_DSI) == 0; | |
| 322 } | |
| 323 | |
| 324 const XRRModeInfo* FindXRRModeInfo(const XRRScreenResources* screen_resources, | |
| 325 XID current_mode) { | |
| 326 for (int m = 0; m < screen_resources->nmode; m++) { | |
| 327 XRRModeInfo *mode = &screen_resources->modes[m]; | |
| 328 if (mode->id == current_mode) | |
| 329 return mode; | |
| 330 } | |
| 331 return NULL; | |
| 332 } | |
| 333 | |
| 334 namespace test { | |
| 335 | |
| 336 XRRModeInfo CreateModeInfo(int id, | |
| 337 int width, | |
| 338 int height, | |
| 339 bool interlaced, | |
| 340 float refresh_rate) { | |
| 341 XRRModeInfo mode_info = {0}; | |
| 342 mode_info.id = id; | |
| 343 mode_info.width = width; | |
| 344 mode_info.height = height; | |
| 345 if (interlaced) | |
| 346 mode_info.modeFlags = RR_Interlace; | |
| 347 if (refresh_rate != 0.0f) { | |
| 348 mode_info.hTotal = 1; | |
| 349 mode_info.vTotal = 1; | |
| 350 mode_info.dotClock = refresh_rate; | |
| 351 } | |
| 352 return mode_info; | |
| 353 } | |
| 354 | |
| 355 } // namespace test | |
| 356 | |
| 357 } // namespace chromeos | |
| OLD | NEW |