From a3fedae32937f8965c9905e12633fa4de0c3658e Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 9 Mar 2024 16:59:09 +0100 Subject: Draw cursor in window capture --- src/capture/nvfbc.c | 10 +++--- src/capture/xcomposite.c | 77 +++++++++++++++++++++++++++++------------- src/capture/xcomposite_cuda.c | 8 +++++ src/capture/xcomposite_vaapi.c | 7 ++++ 4 files changed, 74 insertions(+), 28 deletions(-) (limited to 'src/capture') diff --git a/src/capture/nvfbc.c b/src/capture/nvfbc.c index d909e1e..a9a7cc8 100644 --- a/src/capture/nvfbc.c +++ b/src/capture/nvfbc.c @@ -397,8 +397,8 @@ static int gsr_capture_nvfbc_capture(gsr_capture *cap, AVFrame *frame) { cap_nvfbc->params.egl->glClear(0); gsr_color_conversion_draw(&cap_nvfbc->base.color_conversion, cap_nvfbc->setup_params.dwTextures[grab_params.dwTextureIndex], - (vec2i){0, 0}, (vec2i){cap_nvfbc->base.video_codec_context->width, cap_nvfbc->base.video_codec_context->height}, - (vec2i){0, 0}, (vec2i){cap_nvfbc->base.video_codec_context->width, cap_nvfbc->base.video_codec_context->height}, + (vec2i){0, 0}, (vec2i){frame->width, frame->height}, + (vec2i){0, 0}, (vec2i){frame->width, frame->height}, 0.0f, false); cap_nvfbc->params.egl->glXSwapBuffers(cap_nvfbc->params.egl->x11.dpy, cap_nvfbc->params.egl->x11.window); @@ -416,11 +416,11 @@ static int gsr_capture_nvfbc_capture(gsr_capture *cap, AVFrame *frame) { memcpy_struct.dstMemoryType = CU_MEMORYTYPE_DEVICE; memcpy_struct.srcArray = cap_nvfbc->mapped_arrays[i]; - memcpy_struct.srcPitch = cap_nvfbc->base.video_codec_context->width / div[i]; + memcpy_struct.srcPitch = frame->width / div[i]; memcpy_struct.dstDevice = (CUdeviceptr)frame->data[i]; memcpy_struct.dstPitch = frame->linesize[i]; - memcpy_struct.WidthInBytes = cap_nvfbc->base.video_codec_context->width * (cap_nvfbc->params.hdr ? 2 : 1); - memcpy_struct.Height = cap_nvfbc->base.video_codec_context->height / div[i]; + memcpy_struct.WidthInBytes = frame->width * (cap_nvfbc->params.hdr ? 2 : 1); + memcpy_struct.Height = frame->height / div[i]; // TODO: Remove this copy if possible cap_nvfbc->cuda.cuMemcpy2DAsync_v2(&memcpy_struct, cap_nvfbc->cuda_stream); } diff --git a/src/capture/xcomposite.c b/src/capture/xcomposite.c index dbe841d..73da951 100644 --- a/src/capture/xcomposite.c +++ b/src/capture/xcomposite.c @@ -57,8 +57,8 @@ int gsr_capture_xcomposite_start(gsr_capture_xcomposite *self, AVCodecContext *v /* TODO: Do these in tick, and allow error if follow_focused */ XWindowAttributes attr; - if(!XGetWindowAttributes(self->params.egl->x11.dpy, self->params.window, &attr) && !self->params.follow_focused) { - fprintf(stderr, "gsr error: gsr_capture_xcomposite_start failed: invalid window id: %lu\n", self->params.window); + if(!XGetWindowAttributes(self->params.egl->x11.dpy, self->window, &attr) && !self->params.follow_focused) { + fprintf(stderr, "gsr error: gsr_capture_xcomposite_start failed: invalid window id: %lu\n", self->window); return -1; } @@ -69,7 +69,7 @@ int gsr_capture_xcomposite_start(gsr_capture_xcomposite *self, AVCodecContext *v XSelectInput(self->params.egl->x11.dpy, DefaultRootWindow(self->params.egl->x11.dpy), PropertyChangeMask); // TODO: Get select and add these on top of it and then restore at the end. Also do the same in other xcomposite - XSelectInput(self->params.egl->x11.dpy, self->params.window, StructureNotifyMask | ExposureMask); + XSelectInput(self->params.egl->x11.dpy, self->window, StructureNotifyMask | ExposureMask); if(!self->params.egl->eglExportDMABUFImageQueryMESA) { fprintf(stderr, "gsr error: gsr_capture_xcomposite_start: could not find eglExportDMABUFImageQueryMESA\n"); @@ -83,8 +83,13 @@ int gsr_capture_xcomposite_start(gsr_capture_xcomposite *self, AVCodecContext *v /* Disable vsync */ self->params.egl->eglSwapInterval(self->params.egl->egl_display, 0); - if(window_texture_init(&self->window_texture, self->params.egl->x11.dpy, self->params.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->params.window); + if(window_texture_init(&self->window_texture, self->params.egl->x11.dpy, 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); + return -1; + } + + if(gsr_cursor_init(&self->cursor, self->params.egl, self->params.egl->x11.dpy) != 0) { + gsr_capture_xcomposite_stop(self, video_codec_context); return -1; } @@ -115,14 +120,9 @@ int gsr_capture_xcomposite_start(gsr_capture_xcomposite *self, AVCodecContext *v } void gsr_capture_xcomposite_stop(gsr_capture_xcomposite *self, AVCodecContext *video_codec_context) { + (void)video_codec_context; window_texture_deinit(&self->window_texture); - - if(video_codec_context->hw_device_ctx) - av_buffer_unref(&video_codec_context->hw_device_ctx); - if(video_codec_context->hw_frames_ctx) - av_buffer_unref(&video_codec_context->hw_frames_ctx); - - gsr_capture_base_stop(&self->base, self->params.egl); + gsr_cursor_deinit(&self->cursor); } void gsr_capture_xcomposite_tick(gsr_capture_xcomposite *self, AVCodecContext *video_codec_context) { @@ -168,6 +168,8 @@ void gsr_capture_xcomposite_tick(gsr_capture_xcomposite *self, AVCodecContext *v break; } } + + gsr_cursor_update(&self->cursor, &self->xev); } if(self->params.follow_focused && !self->follow_focused_initialized) { @@ -194,17 +196,6 @@ void gsr_capture_xcomposite_tick(gsr_capture_xcomposite *self, AVCodecContext *v window_texture_deinit(&self->window_texture); window_texture_init(&self->window_texture, self->params.egl->x11.dpy, self->window, self->params.egl); // TODO: Do not do the below window_texture_on_resize after this - - self->texture_size.x = 0; - self->texture_size.y = 0; - - self->params.egl->glBindTexture(GL_TEXTURE_2D, window_texture_get_opengl_texture_id(&self->window_texture)); - self->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &self->texture_size.x); - self->params.egl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &self->texture_size.y); - self->params.egl->glBindTexture(GL_TEXTURE_2D, 0); - - self->texture_size.x = min_int(video_codec_context->width, max_int(2, even_number_ceil(self->texture_size.x))); - self->texture_size.y = min_int(video_codec_context->height, max_int(2, even_number_ceil(self->texture_size.y))); } } @@ -252,11 +243,51 @@ int gsr_capture_xcomposite_capture(gsr_capture_xcomposite *self, AVFrame *frame) const int target_x = max_int(0, frame->width / 2 - self->texture_size.x / 2); const int target_y = max_int(0, frame->height / 2 - self->texture_size.y / 2); + // TODO: Can we do this a better way than to call it every capture? + gsr_cursor_tick(&self->cursor, self->window); + + const vec2i cursor_pos = { + target_x + self->cursor.position.x - self->cursor.hotspot.x, + target_y + self->cursor.position.y - self->cursor.hotspot.y + }; + + const bool cursor_completely_inside_window = + cursor_pos.x >= target_x && + cursor_pos.x <= target_x + self->texture_size.x && + cursor_pos.y >= target_y && + cursor_pos.y <= target_y + self->texture_size.x; + + const bool cursor_inside_window = + cursor_pos.x + self->cursor.size.x >= target_x && + cursor_pos.x <= target_x + self->texture_size.x && + cursor_pos.y + self->cursor.size.y >= target_y && + cursor_pos.y <= target_y + self->texture_size.x; + + if(self->clear_next_frame) { + self->clear_next_frame = false; + gsr_color_conversion_clear(&self->base.color_conversion); + } + + /* + We dont draw the cursor if it's outside the window but if it's partially inside the window then the cursor area that is outside the window + will not get overdrawn the next frame causing a cursor trail to be visible since we dont clear the background. + To fix this we detect if the cursor is partially inside the window and clear the background only in that case. + */ + if(!cursor_completely_inside_window && cursor_inside_window) + self->clear_next_frame = true; + gsr_color_conversion_draw(&self->base.color_conversion, window_texture_get_opengl_texture_id(&self->window_texture), (vec2i){target_x, target_y}, self->texture_size, (vec2i){0, 0}, self->texture_size, 0.0f, false); + if(cursor_inside_window) { + gsr_color_conversion_draw(&self->base.color_conversion, self->cursor.texture_id, + cursor_pos, self->cursor.size, + (vec2i){0, 0}, self->cursor.size, + 0.0f, false); + } + self->params.egl->eglSwapBuffers(self->params.egl->egl_display, self->params.egl->egl_surface); //self->params.egl->glFlush(); //self->params.egl->glFinish(); diff --git a/src/capture/xcomposite_cuda.c b/src/capture/xcomposite_cuda.c index 12cea8c..0a324bd 100644 --- a/src/capture/xcomposite_cuda.c +++ b/src/capture/xcomposite_cuda.c @@ -3,6 +3,7 @@ #include #include #include +#include typedef struct { gsr_capture_xcomposite xcomposite; @@ -70,6 +71,13 @@ static void gsr_capture_xcomposite_unload_cuda_graphics(gsr_capture_xcomposite_c static void gsr_capture_xcomposite_cuda_stop(gsr_capture *cap, AVCodecContext *video_codec_context) { gsr_capture_xcomposite_cuda *cap_xcomp = cap->priv; + + if(video_codec_context->hw_device_ctx) + av_buffer_unref(&video_codec_context->hw_device_ctx); + if(video_codec_context->hw_frames_ctx) + av_buffer_unref(&video_codec_context->hw_frames_ctx); + + gsr_capture_base_stop(&cap_xcomp->xcomposite.base, cap_xcomp->xcomposite.params.egl); gsr_capture_xcomposite_stop(&cap_xcomp->xcomposite, video_codec_context); gsr_capture_xcomposite_unload_cuda_graphics(cap_xcomp); gsr_cuda_unload(&cap_xcomp->cuda); diff --git a/src/capture/xcomposite_vaapi.c b/src/capture/xcomposite_vaapi.c index 570901b..a19956d 100644 --- a/src/capture/xcomposite_vaapi.c +++ b/src/capture/xcomposite_vaapi.c @@ -5,6 +5,7 @@ #include #include #include +#include typedef struct { gsr_capture_xcomposite xcomposite; @@ -62,6 +63,12 @@ static void gsr_capture_xcomposite_vaapi_stop(gsr_capture *cap, AVCodecContext * } } + if(video_codec_context->hw_device_ctx) + av_buffer_unref(&video_codec_context->hw_device_ctx); + if(video_codec_context->hw_frames_ctx) + av_buffer_unref(&video_codec_context->hw_frames_ctx); + + gsr_capture_base_stop(&cap_xcomp->xcomposite.base, cap_xcomp->xcomposite.params.egl); gsr_capture_xcomposite_stop(&cap_xcomp->xcomposite, video_codec_context); } -- cgit v1.2.3