Add support for synchronous get().

bug 8599910

Change-Id: I0e7c52350cc1abb14a5ed59bb92e8e0346209d53
diff --git a/api/current.txt b/api/current.txt
index 7451a93..a878abb 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -19969,6 +19969,35 @@
     method public void reset();
     method public void reset(int);
     method public void skip(int);
+    method public boolean subBoolean();
+    method public android.renderscript.Byte2 subByte2();
+    method public android.renderscript.Byte3 subByte3();
+    method public android.renderscript.Byte4 subByte4();
+    method public android.renderscript.Double2 subDouble2();
+    method public android.renderscript.Double3 subDouble3();
+    method public android.renderscript.Double4 subDouble4();
+    method public float subF32();
+    method public double subF64();
+    method public android.renderscript.Float2 subFloat2();
+    method public android.renderscript.Float3 subFloat3();
+    method public android.renderscript.Float4 subFloat4();
+    method public short subI16();
+    method public int subI32();
+    method public long subI64();
+    method public byte subI8();
+    method public android.renderscript.Int2 subInt2();
+    method public android.renderscript.Int3 subInt3();
+    method public android.renderscript.Int4 subInt4();
+    method public android.renderscript.Long2 subLong2();
+    method public android.renderscript.Long3 subLong3();
+    method public android.renderscript.Long4 subLong4();
+    method public android.renderscript.Matrix2f subMatrix2f();
+    method public android.renderscript.Matrix3f subMatrix3f();
+    method public android.renderscript.Matrix4f subMatrix4f();
+    method public android.renderscript.Short2 subShort2();
+    method public android.renderscript.Short3 subShort3();
+    method public android.renderscript.Short4 subShort4();
+    method public void subalign(int);
   }
 
   public class Float2 {
@@ -20213,6 +20242,12 @@
     method protected android.renderscript.Script.KernelID createKernelID(int, int, android.renderscript.Element, android.renderscript.Element);
     method protected void forEach(int, android.renderscript.Allocation, android.renderscript.Allocation, android.renderscript.FieldPacker);
     method protected void forEach(int, android.renderscript.Allocation, android.renderscript.Allocation, android.renderscript.FieldPacker, android.renderscript.Script.LaunchOptions);
+    method public boolean getVarB(int);
+    method public double getVarD(int);
+    method public float getVarF(int);
+    method public int getVarI(int);
+    method public long getVarJ(int);
+    method public void getVarV(int, android.renderscript.FieldPacker);
     method protected void invoke(int);
     method protected void invoke(int, android.renderscript.FieldPacker);
     method public void setTimeZone(java.lang.String);
diff --git a/graphics/java/android/renderscript/Allocation.java b/graphics/java/android/renderscript/Allocation.java
index 5751331..051b03f 100644
--- a/graphics/java/android/renderscript/Allocation.java
+++ b/graphics/java/android/renderscript/Allocation.java
@@ -273,14 +273,14 @@
                 throw new RSIllegalArgumentException("Invalid usage combination.");
             }
         }
-
-        // don't need to account for USAGE_SHARED Allocations
-        if ((usage & USAGE_SHARED) == 0) {
-            int numBytes = t.getCount() * t.getElement().getBytesSize();
-            rs.addAllocSizeForGC(numBytes);
-            mGCSize = numBytes;
+        if (t != null) {
+            // don't need to account for USAGE_SHARED Allocations
+            if ((usage & USAGE_SHARED) == 0) {
+                int numBytes = t.getCount() * t.getElement().getBytesSize();
+                rs.addAllocSizeForGC(numBytes);
+                mGCSize = numBytes;
+            }
         }
-
         mType = t;
         mUsage = usage;
 
@@ -351,6 +351,12 @@
             mType.updateFromNative();
             updateCacheInfo(mType);
         }
