diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c index 23de498447..ec4bc46818 100644 --- a/app/src/scrcpy.c +++ b/app/src/scrcpy.c @@ -83,6 +83,9 @@ static SDL_bool event_loop(void) { switch (event.window.event) { case SDL_WINDOWEVENT_EXPOSED: case SDL_WINDOWEVENT_SIZE_CHANGED: + if (!screen_update_logical_size_if_needed(&screen)) { + return SDL_FALSE; + } screen_render(&screen); break; } diff --git a/app/src/screen.c b/app/src/screen.c index 5b4f0634db..9f8cff510e 100644 --- a/app/src/screen.c +++ b/app/src/screen.c @@ -65,6 +65,33 @@ static void set_window_size(struct screen *screen, struct size new_size) { } } +void get_window_scale(SDL_Window *window, float *scale_x, float *scale_y) { + int win_w, win_h; + SDL_GetWindowSize(window, &win_w, &win_h); + int output_w, output_h; + SDL_GL_GetDrawableSize(window, &output_w, &output_h); + *scale_x = (float)output_w / win_w; + *scale_y = (float)output_h / win_h; +} + +SDL_bool screen_update_logical_size_if_needed(struct screen *screen) { + float scale_x, scale_y; + get_window_scale(screen->window, &scale_x, &scale_y); + if (scale_x != screen->scale_x || scale_y != screen->scale_y) { + int logical_width, logical_height; + SDL_RenderGetLogicalSize(screen->renderer, &logical_width, &logical_height); + logical_width *= scale_x / screen->scale_x; + logical_height *= scale_y / screen->scale_y; + screen->scale_x = scale_x; + screen->scale_y = scale_y; + if (SDL_RenderSetLogicalSize(screen->renderer, logical_width, logical_height)) { + LOGE("Could not set renderer logical size: %s", SDL_GetError()); + return SDL_FALSE; + } + } + return SDL_TRUE; +} + // get the preferred display bounds (i.e. the screen bounds with some margins) static SDL_bool get_preferred_display_bounds(struct size *bounds) { SDL_Rect rect; @@ -168,6 +195,7 @@ SDL_bool screen_init_rendering(struct screen *screen, const char *device_name, s screen_destroy(screen); return SDL_FALSE; } + get_window_scale(screen->window, &screen->scale_x, &screen->scale_y); SDL_Surface *icon = read_xpm(icon_xpm); if (!icon) { diff --git a/app/src/screen.h b/app/src/screen.h index 13103eaa6f..41b96aeebc 100644 --- a/app/src/screen.h +++ b/app/src/screen.h @@ -12,6 +12,8 @@ struct screen { SDL_Renderer *renderer; SDL_Texture *texture; struct size frame_size; + float scale_x; + float scale_y; //used only in fullscreen mode to know the windowed window size struct size windowed_window_size; SDL_bool has_frame; @@ -66,4 +68,8 @@ void screen_resize_to_fit(struct screen *screen); // resize window to 1:1 (pixel-perfect) void screen_resize_to_pixel_perfect(struct screen *screen); +void get_window_scale(SDL_Window *window, float *scale_x, float *scale_y); + +SDL_bool screen_update_logical_size_if_needed(struct screen *screen); + #endif