Commit 10376eb926c09f2f420a5170540ae91cc5b267ec

Sam Lantinga 2017-10-21T04:20:57

Fixed bug 3901 - Fix vsync-ed pageflips on the KMSDRM video driver Manuel I noticed that, at least on Intel GPU hardware, passing SDL_RENDERER_PRESENTVSYNC would result on a static console instead of the program graphics. That was due to the fact that calling drmModePageFlip() only works if we have previously set up CRTC to one of the GBM buffers with a drmModeSetCrtc() call, so now it's done and things work as expected. The KMSDRM_GLES_SetupCrtc() call is done only one time, only when needed (when egl_swapinterval is not 0: when it's 0, there's no need for it because we flip by calling drmModePageFlip() anyway). The place where KMSDRM_GLES_SetupCrtc() call is done may look strange, but it's right: it needs EGL completely ready because it needs to call eglSwapBuffers() internally to work (see more comments about it in the code).

diff --git a/src/video/kmsdrm/SDL_kmsdrmopengles.c b/src/video/kmsdrm/SDL_kmsdrmopengles.c
index 7063e5f..7ba663f 100644
--- a/src/video/kmsdrm/SDL_kmsdrmopengles.c
+++ b/src/video/kmsdrm/SDL_kmsdrmopengles.c
@@ -42,6 +42,40 @@ KMSDRM_GLES_LoadLibrary(_THIS, const char *path) {
 
 SDL_EGL_CreateContext_impl(KMSDRM)
 
+SDL_bool
+KMSDRM_GLES_SetupCrtc(_THIS, SDL_Window * window) {
+    SDL_WindowData *wdata = ((SDL_WindowData *) window->driverdata);
+    SDL_DisplayData *displaydata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
+    SDL_VideoData *vdata = ((SDL_VideoData *)_this->driverdata);
+    KMSDRM_FBInfo *fb_info;
+
+    if (!(_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, wdata->egl_surface))) {
+	SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "eglSwapBuffers failed on CRTC setup");
+	return SDL_FALSE;
+    }
+
+    wdata->next_bo = KMSDRM_gbm_surface_lock_front_buffer(wdata->gs);
+    if (wdata->next_bo == NULL) {
+	SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not lock GBM surface front buffer on CRTC setup");
+	return SDL_FALSE;
+    }
+
+    fb_info = KMSDRM_FBFromBO(_this, wdata->next_bo);
+    if (fb_info == NULL) {
+	return SDL_FALSE;
+    }
+
+    if(KMSDRM_drmModeSetCrtc(vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id,
+			    0, 0, &vdata->saved_conn_id, 1, &displaydata->cur_mode) != 0) {
+       SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Could not set up CRTC to a GBM buffer");
+       return SDL_FALSE;
+
+    }
+    
+    wdata->crtc_ready = SDL_TRUE;
+    return SDL_TRUE;
+}
+
 int KMSDRM_GLES_SetSwapInterval(_THIS, int interval) {
     if (!_this->egl_data) {
         return SDL_SetError("EGL not initialized");
@@ -118,6 +152,16 @@ KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window) {
         }
     } else {
         /* Queue page flip at vsync */
+
+	/* Have we already setup the CRTC to one of the GBM buffers? Do so if we have not,
+           or FlipPage won't work in some cases. */
+	if (!wdata->crtc_ready) {
+            if(!KMSDRM_GLES_SetupCrtc(_this, window)) {
+                SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not set up CRTC for doing vsync-ed pageflips");
+                return 0;
+            } 
+	}
+
         /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "drmModePageFlip(%d, %u, %u, DRM_MODE_PAGE_FLIP_EVENT, &wdata->waiting_for_flip)",
             vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id); */
         ret = KMSDRM_drmModePageFlip(vdata->drm_fd, displaydata->crtc_id, fb_info->fb_id,
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index 63376f2..ee3ac05 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -522,6 +522,12 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window)
     }
 #endif /* SDL_VIDEO_OPENGL_EGL */
 
+    /* Window is created, but we have yet to set up CRTC to one of the GBM buffers if we want
+       drmModePageFlip to work, and we can't do it until EGL is completely setup, because we
+       need to do eglSwapBuffers so we can get a valid GBM buffer object to call 
+       drmModeSetCrtc on it. */
+    wdata->crtc_ready = SDL_FALSE;    
+
     /* Setup driver data for this window */
     window->driverdata = wdata;
 
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.h b/src/video/kmsdrm/SDL_kmsdrmvideo.h
index 307c475..71f0de7 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.h
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.h
@@ -62,6 +62,7 @@ typedef struct SDL_WindowData
     struct gbm_bo *current_bo;
     struct gbm_bo *next_bo;
     SDL_bool waiting_for_flip;
+    SDL_bool crtc_ready;
 #if SDL_VIDEO_OPENGL_EGL
     EGLSurface egl_surface;
 #endif