Android: Add tests for VideoFrame.Buffer.toI420() and cropAndScale()

This CL adds tests that are primarily targeting
VideoFrame.Buffer.toI420() and cropAndScale(), but includes the whole
chain for YuvConverter, GlRectDrawer, and VideoFrameDrawer.

It also includes a couple of fixes to bugs that were exposed by the new
tests.

Bug: webrtc:9186, webrtc:9391
Change-Id: I5eb62979a8fd8def28c3cb2e82dcede57c42216f
Reviewed-on: https://webrtc-review.googlesource.com/83163
Commit-Queue: Magnus Jedvert <magjed@webrtc.org>
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23611}
diff --git a/sdk/android/api/org/webrtc/JavaI420Buffer.java b/sdk/android/api/org/webrtc/JavaI420Buffer.java
index 19dd3e9..7231fda 100644
--- a/sdk/android/api/org/webrtc/JavaI420Buffer.java
+++ b/sdk/android/api/org/webrtc/JavaI420Buffer.java
@@ -39,6 +39,15 @@
     this.refCountDelegate = new RefCountDelegate(releaseCallback);
   }
 
+  private static void checkCapacity(ByteBuffer data, int width, int height, int stride) {
+    // The last row does not necessarily need padding.
+    final int minCapacity = stride * (height - 1) + width;
+    if (data.capacity() < minCapacity) {
+      throw new IllegalArgumentException(
+          "Buffer must be at least " + minCapacity + " bytes, but was " + data.capacity());
+    }
+  }
+
   /** Wraps existing ByteBuffers into JavaI420Buffer object without copying the contents. */
   public static JavaI420Buffer wrap(int width, int height, ByteBuffer dataY, int strideY,
       ByteBuffer dataU, int strideU, ByteBuffer dataV, int strideV, Runnable releaseCallback) {
@@ -55,19 +64,11 @@
     dataU = dataU.slice();
     dataV = dataV.slice();
 
+    final int chromaWidth = (width + 1) / 2;
     final int chromaHeight = (height + 1) / 2;
-    final int minCapacityY = strideY * height;
-    final int minCapacityU = strideU * chromaHeight;
-    final int minCapacityV = strideV * chromaHeight;
-    if (dataY.capacity() < minCapacityY) {
-      throw new IllegalArgumentException("Y-buffer must be at least " + minCapacityY + " bytes.");
-    }
-    if (dataU.capacity() < minCapacityU) {
-      throw new IllegalArgumentException("U-buffer must be at least " + minCapacityU + " bytes.");
-    }
-    if (dataV.capacity() < minCapacityV) {
-      throw new IllegalArgumentException("V-buffer must be at least " + minCapacityV + " bytes.");
-    }
+    checkCapacity(dataY, width, height, strideY);
+    checkCapacity(dataU, chromaWidth, chromaHeight, strideU);
+    checkCapacity(dataV, chromaWidth, chromaHeight, strideV);
 
     return new JavaI420Buffer(
         width, height, dataY, strideY, dataU, strideU, dataV, strideV, releaseCallback);