blob: 51fc7dd06232d0350e78f6e3584c512ccdf5f21c [file] [log] [blame]
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001/*
Stephen Hinesbe74bdd2012-02-03 15:29:36 -08002 * Copyright (C) 2008-2012 The Android Open Source Project
Jack Palevich60aa3ea2009-05-26 13:45:08 -07003 *
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
Jason Sams94d8e90a2009-06-10 16:09:05 -070017package android.renderscript;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070018
Tim Murray47f31582015-04-07 15:43:24 -070019import java.io.File;
Tim Murray2f2472c2013-08-22 14:55:26 -070020import java.lang.reflect.Method;
Miao Wang0facf022015-11-25 11:21:13 -080021import java.nio.ByteBuffer;
22import java.util.ArrayList;
Tim Murray06b45672014-01-07 11:13:56 -080023import java.util.concurrent.locks.ReentrantReadWriteLock;
Jason Sams36e612a2009-07-31 16:26:13 -070024
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080025import android.content.Context;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080026import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070027import android.graphics.Bitmap;
Jason Samsfaa32b32011-06-20 16:58:04 -070028import android.graphics.SurfaceTexture;
Dan Morrille4d9a012013-03-28 18:10:43 -070029import android.os.SystemProperties;
Tim Murray6d7a53c2013-05-23 16:59:23 -070030import android.os.Trace;
Miao Wang0facf022015-11-25 11:21:13 -080031import android.util.Log;
32import android.view.Surface;
Stephen Hines4382467a2011-08-01 15:02:34 -070033
Matt Wala36eb1f72015-07-20 15:35:27 -070034// TODO: Clean up the whitespace that separates methods in this class.
35
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070036/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070037 * This class provides access to a RenderScript context, which controls RenderScript
38 * initialization, resource management, and teardown. An instance of the RenderScript
39 * class must be created before any other RS objects can be created.
Jason Sams27676fe2010-11-10 17:00:59 -080040 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080041 * <div class="special reference">
42 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070043 * <p>For more information about creating an application that uses RenderScript, read the
44 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080045 * </div>
Jason Samse29d4712009-07-23 15:19:03 -070046 **/
Jack Palevich60aa3ea2009-05-26 13:45:08 -070047public class RenderScript {
Tim Murray6d7a53c2013-05-23 16:59:23 -070048 static final long TRACE_TAG = Trace.TRACE_TAG_RS;
49
Jason Sams3bc47d42009-11-12 15:10:25 -080050 static final String LOG_TAG = "RenderScript_jni";
Jason Samsbf6ef8d72010-12-06 15:59:59 -080051 static final boolean DEBUG = false;
Romain Guy650a3eb2009-08-31 14:06:43 -070052 @SuppressWarnings({"UnusedDeclaration", "deprecation"})
Joe Onorato43a17652011-04-06 19:22:23 -070053 static final boolean LOG_ENABLED = false;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070054
Jason Samse16da122015-03-18 17:04:18 -070055 static private ArrayList<RenderScript> mProcessContextList = new ArrayList<RenderScript>();
56 private boolean mIsProcessContext = false;
57 private int mContextFlags = 0;
58 private int mContextSdkVersion = 0;
59
60
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080061 private Context mApplicationContext;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070062
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080063 /*
Jack Palevich60aa3ea2009-05-26 13:45:08 -070064 * We use a class initializer to allow the native code to cache some
65 * field offsets.
66 */
Dan Morrille4d9a012013-03-28 18:10:43 -070067 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) // TODO: now used locally; remove?
Jason Samsbf6ef8d72010-12-06 15:59:59 -080068 static boolean sInitialized;
69 native static void _nInit();
Jack Palevich60aa3ea2009-05-26 13:45:08 -070070
Tim Murray2f2472c2013-08-22 14:55:26 -070071 static Object sRuntime;
72 static Method registerNativeAllocation;
73 static Method registerNativeFree;
Jason Samsdba3ba52009-07-30 14:56:12 -070074
Jason Sams26e90512014-05-07 14:23:27 -070075 /*
Tim Murrayfd710e72014-06-06 11:10:45 -070076 * Context creation flag that specifies a normal context.
Jason Sams26e90512014-05-07 14:23:27 -070077 */
Tim Murrayfd710e72014-06-06 11:10:45 -070078 public static final int CREATE_FLAG_NONE = 0x0000;
Jason Sams26e90512014-05-07 14:23:27 -070079
80 /*
81 * Context creation flag which specifies a context optimized for low
82 * latency over peak performance. This is a hint and may have no effect
83 * on some implementations.
84 */
Tim Murrayfd710e72014-06-06 11:10:45 -070085 public static final int CREATE_FLAG_LOW_LATENCY = 0x0002;
Jason Sams26e90512014-05-07 14:23:27 -070086
87 /*
88 * Context creation flag which specifies a context optimized for long
89 * battery life over peak performance. This is a hint and may have no effect
90 * on some implementations.
91 */
Tim Murrayfd710e72014-06-06 11:10:45 -070092 public static final int CREATE_FLAG_LOW_POWER = 0x0004;
Jason Sams26e90512014-05-07 14:23:27 -070093
Stephen McGroarty62cb9bd2015-05-08 15:56:58 +010094 /**
95 * @hide
96 * Context creation flag which instructs the implementation to wait for
97 * a debugger to be attached before continuing execution.
98 */
99 public static final int CREATE_FLAG_WAIT_FOR_ATTACH = 0x0008;
100
verena beckhamc9659ea2015-05-22 16:47:53 +0100101 /**
102 * @hide
103 * Context creation flag which specifies that optimization level 0 is
104 * passed to the device compiler upon execution of the RenderScript kernel.
105 * The default optimization level is 3.
106 */
107 public static final int CREATE_FLAG_OPT_LEVEL_0 = 0x0010;
108
Tim Murray56f9e6f2014-05-16 11:47:26 -0700109 /*
110 * Detect the bitness of the VM to allow FieldPacker to do the right thing.
111 */
112 static native int rsnSystemGetPointerSize();
113 static int sPointerSize;
114
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700115 static {
116 sInitialized = false;
Dan Morrille4d9a012013-03-28 18:10:43 -0700117 if (!SystemProperties.getBoolean("config.disable_renderscript", false)) {
118 try {
Tim Murray2f2472c2013-08-22 14:55:26 -0700119 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
120 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
121 sRuntime = get_runtime.invoke(null);
122 registerNativeAllocation = vm_runtime.getDeclaredMethod("registerNativeAllocation", Integer.TYPE);
123 registerNativeFree = vm_runtime.getDeclaredMethod("registerNativeFree", Integer.TYPE);
124 } catch (Exception e) {
125 Log.e(LOG_TAG, "Error loading GC methods: " + e);
126 throw new RSRuntimeException("Error loading GC methods: " + e);
127 }
128 try {
Dan Morrille4d9a012013-03-28 18:10:43 -0700129 System.loadLibrary("rs_jni");
130 _nInit();
131 sInitialized = true;
Tim Murray56f9e6f2014-05-16 11:47:26 -0700132 sPointerSize = rsnSystemGetPointerSize();
Dan Morrille4d9a012013-03-28 18:10:43 -0700133 } catch (UnsatisfiedLinkError e) {
134 Log.e(LOG_TAG, "Error loading RS jni library: " + e);
135 throw new RSRuntimeException("Error loading RS jni library: " + e);
136 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700137 }
138 }
139
Jason Sams2e1872f2010-08-17 16:25:41 -0700140 // Non-threadsafe functions.
Tim Murrayeff663f2013-11-15 13:08:30 -0800141 native long nDeviceCreate();
142 native void nDeviceDestroy(long dev);
143 native void nDeviceSetConfig(long dev, int param, int value);
144 native int nContextGetUserMessage(long con, int[] data);
145 native String nContextGetErrorMessage(long con);
146 native int nContextPeekMessage(long con, int[] subID);
147 native void nContextInitToClient(long con);
148 native void nContextDeinitToClient(long con);
Jason Sams3eaa338e2009-06-10 15:04:38 -0700149
Tim Murray67cc2d02014-02-06 16:39:38 -0800150 // this should be a monotonically increasing ID
151 // used in conjunction with the API version of a device
Jason Samsf7642302015-05-12 14:06:56 -0700152 static final long sMinorVersion = 1;
Tim Murray67cc2d02014-02-06 16:39:38 -0800153
154 /**
Miao Wangf9d518a2015-05-14 14:53:30 -0700155 * @hide
156 *
157 * Only exist to be compatible with old version RenderScript Support lib.
158 * Will eventually be removed.
159 *
160 * @return Always return 1
161 *
162 */
163 public static long getMinorID() {
164 return 1;
165 }
166
167
168 /**
Tim Murray67cc2d02014-02-06 16:39:38 -0800169 * Returns an identifier that can be used to identify a particular
170 * minor version of RS.
171 *
Jason Sams6a420b52015-03-30 15:31:26 -0700172 * @return The minor RenderScript version number
173 *
Tim Murray67cc2d02014-02-06 16:39:38 -0800174 */
Jason Samsf7642302015-05-12 14:06:56 -0700175 public static long getMinorVersion() {
176 return sMinorVersion;
Tim Murray67cc2d02014-02-06 16:39:38 -0800177 }
178
Jason Sams02d56d92013-04-12 16:40:50 -0700179 /**
180 * ContextType specifies the specific type of context to be created.
181 *
182 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800183 public enum ContextType {
Jason Sams02d56d92013-04-12 16:40:50 -0700184 /**
185 * NORMAL context, this is the default and what shipping apps should
186 * use.
187 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800188 NORMAL (0),
Jason Sams02d56d92013-04-12 16:40:50 -0700189
190 /**
191 * DEBUG context, perform extra runtime checks to validate the
192 * kernels and APIs are being used as intended. Get and SetElementAt
193 * will be bounds checked in this mode.
194 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800195 DEBUG (1),
Jason Sams02d56d92013-04-12 16:40:50 -0700196
197 /**
198 * PROFILE context, Intended to be used once the first time an
199 * application is run on a new device. This mode allows the runtime to
200 * do additional testing and performance tuning.
201 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800202 PROFILE (2);
203
204 int mID;
205 ContextType(int id) {
206 mID = id;
207 }
208 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800209
Stephen Hines42028a82013-04-17 19:22:01 -0700210 ContextType mContextType;
Tim Murray06b45672014-01-07 11:13:56 -0800211 ReentrantReadWriteLock mRWLock;
Stephen Hines42028a82013-04-17 19:22:01 -0700212
Jason Sams2e1872f2010-08-17 16:25:41 -0700213 // Methods below are wrapped to protect the non-threadsafe
214 // lockless fifo.
Tim Murrayeff663f2013-11-15 13:08:30 -0800215 native long rsnContextCreateGL(long dev, int ver, int sdkVer,
Jason Sams11c8af92010-10-13 15:31:10 -0700216 int colorMin, int colorPref,
217 int alphaMin, int alphaPref,
218 int depthMin, int depthPref,
219 int stencilMin, int stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700220 int samplesMin, int samplesPref, float samplesQ, int dpi);
Tim Murrayeff663f2013-11-15 13:08:30 -0800221 synchronized long nContextCreateGL(long dev, int ver, int sdkVer,
Jason Sams11c8af92010-10-13 15:31:10 -0700222 int colorMin, int colorPref,
223 int alphaMin, int alphaPref,
224 int depthMin, int depthPref,
225 int stencilMin, int stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700226 int samplesMin, int samplesPref, float samplesQ, int dpi) {
Stephen Hines4382467a2011-08-01 15:02:34 -0700227 return rsnContextCreateGL(dev, ver, sdkVer, colorMin, colorPref,
Jason Sams11c8af92010-10-13 15:31:10 -0700228 alphaMin, alphaPref, depthMin, depthPref,
229 stencilMin, stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700230 samplesMin, samplesPref, samplesQ, dpi);
Jason Sams2e1872f2010-08-17 16:25:41 -0700231 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800232 native long rsnContextCreate(long dev, int ver, int sdkVer, int contextType);
233 synchronized long nContextCreate(long dev, int ver, int sdkVer, int contextType) {
Jason Samsadd26dc2013-02-22 18:43:45 -0800234 return rsnContextCreate(dev, ver, sdkVer, contextType);
Jason Sams2e1872f2010-08-17 16:25:41 -0700235 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800236 native void rsnContextDestroy(long con);
Jason Sams2e1872f2010-08-17 16:25:41 -0700237 synchronized void nContextDestroy() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800238 validate();
Tim Murray06b45672014-01-07 11:13:56 -0800239
240 // take teardown lock
241 // teardown lock can only be taken when no objects are being destroyed
242 ReentrantReadWriteLock.WriteLock wlock = mRWLock.writeLock();
243 wlock.lock();
244
245 long curCon = mContext;
246 // context is considered dead as of this point
247 mContext = 0;
248
249 wlock.unlock();
250 rsnContextDestroy(curCon);
Jason Sams2e1872f2010-08-17 16:25:41 -0700251 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800252 native void rsnContextSetSurface(long con, int w, int h, Surface sur);
Jason Sams2e1872f2010-08-17 16:25:41 -0700253 synchronized void nContextSetSurface(int w, int h, Surface sur) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800254 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700255 rsnContextSetSurface(mContext, w, h, sur);
256 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800257 native void rsnContextSetSurfaceTexture(long con, int w, int h, SurfaceTexture sur);
Jason Samsfaa32b32011-06-20 16:58:04 -0700258 synchronized void nContextSetSurfaceTexture(int w, int h, SurfaceTexture sur) {
259 validate();
260 rsnContextSetSurfaceTexture(mContext, w, h, sur);
261 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800262 native void rsnContextSetPriority(long con, int p);
Jason Sams2e1872f2010-08-17 16:25:41 -0700263 synchronized void nContextSetPriority(int p) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800264 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700265 rsnContextSetPriority(mContext, p);
266 }
Tim Murray47f31582015-04-07 15:43:24 -0700267 native void rsnContextSetCacheDir(long con, String cacheDir);
268 synchronized void nContextSetCacheDir(String cacheDir) {
269 validate();
270 rsnContextSetCacheDir(mContext, cacheDir);
271 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800272 native void rsnContextDump(long con, int bits);
Jason Sams2e1872f2010-08-17 16:25:41 -0700273 synchronized void nContextDump(int bits) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800274 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700275 rsnContextDump(mContext, bits);
276 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800277 native void rsnContextFinish(long con);
Jason Sams2e1872f2010-08-17 16:25:41 -0700278 synchronized void nContextFinish() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800279 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700280 rsnContextFinish(mContext);
281 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700282
Tim Murrayeff663f2013-11-15 13:08:30 -0800283 native void rsnContextSendMessage(long con, int id, int[] data);
Jason Sams455d6442013-02-05 19:20:18 -0800284 synchronized void nContextSendMessage(int id, int[] data) {
285 validate();
286 rsnContextSendMessage(mContext, id, data);
287 }
288
Narayan Kamath78c0ce52014-03-19 10:15:51 +0000289 native void rsnContextBindRootScript(long con, long script);
290 synchronized void nContextBindRootScript(long script) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800291 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700292 rsnContextBindRootScript(mContext, script);
293 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800294 native void rsnContextBindSampler(long con, int sampler, int slot);
Jason Sams2e1872f2010-08-17 16:25:41 -0700295 synchronized void nContextBindSampler(int sampler, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800296 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700297 rsnContextBindSampler(mContext, sampler, slot);
298 }
Narayan Kamath78c0ce52014-03-19 10:15:51 +0000299 native void rsnContextBindProgramStore(long con, long pfs);
300 synchronized void nContextBindProgramStore(long pfs) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800301 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700302 rsnContextBindProgramStore(mContext, pfs);
303 }
Narayan Kamath78c0ce52014-03-19 10:15:51 +0000304 native void rsnContextBindProgramFragment(long con, long pf);
305 synchronized void nContextBindProgramFragment(long pf) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800306 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700307 rsnContextBindProgramFragment(mContext, pf);
308 }
Narayan Kamath78c0ce52014-03-19 10:15:51 +0000309 native void rsnContextBindProgramVertex(long con, long pv);
310 synchronized void nContextBindProgramVertex(long pv) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800311 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700312 rsnContextBindProgramVertex(mContext, pv);
313 }
Narayan Kamath78c0ce52014-03-19 10:15:51 +0000314 native void rsnContextBindProgramRaster(long con, long pr);
315 synchronized void nContextBindProgramRaster(long pr) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800316 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700317 rsnContextBindProgramRaster(mContext, pr);
318 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800319 native void rsnContextPause(long con);
Jason Sams2e1872f2010-08-17 16:25:41 -0700320 synchronized void nContextPause() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800321 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700322 rsnContextPause(mContext);
323 }
Tim Murrayeff663f2013-11-15 13:08:30 -0800324 native void rsnContextResume(long con);
Jason Sams2e1872f2010-08-17 16:25:41 -0700325 synchronized void nContextResume() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800326 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700327 rsnContextResume(mContext);
328 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700329
Yang Ni281c3252014-10-24 08:52:24 -0700330 native long rsnClosureCreate(long con, long kernelID, long returnValue,
331 long[] fieldIDs, long[] values, int[] sizes, long[] depClosures,
332 long[] depFieldIDs);
333 synchronized long nClosureCreate(long kernelID, long returnValue,
334 long[] fieldIDs, long[] values, int[] sizes, long[] depClosures,
335 long[] depFieldIDs) {
336 validate();
Yang Ni17c2d7a2015-04-30 16:13:54 -0700337 long c = rsnClosureCreate(mContext, kernelID, returnValue, fieldIDs, values,
Yang Ni281c3252014-10-24 08:52:24 -0700338 sizes, depClosures, depFieldIDs);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700339 if (c == 0) {
340 throw new RSRuntimeException("Failed creating closure.");
341 }
342 return c;
Yang Ni281c3252014-10-24 08:52:24 -0700343 }
344
Yang Nibe392ad2015-01-23 17:16:02 -0800345 native long rsnInvokeClosureCreate(long con, long invokeID, byte[] params,
346 long[] fieldIDs, long[] values, int[] sizes);
347 synchronized long nInvokeClosureCreate(long invokeID, byte[] params,
348 long[] fieldIDs, long[] values, int[] sizes) {
349 validate();
Yang Ni17c2d7a2015-04-30 16:13:54 -0700350 long c = rsnInvokeClosureCreate(mContext, invokeID, params, fieldIDs,
Yang Nibe392ad2015-01-23 17:16:02 -0800351 values, sizes);
Yang Ni17c2d7a2015-04-30 16:13:54 -0700352 if (c == 0) {
353 throw new RSRuntimeException("Failed creating closure.");
354 }
355 return c;
Yang Nibe392ad2015-01-23 17:16:02 -0800356 }
357
Yang Ni281c3252014-10-24 08:52:24 -0700358 native void rsnClosureSetArg(long con, long closureID, int index,
359 long value, int size);
360 synchronized void nClosureSetArg(long closureID, int index, long value,
361 int size) {
362 validate();
363 rsnClosureSetArg(mContext, closureID, index, value, size);
364 }
365
366 native void rsnClosureSetGlobal(long con, long closureID, long fieldID,
367 long value, int size);
368 // Does this have to be synchronized?
369 synchronized void nClosureSetGlobal(long closureID, long fieldID,
370 long value, int size) {
371 validate(); // TODO: is this necessary?
372 rsnClosureSetGlobal(mContext, closureID, fieldID, value, size);
373 }
374
Yang Ni35be56c2015-04-02 17:47:56 -0700375 native long rsnScriptGroup2Create(long con, String name, String cachePath,
376 long[] closures);
377 synchronized long nScriptGroup2Create(String name, String cachePath,
378 long[] closures) {
Yang Ni281c3252014-10-24 08:52:24 -0700379 validate();
Yang Ni17c2d7a2015-04-30 16:13:54 -0700380 long g = rsnScriptGroup2Create(mContext, name, cachePath, closures);
381 if (g == 0) {
382 throw new RSRuntimeException("Failed creating script group.");
383 }
384 return g;
Yang Ni281c3252014-10-24 08:52:24 -0700385 }
386
387 native void rsnScriptGroup2Execute(long con, long groupID);
388 synchronized void nScriptGroup2Execute(long groupID) {
389 validate();
390 rsnScriptGroup2Execute(mContext, groupID);
391 }
392
Tim Murray460a0492013-11-19 12:45:54 -0800393 native void rsnAssignName(long con, long obj, byte[] name);
394 synchronized void nAssignName(long obj, byte[] name) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800395 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700396 rsnAssignName(mContext, obj, name);
397 }
Tim Murray460a0492013-11-19 12:45:54 -0800398 native String rsnGetName(long con, long obj);
399 synchronized String nGetName(long obj) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800400 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700401 return rsnGetName(mContext, obj);
402 }
Tim Murray06b45672014-01-07 11:13:56 -0800403
404 // nObjDestroy is explicitly _not_ synchronous to prevent crashes in finalizers
Tim Murray460a0492013-11-19 12:45:54 -0800405 native void rsnObjDestroy(long con, long id);
Tim Murray06b45672014-01-07 11:13:56 -0800406 void nObjDestroy(long id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800407 // There is a race condition here. The calling code may be run
408 // by the gc while teardown is occuring. This protects againts
409 // deleting dead objects.
410 if (mContext != 0) {
411 rsnObjDestroy(mContext, id);
412 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700413 }
Jason Samsfe08d992009-05-27 14:45:32 -0700414
Tim Murray460a0492013-11-19 12:45:54 -0800415 native long rsnElementCreate(long con, long type, int kind, boolean norm, int vecSize);
416 synchronized long nElementCreate(long type, int kind, boolean norm, int vecSize) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800417 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700418 return rsnElementCreate(mContext, type, kind, norm, vecSize);
419 }
Ashok Bhat98071552014-02-12 09:54:43 +0000420 native long rsnElementCreate2(long con, long[] elements, String[] names, int[] arraySizes);
421 synchronized long nElementCreate2(long[] elements, String[] names, int[] arraySizes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800422 validate();
Jason Sams70d4e502010-09-02 17:35:23 -0700423 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700424 }
Tim Murray460a0492013-11-19 12:45:54 -0800425 native void rsnElementGetNativeData(long con, long id, int[] elementData);
426 synchronized void nElementGetNativeData(long id, int[] elementData) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800427 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700428 rsnElementGetNativeData(mContext, id, elementData);
429 }
Tim Murray460a0492013-11-19 12:45:54 -0800430 native void rsnElementGetSubElements(long con, long id,
Ashok Bhat98071552014-02-12 09:54:43 +0000431 long[] IDs, String[] names, int[] arraySizes);
432 synchronized void nElementGetSubElements(long id, long[] IDs, String[] names, int[] arraySizes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800433 validate();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700434 rsnElementGetSubElements(mContext, id, IDs, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700435 }
Jason Sams768bc022009-09-21 19:41:04 -0700436
Tim Murray460a0492013-11-19 12:45:54 -0800437 native long rsnTypeCreate(long con, long eid, int x, int y, int z, boolean mips, boolean faces, int yuv);
438 synchronized long nTypeCreate(long eid, int x, int y, int z, boolean mips, boolean faces, int yuv) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800439 validate();
Jason Samsb109cc72013-01-07 18:20:12 -0800440 return rsnTypeCreate(mContext, eid, x, y, z, mips, faces, yuv);
Jason Sams2e1872f2010-08-17 16:25:41 -0700441 }
Ashok Bhat98071552014-02-12 09:54:43 +0000442 native void rsnTypeGetNativeData(long con, long id, long[] typeData);
443 synchronized void nTypeGetNativeData(long id, long[] typeData) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800444 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700445 rsnTypeGetNativeData(mContext, id, typeData);
446 }
Jason Sams768bc022009-09-21 19:41:04 -0700447
Ashok Bhat98071552014-02-12 09:54:43 +0000448 native long rsnAllocationCreateTyped(long con, long type, int mip, int usage, long pointer);
449 synchronized long nAllocationCreateTyped(long type, int mip, int usage, long pointer) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800450 validate();
Jason Sams857d0c72011-11-23 15:02:15 -0800451 return rsnAllocationCreateTyped(mContext, type, mip, usage, pointer);
Jason Sams2e1872f2010-08-17 16:25:41 -0700452 }
Tim Murray460a0492013-11-19 12:45:54 -0800453 native long rsnAllocationCreateFromBitmap(long con, long type, int mip, Bitmap bmp, int usage);
454 synchronized long nAllocationCreateFromBitmap(long type, int mip, Bitmap bmp, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800455 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800456 return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700457 }
Tim Murraya3145512012-12-04 17:59:29 -0800458
Tim Murray460a0492013-11-19 12:45:54 -0800459 native long rsnAllocationCreateBitmapBackedAllocation(long con, long type, int mip, Bitmap bmp, int usage);
460 synchronized long nAllocationCreateBitmapBackedAllocation(long type, int mip, Bitmap bmp, int usage) {
Tim Murraya3145512012-12-04 17:59:29 -0800461 validate();
462 return rsnAllocationCreateBitmapBackedAllocation(mContext, type, mip, bmp, usage);
463 }
464
Tim Murray460a0492013-11-19 12:45:54 -0800465 native long rsnAllocationCubeCreateFromBitmap(long con, long type, int mip, Bitmap bmp, int usage);
466 synchronized long nAllocationCubeCreateFromBitmap(long type, int mip, Bitmap bmp, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800467 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800468 return rsnAllocationCubeCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800469 }
Tim Murray460a0492013-11-19 12:45:54 -0800470 native long rsnAllocationCreateBitmapRef(long con, long type, Bitmap bmp);
471 synchronized long nAllocationCreateBitmapRef(long type, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800472 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700473 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
474 }
Tim Murray460a0492013-11-19 12:45:54 -0800475 native long rsnAllocationCreateFromAssetStream(long con, int mips, int assetStream, int usage);
476 synchronized long nAllocationCreateFromAssetStream(int mips, int assetStream, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800477 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800478 return rsnAllocationCreateFromAssetStream(mContext, mips, assetStream, usage);
479 }
480
Tim Murray460a0492013-11-19 12:45:54 -0800481 native void rsnAllocationCopyToBitmap(long con, long alloc, Bitmap bmp);
482 synchronized void nAllocationCopyToBitmap(long alloc, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800483 validate();
Jason Sams4ef66502010-12-10 16:03:15 -0800484 rsnAllocationCopyToBitmap(mContext, alloc, bmp);
485 }
486
Tim Murray460a0492013-11-19 12:45:54 -0800487 native void rsnAllocationSyncAll(long con, long alloc, int src);
488 synchronized void nAllocationSyncAll(long alloc, int src) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800489 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800490 rsnAllocationSyncAll(mContext, alloc, src);
491 }
Miao Wang0facf022015-11-25 11:21:13 -0800492
493 native ByteBuffer rsnAllocationGetByteBuffer(long con, long alloc, long[] stride, int xBytesSize, int dimY, int dimZ);
494 synchronized ByteBuffer nAllocationGetByteBuffer(long alloc, long[] stride, int xBytesSize, int dimY, int dimZ) {
495 validate();
496 return rsnAllocationGetByteBuffer(mContext, alloc, stride, xBytesSize, dimY, dimZ);
497 }
498
Miao Wang8c150922015-10-26 17:44:10 -0700499 native void rsnAllocationSetupBufferQueue(long con, long alloc, int numAlloc);
500 synchronized void nAllocationSetupBufferQueue(long alloc, int numAlloc) {
501 validate();
502 rsnAllocationSetupBufferQueue(mContext, alloc, numAlloc);
503 }
504 native void rsnAllocationShareBufferQueue(long con, long alloc1, long alloc2);
505 synchronized void nAllocationShareBufferQueue(long alloc1, long alloc2) {
506 validate();
507 rsnAllocationShareBufferQueue(mContext, alloc1, alloc2);
508 }
Tim Murray460a0492013-11-19 12:45:54 -0800509 native Surface rsnAllocationGetSurface(long con, long alloc);
510 synchronized Surface nAllocationGetSurface(long alloc) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800511 validate();
Jason Sams72226e02013-02-22 12:45:54 -0800512 return rsnAllocationGetSurface(mContext, alloc);
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700513 }
Tim Murray460a0492013-11-19 12:45:54 -0800514 native void rsnAllocationSetSurface(long con, long alloc, Surface sur);
515 synchronized void nAllocationSetSurface(long alloc, Surface sur) {
Jason Sams163766c2012-02-15 12:04:24 -0800516 validate();
Jason Samsfb9aa9f2012-03-28 15:30:07 -0700517 rsnAllocationSetSurface(mContext, alloc, sur);
Jason Sams163766c2012-02-15 12:04:24 -0800518 }
Tim Murray460a0492013-11-19 12:45:54 -0800519 native void rsnAllocationIoSend(long con, long alloc);
520 synchronized void nAllocationIoSend(long alloc) {
Jason Sams163766c2012-02-15 12:04:24 -0800521 validate();
522 rsnAllocationIoSend(mContext, alloc);
523 }
Miao Wang8c150922015-10-26 17:44:10 -0700524 native long rsnAllocationIoReceive(long con, long alloc);
525 synchronized long nAllocationIoReceive(long alloc) {
Jason Sams163766c2012-02-15 12:04:24 -0800526 validate();
Miao Wang8c150922015-10-26 17:44:10 -0700527 return rsnAllocationIoReceive(mContext, alloc);
Jason Sams163766c2012-02-15 12:04:24 -0800528 }
529
Tim Murray460a0492013-11-19 12:45:54 -0800530 native void rsnAllocationGenerateMipmaps(long con, long alloc);
531 synchronized void nAllocationGenerateMipmaps(long alloc) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800532 validate();
Jason Samsf7086092011-01-12 13:28:37 -0800533 rsnAllocationGenerateMipmaps(mContext, alloc);
534 }
Tim Murray460a0492013-11-19 12:45:54 -0800535 native void rsnAllocationCopyFromBitmap(long con, long alloc, Bitmap bmp);
536 synchronized void nAllocationCopyFromBitmap(long alloc, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800537 validate();
Jason Sams4ef66502010-12-10 16:03:15 -0800538 rsnAllocationCopyFromBitmap(mContext, alloc, bmp);
Jason Sams2e1872f2010-08-17 16:25:41 -0700539 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700540
Jason Sams49a05d72010-12-29 14:31:29 -0800541
Miao Wang87e908d2015-03-02 15:15:15 -0800542 native void rsnAllocationData1D(long con, long id, int off, int mip, int count, Object d, int sizeBytes, int dt,
543 int mSize, boolean usePadding);
544 synchronized void nAllocationData1D(long id, int off, int mip, int count, Object d, int sizeBytes, Element.DataType dt,
545 int mSize, boolean usePadding) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800546 validate();
Miao Wang87e908d2015-03-02 15:15:15 -0800547 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes, dt.mID, mSize, usePadding);
Jason Sams2e1872f2010-08-17 16:25:41 -0700548 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700549
Miao Wangc8e237e2015-02-20 18:36:32 -0800550 native void rsnAllocationElementData(long con,long id, int xoff, int yoff, int zoff, int mip, int compIdx, byte[] d, int sizeBytes);
551 synchronized void nAllocationElementData(long id, int xoff, int yoff, int zoff, int mip, int compIdx, byte[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800552 validate();
Miao Wangc8e237e2015-02-20 18:36:32 -0800553 rsnAllocationElementData(mContext, id, xoff, yoff, zoff, mip, compIdx, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700554 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700555
Tim Murrayeff663f2013-11-15 13:08:30 -0800556 native void rsnAllocationData2D(long con,
Tim Murray460a0492013-11-19 12:45:54 -0800557 long dstAlloc, int dstXoff, int dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700558 int dstMip, int dstFace,
559 int width, int height,
Tim Murray460a0492013-11-19 12:45:54 -0800560 long srcAlloc, int srcXoff, int srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700561 int srcMip, int srcFace);
Tim Murray460a0492013-11-19 12:45:54 -0800562 synchronized void nAllocationData2D(long dstAlloc, int dstXoff, int dstYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700563 int dstMip, int dstFace,
564 int width, int height,
Tim Murray460a0492013-11-19 12:45:54 -0800565 long srcAlloc, int srcXoff, int srcYoff,
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700566 int srcMip, int srcFace) {
567 validate();
568 rsnAllocationData2D(mContext,
569 dstAlloc, dstXoff, dstYoff,
570 dstMip, dstFace,
571 width, height,
572 srcAlloc, srcXoff, srcYoff,
573 srcMip, srcFace);
574 }
575
Tim Murray460a0492013-11-19 12:45:54 -0800576 native void rsnAllocationData2D(long con, long id, int xoff, int yoff, int mip, int face,
Miao Wang87e908d2015-03-02 15:15:15 -0800577 int w, int h, Object d, int sizeBytes, int dt,
578 int mSize, boolean usePadding);
Tim Murray460a0492013-11-19 12:45:54 -0800579 synchronized void nAllocationData2D(long id, int xoff, int yoff, int mip, int face,
Miao Wang87e908d2015-03-02 15:15:15 -0800580 int w, int h, Object d, int sizeBytes, Element.DataType dt,
581 int mSize, boolean usePadding) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800582 validate();
Miao Wang87e908d2015-03-02 15:15:15 -0800583 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes, dt.mID, mSize, usePadding);
Jason Sams2e1872f2010-08-17 16:25:41 -0700584 }
Jason Sams21659ac2013-11-06 15:08:07 -0800585
Tim Murray460a0492013-11-19 12:45:54 -0800586 native void rsnAllocationData2D(long con, long id, int xoff, int yoff, int mip, int face, Bitmap b);
587 synchronized void nAllocationData2D(long id, int xoff, int yoff, int mip, int face, Bitmap b) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800588 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800589 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, b);
590 }
Jason Sams49a05d72010-12-29 14:31:29 -0800591
Tim Murrayeff663f2013-11-15 13:08:30 -0800592 native void rsnAllocationData3D(long con,
Tim Murray460a0492013-11-19 12:45:54 -0800593 long dstAlloc, int dstXoff, int dstYoff, int dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -0700594 int dstMip,
595 int width, int height, int depth,
Tim Murray460a0492013-11-19 12:45:54 -0800596 long srcAlloc, int srcXoff, int srcYoff, int srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -0700597 int srcMip);
Tim Murray460a0492013-11-19 12:45:54 -0800598 synchronized void nAllocationData3D(long dstAlloc, int dstXoff, int dstYoff, int dstZoff,
Jason Samsb05d6892013-04-09 15:59:24 -0700599 int dstMip,
600 int width, int height, int depth,
Tim Murray460a0492013-11-19 12:45:54 -0800601 long srcAlloc, int srcXoff, int srcYoff, int srcZoff,
Jason Samsb05d6892013-04-09 15:59:24 -0700602 int srcMip) {
603 validate();
604 rsnAllocationData3D(mContext,
605 dstAlloc, dstXoff, dstYoff, dstZoff,
606 dstMip, width, height, depth,
607 srcAlloc, srcXoff, srcYoff, srcZoff, srcMip);
608 }
609
Tim Murray460a0492013-11-19 12:45:54 -0800610 native void rsnAllocationData3D(long con, long id, int xoff, int yoff, int zoff, int mip,
Miao Wang87e908d2015-03-02 15:15:15 -0800611 int w, int h, int depth, Object d, int sizeBytes, int dt,
612 int mSize, boolean usePadding);
Tim Murray460a0492013-11-19 12:45:54 -0800613 synchronized void nAllocationData3D(long id, int xoff, int yoff, int zoff, int mip,
Miao Wang87e908d2015-03-02 15:15:15 -0800614 int w, int h, int depth, Object d, int sizeBytes, Element.DataType dt,
615 int mSize, boolean usePadding) {
Jason Samsb05d6892013-04-09 15:59:24 -0700616 validate();
Miao Wang87e908d2015-03-02 15:15:15 -0800617 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes,
618 dt.mID, mSize, usePadding);
Jason Samsb05d6892013-04-09 15:59:24 -0700619 }
Jason Samsb05d6892013-04-09 15:59:24 -0700620
Miao Wang87e908d2015-03-02 15:15:15 -0800621 native void rsnAllocationRead(long con, long id, Object d, int dt, int mSize, boolean usePadding);
622 synchronized void nAllocationRead(long id, Object d, Element.DataType dt, int mSize, boolean usePadding) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800623 validate();
Miao Wang87e908d2015-03-02 15:15:15 -0800624 rsnAllocationRead(mContext, id, d, dt.mID, mSize, usePadding);
Jason Samsfa445b92011-01-07 17:00:07 -0800625 }
Jason Sams21659ac2013-11-06 15:08:07 -0800626
Tim Murray460a0492013-11-19 12:45:54 -0800627 native void rsnAllocationRead1D(long con, long id, int off, int mip, int count, Object d,
Miao Wang87e908d2015-03-02 15:15:15 -0800628 int sizeBytes, int dt, int mSize, boolean usePadding);
Tim Murray460a0492013-11-19 12:45:54 -0800629 synchronized void nAllocationRead1D(long id, int off, int mip, int count, Object d,
Miao Wang87e908d2015-03-02 15:15:15 -0800630 int sizeBytes, Element.DataType dt, int mSize, boolean usePadding) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800631 validate();
Miao Wang87e908d2015-03-02 15:15:15 -0800632 rsnAllocationRead1D(mContext, id, off, mip, count, d, sizeBytes, dt.mID, mSize, usePadding);
Jason Samsfa445b92011-01-07 17:00:07 -0800633 }
Jason Sams21659ac2013-11-06 15:08:07 -0800634
Miao Wangc8e237e2015-02-20 18:36:32 -0800635 native void rsnAllocationElementRead(long con,long id, int xoff, int yoff, int zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -0800636 int mip, int compIdx, byte[] d, int sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -0800637 synchronized void nAllocationElementRead(long id, int xoff, int yoff, int zoff,
Miao Wang45cec0a2015-03-04 16:40:21 -0800638 int mip, int compIdx, byte[] d, int sizeBytes) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800639 validate();
Miao Wang45cec0a2015-03-04 16:40:21 -0800640 rsnAllocationElementRead(mContext, id, xoff, yoff, zoff, mip, compIdx, d, sizeBytes);
Miao Wangc8e237e2015-02-20 18:36:32 -0800641 }
642
Tim Murray460a0492013-11-19 12:45:54 -0800643 native void rsnAllocationRead2D(long con, long id, int xoff, int yoff, int mip, int face,
Miao Wang87e908d2015-03-02 15:15:15 -0800644 int w, int h, Object d, int sizeBytes, int dt,
645 int mSize, boolean usePadding);
Tim Murray460a0492013-11-19 12:45:54 -0800646 synchronized void nAllocationRead2D(long id, int xoff, int yoff, int mip, int face,
Miao Wang87e908d2015-03-02 15:15:15 -0800647 int w, int h, Object d, int sizeBytes, Element.DataType dt,
648 int mSize, boolean usePadding) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800649 validate();
Miao Wang87e908d2015-03-02 15:15:15 -0800650 rsnAllocationRead2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes, dt.mID, mSize, usePadding);
Jason Sams2e1872f2010-08-17 16:25:41 -0700651 }
Jason Sams21659ac2013-11-06 15:08:07 -0800652
Miao Wangc8e237e2015-02-20 18:36:32 -0800653 native void rsnAllocationRead3D(long con, long id, int xoff, int yoff, int zoff, int mip,
Miao Wang87e908d2015-03-02 15:15:15 -0800654 int w, int h, int depth, Object d, int sizeBytes, int dt,
655 int mSize, boolean usePadding);
Miao Wangc8e237e2015-02-20 18:36:32 -0800656 synchronized void nAllocationRead3D(long id, int xoff, int yoff, int zoff, int mip,
Miao Wang87e908d2015-03-02 15:15:15 -0800657 int w, int h, int depth, Object d, int sizeBytes, Element.DataType dt,
658 int mSize, boolean usePadding) {
Miao Wangc8e237e2015-02-20 18:36:32 -0800659 validate();
Miao Wang87e908d2015-03-02 15:15:15 -0800660 rsnAllocationRead3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes, dt.mID, mSize, usePadding);
Miao Wangc8e237e2015-02-20 18:36:32 -0800661 }
662
Tim Murray460a0492013-11-19 12:45:54 -0800663 native long rsnAllocationGetType(long con, long id);
664 synchronized long nAllocationGetType(long id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800665 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700666 return rsnAllocationGetType(mContext, id);
667 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700668
Tim Murray460a0492013-11-19 12:45:54 -0800669 native void rsnAllocationResize1D(long con, long id, int dimX);
670 synchronized void nAllocationResize1D(long id, int dimX) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800671 validate();
Jason Sams5edc6082010-10-05 13:32:49 -0700672 rsnAllocationResize1D(mContext, id, dimX);
673 }
Jason Sams5edc6082010-10-05 13:32:49 -0700674
Jason Sams46ba27e32015-02-06 17:45:15 -0800675 native long rsnAllocationAdapterCreate(long con, long allocId, long typeId);
676 synchronized long nAllocationAdapterCreate(long allocId, long typeId) {
677 validate();
678 return rsnAllocationAdapterCreate(mContext, allocId, typeId);
679 }
680
681 native void rsnAllocationAdapterOffset(long con, long id, int x, int y, int z,
682 int mip, int face, int a1, int a2, int a3, int a4);
683 synchronized void nAllocationAdapterOffset(long id, int x, int y, int z,
684 int mip, int face, int a1, int a2, int a3, int a4) {
685 validate();
686 rsnAllocationAdapterOffset(mContext, id, x, y, z, mip, face, a1, a2, a3, a4);
687 }
688
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000689 native long rsnFileA3DCreateFromAssetStream(long con, long assetStream);
690 synchronized long nFileA3DCreateFromAssetStream(long assetStream) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800691 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700692 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
693 }
Tim Murray460a0492013-11-19 12:45:54 -0800694 native long rsnFileA3DCreateFromFile(long con, String path);
695 synchronized long nFileA3DCreateFromFile(String path) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800696 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800697 return rsnFileA3DCreateFromFile(mContext, path);
698 }
Tim Murray460a0492013-11-19 12:45:54 -0800699 native long rsnFileA3DCreateFromAsset(long con, AssetManager mgr, String path);
700 synchronized long nFileA3DCreateFromAsset(AssetManager mgr, String path) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800701 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800702 return rsnFileA3DCreateFromAsset(mContext, mgr, path);
703 }
Tim Murray460a0492013-11-19 12:45:54 -0800704 native int rsnFileA3DGetNumIndexEntries(long con, long fileA3D);
705 synchronized int nFileA3DGetNumIndexEntries(long fileA3D) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800706 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700707 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
708 }
Tim Murray460a0492013-11-19 12:45:54 -0800709 native void rsnFileA3DGetIndexEntries(long con, long fileA3D, int numEntries, int[] IDs, String[] names);
710 synchronized void nFileA3DGetIndexEntries(long fileA3D, int numEntries, int[] IDs, String[] names) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800711 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700712 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
713 }
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000714 native long rsnFileA3DGetEntryByIndex(long con, long fileA3D, int index);
715 synchronized long nFileA3DGetEntryByIndex(long fileA3D, int index) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800716 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700717 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
718 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700719
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000720 native long rsnFontCreateFromFile(long con, String fileName, float size, int dpi);
721 synchronized long nFontCreateFromFile(String fileName, float size, int dpi) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800722 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700723 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
724 }
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000725 native long rsnFontCreateFromAssetStream(long con, String name, float size, int dpi, long assetStream);
726 synchronized long nFontCreateFromAssetStream(String name, float size, int dpi, long assetStream) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800727 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800728 return rsnFontCreateFromAssetStream(mContext, name, size, dpi, assetStream);
729 }
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000730 native long rsnFontCreateFromAsset(long con, AssetManager mgr, String path, float size, int dpi);
731 synchronized long nFontCreateFromAsset(AssetManager mgr, String path, float size, int dpi) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800732 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800733 return rsnFontCreateFromAsset(mContext, mgr, path, size, dpi);
734 }
Jason Sams22534172009-08-04 16:58:20 -0700735
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700736
Tim Murray460a0492013-11-19 12:45:54 -0800737 native void rsnScriptBindAllocation(long con, long script, long alloc, int slot);
738 synchronized void nScriptBindAllocation(long script, long alloc, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800739 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700740 rsnScriptBindAllocation(mContext, script, alloc, slot);
741 }
Tim Murray460a0492013-11-19 12:45:54 -0800742 native void rsnScriptSetTimeZone(long con, long script, byte[] timeZone);
743 synchronized void nScriptSetTimeZone(long script, byte[] timeZone) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800744 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700745 rsnScriptSetTimeZone(mContext, script, timeZone);
746 }
Tim Murray460a0492013-11-19 12:45:54 -0800747 native void rsnScriptInvoke(long con, long id, int slot);
748 synchronized void nScriptInvoke(long id, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800749 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700750 rsnScriptInvoke(mContext, id, slot);
751 }
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700752
753 native void rsnScriptForEach(long con, long id, int slot, long[] ains,
754 long aout, byte[] params, int[] limits);
755
756 synchronized void nScriptForEach(long id, int slot, long[] ains, long aout,
757 byte[] params, int[] limits) {
Jason Sams6e494d32011-04-27 16:33:11 -0700758 validate();
Chris Wailesbe7b1de2014-07-15 10:56:14 -0700759 rsnScriptForEach(mContext, id, slot, ains, aout, params, limits);
Chris Wailes94961062014-06-11 12:01:28 -0700760 }
761
Matt Wala36eb1f72015-07-20 15:35:27 -0700762 native void rsnScriptReduce(long con, long id, int slot, long ain,
763 long aout, int[] limits);
764 synchronized void nScriptReduce(long id, int slot, long ain, long aout,
765 int[] limits) {
766 validate();
767 rsnScriptReduce(mContext, id, slot, ain, aout, limits);
768 }
769
David Gross26ef7a732016-01-12 12:19:15 -0800770 native void rsnScriptReduceNew(long con, long id, int slot, long[] ains,
771 long aout, int[] limits);
772 synchronized void nScriptReduceNew(long id, int slot, long ains[], long aout,
773 int[] limits) {
774 validate();
775 rsnScriptReduceNew(mContext, id, slot, ains, aout, limits);
776 }
777
Tim Murray460a0492013-11-19 12:45:54 -0800778 native void rsnScriptInvokeV(long con, long id, int slot, byte[] params);
779 synchronized void nScriptInvokeV(long id, int slot, byte[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800780 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700781 rsnScriptInvokeV(mContext, id, slot, params);
782 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700783
Tim Murray460a0492013-11-19 12:45:54 -0800784 native void rsnScriptSetVarI(long con, long id, int slot, int val);
785 synchronized void nScriptSetVarI(long id, int slot, int val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800786 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700787 rsnScriptSetVarI(mContext, id, slot, val);
788 }
Tim Murray460a0492013-11-19 12:45:54 -0800789 native int rsnScriptGetVarI(long con, long id, int slot);
790 synchronized int nScriptGetVarI(long id, int slot) {
Tim Murray7c4caad2013-04-10 16:21:40 -0700791 validate();
792 return rsnScriptGetVarI(mContext, id, slot);
793 }
794
Tim Murray460a0492013-11-19 12:45:54 -0800795 native void rsnScriptSetVarJ(long con, long id, int slot, long val);
796 synchronized void nScriptSetVarJ(long id, int slot, long val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800797 validate();
Stephen Hines031ec58c2010-10-11 10:54:21 -0700798 rsnScriptSetVarJ(mContext, id, slot, val);
799 }
Tim Murray460a0492013-11-19 12:45:54 -0800800 native long rsnScriptGetVarJ(long con, long id, int slot);
801 synchronized long nScriptGetVarJ(long id, int slot) {
Tim Murray7c4caad2013-04-10 16:21:40 -0700802 validate();
803 return rsnScriptGetVarJ(mContext, id, slot);
804 }
805
Tim Murray460a0492013-11-19 12:45:54 -0800806 native void rsnScriptSetVarF(long con, long id, int slot, float val);
807 synchronized void nScriptSetVarF(long id, int slot, float val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800808 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700809 rsnScriptSetVarF(mContext, id, slot, val);
810 }
Tim Murray460a0492013-11-19 12:45:54 -0800811 native float rsnScriptGetVarF(long con, long id, int slot);
812 synchronized float nScriptGetVarF(long id, int slot) {
Tim Murray7c4caad2013-04-10 16:21:40 -0700813 validate();
814 return rsnScriptGetVarF(mContext, id, slot);
815 }
Tim Murray460a0492013-11-19 12:45:54 -0800816 native void rsnScriptSetVarD(long con, long id, int slot, double val);
817 synchronized void nScriptSetVarD(long id, int slot, double val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800818 validate();
Stephen Hinesca54ec32010-09-20 17:20:30 -0700819 rsnScriptSetVarD(mContext, id, slot, val);
820 }
Tim Murray460a0492013-11-19 12:45:54 -0800821 native double rsnScriptGetVarD(long con, long id, int slot);
822 synchronized double nScriptGetVarD(long id, int slot) {
Tim Murray7c4caad2013-04-10 16:21:40 -0700823 validate();
824 return rsnScriptGetVarD(mContext, id, slot);
825 }
Tim Murray460a0492013-11-19 12:45:54 -0800826 native void rsnScriptSetVarV(long con, long id, int slot, byte[] val);
827 synchronized void nScriptSetVarV(long id, int slot, byte[] val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800828 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700829 rsnScriptSetVarV(mContext, id, slot, val);
830 }
Tim Murray460a0492013-11-19 12:45:54 -0800831 native void rsnScriptGetVarV(long con, long id, int slot, byte[] val);
832 synchronized void nScriptGetVarV(long id, int slot, byte[] val) {
Tim Murray7c4caad2013-04-10 16:21:40 -0700833 validate();
834 rsnScriptGetVarV(mContext, id, slot, val);
835 }
Tim Murray460a0492013-11-19 12:45:54 -0800836 native void rsnScriptSetVarVE(long con, long id, int slot, byte[] val,
837 long e, int[] dims);
838 synchronized void nScriptSetVarVE(long id, int slot, byte[] val,
839 long e, int[] dims) {
Stephen Hinesadeb8092012-04-20 14:26:06 -0700840 validate();
841 rsnScriptSetVarVE(mContext, id, slot, val, e, dims);
842 }
Tim Murray460a0492013-11-19 12:45:54 -0800843 native void rsnScriptSetVarObj(long con, long id, int slot, long val);
844 synchronized void nScriptSetVarObj(long id, int slot, long val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800845 validate();
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800846 rsnScriptSetVarObj(mContext, id, slot, val);
847 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700848
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000849 native long rsnScriptCCreate(long con, String resName, String cacheDir,
Jason Samse4a06c52011-03-16 16:29:28 -0700850 byte[] script, int length);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000851 synchronized long nScriptCCreate(String resName, String cacheDir, byte[] script, int length) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800852 validate();
Jason Samse4a06c52011-03-16 16:29:28 -0700853 return rsnScriptCCreate(mContext, resName, cacheDir, script, length);
Jason Sams2e1872f2010-08-17 16:25:41 -0700854 }
Jason Samsebfb4362009-09-23 13:57:02 -0700855
Tim Murray460a0492013-11-19 12:45:54 -0800856 native long rsnScriptIntrinsicCreate(long con, int id, long eid);
857 synchronized long nScriptIntrinsicCreate(int id, long eid) {
Jason Sams6ab97682012-08-10 12:09:43 -0700858 validate();
859 return rsnScriptIntrinsicCreate(mContext, id, eid);
860 }
861
Tim Murray460a0492013-11-19 12:45:54 -0800862 native long rsnScriptKernelIDCreate(long con, long sid, int slot, int sig);
863 synchronized long nScriptKernelIDCreate(long sid, int slot, int sig) {
Jason Sams08a81582012-09-18 12:32:10 -0700864 validate();
865 return rsnScriptKernelIDCreate(mContext, sid, slot, sig);
866 }
867
Yang Nibe392ad2015-01-23 17:16:02 -0800868 native long rsnScriptInvokeIDCreate(long con, long sid, int slot);
869 synchronized long nScriptInvokeIDCreate(long sid, int slot) {
870 validate();
871 return rsnScriptInvokeIDCreate(mContext, sid, slot);
872 }
873
Tim Murray460a0492013-11-19 12:45:54 -0800874 native long rsnScriptFieldIDCreate(long con, long sid, int slot);
875 synchronized long nScriptFieldIDCreate(long sid, int slot) {
Jason Sams08a81582012-09-18 12:32:10 -0700876 validate();
877 return rsnScriptFieldIDCreate(mContext, sid, slot);
878 }
879
Ashok Bhat98071552014-02-12 09:54:43 +0000880 native long rsnScriptGroupCreate(long con, long[] kernels, long[] src, long[] dstk, long[] dstf, long[] types);
881 synchronized long nScriptGroupCreate(long[] kernels, long[] src, long[] dstk, long[] dstf, long[] types) {
Jason Sams08a81582012-09-18 12:32:10 -0700882 validate();
883 return rsnScriptGroupCreate(mContext, kernels, src, dstk, dstf, types);
884 }
885
Tim Murray460a0492013-11-19 12:45:54 -0800886 native void rsnScriptGroupSetInput(long con, long group, long kernel, long alloc);
887 synchronized void nScriptGroupSetInput(long group, long kernel, long alloc) {
Jason Sams08a81582012-09-18 12:32:10 -0700888 validate();
889 rsnScriptGroupSetInput(mContext, group, kernel, alloc);
890 }
891
Tim Murray460a0492013-11-19 12:45:54 -0800892 native void rsnScriptGroupSetOutput(long con, long group, long kernel, long alloc);
893 synchronized void nScriptGroupSetOutput(long group, long kernel, long alloc) {
Jason Sams08a81582012-09-18 12:32:10 -0700894 validate();
895 rsnScriptGroupSetOutput(mContext, group, kernel, alloc);
896 }
897
Tim Murray460a0492013-11-19 12:45:54 -0800898 native void rsnScriptGroupExecute(long con, long group);
899 synchronized void nScriptGroupExecute(long group) {
Jason Sams08a81582012-09-18 12:32:10 -0700900 validate();
901 rsnScriptGroupExecute(mContext, group);
902 }
903
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000904 native long rsnSamplerCreate(long con, int magFilter, int minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -0700905 int wrapS, int wrapT, int wrapR, float aniso);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000906 synchronized long nSamplerCreate(int magFilter, int minFilter,
Alex Sakhartchouka89094a2011-05-04 17:45:36 -0700907 int wrapS, int wrapT, int wrapR, float aniso) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800908 validate();
Alex Sakhartchouka89094a2011-05-04 17:45:36 -0700909 return rsnSamplerCreate(mContext, magFilter, minFilter, wrapS, wrapT, wrapR, aniso);
Jason Sams2e1872f2010-08-17 16:25:41 -0700910 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800911
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000912 native long rsnProgramStoreCreate(long con, boolean r, boolean g, boolean b, boolean a,
Jason Sams331bf9b2011-04-06 11:23:54 -0700913 boolean depthMask, boolean dither,
914 int srcMode, int dstMode, int depthFunc);
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000915 synchronized long nProgramStoreCreate(boolean r, boolean g, boolean b, boolean a,
Jason Sams331bf9b2011-04-06 11:23:54 -0700916 boolean depthMask, boolean dither,
917 int srcMode, int dstMode, int depthFunc) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800918 validate();
Jason Samsbd184c52011-04-06 11:44:47 -0700919 return rsnProgramStoreCreate(mContext, r, g, b, a, depthMask, dither, srcMode,
920 dstMode, depthFunc);
Jason Sams2e1872f2010-08-17 16:25:41 -0700921 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700922
Tim Murray460a0492013-11-19 12:45:54 -0800923 native long rsnProgramRasterCreate(long con, boolean pointSprite, int cullMode);
924 synchronized long nProgramRasterCreate(boolean pointSprite, int cullMode) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800925 validate();
Jason Sams94aaed32011-09-23 14:18:53 -0700926 return rsnProgramRasterCreate(mContext, pointSprite, cullMode);
Jason Sams2e1872f2010-08-17 16:25:41 -0700927 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700928
Tim Murray460a0492013-11-19 12:45:54 -0800929 native void rsnProgramBindConstants(long con, long pv, int slot, long mID);
930 synchronized void nProgramBindConstants(long pv, int slot, long mID) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800931 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700932 rsnProgramBindConstants(mContext, pv, slot, mID);
933 }
Tim Murray460a0492013-11-19 12:45:54 -0800934 native void rsnProgramBindTexture(long con, long vpf, int slot, long a);
935 synchronized void nProgramBindTexture(long vpf, int slot, long a) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800936 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700937 rsnProgramBindTexture(mContext, vpf, slot, a);
938 }
Tim Murray460a0492013-11-19 12:45:54 -0800939 native void rsnProgramBindSampler(long con, long vpf, int slot, long s);
940 synchronized void nProgramBindSampler(long vpf, int slot, long s) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800941 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700942 rsnProgramBindSampler(mContext, vpf, slot, s);
943 }
Ashok Bhat98071552014-02-12 09:54:43 +0000944 native long rsnProgramFragmentCreate(long con, String shader, String[] texNames, long[] params);
945 synchronized long nProgramFragmentCreate(String shader, String[] texNames, long[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800946 validate();
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800947 return rsnProgramFragmentCreate(mContext, shader, texNames, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700948 }
Ashok Bhat98071552014-02-12 09:54:43 +0000949 native long rsnProgramVertexCreate(long con, String shader, String[] texNames, long[] params);
950 synchronized long nProgramVertexCreate(String shader, String[] texNames, long[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800951 validate();
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800952 return rsnProgramVertexCreate(mContext, shader, texNames, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700953 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700954
Ashok Bhat98071552014-02-12 09:54:43 +0000955 native long rsnMeshCreate(long con, long[] vtx, long[] idx, int[] prim);
956 synchronized long nMeshCreate(long[] vtx, long[] idx, int[] prim) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800957 validate();
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700958 return rsnMeshCreate(mContext, vtx, idx, prim);
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800959 }
Tim Murray460a0492013-11-19 12:45:54 -0800960 native int rsnMeshGetVertexBufferCount(long con, long id);
961 synchronized int nMeshGetVertexBufferCount(long id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800962 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700963 return rsnMeshGetVertexBufferCount(mContext, id);
964 }
Tim Murray460a0492013-11-19 12:45:54 -0800965 native int rsnMeshGetIndexCount(long con, long id);
966 synchronized int nMeshGetIndexCount(long id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800967 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700968 return rsnMeshGetIndexCount(mContext, id);
969 }
Ashok Bhat98071552014-02-12 09:54:43 +0000970 native void rsnMeshGetVertices(long con, long id, long[] vtxIds, int vtxIdCount);
971 synchronized void nMeshGetVertices(long id, long[] vtxIds, int vtxIdCount) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800972 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700973 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
974 }
Ashok Bhat98071552014-02-12 09:54:43 +0000975 native void rsnMeshGetIndices(long con, long id, long[] idxIds, int[] primitives, int vtxIdCount);
976 synchronized void nMeshGetIndices(long id, long[] idxIds, int[] primitives, int vtxIdCount) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800977 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700978 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
979 }
980
Tim Murray25207df2015-01-12 16:47:56 -0800981 native void rsnScriptIntrinsicBLAS_Single(long con, long id, int func, int TransA,
982 int TransB, int Side, int Uplo, int Diag, int M, int N, int K,
983 float alpha, long A, long B, float beta, long C, int incX, int incY,
984 int KL, int KU);
985 synchronized void nScriptIntrinsicBLAS_Single(long id, int func, int TransA,
986 int TransB, int Side, int Uplo, int Diag, int M, int N, int K,
987 float alpha, long A, long B, float beta, long C, int incX, int incY,
988 int KL, int KU) {
989 validate();
990 rsnScriptIntrinsicBLAS_Single(mContext, id, func, TransA, TransB, Side, Uplo, Diag, M, N, K, alpha, A, B, beta, C, incX, incY, KL, KU);
991 }
992
993 native void rsnScriptIntrinsicBLAS_Double(long con, long id, int func, int TransA,
994 int TransB, int Side, int Uplo, int Diag, int M, int N, int K,
995 double alpha, long A, long B, double beta, long C, int incX, int incY,
996 int KL, int KU);
997 synchronized void nScriptIntrinsicBLAS_Double(long id, int func, int TransA,
998 int TransB, int Side, int Uplo, int Diag, int M, int N, int K,
999 double alpha, long A, long B, double beta, long C, int incX, int incY,
1000 int KL, int KU) {
1001 validate();
1002 rsnScriptIntrinsicBLAS_Double(mContext, id, func, TransA, TransB, Side, Uplo, Diag, M, N, K, alpha, A, B, beta, C, incX, incY, KL, KU);
1003 }
1004
1005 native void rsnScriptIntrinsicBLAS_Complex(long con, long id, int func, int TransA,
1006 int TransB, int Side, int Uplo, int Diag, int M, int N, int K,
1007 float alphaX, float alphaY, long A, long B, float betaX, float betaY, long C, int incX, int incY,
1008 int KL, int KU);
1009 synchronized void nScriptIntrinsicBLAS_Complex(long id, int func, int TransA,
1010 int TransB, int Side, int Uplo, int Diag, int M, int N, int K,
1011 float alphaX, float alphaY, long A, long B, float betaX, float betaY, long C, int incX, int incY,
1012 int KL, int KU) {
1013 validate();
1014 rsnScriptIntrinsicBLAS_Complex(mContext, id, func, TransA, TransB, Side, Uplo, Diag, M, N, K, alphaX, alphaY, A, B, betaX, betaY, C, incX, incY, KL, KU);
1015 }
1016
1017 native void rsnScriptIntrinsicBLAS_Z(long con, long id, int func, int TransA,
1018 int TransB, int Side, int Uplo, int Diag, int M, int N, int K,
1019 double alphaX, double alphaY, long A, long B, double betaX, double betaY, long C, int incX, int incY,
1020 int KL, int KU);
1021 synchronized void nScriptIntrinsicBLAS_Z(long id, int func, int TransA,
1022 int TransB, int Side, int Uplo, int Diag, int M, int N, int K,
1023 double alphaX, double alphaY, long A, long B, double betaX, double betaY, long C, int incX, int incY,
1024 int KL, int KU) {
1025 validate();
1026 rsnScriptIntrinsicBLAS_Z(mContext, id, func, TransA, TransB, Side, Uplo, Diag, M, N, K, alphaX, alphaY, A, B, betaX, betaY, C, incX, incY, KL, KU);
1027 }
1028
Tim Murray9cb16a22015-04-01 11:07:16 -07001029 native void rsnScriptIntrinsicBLAS_BNNM(long con, long id, int M, int N, int K,
1030 long A, int a_offset, long B, int b_offset, long C, int c_offset,
1031 int c_mult_int);
1032 synchronized void nScriptIntrinsicBLAS_BNNM(long id, int M, int N, int K,
1033 long A, int a_offset, long B, int b_offset, long C, int c_offset,
1034 int c_mult_int) {
1035 validate();
1036 rsnScriptIntrinsicBLAS_BNNM(mContext, id, M, N, K, A, a_offset, B, b_offset, C, c_offset, c_mult_int);
1037 }
1038
1039
Tim Murray25207df2015-01-12 16:47:56 -08001040
Tim Murrayeff663f2013-11-15 13:08:30 -08001041 long mDev;
1042 long mContext;
Jason Samsd22a6f02015-02-19 17:19:52 -08001043 private boolean mDestroyed = false;
1044
Romain Guy650a3eb2009-08-31 14:06:43 -07001045 @SuppressWarnings({"FieldCanBeLocal"})
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001046 MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001047
Jason Sams8cb39de2010-06-01 15:47:01 -07001048 Element mElement_U8;
1049 Element mElement_I8;
1050 Element mElement_U16;
1051 Element mElement_I16;
1052 Element mElement_U32;
1053 Element mElement_I32;
Stephen Hines52d83632010-10-11 16:10:42 -07001054 Element mElement_U64;
Stephen Hinesef1dac22010-10-01 15:39:33 -07001055 Element mElement_I64;
Jason Samsa5835a22014-11-05 15:16:26 -08001056 Element mElement_F16;
Jason Sams8cb39de2010-06-01 15:47:01 -07001057 Element mElement_F32;
Stephen Hines02f417052010-09-30 15:19:22 -07001058 Element mElement_F64;
Jason Samsf110d4b2010-06-21 17:42:41 -07001059 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -07001060
Jason Sams8cb39de2010-06-01 15:47:01 -07001061 Element mElement_ELEMENT;
1062 Element mElement_TYPE;
1063 Element mElement_ALLOCATION;
1064 Element mElement_SAMPLER;
1065 Element mElement_SCRIPT;
1066 Element mElement_MESH;
1067 Element mElement_PROGRAM_FRAGMENT;
1068 Element mElement_PROGRAM_VERTEX;
1069 Element mElement_PROGRAM_RASTER;
1070 Element mElement_PROGRAM_STORE;
Stephen Hines3a291412012-04-11 17:27:29 -07001071 Element mElement_FONT;
Jason Samsa70f4162010-03-26 15:33:42 -07001072
Jason Sams3c0dfba2009-09-27 17:50:38 -07001073 Element mElement_A_8;
1074 Element mElement_RGB_565;
1075 Element mElement_RGB_888;
1076 Element mElement_RGBA_5551;
1077 Element mElement_RGBA_4444;
1078 Element mElement_RGBA_8888;
1079
Jason Samsa5835a22014-11-05 15:16:26 -08001080 Element mElement_HALF_2;
1081 Element mElement_HALF_3;
1082 Element mElement_HALF_4;
1083
Jason Sams8cb39de2010-06-01 15:47:01 -07001084 Element mElement_FLOAT_2;
1085 Element mElement_FLOAT_3;
1086 Element mElement_FLOAT_4;
Stephen Hines836c4a52011-06-01 14:38:10 -07001087
1088 Element mElement_DOUBLE_2;
1089 Element mElement_DOUBLE_3;
1090 Element mElement_DOUBLE_4;
1091
1092 Element mElement_UCHAR_2;
1093 Element mElement_UCHAR_3;
Jason Sams8cb39de2010-06-01 15:47:01 -07001094 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -08001095
Stephen Hines836c4a52011-06-01 14:38:10 -07001096 Element mElement_CHAR_2;
1097 Element mElement_CHAR_3;
1098 Element mElement_CHAR_4;
1099
1100 Element mElement_USHORT_2;
1101 Element mElement_USHORT_3;
1102 Element mElement_USHORT_4;
1103
1104 Element mElement_SHORT_2;
1105 Element mElement_SHORT_3;
1106 Element mElement_SHORT_4;
1107
1108 Element mElement_UINT_2;
1109 Element mElement_UINT_3;
1110 Element mElement_UINT_4;
1111
1112 Element mElement_INT_2;
1113 Element mElement_INT_3;
1114 Element mElement_INT_4;
1115
1116 Element mElement_ULONG_2;
1117 Element mElement_ULONG_3;
1118 Element mElement_ULONG_4;
1119
1120 Element mElement_LONG_2;
1121 Element mElement_LONG_3;
1122 Element mElement_LONG_4;
1123
Tim Murray932e78e2013-09-03 11:42:26 -07001124 Element mElement_YUV;
1125
Jason Sams1d45c472010-08-25 14:31:48 -07001126 Element mElement_MATRIX_4X4;
1127 Element mElement_MATRIX_3X3;
1128 Element mElement_MATRIX_2X2;
1129
Jason Sams4d339932010-05-11 14:03:58 -07001130 Sampler mSampler_CLAMP_NEAREST;
1131 Sampler mSampler_CLAMP_LINEAR;
1132 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
1133 Sampler mSampler_WRAP_NEAREST;
1134 Sampler mSampler_WRAP_LINEAR;
1135 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
Tim Murray6b9b2ca2013-02-15 13:25:55 -08001136 Sampler mSampler_MIRRORED_REPEAT_NEAREST;
1137 Sampler mSampler_MIRRORED_REPEAT_LINEAR;
1138 Sampler mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
Jason Sams4d339932010-05-11 14:03:58 -07001139
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -07001140 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
1141 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -07001142 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
1143 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -07001144
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -07001145 ProgramRaster mProgramRaster_CULL_BACK;
1146 ProgramRaster mProgramRaster_CULL_FRONT;
1147 ProgramRaster mProgramRaster_CULL_NONE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -07001148
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001149 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -07001150 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001151
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001152 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001153 * The base class from which an application should derive in order
1154 * to receive RS messages from scripts. When a script calls {@code
1155 * rsSendToClient}, the data fields will be filled, and the run
1156 * method will be called on a separate thread. This will occur
1157 * some time after {@code rsSendToClient} completes in the script,
1158 * as {@code rsSendToClient} is asynchronous. Message handlers are
1159 * not guaranteed to have completed when {@link
1160 * android.renderscript.RenderScript#finish} returns.
Jason Sams27676fe2010-11-10 17:00:59 -08001161 *
1162 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001163 public static class RSMessageHandler implements Runnable {
Jason Sams516c3192009-10-06 13:58:47 -07001164 protected int[] mData;
1165 protected int mID;
Jason Sams1c415172010-11-08 17:06:46 -08001166 protected int mLength;
Jason Sams516c3192009-10-06 13:58:47 -07001167 public void run() {
1168 }
1169 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001170 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001171 * If an application is expecting messages, it should set this
1172 * field to an instance of {@link RSMessageHandler}. This
1173 * instance will receive all the user messages sent from {@code
1174 * sendToClient} by scripts from this context.
Jason Sams27676fe2010-11-10 17:00:59 -08001175 *
1176 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001177 RSMessageHandler mMessageCallback = null;
1178
1179 public void setMessageHandler(RSMessageHandler msg) {
1180 mMessageCallback = msg;
1181 }
1182 public RSMessageHandler getMessageHandler() {
1183 return mMessageCallback;
1184 }
Jason Sams516c3192009-10-06 13:58:47 -07001185
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001186 /**
Jason Sams02d56d92013-04-12 16:40:50 -07001187 * Place a message into the message queue to be sent back to the message
1188 * handler once all previous commands have been executed.
Jason Sams455d6442013-02-05 19:20:18 -08001189 *
1190 * @param id
1191 * @param data
1192 */
1193 public void sendMessage(int id, int[] data) {
1194 nContextSendMessage(id, data);
1195 }
1196
1197 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001198 * The runtime error handler base class. An application should derive from this class
1199 * if it wishes to install an error handler. When errors occur at runtime,
1200 * the fields in this class will be filled, and the run method will be called.
Jason Sams27676fe2010-11-10 17:00:59 -08001201 *
1202 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001203 public static class RSErrorHandler implements Runnable {
Jason Sams1c415172010-11-08 17:06:46 -08001204 protected String mErrorMessage;
1205 protected int mErrorNum;
1206 public void run() {
1207 }
1208 }
Jason Sams27676fe2010-11-10 17:00:59 -08001209
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001210 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001211 * Application Error handler. All runtime errors will be dispatched to the
1212 * instance of RSAsyncError set here. If this field is null a
Tim Murrayc11e25c2013-04-09 11:01:01 -07001213 * {@link RSRuntimeException} will instead be thrown with details about the error.
Jason Sams27676fe2010-11-10 17:00:59 -08001214 * This will cause program termaination.
1215 *
1216 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001217 RSErrorHandler mErrorCallback = null;
1218
1219 public void setErrorHandler(RSErrorHandler msg) {
1220 mErrorCallback = msg;
1221 }
1222 public RSErrorHandler getErrorHandler() {
1223 return mErrorCallback;
1224 }
Jason Sams1c415172010-11-08 17:06:46 -08001225
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001226 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001227 * RenderScript worker thread priority enumeration. The default value is
1228 * NORMAL. Applications wishing to do background processing should set
1229 * their priority to LOW to avoid starving forground processes.
Jason Sams27676fe2010-11-10 17:00:59 -08001230 */
Jason Sams7d787b42009-11-15 12:14:26 -08001231 public enum Priority {
Jason Samsc9870c12015-01-21 12:55:14 -08001232 // These values used to represent official thread priority values
1233 // now they are simply enums to be used by the runtime side
1234 LOW (15),
1235 NORMAL (-8);
Jason Sams7d787b42009-11-15 12:14:26 -08001236
1237 int mID;
1238 Priority(int id) {
1239 mID = id;
1240 }
1241 }
1242
Jason Sams678cc7f2014-03-05 16:09:02 -08001243 void validateObject(BaseObj o) {
1244 if (o != null) {
1245 if (o.mRS != this) {
1246 throw new RSIllegalArgumentException("Attempting to use an object across contexts.");
1247 }
1248 }
1249 }
1250
Jason Sams771bebb2009-12-07 12:40:12 -08001251 void validate() {
1252 if (mContext == 0) {
Jason Samsc1d62102010-11-04 14:32:19 -07001253 throw new RSInvalidStateException("Calling RS with no Context active.");
Jason Sams771bebb2009-12-07 12:40:12 -08001254 }
1255 }
1256
Jason Sams27676fe2010-11-10 17:00:59 -08001257
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001258 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001259 * Change the priority of the worker threads for this context.
1260 *
1261 * @param p New priority to be set.
1262 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001263 public void setPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -08001264 validate();
Jason Sams7d787b42009-11-15 12:14:26 -08001265 nContextSetPriority(p.mID);
1266 }
1267
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001268 static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -07001269 RenderScript mRS;
1270 boolean mRun = true;
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001271 int[] mAuxData = new int[2];
Jason Sams1c415172010-11-08 17:06:46 -08001272
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001273 static final int RS_MESSAGE_TO_CLIENT_NONE = 0;
1274 static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1;
1275 static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
1276 static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
1277 static final int RS_MESSAGE_TO_CLIENT_USER = 4;
Jason Sams739c8262013-04-11 18:07:52 -07001278 static final int RS_MESSAGE_TO_CLIENT_NEW_BUFFER = 5;
Jason Sams516c3192009-10-06 13:58:47 -07001279
Stephen Hines42028a82013-04-17 19:22:01 -07001280 static final int RS_ERROR_FATAL_DEBUG = 0x0800;
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001281 static final int RS_ERROR_FATAL_UNKNOWN = 0x1000;
Jason Samsadd9d962010-11-22 16:20:16 -08001282
Jason Sams516c3192009-10-06 13:58:47 -07001283 MessageThread(RenderScript rs) {
1284 super("RSMessageThread");
1285 mRS = rs;
1286
1287 }
1288
1289 public void run() {
1290 // This function is a temporary solution. The final solution will
1291 // used typed allocations where the message id is the type indicator.
1292 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -07001293 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -07001294 while(mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -07001295 rbuf[0] = 0;
Jason Samsedbfabd2011-05-17 15:01:29 -07001296 int msg = mRS.nContextPeekMessage(mRS.mContext, mAuxData);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001297 int size = mAuxData[1];
1298 int subID = mAuxData[0];
Jason Sams1c415172010-11-08 17:06:46 -08001299
1300 if (msg == RS_MESSAGE_TO_CLIENT_USER) {
1301 if ((size>>2) >= rbuf.length) {
1302 rbuf = new int[(size + 3) >> 2];
1303 }
Jason Samsedbfabd2011-05-17 15:01:29 -07001304 if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) !=
1305 RS_MESSAGE_TO_CLIENT_USER) {
Tim Murrayc11e25c2013-04-09 11:01:01 -07001306 throw new RSDriverException("Error processing message from RenderScript.");
Jason Samsedbfabd2011-05-17 15:01:29 -07001307 }
Jason Sams1c415172010-11-08 17:06:46 -08001308
1309 if(mRS.mMessageCallback != null) {
1310 mRS.mMessageCallback.mData = rbuf;
1311 mRS.mMessageCallback.mID = subID;
1312 mRS.mMessageCallback.mLength = size;
1313 mRS.mMessageCallback.run();
Jason Sams1d45c472010-08-25 14:31:48 -07001314 } else {
Jason Sams1c415172010-11-08 17:06:46 -08001315 throw new RSInvalidStateException("Received a message from the script with no message handler installed.");
Jason Sams516c3192009-10-06 13:58:47 -07001316 }
Stephen Hinesab98bb62010-09-24 14:38:30 -07001317 continue;
Jason Sams516c3192009-10-06 13:58:47 -07001318 }
Jason Sams1c415172010-11-08 17:06:46 -08001319
1320 if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
1321 String e = mRS.nContextGetErrorMessage(mRS.mContext);
1322
Stephen Hines42028a82013-04-17 19:22:01 -07001323 // Throw RSRuntimeException under the following conditions:
1324 //
1325 // 1) It is an unknown fatal error.
1326 // 2) It is a debug fatal error, and we are not in a
1327 // debug context.
1328 // 3) It is a debug fatal error, and we do not have an
1329 // error callback.
1330 if (subID >= RS_ERROR_FATAL_UNKNOWN ||
1331 (subID >= RS_ERROR_FATAL_DEBUG &&
1332 (mRS.mContextType != ContextType.DEBUG ||
1333 mRS.mErrorCallback == null))) {
Jason Samsadd9d962010-11-22 16:20:16 -08001334 throw new RSRuntimeException("Fatal error " + subID + ", details: " + e);
1335 }
1336
Jason Sams1c415172010-11-08 17:06:46 -08001337 if(mRS.mErrorCallback != null) {
1338 mRS.mErrorCallback.mErrorMessage = e;
1339 mRS.mErrorCallback.mErrorNum = subID;
1340 mRS.mErrorCallback.run();
1341 } else {
Jason Samsa4b7bc92013-02-05 15:05:39 -08001342 android.util.Log.e(LOG_TAG, "non fatal RS error, " + e);
Stephen Hinesbe74bdd2012-02-03 15:29:36 -08001343 // Do not throw here. In these cases, we do not have
1344 // a fatal error.
Jason Sams1c415172010-11-08 17:06:46 -08001345 }
1346 continue;
1347 }
1348
Jason Sams739c8262013-04-11 18:07:52 -07001349 if (msg == RS_MESSAGE_TO_CLIENT_NEW_BUFFER) {
Tim Murrayb730d862014-08-18 16:14:24 -07001350 if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) !=
1351 RS_MESSAGE_TO_CLIENT_NEW_BUFFER) {
1352 throw new RSDriverException("Error processing message from RenderScript.");
1353 }
1354 long bufferID = ((long)rbuf[1] << 32L) + ((long)rbuf[0] & 0xffffffffL);
1355 Allocation.sendBufferNotification(bufferID);
Jason Sams739c8262013-04-11 18:07:52 -07001356 continue;
1357 }
1358
Jason Sams1c415172010-11-08 17:06:46 -08001359 // 2: teardown.
1360 // But we want to avoid starving other threads during
1361 // teardown by yielding until the next line in the destructor
1362 // can execute to set mRun = false
1363 try {
1364 sleep(1, 0);
1365 } catch(InterruptedException e) {
Jason Sams516c3192009-10-06 13:58:47 -07001366 }
Jason Sams516c3192009-10-06 13:58:47 -07001367 }
Tim Murrayda67deb2013-05-09 12:02:50 -07001368 //Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -07001369 }
1370 }
1371
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001372 RenderScript(Context ctx) {
Stephen Hines42028a82013-04-17 19:22:01 -07001373 mContextType = ContextType.NORMAL;
Jason Sams1a4e1f3e2012-02-24 17:51:24 -08001374 if (ctx != null) {
1375 mApplicationContext = ctx.getApplicationContext();
1376 }
Tim Murray06b45672014-01-07 11:13:56 -08001377 mRWLock = new ReentrantReadWriteLock();
Tim Murrayaefbd5f2014-12-12 11:34:48 -08001378 try {
Tim Murrayd11a6582014-12-16 09:59:09 -08001379 registerNativeAllocation.invoke(sRuntime, 4 * 1024 * 1024); // 4MB for GC sake
Tim Murrayaefbd5f2014-12-12 11:34:48 -08001380 } catch (Exception e) {
1381 Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
1382 throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
1383 }
1384
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001385 }
1386
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001387 /**
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001388 * Gets the application context associated with the RenderScript context.
1389 *
1390 * @return The application context.
1391 */
1392 public final Context getApplicationContext() {
1393 return mApplicationContext;
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001394 }
1395
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001396 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001397 * Create a RenderScript context.
Jason Sams27676fe2010-11-10 17:00:59 -08001398 *
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001399 * @param ctx The context.
Jason Sams27676fe2010-11-10 17:00:59 -08001400 * @return RenderScript
1401 */
Jason Samse16da122015-03-18 17:04:18 -07001402 private static RenderScript internalCreate(Context ctx, int sdkVersion, ContextType ct, int flags) {
Dan Morrille4d9a012013-03-28 18:10:43 -07001403 if (!sInitialized) {
1404 Log.e(LOG_TAG, "RenderScript.create() called when disabled; someone is likely to crash");
1405 return null;
1406 }
1407
verena beckhamc9659ea2015-05-22 16:47:53 +01001408 if ((flags & ~(CREATE_FLAG_LOW_LATENCY | CREATE_FLAG_LOW_POWER |
1409 CREATE_FLAG_WAIT_FOR_ATTACH | CREATE_FLAG_OPT_LEVEL_0)) != 0) {
Jason Samsb69c7912014-05-20 18:48:35 -07001410 throw new RSIllegalArgumentException("Invalid flags passed.");
1411 }
1412
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001413 RenderScript rs = new RenderScript(ctx);
Jason Sams704ff642010-02-09 16:05:07 -08001414
1415 rs.mDev = rs.nDeviceCreate();
Tim Murrayfd710e72014-06-06 11:10:45 -07001416 rs.mContext = rs.nContextCreate(rs.mDev, flags, sdkVersion, ct.mID);
Stephen Hines42028a82013-04-17 19:22:01 -07001417 rs.mContextType = ct;
Jason Samse16da122015-03-18 17:04:18 -07001418 rs.mContextFlags = flags;
1419 rs.mContextSdkVersion = sdkVersion;
Jason Sams26985362011-05-03 15:01:58 -07001420 if (rs.mContext == 0) {
1421 throw new RSDriverException("Failed to create RS context.");
1422 }
Tim Murray47f31582015-04-07 15:43:24 -07001423
1424 // set up cache directory for entire context
1425 final String CACHE_PATH = "com.android.renderscript.cache";
1426 File f = new File(RenderScriptCacheDir.mCacheDir, CACHE_PATH);
1427 String mCachePath = f.getAbsolutePath();
1428 f.mkdirs();
1429 rs.nContextSetCacheDir(mCachePath);
1430
Jason Sams704ff642010-02-09 16:05:07 -08001431 rs.mMessageThread = new MessageThread(rs);
1432 rs.mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -08001433 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -08001434 }
1435
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001436 /**
Miao Wanga4e5adf2015-03-23 11:09:56 -07001437 * calls create(ctx, ContextType.NORMAL, CREATE_FLAG_NONE)
Jason Samse16da122015-03-18 17:04:18 -07001438 *
1439 * See documentation for @create for details
Jason Sams1a4e1f3e2012-02-24 17:51:24 -08001440 *
1441 * @param ctx The context.
1442 * @return RenderScript
1443 */
1444 public static RenderScript create(Context ctx) {
Jason Samsadd26dc2013-02-22 18:43:45 -08001445 return create(ctx, ContextType.NORMAL);
1446 }
1447
1448 /**
Miao Wanga4e5adf2015-03-23 11:09:56 -07001449 * calls create(ctx, ct, CREATE_FLAG_NONE)
Jason Samsadd26dc2013-02-22 18:43:45 -08001450 *
Jason Samse16da122015-03-18 17:04:18 -07001451 * See documentation for @create for details
Jason Samsadd26dc2013-02-22 18:43:45 -08001452 *
1453 * @param ctx The context.
Jason Sams02d56d92013-04-12 16:40:50 -07001454 * @param ct The type of context to be created.
Jason Samsadd26dc2013-02-22 18:43:45 -08001455 * @return RenderScript
1456 */
1457 public static RenderScript create(Context ctx, ContextType ct) {
Jason Samse16da122015-03-18 17:04:18 -07001458 return create(ctx, ct, CREATE_FLAG_NONE);
Jason Sams26e90512014-05-07 14:23:27 -07001459 }
1460
Miao Wanga4e5adf2015-03-23 11:09:56 -07001461
1462 /**
Jason Samse16da122015-03-18 17:04:18 -07001463 * Gets or creates a RenderScript context of the specified type.
Jason Sams26e90512014-05-07 14:23:27 -07001464 *
Jason Samse16da122015-03-18 17:04:18 -07001465 * The returned context will be cached for future reuse within
1466 * the process. When an application is finished using
1467 * RenderScript it should call releaseAllContexts()
1468 *
1469 * A process context is a context designed for easy creation and
1470 * lifecycle management. Multiple calls to this function will
1471 * return the same object provided they are called with the same
1472 * options. This allows it to be used any time a RenderScript
1473 * context is needed.
1474 *
1475 * Prior to API 23 this always created a new context.
Jason Sams26e90512014-05-07 14:23:27 -07001476 *
1477 * @param ctx The context.
1478 * @param ct The type of context to be created.
1479 * @param flags The OR of the CREATE_FLAG_* options desired
1480 * @return RenderScript
1481 */
Tim Murrayfd710e72014-06-06 11:10:45 -07001482 public static RenderScript create(Context ctx, ContextType ct, int flags) {
Jason Sams26e90512014-05-07 14:23:27 -07001483 int v = ctx.getApplicationInfo().targetSdkVersion;
Miao Wanga4e5adf2015-03-23 11:09:56 -07001484 return create(ctx, v, ct, flags);
1485 }
1486
1487 /**
1488 * calls create(ctx, sdkVersion, ContextType.NORMAL, CREATE_FLAG_NONE)
1489 *
1490 * Used by the RenderScriptThunker to maintain backward compatibility.
1491 *
1492 * @hide
1493 * @param ctx The context.
1494 * @param sdkVersion The target SDK Version.
1495 * @return RenderScript
1496 */
1497 public static RenderScript create(Context ctx, int sdkVersion) {
1498 return create(ctx, sdkVersion, ContextType.NORMAL, CREATE_FLAG_NONE);
1499 }
1500
1501 /**
1502 * Gets or creates a RenderScript context of the specified type.
1503 *
Miao Wanga4e5adf2015-03-23 11:09:56 -07001504 * @param ctx The context.
1505 * @param ct The type of context to be created.
1506 * @param sdkVersion The target SDK Version.
1507 * @param flags The OR of the CREATE_FLAG_* options desired
1508 * @return RenderScript
1509 */
Jason Sams6a420b52015-03-30 15:31:26 -07001510 private static RenderScript create(Context ctx, int sdkVersion, ContextType ct, int flags) {
Miao Wanga4e5adf2015-03-23 11:09:56 -07001511 if (sdkVersion < 23) {
1512 return internalCreate(ctx, sdkVersion, ct, flags);
Jason Samse16da122015-03-18 17:04:18 -07001513 }
1514
1515 synchronized (mProcessContextList) {
1516 for (RenderScript prs : mProcessContextList) {
1517 if ((prs.mContextType == ct) &&
1518 (prs.mContextFlags == flags) &&
Miao Wanga4e5adf2015-03-23 11:09:56 -07001519 (prs.mContextSdkVersion == sdkVersion)) {
Jason Samse16da122015-03-18 17:04:18 -07001520
1521 return prs;
1522 }
1523 }
1524
Miao Wanga4e5adf2015-03-23 11:09:56 -07001525 RenderScript prs = internalCreate(ctx, sdkVersion, ct, flags);
Jason Samse16da122015-03-18 17:04:18 -07001526 prs.mIsProcessContext = true;
1527 mProcessContextList.add(prs);
1528 return prs;
1529 }
Jason Sams1a4e1f3e2012-02-24 17:51:24 -08001530 }
1531
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001532 /**
Jason Samse16da122015-03-18 17:04:18 -07001533 * Releases all the process contexts. This is the same as
1534 * calling .destroy() on each unique context retreived with
1535 * create(...). If no contexts have been created this
1536 * function does nothing.
1537 *
1538 * Typically you call this when your application is losing focus
1539 * and will not be using a context for some time.
1540 *
1541 * This has no effect on a context created with
1542 * createMultiContext()
1543 */
1544 public static void releaseAllContexts() {
1545 ArrayList<RenderScript> oldList;
1546 synchronized (mProcessContextList) {
1547 oldList = mProcessContextList;
1548 mProcessContextList = new ArrayList<RenderScript>();
1549 }
1550
1551 for (RenderScript prs : oldList) {
1552 prs.mIsProcessContext = false;
1553 prs.destroy();
1554 }
1555 oldList.clear();
1556 }
1557
1558
1559
1560 /**
1561 * Create a RenderScript context.
1562 *
1563 * This is an advanced function intended for applications which
1564 * need to create more than one RenderScript context to be used
1565 * at the same time.
1566 *
1567 * If you need a single context please use create()
1568 *
Jason Samse16da122015-03-18 17:04:18 -07001569 * @param ctx The context.
1570 * @return RenderScript
1571 */
1572 public static RenderScript createMultiContext(Context ctx, ContextType ct, int flags, int API_number) {
1573 return internalCreate(ctx, API_number, ct, flags);
1574 }
1575
1576
1577 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001578 * Print the currently available debugging information about the state of
1579 * the RS context to the log.
1580 *
Jason Sams27676fe2010-11-10 17:00:59 -08001581 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001582 public void contextDump() {
Jason Sams5dbfe932010-01-27 14:41:43 -08001583 validate();
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001584 nContextDump(0);
Jason Sams715333b2009-11-17 17:26:46 -08001585 }
1586
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001587 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001588 * Wait for any pending asynchronous opeations (such as copies to a RS
1589 * allocation or RS script executions) to complete.
Jason Sams27676fe2010-11-10 17:00:59 -08001590 *
1591 */
Jason Sams96ed4cf2010-06-15 12:15:57 -07001592 public void finish() {
1593 nContextFinish();
1594 }
1595
Jason Samsd22a6f02015-02-19 17:19:52 -08001596 private void helpDestroy() {
1597 boolean shouldDestroy = false;
1598 synchronized(this) {
1599 if (!mDestroyed) {
1600 shouldDestroy = true;
1601 mDestroyed = true;
1602 }
1603 }
1604
1605 if (shouldDestroy) {
1606 nContextFinish();
1607
1608 nContextDeinitToClient(mContext);
1609 mMessageThread.mRun = false;
Pirama Arumuga Nainar2f25ce772015-10-02 12:06:10 -07001610
1611 // Wait for mMessageThread to join. Try in a loop, in case this thread gets interrupted
Pirama Arumuga Nainar83461d72015-10-02 16:38:08 -07001612 // during the wait. If interrupted, set the "interrupted" status of the current thread.
1613 boolean hasJoined = false, interrupted = false;
Pirama Arumuga Nainar2f25ce772015-10-02 12:06:10 -07001614 while (!hasJoined) {
1615 try {
1616 mMessageThread.join();
1617 hasJoined = true;
Pirama Arumuga Nainar83461d72015-10-02 16:38:08 -07001618 } catch (InterruptedException e) {
1619 interrupted = true;
Pirama Arumuga Nainar2f25ce772015-10-02 12:06:10 -07001620 }
Jason Samsd22a6f02015-02-19 17:19:52 -08001621 }
Pirama Arumuga Nainar83461d72015-10-02 16:38:08 -07001622 if (interrupted) {
1623 Log.v(LOG_TAG, "Interrupted during wait for MessageThread to join");
1624 Thread.currentThread().interrupt();
1625 }
Jason Samsd22a6f02015-02-19 17:19:52 -08001626
1627 nContextDestroy();
1628
1629 nDeviceDestroy(mDev);
1630 mDev = 0;
1631 }
1632 }
1633
1634 protected void finalize() throws Throwable {
1635 helpDestroy();
1636 super.finalize();
1637 }
1638
1639
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001640 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001641 * Destroys this RenderScript context. Once this function is called,
1642 * using this context or any objects belonging to this context is
1643 * illegal.
Jason Sams27676fe2010-11-10 17:00:59 -08001644 *
Jason Samse16da122015-03-18 17:04:18 -07001645 * API 23+, this function is a NOP if the context was created
1646 * with create(). Please use releaseAllContexts() to clean up
1647 * contexts created with the create function.
1648 *
Jason Sams27676fe2010-11-10 17:00:59 -08001649 */
Jason Samsf5b45962009-08-25 14:49:07 -07001650 public void destroy() {
Jason Samse16da122015-03-18 17:04:18 -07001651 if (mIsProcessContext) {
1652 // users cannot destroy a process context
1653 return;
1654 }
Jason Sams5dbfe932010-01-27 14:41:43 -08001655 validate();
Jason Samsd22a6f02015-02-19 17:19:52 -08001656 helpDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -07001657 }
Jason Sams02fb2cb2009-05-28 15:37:57 -07001658
Jason Samsa9e7a052009-09-25 14:51:22 -07001659 boolean isAlive() {
1660 return mContext != 0;
1661 }
1662
Tim Murray460a0492013-11-19 12:45:54 -08001663 long safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -07001664 if(o != null) {
Jason Samse07694b2012-04-03 15:36:36 -07001665 return o.getID(this);
Jason Samsd8e41612009-08-20 17:22:40 -07001666 }
Jason Sams6b9dec02009-09-23 16:38:37 -07001667 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001668 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001669}