SDL_kmsdrmopengles.c (16812B)
1 /* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org> 4 Atomic KMSDRM backend by Manuel Alfayate Corchete <redwindwanderer@gmail.com> 5 6 This software is provided 'as-is', without any express or implied 7 warranty. In no event will the authors be held liable for any damages 8 arising from the use of this software. 9 10 Permission is granted to anyone to use this software for any purpose, 11 including commercial applications, and to alter it and redistribute it 12 freely, subject to the following restrictions: 13 14 1. The origin of this software must not be misrepresented; you must not 15 claim that you wrote the original software. If you use this software 16 in a product, an acknowledgment in the product documentation would be 17 appreciated but is not required. 18 2. Altered source versions must be plainly marked as such, and must not be 19 misrepresented as being the original software. 20 3. This notice may not be removed or altered from any source distribution. 21 */ 22 23 #include "../../SDL_internal.h" 24 25 #if SDL_VIDEO_DRIVER_KMSDRM && SDL_VIDEO_OPENGL_EGL 26 27 #include "SDL_kmsdrmvideo.h" 28 #include "SDL_kmsdrmopengles.h" 29 #include "SDL_kmsdrmdyn.h" 30 #include "SDL_hints.h" 31 32 #ifndef EGL_PLATFORM_GBM_MESA 33 #define EGL_PLATFORM_GBM_MESA 0x31D7 34 #endif 35 36 #ifndef EGL_SYNC_NATIVE_FENCE_ANDROID 37 #define EGL_SYNC_NATIVE_FENCE_ANDROID 0x3144 38 #endif 39 40 #ifndef EGL_SYNC_NATIVE_FENCE_FD_ANDROID 41 #define EGL_SYNC_NATIVE_FENCE_FD_ANDROID 0x3145 42 #endif 43 44 #ifndef EGL_NO_NATIVE_FENCE_FD_ANDROID 45 #define EGL_NO_NATIVE_FENCE_FD_ANDROID -1 46 #endif 47 48 /* EGL implementation of SDL OpenGL support */ 49 50 void 51 KMSDRM_GLES_DefaultProfileConfig(_THIS, int *mask, int *major, int *minor) 52 { 53 /* if SDL was _also_ built with the Raspberry Pi driver (so we're 54 definitely a Pi device), default to GLES2. */ 55 #if SDL_VIDEO_DRIVER_RPI 56 *mask = SDL_GL_CONTEXT_PROFILE_ES; 57 *major = 2; 58 *minor = 0; 59 #endif 60 } 61 62 int 63 KMSDRM_GLES_LoadLibrary(_THIS, const char *path) { 64 /* Just pretend you do this here, but don't do it until KMSDRM_CreateWindow(), 65 where we do the same library load we would normally do here. 66 because this gets called by SDL_CreateWindow() before KMSDR_CreateWindow(), 67 so gbm dev isn't yet created when this is called, AND we can't alter the 68 call order in SDL_CreateWindow(). */ 69 #if 0 70 NativeDisplayType display = (NativeDisplayType)((SDL_VideoData *)_this->driverdata)->gbm_dev; 71 return SDL_EGL_LoadLibrary(_this, path, display, EGL_PLATFORM_GBM_MESA); 72 #endif 73 return 0; 74 } 75 76 void 77 KMSDRM_GLES_UnloadLibrary(_THIS) { 78 /* As with KMSDRM_GLES_LoadLibrary(), we define our own unloading function so 79 we manually unload the library whenever we want. */ 80 } 81 82 SDL_EGL_CreateContext_impl(KMSDRM) 83 84 int KMSDRM_GLES_SetSwapInterval(_THIS, int interval) { 85 if (!_this->egl_data) { 86 return SDL_SetError("EGL not initialized"); 87 } 88 89 if (interval == 0 || interval == 1) { 90 _this->egl_data->egl_swapinterval = interval; 91 } else { 92 return SDL_SetError("Only swap intervals of 0 or 1 are supported"); 93 } 94 95 return 0; 96 } 97 98 /*********************************/ 99 /* Atomic functions block */ 100 /*********************************/ 101 102 #define VOID2U64(x) ((uint64_t)(unsigned long)(x)) 103 104 static EGLSyncKHR create_fence(int fd, _THIS) 105 { 106 EGLint attrib_list[] = { 107 EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fd, 108 EGL_NONE, 109 }; 110 111 EGLSyncKHR fence = _this->egl_data->eglCreateSyncKHR 112 (_this->egl_data->egl_display, EGL_SYNC_NATIVE_FENCE_ANDROID, attrib_list); 113 114 assert(fence); 115 return fence; 116 } 117 118 /***********************************************************************************/ 119 /* Comments about buffer access protection mechanism (=fences) are the ones boxed. */ 120 /* Also, DON'T remove the asserts: if a fence-related call fails, it's better that */ 121 /* program exits immediately, or we could leave KMS waiting for a failed/missing */ 122 /* fence forevever. */ 123 /***********************************************************************************/ 124 int 125 KMSDRM_GLES_SwapWindowFenced(_THIS, SDL_Window * window) 126 { 127 SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata); 128 SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata; 129 KMSDRM_FBInfo *fb; 130 KMSDRM_PlaneInfo info = {0}; 131 132 /******************************************************************/ 133 /* Create the GPU-side FENCE OBJECT. It will be inserted into the */ 134 /* GL CMDSTREAM exactly at the end of the gl commands that form a */ 135 /* frame.(KMS will have to wait on it before doing a pageflip.) */ 136 /******************************************************************/ 137 dispdata->gpu_fence = create_fence(EGL_NO_NATIVE_FENCE_FD_ANDROID, _this); 138 assert(dispdata->gpu_fence); 139 140 /******************************************************************/ 141 /* eglSwapBuffers flushes the fence down the GL CMDSTREAM, so we */ 142 /* know for sure it's there now. */ 143 /* Also it marks, at EGL level, the buffer that we want to become */ 144 /* the new front buffer. (Remember that won't really happen until */ 145 /* we request a pageflip at the KMS level and it completes. */ 146 /******************************************************************/ 147 if (! _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, windata->egl_surface)) { 148 return SDL_EGL_SetError("Failed to swap EGL buffers", "eglSwapBuffers"); 149 } 150 151 /******************************************************************/ 152 /* EXPORT the GPU-side FENCE OBJECT to the fence INPUT FD, so we */ 153 /* can pass it into the kernel. Atomic ioctl will pass the */ 154 /* in-fence fd into the kernel, thus telling KMS that it has to */ 155 /* wait for GPU to finish rendering the frame (remember where we */ 156 /* put the fence in the GL CMDSTREAM) before doing the changes */ 157 /* requested in the atomic ioct (the pageflip in this case). */ 158 /* (We export the GPU-side FENCE OJECT to the fence INPUT FD now, */ 159 /* not sooner, because now we are sure that the GPU-side fence is */ 160 /* in the CMDSTREAM to be lifted when the CMDSTREAM to this point */ 161 /* is completed). */ 162 /******************************************************************/ 163 dispdata->kms_in_fence_fd = _this->egl_data->eglDupNativeFenceFDANDROID (_this->egl_data->egl_display, 164 dispdata->gpu_fence); 165 166 _this->egl_data->eglDestroySyncKHR(_this->egl_data->egl_display, dispdata->gpu_fence); 167 assert(dispdata->kms_in_fence_fd != -1); 168 169 /* Lock the buffer that is marked by eglSwapBuffers() to become the 170 next front buffer (so it can not be chosen by EGL as back buffer 171 to draw on), and get a handle to it to request the pageflip on it. 172 REMEMBER that gbm_surface_lock_front_buffer() ALWAYS has to be 173 called after eglSwapBuffers(). */ 174 windata->next_bo = KMSDRM_gbm_surface_lock_front_buffer(windata->gs); 175 if (!windata->next_bo) { 176 return SDL_SetError("Failed to lock frontbuffer"); 177 } 178 fb = KMSDRM_FBFromBO(_this, windata->next_bo); 179 if (!fb) { 180 return SDL_SetError("Failed to get a new framebuffer from BO"); 181 } 182 183 /* Add the pageflip to the request list. */ 184 info.plane = dispdata->display_plane; 185 info.crtc_id = dispdata->crtc->crtc->crtc_id; 186 info.fb_id = fb->fb_id; 187 188 info.src_w = windata->src_w; 189 info.src_h = windata->src_h; 190 info.crtc_w = windata->output_w; 191 info.crtc_h = windata->output_h; 192 info.crtc_x = windata->output_x; 193 194 drm_atomic_set_plane_props(&info); 195 196 /*****************************************************************/ 197 /* Tell the display (KMS) that it will have to wait on the fence */ 198 /* for the GPU-side FENCE. */ 199 /* */ 200 /* Since KMS is a kernel thing, we have to pass an FD into */ 201 /* the kernel, and get another FD out of the kernel. */ 202 /* */ 203 /* 1) To pass the GPU-side fence into the kernel, we set the */ 204 /* INPUT FD as the IN_FENCE_FD prop of the PRIMARY PLANE. */ 205 /* This FD tells KMS (the kernel) to wait for the GPU-side fence.*/ 206 /* */ 207 /* 2) To get the KMS-side fence out of the kernel, we set the */ 208 /* OUTPUT FD as the OUT_FEWNCE_FD prop of the CRTC. */ 209 /* This FD will be later imported as a FENCE OBJECT which will be*/ 210 /* used to tell the GPU to wait for KMS to complete the changes */ 211 /* requested in atomic_commit (the pageflip in this case). */ 212 /*****************************************************************/ 213 if (dispdata->kms_in_fence_fd != -1) 214 { 215 add_plane_property(dispdata->atomic_req, dispdata->display_plane, 216 "IN_FENCE_FD", dispdata->kms_in_fence_fd); 217 add_crtc_property(dispdata->atomic_req, dispdata->crtc, 218 "OUT_FENCE_PTR", VOID2U64(&dispdata->kms_out_fence_fd)); 219 } 220 221 /* Do we have a pending modesetting? If so, set the necessary 222 props so it's included in the incoming atomic commit. */ 223 if (dispdata->modeset_pending) { 224 uint32_t blob_id; 225 SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata; 226 227 dispdata->atomic_flags |= DRM_MODE_ATOMIC_ALLOW_MODESET; 228 add_connector_property(dispdata->atomic_req, dispdata->connector, "CRTC_ID", dispdata->crtc->crtc->crtc_id); 229 KMSDRM_drmModeCreatePropertyBlob(viddata->drm_fd, &dispdata->mode, sizeof(dispdata->mode), &blob_id); 230 add_crtc_property(dispdata->atomic_req, dispdata->crtc, "MODE_ID", blob_id); 231 add_crtc_property(dispdata->atomic_req, dispdata->crtc, "ACTIVE", 1); 232 dispdata->modeset_pending = SDL_FALSE; 233 } 234 235 /*****************************************************************/ 236 /* Issue a non-blocking atomic commit: for triple buffering, */ 237 /* this must not block so the game can start building another */ 238 /* frame, even if the just-requested pageflip hasnt't completed. */ 239 /*****************************************************************/ 240 if (drm_atomic_commit(_this, SDL_FALSE)) { 241 return SDL_SetError("Failed to issue atomic commit on pageflip"); 242 } 243 244 /* Release the previous front buffer so EGL can chose it as back buffer 245 and render on it again. */ 246 if (windata->bo) { 247 KMSDRM_gbm_surface_release_buffer(windata->gs, windata->bo); 248 } 249 250 /* Take note of the buffer about to become front buffer, so next 251 time we come here we can free it like we just did with the previous 252 front buffer. */ 253 windata->bo = windata->next_bo; 254 255 /****************************************************************/ 256 /* Import the KMS-side FENCE OUTPUT FD from the kernel to the */ 257 /* KMS-side FENCE OBJECT so we can use use it to fence the GPU. */ 258 /****************************************************************/ 259 dispdata->kms_fence = create_fence(dispdata->kms_out_fence_fd, _this); 260 assert(dispdata->kms_fence); 261 262 /****************************************************************/ 263 /* "Delete" the fence OUTPUT FD, because we already have the */ 264 /* KMS FENCE OBJECT, the fence itself is away from us, on the */ 265 /* kernel side. */ 266 /****************************************************************/ 267 dispdata->kms_out_fence_fd = -1; 268 269 /*****************************************************************/ 270 /* Tell the GPU to wait on the fence for the KMS-side FENCE, */ 271 /* which means waiting until the requested pageflip is completed.*/ 272 /*****************************************************************/ 273 _this->egl_data->eglWaitSyncKHR(_this->egl_data->egl_display, dispdata->kms_fence, 0); 274 275 return 0; 276 } 277 278 int 279 KMSDRM_GLES_SwapWindowDoubleBuffered(_THIS, SDL_Window * window) 280 { 281 SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata); 282 SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata; 283 KMSDRM_FBInfo *fb; 284 KMSDRM_PlaneInfo info = {0}; 285 286 /****************************************************************************************************/ 287 /* In double-buffer mode, atomic_commit will always be synchronous/blocking (ie: won't return until */ 288 /* the requested changes are really done). */ 289 /* Also, there's no need to fence KMS or the GPU, because we won't be entering game loop again */ 290 /* (hence not building or executing a new cmdstring) until pageflip is done, so we don't need to */ 291 /* protect the KMS/GPU access to the buffer. */ 292 /****************************************************************************************************/ 293 294 /* Mark, at EGL level, the buffer that we want to become the new front buffer. 295 However, it won't really happen until we request a pageflip at the KMS level and it completes. */ 296 if (! _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, windata->egl_surface)) { 297 return SDL_EGL_SetError("Failed to swap EGL buffers", "eglSwapBuffers"); 298 } 299 300 /* Lock the buffer that is marked by eglSwapBuffers() to become the next front buffer (so it can not 301 be chosen by EGL as back buffer to draw on), and get a handle to it to request the pageflip on it. */ 302 windata->next_bo = KMSDRM_gbm_surface_lock_front_buffer(windata->gs); 303 if (!windata->next_bo) { 304 return SDL_SetError("Failed to lock frontbuffer"); 305 } 306 fb = KMSDRM_FBFromBO(_this, windata->next_bo); 307 if (!fb) { 308 return SDL_SetError("Failed to get a new framebuffer BO"); 309 } 310 311 /* Add the pageflip to the request list. */ 312 info.plane = dispdata->display_plane; 313 info.crtc_id = dispdata->crtc->crtc->crtc_id; 314 info.fb_id = fb->fb_id; 315 316 info.src_w = windata->src_w; 317 info.src_h = windata->src_h; 318 info.crtc_w = windata->output_w; 319 info.crtc_h = windata->output_h; 320 info.crtc_x = windata->output_x; 321 322 drm_atomic_set_plane_props(&info); 323 324 /* Do we have a pending modesetting? If so, set the necessary 325 props so it's included in the incoming atomic commit. */ 326 if (dispdata->modeset_pending) { 327 SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata; 328 uint32_t blob_id; 329 dispdata->atomic_flags |= DRM_MODE_ATOMIC_ALLOW_MODESET; 330 add_connector_property(dispdata->atomic_req, dispdata->connector, "CRTC_ID", dispdata->crtc->crtc->crtc_id); 331 KMSDRM_drmModeCreatePropertyBlob(viddata->drm_fd, &dispdata->mode, sizeof(dispdata->mode), &blob_id); 332 add_crtc_property(dispdata->atomic_req, dispdata->crtc, "MODE_ID", blob_id); 333 add_crtc_property(dispdata->atomic_req, dispdata->crtc, "ACTIVE", 1); 334 dispdata->modeset_pending = SDL_FALSE; 335 } 336 337 /* Issue the one and only atomic commit where all changes will be requested!. 338 Blocking for double buffering: won't return until completed. */ 339 if (drm_atomic_commit(_this, SDL_TRUE)) { 340 return SDL_SetError("Failed to issue atomic commit"); 341 } 342 343 /* Release last front buffer so EGL can chose it as back buffer and render on it again. */ 344 if (windata->bo) { 345 KMSDRM_gbm_surface_release_buffer(windata->gs, windata->bo); 346 } 347 348 /* Take note of current front buffer, so we can free it next time we come here. */ 349 windata->bo = windata->next_bo; 350 351 return 0; 352 } 353 354 int 355 KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window) 356 { 357 SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata); 358 359 if (windata->swap_window == NULL) { 360 /* We want the fenced version by default, but it needs extensions. */ 361 if ( (SDL_GetHintBoolean(SDL_HINT_VIDEO_DOUBLE_BUFFER, SDL_FALSE)) || 362 (!SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_ANDROID_native_fence_sync")) ) 363 { 364 windata->swap_window = KMSDRM_GLES_SwapWindowDoubleBuffered; 365 } else { 366 windata->swap_window = KMSDRM_GLES_SwapWindowFenced; 367 } 368 } 369 370 return windata->swap_window(_this, window); 371 } 372 373 /***************************************/ 374 /* End of Atomic functions block */ 375 /***************************************/ 376 377 SDL_EGL_MakeCurrent_impl(KMSDRM) 378 379 #endif /* SDL_VIDEO_DRIVER_KMSDRM && SDL_VIDEO_OPENGL_EGL */ 380 381 /* vi: set ts=4 sw=4 expandtab: */