blob: 35fe8c4986488968c1d4eee546a3e2686d6d90a9 [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
Jason Samsa6f338c2012-02-24 16:22:16 -080019import java.io.File;
Jason Samsfe1d5ff2012-03-23 11:47:26 -070020import java.lang.reflect.Field;
Tim Murray2f2472c2013-08-22 14:55:26 -070021import java.lang.reflect.Method;
Jason Samsff7256e2014-03-10 13:31:51 -070022import java.util.concurrent.locks.ReentrantReadWriteLock;
Jason Sams36e612a2009-07-31 16:26:13 -070023
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080024import android.content.Context;
Stephen Hines4382467a2011-08-01 15:02:34 -070025import android.content.pm.ApplicationInfo;
26import android.content.pm.PackageManager;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080027import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070028import android.graphics.Bitmap;
Romain Guy650a3eb2009-08-31 14:06:43 -070029import android.graphics.BitmapFactory;
Jason Samsfaa32b32011-06-20 16:58:04 -070030import android.graphics.SurfaceTexture;
Glenn Kasten260c77a2011-06-01 17:25:54 -070031import android.os.Process;
Jason Sams36e612a2009-07-31 16:26:13 -070032import android.util.Log;
33import android.view.Surface;
Dan Morrille4d9a012013-03-28 18:10:43 -070034import android.os.SystemProperties;
Tim Murray6d7a53c2013-05-23 16:59:23 -070035import android.os.Trace;
Stephen Hines4382467a2011-08-01 15:02:34 -070036
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070037/**
Tim Murrayc11e25c2013-04-09 11:01:01 -070038 * This class provides access to a RenderScript context, which controls RenderScript
39 * initialization, resource management, and teardown. An instance of the RenderScript
40 * class must be created before any other RS objects can be created.
Jason Sams27676fe2010-11-10 17:00:59 -080041 *
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080042 * <div class="special reference">
43 * <h3>Developer Guides</h3>
Tim Murrayc11e25c2013-04-09 11:01:01 -070044 * <p>For more information about creating an application that uses RenderScript, read the
45 * <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080046 * </div>
Jason Samse29d4712009-07-23 15:19:03 -070047 **/
Jack Palevich60aa3ea2009-05-26 13:45:08 -070048public class RenderScript {
Tim Murray6d7a53c2013-05-23 16:59:23 -070049 static final long TRACE_TAG = Trace.TRACE_TAG_RS;
50
Jason Sams3bc47d42009-11-12 15:10:25 -080051 static final String LOG_TAG = "RenderScript_jni";
Jason Samsbf6ef8d72010-12-06 15:59:59 -080052 static final boolean DEBUG = false;
Romain Guy650a3eb2009-08-31 14:06:43 -070053 @SuppressWarnings({"UnusedDeclaration", "deprecation"})
Joe Onorato43a17652011-04-06 19:22:23 -070054 static final boolean LOG_ENABLED = false;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070055
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080056 private Context mApplicationContext;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070057
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080058 /*
Jack Palevich60aa3ea2009-05-26 13:45:08 -070059 * We use a class initializer to allow the native code to cache some
60 * field offsets.
61 */
Dan Morrille4d9a012013-03-28 18:10:43 -070062 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) // TODO: now used locally; remove?
Jason Samsbf6ef8d72010-12-06 15:59:59 -080063 static boolean sInitialized;
64 native static void _nInit();
Jack Palevich60aa3ea2009-05-26 13:45:08 -070065
Tim Murray2f2472c2013-08-22 14:55:26 -070066 static Object sRuntime;
67 static Method registerNativeAllocation;
68 static Method registerNativeFree;
Jason Samsdba3ba52009-07-30 14:56:12 -070069
Jack Palevich60aa3ea2009-05-26 13:45:08 -070070 static {
71 sInitialized = false;
Dan Morrille4d9a012013-03-28 18:10:43 -070072 if (!SystemProperties.getBoolean("config.disable_renderscript", false)) {
73 try {
Tim Murray2f2472c2013-08-22 14:55:26 -070074 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
75 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
76 sRuntime = get_runtime.invoke(null);
77 registerNativeAllocation = vm_runtime.getDeclaredMethod("registerNativeAllocation", Integer.TYPE);
78 registerNativeFree = vm_runtime.getDeclaredMethod("registerNativeFree", Integer.TYPE);
79 } catch (Exception e) {
80 Log.e(LOG_TAG, "Error loading GC methods: " + e);
81 throw new RSRuntimeException("Error loading GC methods: " + e);
82 }
83 try {
Dan Morrille4d9a012013-03-28 18:10:43 -070084 System.loadLibrary("rs_jni");
85 _nInit();
86 sInitialized = true;
87 } catch (UnsatisfiedLinkError e) {
88 Log.e(LOG_TAG, "Error loading RS jni library: " + e);
89 throw new RSRuntimeException("Error loading RS jni library: " + e);
90 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -070091 }
92 }
93
Jason Sams2e1872f2010-08-17 16:25:41 -070094 // Non-threadsafe functions.
Jason Sams36e612a2009-07-31 16:26:13 -070095 native int nDeviceCreate();
96 native void nDeviceDestroy(int dev);
Jason Samsebfb4362009-09-23 13:57:02 -070097 native void nDeviceSetConfig(int dev, int param, int value);
Jason Samsedbfabd2011-05-17 15:01:29 -070098 native int nContextGetUserMessage(int con, int[] data);
Jason Sams1c415172010-11-08 17:06:46 -080099 native String nContextGetErrorMessage(int con);
Jason Samsedbfabd2011-05-17 15:01:29 -0700100 native int nContextPeekMessage(int con, int[] subID);
Jason Sams2e1872f2010-08-17 16:25:41 -0700101 native void nContextInitToClient(int con);
102 native void nContextDeinitToClient(int con);
Jason Sams3eaa338e2009-06-10 15:04:38 -0700103
Stephen Hines7d25a822013-04-09 23:51:56 -0700104 static File mCacheDir;
Jason Samsa6f338c2012-02-24 16:22:16 -0800105
Jason Sams5757bf82014-03-10 13:32:21 -0700106 // this should be a monotonically increasing ID
107 // used in conjunction with the API version of a device
108 static final long sMinorID = 1;
109
110 /**
111 * Returns an identifier that can be used to identify a particular
112 * minor version of RS.
113 *
114 * @hide
115 */
116 public static long getMinorID() {
117 return sMinorID;
118 }
119
Jason Samsa6f338c2012-02-24 16:22:16 -0800120 /**
121 * Sets the directory to use as a persistent storage for the
122 * renderscript object file cache.
123 *
124 * @hide
125 * @param cacheDir A directory the current process can write to
126 */
Jason Samsa6f338c2012-02-24 16:22:16 -0800127 public static void setupDiskCache(File cacheDir) {
Dan Morrille4d9a012013-03-28 18:10:43 -0700128 if (!sInitialized) {
129 Log.e(LOG_TAG, "RenderScript.setupDiskCache() called when disabled");
130 return;
131 }
132
Stephen Hines7d25a822013-04-09 23:51:56 -0700133 // Defer creation of cache path to nScriptCCreate().
134 mCacheDir = cacheDir;
Jason Samsa6f338c2012-02-24 16:22:16 -0800135 }
136
Jason Sams02d56d92013-04-12 16:40:50 -0700137 /**
138 * ContextType specifies the specific type of context to be created.
139 *
140 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800141 public enum ContextType {
Jason Sams02d56d92013-04-12 16:40:50 -0700142 /**
143 * NORMAL context, this is the default and what shipping apps should
144 * use.
145 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800146 NORMAL (0),
Jason Sams02d56d92013-04-12 16:40:50 -0700147
148 /**
149 * DEBUG context, perform extra runtime checks to validate the
150 * kernels and APIs are being used as intended. Get and SetElementAt
151 * will be bounds checked in this mode.
152 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800153 DEBUG (1),
Jason Sams02d56d92013-04-12 16:40:50 -0700154
155 /**
156 * PROFILE context, Intended to be used once the first time an
157 * application is run on a new device. This mode allows the runtime to
158 * do additional testing and performance tuning.
159 */
Jason Samsadd26dc2013-02-22 18:43:45 -0800160 PROFILE (2);
161
162 int mID;
163 ContextType(int id) {
164 mID = id;
165 }
166 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800167
Stephen Hines42028a82013-04-17 19:22:01 -0700168 ContextType mContextType;
Jason Samsff7256e2014-03-10 13:31:51 -0700169 ReentrantReadWriteLock mRWLock;
Stephen Hines42028a82013-04-17 19:22:01 -0700170
Jason Sams2e1872f2010-08-17 16:25:41 -0700171 // Methods below are wrapped to protect the non-threadsafe
172 // lockless fifo.
Stephen Hines4382467a2011-08-01 15:02:34 -0700173 native int rsnContextCreateGL(int dev, int ver, int sdkVer,
Jason Sams11c8af92010-10-13 15:31:10 -0700174 int colorMin, int colorPref,
175 int alphaMin, int alphaPref,
176 int depthMin, int depthPref,
177 int stencilMin, int stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700178 int samplesMin, int samplesPref, float samplesQ, int dpi);
Stephen Hines4382467a2011-08-01 15:02:34 -0700179 synchronized int nContextCreateGL(int dev, int ver, int sdkVer,
Jason Sams11c8af92010-10-13 15:31:10 -0700180 int colorMin, int colorPref,
181 int alphaMin, int alphaPref,
182 int depthMin, int depthPref,
183 int stencilMin, int stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700184 int samplesMin, int samplesPref, float samplesQ, int dpi) {
Stephen Hines4382467a2011-08-01 15:02:34 -0700185 return rsnContextCreateGL(dev, ver, sdkVer, colorMin, colorPref,
Jason Sams11c8af92010-10-13 15:31:10 -0700186 alphaMin, alphaPref, depthMin, depthPref,
187 stencilMin, stencilPref,
Alex Sakhartchouk2c74ad92011-03-16 19:28:25 -0700188 samplesMin, samplesPref, samplesQ, dpi);
Jason Sams2e1872f2010-08-17 16:25:41 -0700189 }
Jason Samsadd26dc2013-02-22 18:43:45 -0800190 native int rsnContextCreate(int dev, int ver, int sdkVer, int contextType);
191 synchronized int nContextCreate(int dev, int ver, int sdkVer, int contextType) {
192 return rsnContextCreate(dev, ver, sdkVer, contextType);
Jason Sams2e1872f2010-08-17 16:25:41 -0700193 }
194 native void rsnContextDestroy(int con);
195 synchronized void nContextDestroy() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800196 validate();
Jason Samsff7256e2014-03-10 13:31:51 -0700197
198 // take teardown lock
199 // teardown lock can only be taken when no objects are being destroyed
200 ReentrantReadWriteLock.WriteLock wlock = mRWLock.writeLock();
201 wlock.lock();
202
203 int curCon = mContext;
204 // context is considered dead as of this point
205 mContext = 0;
206
207 wlock.unlock();
208 rsnContextDestroy(curCon);
Jason Sams2e1872f2010-08-17 16:25:41 -0700209 }
210 native void rsnContextSetSurface(int con, int w, int h, Surface sur);
211 synchronized void nContextSetSurface(int w, int h, Surface sur) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800212 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700213 rsnContextSetSurface(mContext, w, h, sur);
214 }
Jason Samsfaa32b32011-06-20 16:58:04 -0700215 native void rsnContextSetSurfaceTexture(int con, int w, int h, SurfaceTexture sur);
216 synchronized void nContextSetSurfaceTexture(int w, int h, SurfaceTexture sur) {
217 validate();
218 rsnContextSetSurfaceTexture(mContext, w, h, sur);
219 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700220 native void rsnContextSetPriority(int con, int p);
221 synchronized void nContextSetPriority(int p) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800222 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700223 rsnContextSetPriority(mContext, p);
224 }
225 native void rsnContextDump(int con, int bits);
226 synchronized void nContextDump(int bits) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800227 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700228 rsnContextDump(mContext, bits);
229 }
230 native void rsnContextFinish(int con);
231 synchronized void nContextFinish() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800232 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700233 rsnContextFinish(mContext);
234 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700235
Jason Sams455d6442013-02-05 19:20:18 -0800236 native void rsnContextSendMessage(int con, int id, int[] data);
237 synchronized void nContextSendMessage(int id, int[] data) {
238 validate();
239 rsnContextSendMessage(mContext, id, data);
240 }
241
Jason Sams2e1872f2010-08-17 16:25:41 -0700242 native void rsnContextBindRootScript(int con, int script);
243 synchronized void nContextBindRootScript(int script) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800244 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700245 rsnContextBindRootScript(mContext, script);
246 }
247 native void rsnContextBindSampler(int con, int sampler, int slot);
248 synchronized void nContextBindSampler(int sampler, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800249 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700250 rsnContextBindSampler(mContext, sampler, slot);
251 }
252 native void rsnContextBindProgramStore(int con, int pfs);
253 synchronized void nContextBindProgramStore(int pfs) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800254 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700255 rsnContextBindProgramStore(mContext, pfs);
256 }
257 native void rsnContextBindProgramFragment(int con, int pf);
258 synchronized void nContextBindProgramFragment(int pf) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800259 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700260 rsnContextBindProgramFragment(mContext, pf);
261 }
262 native void rsnContextBindProgramVertex(int con, int pv);
263 synchronized void nContextBindProgramVertex(int pv) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800264 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700265 rsnContextBindProgramVertex(mContext, pv);
266 }
267 native void rsnContextBindProgramRaster(int con, int pr);
268 synchronized void nContextBindProgramRaster(int pr) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800269 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700270 rsnContextBindProgramRaster(mContext, pr);
271 }
272 native void rsnContextPause(int con);
273 synchronized void nContextPause() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800274 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700275 rsnContextPause(mContext);
276 }
277 native void rsnContextResume(int con);
278 synchronized void nContextResume() {
Jason Samsd1ac9812011-01-18 18:12:26 -0800279 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700280 rsnContextResume(mContext);
281 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700282
Jason Sams2e1872f2010-08-17 16:25:41 -0700283 native void rsnAssignName(int con, int obj, byte[] name);
284 synchronized void nAssignName(int obj, byte[] name) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800285 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700286 rsnAssignName(mContext, obj, name);
287 }
288 native String rsnGetName(int con, int obj);
289 synchronized String nGetName(int obj) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800290 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700291 return rsnGetName(mContext, obj);
292 }
Jason Samsff7256e2014-03-10 13:31:51 -0700293 // nObjDestroy is explicitly _not_ synchronous to prevent crashes in finalizers
Jason Sams2e1872f2010-08-17 16:25:41 -0700294 native void rsnObjDestroy(int con, int id);
Jason Samsff7256e2014-03-10 13:31:51 -0700295 void nObjDestroy(int id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800296 // There is a race condition here. The calling code may be run
297 // by the gc while teardown is occuring. This protects againts
298 // deleting dead objects.
299 if (mContext != 0) {
300 rsnObjDestroy(mContext, id);
301 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700302 }
Jason Samsfe08d992009-05-27 14:45:32 -0700303
Jason Sams2e1872f2010-08-17 16:25:41 -0700304 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
305 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800306 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700307 return rsnElementCreate(mContext, type, kind, norm, vecSize);
308 }
Jason Sams70d4e502010-09-02 17:35:23 -0700309 native int rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
310 synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800311 validate();
Jason Sams70d4e502010-09-02 17:35:23 -0700312 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700313 }
314 native void rsnElementGetNativeData(int con, int id, int[] elementData);
315 synchronized void nElementGetNativeData(int id, int[] elementData) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800316 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700317 rsnElementGetNativeData(mContext, id, elementData);
318 }
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700319 native void rsnElementGetSubElements(int con, int id,
320 int[] IDs, String[] names, int[] arraySizes);
321 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names, int[] arraySizes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800322 validate();
Alex Sakhartchouk7d5f5e72011-10-18 11:08:31 -0700323 rsnElementGetSubElements(mContext, id, IDs, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700324 }
Jason Sams768bc022009-09-21 19:41:04 -0700325
Jason Samsb109cc72013-01-07 18:20:12 -0800326 native int rsnTypeCreate(int con, int eid, int x, int y, int z, boolean mips, boolean faces, int yuv);
327 synchronized int nTypeCreate(int eid, int x, int y, int z, boolean mips, boolean faces, int yuv) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800328 validate();
Jason Samsb109cc72013-01-07 18:20:12 -0800329 return rsnTypeCreate(mContext, eid, x, y, z, mips, faces, yuv);
Jason Sams2e1872f2010-08-17 16:25:41 -0700330 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700331 native void rsnTypeGetNativeData(int con, int id, int[] typeData);
332 synchronized void nTypeGetNativeData(int id, int[] typeData) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800333 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700334 rsnTypeGetNativeData(mContext, id, typeData);
335 }
Jason Sams768bc022009-09-21 19:41:04 -0700336
Jason Sams857d0c72011-11-23 15:02:15 -0800337 native int rsnAllocationCreateTyped(int con, int type, int mip, int usage, int pointer);
338 synchronized int nAllocationCreateTyped(int type, int mip, int usage, int pointer) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800339 validate();
Jason Sams857d0c72011-11-23 15:02:15 -0800340 return rsnAllocationCreateTyped(mContext, type, mip, usage, pointer);
Jason Sams2e1872f2010-08-17 16:25:41 -0700341 }
Jason Sams5476b452010-12-08 16:14:36 -0800342 native int rsnAllocationCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
343 synchronized int nAllocationCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800344 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800345 return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700346 }
Tim Murraya3145512012-12-04 17:59:29 -0800347
348 native int rsnAllocationCreateBitmapBackedAllocation(int con, int type, int mip, Bitmap bmp, int usage);
349 synchronized int nAllocationCreateBitmapBackedAllocation(int type, int mip, Bitmap bmp, int usage) {
350 validate();
351 return rsnAllocationCreateBitmapBackedAllocation(mContext, type, mip, bmp, usage);
352 }
353
354
Jason Sams5476b452010-12-08 16:14:36 -0800355 native int rsnAllocationCubeCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
356 synchronized int nAllocationCubeCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800357 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800358 return rsnAllocationCubeCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800359 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700360 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
361 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800362 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700363 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
364 }
Jason Sams5476b452010-12-08 16:14:36 -0800365 native int rsnAllocationCreateFromAssetStream(int con, int mips, int assetStream, int usage);
366 synchronized int nAllocationCreateFromAssetStream(int mips, int assetStream, int usage) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800367 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800368 return rsnAllocationCreateFromAssetStream(mContext, mips, assetStream, usage);
369 }
370
Jason Sams4ef66502010-12-10 16:03:15 -0800371 native void rsnAllocationCopyToBitmap(int con, int alloc, Bitmap bmp);
372 synchronized void nAllocationCopyToBitmap(int alloc, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800373 validate();
Jason Sams4ef66502010-12-10 16:03:15 -0800374 rsnAllocationCopyToBitmap(mContext, alloc, bmp);
375 }
376
377
Jason Sams5476b452010-12-08 16:14:36 -0800378 native void rsnAllocationSyncAll(int con, int alloc, int src);
379 synchronized void nAllocationSyncAll(int alloc, int src) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800380 validate();
Jason Sams5476b452010-12-08 16:14:36 -0800381 rsnAllocationSyncAll(mContext, alloc, src);
382 }
Jason Sams72226e02013-02-22 12:45:54 -0800383 native Surface rsnAllocationGetSurface(int con, int alloc);
384 synchronized Surface nAllocationGetSurface(int alloc) {
Jason Sams615e7ce2012-01-13 14:01:20 -0800385 validate();
Jason Sams72226e02013-02-22 12:45:54 -0800386 return rsnAllocationGetSurface(mContext, alloc);
Jason Samsfe1d5ff2012-03-23 11:47:26 -0700387 }
Jason Samsfb9aa9f2012-03-28 15:30:07 -0700388 native void rsnAllocationSetSurface(int con, int alloc, Surface sur);
389 synchronized void nAllocationSetSurface(int alloc, Surface sur) {
Jason Sams163766c2012-02-15 12:04:24 -0800390 validate();
Jason Samsfb9aa9f2012-03-28 15:30:07 -0700391 rsnAllocationSetSurface(mContext, alloc, sur);
Jason Sams163766c2012-02-15 12:04:24 -0800392 }
393 native void rsnAllocationIoSend(int con, int alloc);
394 synchronized void nAllocationIoSend(int alloc) {
395 validate();
396 rsnAllocationIoSend(mContext, alloc);
397 }
398 native void rsnAllocationIoReceive(int con, int alloc);
399 synchronized void nAllocationIoReceive(int alloc) {
400 validate();
401 rsnAllocationIoReceive(mContext, alloc);
402 }
403
Jason Sams615e7ce2012-01-13 14:01:20 -0800404
Jason Samsf7086092011-01-12 13:28:37 -0800405 native void rsnAllocationGenerateMipmaps(int con, int alloc);
406 synchronized void nAllocationGenerateMipmaps(int alloc) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800407 validate();
Jason Samsf7086092011-01-12 13:28:37 -0800408 rsnAllocationGenerateMipmaps(mContext, alloc);
409 }
Jason Sams4ef66502010-12-10 16:03:15 -0800410 native void rsnAllocationCopyFromBitmap(int con, int alloc, Bitmap bmp);
411 synchronized void nAllocationCopyFromBitmap(int alloc, Bitmap bmp) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800412 validate();
Jason Sams4ef66502010-12-10 16:03:15 -0800413 rsnAllocationCopyFromBitmap(mContext, alloc, bmp);
Jason Sams2e1872f2010-08-17 16:25:41 -0700414 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700415
Jason Sams49a05d72010-12-29 14:31:29 -0800416
417 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, int[] d, int sizeBytes);
418 synchronized void nAllocationData1D(int id, int off, int mip, int count, int[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800419 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800420 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700421 }
Jason Sams49a05d72010-12-29 14:31:29 -0800422 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, short[] d, int sizeBytes);
423 synchronized void nAllocationData1D(int id, int off, int mip, int count, short[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800424 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800425 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
426 }
427 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, byte[] d, int sizeBytes);
428 synchronized void nAllocationData1D(int id, int off, int mip, int count, byte[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800429 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800430 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
431 }
432 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, float[] d, int sizeBytes);
433 synchronized void nAllocationData1D(int id, int off, int mip, int count, float[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800434 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800435 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700436 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700437
Jason Sams49a05d72010-12-29 14:31:29 -0800438 native void rsnAllocationElementData1D(int con, int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes);
439 synchronized void nAllocationElementData1D(int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800440 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800441 rsnAllocationElementData1D(mContext, id, xoff, mip, compIdx, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700442 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700443
Alex Sakhartchouk304b1f52011-06-14 11:13:19 -0700444 native void rsnAllocationData2D(int con,
445 int dstAlloc, int dstXoff, int dstYoff,
446 int dstMip, int dstFace,
447 int width, int height,
448 int srcAlloc, int srcXoff, int srcYoff,
449 int srcMip, int srcFace);
450 synchronized void nAllocationData2D(int dstAlloc, int dstXoff, int dstYoff,
451 int dstMip, int dstFace,
452 int width, int height,
453 int srcAlloc, int srcXoff, int srcYoff,
454 int srcMip, int srcFace) {
455 validate();
456 rsnAllocationData2D(mContext,
457 dstAlloc, dstXoff, dstYoff,
458 dstMip, dstFace,
459 width, height,
460 srcAlloc, srcXoff, srcYoff,
461 srcMip, srcFace);
462 }
463
Jason Samsfa445b92011-01-07 17:00:07 -0800464 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, byte[] d, int sizeBytes);
465 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, byte[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800466 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800467 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
468 }
469 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, short[] d, int sizeBytes);
470 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, short[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800471 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800472 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
473 }
Jason Sams49a05d72010-12-29 14:31:29 -0800474 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, int[] d, int sizeBytes);
475 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, int[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800476 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800477 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700478 }
Jason Sams49a05d72010-12-29 14:31:29 -0800479 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, float[] d, int sizeBytes);
480 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, float[] d, int sizeBytes) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800481 validate();
Jason Sams49a05d72010-12-29 14:31:29 -0800482 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700483 }
Jason Samsfa445b92011-01-07 17:00:07 -0800484 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, Bitmap b);
485 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, Bitmap b) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800486 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800487 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, b);
488 }
Jason Sams49a05d72010-12-29 14:31:29 -0800489
Jason Samsb05d6892013-04-09 15:59:24 -0700490 native void rsnAllocationData3D(int con,
491 int dstAlloc, int dstXoff, int dstYoff, int dstZoff,
492 int dstMip,
493 int width, int height, int depth,
494 int srcAlloc, int srcXoff, int srcYoff, int srcZoff,
495 int srcMip);
496 synchronized void nAllocationData3D(int dstAlloc, int dstXoff, int dstYoff, int dstZoff,
497 int dstMip,
498 int width, int height, int depth,
499 int srcAlloc, int srcXoff, int srcYoff, int srcZoff,
500 int srcMip) {
501 validate();
502 rsnAllocationData3D(mContext,
503 dstAlloc, dstXoff, dstYoff, dstZoff,
504 dstMip, width, height, depth,
505 srcAlloc, srcXoff, srcYoff, srcZoff, srcMip);
506 }
507
508 native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, byte[] d, int sizeBytes);
509 synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, byte[] d, int sizeBytes) {
510 validate();
511 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
512 }
513 native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, short[] d, int sizeBytes);
514 synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, short[] d, int sizeBytes) {
515 validate();
516 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
517 }
518 native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, int[] d, int sizeBytes);
519 synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, int[] d, int sizeBytes) {
520 validate();
521 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
522 }
523 native void rsnAllocationData3D(int con, int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, float[] d, int sizeBytes);
524 synchronized void nAllocationData3D(int id, int xoff, int yoff, int zoff, int mip, int w, int h, int depth, float[] d, int sizeBytes) {
525 validate();
526 rsnAllocationData3D(mContext, id, xoff, yoff, zoff, mip, w, h, depth, d, sizeBytes);
527 }
528
529
Jason Samsfa445b92011-01-07 17:00:07 -0800530 native void rsnAllocationRead(int con, int id, byte[] d);
531 synchronized void nAllocationRead(int id, byte[] d) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800532 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800533 rsnAllocationRead(mContext, id, d);
534 }
535 native void rsnAllocationRead(int con, int id, short[] d);
536 synchronized void nAllocationRead(int id, short[] d) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800537 validate();
Jason Samsfa445b92011-01-07 17:00:07 -0800538 rsnAllocationRead(mContext, id, d);
539 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700540 native void rsnAllocationRead(int con, int id, int[] d);
541 synchronized void nAllocationRead(int id, int[] d) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800542 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700543 rsnAllocationRead(mContext, id, d);
544 }
545 native void rsnAllocationRead(int con, int id, float[] d);
546 synchronized void nAllocationRead(int id, float[] d) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800547 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700548 rsnAllocationRead(mContext, id, d);
549 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700550 native int rsnAllocationGetType(int con, int id);
551 synchronized int nAllocationGetType(int id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800552 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700553 return rsnAllocationGetType(mContext, id);
554 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700555
Jason Sams5edc6082010-10-05 13:32:49 -0700556 native void rsnAllocationResize1D(int con, int id, int dimX);
557 synchronized void nAllocationResize1D(int id, int dimX) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800558 validate();
Jason Sams5edc6082010-10-05 13:32:49 -0700559 rsnAllocationResize1D(mContext, id, dimX);
560 }
Jason Sams5edc6082010-10-05 13:32:49 -0700561
Jason Sams2e1872f2010-08-17 16:25:41 -0700562 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
563 synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800564 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700565 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
566 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800567 native int rsnFileA3DCreateFromFile(int con, String path);
568 synchronized int nFileA3DCreateFromFile(String path) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800569 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800570 return rsnFileA3DCreateFromFile(mContext, path);
571 }
572 native int rsnFileA3DCreateFromAsset(int con, AssetManager mgr, String path);
573 synchronized int nFileA3DCreateFromAsset(AssetManager mgr, String path) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800574 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800575 return rsnFileA3DCreateFromAsset(mContext, mgr, path);
576 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700577 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
578 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800579 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700580 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
581 }
582 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
583 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800584 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700585 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
586 }
587 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
588 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800589 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700590 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
591 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700592
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800593 native int rsnFontCreateFromFile(int con, String fileName, float size, int dpi);
594 synchronized int nFontCreateFromFile(String fileName, float size, int dpi) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800595 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700596 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
597 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800598 native int rsnFontCreateFromAssetStream(int con, String name, float size, int dpi, int assetStream);
599 synchronized int nFontCreateFromAssetStream(String name, float size, int dpi, int assetStream) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800600 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800601 return rsnFontCreateFromAssetStream(mContext, name, size, dpi, assetStream);
602 }
603 native int rsnFontCreateFromAsset(int con, AssetManager mgr, String path, float size, int dpi);
604 synchronized int nFontCreateFromAsset(AssetManager mgr, String path, float size, int dpi) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800605 validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800606 return rsnFontCreateFromAsset(mContext, mgr, path, size, dpi);
607 }
Jason Sams22534172009-08-04 16:58:20 -0700608
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700609
Jason Sams2e1872f2010-08-17 16:25:41 -0700610 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
611 synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800612 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700613 rsnScriptBindAllocation(mContext, script, alloc, slot);
614 }
615 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
616 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800617 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700618 rsnScriptSetTimeZone(mContext, script, timeZone);
619 }
620 native void rsnScriptInvoke(int con, int id, int slot);
621 synchronized void nScriptInvoke(int id, int slot) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800622 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700623 rsnScriptInvoke(mContext, id, slot);
624 }
Jason Sams6e494d32011-04-27 16:33:11 -0700625 native void rsnScriptForEach(int con, int id, int slot, int ain, int aout, byte[] params);
626 native void rsnScriptForEach(int con, int id, int slot, int ain, int aout);
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800627 native void rsnScriptForEachClipped(int con, int id, int slot, int ain, int aout, byte[] params,
628 int xstart, int xend, int ystart, int yend, int zstart, int zend);
Stephen Hinesdac6ed02013-02-13 00:09:02 -0800629 native void rsnScriptForEachClipped(int con, int id, int slot, int ain, int aout,
630 int xstart, int xend, int ystart, int yend, int zstart, int zend);
Jason Sams6e494d32011-04-27 16:33:11 -0700631 synchronized void nScriptForEach(int id, int slot, int ain, int aout, byte[] params) {
632 validate();
633 if (params == null) {
634 rsnScriptForEach(mContext, id, slot, ain, aout);
635 } else {
636 rsnScriptForEach(mContext, id, slot, ain, aout, params);
637 }
638 }
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800639
640 synchronized void nScriptForEachClipped(int id, int slot, int ain, int aout, byte[] params,
641 int xstart, int xend, int ystart, int yend, int zstart, int zend) {
642 validate();
Stephen Hinesdac6ed02013-02-13 00:09:02 -0800643 if (params == null) {
644 rsnScriptForEachClipped(mContext, id, slot, ain, aout, xstart, xend, ystart, yend, zstart, zend);
645 } else {
646 rsnScriptForEachClipped(mContext, id, slot, ain, aout, params, xstart, xend, ystart, yend, zstart, zend);
647 }
Tim Murrayeb8c29c2013-02-07 12:16:41 -0800648 }
649
Jason Sams2e1872f2010-08-17 16:25:41 -0700650 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
651 synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800652 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700653 rsnScriptInvokeV(mContext, id, slot, params);
654 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700655
Jason Sams2e1872f2010-08-17 16:25:41 -0700656 native void rsnScriptSetVarI(int con, int id, int slot, int val);
657 synchronized void nScriptSetVarI(int id, int slot, int val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800658 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700659 rsnScriptSetVarI(mContext, id, slot, val);
660 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700661 native int rsnScriptGetVarI(int con, int id, int slot);
662 synchronized int nScriptGetVarI(int id, int slot) {
663 validate();
664 return rsnScriptGetVarI(mContext, id, slot);
665 }
666
Stephen Hines031ec58c2010-10-11 10:54:21 -0700667 native void rsnScriptSetVarJ(int con, int id, int slot, long val);
668 synchronized void nScriptSetVarJ(int id, int slot, long val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800669 validate();
Stephen Hines031ec58c2010-10-11 10:54:21 -0700670 rsnScriptSetVarJ(mContext, id, slot, val);
671 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700672 native long rsnScriptGetVarJ(int con, int id, int slot);
673 synchronized long nScriptGetVarJ(int id, int slot) {
674 validate();
675 return rsnScriptGetVarJ(mContext, id, slot);
676 }
677
Jason Sams2e1872f2010-08-17 16:25:41 -0700678 native void rsnScriptSetVarF(int con, int id, int slot, float val);
679 synchronized void nScriptSetVarF(int id, int slot, float val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800680 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700681 rsnScriptSetVarF(mContext, id, slot, val);
682 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700683 native float rsnScriptGetVarF(int con, int id, int slot);
684 synchronized float nScriptGetVarF(int id, int slot) {
685 validate();
686 return rsnScriptGetVarF(mContext, id, slot);
687 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700688 native void rsnScriptSetVarD(int con, int id, int slot, double val);
689 synchronized void nScriptSetVarD(int id, int slot, double val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800690 validate();
Stephen Hinesca54ec32010-09-20 17:20:30 -0700691 rsnScriptSetVarD(mContext, id, slot, val);
692 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700693 native double rsnScriptGetVarD(int con, int id, int slot);
694 synchronized double nScriptGetVarD(int id, int slot) {
695 validate();
696 return rsnScriptGetVarD(mContext, id, slot);
697 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700698 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
699 synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800700 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700701 rsnScriptSetVarV(mContext, id, slot, val);
702 }
Tim Murray7c4caad2013-04-10 16:21:40 -0700703 native void rsnScriptGetVarV(int con, int id, int slot, byte[] val);
704 synchronized void nScriptGetVarV(int id, int slot, byte[] val) {
705 validate();
706 rsnScriptGetVarV(mContext, id, slot, val);
707 }
Stephen Hinesadeb8092012-04-20 14:26:06 -0700708 native void rsnScriptSetVarVE(int con, int id, int slot, byte[] val,
709 int e, int[] dims);
710 synchronized void nScriptSetVarVE(int id, int slot, byte[] val,
711 int e, int[] dims) {
712 validate();
713 rsnScriptSetVarVE(mContext, id, slot, val, e, dims);
714 }
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800715 native void rsnScriptSetVarObj(int con, int id, int slot, int val);
716 synchronized void nScriptSetVarObj(int id, int slot, int val) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800717 validate();
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800718 rsnScriptSetVarObj(mContext, id, slot, val);
719 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700720
Jason Samse4a06c52011-03-16 16:29:28 -0700721 native int rsnScriptCCreate(int con, String resName, String cacheDir,
722 byte[] script, int length);
723 synchronized int nScriptCCreate(String resName, String cacheDir, byte[] script, int length) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800724 validate();
Jason Samse4a06c52011-03-16 16:29:28 -0700725 return rsnScriptCCreate(mContext, resName, cacheDir, script, length);
Jason Sams2e1872f2010-08-17 16:25:41 -0700726 }
Jason Samsebfb4362009-09-23 13:57:02 -0700727
Jason Sams6ab97682012-08-10 12:09:43 -0700728 native int rsnScriptIntrinsicCreate(int con, int id, int eid);
729 synchronized int nScriptIntrinsicCreate(int id, int eid) {
730 validate();
731 return rsnScriptIntrinsicCreate(mContext, id, eid);
732 }
733
Jason Sams08a81582012-09-18 12:32:10 -0700734 native int rsnScriptKernelIDCreate(int con, int sid, int slot, int sig);
735 synchronized int nScriptKernelIDCreate(int sid, int slot, int sig) {
736 validate();
737 return rsnScriptKernelIDCreate(mContext, sid, slot, sig);
738 }
739
740 native int rsnScriptFieldIDCreate(int con, int sid, int slot);
741 synchronized int nScriptFieldIDCreate(int sid, int slot) {
742 validate();
743 return rsnScriptFieldIDCreate(mContext, sid, slot);
744 }
745
746 native int rsnScriptGroupCreate(int con, int[] kernels, int[] src, int[] dstk, int[] dstf, int[] types);
747 synchronized int nScriptGroupCreate(int[] kernels, int[] src, int[] dstk, int[] dstf, int[] types) {
748 validate();
749 return rsnScriptGroupCreate(mContext, kernels, src, dstk, dstf, types);
750 }
751
752 native void rsnScriptGroupSetInput(int con, int group, int kernel, int alloc);
753 synchronized void nScriptGroupSetInput(int group, int kernel, int alloc) {
754 validate();
755 rsnScriptGroupSetInput(mContext, group, kernel, alloc);
756 }
757
758 native void rsnScriptGroupSetOutput(int con, int group, int kernel, int alloc);
759 synchronized void nScriptGroupSetOutput(int group, int kernel, int alloc) {
760 validate();
761 rsnScriptGroupSetOutput(mContext, group, kernel, alloc);
762 }
763
764 native void rsnScriptGroupExecute(int con, int group);
765 synchronized void nScriptGroupExecute(int group) {
766 validate();
767 rsnScriptGroupExecute(mContext, group);
768 }
769
Alex Sakhartchouka89094a2011-05-04 17:45:36 -0700770 native int rsnSamplerCreate(int con, int magFilter, int minFilter,
771 int wrapS, int wrapT, int wrapR, float aniso);
772 synchronized int nSamplerCreate(int magFilter, int minFilter,
773 int wrapS, int wrapT, int wrapR, float aniso) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800774 validate();
Alex Sakhartchouka89094a2011-05-04 17:45:36 -0700775 return rsnSamplerCreate(mContext, magFilter, minFilter, wrapS, wrapT, wrapR, aniso);
Jason Sams2e1872f2010-08-17 16:25:41 -0700776 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800777
Jason Sams331bf9b2011-04-06 11:23:54 -0700778 native int rsnProgramStoreCreate(int con, boolean r, boolean g, boolean b, boolean a,
779 boolean depthMask, boolean dither,
780 int srcMode, int dstMode, int depthFunc);
781 synchronized int nProgramStoreCreate(boolean r, boolean g, boolean b, boolean a,
782 boolean depthMask, boolean dither,
783 int srcMode, int dstMode, int depthFunc) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800784 validate();
Jason Samsbd184c52011-04-06 11:44:47 -0700785 return rsnProgramStoreCreate(mContext, r, g, b, a, depthMask, dither, srcMode,
786 dstMode, depthFunc);
Jason Sams2e1872f2010-08-17 16:25:41 -0700787 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700788
Jason Sams94aaed32011-09-23 14:18:53 -0700789 native int rsnProgramRasterCreate(int con, boolean pointSprite, int cullMode);
790 synchronized int nProgramRasterCreate(boolean pointSprite, int cullMode) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800791 validate();
Jason Sams94aaed32011-09-23 14:18:53 -0700792 return rsnProgramRasterCreate(mContext, pointSprite, cullMode);
Jason Sams2e1872f2010-08-17 16:25:41 -0700793 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700794
Jason Sams2e1872f2010-08-17 16:25:41 -0700795 native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
796 synchronized void nProgramBindConstants(int pv, int slot, int mID) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800797 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700798 rsnProgramBindConstants(mContext, pv, slot, mID);
799 }
800 native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
801 synchronized void nProgramBindTexture(int vpf, int slot, int a) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800802 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700803 rsnProgramBindTexture(mContext, vpf, slot, a);
804 }
805 native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
806 synchronized void nProgramBindSampler(int vpf, int slot, int s) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800807 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700808 rsnProgramBindSampler(mContext, vpf, slot, s);
809 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800810 native int rsnProgramFragmentCreate(int con, String shader, String[] texNames, int[] params);
811 synchronized int nProgramFragmentCreate(String shader, String[] texNames, int[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800812 validate();
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800813 return rsnProgramFragmentCreate(mContext, shader, texNames, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700814 }
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800815 native int rsnProgramVertexCreate(int con, String shader, String[] texNames, int[] params);
816 synchronized int nProgramVertexCreate(String shader, String[] texNames, int[] params) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800817 validate();
Alex Sakhartchouk2123b462012-02-15 16:21:46 -0800818 return rsnProgramVertexCreate(mContext, shader, texNames, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700819 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700820
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700821 native int rsnMeshCreate(int con, int[] vtx, int[] idx, int[] prim);
822 synchronized int nMeshCreate(int[] vtx, int[] idx, int[] prim) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800823 validate();
Alex Sakhartchouk25999a02011-05-12 10:38:03 -0700824 return rsnMeshCreate(mContext, vtx, idx, prim);
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800825 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700826 native int rsnMeshGetVertexBufferCount(int con, int id);
827 synchronized int nMeshGetVertexBufferCount(int id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800828 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700829 return rsnMeshGetVertexBufferCount(mContext, id);
830 }
831 native int rsnMeshGetIndexCount(int con, int id);
832 synchronized int nMeshGetIndexCount(int id) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800833 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700834 return rsnMeshGetIndexCount(mContext, id);
835 }
836 native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
837 synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800838 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700839 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
840 }
841 native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
842 synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
Jason Samsd1ac9812011-01-18 18:12:26 -0800843 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700844 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
845 }
846
Jason Samsf15ed012011-10-31 13:23:43 -0700847 native int rsnPathCreate(int con, int prim, boolean isStatic, int vtx, int loop, float q);
848 synchronized int nPathCreate(int prim, boolean isStatic, int vtx, int loop, float q) {
849 validate();
850 return rsnPathCreate(mContext, prim, isStatic, vtx, loop, q);
851 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700852
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800853 int mDev;
854 int mContext;
Romain Guy650a3eb2009-08-31 14:06:43 -0700855 @SuppressWarnings({"FieldCanBeLocal"})
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800856 MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700857
Jason Sams8cb39de2010-06-01 15:47:01 -0700858 Element mElement_U8;
859 Element mElement_I8;
860 Element mElement_U16;
861 Element mElement_I16;
862 Element mElement_U32;
863 Element mElement_I32;
Stephen Hines52d83632010-10-11 16:10:42 -0700864 Element mElement_U64;
Stephen Hinesef1dac22010-10-01 15:39:33 -0700865 Element mElement_I64;
Jason Sams8cb39de2010-06-01 15:47:01 -0700866 Element mElement_F32;
Stephen Hines02f417052010-09-30 15:19:22 -0700867 Element mElement_F64;
Jason Samsf110d4b2010-06-21 17:42:41 -0700868 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -0700869
Jason Sams8cb39de2010-06-01 15:47:01 -0700870 Element mElement_ELEMENT;
871 Element mElement_TYPE;
872 Element mElement_ALLOCATION;
873 Element mElement_SAMPLER;
874 Element mElement_SCRIPT;
875 Element mElement_MESH;
876 Element mElement_PROGRAM_FRAGMENT;
877 Element mElement_PROGRAM_VERTEX;
878 Element mElement_PROGRAM_RASTER;
879 Element mElement_PROGRAM_STORE;
Stephen Hines3a291412012-04-11 17:27:29 -0700880 Element mElement_FONT;
Jason Samsa70f4162010-03-26 15:33:42 -0700881
Jason Sams3c0dfba2009-09-27 17:50:38 -0700882 Element mElement_A_8;
883 Element mElement_RGB_565;
884 Element mElement_RGB_888;
885 Element mElement_RGBA_5551;
886 Element mElement_RGBA_4444;
887 Element mElement_RGBA_8888;
888
Jason Sams8cb39de2010-06-01 15:47:01 -0700889 Element mElement_FLOAT_2;
890 Element mElement_FLOAT_3;
891 Element mElement_FLOAT_4;
Stephen Hines836c4a52011-06-01 14:38:10 -0700892
893 Element mElement_DOUBLE_2;
894 Element mElement_DOUBLE_3;
895 Element mElement_DOUBLE_4;
896
897 Element mElement_UCHAR_2;
898 Element mElement_UCHAR_3;
Jason Sams8cb39de2010-06-01 15:47:01 -0700899 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -0800900
Stephen Hines836c4a52011-06-01 14:38:10 -0700901 Element mElement_CHAR_2;
902 Element mElement_CHAR_3;
903 Element mElement_CHAR_4;
904
905 Element mElement_USHORT_2;
906 Element mElement_USHORT_3;
907 Element mElement_USHORT_4;
908
909 Element mElement_SHORT_2;
910 Element mElement_SHORT_3;
911 Element mElement_SHORT_4;
912
913 Element mElement_UINT_2;
914 Element mElement_UINT_3;
915 Element mElement_UINT_4;
916
917 Element mElement_INT_2;
918 Element mElement_INT_3;
919 Element mElement_INT_4;
920
921 Element mElement_ULONG_2;
922 Element mElement_ULONG_3;
923 Element mElement_ULONG_4;
924
925 Element mElement_LONG_2;
926 Element mElement_LONG_3;
927 Element mElement_LONG_4;
928
Tim Murray932e78e2013-09-03 11:42:26 -0700929 Element mElement_YUV;
930
Jason Sams1d45c472010-08-25 14:31:48 -0700931 Element mElement_MATRIX_4X4;
932 Element mElement_MATRIX_3X3;
933 Element mElement_MATRIX_2X2;
934
Jason Sams4d339932010-05-11 14:03:58 -0700935 Sampler mSampler_CLAMP_NEAREST;
936 Sampler mSampler_CLAMP_LINEAR;
937 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
938 Sampler mSampler_WRAP_NEAREST;
939 Sampler mSampler_WRAP_LINEAR;
940 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
Tim Murray6b9b2ca2013-02-15 13:25:55 -0800941 Sampler mSampler_MIRRORED_REPEAT_NEAREST;
942 Sampler mSampler_MIRRORED_REPEAT_LINEAR;
943 Sampler mSampler_MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
Jason Sams4d339932010-05-11 14:03:58 -0700944
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700945 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
946 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700947 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
948 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700949
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700950 ProgramRaster mProgramRaster_CULL_BACK;
951 ProgramRaster mProgramRaster_CULL_FRONT;
952 ProgramRaster mProgramRaster_CULL_NONE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700953
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700954 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700955 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700956
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700957 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700958 * The base class from which an application should derive in order
959 * to receive RS messages from scripts. When a script calls {@code
960 * rsSendToClient}, the data fields will be filled, and the run
961 * method will be called on a separate thread. This will occur
962 * some time after {@code rsSendToClient} completes in the script,
963 * as {@code rsSendToClient} is asynchronous. Message handlers are
964 * not guaranteed to have completed when {@link
965 * android.renderscript.RenderScript#finish} returns.
Jason Sams27676fe2010-11-10 17:00:59 -0800966 *
967 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800968 public static class RSMessageHandler implements Runnable {
Jason Sams516c3192009-10-06 13:58:47 -0700969 protected int[] mData;
970 protected int mID;
Jason Sams1c415172010-11-08 17:06:46 -0800971 protected int mLength;
Jason Sams516c3192009-10-06 13:58:47 -0700972 public void run() {
973 }
974 }
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700975 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -0700976 * If an application is expecting messages, it should set this
977 * field to an instance of {@link RSMessageHandler}. This
978 * instance will receive all the user messages sent from {@code
979 * sendToClient} by scripts from this context.
Jason Sams27676fe2010-11-10 17:00:59 -0800980 *
981 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800982 RSMessageHandler mMessageCallback = null;
983
984 public void setMessageHandler(RSMessageHandler msg) {
985 mMessageCallback = msg;
986 }
987 public RSMessageHandler getMessageHandler() {
988 return mMessageCallback;
989 }
Jason Sams516c3192009-10-06 13:58:47 -0700990
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700991 /**
Jason Sams02d56d92013-04-12 16:40:50 -0700992 * Place a message into the message queue to be sent back to the message
993 * handler once all previous commands have been executed.
Jason Sams455d6442013-02-05 19:20:18 -0800994 *
995 * @param id
996 * @param data
997 */
998 public void sendMessage(int id, int[] data) {
999 nContextSendMessage(id, data);
1000 }
1001
1002 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001003 * The runtime error handler base class. An application should derive from this class
1004 * if it wishes to install an error handler. When errors occur at runtime,
1005 * the fields in this class will be filled, and the run method will be called.
Jason Sams27676fe2010-11-10 17:00:59 -08001006 *
1007 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001008 public static class RSErrorHandler implements Runnable {
Jason Sams1c415172010-11-08 17:06:46 -08001009 protected String mErrorMessage;
1010 protected int mErrorNum;
1011 public void run() {
1012 }
1013 }
Jason Sams27676fe2010-11-10 17:00:59 -08001014
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001015 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001016 * Application Error handler. All runtime errors will be dispatched to the
1017 * instance of RSAsyncError set here. If this field is null a
Tim Murrayc11e25c2013-04-09 11:01:01 -07001018 * {@link RSRuntimeException} will instead be thrown with details about the error.
Jason Sams27676fe2010-11-10 17:00:59 -08001019 * This will cause program termaination.
1020 *
1021 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001022 RSErrorHandler mErrorCallback = null;
1023
1024 public void setErrorHandler(RSErrorHandler msg) {
1025 mErrorCallback = msg;
1026 }
1027 public RSErrorHandler getErrorHandler() {
1028 return mErrorCallback;
1029 }
Jason Sams1c415172010-11-08 17:06:46 -08001030
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001031 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001032 * RenderScript worker thread priority enumeration. The default value is
1033 * NORMAL. Applications wishing to do background processing should set
1034 * their priority to LOW to avoid starving forground processes.
Jason Sams27676fe2010-11-10 17:00:59 -08001035 */
Jason Sams7d787b42009-11-15 12:14:26 -08001036 public enum Priority {
Glenn Kasten260c77a2011-06-01 17:25:54 -07001037 LOW (Process.THREAD_PRIORITY_BACKGROUND + (5 * Process.THREAD_PRIORITY_LESS_FAVORABLE)),
1038 NORMAL (Process.THREAD_PRIORITY_DISPLAY);
Jason Sams7d787b42009-11-15 12:14:26 -08001039
1040 int mID;
1041 Priority(int id) {
1042 mID = id;
1043 }
1044 }
1045
Jason Sams771bebb2009-12-07 12:40:12 -08001046 void validate() {
1047 if (mContext == 0) {
Jason Samsc1d62102010-11-04 14:32:19 -07001048 throw new RSInvalidStateException("Calling RS with no Context active.");
Jason Sams771bebb2009-12-07 12:40:12 -08001049 }
1050 }
1051
Jason Sams27676fe2010-11-10 17:00:59 -08001052
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001053 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001054 * Change the priority of the worker threads for this context.
1055 *
1056 * @param p New priority to be set.
1057 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001058 public void setPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -08001059 validate();
Jason Sams7d787b42009-11-15 12:14:26 -08001060 nContextSetPriority(p.mID);
1061 }
1062
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001063 static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -07001064 RenderScript mRS;
1065 boolean mRun = true;
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001066 int[] mAuxData = new int[2];
Jason Sams1c415172010-11-08 17:06:46 -08001067
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001068 static final int RS_MESSAGE_TO_CLIENT_NONE = 0;
1069 static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1;
1070 static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
1071 static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
1072 static final int RS_MESSAGE_TO_CLIENT_USER = 4;
Jason Sams739c8262013-04-11 18:07:52 -07001073 static final int RS_MESSAGE_TO_CLIENT_NEW_BUFFER = 5;
Jason Sams516c3192009-10-06 13:58:47 -07001074
Stephen Hines42028a82013-04-17 19:22:01 -07001075 static final int RS_ERROR_FATAL_DEBUG = 0x0800;
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001076 static final int RS_ERROR_FATAL_UNKNOWN = 0x1000;
Jason Samsadd9d962010-11-22 16:20:16 -08001077
Jason Sams516c3192009-10-06 13:58:47 -07001078 MessageThread(RenderScript rs) {
1079 super("RSMessageThread");
1080 mRS = rs;
1081
1082 }
1083
1084 public void run() {
1085 // This function is a temporary solution. The final solution will
1086 // used typed allocations where the message id is the type indicator.
1087 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -07001088 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -07001089 while(mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -07001090 rbuf[0] = 0;
Jason Samsedbfabd2011-05-17 15:01:29 -07001091 int msg = mRS.nContextPeekMessage(mRS.mContext, mAuxData);
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001092 int size = mAuxData[1];
1093 int subID = mAuxData[0];
Jason Sams1c415172010-11-08 17:06:46 -08001094
1095 if (msg == RS_MESSAGE_TO_CLIENT_USER) {
1096 if ((size>>2) >= rbuf.length) {
1097 rbuf = new int[(size + 3) >> 2];
1098 }
Jason Samsedbfabd2011-05-17 15:01:29 -07001099 if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) !=
1100 RS_MESSAGE_TO_CLIENT_USER) {
Tim Murrayc11e25c2013-04-09 11:01:01 -07001101 throw new RSDriverException("Error processing message from RenderScript.");
Jason Samsedbfabd2011-05-17 15:01:29 -07001102 }
Jason Sams1c415172010-11-08 17:06:46 -08001103
1104 if(mRS.mMessageCallback != null) {
1105 mRS.mMessageCallback.mData = rbuf;
1106 mRS.mMessageCallback.mID = subID;
1107 mRS.mMessageCallback.mLength = size;
1108 mRS.mMessageCallback.run();
Jason Sams1d45c472010-08-25 14:31:48 -07001109 } else {
Jason Sams1c415172010-11-08 17:06:46 -08001110 throw new RSInvalidStateException("Received a message from the script with no message handler installed.");
Jason Sams516c3192009-10-06 13:58:47 -07001111 }
Stephen Hinesab98bb62010-09-24 14:38:30 -07001112 continue;
Jason Sams516c3192009-10-06 13:58:47 -07001113 }
Jason Sams1c415172010-11-08 17:06:46 -08001114
1115 if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
1116 String e = mRS.nContextGetErrorMessage(mRS.mContext);
1117
Stephen Hines42028a82013-04-17 19:22:01 -07001118 // Throw RSRuntimeException under the following conditions:
1119 //
1120 // 1) It is an unknown fatal error.
1121 // 2) It is a debug fatal error, and we are not in a
1122 // debug context.
1123 // 3) It is a debug fatal error, and we do not have an
1124 // error callback.
1125 if (subID >= RS_ERROR_FATAL_UNKNOWN ||
1126 (subID >= RS_ERROR_FATAL_DEBUG &&
1127 (mRS.mContextType != ContextType.DEBUG ||
1128 mRS.mErrorCallback == null))) {
Jason Samsadd9d962010-11-22 16:20:16 -08001129 throw new RSRuntimeException("Fatal error " + subID + ", details: " + e);
1130 }
1131
Jason Sams1c415172010-11-08 17:06:46 -08001132 if(mRS.mErrorCallback != null) {
1133 mRS.mErrorCallback.mErrorMessage = e;
1134 mRS.mErrorCallback.mErrorNum = subID;
1135 mRS.mErrorCallback.run();
1136 } else {
Jason Samsa4b7bc92013-02-05 15:05:39 -08001137 android.util.Log.e(LOG_TAG, "non fatal RS error, " + e);
Stephen Hinesbe74bdd2012-02-03 15:29:36 -08001138 // Do not throw here. In these cases, we do not have
1139 // a fatal error.
Jason Sams1c415172010-11-08 17:06:46 -08001140 }
1141 continue;
1142 }
1143
Jason Sams739c8262013-04-11 18:07:52 -07001144 if (msg == RS_MESSAGE_TO_CLIENT_NEW_BUFFER) {
1145 Allocation.sendBufferNotification(subID);
1146 continue;
1147 }
1148
Jason Sams1c415172010-11-08 17:06:46 -08001149 // 2: teardown.
1150 // But we want to avoid starving other threads during
1151 // teardown by yielding until the next line in the destructor
1152 // can execute to set mRun = false
1153 try {
1154 sleep(1, 0);
1155 } catch(InterruptedException e) {
Jason Sams516c3192009-10-06 13:58:47 -07001156 }
Jason Sams516c3192009-10-06 13:58:47 -07001157 }
Tim Murrayda67deb2013-05-09 12:02:50 -07001158 //Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -07001159 }
1160 }
1161
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001162 RenderScript(Context ctx) {
Stephen Hines42028a82013-04-17 19:22:01 -07001163 mContextType = ContextType.NORMAL;
Jason Sams1a4e1f3e2012-02-24 17:51:24 -08001164 if (ctx != null) {
1165 mApplicationContext = ctx.getApplicationContext();
1166 }
Jason Samsff7256e2014-03-10 13:31:51 -07001167 mRWLock = new ReentrantReadWriteLock();
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001168 }
1169
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001170 /**
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001171 * Gets the application context associated with the RenderScript context.
1172 *
1173 * @return The application context.
1174 */
1175 public final Context getApplicationContext() {
1176 return mApplicationContext;
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001177 }
1178
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001179 /**
Jason Samsadd26dc2013-02-22 18:43:45 -08001180 * @hide
1181 */
1182 public static RenderScript create(Context ctx, int sdkVersion) {
1183 return create(ctx, sdkVersion, ContextType.NORMAL);
1184 }
1185
1186 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001187 * Create a RenderScript context.
Jason Sams27676fe2010-11-10 17:00:59 -08001188 *
Jason Sams1a4e1f3e2012-02-24 17:51:24 -08001189 * @hide
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001190 * @param ctx The context.
Jason Sams27676fe2010-11-10 17:00:59 -08001191 * @return RenderScript
1192 */
Jason Samsadd26dc2013-02-22 18:43:45 -08001193 public static RenderScript create(Context ctx, int sdkVersion, ContextType ct) {
Dan Morrille4d9a012013-03-28 18:10:43 -07001194 if (!sInitialized) {
1195 Log.e(LOG_TAG, "RenderScript.create() called when disabled; someone is likely to crash");
1196 return null;
1197 }
1198
Shih-wei Liao6b32fab2010-12-10 01:03:59 -08001199 RenderScript rs = new RenderScript(ctx);
Jason Sams704ff642010-02-09 16:05:07 -08001200
1201 rs.mDev = rs.nDeviceCreate();
Jason Samsadd26dc2013-02-22 18:43:45 -08001202 rs.mContext = rs.nContextCreate(rs.mDev, 0, sdkVersion, ct.mID);
Stephen Hines42028a82013-04-17 19:22:01 -07001203 rs.mContextType = ct;
Jason Sams26985362011-05-03 15:01:58 -07001204 if (rs.mContext == 0) {
1205 throw new RSDriverException("Failed to create RS context.");
1206 }
Jason Sams704ff642010-02-09 16:05:07 -08001207 rs.mMessageThread = new MessageThread(rs);
1208 rs.mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -08001209 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -08001210 }
1211
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001212 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001213 * Create a RenderScript context.
Jason Sams1a4e1f3e2012-02-24 17:51:24 -08001214 *
1215 * @param ctx The context.
1216 * @return RenderScript
1217 */
1218 public static RenderScript create(Context ctx) {
Jason Samsadd26dc2013-02-22 18:43:45 -08001219 return create(ctx, ContextType.NORMAL);
1220 }
1221
1222 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001223 * Create a RenderScript context.
Jason Samsadd26dc2013-02-22 18:43:45 -08001224 *
Jason Samsadd26dc2013-02-22 18:43:45 -08001225 *
1226 * @param ctx The context.
Jason Sams02d56d92013-04-12 16:40:50 -07001227 * @param ct The type of context to be created.
Jason Samsadd26dc2013-02-22 18:43:45 -08001228 * @return RenderScript
1229 */
1230 public static RenderScript create(Context ctx, ContextType ct) {
Jason Sams1a4e1f3e2012-02-24 17:51:24 -08001231 int v = ctx.getApplicationInfo().targetSdkVersion;
Jason Samsadd26dc2013-02-22 18:43:45 -08001232 return create(ctx, v, ct);
Jason Sams1a4e1f3e2012-02-24 17:51:24 -08001233 }
1234
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001235 /**
Jason Sams27676fe2010-11-10 17:00:59 -08001236 * Print the currently available debugging information about the state of
1237 * the RS context to the log.
1238 *
Jason Sams27676fe2010-11-10 17:00:59 -08001239 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001240 public void contextDump() {
Jason Sams5dbfe932010-01-27 14:41:43 -08001241 validate();
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001242 nContextDump(0);
Jason Sams715333b2009-11-17 17:26:46 -08001243 }
1244
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001245 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001246 * Wait for any pending asynchronous opeations (such as copies to a RS
1247 * allocation or RS script executions) to complete.
Jason Sams27676fe2010-11-10 17:00:59 -08001248 *
1249 */
Jason Sams96ed4cf2010-06-15 12:15:57 -07001250 public void finish() {
1251 nContextFinish();
1252 }
1253
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -07001254 /**
Tim Murrayc11e25c2013-04-09 11:01:01 -07001255 * Destroys this RenderScript context. Once this function is called,
1256 * using this context or any objects belonging to this context is
1257 * illegal.
Jason Sams27676fe2010-11-10 17:00:59 -08001258 *
1259 */
Jason Samsf5b45962009-08-25 14:49:07 -07001260 public void destroy() {
Jason Sams5dbfe932010-01-27 14:41:43 -08001261 validate();
Jason Samsff7256e2014-03-10 13:31:51 -07001262 nContextFinish();
1263
Jason Sams2e1872f2010-08-17 16:25:41 -07001264 nContextDeinitToClient(mContext);
Jason Sams516c3192009-10-06 13:58:47 -07001265 mMessageThread.mRun = false;
Jason Samsa8bf9422010-09-16 13:43:19 -07001266 try {
1267 mMessageThread.join();
1268 } catch(InterruptedException e) {
1269 }
Jason Sams516c3192009-10-06 13:58:47 -07001270
Jason Sams2e1872f2010-08-17 16:25:41 -07001271 nContextDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -07001272
1273 nDeviceDestroy(mDev);
1274 mDev = 0;
1275 }
Jason Sams02fb2cb2009-05-28 15:37:57 -07001276
Jason Samsa9e7a052009-09-25 14:51:22 -07001277 boolean isAlive() {
1278 return mContext != 0;
1279 }
1280
Jason Samsbf6ef8d72010-12-06 15:59:59 -08001281 int safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -07001282 if(o != null) {
Jason Samse07694b2012-04-03 15:36:36 -07001283 return o.getID(this);
Jason Samsd8e41612009-08-20 17:22:40 -07001284 }
Jason Sams6b9dec02009-09-23 16:38:37 -07001285 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001286 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001287}