diff --git a/server/src/main/java/com/genymobile/scrcpy/video/DisplayCapture.java b/server/src/main/java/com/genymobile/scrcpy/video/DisplayCapture.java new file mode 100644 index 00000000..825c87ec --- /dev/null +++ b/server/src/main/java/com/genymobile/scrcpy/video/DisplayCapture.java @@ -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()"); + } + } + } +} 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 bc0f825a..d8c1d3cc 100644 --- a/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java +++ b/server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java @@ -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