Extract DisplayCapture

This will allow to share common code between ScreenCapture and
NewDisplayCapture.
This commit is contained in:
Romain Vimont
2024-11-10 11:40:11 +01:00
parent 26d66a7c28
commit bf2c8cac9f
2 changed files with 55 additions and 42 deletions

View File

@@ -0,0 +1,53 @@
package com.genymobile.scrcpy.video;
import com.genymobile.scrcpy.device.DisplayInfo;
import com.genymobile.scrcpy.device.Size;
import com.genymobile.scrcpy.util.Ln;
import com.genymobile.scrcpy.wrappers.ServiceManager;
public abstract class DisplayCapture extends SurfaceCapture {
// Source display size (before resizing/crop) for the current session
private Size sessionDisplaySize;
private synchronized Size getSessionDisplaySize() {
return sessionDisplaySize;
}
protected synchronized void setSessionDisplaySize(Size sessionDisplaySize) {
this.sessionDisplaySize = sessionDisplaySize;
}
protected void handleDisplayChanged(int displayId) {
DisplayInfo di = ServiceManager.getDisplayManager().getDisplayInfo(displayId);
if (di == null) {
Ln.w("DisplayInfo for " + displayId + " cannot be retrieved");
// We can't compare with the current size, so reset unconditionally
if (Ln.isEnabled(Ln.Level.VERBOSE)) {
Ln.v(getClass().getCanonicalName() + ": requestReset(): " + getSessionDisplaySize() + " -> (unknown)");
}
setSessionDisplaySize(null);
invalidate();
} else {
Size size = di.getSize();
// The field is hidden on purpose, to read it with synchronization
@SuppressWarnings("checkstyle:HiddenField")
Size sessionDisplaySize = getSessionDisplaySize(); // synchronized
// .equals() also works if sessionDisplaySize == null
if (!size.equals(sessionDisplaySize)) {
// Reset only if the size is different
if (Ln.isEnabled(Ln.Level.VERBOSE)) {
Ln.v(getClass().getCanonicalName() + ": requestReset(): " + sessionDisplaySize + " -> " + size);
}
// Set the new size immediately, so that a future onDisplayChanged() event called before the asynchronous prepare()
// considers that the current size is the requested size (to avoid a duplicate requestReset())
setSessionDisplaySize(size);
invalidate();
} else if (Ln.isEnabled(Ln.Level.VERBOSE)) {
Ln.v(getClass().getCanonicalName() + ": Size not changed (" + size + "): do not requestReset()");
}
}
}
}

View File

@@ -29,7 +29,7 @@ import android.view.Surface;
import java.io.IOException;
public class ScreenCapture extends SurfaceCapture {
public class ScreenCapture extends DisplayCapture {
private final VirtualDisplayListener vdListener;
private final int displayId;
@@ -40,9 +40,6 @@ public class ScreenCapture extends SurfaceCapture {
private DisplayInfo displayInfo;
private Size videoSize;
// Source display size (before resizing/crop) for the current session
private Size sessionDisplaySize;
private IBinder display;
private VirtualDisplay virtualDisplay;
@@ -89,36 +86,7 @@ public class ScreenCapture extends SurfaceCapture {
}
}
if (this.displayId == displayId) {
DisplayInfo di = ServiceManager.getDisplayManager().getDisplayInfo(displayId);
if (di == null) {
Ln.w("DisplayInfo for " + displayId + " cannot be retrieved");
// We can't compare with the current size, so reset unconditionally
if (Ln.isEnabled(Ln.Level.VERBOSE)) {
Ln.v("ScreenCapture: requestReset(): " + getSessionDisplaySize() + " -> (unknown)");
}
setSessionDisplaySize(null);
invalidate();
} else {
Size size = di.getSize();
// The field is hidden on purpose, to read it with synchronization
@SuppressWarnings("checkstyle:HiddenField")
Size sessionDisplaySize = getSessionDisplaySize(); // synchronized
// .equals() also works if sessionDisplaySize == null
if (!size.equals(sessionDisplaySize)) {
// Reset only if the size is different
if (Ln.isEnabled(Ln.Level.VERBOSE)) {
Ln.v("ScreenCapture: requestReset(): " + sessionDisplaySize + " -> " + size);
}
// Set the new size immediately, so that a future onDisplayChanged() event called before the asynchronous prepare()
// considers that the current size is the requested size (to avoid a duplicate requestReset())
setSessionDisplaySize(size);
invalidate();
} else if (Ln.isEnabled(Ln.Level.VERBOSE)) {
Ln.v("ScreenCapture: Size not changed (" + size + "): do not requestReset()");
}
}
handleDisplayChanged(displayId);
}
}, handler);
}
@@ -279,14 +247,6 @@ public class ScreenCapture extends SurfaceCapture {
}
}
private synchronized Size getSessionDisplaySize() {
return sessionDisplaySize;
}
private synchronized void setSessionDisplaySize(Size sessionDisplaySize) {
this.sessionDisplaySize = sessionDisplaySize;
}
private void registerDisplayListenerFallbacks() {
rotationWatcher = new IRotationWatcher.Stub() {
@Override