Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "DisplayIdentification" |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <cctype> |
| 22 | #include <numeric> |
| 23 | #include <optional> |
| 24 | |
| 25 | #include <log/log.h> |
| 26 | |
| 27 | #include "DisplayIdentification.h" |
| 28 | |
| 29 | namespace android { |
| 30 | namespace { |
| 31 | |
| 32 | using byte_view = std::basic_string_view<uint8_t>; |
| 33 | |
| 34 | constexpr size_t kEdidHeaderLength = 5; |
| 35 | |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame^] | 36 | constexpr uint16_t kFallbackEdidManufacturerId = 0; |
| 37 | constexpr uint16_t kVirtualEdidManufacturerId = 0xffffu; |
| 38 | |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 39 | std::optional<uint8_t> getEdidDescriptorType(const byte_view& view) { |
| 40 | if (view.size() < kEdidHeaderLength || view[0] || view[1] || view[2] || view[4]) { |
| 41 | return {}; |
| 42 | } |
| 43 | |
| 44 | return view[3]; |
| 45 | } |
| 46 | |
| 47 | std::string_view parseEdidText(const byte_view& view) { |
| 48 | std::string_view text(reinterpret_cast<const char*>(view.data()), view.size()); |
| 49 | text = text.substr(0, text.find('\n')); |
| 50 | |
| 51 | if (!std::all_of(text.begin(), text.end(), ::isprint)) { |
| 52 | ALOGW("Invalid EDID: ASCII text is not printable."); |
| 53 | return {}; |
| 54 | } |
| 55 | |
| 56 | return text; |
| 57 | } |
| 58 | |
| 59 | // Big-endian 16-bit value encodes three 5-bit letters where A is 0b00001. |
| 60 | template <size_t I> |
| 61 | char getPnpLetter(uint16_t id) { |
| 62 | static_assert(I < 3); |
| 63 | const char letter = 'A' + (static_cast<uint8_t>(id >> ((2 - I) * 5)) & 0b00011111) - 1; |
| 64 | return letter < 'A' || letter > 'Z' ? '\0' : letter; |
| 65 | } |
| 66 | |
| 67 | DisplayId getEdidDisplayId(uint8_t port, uint16_t manufacturerId, uint32_t displayNameHash) { |
| 68 | return (static_cast<DisplayId>(manufacturerId) << 40) | |
| 69 | (static_cast<DisplayId>(displayNameHash) << 8) | port; |
| 70 | } |
| 71 | |
| 72 | } // namespace |
| 73 | |
| 74 | bool isEdid(const DisplayIdentificationData& data) { |
| 75 | const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}; |
| 76 | return data.size() >= sizeof(kMagic) && |
| 77 | std::equal(std::begin(kMagic), std::end(kMagic), data.begin()); |
| 78 | } |
| 79 | |
| 80 | std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) { |
| 81 | constexpr size_t kMinLength = 128; |
| 82 | if (edid.size() < kMinLength) { |
| 83 | ALOGW("Invalid EDID: structure is truncated."); |
| 84 | // Attempt parsing even if EDID is malformed. |
| 85 | } else { |
| 86 | ALOGW_IF(edid[126] != 0, "EDID extensions are currently unsupported."); |
| 87 | ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kMinLength, static_cast<uint8_t>(0)), |
| 88 | "Invalid EDID: structure does not checksum."); |
| 89 | } |
| 90 | |
| 91 | constexpr size_t kManufacturerOffset = 8; |
| 92 | if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) { |
| 93 | ALOGE("Invalid EDID: manufacturer ID is truncated."); |
| 94 | return {}; |
| 95 | } |
| 96 | |
| 97 | // Plug and play ID encoded as big-endian 16-bit value. |
| 98 | const uint16_t manufacturerId = |
| 99 | (edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1]; |
| 100 | |
| 101 | const auto pnpId = getPnpId(manufacturerId); |
| 102 | if (!pnpId) { |
| 103 | ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID."); |
| 104 | return {}; |
| 105 | } |
| 106 | |
| 107 | constexpr size_t kDescriptorOffset = 54; |
| 108 | if (edid.size() < kDescriptorOffset) { |
| 109 | ALOGE("Invalid EDID: descriptors are missing."); |
| 110 | return {}; |
| 111 | } |
| 112 | |
| 113 | byte_view view(edid.data(), edid.size()); |
| 114 | view.remove_prefix(kDescriptorOffset); |
| 115 | |
| 116 | std::string_view displayName; |
| 117 | std::string_view serialNumber; |
| 118 | std::string_view asciiText; |
| 119 | |
| 120 | constexpr size_t kDescriptorCount = 4; |
| 121 | constexpr size_t kDescriptorLength = 18; |
| 122 | |
| 123 | for (size_t i = 0; i < kDescriptorCount; i++) { |
| 124 | if (view.size() < kDescriptorLength) { |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | if (const auto type = getEdidDescriptorType(view)) { |
| 129 | byte_view descriptor(view.data(), kDescriptorLength); |
| 130 | descriptor.remove_prefix(kEdidHeaderLength); |
| 131 | |
| 132 | switch (*type) { |
| 133 | case 0xfc: |
| 134 | displayName = parseEdidText(descriptor); |
| 135 | break; |
| 136 | case 0xfe: |
| 137 | asciiText = parseEdidText(descriptor); |
| 138 | break; |
| 139 | case 0xff: |
| 140 | serialNumber = parseEdidText(descriptor); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | view.remove_prefix(kDescriptorLength); |
| 146 | } |
| 147 | |
| 148 | if (displayName.empty()) { |
| 149 | ALOGW("Invalid EDID: falling back to serial number due to missing display name."); |
| 150 | displayName = serialNumber; |
| 151 | } |
| 152 | if (displayName.empty()) { |
| 153 | ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number."); |
| 154 | displayName = asciiText; |
| 155 | } |
| 156 | if (displayName.empty()) { |
| 157 | ALOGE("Invalid EDID: display name and fallback descriptors are missing."); |
| 158 | return {}; |
| 159 | } |
| 160 | |
| 161 | return Edid{manufacturerId, *pnpId, displayName}; |
| 162 | } |
| 163 | |
| 164 | std::optional<PnpId> getPnpId(uint16_t manufacturerId) { |
| 165 | const char a = getPnpLetter<0>(manufacturerId); |
| 166 | const char b = getPnpLetter<1>(manufacturerId); |
| 167 | const char c = getPnpLetter<2>(manufacturerId); |
| 168 | return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt; |
| 169 | } |
| 170 | |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame^] | 171 | std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData( |
| 172 | uint8_t port, const DisplayIdentificationData& data) { |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 173 | if (!isEdid(data)) { |
| 174 | ALOGE("Display identification data has unknown format."); |
| 175 | return {}; |
| 176 | } |
| 177 | |
| 178 | const auto edid = parseEdid(data); |
| 179 | if (!edid) { |
| 180 | return {}; |
| 181 | } |
| 182 | |
| 183 | // Hash display name instead of using product code or serial number, since the latter have been |
| 184 | // observed to change on some displays with multiple inputs. |
| 185 | const auto hash = static_cast<uint32_t>(std::hash<std::string_view>()(edid->displayName)); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame^] | 186 | return DisplayIdentificationInfo{getEdidDisplayId(port, edid->manufacturerId, hash), |
| 187 | std::string(edid->displayName)}; |
| 188 | } |
| 189 | |
| 190 | DisplayId getFallbackDisplayId(uint8_t port) { |
| 191 | return getEdidDisplayId(port, kFallbackEdidManufacturerId, 0); |
| 192 | } |
| 193 | |
| 194 | DisplayId getVirtualDisplayId(uint32_t id) { |
| 195 | return getEdidDisplayId(0, kVirtualEdidManufacturerId, id); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | } // namespace android |