diff options
author | dec05eba <dec05eba@protonmail.com> | 2023-07-21 18:56:34 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2023-07-21 18:56:34 +0200 |
commit | a1d31a5cf0b90d3afabc57d5a6f4ccdfb1eeb054 (patch) | |
tree | 52376ebfde02b88b624e43c1d08f06fd512e599a | |
parent | 5a9673b1d751ca82f33db31c443dd2eb53cf693b (diff) |
Fix capture not working on intel + wayland
Ignore nouveau cards.
Fix lag on start of recording on amd/intel by only loading egl once,
on startup.
-rw-r--r-- | include/capture/kms_cuda.h | 1 | ||||
-rw-r--r-- | include/capture/kms_vaapi.h | 1 | ||||
-rw-r--r-- | include/capture/nvfbc.h | 2 | ||||
-rw-r--r-- | include/capture/xcomposite_cuda.h | 2 | ||||
-rw-r--r-- | include/capture/xcomposite_vaapi.h | 2 | ||||
-rw-r--r-- | include/utils.h | 5 | ||||
-rw-r--r-- | src/capture/kms_cuda.c | 63 | ||||
-rw-r--r-- | src/capture/kms_vaapi.c | 116 | ||||
-rw-r--r-- | src/capture/xcomposite_cuda.c | 69 | ||||
-rw-r--r-- | src/capture/xcomposite_vaapi.c | 82 | ||||
-rw-r--r-- | src/color_conversion.c | 4 | ||||
-rw-r--r-- | src/main.cpp | 28 | ||||
-rw-r--r-- | src/utils.c | 56 |
13 files changed, 222 insertions, 209 deletions
diff --git a/include/capture/kms_cuda.h b/include/capture/kms_cuda.h index e34802e..b631226 100644 --- a/include/capture/kms_cuda.h +++ b/include/capture/kms_cuda.h @@ -8,6 +8,7 @@ typedef struct _XDisplay Display; typedef struct { + gsr_egl *egl; const char *display_to_capture; /* if this is "screen", then the entire x11 screen is captured (all displays). A copy is made of this */ gsr_gpu_info gpu_inf; const char *card_path; /* reference */ diff --git a/include/capture/kms_vaapi.h b/include/capture/kms_vaapi.h index e7bd652..77b292a 100644 --- a/include/capture/kms_vaapi.h +++ b/include/capture/kms_vaapi.h @@ -8,6 +8,7 @@ typedef struct _XDisplay Display; typedef struct { + gsr_egl *egl; const char *display_to_capture; /* if this is "screen", then the entire x11 screen is captured (all displays). A copy is made of this */ gsr_gpu_info gpu_inf; const char *card_path; /* reference */ diff --git a/include/capture/nvfbc.h b/include/capture/nvfbc.h index 431777b..5678473 100644 --- a/include/capture/nvfbc.h +++ b/include/capture/nvfbc.h @@ -2,12 +2,14 @@ #define GSR_CAPTURE_NVFBC_H #include "capture.h" +#include "../egl.h" #include "../vec2.h" typedef struct _XDisplay Display; typedef struct { Display *dpy; + gsr_egl *egl; const char *display_to_capture; /* if this is "screen", then the entire x11 screen is captured (all displays). A copy is made of this */ int fps; vec2i pos; diff --git a/include/capture/xcomposite_cuda.h b/include/capture/xcomposite_cuda.h index 20d358e..547a6f3 100644 --- a/include/capture/xcomposite_cuda.h +++ b/include/capture/xcomposite_cuda.h @@ -2,12 +2,14 @@ #define GSR_CAPTURE_XCOMPOSITE_CUDA_H #include "capture.h" +#include "../egl.h" #include "../vec2.h" #include <X11/X.h> typedef struct _XDisplay Display; typedef struct { + gsr_egl *egl; 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 6cf30c3..bba485b 100644 --- a/include/capture/xcomposite_vaapi.h +++ b/include/capture/xcomposite_vaapi.h @@ -2,12 +2,14 @@ #define GSR_CAPTURE_XCOMPOSITE_VAAPI_H #include "capture.h" +#include "../egl.h" #include "../vec2.h" #include <X11/X.h> typedef struct _XDisplay Display; typedef struct { + gsr_egl *egl; 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/utils.h b/include/utils.h index 32977a1..392591a 100644 --- a/include/utils.h +++ b/include/utils.h @@ -2,6 +2,7 @@ #define GSR_UTILS_H #include "vec2.h" +#include "../include/egl.h" #include <stdbool.h> #include <stdint.h> #include <X11/extensions/Xrandr.h> @@ -45,10 +46,12 @@ typedef void (*active_monitor_callback)(const gsr_monitor *monitor, void *userda 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); -bool gl_get_gpu_info(Display *dpy, gsr_gpu_info *info, bool wayland); +bool gl_get_gpu_info(gsr_egl *egl, gsr_gpu_info *info); /* |output| should be at least 128 bytes in size */ bool gsr_get_valid_card_path(char *output); +/* |render_path| should be at least 128 bytes in size */ +bool gsr_card_path_get_render_path(const char *card_path, char *render_path); int even_number_ceil(int value); diff --git a/src/capture/kms_cuda.c b/src/capture/kms_cuda.c index 284ec77..e8a4212 100644 --- a/src/capture/kms_cuda.c +++ b/src/capture/kms_cuda.c @@ -1,6 +1,5 @@ #include "../../include/capture/kms_cuda.h" #include "../../kms/client/kms_client.h" -#include "../../include/egl.h" #include "../../include/utils.h" #include "../../include/cuda.h" #include <stdlib.h> @@ -29,7 +28,6 @@ typedef struct { bool stop_is_error; bool created_hw_frame; - gsr_egl egl; gsr_cuda cuda; gsr_kms_client kms_client; @@ -133,16 +131,10 @@ static void monitor_callback(const gsr_monitor *monitor, void *userdata) { static int gsr_capture_kms_cuda_start(gsr_capture *cap, AVCodecContext *video_codec_context) { gsr_capture_kms_cuda *cap_kms = cap->priv; - if(!gsr_egl_load(&cap_kms->egl, NULL, true)) { - fprintf(stderr, "gsr error: gsr_capture_kms_cuda_start: failed to load opengl\n"); - gsr_capture_kms_cuda_stop(cap, video_codec_context); - return -1; - } - gsr_monitor monitor; cap_kms->monitor_id.num_connector_ids = 0; - if(gsr_egl_start_capture(&cap_kms->egl, cap_kms->params.display_to_capture)) { - if(!get_monitor_by_name(&cap_kms->egl, GSR_CONNECTION_WAYLAND, cap_kms->params.display_to_capture, &monitor)) { + if(gsr_egl_start_capture(cap_kms->params.egl, cap_kms->params.display_to_capture)) { + if(!get_monitor_by_name(cap_kms->params.egl, GSR_CONNECTION_WAYLAND, 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; @@ -175,7 +167,7 @@ static int gsr_capture_kms_cuda_start(gsr_capture *cap, AVCodecContext *video_co video_codec_context->height = max_int(2, even_number_ceil(cap_kms->capture_size.y)); /* Disable vsync */ - cap_kms->egl.eglSwapInterval(cap_kms->egl.egl_display, 0); + cap_kms->params.egl->eglSwapInterval(cap_kms->params.egl->egl_display, 0); // TODO: overclocking is not supported on wayland... if(!gsr_cuda_load(&cap_kms->cuda, NULL, false)) { @@ -200,7 +192,7 @@ static void gsr_capture_kms_cuda_tick(gsr_capture *cap, AVCodecContext *video_co gsr_capture_kms_cuda *cap_kms = cap->priv; // TODO: - cap_kms->egl.glClear(GL_COLOR_BUFFER_BIT); + cap_kms->params.egl->glClear(GL_COLOR_BUFFER_BIT); if(!cap_kms->created_hw_frame) { cap_kms->created_hw_frame = true; @@ -333,14 +325,14 @@ static int gsr_capture_kms_cuda_capture(gsr_capture *cap, AVFrame *frame) { gsr_kms_response_fd *drm_fd = NULL; if(cap_kms->using_wayland_capture) { - gsr_egl_update(&cap_kms->egl); - cap_kms->wayland_kms_data.fd = cap_kms->egl.fd; - cap_kms->wayland_kms_data.width = cap_kms->egl.width; - cap_kms->wayland_kms_data.height = cap_kms->egl.height; - cap_kms->wayland_kms_data.pitch = cap_kms->egl.pitch; - cap_kms->wayland_kms_data.offset = cap_kms->egl.offset; - cap_kms->wayland_kms_data.pixel_format = cap_kms->egl.pixel_format; - cap_kms->wayland_kms_data.modifier = cap_kms->egl.modifier; + gsr_egl_update(cap_kms->params.egl); + cap_kms->wayland_kms_data.fd = cap_kms->params.egl->fd; + cap_kms->wayland_kms_data.width = cap_kms->params.egl->width; + cap_kms->wayland_kms_data.height = cap_kms->params.egl->height; + cap_kms->wayland_kms_data.pitch = cap_kms->params.egl->pitch; + cap_kms->wayland_kms_data.offset = cap_kms->params.egl->offset; + cap_kms->wayland_kms_data.pixel_format = cap_kms->params.egl->pixel_format; + cap_kms->wayland_kms_data.modifier = cap_kms->params.egl->modifier; cap_kms->wayland_kms_data.connector_id = 0; cap_kms->wayland_kms_data.is_combined_plane = false; @@ -383,28 +375,28 @@ static int gsr_capture_kms_cuda_capture(gsr_capture *cap, AVFrame *frame) { const intptr_t img_attr[] = { //EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, - EGL_LINUX_DRM_FOURCC_EXT, fourcc('A', 'R', '2', '4'),//cap_kms->egl.pixel_format, ARGB8888 - EGL_WIDTH, drm_fd->width,//cap_kms->egl.width, - EGL_HEIGHT, drm_fd->height,//cap_kms->egl.height, - EGL_DMA_BUF_PLANE0_FD_EXT, drm_fd->fd,//cap_kms->egl.fd, - EGL_DMA_BUF_PLANE0_OFFSET_EXT, drm_fd->offset,//cap_kms->egl.offset, - EGL_DMA_BUF_PLANE0_PITCH_EXT, drm_fd->pitch,//cap_kms->egl.pitch, - EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, drm_fd->modifier & 0xFFFFFFFFULL,//cap_kms->egl.modifier & 0xFFFFFFFFULL, - EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, drm_fd->modifier >> 32ULL,//cap_kms->egl.modifier >> 32ULL, + EGL_LINUX_DRM_FOURCC_EXT, fourcc('A', 'R', '2', '4'),//cap_kms->params.egl->pixel_format, ARGB8888 + EGL_WIDTH, drm_fd->width,//cap_kms->params.egl->width, + EGL_HEIGHT, drm_fd->height,//cap_kms->params.egl->height, + EGL_DMA_BUF_PLANE0_FD_EXT, drm_fd->fd,//cap_kms->params.egl->fd, + EGL_DMA_BUF_PLANE0_OFFSET_EXT, drm_fd->offset,//cap_kms->params.egl->offset, + EGL_DMA_BUF_PLANE0_PITCH_EXT, drm_fd->pitch,//cap_kms->params.egl->pitch, + EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, drm_fd->modifier & 0xFFFFFFFFULL,//cap_kms->params.egl->modifier & 0xFFFFFFFFULL, + EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, drm_fd->modifier >> 32ULL,//cap_kms->params.egl->modifier >> 32ULL, EGL_NONE }; - while(cap_kms->egl.glGetError()) {} - while(cap_kms->egl.eglGetError() != EGL_SUCCESS){} - EGLImage image = cap_kms->egl.eglCreateImage(cap_kms->egl.egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, img_attr); - if(cap_kms->egl.glGetError() != 0 || cap_kms->egl.eglGetError() != EGL_SUCCESS) { + while(cap_kms->params.egl->glGetError()) {} + while(cap_kms->params.egl->eglGetError() != EGL_SUCCESS){} + EGLImage image = cap_kms->params.egl->eglCreateImage(cap_kms->params.egl->egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, img_attr); + if(cap_kms->params.egl->glGetError() != 0 || cap_kms->params.egl->eglGetError() != EGL_SUCCESS) { fprintf(stderr, "egl error!\n"); } gsr_capture_kms_register_egl_image_in_cuda(cap_kms, image); - cap_kms->egl.eglDestroyImage(cap_kms->egl.egl_display, image); + cap_kms->params.egl->eglDestroyImage(cap_kms->params.egl->egl_display, image); - cap_kms->egl.eglSwapBuffers(cap_kms->egl.egl_display, cap_kms->egl.egl_surface); + cap_kms->params.egl->eglSwapBuffers(cap_kms->params.egl->egl_display, cap_kms->params.egl->egl_surface); frame->linesize[0] = frame->width * 4; @@ -427,7 +419,7 @@ static int gsr_capture_kms_cuda_capture(gsr_capture *cap, AVFrame *frame) { gsr_capture_kms_unload_cuda_graphics(cap_kms); - gsr_egl_cleanup_frame(&cap_kms->egl); + gsr_egl_cleanup_frame(cap_kms->params.egl); for(int i = 0; i < cap_kms->kms_response.num_fds; ++i) { if(cap_kms->kms_response.fds[i].fd > 0) @@ -457,7 +449,6 @@ static void gsr_capture_kms_cuda_stop(gsr_capture *cap, AVCodecContext *video_co av_buffer_unref(&video_codec_context->hw_frames_ctx); gsr_cuda_unload(&cap_kms->cuda); - gsr_egl_unload(&cap_kms->egl); gsr_kms_client_deinit(&cap_kms->kms_client); } diff --git a/src/capture/kms_vaapi.c b/src/capture/kms_vaapi.c index 912e260..5348509 100644 --- a/src/capture/kms_vaapi.c +++ b/src/capture/kms_vaapi.c @@ -1,6 +1,5 @@ #include "../../include/capture/kms_vaapi.h" #include "../../kms/client/kms_client.h" -#include "../../include/egl.h" #include "../../include/utils.h" #include "../../include/color_conversion.h" #include "../../include/cursor.h" @@ -39,8 +38,6 @@ typedef struct { bool should_stop; bool stop_is_error; bool created_hw_frame; - - gsr_egl egl; gsr_kms_client kms_client; gsr_kms_response kms_response; @@ -75,8 +72,14 @@ static int max_int(int a, int b) { static void gsr_capture_kms_vaapi_stop(gsr_capture *cap, AVCodecContext *video_codec_context); 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); + return false; + } + AVBufferRef *device_ctx; - if(av_hwdevice_ctx_create(&device_ctx, AV_HWDEVICE_TYPE_VAAPI, cap_kms->params.card_path, NULL, 0) < 0) { + if(av_hwdevice_ctx_create(&device_ctx, AV_HWDEVICE_TYPE_VAAPI, render_path, NULL, 0) < 0) { fprintf(stderr, "Error: Failed to create hardware device context\n"); return false; } @@ -194,16 +197,10 @@ static int gsr_capture_kms_vaapi_start(gsr_capture *cap, AVCodecContext *video_c cap_kms->x11_rot = X11_ROT_0; - if(!gsr_egl_load(&cap_kms->egl, cap_kms->dpy, cap_kms->params.wayland)) { - fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_start: failed to load opengl\n"); - gsr_capture_kms_vaapi_stop(cap, video_codec_context); - return -1; - } - gsr_monitor monitor; cap_kms->monitor_id.num_connector_ids = 0; - if(cap_kms->params.wayland && gsr_egl_start_capture(&cap_kms->egl, cap_kms->params.display_to_capture)) { - if(!get_monitor_by_name(&cap_kms->egl, GSR_CONNECTION_WAYLAND, cap_kms->params.display_to_capture, &monitor)) { + if(gsr_egl_start_capture(cap_kms->params.egl, cap_kms->params.display_to_capture)) { + if(!get_monitor_by_name(cap_kms->params.egl, GSR_CONNECTION_WAYLAND, 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_vaapi_stop(cap, video_codec_context); return -1; @@ -269,7 +266,7 @@ static int gsr_capture_kms_vaapi_start(gsr_capture *cap, AVCodecContext *video_c cap_kms->capture_size = monitor.size; /* Disable vsync */ - cap_kms->egl.eglSwapInterval(cap_kms->egl.egl_display, 0); + cap_kms->params.egl->eglSwapInterval(cap_kms->params.egl->egl_display, 0); video_codec_context->width = max_int(2, even_number_ceil(cap_kms->capture_size.x)); video_codec_context->height = max_int(2, even_number_ceil(cap_kms->capture_size.y)); @@ -280,7 +277,7 @@ static int gsr_capture_kms_vaapi_start(gsr_capture *cap, AVCodecContext *video_c } if(cap_kms->dpy && !cap_kms->params.wayland) { - if(gsr_cursor_init(&cap_kms->cursor, &cap_kms->egl, cap_kms->dpy) != 0) { + if(gsr_cursor_init(&cap_kms->cursor, cap_kms->params.egl, cap_kms->dpy) != 0) { gsr_capture_kms_vaapi_stop(cap, video_codec_context); return -1; } @@ -302,7 +299,7 @@ static void gsr_capture_kms_vaapi_tick(gsr_capture *cap, AVCodecContext *video_c gsr_capture_kms_vaapi *cap_kms = cap->priv; // TODO: - cap_kms->egl.glClear(GL_COLOR_BUFFER_BIT); + cap_kms->params.egl->glClear(GL_COLOR_BUFFER_BIT); if(cap_kms->dpy && !cap_kms->params.wayland) { while(XPending(cap_kms->dpy)) { @@ -350,16 +347,16 @@ static void gsr_capture_kms_vaapi_tick(gsr_capture *cap, AVCodecContext *video_c } vaSyncSurface(cap_kms->va_dpy, target_surface_id); - cap_kms->egl.glGenTextures(1, &cap_kms->input_texture); - cap_kms->egl.glBindTexture(GL_TEXTURE_2D, cap_kms->input_texture); - cap_kms->egl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - cap_kms->egl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - cap_kms->egl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - cap_kms->egl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - cap_kms->egl.glBindTexture(GL_TEXTURE_2D, 0); + cap_kms->params.egl->glGenTextures(1, &cap_kms->input_texture); + cap_kms->params.egl->glBindTexture(GL_TEXTURE_2D, cap_kms->input_texture); + cap_kms->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + cap_kms->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + cap_kms->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + cap_kms->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + cap_kms->params.egl->glBindTexture(GL_TEXTURE_2D, 0); if(cap_kms->prime.fourcc == FOURCC_NV12) { - cap_kms->egl.glGenTextures(2, cap_kms->target_textures); + cap_kms->params.egl->glGenTextures(2, cap_kms->target_textures); for(int i = 0; i < 2; ++i) { const uint32_t formats[2] = { fourcc('R', '8', ' ', ' '), fourcc('G', 'R', '8', '8') }; const int layer = i; @@ -381,40 +378,40 @@ static void gsr_capture_kms_vaapi_tick(gsr_capture *cap, AVCodecContext *video_c EGL_NONE }; - while(cap_kms->egl.eglGetError() != EGL_SUCCESS){} - EGLImage image = cap_kms->egl.eglCreateImage(cap_kms->egl.egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, img_attr); + while(cap_kms->params.egl->eglGetError() != EGL_SUCCESS){} + EGLImage image = cap_kms->params.egl->eglCreateImage(cap_kms->params.egl->egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, img_attr); if(!image) { - fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: failed to create egl image from drm fd for output drm fd, error: %d\n", cap_kms->egl.eglGetError()); + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: failed to create egl image from drm fd for output drm fd, error: %d\n", cap_kms->params.egl->eglGetError()); cap_kms->should_stop = true; cap_kms->stop_is_error = true; return; } - cap_kms->egl.glBindTexture(GL_TEXTURE_2D, cap_kms->target_textures[i]); - cap_kms->egl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - cap_kms->egl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - cap_kms->egl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - cap_kms->egl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + cap_kms->params.egl->glBindTexture(GL_TEXTURE_2D, cap_kms->target_textures[i]); + cap_kms->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + cap_kms->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + cap_kms->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + cap_kms->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - while(cap_kms->egl.glGetError()) {} - while(cap_kms->egl.eglGetError() != EGL_SUCCESS){} - cap_kms->egl.glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image); - if(cap_kms->egl.glGetError() != 0 || cap_kms->egl.eglGetError() != EGL_SUCCESS) { + while(cap_kms->params.egl->glGetError()) {} + while(cap_kms->params.egl->eglGetError() != EGL_SUCCESS){} + cap_kms->params.egl->glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image); + if(cap_kms->params.egl->glGetError() != 0 || cap_kms->params.egl->eglGetError() != EGL_SUCCESS) { // TODO: Get the error properly - fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: failed to bind egl image to gl texture, error: %d\n", cap_kms->egl.eglGetError()); + fprintf(stderr, "gsr error: gsr_capture_kms_vaapi_tick: failed to bind egl image to gl texture, error: %d\n", cap_kms->params.egl->eglGetError()); cap_kms->should_stop = true; cap_kms->stop_is_error = true; - cap_kms->egl.eglDestroyImage(cap_kms->egl.egl_display, image); - cap_kms->egl.glBindTexture(GL_TEXTURE_2D, 0); + cap_kms->params.egl->eglDestroyImage(cap_kms->params.egl->egl_display, image); + cap_kms->params.egl->glBindTexture(GL_TEXTURE_2D, 0); return; } - cap_kms->egl.eglDestroyImage(cap_kms->egl.egl_display, image); - cap_kms->egl.glBindTexture(GL_TEXTURE_2D, 0); + cap_kms->params.egl->eglDestroyImage(cap_kms->params.egl->egl_display, image); + cap_kms->params.egl->glBindTexture(GL_TEXTURE_2D, 0); } gsr_color_conversion_params color_conversion_params = {0}; - color_conversion_params.egl = &cap_kms->egl; + color_conversion_params.egl = cap_kms->params.egl; color_conversion_params.source_color = GSR_SOURCE_COLOR_RGB; color_conversion_params.destination_color = GSR_DESTINATION_COLOR_NV12; @@ -496,14 +493,14 @@ static int gsr_capture_kms_vaapi_capture(gsr_capture *cap, AVFrame *frame) { gsr_kms_response_fd *drm_fd = NULL; bool requires_rotation = cap_kms->requires_rotation; if(cap_kms->using_wayland_capture) { - gsr_egl_update(&cap_kms->egl); - cap_kms->wayland_kms_data.fd = cap_kms->egl.fd; - cap_kms->wayland_kms_data.width = cap_kms->egl.width; - cap_kms->wayland_kms_data.height = cap_kms->egl.height; - cap_kms->wayland_kms_data.pitch = cap_kms->egl.pitch; - cap_kms->wayland_kms_data.offset = cap_kms->egl.offset; - cap_kms->wayland_kms_data.pixel_format = cap_kms->egl.pixel_format; - cap_kms->wayland_kms_data.modifier = cap_kms->egl.modifier; + gsr_egl_update(cap_kms->params.egl); + cap_kms->wayland_kms_data.fd = cap_kms->params.egl->fd; + cap_kms->wayland_kms_data.width = cap_kms->params.egl->width; + cap_kms->wayland_kms_data.height = cap_kms->params.egl->height; + cap_kms->wayland_kms_data.pitch = cap_kms->params.egl->pitch; + cap_kms->wayland_kms_data.offset = cap_kms->params.egl->offset; + cap_kms->wayland_kms_data.pixel_format = cap_kms->params.egl->pixel_format; + cap_kms->wayland_kms_data.modifier = cap_kms->params.egl->modifier; cap_kms->wayland_kms_data.connector_id = 0; cap_kms->wayland_kms_data.is_combined_plane = false; @@ -583,11 +580,11 @@ static int gsr_capture_kms_vaapi_capture(gsr_capture *cap, AVFrame *frame) { EGL_NONE }; - EGLImage image = cap_kms->egl.eglCreateImage(cap_kms->egl.egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, img_attr); - cap_kms->egl.glBindTexture(GL_TEXTURE_2D, cap_kms->input_texture); - cap_kms->egl.glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image); - cap_kms->egl.eglDestroyImage(cap_kms->egl.egl_display, image); - cap_kms->egl.glBindTexture(GL_TEXTURE_2D, 0); + EGLImage image = cap_kms->params.egl->eglCreateImage(cap_kms->params.egl->egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, img_attr); + cap_kms->params.egl->glBindTexture(GL_TEXTURE_2D, cap_kms->input_texture); + cap_kms->params.egl->glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image); + cap_kms->params.egl->eglDestroyImage(cap_kms->params.egl->egl_display, image); + cap_kms->params.egl->glBindTexture(GL_TEXTURE_2D, 0); if(cap_kms->using_wayland_capture) { gsr_color_conversion_draw(&cap_kms->color_conversion, cap_kms->input_texture, @@ -638,9 +635,9 @@ static int gsr_capture_kms_vaapi_capture(gsr_capture *cap, AVFrame *frame) { } } - cap_kms->egl.eglSwapBuffers(cap_kms->egl.egl_display, cap_kms->egl.egl_surface); + cap_kms->params.egl->eglSwapBuffers(cap_kms->params.egl->egl_display, cap_kms->params.egl->egl_surface); - gsr_egl_cleanup_frame(&cap_kms->egl); + gsr_egl_cleanup_frame(cap_kms->params.egl); for(int i = 0; i < cap_kms->kms_response.num_fds; ++i) { if(cap_kms->kms_response.fds[i].fd > 0) @@ -665,13 +662,13 @@ static void gsr_capture_kms_vaapi_stop(gsr_capture *cap, AVCodecContext *video_c } } - if(cap_kms->egl.egl_context) { + if(cap_kms->params.egl->egl_context) { if(cap_kms->input_texture) { - cap_kms->egl.glDeleteTextures(1, &cap_kms->input_texture); + cap_kms->params.egl->glDeleteTextures(1, &cap_kms->input_texture); cap_kms->input_texture = 0; } - cap_kms->egl.glDeleteTextures(2, cap_kms->target_textures); + cap_kms->params.egl->glDeleteTextures(2, cap_kms->target_textures); cap_kms->target_textures[0] = 0; cap_kms->target_textures[1] = 0; } @@ -688,7 +685,6 @@ static void gsr_capture_kms_vaapi_stop(gsr_capture *cap, AVCodecContext *video_c if(video_codec_context->hw_frames_ctx) av_buffer_unref(&video_codec_context->hw_frames_ctx); - gsr_egl_unload(&cap_kms->egl); gsr_kms_client_deinit(&cap_kms->kms_client); if(cap_kms->dpy) { // TODO: This causes a crash, why? maybe some other library dlclose xlib and that also happened to unload this??? diff --git a/src/capture/xcomposite_cuda.c b/src/capture/xcomposite_cuda.c index fd967a3..9867f84 100644 --- a/src/capture/xcomposite_cuda.c +++ b/src/capture/xcomposite_cuda.c @@ -1,5 +1,4 @@ #include "../../include/capture/xcomposite_cuda.h" -#include "../../include/egl.h" #include "../../include/cuda.h" #include "../../include/window_texture.h" #include "../../include/utils.h" @@ -31,7 +30,6 @@ typedef struct { CUgraphicsResource cuda_graphics_resource; CUarray mapped_array; - gsr_egl egl; gsr_cuda cuda; } gsr_capture_xcomposite_cuda; @@ -143,16 +141,16 @@ static bool cuda_create_codec_context(gsr_capture_xcomposite_cuda *cap_xcomp, AV static unsigned int gl_create_texture(gsr_capture_xcomposite_cuda *cap_xcomp, int width, int height) { unsigned int texture_id = 0; - cap_xcomp->egl.glGenTextures(1, &texture_id); - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, texture_id); - cap_xcomp->egl.glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + cap_xcomp->params.egl->glGenTextures(1, &texture_id); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, texture_id); + cap_xcomp->params.egl->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); - cap_xcomp->egl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - cap_xcomp->egl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - cap_xcomp->egl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - cap_xcomp->egl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + cap_xcomp->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + cap_xcomp->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + cap_xcomp->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + cap_xcomp->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, 0); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0); return texture_id; } @@ -188,25 +186,19 @@ static int gsr_capture_xcomposite_cuda_start(gsr_capture *cap, AVCodecContext *v XSelectInput(cap_xcomp->dpy, cap_xcomp->window, StructureNotifyMask | ExposureMask); - if(!gsr_egl_load(&cap_xcomp->egl, cap_xcomp->dpy, false)) { - fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_start: failed to load opengl\n"); - return -1; - } - - cap_xcomp->egl.eglSwapInterval(cap_xcomp->egl.egl_display, 0); - if(window_texture_init(&cap_xcomp->window_texture, cap_xcomp->dpy, cap_xcomp->window, &cap_xcomp->egl) != 0 && !cap_xcomp->params.follow_focused) { + cap_xcomp->params.egl->eglSwapInterval(cap_xcomp->params.egl->egl_display, 0); + if(window_texture_init(&cap_xcomp->window_texture, cap_xcomp->dpy, cap_xcomp->window, cap_xcomp->params.egl) != 0 && !cap_xcomp->params.follow_focused) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_cuda_start: failed get window texture for window %ld\n", cap_xcomp->window); - gsr_egl_unload(&cap_xcomp->egl); return -1; } cap_xcomp->texture_size.x = 0; cap_xcomp->texture_size.y = 0; - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cap_xcomp->texture_size.x); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, 0); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cap_xcomp->texture_size.x); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0); cap_xcomp->texture_size.x = max_int(2, even_number_ceil(cap_xcomp->texture_size.x)); cap_xcomp->texture_size.y = max_int(2, even_number_ceil(cap_xcomp->texture_size.y)); @@ -263,7 +255,7 @@ static void gsr_capture_xcomposite_cuda_stop(gsr_capture *cap, AVCodecContext *v window_texture_deinit(&cap_xcomp->window_texture); if(cap_xcomp->target_texture_id) { - cap_xcomp->egl.glDeleteTextures(1, &cap_xcomp->target_texture_id); + cap_xcomp->params.egl->glDeleteTextures(1, &cap_xcomp->target_texture_id); cap_xcomp->target_texture_id = 0; } @@ -274,7 +266,6 @@ static void gsr_capture_xcomposite_cuda_stop(gsr_capture *cap, AVCodecContext *v gsr_cuda_unload(&cap_xcomp->cuda); - gsr_egl_unload(&cap_xcomp->egl); if(cap_xcomp->dpy) { // TODO: This causes a crash, why? maybe some other library dlclose xlib and that also happened to unload this??? //XCloseDisplay(cap_xcomp->dpy); @@ -285,7 +276,7 @@ static void gsr_capture_xcomposite_cuda_stop(gsr_capture *cap, AVCodecContext *v 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->egl.glClear(GL_COLOR_BUFFER_BIT); + cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); bool init_new_window = false; while(XPending(cap_xcomp->dpy)) { @@ -351,15 +342,15 @@ 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->dpy, cap_xcomp->window, &cap_xcomp->egl); // TODO: Do not do the below window_texture_on_resize after this + window_texture_init(&cap_xcomp->window_texture, cap_xcomp->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; - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cap_xcomp->texture_size.x); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, 0); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cap_xcomp->texture_size.x); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0); cap_xcomp->texture_size.x = min_int(video_codec_context->width, max_int(2, even_number_ceil(cap_xcomp->texture_size.x))); cap_xcomp->texture_size.y = min_int(video_codec_context->height, max_int(2, even_number_ceil(cap_xcomp->texture_size.y))); @@ -379,10 +370,10 @@ static void gsr_capture_xcomposite_cuda_tick(gsr_capture *cap, AVCodecContext *v cap_xcomp->texture_size.x = 0; cap_xcomp->texture_size.y = 0; - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cap_xcomp->texture_size.x); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, 0); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cap_xcomp->texture_size.x); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0); cap_xcomp->texture_size.x = min_int(video_codec_context->width, max_int(2, even_number_ceil(cap_xcomp->texture_size.x))); cap_xcomp->texture_size.y = min_int(video_codec_context->height, max_int(2, even_number_ceil(cap_xcomp->texture_size.y))); @@ -416,7 +407,7 @@ static void gsr_capture_xcomposite_cuda_tick(gsr_capture *cap, AVCodecContext *v // Clear texture with black background because the source texture (window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)) // might be smaller than cap_xcomp->target_texture_id - cap_xcomp->egl.glClearTexImage(cap_xcomp->target_texture_id, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + cap_xcomp->params.egl->glClearTexImage(cap_xcomp->target_texture_id, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); } } @@ -440,13 +431,13 @@ static int gsr_capture_xcomposite_cuda_capture(gsr_capture *cap, AVFrame *frame) vec2i source_size = cap_xcomp->texture_size; if(cap_xcomp->window_texture.texture_id != 0) { - while(cap_xcomp->egl.glGetError()) {} + while(cap_xcomp->params.egl->glGetError()) {} /* TODO: Remove this copy, which is only possible by using nvenc directly and encoding window_pixmap.target_texture_id */ - cap_xcomp->egl.glCopyImageSubData( + cap_xcomp->params.egl->glCopyImageSubData( window_texture_get_opengl_texture_id(&cap_xcomp->window_texture), GL_TEXTURE_2D, 0, source_pos.x, source_pos.y, 0, cap_xcomp->target_texture_id, GL_TEXTURE_2D, 0, 0, 0, 0, source_size.x, source_size.y, 1); - unsigned int err = cap_xcomp->egl.glGetError(); + unsigned int err = cap_xcomp->params.egl->glGetError(); if(err != 0) { static bool error_shown = false; if(!error_shown) { @@ -455,7 +446,7 @@ static int gsr_capture_xcomposite_cuda_capture(gsr_capture *cap, AVFrame *frame) } } } - cap_xcomp->egl.eglSwapBuffers(cap_xcomp->egl.egl_display, cap_xcomp->egl.egl_surface); + cap_xcomp->params.egl->eglSwapBuffers(cap_xcomp->params.egl->egl_display, cap_xcomp->params.egl->egl_surface); frame->linesize[0] = frame->width * 4; //frame->linesize[0] = frame->width * 1; diff --git a/src/capture/xcomposite_vaapi.c b/src/capture/xcomposite_vaapi.c index 752be69..4b1d9c4 100644 --- a/src/capture/xcomposite_vaapi.c +++ b/src/capture/xcomposite_vaapi.c @@ -1,5 +1,4 @@ #include "../../include/capture/xcomposite_vaapi.h" -#include "../../include/egl.h" #include "../../include/window_texture.h" #include "../../include/utils.h" #include <stdlib.h> @@ -30,8 +29,6 @@ typedef struct { WindowTexture window_texture; - gsr_egl egl; - int fourcc; int num_planes; uint64_t modifiers; @@ -74,8 +71,14 @@ 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); + return false; + } + AVBufferRef *device_ctx; - if(av_hwdevice_ctx_create(&device_ctx, AV_HWDEVICE_TYPE_VAAPI, cap_xcomp->params.card_path, NULL, 0) < 0) { + if(av_hwdevice_ctx_create(&device_ctx, AV_HWDEVICE_TYPE_VAAPI, render_path, NULL, 0) < 0) { fprintf(stderr, "Error: Failed to create hardware device context\n"); return false; } @@ -147,38 +150,30 @@ static int gsr_capture_xcomposite_vaapi_start(gsr_capture *cap, AVCodecContext * // 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->dpy, cap_xcomp->params.window, StructureNotifyMask | ExposureMask); - if(!gsr_egl_load(&cap_xcomp->egl, cap_xcomp->dpy, false)) { - fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start: failed to load opengl\n"); - return -1; - } - - if(!cap_xcomp->egl.eglExportDMABUFImageQueryMESA) { + if(!cap_xcomp->params.egl->eglExportDMABUFImageQueryMESA) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start: could not find eglExportDMABUFImageQueryMESA\n"); - gsr_egl_unload(&cap_xcomp->egl); return -1; } - if(!cap_xcomp->egl.eglExportDMABUFImageMESA) { + if(!cap_xcomp->params.egl->eglExportDMABUFImageMESA) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_start: could not find eglExportDMABUFImageMESA\n"); - gsr_egl_unload(&cap_xcomp->egl); return -1; } /* Disable vsync */ - cap_xcomp->egl.eglSwapInterval(cap_xcomp->egl.egl_display, 0); - if(window_texture_init(&cap_xcomp->window_texture, cap_xcomp->dpy, cap_xcomp->params.window, &cap_xcomp->egl) != 0 && !cap_xcomp->params.follow_focused) { + cap_xcomp->params.egl->eglSwapInterval(cap_xcomp->params.egl->egl_display, 0); + if(window_texture_init(&cap_xcomp->window_texture, cap_xcomp->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 get window texture for window %ld\n", cap_xcomp->params.window); - gsr_egl_unload(&cap_xcomp->egl); return -1; } cap_xcomp->texture_size.x = 0; cap_xcomp->texture_size.y = 0; - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cap_xcomp->texture_size.x); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, 0); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cap_xcomp->texture_size.x); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0); cap_xcomp->texture_size.x = max_int(2, even_number_ceil(cap_xcomp->texture_size.x)); cap_xcomp->texture_size.y = max_int(2, even_number_ceil(cap_xcomp->texture_size.y)); @@ -204,7 +199,7 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext * gsr_capture_xcomposite_vaapi *cap_xcomp = cap->priv; // TODO: - //cap_xcomp->egl.glClear(GL_COLOR_BUFFER_BIT); + //cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); bool init_new_window = false; while(XPending(cap_xcomp->dpy)) { @@ -270,15 +265,15 @@ 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->dpy, cap_xcomp->window, &cap_xcomp->egl); // TODO: Do not do the below window_texture_on_resize after this + window_texture_init(&cap_xcomp->window_texture, cap_xcomp->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; - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cap_xcomp->texture_size.x); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, 0); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cap_xcomp->texture_size.x); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0); cap_xcomp->texture_size.x = min_int(video_codec_context->width, max_int(2, even_number_ceil(cap_xcomp->texture_size.x))); cap_xcomp->texture_size.y = min_int(video_codec_context->height, max_int(2, even_number_ceil(cap_xcomp->texture_size.y))); @@ -299,10 +294,10 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext * cap_xcomp->texture_size.x = 0; cap_xcomp->texture_size.y = 0; - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cap_xcomp->texture_size.x); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, 0); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &cap_xcomp->texture_size.x); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &cap_xcomp->texture_size.y); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0); cap_xcomp->texture_size.x = min_int(video_codec_context->width, max_int(2, even_number_ceil(cap_xcomp->texture_size.x))); cap_xcomp->texture_size.y = min_int(video_codec_context->height, max_int(2, even_number_ceil(cap_xcomp->texture_size.y))); @@ -361,10 +356,10 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext * } int xx = 0, yy = 0; - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &xx); - cap_xcomp->egl.glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &yy); - cap_xcomp->egl.glBindTexture(GL_TEXTURE_2D, 0); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &xx); + cap_xcomp->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &yy); + cap_xcomp->params.egl->glBindTexture(GL_TEXTURE_2D, 0); const intptr_t pixmap_attrs[] = { EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, @@ -372,7 +367,7 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext * }; // TODO: Use the window texture egl image directly instead of exporting it to opengl texture and then importing it to egl image again - EGLImage img = cap_xcomp->egl.eglCreateImage(cap_xcomp->egl.egl_display, cap_xcomp->egl.egl_context, EGL_GL_TEXTURE_2D, (EGLClientBuffer)(uint64_t)window_texture_get_opengl_texture_id(&cap_xcomp->window_texture), pixmap_attrs); + EGLImage img = cap_xcomp->params.egl->eglCreateImage(cap_xcomp->params.egl->egl_display, cap_xcomp->params.egl->egl_context, EGL_GL_TEXTURE_2D, (EGLClientBuffer)(uint64_t)window_texture_get_opengl_texture_id(&cap_xcomp->window_texture), pixmap_attrs); if(!img) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_tick: eglCreateImage failed\n"); cap_xcomp->should_stop = true; @@ -380,11 +375,11 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext * return; } - if(!cap_xcomp->egl.eglExportDMABUFImageQueryMESA(cap_xcomp->egl.egl_display, img, &cap_xcomp->fourcc, &cap_xcomp->num_planes, &cap_xcomp->modifiers)) { + if(!cap_xcomp->params.egl->eglExportDMABUFImageQueryMESA(cap_xcomp->params.egl->egl_display, img, &cap_xcomp->fourcc, &cap_xcomp->num_planes, &cap_xcomp->modifiers)) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_tick: eglExportDMABUFImageQueryMESA failed\n"); cap_xcomp->should_stop = true; cap_xcomp->stop_is_error = true; - cap_xcomp->egl.eglDestroyImage(cap_xcomp->egl.egl_display, img); + cap_xcomp->params.egl->eglDestroyImage(cap_xcomp->params.egl->egl_display, img); return; } @@ -392,19 +387,19 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext * fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_tick: expected 1 plane for drm buf, got %d planes\n", cap_xcomp->num_planes); cap_xcomp->should_stop = true; cap_xcomp->stop_is_error = true; - cap_xcomp->egl.eglDestroyImage(cap_xcomp->egl.egl_display, img); + cap_xcomp->params.egl->eglDestroyImage(cap_xcomp->params.egl->egl_display, img); return; } - if(!cap_xcomp->egl.eglExportDMABUFImageMESA(cap_xcomp->egl.egl_display, img, &cap_xcomp->dmabuf_fd, &cap_xcomp->pitch, &cap_xcomp->offset)) { + if(!cap_xcomp->params.egl->eglExportDMABUFImageMESA(cap_xcomp->params.egl->egl_display, img, &cap_xcomp->dmabuf_fd, &cap_xcomp->pitch, &cap_xcomp->offset)) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_tick: eglExportDMABUFImageMESA failed\n"); cap_xcomp->should_stop = true; cap_xcomp->stop_is_error = true; - cap_xcomp->egl.eglDestroyImage(cap_xcomp->egl.egl_display, img); + cap_xcomp->params.egl->eglDestroyImage(cap_xcomp->params.egl->egl_display, img); return; } - cap_xcomp->egl.eglDestroyImage(cap_xcomp->egl.egl_display, img); + cap_xcomp->params.egl->eglDestroyImage(cap_xcomp->params.egl->egl_display, img); uintptr_t dmabuf = cap_xcomp->dmabuf_fd; @@ -498,7 +493,7 @@ static void gsr_capture_xcomposite_vaapi_tick(gsr_capture *cap, AVCodecContext * // Clear texture with black background because the source texture (window_texture_get_opengl_texture_id(&cap_xcomp->window_texture)) // might be smaller than cap_xcomp->target_texture_id // TODO: - //cap_xcomp->egl.glClearTexImage(cap_xcomp->target_texture_id, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + //cap_xcomp->params.egl->glClearTexImage(cap_xcomp->target_texture_id, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); } } @@ -555,7 +550,7 @@ static int gsr_capture_xcomposite_vaapi_capture(gsr_capture *cap, AVFrame *frame //vaSyncSurface(cap_xcomp->va_dpy, target_surface_id); // TODO: Remove - //cap_xcomp->egl.eglSwapBuffers(cap_xcomp->egl.egl_display, cap_xcomp->egl.egl_surface); + //cap_xcomp->params.egl->eglSwapBuffers(cap_xcomp->params.egl->egl_display, cap_xcomp->params.egl->egl_surface); return 0; } @@ -596,7 +591,6 @@ static void gsr_capture_xcomposite_vaapi_stop(gsr_capture *cap, AVCodecContext * if(video_codec_context->hw_frames_ctx) av_buffer_unref(&video_codec_context->hw_frames_ctx); - gsr_egl_unload(&cap_xcomp->egl); if(cap_xcomp->dpy) { // TODO: This causes a crash, why? maybe some other library dlclose xlib and that also happened to unload this??? //XCloseDisplay(cap_xcomp->dpy); diff --git a/src/color_conversion.c b/src/color_conversion.c index 5b61325..8f8d4bc 100644 --- a/src/color_conversion.c +++ b/src/color_conversion.c @@ -322,7 +322,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); // 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) + //cap_xcomp->params.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); @@ -331,7 +331,7 @@ int gsr_color_conversion_draw(gsr_color_conversion *self, unsigned int texture_i if(self->params.num_destination_textures > 1) { self->params.egl->glBindFramebuffer(GL_FRAMEBUFFER, self->framebuffers[1]); - //cap_xcomp->egl.glClear(GL_COLOR_BUFFER_BIT); + //cap_xcomp->params.egl->glClear(GL_COLOR_BUFFER_BIT); gsr_shader_use(&self->shaders[1]); self->params.egl->glUniform1f(self->rotation_uniforms[1], rotation); diff --git a/src/main.cpp b/src/main.cpp index cbbc594..1c1e68c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -374,8 +374,14 @@ static AVCodecContext *create_video_codec_context(AVPixelFormat pix_fmt, } static bool vaapi_create_codec_context(AVCodecContext *video_codec_context, const char *card_path) { + char render_path[128]; + if(!gsr_card_path_get_render_path(card_path, render_path)) { + fprintf(stderr, "gsr error: failed to get /dev/dri/renderDXXX file from %s\n", card_path); + return false; + } + AVBufferRef *device_ctx; - if(av_hwdevice_ctx_create(&device_ctx, AV_HWDEVICE_TYPE_VAAPI, card_path, NULL, 0) < 0) { + if(av_hwdevice_ctx_create(&device_ctx, AV_HWDEVICE_TYPE_VAAPI, render_path, NULL, 0) < 0) { fprintf(stderr, "Error: Failed to create hardware device context\n"); return false; } @@ -1300,9 +1306,15 @@ int main(int argc, char **argv) { if(!wayland) wayland = is_xwayland(dpy); + gsr_egl egl; + if(!gsr_egl_load(&egl, dpy, wayland)) { + fprintf(stderr, "gsr error: failed to load opengl\n"); + _exit(1); + } + gsr_gpu_info gpu_inf; bool very_old_gpu = false; - if(!gl_get_gpu_info(dpy, &gpu_inf, wayland)) + if(!gl_get_gpu_info(&egl, &gpu_inf)) _exit(2); if(gpu_inf.vendor == GSR_GPU_VENDOR_NVIDIA && gpu_inf.gpu_version != 0 && gpu_inf.gpu_version < 900) { @@ -1379,12 +1391,6 @@ int main(int argc, char **argv) { } else if(contains_non_hex_number(window_str)) { // TODO: wayland, not only drm (if wlroots) if(wayland) { - gsr_egl egl; - if(!gsr_egl_load(&egl, NULL, true)) { - fprintf(stderr, "gsr error: failed to load opengl\n"); - _exit(1); - } - if(gsr_egl_supports_wayland_capture(&egl)) { gsr_monitor gmon; if(!get_monitor_by_name(&egl, GSR_CONNECTION_WAYLAND, window_str, &gmon)) { @@ -1400,7 +1406,6 @@ int main(int argc, char **argv) { _exit(1); } } - gsr_egl_unload(&egl); } else { if(strcmp(window_str, "screen") != 0 && strcmp(window_str, "screen-direct") != 0 && strcmp(window_str, "screen-direct-force") != 0) { gsr_monitor gmon; @@ -1423,6 +1428,7 @@ int main(int argc, char **argv) { } gsr_capture_kms_cuda_params kms_params; + kms_params.egl = &egl; kms_params.display_to_capture = capture_target; kms_params.gpu_inf = gpu_inf; kms_params.card_path = card_path; @@ -1463,6 +1469,7 @@ int main(int argc, char **argv) { } gsr_capture_kms_vaapi_params kms_params; + kms_params.egl = &egl; kms_params.display_to_capture = capture_target; kms_params.gpu_inf = gpu_inf; kms_params.card_path = card_path; @@ -1489,6 +1496,7 @@ int main(int argc, char **argv) { switch(gpu_inf.vendor) { case GSR_GPU_VENDOR_AMD: { gsr_capture_xcomposite_vaapi_params xcomposite_params; + xcomposite_params.egl = &egl; xcomposite_params.window = src_window_id; xcomposite_params.follow_focused = follow_focused; xcomposite_params.region_size = region_size; @@ -1500,6 +1508,7 @@ int main(int argc, char **argv) { } case GSR_GPU_VENDOR_INTEL: { gsr_capture_xcomposite_vaapi_params xcomposite_params; + xcomposite_params.egl = &egl; xcomposite_params.window = src_window_id; xcomposite_params.follow_focused = follow_focused; xcomposite_params.region_size = region_size; @@ -1511,6 +1520,7 @@ int main(int argc, char **argv) { } case GSR_GPU_VENDOR_NVIDIA: { gsr_capture_xcomposite_cuda_params xcomposite_params; + xcomposite_params.egl = &egl; xcomposite_params.window = src_window_id; xcomposite_params.follow_focused = follow_focused; xcomposite_params.region_size = region_size; diff --git a/src/utils.c b/src/utils.c index be77923..88893ac 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,5 +1,4 @@ #include "../include/utils.h" -#include "../include/egl.h" #include <time.h> #include <string.h> #include <stdio.h> @@ -7,6 +6,7 @@ #include <fcntl.h> #include <xf86drmMode.h> #include <xf86drm.h> +#include <stdlib.h> double clock_get_monotonic_seconds(void) { struct timespec ts; @@ -209,16 +209,10 @@ bool get_monitor_by_name(void *connection, gsr_connection_type connection_type, return userdata.found_monitor; } -bool gl_get_gpu_info(Display *dpy, gsr_gpu_info *info, bool wayland) { - gsr_egl gl; - if(!gsr_egl_load(&gl, dpy, wayland)) { - fprintf(stderr, "gsr error: failed to load opengl\n"); - return false; - } - +bool gl_get_gpu_info(gsr_egl *egl, gsr_gpu_info *info) { bool supported = true; - const unsigned char *gl_vendor = gl.glGetString(GL_VENDOR); - const unsigned char *gl_renderer = gl.glGetString(GL_RENDERER); + const unsigned char *gl_vendor = egl->glGetString(GL_VENDOR); + const unsigned char *gl_renderer = egl->glGetString(GL_RENDERER); info->gpu_version = 0; @@ -252,26 +246,30 @@ bool gl_get_gpu_info(Display *dpy, gsr_gpu_info *info, bool wayland) { } end: - gsr_egl_unload(&gl); return supported; } bool gsr_get_valid_card_path(char *output) { for(int i = 0; i < 10; ++i) { - sprintf(output, "/dev/dri/card%d", i); + drmVersion *ver = NULL; + drmModePlaneResPtr planes = NULL; + bool found_screen_card = false; + + sprintf(output, DRM_DEV_NAME, DRM_DIR_NAME, i); int fd = open(output, O_RDONLY); if(fd == -1) continue; + ver = drmGetVersion(fd); + if(!ver || strstr(ver->name, "nouveau")) + goto next; + drmSetClientCap(fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); - drmModePlaneResPtr planes = drmModeGetPlaneResources(fd); - if(!planes) { - close(fd); - continue; - } + planes = drmModeGetPlaneResources(fd); + if(!planes) + goto next; - bool found_screen_card = false; for(uint32_t i = 0; i < planes->count_planes; ++i) { drmModePlanePtr plane = drmModeGetPlane(fd, planes->planes[i]); if(!plane) @@ -285,6 +283,11 @@ bool gsr_get_valid_card_path(char *output) { break; } + next: + if(planes) + drmModeFreePlaneResources(planes); + if(ver) + drmFreeVersion(ver); close(fd); if(found_screen_card) return true; @@ -292,6 +295,23 @@ bool gsr_get_valid_card_path(char *output) { return false; } +bool gsr_card_path_get_render_path(const char *card_path, char *render_path) { + int fd = open(card_path, O_RDONLY); + if(fd == -1) + return false; + + char *render_path_tmp = drmGetRenderDeviceNameFromFd(fd); + if(render_path_tmp) { + strncpy(render_path, render_path_tmp, 128); + free(render_path_tmp); + close(fd); + return true; + } + + close(fd); + return false; +} + int even_number_ceil(int value) { return value + (value & 1); } |