| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 1 | /* |
| Stephen Hines | adeb809 | 2012-04-20 14:26:06 -0700 | [diff] [blame] | 2 | * Copyright (C) 2008-2012 The Android Open Source Project |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 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 | |
| Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 19 | import android.util.SparseArray; |
| 20 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 21 | /** |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 22 | * The parent class for all executable scripts. This should not be used by |
| 23 | * applications. |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 24 | **/ |
| 25 | public class Script extends BaseObj { |
| Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * KernelID is an identifier for a Script + root function pair. It is used |
| 29 | * as an identifier for ScriptGroup creation. |
| 30 | * |
| 31 | * This class should not be directly created. Instead use the method in the |
| 32 | * reflected or intrinsic code "getKernelID_funcname()". |
| 33 | * |
| 34 | */ |
| 35 | public static final class KernelID extends BaseObj { |
| 36 | Script mScript; |
| 37 | int mSlot; |
| 38 | int mSig; |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 39 | KernelID(long id, RenderScript rs, Script s, int slot, int sig) { |
| Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 40 | super(id, rs); |
| 41 | mScript = s; |
| 42 | mSlot = slot; |
| 43 | mSig = sig; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | private final SparseArray<KernelID> mKIDs = new SparseArray<KernelID>(); |
| 48 | /** |
| 49 | * Only to be used by generated reflected classes. |
| Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 50 | */ |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 51 | protected KernelID createKernelID(int slot, int sig, Element ein, |
| 52 | Element eout) { |
| Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 53 | KernelID k = mKIDs.get(slot); |
| 54 | if (k != null) { |
| 55 | return k; |
| 56 | } |
| 57 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 58 | long id = mRS.nScriptKernelIDCreate(getID(mRS), slot, sig); |
| Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 59 | if (id == 0) { |
| 60 | throw new RSDriverException("Failed to create KernelID"); |
| 61 | } |
| 62 | |
| 63 | k = new KernelID(id, mRS, this, slot, sig); |
| 64 | mKIDs.put(slot, k); |
| 65 | return k; |
| 66 | } |
| 67 | |
| 68 | /** |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 69 | * InvokeID is an identifier for an invoke function. It is used |
| 70 | * as an identifier for ScriptGroup creation. |
| 71 | * |
| 72 | * This class should not be directly created. Instead use the method in the |
| 73 | * reflected or intrinsic code "getInvokeID_funcname()". |
| 74 | * |
| 75 | */ |
| 76 | public static final class InvokeID extends BaseObj { |
| 77 | Script mScript; |
| 78 | int mSlot; |
| 79 | InvokeID(long id, RenderScript rs, Script s, int slot) { |
| 80 | super(id, rs); |
| 81 | mScript = s; |
| 82 | mSlot = slot; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private final SparseArray<InvokeID> mIIDs = new SparseArray<InvokeID>(); |
| 87 | /** |
| Yang Ni | be392ad | 2015-01-23 17:16:02 -0800 | [diff] [blame] | 88 | * Only to be used by generated reflected classes. |
| 89 | */ |
| 90 | protected InvokeID createInvokeID(int slot) { |
| 91 | InvokeID i = mIIDs.get(slot); |
| 92 | if (i != null) { |
| 93 | return i; |
| 94 | } |
| 95 | |
| 96 | long id = mRS.nScriptInvokeIDCreate(getID(mRS), slot); |
| 97 | if (id == 0) { |
| 98 | throw new RSDriverException("Failed to create KernelID"); |
| 99 | } |
| 100 | |
| 101 | i = new InvokeID(id, mRS, this, slot); |
| 102 | mIIDs.put(slot, i); |
| 103 | return i; |
| 104 | } |
| 105 | |
| 106 | /** |
| Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 107 | * FieldID is an identifier for a Script + exported field pair. It is used |
| 108 | * as an identifier for ScriptGroup creation. |
| 109 | * |
| 110 | * This class should not be directly created. Instead use the method in the |
| 111 | * reflected or intrinsic code "getFieldID_funcname()". |
| 112 | * |
| 113 | */ |
| 114 | public static final class FieldID extends BaseObj { |
| 115 | Script mScript; |
| 116 | int mSlot; |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 117 | FieldID(long id, RenderScript rs, Script s, int slot) { |
| Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 118 | super(id, rs); |
| 119 | mScript = s; |
| 120 | mSlot = slot; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private final SparseArray<FieldID> mFIDs = new SparseArray(); |
| 125 | /** |
| 126 | * Only to be used by generated reflected classes. |
| Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 127 | */ |
| 128 | protected FieldID createFieldID(int slot, Element e) { |
| 129 | FieldID f = mFIDs.get(slot); |
| 130 | if (f != null) { |
| 131 | return f; |
| 132 | } |
| 133 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 134 | long id = mRS.nScriptFieldIDCreate(getID(mRS), slot); |
| Jason Sams | 08a8158 | 2012-09-18 12:32:10 -0700 | [diff] [blame] | 135 | if (id == 0) { |
| 136 | throw new RSDriverException("Failed to create FieldID"); |
| 137 | } |
| 138 | |
| 139 | f = new FieldID(id, mRS, this, slot); |
| 140 | mFIDs.put(slot, f); |
| 141 | return f; |
| 142 | } |
| 143 | |
| 144 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 145 | /** |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 146 | * Only intended for use by generated reflected code. |
| 147 | * |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 148 | */ |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 149 | protected void invoke(int slot) { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 150 | mRS.nScriptInvoke(getID(mRS), slot); |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 153 | /** |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 154 | * Only intended for use by generated reflected code. |
| 155 | * |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 156 | */ |
| Jason Sams | 96ed4cf | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 157 | protected void invoke(int slot, FieldPacker v) { |
| 158 | if (v != null) { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 159 | mRS.nScriptInvokeV(getID(mRS), slot, v.getData()); |
| Jason Sams | 96ed4cf | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 160 | } else { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 161 | mRS.nScriptInvoke(getID(mRS), slot); |
| Jason Sams | 96ed4cf | 2010-06-15 12:15:57 -0700 | [diff] [blame] | 162 | } |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 165 | /** |
| Jason Sams | 6e494d3 | 2011-04-27 16:33:11 -0700 | [diff] [blame] | 166 | * Only intended for use by generated reflected code. |
| 167 | * |
| Jason Sams | 6e494d3 | 2011-04-27 16:33:11 -0700 | [diff] [blame] | 168 | */ |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 169 | protected void forEach(int slot, Allocation ain, Allocation aout, |
| 170 | FieldPacker v) { |
| 171 | forEach(slot, ain, aout, v, null); |
| Jason Sams | 6e494d3 | 2011-04-27 16:33:11 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 174 | /** |
| 175 | * Only intended for use by generated reflected code. |
| 176 | * |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 177 | */ |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 178 | protected void forEach(int slot, Allocation ain, Allocation aout, |
| 179 | FieldPacker v, LaunchOptions sc) { |
| 180 | // TODO: Is this necessary if nScriptForEach calls validate as well? |
| Jason Sams | 678cc7f | 2014-03-05 16:09:02 -0800 | [diff] [blame] | 181 | mRS.validate(); |
| 182 | mRS.validateObject(ain); |
| 183 | mRS.validateObject(aout); |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 184 | |
| Jason Sams | d1516df | 2015-05-05 18:00:34 -0700 | [diff] [blame] | 185 | if (ain == null && aout == null && sc == null) { |
| Tim Murray | eb8c29c | 2013-02-07 12:16:41 -0800 | [diff] [blame] | 186 | throw new RSIllegalArgumentException( |
| Jason Sams | d1516df | 2015-05-05 18:00:34 -0700 | [diff] [blame] | 187 | "At least one of input allocation, output allocation, or LaunchOptions is required to be non-null."); |
| Tim Murray | eb8c29c | 2013-02-07 12:16:41 -0800 | [diff] [blame] | 188 | } |
| Tim Murray | ba9dd06 | 2013-02-12 16:22:34 -0800 | [diff] [blame] | 189 | |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 190 | long[] in_ids = null; |
| Stephen Hines | c9c7daf | 2014-08-13 17:32:19 +0000 | [diff] [blame] | 191 | if (ain != null) { |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 192 | in_ids = mInIdsBuffer; |
| 193 | in_ids[0] = ain.getID(mRS); |
| Stephen Hines | c9c7daf | 2014-08-13 17:32:19 +0000 | [diff] [blame] | 194 | } |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 195 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 196 | long out_id = 0; |
| Tim Murray | eb8c29c | 2013-02-07 12:16:41 -0800 | [diff] [blame] | 197 | if (aout != null) { |
| 198 | out_id = aout.getID(mRS); |
| 199 | } |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 200 | |
| Tim Murray | eb8c29c | 2013-02-07 12:16:41 -0800 | [diff] [blame] | 201 | byte[] params = null; |
| 202 | if (v != null) { |
| 203 | params = v.getData(); |
| 204 | } |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 205 | |
| 206 | int[] limits = null; |
| 207 | if (sc != null) { |
| 208 | limits = new int[6]; |
| 209 | |
| 210 | limits[0] = sc.xstart; |
| 211 | limits[1] = sc.xend; |
| 212 | limits[2] = sc.ystart; |
| 213 | limits[3] = sc.yend; |
| 214 | limits[4] = sc.zstart; |
| 215 | limits[5] = sc.zend; |
| 216 | } |
| 217 | |
| 218 | mRS.nScriptForEach(getID(mRS), slot, in_ids, out_id, params, limits); |
| Tim Murray | eb8c29c | 2013-02-07 12:16:41 -0800 | [diff] [blame] | 219 | } |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 220 | |
| Chris Wailes | 9496106 | 2014-06-11 12:01:28 -0700 | [diff] [blame] | 221 | /** |
| 222 | * Only intended for use by generated reflected code. |
| Chris Wailes | 9496106 | 2014-06-11 12:01:28 -0700 | [diff] [blame] | 223 | */ |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 224 | protected void forEach(int slot, Allocation[] ains, Allocation aout, |
| 225 | FieldPacker v) { |
| Jason Sams | 6a420b5 | 2015-03-30 15:31:26 -0700 | [diff] [blame] | 226 | |
| 227 | // FieldPacker is kept here to support regular params in the future. |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 228 | forEach(slot, ains, aout, v, null); |
| Chris Wailes | 9496106 | 2014-06-11 12:01:28 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Only intended for use by generated reflected code. |
| Chris Wailes | 9496106 | 2014-06-11 12:01:28 -0700 | [diff] [blame] | 233 | */ |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 234 | protected void forEach(int slot, Allocation[] ains, Allocation aout, |
| 235 | FieldPacker v, LaunchOptions sc) { |
| 236 | // TODO: Is this necessary if nScriptForEach calls validate as well? |
| Jason Sams | 6a420b5 | 2015-03-30 15:31:26 -0700 | [diff] [blame] | 237 | // FieldPacker is kept here to support regular params in the future. |
| Chris Wailes | 9496106 | 2014-06-11 12:01:28 -0700 | [diff] [blame] | 238 | mRS.validate(); |
| Andreas Gampe | c8ddcdd | 2015-03-15 15:57:30 -0700 | [diff] [blame] | 239 | if (ains != null) { |
| 240 | for (Allocation ain : ains) { |
| 241 | mRS.validateObject(ain); |
| 242 | } |
| Chris Wailes | 9496106 | 2014-06-11 12:01:28 -0700 | [diff] [blame] | 243 | } |
| Stephen Hines | c9c7daf | 2014-08-13 17:32:19 +0000 | [diff] [blame] | 244 | mRS.validateObject(aout); |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 245 | |
| Chris Wailes | 9496106 | 2014-06-11 12:01:28 -0700 | [diff] [blame] | 246 | if (ains == null && aout == null) { |
| 247 | throw new RSIllegalArgumentException( |
| 248 | "At least one of ain or aout is required to be non-null."); |
| 249 | } |
| 250 | |
| Andreas Gampe | ad555f9 | 2015-03-17 20:05:46 -0700 | [diff] [blame] | 251 | long[] in_ids; |
| 252 | if (ains != null) { |
| 253 | in_ids = new long[ains.length]; |
| 254 | for (int index = 0; index < ains.length; ++index) { |
| 255 | in_ids[index] = ains[index].getID(mRS); |
| 256 | } |
| 257 | } else { |
| 258 | in_ids = null; |
| Chris Wailes | 9496106 | 2014-06-11 12:01:28 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | long out_id = 0; |
| 262 | if (aout != null) { |
| 263 | out_id = aout.getID(mRS); |
| 264 | } |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 265 | |
| Chris Wailes | 9496106 | 2014-06-11 12:01:28 -0700 | [diff] [blame] | 266 | byte[] params = null; |
| 267 | if (v != null) { |
| 268 | params = v.getData(); |
| 269 | } |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 270 | |
| 271 | int[] limits = null; |
| 272 | if (sc != null) { |
| 273 | limits = new int[6]; |
| 274 | |
| 275 | limits[0] = sc.xstart; |
| 276 | limits[1] = sc.xend; |
| 277 | limits[2] = sc.ystart; |
| 278 | limits[3] = sc.yend; |
| 279 | limits[4] = sc.zstart; |
| 280 | limits[5] = sc.zend; |
| 281 | } |
| 282 | |
| 283 | mRS.nScriptForEach(getID(mRS), slot, in_ids, out_id, params, limits); |
| Chris Wailes | 9496106 | 2014-06-11 12:01:28 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| Matt Wala | 36eb1f7 | 2015-07-20 15:35:27 -0700 | [diff] [blame] | 286 | /** |
| David Gross | 26ef7a73 | 2016-01-12 12:19:15 -0800 | [diff] [blame] | 287 | * Only intended for use by generated reflected code. (Simple reduction) |
| Matt Wala | 36eb1f7 | 2015-07-20 15:35:27 -0700 | [diff] [blame] | 288 | * |
| 289 | * @hide |
| 290 | */ |
| 291 | protected void reduce(int slot, Allocation ain, Allocation aout, LaunchOptions sc) { |
| 292 | mRS.validate(); |
| 293 | mRS.validateObject(ain); |
| 294 | mRS.validateObject(aout); |
| 295 | |
| 296 | if (ain == null || aout == null) { |
| 297 | throw new RSIllegalArgumentException( |
| 298 | "Both ain and aout are required to be non-null."); |
| 299 | } |
| 300 | |
| 301 | long in_id = ain.getID(mRS); |
| 302 | long out_id = aout.getID(mRS); |
| 303 | |
| 304 | int[] limits = null; |
| 305 | if (sc != null) { |
| 306 | limits = new int[2]; |
| 307 | |
| 308 | limits[0] = sc.xstart; |
| 309 | limits[1] = sc.xend; |
| 310 | } |
| 311 | |
| 312 | mRS.nScriptReduce(getID(mRS), slot, in_id, out_id, limits); |
| 313 | } |
| 314 | |
| David Gross | 26ef7a73 | 2016-01-12 12:19:15 -0800 | [diff] [blame] | 315 | /** |
| 316 | * Only intended for use by generated reflected code. (General reduction) |
| 317 | * |
| David Gross | 26ef7a73 | 2016-01-12 12:19:15 -0800 | [diff] [blame] | 318 | */ |
| 319 | protected void reduce(int slot, Allocation[] ains, Allocation aout, LaunchOptions sc) { |
| 320 | mRS.validate(); |
| 321 | if (ains == null || ains.length < 1) { |
| 322 | throw new RSIllegalArgumentException( |
| 323 | "At least one input is required."); |
| 324 | } |
| 325 | if (aout == null) { |
| 326 | throw new RSIllegalArgumentException( |
| 327 | "aout is required to be non-null."); |
| 328 | } |
| 329 | for (Allocation ain : ains) { |
| 330 | mRS.validateObject(ain); |
| 331 | } |
| 332 | |
| 333 | long[] in_ids = new long[ains.length]; |
| 334 | for (int index = 0; index < ains.length; ++index) { |
| 335 | in_ids[index] = ains[index].getID(mRS); |
| 336 | } |
| 337 | long out_id = aout.getID(mRS); |
| 338 | |
| 339 | int[] limits = null; |
| 340 | if (sc != null) { |
| 341 | limits = new int[6]; |
| 342 | |
| 343 | limits[0] = sc.xstart; |
| 344 | limits[1] = sc.xend; |
| 345 | limits[2] = sc.ystart; |
| 346 | limits[3] = sc.yend; |
| 347 | limits[4] = sc.zstart; |
| 348 | limits[5] = sc.zend; |
| 349 | } |
| 350 | |
| 351 | mRS.nScriptReduceNew(getID(mRS), slot, in_ids, out_id, limits); |
| 352 | } |
| 353 | |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 354 | long[] mInIdsBuffer; |
| 355 | |
| Tim Murray | 7a629fa | 2013-11-19 12:45:54 -0800 | [diff] [blame] | 356 | Script(long id, RenderScript rs) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 357 | super(id, rs); |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 358 | |
| 359 | mInIdsBuffer = new long[1]; |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 362 | /** |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 363 | * Only intended for use by generated reflected code. |
| 364 | * |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 365 | */ |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 366 | public void bindAllocation(Allocation va, int slot) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 367 | mRS.validate(); |
| Jason Sams | 678cc7f | 2014-03-05 16:09:02 -0800 | [diff] [blame] | 368 | mRS.validateObject(va); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 369 | if (va != null) { |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 370 | |
| 371 | android.content.Context context = mRS.getApplicationContext(); |
| 372 | |
| 373 | if (context.getApplicationInfo().targetSdkVersion >= 20) { |
| Jason Sams | cf9c894 | 2014-01-14 16:18:14 -0800 | [diff] [blame] | 374 | final Type t = va.mType; |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 375 | if (t.hasMipmaps() || t.hasFaces() || (t.getY() != 0) || |
| 376 | (t.getZ() != 0)) { |
| 377 | |
| Jason Sams | cf9c894 | 2014-01-14 16:18:14 -0800 | [diff] [blame] | 378 | throw new RSIllegalArgumentException( |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 379 | "API 20+ only allows simple 1D allocations to be " + |
| 380 | "used with bind."); |
| Jason Sams | cf9c894 | 2014-01-14 16:18:14 -0800 | [diff] [blame] | 381 | } |
| 382 | } |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 383 | mRS.nScriptBindAllocation(getID(mRS), va.getID(mRS), slot); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 384 | } else { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 385 | mRS.nScriptBindAllocation(getID(mRS), 0, slot); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 389 | /** |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 390 | * Only intended for use by generated reflected code. |
| 391 | * |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 392 | */ |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 393 | public void setVar(int index, float v) { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 394 | mRS.nScriptSetVarF(getID(mRS), index, v); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 395 | } |
| Tim Murray | 7c4caad | 2013-04-10 16:21:40 -0700 | [diff] [blame] | 396 | public float getVarF(int index) { |
| 397 | return mRS.nScriptGetVarF(getID(mRS), index); |
| 398 | } |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 399 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 400 | /** |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 401 | * Only intended for use by generated reflected code. |
| 402 | * |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 403 | */ |
| Stephen Hines | ca54ec3 | 2010-09-20 17:20:30 -0700 | [diff] [blame] | 404 | public void setVar(int index, double v) { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 405 | mRS.nScriptSetVarD(getID(mRS), index, v); |
| Stephen Hines | ca54ec3 | 2010-09-20 17:20:30 -0700 | [diff] [blame] | 406 | } |
| Tim Murray | 7c4caad | 2013-04-10 16:21:40 -0700 | [diff] [blame] | 407 | public double getVarD(int index) { |
| 408 | return mRS.nScriptGetVarD(getID(mRS), index); |
| 409 | } |
| Stephen Hines | ca54ec3 | 2010-09-20 17:20:30 -0700 | [diff] [blame] | 410 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 411 | /** |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 412 | * Only intended for use by generated reflected code. |
| 413 | * |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 414 | */ |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 415 | public void setVar(int index, int v) { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 416 | mRS.nScriptSetVarI(getID(mRS), index, v); |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 417 | } |
| Tim Murray | 7c4caad | 2013-04-10 16:21:40 -0700 | [diff] [blame] | 418 | public int getVarI(int index) { |
| 419 | return mRS.nScriptGetVarI(getID(mRS), index); |
| 420 | } |
| 421 | |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 422 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 423 | /** |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 424 | * Only intended for use by generated reflected code. |
| 425 | * |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 426 | */ |
| Stephen Hines | 031ec58c | 2010-10-11 10:54:21 -0700 | [diff] [blame] | 427 | public void setVar(int index, long v) { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 428 | mRS.nScriptSetVarJ(getID(mRS), index, v); |
| Stephen Hines | 031ec58c | 2010-10-11 10:54:21 -0700 | [diff] [blame] | 429 | } |
| Tim Murray | 7c4caad | 2013-04-10 16:21:40 -0700 | [diff] [blame] | 430 | public long getVarJ(int index) { |
| 431 | return mRS.nScriptGetVarJ(getID(mRS), index); |
| 432 | } |
| 433 | |
| Stephen Hines | 031ec58c | 2010-10-11 10:54:21 -0700 | [diff] [blame] | 434 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 435 | /** |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 436 | * Only intended for use by generated reflected code. |
| 437 | * |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 438 | */ |
| Jason Sams | 0b9a22c | 2010-07-02 15:35:19 -0700 | [diff] [blame] | 439 | public void setVar(int index, boolean v) { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 440 | mRS.nScriptSetVarI(getID(mRS), index, v ? 1 : 0); |
| Jason Sams | 0b9a22c | 2010-07-02 15:35:19 -0700 | [diff] [blame] | 441 | } |
| Tim Murray | 7c4caad | 2013-04-10 16:21:40 -0700 | [diff] [blame] | 442 | public boolean getVarB(int index) { |
| 443 | return mRS.nScriptGetVarI(getID(mRS), index) > 0 ? true : false; |
| 444 | } |
| Jason Sams | 0b9a22c | 2010-07-02 15:35:19 -0700 | [diff] [blame] | 445 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 446 | /** |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 447 | * Only intended for use by generated reflected code. |
| 448 | * |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 449 | */ |
| Jason Sams | 6f4cf0b | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 450 | public void setVar(int index, BaseObj o) { |
| Jason Sams | 678cc7f | 2014-03-05 16:09:02 -0800 | [diff] [blame] | 451 | mRS.validate(); |
| 452 | mRS.validateObject(o); |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 453 | mRS.nScriptSetVarObj(getID(mRS), index, (o == null) ? 0 : o.getID(mRS)); |
| Jason Sams | 6f4cf0b | 2010-11-16 17:37:02 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 456 | /** |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 457 | * Only intended for use by generated reflected code. |
| 458 | * |
| Jason Sams | 67e3d20 | 2011-01-09 13:49:01 -0800 | [diff] [blame] | 459 | */ |
| Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 460 | public void setVar(int index, FieldPacker v) { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 461 | mRS.nScriptSetVarV(getID(mRS), index, v.getData()); |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 464 | /** |
| Stephen Hines | adeb809 | 2012-04-20 14:26:06 -0700 | [diff] [blame] | 465 | * Only intended for use by generated reflected code. |
| 466 | * |
| Stephen Hines | adeb809 | 2012-04-20 14:26:06 -0700 | [diff] [blame] | 467 | */ |
| 468 | public void setVar(int index, FieldPacker v, Element e, int[] dims) { |
| 469 | mRS.nScriptSetVarVE(getID(mRS), index, v.getData(), e.getID(mRS), dims); |
| 470 | } |
| 471 | |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 472 | /** |
| 473 | * Only intended for use by generated reflected code. |
| 474 | * |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 475 | */ |
| Tim Murray | 7c4caad | 2013-04-10 16:21:40 -0700 | [diff] [blame] | 476 | public void getVarV(int index, FieldPacker v) { |
| 477 | mRS.nScriptGetVarV(getID(mRS), index, v.getData()); |
| 478 | } |
| 479 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 480 | public void setTimeZone(String timeZone) { |
| Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 481 | mRS.validate(); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 482 | try { |
| Jason Sams | e07694b | 2012-04-03 15:36:36 -0700 | [diff] [blame] | 483 | mRS.nScriptSetTimeZone(getID(mRS), timeZone.getBytes("UTF-8")); |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 484 | } catch (java.io.UnsupportedEncodingException e) { |
| 485 | throw new RuntimeException(e); |
| 486 | } |
| 487 | } |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 488 | |
| Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 489 | /** |
| 490 | * Only intended for use by generated reflected code. |
| 491 | * |
| 492 | */ |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 493 | public static class Builder { |
| 494 | RenderScript mRS; |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 495 | |
| 496 | Builder(RenderScript rs) { |
| 497 | mRS = rs; |
| 498 | } |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 501 | |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 502 | /** |
| 503 | * Only intended for use by generated reflected code. |
| 504 | * |
| 505 | */ |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 506 | public static class FieldBase { |
| 507 | protected Element mElement; |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 508 | protected Allocation mAllocation; |
| 509 | |
| 510 | protected void init(RenderScript rs, int dimx) { |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 511 | mAllocation = Allocation.createSized(rs, mElement, dimx, |
| 512 | Allocation.USAGE_SCRIPT); |
| Jason Sams | 5476b45 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | protected void init(RenderScript rs, int dimx, int usages) { |
| Chris Wailes | be7b1de | 2014-07-15 10:56:14 -0700 | [diff] [blame] | 516 | mAllocation = |
| 517 | Allocation.createSized(rs, mElement, dimx, |
| 518 | Allocation.USAGE_SCRIPT | usages); |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | protected FieldBase() { |
| 522 | } |
| 523 | |
| 524 | public Element getElement() { |
| 525 | return mElement; |
| 526 | } |
| 527 | |
| 528 | public Type getType() { |
| Jason Sams | 31a7e42 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 529 | return mAllocation.getType(); |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | public Allocation getAllocation() { |
| 533 | return mAllocation; |
| 534 | } |
| 535 | |
| 536 | //@Override |
| 537 | public void updateAllocation() { |
| 538 | } |
| Jason Sams | 2d71bc7 | 2010-03-26 16:06:43 -0700 | [diff] [blame] | 539 | } |
| Tim Murray | fbfaa85 | 2012-12-14 16:01:58 -0800 | [diff] [blame] | 540 | |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 541 | |
| 542 | /** |
| Jason Sams | 8610f83 | 2015-03-30 17:01:10 -0700 | [diff] [blame] | 543 | * Class for specifying the specifics about how a kernel will be |
| 544 | * launched |
| 545 | * |
| 546 | * This class can specify a potential range of cells on which to |
| 547 | * run a kernel. If no set is called for a dimension then this |
| 548 | * class will have no impact on that dimension when the kernel |
| 549 | * is executed. |
| 550 | * |
| 551 | * The forEach launch will operate over the intersection of the |
| 552 | * dimensions. |
| 553 | * |
| 554 | * Example: |
| 555 | * LaunchOptions with setX(5, 15) |
| 556 | * Allocation with dimension X=10, Y=10 |
| 557 | * The resulting forEach run would execute over x = 5 to 10 and |
| 558 | * y = 0 to 10. |
| 559 | * |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 560 | * |
| 561 | */ |
| Tim Murray | fbfaa85 | 2012-12-14 16:01:58 -0800 | [diff] [blame] | 562 | public static final class LaunchOptions { |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 563 | private int xstart = 0; |
| 564 | private int ystart = 0; |
| 565 | private int xend = 0; |
| 566 | private int yend = 0; |
| 567 | private int zstart = 0; |
| 568 | private int zend = 0; |
| 569 | private int strategy; |
| Tim Murray | fbfaa85 | 2012-12-14 16:01:58 -0800 | [diff] [blame] | 570 | |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 571 | /** |
| 572 | * Set the X range. If the end value is set to 0 the X dimension is not |
| 573 | * clipped. |
| 574 | * |
| 575 | * @param xstartArg Must be >= 0 |
| 576 | * @param xendArg Must be >= xstartArg |
| 577 | * |
| 578 | * @return LaunchOptions |
| 579 | */ |
| Tim Murray | eb8c29c | 2013-02-07 12:16:41 -0800 | [diff] [blame] | 580 | public LaunchOptions setX(int xstartArg, int xendArg) { |
| Tim Murray | fbfaa85 | 2012-12-14 16:01:58 -0800 | [diff] [blame] | 581 | if (xstartArg < 0 || xendArg <= xstartArg) { |
| 582 | throw new RSIllegalArgumentException("Invalid dimensions"); |
| 583 | } |
| 584 | xstart = xstartArg; |
| 585 | xend = xendArg; |
| Tim Murray | eb8c29c | 2013-02-07 12:16:41 -0800 | [diff] [blame] | 586 | return this; |
| Tim Murray | fbfaa85 | 2012-12-14 16:01:58 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 589 | /** |
| 590 | * Set the Y range. If the end value is set to 0 the Y dimension is not |
| 591 | * clipped. |
| 592 | * |
| 593 | * @param ystartArg Must be >= 0 |
| 594 | * @param yendArg Must be >= ystartArg |
| 595 | * |
| 596 | * @return LaunchOptions |
| 597 | */ |
| Tim Murray | eb8c29c | 2013-02-07 12:16:41 -0800 | [diff] [blame] | 598 | public LaunchOptions setY(int ystartArg, int yendArg) { |
| Tim Murray | fbfaa85 | 2012-12-14 16:01:58 -0800 | [diff] [blame] | 599 | if (ystartArg < 0 || yendArg <= ystartArg) { |
| 600 | throw new RSIllegalArgumentException("Invalid dimensions"); |
| 601 | } |
| 602 | ystart = ystartArg; |
| 603 | yend = yendArg; |
| Tim Murray | eb8c29c | 2013-02-07 12:16:41 -0800 | [diff] [blame] | 604 | return this; |
| Tim Murray | fbfaa85 | 2012-12-14 16:01:58 -0800 | [diff] [blame] | 605 | } |
| 606 | |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 607 | /** |
| 608 | * Set the Z range. If the end value is set to 0 the Z dimension is not |
| 609 | * clipped. |
| 610 | * |
| 611 | * @param zstartArg Must be >= 0 |
| 612 | * @param zendArg Must be >= zstartArg |
| 613 | * |
| 614 | * @return LaunchOptions |
| 615 | */ |
| Tim Murray | eb8c29c | 2013-02-07 12:16:41 -0800 | [diff] [blame] | 616 | public LaunchOptions setZ(int zstartArg, int zendArg) { |
| 617 | if (zstartArg < 0 || zendArg <= zstartArg) { |
| 618 | throw new RSIllegalArgumentException("Invalid dimensions"); |
| 619 | } |
| 620 | zstart = zstartArg; |
| 621 | zend = zendArg; |
| 622 | return this; |
| 623 | } |
| 624 | |
| 625 | |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 626 | /** |
| 627 | * Returns the current X start |
| 628 | * |
| 629 | * @return int current value |
| 630 | */ |
| Tim Murray | fbfaa85 | 2012-12-14 16:01:58 -0800 | [diff] [blame] | 631 | public int getXStart() { |
| 632 | return xstart; |
| 633 | } |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 634 | /** |
| 635 | * Returns the current X end |
| 636 | * |
| 637 | * @return int current value |
| 638 | */ |
| Tim Murray | fbfaa85 | 2012-12-14 16:01:58 -0800 | [diff] [blame] | 639 | public int getXEnd() { |
| 640 | return xend; |
| 641 | } |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 642 | /** |
| 643 | * Returns the current Y start |
| 644 | * |
| 645 | * @return int current value |
| 646 | */ |
| Tim Murray | fbfaa85 | 2012-12-14 16:01:58 -0800 | [diff] [blame] | 647 | public int getYStart() { |
| 648 | return ystart; |
| 649 | } |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 650 | /** |
| 651 | * Returns the current Y end |
| 652 | * |
| 653 | * @return int current value |
| 654 | */ |
| Tim Murray | fbfaa85 | 2012-12-14 16:01:58 -0800 | [diff] [blame] | 655 | public int getYEnd() { |
| 656 | return yend; |
| 657 | } |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 658 | /** |
| 659 | * Returns the current Z start |
| 660 | * |
| 661 | * @return int current value |
| 662 | */ |
| Tim Murray | eb8c29c | 2013-02-07 12:16:41 -0800 | [diff] [blame] | 663 | public int getZStart() { |
| 664 | return zstart; |
| 665 | } |
| Jason Sams | f64cca9 | 2013-04-19 12:56:37 -0700 | [diff] [blame] | 666 | /** |
| 667 | * Returns the current Z end |
| 668 | * |
| 669 | * @return int current value |
| 670 | */ |
| Tim Murray | eb8c29c | 2013-02-07 12:16:41 -0800 | [diff] [blame] | 671 | public int getZEnd() { |
| 672 | return zend; |
| 673 | } |
| Tim Murray | fbfaa85 | 2012-12-14 16:01:58 -0800 | [diff] [blame] | 674 | |
| 675 | } |
| Jason Sams | 69f0d31 | 2009-08-03 18:11:17 -0700 | [diff] [blame] | 676 | } |