blob: 4c9ad569262a6919400ce5de51776688f03276a5 [file] [log] [blame]
Jack Palevich60aa3ea2009-05-26 13:45:08 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jason Sams94d8e90a2009-06-10 16:09:05 -070017package android.renderscript;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070018
Jason Sams43ee06852009-08-12 17:54:11 -070019import java.lang.reflect.Field;
Jason Sams36e612a2009-07-31 16:26:13 -070020
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080021import android.content.Context;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -080022import android.content.res.AssetManager;
Jason Samsb8c5a842009-07-31 20:40:47 -070023import android.graphics.Bitmap;
Romain Guy650a3eb2009-08-31 14:06:43 -070024import android.graphics.BitmapFactory;
Jason Sams36e612a2009-07-31 16:26:13 -070025import android.util.Config;
26import android.util.Log;
27import android.view.Surface;
Jack Palevich43702d82009-05-28 13:38:16 -070028
Jack Palevich60aa3ea2009-05-26 13:45:08 -070029
Jason Samse29d4712009-07-23 15:19:03 -070030/**
Jason Sams27676fe2010-11-10 17:00:59 -080031 * RenderScript base master class. An instance of this class creates native
32 * worker threads for processing commands from this object. This base class
33 * does not provide any extended capabilities beyond simple data processing.
34 * For extended capabilities use derived classes such as RenderScriptGL.
35 *
36 *
37 *
Jason Samse29d4712009-07-23 15:19:03 -070038 **/
Jack Palevich60aa3ea2009-05-26 13:45:08 -070039public class RenderScript {
Jason Sams3bc47d42009-11-12 15:10:25 -080040 static final String LOG_TAG = "RenderScript_jni";
Jason Samsbf6ef8d72010-12-06 15:59:59 -080041 static final boolean DEBUG = false;
Romain Guy650a3eb2009-08-31 14:06:43 -070042 @SuppressWarnings({"UnusedDeclaration", "deprecation"})
Jason Samsbf6ef8d72010-12-06 15:59:59 -080043 static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070044
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080045 private Context mApplicationContext;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070046
Shih-wei Liao6b32fab2010-12-10 01:03:59 -080047 /*
Jack Palevich60aa3ea2009-05-26 13:45:08 -070048 * We use a class initializer to allow the native code to cache some
49 * field offsets.
50 */
Romain Guy650a3eb2009-08-31 14:06:43 -070051 @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
Jason Samsbf6ef8d72010-12-06 15:59:59 -080052 static boolean sInitialized;
53 native static void _nInit();
Jack Palevich60aa3ea2009-05-26 13:45:08 -070054
Jason Samsdba3ba52009-07-30 14:56:12 -070055
Jack Palevich60aa3ea2009-05-26 13:45:08 -070056 static {
57 sInitialized = false;
58 try {
Jason Samse29d4712009-07-23 15:19:03 -070059 System.loadLibrary("rs_jni");
Jack Palevich60aa3ea2009-05-26 13:45:08 -070060 _nInit();
Jack Palevich60aa3ea2009-05-26 13:45:08 -070061 sInitialized = true;
62 } catch (UnsatisfiedLinkError e) {
Jason Samsfa445b92011-01-07 17:00:07 -080063 Log.e(LOG_TAG, "Error loading RS jni library: " + e);
64 throw new RSRuntimeException("Error loading RS jni library: " + e);
Jack Palevich60aa3ea2009-05-26 13:45:08 -070065 }
66 }
67
Jason Sams2e1872f2010-08-17 16:25:41 -070068 // Non-threadsafe functions.
Jason Sams36e612a2009-07-31 16:26:13 -070069 native int nDeviceCreate();
70 native void nDeviceDestroy(int dev);
Jason Samsebfb4362009-09-23 13:57:02 -070071 native void nDeviceSetConfig(int dev, int param, int value);
Jason Sams1c415172010-11-08 17:06:46 -080072 native void nContextGetUserMessage(int con, int[] data);
73 native String nContextGetErrorMessage(int con);
74 native int nContextPeekMessage(int con, int[] subID, boolean wait);
Jason Sams2e1872f2010-08-17 16:25:41 -070075 native void nContextInitToClient(int con);
76 native void nContextDeinitToClient(int con);
Jason Sams3eaa338e2009-06-10 15:04:38 -070077
Jason Sams718cd1f2009-12-23 14:35:29 -080078
Jason Sams2e1872f2010-08-17 16:25:41 -070079 // Methods below are wrapped to protect the non-threadsafe
80 // lockless fifo.
Jason Sams11c8af92010-10-13 15:31:10 -070081 native int rsnContextCreateGL(int dev, int ver,
82 int colorMin, int colorPref,
83 int alphaMin, int alphaPref,
84 int depthMin, int depthPref,
85 int stencilMin, int stencilPref,
86 int samplesMin, int samplesPref, float samplesQ);
87 synchronized int nContextCreateGL(int dev, int ver,
88 int colorMin, int colorPref,
89 int alphaMin, int alphaPref,
90 int depthMin, int depthPref,
91 int stencilMin, int stencilPref,
92 int samplesMin, int samplesPref, float samplesQ) {
93 return rsnContextCreateGL(dev, ver, colorMin, colorPref,
94 alphaMin, alphaPref, depthMin, depthPref,
95 stencilMin, stencilPref,
96 samplesMin, samplesPref, samplesQ);
Jason Sams2e1872f2010-08-17 16:25:41 -070097 }
98 native int rsnContextCreate(int dev, int ver);
99 synchronized int nContextCreate(int dev, int ver) {
100 return rsnContextCreate(dev, ver);
101 }
102 native void rsnContextDestroy(int con);
103 synchronized void nContextDestroy() {
104 rsnContextDestroy(mContext);
105 }
106 native void rsnContextSetSurface(int con, int w, int h, Surface sur);
107 synchronized void nContextSetSurface(int w, int h, Surface sur) {
108 rsnContextSetSurface(mContext, w, h, sur);
109 }
110 native void rsnContextSetPriority(int con, int p);
111 synchronized void nContextSetPriority(int p) {
112 rsnContextSetPriority(mContext, p);
113 }
114 native void rsnContextDump(int con, int bits);
115 synchronized void nContextDump(int bits) {
116 rsnContextDump(mContext, bits);
117 }
118 native void rsnContextFinish(int con);
119 synchronized void nContextFinish() {
120 rsnContextFinish(mContext);
121 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700122
Jason Sams2e1872f2010-08-17 16:25:41 -0700123 native void rsnContextBindRootScript(int con, int script);
124 synchronized void nContextBindRootScript(int script) {
125 rsnContextBindRootScript(mContext, script);
126 }
127 native void rsnContextBindSampler(int con, int sampler, int slot);
128 synchronized void nContextBindSampler(int sampler, int slot) {
129 rsnContextBindSampler(mContext, sampler, slot);
130 }
131 native void rsnContextBindProgramStore(int con, int pfs);
132 synchronized void nContextBindProgramStore(int pfs) {
133 rsnContextBindProgramStore(mContext, pfs);
134 }
135 native void rsnContextBindProgramFragment(int con, int pf);
136 synchronized void nContextBindProgramFragment(int pf) {
137 rsnContextBindProgramFragment(mContext, pf);
138 }
139 native void rsnContextBindProgramVertex(int con, int pv);
140 synchronized void nContextBindProgramVertex(int pv) {
141 rsnContextBindProgramVertex(mContext, pv);
142 }
143 native void rsnContextBindProgramRaster(int con, int pr);
144 synchronized void nContextBindProgramRaster(int pr) {
145 rsnContextBindProgramRaster(mContext, pr);
146 }
147 native void rsnContextPause(int con);
148 synchronized void nContextPause() {
149 rsnContextPause(mContext);
150 }
151 native void rsnContextResume(int con);
152 synchronized void nContextResume() {
153 rsnContextResume(mContext);
154 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700155
Jason Sams2e1872f2010-08-17 16:25:41 -0700156 native void rsnAssignName(int con, int obj, byte[] name);
157 synchronized void nAssignName(int obj, byte[] name) {
158 rsnAssignName(mContext, obj, name);
159 }
160 native String rsnGetName(int con, int obj);
161 synchronized String nGetName(int obj) {
162 return rsnGetName(mContext, obj);
163 }
164 native void rsnObjDestroy(int con, int id);
165 synchronized void nObjDestroy(int id) {
166 rsnObjDestroy(mContext, id);
167 }
Jason Samsfe08d992009-05-27 14:45:32 -0700168
Jason Sams2e1872f2010-08-17 16:25:41 -0700169 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
170 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
171 return rsnElementCreate(mContext, type, kind, norm, vecSize);
172 }
Jason Sams70d4e502010-09-02 17:35:23 -0700173 native int rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
174 synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
175 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700176 }
177 native void rsnElementGetNativeData(int con, int id, int[] elementData);
178 synchronized void nElementGetNativeData(int id, int[] elementData) {
179 rsnElementGetNativeData(mContext, id, elementData);
180 }
181 native void rsnElementGetSubElements(int con, int id, int[] IDs, String[] names);
182 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names) {
183 rsnElementGetSubElements(mContext, id, IDs, names);
184 }
Jason Sams768bc022009-09-21 19:41:04 -0700185
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800186 native int rsnTypeCreate(int con, int eid, int x, int y, int z, boolean mips, boolean faces);
187 synchronized int nTypeCreate(int eid, int x, int y, int z, boolean mips, boolean faces) {
188 return rsnTypeCreate(mContext, eid, x, y, z, mips, faces);
Jason Sams2e1872f2010-08-17 16:25:41 -0700189 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700190 native void rsnTypeGetNativeData(int con, int id, int[] typeData);
191 synchronized void nTypeGetNativeData(int id, int[] typeData) {
192 rsnTypeGetNativeData(mContext, id, typeData);
193 }
Jason Sams768bc022009-09-21 19:41:04 -0700194
Jason Samsd4b23b52010-12-13 15:32:35 -0800195 native int rsnAllocationCreateTyped(int con, int type, int mip, int usage);
196 synchronized int nAllocationCreateTyped(int type, int mip, int usage) {
197 return rsnAllocationCreateTyped(mContext, type, mip, usage);
Jason Sams2e1872f2010-08-17 16:25:41 -0700198 }
Jason Sams5476b452010-12-08 16:14:36 -0800199 native int rsnAllocationCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
200 synchronized int nAllocationCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
201 return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700202 }
Jason Sams5476b452010-12-08 16:14:36 -0800203 native int rsnAllocationCubeCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
204 synchronized int nAllocationCubeCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
205 return rsnAllocationCubeCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800206 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700207 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
208 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
209 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
210 }
Jason Sams5476b452010-12-08 16:14:36 -0800211 native int rsnAllocationCreateFromAssetStream(int con, int mips, int assetStream, int usage);
212 synchronized int nAllocationCreateFromAssetStream(int mips, int assetStream, int usage) {
213 return rsnAllocationCreateFromAssetStream(mContext, mips, assetStream, usage);
214 }
215
Jason Sams4ef66502010-12-10 16:03:15 -0800216 native void rsnAllocationCopyToBitmap(int con, int alloc, Bitmap bmp);
217 synchronized void nAllocationCopyToBitmap(int alloc, Bitmap bmp) {
218 rsnAllocationCopyToBitmap(mContext, alloc, bmp);
219 }
220
221
Jason Sams5476b452010-12-08 16:14:36 -0800222 native void rsnAllocationSyncAll(int con, int alloc, int src);
223 synchronized void nAllocationSyncAll(int alloc, int src) {
224 rsnAllocationSyncAll(mContext, alloc, src);
225 }
Jason Sams4ef66502010-12-10 16:03:15 -0800226 native void rsnAllocationCopyFromBitmap(int con, int alloc, Bitmap bmp);
227 synchronized void nAllocationCopyFromBitmap(int alloc, Bitmap bmp) {
228 rsnAllocationCopyFromBitmap(mContext, alloc, bmp);
Jason Sams2e1872f2010-08-17 16:25:41 -0700229 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700230
Jason Sams49a05d72010-12-29 14:31:29 -0800231
232 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, int[] d, int sizeBytes);
233 synchronized void nAllocationData1D(int id, int off, int mip, int count, int[] d, int sizeBytes) {
234 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700235 }
Jason Sams49a05d72010-12-29 14:31:29 -0800236 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, short[] d, int sizeBytes);
237 synchronized void nAllocationData1D(int id, int off, int mip, int count, short[] d, int sizeBytes) {
238 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
239 }
240 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, byte[] d, int sizeBytes);
241 synchronized void nAllocationData1D(int id, int off, int mip, int count, byte[] d, int sizeBytes) {
242 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
243 }
244 native void rsnAllocationData1D(int con, int id, int off, int mip, int count, float[] d, int sizeBytes);
245 synchronized void nAllocationData1D(int id, int off, int mip, int count, float[] d, int sizeBytes) {
246 rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700247 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700248
Jason Sams49a05d72010-12-29 14:31:29 -0800249 native void rsnAllocationElementData1D(int con, int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes);
250 synchronized void nAllocationElementData1D(int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes) {
251 rsnAllocationElementData1D(mContext, id, xoff, mip, compIdx, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700252 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700253
Jason Samsfa445b92011-01-07 17:00:07 -0800254 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, byte[] d, int sizeBytes);
255 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, byte[] d, int sizeBytes) {
256 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
257 }
258 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, short[] d, int sizeBytes);
259 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, short[] d, int sizeBytes) {
260 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
261 }
Jason Sams49a05d72010-12-29 14:31:29 -0800262 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, int[] d, int sizeBytes);
263 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, int[] d, int sizeBytes) {
264 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700265 }
Jason Sams49a05d72010-12-29 14:31:29 -0800266 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, float[] d, int sizeBytes);
267 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, float[] d, int sizeBytes) {
268 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700269 }
Jason Samsfa445b92011-01-07 17:00:07 -0800270 native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, Bitmap b);
271 synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, Bitmap b) {
272 rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, b);
273 }
Jason Sams49a05d72010-12-29 14:31:29 -0800274
Jason Samsfa445b92011-01-07 17:00:07 -0800275 native void rsnAllocationRead(int con, int id, byte[] d);
276 synchronized void nAllocationRead(int id, byte[] d) {
277 rsnAllocationRead(mContext, id, d);
278 }
279 native void rsnAllocationRead(int con, int id, short[] d);
280 synchronized void nAllocationRead(int id, short[] d) {
281 rsnAllocationRead(mContext, id, d);
282 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700283 native void rsnAllocationRead(int con, int id, int[] d);
284 synchronized void nAllocationRead(int id, int[] d) {
285 rsnAllocationRead(mContext, id, d);
286 }
287 native void rsnAllocationRead(int con, int id, float[] d);
288 synchronized void nAllocationRead(int id, float[] d) {
289 rsnAllocationRead(mContext, id, d);
290 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700291 native int rsnAllocationGetType(int con, int id);
292 synchronized int nAllocationGetType(int id) {
293 return rsnAllocationGetType(mContext, id);
294 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700295
Jason Sams5edc6082010-10-05 13:32:49 -0700296 native void rsnAllocationResize1D(int con, int id, int dimX);
297 synchronized void nAllocationResize1D(int id, int dimX) {
298 rsnAllocationResize1D(mContext, id, dimX);
299 }
300 native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
301 synchronized void nAllocationResize2D(int id, int dimX, int dimY) {
302 rsnAllocationResize2D(mContext, id, dimX, dimY);
303 }
304
Jason Sams2e1872f2010-08-17 16:25:41 -0700305 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
306 synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
307 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
308 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800309 native int rsnFileA3DCreateFromFile(int con, String path);
310 synchronized int nFileA3DCreateFromFile(String path) {
311 return rsnFileA3DCreateFromFile(mContext, path);
312 }
313 native int rsnFileA3DCreateFromAsset(int con, AssetManager mgr, String path);
314 synchronized int nFileA3DCreateFromAsset(AssetManager mgr, String path) {
315 return rsnFileA3DCreateFromAsset(mContext, mgr, path);
316 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700317 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
318 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
319 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
320 }
321 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
322 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
323 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
324 }
325 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
326 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
327 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
328 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700329
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800330 native int rsnFontCreateFromFile(int con, String fileName, float size, int dpi);
331 synchronized int nFontCreateFromFile(String fileName, float size, int dpi) {
Jason Sams2e1872f2010-08-17 16:25:41 -0700332 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
333 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800334 native int rsnFontCreateFromAssetStream(int con, String name, float size, int dpi, int assetStream);
335 synchronized int nFontCreateFromAssetStream(String name, float size, int dpi, int assetStream) {
336 return rsnFontCreateFromAssetStream(mContext, name, size, dpi, assetStream);
337 }
338 native int rsnFontCreateFromAsset(int con, AssetManager mgr, String path, float size, int dpi);
339 synchronized int nFontCreateFromAsset(AssetManager mgr, String path, float size, int dpi) {
340 return rsnFontCreateFromAsset(mContext, mgr, path, size, dpi);
341 }
Jason Sams22534172009-08-04 16:58:20 -0700342
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700343
Jason Sams2e1872f2010-08-17 16:25:41 -0700344 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
345 synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
346 rsnScriptBindAllocation(mContext, script, alloc, slot);
347 }
348 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
349 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
350 rsnScriptSetTimeZone(mContext, script, timeZone);
351 }
352 native void rsnScriptInvoke(int con, int id, int slot);
353 synchronized void nScriptInvoke(int id, int slot) {
354 rsnScriptInvoke(mContext, id, slot);
355 }
356 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
357 synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
358 rsnScriptInvokeV(mContext, id, slot, params);
359 }
360 native void rsnScriptSetVarI(int con, int id, int slot, int val);
361 synchronized void nScriptSetVarI(int id, int slot, int val) {
362 rsnScriptSetVarI(mContext, id, slot, val);
363 }
Stephen Hines031ec58c2010-10-11 10:54:21 -0700364 native void rsnScriptSetVarJ(int con, int id, int slot, long val);
365 synchronized void nScriptSetVarJ(int id, int slot, long val) {
366 rsnScriptSetVarJ(mContext, id, slot, val);
367 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700368 native void rsnScriptSetVarF(int con, int id, int slot, float val);
369 synchronized void nScriptSetVarF(int id, int slot, float val) {
370 rsnScriptSetVarF(mContext, id, slot, val);
371 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700372 native void rsnScriptSetVarD(int con, int id, int slot, double val);
373 synchronized void nScriptSetVarD(int id, int slot, double val) {
374 rsnScriptSetVarD(mContext, id, slot, val);
375 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700376 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
377 synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
378 rsnScriptSetVarV(mContext, id, slot, val);
379 }
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800380 native void rsnScriptSetVarObj(int con, int id, int slot, int val);
381 synchronized void nScriptSetVarObj(int id, int slot, int val) {
382 rsnScriptSetVarObj(mContext, id, slot, val);
383 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700384
Jason Sams2e1872f2010-08-17 16:25:41 -0700385 native void rsnScriptCBegin(int con);
386 synchronized void nScriptCBegin() {
387 rsnScriptCBegin(mContext);
388 }
389 native void rsnScriptCSetScript(int con, byte[] script, int offset, int length);
390 synchronized void nScriptCSetScript(byte[] script, int offset, int length) {
391 rsnScriptCSetScript(mContext, script, offset, length);
392 }
Shih-wei Liaoeeca4352010-12-20 20:45:56 +0800393 native int rsnScriptCCreate(int con, String packageName, String resName, String cacheDir);
394 synchronized int nScriptCCreate(String packageName, String resName, String cacheDir) {
395 return rsnScriptCCreate(mContext, packageName, resName, cacheDir);
Jason Sams2e1872f2010-08-17 16:25:41 -0700396 }
Jason Samsebfb4362009-09-23 13:57:02 -0700397
Jason Sams2e1872f2010-08-17 16:25:41 -0700398 native void rsnSamplerBegin(int con);
399 synchronized void nSamplerBegin() {
400 rsnSamplerBegin(mContext);
401 }
402 native void rsnSamplerSet(int con, int param, int value);
403 synchronized void nSamplerSet(int param, int value) {
404 rsnSamplerSet(mContext, param, value);
405 }
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700406 native void rsnSamplerSet2(int con, int param, float value);
407 synchronized void nSamplerSet2(int param, float value) {
408 rsnSamplerSet2(mContext, param, value);
409 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700410 native int rsnSamplerCreate(int con);
411 synchronized int nSamplerCreate() {
412 return rsnSamplerCreate(mContext);
413 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800414
Jason Sams2e1872f2010-08-17 16:25:41 -0700415 native void rsnProgramStoreBegin(int con, int in, int out);
416 synchronized void nProgramStoreBegin(int in, int out) {
417 rsnProgramStoreBegin(mContext, in, out);
418 }
419 native void rsnProgramStoreDepthFunc(int con, int func);
420 synchronized void nProgramStoreDepthFunc(int func) {
421 rsnProgramStoreDepthFunc(mContext, func);
422 }
423 native void rsnProgramStoreDepthMask(int con, boolean enable);
424 synchronized void nProgramStoreDepthMask(boolean enable) {
425 rsnProgramStoreDepthMask(mContext, enable);
426 }
427 native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a);
428 synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
429 rsnProgramStoreColorMask(mContext, r, g, b, a);
430 }
431 native void rsnProgramStoreBlendFunc(int con, int src, int dst);
432 synchronized void nProgramStoreBlendFunc(int src, int dst) {
433 rsnProgramStoreBlendFunc(mContext, src, dst);
434 }
435 native void rsnProgramStoreDither(int con, boolean enable);
436 synchronized void nProgramStoreDither(boolean enable) {
437 rsnProgramStoreDither(mContext, enable);
438 }
439 native int rsnProgramStoreCreate(int con);
440 synchronized int nProgramStoreCreate() {
441 return rsnProgramStoreCreate(mContext);
442 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700443
Jason Sams2e1872f2010-08-17 16:25:41 -0700444 native int rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
445 synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) {
446 return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite);
447 }
448 native void rsnProgramRasterSetLineWidth(int con, int pr, float v);
449 synchronized void nProgramRasterSetLineWidth(int pr, float v) {
450 rsnProgramRasterSetLineWidth(mContext, pr, v);
451 }
452 native void rsnProgramRasterSetCullMode(int con, int pr, int mode);
453 synchronized void nProgramRasterSetCullMode(int pr, int mode) {
454 rsnProgramRasterSetCullMode(mContext, pr, mode);
455 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700456
Jason Sams2e1872f2010-08-17 16:25:41 -0700457 native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
458 synchronized void nProgramBindConstants(int pv, int slot, int mID) {
459 rsnProgramBindConstants(mContext, pv, slot, mID);
460 }
461 native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
462 synchronized void nProgramBindTexture(int vpf, int slot, int a) {
463 rsnProgramBindTexture(mContext, vpf, slot, a);
464 }
465 native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
466 synchronized void nProgramBindSampler(int vpf, int slot, int s) {
467 rsnProgramBindSampler(mContext, vpf, slot, s);
468 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700469 native int rsnProgramFragmentCreate(int con, String shader, int[] params);
470 synchronized int nProgramFragmentCreate(String shader, int[] params) {
471 return rsnProgramFragmentCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700472 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700473 native int rsnProgramVertexCreate(int con, String shader, int[] params);
474 synchronized int nProgramVertexCreate(String shader, int[] params) {
475 return rsnProgramVertexCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700476 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700477
Jason Sams2e1872f2010-08-17 16:25:41 -0700478 native int rsnMeshCreate(int con, int vtxCount, int indexCount);
479 synchronized int nMeshCreate(int vtxCount, int indexCount) {
480 return rsnMeshCreate(mContext, vtxCount, indexCount);
481 }
482 native void rsnMeshBindVertex(int con, int id, int alloc, int slot);
483 synchronized void nMeshBindVertex(int id, int alloc, int slot) {
484 rsnMeshBindVertex(mContext, id, alloc, slot);
485 }
486 native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot);
487 synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) {
488 rsnMeshBindIndex(mContext, id, alloc, prim, slot);
489 }
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800490 native void rsnMeshInitVertexAttribs(int con, int id);
491 synchronized void nMeshInitVertexAttribs(int id) {
492 rsnMeshInitVertexAttribs(mContext, id);
493 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700494 native int rsnMeshGetVertexBufferCount(int con, int id);
495 synchronized int nMeshGetVertexBufferCount(int id) {
496 return rsnMeshGetVertexBufferCount(mContext, id);
497 }
498 native int rsnMeshGetIndexCount(int con, int id);
499 synchronized int nMeshGetIndexCount(int id) {
500 return rsnMeshGetIndexCount(mContext, id);
501 }
502 native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
503 synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
504 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
505 }
506 native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
507 synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
508 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
509 }
510
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700511
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800512 int mDev;
513 int mContext;
Romain Guy650a3eb2009-08-31 14:06:43 -0700514 @SuppressWarnings({"FieldCanBeLocal"})
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800515 MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700516
Jason Sams8cb39de2010-06-01 15:47:01 -0700517 Element mElement_U8;
518 Element mElement_I8;
519 Element mElement_U16;
520 Element mElement_I16;
521 Element mElement_U32;
522 Element mElement_I32;
Stephen Hines52d83632010-10-11 16:10:42 -0700523 Element mElement_U64;
Stephen Hinesef1dac22010-10-01 15:39:33 -0700524 Element mElement_I64;
Jason Sams8cb39de2010-06-01 15:47:01 -0700525 Element mElement_F32;
Stephen Hines02f417052010-09-30 15:19:22 -0700526 Element mElement_F64;
Jason Samsf110d4b2010-06-21 17:42:41 -0700527 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -0700528
Jason Sams8cb39de2010-06-01 15:47:01 -0700529 Element mElement_ELEMENT;
530 Element mElement_TYPE;
531 Element mElement_ALLOCATION;
532 Element mElement_SAMPLER;
533 Element mElement_SCRIPT;
534 Element mElement_MESH;
535 Element mElement_PROGRAM_FRAGMENT;
536 Element mElement_PROGRAM_VERTEX;
537 Element mElement_PROGRAM_RASTER;
538 Element mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700539
Jason Sams3c0dfba2009-09-27 17:50:38 -0700540 Element mElement_A_8;
541 Element mElement_RGB_565;
542 Element mElement_RGB_888;
543 Element mElement_RGBA_5551;
544 Element mElement_RGBA_4444;
545 Element mElement_RGBA_8888;
546
Jason Sams8cb39de2010-06-01 15:47:01 -0700547 Element mElement_FLOAT_2;
548 Element mElement_FLOAT_3;
549 Element mElement_FLOAT_4;
550 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -0800551
Jason Sams1d45c472010-08-25 14:31:48 -0700552 Element mElement_MATRIX_4X4;
553 Element mElement_MATRIX_3X3;
554 Element mElement_MATRIX_2X2;
555
Jason Sams4d339932010-05-11 14:03:58 -0700556 Sampler mSampler_CLAMP_NEAREST;
557 Sampler mSampler_CLAMP_LINEAR;
558 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
559 Sampler mSampler_WRAP_NEAREST;
560 Sampler mSampler_WRAP_LINEAR;
561 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
562
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700563 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
564 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700565 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
566 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700567
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700568 ProgramRaster mProgramRaster_CULL_BACK;
569 ProgramRaster mProgramRaster_CULL_FRONT;
570 ProgramRaster mProgramRaster_CULL_NONE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700571
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700572 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700573 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700574
Jason Sams27676fe2010-11-10 17:00:59 -0800575 /**
576 * Base class application should derive from for handling RS messages
577 * comming from their scripts. When a script calls sendToClient the data
578 * fields will be filled in and then the run method called by a message
579 * handling thread. This will occur some time after sendToClient completes
580 * in the script.
581 *
582 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800583 public static class RSMessageHandler implements Runnable {
Jason Sams516c3192009-10-06 13:58:47 -0700584 protected int[] mData;
585 protected int mID;
Jason Sams1c415172010-11-08 17:06:46 -0800586 protected int mLength;
Jason Sams516c3192009-10-06 13:58:47 -0700587 public void run() {
588 }
589 }
Jason Sams27676fe2010-11-10 17:00:59 -0800590 /**
591 * If an application is expecting messages it should set this field to an
592 * instance of RSMessage. This instance will receive all the user messages
593 * sent from sendToClient by scripts from this context.
594 *
595 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800596 RSMessageHandler mMessageCallback = null;
597
598 public void setMessageHandler(RSMessageHandler msg) {
599 mMessageCallback = msg;
600 }
601 public RSMessageHandler getMessageHandler() {
602 return mMessageCallback;
603 }
Jason Sams516c3192009-10-06 13:58:47 -0700604
Jason Sams27676fe2010-11-10 17:00:59 -0800605 /**
606 * Runtime error base class. An application should derive from this class
607 * if it wishes to install an error handler. When errors occur at runtime
608 * the fields in this class will be filled and the run method called.
609 *
610 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800611 public static class RSErrorHandler implements Runnable {
Jason Sams1c415172010-11-08 17:06:46 -0800612 protected String mErrorMessage;
613 protected int mErrorNum;
614 public void run() {
615 }
616 }
Jason Sams27676fe2010-11-10 17:00:59 -0800617
618 /**
619 * Application Error handler. All runtime errors will be dispatched to the
620 * instance of RSAsyncError set here. If this field is null a
621 * RSRuntimeException will instead be thrown with details about the error.
622 * This will cause program termaination.
623 *
624 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800625 RSErrorHandler mErrorCallback = null;
626
627 public void setErrorHandler(RSErrorHandler msg) {
628 mErrorCallback = msg;
629 }
630 public RSErrorHandler getErrorHandler() {
631 return mErrorCallback;
632 }
Jason Sams1c415172010-11-08 17:06:46 -0800633
Jason Sams27676fe2010-11-10 17:00:59 -0800634 /**
635 * RenderScript worker threads priority enumeration. The default value is
636 * NORMAL. Applications wishing to do background processing such as
637 * wallpapers should set their priority to LOW to avoid starving forground
638 * processes.
639 */
Jason Sams7d787b42009-11-15 12:14:26 -0800640 public enum Priority {
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800641 // Remap these numbers to opaque...
Jason Sams7d787b42009-11-15 12:14:26 -0800642 LOW (5), //ANDROID_PRIORITY_BACKGROUND + 5
643 NORMAL (-4); //ANDROID_PRIORITY_DISPLAY
644
645 int mID;
646 Priority(int id) {
647 mID = id;
648 }
649 }
650
Jason Sams771bebb2009-12-07 12:40:12 -0800651 void validate() {
652 if (mContext == 0) {
Jason Samsc1d62102010-11-04 14:32:19 -0700653 throw new RSInvalidStateException("Calling RS with no Context active.");
Jason Sams771bebb2009-12-07 12:40:12 -0800654 }
655 }
656
Jason Sams27676fe2010-11-10 17:00:59 -0800657
658 /**
659 * Change the priority of the worker threads for this context.
660 *
661 * @param p New priority to be set.
662 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800663 public void setPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800664 validate();
Jason Sams7d787b42009-11-15 12:14:26 -0800665 nContextSetPriority(p.mID);
666 }
667
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800668 static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -0700669 RenderScript mRS;
670 boolean mRun = true;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800671 int[] mAuxData = new int[2];
Jason Sams1c415172010-11-08 17:06:46 -0800672
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800673 static final int RS_MESSAGE_TO_CLIENT_NONE = 0;
674 static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1;
675 static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
676 static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
677 static final int RS_MESSAGE_TO_CLIENT_USER = 4;
Jason Sams516c3192009-10-06 13:58:47 -0700678
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800679 static final int RS_ERROR_FATAL_UNKNOWN = 0x1000;
Jason Samsadd9d962010-11-22 16:20:16 -0800680
Jason Sams516c3192009-10-06 13:58:47 -0700681 MessageThread(RenderScript rs) {
682 super("RSMessageThread");
683 mRS = rs;
684
685 }
686
687 public void run() {
688 // This function is a temporary solution. The final solution will
689 // used typed allocations where the message id is the type indicator.
690 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -0700691 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700692 while(mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -0700693 rbuf[0] = 0;
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800694 int msg = mRS.nContextPeekMessage(mRS.mContext, mAuxData, true);
695 int size = mAuxData[1];
696 int subID = mAuxData[0];
Jason Sams1c415172010-11-08 17:06:46 -0800697
698 if (msg == RS_MESSAGE_TO_CLIENT_USER) {
699 if ((size>>2) >= rbuf.length) {
700 rbuf = new int[(size + 3) >> 2];
701 }
702 mRS.nContextGetUserMessage(mRS.mContext, rbuf);
703
704 if(mRS.mMessageCallback != null) {
705 mRS.mMessageCallback.mData = rbuf;
706 mRS.mMessageCallback.mID = subID;
707 mRS.mMessageCallback.mLength = size;
708 mRS.mMessageCallback.run();
Jason Sams1d45c472010-08-25 14:31:48 -0700709 } else {
Jason Sams1c415172010-11-08 17:06:46 -0800710 throw new RSInvalidStateException("Received a message from the script with no message handler installed.");
Jason Sams516c3192009-10-06 13:58:47 -0700711 }
Stephen Hinesab98bb62010-09-24 14:38:30 -0700712 continue;
Jason Sams516c3192009-10-06 13:58:47 -0700713 }
Jason Sams1c415172010-11-08 17:06:46 -0800714
715 if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
716 String e = mRS.nContextGetErrorMessage(mRS.mContext);
717
Jason Samsadd9d962010-11-22 16:20:16 -0800718 if (subID >= RS_ERROR_FATAL_UNKNOWN) {
719 throw new RSRuntimeException("Fatal error " + subID + ", details: " + e);
720 }
721
Jason Sams1c415172010-11-08 17:06:46 -0800722 if(mRS.mErrorCallback != null) {
723 mRS.mErrorCallback.mErrorMessage = e;
724 mRS.mErrorCallback.mErrorNum = subID;
725 mRS.mErrorCallback.run();
726 } else {
727 //throw new RSRuntimeException("Received error num " + subID + ", details: " + e);
728 }
729 continue;
730 }
731
732 // 2: teardown.
733 // But we want to avoid starving other threads during
734 // teardown by yielding until the next line in the destructor
735 // can execute to set mRun = false
736 try {
737 sleep(1, 0);
738 } catch(InterruptedException e) {
Jason Sams516c3192009-10-06 13:58:47 -0700739 }
Jason Sams516c3192009-10-06 13:58:47 -0700740 }
Jason Sams3bc47d42009-11-12 15:10:25 -0800741 Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -0700742 }
743 }
744
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800745 RenderScript(Context ctx) {
746 mApplicationContext = ctx.getApplicationContext();
747 }
748
749 /**
750 * Gets the application context associated with the RenderScript context.
751 *
752 * @return The application context.
753 */
754 public final Context getApplicationContext() {
755 return mApplicationContext;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700756 }
757
Jason Sams27676fe2010-11-10 17:00:59 -0800758 /**
759 * Create a basic RenderScript context.
760 *
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800761 * @param ctx The context.
Jason Sams27676fe2010-11-10 17:00:59 -0800762 * @return RenderScript
763 */
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800764 public static RenderScript create(Context ctx) {
765 RenderScript rs = new RenderScript(ctx);
Jason Sams704ff642010-02-09 16:05:07 -0800766
767 rs.mDev = rs.nDeviceCreate();
768 rs.mContext = rs.nContextCreate(rs.mDev, 0);
769 rs.mMessageThread = new MessageThread(rs);
770 rs.mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -0800771 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800772 }
773
Jason Sams27676fe2010-11-10 17:00:59 -0800774 /**
775 * Print the currently available debugging information about the state of
776 * the RS context to the log.
777 *
Jason Sams27676fe2010-11-10 17:00:59 -0800778 */
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800779 public void contextDump() {
Jason Sams5dbfe932010-01-27 14:41:43 -0800780 validate();
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800781 nContextDump(0);
Jason Sams715333b2009-11-17 17:26:46 -0800782 }
783
Jason Sams27676fe2010-11-10 17:00:59 -0800784 /**
785 * Wait for any commands in the fifo between the java bindings and native to
786 * be processed.
787 *
788 */
Jason Sams96ed4cf2010-06-15 12:15:57 -0700789 public void finish() {
790 nContextFinish();
791 }
792
Jason Sams27676fe2010-11-10 17:00:59 -0800793 /**
794 * Destroy this renderscript context. Once this function is called its no
795 * longer legal to use this or any objects created by this context.
796 *
797 */
Jason Samsf5b45962009-08-25 14:49:07 -0700798 public void destroy() {
Jason Sams5dbfe932010-01-27 14:41:43 -0800799 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700800 nContextDeinitToClient(mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700801 mMessageThread.mRun = false;
Jason Samsa8bf9422010-09-16 13:43:19 -0700802 try {
803 mMessageThread.join();
804 } catch(InterruptedException e) {
805 }
Jason Sams516c3192009-10-06 13:58:47 -0700806
Jason Sams2e1872f2010-08-17 16:25:41 -0700807 nContextDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -0700808 mContext = 0;
809
810 nDeviceDestroy(mDev);
811 mDev = 0;
812 }
Jason Sams02fb2cb2009-05-28 15:37:57 -0700813
Jason Samsa9e7a052009-09-25 14:51:22 -0700814 boolean isAlive() {
815 return mContext != 0;
816 }
817
Jason Samsbf6ef8d72010-12-06 15:59:59 -0800818 int safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -0700819 if(o != null) {
Jason Sams06d69de2010-11-09 17:11:40 -0800820 return o.getID();
Jason Samsd8e41612009-08-20 17:22:40 -0700821 }
Jason Sams6b9dec02009-09-23 16:38:37 -0700822 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700823 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700824}