diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 8784b8d3..df660c3e 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -46,19 +46,6 @@ static void count_frame(void) { } } -static SDL_bool handle_new_frame(void) { - mutex_lock(frames.mutex); - const AVFrame *frame = frames_consume_rendered_frame(&frames); - if (!screen_update(&screen, frame)){ - mutex_unlock(frames.mutex); - return SDL_FALSE; - } - mutex_unlock(frames.mutex); - - screen_render(&screen); - return SDL_TRUE; -} - static void event_loop(void) { SDL_Event event; while (SDL_WaitEvent(&event)) { @@ -70,7 +57,7 @@ static void event_loop(void) { SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "User requested to quit"); return; case EVENT_NEW_FRAME: - if (!handle_new_frame()) { + if (!screen_update_frame(&screen, &frames)) { return; } count_frame(); // display fps for debug diff --git a/app/src/screen.c b/app/src/screen.c index 33554429..b56f8861 100644 --- a/app/src/screen.c +++ b/app/src/screen.c @@ -4,6 +4,7 @@ #include #include "icon.xpm" +#include "lockutil.h" #include "tinyxpm.h" #define DISPLAY_MARGINS 96 @@ -237,12 +238,18 @@ static void update_texture(struct screen *screen, const AVFrame *frame) { frame->data[2], frame->linesize[2]); } -SDL_bool screen_update(struct screen *screen, const AVFrame *frame) { +SDL_bool screen_update_frame(struct screen *screen, struct frames *frames) { + mutex_lock(frames->mutex); + const AVFrame *frame = frames_consume_rendered_frame(frames); struct size new_frame_size = {frame->width, frame->height}; if (!prepare_for_frame(screen, new_frame_size)) { + mutex_unlock(frames->mutex); return SDL_FALSE; } update_texture(screen, frame); + mutex_unlock(frames->mutex); + + screen_render(screen); return SDL_TRUE; } diff --git a/app/src/screen.h b/app/src/screen.h index 7e91751b..1dc31904 100644 --- a/app/src/screen.h +++ b/app/src/screen.h @@ -5,6 +5,7 @@ #include #include "common.h" +#include "frames.h" struct screen { SDL_Window *window; @@ -47,8 +48,8 @@ SDL_bool screen_init_rendering(struct screen *screen, // destroy window, renderer and texture (if any) void screen_destroy(struct screen *screen); -// resize if necessary and write the frame into the texture -SDL_bool screen_update(struct screen *screen, const AVFrame *frame); +// resize if necessary and write the rendered frame into the texture +SDL_bool screen_update_frame(struct screen *screen, struct frames *frames); // render the texture to the renderer void screen_render(struct screen *screen);