Add experimental camera session prepare API.

Bug: 18949148
Change-Id: I35fde35436ae5bebab3c912243e621285307c211
diff --git a/core/java/android/hardware/camera2/CameraCaptureSession.java b/core/java/android/hardware/camera2/CameraCaptureSession.java
index 46ffe36..766868d 100644
--- a/core/java/android/hardware/camera2/CameraCaptureSession.java
+++ b/core/java/android/hardware/camera2/CameraCaptureSession.java
@@ -138,6 +138,48 @@
      */
     public abstract void prepare(@NonNull Surface surface) throws CameraAccessException;
 
+    /**
+     * <p>Pre-allocate at most maxCount buffers for an output Surface.</p>
+     *
+     * <p>Like the {@link #prepare(Surface)} method, this method can be used to allocate output
+     * buffers for a given Surface.  However, while the {@link #prepare(Surface)} method allocates
+     * the maximum possible buffer count, this method allocates at most maxCount buffers.</p>
+     *
+     * <p>If maxCount is greater than the possible maximum count (which is the sum of the buffer
+     * count requested by the creator of the Surface and the count requested by the camera device),
+     * only the possible maximum count is allocated, in which case the function acts exactly like
+     * {@link #prepare(Surface)}.</p>
+     *
+     * <p>The restrictions on when this method can be called are the same as for
+     * {@link #prepare(Surface)}.</p>
+     *
+     * <p>Repeated calls to this method are allowed, and a mix of {@link #prepare(Surface)} and
+     * this method is also allowed. Note that after the first call to {@link #prepare(Surface)},
+     * subsequent calls to either prepare method are effectively no-ops.  In addition, this method
+     * is not additive in terms of buffer count.  This means calling it twice with maxCount = 2
+     * will only allocate 2 buffers, not 4 (assuming the possible maximum is at least 2); to
+     * allocate two buffers on the first call and two on the second, the application needs to call
+     * prepare with prepare(surface, 2) and prepare(surface, 4).</p>
+     *
+     * @param maxCount the buffer count to try to allocate. If this is greater than the possible
+     *                 maximum for this output, the possible maximum is allocated instead. If
+     *                 maxCount buffers are already allocated, then prepare will do nothing.
+     * @param surface the output Surface for which buffers should be pre-allocated.
+     *
+     * @throws CameraAccessException if the camera device is no longer connected or has
+     *                               encountered a fatal error.
+     * @throws IllegalStateException if this session is no longer active, either because the
+     *                               session was explicitly closed, a new session has been created
+     *                               or the camera device has been closed.
+     * @throws IllegalArgumentException if the Surface is invalid, not part of this Session,
+     *                                  or has already been used as a target of a CaptureRequest in
+     *                                  this session or immediately prior sessions without an
+     *                                  intervening tearDown call.
+     *
+     * @hide
+     */
+    public abstract void prepare(int maxCount, @NonNull Surface surface)
+            throws CameraAccessException;
 
     /**
      * <p>Free all buffers allocated for an output Surface.</p>
diff --git a/core/java/android/hardware/camera2/ICameraDeviceUser.aidl b/core/java/android/hardware/camera2/ICameraDeviceUser.aidl
index 7cb3673..c9c9abc 100644
--- a/core/java/android/hardware/camera2/ICameraDeviceUser.aidl
+++ b/core/java/android/hardware/camera2/ICameraDeviceUser.aidl
@@ -102,4 +102,6 @@
     int prepare(int streamId);
 
     int tearDown(int streamId);
+
+    int prepare2(int maxCount, int streamId);
 }
diff --git a/core/java/android/hardware/camera2/impl/CameraCaptureSessionImpl.java b/core/java/android/hardware/camera2/impl/CameraCaptureSessionImpl.java
index d325c77..5573896 100644
--- a/core/java/android/hardware/camera2/impl/CameraCaptureSessionImpl.java
+++ b/core/java/android/hardware/camera2/impl/CameraCaptureSessionImpl.java
@@ -146,6 +146,11 @@
     }
 
     @Override
+    public void prepare(int maxCount, Surface surface) throws CameraAccessException {
+        mDeviceImpl.prepare(maxCount, surface);
+    }
+
+    @Override
     public void tearDown(Surface surface) throws CameraAccessException {
         mDeviceImpl.tearDown(surface);
     }
diff --git a/core/java/android/hardware/camera2/impl/CameraConstrainedHighSpeedCaptureSessionImpl.java b/core/java/android/hardware/camera2/impl/CameraConstrainedHighSpeedCaptureSessionImpl.java
index a920e2b..8cd1da5 100644
--- a/core/java/android/hardware/camera2/impl/CameraConstrainedHighSpeedCaptureSessionImpl.java
+++ b/core/java/android/hardware/camera2/impl/CameraConstrainedHighSpeedCaptureSessionImpl.java
@@ -169,6 +169,11 @@
     }
 
     @Override
+    public void prepare(int maxCount, Surface surface) throws CameraAccessException {
+        mSessionImpl.prepare(maxCount, surface);
+    }
+
+    @Override
     public void tearDown(Surface surface) throws CameraAccessException {
         mSessionImpl.tearDown(surface);
     }
diff --git a/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java b/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
index 91d623e..6e02df1 100644
--- a/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
+++ b/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
@@ -679,6 +679,33 @@
         }
     }
 
+    public void prepare(int maxCount, Surface surface) throws CameraAccessException {
+        if (surface == null) throw new IllegalArgumentException("Surface is null");
+        if (maxCount <= 0) throw new IllegalArgumentException("Invalid maxCount given: " +
+                maxCount);
+
+        synchronized(mInterfaceLock) {
+            int streamId = -1;
+            for (int i = 0; i < mConfiguredOutputs.size(); i++) {
+                if (surface == mConfiguredOutputs.valueAt(i).getSurface()) {
+                    streamId = mConfiguredOutputs.keyAt(i);
+                    break;
+                }
+            }
+            if (streamId == -1) {
+                throw new IllegalArgumentException("Surface is not part of this session");
+            }
+            try {
+                mRemoteDevice.prepare2(maxCount, streamId);
+            } catch (CameraRuntimeException e) {
+                throw e.asChecked();
+            } catch (RemoteException e) {
+                // impossible
+                return;
+            }
+        }
+    }
+
     public void tearDown(Surface surface) throws CameraAccessException {
         if (surface == null) throw new IllegalArgumentException("Surface is null");
 
diff --git a/core/java/android/hardware/camera2/legacy/CameraDeviceUserShim.java b/core/java/android/hardware/camera2/legacy/CameraDeviceUserShim.java
index e20eaa7..6b8e113 100644
--- a/core/java/android/hardware/camera2/legacy/CameraDeviceUserShim.java
+++ b/core/java/android/hardware/camera2/legacy/CameraDeviceUserShim.java
@@ -636,6 +636,11 @@
         return CameraBinderDecorator.NO_ERROR;
     }
 
+    public int prepare2(int maxCount, int streamId) {
+        // We don't support this in LEGACY mode.
+        return prepare(streamId);
+    }
+
     public int tearDown(int streamId) {
         if (DEBUG) {
             Log.d(TAG, "tearDown called.");