|
@@ -1,7 +1,6 @@
|
|
|
/*
|
|
|
Simple DirectMedia Layer
|
|
|
Copyright (C) 1997-2021 Sam Lantinga <[email protected]>
|
|
|
- Atomic KMSDRM backend by Manuel Alfayate Corchete <[email protected]>
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
@@ -24,27 +23,16 @@
|
|
|
|
|
|
#if SDL_VIDEO_DRIVER_KMSDRM
|
|
|
|
|
|
+#include "SDL_log.h"
|
|
|
+
|
|
|
#include "SDL_kmsdrmvideo.h"
|
|
|
#include "SDL_kmsdrmopengles.h"
|
|
|
#include "SDL_kmsdrmdyn.h"
|
|
|
-#include "SDL_hints.h"
|
|
|
|
|
|
#ifndef EGL_PLATFORM_GBM_MESA
|
|
|
#define EGL_PLATFORM_GBM_MESA 0x31D7
|
|
|
#endif
|
|
|
|
|
|
-#ifndef EGL_SYNC_NATIVE_FENCE_ANDROID
|
|
|
-#define EGL_SYNC_NATIVE_FENCE_ANDROID 0x3144
|
|
|
-#endif
|
|
|
-
|
|
|
-#ifndef EGL_SYNC_NATIVE_FENCE_FD_ANDROID
|
|
|
-#define EGL_SYNC_NATIVE_FENCE_FD_ANDROID 0x3145
|
|
|
-#endif
|
|
|
-
|
|
|
-#ifndef EGL_NO_NATIVE_FENCE_FD_ANDROID
|
|
|
-#define EGL_NO_NATIVE_FENCE_FD_ANDROID -1
|
|
|
-#endif
|
|
|
-
|
|
|
/* EGL implementation of SDL OpenGL support */
|
|
|
|
|
|
void
|
|
@@ -95,291 +83,82 @@ int KMSDRM_GLES_SetSwapInterval(_THIS, int interval) {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-/*********************************/
|
|
|
-/* Atomic functions block */
|
|
|
-/*********************************/
|
|
|
-
|
|
|
-#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
|
|
|
-
|
|
|
-static EGLSyncKHR create_fence(int fd, _THIS)
|
|
|
-{
|
|
|
- EGLint attrib_list[] = {
|
|
|
- EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fd,
|
|
|
- EGL_NONE,
|
|
|
- };
|
|
|
-
|
|
|
- EGLSyncKHR fence = _this->egl_data->eglCreateSyncKHR
|
|
|
- (_this->egl_data->egl_display, EGL_SYNC_NATIVE_FENCE_ANDROID, attrib_list);
|
|
|
-
|
|
|
- assert(fence);
|
|
|
- return fence;
|
|
|
-}
|
|
|
-
|
|
|
-/***********************************************************************************/
|
|
|
-/* Comments about buffer access protection mechanism (=fences) are the ones boxed. */
|
|
|
-/* Also, DON'T remove the asserts: if a fence-related call fails, it's better that */
|
|
|
-/* program exits immediately, or we could leave KMS waiting for a failed/missing */
|
|
|
-/* fence forevever. */
|
|
|
-/***********************************************************************************/
|
|
|
int
|
|
|
-KMSDRM_GLES_SwapWindowFenced(_THIS, SDL_Window * window)
|
|
|
-{
|
|
|
+KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window) {
|
|
|
SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata);
|
|
|
SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
|
|
|
- KMSDRM_FBInfo *fb;
|
|
|
- KMSDRM_PlaneInfo info = {0};
|
|
|
- SDL_bool modesetting = SDL_FALSE;
|
|
|
-
|
|
|
- /******************************************************************/
|
|
|
- /* Create the GPU-side FENCE OBJECT. It will be inserted into the */
|
|
|
- /* GL CMDSTREAM exactly at the end of the gl commands that form a */
|
|
|
- /* frame.(KMS will have to wait on it before doing a pageflip.) */
|
|
|
- /******************************************************************/
|
|
|
- dispdata->gpu_fence = create_fence(EGL_NO_NATIVE_FENCE_FD_ANDROID, _this);
|
|
|
- assert(dispdata->gpu_fence);
|
|
|
+ SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
|
|
|
+ KMSDRM_FBInfo *fb_info;
|
|
|
+ int ret, timeout;
|
|
|
|
|
|
- /******************************************************************/
|
|
|
- /* eglSwapBuffers flushes the fence down the GL CMDSTREAM, so we */
|
|
|
- /* know for sure it's there now. */
|
|
|
- /* Also it marks, at EGL level, the buffer that we want to become */
|
|
|
- /* the new front buffer. (Remember that won't really happen until */
|
|
|
- /* we request a pageflip at the KMS level and it completes. */
|
|
|
- /******************************************************************/
|
|
|
- if (! _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, windata->egl_surface)) {
|
|
|
- return SDL_EGL_SetError("Failed to swap EGL buffers", "eglSwapBuffers");
|
|
|
+ /* Recreate the GBM / EGL surfaces if the display mode has changed */
|
|
|
+ if (windata->egl_surface_dirty) {
|
|
|
+ KMSDRM_CreateSurfaces(_this, window);
|
|
|
}
|
|
|
|
|
|
- /******************************************************************/
|
|
|
- /* EXPORT the GPU-side FENCE OBJECT to the fence INPUT FD, so we */
|
|
|
- /* can pass it into the kernel. Atomic ioctl will pass the */
|
|
|
- /* in-fence fd into the kernel, thus telling KMS that it has to */
|
|
|
- /* wait for GPU to finish rendering the frame (remember where we */
|
|
|
- /* put the fence in the GL CMDSTREAM) before doing the changes */
|
|
|
- /* requested in the atomic ioct (the pageflip in this case). */
|
|
|
- /* (We export the GPU-side FENCE OJECT to the fence INPUT FD now, */
|
|
|
- /* not sooner, because now we are sure that the GPU-side fence is */
|
|
|
- /* in the CMDSTREAM to be lifted when the CMDSTREAM to this point */
|
|
|
- /* is completed). */
|
|
|
- /******************************************************************/
|
|
|
- dispdata->kms_in_fence_fd = _this->egl_data->eglDupNativeFenceFDANDROID (_this->egl_data->egl_display,
|
|
|
- dispdata->gpu_fence);
|
|
|
-
|
|
|
- _this->egl_data->eglDestroySyncKHR(_this->egl_data->egl_display, dispdata->gpu_fence);
|
|
|
- assert(dispdata->kms_in_fence_fd != -1);
|
|
|
-
|
|
|
- /* Lock the buffer that is marked by eglSwapBuffers() to become the
|
|
|
- next front buffer (so it can not be chosen by EGL as back buffer
|
|
|
- to draw on), and get a handle to it to request the pageflip on it.
|
|
|
- REMEMBER that gbm_surface_lock_front_buffer() ALWAYS has to be
|
|
|
- called after eglSwapBuffers(). */
|
|
|
- windata->next_bo = KMSDRM_gbm_surface_lock_front_buffer(windata->gs);
|
|
|
- if (!windata->next_bo) {
|
|
|
- return SDL_SetError("Failed to lock frontbuffer");
|
|
|
- }
|
|
|
- fb = KMSDRM_FBFromBO(_this, windata->next_bo);
|
|
|
- if (!fb) {
|
|
|
- return SDL_SetError("Failed to get a new framebuffer from BO");
|
|
|
+ /* Wait for confirmation that the next front buffer has been flipped, at which
|
|
|
+ point the previous front buffer can be released */
|
|
|
+ timeout = 0;
|
|
|
+ if (_this->egl_data->egl_swapinterval == 1) {
|
|
|
+ timeout = -1;
|
|
|
}
|
|
|
-
|
|
|
- /* Add the pageflip to the request list. */
|
|
|
- info.plane = dispdata->display_plane;
|
|
|
- info.crtc_id = dispdata->crtc->crtc->crtc_id;
|
|
|
- info.fb_id = fb->fb_id;
|
|
|
-
|
|
|
- info.src_w = windata->src_w;
|
|
|
- info.src_h = windata->src_h;
|
|
|
- info.crtc_w = windata->output_w;
|
|
|
- info.crtc_h = windata->output_h;
|
|
|
- info.crtc_x = windata->output_x;
|
|
|
-
|
|
|
- drm_atomic_set_plane_props(&info);
|
|
|
-
|
|
|
- /*****************************************************************/
|
|
|
- /* Tell the display (KMS) that it will have to wait on the fence */
|
|
|
- /* for the GPU-side FENCE. */
|
|
|
- /* */
|
|
|
- /* Since KMS is a kernel thing, we have to pass an FD into */
|
|
|
- /* the kernel, and get another FD out of the kernel. */
|
|
|
- /* */
|
|
|
- /* 1) To pass the GPU-side fence into the kernel, we set the */
|
|
|
- /* INPUT FD as the IN_FENCE_FD prop of the PRIMARY PLANE. */
|
|
|
- /* This FD tells KMS (the kernel) to wait for the GPU-side fence.*/
|
|
|
- /* */
|
|
|
- /* 2) To get the KMS-side fence out of the kernel, we set the */
|
|
|
- /* OUTPUT FD as the OUT_FEWNCE_FD prop of the CRTC. */
|
|
|
- /* This FD will be later imported as a FENCE OBJECT which will be*/
|
|
|
- /* used to tell the GPU to wait for KMS to complete the changes */
|
|
|
- /* requested in atomic_commit (the pageflip in this case). */
|
|
|
- /*****************************************************************/
|
|
|
- if (dispdata->kms_in_fence_fd != -1)
|
|
|
- {
|
|
|
- add_plane_property(dispdata->atomic_req, dispdata->display_plane,
|
|
|
- "IN_FENCE_FD", dispdata->kms_in_fence_fd);
|
|
|
- add_crtc_property(dispdata->atomic_req, dispdata->crtc,
|
|
|
- "OUT_FENCE_PTR", VOID2U64(&dispdata->kms_out_fence_fd));
|
|
|
+ if (!KMSDRM_WaitPageFlip(_this, windata, timeout)) {
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
- /* Do we have a pending modesetting? If so, set the necessary
|
|
|
- props so it's included in the incoming atomic commit. */
|
|
|
- if (dispdata->modeset_pending) {
|
|
|
- uint32_t blob_id;
|
|
|
- SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata;
|
|
|
-
|
|
|
- add_connector_property(dispdata->atomic_req, dispdata->connector, "CRTC_ID", dispdata->crtc->crtc->crtc_id);
|
|
|
- KMSDRM_drmModeCreatePropertyBlob(viddata->drm_fd, &dispdata->mode, sizeof(dispdata->mode), &blob_id);
|
|
|
- add_crtc_property(dispdata->atomic_req, dispdata->crtc, "MODE_ID", blob_id);
|
|
|
- add_crtc_property(dispdata->atomic_req, dispdata->crtc, "active", 1);
|
|
|
- modesetting = SDL_TRUE;
|
|
|
- dispdata->modeset_pending = SDL_FALSE;
|
|
|
- }
|
|
|
-
|
|
|
- /*****************************************************************/
|
|
|
- /* Issue a non-blocking atomic commit: for triple buffering, */
|
|
|
- /* this must not block so the game can start building another */
|
|
|
- /* frame, even if the just-requested pageflip hasnt't completed. */
|
|
|
- /*****************************************************************/
|
|
|
- if (drm_atomic_commit(_this, SDL_FALSE, modesetting)) {
|
|
|
- return SDL_SetError("Failed to issue atomic commit on pageflip");
|
|
|
- }
|
|
|
-
|
|
|
- /* Release the previous front buffer so EGL can chose it as back buffer
|
|
|
- and render on it again. */
|
|
|
+ /* Release the previous front buffer */
|
|
|
if (windata->bo) {
|
|
|
KMSDRM_gbm_surface_release_buffer(windata->gs, windata->bo);
|
|
|
+ windata->bo = NULL;
|
|
|
}
|
|
|
|
|
|
- /* Take note of the buffer about to become front buffer, so next
|
|
|
- time we come here we can free it like we just did with the previous
|
|
|
- front buffer. */
|
|
|
windata->bo = windata->next_bo;
|
|
|
|
|
|
- /****************************************************************/
|
|
|
- /* Import the KMS-side FENCE OUTPUT FD from the kernel to the */
|
|
|
- /* KMS-side FENCE OBJECT so we can use use it to fence the GPU. */
|
|
|
- /****************************************************************/
|
|
|
- dispdata->kms_fence = create_fence(dispdata->kms_out_fence_fd, _this);
|
|
|
- assert(dispdata->kms_fence);
|
|
|
-
|
|
|
- /****************************************************************/
|
|
|
- /* "Delete" the fence OUTPUT FD, because we already have the */
|
|
|
- /* KMS FENCE OBJECT, the fence itself is away from us, on the */
|
|
|
- /* kernel side. */
|
|
|
- /****************************************************************/
|
|
|
- dispdata->kms_out_fence_fd = -1;
|
|
|
-
|
|
|
- /*****************************************************************/
|
|
|
- /* Tell the GPU to wait on the fence for the KMS-side FENCE, */
|
|
|
- /* which means waiting until the requested pageflip is completed.*/
|
|
|
- /*****************************************************************/
|
|
|
- _this->egl_data->eglWaitSyncKHR(_this->egl_data->egl_display, dispdata->kms_fence, 0);
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-int
|
|
|
-KMSDRM_GLES_SwapWindowDoubleBuffered(_THIS, SDL_Window * window)
|
|
|
-{
|
|
|
- SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata);
|
|
|
- SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
|
|
|
- KMSDRM_FBInfo *fb;
|
|
|
- KMSDRM_PlaneInfo info = {0};
|
|
|
- SDL_bool modesetting = SDL_FALSE;
|
|
|
-
|
|
|
- /**********************************************************************************/
|
|
|
- /* In double-buffer mode, atomic_commit will always be synchronous/blocking (ie: */
|
|
|
- /* won't return until the requested changes are really done). */
|
|
|
- /* Also, there's no need to fence KMS or the GPU, because we won't be entering */
|
|
|
- /* game loop again (hence not building or executing a new cmdstring) until */
|
|
|
- /* pageflip is done, so we don't need to protect the KMS/GPU access to the buffer.*/
|
|
|
- /**********************************************************************************/
|
|
|
-
|
|
|
- /* Mark, at EGL level, the buffer that we want to become the new front buffer.
|
|
|
- It won't really happen until we request a pageflip at the KMS level and it
|
|
|
- completes. */
|
|
|
- if (! _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, windata->egl_surface)) {
|
|
|
- return SDL_EGL_SetError("Failed to swap EGL buffers", "eglSwapBuffers");
|
|
|
+ /* Mark a buffer to becume the next front buffer.
|
|
|
+ This won't happen until pagelip completes. */
|
|
|
+ if (!(_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display,
|
|
|
+ windata->egl_surface))) {
|
|
|
+ SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "eglSwapBuffers failed.");
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
- /* Lock the buffer that is marked by eglSwapBuffers() to become the next front buffer
|
|
|
- (so it can not be chosen by EGL as back buffer to draw on), and get a handle to it,
|
|
|
- to request the pageflip on it. */
|
|
|
+ /* Lock the next front buffer so it can't be allocated as a back buffer */
|
|
|
windata->next_bo = KMSDRM_gbm_surface_lock_front_buffer(windata->gs);
|
|
|
if (!windata->next_bo) {
|
|
|
- return SDL_SetError("Failed to lock frontbuffer");
|
|
|
+ SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not lock GBM surface front buffer");
|
|
|
+ return 0;
|
|
|
}
|
|
|
- fb = KMSDRM_FBFromBO(_this, windata->next_bo);
|
|
|
- if (!fb) {
|
|
|
- return SDL_SetError("Failed to get a new framebuffer BO");
|
|
|
- }
|
|
|
-
|
|
|
- /* Add the pageflip to the request list. */
|
|
|
- info.plane = dispdata->display_plane;
|
|
|
- info.crtc_id = dispdata->crtc->crtc->crtc_id;
|
|
|
- info.fb_id = fb->fb_id;
|
|
|
-
|
|
|
- info.src_w = windata->src_w;
|
|
|
- info.src_h = windata->src_h;
|
|
|
- info.crtc_w = windata->output_w;
|
|
|
- info.crtc_h = windata->output_h;
|
|
|
- info.crtc_x = windata->output_x;
|
|
|
-
|
|
|
- drm_atomic_set_plane_props(&info);
|
|
|
-
|
|
|
- /* Do we have a pending modesetting? If so, set the necessary
|
|
|
- props so it's included in the incoming atomic commit. */
|
|
|
- if (dispdata->modeset_pending) {
|
|
|
- uint32_t blob_id;
|
|
|
|
|
|
- SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata;
|
|
|
-
|
|
|
- add_connector_property(dispdata->atomic_req, dispdata->connector, "CRTC_ID", dispdata->crtc->crtc->crtc_id);
|
|
|
- KMSDRM_drmModeCreatePropertyBlob(viddata->drm_fd, &dispdata->mode, sizeof(dispdata->mode), &blob_id);
|
|
|
- add_crtc_property(dispdata->atomic_req, dispdata->crtc, "MODE_ID", blob_id);
|
|
|
- add_crtc_property(dispdata->atomic_req, dispdata->crtc, "active", 1);
|
|
|
- modesetting = SDL_TRUE;
|
|
|
- dispdata->modeset_pending = SDL_FALSE;
|
|
|
+ /* Get the fb_info for the next front buffer. */
|
|
|
+ fb_info = KMSDRM_FBFromBO(_this, windata->next_bo);
|
|
|
+ if (!fb_info) {
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
- /* Issue the one and only atomic commit where all changes will be requested!.
|
|
|
- Blocking for double buffering: won't return until completed. */
|
|
|
- if (drm_atomic_commit(_this, SDL_TRUE, modesetting)) {
|
|
|
- return SDL_SetError("Failed to issue atomic commit on pageflip");
|
|
|
- }
|
|
|
+ /* Issue pageflip on the next front buffer.
|
|
|
+ The pageflip will be done during the next vblank. */
|
|
|
+ ret = KMSDRM_drmModePageFlip(viddata->drm_fd, dispdata->crtc->crtc_id,
|
|
|
+ fb_info->fb_id, DRM_MODE_PAGE_FLIP_EVENT, &windata->waiting_for_flip);
|
|
|
|
|
|
- /* Release last front buffer so EGL can chose it as back buffer and render on it again. */
|
|
|
- if (windata->bo) {
|
|
|
- KMSDRM_gbm_surface_release_buffer(windata->gs, windata->bo);
|
|
|
+ if (_this->egl_data->egl_swapinterval == 1) {
|
|
|
+ if (ret == 0) {
|
|
|
+ windata->waiting_for_flip = SDL_TRUE;
|
|
|
+ } else {
|
|
|
+ SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not queue pageflip: %d", ret);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- /* Take note of current front buffer, so we can free it next time we come here. */
|
|
|
- windata->bo = windata->next_bo;
|
|
|
-
|
|
|
- return 0;
|
|
|
-}
|
|
|
-
|
|
|
-int
|
|
|
-KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window)
|
|
|
-{
|
|
|
- SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata);
|
|
|
-
|
|
|
- if (windata->swap_window == NULL) {
|
|
|
- /* We want the fenced version by default, but it needs extensions. */
|
|
|
- if ( (SDL_GetHintBoolean(SDL_HINT_VIDEO_DOUBLE_BUFFER, SDL_FALSE)) ||
|
|
|
- (!SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_ANDROID_native_fence_sync")) )
|
|
|
- {
|
|
|
- windata->swap_window = KMSDRM_GLES_SwapWindowDoubleBuffered;
|
|
|
- } else {
|
|
|
- windata->swap_window = KMSDRM_GLES_SwapWindowFenced;
|
|
|
- }
|
|
|
+ /* If we are in double-buffer mode, wait immediately for vsync
|
|
|
+ (as if we only had two buffers),
|
|
|
+ Run your SDL2 program with "SDL_KMSDRM_DOUBLE_BUFFER=1 <program_name>"
|
|
|
+ to enable this. */
|
|
|
+ if (_this->egl_data->egl_swapinterval == 1 && windata->double_buffer) {
|
|
|
+ KMSDRM_WaitPageFlip(_this, windata, -1);
|
|
|
}
|
|
|
|
|
|
- return windata->swap_window(_this, window);
|
|
|
+ return 0;
|
|
|
}
|
|
|
|
|
|
-/***************************************/
|
|
|
-/* End of Atomic functions block */
|
|
|
-/***************************************/
|
|
|
-
|
|
|
SDL_EGL_MakeCurrent_impl(KMSDRM)
|
|
|
|
|
|
#endif /* SDL_VIDEO_DRIVER_KMSDRM */
|