Skip to content

Commit

Permalink
Fix incorrect capture when using DisplayManager API
Browse files Browse the repository at this point in the history
  • Loading branch information
eiyooooo committed Apr 12, 2024
1 parent 7011dd1 commit dcea343
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions server/src/main/java/com/genymobile/scrcpy/ScreenCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ public void start(Surface surface) {
SurfaceControl.destroyDisplay(display);
display = null;
}
if (virtualDisplay != null) {
virtualDisplay.release();
virtualDisplay = null;
}

try {
display = createDisplay();
Expand All @@ -51,8 +47,20 @@ public void start(Surface surface) {
} catch (Exception surfaceControlException) {
Rect videoRect = screenInfo.getVideoSize().toRect();
try {
virtualDisplay = ServiceManager.getDisplayManager()
.createVirtualDisplay("scrcpy", videoRect.width(), videoRect.height(), device.getDisplayId(), surface);
if (virtualDisplay == null) {
// Do not release virtual display, to avoid displayId increase
virtualDisplay = ServiceManager.getDisplayManager()
.createVirtualDisplay("scrcpy", videoRect.width(), videoRect.height(), device.getDisplayId(), surface);
} else {
// Density doesn't matter since this virtual display is only used for mirroring.
virtualDisplay.resize(videoRect.width(), videoRect.height(), 1);
virtualDisplay.setSurface(surface);
}
// 'createVirtualDisplay' will copy the configuration of the original display (including the rotation),
// but 'videoRect' is already rotated according to the device rotation,
// so we need to freeze the rotation to 0 to avoid a double rotation
int displayId = virtualDisplay.getDisplay().getDisplayId();
ServiceManager.getWindowManager().freezeRotation(displayId, 0);
Ln.d("Display: using DisplayManager API");
} catch (Exception displayManagerException) {
Ln.e("Could not create display using SurfaceControl", surfaceControlException);
Expand Down

0 comments on commit dcea343

Please sign in to comment.