diff options
Diffstat (limited to 'src/capture')
-rw-r--r-- | src/capture/capture.c | 12 | ||||
-rw-r--r-- | src/capture/kms.c | 204 | ||||
-rw-r--r-- | src/capture/nvfbc.c | 106 | ||||
-rw-r--r-- | src/capture/portal.c | 209 | ||||
-rw-r--r-- | src/capture/xcomposite.c | 74 | ||||
-rw-r--r-- | src/capture/ximage.c | 247 |
6 files changed, 505 insertions, 347 deletions
diff --git a/src/capture/capture.c b/src/capture/capture.c index 2a4a689..bc95300 100644 --- a/src/capture/capture.c +++ b/src/capture/capture.c @@ -1,9 +1,9 @@ #include "../../include/capture/capture.h" #include <assert.h> -int gsr_capture_start(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame *frame) { +int gsr_capture_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) { assert(!cap->started); - int res = cap->start(cap, video_codec_context, frame); + int res = cap->start(cap, capture_metadata); if(res == 0) cap->started = true; @@ -29,9 +29,9 @@ bool gsr_capture_should_stop(gsr_capture *cap, bool *err) { return false; } -int gsr_capture_capture(gsr_capture *cap, AVFrame *frame, gsr_color_conversion *color_conversion) { +int gsr_capture_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) { assert(cap->started); - return cap->capture(cap, frame, color_conversion); + return cap->capture(cap, capture_metadata, color_conversion); } bool gsr_capture_uses_external_image(gsr_capture *cap) { @@ -48,6 +48,6 @@ bool gsr_capture_set_hdr_metadata(gsr_capture *cap, AVMasteringDisplayMetadata * return false; } -void gsr_capture_destroy(gsr_capture *cap, AVCodecContext *video_codec_context) { - cap->destroy(cap, video_codec_context); +void gsr_capture_destroy(gsr_capture *cap) { + cap->destroy(cap); } diff --git a/src/capture/kms.c b/src/capture/kms.c index 27ddec4..36a5355 100644 --- a/src/capture/kms.c +++ b/src/capture/kms.c @@ -12,11 +12,11 @@ #include <fcntl.h> #include <xf86drm.h> -#include <libdrm/drm_fourcc.h> +#include <drm_fourcc.h> -#include <libavcodec/avcodec.h> #include <libavutil/mastering_display_metadata.h> -#include <libavformat/avformat.h> + +#define FIND_CRTC_BY_NAME_TIMEOUT_SECONDS 2.0 #define HDMI_STATIC_METADATA_TYPE1 0 #define HDMI_EOTF_SMPTE_ST2084 2 @@ -53,16 +53,14 @@ typedef struct { bool is_x11; gsr_cursor x11_cursor; - AVCodecContext *video_codec_context; - bool performance_error_shown; - bool fast_path_failed; - //int drm_fd; //uint64_t prev_sequence; //bool damaged; vec2i prev_target_pos; vec2i prev_plane_size; + + double last_time_monitor_check; } gsr_capture_kms; static void gsr_capture_kms_cleanup_kms_fds(gsr_capture_kms *self) { @@ -112,16 +110,12 @@ static int max_int(int a, int b) { static void gsr_capture_kms_create_input_texture_ids(gsr_capture_kms *self) { self->params.egl->glGenTextures(1, &self->input_texture_id); self->params.egl->glBindTexture(GL_TEXTURE_2D, self->input_texture_id); - self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); self->params.egl->glBindTexture(GL_TEXTURE_2D, 0); self->params.egl->glGenTextures(1, &self->external_input_texture_id); self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, self->external_input_texture_id); - self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR); self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR); self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0); @@ -131,8 +125,6 @@ static void gsr_capture_kms_create_input_texture_ids(gsr_capture_kms *self) { self->params.egl->glGenTextures(1, &self->cursor_texture_id); self->params.egl->glBindTexture(cursor_texture_id_target, self->cursor_texture_id); - self->params.egl->glTexParameteri(cursor_texture_id_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - self->params.egl->glTexParameteri(cursor_texture_id_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); self->params.egl->glTexParameteri(cursor_texture_id_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); self->params.egl->glTexParameteri(cursor_texture_id_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); self->params.egl->glBindTexture(cursor_texture_id_target, 0); @@ -172,7 +164,7 @@ static vec2i rotate_capture_size_if_rotated(gsr_capture_kms *self, vec2i capture return capture_size; } -static int gsr_capture_kms_start(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame *frame) { +static int gsr_capture_kms_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) { gsr_capture_kms *self = cap->priv; gsr_capture_kms_create_input_texture_ids(self); @@ -205,7 +197,8 @@ static int gsr_capture_kms_start(gsr_capture *cap, AVCodecContext *video_codec_c } monitor.name = self->params.display_to_capture; - self->monitor_rotation = drm_monitor_get_display_server_rotation(self->params.egl->window, &monitor); + vec2i monitor_position = {0, 0}; + drm_monitor_get_display_server_data(self->params.egl->window, &monitor, &self->monitor_rotation, &monitor_position); self->capture_pos = monitor.pos; /* Monitor size is already rotated on x11 when the monitor is rotated, no need to apply it ourselves */ @@ -214,27 +207,19 @@ static int gsr_capture_kms_start(gsr_capture *cap, AVCodecContext *video_codec_c else self->capture_size = rotate_capture_size_if_rotated(self, monitor.size); - /* Disable vsync */ - self->params.egl->eglSwapInterval(self->params.egl->egl_display, 0); - - if(self->params.output_resolution.x == 0 && self->params.output_resolution.y == 0) { - self->params.output_resolution = self->capture_size; - video_codec_context->width = FFALIGN(self->capture_size.x, 2); - video_codec_context->height = FFALIGN(self->capture_size.y, 2); - } else { + if(self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0) { self->params.output_resolution = scale_keep_aspect_ratio(self->capture_size, self->params.output_resolution); - video_codec_context->width = FFALIGN(self->params.output_resolution.x, 2); - video_codec_context->height = FFALIGN(self->params.output_resolution.y, 2); + capture_metadata->width = self->params.output_resolution.x; + capture_metadata->height = self->params.output_resolution.y; + } else if(self->params.region_size.x > 0 && self->params.region_size.y > 0) { + capture_metadata->width = self->params.region_size.x; + capture_metadata->height = self->params.region_size.y; + } else { + capture_metadata->width = self->capture_size.x; + capture_metadata->height = self->capture_size.y; } - self->fast_path_failed = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && !gl_driver_version_greater_than(self->params.egl, 24, 0, 9); - if(self->fast_path_failed) - fprintf(stderr, "gsr warning: gsr_capture_kms_start: your amd driver (mesa) version is known to be buggy (<= version 24.0.9), falling back to opengl copy\n"); - - frame->width = video_codec_context->width; - frame->height = video_codec_context->height; - - self->video_codec_context = video_codec_context; + self->last_time_monitor_check = clock_get_monotonic_seconds(); return 0; } @@ -268,16 +253,6 @@ static void gsr_capture_kms_on_event(gsr_capture *cap, gsr_egl *egl) { // } // } -static float monitor_rotation_to_radians(gsr_monitor_rotation rot) { - switch(rot) { - case GSR_MONITOR_ROT_0: return 0.0f; - case GSR_MONITOR_ROT_90: return M_PI_2; - case GSR_MONITOR_ROT_180: return M_PI; - case GSR_MONITOR_ROT_270: return M_PI + M_PI_2; - } - return 0.0f; -} - static gsr_kms_response_item* find_drm_by_connector_id(gsr_kms_response *kms_response, uint32_t connector_id) { for(int i = 0; i < kms_response->num_items; ++i) { if(kms_response->items[i].connector_id == connector_id && !kms_response->items[i].is_cursor) @@ -428,7 +403,7 @@ static gsr_kms_response_item* find_monitor_drm(gsr_capture_kms *self, bool *capt } // Will never happen on wayland unless the target monitor has been disconnected - if(!drm_fd) { + if(!drm_fd && self->is_x11) { drm_fd = find_largest_drm(&self->kms_response); *capture_is_combined_plane = true; } @@ -443,7 +418,7 @@ static gsr_kms_response_item* find_cursor_drm_if_on_monitor(gsr_capture_kms *sel return cursor_drm_fd; } -static void render_drm_cursor(gsr_capture_kms *self, gsr_color_conversion *color_conversion, const gsr_kms_response_item *cursor_drm_fd, vec2i target_pos, float texture_rotation, vec2i output_size) { +static void render_drm_cursor(gsr_capture_kms *self, gsr_color_conversion *color_conversion, const gsr_kms_response_item *cursor_drm_fd, vec2i target_pos, vec2i output_size, vec2i framebuffer_size) { const vec2d scale = { self->capture_size.x == 0 ? 0 : (double)output_size.x / (double)self->capture_size.x, self->capture_size.y == 0 ? 0 : (double)output_size.y / (double)self->capture_size.y @@ -458,25 +433,28 @@ static void render_drm_cursor(gsr_capture_kms *self, gsr_color_conversion *color break; case GSR_MONITOR_ROT_90: cursor_pos = swap_vec2i(cursor_pos); - cursor_pos.x = self->capture_size.x - cursor_pos.x; + cursor_pos.x = framebuffer_size.x - cursor_pos.x; // TODO: Remove this horrible hack cursor_pos.x -= cursor_size.x; break; case GSR_MONITOR_ROT_180: - cursor_pos.x = self->capture_size.x - cursor_pos.x; - cursor_pos.y = self->capture_size.y - cursor_pos.y; + cursor_pos.x = framebuffer_size.x - cursor_pos.x; + cursor_pos.y = framebuffer_size.y - cursor_pos.y; // TODO: Remove this horrible hack cursor_pos.x -= cursor_size.x; cursor_pos.y -= cursor_size.y; break; case GSR_MONITOR_ROT_270: cursor_pos = swap_vec2i(cursor_pos); - cursor_pos.y = self->capture_size.y - cursor_pos.y; + cursor_pos.y = framebuffer_size.y - cursor_pos.y; // TODO: Remove this horrible hack cursor_pos.y -= cursor_size.y; break; } + cursor_pos.x -= self->params.region_position.x; + cursor_pos.y -= self->params.region_position.y; + cursor_pos.x *= scale.x; cursor_pos.y *= scale.y; @@ -513,8 +491,8 @@ static void render_drm_cursor(gsr_capture_kms *self, gsr_color_conversion *color gsr_color_conversion_draw(color_conversion, self->cursor_texture_id, cursor_pos, (vec2i){cursor_size.x * scale.x, cursor_size.y * scale.y}, - (vec2i){0, 0}, cursor_size, - texture_rotation, cursor_texture_id_is_external, GSR_SOURCE_COLOR_RGB); + (vec2i){0, 0}, cursor_size, cursor_size, + gsr_monitor_rotation_to_rotation(self->monitor_rotation), GSR_SOURCE_COLOR_RGB, cursor_texture_id_is_external, true); self->params.egl->glDisable(GL_SCISSOR_TEST); } @@ -541,8 +519,8 @@ static void render_x11_cursor(gsr_capture_kms *self, gsr_color_conversion *color gsr_color_conversion_draw(color_conversion, self->x11_cursor.texture_id, cursor_pos, (vec2i){self->x11_cursor.size.x * scale.x, self->x11_cursor.size.y * scale.y}, - (vec2i){0, 0}, self->x11_cursor.size, - 0.0f, false, GSR_SOURCE_COLOR_RGB); + (vec2i){0, 0}, self->x11_cursor.size, self->x11_cursor.size, + GSR_ROT_0, GSR_SOURCE_COLOR_RGB, false, true); self->params.egl->glDisable(GL_SCISSOR_TEST); } @@ -555,7 +533,48 @@ static void gsr_capture_kms_update_capture_size_change(gsr_capture_kms *self, gs } } -static int gsr_capture_kms_capture(gsr_capture *cap, AVFrame *frame, gsr_color_conversion *color_conversion) { +static void gsr_capture_kms_update_connector_ids(gsr_capture_kms *self) { + const double now = clock_get_monotonic_seconds(); + if(now - self->last_time_monitor_check < FIND_CRTC_BY_NAME_TIMEOUT_SECONDS) + return; + + self->last_time_monitor_check = now; + /* TODO: Assume for now that there is only 1 framebuffer for all monitors and it doesn't change */ + if(self->is_x11) + return; + + self->monitor_id.num_connector_ids = 0; + const gsr_connection_type connection_type = self->is_x11 ? GSR_CONNECTION_X11 : GSR_CONNECTION_DRM; + // MonitorCallbackUserdata monitor_callback_userdata = { + // &self->monitor_id, + // self->params.display_to_capture, strlen(self->params.display_to_capture), + // 0, + // }; + // for_each_active_monitor_output(self->params.egl->window, self->params.egl->card_path, connection_type, monitor_callback, &monitor_callback_userdata); + + gsr_monitor monitor; + if(!get_monitor_by_name(self->params.egl, connection_type, self->params.display_to_capture, &monitor)) { + fprintf(stderr, "gsr error: gsr_capture_kms_update_connector_ids: failed to find monitor by name \"%s\"\n", self->params.display_to_capture); + return; + } + + self->monitor_id.num_connector_ids = 1; + self->monitor_id.connector_ids[0] = monitor.connector_id; + + monitor.name = self->params.display_to_capture; + vec2i monitor_position = {0, 0}; + // TODO: This is cached. We need it updated. + drm_monitor_get_display_server_data(self->params.egl->window, &monitor, &self->monitor_rotation, &monitor_position); + + self->capture_pos = monitor.pos; + /* Monitor size is already rotated on x11 when the monitor is rotated, no need to apply it ourselves */ + if(self->is_x11) + self->capture_size = monitor.size; + else + self->capture_size = rotate_capture_size_if_rotated(self, monitor.size); +} + +static int gsr_capture_kms_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) { gsr_capture_kms *self = cap->priv; gsr_capture_kms_cleanup_kms_fds(self); @@ -574,6 +593,8 @@ static int gsr_capture_kms_capture(gsr_capture *cap, AVFrame *frame, gsr_color_c return -1; } + gsr_capture_kms_update_connector_ids(self); + bool capture_is_combined_plane = false; const gsr_kms_response_item *drm_fd = find_monitor_drm(self, &capture_is_combined_plane); if(!drm_fd) { @@ -584,62 +605,39 @@ static int gsr_capture_kms_capture(gsr_capture *cap, AVFrame *frame, gsr_color_c if(drm_fd->has_hdr_metadata && self->params.hdr && hdr_metadata_is_supported_format(&drm_fd->hdr_metadata)) gsr_kms_set_hdr_metadata(self, drm_fd); - if(!self->performance_error_shown && self->monitor_rotation != GSR_MONITOR_ROT_0 && video_codec_context_is_vaapi(self->video_codec_context) && self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD) { - self->performance_error_shown = true; - fprintf(stderr,"gsr warning: gsr_capture_kms_capture: the monitor you are recording is rotated, composition will have to be used." - " If you are experience performance problems in the video then record a single window on X11 or use portal capture option instead\n"); - } - self->capture_size = rotate_capture_size_if_rotated(self, (vec2i){ drm_fd->src_w, drm_fd->src_h }); + const vec2i original_frame_size = self->capture_size; + if(self->params.region_size.x > 0 && self->params.region_size.y > 0) + self->capture_size = self->params.region_size; const bool is_scaled = self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0; vec2i output_size = is_scaled ? self->params.output_resolution : self->capture_size; output_size = scale_keep_aspect_ratio(self->capture_size, output_size); - const float texture_rotation = monitor_rotation_to_radians(self->monitor_rotation); - const vec2i target_pos = { max_int(0, frame->width / 2 - output_size.x / 2), max_int(0, frame->height / 2 - output_size.y / 2) }; + const vec2i target_pos = { max_int(0, capture_metadata->width / 2 - output_size.x / 2), max_int(0, capture_metadata->height / 2 - output_size.y / 2) }; gsr_capture_kms_update_capture_size_change(self, color_conversion, target_pos, drm_fd); vec2i capture_pos = self->capture_pos; if(!capture_is_combined_plane) capture_pos = (vec2i){drm_fd->x, drm_fd->y}; - self->params.egl->glFlush(); - self->params.egl->glFinish(); - - /* Fast opengl free path */ - if(!self->fast_path_failed && self->monitor_rotation == GSR_MONITOR_ROT_0 && video_codec_context_is_vaapi(self->video_codec_context) && self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD) { - int fds[4]; - uint32_t offsets[4]; - uint32_t pitches[4]; - uint64_t modifiers[4]; - for(int i = 0; i < drm_fd->num_dma_bufs; ++i) { - fds[i] = drm_fd->dma_buf[i].fd; - offsets[i] = drm_fd->dma_buf[i].offset; - pitches[i] = drm_fd->dma_buf[i].pitch; - modifiers[i] = drm_fd->modifier; - } - if(!vaapi_copy_drm_planes_to_video_surface(self->video_codec_context, frame, (vec2i){capture_pos.x, capture_pos.y}, self->capture_size, target_pos, output_size, drm_fd->pixel_format, (vec2i){drm_fd->width, drm_fd->height}, fds, offsets, pitches, modifiers, drm_fd->num_dma_bufs)) { - fprintf(stderr, "gsr error: gsr_capture_kms_capture: vaapi_copy_drm_planes_to_video_surface failed, falling back to opengl copy. Please report this as an issue at https://github.com/dec05eba/gpu-screen-recorder-issues\n"); - self->fast_path_failed = true; - } - } else { - self->fast_path_failed = true; - } + capture_pos.x += self->params.region_position.x; + capture_pos.y += self->params.region_position.y; - if(self->fast_path_failed) { - EGLImage image = gsr_capture_kms_create_egl_image_with_fallback(self, drm_fd); - if(image) { - gsr_capture_kms_bind_image_to_input_texture_with_fallback(self, image); - self->params.egl->eglDestroyImage(self->params.egl->egl_display, image); - } + //self->params.egl->glFlush(); + //self->params.egl->glFinish(); - gsr_color_conversion_draw(color_conversion, self->external_texture_fallback ? self->external_input_texture_id : self->input_texture_id, - target_pos, output_size, - capture_pos, self->capture_size, - texture_rotation, self->external_texture_fallback, GSR_SOURCE_COLOR_RGB); + EGLImage image = gsr_capture_kms_create_egl_image_with_fallback(self, drm_fd); + if(image) { + gsr_capture_kms_bind_image_to_input_texture_with_fallback(self, image); + self->params.egl->eglDestroyImage(self->params.egl->egl_display, image); } + gsr_color_conversion_draw(color_conversion, self->external_texture_fallback ? self->external_input_texture_id : self->input_texture_id, + target_pos, output_size, + capture_pos, self->capture_size, original_frame_size, + gsr_monitor_rotation_to_rotation(self->monitor_rotation), GSR_SOURCE_COLOR_RGB, self->external_texture_fallback, false); + if(self->params.record_cursor) { gsr_kms_response_item *cursor_drm_fd = find_cursor_drm_if_on_monitor(self, drm_fd->connector_id, capture_is_combined_plane); // The cursor is handled by x11 on x11 instead of using the cursor drm plane because on prime systems with a dedicated nvidia gpu @@ -647,15 +645,18 @@ static int gsr_capture_kms_capture(gsr_capture *cap, AVFrame *frame, gsr_color_c // TODO: This doesn't work properly with software cursor on x11 since it will draw the x11 cursor on top of the cursor already in the framebuffer. // Detect if software cursor is used on x11 somehow. if(self->is_x11) { - const vec2i cursor_monitor_offset = self->capture_pos; + vec2i cursor_monitor_offset = self->capture_pos; + cursor_monitor_offset.x += self->params.region_position.x; + cursor_monitor_offset.y += self->params.region_position.y; render_x11_cursor(self, color_conversion, cursor_monitor_offset, target_pos, output_size); } else if(cursor_drm_fd) { - render_drm_cursor(self, color_conversion, cursor_drm_fd, target_pos, texture_rotation, output_size); + const vec2i framebuffer_size = rotate_capture_size_if_rotated(self, (vec2i){ drm_fd->src_w, drm_fd->src_h }); + render_drm_cursor(self, color_conversion, cursor_drm_fd, target_pos, output_size, framebuffer_size); } } - self->params.egl->glFlush(); - self->params.egl->glFinish(); + //self->params.egl->glFlush(); + //self->params.egl->glFinish(); gsr_capture_kms_cleanup_kms_fds(self); @@ -694,8 +695,8 @@ static bool gsr_capture_kms_set_hdr_metadata(gsr_capture *cap, AVMasteringDispla mastering_display_metadata->min_luminance = av_make_q(self->hdr_metadata.hdmi_metadata_type1.min_display_mastering_luminance, 10000); mastering_display_metadata->max_luminance = av_make_q(self->hdr_metadata.hdmi_metadata_type1.max_display_mastering_luminance, 1); - mastering_display_metadata->has_primaries = mastering_display_metadata->display_primaries[0][0].num > 0; - mastering_display_metadata->has_luminance = mastering_display_metadata->max_luminance.num > 0; + mastering_display_metadata->has_primaries = true; + mastering_display_metadata->has_luminance = true; return true; } @@ -710,8 +711,7 @@ static bool gsr_capture_kms_set_hdr_metadata(gsr_capture *cap, AVMasteringDispla // self->damaged = false; // } -static void gsr_capture_kms_destroy(gsr_capture *cap, AVCodecContext *video_codec_context) { - (void)video_codec_context; +static void gsr_capture_kms_destroy(gsr_capture *cap) { gsr_capture_kms *self = cap->priv; if(cap->priv) { gsr_capture_kms_stop(self); diff --git a/src/capture/nvfbc.c b/src/capture/nvfbc.c index 676d269..13b46c3 100644 --- a/src/capture/nvfbc.c +++ b/src/capture/nvfbc.c @@ -13,7 +13,6 @@ #include <assert.h> #include <X11/Xlib.h> -#include <libavcodec/avcodec.h> typedef struct { gsr_capture_nvfbc_params params; @@ -28,8 +27,7 @@ typedef struct { NVFBC_TOGL_SETUP_PARAMS setup_params; bool supports_direct_cursor; - bool capture_region; - uint32_t x, y, width, height; + uint32_t width, height; NVFBC_TRACKING_TYPE tracking_type; uint32_t output_id; uint32_t tracking_width, tracking_height; @@ -133,31 +131,6 @@ static bool gsr_capture_nvfbc_load_library(gsr_capture *cap) { return true; } -/* TODO: check for glx swap control extension string (GLX_EXT_swap_control, etc) */ -static void set_vertical_sync_enabled(gsr_egl *egl, int enabled) { - int result = 0; - - if(egl->glXSwapIntervalEXT) { - assert(gsr_window_get_display_server(egl->window) == GSR_DISPLAY_SERVER_X11); - Display *display = gsr_window_get_display(egl->window); - const Window window = (Window)gsr_window_get_window(egl->window); - egl->glXSwapIntervalEXT(display, window, enabled ? 1 : 0); - } else if(egl->glXSwapIntervalMESA) { - result = egl->glXSwapIntervalMESA(enabled ? 1 : 0); - } else if(egl->glXSwapIntervalSGI) { - result = egl->glXSwapIntervalSGI(enabled ? 1 : 0); - } else { - static int warned = 0; - if (!warned) { - warned = 1; - fprintf(stderr, "gsr warning: setting vertical sync not supported\n"); - } - } - - if(result != 0) - fprintf(stderr, "gsr warning: setting vertical sync failed\n"); -} - static void gsr_capture_nvfbc_destroy_session(gsr_capture_nvfbc *self) { if(self->fbc_handle_created && self->capture_session_created) { NVFBC_DESTROY_CAPTURE_SESSION_PARAMS destroy_capture_params; @@ -248,11 +221,8 @@ static int gsr_capture_nvfbc_setup_handle(gsr_capture_nvfbc *self) { } } - if(!self->capture_region) { - self->width = self->tracking_width; - self->height = self->tracking_height; - } - + self->width = self->tracking_width; + self->height = self->tracking_height; return 0; error_cleanup: @@ -268,8 +238,6 @@ static int gsr_capture_nvfbc_setup_session(gsr_capture_nvfbc *self) { create_capture_params.bWithCursor = (!self->params.direct_capture || self->supports_direct_cursor) ? NVFBC_TRUE : NVFBC_FALSE; if(!self->params.record_cursor) create_capture_params.bWithCursor = false; - if(self->capture_region) - create_capture_params.captureBox = (NVFBC_BOX){ self->x, self->y, self->width, self->height }; create_capture_params.eTrackingType = self->tracking_type; create_capture_params.dwSamplingRateMs = (uint32_t)ceilf(1000.0f / (float)self->params.fps); create_capture_params.bAllowDirectCapture = self->params.direct_capture ? NVFBC_TRUE : NVFBC_FALSE; @@ -311,29 +279,22 @@ static void gsr_capture_nvfbc_stop(gsr_capture_nvfbc *self) { } } -static int gsr_capture_nvfbc_start(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame *frame) { +static int gsr_capture_nvfbc_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) { gsr_capture_nvfbc *self = cap->priv; if(!gsr_capture_nvfbc_load_library(cap)) return -1; - self->x = max_int(self->params.pos.x, 0); - self->y = max_int(self->params.pos.y, 0); - self->width = max_int(self->params.size.x, 0); - self->height = max_int(self->params.size.y, 0); - - self->capture_region = (self->x > 0 || self->y > 0 || self->width > 0 || self->height > 0); - self->supports_direct_cursor = false; int driver_major_version = 0; int driver_minor_version = 0; if(self->params.direct_capture && get_driver_version(&driver_major_version, &driver_minor_version)) { - fprintf(stderr, "Info: detected nvidia version: %d.%d\n", driver_major_version, driver_minor_version); + fprintf(stderr, "gsr info: detected nvidia version: %d.%d\n", driver_major_version, driver_minor_version); // TODO: if(version_at_least(driver_major_version, driver_minor_version, 515, 57) && version_less_than(driver_major_version, driver_minor_version, 520, 56)) { self->params.direct_capture = false; - fprintf(stderr, "Warning: \"screen-direct\" has temporary been disabled as it causes stuttering with driver versions >= 515.57 and < 520.56. Please update your driver if possible. Capturing \"screen\" instead.\n"); + fprintf(stderr, "gsr warning: \"screen-direct\" has temporary been disabled as it causes stuttering with driver versions >= 515.57 and < 520.56. Please update your driver if possible. Capturing \"screen\" instead.\n"); } // TODO: @@ -343,7 +304,7 @@ static int gsr_capture_nvfbc_start(gsr_capture *cap, AVCodecContext *video_codec if(version_at_least(driver_major_version, driver_minor_version, 515, 57)) self->supports_direct_cursor = true; else - fprintf(stderr, "Info: capturing \"screen-direct\" but driver version appears to be less than 515.57. Disabling capture of cursor. Please update your driver if you want to capture your cursor or record \"screen\" instead.\n"); + fprintf(stderr, "gsr info: capturing \"screen-direct\" but driver version appears to be less than 515.57. Disabling capture of cursor. Please update your driver if you want to capture your cursor or record \"screen\" instead.\n"); } */ } @@ -356,28 +317,18 @@ static int gsr_capture_nvfbc_start(gsr_capture *cap, AVCodecContext *video_codec goto error_cleanup; } - if(self->capture_region) { - video_codec_context->width = FFALIGN(self->width, 2); - video_codec_context->height = FFALIGN(self->height, 2); - } else { - video_codec_context->width = FFALIGN(self->tracking_width, 2); - video_codec_context->height = FFALIGN(self->tracking_height, 2); - } + capture_metadata->width = self->tracking_width; + capture_metadata->height = self->tracking_height; - if(self->params.output_resolution.x == 0 && self->params.output_resolution.y == 0) { - self->params.output_resolution = (vec2i){video_codec_context->width, video_codec_context->height}; - } else { - self->params.output_resolution = scale_keep_aspect_ratio((vec2i){video_codec_context->width, video_codec_context->height}, self->params.output_resolution); - video_codec_context->width = FFALIGN(self->params.output_resolution.x, 2); - video_codec_context->height = FFALIGN(self->params.output_resolution.y, 2); + if(self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0) { + self->params.output_resolution = scale_keep_aspect_ratio((vec2i){capture_metadata->width, capture_metadata->height}, self->params.output_resolution); + capture_metadata->width = self->params.output_resolution.x; + capture_metadata->height = self->params.output_resolution.y; + } else if(self->params.region_size.x > 0 && self->params.region_size.y > 0) { + capture_metadata->width = self->params.region_size.x; + capture_metadata->height = self->params.region_size.y; } - frame->width = video_codec_context->width; - frame->height = video_codec_context->height; - - /* Disable vsync */ - set_vertical_sync_enabled(self->params.egl, 0); - return 0; error_cleanup: @@ -385,7 +336,7 @@ static int gsr_capture_nvfbc_start(gsr_capture *cap, AVCodecContext *video_codec return -1; } -static int gsr_capture_nvfbc_capture(gsr_capture *cap, AVFrame *frame, gsr_color_conversion *color_conversion) { +static int gsr_capture_nvfbc_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) { gsr_capture_nvfbc *self = cap->priv; const double nvfbc_recreate_retry_time_seconds = 1.0; @@ -411,12 +362,16 @@ static int gsr_capture_nvfbc_capture(gsr_capture *cap, AVFrame *frame, gsr_color } } - const vec2i frame_size = (vec2i){self->width, self->height}; + vec2i frame_size = (vec2i){self->width, self->height}; + const vec2i original_frame_size = frame_size; + if(self->params.region_size.x > 0 && self->params.region_size.y > 0) + frame_size = self->params.region_size; + const bool is_scaled = self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0; vec2i output_size = is_scaled ? self->params.output_resolution : frame_size; output_size = scale_keep_aspect_ratio(frame_size, output_size); - const vec2i target_pos = { max_int(0, frame->width / 2 - output_size.x / 2), max_int(0, frame->height / 2 - output_size.y / 2) }; + const vec2i target_pos = { max_int(0, capture_metadata->width / 2 - output_size.x / 2), max_int(0, capture_metadata->height / 2 - output_size.y / 2) }; NVFBC_FRAME_GRAB_INFO frame_info; memset(&frame_info, 0, sizeof(frame_info)); @@ -436,22 +391,21 @@ static int gsr_capture_nvfbc_capture(gsr_capture *cap, AVFrame *frame, gsr_color return 0; } - self->params.egl->glFlush(); - self->params.egl->glFinish(); + //self->params.egl->glFlush(); + //self->params.egl->glFinish(); gsr_color_conversion_draw(color_conversion, self->setup_params.dwTextures[grab_params.dwTextureIndex], target_pos, (vec2i){output_size.x, output_size.y}, - (vec2i){0, 0}, frame_size, - 0.0f, false, GSR_SOURCE_COLOR_BGR); + self->params.region_position, frame_size, original_frame_size, + GSR_ROT_0, GSR_SOURCE_COLOR_BGR, false, false); - self->params.egl->glFlush(); - self->params.egl->glFinish(); + //self->params.egl->glFlush(); + //self->params.egl->glFinish(); return 0; } -static void gsr_capture_nvfbc_destroy(gsr_capture *cap, AVCodecContext *video_codec_context) { - (void)video_codec_context; +static void gsr_capture_nvfbc_destroy(gsr_capture *cap) { gsr_capture_nvfbc *self = cap->priv; gsr_capture_nvfbc_stop(self); free(cap->priv); diff --git a/src/capture/portal.c b/src/capture/portal.c index a441299..d2217d1 100644 --- a/src/capture/portal.c +++ b/src/capture/portal.c @@ -8,9 +8,17 @@ #include <stdlib.h> #include <stdio.h> #include <unistd.h> +#include <limits.h> #include <assert.h> -#include <libavcodec/avcodec.h> +#define PORTAL_CAPTURE_CANCELED_BY_USER_EXIT_CODE 60 + +typedef enum { + PORTAL_CAPTURE_SETUP_IDLE, + PORTAL_CAPTURE_SETUP_IN_PROGRESS, + PORTAL_CAPTURE_SETUP_FINISHED, + PORTAL_CAPTURE_SETUP_FAILED +} gsr_portal_capture_setup_state; typedef struct { gsr_capture_portal_params params; @@ -25,8 +33,14 @@ typedef struct { gsr_pipewire_video_dmabuf_data dmabuf_data[GSR_PIPEWIRE_VIDEO_DMABUF_MAX_PLANES]; int num_dmabuf_data; - AVCodecContext *video_codec_context; - bool fast_path_failed; + gsr_pipewire_video_region region; + gsr_pipewire_video_region cursor_region; + uint32_t pipewire_fourcc; + uint64_t pipewire_modifiers; + bool using_external_image; + + bool should_stop; + bool stop_is_error; } gsr_capture_portal; static void gsr_capture_portal_cleanup_plane_fds(gsr_capture_portal *self) { @@ -56,38 +70,25 @@ static void gsr_capture_portal_stop(gsr_capture_portal *self) { } gsr_capture_portal_cleanup_plane_fds(self); - gsr_pipewire_video_deinit(&self->pipewire); - - if(self->session_handle) { - free(self->session_handle); - self->session_handle = NULL; - } - gsr_dbus_deinit(&self->dbus); } static void gsr_capture_portal_create_input_textures(gsr_capture_portal *self) { self->params.egl->glGenTextures(1, &self->texture_map.texture_id); self->params.egl->glBindTexture(GL_TEXTURE_2D, self->texture_map.texture_id); - self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); self->params.egl->glBindTexture(GL_TEXTURE_2D, 0); self->params.egl->glGenTextures(1, &self->texture_map.external_texture_id); self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, self->texture_map.external_texture_id); - self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR); self->params.egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR); self->params.egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0); self->params.egl->glGenTextures(1, &self->texture_map.cursor_texture_id); self->params.egl->glBindTexture(GL_TEXTURE_2D, self->texture_map.cursor_texture_id); - self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); self->params.egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); self->params.egl->glBindTexture(GL_TEXTURE_2D, 0); @@ -233,19 +234,13 @@ static int gsr_capture_portal_setup_dbus(gsr_capture_portal *self, int *pipewire } static bool gsr_capture_portal_get_frame_dimensions(gsr_capture_portal *self) { - gsr_pipewire_video_region region = {0, 0, 0, 0}; - gsr_pipewire_video_region cursor_region = {0, 0, 0, 0}; fprintf(stderr, "gsr info: gsr_capture_portal_start: waiting for pipewire negotiation\n"); const double start_time = clock_get_monotonic_seconds(); while(clock_get_monotonic_seconds() - start_time < 5.0) { - bool uses_external_image = false; - uint32_t fourcc = 0; - uint64_t modifiers = 0; - if(gsr_pipewire_video_map_texture(&self->pipewire, self->texture_map, ®ion, &cursor_region, self->dmabuf_data, &self->num_dmabuf_data, &fourcc, &modifiers, &uses_external_image)) { - gsr_capture_portal_cleanup_plane_fds(self); - self->capture_size.x = region.width; - self->capture_size.y = region.height; + if(gsr_pipewire_video_map_texture(&self->pipewire, self->texture_map, &self->region, &self->cursor_region, self->dmabuf_data, &self->num_dmabuf_data, &self->pipewire_fourcc, &self->pipewire_modifiers, &self->using_external_image)) { + self->capture_size.x = self->region.width; + self->capture_size.y = self->region.height; fprintf(stderr, "gsr info: gsr_capture_portal_start: pipewire negotiation finished\n"); return true; } @@ -256,68 +251,62 @@ static bool gsr_capture_portal_get_frame_dimensions(gsr_capture_portal *self) { return false; } -static int gsr_capture_portal_start(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame *frame) { - gsr_capture_portal *self = cap->priv; - +static int gsr_capture_portal_setup(gsr_capture_portal *self, int fps) { gsr_capture_portal_create_input_textures(self); int pipewire_fd = 0; uint32_t pipewire_node = 0; const int response_status = gsr_capture_portal_setup_dbus(self, &pipewire_fd, &pipewire_node); if(response_status != 0) { - gsr_capture_portal_stop(self); // Response status values: // 0: Success, the request is carried out // 1: The user cancelled the interaction // 2: The user interaction was ended in some other way // Response status value 2 happens usually if there was some kind of error in the desktop portal on the system if(response_status == 2) { - fprintf(stderr, "gsr error: gsr_capture_portal_start: desktop portal capture failed. Either you Wayland compositor doesn't support desktop portal capture or it's incorrectly setup on your system\n"); + fprintf(stderr, "gsr error: gsr_capture_portal_setup: desktop portal capture failed. Either you Wayland compositor doesn't support desktop portal capture or it's incorrectly setup on your system\n"); return 50; } else if(response_status == 1) { - fprintf(stderr, "gsr error: gsr_capture_portal_start: desktop portal capture failed. It seems like desktop portal capture was canceled by the user.\n"); - return 60; + fprintf(stderr, "gsr error: gsr_capture_portal_setup: desktop portal capture failed. It seems like desktop portal capture was canceled by the user.\n"); + return PORTAL_CAPTURE_CANCELED_BY_USER_EXIT_CODE; } else { return -1; } } - fprintf(stderr, "gsr info: gsr_capture_portal_start: setting up pipewire\n"); + fprintf(stderr, "gsr info: gsr_capture_portal_setup: setting up pipewire\n"); /* TODO: support hdr when pipewire supports it */ /* gsr_pipewire closes the pipewire fd, even on failure */ - if(!gsr_pipewire_video_init(&self->pipewire, pipewire_fd, pipewire_node, video_codec_context->framerate.num, self->params.record_cursor, self->params.egl)) { - fprintf(stderr, "gsr error: gsr_capture_portal_start: failed to setup pipewire with fd: %d, node: %" PRIu32 "\n", pipewire_fd, pipewire_node); - gsr_capture_portal_stop(self); + if(!gsr_pipewire_video_init(&self->pipewire, pipewire_fd, pipewire_node, fps, self->params.record_cursor, self->params.egl)) { + fprintf(stderr, "gsr error: gsr_capture_portal_setup: failed to setup pipewire with fd: %d, node: %" PRIu32 "\n", pipewire_fd, pipewire_node); return -1; } - fprintf(stderr, "gsr info: gsr_capture_portal_start: pipewire setup finished\n"); + fprintf(stderr, "gsr info: gsr_capture_portal_setup: pipewire setup finished\n"); - if(!gsr_capture_portal_get_frame_dimensions(self)) { - gsr_capture_portal_stop(self); + if(!gsr_capture_portal_get_frame_dimensions(self)) return -1; - } - /* Disable vsync */ - self->params.egl->eglSwapInterval(self->params.egl->egl_display, 0); + return 0; +} + +static int gsr_capture_portal_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) { + gsr_capture_portal *self = cap->priv; + + const int result = gsr_capture_portal_setup(self, capture_metadata->fps); + if(result != 0) { + gsr_capture_portal_stop(self); + return result; + } if(self->params.output_resolution.x == 0 && self->params.output_resolution.y == 0) { - self->params.output_resolution = self->capture_size; - video_codec_context->width = FFALIGN(self->capture_size.x, 2); - video_codec_context->height = FFALIGN(self->capture_size.y, 2); + capture_metadata->width = self->capture_size.x; + capture_metadata->height = self->capture_size.y; } else { self->params.output_resolution = scale_keep_aspect_ratio(self->capture_size, self->params.output_resolution); - video_codec_context->width = FFALIGN(self->params.output_resolution.x, 2); - video_codec_context->height = FFALIGN(self->params.output_resolution.y, 2); + capture_metadata->width = self->params.output_resolution.x; + capture_metadata->height = self->params.output_resolution.y; } - self->fast_path_failed = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && !gl_driver_version_greater_than(self->params.egl, 24, 0, 9); - if(self->fast_path_failed) - fprintf(stderr, "gsr warning: gsr_capture_kms_start: your amd driver (mesa) version is known to be buggy (<= version 24.0.9), falling back to opengl copy\n"); - - frame->width = video_codec_context->width; - frame->height = video_codec_context->height; - - self->video_codec_context = video_codec_context; return 0; } @@ -325,87 +314,80 @@ static int max_int(int a, int b) { return a > b ? a : b; } -static int gsr_capture_portal_capture(gsr_capture *cap, AVFrame *frame, gsr_color_conversion *color_conversion) { - (void)frame; +static bool gsr_capture_portal_capture_has_synchronous_task(gsr_capture *cap) { + gsr_capture_portal *self = cap->priv; + return gsr_pipewire_video_should_restart(&self->pipewire); +} + +static int gsr_capture_portal_capture(gsr_capture *cap, gsr_capture_metadata *capture_metadata, gsr_color_conversion *color_conversion) { (void)color_conversion; gsr_capture_portal *self = cap->priv; - /* TODO: Handle formats other than RGB(a) */ - gsr_pipewire_video_region region = {0, 0, 0, 0}; - gsr_pipewire_video_region cursor_region = {0, 0, 0, 0}; - uint32_t pipewire_fourcc = 0; - uint64_t pipewire_modifiers = 0; - bool using_external_image = false; - if(gsr_pipewire_video_map_texture(&self->pipewire, self->texture_map, ®ion, &cursor_region, self->dmabuf_data, &self->num_dmabuf_data, &pipewire_fourcc, &pipewire_modifiers, &using_external_image)) { - if(region.width != self->capture_size.x || region.height != self->capture_size.y) { - self->capture_size.x = region.width; - self->capture_size.y = region.height; - gsr_color_conversion_clear(color_conversion); + if(self->should_stop) + return -1; + + if(gsr_pipewire_video_should_restart(&self->pipewire)) { + fprintf(stderr, "gsr info: gsr_capture_portal_capture: pipewire capture was paused, trying to start capture again\n"); + gsr_capture_portal_stop(self); + const int result = gsr_capture_portal_setup(self, capture_metadata->fps); + if(result != 0) { + self->stop_is_error = result != PORTAL_CAPTURE_CANCELED_BY_USER_EXIT_CODE; + self->should_stop = true; + } + return -1; + } + + /* TODO: Handle formats other than RGB(A) */ + if(self->num_dmabuf_data == 0) { + if(gsr_pipewire_video_map_texture(&self->pipewire, self->texture_map, &self->region, &self->cursor_region, self->dmabuf_data, &self->num_dmabuf_data, &self->pipewire_fourcc, &self->pipewire_modifiers, &self->using_external_image)) { + if(self->region.width != self->capture_size.x || self->region.height != self->capture_size.y) { + self->capture_size.x = self->region.width; + self->capture_size.y = self->region.height; + gsr_color_conversion_clear(color_conversion); + } + } else { + return -1; } - } else { - return 0; } const bool is_scaled = self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0; vec2i output_size = is_scaled ? self->params.output_resolution : self->capture_size; output_size = scale_keep_aspect_ratio(self->capture_size, output_size); - const vec2i target_pos = { max_int(0, frame->width / 2 - output_size.x / 2), max_int(0, frame->height / 2 - output_size.y / 2) }; + const vec2i target_pos = { max_int(0, capture_metadata->width / 2 - output_size.x / 2), max_int(0, capture_metadata->height / 2 - output_size.y / 2) }; - self->params.egl->glFlush(); - self->params.egl->glFinish(); + //self->params.egl->glFlush(); + //self->params.egl->glFinish(); // TODO: Handle region crop - /* Fast opengl free path */ - if(!self->fast_path_failed && video_codec_context_is_vaapi(self->video_codec_context) && self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD) { - int fds[4]; - uint32_t offsets[4]; - uint32_t pitches[4]; - uint64_t modifiers[4]; - for(int i = 0; i < self->num_dmabuf_data; ++i) { - fds[i] = self->dmabuf_data[i].fd; - offsets[i] = self->dmabuf_data[i].offset; - pitches[i] = self->dmabuf_data[i].stride; - modifiers[i] = pipewire_modifiers; - } - if(!vaapi_copy_drm_planes_to_video_surface(self->video_codec_context, frame, (vec2i){region.x, region.y}, self->capture_size, target_pos, output_size, pipewire_fourcc, self->capture_size, fds, offsets, pitches, modifiers, self->num_dmabuf_data)) { - fprintf(stderr, "gsr error: gsr_capture_portal_capture: vaapi_copy_drm_planes_to_video_surface failed, falling back to opengl copy. Please report this as an issue at https://github.com/dec05eba/gpu-screen-recorder-issues\n"); - self->fast_path_failed = true; - } - } else { - self->fast_path_failed = true; - } - - if(self->fast_path_failed) { - gsr_color_conversion_draw(color_conversion, using_external_image ? self->texture_map.external_texture_id : self->texture_map.texture_id, - target_pos, output_size, - (vec2i){region.x, region.y}, self->capture_size, - 0.0f, using_external_image, GSR_SOURCE_COLOR_RGB); - } + gsr_color_conversion_draw(color_conversion, self->using_external_image ? self->texture_map.external_texture_id : self->texture_map.texture_id, + target_pos, output_size, + (vec2i){self->region.x, self->region.y}, self->capture_size, self->capture_size, + GSR_ROT_0, GSR_SOURCE_COLOR_RGB, self->using_external_image, false); - if(self->params.record_cursor && self->texture_map.cursor_texture_id > 0 && cursor_region.width > 0) { + if(self->params.record_cursor && self->texture_map.cursor_texture_id > 0 && self->cursor_region.width > 0) { const vec2d scale = { self->capture_size.x == 0 ? 0 : (double)output_size.x / (double)self->capture_size.x, self->capture_size.y == 0 ? 0 : (double)output_size.y / (double)self->capture_size.y }; const vec2i cursor_pos = { - target_pos.x + (cursor_region.x * scale.x), - target_pos.y + (cursor_region.y * scale.y) + target_pos.x + (self->cursor_region.x * scale.x), + target_pos.y + (self->cursor_region.y * scale.y) }; self->params.egl->glEnable(GL_SCISSOR_TEST); self->params.egl->glScissor(target_pos.x, target_pos.y, output_size.x, output_size.y); gsr_color_conversion_draw(color_conversion, self->texture_map.cursor_texture_id, - (vec2i){cursor_pos.x, cursor_pos.y}, (vec2i){cursor_region.width * scale.x, cursor_region.height * scale.y}, - (vec2i){0, 0}, (vec2i){cursor_region.width, cursor_region.height}, - 0.0f, false, GSR_SOURCE_COLOR_RGB); + (vec2i){cursor_pos.x, cursor_pos.y}, (vec2i){self->cursor_region.width * scale.x, self->cursor_region.height * scale.y}, + (vec2i){0, 0}, (vec2i){self->cursor_region.width, self->cursor_region.height}, (vec2i){self->cursor_region.width, self->cursor_region.height}, + GSR_ROT_0, GSR_SOURCE_COLOR_RGB, false, true); self->params.egl->glDisable(GL_SCISSOR_TEST); } - self->params.egl->glFlush(); - self->params.egl->glFinish(); + //self->params.egl->glFlush(); + //self->params.egl->glFinish(); gsr_capture_portal_cleanup_plane_fds(self); @@ -417,6 +399,13 @@ static bool gsr_capture_portal_uses_external_image(gsr_capture *cap) { return true; } +static bool gsr_capture_portal_should_stop(gsr_capture *cap, bool *err) { + gsr_capture_portal *self = cap->priv; + if(err) + *err = self->stop_is_error; + return self->should_stop; +} + static bool gsr_capture_portal_is_damaged(gsr_capture *cap) { gsr_capture_portal *self = cap->priv; return gsr_pipewire_video_is_damaged(&self->pipewire); @@ -427,8 +416,7 @@ static void gsr_capture_portal_clear_damage(gsr_capture *cap) { gsr_pipewire_video_clear_damage(&self->pipewire); } -static void gsr_capture_portal_destroy(gsr_capture *cap, AVCodecContext *video_codec_context) { - (void)video_codec_context; +static void gsr_capture_portal_destroy(gsr_capture *cap) { gsr_capture_portal *self = cap->priv; if(cap->priv) { gsr_capture_portal_stop(self); @@ -459,7 +447,8 @@ gsr_capture* gsr_capture_portal_create(const gsr_capture_portal_params *params) *cap = (gsr_capture) { .start = gsr_capture_portal_start, .tick = NULL, - .should_stop = NULL, + .should_stop = gsr_capture_portal_should_stop, + .capture_has_synchronous_task = gsr_capture_portal_capture_has_synchronous_task, .capture = gsr_capture_portal_capture, .uses_external_image = gsr_capture_portal_uses_external_image, .is_damaged = gsr_capture_portal_is_damaged, diff --git a/src/capture/xcomposite.c b/src/capture/xcomposite.c index 66c700d..db41f63 100644 --- a/src/capture/xcomposite.c +++ b/src/capture/xcomposite.c @@ -12,9 +12,6 @@ #include <X11/Xlib.h> -#include <libavutil/frame.h> -#include <libavcodec/avcodec.h> - typedef struct { gsr_capture_xcomposite_params params; Display *display; @@ -31,14 +28,12 @@ typedef struct { double window_resize_timer; WindowTexture window_texture; - AVCodecContext *video_codec_context; Atom net_active_window_atom; gsr_cursor cursor; bool clear_background; - bool fast_path_failed; } gsr_capture_xcomposite; static void gsr_capture_xcomposite_stop(gsr_capture_xcomposite *self) { @@ -64,7 +59,7 @@ static Window get_focused_window(Display *display, Atom net_active_window_atom) return None; } -static int gsr_capture_xcomposite_start(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame *frame) { +static int gsr_capture_xcomposite_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) { gsr_capture_xcomposite *self = cap->priv; if(self->params.follow_focused) { @@ -95,10 +90,8 @@ static int gsr_capture_xcomposite_start(gsr_capture *cap, AVCodecContext *video_ // TODO: Get select and add these on top of it and then restore at the end. Also do the same in other xcomposite XSelectInput(self->display, self->window, StructureNotifyMask | ExposureMask); - /* Disable vsync */ - self->params.egl->eglSwapInterval(self->params.egl->egl_display, 0); if(window_texture_init(&self->window_texture, self->display, self->window, self->params.egl) != 0 && !self->params.follow_focused) { - fprintf(stderr, "gsr error: gsr_capture_xcomposite_start: failed to get window texture for window %ld\n", self->window); + fprintf(stderr, "gsr error: gsr_capture_xcomposite_start: failed to get window texture for window %ld\n", (long)self->window); return -1; } @@ -116,22 +109,13 @@ static int gsr_capture_xcomposite_start(gsr_capture *cap, AVCodecContext *video_ self->params.egl->glBindTexture(GL_TEXTURE_2D, 0); if(self->params.output_resolution.x == 0 && self->params.output_resolution.y == 0) { - self->params.output_resolution = self->texture_size; - video_codec_context->width = FFALIGN(self->texture_size.x, 2); - video_codec_context->height = FFALIGN(self->texture_size.y, 2); + capture_metadata->width = self->texture_size.x; + capture_metadata->height = self->texture_size.y; } else { - video_codec_context->width = FFALIGN(self->params.output_resolution.x, 2); - video_codec_context->height = FFALIGN(self->params.output_resolution.y, 2); + capture_metadata->width = self->params.output_resolution.x; + capture_metadata->height = self->params.output_resolution.y; } - self->fast_path_failed = self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD && !gl_driver_version_greater_than(self->params.egl, 24, 0, 9); - if(self->fast_path_failed) - fprintf(stderr, "gsr warning: gsr_capture_kms_start: your amd driver (mesa) version is known to be buggy (<= version 24.0.9), falling back to opengl copy\n"); - - frame->width = video_codec_context->width; - frame->height = video_codec_context->height; - - self->video_codec_context = video_codec_context; self->window_resize_timer = clock_get_monotonic_seconds(); return 0; } @@ -255,9 +239,8 @@ static bool gsr_capture_xcomposite_should_stop(gsr_capture *cap, bool *err) { return false; } -static int gsr_capture_xcomposite_capture(gsr_capture *cap, AVFrame *frame, gsr_color_conversion *color_conversion) { +static int gsr_capture_xcomposite_capture(gsr_capture *cap, gsr_capture_metadata *capture_metdata, gsr_color_conversion *color_conversion) { gsr_capture_xcomposite *self = cap->priv; - (void)frame; if(self->clear_background) { self->clear_background = false; @@ -268,27 +251,15 @@ static int gsr_capture_xcomposite_capture(gsr_capture *cap, AVFrame *frame, gsr_ vec2i output_size = is_scaled ? self->params.output_resolution : self->texture_size; output_size = scale_keep_aspect_ratio(self->texture_size, output_size); - const vec2i target_pos = { max_int(0, frame->width / 2 - output_size.x / 2), max_int(0, frame->height / 2 - output_size.y / 2) }; + const vec2i target_pos = { max_int(0, capture_metdata->width / 2 - output_size.x / 2), max_int(0, capture_metdata->height / 2 - output_size.y / 2) }; - self->params.egl->glFlush(); - self->params.egl->glFinish(); - - /* Fast opengl free path */ - if(!self->fast_path_failed && video_codec_context_is_vaapi(self->video_codec_context) && self->params.egl->gpu_info.vendor == GSR_GPU_VENDOR_AMD) { - if(!vaapi_copy_egl_image_to_video_surface(self->params.egl, self->window_texture.image, (vec2i){0, 0}, self->texture_size, target_pos, output_size, self->video_codec_context, frame)) { - fprintf(stderr, "gsr error: gsr_capture_xcomposite_capture: vaapi_copy_egl_image_to_video_surface failed, falling back to opengl copy. Please report this as an issue at https://github.com/dec05eba/gpu-screen-recorder-issues\n"); - self->fast_path_failed = true; - } - } else { - self->fast_path_failed = true; - } + //self->params.egl->glFlush(); + //self->params.egl->glFinish(); - if(self->fast_path_failed) { - gsr_color_conversion_draw(color_conversion, window_texture_get_opengl_texture_id(&self->window_texture), - target_pos, output_size, - (vec2i){0, 0}, self->texture_size, - 0.0f, false, GSR_SOURCE_COLOR_RGB); - } + gsr_color_conversion_draw(color_conversion, window_texture_get_opengl_texture_id(&self->window_texture), + target_pos, output_size, + (vec2i){0, 0}, self->texture_size, self->texture_size, + GSR_ROT_0, GSR_SOURCE_COLOR_RGB, false, false); if(self->params.record_cursor && self->cursor.visible) { const vec2d scale = { @@ -303,19 +274,17 @@ static int gsr_capture_xcomposite_capture(gsr_capture *cap, AVFrame *frame, gsr_ target_pos.y + (self->cursor.position.y - self->cursor.hotspot.y) * scale.y }; - self->params.egl->glEnable(GL_SCISSOR_TEST); - self->params.egl->glScissor(target_pos.x, target_pos.y, output_size.x, output_size.y); + if(cursor_pos.x < target_pos.x || cursor_pos.x + self->cursor.size.x > target_pos.x + output_size.x || cursor_pos.y < target_pos.y || cursor_pos.y + self->cursor.size.y > target_pos.y + output_size.y) + self->clear_background = true; gsr_color_conversion_draw(color_conversion, self->cursor.texture_id, cursor_pos, (vec2i){self->cursor.size.x * scale.x, self->cursor.size.y * scale.y}, - (vec2i){0, 0}, self->cursor.size, - 0.0f, false, GSR_SOURCE_COLOR_RGB); - - self->params.egl->glDisable(GL_SCISSOR_TEST); + (vec2i){0, 0}, self->cursor.size, self->cursor.size, + GSR_ROT_0, GSR_SOURCE_COLOR_RGB, false, true); } - self->params.egl->glFlush(); - self->params.egl->glFinish(); + //self->params.egl->glFlush(); + //self->params.egl->glFinish(); return 0; } @@ -325,8 +294,7 @@ static uint64_t gsr_capture_xcomposite_get_window_id(gsr_capture *cap) { return self->window; } -static void gsr_capture_xcomposite_destroy(gsr_capture *cap, AVCodecContext *video_codec_context) { - (void)video_codec_context; +static void gsr_capture_xcomposite_destroy(gsr_capture *cap) { if(cap->priv) { gsr_capture_xcomposite_stop(cap->priv); free(cap->priv); diff --git a/src/capture/ximage.c b/src/capture/ximage.c new file mode 100644 index 0000000..9b02907 --- /dev/null +++ b/src/capture/ximage.c @@ -0,0 +1,247 @@ +#include "../../include/capture/ximage.h" +#include "../../include/utils.h" +#include "../../include/cursor.h" +#include "../../include/color_conversion.h" +#include "../../include/window/window.h" + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <assert.h> + +#include <X11/Xlib.h> + +/* TODO: update when monitors are reconfigured */ + +typedef struct { + gsr_capture_ximage_params params; + Display *display; + gsr_cursor cursor; + gsr_monitor monitor; + vec2i capture_pos; + vec2i capture_size; + unsigned int texture_id; + Window root_window; +} gsr_capture_ximage; + +static void gsr_capture_ximage_stop(gsr_capture_ximage *self) { + gsr_cursor_deinit(&self->cursor); + if(self->texture_id) { + self->params.egl->glDeleteTextures(1, &self->texture_id); + self->texture_id = 0; + } +} + +static int max_int(int a, int b) { + return a > b ? a : b; +} + +static int gsr_capture_ximage_start(gsr_capture *cap, gsr_capture_metadata *capture_metadata) { + gsr_capture_ximage *self = cap->priv; + self->root_window = DefaultRootWindow(self->display); + + if(gsr_cursor_init(&self->cursor, self->params.egl, self->display) != 0) { + gsr_capture_ximage_stop(self); + return -1; + } + + if(!get_monitor_by_name(self->params.egl, GSR_CONNECTION_X11, self->params.display_to_capture, &self->monitor)) { + fprintf(stderr, "gsr error: gsr_capture_ximage_start: failed to find monitor by name \"%s\"\n", self->params.display_to_capture); + gsr_capture_ximage_stop(self); + return -1; + } + + self->capture_pos = self->monitor.pos; + self->capture_size = self->monitor.size; + + if(self->params.region_size.x > 0 && self->params.region_size.y > 0) + self->capture_size = self->params.region_size; + + if(self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0) { + self->params.output_resolution = scale_keep_aspect_ratio(self->capture_size, self->params.output_resolution); + capture_metadata->width = self->params.output_resolution.x; + capture_metadata->height = self->params.output_resolution.y; + } else if(self->params.region_size.x > 0 && self->params.region_size.y > 0) { + capture_metadata->width = self->params.region_size.x; + capture_metadata->height = self->params.region_size.y; + } else { + capture_metadata->width = self->capture_size.x; + capture_metadata->height = self->capture_size.y; + } + + self->texture_id = gl_create_texture(self->params.egl, self->capture_size.x, self->capture_size.y, GL_RGB8, GL_RGB, GL_LINEAR); + if(self->texture_id == 0) { + fprintf(stderr, "gsr error: gsr_capture_ximage_start: failed to create texture\n"); + gsr_capture_ximage_stop(self); + return -1; + } + + return 0; +} + +static void gsr_capture_ximage_on_event(gsr_capture *cap, gsr_egl *egl) { + gsr_capture_ximage *self = cap->priv; + XEvent *xev = gsr_window_get_event_data(egl->window); + gsr_cursor_on_event(&self->cursor, xev); +} + +static bool gsr_capture_ximage_upload_to_texture(gsr_capture_ximage *self, int x, int y, int width, int height) { + const int max_width = XWidthOfScreen(DefaultScreenOfDisplay(self->display)); + const int max_height = XHeightOfScreen(DefaultScreenOfDisplay(self->display)); + + if(x < 0) + x = 0; + else if(x >= max_width) + x = max_width - 1; + + if(y < 0) + y = 0; + else if(y >= max_height) + y = max_height - 1; + + if(width < 0) + width = 0; + else if(x + width >= max_width) + width = max_width - x; + + if(height < 0) + height = 0; + else if(y + height >= max_height) + height = max_height - y; + + XImage *image = XGetImage(self->display, self->root_window, x, y, width, height, AllPlanes, ZPixmap); + if(!image) { + fprintf(stderr, "gsr error: gsr_capture_ximage_upload_to_texture: XGetImage failed\n"); + return false; + } + + bool success = false; + uint8_t *image_data = malloc(image->width * image->height * 3); + if(!image_data) { + fprintf(stderr, "gsr error: gsr_capture_ximage_upload_to_texture: failed to allocate image data\n"); + goto done; + } + + for(int y = 0; y < image->height; ++y) { + for(int x = 0; x < image->width; ++x) { + unsigned long pixel = XGetPixel(image, x, y); + unsigned char red = (pixel & image->red_mask) >> 16; + unsigned char green = (pixel & image->green_mask) >> 8; + unsigned char blue = pixel & image->blue_mask; + + const size_t texture_data_index = (x + y * image->width) * 3; + image_data[texture_data_index + 0] = red; + image_data[texture_data_index + 1] = green; + image_data[texture_data_index + 2] = blue; + } + } + + self->params.egl->glBindTexture(GL_TEXTURE_2D, self->texture_id); + self->params.egl->glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image->width, image->height, GL_RGB, GL_UNSIGNED_BYTE, image_data); + self->params.egl->glBindTexture(GL_TEXTURE_2D, 0); + success = true; + + done: + free(image_data); + XDestroyImage(image); + return success; +} + +static int gsr_capture_ximage_capture(gsr_capture *cap, gsr_capture_metadata *capture_metdata, gsr_color_conversion *color_conversion) { + gsr_capture_ximage *self = cap->priv; + + const bool is_scaled = self->params.output_resolution.x > 0 && self->params.output_resolution.y > 0; + vec2i output_size = is_scaled ? self->params.output_resolution : self->capture_size; + output_size = scale_keep_aspect_ratio(self->capture_size, output_size); + + const vec2i target_pos = { max_int(0, capture_metdata->width / 2 - output_size.x / 2), max_int(0, capture_metdata->height / 2 - output_size.y / 2) }; + gsr_capture_ximage_upload_to_texture(self, self->capture_pos.x + self->params.region_position.x, self->capture_pos.y + self->params.region_position.y, self->capture_size.x, self->capture_size.y); + + gsr_color_conversion_draw(color_conversion, self->texture_id, + target_pos, output_size, + (vec2i){0, 0}, self->capture_size, self->capture_size, + GSR_ROT_0, GSR_SOURCE_COLOR_RGB, false, false); + + if(self->params.record_cursor && self->cursor.visible) { + const vec2d scale = { + self->capture_size.x == 0 ? 0 : (double)output_size.x / (double)self->capture_size.x, + self->capture_size.y == 0 ? 0 : (double)output_size.y / (double)self->capture_size.y + }; + + gsr_cursor_tick(&self->cursor, self->root_window); + + const vec2i cursor_pos = { + target_pos.x + (self->cursor.position.x - self->cursor.hotspot.x) * scale.x - self->capture_pos.x - self->params.region_position.x, + target_pos.y + (self->cursor.position.y - self->cursor.hotspot.y) * scale.y - self->capture_pos.y - self->params.region_position.y + }; + + self->params.egl->glEnable(GL_SCISSOR_TEST); + self->params.egl->glScissor(target_pos.x, target_pos.y, output_size.x, output_size.y); + + gsr_color_conversion_draw(color_conversion, self->cursor.texture_id, + cursor_pos, (vec2i){self->cursor.size.x * scale.x, self->cursor.size.y * scale.y}, + (vec2i){0, 0}, self->cursor.size, self->cursor.size, + GSR_ROT_0, GSR_SOURCE_COLOR_RGB, false, true); + + self->params.egl->glDisable(GL_SCISSOR_TEST); + } + + self->params.egl->glFlush(); + self->params.egl->glFinish(); + + return 0; +} + +static void gsr_capture_ximage_destroy(gsr_capture *cap) { + gsr_capture_ximage *self = cap->priv; + if(cap->priv) { + gsr_capture_ximage_stop(self); + free((void*)self->params.display_to_capture); + self->params.display_to_capture = NULL; + free(self); + cap->priv = NULL; + } + free(cap); +} + +gsr_capture* gsr_capture_ximage_create(const gsr_capture_ximage_params *params) { + if(!params) { + fprintf(stderr, "gsr error: gsr_capture_ximage_create params is NULL\n"); + return NULL; + } + + gsr_capture *cap = calloc(1, sizeof(gsr_capture)); + if(!cap) + return NULL; + + gsr_capture_ximage *cap_ximage = calloc(1, sizeof(gsr_capture_ximage)); + if(!cap_ximage) { + free(cap); + return NULL; + } + + const char *display_to_capture = strdup(params->display_to_capture); + if(!display_to_capture) { + free(cap); + free(cap_ximage); + return NULL; + } + + cap_ximage->params = *params; + cap_ximage->display = gsr_window_get_display(params->egl->window); + cap_ximage->params.display_to_capture = display_to_capture; + + *cap = (gsr_capture) { + .start = gsr_capture_ximage_start, + .on_event = gsr_capture_ximage_on_event, + .tick = NULL, + .should_stop = NULL, + .capture = gsr_capture_ximage_capture, + .uses_external_image = NULL, + .get_window_id = NULL, + .destroy = gsr_capture_ximage_destroy, + .priv = cap_ximage + }; + + return cap; +} |