Skip to content

Display and control your Android device

Fix size constraints algorithm

Romain Vimont71c6bf4a87a8
diff --git a/server/src/main/java/com/genymobile/scrcpy/model/Size.java b/server/src/main/java/com/genymobile/scrcpy/model/Size.java
index 63648a9e..cbfec9e8 100644
--- a/server/src/main/java/com/genymobile/scrcpy/model/Size.java
+++ b/server/src/main/java/com/genymobile/scrcpy/model/Size.java
@@ -1,8 +1,12 @@
package com.genymobile.scrcpy.model;
+import com.genymobile.scrcpy.util.BinarySearch;
+import com.genymobile.scrcpy.util.Ln;
import com.genymobile.scrcpy.video.VideoConstraints;
import android.graphics.Rect;
+import android.media.MediaCodecInfo;
+import android.util.Range;
import java.util.Objects;
@@ -38,61 +42,84 @@ public final class Size {
public Size constrain(VideoConstraints constraints, boolean preserveAspectRatio) {
int maxSize = constraints.getMaxSize();
int alignment = constraints.getAlignment();
+ MediaCodecInfo.VideoCapabilities caps = constraints.getEncoderCapabilities();
assert maxSize >= 0 : "Max size may not be negative";
assert alignment > 0 : "Alignment must be positive";
assert (alignment & (alignment - 1)) == 0 : "Alignment must be a power-of-two";
- boolean portrait = width < height;
- Size maxCodecSize = portrait ? constraints.getMaxCodecPortraitSize() : constraints.getMaxCodecLandscapeSize();
+ boolean landscape = width >= height;
+ int major = landscape ? width : height;
+ int minor = landscape ? height : width;
- int maxWidth = maxCodecSize.width;
- int maxHeight = maxCodecSize.height;
- if (maxSize > 0) {
- if (maxWidth > maxSize) {
- maxWidth = maxSize;
- }
- if (maxHeight > maxSize) {
- maxHeight = maxSize;
- }
+ Range<Integer> majorRange = landscape ? caps.getSupportedWidths() : caps.getSupportedHeights();
+ int minMajor = majorRange.getLower();
+ int maxMajor = majorRange.getUpper();
+ if (maxMajor > major) {
+ // Never increase the size
+ maxMajor = major;
+ }
+ if (maxSize > 0 && maxMajor > maxSize) {
+ maxMajor = maxSize;
}
- maxWidth = align(maxWidth, alignment);
- maxHeight = align(maxHeight, alignment);
- int w, h;
- if (preserveAspectRatio) {
- if (width > maxWidth || height > maxHeight) {
- if (width * maxHeight > height * maxWidth) {
- w = maxWidth;
- h = height * maxWidth / width;
- } else {
- w = width * maxHeight / height;
- h = maxHeight;
- }
- } else {
- w = width;
- h = height;
- }
- } else {
- w = Math.min(width, maxWidth);
- h = Math.min(height, maxHeight);
+ int minBlock = (minMajor + alignment - 1) / alignment;
+ int maxBlock = maxMajor / alignment;
+
+ int bestBlock = BinarySearch.findHighestTrue(
+ minBlock, maxBlock, block -> {
+ int pixels = block * alignment;
+ int w = align(width * pixels / major, alignment);
+ int h = align(height * pixels / major, alignment);
+ return caps.isSizeSupported(w, h);
+ });
+
+ if (bestBlock < minBlock) {
+ Ln.d("No matching size found, ignore encoder size validation");
+ bestBlock = maxBlock;
}
- w = align(w, alignment);
- h = align(h, alignment);
+ int bestMajor = bestBlock * alignment;
+ int bestMinor;
+
+ if (preserveAspectRatio) {
+ bestMinor = align(minor * bestMajor / major, alignment);
+ } else {
+ // The minor dimension can potentially be extended
+ int maxMinor = landscape ? caps.getSupportedHeightsFor(bestMajor).getUpper() : caps.getSupportedWidthsFor(bestMajor).getUpper();
+ if (maxMinor > minor) {
+ maxMinor = minor;
+ }
+ if (maxSize > 0 && maxMinor > maxSize) {
+ maxMinor = maxSize;
+ }
+ bestMinor = align(maxMinor, alignment);
- assert w <= maxWidth : "The width cannot exceed maxWidth";
- assert h <= maxHeight : "The height cannot exceed maxHeight";
+ // The major dimension can potentially be extended
+ maxMajor = landscape ? caps.getSupportedWidthsFor(bestMinor).getUpper() : caps.getSupportedHeightsFor(bestMinor).getUpper();
+ if (maxMajor > major) {
+ maxMajor = major;
+ }
+ if (maxSize > 0 && maxMajor > maxSize) {
+ maxMajor = maxSize;
+ }
+ bestMajor = align(maxMajor, alignment);
+ }
- // Minimum codec size must be respected (regardless of requested maxSize)
- int minCodecSize = alignUp(constraints.getMinCodecSize(), alignment);
- if (w < minCodecSize) {
- w = minCodecSize;
+ minMajor = alignUp(minMajor, alignment);
+ if (bestMajor < minMajor) {
+ bestMajor = minMajor;
}
- if (h < minCodecSize) {
- h = minCodecSize;
+
+ int minMinor = landscape ? caps.getSupportedHeights().getLower() : caps.getSupportedWidths().getLower();
+ minMinor = alignUp(minMinor, alignment);
+ if (bestMinor < minMinor) {
+ bestMinor = minMinor;
}
+ int w = landscape ? bestMajor : bestMinor;
+ int h = landscape ? bestMinor : bestMajor;
+
assert w % alignment == 0 : "The width must be a multiple of alignment";
assert h % alignment == 0 : "The height must be a multiple of alignment";
diff --git a/server/src/main/java/com/genymobile/scrcpy/util/BinarySearch.java b/server/src/main/java/com/genymobile/scrcpy/util/BinarySearch.java
new file mode 100644
index 00000000..381601fe
--- /dev/null
+++ b/server/src/main/java/com/genymobile/scrcpy/util/BinarySearch.java
@@ -0,0 +1,53 @@
+package com.genymobile.scrcpy.util;
+
+import com.genymobile.scrcpy.AndroidVersions;
+
+import android.annotation.TargetApi;
+
+import java.util.function.Predicate;
+
+public final class BinarySearch {
+ private BinarySearch() {
+ // not instantiable
+ }
+
+ /**
+ * Find the highest value for which the predicate is true.
+ * <p>
+ * This assumes that the predicate is monotonic:
+ * - if it returns {@code false} for a given value, it must return {@code false} for higher values, or equivalently
+ * - if it returns {@code true} for a given value, it must return {@code true} for lower values.
+ *
+ * @param low the lowest value
+ * @param high the highest value
+ * @param predicate the predicate
+ * @return the highest value for which {@code predicate.test(value)} is {@code true}, or {@code low - 1} if not found.
+ */
+ // This function is used for virtual displays, so we don't need to support older versions anyway
+ @TargetApi(AndroidVersions.API_24_ANDROID_7_0) // for Predicate<T>
+ public static int findHighestTrue(int low, int high, Predicate<Integer> predicate) {
+ if (low <= high) {
+ // Fast path
+ if (predicate.test(high)) {
+ return high;
+ }
+ --high;
+ }
+
+ int result = low - 1; // low-1 means "not found"
+ while (low <= high) {
+ int mid = low + (high - low + 1) / 2;
+ if (predicate.test(mid)) {
+ result = mid;
+
+ // predicate holds, go right
+ low = mid + 1;
+ } else {
+ // predicate false, go left
+ high = mid - 1;
+ }
+ }
+
+ return result;
+ }
+}
diff --git a/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java b/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java
index 1ccb775a..652e59a2 100644
--- a/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java
+++ b/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java
@@ -75,19 +75,7 @@ public class SurfaceEncoder implements AsyncProcessor {
Ln.d("Actual video size alignment: " + alignment + "px");
}
- int maxLandscapeWidth = caps.getSupportedWidths().getUpper();
- int maxLandscapeHeight = caps.getSupportedHeightsFor(maxLandscapeWidth).getUpper();
- Size maxLandscapeSize = new Size(maxLandscapeWidth, maxLandscapeHeight);
-
- int maxPortraitHeight = caps.getSupportedHeights().getUpper();
- int maxPortraitWidth = caps.getSupportedWidthsFor(maxPortraitHeight).getUpper();
- Size maxPortraitSize = new Size(maxPortraitWidth, maxPortraitHeight);
-
- int minWidth = caps.getSupportedWidths().getLower();
- int minHeight = caps.getSupportedHeights().getLower();
- int minSize = Math.max(minWidth, minHeight);
-
- return new VideoConstraints(maxSize, alignment, maxLandscapeSize, maxPortraitSize, minSize);
+ return new VideoConstraints(maxSize, alignment, caps);
}
private void streamCapture() throws IOException, ConfigurationException {
diff --git a/server/src/main/java/com/genymobile/scrcpy/video/VideoConstraints.java b/server/src/main/java/com/genymobile/scrcpy/video/VideoConstraints.java
index 39b28682..38843025 100644
--- a/server/src/main/java/com/genymobile/scrcpy/video/VideoConstraints.java
+++ b/server/src/main/java/com/genymobile/scrcpy/video/VideoConstraints.java
@@ -1,15 +1,13 @@
package com.genymobile.scrcpy.video;
-import com.genymobile.scrcpy.model.Size;
+import android.media.MediaCodecInfo;
public class VideoConstraints {
private final int maxSize;
private final int alignment;
- private final Size maxCodecLandscapeSize;
- private final Size maxCodecPortraitSize;
- private final int minCodecSize;
+ private final MediaCodecInfo.VideoCapabilities caps;
- public VideoConstraints(int maxSize, int alignment, Size maxCodecLandscapeSize, Size maxCodecPortraitSize, int minCodecSize) {
+ public VideoConstraints(int maxSize, int alignment, MediaCodecInfo.VideoCapabilities caps) {
assert maxSize >= 0 : "Max size must not be negative";
this.maxSize = maxSize;
@@ -17,14 +15,8 @@ public class VideoConstraints {
assert (alignment & (alignment - 1)) == 0 : "Alignment must be a power-of-two";
this.alignment = alignment;
- assert maxCodecLandscapeSize != null;
- this.maxCodecLandscapeSize = maxCodecLandscapeSize;
-
- assert maxCodecPortraitSize != null;
- this.maxCodecPortraitSize = maxCodecPortraitSize;
-
- assert minCodecSize >= 0;
- this.minCodecSize = minCodecSize;
+ assert caps != null;
+ this.caps = caps;
}
/**
@@ -48,30 +40,12 @@ public class VideoConstraints {
}
/**
- * Return the max landscape size supported by the codec.
- *
- * @return the max landscape size
- */
- public Size getMaxCodecLandscapeSize() {
- return maxCodecLandscapeSize;
- }
-
- /**
- * Return the max portrait size supported by the codec.
- *
- * @return the max portrait size
- */
- public Size getMaxCodecPortraitSize() {
- return maxCodecPortraitSize;
- }
-
- /**
- * Return the min size supported by the codec.
+ * Return the video encoder capabilities.
*
- * @return the min size
+ * @return the video encoder capabilities
*/
- public int getMinCodecSize() {
- return minCodecSize;
+ public MediaCodecInfo.VideoCapabilities getEncoderCapabilities() {
+ return caps;
}
/**
@@ -81,6 +55,6 @@ public class VideoConstraints {
* @return the new video constraints
*/
public VideoConstraints withMaxSize(int maxSize) {
- return new VideoConstraints(maxSize, alignment, maxCodecLandscapeSize, maxCodecPortraitSize, minCodecSize);
+ return new VideoConstraints(maxSize, alignment, caps);
}
}
diff --git a/server/src/test/java/com/genymobile/scrcpy/model/SizeTest.java b/server/src/test/java/com/genymobile/scrcpy/model/SizeTest.java
deleted file mode 100644
index d86048fa..00000000
--- a/server/src/test/java/com/genymobile/scrcpy/model/SizeTest.java
+++ /dev/null
@@ -1,75 +0,0 @@
-package com.genymobile.scrcpy.model;
-
-import com.genymobile.scrcpy.video.VideoConstraints;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-public class SizeTest {
-
- private VideoConstraints createVideoConstraints(int maxSize, int alignment) {
- Size maxCodecSize = new Size(4096, 4096);
- return createVideoConstraints(maxSize, alignment, maxCodecSize);
- }
-
- private VideoConstraints createVideoConstraints(int maxSize, int alignment, Size maxCodecSize) {
- return new VideoConstraints(maxSize, alignment, maxCodecSize, maxCodecSize, 0);
- }
-
- private VideoConstraints createVideoConstraints(int maxSize, int alignment, Size maxCodecSize, int minCodecSize) {
- return new VideoConstraints(maxSize, alignment, maxCodecSize, maxCodecSize, minCodecSize);
- }
-
- @Test
- public void testConstrainSize() {
- Size size = new Size(207, 209);
-
- Assert.assertEquals(size, size.constrain(createVideoConstraints(0, 1)));
- Assert.assertEquals(new Size(206, 208), size.constrain(createVideoConstraints(0, 2)));
- Assert.assertEquals(new Size(204, 208), size.constrain(createVideoConstraints(0, 4)));
-
- Size s = size.constrain(createVideoConstraints(208, 2));
- Assert.assertEquals(208, s.getHeight());
- Assert.assertTrue(s.getWidth() >= 206 && s.getWidth() <= 208 && s.getWidth() % 2 == 0);
-
- Assert.assertEquals(new Size(207 * 208 / 209, 208), size.constrain(createVideoConstraints(208, 2)));
- Assert.assertEquals(new Size(204, 208), size.constrain(createVideoConstraints(208, 4)));
- }
-
- @Test
- public void testConstrainSizeUnchanged() {
- Size size = new Size(256, 512);
-
- Assert.assertEquals(size, size.constrain(createVideoConstraints(0, 1)));
- Assert.assertEquals(size, size.constrain(createVideoConstraints(512, 1)));
- Assert.assertEquals(size, size.constrain(createVideoConstraints(515, 1)));
- Assert.assertEquals(size, size.constrain(createVideoConstraints(512, 16)));
- Assert.assertEquals(size, size.constrain(createVideoConstraints(515, 16)));
- Assert.assertEquals(size, size.constrain(createVideoConstraints(0, 16)));
- Assert.assertEquals(size, size.constrain(createVideoConstraints(4096, 16)));
- }
-
- @Test
- public void testConstrainMaxCodecSize() {
- Size maxCodecSize = new Size(400, 600);
-
- Size size = new Size(314, 617);
- Assert.assertEquals(new Size(314 * 600 / 617, 600), size.constrain(createVideoConstraints(0, 1, maxCodecSize)));
- Assert.assertEquals(new Size(314 * 550 / 617, 550), size.constrain(createVideoConstraints(550, 1, maxCodecSize)));
-
- size = new Size(512, 536);
- Assert.assertEquals(new Size(400, 536 * 400 / 512), size.constrain(createVideoConstraints(0, 1, maxCodecSize)));
- Assert.assertEquals(new Size(400, 536 * 400 / 512), size.constrain(createVideoConstraints(550, 1, maxCodecSize)));
- Assert.assertEquals(new Size(400, 536 * 400 / 512), size.constrain(createVideoConstraints(500, 1, maxCodecSize)));
- Assert.assertEquals(new Size(512 * 410 / 536, 410), size.constrain(createVideoConstraints(410, 1, maxCodecSize)));
- }
-
- @Test
- public void testConstrainMinCodecSize() {
- Size maxCodecSize = new Size(1024, 1024);
-
- Size size = new Size(800, 600);
- Assert.assertEquals(new Size(800, 600), size.constrain(createVideoConstraints(800, 1, maxCodecSize, 512)));
- Assert.assertEquals(new Size(512, 512), size.constrain(createVideoConstraints(400, 1, maxCodecSize, 512)));
- }
-}
diff --git a/server/src/test/java/com/genymobile/scrcpy/util/BinarySearchTest.java b/server/src/test/java/com/genymobile/scrcpy/util/BinarySearchTest.java
new file mode 100644
index 00000000..428e1442
--- /dev/null
+++ b/server/src/test/java/com/genymobile/scrcpy/util/BinarySearchTest.java
@@ -0,0 +1,23 @@
+package com.genymobile.scrcpy.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class BinarySearchTest {
+
+ @Test
+ public void testFindHighestTrue() {
+ Assert.assertEquals(4, BinarySearch.findHighestTrue(5, 15, i -> false));
+ Assert.assertEquals(5, BinarySearch.findHighestTrue(5, 15, i -> i < 6));
+ Assert.assertEquals(6, BinarySearch.findHighestTrue(5, 15, i -> i < 7));
+ Assert.assertEquals(7, BinarySearch.findHighestTrue(5, 15, i -> i < 8));
+ Assert.assertEquals(8, BinarySearch.findHighestTrue(5, 15, i -> i < 9));
+ Assert.assertEquals(9, BinarySearch.findHighestTrue(5, 15, i -> i < 10));
+ Assert.assertEquals(10, BinarySearch.findHighestTrue(5, 15, i -> i < 11));
+ Assert.assertEquals(11, BinarySearch.findHighestTrue(5, 15, i -> i < 12));
+ Assert.assertEquals(12, BinarySearch.findHighestTrue(5, 15, i -> i < 13));
+ Assert.assertEquals(13, BinarySearch.findHighestTrue(5, 15, i -> i < 14));
+ Assert.assertEquals(14, BinarySearch.findHighestTrue(5, 15, i -> i < 15));
+ Assert.assertEquals(15, BinarySearch.findHighestTrue(5, 15, i -> true));
+ }
+}