Skip to content

Commit

Permalink
WIP: requestinvalidate
Browse files Browse the repository at this point in the history
Send the request to the surface capture instead of the surface encoder,
which may ignore the request if it does not support it.

Also make NewVirtualDisplay support a "restart" without creating a new
display. (WIP)
  • Loading branch information
rom1v committed Nov 3, 2024
1 parent 3b312d8 commit 1bbd96f
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 42 deletions.
2 changes: 1 addition & 1 deletion server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ private static void scrcpy(Options options) throws IOException, ConfigurationExc
asyncProcessors.add(surfaceEncoder);

if (controller != null) {
controller.setSurfaceEncoder(surfaceEncoder);
controller.setSurfaceCapture(surfaceCapture);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.genymobile.scrcpy.device.Position;
import com.genymobile.scrcpy.util.Ln;
import com.genymobile.scrcpy.util.LogUtils;
import com.genymobile.scrcpy.video.SurfaceEncoder;
import com.genymobile.scrcpy.video.SurfaceCapture;
import com.genymobile.scrcpy.video.VirtualDisplayListener;
import com.genymobile.scrcpy.wrappers.ClipboardManager;
import com.genymobile.scrcpy.wrappers.InputManager;
Expand Down Expand Up @@ -94,8 +94,8 @@ private DisplayData(int virtualDisplayId, PositionMapper positionMapper) {

private boolean keepDisplayPowerOff;

// Used for resetting video capture/encoding on RESET_VIDEO message
private SurfaceEncoder surfaceEncoder;
// Used for resetting video encoding on RESET_VIDEO message
private SurfaceCapture surfaceCapture;

public Controller(int displayId, ControlChannel controlChannel, CleanUp cleanUp, boolean clipboardAutosync, boolean powerOn) {
this.displayId = displayId;
Expand Down Expand Up @@ -147,8 +147,8 @@ public void onNewVirtualDisplay(int virtualDisplayId, PositionMapper positionMap
}
}

public void setSurfaceEncoder(SurfaceEncoder surfaceEncoder) {
this.surfaceEncoder = surfaceEncoder;
public void setSurfaceCapture(SurfaceCapture surfaceCapture) {
this.surfaceCapture = surfaceCapture;
}

private UhidManager getUhidManager() {
Expand Down Expand Up @@ -693,9 +693,9 @@ private void setDisplayPower(boolean on) {
}

private void resetVideo() {
if (surfaceEncoder != null) {
if (surfaceCapture != null) {
Ln.i("Video capture reset");
surfaceEncoder.requestReset();
surfaceCapture.requestInvalidate();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,9 @@ public void onCaptureFailed(CameraCaptureSession session, CaptureRequest request
public boolean isClosed() {
return disconnected.get();
}

@Override
public void requestInvalidate() {
// do nothing (the user could not request a reset anyway for now, since there is no controller for camera mirroring)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,43 +74,36 @@ public void prepare() {

@Override
public void start(Surface surface) {
if (virtualDisplay != null) {
virtualDisplay.release();
virtualDisplay = null;
}

int virtualDisplayId;
try {
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC
| DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY
| VIRTUAL_DISPLAY_FLAG_SUPPORTS_TOUCH
| VIRTUAL_DISPLAY_FLAG_ROTATES_WITH_CONTENT
| VIRTUAL_DISPLAY_FLAG_DESTROY_CONTENT_ON_REMOVAL
| VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
if (Build.VERSION.SDK_INT >= AndroidVersions.API_33_ANDROID_13) {
flags |= VIRTUAL_DISPLAY_FLAG_TRUSTED
| VIRTUAL_DISPLAY_FLAG_OWN_DISPLAY_GROUP
| VIRTUAL_DISPLAY_FLAG_ALWAYS_UNLOCKED
| VIRTUAL_DISPLAY_FLAG_TOUCH_FEEDBACK_DISABLED;
if (Build.VERSION.SDK_INT >= AndroidVersions.API_34_ANDROID_14) {
flags |= VIRTUAL_DISPLAY_FLAG_OWN_FOCUS
| VIRTUAL_DISPLAY_FLAG_DEVICE_DISPLAY_GROUP;
if (virtualDisplay == null) {
int virtualDisplayId;
try {
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC | DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY
| VIRTUAL_DISPLAY_FLAG_SUPPORTS_TOUCH | VIRTUAL_DISPLAY_FLAG_ROTATES_WITH_CONTENT
| VIRTUAL_DISPLAY_FLAG_DESTROY_CONTENT_ON_REMOVAL | VIRTUAL_DISPLAY_FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
if (Build.VERSION.SDK_INT >= AndroidVersions.API_33_ANDROID_13) {
flags |= VIRTUAL_DISPLAY_FLAG_TRUSTED | VIRTUAL_DISPLAY_FLAG_OWN_DISPLAY_GROUP | VIRTUAL_DISPLAY_FLAG_ALWAYS_UNLOCKED
| VIRTUAL_DISPLAY_FLAG_TOUCH_FEEDBACK_DISABLED;
if (Build.VERSION.SDK_INT >= AndroidVersions.API_34_ANDROID_14) {
flags |= VIRTUAL_DISPLAY_FLAG_OWN_FOCUS | VIRTUAL_DISPLAY_FLAG_DEVICE_DISPLAY_GROUP;
}
}
virtualDisplay = ServiceManager.getDisplayManager()
.createNewVirtualDisplay("scrcpy", size.getWidth(), size.getHeight(), dpi, surface, flags);
virtualDisplayId = virtualDisplay.getDisplay().getDisplayId();
Ln.i("New display: " + size.getWidth() + "x" + size.getHeight() + "/" + dpi + " (id=" + virtualDisplayId + ")");
} catch (Exception e) {
Ln.e("Could not create display", e);
throw new AssertionError("Could not create display");
}
virtualDisplay = ServiceManager.getDisplayManager()
.createNewVirtualDisplay("scrcpy", size.getWidth(), size.getHeight(), dpi, surface, flags);
virtualDisplayId = virtualDisplay.getDisplay().getDisplayId();
Ln.i("New display: " + size.getWidth() + "x" + size.getHeight() + "/" + dpi + " (id=" + virtualDisplayId + ")");
} catch (Exception e) {
Ln.e("Could not create display", e);
throw new AssertionError("Could not create display");
}

if (vdListener != null) {
virtualDisplayId = virtualDisplay.getDisplay().getDisplayId();
Rect contentRect = new Rect(0, 0, size.getWidth(), size.getHeight());
PositionMapper positionMapper = new PositionMapper(size, contentRect, 0);
vdListener.onNewVirtualDisplay(virtualDisplayId, positionMapper);
if (vdListener != null) {
virtualDisplayId = virtualDisplay.getDisplay().getDisplayId();
Rect contentRect = new Rect(0, 0, size.getWidth(), size.getHeight());
PositionMapper positionMapper = new PositionMapper(size, contentRect, 0);
vdListener.onNewVirtualDisplay(virtualDisplayId, positionMapper);
}
} else {
virtualDisplay.setSurface(surface);
}
}

Expand Down Expand Up @@ -143,4 +136,9 @@ private static int scaleDpi(Size initialSize, int initialDpi, Size size) {
int num = size.getMax();
return initialDpi * num / den;
}

@Override
public void requestInvalidate() {
invalidate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,9 @@ private void unregisterDisplayListenerFallbacks() {
}
}
}

@Override
public void requestInvalidate() {
invalidate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,11 @@ public void prepare() throws ConfigurationException {
public boolean isClosed() {
return false;
}

/**
* Manually request to invalidate (typically a user request).
* <p>
* The capture implementation is free to ignore the request and do nothing.
*/
public abstract void requestInvalidate();
}

0 comments on commit 1bbd96f

Please sign in to comment.