From 7f18cae282561be3a53179d8186ff3dd5b1fefc5 Mon Sep 17 00:00:00 2001 From: adarshgm Date: Tue, 7 Mar 2023 05:21:19 +0000 Subject: [PATCH] edid: Integrate libdisplay-info for edid parsing Hides libdisplay-info under a build time default-off flag, provides provision to parse essential edid parameters with APIs provided by libdisplay-info. This implementaion increases readibility, avoids code duplication and decreases complexity of edid parsing. Part-of: --- config.h.meson | 3 + meson.build | 6 + meson_options.txt | 6 + src/backends/edid-parse.c | 217 +++++++++++++++++++++++++++++++++++-- src/backends/edid.h | 3 +- src/backends/meta-output.c | 8 +- src/meson.build | 6 + src/tests/edid-tests.c | 16 +-- 8 files changed, 245 insertions(+), 20 deletions(-) diff --git a/config.h.meson b/config.h.meson index 6f61ddeb0..c4038ecc5 100644 --- a/config.h.meson +++ b/config.h.meson @@ -109,3 +109,6 @@ /* Whether the Xwayland supports +/-byteswappedclients */ #mesondefine HAVE_XWAYLAND_BYTE_SWAPPED_CLIENTS + +/* Defined if libdisplay-info is enabled */ +#mesondefine HAVE_LIBDISPLAY_INFO diff --git a/meson.build b/meson.build index 7fb0baf58..d3b22a270 100644 --- a/meson.build +++ b/meson.build @@ -168,6 +168,10 @@ if have_x11_client xau_dep = dependency('xau') endif +have_libdisplay_info = get_option('libdisplay_info') +if have_libdisplay_info + libdisplay_info_dep = dependency('libdisplay-info') +endif have_gnome_desktop = get_option('libgnome_desktop') if have_gnome_desktop @@ -532,6 +536,7 @@ cdata.set('HAVE_SM', have_sm) cdata.set('HAVE_STARTUP_NOTIFICATION', have_startup_notification) cdata.set('HAVE_INTROSPECTION', have_introspection) cdata.set('HAVE_PROFILER', have_profiler) +cdata.set('HAVE_LIBDISPLAY_INFO', have_libdisplay_info) if have_x11_client xkb_base = xkeyboard_config_dep.get_variable('xkb_base') @@ -687,6 +692,7 @@ summary('Native Backend', have_native_backend, section: 'Options') summary('EGL Device', have_egl_device, section: 'Options') summary('Remote desktop', have_remote_desktop, section: 'Options') summary('libgnome-desktop', have_gnome_desktop, section: 'Options') +summary('libdisplay-info', have_libdisplay_info, section: 'Options') summary('Sound player', have_sound_player, section: 'Options') summary('gudev', have_libgudev, section: 'Options') summary('Wacom', have_libwacom, section: 'Options') diff --git a/meson_options.txt b/meson_options.txt index a13a25c9b..b5d215b24 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -224,3 +224,9 @@ option('catch', value: false, description: 'Use catch to catch backtraces' ) + +option('libdisplay_info', + type: 'boolean', + value: false, + description: 'Build with or without libdisplay-info' +) diff --git a/src/backends/edid-parse.c b/src/backends/edid-parse.c index 2723c4a14..6c3878429 100644 --- a/src/backends/edid-parse.c +++ b/src/backends/edid-parse.c @@ -29,6 +29,11 @@ #include #include +#ifdef HAVE_LIBDISPLAY_INFO +#include +#include +#endif + #include "backends/edid.h" /* VESA E-EDID */ @@ -313,19 +318,217 @@ decode_extensions (const uint8_t *edid, return TRUE; } +#ifdef HAVE_LIBDISPLAY_INFO +static void +decode_edid_descriptors (const struct di_edid *di_edid, + const struct di_edid_display_descriptor *desc, + MetaEdidInfo *info) +{ + enum di_edid_display_descriptor_tag desc_tag; + + desc_tag = di_edid_display_descriptor_get_tag (desc); + + switch (desc_tag) + { + case DI_EDID_DISPLAY_DESCRIPTOR_PRODUCT_SERIAL: + info->dsc_serial_number = di_edid_display_descriptor_get_string (desc); + break; + case DI_EDID_DISPLAY_DESCRIPTOR_PRODUCT_NAME: + info->dsc_product_name = di_edid_display_descriptor_get_string (desc); + break; + } +} + +static void +decode_edid_colorimetry (const struct di_cta_colorimetry_block *colorimetry, + MetaEdidInfo *info) +{ + /* Colorimetry Data Block */ + if (colorimetry->xvycc_601) + info->colorimetry |= META_EDID_COLORIMETRY_XVYCC601; + if (colorimetry->xvycc_709) + info->colorimetry |= META_EDID_COLORIMETRY_XVYCC709; + if (colorimetry->sycc_601) + info->colorimetry |= META_EDID_COLORIMETRY_SYCC601; + if (colorimetry->opycc_601) + info->colorimetry |= META_EDID_COLORIMETRY_OPYCC601; + if (colorimetry->oprgb) + info->colorimetry |= META_EDID_COLORIMETRY_OPRGB; + if (colorimetry->bt2020_cycc) + info->colorimetry |= META_EDID_COLORIMETRY_BT2020CYCC; + if (colorimetry->bt2020_ycc) + info->colorimetry |= META_EDID_COLORIMETRY_BT2020YCC; + if (colorimetry->bt2020_rgb) + info->colorimetry |= META_EDID_COLORIMETRY_BT2020RGB; + if (colorimetry->st2113_rgb) + info->colorimetry |= META_EDID_COLORIMETRY_ST2113RGB; + if (colorimetry->ictcp) + info->colorimetry |= META_EDID_COLORIMETRY_ICTCP; +} + +static void +decode_edid_hdr_static_metadata (const struct di_cta_hdr_static_metadata_block *hdr, + MetaEdidInfo *info) +{ + /* HDR Static Metadata Block */ + if (hdr->eotfs->traditional_sdr) + info->hdr_static_metadata.tf |= META_EDID_TF_TRADITIONAL_GAMMA_SDR; + if (hdr->eotfs->traditional_hdr) + info->hdr_static_metadata.tf |= META_EDID_TF_TRADITIONAL_GAMMA_HDR; + if (hdr->eotfs->pq) + info->hdr_static_metadata.tf |= META_EDID_TF_PQ; + if (hdr->eotfs->hlg) + info->hdr_static_metadata.tf |= META_EDID_TF_HLG; + + if (hdr->descriptors->type1) + info->hdr_static_metadata.sm |= META_EDID_STATIC_METADATA_TYPE1; + + if (hdr->desired_content_max_luminance != 0) + info->hdr_static_metadata.max_luminance = round (hdr->desired_content_max_luminance); + if (hdr->desired_content_max_frame_avg_luminance != 0) + info->hdr_static_metadata.max_fal = hdr->desired_content_max_frame_avg_luminance; + if (hdr->desired_content_min_luminance != 0) + info->hdr_static_metadata.min_luminance = round (hdr->desired_content_min_luminance); +} +static void +decode_edid_cta_ext (const struct di_edid_cta *cta, + MetaEdidInfo *info) +{ + const struct di_cta_data_block *const *data_blks; + const struct di_cta_data_block *data_blk; + enum di_cta_data_block_tag data_blk_tag; + const struct di_cta_colorimetry_block *colorimetry; + const struct di_cta_hdr_static_metadata_block *hdr_static_metadata; + + data_blks = di_edid_cta_get_data_blocks (cta); + for (data_index = 0; data_blks[data_index]!=NULL; data_index++) + { + data_blk = data_blks[data_index]; + data_blk_tag = di_cta_data_block_get_tag (data_blk); + + switch (data_blk_tag) + { + case DI_CTA_DATA_BLOCK_COLORIMETRY: + colorimetry = di_cta_data_block_get_colorimetry (data_blk); + g_assert (colorimetry); + decode_edid_colorimetry (colorimetry, info) + break; + case DI_CTA_DATA_BLOCK_HDR_STATIC_METADATA: + hdr_static_metadata = di_cta_data_block_get_hdr_static_metadata (data_blk); + g_assert (hdr_static_metadata); + decode_edid_hdr_static_metadata (hdr_static_metadata, info); + break; + } + } +} + +static void +decode_edid_extensions (const struct di_edid_ext *ext, + MetaEdidInfo *info) +{ + enum di_edid_ext_tag ext_tag; + const struct di_edid_cta *cta; + ext_tag = di_edid_ext_get_tag (ext); + + switch (ext_tag) + { + case DI_EDID_EXT_CEA: + cta = di_edid_ext_get_cta (ext); + decode_edid_cta_ext (cta, info); + break; + } +} +#endif + +static gboolean +decode_edid_info (const uint8_t *edid, + MetaEdidInfo *info, + size_t size) +{ +#ifdef HAVE_LIBDISPLAY_INFO + const struct di_edid *di_edid; + struct di_info *edid_info; + const struct di_edid_vendor_product *vendor_product; + const struct di_edid_chromaticity_coords *chromaticity_coords; + float gamma; + const struct di_edid_display_descriptor *const *edid_descriptors; + const struct di_edid_ext *const *extensions; + + edid_info = di_info_parse_edid (edid, size); + + if (!edid_info) + { + return FALSE; + } + + di_edid = di_info_get_edid (edid_info); + + /* Vendor and Product identification */ + vendor_product = di_edid_get_vendor_product (di_edid); + + /* Manufacturer Code */ + strncpy (info->manufacturer_code, vendor_product->manufacturer, + sizeof(info->manufacturer_code)); + + /* Product Code */ + info->product_code = vendor_product->product; + + /* Serial Number */ + info->serial_number = vendor_product->serial; + + /* Color Characteristics */ + chromaticity_coords = di_edid_get_chromaticity_coords (di_edid); + info->red_x = chromaticity_coords->red_x; + info->red_y = chromaticity_coords->red_y; + info->green_x = chromaticity_coords->green_x; + info->green_y = chromaticity_coords->green_y; + info->blue_x = chromaticity_coords->blue_x; + info->blue_y = chromaticity_coords->blue_y; + info->white_x = chromaticity_coords->white_x; + info->white_y = chromaticity_coords->white_y; + + /* Gamma */ + gamma = di_edid_get_basic_gamma (di_edid); + if (gamma != 0) + info->gamma = gamma; + else + info->gamma = -1; + + /* Descriptors */ + edid_descriptors = di_edid_get_display_descriptors (di_edid); + for (desc_index = 0; edid_descriptors[desc_index] != NULL; i++) + { + decode_edid_descriptors (di_edid, edid_descriptors[desc_index], info); + } + + /* Extension Blocks */ + extensions = di_edid_get_extensions (di_edid); + + for (ext_index = 0; extensions[ext_index] != NULL; i++) + { + decode_edid_extensions (extensions[ext_index], info); + } + + return TRUE; +#else + return decode_header (edid) && + decode_vendor_and_product_identification (edid, info) && + decode_display_parameters (edid, info) && + decode_color_characteristics (edid, info) && + decode_descriptors (edid, info) && + decode_extensions (edid, info); +#endif +} + MetaEdidInfo * -meta_edid_info_new_parse (const uint8_t *edid) +meta_edid_info_new_parse (const uint8_t *edid, + size_t size) { MetaEdidInfo *info; info = g_new0 (MetaEdidInfo, 1); - if (decode_header (edid) - && decode_vendor_and_product_identification (edid, info) - && decode_display_parameters (edid, info) - && decode_color_characteristics (edid, info) - && decode_descriptors (edid, info) - && decode_extensions (edid, info)) + if (decode_edid_info (edid, info, size)) { return info; } diff --git a/src/backends/edid.h b/src/backends/edid.h index 149d12924..98cb32948 100644 --- a/src/backends/edid.h +++ b/src/backends/edid.h @@ -96,6 +96,7 @@ struct _MetaEdidInfo }; META_EXPORT_TEST -MetaEdidInfo *meta_edid_info_new_parse (const uint8_t *data); +MetaEdidInfo *meta_edid_info_new_parse (const uint8_t *edid, + size_t size); #endif diff --git a/src/backends/meta-output.c b/src/backends/meta-output.c index 2610b3509..48c209e99 100644 --- a/src/backends/meta-output.c +++ b/src/backends/meta-output.c @@ -355,17 +355,17 @@ meta_output_info_parse_edid (MetaOutputInfo *output_info, GBytes *edid) { MetaEdidInfo *edid_info; - size_t len; + size_t size; gconstpointer data; g_return_if_fail (!output_info->edid_info); g_return_if_fail (edid); - data = g_bytes_get_data (edid, &len); - edid_info = meta_edid_info_new_parse (data); + data = g_bytes_get_data (edid, &size); + edid_info = meta_edid_info_new_parse (data, size); output_info->edid_checksum_md5 = g_compute_checksum_for_data (G_CHECKSUM_MD5, - data, len); + data, size); if (edid_info) { diff --git a/src/meson.build b/src/meson.build index a4fe0850c..3edefc51e 100644 --- a/src/meson.build +++ b/src/meson.build @@ -35,6 +35,12 @@ if have_gnome_desktop ] endif +if have_libdisplay_info + mutter_pkg_private_deps += [ + libdisplay_info_dep, + ] +endif + if have_sound_player mutter_pkg_private_deps += [ libcanberra_dep, diff --git a/src/tests/edid-tests.c b/src/tests/edid-tests.c index 055d48c6c..ebe66e388 100644 --- a/src/tests/edid-tests.c +++ b/src/tests/edid-tests.c @@ -65,15 +65,15 @@ int main (int argc, char **argv) { - MetaEdidInfo *edid; + MetaEdidInfo *edid_info; + edid_info = meta_edid_info_new_parse (edid_blob,edid_blob_len); - edid = meta_edid_info_new_parse (edid_blob); - g_assert (edid != NULL); - g_assert (strcmp (edid->manufacturer_code, "GSM") == 0); - g_assert (edid->product_code == 23507); - g_assert (edid->hdr_static_metadata.max_luminance == 97); - g_assert (edid->hdr_static_metadata.tf == + g_assert (edid_info != NULL); + g_assert (strcmp (edid_info->manufacturer_code, "GSM") == 0); + g_assert (edid_info->product_code == 23507); + g_assert (edid_info->hdr_static_metadata.max_luminance == 97); + g_assert (edid_info->hdr_static_metadata.tf == (META_EDID_TF_TRADITIONAL_GAMMA_SDR | META_EDID_TF_PQ)); - g_assert (edid->colorimetry == + g_assert (edid_info->colorimetry == (META_EDID_COLORIMETRY_BT2020YCC | META_EDID_COLORIMETRY_BT2020RGB)); }