2017-08-02 19:22:48 +02:00
|
|
|
/*
|
|
|
|
Simple DirectMedia Layer
|
2020-01-17 05:49:25 +01:00
|
|
|
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
2017-08-02 19:22:48 +02:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
2017-08-22 02:20:50 +02:00
|
|
|
|
2017-08-02 19:22:48 +02:00
|
|
|
#include "../../SDL_internal.h"
|
|
|
|
|
|
|
|
#if SDL_VIDEO_DRIVER_KMSDRM && SDL_VIDEO_OPENGL_EGL
|
|
|
|
|
|
|
|
#include "SDL_kmsdrmvideo.h"
|
|
|
|
#include "SDL_kmsdrmopengles.h"
|
|
|
|
#include "SDL_kmsdrmdyn.h"
|
|
|
|
|
|
|
|
#ifndef EGL_PLATFORM_GBM_MESA
|
|
|
|
#define EGL_PLATFORM_GBM_MESA 0x31D7
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* EGL implementation of SDL OpenGL support */
|
|
|
|
|
|
|
|
int
|
|
|
|
KMSDRM_GLES_LoadLibrary(_THIS, const char *path) {
|
2020-07-20 11:42:23 +02:00
|
|
|
NativeDisplayType display = (NativeDisplayType)((SDL_VideoData *)_this->driverdata)->gbm_dev;
|
2020-02-27 17:20:34 +01:00
|
|
|
return SDL_EGL_LoadLibrary(_this, path, display, EGL_PLATFORM_GBM_MESA);
|
2017-08-02 19:22:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_EGL_CreateContext_impl(KMSDRM)
|
|
|
|
|
|
|
|
int KMSDRM_GLES_SetSwapInterval(_THIS, int interval) {
|
|
|
|
if (!_this->egl_data) {
|
|
|
|
return SDL_SetError("EGL not initialized");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interval == 0 || interval == 1) {
|
|
|
|
_this->egl_data->egl_swapinterval = interval;
|
|
|
|
} else {
|
|
|
|
return SDL_SetError("Only swap intervals of 0 or 1 are supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-28 21:11:25 +02:00
|
|
|
/*********************************/
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:22:48 +02:00
|
|
|
int
|
|
|
|
KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window) {
|
2020-07-28 21:11:25 +02:00
|
|
|
|
2020-02-09 20:44:22 +01:00
|
|
|
SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata);
|
|
|
|
SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
|
2020-07-28 21:11:25 +02:00
|
|
|
KMSDRM_FBInfo *fb;
|
|
|
|
int ret;
|
Fixed bug 5140 - KMSDRM: Dynamic vsync toggle does not work
Manuel Alfayate Corchete
The KMSDRM backend was doing things wrong because of some small (but important) misconceptions on how KMS/DRM works: to implement a largely broken non-vsync refresh mechanism, the SwapWindow() function was issuing new pageflips before previous ones had completed, thus causing EBUSY returns, buffer mismanagement, etc... resulting in general breakage on vsync disabling from apps, that would not allow vsync to work again without KMSDRM video re-initialization.
To further clarify, on most DRM drivers async pageflips are NOT working nowadays, so all issued pageflips will complete on next VBLANK, NOT ASAP (calling drmModePageFlip() with the DRM_MODE_PAGE_FLIP_ASYNC flag will return error).
The old code was assuming that can just issue a synchronous (=on VBLANK) pageflip and then pass a 0 timeout to the pull() function so we do not wait for the pageflip event, thinking that this will lead to correct non-vsynced screen updates from the program: That is plain wrong.
Each pageflip has to be waite before issuing a new one, ALWAYS. And if we do not support ASYNC pageflips on the DRM driver level, then we are forced to wait for the next VBLANK. There is no way around it.
I have also added many comments on the KMSDRM code. This is needed for future reference for me or others who may need to look at this code: KMS/DRM terminology regarding what SYNC and ASYNC mean in pageflip terms, and where to do certain things and why, is not trivial. It is not desirable or possible to invest time on researching the same concepts every time there is need to dive into this code. So please leave all these comments in the patch.
2020-05-27 01:27:00 +02:00
|
|
|
|
2020-07-28 21:11:25 +02:00
|
|
|
uint32_t flags = DRM_MODE_ATOMIC_NONBLOCK;
|
Fixed bug 5140 - KMSDRM: Dynamic vsync toggle does not work
Manuel Alfayate Corchete
The KMSDRM backend was doing things wrong because of some small (but important) misconceptions on how KMS/DRM works: to implement a largely broken non-vsync refresh mechanism, the SwapWindow() function was issuing new pageflips before previous ones had completed, thus causing EBUSY returns, buffer mismanagement, etc... resulting in general breakage on vsync disabling from apps, that would not allow vsync to work again without KMSDRM video re-initialization.
To further clarify, on most DRM drivers async pageflips are NOT working nowadays, so all issued pageflips will complete on next VBLANK, NOT ASAP (calling drmModePageFlip() with the DRM_MODE_PAGE_FLIP_ASYNC flag will return error).
The old code was assuming that can just issue a synchronous (=on VBLANK) pageflip and then pass a 0 timeout to the pull() function so we do not wait for the pageflip event, thinking that this will lead to correct non-vsynced screen updates from the program: That is plain wrong.
Each pageflip has to be waite before issuing a new one, ALWAYS. And if we do not support ASYNC pageflips on the DRM driver level, then we are forced to wait for the next VBLANK. There is no way around it.
I have also added many comments on the KMSDRM code. This is needed for future reference for me or others who may need to look at this code: KMS/DRM terminology regarding what SYNC and ASYNC mean in pageflip terms, and where to do certain things and why, is not trivial. It is not desirable or possible to invest time on researching the same concepts every time there is need to dive into this code. So please leave all these comments in the patch.
2020-05-27 01:27:00 +02:00
|
|
|
|
2020-07-28 21:11:25 +02:00
|
|
|
EGLSyncKHR gpu_fence = NULL; /* out-fence from gpu, in-fence to kms */
|
|
|
|
EGLSyncKHR kms_fence = NULL; /* in-fence to gpu, out-fence from kms */
|
Fixed bug 5140 - KMSDRM: Dynamic vsync toggle does not work
Manuel Alfayate Corchete
The KMSDRM backend was doing things wrong because of some small (but important) misconceptions on how KMS/DRM works: to implement a largely broken non-vsync refresh mechanism, the SwapWindow() function was issuing new pageflips before previous ones had completed, thus causing EBUSY returns, buffer mismanagement, etc... resulting in general breakage on vsync disabling from apps, that would not allow vsync to work again without KMSDRM video re-initialization.
To further clarify, on most DRM drivers async pageflips are NOT working nowadays, so all issued pageflips will complete on next VBLANK, NOT ASAP (calling drmModePageFlip() with the DRM_MODE_PAGE_FLIP_ASYNC flag will return error).
The old code was assuming that can just issue a synchronous (=on VBLANK) pageflip and then pass a 0 timeout to the pull() function so we do not wait for the pageflip event, thinking that this will lead to correct non-vsynced screen updates from the program: That is plain wrong.
Each pageflip has to be waite before issuing a new one, ALWAYS. And if we do not support ASYNC pageflips on the DRM driver level, then we are forced to wait for the next VBLANK. There is no way around it.
I have also added many comments on the KMSDRM code. This is needed for future reference for me or others who may need to look at this code: KMS/DRM terminology regarding what SYNC and ASYNC mean in pageflip terms, and where to do certain things and why, is not trivial. It is not desirable or possible to invest time on researching the same concepts every time there is need to dive into this code. So please leave all these comments in the patch.
2020-05-27 01:27:00 +02:00
|
|
|
|
2020-07-28 21:11:25 +02:00
|
|
|
/* Allow modeset (which is done inside atomic_commit). */
|
|
|
|
flags |= DRM_MODE_ATOMIC_ALLOW_MODESET;
|
Fixed bug 5140 - KMSDRM: Dynamic vsync toggle does not work
Manuel Alfayate Corchete
The KMSDRM backend was doing things wrong because of some small (but important) misconceptions on how KMS/DRM works: to implement a largely broken non-vsync refresh mechanism, the SwapWindow() function was issuing new pageflips before previous ones had completed, thus causing EBUSY returns, buffer mismanagement, etc... resulting in general breakage on vsync disabling from apps, that would not allow vsync to work again without KMSDRM video re-initialization.
To further clarify, on most DRM drivers async pageflips are NOT working nowadays, so all issued pageflips will complete on next VBLANK, NOT ASAP (calling drmModePageFlip() with the DRM_MODE_PAGE_FLIP_ASYNC flag will return error).
The old code was assuming that can just issue a synchronous (=on VBLANK) pageflip and then pass a 0 timeout to the pull() function so we do not wait for the pageflip event, thinking that this will lead to correct non-vsynced screen updates from the program: That is plain wrong.
Each pageflip has to be waite before issuing a new one, ALWAYS. And if we do not support ASYNC pageflips on the DRM driver level, then we are forced to wait for the next VBLANK. There is no way around it.
I have also added many comments on the KMSDRM code. This is needed for future reference for me or others who may need to look at this code: KMS/DRM terminology regarding what SYNC and ASYNC mean in pageflip terms, and where to do certain things and why, is not trivial. It is not desirable or possible to invest time on researching the same concepts every time there is need to dive into this code. So please leave all these comments in the patch.
2020-05-27 01:27:00 +02:00
|
|
|
|
2020-07-28 21:11:25 +02:00
|
|
|
if (dispdata->kms_out_fence_fd != -1) {
|
|
|
|
kms_fence = create_fence(dispdata->kms_out_fence_fd, _this);
|
|
|
|
assert(kms_fence);
|
2017-08-02 19:22:48 +02:00
|
|
|
|
2020-07-28 21:11:25 +02:00
|
|
|
/* driver now has ownership of the fence fd: */
|
|
|
|
dispdata->kms_out_fence_fd = -1;
|
|
|
|
|
|
|
|
/* wait "on the gpu" (ie. this won't necessarily block, but
|
|
|
|
* will block the rendering until fence is signaled), until
|
|
|
|
* the previous pageflip completes so we don't render into
|
|
|
|
* the buffer that is still on screen.
|
|
|
|
*/
|
|
|
|
_this->egl_data->eglWaitSyncKHR(_this->egl_data->egl_display, kms_fence, 0);
|
2020-02-09 20:44:22 +01:00
|
|
|
}
|
|
|
|
|
2020-07-28 21:11:25 +02:00
|
|
|
/* insert fence to be singled in cmdstream.. this fence will be
|
|
|
|
* signaled when gpu rendering done
|
|
|
|
*/
|
|
|
|
gpu_fence = create_fence(EGL_NO_NATIVE_FENCE_FD_ANDROID, _this);
|
|
|
|
assert(gpu_fence);
|
|
|
|
|
|
|
|
_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, windata->egl_surface);
|
|
|
|
|
|
|
|
/* after swapbuffers, gpu_fence should be flushed, so safe to get the fd: */
|
|
|
|
dispdata->kms_in_fence_fd = _this->egl_data->eglDupNativeFenceFDANDROID(_this->egl_data->egl_display, gpu_fence);
|
|
|
|
_this->egl_data->eglDestroySyncKHR(_this->egl_data->egl_display, gpu_fence);
|
|
|
|
assert(dispdata->kms_in_fence_fd != -1);
|
|
|
|
|
|
|
|
windata->next_bo = KMSDRM_gbm_surface_lock_front_buffer(windata->gs);
|
|
|
|
if (!windata->next_bo) {
|
|
|
|
printf("Failed to lock frontbuffer\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fb = KMSDRM_FBFromBO(_this, windata->next_bo);
|
|
|
|
if (!fb) {
|
|
|
|
printf("Failed to get a new framebuffer BO\n");
|
|
|
|
return -1;
|
2017-08-02 19:22:48 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 21:11:25 +02:00
|
|
|
if (kms_fence) {
|
|
|
|
EGLint status;
|
|
|
|
|
|
|
|
/* Wait on the CPU side for the _previous_ commit to
|
|
|
|
* complete before we post the flip through KMS, as
|
|
|
|
* atomic will reject the commit if we post a new one
|
|
|
|
* whilst the previous one is still pending.
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
status = _this->egl_data->eglClientWaitSyncKHR(_this->egl_data->egl_display,
|
|
|
|
kms_fence,
|
|
|
|
0,
|
|
|
|
EGL_FOREVER_KHR);
|
|
|
|
} while (status != EGL_CONDITION_SATISFIED_KHR);
|
|
|
|
|
|
|
|
_this->egl_data->eglDestroySyncKHR(_this->egl_data->egl_display, kms_fence);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here you could also update drm plane layers if you want
|
|
|
|
* hw composition
|
|
|
|
*/
|
|
|
|
ret = drm_atomic_commit(_this, fb->fb_id, flags);
|
|
|
|
if (ret) {
|
|
|
|
printf("failed to do atomic commit\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release last buffer to render on again: */
|
|
|
|
if (windata->curr_bo) {
|
|
|
|
KMSDRM_gbm_surface_release_buffer(windata->gs, windata->curr_bo);
|
|
|
|
windata->curr_bo = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Take note of the current front buffer, so it can be freed next time this function is called. */
|
|
|
|
windata->curr_bo = windata->next_bo;
|
|
|
|
|
|
|
|
/* Allow a modeset change for the first commit only. */
|
|
|
|
flags &= ~(DRM_MODE_ATOMIC_ALLOW_MODESET);
|
|
|
|
|
|
|
|
return ret;
|
2017-08-02 19:22:48 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 21:11:25 +02:00
|
|
|
/***************************************/
|
|
|
|
/* End of Atomic functions block */
|
|
|
|
/***************************************/
|
|
|
|
|
2017-08-02 19:22:48 +02:00
|
|
|
SDL_EGL_MakeCurrent_impl(KMSDRM)
|
|
|
|
|
|
|
|
#endif /* SDL_VIDEO_DRIVER_KMSDRM && SDL_VIDEO_OPENGL_EGL */
|
|
|
|
|
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|