Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
2dc6a2e30c | ||
|
80233b7b72 | ||
|
60c882841f | ||
|
348c478359 | ||
|
c90247ffb6 | ||
|
f31010f4ee |
@ -3,6 +3,7 @@ package com.genymobile.scrcpy;
|
||||
import com.genymobile.scrcpy.wrappers.ServiceManager;
|
||||
|
||||
import android.graphics.Point;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Build;
|
||||
import android.os.RemoteException;
|
||||
import android.view.IRotationWatcher;
|
||||
@ -18,14 +19,32 @@ public final class Device {
|
||||
|
||||
private ScreenInfo screenInfo;
|
||||
private RotationListener rotationListener;
|
||||
private boolean rotated;
|
||||
|
||||
public Device(Options options) {
|
||||
screenInfo = computeScreenInfo(options.getMaxSize());
|
||||
options.setCrop(new Rect(0, 0, 1280, 1440));
|
||||
|
||||
// TODO crop the crop to the device size!!!
|
||||
|
||||
final int maxSize = options.getMaxSize();
|
||||
final Rect crop = options.getCrop();
|
||||
|
||||
DisplayInfo displayInfo = serviceManager.getDisplayManager().getDisplayInfo();
|
||||
rotated = (displayInfo.getRotation() & 1) != 0;
|
||||
|
||||
screenInfo = ScreenInfo.create(displayInfo.getSize(), maxSize, crop, rotated);
|
||||
registerRotationWatcher(new IRotationWatcher.Stub() {
|
||||
@Override
|
||||
public void onRotationChanged(int rotation) throws RemoteException {
|
||||
public void onRotationChanged(int rotation) {
|
||||
boolean rotated = (rotation & 1) != 0;
|
||||
synchronized (Device.this) {
|
||||
screenInfo = screenInfo.withRotation(rotation);
|
||||
// Do not call getDisplayInfo(), the resulting rotation may be inconsistent with
|
||||
// the rotation parameter (race condition).
|
||||
// Instead, compute the new size from the (rotated) old size.
|
||||
Size oldSize = screenInfo.getDeviceSize();
|
||||
Size newSize = rotated != Device.this.rotated ? oldSize.rotate() : oldSize;
|
||||
screenInfo = ScreenInfo.create(newSize, maxSize, crop, rotated);
|
||||
Device.this.rotated = rotated;
|
||||
|
||||
// notify
|
||||
if (rotationListener != null) {
|
||||
@ -40,40 +59,9 @@ public final class Device {
|
||||
return screenInfo;
|
||||
}
|
||||
|
||||
@SuppressWarnings("checkstyle:MagicNumber")
|
||||
private ScreenInfo computeScreenInfo(int maxSize) {
|
||||
// Compute the video size and the padding of the content inside this video.
|
||||
// Principle:
|
||||
// - scale down the great side of the screen to maxSize (if necessary);
|
||||
// - scale down the other side so that the aspect ratio is preserved;
|
||||
// - round this value to the nearest multiple of 8 (H.264 only accepts multiples of 8)
|
||||
DisplayInfo displayInfo = serviceManager.getDisplayManager().getDisplayInfo();
|
||||
boolean rotated = (displayInfo.getRotation() & 1) != 0;
|
||||
Size deviceSize = displayInfo.getSize();
|
||||
int w = deviceSize.getWidth() & ~7; // in case it's not a multiple of 8
|
||||
int h = deviceSize.getHeight() & ~7;
|
||||
if (maxSize > 0) {
|
||||
if (BuildConfig.DEBUG && maxSize % 8 != 0) {
|
||||
throw new AssertionError("Max size must be a multiple of 8");
|
||||
}
|
||||
boolean portrait = h > w;
|
||||
int major = portrait ? h : w;
|
||||
int minor = portrait ? w : h;
|
||||
if (major > maxSize) {
|
||||
int minorExact = minor * maxSize / major;
|
||||
// +4 to round the value to the nearest multiple of 8
|
||||
minor = (minorExact + 4) & ~7;
|
||||
major = maxSize;
|
||||
}
|
||||
w = portrait ? minor : major;
|
||||
h = portrait ? major : minor;
|
||||
}
|
||||
Size videoSize = new Size(w, h);
|
||||
return new ScreenInfo(deviceSize, videoSize, rotated);
|
||||
}
|
||||
|
||||
public Point getPhysicalPoint(Position position) {
|
||||
@SuppressWarnings("checkstyle:HiddenField") // it hides the field on purpose, to read it with a lock
|
||||
// it hides the field on purpose, to read it with a lock
|
||||
@SuppressWarnings("checkstyle:HiddenField")
|
||||
ScreenInfo screenInfo = getScreenInfo(); // read with synchronization
|
||||
Size videoSize = screenInfo.getVideoSize();
|
||||
Size clientVideoSize = position.getScreenSize();
|
||||
@ -82,11 +70,11 @@ public final class Device {
|
||||
// the device may have been rotated since the event was generated, so ignore the event
|
||||
return null;
|
||||
}
|
||||
Size deviceSize = screenInfo.getDeviceSize();
|
||||
Rect contentRect = screenInfo.getContentRect();
|
||||
Point point = position.getPoint();
|
||||
int scaledX = point.x * deviceSize.getWidth() / videoSize.getWidth();
|
||||
int scaledY = point.y * deviceSize.getHeight() / videoSize.getHeight();
|
||||
return new Point(scaledX, scaledY);
|
||||
int deviceX = contentRect.left + point.x * contentRect.width() / videoSize.getWidth();
|
||||
int deviceY = contentRect.top + point.y * contentRect.height() / videoSize.getHeight();
|
||||
return new Point(deviceX, deviceY);
|
||||
}
|
||||
|
||||
public static String getDeviceName() {
|
||||
|
@ -16,5 +16,16 @@ public final class DisplayInfo {
|
||||
public int getRotation() {
|
||||
return rotation;
|
||||
}
|
||||
|
||||
public DisplayInfo withRotation(int rotation) {
|
||||
if (rotation == this.rotation) {
|
||||
return this;
|
||||
}
|
||||
Size newSize = size;
|
||||
if ((rotation & 1) != (this.rotation & 1)) {
|
||||
newSize = size.rotate();
|
||||
}
|
||||
return new DisplayInfo(newSize, rotation);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,12 @@
|
||||
package com.genymobile.scrcpy;
|
||||
|
||||
import android.graphics.Rect;
|
||||
|
||||
public class Options {
|
||||
private int maxSize;
|
||||
private int bitRate;
|
||||
private boolean tunnelForward;
|
||||
private Rect crop;
|
||||
|
||||
public int getMaxSize() {
|
||||
return maxSize;
|
||||
@ -28,4 +31,12 @@ public class Options {
|
||||
public void setTunnelForward(boolean tunnelForward) {
|
||||
this.tunnelForward = tunnelForward;
|
||||
}
|
||||
|
||||
public Rect getCrop() {
|
||||
return crop;
|
||||
}
|
||||
|
||||
public void setCrop(Rect crop) {
|
||||
this.crop = crop;
|
||||
}
|
||||
}
|
||||
|
@ -56,12 +56,12 @@ public class ScreenEncoder implements Device.RotationListener {
|
||||
do {
|
||||
MediaCodec codec = createCodec();
|
||||
IBinder display = createDisplay();
|
||||
Rect deviceRect = device.getScreenInfo().getDeviceSize().toRect();
|
||||
Rect contentRect = device.getScreenInfo().getContentRect();
|
||||
Rect videoRect = device.getScreenInfo().getVideoSize().toRect();
|
||||
setSize(format, videoRect.width(), videoRect.height());
|
||||
configure(codec, format);
|
||||
Surface surface = codec.createInputSurface();
|
||||
setDisplaySurface(display, surface, deviceRect, videoRect);
|
||||
setDisplaySurface(display, surface, contentRect, videoRect);
|
||||
codec.start();
|
||||
try {
|
||||
alive = encode(codec, outputStream);
|
||||
|
@ -1,14 +1,60 @@
|
||||
package com.genymobile.scrcpy;
|
||||
|
||||
import android.graphics.Rect;
|
||||
|
||||
public final class ScreenInfo {
|
||||
private final Size deviceSize;
|
||||
private final Size videoSize;
|
||||
private final boolean rotated;
|
||||
private final Rect crop;
|
||||
|
||||
public ScreenInfo(Size deviceSize, Size videoSize, boolean rotated) {
|
||||
private ScreenInfo(Size deviceSize, Size videoSize, Rect crop, boolean rotated) {
|
||||
this.deviceSize = deviceSize;
|
||||
this.videoSize = videoSize;
|
||||
this.rotated = rotated;
|
||||
this.crop = crop;
|
||||
}
|
||||
|
||||
public static ScreenInfo create(Size deviceSize, int maxSize, Rect crop, boolean rotated) {
|
||||
Size inputSize;
|
||||
if (crop == null) {
|
||||
inputSize = deviceSize;
|
||||
} else {
|
||||
if (rotated) {
|
||||
// the crop (provided by the user) is expressed in the natural orientation
|
||||
// the device size (provided by the system) already takes the rotation into account
|
||||
crop = rotateCrop(crop, deviceSize);
|
||||
}
|
||||
inputSize = new Size(crop.width(), crop.height());
|
||||
}
|
||||
Size videoSize = computeVideoSize(inputSize, maxSize);
|
||||
return new ScreenInfo(deviceSize, videoSize, crop, rotated);
|
||||
}
|
||||
|
||||
@SuppressWarnings("checkstyle:MagicNumber")
|
||||
private static Size computeVideoSize(Size inputSize, int maxSize) {
|
||||
// Compute the video size and the padding of the content inside this video.
|
||||
// Principle:
|
||||
// - scale down the great side of the screen to maxSize (if necessary);
|
||||
// - scale down the other side so that the aspect ratio is preserved;
|
||||
// - round this value to the nearest multiple of 8 (H.264 only accepts multiples of 8)
|
||||
int w = inputSize.getWidth() & ~7; // in case it's not a multiple of 8
|
||||
int h = inputSize.getHeight() & ~7;
|
||||
if (maxSize > 0) {
|
||||
if (BuildConfig.DEBUG && maxSize % 8 != 0) {
|
||||
throw new AssertionError("Max size must be a multiple of 8");
|
||||
}
|
||||
boolean portrait = h > w;
|
||||
int major = portrait ? h : w;
|
||||
int minor = portrait ? w : h;
|
||||
if (major > maxSize) {
|
||||
int minorExact = minor * maxSize / major;
|
||||
// +4 to round the value to the nearest multiple of 8
|
||||
minor = (minorExact + 4) & ~7;
|
||||
major = maxSize;
|
||||
}
|
||||
w = portrait ? minor : major;
|
||||
h = portrait ? major : minor;
|
||||
}
|
||||
return new Size(w, h);
|
||||
}
|
||||
|
||||
public Size getDeviceSize() {
|
||||
@ -19,11 +65,17 @@ public final class ScreenInfo {
|
||||
return videoSize;
|
||||
}
|
||||
|
||||
public ScreenInfo withRotation(int rotation) {
|
||||
boolean newRotated = (rotation & 1) != 0;
|
||||
if (rotated == newRotated) {
|
||||
return this;
|
||||
public Rect getCrop() {
|
||||
return crop;
|
||||
}
|
||||
return new ScreenInfo(deviceSize.rotate(), videoSize.rotate(), newRotated);
|
||||
|
||||
public Rect getContentRect() {
|
||||
Rect crop = getCrop();
|
||||
return crop != null ? crop : deviceSize.toRect();
|
||||
}
|
||||
|
||||
private static Rect rotateCrop(Rect crop, Size rotatedSize) {
|
||||
int w = rotatedSize.getHeight(); // the size is already rotated
|
||||
return new Rect(crop.top, w - crop.right, crop.bottom, w - crop.left);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user