Skip to content

Display and control your Android device

Refactor virtual display properties initialization

Romain Vimontf90dc216d10a
diff --git a/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java b/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java
index 5d026a73..5f4e1803 100644
--- a/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java
+++ b/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java
@@ -124,23 +124,9 @@ public class ScreenCapture extends SurfaceCapture {
inputSize = videoSize;
}
- int virtualDisplayId;
- PositionMapper positionMapper;
try {
virtualDisplay = ServiceManager.getDisplayManager()
.createVirtualDisplay("scrcpy", inputSize.getWidth(), inputSize.getHeight(), displayId, surface);
-
-
- if (displayId == 0) {
- // Main display: send all events to the original display, relative to the device size
- Size deviceSize = displayInfo.getSize();
- positionMapper = PositionMapper.create(videoSize, transform, deviceSize);
- virtualDisplayId = 0;
- } else {
- // The positions are relative to the virtual display, not the original display (so use inputSize, not deviceSize!)
- positionMapper = PositionMapper.create(videoSize, transform, inputSize);
- virtualDisplayId = virtualDisplay.getDisplay().getDisplayId();
- }
Ln.d("Display: using DisplayManager API");
} catch (Exception displayManagerException) {
try {
@@ -148,11 +134,7 @@ public class ScreenCapture extends SurfaceCapture {
Size deviceSize = displayInfo.getSize();
int layerStack = displayInfo.getLayerStack();
-
setDisplaySurface(display, surface, deviceSize.toRect(), inputSize.toRect(), layerStack);
- virtualDisplayId = displayId;
-
- positionMapper = PositionMapper.create(videoSize, transform, deviceSize);
Ln.d("Display: using SurfaceControl API");
} catch (Exception surfaceControlException) {
Ln.e("Could not create display using DisplayManager", displayManagerException);
@@ -162,6 +144,18 @@ public class ScreenCapture extends SurfaceCapture {
}
if (vdListener != null) {
+ int virtualDisplayId;
+ PositionMapper positionMapper;
+ if (virtualDisplay == null || displayId == 0) {
+ // Surface control or main display: send all events to the original display, relative to the device size
+ Size deviceSize = displayInfo.getSize();
+ positionMapper = PositionMapper.create(videoSize, transform, deviceSize);
+ virtualDisplayId = displayId;
+ } else {
+ // The positions are relative to the virtual display, not the original display (so use inputSize, not deviceSize!)
+ positionMapper = PositionMapper.create(videoSize, transform, inputSize);
+ virtualDisplayId = virtualDisplay.getDisplay().getDisplayId();
+ }
vdListener.onNewVirtualDisplay(virtualDisplayId, positionMapper);
}
}