From 99dbbd07ab9e8c9a899fd8ccb042b74f336dd026 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 12 May 2023 03:49:29 +0200 Subject: kms_vaapi: support multiple drm planes (fixes capture on some multi monitor systems) --- TODO | 4 +- kms/client/kms_client.c | 24 ++--- kms/kms_shared.h | 12 ++- kms/server/kms_server.c | 256 +++++++++++++++++++++++++++++++----------------- src/capture/kms_vaapi.c | 204 ++++++++++++++++++++++++++++++-------- src/color_conversion.c | 2 +- src/main.cpp | 1 + 7 files changed, 355 insertions(+), 148 deletions(-) diff --git a/TODO b/TODO index c642f2b..e98c227 100644 --- a/TODO +++ b/TODO @@ -62,4 +62,6 @@ Intel is a bit weird with monitor capture and multiple monitors. If one of the m When using multiple monitors kms grab the target monitor instead of the whole screen. 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. \ No newline at end of file +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. \ No newline at end of file diff --git a/kms/client/kms_client.c b/kms/client/kms_client.c index 2d1cd0f..bdc556b 100644 --- a/kms/client/kms_client.c +++ b/kms/client/kms_client.c @@ -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_iovlen = 1; - char cmsgbuf[CMSG_SPACE(sizeof(int))]; + char cmsgbuf[CMSG_SPACE(sizeof(int) * GSR_KMS_MAX_PLANES)]; memset(cmsgbuf, 0, sizeof(cmsgbuf)); response_message.msg_control = 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) return res; - if(response->result == KMS_RESULT_OK) - response->data.fd.fd = 0; - - struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message); - if(cmsg) { - if(cmsg->cmsg_type == SCM_RIGHTS) { - int kms_fd = 0; - memcpy(&kms_fd, CMSG_DATA(cmsg), sizeof(int)); - response->data.fd.fd = kms_fd; + if(response->num_fds > 0) { + struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message); + if(cmsg) { + int *fds = (int*)CMSG_DATA(cmsg); + for(int i = 0; i < response->num_fds; ++i) { + response->fds[i].fd = fds[i]; + } + } else { + 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) { 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; request.type = KMS_REQUEST_TYPE_GET_KMS; diff --git a/kms/kms_shared.h b/kms/kms_shared.h index 35d54c4..e0687b2 100644 --- a/kms/kms_shared.h +++ b/kms/kms_shared.h @@ -2,6 +2,9 @@ #define GSR_KMS_SHARED_H #include +#include + +#define GSR_KMS_MAX_PLANES 32 typedef enum { KMS_REQUEST_TYPE_GET_KMS @@ -26,14 +29,15 @@ typedef struct { uint32_t offset; uint32_t pixel_format; uint64_t modifier; + uint32_t connector_id; /* 0 if unknown */ + bool is_combined_plane; } gsr_kms_response_fd; typedef struct { int result; /* gsr_kms_result */ - union { - char err_msg[128]; - gsr_kms_response_fd fd; - } data; + char err_msg[128]; + gsr_kms_response_fd fds[GSR_KMS_MAX_PLANES]; + int num_fds; } gsr_kms_response; #endif /* #define GSR_KMS_SHARED_H */ diff --git a/kms/server/kms_server.c b/kms/server/kms_server.c index 9c4cb36..76bf718 100644 --- a/kms/server/kms_server.c +++ b/kms/server/kms_server.c @@ -15,18 +15,30 @@ #include #include -#define DRM_CLIENT_CAP_UNIVERSAL_PLANES 2 +#define MAX_CONNECTORS 32 typedef struct { 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; +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) { 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; iov.iov_base = 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_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)); - if(num_fds > 0) { + if(response->num_fds > 0) { response_message.msg_control = cmsgbuf; response_message.msg_controllen = sizeof(cmsgbuf); - int total_msg_len = 0; - struct cmsghdr *cmsg = NULL; - for(int i = 0; i < num_fds; ++i) { - if(i == 0) - cmsg = CMSG_FIRSTHDR(&response_message); - else - cmsg = CMSG_NXTHDR(&response_message, cmsg); - - cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = SCM_RIGHTS; - cmsg->cmsg_len = CMSG_LEN(sizeof(int)); - memcpy(CMSG_DATA(cmsg), &fds[i], sizeof(int)); - total_msg_len += cmsg->cmsg_len; + struct cmsghdr *cmsg = CMSG_FIRSTHDR(&response_message); + cmsg->cmsg_level = SOL_SOCKET; + cmsg->cmsg_type = SCM_RIGHTS; + cmsg->cmsg_len = CMSG_LEN(sizeof(int) * response->num_fds); + + int *fds = (int*)CMSG_DATA(cmsg); + for(int i = 0; i < response->num_fds; ++i) { + fds[i] = response->fds[i].fd; } - response_message.msg_controllen = total_msg_len; + response_message.msg_controllen = cmsg->cmsg_len; } 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; + drmModeResPtr resources = NULL; 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) { - 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; } + 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); if(!planes) { fprintf(stderr, "kms server error: failed to access planes, error: %s\n", strerror(errno)); 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]); if(!plane) { 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)? drmModeFB2Ptr drmfb = drmModeGetFB2(drm->drmfd, plane->fb_id); if(drmfb) { - const int64_t plane_size = (int64_t)drmfb->width * (int64_t)drmfb->height; - if(drmfb->handles[0] && plane_size >= max_size) { - max_size = plane_size; - best_plane_match = plane->plane_id; - } + drm->plane_ids[drm->num_plane_ids] = plane->plane_id; + drm->connector_ids[drm->num_plane_ids] = get_connector_by_crtc_id(&c2crtc_map, plane->crtc_id); + ++drm->num_plane_ids; drmModeFreeFB2(drmfb); } 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; error: @@ -120,65 +167,87 @@ static int kms_get_plane_id(gsr_drm *drm) { 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) { - drmModePlanePtr plane = NULL; - drmModeFB2 *drmfb = NULL; int result = -1; response->result = KMS_RESULT_OK; - response->data.fd.fd = 0; - response->data.fd.width = 0; - response->data.fd.height = 0; - - plane = drmModeGetPlane(drm->drmfd, drm->plane_id); - if(!plane) { - response->result = KMS_RESULT_FAILED_TO_GET_PLANE; - 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); - goto error; - } + response->err_msg[0] = '\0'; + response->num_fds = 0; - drmfb = drmModeGetFB2(drm->drmfd, plane->fb_id); - if(!drmfb) { - response->result = KMS_RESULT_FAILED_TO_GET_PLANE; - snprintf(response->data.err_msg, sizeof(response->data.err_msg), "drmModeGetFB2 failed, error: %s", strerror(errno)); - fprintf(stderr, "kms server error: %s\n", response->data.err_msg); - goto error; - } + for(size_t i = 0; i < drm->num_plane_ids && response->num_fds < GSR_KMS_MAX_PLANES; ++i) { + drmModePlanePtr plane = NULL; + drmModeFB2 *drmfb = NULL; - 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; - } + plane = drmModeGetPlane(drm->drmfd, drm->plane_ids[i]); + 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; + } - // 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). + 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; + } - 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; - } + 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; + } - 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; + // 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). - error: - if(drmfb) - drmModeFreeFB2(drmfb); - if(plane) - drmModeFreePlane(plane); + 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); + } + + if(response->num_fds > 0 || response->result == KMS_RESULT_OK) { + result = 0; + } else { + for(int i = 0; i < response->num_fds; ++i) { + close(response->fds[i].fd); + } + response->num_fds = 0; + } return result; } @@ -207,14 +276,14 @@ int main(int argc, char **argv) { const char *card_path = argv[2]; gsr_drm drm; - drm.plane_id = 0; + drm.num_plane_ids = 0; drm.drmfd = open(card_path, O_RDONLY); if(drm.drmfd < 0) { fprintf(stderr, "kms server error: failed to open %s, error: %s", card_path, strerror(errno)); return 2; } - if(kms_get_plane_id(&drm) != 0) { + if(kms_get_plane_ids(&drm) != 0) { close(drm.drmfd); return 2; } @@ -284,11 +353,14 @@ int main(int argc, char **argv) { gsr_kms_response response; 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"); - close(response.data.fd.fd); + + for(int i = 0; i < response.num_fds; ++i) { + close(response.fds[i].fd); + } } 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"); } @@ -297,9 +369,9 @@ int main(int argc, char **argv) { default: { gsr_kms_response response; 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"); - fprintf(stderr, "kms server error: %s\n", response.data.err_msg); - if(send_msg_to_client(socket_fd, &response, NULL, 0) == -1) { + 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.err_msg); + if(send_msg_to_client(socket_fd, &response) == -1) { fprintf(stderr, "kms server error: failed to respond to client request\n"); break; } diff --git a/src/capture/kms_vaapi.c b/src/capture/kms_vaapi.c index bee24b8..31cc492 100644 --- a/src/capture/kms_vaapi.c +++ b/src/capture/kms_vaapi.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -16,6 +17,13 @@ #include #include +#define MAX_CONNECTOR_IDS 32 + +typedef struct { + uint32_t connector_ids[MAX_CONNECTOR_IDS]; + int num_connector_ids; +} MonitorId; + typedef enum { X11_ROT_0 = 1 << 0, X11_ROT_90 = 1 << 1, @@ -35,17 +43,12 @@ typedef struct { gsr_egl egl; gsr_kms_client kms_client; - - uint32_t fourcc; - uint64_t modifiers; - int dmabuf_fd; - uint32_t pitch; - uint32_t offset; - vec2i kms_size; + gsr_kms_response kms_response; vec2i capture_pos; vec2i capture_size; bool screen_capture; + MonitorId monitor_id; 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. 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 rotation; } 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) { MonitorCallbackUserdata *monitor_callback_userdata = userdata; - monitor_callback_userdata->rotation = crt_info->rotation; ++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) { 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) { 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); gsr_monitor monitor; @@ -341,27 +396,86 @@ static bool gsr_capture_kms_vaapi_should_stop(gsr_capture *cap, bool *err) { 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) { gsr_capture_kms_vaapi *cap_kms = cap->priv; - if(cap_kms->dmabuf_fd > 0) { - close(cap_kms->dmabuf_fd); - cap_kms->dmabuf_fd = 0; + for(int i = 0; i < cap_kms->kms_response.num_fds; ++i) { + if(cap_kms->kms_response.fds[i].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, &kms_response) != 0) { - 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); + if(gsr_kms_client_get_kms(&cap_kms->kms_client, &cap_kms->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); return -1; } - cap_kms->dmabuf_fd = kms_response.data.fd.fd; - cap_kms->pitch = kms_response.data.fd.pitch; - cap_kms->offset = kms_response.data.fd.offset; - cap_kms->fourcc = kms_response.data.fd.pixel_format; - cap_kms->modifiers = kms_response.data.fd.modifier; - cap_kms->kms_size.x = kms_response.data.fd.width; - cap_kms->kms_size.y = kms_response.data.fd.height; + if(cap_kms->kms_response.num_fds == 0) { + static bool error_shown = false; + if(!error_shown) { + error_shown = true; + fprintf(stderr, "gsr error: no drm found, capture will fail\n"); + } + 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. // 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 // kms server info: kms client shutdown, shutting down the server const intptr_t img_attr[] = { - EGL_LINUX_DRM_FOURCC_EXT, cap_kms->fourcc, - EGL_WIDTH, cap_kms->kms_size.x, - EGL_HEIGHT, cap_kms->kms_size.y, - EGL_DMA_BUF_PLANE0_FD_EXT, cap_kms->dmabuf_fd, - EGL_DMA_BUF_PLANE0_OFFSET_EXT, cap_kms->offset, - EGL_DMA_BUF_PLANE0_PITCH_EXT, cap_kms->pitch, + EGL_LINUX_DRM_FOURCC_EXT, drm_fd->pixel_format, + EGL_WIDTH, drm_fd->width, + EGL_HEIGHT, drm_fd->height, + EGL_DMA_BUF_PLANE0_FD_EXT, drm_fd->fd, + EGL_DMA_BUF_PLANE0_OFFSET_EXT, drm_fd->offset, + EGL_DMA_BUF_PLANE0_PITCH_EXT, drm_fd->pitch, 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); float texture_rotation = 0.0f; - if(cap_kms->requires_rotation) { + if(requires_rotation) { switch(cap_kms->x11_rot) { case X11_ROT_90: 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); + 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, - (vec2i){0, 0}, (vec2i){cap_kms->capture_size.x, cap_kms->capture_size.y}, - (vec2i){cap_kms->capture_pos.x, cap_kms->capture_pos.y}, (vec2i){cap_kms->capture_size.x, cap_kms->capture_size.y}, + (vec2i){0, 0}, capture_size, + capture_pos, capture_size, texture_rotation); 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}, 0.0f); cap_kms->egl.eglSwapBuffers(cap_kms->egl.egl_display, cap_kms->egl.egl_surface); - if(cap_kms->dmabuf_fd > 0) { - close(cap_kms->dmabuf_fd); - cap_kms->dmabuf_fd = 0; + for(int i = 0; i < cap_kms->kms_response.num_fds; ++i) { + if(cap_kms->kms_response.fds[i].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; } @@ -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[1] = 0; - if(cap_kms->dmabuf_fd > 0) { - close(cap_kms->dmabuf_fd); - cap_kms->dmabuf_fd = 0; + for(int i = 0; i < cap_kms->kms_response.num_fds; ++i) { + if(cap_kms->kms_response.fds[i].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) av_buffer_unref(&video_codec_context->hw_device_ctx); diff --git a/src/color_conversion.c b/src/color_conversion.c index e78d97d..84500fd 100644 --- a/src/color_conversion.c +++ b/src/color_conversion.c @@ -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]); - //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]); self->params.egl->glUniform1f(self->rotation_uniforms[0], rotation); diff --git a/src/main.cpp b/src/main.cpp index 2c73cb5..ed757a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1304,6 +1304,7 @@ int main(int argc, char **argv) { char card_path[128]; card_path[0] = '\0'; 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)) { fprintf(stderr, "Error: no /dev/dri/cardX device found\n"); return 2; -- cgit v1.2.3