Display and control your Android device
Add --render-fit feature
Romain Vimontb570bb026bb7
diff --git a/app/data/bash-completion/scrcpy b/app/data/bash-completion/scrcpyindex e8cae851..c2c6da0d 100644--- a/app/data/bash-completion/scrcpy+++ b/app/data/bash-completion/scrcpy@@ -82,6 +82,7 @@ _scrcpy() {--record-format=--record-orientation=--render-driver=+ --render-fit=--require-audio-s --serial=-S --turn-screen-off@@ -178,6 +179,10 @@ _scrcpy() {COMPREPLY=($(compgen -W 'direct3d opengl opengles2 opengles metal software' -- "$cur"))return;;+ --render-fit)+ COMPREPLY=($(compgen -W 'letterbox unscaled' -- "$cur"))+ return+ ;;--shortcut-mod)# Only auto-complete a single keyCOMPREPLY=($(compgen -W 'lctrl rctrl lalt ralt lsuper rsuper' -- "$cur"))diff --git a/app/data/zsh-completion/_scrcpy b/app/data/zsh-completion/_scrcpyindex 8896e738..20a676df 100644--- a/app/data/zsh-completion/_scrcpy+++ b/app/data/zsh-completion/_scrcpy@@ -87,6 +87,7 @@ arguments=('--record-format=[Force recording format]:format:(mp4 mkv m4a mka opus aac flac wav)''--record-orientation=[Set the record orientation]:orientation values:(0 90 180 270)''--render-driver=[Request SDL to use the given render driver]:driver name:(direct3d opengl opengles2 opengles metal software)'+ '--render-fit=[Set the render-fit mode]:mode:(letterbox unscaled)''--require-audio=[Make scrcpy fail if audio is enabled but does not work]'{-s,--serial=}'[The device serial number \(mandatory for multiple devices only\)]:serial:($("${ADB-adb}" devices | awk '\''$2 == "device" {print $1}'\''))'{-S,--turn-screen-off}'[Turn the device screen off immediately]'diff --git a/app/scrcpy.1 b/app/scrcpy.1index bfa1267f..a9a67b37 100644--- a/app/scrcpy.1+++ b/app/scrcpy.1@@ -524,6 +524,17 @@ Supported names are currently "direct3d", "opengl", "opengles2", "opengles", "me<https://wiki.libsdl.org/SDL_HINT_RENDER_DRIVER>+.TP+.BI "\-\-render\-fit " mode+Set the render-fit mode to configure how the rendering fits the window.++Possible values are "letterbox" and "unscaled":++ - "letterbox": preserve the aspect ratio and fit the window as best as possible (black bars are added either at the top and bottom or at the sides if needed).+ - "unscaled": render the display without scaling.++Default is "letterbox".+.TP.B \-\-require\-audioBy default, scrcpy mirrors only the video if audio capture fails on the device. This option makes scrcpy fail if audio is enabled but does not work.diff --git a/app/src/cli.c b/app/src/cli.cindex 2cb0d3e2..4fb7816d 100644--- a/app/src/cli.c+++ b/app/src/cli.c@@ -107,6 +107,7 @@ enum {OPT_NO_WINDOW_ASPECT_RATIO_LOCK,OPT_KEEP_ACTIVE,OPT_BACKGROUND_COLOR,+ OPT_RENDER_FIT,};struct sc_option {@@ -792,6 +793,19 @@ static const struct sc_option options[] = {"\"opengles2\", \"opengles\", \"metal\" and \"software\".\n""<https://wiki.libsdl.org/SDL_HINT_RENDER_DRIVER>",},+ {+ .longopt_id = OPT_RENDER_FIT,+ .longopt = "render-fit",+ .argdesc = "mode",+ .text = "Set the render-fit mode to configure how the rendering fits "+ "the window.\n"+ "Possible values are \"letterbox\" and \"unscaled\".\n"+ "\"letterbox\": preserve the aspect ratio and fit the window "+ "as best as possible (black bars are added either at the top "+ "and bottom or at the sides if needed).\n"+ "\"unscaled\": render the display without scaling.\n"+ "Default is \"letterbox\".",+ },{.longopt_id = OPT_REQUIRE_AUDIO,.longopt = "require-audio",@@ -2409,6 +2423,22 @@ parse_hex_color(const char *s, uint32_t *color) {return true;}+static bool+parse_render_fit(const char *optarg, enum sc_render_fit *mode) {+ if (!strcmp(optarg, "letterbox")) {+ *mode = SC_RENDER_FIT_LETTERBOX;+ return true;+ }++ if (!strcmp(optarg, "unscaled")) {+ *mode = SC_RENDER_FIT_UNSCALED;+ return true;+ }++ LOGE("Unsupported render-fit: %s (expected letterbox or unscaled)", optarg);+ return false;+}+static boolparse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],const char *optstring, const struct option *longopts) {@@ -2850,6 +2880,11 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],return false;}break;+ case OPT_RENDER_FIT:+ if (!parse_render_fit(optarg, &opts->render_fit)) {+ return false;+ }+ break;default:// getopt prints the error message on stderrreturn false;diff --git a/app/src/options.c b/app/src/options.cindex 0f541bf0..b4924bd4 100644--- a/app/src/options.c+++ b/app/src/options.c@@ -59,6 +59,7 @@ const struct scrcpy_options scrcpy_options_default = {.display_orientation = SC_ORIENTATION_0,.record_orientation = SC_ORIENTATION_0,.display_ime_policy = SC_DISPLAY_IME_POLICY_UNDEFINED,+ .render_fit = SC_RENDER_FIT_LETTERBOX,.window_x = SC_WINDOW_POSITION_UNDEFINED,.window_y = SC_WINDOW_POSITION_UNDEFINED,.window_width = 0,diff --git a/app/src/options.h b/app/src/options.hindex 1046bb14..58aacf72 100644--- a/app/src/options.h+++ b/app/src/options.h@@ -220,6 +220,11 @@ enum sc_shortcut_mod {SC_SHORTCUT_MOD_RSUPER = 1 << 5,};+enum sc_render_fit {+ SC_RENDER_FIT_LETTERBOX,+ SC_RENDER_FIT_UNSCALED,+};+struct sc_port_range {uint16_t first;uint16_t last;@@ -269,6 +274,7 @@ struct scrcpy_options {enum sc_orientation display_orientation;enum sc_orientation record_orientation;enum sc_display_ime_policy display_ime_policy;+ enum sc_render_fit render_fit;int16_t window_x; // SC_WINDOW_POSITION_UNDEFINED for "auto"int16_t window_y; // SC_WINDOW_POSITION_UNDEFINED for "auto"uint16_t window_width;diff --git a/app/src/scrcpy.c b/app/src/scrcpy.cindex 05b16ed5..3ef9bd82 100644--- a/app/src/scrcpy.c+++ b/app/src/scrcpy.c@@ -774,6 +774,7 @@ aoa_complete:.background_color = options->background_color,.window_aspect_ratio_lock = options->window_aspect_ratio_lock,.window_borderless = options->window_borderless,+ .render_fit = options->render_fit,.orientation = options->display_orientation,.mipmaps = options->mipmaps,.fullscreen = options->fullscreen,diff --git a/app/src/screen.c b/app/src/screen.cindex ffff7575..cba30628 100644--- a/app/src/screen.c+++ b/app/src/screen.c@@ -158,17 +158,31 @@ sc_screen_is_relative_mode(struct sc_screen *screen) {static voidcompute_content_rect(struct sc_size render_size, struct sc_size content_size,- bool is_icon, SDL_FRect *rect) {- if (is_icon && content_size.width <= render_size.width+ bool is_icon, enum sc_render_fit render_fit,+ SDL_FRect *rect) {+ if (is_icon) {+ if (content_size.width <= render_size.width&& content_size.height <= render_size.height) {- // Center without upscaling- rect->x = (render_size.width - content_size.width) / 2.f;- rect->y = (render_size.height - content_size.height) / 2.f;+ // Center without upscaling+ rect->x = (render_size.width - content_size.width) / 2.f;+ rect->y = (render_size.height - content_size.height) / 2.f;+ rect->w = content_size.width;+ rect->h = content_size.height;+ return;+ }+ } else if (render_fit == SC_RENDER_FIT_UNSCALED) {+ // Cast to float first because input sizes are unsigned+ float x = ((float) render_size.width - content_size.width) / 2.f;+ float y = ((float) render_size.height - content_size.height) / 2.f;+ rect->x = MAX(0, x);+ rect->y = MAX(0, y);rect->w = content_size.width;rect->h = content_size.height;return;}+ assert(is_icon || render_fit == SC_RENDER_FIT_LETTERBOX);+if (is_optimal_size(render_size, content_size)) {rect->x = 0;rect->y = 0;@@ -202,7 +216,7 @@ sc_screen_update_content_rect(struct sc_screen *screen) {struct sc_size render_size =sc_sdl_get_render_output_size(screen->renderer);compute_content_rect(render_size, screen->content_size, is_icon,- &screen->rect);+ screen->render_fit, &screen->rect);}// render the texture to the renderer@@ -396,6 +410,7 @@ sc_screen_init(struct sc_screen *screen,screen->video = params->video;screen->camera = params->camera;screen->window_aspect_ratio_lock = params->window_aspect_ratio_lock;+ screen->render_fit = params->render_fit;screen->bg.r = (params->background_color >> 16) & 0xFF;screen->bg.g = (params->background_color >> 8) & 0xFF;@@ -901,9 +916,38 @@ sc_screen_resize_to_fit(struct sc_screen *screen) {return;}- struct sc_point point = sc_sdl_get_window_position(screen->window);struct sc_size window_size = sc_sdl_get_window_size(screen->window);+ if (screen->render_fit == SC_RENDER_FIT_UNSCALED) {+ struct sc_size content_size = screen->content_size;+ set_aspect_ratio(screen, content_size);+ sc_sdl_set_window_size(screen->window, content_size);++ int32_t x_offset = 0;+ if (content_size.width < window_size.width) {+ x_offset = (window_size.width - content_size.width) / 2;+ }+ int32_t y_offset = 0;+ if (content_size.height < window_size.height) {+ y_offset = (window_size.height - content_size.height) / 2;+ }+ assert(x_offset >= 0 && y_offset >= 0);+ if (x_offset || y_offset) {+ struct sc_point pos = sc_sdl_get_window_position(screen->window);+ pos.x += x_offset;+ pos.y += y_offset;+ sc_sdl_set_window_position(screen->window, pos);+ }++ LOGD("Resized to content size: %ux%u", content_size.width,+ content_size.height);+ return;+ }++ assert(screen->render_fit == SC_RENDER_FIT_LETTERBOX);++ struct sc_point point = sc_sdl_get_window_position(screen->window);+struct sc_size optimal_size =get_optimal_size(window_size, screen->content_size, false);diff --git a/app/src/screen.h b/app/src/screen.hindex 63f2e636..09dfc6d2 100644--- a/app/src/screen.h+++ b/app/src/screen.h@@ -71,6 +71,8 @@ struct sc_screen {SDL_GLContext gl_context;#endif+ enum sc_render_fit render_fit;+struct sc_size frame_size;struct sc_size content_size; // rotated frame_size@@ -126,6 +128,7 @@ struct sc_screen_params {bool window_aspect_ratio_lock;bool window_borderless;+ enum sc_render_fit render_fit;enum sc_orientation orientation;bool mipmaps;