[PATCH xserver 2/2] modesetting: Use helper to fetch drmModeProperty(Blob)s

Daniel Martin consume.noise at gmail.com
Mon Oct 23 08:31:21 UTC 2017


Replace the various loops to lookup drmModeProperty(Blob)s by
introducing helper functions.

Signed-off-by: Daniel Martin <consume.noise at gmail.com>
---
 hw/xfree86/drivers/modesetting/drmmode_display.c | 163 +++++++++++------------
 1 file changed, 77 insertions(+), 86 deletions(-)

diff --git a/hw/xfree86/drivers/modesetting/drmmode_display.c b/hw/xfree86/drivers/modesetting/drmmode_display.c
index 2375fa07d..3897cbcf2 100644
--- a/hw/xfree86/drivers/modesetting/drmmode_display.c
+++ b/hw/xfree86/drivers/modesetting/drmmode_display.c
@@ -1187,13 +1187,57 @@ drmmode_output_mode_valid(xf86OutputPtr output, DisplayModePtr pModes)
     return MODE_OK;
 }
 
+static int
+koutput_get_prop_idx(int fd, drmModeConnectorPtr koutput,
+        int type, const char *name)
+{
+    int idx = -1;
+
+    for (int i = 0; i < koutput->count_props; i++) {
+        drmModePropertyPtr prop = drmModeGetProperty(fd, koutput->props[i]);
+
+        if (!prop)
+            continue;
+
+        if (drm_property_type_is(prop, type) && !strcmp(prop->name, name))
+            idx = i;
+
+        drmModeFreeProperty(prop);
+
+        if (idx > -1)
+            break;
+    }
+
+    return idx;
+}
+
+static int
+koutput_get_prop_id(int fd, drmModeConnectorPtr koutput,
+        int type, const char *name)
+{
+    int idx = koutput_get_prop_idx(fd, koutput, type, name);
+
+    return (idx > -1) ? koutput->props[idx] : -1;
+}
+
+static drmModePropertyBlobPtr
+koutput_get_prop_blob(int fd, drmModeConnectorPtr koutput, const char *name)
+{
+    drmModePropertyBlobPtr blob = NULL;
+    int idx = koutput_get_prop_idx(fd, koutput, DRM_MODE_PROP_BLOB, name);
+
+    if (idx > -1)
+        blob = drmModeGetPropertyBlob(fd, koutput->prop_values[idx]);
+
+    return blob;
+}
+
 static void
 drmmode_output_attach_tile(xf86OutputPtr output)
 {
     drmmode_output_private_ptr drmmode_output = output->driver_private;
     drmModeConnectorPtr koutput = drmmode_output->mode_output;
     drmmode_ptr drmmode = drmmode_output->drmmode;
-    int i;
     struct xf86CrtcTileInfo tile_info, *set = NULL;
 
     if (!koutput) {
@@ -1201,25 +1245,12 @@ drmmode_output_attach_tile(xf86OutputPtr output)
         return;
     }
 
+    drmModeFreePropertyBlob(drmmode_output->tile_blob);
+
     /* look for a TILE property */
-    for (i = 0; i < koutput->count_props; i++) {
-        drmModePropertyPtr props;
-        props = drmModeGetProperty(drmmode->fd, koutput->props[i]);
-        if (!props)
-            continue;
+    drmmode_output->tile_blob =
+        koutput_get_prop_blob(drmmode->fd, koutput, "TILE");
 
-        if (!(props->flags & DRM_MODE_PROP_BLOB)) {
-            drmModeFreeProperty(props);
-            continue;
-        }
-
-        if (!strcmp(props->name, "TILE")) {
-            drmModeFreePropertyBlob(drmmode_output->tile_blob);
-            drmmode_output->tile_blob =
-                drmModeGetPropertyBlob(drmmode->fd, koutput->prop_values[i]);
-        }
-        drmModeFreeProperty(props);
-    }
     if (drmmode_output->tile_blob) {
         if (xf86OutputParseKMSTile(drmmode_output->tile_blob->data, drmmode_output->tile_blob->length, &tile_info) == TRUE)
             set = &tile_info;
@@ -1233,26 +1264,15 @@ has_panel_fitter(xf86OutputPtr output)
     drmmode_output_private_ptr drmmode_output = output->driver_private;
     drmModeConnectorPtr koutput = drmmode_output->mode_output;
     drmmode_ptr drmmode = drmmode_output->drmmode;
-    int i;
+    int idx;
 
     /* Presume that if the output supports scaling, then we have a
      * panel fitter capable of adjust any mode to suit.
      */
-    for (i = 0; i < koutput->count_props; i++) {
-        drmModePropertyPtr props;
-        Bool found = FALSE;
+    idx = koutput_get_prop_idx(drmmode->fd, koutput,
+            DRM_MODE_PROP_ENUM, "scaling mode");
 
-        props = drmModeGetProperty(drmmode->fd, koutput->props[i]);
-        if (props) {
-            found = strcmp(props->name, "scaling mode") == 0;
-            drmModeFreeProperty(props);
-        }
-
-        if (found)
-            return TRUE;
-    }
-
-    return FALSE;
+    return (idx > -1) ? TRUE : FALSE;
 }
 
 static DisplayModePtr
@@ -1306,26 +1326,16 @@ drmmode_output_get_modes(xf86OutputPtr output)
     drmmode_ptr drmmode = drmmode_output->drmmode;
     int i;
     DisplayModePtr Modes = NULL, Mode;
-    drmModePropertyPtr props;
     xf86MonPtr mon = NULL;
 
     if (!koutput)
         return NULL;
 
+    drmModeFreePropertyBlob(drmmode_output->edid_blob);
+
     /* look for an EDID property */
-    for (i = 0; i < koutput->count_props; i++) {
-        props = drmModeGetProperty(drmmode->fd, koutput->props[i]);
-        if (props && (props->flags & DRM_MODE_PROP_BLOB)) {
-            if (!strcmp(props->name, "EDID")) {
-                if (drmmode_output->edid_blob)
-                    drmModeFreePropertyBlob(drmmode_output->edid_blob);
-                drmmode_output->edid_blob =
-                    drmModeGetPropertyBlob(drmmode->fd,
-                                           koutput->prop_values[i]);
-            }
-            drmModeFreeProperty(props);
-        }
-    }
+    drmmode_output->edid_blob =
+        koutput_get_prop_blob(drmmode->fd, koutput, "EDID");
 
     if (drmmode_output->edid_blob) {
         mon = xf86InterpretEDID(output->scrn->scrnIndex,
@@ -1706,7 +1716,6 @@ drmmode_output_init(ScrnInfoPtr pScrn, drmmode_ptr drmmode, drmModeResPtr mode_r
     drmModeConnectorPtr koutput;
     drmModeEncoderPtr *kencoders = NULL;
     drmmode_output_private_ptr drmmode_output;
-    drmModePropertyPtr props;
     char name[32];
     int i;
     drmModePropertyBlobPtr path_blob = NULL;
@@ -1716,17 +1725,7 @@ drmmode_output_init(ScrnInfoPtr pScrn, drmmode_ptr drmmode, drmModeResPtr mode_r
     if (!koutput)
         return 0;
 
-    for (i = 0; i < koutput->count_props; i++) {
-        props = drmModeGetProperty(drmmode->fd, koutput->props[i]);
-        if (props && (props->flags & DRM_MODE_PROP_BLOB)) {
-            if (!strcmp(props->name, "PATH")) {
-                path_blob = drmModeGetPropertyBlob(drmmode->fd, koutput->prop_values[i]);
-                drmModeFreeProperty(props);
-                break;
-            }
-            drmModeFreeProperty(props);
-        }
-    }
+    path_blob = koutput_get_prop_blob(drmmode->fd, koutput, "PATH");
 
     drmmode_create_name(pScrn, koutput, name, path_blob);
 
@@ -1803,17 +1802,8 @@ drmmode_output_init(ScrnInfoPtr pScrn, drmmode_ptr drmmode, drmModeResPtr mode_r
     /* work out the possible clones later */
     output->possible_clones = 0;
 
-    for (i = 0; i < koutput->count_props; i++) {
-        props = drmModeGetProperty(drmmode->fd, koutput->props[i]);
-        if (props && (props->flags & DRM_MODE_PROP_ENUM)) {
-            if (!strcmp(props->name, "DPMS")) {
-                drmmode_output->dpms_enum_id = koutput->props[i];
-                drmModeFreeProperty(props);
-                break;
-            }
-            drmModeFreeProperty(props);
-        }
-    }
+    drmmode_output->dpms_enum_id =
+        koutput_get_prop_id(drmmode->fd, koutput, DRM_MODE_PROP_ENUM, "DPMS");
 
     if (dynamic)
         output->randr_output = RROutputCreate(xf86ScrnToScreen(pScrn), output->name, strlen(output->name), output);
@@ -2273,7 +2263,7 @@ drmmode_handle_uevents(int fd, void *closure)
         xf86OutputPtr output = config->output[i];
         xf86CrtcPtr crtc = output->crtc;
         drmmode_output_private_ptr drmmode_output = output->driver_private;
-        uint32_t con_id;
+        uint32_t con_id, idx;
         drmModeConnectorPtr koutput;
 
         if (crtc == NULL || drmmode_output->mode_output == NULL)
@@ -2284,23 +2274,24 @@ drmmode_handle_uevents(int fd, void *closure)
          * look for the link-status property
          */
         koutput = drmModeGetConnectorCurrent(drmmode->fd, con_id);
-        for (j = 0; koutput && j < koutput->count_props; j++) {
-            drmModePropertyPtr props;
-            props = drmModeGetProperty(drmmode->fd, koutput->props[j]);
-            if (props && props->flags & DRM_MODE_PROP_ENUM &&
-                !strcmp(props->name, "link-status") &&
-                koutput->prop_values[j] == DRM_MODE_LINK_STATUS_BAD) {
-                /* the connector got a link failure, re-set the current mode */
-                drmmode_set_mode_major(crtc, &crtc->mode, crtc->rotation,
-                                       crtc->x, crtc->y);
+        if (!koutput)
+            continue;
 
-                xf86DrvMsg(scrn->scrnIndex, X_WARNING,
-                           "hotplug event: connector %u's link-state is BAD, "
-                           "tried resetting the current mode. You may be left"
-                           "with a black screen if this fails...\n", con_id);
-            }
-            drmModeFreeProperty(props);
+        idx = koutput_get_prop_idx(drmmode->fd, koutput,
+                DRM_MODE_PROP_ENUM, "link-status");
+
+        if ((idx > -1) &&
+                (koutput->prop_values[idx] == DRM_MODE_LINK_STATUS_BAD)) {
+            /* the connector got a link failure, re-set the current mode */
+            drmmode_set_mode_major(crtc, &crtc->mode, crtc->rotation,
+                                   crtc->x, crtc->y);
+
+            xf86DrvMsg(scrn->scrnIndex, X_WARNING,
+                       "hotplug event: connector %u's link-state is BAD, "
+                       "tried resetting the current mode. You may be left"
+                       "with a black screen if this fails...\n", con_id);
         }
+
         drmModeFreeConnector(koutput);
     }
 
-- 
2.13.6



More information about the xorg-devel mailing list