Skip to content

Commit

Permalink
kmsdrm: move hardware cursor functionality to the ATOMIC interface. D…
Browse files Browse the repository at this point in the history
…isconnect the display plane from the GBM surface buffers before destroying the GBM surface.
  • Loading branch information
vanfanel committed Aug 17, 2020
1 parent c3ecf18 commit 92cb919
Show file tree
Hide file tree
Showing 6 changed files with 436 additions and 320 deletions.
149 changes: 81 additions & 68 deletions src/video/kmsdrm/SDL_kmsdrmmouse.c
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -196,18 +210,21 @@ KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
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) {
Expand All @@ -221,90 +238,86 @@ 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.");
}
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;

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);
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;

SDL_free(cursor->driverdata);
SDL_free(cursor);
}
SDL_free(cursor);
}
}

Expand All @@ -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.");
}
Expand Down Expand Up @@ -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);

if (ret) {
SDL_SetError("drmModeMoveCursor() failed.");
}
/* 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("drm_atomic_movecursor() failed.");
}
}
}

Expand Down
8 changes: 0 additions & 8 deletions src/video/kmsdrm/SDL_kmsdrmmouse.h
Expand Up @@ -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);

Expand Down
5 changes: 3 additions & 2 deletions src/video/kmsdrm/SDL_kmsdrmopengles.c
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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. */
Expand Down
18 changes: 18 additions & 0 deletions src/video/kmsdrm/SDL_kmsdrmsym.h
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 92cb919

Please sign in to comment.