blob: a45ffe1a1959c4462537238a524e4d970f3771b7 [file] [log] [blame]
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -07001/*
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>
Ryan Prichard3b17f282024-02-08 02:31:50 -080024#include <span>
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070025
26#include <log/log.h>
27
Alec Mouriff793872022-01-13 17:45:06 -080028#include <ui/DisplayIdentification.h>
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070029
30namespace android {
31namespace {
32
Alec Mouriff793872022-01-13 17:45:06 -080033template <class T>
34inline T load(const void* p) {
35 static_assert(std::is_integral<T>::value, "T must be integral");
36
37 T r;
38 std::memcpy(&r, p, sizeof(r));
39 return r;
40}
41
42uint64_t rotateByAtLeast1(uint64_t val, uint8_t shift) {
43 return (val >> shift) | (val << (64 - shift));
44}
45
46uint64_t shiftMix(uint64_t val) {
47 return val ^ (val >> 47);
48}
49
50uint64_t hash64Len16(uint64_t u, uint64_t v) {
51 constexpr uint64_t kMul = 0x9ddfea08eb382d69;
52 uint64_t a = (u ^ v) * kMul;
53 a ^= (a >> 47);
54 uint64_t b = (v ^ a) * kMul;
55 b ^= (b >> 47);
56 b *= kMul;
57 return b;
58}
59
60uint64_t hash64Len0To16(const char* s, uint64_t len) {
61 constexpr uint64_t k2 = 0x9ae16a3b2f90404f;
62 constexpr uint64_t k3 = 0xc949d7c7509e6557;
63
64 if (len > 8) {
65 const uint64_t a = load<uint64_t>(s);
66 const uint64_t b = load<uint64_t>(s + len - 8);
67 return hash64Len16(a, rotateByAtLeast1(b + len, static_cast<uint8_t>(len))) ^ b;
68 }
69 if (len >= 4) {
70 const uint32_t a = load<uint32_t>(s);
71 const uint32_t b = load<uint32_t>(s + len - 4);
72 return hash64Len16(len + (a << 3), b);
73 }
74 if (len > 0) {
75 const unsigned char a = static_cast<unsigned char>(s[0]);
76 const unsigned char b = static_cast<unsigned char>(s[len >> 1]);
77 const unsigned char c = static_cast<unsigned char>(s[len - 1]);
78 const uint32_t y = static_cast<uint32_t>(a) + (static_cast<uint32_t>(b) << 8);
79 const uint32_t z = static_cast<uint32_t>(len) + (static_cast<uint32_t>(c) << 2);
80 return shiftMix(y * k2 ^ z * k3) * k2;
81 }
82 return k2;
83}
84
Ryan Prichard3b17f282024-02-08 02:31:50 -080085using byte_view = std::span<const uint8_t>;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070086
Marin Shalamanov7a9ba302020-03-02 17:49:16 +010087constexpr size_t kEdidBlockSize = 128;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070088constexpr size_t kEdidHeaderLength = 5;
89
Dominik Laskowski075d3172018-05-24 15:50:06 -070090constexpr uint16_t kVirtualEdidManufacturerId = 0xffffu;
91
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070092std::optional<uint8_t> getEdidDescriptorType(const byte_view& view) {
Ryan Prichard3b17f282024-02-08 02:31:50 -080093 if (static_cast<size_t>(view.size()) < kEdidHeaderLength || view[0] || view[1] || view[2] ||
94 view[4]) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070095 return {};
96 }
97
98 return view[3];
99}
100
101std::string_view parseEdidText(const byte_view& view) {
102 std::string_view text(reinterpret_cast<const char*>(view.data()), view.size());
103 text = text.substr(0, text.find('\n'));
104
105 if (!std::all_of(text.begin(), text.end(), ::isprint)) {
106 ALOGW("Invalid EDID: ASCII text is not printable.");
107 return {};
108 }
109
110 return text;
111}
112
113// Big-endian 16-bit value encodes three 5-bit letters where A is 0b00001.
114template <size_t I>
115char getPnpLetter(uint16_t id) {
116 static_assert(I < 3);
117 const char letter = 'A' + (static_cast<uint8_t>(id >> ((2 - I) * 5)) & 0b00011111) - 1;
118 return letter < 'A' || letter > 'Z' ? '\0' : letter;
119}
120
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200121DeviceProductInfo buildDeviceProductInfo(const Edid& edid) {
122 DeviceProductInfo info;
Marin Shalamanov359a7e72020-02-17 17:03:07 +0100123 info.name.assign(edid.displayName);
124 info.productId = std::to_string(edid.productId);
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200125 info.manufacturerPnpId = edid.pnpId;
126
127 constexpr uint8_t kModelYearFlag = 0xff;
128 constexpr uint32_t kYearOffset = 1990;
129
130 const auto year = edid.manufactureOrModelYear + kYearOffset;
131 if (edid.manufactureWeek == kModelYearFlag) {
132 info.manufactureOrModelDate = DeviceProductInfo::ModelYear{.year = year};
133 } else if (edid.manufactureWeek == 0) {
134 DeviceProductInfo::ManufactureYear date;
135 date.year = year;
136 info.manufactureOrModelDate = date;
137 } else {
138 DeviceProductInfo::ManufactureWeekAndYear date;
139 date.year = year;
140 date.week = edid.manufactureWeek;
141 info.manufactureOrModelDate = date;
142 }
143
Marin Shalamanov896e6302020-04-06 16:11:25 +0200144 if (edid.cea861Block && edid.cea861Block->hdmiVendorDataBlock) {
145 const auto& address = edid.cea861Block->hdmiVendorDataBlock->physicalAddress;
146 info.relativeAddress = {address.a, address.b, address.c, address.d};
147 }
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200148 return info;
149}
150
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100151Cea861ExtensionBlock parseCea861Block(const byte_view& block) {
152 Cea861ExtensionBlock cea861Block;
153
154 constexpr size_t kRevisionNumberOffset = 1;
155 cea861Block.revisionNumber = block[kRevisionNumberOffset];
156
157 constexpr size_t kDetailedTimingDescriptorsOffset = 2;
158 const size_t dtdStart =
159 std::min(kEdidBlockSize, static_cast<size_t>(block[kDetailedTimingDescriptorsOffset]));
160
161 // Parse data blocks.
162 for (size_t dataBlockOffset = 4; dataBlockOffset < dtdStart;) {
163 const uint8_t header = block[dataBlockOffset];
164 const uint8_t tag = header >> 5;
165 const size_t bodyLength = header & 0b11111;
166 constexpr size_t kDataBlockHeaderSize = 1;
167 const size_t dataBlockSize = bodyLength + kDataBlockHeaderSize;
168
Ryan Prichard3b17f282024-02-08 02:31:50 -0800169 if (static_cast<size_t>(block.size()) < dataBlockOffset + dataBlockSize) {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100170 ALOGW("Invalid EDID: CEA 861 data block is truncated.");
171 break;
172 }
173
174 const byte_view dataBlock(block.data() + dataBlockOffset, dataBlockSize);
175 constexpr uint8_t kVendorSpecificDataBlockTag = 0x3;
176
177 if (tag == kVendorSpecificDataBlockTag) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200178 const uint32_t ieeeRegistrationId = static_cast<uint32_t>(
179 dataBlock[1] | (dataBlock[2] << 8) | (dataBlock[3] << 16));
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100180 constexpr uint32_t kHdmiIeeeRegistrationId = 0xc03;
181
182 if (ieeeRegistrationId == kHdmiIeeeRegistrationId) {
183 const uint8_t a = dataBlock[4] >> 4;
184 const uint8_t b = dataBlock[4] & 0b1111;
185 const uint8_t c = dataBlock[5] >> 4;
186 const uint8_t d = dataBlock[5] & 0b1111;
187 cea861Block.hdmiVendorDataBlock =
188 HdmiVendorDataBlock{.physicalAddress = HdmiPhysicalAddress{a, b, c, d}};
189 } else {
190 ALOGV("Ignoring vendor specific data block for vendor with IEEE OUI %x",
191 ieeeRegistrationId);
192 }
193 } else {
194 ALOGV("Ignoring CEA-861 data block with tag %x", tag);
195 }
196 dataBlockOffset += bodyLength + kDataBlockHeaderSize;
197 }
198
199 return cea861Block;
200}
201
Dominik Laskowski34157762018-10-31 13:07:19 -0700202} // namespace
203
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700204bool isEdid(const DisplayIdentificationData& data) {
205 const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
206 return data.size() >= sizeof(kMagic) &&
207 std::equal(std::begin(kMagic), std::end(kMagic), data.begin());
208}
209
210std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100211 if (edid.size() < kEdidBlockSize) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700212 ALOGW("Invalid EDID: structure is truncated.");
213 // Attempt parsing even if EDID is malformed.
214 } else {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100215 ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kEdidBlockSize,
216 static_cast<uint8_t>(0)),
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700217 "Invalid EDID: structure does not checksum.");
218 }
219
220 constexpr size_t kManufacturerOffset = 8;
221 if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) {
222 ALOGE("Invalid EDID: manufacturer ID is truncated.");
223 return {};
224 }
225
226 // Plug and play ID encoded as big-endian 16-bit value.
227 const uint16_t manufacturerId =
Marin Shalamanova524a092020-07-27 21:39:55 +0200228 static_cast<uint16_t>((edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1]);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700229
230 const auto pnpId = getPnpId(manufacturerId);
231 if (!pnpId) {
232 ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID.");
233 return {};
234 }
235
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200236 constexpr size_t kProductIdOffset = 10;
237 if (edid.size() < kProductIdOffset + sizeof(uint16_t)) {
238 ALOGE("Invalid EDID: product ID is truncated.");
239 return {};
240 }
Marin Shalamanova524a092020-07-27 21:39:55 +0200241 const uint16_t productId =
242 static_cast<uint16_t>(edid[kProductIdOffset] | (edid[kProductIdOffset + 1] << 8));
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200243
244 constexpr size_t kManufactureWeekOffset = 16;
245 if (edid.size() < kManufactureWeekOffset + sizeof(uint8_t)) {
246 ALOGE("Invalid EDID: manufacture week is truncated.");
247 return {};
248 }
249 const uint8_t manufactureWeek = edid[kManufactureWeekOffset];
250 ALOGW_IF(0x37 <= manufactureWeek && manufactureWeek <= 0xfe,
251 "Invalid EDID: week of manufacture cannot be in the range [0x37, 0xfe].");
252
253 constexpr size_t kManufactureYearOffset = 17;
254 if (edid.size() < kManufactureYearOffset + sizeof(uint8_t)) {
255 ALOGE("Invalid EDID: manufacture year is truncated.");
256 return {};
257 }
258 const uint8_t manufactureOrModelYear = edid[kManufactureYearOffset];
259 ALOGW_IF(manufactureOrModelYear <= 0xf,
260 "Invalid EDID: model year or manufacture year cannot be in the range [0x0, 0xf].");
261
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700262 constexpr size_t kDescriptorOffset = 54;
263 if (edid.size() < kDescriptorOffset) {
264 ALOGE("Invalid EDID: descriptors are missing.");
265 return {};
266 }
267
268 byte_view view(edid.data(), edid.size());
Ryan Prichard3b17f282024-02-08 02:31:50 -0800269 view = view.subspan(kDescriptorOffset);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700270
271 std::string_view displayName;
272 std::string_view serialNumber;
273 std::string_view asciiText;
274
275 constexpr size_t kDescriptorCount = 4;
276 constexpr size_t kDescriptorLength = 18;
277
278 for (size_t i = 0; i < kDescriptorCount; i++) {
Ryan Prichard3b17f282024-02-08 02:31:50 -0800279 if (static_cast<size_t>(view.size()) < kDescriptorLength) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700280 break;
281 }
282
283 if (const auto type = getEdidDescriptorType(view)) {
284 byte_view descriptor(view.data(), kDescriptorLength);
Ryan Prichard3b17f282024-02-08 02:31:50 -0800285 descriptor = descriptor.subspan(kEdidHeaderLength);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700286
287 switch (*type) {
288 case 0xfc:
289 displayName = parseEdidText(descriptor);
290 break;
291 case 0xfe:
292 asciiText = parseEdidText(descriptor);
293 break;
294 case 0xff:
295 serialNumber = parseEdidText(descriptor);
296 break;
297 }
298 }
299
Ryan Prichard3b17f282024-02-08 02:31:50 -0800300 view = view.subspan(kDescriptorLength);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700301 }
302
Dominik Laskowski17337962020-03-02 15:51:15 -0800303 std::string_view modelString = displayName;
304
305 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700306 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800307 modelString = serialNumber;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700308 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800309 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700310 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800311 modelString = asciiText;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700312 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800313 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700314 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
315 return {};
316 }
317
Dominik Laskowski17337962020-03-02 15:51:15 -0800318 // Hash model string instead of using product code or (integer) serial number, since the latter
Jason Macnak4afe8572021-07-16 13:57:41 -0700319 // have been observed to change on some displays with multiple inputs. Use a stable hash instead
320 // of std::hash which is only required to be same within a single execution of a program.
321 const uint32_t modelHash = static_cast<uint32_t>(cityHash64Len0To16(modelString));
Dominik Laskowski17337962020-03-02 15:51:15 -0800322
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100323 // Parse extension blocks.
324 std::optional<Cea861ExtensionBlock> cea861Block;
325 if (edid.size() < kEdidBlockSize) {
326 ALOGW("Invalid EDID: block 0 is truncated.");
327 } else {
328 constexpr size_t kNumExtensionsOffset = 126;
329 const size_t numExtensions = edid[kNumExtensionsOffset];
330 view = byte_view(edid.data(), edid.size());
331 for (size_t blockNumber = 1; blockNumber <= numExtensions; blockNumber++) {
Ryan Prichard3b17f282024-02-08 02:31:50 -0800332 view = view.subspan(kEdidBlockSize);
333 if (static_cast<size_t>(view.size()) < kEdidBlockSize) {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100334 ALOGW("Invalid EDID: block %zu is truncated.", blockNumber);
335 break;
336 }
337
338 const byte_view block(view.data(), kEdidBlockSize);
339 ALOGW_IF(std::accumulate(block.begin(), block.end(), static_cast<uint8_t>(0)),
340 "Invalid EDID: block %zu does not checksum.", blockNumber);
341 const uint8_t tag = block[0];
342
343 constexpr uint8_t kCea861BlockTag = 0x2;
344 if (tag == kCea861BlockTag) {
345 cea861Block = parseCea861Block(block);
346 } else {
347 ALOGV("Ignoring block number %zu with tag %x.", blockNumber, tag);
348 }
349 }
350 }
351
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200352 return Edid{.manufacturerId = manufacturerId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200353 .productId = productId,
Dominik Laskowski17337962020-03-02 15:51:15 -0800354 .pnpId = *pnpId,
355 .modelHash = modelHash,
356 .displayName = displayName,
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100357 .manufactureOrModelYear = manufactureOrModelYear,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200358 .manufactureWeek = manufactureWeek,
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100359 .cea861Block = cea861Block};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700360}
361
362std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
363 const char a = getPnpLetter<0>(manufacturerId);
364 const char b = getPnpLetter<1>(manufacturerId);
365 const char c = getPnpLetter<2>(manufacturerId);
366 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
367}
368
Marin Shalamanova524a092020-07-27 21:39:55 +0200369std::optional<PnpId> getPnpId(PhysicalDisplayId displayId) {
370 return getPnpId(displayId.getManufacturerId());
Dominik Laskowski34157762018-10-31 13:07:19 -0700371}
372
Dominik Laskowski075d3172018-05-24 15:50:06 -0700373std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
374 uint8_t port, const DisplayIdentificationData& data) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700375 if (!isEdid(data)) {
376 ALOGE("Display identification data has unknown format.");
377 return {};
378 }
379
380 const auto edid = parseEdid(data);
381 if (!edid) {
382 return {};
383 }
384
Marin Shalamanova524a092020-07-27 21:39:55 +0200385 const auto displayId = PhysicalDisplayId::fromEdid(port, edid->manufacturerId, edid->modelHash);
Dominik Laskowski17337962020-03-02 15:51:15 -0800386 return DisplayIdentificationInfo{.id = displayId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200387 .name = std::string(edid->displayName),
388 .deviceProductInfo = buildDeviceProductInfo(*edid)};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700389}
390
Marin Shalamanova524a092020-07-27 21:39:55 +0200391PhysicalDisplayId getVirtualDisplayId(uint32_t id) {
392 return PhysicalDisplayId::fromEdid(0, kVirtualEdidManufacturerId, id);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700393}
394
Alec Mouriff793872022-01-13 17:45:06 -0800395uint64_t cityHash64Len0To16(std::string_view sv) {
396 auto len = sv.length();
397 if (len > 16) {
398 ALOGE("%s called with length %zu. Only hashing the first 16 chars", __FUNCTION__, len);
399 len = 16;
400 }
401 return hash64Len0To16(sv.data(), len);
402}
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800403
Alec Mouriff793872022-01-13 17:45:06 -0800404} // namespace android