Fixed bug 3980 - Fix for KMSDRM driver where cursor would not be shown on some gfx hardware because of unsupported cursor size Manuel Alfayate Corchete This fixes a problem with KMSDRM on some graphics hardware where only bigger cursor sizes are supported, such as current Intel gfx. (The kernel-side driver is what limits this: had to look for failing IOCTLs...) That caused SDL_SetCursor() to fail silently, and we were left with a missing cursor without further explanation. With this patch, different "standard" sizes are tried and a bigger one is used (with an intermediate and clean buffer only used to write the new cursor to the BO where it will live after) if we get, let's say, 16x16 which is pretty common but our hardware does not support that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
diff --git a/src/video/kmsdrm/SDL_kmsdrmmouse.c b/src/video/kmsdrm/SDL_kmsdrmmouse.c
index f44402a..f834339 100644
--- a/src/video/kmsdrm/SDL_kmsdrmmouse.c
+++ b/src/video/kmsdrm/SDL_kmsdrmmouse.c
@@ -44,6 +44,40 @@ KMSDRM_CreateDefaultCursor(void)
return SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH, DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY);
}
+/* Evaluate if a given cursor size is supported or not. Notably, current Intel gfx only support 64x64 and up. */
+static SDL_bool
+KMSDRM_IsCursorSizeSupported (int w, int h, uint32_t bo_format) {
+
+ SDL_VideoDevice *dev = SDL_GetVideoDevice();
+ SDL_VideoData *vdata = ((SDL_VideoData *)dev->driverdata);
+ int ret;
+ uint32_t bo_handle;
+ struct gbm_bo *bo = KMSDRM_gbm_bo_create(vdata->gbm, w, h, bo_format,
+ GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
+
+ if (bo == NULL) {
+ SDL_SetError("Could not create GBM cursor BO width size %dx%d for size testing", w, h);
+ goto cleanup;
+ }
+
+ bo_handle = KMSDRM_gbm_bo_get_handle(bo).u32;
+ ret = KMSDRM_drmModeSetCursor(vdata->drm_fd, vdata->crtc_id, bo_handle, w, h);
+
+ if (ret) {
+ goto cleanup;
+ }
+ else {
+ KMSDRM_gbm_bo_destroy(bo);
+ return SDL_TRUE;
+ }
+
+cleanup:
+ if (bo != NULL) {
+ KMSDRM_gbm_bo_destroy(bo);
+ }
+ return SDL_FALSE;
+}
+
/* Create a cursor from a surface */
static SDL_Cursor *
KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
@@ -53,7 +87,8 @@ KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
SDL_PixelFormat *pixlfmt = surface->format;
KMSDRM_CursorData *curdata;
SDL_Cursor *cursor;
- int i, ret;
+ SDL_bool cursor_supported = SDL_FALSE;
+ int i, ret, usable_cursor_w, usable_cursor_h;
uint32_t bo_format, bo_stride;
char *buffer = NULL;
size_t bufsize;
@@ -143,20 +178,43 @@ KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
return NULL;
}
+ /* We have to know beforehand if a cursor with the same size as the surface is supported.
+ * If it's not, we have to find an usable cursor size and use an intermediate and clean buffer.
+ * If we can't find a cursor size supported by the hardware, we won't go on trying to
+ * call SDL_SetCursor() later. */
+
+ usable_cursor_w = surface->w;
+ usable_cursor_h = surface->h;
+
+ while (usable_cursor_w <= MAX_CURSOR_W && usable_cursor_h <= MAX_CURSOR_H) {
+ if (KMSDRM_IsCursorSizeSupported(usable_cursor_w, usable_cursor_h, bo_format)) {
+ cursor_supported = SDL_TRUE;
+ break;
+ }
+ usable_cursor_w += usable_cursor_w;
+ usable_cursor_h += usable_cursor_h;
+ }
+
+ if (!cursor_supported) {
+ SDL_SetError("Could not find a cursor size supported by the kernel driver");
+ goto cleanup;
+ }
+
curdata->hot_x = hot_x;
curdata->hot_y = hot_y;
- curdata->w = surface->w;
- curdata->h = surface->h;
+ curdata->w = usable_cursor_w;
+ curdata->h = usable_cursor_h;
- curdata->bo = KMSDRM_gbm_bo_create(vdata->gbm, surface->w, surface->h, bo_format,
+ curdata->bo = KMSDRM_gbm_bo_create(vdata->gbm, usable_cursor_w, usable_cursor_h, bo_format,
GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
+
if (curdata->bo == NULL) {
SDL_SetError("Could not create GBM cursor BO");
goto cleanup;
}
bo_stride = KMSDRM_gbm_bo_get_stride(curdata->bo);
- bufsize = bo_stride * surface->h;
+ bufsize = bo_stride * curdata->h;
if (surface->pitch != bo_stride) {
/* pitch doesn't match stride, must be copied to temp buffer */
@@ -173,6 +231,9 @@ KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
}
}
+ /* Clean the whole temporary buffer */
+ SDL_memset(buffer, 0x00, bo_stride * curdata->h);
+
/* Copy to temporary buffer */
for (i = 0; i < surface->h; i++) {
SDL_memcpy(buffer + (i * bo_stride),
diff --git a/src/video/kmsdrm/SDL_kmsdrmmouse.h b/src/video/kmsdrm/SDL_kmsdrmmouse.h
index 05c7288..ef4d817 100644
--- a/src/video/kmsdrm/SDL_kmsdrmmouse.h
+++ b/src/video/kmsdrm/SDL_kmsdrmmouse.h
@@ -26,6 +26,9 @@
#include <gbm.h>
+#define MAX_CURSOR_W 512
+#define MAX_CURSOR_H 512
+
typedef struct _KMSDRM_CursorData
{
struct gbm_bo *bo;
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index 9c1b291..73f6f60 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -362,6 +362,7 @@ KMSDRM_VideoInit(_THIS)
vdata->saved_crtc->y, vdata->saved_crtc->width, vdata->saved_crtc->height);
data->crtc_id = encoder->crtc_id;
data->cur_mode = vdata->saved_crtc->mode;
+ vdata->crtc_id = encoder->crtc_id;
SDL_zero(current_mode);
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.h b/src/video/kmsdrm/SDL_kmsdrmvideo.h
index c4735e4..26ce0bb 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.h
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.h
@@ -45,6 +45,7 @@ typedef struct SDL_VideoData
struct pollfd drm_pollfd; /* pollfd containing DRM file desc */
drmModeCrtc *saved_crtc; /* Saved CRTC to restore on quit */
uint32_t saved_conn_id; /* Saved DRM connector ID */
+ uint32_t crtc_id; /* CRTC in use */
} SDL_VideoData;