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 | |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 21 | #undef LOG_TAG |
| 22 | #define LOG_TAG "DisplayIdentification" |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <cctype> |
| 26 | #include <numeric> |
| 27 | #include <optional> |
| 28 | |
| 29 | #include <log/log.h> |
| 30 | |
| 31 | #include "DisplayIdentification.h" |
| 32 | |
| 33 | namespace android { |
| 34 | namespace { |
| 35 | |
| 36 | using byte_view = std::basic_string_view<uint8_t>; |
| 37 | |
| 38 | constexpr size_t kEdidHeaderLength = 5; |
| 39 | |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 40 | constexpr uint16_t kFallbackEdidManufacturerId = 0; |
| 41 | constexpr uint16_t kVirtualEdidManufacturerId = 0xffffu; |
| 42 | |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 43 | std::optional<uint8_t> getEdidDescriptorType(const byte_view& view) { |
| 44 | if (view.size() < kEdidHeaderLength || view[0] || view[1] || view[2] || view[4]) { |
| 45 | return {}; |
| 46 | } |
| 47 | |
| 48 | return view[3]; |
| 49 | } |
| 50 | |
| 51 | std::string_view parseEdidText(const byte_view& view) { |
| 52 | std::string_view text(reinterpret_cast<const char*>(view.data()), view.size()); |
| 53 | text = text.substr(0, text.find('\n')); |
| 54 | |
| 55 | if (!std::all_of(text.begin(), text.end(), ::isprint)) { |
| 56 | ALOGW("Invalid EDID: ASCII text is not printable."); |
| 57 | return {}; |
| 58 | } |
| 59 | |
| 60 | return text; |
| 61 | } |
| 62 | |
| 63 | // Big-endian 16-bit value encodes three 5-bit letters where A is 0b00001. |
| 64 | template <size_t I> |
| 65 | char getPnpLetter(uint16_t id) { |
| 66 | static_assert(I < 3); |
| 67 | const char letter = 'A' + (static_cast<uint8_t>(id >> ((2 - I) * 5)) & 0b00011111) - 1; |
| 68 | return letter < 'A' || letter > 'Z' ? '\0' : letter; |
| 69 | } |
| 70 | |
Dominik Laskowski | 3415776 | 2018-10-31 13:07:19 -0700 | [diff] [blame] | 71 | } // namespace |
| 72 | |
| 73 | uint16_t DisplayId::manufacturerId() const { |
| 74 | return static_cast<uint16_t>(value >> 40); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Dominik Laskowski | 3415776 | 2018-10-31 13:07:19 -0700 | [diff] [blame] | 77 | DisplayId DisplayId::fromEdid(uint8_t port, uint16_t manufacturerId, uint32_t displayNameHash) { |
| 78 | return {(static_cast<Type>(manufacturerId) << 40) | (static_cast<Type>(displayNameHash) << 8) | |
| 79 | port}; |
| 80 | } |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 81 | |
| 82 | bool isEdid(const DisplayIdentificationData& data) { |
| 83 | const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}; |
| 84 | return data.size() >= sizeof(kMagic) && |
| 85 | std::equal(std::begin(kMagic), std::end(kMagic), data.begin()); |
| 86 | } |
| 87 | |
| 88 | std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) { |
| 89 | constexpr size_t kMinLength = 128; |
| 90 | if (edid.size() < kMinLength) { |
| 91 | ALOGW("Invalid EDID: structure is truncated."); |
| 92 | // Attempt parsing even if EDID is malformed. |
| 93 | } else { |
| 94 | ALOGW_IF(edid[126] != 0, "EDID extensions are currently unsupported."); |
| 95 | ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kMinLength, static_cast<uint8_t>(0)), |
| 96 | "Invalid EDID: structure does not checksum."); |
| 97 | } |
| 98 | |
| 99 | constexpr size_t kManufacturerOffset = 8; |
| 100 | if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) { |
| 101 | ALOGE("Invalid EDID: manufacturer ID is truncated."); |
| 102 | return {}; |
| 103 | } |
| 104 | |
| 105 | // Plug and play ID encoded as big-endian 16-bit value. |
| 106 | const uint16_t manufacturerId = |
| 107 | (edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1]; |
| 108 | |
| 109 | const auto pnpId = getPnpId(manufacturerId); |
| 110 | if (!pnpId) { |
| 111 | ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID."); |
| 112 | return {}; |
| 113 | } |
| 114 | |
| 115 | constexpr size_t kDescriptorOffset = 54; |
| 116 | if (edid.size() < kDescriptorOffset) { |
| 117 | ALOGE("Invalid EDID: descriptors are missing."); |
| 118 | return {}; |
| 119 | } |
| 120 | |
| 121 | byte_view view(edid.data(), edid.size()); |
| 122 | view.remove_prefix(kDescriptorOffset); |
| 123 | |
| 124 | std::string_view displayName; |
| 125 | std::string_view serialNumber; |
| 126 | std::string_view asciiText; |
| 127 | |
| 128 | constexpr size_t kDescriptorCount = 4; |
| 129 | constexpr size_t kDescriptorLength = 18; |
| 130 | |
| 131 | for (size_t i = 0; i < kDescriptorCount; i++) { |
| 132 | if (view.size() < kDescriptorLength) { |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | if (const auto type = getEdidDescriptorType(view)) { |
| 137 | byte_view descriptor(view.data(), kDescriptorLength); |
| 138 | descriptor.remove_prefix(kEdidHeaderLength); |
| 139 | |
| 140 | switch (*type) { |
| 141 | case 0xfc: |
| 142 | displayName = parseEdidText(descriptor); |
| 143 | break; |
| 144 | case 0xfe: |
| 145 | asciiText = parseEdidText(descriptor); |
| 146 | break; |
| 147 | case 0xff: |
| 148 | serialNumber = parseEdidText(descriptor); |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | view.remove_prefix(kDescriptorLength); |
| 154 | } |
| 155 | |
| 156 | if (displayName.empty()) { |
| 157 | ALOGW("Invalid EDID: falling back to serial number due to missing display name."); |
| 158 | displayName = serialNumber; |
| 159 | } |
| 160 | if (displayName.empty()) { |
| 161 | ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number."); |
| 162 | displayName = asciiText; |
| 163 | } |
| 164 | if (displayName.empty()) { |
| 165 | ALOGE("Invalid EDID: display name and fallback descriptors are missing."); |
| 166 | return {}; |
| 167 | } |
| 168 | |
| 169 | return Edid{manufacturerId, *pnpId, displayName}; |
| 170 | } |
| 171 | |
| 172 | std::optional<PnpId> getPnpId(uint16_t manufacturerId) { |
| 173 | const char a = getPnpLetter<0>(manufacturerId); |
| 174 | const char b = getPnpLetter<1>(manufacturerId); |
| 175 | const char c = getPnpLetter<2>(manufacturerId); |
| 176 | return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt; |
| 177 | } |
| 178 | |
Dominik Laskowski | 3415776 | 2018-10-31 13:07:19 -0700 | [diff] [blame] | 179 | std::optional<PnpId> getPnpId(DisplayId displayId) { |
| 180 | return getPnpId(displayId.manufacturerId()); |
| 181 | } |
| 182 | |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 183 | std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData( |
| 184 | uint8_t port, const DisplayIdentificationData& data) { |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 185 | if (!isEdid(data)) { |
| 186 | ALOGE("Display identification data has unknown format."); |
| 187 | return {}; |
| 188 | } |
| 189 | |
| 190 | const auto edid = parseEdid(data); |
| 191 | if (!edid) { |
| 192 | return {}; |
| 193 | } |
| 194 | |
| 195 | // Hash display name instead of using product code or serial number, since the latter have been |
| 196 | // observed to change on some displays with multiple inputs. |
| 197 | const auto hash = static_cast<uint32_t>(std::hash<std::string_view>()(edid->displayName)); |
Dominik Laskowski | 3415776 | 2018-10-31 13:07:19 -0700 | [diff] [blame] | 198 | return DisplayIdentificationInfo{DisplayId::fromEdid(port, edid->manufacturerId, hash), |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 199 | std::string(edid->displayName)}; |
| 200 | } |
| 201 | |
| 202 | DisplayId getFallbackDisplayId(uint8_t port) { |
Dominik Laskowski | 3415776 | 2018-10-31 13:07:19 -0700 | [diff] [blame] | 203 | return DisplayId::fromEdid(port, kFallbackEdidManufacturerId, 0); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | DisplayId getVirtualDisplayId(uint32_t id) { |
Dominik Laskowski | 3415776 | 2018-10-31 13:07:19 -0700 | [diff] [blame] | 207 | return DisplayId::fromEdid(0, kVirtualEdidManufacturerId, id); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 211 | |
| 212 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 213 | #pragma clang diagnostic pop // ignored "-Wconversion" |