kmsdrm: fix first frame display: no need to wait for SwapWindow() for EGL surface creation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
diff --git a/src/video/kmsdrm/SDL_kmsdrmopengles.c b/src/video/kmsdrm/SDL_kmsdrmopengles.c
index e9c55c9..31204f0 100644
--- a/src/video/kmsdrm/SDL_kmsdrmopengles.c
+++ b/src/video/kmsdrm/SDL_kmsdrmopengles.c
@@ -82,12 +82,16 @@ KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window)
KMSDRM_FBInfo *fb;
KMSDRM_PlaneInfo info = {0};
- /* Recreate the GBM / EGL surfaces if the window has been reconfigured. */
- if (windata->egl_surface_dirty) {
- if (KMSDRM_CreateSurfaces(_this, window)) {
- return SDL_SetError("Failed to do pending surfaces creation");
- }
+ /* Get the EGL context, now that SDL_CreateRenderer() has already been called,
+ and call eglMakeCurrent() on it and the EGL surface. */
+#if SDL_VIDEO_OPENGL_EGL
+ if (windata->egl_context_pending) {
+ EGLContext egl_context;
+ egl_context = (EGLContext)SDL_GL_GetCurrentContext();
+ SDL_EGL_MakeCurrent(_this, windata->egl_surface, egl_context);
+ windata->egl_context_pending = SDL_FALSE;
}
+#endif
/*************************************************************************/
/* Block for telling KMS to wait for GPU rendering of the current frame */
@@ -210,12 +214,16 @@ KMSDRM_GLES_SwapWindowDB(_THIS, SDL_Window * window)
KMSDRM_FBInfo *fb;
KMSDRM_PlaneInfo info = {0};
- /* Recreate the GBM / EGL surfaces if the window has been reconfigured. */
- if (windata->egl_surface_dirty) {
- if (KMSDRM_CreateSurfaces(_this, window)) {
- return SDL_SetError("Failed to do pending surfaces creation");
- }
+ /* Get the EGL context, now that SDL_CreateRenderer() has already been called,
+ and call eglMakeCurrent() on it and the EGL surface. */
+#if SDL_VIDEO_OPENGL_EGL
+ if (windata->egl_context_pending) {
+ EGLContext egl_context;
+ egl_context = (EGLContext)SDL_GL_GetCurrentContext();
+ SDL_EGL_MakeCurrent(_this, windata->egl_surface, egl_context);
+ windata->egl_context_pending = SDL_FALSE;
}
+#endif
/****************************************************************************************************/
/* In double-buffer mode, atomic commit will always be synchronous/blocking (ie: won't return until */
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index 7d9245f..d441290 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -760,6 +760,71 @@ void
KMSDRM_DestroySurfaces(_THIS, SDL_Window *window)
{
SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
+ SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
+ KMSDRM_PlaneInfo plane_info = {0};
+
+#if SDL_VIDEO_OPENGL_EGL
+ EGLContext egl_context;
+#endif
+
+ /********************************************************************/
+ /* BLOCK 1: protect the PRIMARY PLANE before destroying the buffers */
+ /* it's using. */
+ /********************************************************************/
+
+#if AMDGPU_COMPAT
+ /************************************************************************/
+ /* We can't do the usual CRTC_ID+FB_ID to 0 with AMDGPU, because */
+ /* the driver never recovers from the CONNECTOR and CRTC disconnection.*/
+ /* The with this solution is that crtc->buffer_id is not guaranteed */
+ /* to be there... */
+ /************************************************************************/
+ plane_info.plane = dispdata->display_plane;
+ plane_info.crtc_id = dispdata->crtc->crtc->crtc_id;
+ plane_info.fb_id = dispdata->crtc->crtc->buffer_id;
+ plane_info.src_w = dispdata->mode.hdisplay;
+ plane_info.src_h = dispdata->mode.vdisplay;
+ plane_info.crtc_w = dispdata->mode.hdisplay;
+ plane_info.crtc_h = dispdata->mode.vdisplay;
+
+ drm_atomic_set_plane_props(&plane_info);
+
+ /* Issue blocking atomic commit. */
+ if (drm_atomic_commit(_this, SDL_TRUE)) {
+ SDL_SetError("Failed to issue atomic commit on DestroyWindow().");
+ }
+#else
+ /*********************************************************************************/
+ /* Disconnect the CONNECTOR from the CRTC (several connectors can read a CRTC), */
+ /* deactivate the CRTC, and set the PRIMARY PLANE props CRTC_ID and FB_ID to 0. */
+ /* We have to do this before setting the PLANE CRTC_ID and FB_ID to 0 because */
+ /* there can be no active CRTC without an active PLANE. */
+ /* We can leave all like this if we are exiting the program after the window */
+ /* destruction, or things will be re-connected again on SwapWindow(), if needed. */
+ /*********************************************************************************/
+ if (add_connector_property(dispdata->atomic_req, dispdata->connector , "CRTC_ID", 0) < 0)
+ SDL_SetError("Failed to set CONNECTOR prop CRTC_ID to zero before buffer destruction");
+
+ if (add_crtc_property(dispdata->atomic_req, dispdata->crtc , "ACTIVE", 0) < 0)
+ SDL_SetError("Failed to set CRTC prop ACTIVE to zero before buffer destruction");
+
+ /* Since we initialize plane_info to all zeros, ALL PRIMARY PLANE props are set to 0 with this,
+ including FB_ID and CRTC_ID. Not all drivers like FB_ID and CRTC_ID to 0 yet. */
+ plane_info.plane = dispdata->display_plane;
+
+ drm_atomic_set_plane_props(&plane_info);
+
+ /* Issue blocking atomic commit. */
+ if (drm_atomic_commit(_this, SDL_TRUE)) {
+ SDL_SetError("Failed to issue atomic commit on window surfaces and buffers destruction.");
+ }
+#endif
+
+ /****************************************************************************/
+ /* BLOCK 2: We can finally destroy the window GBM and EGL surfaces, and */
+ /* GBM buffers now that the buffers are not being used by the PRIMARY PLANE */
+ /* anymore. */
+ /****************************************************************************/
/* Destroy the GBM surface and buffers. */
if (windata->bo) {
@@ -774,7 +839,17 @@ KMSDRM_DestroySurfaces(_THIS, SDL_Window *window)
/* Destroy the EGL surface. */
#if SDL_VIDEO_OPENGL_EGL
- SDL_EGL_MakeCurrent(_this, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+ /***************************************************************************/
+ /* In this eglMakeCurrent() call, we disable the current EGL surface */
+ /* because we're going to destroy it, but DON'T disable the EGL context, */
+ /* because it won't be enabled again until the programs ask for a pageflip */
+ /* so we get to SwapWindow(). */
+ /* If we disable the context until then and a program tries to retrieve */
+ /* the context version info before calling for a pageflip, the program */
+ /* will get wrong info and we will be in trouble. */
+ /***************************************************************************/
+ egl_context = (EGLContext)SDL_GL_GetCurrentContext();
+ SDL_EGL_MakeCurrent(_this, EGL_NO_SURFACE, egl_context);
if (windata->egl_surface != EGL_NO_SURFACE) {
SDL_EGL_DestroySurface(_this, windata->egl_surface);
@@ -798,12 +873,6 @@ KMSDRM_CreateSurfaces(_THIS, SDL_Window * window)
uint32_t surface_flags = GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING;
uint32_t width, height;
-#if SDL_VIDEO_OPENGL_EGL
- EGLContext egl_context;
- SDL_EGL_SetRequiredVisualId(_this, surface_fmt);
- egl_context = (EGLContext)SDL_GL_GetCurrentContext();
-#endif
-
/* Destroy the surfaces and buffers before creating the new ones. */
KMSDRM_DestroySurfaces(_this, window);
@@ -827,15 +896,20 @@ KMSDRM_CreateSurfaces(_THIS, SDL_Window * window)
}
#if SDL_VIDEO_OPENGL_EGL
+ /* We can't get the EGL context yet because SDL_CreateRenderer has not been called,
+ but we need an EGL surface NOW, or GL won't be able to render into any surface
+ and we won't see the first frame. */
+ SDL_EGL_SetRequiredVisualId(_this, surface_fmt);
windata->egl_surface = SDL_EGL_CreateSurface(_this, (NativeWindowType)windata->gs);
if (windata->egl_surface == EGL_NO_SURFACE) {
return SDL_SetError("Could not create EGL window surface");
}
- SDL_EGL_MakeCurrent(_this, windata->egl_surface, egl_context);
-
- windata->egl_surface_dirty = 0;
+ /* Take note that we're still missing the EGL contex,
+ so we can get it in SwapWindow, when SDL_CreateRenderer()
+ has already been called. */
+ windata->egl_context_pending = SDL_TRUE;
#endif
return 0;
@@ -845,65 +919,12 @@ void
KMSDRM_DestroyWindow(_THIS, SDL_Window *window)
{
SDL_WindowData *windata = (SDL_WindowData *) window->driverdata;
- SDL_DisplayData *dispdata = (SDL_DisplayData *)SDL_GetDisplayDriverData(0);
- KMSDRM_PlaneInfo plane_info = {0};
-
SDL_VideoData *viddata;
+
if (!windata) {
return;
}
-#if AMDGPU_COMPAT
- /************************************************************************/
- /* We can't do the usual CRTC_ID+FB_ID to 0 with AMDGPU, because */
- /* the driver never recovers from the CONNECTOR and CRTC disconnection.*/
- /* The with this solution is that crtc->buffer_id is not guaranteed */
- /* to be there... */
- /************************************************************************/
- plane_info.plane = dispdata->display_plane;
- plane_info.crtc_id = dispdata->crtc->crtc->crtc_id;
- plane_info.fb_id = dispdata->crtc->crtc->buffer_id;
- plane_info.src_w = dispdata->mode.hdisplay;
- plane_info.src_h = dispdata->mode.vdisplay;
- plane_info.crtc_w = dispdata->mode.hdisplay;
- plane_info.crtc_h = dispdata->mode.vdisplay;
- drm_atomic_set_plane_props(&plane_info);
-
- /* Issue blocking atomic commit. */
- if (drm_atomic_commit(_this, SDL_TRUE)) {
- SDL_SetError("Failed to issue atomic commit on DestroyWindow().");
- }
-#else
- /*********************************************************************************/
- /* Disconnect the CONNECTOR from the CRTC (several connectors can read a CRTC), */
- /* deactivate the CRTC, and set the PRIMARY PLANE props CRTC_ID and FB_ID to 0. */
- /* We have to do this before setting the PLANE CRTC_ID and FB_ID to 0 because */
- /* there can be no active CRTC without an active PLANE. */
- /* We can leave all like this if we are exiting the program after the window */
- /* destruction, or things will be re-connected again on SwapWindow(), if needed. */
- /*********************************************************************************/
- if (add_connector_property(dispdata->atomic_req, dispdata->connector , "CRTC_ID", 0) < 0)
- SDL_SetError("Failed to set CONNECTOR prop CRTC_ID to zero before buffer destruction");
-
- if (add_crtc_property(dispdata->atomic_req, dispdata->crtc , "ACTIVE", 0) < 0)
- SDL_SetError("Failed to set CRTC prop ACTIVE to zero before buffer destruction");
-
- /* Since we initialize plane_info to all zeros, ALL PRIMARY PLANE props are set to 0 with this,
- including FB_ID and CRTC_ID. Not all drivers like FB_ID and CRTC_ID to 0 yet. */
- plane_info.plane = dispdata->display_plane;
-
- drm_atomic_set_plane_props(&plane_info);
-
- /* Issue blocking atomic commit. */
- if (drm_atomic_commit(_this, SDL_TRUE)) {
- SDL_SetError("Failed to issue atomic commit on window surfaces and buffers destruction.");
- }
-#endif
-
- /****************************************************************************/
- /* We can finally destroy the window GBM and EGL surfaces, and GBM buffers, */
- /* now that the buffers are not being used by the PRIMARY PLANE anymore. */
- /****************************************************************************/
KMSDRM_DestroySurfaces(_this, window);
/********************************************/
@@ -957,19 +978,9 @@ KMSDRM_ReconfigureWindow( _THIS, SDL_Window * window) {
windata->output_x = (dispdata->mode.hdisplay - windata->output_w) / 2;
}
-#if SDL_VIDEO_OPENGL_EGL
- /* Can't recreate EGL surfaces right now, need to wait until SwapWindow
- so the EGL context is available. That's because SDL_CreateRenderer(),
- where the EGL context is created, is always called after SDL_CreateWindow()
- since SDL_CreateRenderer() takes a window as parameter.
- On window destruction, SDL_DestroyRenderer() is called before SDL_DestroWindow(),
- so on SDL_DestroyWindow() the EGL context isn't available anymore. */
- windata->egl_surface_dirty = SDL_TRUE;
-#else
if (KMSDRM_CreateSurfaces(_this, window)) {
return -1;
}
-#endif
return 0;
}
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.h b/src/video/kmsdrm/SDL_kmsdrmvideo.h
index bb16237..16bee13 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.h
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.h
@@ -126,7 +126,9 @@ typedef struct SDL_WindowData
int32_t output_h;
int32_t output_x;
- SDL_bool egl_surface_dirty;
+ /* This is for deferred eglMakeCurrent() call: we can't call it until
+ the EGL context is available, but we need the EGL surface sooner. */
+ SDL_bool egl_context_pending;
} SDL_WindowData;
@@ -152,7 +154,7 @@ typedef struct KMSDRM_PlaneInfo
} KMSDRM_PlaneInfo;
/* Helper functions */
-int KMSDRM_CreateSurfaces(_THIS, SDL_Window * window);
+int KMSDRM_CreateEGLSurface(_THIS, SDL_Window * window);
KMSDRM_FBInfo *KMSDRM_FBFromBO(_THIS, struct gbm_bo *bo);
/* Atomic functions that are used from SDL_kmsdrmopengles.c and SDL_kmsdrmmouse.c */