kmscube.c: init_drm(): select the current CRTC display mode is valid
authorEric Ruei <e-ruei1@ti.com>
Thu, 10 Dec 2015 17:41:15 +0000 (12:41 -0500)
committerKarthik Ramanan <a0393906@ti.com>
Thu, 16 Mar 2017 11:33:00 +0000 (17:03 +0530)
Signed-off-by: Eric Ruei <e-ruei1@ti.com>
kmscube.c

index 919b3e7e90f3a37fc251cf0d1a24100c3133f4e5..6bfa86a0f1d7b6cd30a7f10ada3f414814b398af 100644 (file)
--- a/kmscube.c
+++ b/kmscube.c
@@ -89,6 +89,8 @@ static int init_drm(void)
        drmModeRes *resources;
        drmModeConnector *connector = NULL;
        drmModeEncoder *encoder = NULL;
+       drmModeCrtc *crtc = NULL;
+
        int i, j;
        uint32_t maxRes, curRes;
 
@@ -119,10 +121,8 @@ static int init_drm(void)
        for (i = 0; i < resources->count_connectors; i++) {
                connector = drmModeGetConnector(drm.fd, resources->connectors[i]);
                if (connector->connection == DRM_MODE_CONNECTED) {
-                       /* choose the first supported mode */
-                       drm.mode[drm.ndisp] = &connector->modes[0];
-                       drm.connector_id[drm.ndisp] = connector->connector_id;
 
+                       /* find the matched encoders */
                        for (j=0; j<resources->count_encoders; j++) {
                                encoder = drmModeGetEncoder(drm.fd, resources->encoders[j]);
                                if (encoder->encoder_id == connector->encoder_id)
@@ -137,6 +137,35 @@ static int init_drm(void)
                                return -1;
                        }
 
+                       /* choose the current or first supported mode */
+                       crtc = drmModeGetCrtc(drm.fd, encoder->crtc_id);
+                       for (j = 0; j < connector->count_modes; j++)
+                       {
+                               if (crtc->mode_valid)
+                               {
+                                       if ((connector->modes[j].hdisplay == crtc->width) &&
+                                       (connector->modes[j].vdisplay == crtc->height))
+                                       {
+                                               drm.mode[drm.ndisp] = &connector->modes[j];
+                                               break;
+                                       }
+                               }
+                               else
+                               {
+                                       if ((connector->modes[j].hdisplay == crtc->x) &&
+                                          (connector->modes[j].vdisplay == crtc->y))
+                                       {
+                                               drm.mode[drm.ndisp] = &connector->modes[j];
+                                               break;
+                                       }
+                               }
+                       }
+
+                       if(j >= connector->count_modes)
+                               drm.mode[drm.ndisp] = &connector->modes[0];
+
+                       drm.connector_id[drm.ndisp] = connector->connector_id;
+
                        drm.encoder[drm.ndisp]  = (uint32_t) encoder;
                        drm.crtc_id[drm.ndisp] = encoder->crtc_id;
                        drm.connectors[drm.ndisp] = connector;