Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 17 | package android.renderscript; |
| 18 | |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 19 | import android.util.Log; |
| 20 | import android.util.Pair; |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 21 | import java.util.ArrayList; |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 22 | import java.util.HashMap; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 27 | * A group of kernels that are executed |
| 28 | * together with one execution call as if they were a single kernel |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 29 | * <p> |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 30 | * In addition to kernels, a script group may contain invocable functions as well. |
| 31 | * A script group may take inputs and generate outputs, which are consumed and |
| 32 | * produced by its member kernels. |
| 33 | * Inside a script group, outputs from one kernel can be passed to another kernel as inputs. |
| 34 | * The API disallows cyclic dependencies among kernels in a script group, |
| 35 | * effectively making it a directed acyclic graph (DAG) of kernels. |
Tim Murray | 2a60389 | 2012-10-10 14:21:46 -0700 | [diff] [blame] | 36 | * <p> |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 37 | * Grouping kernels together allows for more efficient execution. For example, |
| 38 | * runtime and compiler optimization can be applied to reduce computation and |
| 39 | * communication overhead, and to make better use of the CPU and the GPU. |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 40 | **/ |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 41 | public final class ScriptGroup extends BaseObj { |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 42 | private static final String TAG = "ScriptGroup"; |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 43 | IO mOutputs[]; |
| 44 | IO mInputs[]; |
| 45 | |
| 46 | static class IO { |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 47 | Script.KernelID mKID; |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 48 | Allocation mAllocation; |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 49 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 50 | IO(Script.KernelID s) { |
| 51 | mKID = s; |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 55 | static class ConnectLine { |
| 56 | ConnectLine(Type t, Script.KernelID from, Script.KernelID to) { |
| 57 | mFrom = from; |
| 58 | mToK = to; |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 59 | mAllocationType = t; |
| 60 | } |
| 61 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 62 | ConnectLine(Type t, Script.KernelID from, Script.FieldID to) { |
| 63 | mFrom = from; |
| 64 | mToF = to; |
| 65 | mAllocationType = t; |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 66 | } |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 67 | |
| 68 | Script.FieldID mToF; |
| 69 | Script.KernelID mToK; |
| 70 | Script.KernelID mFrom; |
| 71 | Type mAllocationType; |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static class Node { |
| 75 | Script mScript; |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 76 | ArrayList<Script.KernelID> mKernels = new ArrayList<Script.KernelID>(); |
| 77 | ArrayList<ConnectLine> mInputs = new ArrayList<ConnectLine>(); |
| 78 | ArrayList<ConnectLine> mOutputs = new ArrayList<ConnectLine>(); |
Tim Murray | 2a60389 | 2012-10-10 14:21:46 -0700 | [diff] [blame] | 79 | int dagNumber; |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 80 | |
| 81 | Node mNext; |
| 82 | |
| 83 | Node(Script s) { |
| 84 | mScript = s; |
| 85 | } |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 89 | /** |
| 90 | * An opaque class for closures |
| 91 | * <p> |
| 92 | * A closure represents a function call to a kernel or invocable function, |
| 93 | * combined with arguments and values for global variables. A closure is |
| 94 | * created using the {@link android.renderscript.ScriptGroup.Builder2#addKernel} or |
| 95 | * {@link android.renderscript.ScriptGroup.Builder2#addInvoke} |
| 96 | * method. |
| 97 | */ |
| 98 | |
| 99 | public static final class Closure extends BaseObj { |
| 100 | private Object[] mArgs; |
| 101 | private Allocation mReturnValue; |
| 102 | private Map<Script.FieldID, Object> mBindings; |
| 103 | |
| 104 | private Future mReturnFuture; |
| 105 | private Map<Script.FieldID, Future> mGlobalFuture; |
| 106 | |
| 107 | private FieldPacker mFP; |
| 108 | |
| 109 | private static final String TAG = "Closure"; |
| 110 | |
| 111 | Closure(long id, RenderScript rs) { |
| 112 | super(id, rs); |
| 113 | } |
| 114 | |
| 115 | Closure(RenderScript rs, Script.KernelID kernelID, Type returnType, |
| 116 | Object[] args, Map<Script.FieldID, Object> globals) { |
| 117 | super(0, rs); |
| 118 | |
| 119 | mArgs = args; |
| 120 | mReturnValue = Allocation.createTyped(rs, returnType); |
| 121 | mBindings = globals; |
| 122 | mGlobalFuture = new HashMap<Script.FieldID, Future>(); |
| 123 | |
| 124 | int numValues = args.length + globals.size(); |
| 125 | |
| 126 | long[] fieldIDs = new long[numValues]; |
| 127 | long[] values = new long[numValues]; |
| 128 | int[] sizes = new int[numValues]; |
| 129 | long[] depClosures = new long[numValues]; |
| 130 | long[] depFieldIDs = new long[numValues]; |
| 131 | |
| 132 | int i; |
| 133 | for (i = 0; i < args.length; i++) { |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 134 | fieldIDs[i] = 0; |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 135 | retrieveValueAndDependenceInfo(rs, i, null, args[i], |
| 136 | values, sizes, depClosures, depFieldIDs); |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 137 | } |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 138 | for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) { |
| 139 | Object obj = entry.getValue(); |
| 140 | Script.FieldID fieldID = entry.getKey(); |
| 141 | fieldIDs[i] = fieldID.getID(rs); |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 142 | retrieveValueAndDependenceInfo(rs, i, fieldID, obj, |
| 143 | values, sizes, depClosures, depFieldIDs); |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 144 | i++; |
| 145 | } |
| 146 | |
| 147 | long id = rs.nClosureCreate(kernelID.getID(rs), mReturnValue.getID(rs), |
| 148 | fieldIDs, values, sizes, depClosures, depFieldIDs); |
| 149 | |
| 150 | setID(id); |
Yang Ni | 6484b6b | 2016-03-24 09:40:32 -0700 | [diff] [blame] | 151 | |
| 152 | guard.open("destroy"); |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | Closure(RenderScript rs, Script.InvokeID invokeID, |
| 156 | Object[] args, Map<Script.FieldID, Object> globals) { |
| 157 | super(0, rs); |
| 158 | mFP = FieldPacker.createFromArray(args); |
| 159 | |
| 160 | mArgs = args; |
| 161 | mBindings = globals; |
| 162 | mGlobalFuture = new HashMap<Script.FieldID, Future>(); |
| 163 | |
| 164 | int numValues = globals.size(); |
| 165 | |
| 166 | long[] fieldIDs = new long[numValues]; |
| 167 | long[] values = new long[numValues]; |
| 168 | int[] sizes = new int[numValues]; |
| 169 | long[] depClosures = new long[numValues]; |
| 170 | long[] depFieldIDs = new long[numValues]; |
| 171 | |
| 172 | int i = 0; |
| 173 | for (Map.Entry<Script.FieldID, Object> entry : globals.entrySet()) { |
| 174 | Object obj = entry.getValue(); |
| 175 | Script.FieldID fieldID = entry.getKey(); |
| 176 | fieldIDs[i] = fieldID.getID(rs); |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 177 | retrieveValueAndDependenceInfo(rs, i, fieldID, obj, values, |
| 178 | sizes, depClosures, depFieldIDs); |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 179 | i++; |
| 180 | } |
| 181 | |
| 182 | long id = rs.nInvokeClosureCreate(invokeID.getID(rs), mFP.getData(), fieldIDs, |
| 183 | values, sizes); |
| 184 | |
| 185 | setID(id); |
Yang Ni | 6484b6b | 2016-03-24 09:40:32 -0700 | [diff] [blame] | 186 | |
| 187 | guard.open("destroy"); |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Yang Ni | 44d1b3b | 2016-04-04 17:09:11 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Destroys this Closure and the Allocation for its return value |
| 192 | */ |
| 193 | public void destroy() { |
| 194 | super.destroy(); |
| 195 | if (mReturnValue != null) { |
| 196 | mReturnValue.destroy(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | protected void finalize() throws Throwable { |
| 201 | // Set null mReturnValue to avoid double-destroying it, in case its |
| 202 | // finalizer races ahead. |
| 203 | mReturnValue = null; |
| 204 | super.finalize(); |
| 205 | } |
| 206 | |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 207 | private void retrieveValueAndDependenceInfo(RenderScript rs, |
| 208 | int index, Script.FieldID fid, Object obj, |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 209 | long[] values, int[] sizes, |
| 210 | long[] depClosures, |
| 211 | long[] depFieldIDs) { |
| 212 | |
| 213 | if (obj instanceof Future) { |
| 214 | Future f = (Future)obj; |
| 215 | obj = f.getValue(); |
| 216 | depClosures[index] = f.getClosure().getID(rs); |
| 217 | Script.FieldID fieldID = f.getFieldID(); |
| 218 | depFieldIDs[index] = fieldID != null ? fieldID.getID(rs) : 0; |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 219 | } else { |
| 220 | depClosures[index] = 0; |
| 221 | depFieldIDs[index] = 0; |
| 222 | } |
| 223 | |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 224 | if (obj instanceof Input) { |
| 225 | Input unbound = (Input)obj; |
| 226 | if (index < mArgs.length) { |
| 227 | unbound.addReference(this, index); |
| 228 | } else { |
| 229 | unbound.addReference(this, fid); |
| 230 | } |
| 231 | values[index] = 0; |
| 232 | sizes[index] = 0; |
| 233 | } else { |
| 234 | ValueAndSize vs = new ValueAndSize(rs, obj); |
| 235 | values[index] = vs.value; |
| 236 | sizes[index] = vs.size; |
| 237 | } |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Returns the future for the return value |
| 242 | * |
| 243 | * @return a future |
| 244 | */ |
| 245 | |
| 246 | public Future getReturn() { |
| 247 | if (mReturnFuture == null) { |
| 248 | mReturnFuture = new Future(this, null, mReturnValue); |
| 249 | } |
| 250 | |
| 251 | return mReturnFuture; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Returns the future for a global variable |
| 256 | * |
| 257 | * @param field the field ID for the global variable |
| 258 | * @return a future |
| 259 | */ |
| 260 | |
| 261 | public Future getGlobal(Script.FieldID field) { |
| 262 | Future f = mGlobalFuture.get(field); |
| 263 | |
| 264 | if (f == null) { |
| 265 | // If the field is not bound to this closure, this will return a future |
| 266 | // without an associated value (reference). So this is not working for |
| 267 | // cross-module (cross-script) linking in this case where a field not |
| 268 | // explicitly bound. |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 269 | Object obj = mBindings.get(field); |
| 270 | if (obj instanceof Future) { |
| 271 | obj = ((Future)obj).getValue(); |
| 272 | } |
| 273 | f = new Future(this, field, obj); |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 274 | mGlobalFuture.put(field, f); |
| 275 | } |
| 276 | |
| 277 | return f; |
| 278 | } |
| 279 | |
| 280 | void setArg(int index, Object obj) { |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 281 | if (obj instanceof Future) { |
| 282 | obj = ((Future)obj).getValue(); |
| 283 | } |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 284 | mArgs[index] = obj; |
| 285 | ValueAndSize vs = new ValueAndSize(mRS, obj); |
| 286 | mRS.nClosureSetArg(getID(mRS), index, vs.value, vs.size); |
| 287 | } |
| 288 | |
| 289 | void setGlobal(Script.FieldID fieldID, Object obj) { |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 290 | if (obj instanceof Future) { |
| 291 | obj = ((Future)obj).getValue(); |
| 292 | } |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 293 | mBindings.put(fieldID, obj); |
| 294 | ValueAndSize vs = new ValueAndSize(mRS, obj); |
| 295 | mRS.nClosureSetGlobal(getID(mRS), fieldID.getID(mRS), vs.value, vs.size); |
| 296 | } |
| 297 | |
| 298 | private static final class ValueAndSize { |
| 299 | public ValueAndSize(RenderScript rs, Object obj) { |
| 300 | if (obj instanceof Allocation) { |
| 301 | value = ((Allocation)obj).getID(rs); |
Yang Ni | 263cc90 | 2015-11-10 13:27:04 -0800 | [diff] [blame] | 302 | // Special value for size to tell the runtime and driver that |
| 303 | // the value is an Allocation |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 304 | size = -1; |
| 305 | } else if (obj instanceof Boolean) { |
| 306 | value = ((Boolean)obj).booleanValue() ? 1 : 0; |
| 307 | size = 4; |
| 308 | } else if (obj instanceof Integer) { |
| 309 | value = ((Integer)obj).longValue(); |
| 310 | size = 4; |
| 311 | } else if (obj instanceof Long) { |
| 312 | value = ((Long)obj).longValue(); |
| 313 | size = 8; |
| 314 | } else if (obj instanceof Float) { |
Yang Ni | 263cc90 | 2015-11-10 13:27:04 -0800 | [diff] [blame] | 315 | value = Float.floatToRawIntBits(((Float)obj).floatValue()); |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 316 | size = 4; |
| 317 | } else if (obj instanceof Double) { |
Yang Ni | 263cc90 | 2015-11-10 13:27:04 -0800 | [diff] [blame] | 318 | value = Double.doubleToRawLongBits(((Double)obj).doubleValue()); |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 319 | size = 8; |
| 320 | } |
| 321 | } |
| 322 | public long value; |
| 323 | public int size; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * An opaque class for futures |
| 329 | * <p> |
| 330 | * A future represents an output of a closure, either the return value of |
| 331 | * the function, or the value of a global variable written by the function. |
| 332 | * A future is created by calling the {@link Closure#getReturn} or |
| 333 | * {@link Closure#getGlobal} method. |
| 334 | */ |
| 335 | |
| 336 | public static final class Future { |
| 337 | Closure mClosure; |
| 338 | Script.FieldID mFieldID; |
| 339 | Object mValue; |
| 340 | |
| 341 | Future(Closure closure, Script.FieldID fieldID, Object value) { |
| 342 | mClosure = closure; |
| 343 | mFieldID = fieldID; |
| 344 | mValue = value; |
| 345 | } |
| 346 | |
| 347 | Closure getClosure() { return mClosure; } |
| 348 | Script.FieldID getFieldID() { return mFieldID; } |
| 349 | Object getValue() { return mValue; } |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * An opaque class for script group inputs |
| 354 | * <p> |
| 355 | * Created by calling the {@link Builder2#addInput} method. The value |
| 356 | * is assigned in {@link ScriptGroup#execute(Object...)} method as |
| 357 | * one of its arguments. Arguments to the execute method should be in |
| 358 | * the same order as intputs are added using the addInput method. |
| 359 | */ |
| 360 | |
| 361 | public static final class Input { |
| 362 | // Either mFieldID or mArgIndex should be set but not both. |
| 363 | List<Pair<Closure, Script.FieldID>> mFieldID; |
| 364 | // -1 means unset. Legal values are 0 .. n-1, where n is the number of |
| 365 | // arguments for the referencing closure. |
| 366 | List<Pair<Closure, Integer>> mArgIndex; |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 367 | Object mValue; |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 368 | |
| 369 | Input() { |
| 370 | mFieldID = new ArrayList<Pair<Closure, Script.FieldID>>(); |
| 371 | mArgIndex = new ArrayList<Pair<Closure, Integer>>(); |
| 372 | } |
| 373 | |
| 374 | void addReference(Closure closure, int index) { |
| 375 | mArgIndex.add(Pair.create(closure, Integer.valueOf(index))); |
| 376 | } |
| 377 | |
| 378 | void addReference(Closure closure, Script.FieldID fieldID) { |
| 379 | mFieldID.add(Pair.create(closure, fieldID)); |
| 380 | } |
| 381 | |
| 382 | void set(Object value) { |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 383 | mValue = value; |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 384 | for (Pair<Closure, Integer> p : mArgIndex) { |
| 385 | Closure closure = p.first; |
| 386 | int index = p.second.intValue(); |
| 387 | closure.setArg(index, value); |
| 388 | } |
| 389 | for (Pair<Closure, Script.FieldID> p : mFieldID) { |
| 390 | Closure closure = p.first; |
| 391 | Script.FieldID fieldID = p.second; |
| 392 | closure.setGlobal(fieldID, value); |
| 393 | } |
| 394 | } |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 395 | |
| 396 | Object get() { return mValue; } |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | private String mName; |
| 400 | private List<Closure> mClosures; |
| 401 | private List<Input> mInputs2; |
| 402 | private Future[] mOutputs2; |
| 403 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 404 | ScriptGroup(long id, RenderScript rs) { |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 405 | super(id, rs); |
Yang Ni | 6484b6b | 2016-03-24 09:40:32 -0700 | [diff] [blame] | 406 | guard.open("destroy"); |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 409 | ScriptGroup(RenderScript rs, String name, List<Closure> closures, |
| 410 | List<Input> inputs, Future[] outputs) { |
| 411 | super(0, rs); |
| 412 | mName = name; |
| 413 | mClosures = closures; |
| 414 | mInputs2 = inputs; |
| 415 | mOutputs2 = outputs; |
| 416 | |
| 417 | long[] closureIDs = new long[closures.size()]; |
| 418 | for (int i = 0; i < closureIDs.length; i++) { |
| 419 | closureIDs[i] = closures.get(i).getID(rs); |
| 420 | } |
Yang Ni | 15fcf61 | 2016-03-10 16:12:31 -0800 | [diff] [blame] | 421 | long id = rs.nScriptGroup2Create(name, RenderScript.getCachePath(), closureIDs); |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 422 | setID(id); |
Yang Ni | 6484b6b | 2016-03-24 09:40:32 -0700 | [diff] [blame] | 423 | guard.open("destroy"); |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Executes a script group |
| 428 | * |
Yang Ni | 4356389 | 2015-05-12 13:53:38 -0700 | [diff] [blame] | 429 | * @param inputs Values for inputs to the script group, in the order as the |
| 430 | * inputs are added via {@link Builder2#addInput}. |
| 431 | * @return Outputs of the script group as an array of objects, in the order |
| 432 | * as futures are passed to {@link Builder2#create}. |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 433 | */ |
| 434 | |
| 435 | public Object[] execute(Object... inputs) { |
| 436 | if (inputs.length < mInputs2.size()) { |
| 437 | Log.e(TAG, this.toString() + " receives " + inputs.length + " inputs, " + |
| 438 | "less than expected " + mInputs2.size()); |
| 439 | return null; |
| 440 | } |
| 441 | |
| 442 | if (inputs.length > mInputs2.size()) { |
| 443 | Log.i(TAG, this.toString() + " receives " + inputs.length + " inputs, " + |
| 444 | "more than expected " + mInputs2.size()); |
| 445 | } |
| 446 | |
| 447 | for (int i = 0; i < mInputs2.size(); i++) { |
| 448 | Object obj = inputs[i]; |
| 449 | if (obj instanceof Future || obj instanceof Input) { |
| 450 | Log.e(TAG, this.toString() + ": input " + i + |
| 451 | " is a future or unbound value"); |
| 452 | return null; |
| 453 | } |
| 454 | Input unbound = mInputs2.get(i); |
| 455 | unbound.set(obj); |
| 456 | } |
| 457 | |
| 458 | mRS.nScriptGroup2Execute(getID(mRS)); |
| 459 | |
| 460 | Object[] outputObjs = new Object[mOutputs2.length]; |
| 461 | int i = 0; |
| 462 | for (Future f : mOutputs2) { |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 463 | Object output = f.getValue(); |
| 464 | if (output instanceof Input) { |
| 465 | output = ((Input)output).get(); |
| 466 | } |
| 467 | outputObjs[i++] = output; |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 468 | } |
| 469 | return outputObjs; |
| 470 | } |
| 471 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 472 | /** |
| 473 | * Sets an input of the ScriptGroup. This specifies an |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 474 | * Allocation to be used for kernels that require an input |
| 475 | * Allocation provided from outside of the ScriptGroup. |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 476 | * |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 477 | * @deprecated Set arguments to {@link #execute(Object...)} instead. |
| 478 | * |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 479 | * @param s The ID of the kernel where the allocation should be |
| 480 | * connected. |
| 481 | * @param a The allocation to connect. |
| 482 | */ |
| 483 | public void setInput(Script.KernelID s, Allocation a) { |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 484 | for (int ct=0; ct < mInputs.length; ct++) { |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 485 | if (mInputs[ct].mKID == s) { |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 486 | mInputs[ct].mAllocation = a; |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 487 | mRS.nScriptGroupSetInput(getID(mRS), s.getID(mRS), mRS.safeID(a)); |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 488 | return; |
| 489 | } |
| 490 | } |
| 491 | throw new RSIllegalArgumentException("Script not found"); |
| 492 | } |
| 493 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 494 | /** |
| 495 | * Sets an output of the ScriptGroup. This specifies an |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 496 | * Allocation to be used for the kernels that require an output |
| 497 | * Allocation visible after the ScriptGroup is executed. |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 498 | * |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 499 | * @deprecated Use return value of {@link #execute(Object...)} instead. |
| 500 | * |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 501 | * @param s The ID of the kernel where the allocation should be |
| 502 | * connected. |
| 503 | * @param a The allocation to connect. |
| 504 | */ |
| 505 | public void setOutput(Script.KernelID s, Allocation a) { |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 506 | for (int ct=0; ct < mOutputs.length; ct++) { |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 507 | if (mOutputs[ct].mKID == s) { |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 508 | mOutputs[ct].mAllocation = a; |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 509 | mRS.nScriptGroupSetOutput(getID(mRS), s.getID(mRS), mRS.safeID(a)); |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 510 | return; |
| 511 | } |
| 512 | } |
| 513 | throw new RSIllegalArgumentException("Script not found"); |
| 514 | } |
| 515 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 516 | /** |
| 517 | * Execute the ScriptGroup. This will run all the kernels in |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 518 | * the ScriptGroup. No internal connection results will be visible |
| 519 | * after execution of the ScriptGroup. |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 520 | * |
| 521 | * @deprecated Use {@link #execute} instead. |
| 522 | * |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 523 | */ |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 524 | public void execute() { |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 525 | mRS.nScriptGroupExecute(getID(mRS)); |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 529 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 530 | * Helper class to build a ScriptGroup. A ScriptGroup is |
| 531 | * created in two steps. |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 532 | * <p> |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 533 | * First, all kernels to be used by the ScriptGroup should be added. |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 534 | * <p> |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 535 | * Second, add connections between kernels. There are two types |
| 536 | * of connections: kernel to kernel and kernel to field. |
| 537 | * Kernel to kernel allows a kernel's output to be passed to |
| 538 | * another kernel as input. Kernel to field allows the output of |
| 539 | * one kernel to be bound as a script global. Kernel to kernel is |
| 540 | * higher performance and should be used where possible. |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 541 | * <p> |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 542 | * A ScriptGroup must contain a single directed acyclic graph (DAG); it |
| 543 | * cannot contain cycles. Currently, all kernels used in a ScriptGroup |
| 544 | * must come from different Script objects. Additionally, all kernels |
| 545 | * in a ScriptGroup must have at least one input, output, or internal |
| 546 | * connection. |
| 547 | * <p> |
| 548 | * Once all connections are made, a call to {@link #create} will |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 549 | * return the ScriptGroup object. |
| 550 | * |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 551 | * @deprecated Use {@link Builder2} instead. |
| 552 | * |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 553 | */ |
| 554 | public static final class Builder { |
| 555 | private RenderScript mRS; |
| 556 | private ArrayList<Node> mNodes = new ArrayList<Node>(); |
| 557 | private ArrayList<ConnectLine> mLines = new ArrayList<ConnectLine>(); |
| 558 | private int mKernelCount; |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 559 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 560 | /** |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 561 | * Create a Builder for generating a ScriptGroup. |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 562 | * |
| 563 | * |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 564 | * @param rs The RenderScript context. |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 565 | */ |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 566 | public Builder(RenderScript rs) { |
| 567 | mRS = rs; |
| 568 | } |
| 569 | |
Tim Murray | 091f7cc | 2012-10-12 12:02:18 -0700 | [diff] [blame] | 570 | // do a DFS from original node, looking for original node |
| 571 | // any cycle that could be created must contain original node |
| 572 | private void validateCycle(Node target, Node original) { |
| 573 | for (int ct = 0; ct < target.mOutputs.size(); ct++) { |
| 574 | final ConnectLine cl = target.mOutputs.get(ct); |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 575 | if (cl.mToK != null) { |
| 576 | Node tn = findNode(cl.mToK.mScript); |
Tim Murray | 091f7cc | 2012-10-12 12:02:18 -0700 | [diff] [blame] | 577 | if (tn.equals(original)) { |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 578 | throw new RSInvalidStateException("Loops in group not allowed."); |
| 579 | } |
Tim Murray | 091f7cc | 2012-10-12 12:02:18 -0700 | [diff] [blame] | 580 | validateCycle(tn, original); |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 581 | } |
| 582 | if (cl.mToF != null) { |
| 583 | Node tn = findNode(cl.mToF.mScript); |
Tim Murray | 091f7cc | 2012-10-12 12:02:18 -0700 | [diff] [blame] | 584 | if (tn.equals(original)) { |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 585 | throw new RSInvalidStateException("Loops in group not allowed."); |
| 586 | } |
Tim Murray | 091f7cc | 2012-10-12 12:02:18 -0700 | [diff] [blame] | 587 | validateCycle(tn, original); |
Tim Murray | 2a60389 | 2012-10-10 14:21:46 -0700 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | private void mergeDAGs(int valueUsed, int valueKilled) { |
| 593 | for (int ct=0; ct < mNodes.size(); ct++) { |
| 594 | if (mNodes.get(ct).dagNumber == valueKilled) |
| 595 | mNodes.get(ct).dagNumber = valueUsed; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | private void validateDAGRecurse(Node n, int dagNumber) { |
| 600 | // combine DAGs if this node has been seen already |
| 601 | if (n.dagNumber != 0 && n.dagNumber != dagNumber) { |
| 602 | mergeDAGs(n.dagNumber, dagNumber); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | n.dagNumber = dagNumber; |
| 607 | for (int ct=0; ct < n.mOutputs.size(); ct++) { |
| 608 | final ConnectLine cl = n.mOutputs.get(ct); |
| 609 | if (cl.mToK != null) { |
| 610 | Node tn = findNode(cl.mToK.mScript); |
| 611 | validateDAGRecurse(tn, dagNumber); |
| 612 | } |
| 613 | if (cl.mToF != null) { |
| 614 | Node tn = findNode(cl.mToF.mScript); |
| 615 | validateDAGRecurse(tn, dagNumber); |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | private void validateDAG() { |
| 621 | for (int ct=0; ct < mNodes.size(); ct++) { |
| 622 | Node n = mNodes.get(ct); |
| 623 | if (n.mInputs.size() == 0) { |
| 624 | if (n.mOutputs.size() == 0 && mNodes.size() > 1) { |
Yang Ni | 870767e | 2015-05-18 10:56:47 -0700 | [diff] [blame] | 625 | String msg = "Groups cannot contain unconnected scripts"; |
| 626 | throw new RSInvalidStateException(msg); |
Tim Murray | 2a60389 | 2012-10-10 14:21:46 -0700 | [diff] [blame] | 627 | } |
| 628 | validateDAGRecurse(n, ct+1); |
| 629 | } |
| 630 | } |
| 631 | int dagNumber = mNodes.get(0).dagNumber; |
| 632 | for (int ct=0; ct < mNodes.size(); ct++) { |
| 633 | if (mNodes.get(ct).dagNumber != dagNumber) { |
| 634 | throw new RSInvalidStateException("Multiple DAGs in group not allowed."); |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 635 | } |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 639 | private Node findNode(Script s) { |
| 640 | for (int ct=0; ct < mNodes.size(); ct++) { |
| 641 | if (s == mNodes.get(ct).mScript) { |
| 642 | return mNodes.get(ct); |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 643 | } |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 644 | } |
| 645 | return null; |
| 646 | } |
| 647 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 648 | private Node findNode(Script.KernelID k) { |
| 649 | for (int ct=0; ct < mNodes.size(); ct++) { |
| 650 | Node n = mNodes.get(ct); |
| 651 | for (int ct2=0; ct2 < n.mKernels.size(); ct2++) { |
| 652 | if (k == n.mKernels.get(ct2)) { |
| 653 | return n; |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 654 | } |
| 655 | } |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 656 | } |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 657 | return null; |
| 658 | } |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 659 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 660 | /** |
| 661 | * Adds a Kernel to the group. |
| 662 | * |
| 663 | * |
| 664 | * @param k The kernel to add. |
| 665 | * |
| 666 | * @return Builder Returns this. |
| 667 | */ |
| 668 | public Builder addKernel(Script.KernelID k) { |
| 669 | if (mLines.size() != 0) { |
| 670 | throw new RSInvalidStateException( |
| 671 | "Kernels may not be added once connections exist."); |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 672 | } |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 673 | |
| 674 | //android.util.Log.v("RSR", "addKernel 1 k=" + k); |
| 675 | if (findNode(k) != null) { |
| 676 | return this; |
| 677 | } |
| 678 | //android.util.Log.v("RSR", "addKernel 2 "); |
| 679 | mKernelCount++; |
| 680 | Node n = findNode(k.mScript); |
| 681 | if (n == null) { |
| 682 | //android.util.Log.v("RSR", "addKernel 3 "); |
| 683 | n = new Node(k.mScript); |
| 684 | mNodes.add(n); |
| 685 | } |
| 686 | n.mKernels.add(k); |
| 687 | return this; |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Adds a connection to the group. |
| 692 | * |
| 693 | * |
| 694 | * @param t The type of the connection. This is used to |
| 695 | * determine the kernel launch sizes on the source side |
| 696 | * of this connection. |
| 697 | * @param from The source for the connection. |
| 698 | * @param to The destination of the connection. |
| 699 | * |
| 700 | * @return Builder Returns this |
| 701 | */ |
| 702 | public Builder addConnection(Type t, Script.KernelID from, Script.FieldID to) { |
| 703 | //android.util.Log.v("RSR", "addConnection " + t +", " + from + ", " + to); |
| 704 | |
| 705 | Node nf = findNode(from); |
| 706 | if (nf == null) { |
Tim Murray | 091f7cc | 2012-10-12 12:02:18 -0700 | [diff] [blame] | 707 | throw new RSInvalidStateException("From script not found."); |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | Node nt = findNode(to.mScript); |
| 711 | if (nt == null) { |
| 712 | throw new RSInvalidStateException("To script not found."); |
| 713 | } |
| 714 | |
| 715 | ConnectLine cl = new ConnectLine(t, from, to); |
| 716 | mLines.add(new ConnectLine(t, from, to)); |
| 717 | |
| 718 | nf.mOutputs.add(cl); |
| 719 | nt.mInputs.add(cl); |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 720 | |
Tim Murray | 091f7cc | 2012-10-12 12:02:18 -0700 | [diff] [blame] | 721 | validateCycle(nf, nf); |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 722 | return this; |
| 723 | } |
| 724 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 725 | /** |
| 726 | * Adds a connection to the group. |
| 727 | * |
| 728 | * |
| 729 | * @param t The type of the connection. This is used to |
| 730 | * determine the kernel launch sizes for both sides of |
| 731 | * this connection. |
| 732 | * @param from The source for the connection. |
| 733 | * @param to The destination of the connection. |
| 734 | * |
| 735 | * @return Builder Returns this |
| 736 | */ |
| 737 | public Builder addConnection(Type t, Script.KernelID from, Script.KernelID to) { |
| 738 | //android.util.Log.v("RSR", "addConnection " + t +", " + from + ", " + to); |
| 739 | |
| 740 | Node nf = findNode(from); |
| 741 | if (nf == null) { |
Tim Murray | 091f7cc | 2012-10-12 12:02:18 -0700 | [diff] [blame] | 742 | throw new RSInvalidStateException("From script not found."); |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | Node nt = findNode(to); |
| 746 | if (nt == null) { |
| 747 | throw new RSInvalidStateException("To script not found."); |
| 748 | } |
| 749 | |
| 750 | ConnectLine cl = new ConnectLine(t, from, to); |
| 751 | mLines.add(new ConnectLine(t, from, to)); |
| 752 | |
| 753 | nf.mOutputs.add(cl); |
| 754 | nt.mInputs.add(cl); |
| 755 | |
Tim Murray | 091f7cc | 2012-10-12 12:02:18 -0700 | [diff] [blame] | 756 | validateCycle(nf, nf); |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 757 | return this; |
| 758 | } |
| 759 | |
| 760 | |
| 761 | |
| 762 | /** |
| 763 | * Creates the Script group. |
| 764 | * |
| 765 | * |
| 766 | * @return ScriptGroup The new ScriptGroup |
| 767 | */ |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 768 | public ScriptGroup create() { |
Tim Murray | 2a60389 | 2012-10-10 14:21:46 -0700 | [diff] [blame] | 769 | |
| 770 | if (mNodes.size() == 0) { |
| 771 | throw new RSInvalidStateException("Empty script groups are not allowed"); |
| 772 | } |
| 773 | |
| 774 | // reset DAG numbers in case we're building a second group |
| 775 | for (int ct=0; ct < mNodes.size(); ct++) { |
| 776 | mNodes.get(ct).dagNumber = 0; |
| 777 | } |
| 778 | validateDAG(); |
| 779 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 780 | ArrayList<IO> inputs = new ArrayList<IO>(); |
| 781 | ArrayList<IO> outputs = new ArrayList<IO>(); |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 782 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 783 | long[] kernels = new long[mKernelCount]; |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 784 | int idx = 0; |
| 785 | for (int ct=0; ct < mNodes.size(); ct++) { |
| 786 | Node n = mNodes.get(ct); |
| 787 | for (int ct2=0; ct2 < n.mKernels.size(); ct2++) { |
| 788 | final Script.KernelID kid = n.mKernels.get(ct2); |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 789 | kernels[idx++] = kid.getID(mRS); |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 790 | |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 791 | boolean hasInput = false; |
| 792 | boolean hasOutput = false; |
| 793 | for (int ct3=0; ct3 < n.mInputs.size(); ct3++) { |
| 794 | if (n.mInputs.get(ct3).mToK == kid) { |
| 795 | hasInput = true; |
| 796 | } |
| 797 | } |
| 798 | for (int ct3=0; ct3 < n.mOutputs.size(); ct3++) { |
| 799 | if (n.mOutputs.get(ct3).mFrom == kid) { |
| 800 | hasOutput = true; |
| 801 | } |
| 802 | } |
| 803 | if (!hasInput) { |
| 804 | inputs.add(new IO(kid)); |
| 805 | } |
| 806 | if (!hasOutput) { |
| 807 | outputs.add(new IO(kid)); |
| 808 | } |
| 809 | |
| 810 | } |
| 811 | } |
| 812 | if (idx != mKernelCount) { |
| 813 | throw new RSRuntimeException("Count mismatch, should not happen."); |
| 814 | } |
| 815 | |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 816 | long[] src = new long[mLines.size()]; |
| 817 | long[] dstk = new long[mLines.size()]; |
| 818 | long[] dstf = new long[mLines.size()]; |
| 819 | long[] types = new long[mLines.size()]; |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 820 | |
| 821 | for (int ct=0; ct < mLines.size(); ct++) { |
| 822 | ConnectLine cl = mLines.get(ct); |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 823 | src[ct] = cl.mFrom.getID(mRS); |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 824 | if (cl.mToK != null) { |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 825 | dstk[ct] = cl.mToK.getID(mRS); |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 826 | } |
| 827 | if (cl.mToF != null) { |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 828 | dstf[ct] = cl.mToF.getID(mRS); |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 829 | } |
Ashok Bhat | 9807155 | 2014-02-12 09:54:43 +0000 | [diff] [blame] | 830 | types[ct] = cl.mAllocationType.getID(mRS); |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Tim Murray | 460a049 | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 833 | long id = mRS.nScriptGroupCreate(kernels, src, dstk, dstf, types); |
Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 834 | if (id == 0) { |
| 835 | throw new RSRuntimeException("Object creation error, should not happen."); |
| 836 | } |
| 837 | |
| 838 | ScriptGroup sg = new ScriptGroup(id, mRS); |
| 839 | sg.mOutputs = new IO[outputs.size()]; |
| 840 | for (int ct=0; ct < outputs.size(); ct++) { |
| 841 | sg.mOutputs[ct] = outputs.get(ct); |
| 842 | } |
| 843 | |
| 844 | sg.mInputs = new IO[inputs.size()]; |
| 845 | for (int ct=0; ct < inputs.size(); ct++) { |
| 846 | sg.mInputs[ct] = inputs.get(ct); |
| 847 | } |
| 848 | |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 849 | return sg; |
| 850 | } |
| 851 | |
| 852 | } |
| 853 | |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 854 | /** |
| 855 | * Represents a binding of a value to a global variable in a |
| 856 | * kernel or invocable function. Used in closure creation. |
| 857 | */ |
| 858 | |
| 859 | public static final class Binding { |
| 860 | private final Script.FieldID mField; |
| 861 | private final Object mValue; |
| 862 | |
| 863 | /** |
| 864 | * Returns a Binding object that binds value to field |
| 865 | * |
| 866 | * @param field the Script.FieldID of the global variable |
| 867 | * @param value the value |
| 868 | */ |
| 869 | |
| 870 | public Binding(Script.FieldID field, Object value) { |
| 871 | mField = field; |
| 872 | mValue = value; |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * Returns the field ID |
| 877 | */ |
| 878 | |
Yang Ni | 77eba48 | 2015-05-15 09:56:49 -0700 | [diff] [blame] | 879 | Script.FieldID getField() { return mField; } |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 880 | |
| 881 | /** |
| 882 | * Returns the value |
| 883 | */ |
| 884 | |
Yang Ni | 77eba48 | 2015-05-15 09:56:49 -0700 | [diff] [blame] | 885 | Object getValue() { return mValue; } |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /** |
| 889 | * The builder class for creating script groups |
| 890 | * <p> |
| 891 | * A script group is created using closures (see class {@link Closure}). |
| 892 | * A closure is a function call to a kernel or |
| 893 | * invocable function. Each function argument or global variable accessed inside |
| 894 | * the function is bound to 1) a known value, 2) a script group input |
| 895 | * (see class {@link Input}), or 3) a |
| 896 | * future (see class {@link Future}). |
| 897 | * A future is the output of a closure, either the return value of the |
| 898 | * function or a global variable written by that function. |
| 899 | * <p> |
| 900 | * Closures are created using the {@link #addKernel} or {@link #addInvoke} |
| 901 | * methods. |
| 902 | * When a closure is created, futures from previously created closures |
| 903 | * can be used as its inputs. |
| 904 | * External script group inputs can be used as inputs to individual closures as well. |
| 905 | * An external script group input is created using the {@link #addInput} method. |
| 906 | * A script group is created by a call to the {@link #create} method, which |
| 907 | * accepts an array of futures as the outputs for the script group. |
| 908 | * <p> |
| 909 | * Closures in a script group can be evaluated in any order as long as the |
| 910 | * following conditions are met: |
| 911 | * 1) a closure must be evaluated before any other closures that take its |
| 912 | * futures as inputs; |
| 913 | * 2) all closures added before an invoke closure must be evaluated |
| 914 | * before it; |
| 915 | * and 3) all closures added after an invoke closure must be evaluated after |
| 916 | * it. |
| 917 | * As a special case, the order that the closures are added is a legal |
| 918 | * evaluation order. However, other evaluation orders are possible, including |
| 919 | * concurrently evaluating independent closures. |
| 920 | */ |
| 921 | |
| 922 | public static final class Builder2 { |
| 923 | RenderScript mRS; |
| 924 | List<Closure> mClosures; |
| 925 | List<Input> mInputs; |
| 926 | private static final String TAG = "ScriptGroup.Builder2"; |
| 927 | |
| 928 | /** |
| 929 | * Returns a Builder object |
| 930 | * |
| 931 | * @param rs the RenderScript context |
| 932 | */ |
| 933 | public Builder2(RenderScript rs) { |
| 934 | mRS = rs; |
| 935 | mClosures = new ArrayList<Closure>(); |
| 936 | mInputs = new ArrayList<Input>(); |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Adds a closure for a kernel |
| 941 | * |
| 942 | * @param k Kernel ID for the kernel function |
| 943 | * @param returnType Allocation type for the return value |
| 944 | * @param args arguments to the kernel function |
| 945 | * @param globalBindings bindings for global variables |
| 946 | * @return a closure |
| 947 | */ |
| 948 | |
| 949 | private Closure addKernelInternal(Script.KernelID k, Type returnType, Object[] args, |
| 950 | Map<Script.FieldID, Object> globalBindings) { |
| 951 | Closure c = new Closure(mRS, k, returnType, args, globalBindings); |
| 952 | mClosures.add(c); |
| 953 | return c; |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * Adds a closure for an invocable function |
| 958 | * |
| 959 | * @param invoke Invoke ID for the invocable function |
| 960 | * @param args arguments to the invocable function |
| 961 | * @param globalBindings bindings for global variables |
| 962 | * @return a closure |
| 963 | */ |
| 964 | |
| 965 | private Closure addInvokeInternal(Script.InvokeID invoke, Object[] args, |
| 966 | Map<Script.FieldID, Object> globalBindings) { |
| 967 | Closure c = new Closure(mRS, invoke, args, globalBindings); |
| 968 | mClosures.add(c); |
| 969 | return c; |
| 970 | } |
| 971 | |
| 972 | /** |
| 973 | * Adds a script group input |
| 974 | * |
| 975 | * @return a script group input, which can be used as an argument or a value to |
| 976 | * a global variable for creating closures |
| 977 | */ |
| 978 | public Input addInput() { |
| 979 | Input unbound = new Input(); |
| 980 | mInputs.add(unbound); |
| 981 | return unbound; |
| 982 | } |
| 983 | |
| 984 | /** |
| 985 | * Adds a closure for a kernel |
| 986 | * |
| 987 | * @param k Kernel ID for the kernel function |
| 988 | * @param argsAndBindings arguments followed by bindings for global variables |
| 989 | * @return a closure |
| 990 | */ |
| 991 | |
| 992 | public Closure addKernel(Script.KernelID k, Type returnType, Object... argsAndBindings) { |
| 993 | ArrayList<Object> args = new ArrayList<Object>(); |
| 994 | Map<Script.FieldID, Object> bindingMap = new HashMap<Script.FieldID, Object>(); |
| 995 | if (!seperateArgsAndBindings(argsAndBindings, args, bindingMap)) { |
| 996 | return null; |
| 997 | } |
| 998 | return addKernelInternal(k, returnType, args.toArray(), bindingMap); |
| 999 | } |
| 1000 | |
| 1001 | /** |
| 1002 | * Adds a closure for an invocable function |
| 1003 | * |
| 1004 | * @param invoke Invoke ID for the invocable function |
| 1005 | * @param argsAndBindings arguments followed by bindings for global variables |
| 1006 | * @return a closure |
| 1007 | */ |
| 1008 | |
| 1009 | public Closure addInvoke(Script.InvokeID invoke, Object... argsAndBindings) { |
| 1010 | ArrayList<Object> args = new ArrayList<Object>(); |
| 1011 | Map<Script.FieldID, Object> bindingMap = new HashMap<Script.FieldID, Object>(); |
| 1012 | if (!seperateArgsAndBindings(argsAndBindings, args, bindingMap)) { |
| 1013 | return null; |
| 1014 | } |
| 1015 | return addInvokeInternal(invoke, args.toArray(), bindingMap); |
| 1016 | } |
| 1017 | |
| 1018 | /** |
| 1019 | * Creates a script group |
| 1020 | * |
| 1021 | * @param name name for the script group. Legal names can only contain letters, digits, |
| 1022 | * '-', or '_'. The name can be no longer than 100 characters. |
Yang Ni | dbb6fd5 | 2015-06-24 17:06:10 -0700 | [diff] [blame] | 1023 | * Try to use unique names, to avoid name conflicts and reduce |
| 1024 | * the cost of group creation. |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 1025 | * @param outputs futures intended as outputs of the script group |
| 1026 | * @return a script group |
| 1027 | */ |
| 1028 | |
| 1029 | public ScriptGroup create(String name, Future... outputs) { |
| 1030 | if (name == null || name.isEmpty() || name.length() > 100 || |
| 1031 | !name.equals(name.replaceAll("[^a-zA-Z0-9-]", "_"))) { |
| 1032 | throw new RSIllegalArgumentException("invalid script group name"); |
| 1033 | } |
| 1034 | ScriptGroup ret = new ScriptGroup(mRS, name, mClosures, mInputs, outputs); |
Yang Ni | 44d1b3b | 2016-04-04 17:09:11 -0700 | [diff] [blame] | 1035 | mClosures = new ArrayList<Closure>(); |
| 1036 | mInputs = new ArrayList<Input>(); |
Yang Ni | 18314ca | 2015-04-17 16:51:55 -0700 | [diff] [blame] | 1037 | return ret; |
| 1038 | } |
| 1039 | |
| 1040 | private boolean seperateArgsAndBindings(Object[] argsAndBindings, |
| 1041 | ArrayList<Object> args, |
| 1042 | Map<Script.FieldID, Object> bindingMap) { |
| 1043 | int i; |
| 1044 | for (i = 0; i < argsAndBindings.length; i++) { |
| 1045 | if (argsAndBindings[i] instanceof Binding) { |
| 1046 | break; |
| 1047 | } |
| 1048 | args.add(argsAndBindings[i]); |
| 1049 | } |
| 1050 | |
| 1051 | for (; i < argsAndBindings.length; i++) { |
| 1052 | if (!(argsAndBindings[i] instanceof Binding)) { |
| 1053 | return false; |
| 1054 | } |
| 1055 | Binding b = (Binding)argsAndBindings[i]; |
| 1056 | bindingMap.put(b.getField(), b.getValue()); |
| 1057 | } |
| 1058 | |
| 1059 | return true; |
| 1060 | } |
| 1061 | |
| 1062 | } |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 1063 | |
Yang Ni | 44d1b3b | 2016-04-04 17:09:11 -0700 | [diff] [blame] | 1064 | /** |
| 1065 | * Destroy this ScriptGroup and all Closures in it |
| 1066 | */ |
| 1067 | public void destroy() { |
| 1068 | super.destroy(); |
Yang Ni | 1b4df16 | 2016-04-20 21:26:24 -0700 | [diff] [blame] | 1069 | // ScriptGroup created using the old Builder class does not |
| 1070 | // initialize the field mClosures |
Yang Ni | 07837d6 | 2016-04-18 14:27:02 -0700 | [diff] [blame] | 1071 | if (mClosures != null) { |
Yang Ni | 1b4df16 | 2016-04-20 21:26:24 -0700 | [diff] [blame] | 1072 | for (Closure c : mClosures) { |
| 1073 | c.destroy(); |
| 1074 | } |
Yang Ni | 07837d6 | 2016-04-18 14:27:02 -0700 | [diff] [blame] | 1075 | } |
Yang Ni | 44d1b3b | 2016-04-04 17:09:11 -0700 | [diff] [blame] | 1076 | } |
Jason Sams | 423ebcb | 2012-08-10 15:40:53 -0700 | [diff] [blame] | 1077 | } |