kms_vaapi: support multiple drm planes (fixes capture on some multi monitor systems)

This commit is contained in:
dec05eba 2023-05-12 03:49:29 +02:00
parent 25af1c81d0
commit 99dbbd07ab
7 changed files with 356 additions and 149 deletions

2
TODO
View File

@ -63,3 +63,5 @@ When using multiple monitors kms grab the target monitor instead of the whole sc
Enable opus/flac again. It's broken right now when merging audio inputs. The audio gets a lot of static noise! Enable opus/flac again. It's broken right now when merging audio inputs. The audio gets a lot of static noise!
Support vp8/vp9/av1. This is especially important on amd which on some distros (such as Manjaro) where hardware accelerated h264/hevc is disabled in the mesa package. Support vp8/vp9/av1. This is especially important on amd which on some distros (such as Manjaro) where hardware accelerated h264/hevc is disabled in the mesa package.
Support screen (all monitors) capture on amd/intel when no combined plane is found.

View File

@ -55,7 +55,7 @@ static int recv_msg_from_server(int server_fd, gsr_kms_response *response) {
response_message.msg_iov = &iov; response_message.msg_iov = &iov;
response_message.msg_iovlen = 1; response_message.msg_iovlen = 1;
char cmsgbuf[CMSG_SPACE(sizeof(int))]; char cmsgbuf[CMSG_SPACE(sizeof(int) * GSR_KMS_MAX_PLANES)];
memset(cmsgbuf, 0, sizeof(cmsgbuf)); memset(cmsgbuf, 0, sizeof(cmsgbuf));
response_message.msg_control = cmsgbuf; response_message.msg_control = cmsgbuf;
response_message.msg_controllen = sizeof(cmsgbuf); response_message.msg_controllen = sizeof(cmsgbuf);
@ -64,15 +64,17 @@ static int recv_msg_from_server(int server_fd, gsr_kms_response *response) {
if(res <= 0) if(res <= 0)
return res; return res;
if(response->result == KMS_RESULT_OK) if(response->num_fds > 0) {
response->data.fd.fd = 0; struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message);
if(cmsg) {
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message); int *fds = (int*)CMSG_DATA(cmsg);
if(cmsg) { for(int i = 0; i < response->num_fds; ++i) {
if(cmsg->cmsg_type == SCM_RIGHTS) { response->fds[i].fd = fds[i];
int kms_fd = 0; }
memcpy(&kms_fd, CMSG_DATA(cmsg), sizeof(int)); } else {
response->data.fd.fd = kms_fd; for(int i = 0; i < response->num_fds; ++i) {
response->fds[i].fd = 0;
}
} }
} }
@ -246,7 +248,7 @@ void gsr_kms_client_deinit(gsr_kms_client *self) {
int gsr_kms_client_get_kms(gsr_kms_client *self, gsr_kms_response *response) { int gsr_kms_client_get_kms(gsr_kms_client *self, gsr_kms_response *response) {
response->result = KMS_RESULT_FAILED_TO_SEND; response->result = KMS_RESULT_FAILED_TO_SEND;
strcpy(response->data.err_msg, "failed to send"); strcpy(response->err_msg, "failed to send");
gsr_kms_request request; gsr_kms_request request;
request.type = KMS_REQUEST_TYPE_GET_KMS; request.type = KMS_REQUEST_TYPE_GET_KMS;

View File

@ -2,6 +2,9 @@
#define GSR_KMS_SHARED_H #define GSR_KMS_SHARED_H
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
#define GSR_KMS_MAX_PLANES 32
typedef enum { typedef enum {
KMS_REQUEST_TYPE_GET_KMS KMS_REQUEST_TYPE_GET_KMS
@ -26,14 +29,15 @@ typedef struct {
uint32_t offset; uint32_t offset;
uint32_t pixel_format; uint32_t pixel_format;
uint64_t modifier; uint64_t modifier;
uint32_t connector_id; /* 0 if unknown */
bool is_combined_plane;
} gsr_kms_response_fd; } gsr_kms_response_fd;
typedef struct { typedef struct {
int result; /* gsr_kms_result */ int result; /* gsr_kms_result */
union { char err_msg[128];
char err_msg[128]; gsr_kms_response_fd fds[GSR_KMS_MAX_PLANES];
gsr_kms_response_fd fd; int num_fds;
} data;
} gsr_kms_response; } gsr_kms_response;
#endif /* #define GSR_KMS_SHARED_H */ #endif /* #define GSR_KMS_SHARED_H */

View File

@ -15,18 +15,30 @@
#include <xf86drm.h> #include <xf86drm.h>
#include <xf86drmMode.h> #include <xf86drmMode.h>
#define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2 #define MAX_CONNECTORS 32
typedef struct { typedef struct {
int drmfd; int drmfd;
uint32_t plane_id; uint32_t plane_ids[GSR_KMS_MAX_PLANES];
uint32_t connector_ids[GSR_KMS_MAX_PLANES];
size_t num_plane_ids;
} gsr_drm; } gsr_drm;
typedef struct {
uint32_t connector_id;
uint64_t crtc_id;
} connector_crtc_pair;
typedef struct {
connector_crtc_pair maps[MAX_CONNECTORS];
int num_maps;
} connector_to_crtc_map;
static int max_int(int a, int b) { static int max_int(int a, int b) {
return a > b ? a : b; return a > b ? a : b;
} }
static int send_msg_to_client(int client_fd, gsr_kms_response *response, int *fds, int num_fds) { static int send_msg_to_client(int client_fd, gsr_kms_response *response) {
struct iovec iov; struct iovec iov;
iov.iov_base = response; iov.iov_base = response;
iov.iov_len = sizeof(*response); iov.iov_len = sizeof(*response);
@ -35,52 +47,95 @@ static int send_msg_to_client(int client_fd, gsr_kms_response *response, int *fd
response_message.msg_iov = &iov; response_message.msg_iov = &iov;
response_message.msg_iovlen = 1; response_message.msg_iovlen = 1;
char cmsgbuf[CMSG_SPACE(sizeof(int)) * max_int(1, num_fds)]; char cmsgbuf[CMSG_SPACE(sizeof(int) * max_int(1, response->num_fds))];
memset(cmsgbuf, 0, sizeof(cmsgbuf)); memset(cmsgbuf, 0, sizeof(cmsgbuf));
if(num_fds > 0) { if(response->num_fds > 0) {
response_message.msg_control = cmsgbuf; response_message.msg_control = cmsgbuf;
response_message.msg_controllen = sizeof(cmsgbuf); response_message.msg_controllen = sizeof(cmsgbuf);
int total_msg_len = 0; struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message);
struct cmsghdr *cmsg = NULL; cmsg->cmsg_level = SOL_SOCKET;
for(int i = 0; i < num_fds; ++i) { cmsg->cmsg_type = SCM_RIGHTS;
if(i == 0) cmsg->cmsg_len = CMSG_LEN(sizeof(int) * response->num_fds);
cmsg = CMSG_FIRSTHDR(&response_message);
else
cmsg = CMSG_NXTHDR(&response_message, cmsg);
cmsg->cmsg_level = SOL_SOCKET; int *fds = (int*)CMSG_DATA(cmsg);
cmsg->cmsg_type = SCM_RIGHTS; for(int i = 0; i < response->num_fds; ++i) {
cmsg->cmsg_len = CMSG_LEN(sizeof(int)); fds[i] = response->fds[i].fd;
memcpy(CMSG_DATA(cmsg), &fds[i], sizeof(int));
total_msg_len += cmsg->cmsg_len;
} }
response_message.msg_controllen = total_msg_len; response_message.msg_controllen = cmsg->cmsg_len;
} }
return sendmsg(client_fd, &response_message, 0); return sendmsg(client_fd, &response_message, 0);
} }
static int kms_get_plane_id(gsr_drm *drm) { static bool connector_get_property_by_name(int drmfd, drmModeConnectorPtr props, const char *name, uint64_t *result) {
for(int i = 0; i < props->count_props; ++i) {
drmModePropertyPtr prop = drmModeGetProperty(drmfd, props->props[i]);
if(prop) {
if(strcmp(name, prop->name) == 0) {
*result = props->prop_values[i];
drmModeFreeProperty(prop);
return true;
}
drmModeFreeProperty(prop);
}
}
return false;
}
/* Returns 0 if not found */
static uint32_t get_connector_by_crtc_id(const connector_to_crtc_map *c2crtc_map, uint32_t crtc_id) {
for(int i = 0; i < c2crtc_map->num_maps; ++i) {
if(c2crtc_map->maps[i].crtc_id == crtc_id)
return c2crtc_map->maps[i].connector_id;
}
return 0;
}
static int kms_get_plane_ids(gsr_drm *drm) {
drmModePlaneResPtr planes = NULL; drmModePlaneResPtr planes = NULL;
drmModeResPtr resources = NULL;
int result = -1; int result = -1;
int64_t max_size = 0;
uint32_t best_plane_match = UINT32_MAX; connector_to_crtc_map c2crtc_map;
c2crtc_map.num_maps = 0;
if(drmSetClientCap(drm->drmfd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) != 0) { if(drmSetClientCap(drm->drmfd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1) != 0) {
fprintf(stderr, "kms server error: drmSetClientCap failed, error: %s\n", strerror(errno)); fprintf(stderr, "kms server error: drmSetClientCap DRM_CLIENT_CAP_UNIVERSAL_PLANES failed, error: %s\n", strerror(errno));
goto error; goto error;
} }
if(drmSetClientCap(drm->drmfd, DRM_CLIENT_CAP_ATOMIC, 1) != 0) {
fprintf(stderr, "kms server warning: drmSetClientCap DRM_CLIENT_CAP_ATOMIC failed, error: %s. The wrong monitor may be captured as a result\n", strerror(errno));
}
planes = drmModeGetPlaneResources(drm->drmfd); planes = drmModeGetPlaneResources(drm->drmfd);
if(!planes) { if(!planes) {
fprintf(stderr, "kms server error: failed to access planes, error: %s\n", strerror(errno)); fprintf(stderr, "kms server error: failed to access planes, error: %s\n", strerror(errno));
goto error; goto error;
} }
for(uint32_t i = 0; i < planes->count_planes; ++i) { resources = drmModeGetResources(drm->drmfd);
if(resources) {
for(int i = 0; i < resources->count_connectors && c2crtc_map.num_maps < MAX_CONNECTORS; ++i) {
drmModeConnectorPtr connector = drmModeGetConnectorCurrent(drm->drmfd, resources->connectors[i]);
if(connector) {
uint64_t crtc_id = 0;
connector_get_property_by_name(drm->drmfd, connector, "CRTC_ID", &crtc_id);
c2crtc_map.maps[c2crtc_map.num_maps].connector_id = connector->connector_id;
c2crtc_map.maps[c2crtc_map.num_maps].crtc_id = crtc_id;
++c2crtc_map.num_maps;
drmModeFreeConnector(connector);
}
}
drmModeFreeResources(resources);
}
for(uint32_t i = 0; i < planes->count_planes && drm->num_plane_ids < GSR_KMS_MAX_PLANES; ++i) {
drmModePlanePtr plane = drmModeGetPlane(drm->drmfd, planes->planes[i]); drmModePlanePtr plane = drmModeGetPlane(drm->drmfd, planes->planes[i]);
if(!plane) { if(!plane) {
fprintf(stderr, "kms server warning: failed to get drmModePlanePtr for plane %#x: %s (%d)\n", planes->planes[i], strerror(errno), errno); fprintf(stderr, "kms server warning: failed to get drmModePlanePtr for plane %#x: %s (%d)\n", planes->planes[i], strerror(errno), errno);
@ -95,22 +150,14 @@ static int kms_get_plane_id(gsr_drm *drm) {
// TODO: Fallback to getfb(1)? // TODO: Fallback to getfb(1)?
drmModeFB2Ptr drmfb = drmModeGetFB2(drm->drmfd, plane->fb_id); drmModeFB2Ptr drmfb = drmModeGetFB2(drm->drmfd, plane->fb_id);
if(drmfb) { if(drmfb) {
const int64_t plane_size = (int64_t)drmfb->width * (int64_t)drmfb->height; drm->plane_ids[drm->num_plane_ids] = plane->plane_id;
if(drmfb->handles[0] && plane_size >= max_size) { drm->connector_ids[drm->num_plane_ids] = get_connector_by_crtc_id(&c2crtc_map, plane->crtc_id);
max_size = plane_size; ++drm->num_plane_ids;
best_plane_match = plane->plane_id;
}
drmModeFreeFB2(drmfb); drmModeFreeFB2(drmfb);
} }
drmModeFreePlane(plane); drmModeFreePlane(plane);
} }
if(best_plane_match == UINT32_MAX || max_size == 0) {
fprintf(stderr, "kms server error: failed to find a usable plane\n");
goto error;
}
drm->plane_id = best_plane_match;
result = 0; result = 0;
error: error:
@ -120,66 +167,88 @@ static int kms_get_plane_id(gsr_drm *drm) {
return result; return result;
} }
static bool drmfb_has_multiple_handles(drmModeFB2 *drmfb) {
int num_handles = 0;
for(uint32_t handle_index = 0; handle_index < 4 && drmfb->handles[handle_index]; ++handle_index) {
++num_handles;
}
return num_handles > 1;
}
static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response) { static int kms_get_fb(gsr_drm *drm, gsr_kms_response *response) {
drmModePlanePtr plane = NULL;
drmModeFB2 *drmfb = NULL;
int result = -1; int result = -1;
response->result = KMS_RESULT_OK; response->result = KMS_RESULT_OK;
response->data.fd.fd = 0; response->err_msg[0] = '\0';
response->data.fd.width = 0; response->num_fds = 0;
response->data.fd.height = 0;
plane = drmModeGetPlane(drm->drmfd, drm->plane_id); for(size_t i = 0; i < drm->num_plane_ids && response->num_fds < GSR_KMS_MAX_PLANES; ++i) {
if(!plane) { drmModePlanePtr plane = NULL;
response->result = KMS_RESULT_FAILED_TO_GET_PLANE; drmModeFB2 *drmfb = NULL;
snprintf(response->data.err_msg, sizeof(response->data.err_msg), "failed to get drm plane with id %u, error: %s\n", drm->plane_id, strerror(errno));
fprintf(stderr, "kms server error: %s\n", response->data.err_msg); plane = drmModeGetPlane(drm->drmfd, drm->plane_ids[i]);
goto error; if(!plane) {
response->result = KMS_RESULT_FAILED_TO_GET_PLANE;
snprintf(response->err_msg, sizeof(response->err_msg), "failed to get drm plane with id %u, error: %s\n", drm->plane_ids[i], strerror(errno));
fprintf(stderr, "kms server error: %s\n", response->err_msg);
goto next;
}
drmfb = drmModeGetFB2(drm->drmfd, plane->fb_id);
if(!drmfb) {
// Commented out for now because we get here if the cursor is moved to another monitor and we dont care about the cursor
//response->result = KMS_RESULT_FAILED_TO_GET_PLANE;
//snprintf(response->err_msg, sizeof(response->err_msg), "drmModeGetFB2 failed, error: %s", strerror(errno));
//fprintf(stderr, "kms server error: %s\n", response->err_msg);
goto next;
}
if(!drmfb->handles[0]) {
response->result = KMS_RESULT_FAILED_TO_GET_PLANE;
snprintf(response->err_msg, sizeof(response->err_msg), "drmfb handle is NULL");
fprintf(stderr, "kms server error: %s\n", response->err_msg);
goto next;
}
// TODO: Check if dimensions have changed by comparing width and height to previous time this was called.
// TODO: Support other plane formats than rgb (with multiple planes, such as direct YUV420 on wayland).
int fb_fd = -1;
const int ret = drmPrimeHandleToFD(drm->drmfd, drmfb->handles[0], O_RDONLY, &fb_fd);
if(ret != 0 || fb_fd == -1) {
response->result = KMS_RESULT_FAILED_TO_GET_PLANE;
snprintf(response->err_msg, sizeof(response->err_msg), "failed to get fd from drm handle, error: %s", strerror(errno));
fprintf(stderr, "kms server error: %s\n", response->err_msg);
continue;
}
response->fds[response->num_fds].fd = fb_fd;
response->fds[response->num_fds].width = drmfb->width;
response->fds[response->num_fds].height = drmfb->height;
response->fds[response->num_fds].pitch = drmfb->pitches[0];
response->fds[response->num_fds].offset = drmfb->offsets[0];
response->fds[response->num_fds].pixel_format = drmfb->pixel_format;
response->fds[response->num_fds].modifier = drmfb->modifier;
response->fds[response->num_fds].connector_id = drm->connector_ids[i];
response->fds[response->num_fds].is_combined_plane = drmfb_has_multiple_handles(drmfb);
++response->num_fds;
next:
if(drmfb)
drmModeFreeFB2(drmfb);
if(plane)
drmModeFreePlane(plane);
} }
drmfb = drmModeGetFB2(drm->drmfd, plane->fb_id); if(response->num_fds > 0 || response->result == KMS_RESULT_OK) {
if(!drmfb) { result = 0;
response->result = KMS_RESULT_FAILED_TO_GET_PLANE; } else {
snprintf(response->data.err_msg, sizeof(response->data.err_msg), "drmModeGetFB2 failed, error: %s", strerror(errno)); for(int i = 0; i < response->num_fds; ++i) {
fprintf(stderr, "kms server error: %s\n", response->data.err_msg); close(response->fds[i].fd);
goto error; }
response->num_fds = 0;
} }
if(!drmfb->handles[0]) {
response->result = KMS_RESULT_FAILED_TO_GET_PLANE;
snprintf(response->data.err_msg, sizeof(response->data.err_msg), "drmfb handle is NULL");
fprintf(stderr, "kms server error: %s\n", response->data.err_msg);
goto error;
}
// TODO: Check if dimensions have changed by comparing width and height to previous time this was called.
// TODO: Support other plane formats than rgb (with multiple planes, such as direct YUV420 on wayland).
int fb_fd = -1;
const int ret = drmPrimeHandleToFD(drm->drmfd, drmfb->handles[0], O_RDONLY, &fb_fd);
if(ret != 0 || fb_fd == -1) {
response->result = KMS_RESULT_FAILED_TO_GET_PLANE;
snprintf(response->data.err_msg, sizeof(response->data.err_msg), "failed to get fd from drm handle, error: %s", strerror(errno));
fprintf(stderr, "kms server error: %s\n", response->data.err_msg);
goto error;
}
response->data.fd.fd = fb_fd;
response->data.fd.width = drmfb->width;
response->data.fd.height = drmfb->height;
response->data.fd.pitch = drmfb->pitches[0];
response->data.fd.offset = drmfb->offsets[0];
response->data.fd.pixel_format = drmfb->pixel_format;
response->data.fd.modifier = drmfb->modifier;
result = 0;
error:
if(drmfb)
drmModeFreeFB2(drmfb);
if(plane)
drmModeFreePlane(plane);
return result; return result;
} }
@ -207,14 +276,14 @@ int main(int argc, char **argv) {
const char *card_path = argv[2]; const char *card_path = argv[2];
gsr_drm drm; gsr_drm drm;
drm.plane_id = 0; drm.num_plane_ids = 0;
drm.drmfd = open(card_path, O_RDONLY); drm.drmfd = open(card_path, O_RDONLY);
if(drm.drmfd < 0) { if(drm.drmfd < 0) {
fprintf(stderr, "kms server error: failed to open %s, error: %s", card_path, strerror(errno)); fprintf(stderr, "kms server error: failed to open %s, error: %s", card_path, strerror(errno));
return 2; return 2;
} }
if(kms_get_plane_id(&drm) != 0) { if(kms_get_plane_ids(&drm) != 0) {
close(drm.drmfd); close(drm.drmfd);
return 2; return 2;
} }
@ -284,11 +353,14 @@ int main(int argc, char **argv) {
gsr_kms_response response; gsr_kms_response response;
if(kms_get_fb(&drm, &response) == 0) { if(kms_get_fb(&drm, &response) == 0) {
if(send_msg_to_client(socket_fd, &response, &response.data.fd.fd, 1) == -1) if(send_msg_to_client(socket_fd, &response) == -1)
fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n"); fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n");
close(response.data.fd.fd);
for(int i = 0; i < response.num_fds; ++i) {
close(response.fds[i].fd);
}
} else { } else {
if(send_msg_to_client(socket_fd, &response, NULL, 0) == -1) if(send_msg_to_client(socket_fd, &response) == -1)
fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n"); fprintf(stderr, "kms server error: failed to respond to client KMS_REQUEST_TYPE_GET_KMS request\n");
} }
@ -297,9 +369,9 @@ int main(int argc, char **argv) {
default: { default: {
gsr_kms_response response; gsr_kms_response response;
response.result = KMS_RESULT_INVALID_REQUEST; response.result = KMS_RESULT_INVALID_REQUEST;
snprintf(response.data.err_msg, sizeof(response.data.err_msg), "invalid request type %d, expected %d (%s)", request.type, KMS_REQUEST_TYPE_GET_KMS, "KMS_REQUEST_TYPE_GET_KMS"); snprintf(response.err_msg, sizeof(response.err_msg), "invalid request type %d, expected %d (%s)", request.type, KMS_REQUEST_TYPE_GET_KMS, "KMS_REQUEST_TYPE_GET_KMS");
fprintf(stderr, "kms server error: %s\n", response.data.err_msg); fprintf(stderr, "kms server error: %s\n", response.err_msg);
if(send_msg_to_client(socket_fd, &response, NULL, 0) == -1) { if(send_msg_to_client(socket_fd, &response) == -1) {
fprintf(stderr, "kms server error: failed to respond to client request\n"); fprintf(stderr, "kms server error: failed to respond to client request\n");
break; break;
} }

View File

@ -9,6 +9,7 @@
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <libavutil/hwcontext.h> #include <libavutil/hwcontext.h>
#include <libavutil/hwcontext_vaapi.h> #include <libavutil/hwcontext_vaapi.h>
#include <libavutil/frame.h> #include <libavutil/frame.h>
@ -16,6 +17,13 @@
#include <va/va.h> #include <va/va.h>
#include <va/va_drmcommon.h> #include <va/va_drmcommon.h>
#define MAX_CONNECTOR_IDS 32
typedef struct {
uint32_t connector_ids[MAX_CONNECTOR_IDS];
int num_connector_ids;
} MonitorId;
typedef enum { typedef enum {
X11_ROT_0 = 1 << 0, X11_ROT_0 = 1 << 0,
X11_ROT_90 = 1 << 1, X11_ROT_90 = 1 << 1,
@ -35,17 +43,12 @@ typedef struct {
gsr_egl egl; gsr_egl egl;
gsr_kms_client kms_client; gsr_kms_client kms_client;
gsr_kms_response kms_response;
uint32_t fourcc;
uint64_t modifiers;
int dmabuf_fd;
uint32_t pitch;
uint32_t offset;
vec2i kms_size;
vec2i capture_pos; vec2i capture_pos;
vec2i capture_size; vec2i capture_size;
bool screen_capture; bool screen_capture;
MonitorId monitor_id;
VADisplay va_dpy; VADisplay va_dpy;
@ -114,25 +117,77 @@ static bool drm_create_codec_context(gsr_capture_kms_vaapi *cap_kms, AVCodecCont
// TODO: On monitor reconfiguration, find monitor x, y, width and height again. Do the same for nvfbc. // TODO: On monitor reconfiguration, find monitor x, y, width and height again. Do the same for nvfbc.
typedef struct { typedef struct {
gsr_capture_kms_vaapi *cap_kms;
const Atom randr_connector_id_atom;
const char *monitor_to_capture;
int monitor_to_capture_len;
int num_monitors; int num_monitors;
int rotation; int rotation;
} MonitorCallbackUserdata; } MonitorCallbackUserdata;
static bool properties_has_atom(Atom *props, int nprop, Atom atom) {
for(int i = 0; i < nprop; ++i) {
if(props[i] == atom)
return true;
}
return false;
}
static void monitor_callback(const XRROutputInfo *output_info, const XRRCrtcInfo *crt_info, const XRRModeInfo *mode_info, void *userdata) { static void monitor_callback(const XRROutputInfo *output_info, const XRRCrtcInfo *crt_info, const XRRModeInfo *mode_info, void *userdata) {
MonitorCallbackUserdata *monitor_callback_userdata = userdata; MonitorCallbackUserdata *monitor_callback_userdata = userdata;
monitor_callback_userdata->rotation = crt_info->rotation;
++monitor_callback_userdata->num_monitors; ++monitor_callback_userdata->num_monitors;
if(monitor_callback_userdata->monitor_to_capture_len != output_info->nameLen || memcmp(monitor_callback_userdata->monitor_to_capture, output_info->name, output_info->nameLen) != 0)
return;
monitor_callback_userdata->rotation = crt_info->rotation;
for(int i = 0; i < crt_info->noutput && monitor_callback_userdata->cap_kms->monitor_id.num_connector_ids < MAX_CONNECTOR_IDS; ++i) {
int nprop = 0;
Atom *props = XRRListOutputProperties(monitor_callback_userdata->cap_kms->dpy, crt_info->outputs[i], &nprop);
if(!props)
continue;
if(!properties_has_atom(props, nprop, monitor_callback_userdata->randr_connector_id_atom)) {
XFree(props);
continue;
}
Atom type = 0;
int format = 0;
unsigned long bytes_after = 0;
unsigned long nitems = 0;
unsigned char *prop = NULL;
XRRGetOutputProperty(monitor_callback_userdata->cap_kms->dpy, crt_info->outputs[i],
monitor_callback_userdata->randr_connector_id_atom,
0, 128, false, false, AnyPropertyType,
&type, &format, &nitems, &bytes_after, &prop);
if(type == XA_INTEGER && format == 32) {
monitor_callback_userdata->cap_kms->monitor_id.connector_ids[monitor_callback_userdata->cap_kms->monitor_id.num_connector_ids] = *(long*)prop;
++monitor_callback_userdata->cap_kms->monitor_id.num_connector_ids;
}
XFree(props);
}
if(monitor_callback_userdata->cap_kms->monitor_id.num_connector_ids == MAX_CONNECTOR_IDS)
fprintf(stderr, "gsr warning: reached max connector ids\n");
} }
static int gsr_capture_kms_vaapi_start(gsr_capture *cap, AVCodecContext *video_codec_context) { static int gsr_capture_kms_vaapi_start(gsr_capture *cap, AVCodecContext *video_codec_context) {
gsr_capture_kms_vaapi *cap_kms = cap->priv; gsr_capture_kms_vaapi *cap_kms = cap->priv;
// TODO: Allow specifying another card, and in other places
if(gsr_kms_client_init(&cap_kms->kms_client, cap_kms->params.card_path) != 0) { if(gsr_kms_client_init(&cap_kms->kms_client, cap_kms->params.card_path) != 0) {
return -1; return -1;
} }
MonitorCallbackUserdata monitor_callback_userdata = {0, X11_ROT_0}; const Atom randr_connector_id_atom = XInternAtom(cap_kms->dpy, "CONNECTOR_ID", False);
cap_kms->monitor_id.num_connector_ids = 0;
MonitorCallbackUserdata monitor_callback_userdata = {
cap_kms, randr_connector_id_atom,
cap_kms->params.display_to_capture, strlen(cap_kms->params.display_to_capture),
0,
X11_ROT_0
};
for_each_active_monitor_output(cap_kms->dpy, monitor_callback, &monitor_callback_userdata); for_each_active_monitor_output(cap_kms->dpy, monitor_callback, &monitor_callback_userdata);
gsr_monitor monitor; gsr_monitor monitor;
@ -341,27 +396,86 @@ static bool gsr_capture_kms_vaapi_should_stop(gsr_capture *cap, bool *err) {
return false; return false;
} }
static gsr_kms_response_fd* find_drm_by_connector_id(gsr_kms_response *kms_response, uint32_t connector_id) {
for(int i = 0; i < kms_response->num_fds; ++i) {
if(kms_response->fds[i].connector_id == connector_id)
return &kms_response->fds[i];
}
return NULL;
}
static gsr_kms_response_fd* find_first_combined_drm(gsr_kms_response *kms_response) {
for(int i = 0; i < kms_response->num_fds; ++i) {
if(kms_response->fds[i].is_combined_plane)
return &kms_response->fds[i];
}
return NULL;
}
static int gsr_capture_kms_vaapi_capture(gsr_capture *cap, AVFrame *frame) { static int gsr_capture_kms_vaapi_capture(gsr_capture *cap, AVFrame *frame) {
gsr_capture_kms_vaapi *cap_kms = cap->priv; gsr_capture_kms_vaapi *cap_kms = cap->priv;
if(cap_kms->dmabuf_fd > 0) { for(int i = 0; i < cap_kms->kms_response.num_fds; ++i) {
close(cap_kms->dmabuf_fd); if(cap_kms->kms_response.fds[i].fd > 0)
cap_kms->dmabuf_fd = 0; close(cap_kms->kms_response.fds[i].fd);
cap_kms->kms_response.fds[i].fd = 0;
} }
cap_kms->kms_response.num_fds = 0;
gsr_kms_response kms_response; if(gsr_kms_client_get_kms(&cap_kms->kms_client, &cap_kms->kms_response) != 0) {
if(gsr_kms_client_get_kms(&cap_kms->kms_client, &kms_response) != 0) { fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_capture: failed to get kms, error: %d (%s)\n", cap_kms->kms_response.result, cap_kms->kms_response.err_msg);
fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_capture: failed to get kms, error: %d (%s)\n", kms_response.result, kms_response.data.err_msg);
return -1; return -1;
} }
cap_kms->dmabuf_fd = kms_response.data.fd.fd; if(cap_kms->kms_response.num_fds == 0) {
cap_kms->pitch = kms_response.data.fd.pitch; static bool error_shown = false;
cap_kms->offset = kms_response.data.fd.offset; if(!error_shown) {
cap_kms->fourcc = kms_response.data.fd.pixel_format; error_shown = true;
cap_kms->modifiers = kms_response.data.fd.modifier; fprintf(stderr, "gsr error: no drm found, capture will fail\n");
cap_kms->kms_size.x = kms_response.data.fd.width; }
cap_kms->kms_size.y = kms_response.data.fd.height; return -1;
}
bool requires_rotation = cap_kms->requires_rotation;
bool capture_is_combined_plane = false;
gsr_kms_response_fd *drm_fd = NULL;
if(cap_kms->screen_capture) {
drm_fd = find_first_combined_drm(&cap_kms->kms_response);
if(drm_fd) {
capture_is_combined_plane = true;
} else {
static bool error_shown = false;
if(!error_shown) {
error_shown = true;
fprintf(stderr, "gsr warning: no combined drm found, screen capture will capture the first monitor found instead\n");
}
drm_fd = &cap_kms->kms_response.fds[0];
}
} else {
for(int i = 0; i < cap_kms->monitor_id.num_connector_ids; ++i) {
drm_fd = find_drm_by_connector_id(&cap_kms->kms_response, cap_kms->monitor_id.connector_ids[i]);
if(drm_fd) {
requires_rotation = cap_kms->x11_rot != X11_ROT_0;
capture_is_combined_plane = drm_fd->is_combined_plane;
break;
}
}
if(!drm_fd) {
drm_fd = find_first_combined_drm(&cap_kms->kms_response);
if(drm_fd) {
capture_is_combined_plane = true;
} else {
static bool error_shown = false;
if(!error_shown) {
error_shown = true;
fprintf(stderr, "gsr error: no drm found for monitor and no combined drm found, capture will fail\n");
}
return -1;
}
}
}
// TODO: This causes a crash sometimes on steam deck, why? is it a driver bug? a vaapi pure version doesn't cause a crash. // TODO: This causes a crash sometimes on steam deck, why? is it a driver bug? a vaapi pure version doesn't cause a crash.
// Even ffmpeg kmsgrab causes this crash. The error is: // Even ffmpeg kmsgrab causes this crash. The error is:
@ -382,12 +496,12 @@ static int gsr_capture_kms_vaapi_capture(gsr_capture *cap, AVFrame *frame) {
// Assertion pic->display_order == pic->encode_order failed at libavcodec/vaapi_encode_h265.c:765 // Assertion pic->display_order == pic->encode_order failed at libavcodec/vaapi_encode_h265.c:765
// kms server info: kms client shutdown, shutting down the server // kms server info: kms client shutdown, shutting down the server
const intptr_t img_attr[] = { const intptr_t img_attr[] = {
EGL_LINUX_DRM_FOURCC_EXT, cap_kms->fourcc, EGL_LINUX_DRM_FOURCC_EXT, drm_fd->pixel_format,
EGL_WIDTH, cap_kms->kms_size.x, EGL_WIDTH, drm_fd->width,
EGL_HEIGHT, cap_kms->kms_size.y, EGL_HEIGHT, drm_fd->height,
EGL_DMA_BUF_PLANE0_FD_EXT, cap_kms->dmabuf_fd, EGL_DMA_BUF_PLANE0_FD_EXT, drm_fd->fd,
EGL_DMA_BUF_PLANE0_OFFSET_EXT, cap_kms->offset, EGL_DMA_BUF_PLANE0_OFFSET_EXT, drm_fd->offset,
EGL_DMA_BUF_PLANE0_PITCH_EXT, cap_kms->pitch, EGL_DMA_BUF_PLANE0_PITCH_EXT, drm_fd->pitch,
EGL_NONE EGL_NONE
}; };
@ -398,7 +512,7 @@ static int gsr_capture_kms_vaapi_capture(gsr_capture *cap, AVFrame *frame) {
cap_kms->egl.glBindTexture(GL_TEXTURE_2D, 0); cap_kms->egl.glBindTexture(GL_TEXTURE_2D, 0);
float texture_rotation = 0.0f; float texture_rotation = 0.0f;
if(cap_kms->requires_rotation) { if(requires_rotation) {
switch(cap_kms->x11_rot) { switch(cap_kms->x11_rot) {
case X11_ROT_90: case X11_ROT_90:
texture_rotation = M_PI*0.5f; texture_rotation = M_PI*0.5f;
@ -417,22 +531,32 @@ static int gsr_capture_kms_vaapi_capture(gsr_capture *cap, AVFrame *frame) {
gsr_cursor_tick(&cap_kms->cursor); gsr_cursor_tick(&cap_kms->cursor);
vec2i capture_pos = cap_kms->capture_pos;
vec2i capture_size = cap_kms->capture_size;
vec2i cursor_capture_pos = (vec2i){cap_kms->cursor.position.x - cap_kms->cursor.hotspot.x - capture_pos.x, cap_kms->cursor.position.y - cap_kms->cursor.hotspot.y - capture_pos.y};
if(!capture_is_combined_plane) {
capture_pos = (vec2i){0, 0};
//cursor_capture_pos = (vec2i){cap_kms->cursor.position.x - cap_kms->cursor.hotspot.x, cap_kms->cursor.position.y - cap_kms->cursor.hotspot.y};
}
gsr_color_conversion_draw(&cap_kms->color_conversion, cap_kms->input_texture, gsr_color_conversion_draw(&cap_kms->color_conversion, cap_kms->input_texture,
(vec2i){0, 0}, (vec2i){cap_kms->capture_size.x, cap_kms->capture_size.y}, (vec2i){0, 0}, capture_size,
(vec2i){cap_kms->capture_pos.x, cap_kms->capture_pos.y}, (vec2i){cap_kms->capture_size.x, cap_kms->capture_size.y}, capture_pos, capture_size,
texture_rotation); texture_rotation);
gsr_color_conversion_draw(&cap_kms->color_conversion, cap_kms->cursor.texture_id, gsr_color_conversion_draw(&cap_kms->color_conversion, cap_kms->cursor.texture_id,
(vec2i){cap_kms->cursor.position.x - cap_kms->cursor.hotspot.x - cap_kms->capture_pos.x, cap_kms->cursor.position.y - cap_kms->cursor.hotspot.y - cap_kms->capture_pos.y}, (vec2i){cap_kms->cursor.size.x, cap_kms->cursor.size.y}, cursor_capture_pos, (vec2i){cap_kms->cursor.size.x, cap_kms->cursor.size.y},
(vec2i){0, 0}, (vec2i){cap_kms->cursor.size.x, cap_kms->cursor.size.y}, (vec2i){0, 0}, (vec2i){cap_kms->cursor.size.x, cap_kms->cursor.size.y},
0.0f); 0.0f);
cap_kms->egl.eglSwapBuffers(cap_kms->egl.egl_display, cap_kms->egl.egl_surface); cap_kms->egl.eglSwapBuffers(cap_kms->egl.egl_display, cap_kms->egl.egl_surface);
if(cap_kms->dmabuf_fd > 0) { for(int i = 0; i < cap_kms->kms_response.num_fds; ++i) {
close(cap_kms->dmabuf_fd); if(cap_kms->kms_response.fds[i].fd > 0)
cap_kms->dmabuf_fd = 0; close(cap_kms->kms_response.fds[i].fd);
cap_kms->kms_response.fds[i].fd = 0;
} }
cap_kms->kms_response.num_fds = 0;
return 0; return 0;
} }
@ -459,10 +583,12 @@ static void gsr_capture_kms_vaapi_stop(gsr_capture *cap, AVCodecContext *video_c
cap_kms->target_textures[0] = 0; cap_kms->target_textures[0] = 0;
cap_kms->target_textures[1] = 0; cap_kms->target_textures[1] = 0;
if(cap_kms->dmabuf_fd > 0) { for(int i = 0; i < cap_kms->kms_response.num_fds; ++i) {
close(cap_kms->dmabuf_fd); if(cap_kms->kms_response.fds[i].fd > 0)
cap_kms->dmabuf_fd = 0; close(cap_kms->kms_response.fds[i].fd);
cap_kms->kms_response.fds[i].fd = 0;
} }
cap_kms->kms_response.num_fds = 0;
if(video_codec_context->hw_device_ctx) if(video_codec_context->hw_device_ctx)
av_buffer_unref(&video_codec_context->hw_device_ctx); av_buffer_unref(&video_codec_context->hw_device_ctx);

View File

@ -267,7 +267,7 @@ int gsr_color_conversion_draw(gsr_color_conversion *self, unsigned int texture_i
{ {
self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[0]); self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[0]);
//cap_xcomp->egl.glClear(GL_COLOR_BUFFER_BIT); //cap_xcomp->egl.glClear(GL_COLOR_BUFFER_BIT); // TODO: Do this in a separate clear_ function. We want to do that when using multiple drm to create the final image (multiple monitors for example)
gsr_shader_use(&self->shaders[0]); gsr_shader_use(&self->shaders[0]);
self->params.egl->glUniform1f(self->rotation_uniforms[0], rotation); self->params.egl->glUniform1f(self->rotation_uniforms[0], rotation);

View File

@ -1304,6 +1304,7 @@ int main(int argc, char **argv) {
char card_path[128]; char card_path[128];
card_path[0] = '\0'; card_path[0] = '\0';
if(gpu_inf.vendor != GSR_GPU_VENDOR_NVIDIA) { if(gpu_inf.vendor != GSR_GPU_VENDOR_NVIDIA) {
// TODO: Allow specifying another card, and in other places
if(!gsr_get_valid_card_path(card_path)) { if(!gsr_get_valid_card_path(card_path)) {
fprintf(stderr, "Error: no /dev/dri/cardX device found\n"); fprintf(stderr, "Error: no /dev/dri/cardX device found\n");
return 2; return 2;