From 3eb7bae12c9fe50913a57656d2b324d4da18c30a Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 9 Feb 2024 00:23:52 +0100 Subject: Move card path to egl struct, use egl struct for monitor enumeration --- include/capture/kms_cuda.h | 1 - include/capture/kms_vaapi.h | 1 - include/capture/xcomposite_cuda.h | 4 --- include/capture/xcomposite_vaapi.h | 5 --- include/egl.h | 3 +- include/utils.h | 5 +-- src/capture/kms_cuda.c | 11 +++---- src/capture/kms_vaapi.c | 15 ++++----- src/capture/nvfbc.c | 6 ++-- src/capture/xcomposite_cuda.c | 38 ++++++++++----------- src/capture/xcomposite_vaapi.c | 31 +++++++++--------- src/color_conversion.c | 18 ++-------- src/egl.c | 2 +- src/main.cpp | 67 ++++++++++++++------------------------ src/utils.c | 20 ++++++------ 15 files changed, 92 insertions(+), 135 deletions(-) diff --git a/include/capture/kms_cuda.h b/include/capture/kms_cuda.h index c2b2ad8..14ed333 100644 --- a/include/capture/kms_cuda.h +++ b/include/capture/kms_cuda.h @@ -11,7 +11,6 @@ typedef struct { gsr_egl *egl; const char *display_to_capture; /* if this is "screen", then the first monitor is captured. A copy is made of this */ gsr_gpu_info gpu_inf; - const char *card_path; /* reference */ } gsr_capture_kms_cuda_params; gsr_capture* gsr_capture_kms_cuda_create(const gsr_capture_kms_cuda_params *params); diff --git a/include/capture/kms_vaapi.h b/include/capture/kms_vaapi.h index b7fbb81..b670a0d 100644 --- a/include/capture/kms_vaapi.h +++ b/include/capture/kms_vaapi.h @@ -12,7 +12,6 @@ typedef struct { gsr_egl *egl; const char *display_to_capture; /* if this is "screen", then the first monitor is captured. A copy is made of this */ gsr_gpu_info gpu_inf; - const char *card_path; /* reference */ bool wayland; bool hdr; gsr_color_range color_range; diff --git a/include/capture/xcomposite_cuda.h b/include/capture/xcomposite_cuda.h index 2106714..53b7c0d 100644 --- a/include/capture/xcomposite_cuda.h +++ b/include/capture/xcomposite_cuda.h @@ -4,13 +4,9 @@ #include "capture.h" #include "../egl.h" #include "../vec2.h" -#include - -typedef struct _XDisplay Display; typedef struct { gsr_egl *egl; - Display *dpy; Window window; bool follow_focused; /* If this is set then |window| is ignored */ vec2i region_size; /* This is currently only used with |follow_focused| */ diff --git a/include/capture/xcomposite_vaapi.h b/include/capture/xcomposite_vaapi.h index ba234ec..c6237e3 100644 --- a/include/capture/xcomposite_vaapi.h +++ b/include/capture/xcomposite_vaapi.h @@ -5,17 +5,12 @@ #include "../egl.h" #include "../vec2.h" #include "../color_conversion.h" -#include - -typedef struct _XDisplay Display; typedef struct { gsr_egl *egl; - Display *dpy; Window window; bool follow_focused; /* If this is set then |window| is ignored */ vec2i region_size; /* This is currently only used with |follow_focused| */ - const char *card_path; /* reference */ gsr_color_range color_range; } gsr_capture_xcomposite_vaapi_params; diff --git a/include/egl.h b/include/egl.h index d8fdf97..a747bdb 100644 --- a/include/egl.h +++ b/include/egl.h @@ -146,6 +146,7 @@ typedef struct { gsr_x11 x11; gsr_wayland wayland; + char card_path[128]; int fd; uint32_t x; @@ -234,7 +235,7 @@ bool gsr_egl_load(gsr_egl *self, Display *dpy, bool wayland); void gsr_egl_unload(gsr_egl *self); /* wayland protocol capture, does not include kms capture */ -bool gsr_egl_supports_wayland_capture(gsr_egl *self); +bool gsr_egl_supports_wayland_capture(const gsr_egl *self); bool gsr_egl_start_capture(gsr_egl *self, const char *monitor_to_capture); void gsr_egl_update(gsr_egl *self); void gsr_egl_cleanup_frame(gsr_egl *self); diff --git a/include/utils.h b/include/utils.h index 392591a..26a0aed 100644 --- a/include/utils.h +++ b/include/utils.h @@ -43,8 +43,9 @@ typedef struct { double clock_get_monotonic_seconds(void); typedef void (*active_monitor_callback)(const gsr_monitor *monitor, void *userdata); -void for_each_active_monitor_output(void *connection, gsr_connection_type connection_type, active_monitor_callback callback, void *userdata); -bool get_monitor_by_name(void *connection, gsr_connection_type connection_type, const char *name, gsr_monitor *monitor); +void for_each_active_monitor_output_x11(Display *display, active_monitor_callback callback, void *userdata); +void for_each_active_monitor_output(const gsr_egl *egl, gsr_connection_type connection_type, active_monitor_callback callback, void *userdata); +bool get_monitor_by_name(const gsr_egl *egl, gsr_connection_type connection_type, const char *name, gsr_monitor *monitor); bool gl_get_gpu_info(gsr_egl *egl, gsr_gpu_info *info); diff --git a/src/capture/kms_cuda.c b/src/capture/kms_cuda.c index 719e5a0..a7e991b 100644 --- a/src/capture/kms_cuda.c +++ b/src/capture/kms_cuda.c @@ -152,7 +152,7 @@ static int gsr_capture_kms_cuda_start(gsr_capture *cap, AVCodecContext *video_co } cap_kms->using_wayland_capture = true; } else { - int kms_init_res = gsr_kms_client_init(&cap_kms->kms_client, cap_kms->params.card_path); + int kms_init_res = gsr_kms_client_init(&cap_kms->kms_client, cap_kms->params.egl->card_path); if(kms_init_res != 0) { gsr_capture_kms_cuda_stop(cap, video_codec_context); return kms_init_res; @@ -163,9 +163,9 @@ static int gsr_capture_kms_cuda_start(gsr_capture *cap, AVCodecContext *video_co cap_kms->params.display_to_capture, strlen(cap_kms->params.display_to_capture), 0 }; - for_each_active_monitor_output((void*)cap_kms->params.card_path, GSR_CONNECTION_DRM, monitor_callback, &monitor_callback_userdata); + for_each_active_monitor_output(cap_kms->params.egl, GSR_CONNECTION_DRM, monitor_callback, &monitor_callback_userdata); - if(!get_monitor_by_name((void*)cap_kms->params.card_path, GSR_CONNECTION_DRM, cap_kms->params.display_to_capture, &monitor)) { + if(!get_monitor_by_name(cap_kms->params.egl, GSR_CONNECTION_DRM, cap_kms->params.display_to_capture, &monitor)) { fprintf(stderr, "gsr error: gsr_capture_kms_cuda_start: failed to find monitor by name \"%s\"\n", cap_kms->params.display_to_capture); gsr_capture_kms_cuda_stop(cap, video_codec_context); return -1; @@ -239,9 +239,6 @@ static bool cuda_register_opengl_texture(gsr_capture_kms_cuda *cap_kms) { static void gsr_capture_kms_cuda_tick(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame **frame) { gsr_capture_kms_cuda *cap_kms = cap->priv; - // TODO: - cap_kms->params.egl->glClear(GL_COLOR_BUFFER_BIT); - if(!cap_kms->created_hw_frame) { cap_kms->created_hw_frame = true; @@ -398,6 +395,8 @@ static int gsr_capture_kms_cuda_capture(gsr_capture *cap, AVFrame *frame) { (void)frame; gsr_capture_kms_cuda *cap_kms = cap->priv; + cap_kms->params.egl->glClear(GL_COLOR_BUFFER_BIT); + 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); diff --git a/src/capture/kms_vaapi.c b/src/capture/kms_vaapi.c index bfd6ea8..b463050 100644 --- a/src/capture/kms_vaapi.c +++ b/src/capture/kms_vaapi.c @@ -58,8 +58,8 @@ static void gsr_capture_kms_vaapi_stop(gsr_capture *cap, AVCodecContext *video_c static bool drm_create_codec_context(gsr_capture_kms_vaapi *cap_kms, AVCodecContext *video_codec_context) { char render_path[128]; - if(!gsr_card_path_get_render_path(cap_kms->params.card_path, render_path)) { - fprintf(stderr, "gsr error: failed to get /dev/dri/renderDXXX file from %s\n", cap_kms->params.card_path); + if(!gsr_card_path_get_render_path(cap_kms->params.egl->card_path, render_path)) { + fprintf(stderr, "gsr error: failed to get /dev/dri/renderDXXX file from %s\n", cap_kms->params.egl->card_path); return false; } @@ -146,7 +146,7 @@ static int gsr_capture_kms_vaapi_start(gsr_capture *cap, AVCodecContext *video_c } cap_kms->using_wayland_capture = true; } else { - int kms_init_res = gsr_kms_client_init(&cap_kms->kms_client, cap_kms->params.card_path); + int kms_init_res = gsr_kms_client_init(&cap_kms->kms_client, cap_kms->params.egl->card_path); if(kms_init_res != 0) { gsr_capture_kms_vaapi_stop(cap, video_codec_context); return kms_init_res; @@ -157,9 +157,9 @@ static int gsr_capture_kms_vaapi_start(gsr_capture *cap, AVCodecContext *video_c cap_kms->params.display_to_capture, strlen(cap_kms->params.display_to_capture), 0, }; - for_each_active_monitor_output((void*)cap_kms->params.card_path, GSR_CONNECTION_DRM, monitor_callback, &monitor_callback_userdata); + for_each_active_monitor_output(cap_kms->params.egl, GSR_CONNECTION_DRM, monitor_callback, &monitor_callback_userdata); - if(!get_monitor_by_name((void*)cap_kms->params.card_path, GSR_CONNECTION_DRM, cap_kms->params.display_to_capture, &monitor)) { + if(!get_monitor_by_name(cap_kms->params.egl, GSR_CONNECTION_DRM, cap_kms->params.display_to_capture, &monitor)) { fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_start: failed to find monitor by name \"%s\"\n", cap_kms->params.display_to_capture); gsr_capture_kms_vaapi_stop(cap, video_codec_context); return -1; @@ -193,9 +193,6 @@ static uint32_t fourcc(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { static void gsr_capture_kms_vaapi_tick(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame **frame) { gsr_capture_kms_vaapi *cap_kms = cap->priv; - // TODO: - cap_kms->params.egl->glClear(GL_COLOR_BUFFER_BIT); - if(!cap_kms->created_hw_frame) { cap_kms->created_hw_frame = true; @@ -462,6 +459,8 @@ static void gsr_capture_kms_vaapi_set_hdr_metadata(gsr_capture_kms_vaapi *cap_km static int gsr_capture_kms_vaapi_capture(gsr_capture *cap, AVFrame *frame) { gsr_capture_kms_vaapi *cap_kms = cap->priv; + cap_kms->params.egl->glClear(GL_COLOR_BUFFER_BIT); + 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); diff --git a/src/capture/nvfbc.c b/src/capture/nvfbc.c index e6ee1a9..b0c4941 100644 --- a/src/capture/nvfbc.c +++ b/src/capture/nvfbc.c @@ -189,7 +189,7 @@ static bool ffmpeg_create_cuda_contexts(gsr_capture_nvfbc *cap_nvfbc, AVCodecCon static int gsr_capture_nvfbc_start(gsr_capture *cap, AVCodecContext *video_codec_context) { gsr_capture_nvfbc *cap_nvfbc = cap->priv; - if(!gsr_cuda_load(&cap_nvfbc->cuda, cap_nvfbc->params.dpy, cap_nvfbc->params.overclock)) + if(!gsr_cuda_load(&cap_nvfbc->cuda, cap_nvfbc->params.egl->x11.dpy, cap_nvfbc->params.overclock)) return -1; if(!gsr_capture_nvfbc_load_library(cap)) { @@ -269,8 +269,8 @@ static int gsr_capture_nvfbc_start(gsr_capture *cap, AVCodecContext *video_codec goto error_cleanup; } - uint32_t tracking_width = XWidthOfScreen(DefaultScreenOfDisplay(cap_nvfbc->params.dpy)); - uint32_t tracking_height = XHeightOfScreen(DefaultScreenOfDisplay(cap_nvfbc->params.dpy)); + uint32_t tracking_width = XWidthOfScreen(DefaultScreenOfDisplay(cap_nvfbc->params.egl->x11.dpy)); + uint32_t tracking_height = XHeightOfScreen(DefaultScreenOfDisplay(cap_nvfbc->params.egl->x11.dpy)); tracking_type = strcmp(cap_nvfbc->params.display_to_capture, "screen") == 0 ? NVFBC_TRACKING_SCREEN : NVFBC_TRACKING_OUTPUT; if(tracking_type == NVFBC_TRACKING_OUTPUT) { if(!status_params.bXRandRAvailable) { diff --git a/src/capture/xcomposite_cuda.c b/src/capture/xcomposite_cuda.c index 079f5e3..8217805 100644 --- a/src/capture/xcomposite_cuda.c +++ b/src/capture/xcomposite_cuda.c @@ -154,12 +154,12 @@ static int gsr_capture_xcomposite_cuda_start(gsr_capture *cap, AVCodecContext *v gsr_capture_xcomposite_cuda *cap_xcomp = cap->priv; if(cap_xcomp->params.follow_focused) { - cap_xcomp->net_active_window_atom = XInternAtom(cap_xcomp->params.dpy, "_NET_ACTIVE_WINDOW", False); + cap_xcomp->net_active_window_atom = XInternAtom(cap_xcomp->params.egl->x11.dpy, "_NET_ACTIVE_WINDOW", False); if(!cap_xcomp->net_active_window_atom) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_start failed: failed to get _NET_ACTIVE_WINDOW atom\n"); return -1; } - cap_xcomp->window = get_focused_window(cap_xcomp->params.dpy, cap_xcomp->net_active_window_atom); + cap_xcomp->window = get_focused_window(cap_xcomp->params.egl->x11.dpy, cap_xcomp->net_active_window_atom); } else { cap_xcomp->window = cap_xcomp->params.window; } @@ -169,7 +169,7 @@ static int gsr_capture_xcomposite_cuda_start(gsr_capture *cap, AVCodecContext *v XWindowAttributes attr; attr.width = 0; attr.height = 0; - if(!XGetWindowAttributes(cap_xcomp->params.dpy, cap_xcomp->window, &attr) && !cap_xcomp->params.follow_focused) { + if(!XGetWindowAttributes(cap_xcomp->params.egl->x11.dpy, cap_xcomp->window, &attr) && !cap_xcomp->params.follow_focused) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_start failed: invalid window id: %lu\n", cap_xcomp->window); return -1; } @@ -178,12 +178,12 @@ static int gsr_capture_xcomposite_cuda_start(gsr_capture *cap, AVCodecContext *v cap_xcomp->window_size.y = max_int(attr.height, 0); if(cap_xcomp->params.follow_focused) - XSelectInput(cap_xcomp->params.dpy, DefaultRootWindow(cap_xcomp->params.dpy), PropertyChangeMask); + XSelectInput(cap_xcomp->params.egl->x11.dpy, DefaultRootWindow(cap_xcomp->params.egl->x11.dpy), PropertyChangeMask); - XSelectInput(cap_xcomp->params.dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask); + XSelectInput(cap_xcomp->params.egl->x11.dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask); cap_xcomp->params.egl->eglSwapInterval(cap_xcomp->params.egl->egl_display, 0); - if(window_texture_init(&cap_xcomp->window_texture, cap_xcomp->params.dpy, cap_xcomp->window, cap_xcomp->params.egl) != 0 && !cap_xcomp->params.follow_focused) { + if(window_texture_init(&cap_xcomp->window_texture, cap_xcomp->params.egl->x11.dpy, cap_xcomp->window, cap_xcomp->params.egl) != 0 && !cap_xcomp->params.follow_focused) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_start: failed to get window texture for window %ld\n", cap_xcomp->window); return -1; } @@ -214,7 +214,7 @@ static int gsr_capture_xcomposite_cuda_start(gsr_capture *cap, AVCodecContext *v return -1; } - if(!gsr_cuda_load(&cap_xcomp->cuda, cap_xcomp->params.dpy, cap_xcomp->params.overclock)) { + if(!gsr_cuda_load(&cap_xcomp->cuda, cap_xcomp->params.egl->x11.dpy, cap_xcomp->params.overclock)) { gsr_capture_xcomposite_cuda_stop(cap, video_codec_context); return -1; } @@ -262,22 +262,19 @@ static void gsr_capture_xcomposite_cuda_stop(gsr_capture *cap, AVCodecContext *v gsr_cuda_unload(&cap_xcomp->cuda); - if(cap_xcomp->params.dpy) { + if(cap_xcomp->params.egl->x11.dpy) { // TODO: This causes a crash, why? maybe some other library dlclose xlib and that also happened to unload this??? //XCloseDisplay(cap_xcomp->dpy); - cap_xcomp->params.dpy = NULL; + cap_xcomp->params.egl->x11.dpy = NULL; } } static void gsr_capture_xcomposite_cuda_tick(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame **frame) { gsr_capture_xcomposite_cuda *cap_xcomp = cap->priv; - cap_xcomp->params.egl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f); - cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); - bool init_new_window = false; - while(XPending(cap_xcomp->params.dpy)) { - XNextEvent(cap_xcomp->params.dpy, &cap_xcomp->xev); + while(XPending(cap_xcomp->params.egl->x11.dpy)) { + XNextEvent(cap_xcomp->params.egl->x11.dpy, &cap_xcomp->xev); switch(cap_xcomp->xev.type) { case DestroyNotify: { @@ -321,17 +318,17 @@ static void gsr_capture_xcomposite_cuda_tick(gsr_capture *cap, AVCodecContext *v } if(init_new_window) { - Window focused_window = get_focused_window(cap_xcomp->params.dpy, cap_xcomp->net_active_window_atom); + Window focused_window = get_focused_window(cap_xcomp->params.egl->x11.dpy, cap_xcomp->net_active_window_atom); if(focused_window != cap_xcomp->window || !cap_xcomp->follow_focused_initialized) { cap_xcomp->follow_focused_initialized = true; - XSelectInput(cap_xcomp->params.dpy, cap_xcomp->window, 0); + XSelectInput(cap_xcomp->params.egl->x11.dpy, cap_xcomp->window, 0); cap_xcomp->window = focused_window; - XSelectInput(cap_xcomp->params.dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask); + XSelectInput(cap_xcomp->params.egl->x11.dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask); XWindowAttributes attr; attr.width = 0; attr.height = 0; - if(!XGetWindowAttributes(cap_xcomp->params.dpy, cap_xcomp->window, &attr)) + if(!XGetWindowAttributes(cap_xcomp->params.egl->x11.dpy, cap_xcomp->window, &attr)) fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_tick failed: invalid window id: %lu\n", cap_xcomp->window); cap_xcomp->window_size.x = max_int(attr.width, 0); @@ -339,7 +336,7 @@ static void gsr_capture_xcomposite_cuda_tick(gsr_capture *cap, AVCodecContext *v cap_xcomp->window_resized = true; window_texture_deinit(&cap_xcomp->window_texture); - window_texture_init(&cap_xcomp->window_texture, cap_xcomp->params.dpy, cap_xcomp->window, cap_xcomp->params.egl); // TODO: Do not do the below window_texture_on_resize after this + window_texture_init(&cap_xcomp->window_texture, cap_xcomp->params.egl->x11.dpy, cap_xcomp->window, cap_xcomp->params.egl); // TODO: Do not do the below window_texture_on_resize after this cap_xcomp->texture_size.x = 0; cap_xcomp->texture_size.y = 0; @@ -424,6 +421,9 @@ static bool gsr_capture_xcomposite_cuda_should_stop(gsr_capture *cap, bool *err) static int gsr_capture_xcomposite_cuda_capture(gsr_capture *cap, AVFrame *frame) { gsr_capture_xcomposite_cuda *cap_xcomp = cap->priv; + cap_xcomp->params.egl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); + vec2i source_pos = { 0, 0 }; vec2i source_size = cap_xcomp->texture_size; diff --git a/src/capture/xcomposite_vaapi.c b/src/capture/xcomposite_vaapi.c index 5e9ba66..2775bb3 100644 --- a/src/capture/xcomposite_vaapi.c +++ b/src/capture/xcomposite_vaapi.c @@ -66,8 +66,8 @@ static Window get_focused_window(Display *display, Atom net_active_window_atom) static bool drm_create_codec_context(gsr_capture_xcomposite_vaapi *cap_xcomp, AVCodecContext *video_codec_context) { char render_path[128]; - if(!gsr_card_path_get_render_path(cap_xcomp->params.card_path, render_path)) { - fprintf(stderr, "gsr error: failed to get /dev/dri/renderDXXX file from %s\n", cap_xcomp->params.card_path); + if(!gsr_card_path_get_render_path(cap_xcomp->params.egl->card_path, render_path)) { + fprintf(stderr, "gsr error: failed to get /dev/dri/renderDXXX file from %s\n", cap_xcomp->params.egl->card_path); return false; } @@ -117,12 +117,12 @@ static int gsr_capture_xcomposite_vaapi_start(gsr_capture *cap, AVCodecContext * gsr_capture_xcomposite_vaapi *cap_xcomp = cap->priv; if(cap_xcomp->params.follow_focused) { - cap_xcomp->net_active_window_atom = XInternAtom(cap_xcomp->params.dpy, "_NET_ACTIVE_WINDOW", False); + cap_xcomp->net_active_window_atom = XInternAtom(cap_xcomp->params.egl->x11.dpy, "_NET_ACTIVE_WINDOW", False); if(!cap_xcomp->net_active_window_atom) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start failed: failed to get _NET_ACTIVE_WINDOW atom\n"); return -1; } - cap_xcomp->window = get_focused_window(cap_xcomp->params.dpy, cap_xcomp->net_active_window_atom); + cap_xcomp->window = get_focused_window(cap_xcomp->params.egl->x11.dpy, cap_xcomp->net_active_window_atom); } else { cap_xcomp->window = cap_xcomp->params.window; } @@ -130,7 +130,7 @@ static int gsr_capture_xcomposite_vaapi_start(gsr_capture *cap, AVCodecContext * /* TODO: Do these in tick, and allow error if follow_focused */ XWindowAttributes attr; - if(!XGetWindowAttributes(cap_xcomp->params.dpy, cap_xcomp->params.window, &attr) && !cap_xcomp->params.follow_focused) { + if(!XGetWindowAttributes(cap_xcomp->params.egl->x11.dpy, cap_xcomp->params.window, &attr) && !cap_xcomp->params.follow_focused) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start failed: invalid window id: %lu\n", cap_xcomp->params.window); return -1; } @@ -139,10 +139,10 @@ static int gsr_capture_xcomposite_vaapi_start(gsr_capture *cap, AVCodecContext * cap_xcomp->window_size.y = max_int(attr.height, 0); if(cap_xcomp->params.follow_focused) - XSelectInput(cap_xcomp->params.dpy, DefaultRootWindow(cap_xcomp->params.dpy), PropertyChangeMask); + XSelectInput(cap_xcomp->params.egl->x11.dpy, DefaultRootWindow(cap_xcomp->params.egl->x11.dpy), PropertyChangeMask); // TODO: Get select and add these on top of it and then restore at the end. Also do the same in other xcomposite - XSelectInput(cap_xcomp->params.dpy, cap_xcomp->params.window, StructureNotifyMask | ExposureMask); + XSelectInput(cap_xcomp->params.egl->x11.dpy, cap_xcomp->params.window, StructureNotifyMask | ExposureMask); if(!cap_xcomp->params.egl->eglExportDMABUFImageQueryMESA) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start: could not find eglExportDMABUFImageQueryMESA\n"); @@ -156,7 +156,7 @@ static int gsr_capture_xcomposite_vaapi_start(gsr_capture *cap, AVCodecContext * /* Disable vsync */ cap_xcomp->params.egl->eglSwapInterval(cap_xcomp->params.egl->egl_display, 0); - if(window_texture_init(&cap_xcomp->window_texture, cap_xcomp->params.dpy, cap_xcomp->params.window, cap_xcomp->params.egl) != 0 && !cap_xcomp->params.follow_focused) { + if(window_texture_init(&cap_xcomp->window_texture, cap_xcomp->params.egl->x11.dpy, cap_xcomp->params.window, cap_xcomp->params.egl) != 0 && !cap_xcomp->params.follow_focused) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start: failed to get window texture for window %ld\n", cap_xcomp->params.window); return -1; } @@ -198,13 +198,12 @@ static uint32_t fourcc(uint32_t a, uint32_t b, uint32_t c, uint32_t d) { static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame **frame) { gsr_capture_xcomposite_vaapi *cap_xcomp = cap->priv; - // TODO: cap_xcomp->params.egl->glClearColor(0.0f, 0.0f, 0.0f, 1.0f); cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); bool init_new_window = false; - while(XPending(cap_xcomp->params.dpy)) { - XNextEvent(cap_xcomp->params.dpy, &cap_xcomp->xev); + while(XPending(cap_xcomp->params.egl->x11.dpy)) { + XNextEvent(cap_xcomp->params.egl->x11.dpy, &cap_xcomp->xev); switch(cap_xcomp->xev.type) { case DestroyNotify: { @@ -248,17 +247,17 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext * } if(init_new_window) { - Window focused_window = get_focused_window(cap_xcomp->params.dpy, cap_xcomp->net_active_window_atom); + Window focused_window = get_focused_window(cap_xcomp->params.egl->x11.dpy, cap_xcomp->net_active_window_atom); if(focused_window != cap_xcomp->window || !cap_xcomp->follow_focused_initialized) { cap_xcomp->follow_focused_initialized = true; - XSelectInput(cap_xcomp->params.dpy, cap_xcomp->window, 0); + XSelectInput(cap_xcomp->params.egl->x11.dpy, cap_xcomp->window, 0); cap_xcomp->window = focused_window; - XSelectInput(cap_xcomp->params.dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask); + XSelectInput(cap_xcomp->params.egl->x11.dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask); XWindowAttributes attr; attr.width = 0; attr.height = 0; - if(!XGetWindowAttributes(cap_xcomp->params.dpy, cap_xcomp->window, &attr)) + if(!XGetWindowAttributes(cap_xcomp->params.egl->x11.dpy, cap_xcomp->window, &attr)) fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_tick failed: invalid window id: %lu\n", cap_xcomp->window); cap_xcomp->window_size.x = max_int(attr.width, 0); @@ -266,7 +265,7 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext * cap_xcomp->window_resized = true; window_texture_deinit(&cap_xcomp->window_texture); - window_texture_init(&cap_xcomp->window_texture, cap_xcomp->params.dpy, cap_xcomp->window, cap_xcomp->params.egl); // TODO: Do not do the below window_texture_on_resize after this + window_texture_init(&cap_xcomp->window_texture, cap_xcomp->params.egl->x11.dpy, cap_xcomp->window, cap_xcomp->params.egl); // TODO: Do not do the below window_texture_on_resize after this cap_xcomp->texture_size.x = 0; cap_xcomp->texture_size.y = 0; diff --git a/src/color_conversion.c b/src/color_conversion.c index 166ba90..bc83ddd 100644 --- a/src/color_conversion.c +++ b/src/color_conversion.c @@ -456,29 +456,15 @@ void gsr_color_conversion_draw(gsr_color_conversion *self, unsigned int texture_ } void gsr_color_conversion_clear(gsr_color_conversion *self) { - float color1[4]; - float color2[4]; + float color1[4] = {0.0f, 0.0f, 0.0f, 1.0f}; + float color2[4] = {0.0f, 0.0f, 0.0f, 1.0f}; switch(self->params.destination_color) { case GSR_DESTINATION_COLOR_BGR: { - color1[0] = 0.0f; - color1[1] = 0.0f; - color1[2] = 0.0f; - color1[3] = 1.0f; - - color2[0] = 0.0f; - color2[1] = 0.0f; - color2[2] = 0.0f; - color2[3] = 1.0f; break; } case GSR_DESTINATION_COLOR_NV12: case GSR_DESTINATION_COLOR_P010: { - color1[0] = 0.0f; - color1[1] = 0.0f; - color1[2] = 0.0f; - color1[3] = 1.0f; - color2[0] = 0.5f; color2[1] = 0.5f; color2[2] = 0.0f; diff --git a/src/egl.c b/src/egl.c index b220612..cc6e1cf 100644 --- a/src/egl.c +++ b/src/egl.c @@ -555,7 +555,7 @@ void gsr_egl_unload(gsr_egl *self) { memset(self, 0, sizeof(gsr_egl)); } -bool gsr_egl_supports_wayland_capture(gsr_egl *self) { +bool gsr_egl_supports_wayland_capture(const gsr_egl *self) { // TODO: wlroots capture is broken right now (black screen) on amd and multiple monitors // so it has to be disabled right now. Find out why it happens and fix it. (void)self; diff --git a/src/main.cpp b/src/main.cpp index b9fbc99..29da47f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1350,7 +1350,7 @@ static bool is_xwayland(Display *display) { return true; bool xwayland_found = false; - for_each_active_monitor_output(display, GSR_CONNECTION_X11, xwayland_check_callback, &xwayland_found); + for_each_active_monitor_output_x11(display, xwayland_check_callback, &xwayland_found); return xwayland_found; } @@ -1404,7 +1404,7 @@ static void list_supported_video_codecs() { fflush(stdout); } -static gsr_capture* create_capture_impl(const char *window_str, const char *screen_region, bool wayland, gsr_gpu_info gpu_inf, gsr_egl &egl, char *card_path, Display *dpy, int fps, bool overclock, VideoCodec video_codec, gsr_color_range color_range) { +static gsr_capture* create_capture_impl(const char *window_str, const char *screen_region, bool wayland, gsr_gpu_info gpu_inf, gsr_egl &egl, int fps, bool overclock, VideoCodec video_codec, gsr_color_range color_range) { vec2i region_size = { 0, 0 }; Window src_window_id = None; bool follow_focused = false; @@ -1440,7 +1440,7 @@ static gsr_capture* create_capture_impl(const char *window_str, const char *scre if(gsr_egl_supports_wayland_capture(&egl)) { for_each_active_monitor_output(&egl, GSR_CONNECTION_WAYLAND, get_first_output, &first_output); } else { - for_each_active_monitor_output(card_path, GSR_CONNECTION_DRM, get_first_output, &first_output); + for_each_active_monitor_output(&egl, GSR_CONNECTION_DRM, get_first_output, &first_output); } if(first_output.output_name) { @@ -1460,22 +1460,24 @@ static gsr_capture* create_capture_impl(const char *window_str, const char *scre } } else { gsr_monitor gmon; - if(!get_monitor_by_name(card_path, GSR_CONNECTION_DRM, window_str, &gmon)) { + if(!get_monitor_by_name(&egl, GSR_CONNECTION_DRM, window_str, &gmon)) { fprintf(stderr, "gsr error: display \"%s\" not found, expected one of:\n", window_str); fprintf(stderr, " \"screen\"\n"); - for_each_active_monitor_output(card_path, GSR_CONNECTION_DRM, monitor_output_callback_print, NULL); + for_each_active_monitor_output(&egl, GSR_CONNECTION_DRM, monitor_output_callback_print, NULL); _exit(1); } } } else { if(strcmp(window_str, "screen") != 0 && strcmp(window_str, "screen-direct") != 0 && strcmp(window_str, "screen-direct-force") != 0) { gsr_monitor gmon; - if(!get_monitor_by_name(dpy, GSR_CONNECTION_X11, window_str, &gmon)) { + if(!get_monitor_by_name(&egl, GSR_CONNECTION_X11, window_str, &gmon)) { + const int screens_width = XWidthOfScreen(DefaultScreenOfDisplay(egl.x11.dpy)); + const int screens_height = XWidthOfScreen(DefaultScreenOfDisplay(egl.x11.dpy)); fprintf(stderr, "gsr error: display \"%s\" not found, expected one of:\n", window_str); - fprintf(stderr, " \"screen\" (%dx%d+%d+%d)\n", XWidthOfScreen(DefaultScreenOfDisplay(dpy)), XHeightOfScreen(DefaultScreenOfDisplay(dpy)), 0, 0); - fprintf(stderr, " \"screen-direct\" (%dx%d+%d+%d)\n", XWidthOfScreen(DefaultScreenOfDisplay(dpy)), XHeightOfScreen(DefaultScreenOfDisplay(dpy)), 0, 0); - fprintf(stderr, " \"screen-direct-force\" (%dx%d+%d+%d)\n", XWidthOfScreen(DefaultScreenOfDisplay(dpy)), XHeightOfScreen(DefaultScreenOfDisplay(dpy)), 0, 0); - for_each_active_monitor_output(dpy, GSR_CONNECTION_X11, monitor_output_callback_print, NULL); + fprintf(stderr, " \"screen\" (%dx%d+%d+%d)\n", screens_width, screens_height, 0, 0); + fprintf(stderr, " \"screen-direct\" (%dx%d+%d+%d)\n", screens_width, screens_height, 0, 0); + fprintf(stderr, " \"screen-direct-force\" (%dx%d+%d+%d)\n", screens_width, screens_height, 0, 0); + for_each_active_monitor_output(&egl, GSR_CONNECTION_X11, monitor_output_callback_print, NULL); _exit(1); } } @@ -1487,7 +1489,6 @@ static gsr_capture* create_capture_impl(const char *window_str, const char *scre kms_params.egl = &egl; kms_params.display_to_capture = window_str; kms_params.gpu_inf = gpu_inf; - kms_params.card_path = card_path; capture = gsr_capture_kms_cuda_create(&kms_params); if(!capture) _exit(1); @@ -1509,7 +1510,7 @@ static gsr_capture* create_capture_impl(const char *window_str, const char *scre gsr_egl_unload(&egl); gsr_capture_nvfbc_params nvfbc_params; - nvfbc_params.dpy = dpy; + nvfbc_params.egl->x11.dpy = egl.x11.dpy; nvfbc_params.display_to_capture = capture_target; nvfbc_params.fps = fps; nvfbc_params.pos = { 0, 0 }; @@ -1525,7 +1526,6 @@ static gsr_capture* create_capture_impl(const char *window_str, const char *scre kms_params.egl = &egl; kms_params.display_to_capture = window_str; kms_params.gpu_inf = gpu_inf; - kms_params.card_path = card_path; kms_params.wayland = wayland; kms_params.hdr = video_codec_is_hdr(video_codec); kms_params.color_range = color_range; @@ -1549,28 +1549,13 @@ static gsr_capture* create_capture_impl(const char *window_str, const char *scre if(!capture) { switch(gpu_inf.vendor) { - case GSR_GPU_VENDOR_AMD: { - gsr_capture_xcomposite_vaapi_params xcomposite_params; - xcomposite_params.egl = &egl; - xcomposite_params.dpy = dpy; - xcomposite_params.window = src_window_id; - xcomposite_params.follow_focused = follow_focused; - xcomposite_params.region_size = region_size; - xcomposite_params.card_path = card_path; - xcomposite_params.color_range = color_range; - capture = gsr_capture_xcomposite_vaapi_create(&xcomposite_params); - if(!capture) - _exit(1); - break; - } + case GSR_GPU_VENDOR_AMD: case GSR_GPU_VENDOR_INTEL: { gsr_capture_xcomposite_vaapi_params xcomposite_params; xcomposite_params.egl = &egl; - xcomposite_params.dpy = dpy; xcomposite_params.window = src_window_id; xcomposite_params.follow_focused = follow_focused; xcomposite_params.region_size = region_size; - xcomposite_params.card_path = card_path; xcomposite_params.color_range = color_range; capture = gsr_capture_xcomposite_vaapi_create(&xcomposite_params); if(!capture) @@ -1580,7 +1565,6 @@ static gsr_capture* create_capture_impl(const char *window_str, const char *scre case GSR_GPU_VENDOR_NVIDIA: { gsr_capture_xcomposite_cuda_params xcomposite_params; xcomposite_params.egl = &egl; - xcomposite_params.dpy = dpy; xcomposite_params.window = src_window_id; xcomposite_params.follow_focused = follow_focused; xcomposite_params.region_size = region_size; @@ -1895,11 +1879,10 @@ int main(int argc, char **argv) { fprintf(stderr, "Info: overclocking is not possible on nvidia on wayland, ignoring option\n"); } - char card_path[128]; - card_path[0] = '\0'; + egl.card_path[0] = '\0'; if(wayland || 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(egl.card_path)) { fprintf(stderr, "Error: no /dev/dri/cardX device found\n"); _exit(2); } @@ -2041,7 +2024,7 @@ int main(int argc, char **argv) { const bool video_codec_auto = strcmp(video_codec_to_use, "auto") == 0; if(video_codec_auto) { if(gpu_inf.vendor == GSR_GPU_VENDOR_INTEL) { - const AVCodec *h264_codec = find_h264_encoder(gpu_inf.vendor, card_path); + const AVCodec *h264_codec = find_h264_encoder(gpu_inf.vendor, egl.card_path); if(!h264_codec) { fprintf(stderr, "Info: using hevc encoder because a codec was not specified and your gpu does not support h264\n"); video_codec_to_use = "hevc"; @@ -2052,7 +2035,7 @@ int main(int argc, char **argv) { video_codec = VideoCodec::H264; } } else { - const AVCodec *h265_codec = find_h265_encoder(gpu_inf.vendor, card_path); + const AVCodec *h265_codec = find_h265_encoder(gpu_inf.vendor, egl.card_path); if(h265_codec && fps > 60) { fprintf(stderr, "Warning: recording at higher fps than 60 with hevc might result in recording at a very low fps. If this happens, switch to h264\n"); @@ -2084,15 +2067,15 @@ int main(int argc, char **argv) { const AVCodec *video_codec_f = nullptr; switch(video_codec) { case VideoCodec::H264: - video_codec_f = find_h264_encoder(gpu_inf.vendor, card_path); + video_codec_f = find_h264_encoder(gpu_inf.vendor, egl.card_path); break; case VideoCodec::HEVC: case VideoCodec::HEVC_HDR: - video_codec_f = find_h265_encoder(gpu_inf.vendor, card_path); + video_codec_f = find_h265_encoder(gpu_inf.vendor, egl.card_path); break; case VideoCodec::AV1: case VideoCodec::AV1_HDR: - video_codec_f = find_av1_encoder(gpu_inf.vendor, card_path); + video_codec_f = find_av1_encoder(gpu_inf.vendor, egl.card_path); break; } @@ -2102,7 +2085,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Warning: selected video codec h264 is not supported, trying hevc instead\n"); video_codec_to_use = "hevc"; video_codec = VideoCodec::HEVC; - video_codec_f = find_h265_encoder(gpu_inf.vendor, card_path); + video_codec_f = find_h265_encoder(gpu_inf.vendor, egl.card_path); break; } case VideoCodec::HEVC: @@ -2110,7 +2093,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Warning: selected video codec hevc is not supported, trying h264 instead\n"); video_codec_to_use = "h264"; video_codec = VideoCodec::H264; - video_codec_f = find_h264_encoder(gpu_inf.vendor, card_path); + video_codec_f = find_h264_encoder(gpu_inf.vendor, egl.card_path); break; } case VideoCodec::AV1: @@ -2118,7 +2101,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Warning: selected video codec av1 is not supported, trying h264 instead\n"); video_codec_to_use = "h264"; video_codec = VideoCodec::H264; - video_codec_f = find_h264_encoder(gpu_inf.vendor, card_path); + video_codec_f = find_h264_encoder(gpu_inf.vendor, egl.card_path); break; } } @@ -2153,7 +2136,7 @@ int main(int argc, char **argv) { _exit(2); } - gsr_capture *capture = create_capture_impl(window_str, screen_region, wayland, gpu_inf, egl, card_path, dpy, fps, overclock, video_codec, color_range); + gsr_capture *capture = create_capture_impl(window_str, screen_region, wayland, gpu_inf, egl, fps, overclock, video_codec, color_range); const bool is_livestream = is_livestream_path(filename); // (Some?) livestreaming services require at least one audio track to work. diff --git a/src/utils.c b/src/utils.c index 94b0037..902f082 100644 --- a/src/utils.c +++ b/src/utils.c @@ -24,7 +24,7 @@ static const XRRModeInfo* get_mode_info(const XRRScreenResources *sr, RRMode id) return NULL; } -static void for_each_active_monitor_output_x11(Display *display, active_monitor_callback callback, void *userdata) { +void for_each_active_monitor_output_x11(Display *display, active_monitor_callback callback, void *userdata) { XRRScreenResources *screen_res = XRRGetScreenResources(display, DefaultRootWindow(display)); if(!screen_res) return; @@ -99,7 +99,7 @@ static bool connector_get_property_by_name(int drmfd, drmModeConnectorPtr props, return false; } -static void for_each_active_monitor_output_wayland(gsr_egl *egl, active_monitor_callback callback, void *userdata) { +static void for_each_active_monitor_output_wayland(const gsr_egl *egl, active_monitor_callback callback, void *userdata) { if(!gsr_egl_supports_wayland_capture(egl)) return; @@ -119,8 +119,8 @@ static void for_each_active_monitor_output_wayland(gsr_egl *egl, active_monitor_ } } -static void for_each_active_monitor_output_drm(const char *drm_card_path, active_monitor_callback callback, void *userdata) { - int fd = open(drm_card_path, O_RDONLY); +static void for_each_active_monitor_output_drm(const gsr_egl *egl, active_monitor_callback callback, void *userdata) { + int fd = open(egl->card_path, O_RDONLY); if(fd == -1) return; @@ -176,16 +176,16 @@ static void for_each_active_monitor_output_drm(const char *drm_card_path, active close(fd); } -void for_each_active_monitor_output(void *connection, gsr_connection_type connection_type, active_monitor_callback callback, void *userdata) { +void for_each_active_monitor_output(const gsr_egl *egl, gsr_connection_type connection_type, active_monitor_callback callback, void *userdata) { switch(connection_type) { case GSR_CONNECTION_X11: - for_each_active_monitor_output_x11(connection, callback, userdata); + for_each_active_monitor_output_x11(egl->x11.dpy, callback, userdata); break; case GSR_CONNECTION_WAYLAND: - for_each_active_monitor_output_wayland(connection, callback, userdata); + for_each_active_monitor_output_wayland(egl, callback, userdata); break; case GSR_CONNECTION_DRM: - for_each_active_monitor_output_drm(connection, callback, userdata); + for_each_active_monitor_output_drm(egl, callback, userdata); break; } } @@ -199,13 +199,13 @@ static void get_monitor_by_name_callback(const gsr_monitor *monitor, void *userd } } -bool get_monitor_by_name(void *connection, gsr_connection_type connection_type, const char *name, gsr_monitor *monitor) { +bool get_monitor_by_name(const gsr_egl *egl, gsr_connection_type connection_type, const char *name, gsr_monitor *monitor) { get_monitor_by_name_userdata userdata; userdata.name = name; userdata.name_len = strlen(name); userdata.monitor = monitor; userdata.found_monitor = false; - for_each_active_monitor_output(connection, connection_type, get_monitor_by_name_callback, &userdata); + for_each_active_monitor_output(egl, connection_type, get_monitor_by_name_callback, &userdata); return userdata.found_monitor; } -- cgit v1.2.3