+        // don't need to account for USAGE_SHARED Allocations
+        if ((mUsage & USAGE_SHARED) == 0) {
+            int numBytes = mType.getCount() * mType.getElement().getBytesSize();
+            mRS.addAllocSizeForGC(numBytes);
+            mGCSize = numBytes;
+        }
     }
 
     /**
diff --git a/graphics/java/android/renderscript/FieldPacker.java b/graphics/java/android/renderscript/FieldPacker.java
index 0a7e882..decd0c7 100644
--- a/graphics/java/android/renderscript/FieldPacker.java
+++ b/graphics/java/android/renderscript/FieldPacker.java
@@ -16,6 +16,8 @@
 
 package android.renderscript;
 
+import android.util.Log;
+import java.util.BitSet;
 
 /**
  * Utility class for packing arguments and structures from Android system objects to
@@ -27,12 +29,14 @@
         mPos = 0;
         mLen = len;
         mData = new byte[len];
+        mAlignment = new BitSet();
     }
 
     public FieldPacker(byte[] data) {
         mPos = 0;
         mLen = data.length;
         mData = data;
+        mAlignment = new BitSet();
     }
 
     public void align(int v) {
@@ -41,10 +45,29 @@
         }
 
         while ((mPos & (v - 1)) != 0) {
+            mAlignment.flip(mPos);
             mData[mPos++] = 0;
         }
     }
 
+    public void subalign(int v) {
+        if ((v & (v - 1)) != 0) {
+            throw new RSIllegalArgumentException("argument must be a non-negative non-zero power of 2: " + v);
+        }
+
+        while ((mPos & (v - 1)) != 0) {
+            mPos--;
+        }
+
+        if (mPos > 0) {
+            while (mAlignment.get(mPos - 1) == true) {
+                mPos--;
+                mAlignment.flip(mPos);
+            }
+        }
+
+    }
+
     public void reset() {
         mPos = 0;
     }
@@ -67,12 +90,26 @@
         mData[mPos++] = v;
     }
 
+    public byte subI8() {
+        subalign(1);
+        return mData[--mPos];
+    }
+
     public void addI16(short v) {
         align(2);
         mData[mPos++] = (byte)(v & 0xff);
         mData[mPos++] = (byte)(v >> 8);
     }
 
+    public short subI16() {
+        subalign(2);
+        short v = 0;
+        v = (short)((mData[--mPos] & 0xff) << 8);
+        v = (short)(v | (short)(mData[--mPos] & 0xff));
+        return v;
+    }
+
+
     public void addI32(int v) {
         align(4);
         mData[mPos++] = (byte)(v & 0xff);
@@ -81,6 +118,17 @@
         mData[mPos++] = (byte)((v >> 24) & 0xff);
     }
 
+    public int subI32() {
+        subalign(4);
+        int v = 0;
+        v = ((mData[--mPos] & 0xff) << 24);
+        v = v | ((mData[--mPos] & 0xff) << 16);
+        v = v | ((mData[--mPos] & 0xff) << 8);
+        v = v | ((mData[--mPos] & 0xff));
+        return v;
+    }
+
+
     public void addI64(long v) {
         align(8);
         mData[mPos++] = (byte)(v & 0xff);
@@ -93,6 +141,29 @@
         mData[mPos++] = (byte)((v >> 56) & 0xff);
     }
 
+    public long subI64() {
+        subalign(8);
+        long v = 0;
+        byte x = 0;
+        x = ((mData[--mPos]));
+        v = (long)(v | (((long)x) & 0xff) << 56l);
+        x = ((mData[--mPos]));
+        v = (long)(v | (((long)x) & 0xff) << 48l);
+        x = ((mData[--mPos]));
+        v = (long)(v | (((long)x) & 0xff) << 40l);
+        x = ((mData[--mPos]));
+        v = (long)(v | (((long)x) & 0xff) << 32l);
+        x = ((mData[--mPos]));
+        v = (long)(v | (((long)x) & 0xff) << 24l);
+        x = ((mData[--mPos]));
+        v = (long)(v | (((long)x) & 0xff) << 16l);
+        x = ((mData[--mPos]));
+        v = (long)(v | (((long)x) & 0xff) << 8l);
+        x = ((mData[--mPos]));
+        v = (long)(v | (((long)x) & 0xff));
+        return v;
+    }
+
     public void addU8(short v) {
         if ((v < 0) || (v > 0xff)) {
             android.util.Log.e("rs", "FieldPacker.addU8( " + v + " )");
@@ -143,10 +214,18 @@
         addI32(Float.floatToRawIntBits(v));
     }
 
+    public float subF32() {
+        return Float.intBitsToFloat(subI32());
+    }
+
     public void addF64(double v) {
         addI64(Double.doubleToRawLongBits(v));
     }
 
+    public double subF64() {
+        return Double.longBitsToDouble(subI64());
+    }
+
     public void addObj(BaseObj obj) {
         if (obj != null) {
             addI32(obj.getID(null));
@@ -315,28 +394,195 @@
         addU64(v.w);
     }
 
+
+    public Float2 subFloat2() {
+        Float2 v = new Float2();
+        v.y = subF32();
+        v.x = subF32();
+        return v;
+    }
+    public Float3 subFloat3() {
+        Float3 v = new Float3();
+        v.z = subF32();
+        v.y = subF32();
+        v.x = subF32();
+        return v;
+    }
+    public Float4 subFloat4() {
+        Float4 v = new Float4();
+        v.w = subF32();
+        v.z = subF32();
+        v.y = subF32();
+        v.x = subF32();
+        return v;
+    }
+
+    public Double2 subDouble2() {
+        Double2 v = new Double2();
+        v.y = subF64();
+        v.x = subF64();
+        return v;
+    }
+    public Double3 subDouble3() {
+        Double3 v = new Double3();
+        v.z = subF64();
+        v.y = subF64();
+        v.x = subF64();
+        return v;
+    }
+    public Double4 subDouble4() {
+        Double4 v = new Double4();
+        v.w = subF64();
+        v.z = subF64();
+        v.y = subF64();
+        v.x = subF64();
+        return v;
+    }
+
+    public Byte2 subByte2() {
+        Byte2 v = new Byte2();
+        v.y = subI8();
+        v.x = subI8();
+        return v;
+    }
+    public Byte3 subByte3() {
+        Byte3 v = new Byte3();
+        v.z = subI8();
+        v.y = subI8();
+        v.x = subI8();
+        return v;
+    }
+    public Byte4 subByte4() {
+        Byte4 v = new Byte4();
+        v.w = subI8();
+        v.z = subI8();
+        v.y = subI8();
+        v.x = subI8();
+        return v;
+    }
+
+    public Short2 subShort2() {
+        Short2 v = new Short2();
+        v.y = subI16();
+        v.x = subI16();
+        return v;
+    }
+    public Short3 subShort3() {
+        Short3 v = new Short3();
+        v.z = subI16();
+        v.y = subI16();
+        v.x = subI16();
+        return v;
+    }
+    public Short4 subShort4() {
+        Short4 v = new Short4();
+        v.w = subI16();
+        v.z = subI16();
+        v.y = subI16();
+        v.x = subI16();
+        return v;
+    }
+
+    public Int2 subInt2() {
+        Int2 v = new Int2();
+        v.y = subI32();
+        v.x = subI32();
+        return v;
+    }
+    public Int3 subInt3() {
+        Int3 v = new Int3();
+        v.z = subI32();
+        v.y = subI32();
+        v.x = subI32();
+        return v;
+    }
+    public Int4 subInt4() {
+        Int4 v = new Int4();
+        v.w = subI32();
+        v.z = subI32();
+        v.y = subI32();
+        v.x = subI32();
+        return v;
+    }
+
+    public Long2 subLong2() {
+        Long2 v = new Long2();
+        v.y = subI64();
+        v.x = subI64();
+        return v;
+    }
+    public Long3 subLong3() {
+        Long3 v = new Long3();
+        v.z = subI64();
+        v.y = subI64();
+        v.x = subI64();
+        return v;
+    }
+    public Long4 subLong4() {
+        Long4 v = new Long4();
+        v.w = subI64();
+        v.z = subI64();
+        v.y = subI64();
+        v.x = subI64();
+        return v;
+    }
+
+
+
     public void addMatrix(Matrix4f v) {
         for (int i=0; i < v.mMat.length; i++) {
             addF32(v.mMat[i]);
         }
     }
 
+    public Matrix4f subMatrix4f() {
+        Matrix4f v = new Matrix4f();
+        for (int i = v.mMat.length - 1; i >= 0; i--) {
+            v.mMat[i] = subF32();
+        }
+        return v;
+    }
+
     public void addMatrix(Matrix3f v) {
         for (int i=0; i < v.mMat.length; i++) {
             addF32(v.mMat[i]);
         }
     }
 
+    public Matrix3f subMatrix3f() {
+        Matrix3f v = new Matrix3f();
+        for (int i = v.mMat.length - 1; i >= 0; i--) {
+            v.mMat[i] = subF32();
+        }
+        return v;
+    }
+
     public void addMatrix(Matrix2f v) {
         for (int i=0; i < v.mMat.length; i++) {
             addF32(v.mMat[i]);
         }
     }
 
+    public Matrix2f subMatrix2f() {
+        Matrix2f v = new Matrix2f();
+        for (int i = v.mMat.length - 1; i >= 0; i--) {
+            v.mMat[i] = subF32();
+        }
+        return v;
+    }
+
     public void addBoolean(boolean v) {
         addI8((byte)(v ? 1 : 0));
     }
 
+    public boolean subBoolean() {
+        byte v = subI8();
+        if (v == 1) {
+            return true;
+        }
+        return false;
+    }
+
     public final byte[] getData() {
         return mData;
     }
@@ -344,6 +590,7 @@
     private final byte mData[];
     private int mPos;
     private int mLen;
+    private BitSet mAlignment;
 
 }
 
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index d5af276..7518689 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -580,31 +580,59 @@
         validate();
         rsnScriptInvokeV(mContext, id, slot, params);
     }
+
     native void rsnScriptSetVarI(int con, int id, int slot, int val);
     synchronized void nScriptSetVarI(int id, int slot, int val) {
         validate();
         rsnScriptSetVarI(mContext, id, slot, val);
     }
+    native int rsnScriptGetVarI(int con, int id, int slot);
+    synchronized int nScriptGetVarI(int id, int slot) {
+        validate();
+        return rsnScriptGetVarI(mContext, id, slot);
+    }
+
     native void rsnScriptSetVarJ(int con, int id, int slot, long val);
     synchronized void nScriptSetVarJ(int id, int slot, long val) {
         validate();
         rsnScriptSetVarJ(mContext, id, slot, val);
     }
+    native long rsnScriptGetVarJ(int con, int id, int slot);
+    synchronized long nScriptGetVarJ(int id, int slot) {
+        validate();
+        return rsnScriptGetVarJ(mContext, id, slot);
+    }
+
     native void rsnScriptSetVarF(int con, int id, int slot, float val);
     synchronized void nScriptSetVarF(int id, int slot, float val) {
         validate();
         rsnScriptSetVarF(mContext, id, slot, val);
     }
+    native float rsnScriptGetVarF(int con, int id, int slot);
+    synchronized float nScriptGetVarF(int id, int slot) {
+        validate();
+        return rsnScriptGetVarF(mContext, id, slot);
+    }
     native void rsnScriptSetVarD(int con, int id, int slot, double val);
     synchronized void nScriptSetVarD(int id, int slot, double val) {
         validate();
         rsnScriptSetVarD(mContext, id, slot, val);
     }
+    native double rsnScriptGetVarD(int con, int id, int slot);
+    synchronized double nScriptGetVarD(int id, int slot) {
+        validate();
+        return rsnScriptGetVarD(mContext, id, slot);
+    }
     native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
     synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
         validate();
         rsnScriptSetVarV(mContext, id, slot, val);
     }
+    native void rsnScriptGetVarV(int con, int id, int slot, byte[] val);
+    synchronized void nScriptGetVarV(int id, int slot, byte[] val) {
+        validate();
+        rsnScriptGetVarV(mContext, id, slot, val);
+    }
     native void rsnScriptSetVarVE(int con, int id, int slot, byte[] val,
                                   int e, int[] dims);
     synchronized void nScriptSetVarVE(int id, int slot, byte[] val,
diff --git a/graphics/java/android/renderscript/Script.java b/graphics/java/android/renderscript/Script.java
index b405588..b4ba943 100644
--- a/graphics/java/android/renderscript/Script.java
+++ b/graphics/java/android/renderscript/Script.java
@@ -220,6 +220,9 @@
     public void setVar(int index, float v) {
         mRS.nScriptSetVarF(getID(mRS), index, v);
     }
+    public float getVarF(int index) {
+        return mRS.nScriptGetVarF(getID(mRS), index);
+    }
 
     /**
      * Only intended for use by generated reflected code.
@@ -230,6 +233,9 @@
     public void setVar(int index, double v) {
         mRS.nScriptSetVarD(getID(mRS), index, v);
     }
+    public double getVarD(int index) {
+        return mRS.nScriptGetVarD(getID(mRS), index);
+    }
 
     /**
      * Only intended for use by generated reflected code.
@@ -240,6 +246,10 @@
     public void setVar(int index, int v) {
         mRS.nScriptSetVarI(getID(mRS), index, v);
     }
+    public int getVarI(int index) {
+        return mRS.nScriptGetVarI(getID(mRS), index);
+    }
+
 
     /**
      * Only intended for use by generated reflected code.
@@ -250,6 +260,10 @@
     public void setVar(int index, long v) {
         mRS.nScriptSetVarJ(getID(mRS), index, v);
     }
+    public long getVarJ(int index) {
+        return mRS.nScriptGetVarJ(getID(mRS), index);
+    }
+
 
     /**
      * Only intended for use by generated reflected code.
@@ -260,6 +274,9 @@
     public void setVar(int index, boolean v) {
         mRS.nScriptSetVarI(getID(mRS), index, v ? 1 : 0);
     }
+    public boolean getVarB(int index) {
+        return mRS.nScriptGetVarI(getID(mRS), index) > 0 ? true : false;
+    }
 
     /**
      * Only intended for use by generated reflected code.
@@ -293,6 +310,10 @@
         mRS.nScriptSetVarVE(getID(mRS), index, v.getData(), e.getID(mRS), dims);
     }
 
+    public void getVarV(int index, FieldPacker v) {
+        mRS.nScriptGetVarV(getID(mRS), index, v.getData());
+    }
+
     public void setTimeZone(String timeZone) {
         mRS.validate();
         try {
diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp
index 460a516..b9f8713 100644
--- a/graphics/jni/android_renderscript_RenderScript.cpp
+++ b/graphics/jni/android_renderscript_RenderScript.cpp
@@ -987,6 +987,15 @@
     rsScriptSetVarI(con, (RsScript)script, slot, val);
 }
 
+static jint
+nScriptGetVarI(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot)
+{
+    LOG_API("nScriptGetVarI, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
+    int value = 0;
+    rsScriptGetVarV(con, (RsScript)script, slot, &value, sizeof(value));
+    return value;
+}
+
 static void
 nScriptSetVarObj(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jint val)
 {
@@ -1001,6 +1010,15 @@
     rsScriptSetVarJ(con, (RsScript)script, slot, val);
 }
 
+static jlong
+nScriptGetVarJ(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot)
+{
+    LOG_API("nScriptGetVarJ, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
+    jlong value = 0;
+    rsScriptGetVarV(con, (RsScript)script, slot, &value, sizeof(value));
+    return value;
+}
+
 static void
 nScriptSetVarF(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, float val)
 {
@@ -1008,6 +1026,15 @@
     rsScriptSetVarF(con, (RsScript)script, slot, val);
 }
 
+static jfloat
+nScriptGetVarF(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot)
+{
+    LOG_API("nScriptGetVarF, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
+    jfloat value = 0;
+    rsScriptGetVarV(con, (RsScript)script, slot, &value, sizeof(value));
+    return value;
+}
+
 static void
 nScriptSetVarD(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, double val)
 {
@@ -1015,6 +1042,15 @@
     rsScriptSetVarD(con, (RsScript)script, slot, val);
 }
 
+static jdouble
+nScriptGetVarD(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot)
+{
+    LOG_API("nScriptGetVarD, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
+    jdouble value = 0;
+    rsScriptGetVarV(con, (RsScript)script, slot, &value, sizeof(value));
+    return value;
+}
+
 static void
 nScriptSetVarV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
 {
@@ -1026,6 +1062,16 @@
 }
 
 static void
+nScriptGetVarV(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data)
+{
+    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
+    jint len = _env->GetArrayLength(data);
+    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
+    rsScriptGetVarV(con, (RsScript)script, slot, ptr, len);
+    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
+}
+
+static void
 nScriptSetVarVE(JNIEnv *_env, jobject _this, RsContext con, jint script, jint slot, jbyteArray data, jint elem, jintArray dims)
 {
     LOG_API("nScriptSetVarVE, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
@@ -1600,10 +1646,15 @@
 {"rsnScriptForEachClipped",          "(IIIIIIIIIII)V",                        (void*)nScriptForEachClipped },
 {"rsnScriptForEachClipped",          "(IIIII[BIIIIII)V",                      (void*)nScriptForEachClippedV },
 {"rsnScriptSetVarI",                 "(IIII)V",                               (void*)nScriptSetVarI },
+{"rsnScriptGetVarI",                 "(III)I",                                (void*)nScriptGetVarI },
 {"rsnScriptSetVarJ",                 "(IIIJ)V",                               (void*)nScriptSetVarJ },
+{"rsnScriptGetVarJ",                 "(III)J",                                (void*)nScriptGetVarJ },
 {"rsnScriptSetVarF",                 "(IIIF)V",                               (void*)nScriptSetVarF },
+{"rsnScriptGetVarF",                 "(III)F",                                (void*)nScriptGetVarF },
 {"rsnScriptSetVarD",                 "(IIID)V",                               (void*)nScriptSetVarD },
+{"rsnScriptGetVarD",                 "(III)D",                                (void*)nScriptGetVarD },
 {"rsnScriptSetVarV",                 "(III[B)V",                              (void*)nScriptSetVarV },
+{"rsnScriptGetVarV",                 "(III[B)V",                              (void*)nScriptGetVarV },
 {"rsnScriptSetVarVE",                "(III[BI[I)V",                           (void*)nScriptSetVarVE },
 {"rsnScriptSetVarObj",               "(IIII)V",                               (void*)nScriptSetVarObj },