Implement USAGE_IO_INPUT

Change-Id: Id5b9e3d0a17e4df15eec36d542fde6dc626138b2
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index 1c83c51..2f3e48c 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -1246,24 +1246,6 @@
     }
 
     /**
-     *
-     *
-     * @hide
-     *
-     */
-    public SurfaceTexture getSurfaceTexture() {
-        if ((mUsage & USAGE_IO_INPUT) == 0) {
-            throw new RSInvalidStateException("Allocation is not a surface texture.");
-        }
-
-        int id = mRS.nAllocationGetSurfaceTextureID(getID(mRS));
-        SurfaceTexture st = new SurfaceTexture(id);
-        mRS.nAllocationGetSurfaceTextureID2(getID(mRS), st);
-
-        return st;
-    }
-
-    /**
      * For allocations used with io operations, returns the handle
      * onto a raw buffer that is being managed by the screen
      * compositor.
@@ -1272,7 +1254,10 @@
      *
      */
     public Surface getSurface() {
-        return new Surface(getSurfaceTexture());
+        if ((mUsage & USAGE_IO_INPUT) == 0) {
+            throw new RSInvalidStateException("Allocation is not a surface texture.");
+        }
+        return mRS.nAllocationGetSurface(getID(mRS));
     }
 
     /**
@@ -1290,19 +1275,6 @@
     }
 
     /**
-     * @hide
-     */
-    public void setSurfaceTexture(SurfaceTexture st) {
-        mRS.validate();
-        if ((mUsage & USAGE_IO_OUTPUT) == 0) {
-            throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
-        }
-
-        Surface s = new Surface(st);
-        mRS.nAllocationSetSurface(getID(mRS), s);
-    }
-
-    /**
      * Creates a RenderScript allocation from a bitmap.
      *
      * With target API version 18 or greater, this allocation will be
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index 10f4daa..c3fcbb5 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -303,15 +303,10 @@
         validate();
         rsnAllocationSyncAll(mContext, alloc, src);
     }
-    native int rsnAllocationGetSurfaceTextureID(int con, int alloc);
-    synchronized int nAllocationGetSurfaceTextureID(int alloc) {
+    native Surface rsnAllocationGetSurface(int con, int alloc);
+    synchronized Surface nAllocationGetSurface(int alloc) {
         validate();
-        return rsnAllocationGetSurfaceTextureID(mContext, alloc);
-    }
-    native void rsnAllocationGetSurfaceTextureID2(int con, int alloc, SurfaceTexture st);
-    synchronized void nAllocationGetSurfaceTextureID2(int alloc, SurfaceTexture st) {
-        validate();
-        rsnAllocationGetSurfaceTextureID2(mContext, alloc, st);
+        return rsnAllocationGetSurface(mContext, alloc);
     }
     native void rsnAllocationSetSurface(int con, int alloc, Surface sur);
     synchronized void nAllocationSetSurface(int alloc, Surface sur) {
diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp
index 5e631af..5b3758a 100644
--- a/graphics/jni/android_renderscript_RenderScript.cpp
+++ b/graphics/jni/android_renderscript_RenderScript.cpp
@@ -237,23 +237,6 @@
 }
 
 static void
-nContextSetSurfaceTexture(JNIEnv *_env, jobject _this, RsContext con, jint width, jint height, jobject sur)
-{
-    LOG_API("nContextSetSurfaceTexture, con(%p), width(%i), height(%i), surface(%p)", con, width, height, (Surface *)sur);
-
-    sp<ANativeWindow> window;
-    sp<GLConsumer> st;
-    if (sur == 0) {
-
-    } else {
-        st = SurfaceTexture_getSurfaceTexture(_env, sur);
-        window = new Surface(st->getBufferQueue());
-    }
-
-    rsContextSetSurface(con, width, height, window.get());
-}
-
-static void
 nContextDestroy(JNIEnv *_env, jobject _this, RsContext con)
 {
     LOG_API("nContextDestroy, con(%p)", con);
@@ -487,20 +470,17 @@
     rsAllocationSyncAll(con, (RsAllocation)a, (RsAllocationUsageType)bits);
 }
 
-static jint
-nAllocationGetSurfaceTextureID(JNIEnv *_env, jobject _this, RsContext con, jint a)
+static jobject
+nAllocationGetSurface(JNIEnv *_env, jobject _this, RsContext con, jint a)
 {
-    LOG_API("nAllocationGetSurfaceTextureID, con(%p), a(%p)", con, (RsAllocation)a);
-    return rsAllocationGetSurfaceTextureID(con, (RsAllocation)a);
-}
+    LOG_API("nAllocationGetSurface, con(%p), a(%p)", con, (RsAllocation)a);
 
-static void
-nAllocationGetSurfaceTextureID2(JNIEnv *_env, jobject _this, RsContext con, jint a, jobject jst)
-{
-    LOG_API("nAllocationGetSurfaceTextureID2, con(%p), a(%p)", con, (RsAllocation)a);
-    sp<GLConsumer> st = SurfaceTexture_getSurfaceTexture(_env, jst);
+    IGraphicBufferProducer *v = (IGraphicBufferProducer *)rsAllocationGetSurface(con, (RsAllocation)a);
+    sp<IGraphicBufferProducer> bp = v;
+    v->decStrong(NULL);
 
-    rsAllocationGetSurfaceTextureID2(con, (RsAllocation)a, st.get(), sizeof(GLConsumer *));
+    jobject o = android_view_Surface_createFromIGraphicBufferProducer(_env, bp);
+    return o;
 }
 
 static void
@@ -1488,7 +1468,6 @@
 {"rsnContextFinish",                 "(I)V",                                  (void*)nContextFinish },
 {"rsnContextSetPriority",            "(II)V",                                 (void*)nContextSetPriority },
 {"rsnContextSetSurface",             "(IIILandroid/view/Surface;)V",          (void*)nContextSetSurface },
-{"rsnContextSetSurfaceTexture",      "(IIILandroid/graphics/SurfaceTexture;)V", (void*)nContextSetSurfaceTexture },
 {"rsnContextDestroy",                "(I)V",                                  (void*)nContextDestroy },
 {"rsnContextDump",                   "(II)V",                                 (void*)nContextDump },
 {"rsnContextPause",                  "(I)V",                                  (void*)nContextPause },
@@ -1526,8 +1505,7 @@
 {"rsnAllocationCopyToBitmap",        "(IILandroid/graphics/Bitmap;)V",        (void*)nAllocationCopyToBitmap },
 
 {"rsnAllocationSyncAll",             "(III)V",                                (void*)nAllocationSyncAll },
-{"rsnAllocationGetSurfaceTextureID", "(II)I",                                 (void*)nAllocationGetSurfaceTextureID },
-{"rsnAllocationGetSurfaceTextureID2","(IILandroid/graphics/SurfaceTexture;)V",(void*)nAllocationGetSurfaceTextureID2 },
+{"rsnAllocationGetSurface",          "(II)Landroid/view/Surface;",            (void*)nAllocationGetSurface },
 {"rsnAllocationSetSurface",          "(IILandroid/view/Surface;)V",           (void*)nAllocationSetSurface },
 {"rsnAllocationIoSend",              "(II)V",                                 (void*)nAllocationIoSend },
 {"rsnAllocationIoReceive",           "(II)V",                                 (void*)nAllocationIoReceive },
diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
index 0a78908..d2139ea 100644
--- a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
+++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java
@@ -97,7 +97,8 @@
         EXPOSURE ("Exposure"),
         WHITE_BALANCE ("White Balance"),
         COLOR_CUBE ("Color Cube"),
-        COLOR_CUBE_3D_INTRINSIC ("Color Cube (3D LUT intrinsic)");
+        COLOR_CUBE_3D_INTRINSIC ("Color Cube (3D LUT intrinsic)"),
+        USAGE_IO ("Usage io)");
 
 
         private final String name;
@@ -352,6 +353,9 @@
         case COLOR_CUBE_3D_INTRINSIC:
             mTest = new ColorCube(true);
             break;
+        case USAGE_IO:
+            mTest = new UsageIO();
+            break;
         }
 
         mTest.createBaseTest(this, mBitmapIn, mBitmapIn2, mBitmapOut);
diff --git a/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/UsageIO.java b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/UsageIO.java
new file mode 100644
index 0000000..3f86311
--- /dev/null
+++ b/tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/UsageIO.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.rs.image;
+
+import java.lang.Math;
+
+import android.view.Surface;
+import android.renderscript.Allocation;
+import android.renderscript.Element;
+import android.renderscript.RenderScript;
+import android.renderscript.ScriptIntrinsicConvolve3x3;
+import android.renderscript.ScriptIntrinsicColorMatrix;
+import android.renderscript.Type;
+import android.renderscript.Matrix4f;
+import android.renderscript.ScriptGroup;
+import android.util.Log;
+
+public class UsageIO extends TestBase {
+    private ScriptIntrinsicColorMatrix mMatrix;
+
+    private Allocation mScratchPixelsAllocation1;
+    private Allocation mScratchPixelsAllocation2;
+
+    public UsageIO() {
+    }
+
+    public void createTest(android.content.res.Resources res) {
+        mMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS));
+
+        Matrix4f m = new Matrix4f();
+        m.set(1, 0, 0.2f);
+        m.set(1, 1, 0.9f);
+        m.set(1, 2, 0.2f);
+        mMatrix.setColorMatrix(m);
+
+        Type connect = mInPixelsAllocation.getType();
+
+        mScratchPixelsAllocation1 = Allocation.createTyped(mRS, connect, Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT);
+        mScratchPixelsAllocation2 = Allocation.createTyped(mRS, connect, Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT);
+
+        Surface s = mScratchPixelsAllocation2.getSurface();
+        mScratchPixelsAllocation1.setSurface(s);
+    }
+
+    public void runTest() {
+        mScratchPixelsAllocation1.copyFrom(mInPixelsAllocation);
+        mScratchPixelsAllocation1.ioSend();
+        mScratchPixelsAllocation2.ioReceive();
+        mMatrix.forEach(mScratchPixelsAllocation2, mOutPixelsAllocation);
+    }
+
+}