mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-05-19 20:07:42 +08:00
kmsdrm: move hardware cursor functionality to the ATOMIC interface. Disconnect the display plane from the GBM surface buffers before destroying the GBM surface.
This commit is contained in:
@@ -39,6 +39,17 @@ static void KMSDRM_FreeCursor(SDL_Cursor * cursor);
|
||||
static void KMSDRM_WarpMouse(SDL_Window * window, int x, int y);
|
||||
static int KMSDRM_WarpMouseGlobal(int x, int y);
|
||||
|
||||
/*********************************/
|
||||
/* Atomic helper functions block.*/
|
||||
/*********************************/
|
||||
|
||||
|
||||
|
||||
/**************************************/
|
||||
/* Atomic helper functions block ends.*/
|
||||
/**************************************/
|
||||
|
||||
|
||||
/* Converts a pixel from straight-alpha [AA, RR, GG, BB], which the SDL cursor surface has,
|
||||
to premultiplied-alpha [AA. AA*RR, AA*GG, AA*BB].
|
||||
These multiplications have to be done with floats instead of uint32_t's, and the resulting values have
|
||||
@@ -119,6 +130,8 @@ KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* Here simply set the hox_x and hot_y, that will be used in drm_atomic_movecursor().
|
||||
These are the coordinates of the "tip of the cursor" from it's base. */
|
||||
curdata->hot_x = hot_x;
|
||||
curdata->hot_y = hot_y;
|
||||
curdata->w = usable_cursor_w;
|
||||
@@ -135,8 +148,9 @@ KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
|
||||
bo_stride = KMSDRM_gbm_bo_get_stride(curdata->bo);
|
||||
bufsize = bo_stride * curdata->h;
|
||||
|
||||
/* Always use a temp buffer: it serves the purpose of storing the alpha-premultiplied pixels (so
|
||||
we can copy them to the gbm BO with a single gb_bo_write() call), and also copying from the
|
||||
/* Always use a temp buffer: it serves the purpose of storing the
|
||||
alpha-premultiplied pixels (so we can copy them to the gbm BO
|
||||
with a single gb_bo_write() call), and also copying from the
|
||||
SDL surface, line by line, to a gbm BO with different pitch. */
|
||||
buffer = (uint32_t*)SDL_malloc(bufsize);
|
||||
if (!buffer) {
|
||||
@@ -196,18 +210,21 @@ cleanup:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Show the specified cursor, or hide if cursor is NULL */
|
||||
/* Show the specified cursor, or hide if cursor is NULL.
|
||||
cur_cursor is the current cursor, and cursor is the new cursor.
|
||||
A cursor is displayed on a display, so we have to add a pointer to dispdata
|
||||
to the driverdata
|
||||
*/
|
||||
static int
|
||||
KMSDRM_ShowCursor(SDL_Cursor * cursor)
|
||||
{
|
||||
SDL_VideoDevice *dev = SDL_GetVideoDevice();
|
||||
SDL_VideoData *viddata = ((SDL_VideoData *)dev->driverdata);
|
||||
SDL_VideoDevice *video_device = SDL_GetVideoDevice();
|
||||
//SDL_VideoData *viddata = ((SDL_VideoData *)dev->driverdata);
|
||||
SDL_Mouse *mouse;
|
||||
KMSDRM_CursorData *curdata;
|
||||
SDL_VideoDisplay *display = NULL;
|
||||
SDL_DisplayData *dispdata = NULL;
|
||||
int ret;
|
||||
uint32_t bo_handle;
|
||||
|
||||
mouse = SDL_GetMouse();
|
||||
if (!mouse) {
|
||||
@@ -221,35 +238,30 @@ KMSDRM_ShowCursor(SDL_Cursor * cursor)
|
||||
}
|
||||
}
|
||||
|
||||
/* Hide cursor */
|
||||
if (!cursor) {
|
||||
/* Hide current cursor */
|
||||
/* Hide CURRENT cursor, a cursor that is already on screen
|
||||
and SDL stores in mouse->cur_cursor. */
|
||||
if (mouse->cur_cursor && mouse->cur_cursor->driverdata) {
|
||||
curdata = (KMSDRM_CursorData *) mouse->cur_cursor->driverdata;
|
||||
if (curdata->video) {
|
||||
|
||||
ret = drm_atomic_setcursor(0, 0, 0);
|
||||
|
||||
if (curdata->crtc_id != 0) {
|
||||
ret = KMSDRM_drmModeSetCursor(viddata->drm_fd, curdata->crtc_id, 0, 0, 0);
|
||||
if (ret) {
|
||||
SDL_SetError("Could not hide current cursor with drmModeSetCursor().");
|
||||
SDL_SetError("Could not hide current cursor with drm_atomic_setcursor).");
|
||||
return ret;
|
||||
}
|
||||
/* Mark previous cursor as not-displayed */
|
||||
curdata->crtc_id = 0;
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
}
|
||||
/* otherwise if possible, hide global cursor */
|
||||
if (dispdata && dispdata->crtc_id != 0) {
|
||||
ret = KMSDRM_drmModeSetCursor(viddata->drm_fd, dispdata->crtc_id, 0, 0, 0);
|
||||
if (ret) {
|
||||
SDL_SetError("Could not hide display's cursor with drmModeSetCursor().");
|
||||
return ret;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SDL_SetError("Couldn't find cursor to hide.");
|
||||
}
|
||||
|
||||
|
||||
/* If cursor != NULL, show new cursor on display */
|
||||
if (!display) {
|
||||
return SDL_SetError("Could not get display for mouse.");
|
||||
@@ -257,54 +269,55 @@ KMSDRM_ShowCursor(SDL_Cursor * cursor)
|
||||
if (!dispdata) {
|
||||
return SDL_SetError("Could not get display driverdata.");
|
||||
}
|
||||
|
||||
|
||||
curdata = (KMSDRM_CursorData *) cursor->driverdata;
|
||||
if (!curdata || !curdata->bo) {
|
||||
return SDL_SetError("Cursor not initialized properly.");
|
||||
}
|
||||
|
||||
bo_handle = KMSDRM_gbm_bo_get_handle(curdata->bo).u32;
|
||||
if (curdata->hot_x == 0 && curdata->hot_y == 0) {
|
||||
ret = KMSDRM_drmModeSetCursor(viddata->drm_fd, dispdata->crtc_id, bo_handle,
|
||||
curdata->w, curdata->h);
|
||||
} else {
|
||||
ret = KMSDRM_drmModeSetCursor2(viddata->drm_fd, dispdata->crtc_id, bo_handle,
|
||||
curdata->w, curdata->h, curdata->hot_x, curdata->hot_y);
|
||||
}
|
||||
curdata->crtc_id = dispdata->crtc->crtc->crtc_id;
|
||||
curdata->video = video_device;
|
||||
|
||||
/* DO show cursor */
|
||||
ret = drm_atomic_setcursor(curdata, mouse->x - curdata->hot_x, mouse->y - curdata->hot_y);
|
||||
|
||||
if (ret) {
|
||||
SDL_SetError("drmModeSetCursor failed.");
|
||||
SDL_SetError("KMSDRM_SetCursor failed.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
curdata->crtc_id = dispdata->crtc_id;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Free a window manager cursor */
|
||||
/* Unset the cursor from the cusror plane, and ONLY WHEN THAT'S DONE,
|
||||
DONE FOR REAL, and not only requested, destroy it by destroying the curso BO.
|
||||
Destroying the cursor BO is an special an delicate situation,
|
||||
because drm_atomic_setcursor() returns immediately, and we DON'T
|
||||
want to get to gbm_bo_destroy() before the prop changes requested
|
||||
in drm_atomic_setcursor() have effectively been done. So we
|
||||
issue a BLOCKING atomic_commit here to avoid that situation.
|
||||
REMEMBER you yan issue an atomic_commit whenever you want, and
|
||||
the changes requested until that moment (for any planes, crtcs, etc.)
|
||||
will be done. */
|
||||
static void
|
||||
KMSDRM_FreeCursor(SDL_Cursor * cursor)
|
||||
{
|
||||
KMSDRM_CursorData *curdata;
|
||||
int drm_fd;
|
||||
KMSDRM_CursorData *curdata = NULL;
|
||||
SDL_VideoDevice *video = NULL;
|
||||
|
||||
if (cursor) {
|
||||
curdata = (KMSDRM_CursorData *) cursor->driverdata;
|
||||
video = curdata->video;
|
||||
if (video && curdata->bo) {
|
||||
drm_atomic_setcursor(0, 0, 0);
|
||||
/* Wait until the cursor is unset from the cursor plane before destroying it's BO. */
|
||||
drm_atomic_commit(video, SDL_TRUE);
|
||||
KMSDRM_gbm_bo_destroy(curdata->bo);
|
||||
curdata->bo = NULL;
|
||||
|
||||
if (curdata) {
|
||||
if (curdata->bo) {
|
||||
if (curdata->crtc_id != 0) {
|
||||
drm_fd = KMSDRM_gbm_device_get_fd(KMSDRM_gbm_bo_get_device(curdata->bo));
|
||||
/* Hide the cursor if previously shown on a CRTC */
|
||||
KMSDRM_drmModeSetCursor(drm_fd, curdata->crtc_id, 0, 0, 0);
|
||||
curdata->crtc_id = 0;
|
||||
}
|
||||
KMSDRM_gbm_bo_destroy(curdata->bo);
|
||||
curdata->bo = NULL;
|
||||
}
|
||||
SDL_free(cursor->driverdata);
|
||||
SDL_free(cursor->driverdata);
|
||||
SDL_free(cursor);
|
||||
}
|
||||
SDL_free(cursor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,20 +343,17 @@ KMSDRM_WarpMouseGlobal(int x, int y)
|
||||
/* And now update the cursor graphic position on screen. */
|
||||
curdata = (KMSDRM_CursorData *) mouse->cur_cursor->driverdata;
|
||||
if (curdata->bo) {
|
||||
int ret;
|
||||
|
||||
if (curdata->crtc_id != 0) {
|
||||
int ret, drm_fd;
|
||||
drm_fd = KMSDRM_gbm_device_get_fd(KMSDRM_gbm_bo_get_device(curdata->bo));
|
||||
ret = KMSDRM_drmModeMoveCursor(drm_fd, curdata->crtc_id, x, y);
|
||||
drm_atomic_movecursor(curdata, x, y);
|
||||
ret = drm_atomic_commit(curdata->video, SDL_TRUE);
|
||||
|
||||
if (ret) {
|
||||
SDL_SetError("drmModeMoveCursor() failed.");
|
||||
}
|
||||
if (ret) {
|
||||
SDL_SetError("drm_atomic_movecursor() failed.");
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
return ret;
|
||||
} else {
|
||||
return SDL_SetError("Cursor is not currently shown.");
|
||||
}
|
||||
} else {
|
||||
return SDL_SetError("Cursor not initialized properly.");
|
||||
}
|
||||
@@ -382,18 +392,21 @@ KMSDRM_MoveCursor(SDL_Cursor * cursor)
|
||||
{
|
||||
SDL_Mouse *mouse = SDL_GetMouse();
|
||||
KMSDRM_CursorData *curdata;
|
||||
int drm_fd, ret;
|
||||
int ret;
|
||||
|
||||
/* We must NOT call SDL_SendMouseMotion() here or we will enter recursivity!
|
||||
That's why we move the cursor graphic ONLY. */
|
||||
if (mouse && mouse->cur_cursor && mouse->cur_cursor->driverdata) {
|
||||
curdata = (KMSDRM_CursorData *) mouse->cur_cursor->driverdata;
|
||||
drm_fd = KMSDRM_gbm_device_get_fd(KMSDRM_gbm_bo_get_device(curdata->bo));
|
||||
ret = KMSDRM_drmModeMoveCursor(drm_fd, curdata->crtc_id, mouse->x, mouse->y);
|
||||
/* In SDLPoP "QUIT?" menu, no more pageflips are generated, so no more atomic_commit() calls
|
||||
from SwapWindow(), causing the cursor movement requested here not to be seen on screen.
|
||||
Thus we have to do an atomic_commit() here, so requested movements are commited and seen. */
|
||||
drm_atomic_movecursor(curdata, mouse->x, mouse->y);
|
||||
ret = drm_atomic_commit(curdata->video, SDL_TRUE);
|
||||
|
||||
if (ret) {
|
||||
SDL_SetError("drmModeMoveCursor() failed.");
|
||||
}
|
||||
if (ret) {
|
||||
SDL_SetError("drm_atomic_movecursor() failed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,14 +29,6 @@
|
||||
#define MAX_CURSOR_W 512
|
||||
#define MAX_CURSOR_H 512
|
||||
|
||||
typedef struct _KMSDRM_CursorData
|
||||
{
|
||||
struct gbm_bo *bo;
|
||||
uint32_t crtc_id;
|
||||
int hot_x, hot_y;
|
||||
int w, h;
|
||||
} KMSDRM_CursorData;
|
||||
|
||||
extern void KMSDRM_InitMouse(_THIS);
|
||||
extern void KMSDRM_QuitMouse(_THIS);
|
||||
|
||||
|
||||
@@ -119,7 +119,7 @@ KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window)
|
||||
}
|
||||
|
||||
/* Add the pageflip to te request list. */
|
||||
drm_atomic_request_pageflip(_this, fb->fb_id);
|
||||
drm_atomic_setbuffer(_this, dispdata->display_plane, fb->fb_id);
|
||||
|
||||
/* Issue the one and only atomic commit where all changes will be requested!.
|
||||
We need e a non-blocking atomic commit for triple buffering, because we
|
||||
@@ -164,6 +164,7 @@ int
|
||||
KMSDRM_GLES_SwapWindowDB(_THIS, SDL_Window * window)
|
||||
{
|
||||
SDL_WindowData *windata = ((SDL_WindowData *) window->driverdata);
|
||||
SDL_DisplayData *dispdata = (SDL_DisplayData *) SDL_GetDisplayForWindow(window)->driverdata;
|
||||
KMSDRM_FBInfo *fb;
|
||||
int ret;
|
||||
|
||||
@@ -188,7 +189,7 @@ KMSDRM_GLES_SwapWindowDB(_THIS, SDL_Window * window)
|
||||
}
|
||||
|
||||
/* Add the pageflip to te request list. */
|
||||
drm_atomic_request_pageflip(_this, fb->fb_id);
|
||||
drm_atomic_setbuffer(_this, dispdata->display_plane, fb->fb_id);
|
||||
|
||||
/* Issue the one and only atomic commit where all changes will be requested!.
|
||||
Blocking for double buffering: won't return until completed. */
|
||||
|
||||
@@ -42,9 +42,22 @@ SDL_KMSDRM_SYM(void,drmModeFreeConnector,(drmModeConnectorPtr ptr))
|
||||
SDL_KMSDRM_SYM(void,drmModeFreeEncoder,(drmModeEncoderPtr ptr))
|
||||
SDL_KMSDRM_SYM(int,drmGetCap,(int fd, uint64_t capability, uint64_t *value))
|
||||
SDL_KMSDRM_SYM(drmModeResPtr,drmModeGetResources,(int fd))
|
||||
|
||||
SDL_KMSDRM_SYM(int,drmModeAddFB,(int fd, uint32_t width, uint32_t height, uint8_t depth,
|
||||
uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
|
||||
uint32_t *buf_id))
|
||||
|
||||
SDL_KMSDRM_SYM(int,drmModeAddFB2,(int fd, uint32_t width, uint32_t height,
|
||||
uint32_t pixel_format, const uint32_t bo_handles[4],
|
||||
const uint32_t pitches[4], const uint32_t offsets[4],
|
||||
uint32_t *buf_id, uint32_t flags))
|
||||
|
||||
SDL_KMSDRM_SYM(int,drmModeAddFB2WithModifiers,(int fd, uint32_t width, uint32_t height,
|
||||
uint32_t pixel_format, const uint32_t bo_handles[4],
|
||||
const uint32_t pitches[4], const uint32_t offsets[4],
|
||||
const uint64_t modifier[4], uint32_t *buf_id,
|
||||
uint32_t flags))
|
||||
|
||||
SDL_KMSDRM_SYM(int,drmModeRmFB,(int fd, uint32_t bufferId))
|
||||
SDL_KMSDRM_SYM(drmModeFBPtr,drmModeGetFB,(int fd, uint32_t buf))
|
||||
SDL_KMSDRM_SYM(drmModeCrtcPtr,drmModeGetCrtc,(int fd, uint32_t crtcId))
|
||||
@@ -94,6 +107,11 @@ SDL_KMSDRM_SYM(struct gbm_device *,gbm_create_device,(int fd))
|
||||
SDL_KMSDRM_SYM(unsigned int,gbm_bo_get_width,(struct gbm_bo *bo))
|
||||
SDL_KMSDRM_SYM(unsigned int,gbm_bo_get_height,(struct gbm_bo *bo))
|
||||
SDL_KMSDRM_SYM(uint32_t,gbm_bo_get_stride,(struct gbm_bo *bo))
|
||||
SDL_KMSDRM_SYM(uint32_t,gbm_bo_get_stride_for_plane,(struct gbm_bo *bo,int plane))
|
||||
SDL_KMSDRM_SYM(uint32_t,gbm_bo_get_format,(struct gbm_bo *bo))
|
||||
SDL_KMSDRM_SYM(uint32_t,gbm_bo_get_offset,(struct gbm_bo *bo, int plane))
|
||||
SDL_KMSDRM_SYM(int,gbm_bo_get_plane_count,(struct gbm_bo *bo))
|
||||
|
||||
SDL_KMSDRM_SYM(union gbm_bo_handle,gbm_bo_get_handle,(struct gbm_bo *bo))
|
||||
SDL_KMSDRM_SYM(int,gbm_bo_write,(struct gbm_bo *bo, const void *buf, size_t count))
|
||||
SDL_KMSDRM_SYM(struct gbm_device *,gbm_bo_get_device,(struct gbm_bo *bo))
|
||||
|
||||
+293
-217
File diff suppressed because it is too large
Load Diff
@@ -54,37 +54,37 @@ typedef struct SDL_DisplayModeData
|
||||
int mode_index;
|
||||
} SDL_DisplayModeData;
|
||||
|
||||
struct plane {
|
||||
drmModePlane *plane;
|
||||
drmModeObjectProperties *props;
|
||||
drmModePropertyRes **props_info;
|
||||
};
|
||||
|
||||
struct crtc {
|
||||
drmModeCrtc *crtc;
|
||||
drmModeObjectProperties *props;
|
||||
drmModePropertyRes **props_info;
|
||||
};
|
||||
|
||||
struct connector {
|
||||
drmModeConnector *connector;
|
||||
drmModeObjectProperties *props;
|
||||
drmModePropertyRes **props_info;
|
||||
};
|
||||
|
||||
typedef struct SDL_DisplayData
|
||||
{
|
||||
|
||||
drmModeModeInfo mode;
|
||||
uint32_t plane_id;
|
||||
uint32_t cusor_plane_id;
|
||||
uint32_t crtc_id;
|
||||
uint32_t connector_id;
|
||||
uint32_t atomic_flags;
|
||||
|
||||
/* All changes will be requested via this one and only atomic request,
|
||||
that will be sent to the kernel in the one and only atomic_commit() call
|
||||
that takes place in SwapWindow(). */
|
||||
drmModeAtomicReq *atomic_req;
|
||||
|
||||
drmModePlane *plane;
|
||||
drmModeObjectProperties *plane_props;
|
||||
drmModePropertyRes **plane_props_info;
|
||||
|
||||
drmModePlane *cursor_plane;
|
||||
drmModeObjectProperties *cursor_plane_props;
|
||||
drmModePropertyRes **cursor_plane_props_info;
|
||||
|
||||
drmModeCrtc *crtc;
|
||||
drmModeObjectProperties *crtc_props;
|
||||
drmModePropertyRes **crtc_props_info;
|
||||
|
||||
drmModeConnector *connector;
|
||||
drmModeObjectProperties *connector_props;
|
||||
drmModePropertyRes **connector_props_info;
|
||||
struct plane *display_plane;
|
||||
struct plane *cursor_plane;
|
||||
struct crtc *crtc;
|
||||
struct connector *connector;
|
||||
|
||||
int kms_in_fence_fd;
|
||||
int kms_out_fence_fd;
|
||||
@@ -114,15 +114,31 @@ typedef struct KMSDRM_FBInfo
|
||||
uint32_t fb_id; /* DRM framebuffer ID */
|
||||
} KMSDRM_FBInfo;
|
||||
|
||||
/* Driver-side info about the cursor. It's here so we know about it in SDL_kmsdrmvideo.c
|
||||
because drm_atomic_setcursor() receives a KMSDRM_CursorData parameter as a cursor. */
|
||||
typedef struct _KMSDRM_CursorData
|
||||
{
|
||||
struct gbm_bo *bo;
|
||||
uint32_t crtc_id;
|
||||
int hot_x, hot_y;
|
||||
int w, h;
|
||||
/* The video devide implemented on SDL_kmsdrmvideo.c
|
||||
* to be used as _THIS pointer in SDL_kmsdrmvideo.c
|
||||
* functions that need it. */
|
||||
SDL_VideoDevice *video;
|
||||
} KMSDRM_CursorData;
|
||||
|
||||
/* Helper functions */
|
||||
int KMSDRM_CreateSurfaces(_THIS, SDL_Window * window);
|
||||
KMSDRM_FBInfo *KMSDRM_FBFromBO(_THIS, struct gbm_bo *bo);
|
||||
|
||||
/* Atomic functions that are used from SDL_kmsdrmopengles.c */
|
||||
void drm_atomic_request_modeset(_THIS);
|
||||
void drm_atomic_request_pageflip(_THIS, uint32_t fb_id);
|
||||
/* Atomic functions that are used from SDL_kmsdrmopengles.c and SDL_kmsdrmmouse.c */
|
||||
void drm_atomic_modeset(_THIS, int mode_index);
|
||||
void drm_atomic_setbuffer(_THIS, struct plane *plane, uint32_t fb_id);
|
||||
void drm_atomic_waitpending(_THIS);
|
||||
int drm_atomic_commit(_THIS, SDL_bool blocking);
|
||||
void drm_atomic_wait_pending(_THIS);
|
||||
int drm_atomic_setcursor(KMSDRM_CursorData *curdata, int x, int y);
|
||||
int drm_atomic_movecursor(KMSDRM_CursorData *curdata, int x, int y);
|
||||
|
||||
/****************************************************************************/
|
||||
/* SDL_VideoDevice functions declaration */
|
||||
|
||||
Reference in New Issue
Block a user