blob: 3fa99657d003f9584e9b9e15003ab733d845eaa0 [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
Jason Samsb8c5a842009-07-31 20:40:47 -070021import android.graphics.Bitmap;
Romain Guy650a3eb2009-08-31 14:06:43 -070022import android.graphics.BitmapFactory;
Jason Sams36e612a2009-07-31 16:26:13 -070023import android.util.Config;
24import android.util.Log;
25import android.view.Surface;
Jack Palevich43702d82009-05-28 13:38:16 -070026
Jack Palevich60aa3ea2009-05-26 13:45:08 -070027
Jason Samse29d4712009-07-23 15:19:03 -070028/**
29 * @hide
30 *
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 Samsbf6ef8d2010-12-06 15:59:59 -080041 static final boolean DEBUG = false;
Romain Guy650a3eb2009-08-31 14:06:43 -070042 @SuppressWarnings({"UnusedDeclaration", "deprecation"})
Jason Samsbf6ef8d2010-12-06 15:59:59 -080043 static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
Jack Palevich60aa3ea2009-05-26 13:45:08 -070044
45
46
Jason Sams02fb2cb2009-05-28 15:37:57 -070047 /*
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 Samsbf6ef8d2010-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) {
63 Log.d(LOG_TAG, "RenderScript JNI library not found!");
64 }
65 }
66
Jason Sams2e1872f2010-08-17 16:25:41 -070067 // Non-threadsafe functions.
Jason Sams36e612a2009-07-31 16:26:13 -070068 native int nDeviceCreate();
69 native void nDeviceDestroy(int dev);
Jason Samsebfb4362009-09-23 13:57:02 -070070 native void nDeviceSetConfig(int dev, int param, int value);
Jason Sams1c415172010-11-08 17:06:46 -080071 native void nContextGetUserMessage(int con, int[] data);
72 native String nContextGetErrorMessage(int con);
73 native int nContextPeekMessage(int con, int[] subID, boolean wait);
Jason Sams2e1872f2010-08-17 16:25:41 -070074 native void nContextInitToClient(int con);
75 native void nContextDeinitToClient(int con);
Jason Sams3eaa3382009-06-10 15:04:38 -070076
Jason Sams718cd1f2009-12-23 14:35:29 -080077
Jason Sams2e1872f2010-08-17 16:25:41 -070078 // Methods below are wrapped to protect the non-threadsafe
79 // lockless fifo.
Jason Sams11c8af92010-10-13 15:31:10 -070080 native int rsnContextCreateGL(int dev, int ver,
81 int colorMin, int colorPref,
82 int alphaMin, int alphaPref,
83 int depthMin, int depthPref,
84 int stencilMin, int stencilPref,
85 int samplesMin, int samplesPref, float samplesQ);
86 synchronized int nContextCreateGL(int dev, int ver,
87 int colorMin, int colorPref,
88 int alphaMin, int alphaPref,
89 int depthMin, int depthPref,
90 int stencilMin, int stencilPref,
91 int samplesMin, int samplesPref, float samplesQ) {
92 return rsnContextCreateGL(dev, ver, colorMin, colorPref,
93 alphaMin, alphaPref, depthMin, depthPref,
94 stencilMin, stencilPref,
95 samplesMin, samplesPref, samplesQ);
Jason Sams2e1872f2010-08-17 16:25:41 -070096 }
97 native int rsnContextCreate(int dev, int ver);
98 synchronized int nContextCreate(int dev, int ver) {
99 return rsnContextCreate(dev, ver);
100 }
101 native void rsnContextDestroy(int con);
102 synchronized void nContextDestroy() {
103 rsnContextDestroy(mContext);
104 }
105 native void rsnContextSetSurface(int con, int w, int h, Surface sur);
106 synchronized void nContextSetSurface(int w, int h, Surface sur) {
107 rsnContextSetSurface(mContext, w, h, sur);
108 }
109 native void rsnContextSetPriority(int con, int p);
110 synchronized void nContextSetPriority(int p) {
111 rsnContextSetPriority(mContext, p);
112 }
113 native void rsnContextDump(int con, int bits);
114 synchronized void nContextDump(int bits) {
115 rsnContextDump(mContext, bits);
116 }
117 native void rsnContextFinish(int con);
118 synchronized void nContextFinish() {
119 rsnContextFinish(mContext);
120 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700121
Jason Sams2e1872f2010-08-17 16:25:41 -0700122 native void rsnContextBindRootScript(int con, int script);
123 synchronized void nContextBindRootScript(int script) {
124 rsnContextBindRootScript(mContext, script);
125 }
126 native void rsnContextBindSampler(int con, int sampler, int slot);
127 synchronized void nContextBindSampler(int sampler, int slot) {
128 rsnContextBindSampler(mContext, sampler, slot);
129 }
130 native void rsnContextBindProgramStore(int con, int pfs);
131 synchronized void nContextBindProgramStore(int pfs) {
132 rsnContextBindProgramStore(mContext, pfs);
133 }
134 native void rsnContextBindProgramFragment(int con, int pf);
135 synchronized void nContextBindProgramFragment(int pf) {
136 rsnContextBindProgramFragment(mContext, pf);
137 }
138 native void rsnContextBindProgramVertex(int con, int pv);
139 synchronized void nContextBindProgramVertex(int pv) {
140 rsnContextBindProgramVertex(mContext, pv);
141 }
142 native void rsnContextBindProgramRaster(int con, int pr);
143 synchronized void nContextBindProgramRaster(int pr) {
144 rsnContextBindProgramRaster(mContext, pr);
145 }
146 native void rsnContextPause(int con);
147 synchronized void nContextPause() {
148 rsnContextPause(mContext);
149 }
150 native void rsnContextResume(int con);
151 synchronized void nContextResume() {
152 rsnContextResume(mContext);
153 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700154
Jason Sams2e1872f2010-08-17 16:25:41 -0700155 native void rsnAssignName(int con, int obj, byte[] name);
156 synchronized void nAssignName(int obj, byte[] name) {
157 rsnAssignName(mContext, obj, name);
158 }
159 native String rsnGetName(int con, int obj);
160 synchronized String nGetName(int obj) {
161 return rsnGetName(mContext, obj);
162 }
163 native void rsnObjDestroy(int con, int id);
164 synchronized void nObjDestroy(int id) {
165 rsnObjDestroy(mContext, id);
166 }
Jason Samsfe08d992009-05-27 14:45:32 -0700167
Jason Sams2e1872f2010-08-17 16:25:41 -0700168 native int rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
169 synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
170 return rsnElementCreate(mContext, type, kind, norm, vecSize);
171 }
Jason Sams70d4e502010-09-02 17:35:23 -0700172 native int rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
173 synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
174 return rsnElementCreate2(mContext, elements, names, arraySizes);
Jason Sams2e1872f2010-08-17 16:25:41 -0700175 }
176 native void rsnElementGetNativeData(int con, int id, int[] elementData);
177 synchronized void nElementGetNativeData(int id, int[] elementData) {
178 rsnElementGetNativeData(mContext, id, elementData);
179 }
180 native void rsnElementGetSubElements(int con, int id, int[] IDs, String[] names);
181 synchronized void nElementGetSubElements(int id, int[] IDs, String[] names) {
182 rsnElementGetSubElements(mContext, id, IDs, names);
183 }
Jason Sams768bc022009-09-21 19:41:04 -0700184
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800185 native int rsnTypeCreate(int con, int eid, int x, int y, int z, boolean mips, boolean faces);
186 synchronized int nTypeCreate(int eid, int x, int y, int z, boolean mips, boolean faces) {
187 return rsnTypeCreate(mContext, eid, x, y, z, mips, faces);
Jason Sams2e1872f2010-08-17 16:25:41 -0700188 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700189 native void rsnTypeGetNativeData(int con, int id, int[] typeData);
190 synchronized void nTypeGetNativeData(int id, int[] typeData) {
191 rsnTypeGetNativeData(mContext, id, typeData);
192 }
Jason Sams768bc022009-09-21 19:41:04 -0700193
Jason Sams5476b452010-12-08 16:14:36 -0800194 native int rsnAllocationCreateTyped(int con, int type, int usage);
195 synchronized int nAllocationCreateTyped(int type, int usage) {
196 return rsnAllocationCreateTyped(mContext, type, usage);
Jason Sams2e1872f2010-08-17 16:25:41 -0700197 }
Jason Sams5476b452010-12-08 16:14:36 -0800198 native int rsnAllocationCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
199 synchronized int nAllocationCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
200 return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk26ae3902010-10-11 12:35:15 -0700201 }
Jason Sams5476b452010-12-08 16:14:36 -0800202 native int rsnAllocationCubeCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
203 synchronized int nAllocationCubeCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
204 return rsnAllocationCubeCreateFromBitmap(mContext, type, mip, bmp, usage);
Alex Sakhartchouk67f2e442010-11-18 15:22:43 -0800205 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700206 native int rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
207 synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
208 return rsnAllocationCreateBitmapRef(mContext, type, bmp);
209 }
Jason Sams5476b452010-12-08 16:14:36 -0800210 native int rsnAllocationCreateFromAssetStream(int con, int mips, int assetStream, int usage);
211 synchronized int nAllocationCreateFromAssetStream(int mips, int assetStream, int usage) {
212 return rsnAllocationCreateFromAssetStream(mContext, mips, assetStream, usage);
213 }
214
Jason Sams4ef66502010-12-10 16:03:15 -0800215 native void rsnAllocationCopyToBitmap(int con, int alloc, Bitmap bmp);
216 synchronized void nAllocationCopyToBitmap(int alloc, Bitmap bmp) {
217 rsnAllocationCopyToBitmap(mContext, alloc, bmp);
218 }
219
220
Jason Sams5476b452010-12-08 16:14:36 -0800221 native void rsnAllocationSyncAll(int con, int alloc, int src);
222 synchronized void nAllocationSyncAll(int alloc, int src) {
223 rsnAllocationSyncAll(mContext, alloc, src);
224 }
Jason Sams4ef66502010-12-10 16:03:15 -0800225 native void rsnAllocationCopyFromBitmap(int con, int alloc, Bitmap bmp);
226 synchronized void nAllocationCopyFromBitmap(int alloc, Bitmap bmp) {
227 rsnAllocationCopyFromBitmap(mContext, alloc, bmp);
Jason Sams2e1872f2010-08-17 16:25:41 -0700228 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700229
Jason Sams2e1872f2010-08-17 16:25:41 -0700230 native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
231 synchronized void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel) {
232 rsnAllocationUploadToTexture(mContext, alloc, genMips, baseMioLevel);
233 }
234 native void rsnAllocationUploadToBufferObject(int con, int alloc);
235 synchronized void nAllocationUploadToBufferObject(int alloc) {
236 rsnAllocationUploadToBufferObject(mContext, alloc);
237 }
Alex Sakhartchoukaae74ad2010-06-04 10:06:50 -0700238
Jason Sams2e1872f2010-08-17 16:25:41 -0700239 native void rsnAllocationSubData1D(int con, int id, int off, int count, int[] d, int sizeBytes);
240 synchronized void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes) {
241 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
242 }
243 native void rsnAllocationSubData1D(int con, int id, int off, int count, short[] d, int sizeBytes);
244 synchronized void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes) {
245 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
246 }
247 native void rsnAllocationSubData1D(int con, int id, int off, int count, byte[] d, int sizeBytes);
248 synchronized void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes) {
249 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
250 }
Jason Sams49bdaf02010-08-31 13:50:42 -0700251 native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
252 synchronized void nAllocationSubElementData1D(int id, int xoff, int compIdx, byte[] d, int sizeBytes) {
253 rsnAllocationSubElementData1D(mContext, id, xoff, compIdx, d, sizeBytes);
254 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700255 native void rsnAllocationSubData1D(int con, int id, int off, int count, float[] d, int sizeBytes);
256 synchronized void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes) {
257 rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
258 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700259
Jason Sams2e1872f2010-08-17 16:25:41 -0700260 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
261 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes) {
262 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
263 }
264 native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
265 synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes) {
266 rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
267 }
268 native void rsnAllocationRead(int con, int id, int[] d);
269 synchronized void nAllocationRead(int id, int[] d) {
270 rsnAllocationRead(mContext, id, d);
271 }
272 native void rsnAllocationRead(int con, int id, float[] d);
273 synchronized void nAllocationRead(int id, float[] d) {
274 rsnAllocationRead(mContext, id, d);
275 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700276 native int rsnAllocationGetType(int con, int id);
277 synchronized int nAllocationGetType(int id) {
278 return rsnAllocationGetType(mContext, id);
279 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700280
Jason Sams5edc6082010-10-05 13:32:49 -0700281 native void rsnAllocationResize1D(int con, int id, int dimX);
282 synchronized void nAllocationResize1D(int id, int dimX) {
283 rsnAllocationResize1D(mContext, id, dimX);
284 }
285 native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
286 synchronized void nAllocationResize2D(int id, int dimX, int dimY) {
287 rsnAllocationResize2D(mContext, id, dimX, dimY);
288 }
289
Jason Sams2e1872f2010-08-17 16:25:41 -0700290 native int rsnFileA3DCreateFromAssetStream(int con, int assetStream);
291 synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
292 return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
293 }
294 native int rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
295 synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
296 return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
297 }
298 native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
299 synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
300 rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
301 }
302 native int rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
303 synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
304 return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
305 }
Jason Samsbd1c3ad2009-08-03 16:03:08 -0700306
Jason Sams2e1872f2010-08-17 16:25:41 -0700307 native int rsnFontCreateFromFile(int con, String fileName, int size, int dpi);
308 synchronized int nFontCreateFromFile(String fileName, int size, int dpi) {
309 return rsnFontCreateFromFile(mContext, fileName, size, dpi);
310 }
Jason Sams22534172009-08-04 16:58:20 -0700311
Jason Sams2e1872f2010-08-17 16:25:41 -0700312 native void rsnAdapter1DBindAllocation(int con, int ad, int alloc);
313 synchronized void nAdapter1DBindAllocation(int ad, int alloc) {
314 rsnAdapter1DBindAllocation(mContext, ad, alloc);
315 }
316 native void rsnAdapter1DSetConstraint(int con, int ad, int dim, int value);
317 synchronized void nAdapter1DSetConstraint(int ad, int dim, int value) {
318 rsnAdapter1DSetConstraint(mContext, ad, dim, value);
319 }
320 native void rsnAdapter1DData(int con, int ad, int[] d);
321 synchronized void nAdapter1DData(int ad, int[] d) {
322 rsnAdapter1DData(mContext, ad, d);
323 }
324 native void rsnAdapter1DData(int con, int ad, float[] d);
325 synchronized void nAdapter1DData(int ad, float[] d) {
326 rsnAdapter1DData(mContext, ad, d);
327 }
328 native void rsnAdapter1DSubData(int con, int ad, int off, int count, int[] d);
329 synchronized void nAdapter1DSubData(int ad, int off, int count, int[] d) {
330 rsnAdapter1DSubData(mContext, ad, off, count, d);
331 }
332 native void rsnAdapter1DSubData(int con, int ad, int off, int count, float[] d);
333 synchronized void nAdapter1DSubData(int ad, int off, int count, float[] d) {
334 rsnAdapter1DSubData(mContext, ad, off, count, d);
335 }
336 native int rsnAdapter1DCreate(int con);
337 synchronized int nAdapter1DCreate() {
338 return rsnAdapter1DCreate(mContext);
339 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700340
Jason Sams2e1872f2010-08-17 16:25:41 -0700341 native void rsnAdapter2DBindAllocation(int con, int ad, int alloc);
342 synchronized void nAdapter2DBindAllocation(int ad, int alloc) {
343 rsnAdapter2DBindAllocation(mContext, ad, alloc);
344 }
345 native void rsnAdapter2DSetConstraint(int con, int ad, int dim, int value);
346 synchronized void nAdapter2DSetConstraint(int ad, int dim, int value) {
347 rsnAdapter2DSetConstraint(mContext, ad, dim, value);
348 }
349 native void rsnAdapter2DData(int con, int ad, int[] d);
350 synchronized void nAdapter2DData(int ad, int[] d) {
351 rsnAdapter2DData(mContext, ad, d);
352 }
353 native void rsnAdapter2DData(int con, int ad, float[] d);
354 synchronized void nAdapter2DData(int ad, float[] d) {
355 rsnAdapter2DData(mContext, ad, d);
356 }
357 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, int[] d);
358 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d) {
359 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
360 }
361 native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, float[] d);
362 synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d) {
363 rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
364 }
365 native int rsnAdapter2DCreate(int con);
366 synchronized int nAdapter2DCreate() {
367 return rsnAdapter2DCreate(mContext);
368 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700369
Jason Sams2e1872f2010-08-17 16:25:41 -0700370 native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
371 synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
372 rsnScriptBindAllocation(mContext, script, alloc, slot);
373 }
374 native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
375 synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
376 rsnScriptSetTimeZone(mContext, script, timeZone);
377 }
378 native void rsnScriptInvoke(int con, int id, int slot);
379 synchronized void nScriptInvoke(int id, int slot) {
380 rsnScriptInvoke(mContext, id, slot);
381 }
382 native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
383 synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
384 rsnScriptInvokeV(mContext, id, slot, params);
385 }
386 native void rsnScriptSetVarI(int con, int id, int slot, int val);
387 synchronized void nScriptSetVarI(int id, int slot, int val) {
388 rsnScriptSetVarI(mContext, id, slot, val);
389 }
Stephen Hines031ec58c2010-10-11 10:54:21 -0700390 native void rsnScriptSetVarJ(int con, int id, int slot, long val);
391 synchronized void nScriptSetVarJ(int id, int slot, long val) {
392 rsnScriptSetVarJ(mContext, id, slot, val);
393 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700394 native void rsnScriptSetVarF(int con, int id, int slot, float val);
395 synchronized void nScriptSetVarF(int id, int slot, float val) {
396 rsnScriptSetVarF(mContext, id, slot, val);
397 }
Stephen Hinesca54ec32010-09-20 17:20:30 -0700398 native void rsnScriptSetVarD(int con, int id, int slot, double val);
399 synchronized void nScriptSetVarD(int id, int slot, double val) {
400 rsnScriptSetVarD(mContext, id, slot, val);
401 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700402 native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
403 synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
404 rsnScriptSetVarV(mContext, id, slot, val);
405 }
Jason Sams6f4cf0b2010-11-16 17:37:02 -0800406 native void rsnScriptSetVarObj(int con, int id, int slot, int val);
407 synchronized void nScriptSetVarObj(int id, int slot, int val) {
408 rsnScriptSetVarObj(mContext, id, slot, val);
409 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700410
Jason Sams2e1872f2010-08-17 16:25:41 -0700411 native void rsnScriptCBegin(int con);
412 synchronized void nScriptCBegin() {
413 rsnScriptCBegin(mContext);
414 }
415 native void rsnScriptCSetScript(int con, byte[] script, int offset, int length);
416 synchronized void nScriptCSetScript(byte[] script, int offset, int length) {
417 rsnScriptCSetScript(mContext, script, offset, length);
418 }
Shih-wei Liaoa914f342010-11-08 01:33:59 -0800419 native int rsnScriptCCreate(int con, String val);
420 synchronized int nScriptCCreate(String val) {
421 return rsnScriptCCreate(mContext, val);
Jason Sams2e1872f2010-08-17 16:25:41 -0700422 }
Jason Samsebfb4362009-09-23 13:57:02 -0700423
Jason Sams2e1872f2010-08-17 16:25:41 -0700424 native void rsnSamplerBegin(int con);
425 synchronized void nSamplerBegin() {
426 rsnSamplerBegin(mContext);
427 }
428 native void rsnSamplerSet(int con, int param, int value);
429 synchronized void nSamplerSet(int param, int value) {
430 rsnSamplerSet(mContext, param, value);
431 }
Alex Sakhartchoukf5b35102010-09-30 11:36:37 -0700432 native void rsnSamplerSet2(int con, int param, float value);
433 synchronized void nSamplerSet2(int param, float value) {
434 rsnSamplerSet2(mContext, param, value);
435 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700436 native int rsnSamplerCreate(int con);
437 synchronized int nSamplerCreate() {
438 return rsnSamplerCreate(mContext);
439 }
Jason Sams0011bcf2009-12-15 12:58:36 -0800440
Jason Sams2e1872f2010-08-17 16:25:41 -0700441 native void rsnProgramStoreBegin(int con, int in, int out);
442 synchronized void nProgramStoreBegin(int in, int out) {
443 rsnProgramStoreBegin(mContext, in, out);
444 }
445 native void rsnProgramStoreDepthFunc(int con, int func);
446 synchronized void nProgramStoreDepthFunc(int func) {
447 rsnProgramStoreDepthFunc(mContext, func);
448 }
449 native void rsnProgramStoreDepthMask(int con, boolean enable);
450 synchronized void nProgramStoreDepthMask(boolean enable) {
451 rsnProgramStoreDepthMask(mContext, enable);
452 }
453 native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a);
454 synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
455 rsnProgramStoreColorMask(mContext, r, g, b, a);
456 }
457 native void rsnProgramStoreBlendFunc(int con, int src, int dst);
458 synchronized void nProgramStoreBlendFunc(int src, int dst) {
459 rsnProgramStoreBlendFunc(mContext, src, dst);
460 }
461 native void rsnProgramStoreDither(int con, boolean enable);
462 synchronized void nProgramStoreDither(boolean enable) {
463 rsnProgramStoreDither(mContext, enable);
464 }
465 native int rsnProgramStoreCreate(int con);
466 synchronized int nProgramStoreCreate() {
467 return rsnProgramStoreCreate(mContext);
468 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700469
Jason Sams2e1872f2010-08-17 16:25:41 -0700470 native int rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
471 synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) {
472 return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite);
473 }
474 native void rsnProgramRasterSetLineWidth(int con, int pr, float v);
475 synchronized void nProgramRasterSetLineWidth(int pr, float v) {
476 rsnProgramRasterSetLineWidth(mContext, pr, v);
477 }
478 native void rsnProgramRasterSetCullMode(int con, int pr, int mode);
479 synchronized void nProgramRasterSetCullMode(int pr, int mode) {
480 rsnProgramRasterSetCullMode(mContext, pr, mode);
481 }
Jason Sams1fe9b8c2009-06-11 14:46:10 -0700482
Jason Sams2e1872f2010-08-17 16:25:41 -0700483 native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
484 synchronized void nProgramBindConstants(int pv, int slot, int mID) {
485 rsnProgramBindConstants(mContext, pv, slot, mID);
486 }
487 native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
488 synchronized void nProgramBindTexture(int vpf, int slot, int a) {
489 rsnProgramBindTexture(mContext, vpf, slot, a);
490 }
491 native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
492 synchronized void nProgramBindSampler(int vpf, int slot, int s) {
493 rsnProgramBindSampler(mContext, vpf, slot, s);
494 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700495 native int rsnProgramFragmentCreate(int con, String shader, int[] params);
496 synchronized int nProgramFragmentCreate(String shader, int[] params) {
497 return rsnProgramFragmentCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700498 }
Alex Sakhartchoukb89aaac2010-09-23 16:16:33 -0700499 native int rsnProgramVertexCreate(int con, String shader, int[] params);
500 synchronized int nProgramVertexCreate(String shader, int[] params) {
501 return rsnProgramVertexCreate(mContext, shader, params);
Jason Sams2e1872f2010-08-17 16:25:41 -0700502 }
Alex Sakhartchouk164aaed2010-07-01 16:14:06 -0700503
Jason Sams2e1872f2010-08-17 16:25:41 -0700504 native int rsnMeshCreate(int con, int vtxCount, int indexCount);
505 synchronized int nMeshCreate(int vtxCount, int indexCount) {
506 return rsnMeshCreate(mContext, vtxCount, indexCount);
507 }
508 native void rsnMeshBindVertex(int con, int id, int alloc, int slot);
509 synchronized void nMeshBindVertex(int id, int alloc, int slot) {
510 rsnMeshBindVertex(mContext, id, alloc, slot);
511 }
512 native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot);
513 synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) {
514 rsnMeshBindIndex(mContext, id, alloc, prim, slot);
515 }
Alex Sakhartchouk9d71e212010-11-08 15:10:52 -0800516 native void rsnMeshInitVertexAttribs(int con, int id);
517 synchronized void nMeshInitVertexAttribs(int id) {
518 rsnMeshInitVertexAttribs(mContext, id);
519 }
Jason Sams2e1872f2010-08-17 16:25:41 -0700520 native int rsnMeshGetVertexBufferCount(int con, int id);
521 synchronized int nMeshGetVertexBufferCount(int id) {
522 return rsnMeshGetVertexBufferCount(mContext, id);
523 }
524 native int rsnMeshGetIndexCount(int con, int id);
525 synchronized int nMeshGetIndexCount(int id) {
526 return rsnMeshGetIndexCount(mContext, id);
527 }
528 native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
529 synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
530 rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
531 }
532 native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
533 synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
534 rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
535 }
536
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700537
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800538 int mDev;
539 int mContext;
Romain Guy650a3eb2009-08-31 14:06:43 -0700540 @SuppressWarnings({"FieldCanBeLocal"})
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800541 MessageThread mMessageThread;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700542
Jason Sams8cb39de2010-06-01 15:47:01 -0700543 Element mElement_U8;
544 Element mElement_I8;
545 Element mElement_U16;
546 Element mElement_I16;
547 Element mElement_U32;
548 Element mElement_I32;
Stephen Hines52d83632010-10-11 16:10:42 -0700549 Element mElement_U64;
Stephen Hinesef1dac22010-10-01 15:39:33 -0700550 Element mElement_I64;
Jason Sams8cb39de2010-06-01 15:47:01 -0700551 Element mElement_F32;
Stephen Hines02f417052010-09-30 15:19:22 -0700552 Element mElement_F64;
Jason Samsf110d4b2010-06-21 17:42:41 -0700553 Element mElement_BOOLEAN;
Jason Sams3c0dfba2009-09-27 17:50:38 -0700554
Jason Sams8cb39de2010-06-01 15:47:01 -0700555 Element mElement_ELEMENT;
556 Element mElement_TYPE;
557 Element mElement_ALLOCATION;
558 Element mElement_SAMPLER;
559 Element mElement_SCRIPT;
560 Element mElement_MESH;
561 Element mElement_PROGRAM_FRAGMENT;
562 Element mElement_PROGRAM_VERTEX;
563 Element mElement_PROGRAM_RASTER;
564 Element mElement_PROGRAM_STORE;
Jason Samsa70f4162010-03-26 15:33:42 -0700565
Jason Sams3c0dfba2009-09-27 17:50:38 -0700566 Element mElement_A_8;
567 Element mElement_RGB_565;
568 Element mElement_RGB_888;
569 Element mElement_RGBA_5551;
570 Element mElement_RGBA_4444;
571 Element mElement_RGBA_8888;
572
Jason Sams8cb39de2010-06-01 15:47:01 -0700573 Element mElement_FLOAT_2;
574 Element mElement_FLOAT_3;
575 Element mElement_FLOAT_4;
576 Element mElement_UCHAR_4;
Jason Sams7d787b42009-11-15 12:14:26 -0800577
Jason Sams1d45c472010-08-25 14:31:48 -0700578 Element mElement_MATRIX_4X4;
579 Element mElement_MATRIX_3X3;
580 Element mElement_MATRIX_2X2;
581
Jason Sams4d339932010-05-11 14:03:58 -0700582 Sampler mSampler_CLAMP_NEAREST;
583 Sampler mSampler_CLAMP_LINEAR;
584 Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
585 Sampler mSampler_WRAP_NEAREST;
586 Sampler mSampler_WRAP_LINEAR;
587 Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
588
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700589 ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
590 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
591 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_TEST;
592 ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_WRITE;
593 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
594 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
595 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_TEST;
596 ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_WRITE;
597 ProgramStore mProgramStore_BLEND_ADD_DEPTH_TEST;
598 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_DEPTH;
599 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_TEST;
600 ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_WRITE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700601
Alex Sakhartchoukd36f2482010-08-24 11:37:33 -0700602 ProgramRaster mProgramRaster_CULL_BACK;
603 ProgramRaster mProgramRaster_CULL_FRONT;
604 ProgramRaster mProgramRaster_CULL_NONE;
Alex Sakhartchouk32e09b52010-08-23 10:24:10 -0700605
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700606 ///////////////////////////////////////////////////////////////////////////////////
Jack Palevich43702d82009-05-28 13:38:16 -0700607 //
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700608
Jason Sams27676fe2010-11-10 17:00:59 -0800609 /**
610 * Base class application should derive from for handling RS messages
611 * comming from their scripts. When a script calls sendToClient the data
612 * fields will be filled in and then the run method called by a message
613 * handling thread. This will occur some time after sendToClient completes
614 * in the script.
615 *
616 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800617 public static class RSMessageHandler implements Runnable {
Jason Sams516c3192009-10-06 13:58:47 -0700618 protected int[] mData;
619 protected int mID;
Jason Sams1c415172010-11-08 17:06:46 -0800620 protected int mLength;
Jason Sams516c3192009-10-06 13:58:47 -0700621 public void run() {
622 }
623 }
Jason Sams27676fe2010-11-10 17:00:59 -0800624 /**
625 * If an application is expecting messages it should set this field to an
626 * instance of RSMessage. This instance will receive all the user messages
627 * sent from sendToClient by scripts from this context.
628 *
629 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800630 RSMessageHandler mMessageCallback = null;
631
632 public void setMessageHandler(RSMessageHandler msg) {
633 mMessageCallback = msg;
634 }
635 public RSMessageHandler getMessageHandler() {
636 return mMessageCallback;
637 }
Jason Sams516c3192009-10-06 13:58:47 -0700638
Jason Sams27676fe2010-11-10 17:00:59 -0800639 /**
640 * Runtime error base class. An application should derive from this class
641 * if it wishes to install an error handler. When errors occur at runtime
642 * the fields in this class will be filled and the run method called.
643 *
644 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800645 public static class RSErrorHandler implements Runnable {
Jason Sams1c415172010-11-08 17:06:46 -0800646 protected String mErrorMessage;
647 protected int mErrorNum;
648 public void run() {
649 }
650 }
Jason Sams27676fe2010-11-10 17:00:59 -0800651
652 /**
653 * Application Error handler. All runtime errors will be dispatched to the
654 * instance of RSAsyncError set here. If this field is null a
655 * RSRuntimeException will instead be thrown with details about the error.
656 * This will cause program termaination.
657 *
658 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800659 RSErrorHandler mErrorCallback = null;
660
661 public void setErrorHandler(RSErrorHandler msg) {
662 mErrorCallback = msg;
663 }
664 public RSErrorHandler getErrorHandler() {
665 return mErrorCallback;
666 }
Jason Sams1c415172010-11-08 17:06:46 -0800667
Jason Sams27676fe2010-11-10 17:00:59 -0800668 /**
669 * RenderScript worker threads priority enumeration. The default value is
670 * NORMAL. Applications wishing to do background processing such as
671 * wallpapers should set their priority to LOW to avoid starving forground
672 * processes.
673 */
Jason Sams7d787b42009-11-15 12:14:26 -0800674 public enum Priority {
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800675 // Remap these numbers to opaque...
Jason Sams7d787b42009-11-15 12:14:26 -0800676 LOW (5), //ANDROID_PRIORITY_BACKGROUND + 5
677 NORMAL (-4); //ANDROID_PRIORITY_DISPLAY
678
679 int mID;
680 Priority(int id) {
681 mID = id;
682 }
683 }
684
Jason Sams771bebb2009-12-07 12:40:12 -0800685 void validate() {
686 if (mContext == 0) {
Jason Samsc1d62102010-11-04 14:32:19 -0700687 throw new RSInvalidStateException("Calling RS with no Context active.");
Jason Sams771bebb2009-12-07 12:40:12 -0800688 }
689 }
690
Jason Sams27676fe2010-11-10 17:00:59 -0800691
692 /**
693 * Change the priority of the worker threads for this context.
694 *
695 * @param p New priority to be set.
696 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800697 public void setPriority(Priority p) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800698 validate();
Jason Sams7d787b42009-11-15 12:14:26 -0800699 nContextSetPriority(p.mID);
700 }
701
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800702 static class MessageThread extends Thread {
Jason Sams516c3192009-10-06 13:58:47 -0700703 RenderScript mRS;
704 boolean mRun = true;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800705 int[] mAuxData = new int[2];
Jason Sams1c415172010-11-08 17:06:46 -0800706
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800707 static final int RS_MESSAGE_TO_CLIENT_NONE = 0;
708 static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1;
709 static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
710 static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
711 static final int RS_MESSAGE_TO_CLIENT_USER = 4;
Jason Sams516c3192009-10-06 13:58:47 -0700712
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800713 static final int RS_ERROR_FATAL_UNKNOWN = 0x1000;
Jason Samsadd9d962010-11-22 16:20:16 -0800714
Jason Sams516c3192009-10-06 13:58:47 -0700715 MessageThread(RenderScript rs) {
716 super("RSMessageThread");
717 mRS = rs;
718
719 }
720
721 public void run() {
722 // This function is a temporary solution. The final solution will
723 // used typed allocations where the message id is the type indicator.
724 int[] rbuf = new int[16];
Jason Sams2e1872f2010-08-17 16:25:41 -0700725 mRS.nContextInitToClient(mRS.mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700726 while(mRun) {
Jason Sams1d45c472010-08-25 14:31:48 -0700727 rbuf[0] = 0;
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800728 int msg = mRS.nContextPeekMessage(mRS.mContext, mAuxData, true);
729 int size = mAuxData[1];
730 int subID = mAuxData[0];
Jason Sams1c415172010-11-08 17:06:46 -0800731
732 if (msg == RS_MESSAGE_TO_CLIENT_USER) {
733 if ((size>>2) >= rbuf.length) {
734 rbuf = new int[(size + 3) >> 2];
735 }
736 mRS.nContextGetUserMessage(mRS.mContext, rbuf);
737
738 if(mRS.mMessageCallback != null) {
739 mRS.mMessageCallback.mData = rbuf;
740 mRS.mMessageCallback.mID = subID;
741 mRS.mMessageCallback.mLength = size;
742 mRS.mMessageCallback.run();
Jason Sams1d45c472010-08-25 14:31:48 -0700743 } else {
Jason Sams1c415172010-11-08 17:06:46 -0800744 throw new RSInvalidStateException("Received a message from the script with no message handler installed.");
Jason Sams516c3192009-10-06 13:58:47 -0700745 }
Stephen Hinesab98bb62010-09-24 14:38:30 -0700746 continue;
Jason Sams516c3192009-10-06 13:58:47 -0700747 }
Jason Sams1c415172010-11-08 17:06:46 -0800748
749 if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
750 String e = mRS.nContextGetErrorMessage(mRS.mContext);
751
Jason Samsadd9d962010-11-22 16:20:16 -0800752 if (subID >= RS_ERROR_FATAL_UNKNOWN) {
753 throw new RSRuntimeException("Fatal error " + subID + ", details: " + e);
754 }
755
Jason Sams1c415172010-11-08 17:06:46 -0800756 if(mRS.mErrorCallback != null) {
757 mRS.mErrorCallback.mErrorMessage = e;
758 mRS.mErrorCallback.mErrorNum = subID;
759 mRS.mErrorCallback.run();
760 } else {
761 //throw new RSRuntimeException("Received error num " + subID + ", details: " + e);
762 }
763 continue;
764 }
765
766 // 2: teardown.
767 // But we want to avoid starving other threads during
768 // teardown by yielding until the next line in the destructor
769 // can execute to set mRun = false
770 try {
771 sleep(1, 0);
772 } catch(InterruptedException e) {
Jason Sams516c3192009-10-06 13:58:47 -0700773 }
Jason Sams516c3192009-10-06 13:58:47 -0700774 }
Jason Sams3bc47d42009-11-12 15:10:25 -0800775 Log.d(LOG_TAG, "MessageThread exiting.");
Jason Sams516c3192009-10-06 13:58:47 -0700776 }
777 }
778
Jason Sams27676fe2010-11-10 17:00:59 -0800779 RenderScript() {
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700780 }
781
Jason Sams27676fe2010-11-10 17:00:59 -0800782 /**
783 * Create a basic RenderScript context.
784 *
785 *
786 * @return RenderScript
787 */
Jason Sams704ff642010-02-09 16:05:07 -0800788 public static RenderScript create() {
789 RenderScript rs = new RenderScript();
790
791 rs.mDev = rs.nDeviceCreate();
792 rs.mContext = rs.nContextCreate(rs.mDev, 0);
793 rs.mMessageThread = new MessageThread(rs);
794 rs.mMessageThread.start();
Jason Sams704ff642010-02-09 16:05:07 -0800795 return rs;
Jason Samsefd9b6fb2009-11-03 13:58:36 -0800796 }
797
Jason Sams27676fe2010-11-10 17:00:59 -0800798 /**
799 * Print the currently available debugging information about the state of
800 * the RS context to the log.
801 *
Jason Sams27676fe2010-11-10 17:00:59 -0800802 */
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800803 public void contextDump() {
Jason Sams5dbfe932010-01-27 14:41:43 -0800804 validate();
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800805 nContextDump(0);
Jason Sams715333b2009-11-17 17:26:46 -0800806 }
807
Jason Sams27676fe2010-11-10 17:00:59 -0800808 /**
809 * Wait for any commands in the fifo between the java bindings and native to
810 * be processed.
811 *
812 */
Jason Sams96ed4cf2010-06-15 12:15:57 -0700813 public void finish() {
814 nContextFinish();
815 }
816
Jason Sams27676fe2010-11-10 17:00:59 -0800817 /**
818 * Destroy this renderscript context. Once this function is called its no
819 * longer legal to use this or any objects created by this context.
820 *
821 */
Jason Samsf5b45962009-08-25 14:49:07 -0700822 public void destroy() {
Jason Sams5dbfe932010-01-27 14:41:43 -0800823 validate();
Jason Sams2e1872f2010-08-17 16:25:41 -0700824 nContextDeinitToClient(mContext);
Jason Sams516c3192009-10-06 13:58:47 -0700825 mMessageThread.mRun = false;
Jason Samsa8bf9422010-09-16 13:43:19 -0700826 try {
827 mMessageThread.join();
828 } catch(InterruptedException e) {
829 }
Jason Sams516c3192009-10-06 13:58:47 -0700830
Jason Sams2e1872f2010-08-17 16:25:41 -0700831 nContextDestroy();
Jason Samsf5b45962009-08-25 14:49:07 -0700832 mContext = 0;
833
834 nDeviceDestroy(mDev);
835 mDev = 0;
836 }
Jason Sams02fb2cb2009-05-28 15:37:57 -0700837
Jason Samsa9e7a052009-09-25 14:51:22 -0700838 boolean isAlive() {
839 return mContext != 0;
840 }
841
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800842 int safeID(BaseObj o) {
Jason Sams6b9dec02009-09-23 16:38:37 -0700843 if(o != null) {
Jason Sams06d69de2010-11-09 17:11:40 -0800844 return o.getID();
Jason Samsd8e41612009-08-20 17:22:40 -0700845 }
Jason Sams6b9dec02009-09-23 16:38:37 -0700846 return 0;
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700847 }
Jack Palevich60aa3ea2009-05-26 13:45:08 -0700848}