kmsdrm: uninitialized KMSDRM fixes
authorSergey Zhuravlevich <zhurxx@gmail.com>
Sat, 01 Dec 2018 13:09:00 -0500
changeset 124402b450a35f471
parent 12439 3a0793e5e134
child 12441 2e2cff8a14b6
kmsdrm: uninitialized KMSDRM fixes

* Search for valid encoder in connector when it doesn't have an
encoder ID set.

* Search for valid CRTC in resources and encoder's possible CRTCs
when encoder doesn't have one set.

* Select default mode if CRTC doesn't have a valid one.

* Pick current_mode's W/H/Refresh Rate basing on current and
valid CRTC mode, not the saved one.
src/video/kmsdrm/SDL_kmsdrmvideo.c
     1.1 --- a/src/video/kmsdrm/SDL_kmsdrmvideo.c	Sat Dec 01 12:19:11 2018 -0500
     1.2 +++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c	Sat Dec 01 13:09:00 2018 -0500
     1.3 @@ -333,7 +333,8 @@
     1.4  int
     1.5  KMSDRM_VideoInit(_THIS)
     1.6  {
     1.7 -    int i;
     1.8 +    int i, j;
     1.9 +    SDL_bool found;
    1.10      int ret = 0;
    1.11      char *devname;
    1.12      SDL_VideoData *vdata = ((SDL_VideoData *)_this->driverdata);
    1.13 @@ -402,6 +403,8 @@
    1.14          goto cleanup;
    1.15      }
    1.16  
    1.17 +    found = SDL_FALSE;
    1.18 +
    1.19      for (i = 0; i < resources->count_encoders; i++) {
    1.20          encoder = KMSDRM_drmModeGetEncoder(vdata->drm_fd, resources->encoders[i]);
    1.21  
    1.22 @@ -409,8 +412,20 @@
    1.23              continue;
    1.24  
    1.25          if (encoder->encoder_id == connector->encoder_id) {
    1.26 -            SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", encoder->encoder_id);
    1.27              data->encoder_id = encoder->encoder_id;
    1.28 +            found = SDL_TRUE;
    1.29 +        } else {
    1.30 +            for (j = 0; j < connector->count_encoders; j++) {
    1.31 +                if (connector->encoders[j] == encoder->encoder_id) {
    1.32 +                    data->encoder_id = encoder->encoder_id;
    1.33 +                    found = SDL_TRUE;
    1.34 +                    break;
    1.35 +                }
    1.36 +            }
    1.37 +        }
    1.38 +
    1.39 +        if (found == SDL_TRUE) {
    1.40 +            SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Found encoder %d.", data->encoder_id);
    1.41              break;
    1.42          }
    1.43  
    1.44 @@ -424,6 +439,18 @@
    1.45      }
    1.46  
    1.47      vdata->saved_crtc = KMSDRM_drmModeGetCrtc(vdata->drm_fd, encoder->crtc_id);
    1.48 +
    1.49 +    if (vdata->saved_crtc == NULL) {
    1.50 +        for (i = 0; i < resources->count_crtcs; i++) {
    1.51 +            if (encoder->possible_crtcs & (1 << i)) {
    1.52 +                encoder->crtc_id = resources->crtcs[i];
    1.53 +                SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "Set encoder's CRTC to %d.", encoder->crtc_id);
    1.54 +                vdata->saved_crtc = KMSDRM_drmModeGetCrtc(vdata->drm_fd, encoder->crtc_id);
    1.55 +                break;
    1.56 +            }
    1.57 +        }
    1.58 +    }
    1.59 +
    1.60      if (vdata->saved_crtc == NULL) {
    1.61          ret = SDL_SetError("No CRTC found.");
    1.62          goto cleanup;
    1.63 @@ -435,11 +462,18 @@
    1.64      data->cur_mode = vdata->saved_crtc->mode;
    1.65      vdata->crtc_id = encoder->crtc_id;
    1.66  
    1.67 +    // select default mode if this one is not valid
    1.68 +    if (vdata->saved_crtc->mode_valid == 0) {
    1.69 +        SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO,
    1.70 +            "Current mode is invalid, selecting connector's mode #0.");
    1.71 +        data->cur_mode = connector->modes[0];
    1.72 +    }
    1.73 +
    1.74      SDL_zero(current_mode);
    1.75  
    1.76 -    current_mode.w = vdata->saved_crtc->mode.hdisplay;
    1.77 -    current_mode.h = vdata->saved_crtc->mode.vdisplay;
    1.78 -    current_mode.refresh_rate = vdata->saved_crtc->mode.vrefresh;
    1.79 +    current_mode.w = data->cur_mode.hdisplay;
    1.80 +    current_mode.h = data->cur_mode.vdisplay;
    1.81 +    current_mode.refresh_rate = data->cur_mode.vrefresh;
    1.82  
    1.83      /* FIXME ?
    1.84      drmModeFB *fb = drmModeGetFB(vdata->drm_fd, vdata->saved_crtc->buffer_id);