| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 17 | package android.renderscript; |
| 18 | |
| 19 | import android.util.Log; |
| 20 | import android.util.Pair; |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.HashMap; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | /** |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 27 | * ScriptGroup2 is a new, enhanced API for script groups. |
| 28 | * A script group is a collection of kernels or invocable functions, with |
| 29 | * data dependencies defined among them. A script group is launched for |
| 30 | * execution as a whole, rather than launching each kernel or invocable function |
| 31 | * separately. Once created, a script group can be repeatedly used with |
| 32 | * different inputs. |
| 33 | * <p> |
| 34 | * In the new ScriptGroup2 API, a script group is modeled using closures. |
| 35 | * A closure, in this context, is defined as a function call to a kernel or |
| 36 | * invocable function. Each function argument or global variable accessed inside |
| 37 | * the function is bound to 1) a known value, 2) a script group input, or 3) a |
| 38 | * future. A future is the output of a closure, i.e., the return value of the |
| 39 | * function or a global variable written by that function. |
| 40 | * <p> |
| 41 | * A script group is a directed acyclic graph (DAG), in which closures are the |
| 42 | * vertices and the dependencies among them are the edges. |
| 43 | * The way the ScriptGroup2 API is designed makes cycles impossible in a script |
| 44 | * group. For example, it is impossible to make forward references to futures, |
| 45 | * i.e., it is impossible to set as input to a closure the future from itself or |
| 46 | * a future from another closure that directly or indirectly depends on it. |
| 47 | * <p> |
| 48 | * Grouping kernels and invocable functions together allows to execute them more |
| 49 | * efficiently. Runtime and compiler optimizations are applied to script |
| 50 | * groups, to reduce computation or communication overhead, and to make more |
| 51 | * efficient use of the CPU and the GPU. |
| 52 | */ |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 53 | |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 54 | public class ScriptGroup2 extends BaseObj { |
| 55 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 56 | /** |
| 57 | * An opaque class for closures |
| 58 | */ |
| 59 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 60 | public static class Closure extends BaseObj { |
| 61 | private Allocation mReturnValue; |
| 62 | private Map<Script.FieldID, Object> mBindings; |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 63 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 64 | private Future mReturnFuture; |
| 65 | private Map<Script.FieldID, Future> mGlobalFuture; |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 66 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 67 | private FieldPacker mFP; |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 68 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 69 | private static final String TAG = "Closure"; |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 70 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 71 | public Closure(long id, RenderScript rs) { |
| 72 | super(id, rs); |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 73 | } |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 74 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 75 | public Closure(RenderScript rs, Script.KernelID kernelID, Type returnType, |
| 76 | Object[] args, Map<Script.FieldID, Object> globals) { |
| 77 | super(0, rs); |
| 78 | |
| 79 | mReturnValue = Allocation.createTyped(rs, returnType); |
| 80 | mBindings = new HashMap<Script.FieldID, Object>(); |
| 81 | mGlobalFuture = new HashMap<Script.FieldID, Future>(); |
| 82 | |
| 83 | int numValues = args.length + globals.size(); |
| 84 | |
| 85 | long[] fieldIDs = new long[numValues]; |
| 86 | long[] values = new long[numValues]; |
| 87 | int[] sizes = new int[numValues]; |
| 88 | long[] depClosures = new long[numValues]; |
| 89 | long[] depFieldIDs = new long[numValues]; |
| 90 | |
| 91 | int i; |
| 92 | for (i = 0; i < args.length; i++) { |
| 93 | Object obj = args[i]; |
| 94 | fieldIDs[i] = 0; |
| 95 | if (obj instanceof UnboundValue) { |
| 96 | UnboundValue unbound = (UnboundValue)obj; |
| 97 | unbound.addReference(this, i); |
| 98 | } else { |
| 99 | retrieveValueAndDependenceInfo(rs, i, args[i], values, sizes, |
| 100 | depClosures, depFieldIDs); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) { |
| 105 | Object obj = entry.getValue(); |
| 106 | Script.FieldID fieldID = entry.getKey(); |
| 107 | fieldIDs[i] = fieldID.getID(rs); |
| 108 | if (obj instanceof UnboundValue) { |
| 109 | UnboundValue unbound = (UnboundValue)obj; |
| 110 | unbound.addReference(this, fieldID); |
| 111 | } else { |
| 112 | retrieveValueAndDependenceInfo(rs, i, obj, values, |
| 113 | sizes, depClosures, depFieldIDs); |
| 114 | } |
| 115 | i++; |
| 116 | } |
| 117 | |
| 118 | long id = rs.nClosureCreate(kernelID.getID(rs), mReturnValue.getID(rs), |
| 119 | fieldIDs, values, sizes, depClosures, depFieldIDs); |
| 120 | |
| 121 | setID(id); |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 122 | } |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 123 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 124 | public Closure(RenderScript rs, Script.InvokeID invokeID, |
| 125 | Object[] args, Map<Script.FieldID, Object> globals) { |
| 126 | super(0, rs); |
| 127 | mFP = FieldPacker.createFieldPack(args); |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 128 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 129 | mBindings = new HashMap<Script.FieldID, Object>(); |
| 130 | mGlobalFuture = new HashMap<Script.FieldID, Future>(); |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 131 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 132 | int numValues = globals.size(); |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 133 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 134 | long[] fieldIDs = new long[numValues]; |
| 135 | long[] values = new long[numValues]; |
| 136 | int[] sizes = new int[numValues]; |
| 137 | long[] depClosures = new long[numValues]; |
| 138 | long[] depFieldIDs = new long[numValues]; |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 139 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 140 | int i = 0; |
| 141 | for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) { |
| 142 | Object obj = entry.getValue(); |
| 143 | Script.FieldID fieldID = entry.getKey(); |
| 144 | fieldIDs[i] = fieldID.getID(rs); |
| 145 | if (obj instanceof UnboundValue) { |
| 146 | UnboundValue unbound = (UnboundValue)obj; |
| 147 | unbound.addReference(this, fieldID); |
| 148 | } else { |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 149 | retrieveValueAndDependenceInfo(rs, i, obj, values, |
| 150 | sizes, depClosures, depFieldIDs); |
| 151 | } |
| 152 | i++; |
| 153 | } |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 154 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 155 | long id = rs.nInvokeClosureCreate(invokeID.getID(rs), mFP.getData(), fieldIDs, |
| 156 | values, sizes); |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 157 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 158 | setID(id); |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 159 | } |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 160 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 161 | private static |
| 162 | void retrieveValueAndDependenceInfo(RenderScript rs, |
| 163 | int index, Object obj, |
| 164 | long[] values, int[] sizes, |
| 165 | long[] depClosures, |
| 166 | long[] depFieldIDs) { |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 167 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 168 | if (obj instanceof Future) { |
| 169 | Future f = (Future)obj; |
| 170 | obj = f.getValue(); |
| 171 | depClosures[index] = f.getClosure().getID(rs); |
| 172 | Script.FieldID fieldID = f.getFieldID(); |
| 173 | depFieldIDs[index] = fieldID != null ? fieldID.getID(rs) : 0; |
| 174 | if (obj == null) { |
| 175 | // Value is originally created by the owner closure |
| 176 | values[index] = 0; |
| 177 | sizes[index] = 0; |
| 178 | return; |
| 179 | } |
| 180 | } else { |
| 181 | depClosures[index] = 0; |
| 182 | depFieldIDs[index] = 0; |
| 183 | } |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 184 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 185 | ValueAndSize vs = new ValueAndSize(rs, obj); |
| 186 | values[index] = vs.value; |
| 187 | sizes[index] = vs.size; |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 188 | } |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 189 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 190 | /** |
| 191 | * Returns the future for the return value |
| 192 | * |
| 193 | * @return a future |
| 194 | */ |
| 195 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 196 | public Future getReturn() { |
| 197 | if (mReturnFuture == null) { |
| 198 | mReturnFuture = new Future(this, null, mReturnValue); |
| 199 | } |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 200 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 201 | return mReturnFuture; |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 202 | } |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 203 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 204 | /** |
| 205 | * Returns the future for a global variable |
| 206 | * |
| 207 | * @param field the field ID for the global variable |
| 208 | * @return a future |
| 209 | */ |
| 210 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 211 | public Future getGlobal(Script.FieldID field) { |
| 212 | Future f = mGlobalFuture.get(field); |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 213 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 214 | if (f == null) { |
| 215 | // If the field is not bound to this closure, this will return a future |
| 216 | // without an associated value (reference). So this is not working for |
| 217 | // cross-module (cross-script) linking in this case where a field not |
| 218 | // explicitly bound. |
| 219 | f = new Future(this, field, mBindings.get(field)); |
| 220 | mGlobalFuture.put(field, f); |
| 221 | } |
| 222 | |
| 223 | return f; |
| 224 | } |
| 225 | |
| 226 | void setArg(int index, Object obj) { |
| 227 | ValueAndSize vs = new ValueAndSize(mRS, obj); |
| 228 | mRS.nClosureSetArg(getID(mRS), index, vs.value, vs.size); |
| 229 | } |
| 230 | |
| 231 | void setGlobal(Script.FieldID fieldID, Object obj) { |
| 232 | ValueAndSize vs = new ValueAndSize(mRS, obj); |
| 233 | mRS.nClosureSetGlobal(getID(mRS), fieldID.getID(mRS), vs.value, vs.size); |
| 234 | } |
| 235 | |
| 236 | private static final class ValueAndSize { |
| 237 | public ValueAndSize(RenderScript rs, Object obj) { |
| 238 | if (obj instanceof Allocation) { |
| 239 | value = ((Allocation)obj).getID(rs); |
| 240 | size = -1; |
| 241 | } else if (obj instanceof Boolean) { |
| 242 | value = ((Boolean)obj).booleanValue() ? 1 : 0; |
| 243 | size = 4; |
| 244 | } else if (obj instanceof Integer) { |
| 245 | value = ((Integer)obj).longValue(); |
| 246 | size = 4; |
| 247 | } else if (obj instanceof Long) { |
| 248 | value = ((Long)obj).longValue(); |
| 249 | size = 8; |
| 250 | } else if (obj instanceof Float) { |
| 251 | value = ((Float)obj).longValue(); |
| 252 | size = 4; |
| 253 | } else if (obj instanceof Double) { |
| 254 | value = ((Double)obj).longValue(); |
| 255 | size = 8; |
| 256 | } |
| 257 | } |
| 258 | public long value; |
| 259 | public int size; |
| 260 | } |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 263 | /** |
| 264 | * An opaque class for futures |
| 265 | */ |
| 266 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 267 | public static class Future { |
| 268 | Closure mClosure; |
| 269 | Script.FieldID mFieldID; |
| 270 | Object mValue; |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 271 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 272 | Future(Closure closure, Script.FieldID fieldID, Object value) { |
| 273 | mClosure = closure; |
| 274 | mFieldID = fieldID; |
| 275 | mValue = value; |
| 276 | } |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 277 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 278 | Closure getClosure() { return mClosure; } |
| 279 | Script.FieldID getFieldID() { return mFieldID; } |
| 280 | Object getValue() { return mValue; } |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 283 | /** |
| 284 | * An opaque class for unbound values (a.k.a. script group inputs) |
| 285 | */ |
| 286 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 287 | public static class UnboundValue { |
| 288 | // Either mFieldID or mArgIndex should be set but not both. |
| 289 | List<Pair<Closure, Script.FieldID>> mFieldID; |
| 290 | // -1 means unset. Legal values are 0 .. n-1, where n is the number of |
| 291 | // arguments for the referencing closure. |
| 292 | List<Pair<Closure, Integer>> mArgIndex; |
| 293 | |
| 294 | UnboundValue() { |
| 295 | mFieldID = new ArrayList<Pair<Closure, Script.FieldID>>(); |
| 296 | mArgIndex = new ArrayList<Pair<Closure, Integer>>(); |
| 297 | } |
| 298 | |
| 299 | void addReference(Closure closure, int index) { |
| 300 | mArgIndex.add(Pair.create(closure, Integer.valueOf(index))); |
| 301 | } |
| 302 | |
| 303 | void addReference(Closure closure, Script.FieldID fieldID) { |
| 304 | mFieldID.add(Pair.create(closure, fieldID)); |
| 305 | } |
| 306 | |
| 307 | void set(Object value) { |
| 308 | for (Pair<Closure, Integer> p : mArgIndex) { |
| 309 | Closure closure = p.first; |
| 310 | int index = p.second.intValue(); |
| 311 | closure.setArg(index, value); |
| 312 | } |
| 313 | for (Pair<Closure, Script.FieldID> p : mFieldID) { |
| 314 | Closure closure = p.first; |
| 315 | Script.FieldID fieldID = p.second; |
| 316 | closure.setGlobal(fieldID, value); |
| 317 | } |
| 318 | } |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 321 | List<Closure> mClosures; |
| 322 | List<UnboundValue> mInputs; |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 323 | Future[] mOutputs; |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 324 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 325 | private static final String TAG = "ScriptGroup2"; |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 326 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 327 | public ScriptGroup2(long id, RenderScript rs) { |
| 328 | super(id, rs); |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 331 | ScriptGroup2(RenderScript rs, List<Closure> closures, |
| 332 | List<UnboundValue> inputs, Future[] outputs) { |
| 333 | super(0, rs); |
| 334 | mClosures = closures; |
| 335 | mInputs = inputs; |
| 336 | mOutputs = outputs; |
| 337 | |
| 338 | long[] closureIDs = new long[closures.size()]; |
| 339 | for (int i = 0; i < closureIDs.length; i++) { |
| 340 | closureIDs[i] = closures.get(i).getID(rs); |
| 341 | } |
| 342 | long id = rs.nScriptGroup2Create(ScriptC.mCachePath, closureIDs); |
| 343 | setID(id); |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 346 | /** |
| 347 | * Executes a script group |
| 348 | * |
| 349 | * @param inputs inputs to the script group |
| 350 | * @return outputs of the script group as an array of objects |
| 351 | */ |
| 352 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 353 | public Object[] execute(Object... inputs) { |
| 354 | if (inputs.length < mInputs.size()) { |
| 355 | Log.e(TAG, this.toString() + " receives " + inputs.length + " inputs, " + |
| 356 | "less than expected " + mInputs.size()); |
| 357 | return null; |
| 358 | } |
| 359 | |
| 360 | if (inputs.length > mInputs.size()) { |
| 361 | Log.i(TAG, this.toString() + " receives " + inputs.length + " inputs, " + |
| 362 | "more than expected " + mInputs.size()); |
| 363 | } |
| 364 | |
| 365 | for (int i = 0; i < mInputs.size(); i++) { |
| 366 | Object obj = inputs[i]; |
| 367 | if (obj instanceof Future || obj instanceof UnboundValue) { |
| 368 | Log.e(TAG, this.toString() + ": input " + i + |
| 369 | " is a future or unbound value"); |
| 370 | return null; |
| 371 | } |
| 372 | UnboundValue unbound = mInputs.get(i); |
| 373 | unbound.set(obj); |
| 374 | } |
| 375 | |
| 376 | mRS.nScriptGroup2Execute(getID(mRS)); |
| 377 | |
| 378 | Object[] outputObjs = new Object[mOutputs.length]; |
| 379 | int i = 0; |
| 380 | for (Future f : mOutputs) { |
| 381 | outputObjs[i++] = f.getValue(); |
| 382 | } |
| 383 | return outputObjs; |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 386 | /** |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 387 | * A class representing a binding of a value to a global variable in a |
| 388 | * kernel or invocable function. Such a binding can be used to create a |
| 389 | * closure. |
| 390 | */ |
| 391 | |
| Yang Ni | 8ff2980 | 2015-03-11 16:25:37 -0700 | [diff] [blame] | 392 | public static final class Binding { |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 393 | private Script.FieldID mField; |
| 394 | private Object mValue; |
| 395 | |
| 396 | /** |
| 397 | * Returns a Binding object that binds value to field |
| 398 | * |
| 399 | * @param field the Script.FieldID of the global variable |
| 400 | * @param value the value |
| 401 | */ |
| 402 | |
| Yang Ni | 8ff2980 | 2015-03-11 16:25:37 -0700 | [diff] [blame] | 403 | public Binding(Script.FieldID field, Object value) { |
| 404 | mField = field; |
| 405 | mValue = value; |
| 406 | } |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 407 | |
| 408 | /** |
| 409 | * Returns the field ID |
| 410 | */ |
| 411 | |
| 412 | public Script.FieldID getField() { return mField; } |
| 413 | |
| 414 | /** |
| 415 | * Returns the value |
| 416 | */ |
| 417 | |
| 418 | public Object getValue() { return mValue; } |
| Yang Ni | 8ff2980 | 2015-03-11 16:25:37 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 422 | * The builder class to create a script group. |
| 423 | * <p> |
| 424 | * Closures are created using the {@link #addKernel} or {@link #addInvoke} |
| 425 | * methods. |
| 426 | * When a closure is created, futures from previously created closures |
| 427 | * can be used as inputs. |
| 428 | * Unbound values can be used as inputs to create closures as well. |
| 429 | * An unbound value is created using the {@link #addInput} method. |
| 430 | * Unbound values become inputs to the script group to be created, |
| 431 | * in the order that they are added. |
| 432 | * A script group is created by a call to the {@link #create} method, which |
| 433 | * accepts an array of futures as the outputs for the script group. |
| 434 | * <p> |
| 435 | * Closures in a script group can be evaluated in any order as long as the |
| 436 | * following conditions are met. |
| 437 | * First, a closure must be evaluated before any other closures that take its |
| 438 | * futures as inputs. |
| 439 | * Second, all closures added before an invoke closure must be evaluated |
| 440 | * before it. |
| 441 | * Third, all closures added after an invoke closure must be evaluated after |
| 442 | * it. |
| 443 | * <p> |
| 444 | * As a special case, the order that the closures are added is a legal |
| 445 | * evaluation order. However, other evaluation orders are allowed, including |
| 446 | * concurrently evaluating independent closures. |
| 447 | */ |
| 448 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 449 | public static final class Builder { |
| 450 | RenderScript mRS; |
| 451 | List<Closure> mClosures; |
| 452 | List<UnboundValue> mInputs; |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 453 | private static final String TAG = "ScriptGroup2.Builder"; |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 454 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 455 | /** |
| 456 | * Returns a Builder object |
| 457 | * |
| 458 | * @param rs the RenderScript context |
| 459 | */ |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 460 | public Builder(RenderScript rs) { |
| 461 | mRS = rs; |
| 462 | mClosures = new ArrayList<Closure>(); |
| 463 | mInputs = new ArrayList<UnboundValue>(); |
| 464 | } |
| 465 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 466 | /** |
| 467 | * Adds a closure for a kernel |
| 468 | * |
| 469 | * @param k Kernel ID for the kernel function |
| 470 | * @param returnType Allocation type for the return value |
| 471 | * @param args arguments to the kernel function |
| 472 | * @param globalBindings bindings for global variables |
| 473 | * @return a closure |
| 474 | */ |
| 475 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 476 | public Closure addKernel(Script.KernelID k, Type returnType, Object[] args, |
| 477 | Map<Script.FieldID, Object> globalBindings) { |
| 478 | Closure c = new Closure(mRS, k, returnType, args, globalBindings); |
| 479 | mClosures.add(c); |
| 480 | return c; |
| 481 | } |
| 482 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 483 | /** |
| 484 | * Adds a closure for an invocable function |
| 485 | * |
| 486 | * @param invoke Invoke ID for the invocable function |
| 487 | * @param args arguments to the invocable function |
| 488 | * @param globalBindings bindings for global variables |
| 489 | * @return a closure |
| 490 | */ |
| 491 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 492 | public Closure addInvoke(Script.InvokeID invoke, Object[] args, |
| 493 | Map<Script.FieldID, Object> globalBindings) { |
| 494 | Closure c = new Closure(mRS, invoke, args, globalBindings); |
| 495 | mClosures.add(c); |
| 496 | return c; |
| 497 | } |
| 498 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 499 | /** |
| 500 | * Adds a script group input |
| 501 | * |
| 502 | * @return a unbound value that can be used to create a closure |
| 503 | */ |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 504 | public UnboundValue addInput() { |
| 505 | UnboundValue unbound = new UnboundValue(); |
| 506 | mInputs.add(unbound); |
| 507 | return unbound; |
| 508 | } |
| 509 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 510 | /** |
| 511 | * Adds a closure for a kernel |
| 512 | * |
| 513 | * @param k Kernel ID for the kernel function |
| 514 | * @param argsAndBindings arguments followed by bindings for global variables |
| 515 | * @return a closure |
| 516 | */ |
| 517 | |
| Yang Ni | 8ff2980 | 2015-03-11 16:25:37 -0700 | [diff] [blame] | 518 | public Closure addKernel(Script.KernelID k, Type returnType, Object... argsAndBindings) { |
| 519 | ArrayList<Object> args = new ArrayList<Object>(); |
| 520 | Map<Script.FieldID, Object> bindingMap = new HashMap<Script.FieldID, Object>(); |
| 521 | if (!seperateArgsAndBindings(argsAndBindings, args, bindingMap)) { |
| 522 | return null; |
| 523 | } |
| 524 | return addKernel(k, returnType, args.toArray(), bindingMap); |
| 525 | } |
| 526 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 527 | /** |
| 528 | * Adds a closure for an invocable function |
| 529 | * |
| 530 | * @param invoke Invoke ID for the invocable function |
| 531 | * @param argsAndBindings arguments followed by bindings for global variables |
| 532 | * @return a closure |
| 533 | */ |
| 534 | |
| Yang Ni | 8ff2980 | 2015-03-11 16:25:37 -0700 | [diff] [blame] | 535 | public Closure addInvoke(Script.InvokeID invoke, Object... argsAndBindings) { |
| 536 | ArrayList<Object> args = new ArrayList<Object>(); |
| 537 | Map<Script.FieldID, Object> bindingMap = new HashMap<Script.FieldID, Object>(); |
| 538 | if (!seperateArgsAndBindings(argsAndBindings, args, bindingMap)) { |
| 539 | return null; |
| 540 | } |
| 541 | return addInvoke(invoke, args.toArray(), bindingMap); |
| 542 | } |
| 543 | |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 544 | /** |
| 545 | * Creates a script group |
| 546 | * |
| 547 | * @param outputs futures intended as outputs of the script group |
| 548 | * @return a script group |
| 549 | */ |
| 550 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 551 | public ScriptGroup2 create(Future... outputs) { |
| 552 | ScriptGroup2 ret = new ScriptGroup2(mRS, mClosures, mInputs, outputs); |
| 553 | return ret; |
| 554 | } |
| 555 | |
| Yang Ni | 8ff2980 | 2015-03-11 16:25:37 -0700 | [diff] [blame] | 556 | private boolean seperateArgsAndBindings(Object[] argsAndBindings, |
| 557 | ArrayList<Object> args, |
| 558 | Map<Script.FieldID, Object> bindingMap) { |
| 559 | int i; |
| 560 | for (i = 0; i < argsAndBindings.length; i++) { |
| 561 | if (argsAndBindings[i] instanceof Binding) { |
| 562 | break; |
| 563 | } |
| 564 | args.add(argsAndBindings[i]); |
| 565 | } |
| 566 | |
| 567 | for (; i < argsAndBindings.length; i++) { |
| 568 | if (!(argsAndBindings[i] instanceof Binding)) { |
| 569 | return false; |
| 570 | } |
| 571 | Binding b = (Binding)argsAndBindings[i]; |
| Yang Ni | 4470d68a | 2015-03-16 16:23:50 -0700 | [diff] [blame^] | 572 | bindingMap.put(b.getField(), b.getValue()); |
| Yang Ni | 8ff2980 | 2015-03-11 16:25:37 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | return true; |
| 576 | } |
| 577 | |
| Yang Ni | cc1ca48 | 2015-03-16 15:53:18 -0700 | [diff] [blame] | 578 | } |
| Yang Ni | 281c325 | 2014-10-24 08:52:24 -0700 | [diff] [blame] | 579 | } |