This commit is contained in:
Romain Vimont 2018-08-07 23:29:08 +02:00
parent c90247ffb6
commit 348c478359
4 changed files with 36 additions and 39 deletions

View File

@ -19,15 +19,30 @@ public final class Device {
private ScreenInfo screenInfo; private ScreenInfo screenInfo;
private RotationListener rotationListener; private RotationListener rotationListener;
private boolean rotated;
public Device(Options options) { public Device(Options options) {
options.setCrop(new Rect(100, 100, 300, 250)); options.setCrop(new Rect(0, 960, 1080, 1920));
screenInfo = computeScreenInfo(options.getMaxSize(), options.getCrop());
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() { registerRotationWatcher(new IRotationWatcher.Stub() {
@Override @Override
public void onRotationChanged(int rotation) throws RemoteException { public void onRotationChanged(int rotation) {
boolean rotated = (rotation & 1) != 0;
synchronized (Device.this) { 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 // notify
if (rotationListener != null) { if (rotationListener != null) {
@ -42,14 +57,9 @@ public final class Device {
return screenInfo; return screenInfo;
} }
private ScreenInfo computeScreenInfo(int maxSize, Rect crop) {
DisplayInfo displayInfo = serviceManager.getDisplayManager().getDisplayInfo();
return ScreenInfo.create(displayInfo, maxSize, crop);
}
public Point getPhysicalPoint(Position position) { public Point getPhysicalPoint(Position position) {
@SuppressWarnings("checkstyle:HiddenField") // it hides the field on purpose, to read it with a lock @SuppressWarnings("checkstyle:HiddenField") // it hides the field on purpose, to read it with a lock
ScreenInfo screenInfo = getScreenInfo(); // read with synchronization ScreenInfo screenInfo = getScreenInfo(); // read with synchronization
Size videoSize = screenInfo.getVideoSize(); Size videoSize = screenInfo.getVideoSize();
Size clientVideoSize = position.getScreenSize(); Size clientVideoSize = position.getScreenSize();
if (!videoSize.equals(clientVideoSize)) { if (!videoSize.equals(clientVideoSize)) {

View File

@ -16,5 +16,16 @@ public final class DisplayInfo {
public int getRotation() { public int getRotation() {
return rotation; 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);
}
} }

View File

@ -57,7 +57,6 @@ public class ScreenEncoder implements Device.RotationListener {
MediaCodec codec = createCodec(); MediaCodec codec = createCodec();
IBinder display = createDisplay(); IBinder display = createDisplay();
Rect contentRect = device.getScreenInfo().getContentRect(); Rect contentRect = device.getScreenInfo().getContentRect();
System.out.println(contentRect);
Rect videoRect = device.getScreenInfo().getVideoSize().toRect(); Rect videoRect = device.getScreenInfo().getVideoSize().toRect();
setSize(format, videoRect.width(), videoRect.height()); setSize(format, videoRect.width(), videoRect.height());
configure(codec, format); configure(codec, format);

View File

@ -6,26 +6,22 @@ public final class ScreenInfo {
private final Size deviceSize; private final Size deviceSize;
private final Size videoSize; private final Size videoSize;
private final Rect crop; private final Rect crop;
private final boolean rotated;
private ScreenInfo(Size deviceSize, Size videoSize, Rect crop, boolean rotated) { private ScreenInfo(Size deviceSize, Size videoSize, Rect crop, boolean rotated) {
this.deviceSize = deviceSize; this.deviceSize = deviceSize;
this.videoSize = videoSize; this.videoSize = videoSize;
this.rotated = rotated;
this.crop = crop; this.crop = crop;
} }
public static ScreenInfo create(DisplayInfo displayInfo, int maxSize, Rect crop) { public static ScreenInfo create(Size deviceSize, int maxSize, Rect crop, boolean rotated) {
boolean rotated = (displayInfo.getRotation() & 1) != 0;
// the device size (provided by the system) takes the rotation into account
Size deviceSize = displayInfo.getSize();
Size inputSize; Size inputSize;
if (crop == null) { if (crop == null) {
inputSize = deviceSize; inputSize = deviceSize;
} else { } else {
if (rotated) { if (rotated) {
// the crop (provided by the user) is expressed in the natural orientation // the crop (provided by the user) is expressed in the natural orientation
crop = rotateCrop(crop, deviceSize.rotate()); // the device size (provided by the system) already takes the rotation into account
crop = rotateCrop(crop, deviceSize);
} }
inputSize = new Size(crop.width(), crop.height()); inputSize = new Size(crop.width(), crop.height());
} }
@ -78,27 +74,8 @@ public final class ScreenInfo {
return crop != null ? crop : deviceSize.toRect(); return crop != null ? crop : deviceSize.toRect();
} }
public ScreenInfo withRotation(int rotation) { private static Rect rotateCrop(Rect crop, Size rotatedSize) {
boolean newRotated = (rotation & 1) != 0; int w = rotatedSize.getHeight(); // the size is already rotated
if (rotated == newRotated) {
return this;
}
Rect newCrop = null;
if (crop != null) {
// the crop has typically a meaning only in one dimension, but we must rotate it so that the
// video size matches on rotation
newCrop = newRotated ? rotateCrop(crop, deviceSize) : unrotateCrop(crop, deviceSize);
}
return new ScreenInfo(deviceSize.rotate(), videoSize.rotate(), newCrop, newRotated);
}
private static Rect rotateCrop(Rect crop, Size deviceSize) {
int w = deviceSize.getWidth();
return new Rect(crop.top, w - crop.right, crop.bottom, w - crop.left); return new Rect(crop.top, w - crop.right, crop.bottom, w - crop.left);
} }
private static Rect unrotateCrop(Rect crop, Size deviceSize) {
int h = deviceSize.getHeight();
return new Rect(h - crop.bottom, crop.left, h - crop.top, crop.right);
}
} }