Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 19 additions & 72 deletions server/src/main/java/com/genymobile/scrcpy/video/ScreenCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@
import com.genymobile.scrcpy.device.DisplayInfo;
import com.genymobile.scrcpy.device.Orientation;
import com.genymobile.scrcpy.device.Size;
import com.genymobile.scrcpy.opengl.AffineOpenGLFilter;
import com.genymobile.scrcpy.opengl.OpenGLFilter;
import com.genymobile.scrcpy.opengl.OpenGLRunner;
import com.genymobile.scrcpy.util.AffineMatrix;
import com.genymobile.scrcpy.util.Ln;
import com.genymobile.scrcpy.util.LogUtils;
import com.genymobile.scrcpy.wrappers.ServiceManager;
import com.genymobile.scrcpy.wrappers.SurfaceControl;

import android.graphics.Rect;
import android.hardware.display.VirtualDisplay;
import android.os.Build;
import android.os.IBinder;
import android.view.Surface;
Expand All @@ -41,10 +37,8 @@ public class ScreenCapture extends SurfaceCapture {
private final DisplaySizeMonitor displaySizeMonitor = new DisplaySizeMonitor();

private IBinder display;
private VirtualDisplay virtualDisplay;

private AffineMatrix transform;
private OpenGLRunner glRunner;

public ScreenCapture(VirtualDisplayListener vdListener, Options options) {
this.vdListener = vdListener;
Expand Down Expand Up @@ -85,19 +79,9 @@ public void prepare() throws ConfigurationException {
captureOrientation = Orientation.fromRotation(displayInfo.getRotation());
}

VideoFilter filter = new VideoFilter(displaySize);

if (crop != null) {
boolean transposed = (displayInfo.getRotation() % 2) != 0;
filter.addCrop(crop, transposed);
}

boolean locked = captureOrientationLock != Orientation.Lock.Unlocked;
filter.addOrientation(displayInfo.getRotation(), locked, captureOrientation);
filter.addAngle(angle);

transform = filter.getInverseTransform();
videoSize = filter.getOutputSize().limit(maxSize).round8();
int halfWidth = displaySize.getWidth() / 2;
videoSize = new Size(halfWidth, displaySize.getHeight()).limit(maxSize).round8();
transform = null;
}

@Override
Expand All @@ -106,66 +90,33 @@ public void start(Surface surface) throws IOException {
SurfaceControl.destroyDisplay(display);
display = null;
}
if (virtualDisplay != null) {
virtualDisplay.release();
virtualDisplay = null;
}

Size inputSize;
if (transform != null) {
// If there is a filter, it must receive the full display content
inputSize = displayInfo.getSize();
assert glRunner == null;
OpenGLFilter glFilter = new AffineOpenGLFilter(transform);
glRunner = new OpenGLRunner(glFilter);
surface = glRunner.start(inputSize, videoSize, surface);
} else {
// If there is no filter, the display must be rendered at target video size directly
inputSize = videoSize;
}
Size inputSize = videoSize;

try {
virtualDisplay = ServiceManager.getDisplayManager()
.createVirtualDisplay("scrcpy", inputSize.getWidth(), inputSize.getHeight(), displayId, surface);
Ln.d("Display: using DisplayManager API");
} catch (Exception displayManagerException) {
try {
display = createDisplay();

Size deviceSize = displayInfo.getSize();
int layerStack = displayInfo.getLayerStack();
setDisplaySurface(display, surface, deviceSize.toRect(), inputSize.toRect(), layerStack);
Ln.d("Display: using SurfaceControl API");
} catch (Exception surfaceControlException) {
Ln.e("Could not create display using DisplayManager", displayManagerException);
Ln.e("Could not create display using SurfaceControl", surfaceControlException);
throw new AssertionError("Could not create display");
}
display = createDisplay();

Size deviceSize = displayInfo.getSize();
Rect deviceRect = new Rect(0, 0, deviceSize.getWidth() / 2, deviceSize.getHeight());
int layerStack = displayInfo.getLayerStack();
setDisplaySurface(display, surface, deviceRect, inputSize.toRect(), layerStack);
Ln.d("Display: using SurfaceControl API");
} catch (Exception e) {
Ln.e("Could not create display", e);
throw new AssertionError("Could not create display");
}

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);
Size deviceSize = displayInfo.getSize();
Size cropSize = new Size(deviceSize.getWidth() / 2, deviceSize.getHeight());
PositionMapper positionMapper = PositionMapper.create(videoSize, transform, cropSize);
vdListener.onNewVirtualDisplay(displayId, positionMapper);
}
}

@Override
public void stop() {
if (glRunner != null) {
glRunner.stopAndRelease();
glRunner = null;
}
// no-op
}

@Override
Expand All @@ -176,10 +127,6 @@ public void release() {
SurfaceControl.destroyDisplay(display);
display = null;
}
if (virtualDisplay != null) {
virtualDisplay.release();
virtualDisplay = null;
}
}

@Override
Expand Down
Loading