2017-08-02 19:22:48 +02:00
|
|
|
/*
|
|
|
|
Simple DirectMedia Layer
|
2021-01-02 19:25:38 +01:00
|
|
|
Copyright (C) 1997-2021 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"
|
|
|
|
|
2020-12-22 14:15:33 +01:00
|
|
|
#if SDL_VIDEO_DRIVER_KMSDRM
|
2017-08-02 19:22:48 +02:00
|
|
|
|
2021-01-08 18:57:12 +01:00
|
|
|
#include "SDL_log.h"
|
|
|
|
|
2017-08-02 19:22:48 +02:00
|
|
|
#include "SDL_kmsdrmvideo.h"
|
|
|
|
#include "SDL_kmsdrmopengles.h"
|
|
|
|
#include "SDL_kmsdrmdyn.h"
|
2021-01-11 20:29:09 +01:00
|
|
|
#include <errno.h>
|
2017-08-02 19:22:48 +02:00
|
|
|
|
|
|
|
#ifndef EGL_PLATFORM_GBM_MESA
|
|
|
|
#define EGL_PLATFORM_GBM_MESA 0x31D7
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* EGL implementation of SDL OpenGL support */
|
|
|
|
|
2020-09-10 21:04:35 +02:00
|
|
|
void
|
|
|
|
KMSDRM_GLES_DefaultProfileConfig(_THIS, int *mask, int *major, int *minor)
|
|
|
|
{
|
|
|
|
/* if SDL was _also_ built with the Raspberry Pi driver (so we're
|
|
|
|
definitely a Pi device), default to GLES2. */
|
|
|
|
#if SDL_VIDEO_DRIVER_RPI
|
|
|
|
*mask = SDL_GL_CONTEXT_PROFILE_ES;
|
|
|
|
*major = 2;
|
|
|
|
*minor = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-08-02 19:22:48 +02:00
|
|
|
int
|
|
|
|
KMSDRM_GLES_LoadLibrary(_THIS, const char *path) {
|
2020-12-18 22:53:51 +01:00
|
|
|
/* Just pretend you do this here, but don't do it until KMSDRM_CreateWindow(),
|
|
|
|
where we do the same library load we would normally do here.
|
|
|
|
because this gets called by SDL_CreateWindow() before KMSDR_CreateWindow(),
|
|
|
|
so gbm dev isn't yet created when this is called, AND we can't alter the
|
|
|
|
call order in SDL_CreateWindow(). */
|
|
|
|
#if 0
|
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);
|
2020-12-18 22:53:51 +01:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
KMSDRM_GLES_UnloadLibrary(_THIS) {
|
2020-12-31 01:31:57 +01:00
|
|
|
/* As with KMSDRM_GLES_LoadLibrary(), we define our own "dummy" unloading function
|
|
|
|
so we manually unload the library whenever we want. */
|
2017-08-02 19:22:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SDL_EGL_CreateContext_impl(KMSDRM)
|
|
|
|
|
|
|
|
int KMSDRM_GLES_SetSwapInterval(_THIS, int interval) {
|
2021-01-11 20:29:09 +01:00
|
|
|
|
2017-08-02 19:22:48 +02:00
|
|
|
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");
|
2021-01-11 22:28:27 +01:00
|
|
|
}
|
2017-08-02 19:22:48 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-10 23:26:02 +02:00
|
|
|
int
|
2021-01-08 18:57:12 +01:00
|
|
|
KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window) {
|
2020-02-09 20:44:22 +01:00
|
|
|
SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata);
|
|
|
|
SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
|
2021-01-08 18:57:12 +01:00
|
|
|
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
|
|
|
|
KMSDRM_FBInfo *fb_info;
|
2021-01-13 15:54:26 +01:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* Always wait for the previous issued flip before issing a new one,
|
|
|
|
even if you do async flips. */
|
|
|
|
uint32_t flip_flags = DRM_MODE_PAGE_FLIP_EVENT;
|
2020-08-03 22:24:49 +02:00
|
|
|
|
2021-01-08 18:57:12 +01:00
|
|
|
/* Wait for confirmation that the next front buffer has been flipped, at which
|
|
|
|
point the previous front buffer can be released */
|
2021-01-15 15:00:17 +01:00
|
|
|
if (!KMSDRM_WaitPageflip(_this, windata)) {
|
2021-01-14 10:18:40 +01:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Wait for previous pageflip failed");
|
2021-01-08 18:57:12 +01:00
|
|
|
return 0;
|
2020-08-25 16:18:49 +02:00
|
|
|
}
|
|
|
|
|
2021-01-08 18:57:12 +01:00
|
|
|
/* Release the previous front buffer */
|
2020-08-05 02:06:59 +02:00
|
|
|
if (windata->bo) {
|
2020-10-22 16:01:51 +02:00
|
|
|
KMSDRM_gbm_surface_release_buffer(windata->gs, windata->bo);
|
2021-01-08 18:57:12 +01:00
|
|
|
windata->bo = NULL;
|
2020-08-03 22:24:49 +02:00
|
|
|
}
|
|
|
|
|
2020-08-05 02:06:59 +02:00
|
|
|
windata->bo = windata->next_bo;
|
|
|
|
|
2021-01-08 18:57:12 +01:00
|
|
|
/* 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))) {
|
2021-01-14 10:18:40 +01:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "eglSwapBuffers failed");
|
2021-01-08 18:57:12 +01:00
|
|
|
return 0;
|
2020-09-06 12:08:22 +02:00
|
|
|
}
|
2020-08-05 02:06:59 +02:00
|
|
|
|
2021-01-11 20:29:09 +01:00
|
|
|
/* From the GBM surface, get the next BO to become the next front buffer,
|
|
|
|
and lock it so it can't be allocated as a back buffer (to prevent EGL
|
|
|
|
from drawing into it!) */
|
2020-08-03 22:24:49 +02:00
|
|
|
windata->next_bo = KMSDRM_gbm_surface_lock_front_buffer(windata->gs);
|
|
|
|
if (!windata->next_bo) {
|
2021-01-14 10:18:40 +01:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not lock front buffer on GBM surface");
|
2021-01-08 18:57:12 +01:00
|
|
|
return 0;
|
2020-08-03 22:24:49 +02:00
|
|
|
}
|
2020-12-29 14:24:38 +01:00
|
|
|
|
2021-01-11 20:29:09 +01:00
|
|
|
/* Get an actual usable fb for the next front buffer. */
|
2021-01-08 18:57:12 +01:00
|
|
|
fb_info = KMSDRM_FBFromBO(_this, windata->next_bo);
|
|
|
|
if (!fb_info) {
|
2021-01-14 10:18:40 +01:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not get a framebuffer");
|
2021-01-08 18:57:12 +01:00
|
|
|
return 0;
|
2020-09-12 04:56:55 +02:00
|
|
|
}
|
|
|
|
|
2021-01-08 22:00:28 +01:00
|
|
|
/* Do we have a modeset pending? If so, configure the new mode on the CRTC.
|
2021-01-14 10:18:40 +01:00
|
|
|
Has to be done before next pageflip issues, so the buffer with the
|
|
|
|
new size is big enough for preventing CRTC from reading out of bounds. */
|
2021-01-08 22:00:28 +01:00
|
|
|
if (dispdata->modeset_pending) {
|
2021-01-09 02:25:13 +01:00
|
|
|
|
2021-01-08 22:00:28 +01:00
|
|
|
ret = KMSDRM_drmModeSetCrtc(viddata->drm_fd,
|
|
|
|
dispdata->crtc->crtc_id, fb_info->fb_id, 0, 0,
|
|
|
|
&dispdata->connector->connector_id, 1, &dispdata->mode);
|
|
|
|
|
2021-01-11 20:29:09 +01:00
|
|
|
dispdata->modeset_pending = SDL_FALSE;
|
|
|
|
|
2021-01-08 22:00:28 +01:00
|
|
|
if (ret) {
|
2021-01-09 02:25:13 +01:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not set videomode on CRTC.");
|
2021-01-14 10:18:40 +01:00
|
|
|
return 0;
|
2021-01-09 02:25:13 +01:00
|
|
|
}
|
2021-01-11 20:29:09 +01:00
|
|
|
|
2021-01-14 10:18:40 +01:00
|
|
|
/* It's OK to return now if we have done a drmModeSetCrtc(),
|
|
|
|
it has done the pageflip and blocked until it was done. */
|
|
|
|
return 1;
|
2021-01-08 22:00:28 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 18:57:12 +01:00
|
|
|
/* Issue pageflip on the next front buffer.
|
2021-01-13 15:54:26 +01:00
|
|
|
Remember: drmModePageFlip() never blocks, it just issues the flip,
|
2021-01-13 20:11:01 +01:00
|
|
|
which will be done during the next vblank, or immediately if
|
|
|
|
we pass the DRM_MODE_PAGE_FLIP_ASYNC flag.
|
|
|
|
Since calling drmModePageFlip() will return EBUSY if we call it
|
|
|
|
without having completed the last issued flip, we must pass the
|
2021-01-13 15:54:26 +01:00
|
|
|
DRM_MODE_PAGE_FLIP_ASYNC if we don't block on EGL (egl_swapinterval = 0).
|
2021-01-13 20:11:01 +01:00
|
|
|
That makes it flip immediately, without waiting for the next vblank
|
|
|
|
to do so, so even if we don't block on EGL, the flip will have completed
|
|
|
|
when we get here again. */
|
2021-01-13 15:54:26 +01:00
|
|
|
|
2021-01-31 03:48:29 +01:00
|
|
|
if (_this->egl_data->egl_swapinterval == 0 && viddata->async_pageflip_support) {
|
2021-01-13 15:54:26 +01:00
|
|
|
flip_flags |= DRM_MODE_PAGE_FLIP_ASYNC;
|
|
|
|
}
|
|
|
|
|
2021-01-08 18:57:12 +01:00
|
|
|
ret = KMSDRM_drmModePageFlip(viddata->drm_fd, dispdata->crtc->crtc_id,
|
2021-02-06 14:03:53 +01:00
|
|
|
fb_info->fb_id, flip_flags, &windata->waiting_for_flip);
|
2020-12-29 14:24:38 +01:00
|
|
|
|
2021-01-11 20:29:09 +01:00
|
|
|
if (ret == 0) {
|
2021-02-06 14:03:53 +01:00
|
|
|
windata->waiting_for_flip = SDL_TRUE;
|
2021-01-11 20:29:09 +01:00
|
|
|
} else {
|
2021-02-06 14:03:53 +01:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not queue pageflip: %d", ret);
|
2020-08-05 02:06:59 +02:00
|
|
|
}
|
2020-08-03 22:24:49 +02:00
|
|
|
|
2021-01-13 20:11:01 +01:00
|
|
|
/* Wait immediately for vsync (as if we only had two buffers).
|
2021-01-15 15:00:17 +01:00
|
|
|
Even if we are already doing a WaitPageflip at the begining of this
|
2021-01-13 20:11:01 +01:00
|
|
|
function, this is NOT redundant because here we wait immediately
|
|
|
|
after submitting the image to the screen, reducing lag, and if
|
|
|
|
we have waited here, there won't be a pending pageflip so the
|
2021-01-15 15:00:17 +01:00
|
|
|
WaitPageflip at the beggining of this function will be a no-op.
|
2021-01-13 20:11:01 +01:00
|
|
|
Just leave it here and don't worry.
|
2021-01-08 18:57:12 +01:00
|
|
|
Run your SDL2 program with "SDL_KMSDRM_DOUBLE_BUFFER=1 <program_name>"
|
|
|
|
to enable this. */
|
2021-01-14 10:18:40 +01:00
|
|
|
if (windata->double_buffer) {
|
2021-02-06 14:03:53 +01:00
|
|
|
if (!KMSDRM_WaitPageflip(_this, windata)) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Immediate wait for previous pageflip failed");
|
|
|
|
return 0;
|
|
|
|
}
|
2020-09-10 21:07:23 +02:00
|
|
|
}
|
|
|
|
|
2021-01-14 10:18:40 +01:00
|
|
|
return 1;
|
2020-09-10 21:07:23 +02:00
|
|
|
}
|
|
|
|
|
2017-08-02 19:22:48 +02:00
|
|
|
SDL_EGL_MakeCurrent_impl(KMSDRM)
|
|
|
|
|
2020-12-22 14:15:33 +01:00
|
|
|
#endif /* SDL_VIDEO_DRIVER_KMSDRM */
|
2017-08-02 19:22:48 +02:00
|
|
|
|
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|