Move TextureLayer into android.graphics

Bug: 155905258
Test: make and boot

It is tightly integrated with android.graphics.HardwareRenderer, and
this follows on I30d34055b6870dc1039f190a88f4a747cee17300, which moved
the native component into android_graphics_TextureLayer.cpp, and
Ifa044281a3c36cbc1b413175711e9b172cda640f, which registers its JNI along
with other graphics classes.

Make TextureLayer AutoCloseable, replacing destroy() with close(). Add
annotations where appropriate.

Change-Id: I1b146ff02a20751246636144c88fe6f8eec43514
diff --git a/core/java/android/view/TextureView.java b/core/java/android/view/TextureView.java
index 277b872..a02070a 100644
--- a/core/java/android/view/TextureView.java
+++ b/core/java/android/view/TextureView.java
@@ -27,6 +27,7 @@
 import android.graphics.RecordingCanvas;
 import android.graphics.Rect;
 import android.graphics.SurfaceTexture;
+import android.graphics.TextureLayer;
 import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
 import android.util.Log;
@@ -247,7 +248,7 @@
     private void destroyHardwareLayer() {
         if (mLayer != null) {
             mLayer.detachSurfaceTexture();
-            mLayer.destroy();
+            mLayer.close();
             mLayer = null;
             mMatrixChanged = true;
         }
diff --git a/graphics/java/android/graphics/HardwareRenderer.java b/graphics/java/android/graphics/HardwareRenderer.java
index 87060ca..6dd1f99 100644
--- a/graphics/java/android/graphics/HardwareRenderer.java
+++ b/graphics/java/android/graphics/HardwareRenderer.java
@@ -39,7 +39,6 @@
 import android.view.PixelCopy;
 import android.view.Surface;
 import android.view.SurfaceHolder;
-import android.view.TextureLayer;
 import android.view.animation.AnimationUtils;
 
 import java.io.File;
diff --git a/graphics/java/android/graphics/RecordingCanvas.java b/graphics/java/android/graphics/RecordingCanvas.java
index 88b32c8..22aacde 100644
--- a/graphics/java/android/graphics/RecordingCanvas.java
+++ b/graphics/java/android/graphics/RecordingCanvas.java
@@ -19,7 +19,6 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.util.Pools.SynchronizedPool;
-import android.view.TextureLayer;
 
 import dalvik.annotation.optimization.CriticalNative;
 import dalvik.annotation.optimization.FastNative;
@@ -214,9 +213,9 @@
      * Draws the specified layer onto this canvas.
      *
      * @param layer The layer to composite on this canvas
-     * @hide
+     * @hide TODO: Make this a SystemApi for b/155905258
      */
-    public void drawTextureLayer(TextureLayer layer) {
+    public void drawTextureLayer(@NonNull TextureLayer layer) {
         nDrawTextureLayer(mNativeCanvasWrapper, layer.getLayerHandle());
     }
 
diff --git a/core/java/android/view/TextureLayer.java b/graphics/java/android/graphics/TextureLayer.java
similarity index 84%
rename from core/java/android/view/TextureLayer.java
rename to graphics/java/android/graphics/TextureLayer.java
index abf4d9f..ac1bd69 100644
--- a/core/java/android/view/TextureLayer.java
+++ b/graphics/java/android/graphics/TextureLayer.java
@@ -14,14 +14,11 @@
  * limitations under the License.
  */
 
-package android.view;
+package android.graphics;
 
+import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.graphics.Bitmap;
-import android.graphics.HardwareRenderer;
-import android.graphics.Matrix;
-import android.graphics.Paint;
-import android.graphics.SurfaceTexture;
+import android.view.View;
 
 import com.android.internal.util.VirtualRefBasePtr;
 
@@ -30,13 +27,13 @@
  * frame when drawn in a HW accelerated Canvas. This is backed by a DeferredLayerUpdater on
  * the native side.
  *
- * @hide
+ * @hide TODO: Make this a SystemApi for b/155905258
  */
-public final class TextureLayer {
+public final class TextureLayer implements AutoCloseable {
     private HardwareRenderer mRenderer;
     private VirtualRefBasePtr mFinalizer;
 
-    private TextureLayer(HardwareRenderer renderer, long deferredUpdater) {
+    private TextureLayer(@NonNull HardwareRenderer renderer, long deferredUpdater) {
         if (renderer == null || deferredUpdater == 0) {
             throw new IllegalArgumentException("Either hardware renderer: " + renderer
                     + " or deferredUpdater: " + deferredUpdater + " is invalid");
@@ -61,14 +58,15 @@
      *
      * @return True if the layer can be rendered into, false otherwise
      */
-    public boolean isValid() {
+    private boolean isValid() {
         return mFinalizer != null && mFinalizer.get() != 0;
     }
 
     /**
      * Destroys resources without waiting for a GC.
      */
-    public void destroy() {
+    @Override
+    public void close() {
         if (!isValid()) {
             // Already destroyed
             return;
@@ -79,7 +77,7 @@
         mFinalizer = null;
     }
 
-    public long getDeferredLayerUpdater() {
+    /*package*/ long getDeferredLayerUpdater() {
         return mFinalizer.get();
     }
 
@@ -90,7 +88,7 @@
      *
      * @return True if the copy was successful, false otherwise
      */
-    public boolean copyInto(Bitmap bitmap) {
+    public boolean copyInto(@NonNull Bitmap bitmap) {
         return mRenderer.copyLayerInto(this, bitmap);
     }
 
@@ -114,7 +112,7 @@
      *
      * @param matrix The transform to apply to the layer.
      */
-    public void setTransform(Matrix matrix) {
+    public void setTransform(@NonNull Matrix matrix) {
         nSetTransform(mFinalizer.get(), matrix.ni());
         mRenderer.pushLayerUpdate(this);
     }
@@ -126,11 +124,11 @@
         mRenderer.detachSurfaceTexture(mFinalizer.get());
     }
 
-    public long getLayerHandle() {
+    /*package*/ long getLayerHandle() {
         return mFinalizer.get();
     }
 
-    public void setSurfaceTexture(SurfaceTexture surface) {
+    public void setSurfaceTexture(@NonNull SurfaceTexture surface) {
         nSetSurfaceTexture(mFinalizer.get(), surface);
         mRenderer.pushLayerUpdate(this);
     }
@@ -140,8 +138,8 @@
         mRenderer.pushLayerUpdate(this);
     }
 
-    /** @hide */
-    public static TextureLayer adoptTextureLayer(HardwareRenderer renderer, long layer) {
+    /*package*/ static TextureLayer adoptTextureLayer(@NonNull HardwareRenderer renderer,
+            long layer) {
         return new TextureLayer(renderer, layer);
     }
 
@@ -149,6 +147,7 @@
             boolean isOpaque);
     private static native void nSetLayerPaint(long layerUpdater, long paint);
     private static native void nSetTransform(long layerUpdater, long matrix);
-    private static native void nSetSurfaceTexture(long layerUpdater, SurfaceTexture surface);
+    private static native void nSetSurfaceTexture(long layerUpdater,
+            @NonNull SurfaceTexture surface);
     private static native void nUpdateSurfaceTexture(long layerUpdater);
 }
diff --git a/libs/hwui/apex/jni_runtime.cpp b/libs/hwui/apex/jni_runtime.cpp
index a114e2f..b933813 100644
--- a/libs/hwui/apex/jni_runtime.cpp
+++ b/libs/hwui/apex/jni_runtime.cpp
@@ -61,6 +61,7 @@
 extern int register_android_graphics_PathMeasure(JNIEnv* env);
 extern int register_android_graphics_Picture(JNIEnv*);
 extern int register_android_graphics_Region(JNIEnv* env);
+extern int register_android_graphics_TextureLayer(JNIEnv* env);
 extern int register_android_graphics_animation_NativeInterpolatorFactory(JNIEnv* env);
 extern int register_android_graphics_animation_RenderNodeAnimator(JNIEnv* env);
 extern int register_android_graphics_drawable_AnimatedVectorDrawable(JNIEnv* env);
@@ -76,7 +77,6 @@
 extern int register_android_util_PathParser(JNIEnv* env);
 extern int register_android_view_DisplayListCanvas(JNIEnv* env);
 extern int register_android_view_RenderNode(JNIEnv* env);
-extern int register_android_view_TextureLayer(JNIEnv* env);
 extern int register_android_view_ThreadedRenderer(JNIEnv* env);
 
 #ifdef NDEBUG
@@ -123,6 +123,7 @@
     REG_JNI(register_android_graphics_Picture),
     REG_JNI(register_android_graphics_Region),
     REG_JNI(register_android_graphics_Shader),
+    REG_JNI(register_android_graphics_TextureLayer),
     REG_JNI(register_android_graphics_Typeface),
     REG_JNI(register_android_graphics_YuvImage),
     REG_JNI(register_android_graphics_animation_NativeInterpolatorFactory),
@@ -140,7 +141,6 @@
     REG_JNI(register_android_util_PathParser),
     REG_JNI(register_android_view_RenderNode),
     REG_JNI(register_android_view_DisplayListCanvas),
-    REG_JNI(register_android_view_TextureLayer),
     REG_JNI(register_android_view_ThreadedRenderer),
 };
 
diff --git a/libs/hwui/jni/android_graphics_TextureLayer.cpp b/libs/hwui/jni/android_graphics_TextureLayer.cpp
index bd20269..4dbb24c 100644
--- a/libs/hwui/jni/android_graphics_TextureLayer.cpp
+++ b/libs/hwui/jni/android_graphics_TextureLayer.cpp
@@ -67,7 +67,7 @@
 // JNI Glue
 // ----------------------------------------------------------------------------
 
-const char* const kClassPathName = "android/view/TextureLayer";
+const char* const kClassPathName = "android/graphics/TextureLayer";
 
 static const JNINativeMethod gMethods[] = {
     { "nPrepare",                "(JIIZ)Z",    (void*) TextureLayer_prepare },
@@ -78,7 +78,7 @@
     { "nUpdateSurfaceTexture",   "(J)V",       (void*) TextureLayer_updateSurfaceTexture },
 };
 
-int register_android_view_TextureLayer(JNIEnv* env) {
+int register_android_graphics_TextureLayer(JNIEnv* env) {
     return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
 }