summaryrefslogtreecommitdiffstats
path: root/rs/java/android
diff options
context:
space:
mode:
Diffstat (limited to 'rs/java/android')
-rw-r--r--rs/java/android/renderscript/Allocation.java9
-rw-r--r--rs/java/android/renderscript/Element.java3
-rw-r--r--rs/java/android/renderscript/FileA3D.java3
-rw-r--r--rs/java/android/renderscript/Mesh.java10
-rw-r--r--rs/java/android/renderscript/RenderScript.java146
-rw-r--r--rs/java/android/renderscript/Script.java11
-rw-r--r--rs/java/android/renderscript/ScriptGroup2.java652
7 files changed, 521 insertions, 313 deletions
diff --git a/rs/java/android/renderscript/Allocation.java b/rs/java/android/renderscript/Allocation.java
index c6afa2c..523c8fb 100644
--- a/rs/java/android/renderscript/Allocation.java
+++ b/rs/java/android/renderscript/Allocation.java
@@ -76,6 +76,8 @@ public class Allocation extends BaseObj {
new HashMap<Long, Allocation>();
OnBufferAvailableListener mBufferNotifier;
+ private Surface mGetSurfaceSurface = null;
+
private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
final Class c = d.getClass();
if (!c.isArray()) {
@@ -1990,7 +1992,12 @@ public class Allocation extends BaseObj {
if ((mUsage & USAGE_IO_INPUT) == 0) {
throw new RSInvalidStateException("Allocation is not a surface texture.");
}
- return mRS.nAllocationGetSurface(getID(mRS));
+
+ if (mGetSurfaceSurface == null) {
+ mGetSurfaceSurface = mRS.nAllocationGetSurface(getID(mRS));
+ }
+
+ return mGetSurfaceSurface;
}
/**
diff --git a/rs/java/android/renderscript/Element.java b/rs/java/android/renderscript/Element.java
index 287b3f1..60ff996 100644
--- a/rs/java/android/renderscript/Element.java
+++ b/rs/java/android/renderscript/Element.java
@@ -114,7 +114,8 @@ public class Element extends BaseObj {
* MATRIX the three matrix types contain FLOAT_32 elements and are treated
* as 32 bits for alignment purposes.
*
- * RS_* objects. 32 bit opaque handles.
+ * RS_* objects: opaque handles with implementation dependent
+ * sizes.
*/
public enum DataType {
NONE (0, 0),
diff --git a/rs/java/android/renderscript/FileA3D.java b/rs/java/android/renderscript/FileA3D.java
index 4164810..9d8f162 100644
--- a/rs/java/android/renderscript/FileA3D.java
+++ b/rs/java/android/renderscript/FileA3D.java
@@ -145,6 +145,9 @@ public class FileA3D extends BaseObj {
case MESH:
entry.mLoadedObj = new Mesh(objectID, rs);
break;
+
+ default:
+ throw new RSRuntimeException("Unrecognized object type in file.");
}
entry.mLoadedObj.updateFromNative();
diff --git a/rs/java/android/renderscript/Mesh.java b/rs/java/android/renderscript/Mesh.java
index 1a5dc9e..13c8e1c 100644
--- a/rs/java/android/renderscript/Mesh.java
+++ b/rs/java/android/renderscript/Mesh.java
@@ -363,6 +363,9 @@ public class Mesh extends BaseObj {
alloc = Allocation.createTyped(mRS, entry.t, mUsage);
} else if(entry.e != null) {
alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
+ } else {
+ // Should never happen because the builder will always set one
+ throw new IllegalStateException("Builder corrupt, no valid element in entry.");
}
vertexBuffers[ct] = alloc;
vtx[ct] = alloc.getID(mRS);
@@ -375,6 +378,9 @@ public class Mesh extends BaseObj {
alloc = Allocation.createTyped(mRS, entry.t, mUsage);
} else if(entry.e != null) {
alloc = Allocation.createSized(mRS, entry.e, entry.size, mUsage);
+ } else {
+ // Should never happen because the builder will always set one
+ throw new IllegalStateException("Builder corrupt, no valid element in entry.");
}
long allocID = (alloc == null) ? 0 : alloc.getID(mRS);
indexBuffers[ct] = alloc;
@@ -811,9 +817,7 @@ public class Mesh extends BaseObj {
sm.getVertexAllocation(0).copy1DRangeFromUnchecked(0, mMaxIndex, mVtxData);
if(uploadToBufferObject) {
- if (uploadToBufferObject) {
- sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
- }
+ sm.getVertexAllocation(0).syncAll(Allocation.USAGE_SCRIPT);
}
sm.getIndexSetAllocation(0).copy1DRangeFromUnchecked(0, mIndexCount, mIndexData);
diff --git a/rs/java/android/renderscript/RenderScript.java b/rs/java/android/renderscript/RenderScript.java
index f08c985..45f0ca6 100644
--- a/rs/java/android/renderscript/RenderScript.java
+++ b/rs/java/android/renderscript/RenderScript.java
@@ -29,6 +29,7 @@ import android.util.Log;
import android.view.Surface;
import android.os.SystemProperties;
import android.os.Trace;
+import java.util.ArrayList;
/**
* This class provides access to a RenderScript context, which controls RenderScript
@@ -49,6 +50,12 @@ public class RenderScript {
@SuppressWarnings({"UnusedDeclaration", "deprecation"})
static final boolean LOG_ENABLED = false;
+ static private ArrayList<RenderScript> mProcessContextList = new ArrayList<RenderScript>();
+ private boolean mIsProcessContext = false;
+ private int mContextFlags = 0;
+ private int mContextSdkVersion = 0;
+
+
private Context mApplicationContext;
/*
@@ -1313,20 +1320,13 @@ public class RenderScript {
}
/**
- * @hide
- */
- public static RenderScript create(Context ctx, int sdkVersion) {
- return create(ctx, sdkVersion, ContextType.NORMAL, CREATE_FLAG_NONE);
- }
-
- /**
* Create a RenderScript context.
*
* @hide
* @param ctx The context.
* @return RenderScript
*/
- public static RenderScript create(Context ctx, int sdkVersion, ContextType ct, int flags) {
+ private static RenderScript internalCreate(Context ctx, int sdkVersion, ContextType ct, int flags) {
if (!sInitialized) {
Log.e(LOG_TAG, "RenderScript.create() called when disabled; someone is likely to crash");
return null;
@@ -1341,6 +1341,8 @@ public class RenderScript {
rs.mDev = rs.nDeviceCreate();
rs.mContext = rs.nContextCreate(rs.mDev, flags, sdkVersion, ct.mID);
rs.mContextType = ct;
+ rs.mContextFlags = flags;
+ rs.mContextSdkVersion = sdkVersion;
if (rs.mContext == 0) {
throw new RSDriverException("Failed to create RS context.");
}
@@ -1350,7 +1352,9 @@ public class RenderScript {
}
/**
- * Create a RenderScript context.
+ * calls create(ctx, ContextType.NORMAL, CREATE_FLAG_NONE)
+ *
+ * See documentation for @create for details
*
* @param ctx The context.
* @return RenderScript
@@ -1360,21 +1364,33 @@ public class RenderScript {
}
/**
- * Create a RenderScript context.
+ * calls create(ctx, ct, CREATE_FLAG_NONE)
*
+ * See documentation for @create for details
*
* @param ctx The context.
* @param ct The type of context to be created.
* @return RenderScript
*/
public static RenderScript create(Context ctx, ContextType ct) {
- int v = ctx.getApplicationInfo().targetSdkVersion;
- return create(ctx, v, ct, CREATE_FLAG_NONE);
+ return create(ctx, ct, CREATE_FLAG_NONE);
}
- /**
- * Create a RenderScript context.
+
+ /**
+ * Gets or creates a RenderScript context of the specified type.
+ *
+ * The returned context will be cached for future reuse within
+ * the process. When an application is finished using
+ * RenderScript it should call releaseAllContexts()
+ *
+ * A process context is a context designed for easy creation and
+ * lifecycle management. Multiple calls to this function will
+ * return the same object provided they are called with the same
+ * options. This allows it to be used any time a RenderScript
+ * context is needed.
*
+ * Prior to API 23 this always created a new context.
*
* @param ctx The context.
* @param ct The type of context to be created.
@@ -1387,6 +1403,100 @@ public class RenderScript {
}
/**
+ * calls create(ctx, sdkVersion, ContextType.NORMAL, CREATE_FLAG_NONE)
+ *
+ * Used by the RenderScriptThunker to maintain backward compatibility.
+ *
+ * @hide
+ * @param ctx The context.
+ * @param sdkVersion The target SDK Version.
+ * @return RenderScript
+ */
+ public static RenderScript create(Context ctx, int sdkVersion) {
+ return create(ctx, sdkVersion, ContextType.NORMAL, CREATE_FLAG_NONE);
+ }
+
+ /**
+ * Gets or creates a RenderScript context of the specified type.
+ *
+ * @hide
+ * @param ctx The context.
+ * @param ct The type of context to be created.
+ * @param sdkVersion The target SDK Version.
+ * @param flags The OR of the CREATE_FLAG_* options desired
+ * @return RenderScript
+ */
+ public static RenderScript create(Context ctx, int sdkVersion, ContextType ct, int flags) {
+ if (sdkVersion < 23) {
+ return internalCreate(ctx, sdkVersion, ct, flags);
+ }
+
+ synchronized (mProcessContextList) {
+ for (RenderScript prs : mProcessContextList) {
+ if ((prs.mContextType == ct) &&
+ (prs.mContextFlags == flags) &&
+ (prs.mContextSdkVersion == sdkVersion)) {
+
+ return prs;
+ }
+ }
+
+ RenderScript prs = internalCreate(ctx, sdkVersion, ct, flags);
+ prs.mIsProcessContext = true;
+ mProcessContextList.add(prs);
+ return prs;
+ }
+ }
+
+ /**
+ * @hide
+ *
+ * Releases all the process contexts. This is the same as
+ * calling .destroy() on each unique context retreived with
+ * create(...). If no contexts have been created this
+ * function does nothing.
+ *
+ * Typically you call this when your application is losing focus
+ * and will not be using a context for some time.
+ *
+ * This has no effect on a context created with
+ * createMultiContext()
+ */
+ public static void releaseAllContexts() {
+ ArrayList<RenderScript> oldList;
+ synchronized (mProcessContextList) {
+ oldList = mProcessContextList;
+ mProcessContextList = new ArrayList<RenderScript>();
+ }
+
+ for (RenderScript prs : oldList) {
+ prs.mIsProcessContext = false;
+ prs.destroy();
+ }
+ oldList.clear();
+ }
+
+
+
+ /**
+ * Create a RenderScript context.
+ *
+ * This is an advanced function intended for applications which
+ * need to create more than one RenderScript context to be used
+ * at the same time.
+ *
+ * If you need a single context please use create()
+ *
+ * @hide
+ * @param ctx The context.
+ * @return RenderScript
+ */
+ public static RenderScript createMultiContext(Context ctx, ContextType ct, int flags, int API_number) {
+ return internalCreate(ctx, API_number, ct, flags);
+ }
+
+
+ /**
* Print the currently available debugging information about the state of
* the RS context to the log.
*
@@ -1442,8 +1552,16 @@ public class RenderScript {
* using this context or any objects belonging to this context is
* illegal.
*
+ * API 23+, this function is a NOP if the context was created
+ * with create(). Please use releaseAllContexts() to clean up
+ * contexts created with the create function.
+ *
*/
public void destroy() {
+ if (mIsProcessContext) {
+ // users cannot destroy a process context
+ return;
+ }
validate();
helpDestroy();
}
diff --git a/rs/java/android/renderscript/Script.java b/rs/java/android/renderscript/Script.java
index 83aeedd..65056ac 100644
--- a/rs/java/android/renderscript/Script.java
+++ b/rs/java/android/renderscript/Script.java
@@ -251,9 +251,14 @@ public class Script extends BaseObj {
"At least one of ain or aout is required to be non-null.");
}
- long[] in_ids = new long[ains.length];
- for (int index = 0; index < ains.length; ++index) {
- in_ids[index] = ains[index].getID(mRS);
+ long[] in_ids;
+ if (ains != null) {
+ in_ids = new long[ains.length];
+ for (int index = 0; index < ains.length; ++index) {
+ in_ids[index] = ains[index].getID(mRS);
+ }
+ } else {
+ in_ids = null;
}
long out_id = 0;
diff --git a/rs/java/android/renderscript/ScriptGroup2.java b/rs/java/android/renderscript/ScriptGroup2.java
index 113b896..4a56572 100644
--- a/rs/java/android/renderscript/ScriptGroup2.java
+++ b/rs/java/android/renderscript/ScriptGroup2.java
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2015 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 android.renderscript;
import android.util.Log;
@@ -13,356 +29,410 @@ import java.util.Map;
You have tried to change the API from what has been previously approved.
To make these errors go away, you have two choices:
- 1) You can add "@hide" javadoc comments to the methods, etc. listed in the
- errors above.
+1) You can add "@hide" javadoc comments to the methods, etc. listed in the
+errors above.
- 2) You can update current.txt by executing the following command:
- make update-api
+2) You can update current.txt by executing the following command:
+make update-api
To submit the revised current.txt to the main Android repository,
you will need approval.
******************************
- @hide Pending Android public API approval.
- */
+@hide Pending Android public API approval.
+*/
public class ScriptGroup2 extends BaseObj {
- public static class Closure extends BaseObj {
- private Allocation mReturnValue;
- private Map<Script.FieldID, Object> mBindings;
+ public static class Closure extends BaseObj {
+ private Allocation mReturnValue;
+ private Map<Script.FieldID, Object> mBindings;
- private Future mReturnFuture;
- private Map<Script.FieldID, Future> mGlobalFuture;
+ private Future mReturnFuture;
+ private Map<Script.FieldID, Future> mGlobalFuture;
- private FieldPacker mFP;
+ private FieldPacker mFP;
- private static final String TAG = "Closure";
+ private static final String TAG = "Closure";
- public Closure(long id, RenderScript rs) {
- super(id, rs);
- }
+ public Closure(long id, RenderScript rs) {
+ super(id, rs);
+ }
- public Closure(RenderScript rs, Script.KernelID kernelID, Type returnType,
- Object[] args, Map<Script.FieldID, Object> globals) {
- super(0, rs);
-
- mReturnValue = Allocation.createTyped(rs, returnType);
- mBindings = new HashMap<Script.FieldID, Object>();
- mGlobalFuture = new HashMap<Script.FieldID, Future>();
-
- int numValues = args.length + globals.size();
-
- long[] fieldIDs = new long[numValues];
- long[] values = new long[numValues];
- int[] sizes = new int[numValues];
- long[] depClosures = new long[numValues];
- long[] depFieldIDs = new long[numValues];
-
- int i;
- for (i = 0; i < args.length; i++) {
- Object obj = args[i];
- fieldIDs[i] = 0;
- if (obj instanceof UnboundValue) {
- UnboundValue unbound = (UnboundValue)obj;
- unbound.addReference(this, i);
- } else {
- retrieveValueAndDependenceInfo(rs, i, args[i], values, sizes,
- depClosures, depFieldIDs);
+ public Closure(RenderScript rs, Script.KernelID kernelID, Type returnType,
+ Object[] args, Map<Script.FieldID, Object> globals) {
+ super(0, rs);
+
+ mReturnValue = Allocation.createTyped(rs, returnType);
+ mBindings = new HashMap<Script.FieldID, Object>();
+ mGlobalFuture = new HashMap<Script.FieldID, Future>();
+
+ int numValues = args.length + globals.size();
+
+ long[] fieldIDs = new long[numValues];
+ long[] values = new long[numValues];
+ int[] sizes = new int[numValues];
+ long[] depClosures = new long[numValues];
+ long[] depFieldIDs = new long[numValues];
+
+ int i;
+ for (i = 0; i < args.length; i++) {
+ Object obj = args[i];
+ fieldIDs[i] = 0;
+ if (obj instanceof UnboundValue) {
+ UnboundValue unbound = (UnboundValue)obj;
+ unbound.addReference(this, i);
+ } else {
+ retrieveValueAndDependenceInfo(rs, i, args[i], values, sizes,
+ depClosures, depFieldIDs);
+ }
+ }
+
+ for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) {
+ Object obj = entry.getValue();
+ Script.FieldID fieldID = entry.getKey();
+ fieldIDs[i] = fieldID.getID(rs);
+ if (obj instanceof UnboundValue) {
+ UnboundValue unbound = (UnboundValue)obj;
+ unbound.addReference(this, fieldID);
+ } else {
+ retrieveValueAndDependenceInfo(rs, i, obj, values,
+ sizes, depClosures, depFieldIDs);
+ }
+ i++;
+ }
+
+ long id = rs.nClosureCreate(kernelID.getID(rs), mReturnValue.getID(rs),
+ fieldIDs, values, sizes, depClosures, depFieldIDs);
+
+ setID(id);
}
- }
-
- for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) {
- Object obj = entry.getValue();
- Script.FieldID fieldID = entry.getKey();
- fieldIDs[i] = fieldID.getID(rs);
- if (obj instanceof UnboundValue) {
- UnboundValue unbound = (UnboundValue)obj;
- unbound.addReference(this, fieldID);
- } else {
- retrieveValueAndDependenceInfo(rs, i, obj, values,
- sizes, depClosures, depFieldIDs);
+
+ public Closure(RenderScript rs, Script.InvokeID invokeID,
+ Object[] args, Map<Script.FieldID, Object> globals) {
+ super(0, rs);
+ mFP = FieldPacker.createFieldPack(args);
+
+ mBindings = new HashMap<Script.FieldID, Object>();
+ mGlobalFuture = new HashMap<Script.FieldID, Future>();
+
+ int numValues = globals.size();
+
+ long[] fieldIDs = new long[numValues];
+ long[] values = new long[numValues];
+ int[] sizes = new int[numValues];
+ long[] depClosures = new long[numValues];
+ long[] depFieldIDs = new long[numValues];
+
+ int i = 0;
+ for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) {
+ Object obj = entry.getValue();
+ Script.FieldID fieldID = entry.getKey();
+ fieldIDs[i] = fieldID.getID(rs);
+ if (obj instanceof UnboundValue) {
+ UnboundValue unbound = (UnboundValue)obj;
+ unbound.addReference(this, fieldID);
+ } else {
+ // TODO(yangni): Verify obj not a future.
+ retrieveValueAndDependenceInfo(rs, i, obj, values,
+ sizes, depClosures, depFieldIDs);
+ }
+ i++;
+ }
+
+ long id = rs.nInvokeClosureCreate(invokeID.getID(rs), mFP.getData(), fieldIDs,
+ values, sizes);
+
+ setID(id);
}
- i++;
- }
- long id = rs.nClosureCreate(kernelID.getID(rs), mReturnValue.getID(rs),
- fieldIDs, values, sizes, depClosures, depFieldIDs);
+ private static
+ void retrieveValueAndDependenceInfo(RenderScript rs,
+ int index, Object obj,
+ long[] values, int[] sizes,
+ long[] depClosures,
+ long[] depFieldIDs) {
+
+ if (obj instanceof Future) {
+ Future f = (Future)obj;
+ obj = f.getValue();
+ depClosures[index] = f.getClosure().getID(rs);
+ Script.FieldID fieldID = f.getFieldID();
+ depFieldIDs[index] = fieldID != null ? fieldID.getID(rs) : 0;
+ if (obj == null) {
+ // Value is originally created by the owner closure
+ values[index] = 0;
+ sizes[index] = 0;
+ return;
+ }
+ } else {
+ depClosures[index] = 0;
+ depFieldIDs[index] = 0;
+ }
+
+ ValueAndSize vs = new ValueAndSize(rs, obj);
+ values[index] = vs.value;
+ sizes[index] = vs.size;
+ }
- setID(id);
- }
+ public Future getReturn() {
+ if (mReturnFuture == null) {
+ mReturnFuture = new Future(this, null, mReturnValue);
+ }
- public Closure(RenderScript rs, Script.InvokeID invokeID,
- Object[] args, Map<Script.FieldID, Object> globals) {
- super(0, rs);
- mFP = FieldPacker.createFieldPack(args);
-
- mBindings = new HashMap<Script.FieldID, Object>();
- mGlobalFuture = new HashMap<Script.FieldID, Future>();
-
- int numValues = globals.size();
-
- long[] fieldIDs = new long[numValues];
- long[] values = new long[numValues];
- int[] sizes = new int[numValues];
- long[] depClosures = new long[numValues];
- long[] depFieldIDs = new long[numValues];
-
- int i = 0;
- for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) {
- Object obj = entry.getValue();
- Script.FieldID fieldID = entry.getKey();
- fieldIDs[i] = fieldID.getID(rs);
- if (obj instanceof UnboundValue) {
- UnboundValue unbound = (UnboundValue)obj;
- unbound.addReference(this, fieldID);
- } else {
- // TODO(yangni): Verify obj not a future.
- retrieveValueAndDependenceInfo(rs, i, obj, values,
- sizes, depClosures, depFieldIDs);
+ return mReturnFuture;
}
- i++;
- }
- long id = rs.nInvokeClosureCreate(invokeID.getID(rs), mFP.getData(), fieldIDs,
- values, sizes);
+ public Future getGlobal(Script.FieldID field) {
+ Future f = mGlobalFuture.get(field);
- setID(id);
- }
+ if (f == null) {
+ // If the field is not bound to this closure, this will return a future
+ // without an associated value (reference). So this is not working for
+ // cross-module (cross-script) linking in this case where a field not
+ // explicitly bound.
+ f = new Future(this, field, mBindings.get(field));
+ mGlobalFuture.put(field, f);
+ }
- private static void retrieveValueAndDependenceInfo(RenderScript rs,
- int index, Object obj, long[] values, int[] sizes, long[] depClosures,
- long[] depFieldIDs) {
-
- if (obj instanceof Future) {
- Future f = (Future)obj;
- obj = f.getValue();
- depClosures[index] = f.getClosure().getID(rs);
- Script.FieldID fieldID = f.getFieldID();
- depFieldIDs[index] = fieldID != null ? fieldID.getID(rs) : 0;
- if (obj == null) {
- // Value is originally created by the owner closure
- values[index] = 0;
- sizes[index] = 0;
- return;
+ return f;
}
- } else {
- depClosures[index] = 0;
- depFieldIDs[index] = 0;
- }
-
- ValueAndSize vs = new ValueAndSize(rs, obj);
- values[index] = vs.value;
- sizes[index] = vs.size;
- }
- public Future getReturn() {
- if (mReturnFuture == null) {
- mReturnFuture = new Future(this, null, mReturnValue);
- }
+ void setArg(int index, Object obj) {
+ ValueAndSize vs = new ValueAndSize(mRS, obj);
+ mRS.nClosureSetArg(getID(mRS), index, vs.value, vs.size);
+ }
+
+ void setGlobal(Script.FieldID fieldID, Object obj) {
+ ValueAndSize vs = new ValueAndSize(mRS, obj);
+ mRS.nClosureSetGlobal(getID(mRS), fieldID.getID(mRS), vs.value, vs.size);
+ }
- return mReturnFuture;
+ private static final class ValueAndSize {
+ public ValueAndSize(RenderScript rs, Object obj) {
+ if (obj instanceof Allocation) {
+ value = ((Allocation)obj).getID(rs);
+ size = -1;
+ } else if (obj instanceof Boolean) {
+ value = ((Boolean)obj).booleanValue() ? 1 : 0;
+ size = 4;
+ } else if (obj instanceof Integer) {
+ value = ((Integer)obj).longValue();
+ size = 4;
+ } else if (obj instanceof Long) {
+ value = ((Long)obj).longValue();
+ size = 8;
+ } else if (obj instanceof Float) {
+ value = ((Float)obj).longValue();
+ size = 4;
+ } else if (obj instanceof Double) {
+ value = ((Double)obj).longValue();
+ size = 8;
+ }
+ }
+ public long value;
+ public int size;
+ }
}
- public Future getGlobal(Script.FieldID field) {
- Future f = mGlobalFuture.get(field);
+ public static class Future {
+ Closure mClosure;
+ Script.FieldID mFieldID;
+ Object mValue;
- if (f == null) {
- // If the field is not bound to this closure, this will return a future
- // without an associated value (reference). So this is not working for
- // cross-module (cross-script) linking in this case where a field not
- // explicitly bound.
- f = new Future(this, field, mBindings.get(field));
- mGlobalFuture.put(field, f);
- }
+ Future(Closure closure, Script.FieldID fieldID, Object value) {
+ mClosure = closure;
+ mFieldID = fieldID;
+ mValue = value;
+ }
- return f;
+ Closure getClosure() { return mClosure; }
+ Script.FieldID getFieldID() { return mFieldID; }
+ Object getValue() { return mValue; }
}
- void setArg(int index, Object obj) {
- ValueAndSize vs = new ValueAndSize(mRS, obj);
- mRS.nClosureSetArg(getID(mRS), index, vs.value, vs.size);
- }
+ public static class UnboundValue {
+ // Either mFieldID or mArgIndex should be set but not both.
+ List<Pair<Closure, Script.FieldID>> mFieldID;
+ // -1 means unset. Legal values are 0 .. n-1, where n is the number of
+ // arguments for the referencing closure.
+ List<Pair<Closure, Integer>> mArgIndex;
- void setGlobal(Script.FieldID fieldID, Object obj) {
- ValueAndSize vs = new ValueAndSize(mRS, obj);
- mRS.nClosureSetGlobal(getID(mRS), fieldID.getID(mRS), vs.value, vs.size);
- }
+ UnboundValue() {
+ mFieldID = new ArrayList<Pair<Closure, Script.FieldID>>();
+ mArgIndex = new ArrayList<Pair<Closure, Integer>>();
+ }
- private static final class ValueAndSize {
- public ValueAndSize(RenderScript rs, Object obj) {
- if (obj instanceof Allocation) {
- value = ((Allocation)obj).getID(rs);
- size = -1;
- } else if (obj instanceof Boolean) {
- value = ((Boolean)obj).booleanValue() ? 1 : 0;
- size = 4;
- } else if (obj instanceof Integer) {
- value = ((Integer)obj).longValue();
- size = 4;
- } else if (obj instanceof Long) {
- value = ((Long)obj).longValue();
- size = 8;
- } else if (obj instanceof Float) {
- value = ((Float)obj).longValue();
- size = 4;
- } else if (obj instanceof Double) {
- value = ((Double)obj).longValue();
- size = 8;
+ void addReference(Closure closure, int index) {
+ mArgIndex.add(Pair.create(closure, Integer.valueOf(index)));
}
- }
- public long value;
- public int size;
- }
- }
- public static class Future {
- Closure mClosure;
- Script.FieldID mFieldID;
- Object mValue;
+ void addReference(Closure closure, Script.FieldID fieldID) {
+ mFieldID.add(Pair.create(closure, fieldID));
+ }
- Future(Closure closure, Script.FieldID fieldID, Object value) {
- mClosure = closure;
- mFieldID = fieldID;
- mValue = value;
+ void set(Object value) {
+ for (Pair<Closure, Integer> p : mArgIndex) {
+ Closure closure = p.first;
+ int index = p.second.intValue();
+ closure.setArg(index, value);
+ }
+ for (Pair<Closure, Script.FieldID> p : mFieldID) {
+ Closure closure = p.first;
+ Script.FieldID fieldID = p.second;
+ closure.setGlobal(fieldID, value);
+ }
+ }
}
- Closure getClosure() { return mClosure; }
- Script.FieldID getFieldID() { return mFieldID; }
- Object getValue() { return mValue; }
- }
-
- public static class UnboundValue {
- // Either mFieldID or mArgIndex should be set but not both.
- List<Pair<Closure, Script.FieldID>> mFieldID;
- // -1 means unset. Legal values are 0 .. n-1, where n is the number of
- // arguments for the referencing closure.
- List<Pair<Closure, Integer>> mArgIndex;
-
- UnboundValue() {
- mFieldID = new ArrayList<Pair<Closure, Script.FieldID>>();
- mArgIndex = new ArrayList<Pair<Closure, Integer>>();
- }
+ List<Closure> mClosures;
+ List<UnboundValue> mInputs;
+ Future[] mOutputs;
- void addReference(Closure closure, int index) {
- mArgIndex.add(Pair.create(closure, Integer.valueOf(index)));
- }
+ private static final String TAG = "ScriptGroup2";
- void addReference(Closure closure, Script.FieldID fieldID) {
- mFieldID.add(Pair.create(closure, fieldID));
+ public ScriptGroup2(long id, RenderScript rs) {
+ super(id, rs);
}
- void set(Object value) {
- for (Pair<Closure, Integer> p : mArgIndex) {
- Closure closure = p.first;
- int index = p.second.intValue();
- closure.setArg(index, value);
- }
- for (Pair<Closure, Script.FieldID> p : mFieldID) {
- Closure closure = p.first;
- Script.FieldID fieldID = p.second;
- closure.setGlobal(fieldID, value);
- }
+ ScriptGroup2(RenderScript rs, List<Closure> closures,
+ List<UnboundValue> inputs, Future[] outputs) {
+ super(0, rs);
+ mClosures = closures;
+ mInputs = inputs;
+ mOutputs = outputs;
+
+ long[] closureIDs = new long[closures.size()];
+ for (int i = 0; i < closureIDs.length; i++) {
+ closureIDs[i] = closures.get(i).getID(rs);
+ }
+ long id = rs.nScriptGroup2Create(ScriptC.mCachePath, closureIDs);
+ setID(id);
}
- }
- List<Closure> mClosures;
- List<UnboundValue> mInputs;
- Future[] mOutputs;
+ public Object[] execute(Object... inputs) {
+ if (inputs.length < mInputs.size()) {
+ Log.e(TAG, this.toString() + " receives " + inputs.length + " inputs, " +
+ "less than expected " + mInputs.size());
+ return null;
+ }
- private static final String TAG = "ScriptGroup2";
+ if (inputs.length > mInputs.size()) {
+ Log.i(TAG, this.toString() + " receives " + inputs.length + " inputs, " +
+ "more than expected " + mInputs.size());
+ }
- public ScriptGroup2(long id, RenderScript rs) {
- super(id, rs);
- }
+ for (int i = 0; i < mInputs.size(); i++) {
+ Object obj = inputs[i];
+ if (obj instanceof Future || obj instanceof UnboundValue) {
+ Log.e(TAG, this.toString() + ": input " + i +
+ " is a future or unbound value");
+ return null;
+ }
+ UnboundValue unbound = mInputs.get(i);
+ unbound.set(obj);
+ }
- ScriptGroup2(RenderScript rs, List<Closure> closures,
- List<UnboundValue> inputs, Future[] outputs) {
- super(0, rs);
- mClosures = closures;
- mInputs = inputs;
- mOutputs = outputs;
+ mRS.nScriptGroup2Execute(getID(mRS));
- long[] closureIDs = new long[closures.size()];
- for (int i = 0; i < closureIDs.length; i++) {
- closureIDs[i] = closures.get(i).getID(rs);
- }
- long id = rs.nScriptGroup2Create(ScriptC.mCachePath, closureIDs);
- setID(id);
- }
-
- public Object[] execute(Object... inputs) {
- if (inputs.length < mInputs.size()) {
- Log.e(TAG, this.toString() + " receives " + inputs.length + " inputs, " +
- "less than expected " + mInputs.size());
- return null;
+ Object[] outputObjs = new Object[mOutputs.length];
+ int i = 0;
+ for (Future f : mOutputs) {
+ outputObjs[i++] = f.getValue();
+ }
+ return outputObjs;
}
- if (inputs.length > mInputs.size()) {
- Log.i(TAG, this.toString() + " receives " + inputs.length + " inputs, " +
- "more than expected " + mInputs.size());
+ /**
+ @hide Pending Android public API approval.
+ */
+ public static final class Binding {
+ public Script.FieldID mField;
+ public Object mValue;
+ public Binding(Script.FieldID field, Object value) {
+ mField = field;
+ mValue = value;
+ }
}
- for (int i = 0; i < mInputs.size(); i++) {
- Object obj = inputs[i];
- if (obj instanceof Future || obj instanceof UnboundValue) {
- Log.e(TAG, this.toString() + ": input " + i +
- " is a future or unbound value");
- return null;
- }
- UnboundValue unbound = mInputs.get(i);
- unbound.set(obj);
- }
+ /**
+ @hide Pending Android public API approval.
+ */
+ public static final class Builder {
+ RenderScript mRS;
+ List<Closure> mClosures;
+ List<UnboundValue> mInputs;
+ private static final String TAG = "ScriptGroup2.Builder";
+
+ public Builder(RenderScript rs) {
+ mRS = rs;
+ mClosures = new ArrayList<Closure>();
+ mInputs = new ArrayList<UnboundValue>();
+ }
- mRS.nScriptGroup2Execute(getID(mRS));
+ public Closure addKernel(Script.KernelID k, Type returnType, Object[] args,
+ Map<Script.FieldID, Object> globalBindings) {
+ Closure c = new Closure(mRS, k, returnType, args, globalBindings);
+ mClosures.add(c);
+ return c;
+ }
- Object[] outputObjs = new Object[mOutputs.length];
- int i = 0;
- for (Future f : mOutputs) {
- outputObjs[i++] = f.getValue();
- }
- return outputObjs;
- }
-
- /**
- @hide Pending Android public API approval.
- */
- public static final class Builder {
- RenderScript mRS;
- List<Closure> mClosures;
- List<UnboundValue> mInputs;
+ public Closure addInvoke(Script.InvokeID invoke, Object[] args,
+ Map<Script.FieldID, Object> globalBindings) {
+ Closure c = new Closure(mRS, invoke, args, globalBindings);
+ mClosures.add(c);
+ return c;
+ }
- private static final String TAG = "ScriptGroup2.Builder";
+ public UnboundValue addInput() {
+ UnboundValue unbound = new UnboundValue();
+ mInputs.add(unbound);
+ return unbound;
+ }
- public Builder(RenderScript rs) {
- mRS = rs;
- mClosures = new ArrayList<Closure>();
- mInputs = new ArrayList<UnboundValue>();
- }
+ public Closure addKernel(Script.KernelID k, Type returnType, Object... argsAndBindings) {
+ ArrayList<Object> args = new ArrayList<Object>();
+ Map<Script.FieldID, Object> bindingMap = new HashMap<Script.FieldID, Object>();
+ if (!seperateArgsAndBindings(argsAndBindings, args, bindingMap)) {
+ return null;
+ }
+ return addKernel(k, returnType, args.toArray(), bindingMap);
+ }
- public Closure addKernel(Script.KernelID k, Type returnType, Object[] args,
- Map<Script.FieldID, Object> globalBindings) {
- Closure c = new Closure(mRS, k, returnType, args, globalBindings);
- mClosures.add(c);
- return c;
- }
+ public Closure addInvoke(Script.InvokeID invoke, Object... argsAndBindings) {
+ ArrayList<Object> args = new ArrayList<Object>();
+ Map<Script.FieldID, Object> bindingMap = new HashMap<Script.FieldID, Object>();
+ if (!seperateArgsAndBindings(argsAndBindings, args, bindingMap)) {
+ return null;
+ }
+ return addInvoke(invoke, args.toArray(), bindingMap);
+ }
- public Closure addInvoke(Script.InvokeID invoke, Object[] args,
- Map<Script.FieldID, Object> globalBindings) {
- Closure c = new Closure(mRS, invoke, args, globalBindings);
- mClosures.add(c);
- return c;
- }
+ public ScriptGroup2 create(Future... outputs) {
+ ScriptGroup2 ret = new ScriptGroup2(mRS, mClosures, mInputs, outputs);
+ return ret;
+ }
- public UnboundValue addInput() {
- UnboundValue unbound = new UnboundValue();
- mInputs.add(unbound);
- return unbound;
- }
+ private boolean seperateArgsAndBindings(Object[] argsAndBindings,
+ ArrayList<Object> args,
+ Map<Script.FieldID, Object> bindingMap) {
+ int i;
+ for (i = 0; i < argsAndBindings.length; i++) {
+ if (argsAndBindings[i] instanceof Binding) {
+ break;
+ }
+ args.add(argsAndBindings[i]);
+ }
+
+ for (; i < argsAndBindings.length; i++) {
+ if (!(argsAndBindings[i] instanceof Binding)) {
+ return false;
+ }
+ Binding b = (Binding)argsAndBindings[i];
+ bindingMap.put(b.mField, b.mValue);
+ }
+
+ return true;
+ }
- public ScriptGroup2 create(Future... outputs) {
- ScriptGroup2 ret = new ScriptGroup2(mRS, mClosures, mInputs, outputs);
- return ret;
}
-
- }
}