Added JNI and Java code to capture a layer

Test: Boots

Change-Id: I1d84c6ad32624513cd0124d6f67e930af655caeb
diff --git a/core/java/android/view/SurfaceControl.java b/core/java/android/view/SurfaceControl.java
index 5482589..4ddd40e 100644
--- a/core/java/android/view/SurfaceControl.java
+++ b/core/java/android/view/SurfaceControl.java
@@ -60,6 +60,8 @@
     private static native void nativeScreenshot(IBinder displayToken, Surface consumer,
             Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
             boolean allLayers, boolean useIdentityTransform);
+    private static native void nativeCaptureLayers(IBinder layerHandleToken, Surface consumer,
+            int rotation);
 
     private static native long nativeCreateTransaction();
     private static native long nativeGetNativeTransactionFinalizer();
@@ -970,6 +972,20 @@
                 minLayer, maxLayer, allLayers, useIdentityTransform);
     }
 
+    /**
+     * Captures a layer and its children into the provided {@link Surface}.
+     *
+     * @param layerHandleToken The root layer to capture.
+     * @param consumer The {@link Surface} to capture the layer into.
+     * @param rotation Apply a custom clockwise rotation to the screenshot, i.e.
+     *                 Surface.ROTATION_0,90,180,270. Surfaceflinger will always capture in its
+     *                 native portrait orientation by default, so this is useful for returning
+     *                 captures that are independent of device orientation.
+     */
+    public static void captureLayers(IBinder layerHandleToken, Surface consumer, int rotation) {
+        nativeCaptureLayers(layerHandleToken, consumer, rotation);
+    }
+
     public static class Transaction implements Closeable {
         public static final NativeAllocationRegistry sRegistry = new NativeAllocationRegistry(
                 Transaction.class.getClassLoader(),
diff --git a/core/jni/android_view_SurfaceControl.cpp b/core/jni/android_view_SurfaceControl.cpp
index 8ae9ada..cfeba83 100644
--- a/core/jni/android_view_SurfaceControl.cpp
+++ b/core/jni/android_view_SurfaceControl.cpp
@@ -290,6 +290,22 @@
     }
 }
 
+static void nativeCaptureLayers(JNIEnv* env, jclass clazz, jobject layerHandleToken,
+        jobject surfaceObj, int rotation) {
+
+    sp<IBinder> layerHandle = ibinderForJavaObject(env, layerHandleToken);
+    if (layerHandle == NULL) {
+        return;
+    }
+
+    sp<Surface> consumer = android_view_Surface_getSurface(env, surfaceObj);
+    if (consumer == NULL) {
+        return;
+    }
+
+    ScreenshotClient::captureLayers(layerHandle, consumer->getIGraphicBufferProducer(), rotation);
+}
+
 static void nativeApplyTransaction(JNIEnv* env, jclass clazz, jlong transactionObj, jboolean sync) {
     auto transaction = reinterpret_cast<SurfaceComposerClient::Transaction*>(transactionObj);
     transaction->apply(sync);
@@ -949,6 +965,8 @@
     {"nativeScreenshotToBuffer",
      "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZI)Landroid/graphics/GraphicBuffer;",
      (void*)nativeScreenshotToBuffer },
+    {"nativeCaptureLayers", "(Landroid/os/IBinder;Landroid/view/Surface;I)V",
+            (void*)nativeCaptureLayers },
 };
 
 int register_android_view_SurfaceControl(JNIEnv* env)