Display and control your Android device
Add resizable virtual display feature
Romain Vimont9c4d2bbfc286
diff --git a/app/data/bash-completion/scrcpy b/app/data/bash-completion/scrcpyindex c2c6da0d..368cec30 100644--- a/app/data/bash-completion/scrcpy+++ b/app/data/bash-completion/scrcpy@@ -110,7 +110,8 @@ _scrcpy() {--window-x=--window-y=--window-width=- --window-height="+ --window-height=+ -x --flex-display"_init_completion -s || returndiff --git a/app/data/zsh-completion/_scrcpy b/app/data/zsh-completion/_scrcpyindex 20a676df..92dd07b0 100644--- a/app/data/zsh-completion/_scrcpy+++ b/app/data/zsh-completion/_scrcpy@@ -115,6 +115,7 @@ arguments=('--window-y=[Set the initial window vertical position]''--window-width=[Set the initial window width]''--window-height=[Set the initial window height]'+ {-x,--flex-display}'[Continuously resize the virtual display to match the window]')_arguments -s $argumentsdiff --git a/app/scrcpy.1 b/app/scrcpy.1index a9a67b37..c275add9 100644--- a/app/scrcpy.1+++ b/app/scrcpy.1@@ -533,7 +533,7 @@ 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".+Default is "letterbox", unless --flex-display is set, in which case it is "unscaled"..TP.B \-\-require\-audio@@ -705,6 +705,10 @@ Set the initial window height.Default is 0 (automatic).+.TP+.B \-x, \-\-flex\-display+Continuously resize the virtual display to match the window.+.SH EXIT STATUS.B scrcpywill exit with code 0 on normal program termination. If an initialdiff --git a/app/src/cli.c b/app/src/cli.cindex 4fb7816d..a83409a9 100644--- a/app/src/cli.c+++ b/app/src/cli.c@@ -804,7 +804,8 @@ static const struct sc_option options[] = {"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\".",+ "Default is \"letterbox\", unless --flex-display is set, in "+ "which case it is \"unscaled\".",},{.longopt_id = OPT_REQUIRE_AUDIO,@@ -1029,6 +1030,11 @@ static const struct sc_option options[] = {.text = "Set the initial window height.\n""Default is 0 (automatic).",},+ {+ .shortopt = 'x',+ .longopt = "flex-display",+ .text = "Continuously resize the virtual display to match the window.",+ },};static const struct sc_shortcut shortcuts[] = {@@ -2885,6 +2891,9 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],return false;}break;+ case 'x':+ opts->flex_display = true;+ break;default:// getopt prints the error message on stderrreturn false;@@ -2974,9 +2983,17 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],}#ifdef HAVE_V4L2- if (v4l2 && !opts->video) {- LOGE("V4L2 sink requires video capture, but --no-video was set.");- return false;+ if (v4l2) {+ if (!opts->video) {+ LOGE("V4L2 sink requires video capture, but --no-video was set.");+ return false;+ }++ if (opts->flex_display) {+ LOGE("V4L2 is incompatible with -x/--flex-display because it does "+ "not support resizing");+ return false;+ }}if (opts->v4l2_buffer && !opts->v4l2_device) {@@ -3064,6 +3081,11 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],}}+ if (opts->render_fit == SC_RENDER_FIT_AUTO) {+ opts->render_fit = opts->flex_display ? SC_RENDER_FIT_UNSCALED+ : SC_RENDER_FIT_LETTERBOX;+ }+if (otg) {if (!opts->control) {LOGE("--no-control is not allowed in OTG mode");@@ -3183,6 +3205,35 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],return false;}+ if (opts->flex_display) {+ if (opts->video_source != SC_VIDEO_SOURCE_DISPLAY+ || !opts->new_display) {+ LOGE("-x/--flex-display can only be applied to displays created "+ "with --new-display");+ return false;+ }++ if (!opts->control) {+ LOGE("-n/--no-control is not compatible with -x/--flex-display");+ return false;+ }++ if (opts->crop) {+ LOGE("--crop is not compatible with -x/--flex-display");+ return false;+ }++ if (opts->window_width || opts->window_height) {+ LOGE("--window-width and --window-height are disabled when using "+ "-x/--flex-display; configure the display size with "+ "--new-display=WxH instead");+ return false;+ }++ // Force free resizing+ opts->window_aspect_ratio_lock = false;+ }+if (opts->display_ime_policy != SC_DISPLAY_IME_POLICY_UNDEFINED&& opts->display_id == 0 && !opts->new_display) {LOGE("--display-ime-policy is only supported on a secondary display");diff --git a/app/src/options.c b/app/src/options.cindex b4924bd4..f113d71a 100644--- a/app/src/options.c+++ b/app/src/options.c@@ -59,7 +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,+ .render_fit = SC_RENDER_FIT_AUTO,.window_x = SC_WINDOW_POSITION_UNDEFINED,.window_y = SC_WINDOW_POSITION_UNDEFINED,.window_width = 0,@@ -119,6 +119,7 @@ const struct scrcpy_options scrcpy_options_default = {.vd_system_decorations = true,.camera_torch = false,.keep_active = false,+ .flex_display = false,};enum sc_orientationdiff --git a/app/src/options.h b/app/src/options.hindex 58aacf72..184b5df5 100644--- a/app/src/options.h+++ b/app/src/options.h@@ -221,6 +221,7 @@ enum sc_shortcut_mod {};enum sc_render_fit {+ SC_RENDER_FIT_AUTO,SC_RENDER_FIT_LETTERBOX,SC_RENDER_FIT_UNSCALED,};@@ -338,6 +339,7 @@ struct scrcpy_options {bool vd_system_decorations;bool camera_torch;bool keep_active;+ bool flex_display;};extern const struct scrcpy_options scrcpy_options_default;diff --git a/app/src/scrcpy.c b/app/src/scrcpy.cindex 3ef9bd82..86a5313b 100644--- a/app/src/scrcpy.c+++ b/app/src/scrcpy.c@@ -424,6 +424,7 @@ scrcpy(struct scrcpy_options *options) {.vd_destroy_content = options->vd_destroy_content,.vd_system_decorations = options->vd_system_decorations,.keep_active = options->keep_active,+ .flex_display = options->flex_display,.list = options->list,};@@ -756,6 +757,7 @@ aoa_complete:struct sc_screen_params screen_params = {.video = options->video_playback,.camera = options->video_source == SC_VIDEO_SOURCE_CAMERA,+ .flex_display = options->flex_display,.controller = controller,.fp = fp,.kp = kp,diff --git a/app/src/screen.c b/app/src/screen.cindex cba30628..27f0076a 100644--- a/app/src/screen.c+++ b/app/src/screen.c@@ -279,10 +279,33 @@ end:}static void-sc_screen_on_resize(struct sc_screen *screen) {+sc_screen_request_resize_display(struct sc_screen *screen, uint16_t width,+ uint16_t height) {+ assert(screen->flex_display);+ assert(!screen->camera);+ if (sc_orientation_is_swap(screen->orientation)) {+ uint16_t tmp = width;+ width = height;+ height = tmp;+ }++ LOGV("resize_display(%" PRIu16 ", %" PRIu16 ")", width, height);+ sc_controller_resize_display(screen->controller, width, height);+}++static void+sc_screen_on_resize(struct sc_screen *screen, const SDL_WindowEvent *event) {// This event can be triggered before the window is shownif (screen->window_shown) {sc_screen_render(screen, true);++ if (screen->flex_display) {+ assert(!(event->data1 & ~0xFFFF));+ assert(!(event->data2 & ~0xFFFF));+ uint16_t width = event->data1;+ uint16_t height = event->data2;+ sc_screen_request_resize_display(screen, width, height);+ }}}@@ -304,7 +327,7 @@ event_watcher(void *data, SDL_Event *event) {if (event->type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) {// In practice, it seems to always be called from the same thread in// that specific case. Anyway, it's just a workaround.- sc_screen_on_resize(screen);+ sc_screen_on_resize(screen, &event->window);}return true;@@ -399,6 +422,8 @@ sc_screen_frame_sink_push_session(struct sc_frame_sink *sink,boolsc_screen_init(struct sc_screen *screen,const struct sc_screen_params *params) {+ screen->controller = params->controller;+screen->resize_pending = false;screen->window_shown = false;screen->paused = false;@@ -411,6 +436,7 @@ sc_screen_init(struct sc_screen *screen,screen->camera = params->camera;screen->window_aspect_ratio_lock = params->window_aspect_ratio_lock;screen->render_fit = params->render_fit;+ screen->flex_display = params->flex_display;screen->bg.r = (params->background_color >> 16) & 0xFF;screen->bg.g = (params->background_color >> 8) & 0xFF;@@ -736,13 +762,15 @@ resize_for_content(struct sc_screen *screen, struct sc_size old_content_size,struct sc_size new_content_size) {assert(screen->video);- struct sc_size window_size = sc_sdl_get_window_size(screen->window);- struct sc_size target_size = {- .width = (uint32_t) window_size.width * new_content_size.width- / old_content_size.width,- .height = (uint32_t) window_size.height * new_content_size.height- / old_content_size.height,- };+ struct sc_size target_size = new_content_size;+ if (!screen->flex_display) {+ struct sc_size window_size = sc_sdl_get_window_size(screen->window);+ // Scale proportionally+ target_size.width = (uint32_t) window_size.width * target_size.width+ / old_content_size.width;+ target_size.height = (uint32_t) window_size.height * target_size.height+ / old_content_size.height;+ }target_size = get_optimal_size(target_size, new_content_size, true);assert(is_windowed(screen));set_aspect_ratio(screen, new_content_size);@@ -757,6 +785,10 @@ set_content_size(struct sc_screen *screen, struct sc_size new_content_size,if (resize) {if (is_windowed(screen)) {resize_for_content(screen, screen->content_size, new_content_size);+ } else if (screen->flex_display) {+ // Force a display resize, the client cannot resize in fullscreen+ struct sc_size size = sc_sdl_get_window_size(screen->window);+ sc_screen_request_resize_display(screen, size.width, size.height);} else if (!screen->resize_pending) {// Store the windowed size to be able to compute the optimal size// once fullscreen/maximized/minimized are disabled@@ -1029,7 +1061,7 @@ sc_screen_handle_event(struct sc_screen *screen, const SDL_Event *event) {// If defined, then the actions are already performed by the event watcher#ifndef CONTINUOUS_RESIZING_WORKAROUNDcase SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:- sc_screen_on_resize(screen);+ sc_screen_on_resize(screen, &event->window);return;#endifcase SDL_EVENT_WINDOW_RESTORED:diff --git a/app/src/screen.h b/app/src/screen.hindex 09dfc6d2..9dbc6e99 100644--- a/app/src/screen.h+++ b/app/src/screen.h@@ -38,6 +38,9 @@ struct sc_screen {bool video;bool camera;bool window_aspect_ratio_lock;+ bool flex_display;++ struct sc_controller *controller;struct sc_screen_bg_color {uint8_t r;@@ -103,6 +106,7 @@ struct sc_screen {struct sc_screen_params {bool video;bool camera;+ bool flex_display;struct sc_controller *controller;struct sc_file_pusher *fp;diff --git a/app/src/server.c b/app/src/server.cindex e108ead0..6a635c83 100644--- a/app/src/server.c+++ b/app/src/server.c@@ -413,6 +413,9 @@ execute_server(struct sc_server *server,VALIDATE_STRING(params->new_display);ADD_PARAM("new_display=%s", params->new_display);}+ if (params->flex_display) {+ ADD_PARAM("flex_display=true");+ }if (params->display_ime_policy != SC_DISPLAY_IME_POLICY_UNDEFINED) {ADD_PARAM("display_ime_policy=%s",sc_server_get_display_ime_policy_name(params->display_ime_policy));diff --git a/app/src/server.h b/app/src/server.hindex 252bb2bc..73fcad58 100644--- a/app/src/server.h+++ b/app/src/server.h@@ -73,6 +73,7 @@ struct sc_server_params {bool vd_destroy_content;bool vd_system_decorations;bool keep_active;+ bool flex_display;uint8_t list;};diff --git a/server/src/main/java/com/genymobile/scrcpy/Options.java b/server/src/main/java/com/genymobile/scrcpy/Options.javaindex 450d4cf7..d015d765 100644--- a/server/src/main/java/com/genymobile/scrcpy/Options.java+++ b/server/src/main/java/com/genymobile/scrcpy/Options.java@@ -66,6 +66,7 @@ public class Options {private NewDisplay newDisplay;private boolean vdDestroyContent = true;private boolean vdSystemDecorations = true;+ private boolean flexDisplay;private boolean keepActive;@@ -264,6 +265,10 @@ public class Options {return keepActive;}+ public boolean getFlexDisplay() {+ return flexDisplay;+ }+public boolean getList() {return listEncoders || listDisplays || listCameras || listCameraSizes || listApps;}@@ -512,6 +517,9 @@ public class Options {case "vd_system_decorations":options.vdSystemDecorations = Boolean.parseBoolean(value);break;+ case "flex_display":+ options.flexDisplay = Boolean.parseBoolean(value);+ break;case "capture_orientation":Pair<Orientation.Lock, Orientation> pair = parseCaptureOrientation(value);options.captureOrientationLock = pair.first;diff --git a/server/src/main/java/com/genymobile/scrcpy/control/Controller.java b/server/src/main/java/com/genymobile/scrcpy/control/Controller.javaindex 3aa038bd..2e604b35 100644--- a/server/src/main/java/com/genymobile/scrcpy/control/Controller.java+++ b/server/src/main/java/com/genymobile/scrcpy/control/Controller.java@@ -14,6 +14,7 @@ import com.genymobile.scrcpy.util.Ln;import com.genymobile.scrcpy.util.LogUtils;import com.genymobile.scrcpy.video.CameraCapture;import com.genymobile.scrcpy.video.CaptureControl;+import com.genymobile.scrcpy.video.NewDisplayCapture;import com.genymobile.scrcpy.video.SurfaceCapture;import com.genymobile.scrcpy.video.VideoSource;import com.genymobile.scrcpy.video.VirtualDisplayListener;@@ -407,6 +408,9 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {case ControlMessage.TYPE_START_APP:startAppAsync(msg.getText());return true;+ case ControlMessage.TYPE_RESIZE_DISPLAY:+ resizeDisplay(msg.getWidth(), msg.getHeight());+ return true;default:// fall through}@@ -866,4 +870,9 @@ public class Controller implements AsyncProcessor, VirtualDisplayListener {surfaceCapture.getCaptureControl().reset(CaptureControl.RESET_REASON_CLIENT_RESET);}}++ private void resizeDisplay(int width, int height) {+ NewDisplayCapture newDisplayCapture = (NewDisplayCapture) surfaceCapture;+ newDisplayCapture.requestResize(width, height);+ }}diff --git a/server/src/main/java/com/genymobile/scrcpy/display/DisplayMonitor.java b/server/src/main/java/com/genymobile/scrcpy/display/DisplayMonitor.javaindex b951c69e..f6d871c9 100644--- a/server/src/main/java/com/genymobile/scrcpy/display/DisplayMonitor.java+++ b/server/src/main/java/com/genymobile/scrcpy/display/DisplayMonitor.java@@ -16,7 +16,7 @@ import android.view.IDisplayWindowListener;public class DisplayMonitor {public interface Listener {- void onDisplayPropertiesChanged();+ void onDisplayPropertiesChanged(DisplayProperties props);}// On Android 14, DisplayListener may be broken (it never sends events). This is fixed in recent Android 14 upgrades, but we can't really@@ -123,9 +123,9 @@ public class DisplayMonitor {// We can't compare with the current properties, so reset unconditionallyDisplayProperties oldProps = getAndSetDisplayProperties(null); // exchange with synchronizationif (Ln.isEnabled(Ln.Level.VERBOSE)) {- Ln.v("DisplayMonitor: requestReset(): " + oldProps + " -> (unknown)");+ Ln.v("DisplayMonitor: " + oldProps + " -> (unknown)");}- listener.onDisplayPropertiesChanged();+ listener.onDisplayPropertiesChanged(null);} else {DisplayProperties newProps = new DisplayProperties(di.getSize(), di.getRotation());@@ -133,11 +133,11 @@ public class DisplayMonitor {if (!newProps.equals(oldProps)) {// Reset only if the properties are differentif (Ln.isEnabled(Ln.Level.VERBOSE)) {- Ln.v("DisplayMonitor: requestReset(): " + oldProps + " -> " + newProps);+ Ln.v("DisplayMonitor: " + oldProps + " -> " + newProps);}- listener.onDisplayPropertiesChanged();+ listener.onDisplayPropertiesChanged(newProps);} else if (Ln.isEnabled(Ln.Level.VERBOSE)) {- Ln.v("DisplayMonitor: DisplayProperties not changed (" + newProps + "): do not requestReset()");+ Ln.v("DisplayMonitor: " + newProps + " (unchanged)");}}}diff --git a/server/src/main/java/com/genymobile/scrcpy/display/DisplayPropertiesTracker.java b/server/src/main/java/com/genymobile/scrcpy/display/DisplayPropertiesTracker.javanew file mode 100644index 00000000..adc606aa--- /dev/null+++ b/server/src/main/java/com/genymobile/scrcpy/display/DisplayPropertiesTracker.java@@ -0,0 +1,84 @@+package com.genymobile.scrcpy.display;++import android.os.SystemClock;++import java.util.ArrayList;+import java.util.List;++public class DisplayPropertiesTracker {++ private static final long PENDING_CACHE_DURATION = 3000; // ms++ private static class PendingChange {+ private final DisplayProperties props;+ private final long timestamp;++ PendingChange(DisplayProperties props, long timestamp) {+ this.props = props;+ this.timestamp = timestamp;+ }+ }++ private final List<PendingChange> pending = new ArrayList<>();++ public synchronized void pushClientRequest(DisplayProperties props) {+ long now = SystemClock.uptimeMillis();+ pending.add(new PendingChange(props, now));+ }++ /**+ * Function to be called when the display properties changed.+ *+ * @param props the new display properties+ * @return {@code true} if this change is the result of a client request+ */+ public synchronized boolean onChanged(DisplayProperties props) {+ cleanExpired();+ int index = getMatchingPendingIndex(props);+ if (index == -1) {+ return false;+ }++ pending.subList(0, index + 1).clear();+ return true;+ }++ private int getMatchingPendingIndex(DisplayProperties props) {+ for (int i = 0; i < pending.size(); ++i) {+ if (pending.get(i).props.equals(props)) {+ return i;+ }+ }+ return -1;+ }++ private int getFirstNonExpiredIndex() {+ long now = SystemClock.uptimeMillis();+ for (int i = 0; i < pending.size(); ++i) {+ if (pending.get(i).timestamp + PENDING_CACHE_DURATION >= now) {+ return i;+ }+ }+ return -1;+ }++ private void cleanExpired() {+ if (pending.isEmpty()) {+ return;+ }++ int firstNonExpiredIndex = getFirstNonExpiredIndex();+ if (firstNonExpiredIndex == 0) {+ // All items are fresh+ return;+ }++ if (firstNonExpiredIndex == -1) {+ // All items have expired+ pending.clear();+ } else {+ // Remove all the items up to the first non-expired index+ pending.subList(0, firstNonExpiredIndex).clear();+ }+ }+}diff --git a/server/src/main/java/com/genymobile/scrcpy/display/DisplayResizeDebouncer.java b/server/src/main/java/com/genymobile/scrcpy/display/DisplayResizeDebouncer.javanew file mode 100644index 00000000..0b961c69--- /dev/null+++ b/server/src/main/java/com/genymobile/scrcpy/display/DisplayResizeDebouncer.java@@ -0,0 +1,85 @@+package com.genymobile.scrcpy.display;++import com.genymobile.scrcpy.model.Size;+import com.genymobile.scrcpy.util.Ln;++import android.os.SystemClock;++public final class DisplayResizeDebouncer {++ private static final long DEBOUNCE_DELAY_MS = 300;++ public interface Callback {+ void trigger(Size size);+ }++ private final Callback callback;+ private Size request;+ private long deadline;++ private Thread thread;++ public DisplayResizeDebouncer(Callback callback) {+ this.callback = callback;+ }++ public void start() {+ assert thread == null;+ thread = new Thread(this::debounce);+ thread.setName("debouncer");+ thread.setDaemon(true);+ thread.start();+ }++ public void stop() {+ if (thread != null) {+ thread.interrupt();+ thread = null;+ }+ }++ private void debounce() {+ try {+ while (true) {+ Size newSize;+ synchronized (this) {+ while (true) {+ long now = SystemClock.uptimeMillis();+ if (request != null && now >= deadline) {+ break;+ }+ if (request == null) {+ wait();+ } else {+ assert now < deadline;+ wait(deadline - now);+ }+ }+ assert request != null : "An active deadline implies request != null";+ newSize = request;+ request = null;+ }+ callback.trigger(newSize);+ }+ } catch (InterruptedException e) {+ // ignore+ } finally {+ Ln.d("Debouncer thread stopped");+ }+ }++ public synchronized void requestResize(Size size) {+ assert size != null;+ if (request == null) {+ deadline = SystemClock.uptimeMillis() + DEBOUNCE_DELAY_MS;+ }+ request = size;+ notify();+ }++ public synchronized void cancelResize() {+ request = null;+ deadline = 0;+ notify();+ }+}diff --git a/server/src/main/java/com/genymobile/scrcpy/model/Orientation.java b/server/src/main/java/com/genymobile/scrcpy/model/Orientation.javaindex 62698569..8e189d77 100644--- a/server/src/main/java/com/genymobile/scrcpy/model/Orientation.java+++ b/server/src/main/java/com/genymobile/scrcpy/model/Orientation.java@@ -46,4 +46,9 @@ public enum Orientation {public int getRotation() {return ordinal() & 3;}++ public boolean isSwap() {+ // width and height are swapped on 90-degree rotations+ return (ordinal() & 1) != 0;+ }}diff --git a/server/src/main/java/com/genymobile/scrcpy/video/CaptureControl.java b/server/src/main/java/com/genymobile/scrcpy/video/CaptureControl.javaindex 14ac7f4a..7933ea72 100644--- a/server/src/main/java/com/genymobile/scrcpy/video/CaptureControl.java+++ b/server/src/main/java/com/genymobile/scrcpy/video/CaptureControl.java@@ -7,6 +7,7 @@ public class CaptureControl {public static final int RESET_REASON_TERMINATED = 1;public static final int RESET_REASON_DISPLAY_PROPERTIES_CHANGED = 1 << 1;public static final int RESET_REASON_CLIENT_RESET = 1 << 2;+ public static final int RESET_REASON_CLIENT_RESIZED = 1 << 3;private int reset = 0;diff --git a/server/src/main/java/com/genymobile/scrcpy/video/NewDisplayCapture.java b/server/src/main/java/com/genymobile/scrcpy/video/NewDisplayCapture.javaindex 1f30fb27..e666a39c 100644--- a/server/src/main/java/com/genymobile/scrcpy/video/NewDisplayCapture.java+++ b/server/src/main/java/com/genymobile/scrcpy/video/NewDisplayCapture.java@@ -6,6 +6,8 @@ import com.genymobile.scrcpy.control.PositionMapper;import com.genymobile.scrcpy.display.DisplayInfo;import com.genymobile.scrcpy.display.DisplayMonitor;import com.genymobile.scrcpy.display.DisplayProperties;+import com.genymobile.scrcpy.display.DisplayPropertiesTracker;+import com.genymobile.scrcpy.display.DisplayResizeDebouncer;import com.genymobile.scrcpy.model.NewDisplay;import com.genymobile.scrcpy.model.Orientation;import com.genymobile.scrcpy.model.Size;@@ -58,6 +60,7 @@ public class NewDisplayCapture extends SurfaceCapture {private final float angle;private final boolean vdDestroyContent;private final boolean vdSystemDecorations;+ private final boolean flexDisplay;private VideoConstraints videoConstraints;@@ -66,6 +69,9 @@ public class NewDisplayCapture extends SurfaceCapture {private Size displaySize; // the logical size of the display (including rotation)private Size physicalSize; // the physical size of the display (without rotation)+ private DisplayPropertiesTracker tracker;+ private DisplayResizeDebouncer debouncer;+private int dpi;public NewDisplayCapture(VirtualDisplayListener vdListener, Options options) {@@ -81,15 +87,32 @@ public class NewDisplayCapture extends SurfaceCapture {this.angle = options.getAngle();this.vdDestroyContent = options.getVDDestroyContent();this.vdSystemDecorations = options.getVDSystemDecorations();+ this.flexDisplay = options.getFlexDisplay();}@Overrideprotected void init(VideoConstraints videoConstraints) {- this.videoConstraints = videoConstraints;+ setVideoConstraints(videoConstraints); // synchronizeddisplaySize = newDisplay.getSize();dpi = newDisplay.getDpi();- if (displaySize == null || dpi == 0) {+ if (flexDisplay) {+ if (crop != null) {+ throw new IllegalArgumentException("Flex display does not support cropping");+ }++ tracker = new DisplayPropertiesTracker();+ debouncer = new DisplayResizeDebouncer(this::triggerResize);+ debouncer.start();++ // Hardcode default values if not defined+ if (displaySize == null) {+ displaySize = new Size(1280, 960);+ }+ if (dpi == 0) {+ dpi = 160;+ }+ } else if (displaySize == null || dpi == 0) {DisplayInfo displayInfo = ServiceManager.getDisplayManager().getDisplayInfo(0);if (displayInfo != null) {mainDisplaySize = displayInfo.getSize();@@ -109,14 +132,21 @@ public class NewDisplayCapture extends SurfaceCapture {public void prepare() {int displayRotation;if (virtualDisplay == null) {- if (!newDisplay.hasExplicitSize()) {- displaySize = mainDisplaySize;- }+ if (flexDisplay) {+ assert displaySize != null;+ displaySize = displaySize.constrain(videoConstraints);+ } else {+ if (displaySize == null) {+ assert !flexDisplay;+ displaySize = mainDisplaySize;+ }- // Align the physical display size to avoid unnecessary mismatches with the output size- displaySize = displaySize.align(videoConstraints.getAlignment());+ // Align the physical display size to avoid unnecessary mismatches with the output size+ displaySize = displaySize.align(videoConstraints.getAlignment());+ }- if (!newDisplay.hasExplicitDpi()) {+ if (dpi == 0) {+ assert !flexDisplay;dpi = scaleDpi(mainDisplaySize, mainDisplayDpi, displaySize);}@@ -127,9 +157,13 @@ public class NewDisplayCapture extends SurfaceCapture {DisplayInfo displayInfo = ServiceManager.getDisplayManager().getDisplayInfo(virtualDisplay.getDisplay().getDisplayId());dpi = displayInfo.getDpi();displayRotation = displayInfo.getRotation();-- // Align the physical display size to avoid unnecessary mismatches with the output size- displaySize = displayInfo.getSize().align(videoConstraints.getAlignment());+ displaySize = displayInfo.getSize();+ if (flexDisplay) {+ displaySize = displaySize.constrain(videoConstraints);+ } else {+ // Align the physical display size to avoid unnecessary mismatches with the output size+ displaySize = displaySize.align(videoConstraints.getAlignment());+ }}VideoFilter filter = new VideoFilter(displaySize);@@ -142,10 +176,12 @@ public class NewDisplayCapture extends SurfaceCapture {filter.addOrientation(displayRotation, captureOrientationLocked, captureOrientation);filter.addAngle(angle);- Size outputSize = filter.getOutputSize();- Size filteredSize = outputSize.constrain(videoConstraints);- if (!filteredSize.equals(outputSize)) {- filter.addResize(filteredSize);+ if (!flexDisplay) {+ Size outputSize = filter.getOutputSize();+ Size filteredSize = outputSize.constrain(videoConstraints);+ if (!filteredSize.equals(outputSize)) {+ filter.addResize(filteredSize);+ }}eventTransform = filter.getInverseTransform();@@ -173,7 +209,6 @@ public class NewDisplayCapture extends SurfaceCapture {}public void startNew(Surface surface) {- int virtualDisplayId;try {int flags = VIRTUAL_DISPLAY_FLAG_PUBLIC| VIRTUAL_DISPLAY_FLAG_PRESENTATION@@ -196,16 +231,32 @@ public class NewDisplayCapture extends SurfaceCapture {| VIRTUAL_DISPLAY_FLAG_DEVICE_DISPLAY_GROUP;}}- virtualDisplay = ServiceManager.getDisplayManager()+ VirtualDisplay vd = ServiceManager.getDisplayManager().createNewVirtualDisplay("scrcpy", displaySize.getWidth(), displaySize.getHeight(), dpi, surface, flags);- virtualDisplayId = virtualDisplay.getDisplay().getDisplayId();+ setCurrentVirtualDisplay(vd); // used for client resize+ int virtualDisplayId = vd.getDisplay().getDisplayId();Ln.i("New display: " + displaySize.getWidth() + "x" + displaySize.getHeight() + "/" + dpi + " (id=" + virtualDisplayId + ")");if (displayImePolicy != -1) {ServiceManager.getWindowManager().setDisplayImePolicy(virtualDisplayId, displayImePolicy);}- displayMonitor.start(virtualDisplayId, () -> getCaptureControl().reset(CaptureControl.RESET_REASON_DISPLAY_PROPERTIES_CHANGED));+ displayMonitor.start(virtualDisplayId, (props) -> {+ int reason;+ if (flexDisplay) {+ boolean isClientResize = tracker.onChanged(props);+ if (isClientResize) {+ reason = CaptureControl.RESET_REASON_CLIENT_RESIZED;+ } else {+ reason = CaptureControl.RESET_REASON_DISPLAY_PROPERTIES_CHANGED;+ // Display properties have changed, cancel pending client resize requests+ debouncer.cancelResize();+ }+ } else {+ reason = CaptureControl.RESET_REASON_DISPLAY_PROPERTIES_CHANGED;+ }+ getCaptureControl().reset(reason);+ });} catch (Exception e) {Ln.e("Could not create display", e);throw new AssertionError("Could not create display");@@ -245,9 +296,16 @@ public class NewDisplayCapture extends SurfaceCapture {public void release() {displayMonitor.stopAndRelease();+ if (debouncer != null) {+ debouncer.stop();+ }+if (virtualDisplay != null) {- virtualDisplay.release();- virtualDisplay = null;+ // synchronized with triggerResize()+ synchronized (this) {+ virtualDisplay.release();+ setCurrentVirtualDisplay(null);+ }}}@@ -261,4 +319,44 @@ public class NewDisplayCapture extends SurfaceCapture {int num = size.getMax();return initialDpi * num / den;}++ public void requestResize(int width, int height) {+ if (!flexDisplay) {+ throw new IllegalStateException("Cannot resize a non-flex display");+ }++ VideoConstraints constraints = getVideoConstraints(); // synchronized+ Size newSize = new Size(width, height).constrain(constraints);+ if (Ln.isEnabled(Ln.Level.VERBOSE)) {+ Ln.v(getClass().getSimpleName() + ": requestResize(" + width + ", " + height + ")");+ Ln.v(getClass().getSimpleName() + ": constrained size = " + newSize);+ }++ debouncer.requestResize(newSize);+ }++ private synchronized void setCurrentVirtualDisplay(VirtualDisplay virtualDisplay) {+ this.virtualDisplay = virtualDisplay;+ }++ private synchronized VideoConstraints getVideoConstraints() {+ return videoConstraints;+ }++ private synchronized void setVideoConstraints(VideoConstraints videoConstraints) {+ this.videoConstraints = videoConstraints;+ }++ private synchronized void triggerResize(Size size) {+ if (virtualDisplay != null) {+ int displayId = virtualDisplay.getDisplay().getDisplayId();+ DisplayInfo displayInfo = ServiceManager.getDisplayManager().getDisplayInfo(displayId);+ int displayRotation = displayInfo.getRotation();+ if (captureOrientation.isSwap() ^ (displayRotation % 2) != 0) {+ size = size.rotate();+ }+ tracker.pushClientRequest(new DisplayProperties(size, displayRotation));+ virtualDisplay.resize(size.getWidth(), size.getHeight(), dpi);+ }+ }}diff --git a/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java b/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.javaindex 95b9003f..68bddab3 100644--- a/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java+++ b/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java@@ -64,7 +64,7 @@ public class ScreenCapture extends SurfaceCapture {@Overridepublic void init(VideoConstraints videoConstraints) {this.videoConstraints = videoConstraints;- displayMonitor.start(displayId, () -> getCaptureControl().reset(CaptureControl.RESET_REASON_DISPLAY_PROPERTIES_CHANGED));+ displayMonitor.start(displayId, (props) -> getCaptureControl().reset(CaptureControl.RESET_REASON_DISPLAY_PROPERTIES_CHANGED));}@Overridediff --git a/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java b/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.javaindex 99449722..be022fc9 100644--- a/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java+++ b/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java@@ -127,8 +127,12 @@ public class SurfaceEncoder implements AsyncProcessor {alive = false;} else {if (!captureControl.isResetRequested()) {+ // The reset is due to a resize initiated by the client+ boolean isClientResize = (resetReasons & CaptureControl.RESET_REASON_CLIENT_RESIZED) != 0+ && (resetReasons & CaptureControl.RESET_REASON_DISPLAY_PROPERTIES_CHANGED) == 0;+ streamer.writeSessionMeta(size.getWidth(), size.getHeight(), isClientResize);+// If a reset is requested during encode(), it will interrupt the encoding by an EOS- streamer.writeSessionMeta(size.getWidth(), size.getHeight(), false);encode(mediaCodec, streamer);}