RenderScript.java revision 2222aa90031604e9752ebc2909303a84139b8b18
1/*
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
17package android.renderscript;
18
19import java.lang.reflect.Field;
20
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23import android.util.Config;
24import android.util.Log;
25import android.view.Surface;
26
27
28/**
29 * @hide
30 *
31 **/
32public class RenderScript {
33    static final String LOG_TAG = "RenderScript_jni";
34    protected static final boolean DEBUG  = false;
35    @SuppressWarnings({"UnusedDeclaration", "deprecation"})
36    protected static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
37
38
39
40     /*
41     * We use a class initializer to allow the native code to cache some
42     * field offsets.
43     */
44    @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
45    protected static boolean sInitialized;
46    native protected static void _nInit();
47
48
49    static {
50        sInitialized = false;
51        try {
52            System.loadLibrary("rs_jni");
53            _nInit();
54            sInitialized = true;
55        } catch (UnsatisfiedLinkError e) {
56            Log.d(LOG_TAG, "RenderScript JNI library not found!");
57        }
58    }
59
60    // Non-threadsafe functions.
61    native void nInitElements(int a8, int rgba4444, int rgba8888, int rgb565);
62    native int  nDeviceCreate();
63    native void nDeviceDestroy(int dev);
64    native void nDeviceSetConfig(int dev, int param, int value);
65    native int  nContextGetMessage(int con, int[] data, boolean wait);
66    native void nContextInitToClient(int con);
67    native void nContextDeinitToClient(int con);
68
69
70    // Methods below are wrapped to protect the non-threadsafe
71    // lockless fifo.
72    native int  rsnContextCreateGL(int dev, int ver, boolean useDepth);
73    synchronized int nContextCreateGL(int dev, int ver, boolean useDepth) {
74        return rsnContextCreateGL(dev, ver, useDepth);
75    }
76    native int  rsnContextCreate(int dev, int ver);
77    synchronized int nContextCreate(int dev, int ver) {
78        return rsnContextCreate(dev, ver);
79    }
80    native void rsnContextDestroy(int con);
81    synchronized void nContextDestroy() {
82        rsnContextDestroy(mContext);
83    }
84    native void rsnContextSetSurface(int con, int w, int h, Surface sur);
85    synchronized void nContextSetSurface(int w, int h, Surface sur) {
86        rsnContextSetSurface(mContext, w, h, sur);
87    }
88    native void rsnContextSetPriority(int con, int p);
89    synchronized void nContextSetPriority(int p) {
90        rsnContextSetPriority(mContext, p);
91    }
92    native void rsnContextDump(int con, int bits);
93    synchronized void nContextDump(int bits) {
94        rsnContextDump(mContext, bits);
95    }
96    native void rsnContextFinish(int con);
97    synchronized void nContextFinish() {
98        rsnContextFinish(mContext);
99    }
100
101    native void rsnContextBindRootScript(int con, int script);
102    synchronized void nContextBindRootScript(int script) {
103        rsnContextBindRootScript(mContext, script);
104    }
105    native void rsnContextBindSampler(int con, int sampler, int slot);
106    synchronized void nContextBindSampler(int sampler, int slot) {
107        rsnContextBindSampler(mContext, sampler, slot);
108    }
109    native void rsnContextBindProgramStore(int con, int pfs);
110    synchronized void nContextBindProgramStore(int pfs) {
111        rsnContextBindProgramStore(mContext, pfs);
112    }
113    native void rsnContextBindProgramFragment(int con, int pf);
114    synchronized void nContextBindProgramFragment(int pf) {
115        rsnContextBindProgramFragment(mContext, pf);
116    }
117    native void rsnContextBindProgramVertex(int con, int pv);
118    synchronized void nContextBindProgramVertex(int pv) {
119        rsnContextBindProgramVertex(mContext, pv);
120    }
121    native void rsnContextBindProgramRaster(int con, int pr);
122    synchronized void nContextBindProgramRaster(int pr) {
123        rsnContextBindProgramRaster(mContext, pr);
124    }
125    native void rsnContextPause(int con);
126    synchronized void nContextPause() {
127        rsnContextPause(mContext);
128    }
129    native void rsnContextResume(int con);
130    synchronized void nContextResume() {
131        rsnContextResume(mContext);
132    }
133
134    native void rsnAssignName(int con, int obj, byte[] name);
135    synchronized void nAssignName(int obj, byte[] name) {
136        rsnAssignName(mContext, obj, name);
137    }
138    native String rsnGetName(int con, int obj);
139    synchronized String nGetName(int obj) {
140        return rsnGetName(mContext, obj);
141    }
142    native void rsnObjDestroy(int con, int id);
143    synchronized void nObjDestroy(int id) {
144        rsnObjDestroy(mContext, id);
145    }
146    native int  rsnFileOpen(int con, byte[] name);
147    synchronized int nFileOpen(byte[] name) {
148        return rsnFileOpen(mContext, name);
149    }
150
151    native int  rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
152    synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
153        return rsnElementCreate(mContext, type, kind, norm, vecSize);
154    }
155    native int  rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
156    synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
157        return rsnElementCreate2(mContext, elements, names, arraySizes);
158    }
159    native void rsnElementGetNativeData(int con, int id, int[] elementData);
160    synchronized void nElementGetNativeData(int id, int[] elementData) {
161        rsnElementGetNativeData(mContext, id, elementData);
162    }
163    native void rsnElementGetSubElements(int con, int id, int[] IDs, String[] names);
164    synchronized void nElementGetSubElements(int id, int[] IDs, String[] names) {
165        rsnElementGetSubElements(mContext, id, IDs, names);
166    }
167
168    native void rsnTypeBegin(int con, int elementID);
169    synchronized void nTypeBegin(int elementID) {
170        rsnTypeBegin(mContext, elementID);
171    }
172    native void rsnTypeAdd(int con, int dim, int val);
173    synchronized void nTypeAdd(int dim, int val) {
174        rsnTypeAdd(mContext, dim, val);
175    }
176    native int  rsnTypeCreate(int con);
177    synchronized int nTypeCreate() {
178        return rsnTypeCreate(mContext);
179    }
180    native void rsnTypeGetNativeData(int con, int id, int[] typeData);
181    synchronized void nTypeGetNativeData(int id, int[] typeData) {
182        rsnTypeGetNativeData(mContext, id, typeData);
183    }
184
185    native int  rsnAllocationCreateTyped(int con, int type);
186    synchronized int nAllocationCreateTyped(int type) {
187        return rsnAllocationCreateTyped(mContext, type);
188    }
189    native int  rsnAllocationCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp);
190    synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
191        return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp);
192    }
193    native int  rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
194    synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
195        return rsnAllocationCreateBitmapRef(mContext, type, bmp);
196    }
197    native int  rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream);
198    synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) {
199        return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream);
200    }
201
202    native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
203    synchronized void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel) {
204        rsnAllocationUploadToTexture(mContext, alloc, genMips, baseMioLevel);
205    }
206    native void rsnAllocationUploadToBufferObject(int con, int alloc);
207    synchronized void nAllocationUploadToBufferObject(int alloc) {
208        rsnAllocationUploadToBufferObject(mContext, alloc);
209    }
210
211    native void rsnAllocationSubData1D(int con, int id, int off, int count, int[] d, int sizeBytes);
212    synchronized void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes) {
213        rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
214    }
215    native void rsnAllocationSubData1D(int con, int id, int off, int count, short[] d, int sizeBytes);
216    synchronized void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes) {
217        rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
218    }
219    native void rsnAllocationSubData1D(int con, int id, int off, int count, byte[] d, int sizeBytes);
220    synchronized void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes) {
221        rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
222    }
223    native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
224    synchronized void nAllocationSubElementData1D(int id, int xoff, int compIdx, byte[] d, int sizeBytes) {
225        rsnAllocationSubElementData1D(mContext, id, xoff, compIdx, d, sizeBytes);
226    }
227    native void rsnAllocationSubData1D(int con, int id, int off, int count, float[] d, int sizeBytes);
228    synchronized void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes) {
229        rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
230    }
231
232    native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
233    synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes) {
234        rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
235    }
236    native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
237    synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes) {
238        rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
239    }
240    native void rsnAllocationRead(int con, int id, int[] d);
241    synchronized void nAllocationRead(int id, int[] d) {
242        rsnAllocationRead(mContext, id, d);
243    }
244    native void rsnAllocationRead(int con, int id, float[] d);
245    synchronized void nAllocationRead(int id, float[] d) {
246        rsnAllocationRead(mContext, id, d);
247    }
248    native int  rsnAllocationGetType(int con, int id);
249    synchronized int nAllocationGetType(int id) {
250        return rsnAllocationGetType(mContext, id);
251    }
252
253    native void rsnAllocationResize1D(int con, int id, int dimX);
254    synchronized void nAllocationResize1D(int id, int dimX) {
255        rsnAllocationResize1D(mContext, id, dimX);
256    }
257    native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
258    synchronized void nAllocationResize2D(int id, int dimX, int dimY) {
259        rsnAllocationResize2D(mContext, id, dimX, dimY);
260    }
261
262    native int  rsnFileA3DCreateFromAssetStream(int con, int assetStream);
263    synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
264        return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
265    }
266    native int  rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
267    synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
268        return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
269    }
270    native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
271    synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
272        rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
273    }
274    native int  rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
275    synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
276        return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
277    }
278
279    native int  rsnFontCreateFromFile(int con, String fileName, int size, int dpi);
280    synchronized int nFontCreateFromFile(String fileName, int size, int dpi) {
281        return rsnFontCreateFromFile(mContext, fileName, size, dpi);
282    }
283
284    native void rsnAdapter1DBindAllocation(int con, int ad, int alloc);
285    synchronized void nAdapter1DBindAllocation(int ad, int alloc) {
286        rsnAdapter1DBindAllocation(mContext, ad, alloc);
287    }
288    native void rsnAdapter1DSetConstraint(int con, int ad, int dim, int value);
289    synchronized void nAdapter1DSetConstraint(int ad, int dim, int value) {
290        rsnAdapter1DSetConstraint(mContext, ad, dim, value);
291    }
292    native void rsnAdapter1DData(int con, int ad, int[] d);
293    synchronized void nAdapter1DData(int ad, int[] d) {
294        rsnAdapter1DData(mContext, ad, d);
295    }
296    native void rsnAdapter1DData(int con, int ad, float[] d);
297    synchronized void nAdapter1DData(int ad, float[] d) {
298        rsnAdapter1DData(mContext, ad, d);
299    }
300    native void rsnAdapter1DSubData(int con, int ad, int off, int count, int[] d);
301    synchronized void nAdapter1DSubData(int ad, int off, int count, int[] d) {
302        rsnAdapter1DSubData(mContext, ad, off, count, d);
303    }
304    native void rsnAdapter1DSubData(int con, int ad, int off, int count, float[] d);
305    synchronized void nAdapter1DSubData(int ad, int off, int count, float[] d) {
306        rsnAdapter1DSubData(mContext, ad, off, count, d);
307    }
308    native int  rsnAdapter1DCreate(int con);
309    synchronized int nAdapter1DCreate() {
310        return rsnAdapter1DCreate(mContext);
311    }
312
313    native void rsnAdapter2DBindAllocation(int con, int ad, int alloc);
314    synchronized void nAdapter2DBindAllocation(int ad, int alloc) {
315        rsnAdapter2DBindAllocation(mContext, ad, alloc);
316    }
317    native void rsnAdapter2DSetConstraint(int con, int ad, int dim, int value);
318    synchronized void nAdapter2DSetConstraint(int ad, int dim, int value) {
319        rsnAdapter2DSetConstraint(mContext, ad, dim, value);
320    }
321    native void rsnAdapter2DData(int con, int ad, int[] d);
322    synchronized void nAdapter2DData(int ad, int[] d) {
323        rsnAdapter2DData(mContext, ad, d);
324    }
325    native void rsnAdapter2DData(int con, int ad, float[] d);
326    synchronized void nAdapter2DData(int ad, float[] d) {
327        rsnAdapter2DData(mContext, ad, d);
328    }
329    native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, int[] d);
330    synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d) {
331        rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
332    }
333    native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, float[] d);
334    synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d) {
335        rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
336    }
337    native int  rsnAdapter2DCreate(int con);
338    synchronized int nAdapter2DCreate() {
339        return rsnAdapter2DCreate(mContext);
340    }
341
342    native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
343    synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
344        rsnScriptBindAllocation(mContext, script, alloc, slot);
345    }
346    native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
347    synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
348        rsnScriptSetTimeZone(mContext, script, timeZone);
349    }
350    native void rsnScriptInvoke(int con, int id, int slot);
351    synchronized void nScriptInvoke(int id, int slot) {
352        rsnScriptInvoke(mContext, id, slot);
353    }
354    native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
355    synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
356        rsnScriptInvokeV(mContext, id, slot, params);
357    }
358    native void rsnScriptSetVarI(int con, int id, int slot, int val);
359    synchronized void nScriptSetVarI(int id, int slot, int val) {
360        rsnScriptSetVarI(mContext, id, slot, val);
361    }
362    native void rsnScriptSetVarF(int con, int id, int slot, float val);
363    synchronized void nScriptSetVarF(int id, int slot, float val) {
364        rsnScriptSetVarF(mContext, id, slot, val);
365    }
366    native void rsnScriptSetVarD(int con, int id, int slot, double val);
367    synchronized void nScriptSetVarD(int id, int slot, double val) {
368        rsnScriptSetVarD(mContext, id, slot, val);
369    }
370    native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
371    synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
372        rsnScriptSetVarV(mContext, id, slot, val);
373    }
374
375    native void rsnScriptCBegin(int con);
376    synchronized void nScriptCBegin() {
377        rsnScriptCBegin(mContext);
378    }
379    native void rsnScriptCSetScript(int con, byte[] script, int offset, int length);
380    synchronized void nScriptCSetScript(byte[] script, int offset, int length) {
381        rsnScriptCSetScript(mContext, script, offset, length);
382    }
383    native int  rsnScriptCCreate(int con);
384    synchronized int nScriptCCreate() {
385        return rsnScriptCCreate(mContext);
386    }
387
388    native void rsnSamplerBegin(int con);
389    synchronized void nSamplerBegin() {
390        rsnSamplerBegin(mContext);
391    }
392    native void rsnSamplerSet(int con, int param, int value);
393    synchronized void nSamplerSet(int param, int value) {
394        rsnSamplerSet(mContext, param, value);
395    }
396    native void rsnSamplerSet2(int con, int param, float value);
397    synchronized void nSamplerSet2(int param, float value) {
398        rsnSamplerSet2(mContext, param, value);
399    }
400    native int  rsnSamplerCreate(int con);
401    synchronized int nSamplerCreate() {
402        return rsnSamplerCreate(mContext);
403    }
404
405    native void rsnProgramStoreBegin(int con, int in, int out);
406    synchronized void nProgramStoreBegin(int in, int out) {
407        rsnProgramStoreBegin(mContext, in, out);
408    }
409    native void rsnProgramStoreDepthFunc(int con, int func);
410    synchronized void nProgramStoreDepthFunc(int func) {
411        rsnProgramStoreDepthFunc(mContext, func);
412    }
413    native void rsnProgramStoreDepthMask(int con, boolean enable);
414    synchronized void nProgramStoreDepthMask(boolean enable) {
415        rsnProgramStoreDepthMask(mContext, enable);
416    }
417    native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a);
418    synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
419        rsnProgramStoreColorMask(mContext, r, g, b, a);
420    }
421    native void rsnProgramStoreBlendFunc(int con, int src, int dst);
422    synchronized void nProgramStoreBlendFunc(int src, int dst) {
423        rsnProgramStoreBlendFunc(mContext, src, dst);
424    }
425    native void rsnProgramStoreDither(int con, boolean enable);
426    synchronized void nProgramStoreDither(boolean enable) {
427        rsnProgramStoreDither(mContext, enable);
428    }
429    native int  rsnProgramStoreCreate(int con);
430    synchronized int nProgramStoreCreate() {
431        return rsnProgramStoreCreate(mContext);
432    }
433
434    native int  rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
435    synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) {
436        return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite);
437    }
438    native void rsnProgramRasterSetLineWidth(int con, int pr, float v);
439    synchronized void nProgramRasterSetLineWidth(int pr, float v) {
440        rsnProgramRasterSetLineWidth(mContext, pr, v);
441    }
442    native void rsnProgramRasterSetCullMode(int con, int pr, int mode);
443    synchronized void nProgramRasterSetCullMode(int pr, int mode) {
444        rsnProgramRasterSetCullMode(mContext, pr, mode);
445    }
446
447    native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
448    synchronized void nProgramBindConstants(int pv, int slot, int mID) {
449        rsnProgramBindConstants(mContext, pv, slot, mID);
450    }
451    native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
452    synchronized void nProgramBindTexture(int vpf, int slot, int a) {
453        rsnProgramBindTexture(mContext, vpf, slot, a);
454    }
455    native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
456    synchronized void nProgramBindSampler(int vpf, int slot, int s) {
457        rsnProgramBindSampler(mContext, vpf, slot, s);
458    }
459    native int  rsnProgramFragmentCreate(int con, String shader, int[] params);
460    synchronized int nProgramFragmentCreate(String shader, int[] params) {
461        return rsnProgramFragmentCreate(mContext, shader, params);
462    }
463    native int  rsnProgramVertexCreate(int con, String shader, int[] params);
464    synchronized int nProgramVertexCreate(String shader, int[] params) {
465        return rsnProgramVertexCreate(mContext, shader, params);
466    }
467
468    native int  rsnMeshCreate(int con, int vtxCount, int indexCount);
469    synchronized int nMeshCreate(int vtxCount, int indexCount) {
470        return rsnMeshCreate(mContext, vtxCount, indexCount);
471    }
472    native void rsnMeshBindVertex(int con, int id, int alloc, int slot);
473    synchronized void nMeshBindVertex(int id, int alloc, int slot) {
474        rsnMeshBindVertex(mContext, id, alloc, slot);
475    }
476    native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot);
477    synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) {
478        rsnMeshBindIndex(mContext, id, alloc, prim, slot);
479    }
480    native int  rsnMeshGetVertexBufferCount(int con, int id);
481    synchronized int nMeshGetVertexBufferCount(int id) {
482        return rsnMeshGetVertexBufferCount(mContext, id);
483    }
484    native int  rsnMeshGetIndexCount(int con, int id);
485    synchronized int nMeshGetIndexCount(int id) {
486        return rsnMeshGetIndexCount(mContext, id);
487    }
488    native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
489    synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
490        rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
491    }
492    native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
493    synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
494        rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
495    }
496
497
498    protected int     mDev;
499    protected int     mContext;
500    @SuppressWarnings({"FieldCanBeLocal"})
501    protected MessageThread mMessageThread;
502
503    Element mElement_U8;
504    Element mElement_I8;
505    Element mElement_U16;
506    Element mElement_I16;
507    Element mElement_U32;
508    Element mElement_I32;
509    Element mElement_I64;
510    Element mElement_F32;
511    Element mElement_F64;
512    Element mElement_BOOLEAN;
513
514    Element mElement_ELEMENT;
515    Element mElement_TYPE;
516    Element mElement_ALLOCATION;
517    Element mElement_SAMPLER;
518    Element mElement_SCRIPT;
519    Element mElement_MESH;
520    Element mElement_PROGRAM_FRAGMENT;
521    Element mElement_PROGRAM_VERTEX;
522    Element mElement_PROGRAM_RASTER;
523    Element mElement_PROGRAM_STORE;
524
525    Element mElement_A_8;
526    Element mElement_RGB_565;
527    Element mElement_RGB_888;
528    Element mElement_RGBA_5551;
529    Element mElement_RGBA_4444;
530    Element mElement_RGBA_8888;
531
532    Element mElement_FLOAT_2;
533    Element mElement_FLOAT_3;
534    Element mElement_FLOAT_4;
535    Element mElement_UCHAR_4;
536
537    Element mElement_MATRIX_4X4;
538    Element mElement_MATRIX_3X3;
539    Element mElement_MATRIX_2X2;
540
541    Sampler mSampler_CLAMP_NEAREST;
542    Sampler mSampler_CLAMP_LINEAR;
543    Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
544    Sampler mSampler_WRAP_NEAREST;
545    Sampler mSampler_WRAP_LINEAR;
546    Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
547
548    ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
549    ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
550    ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_TEST;
551    ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_WRITE;
552    ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
553    ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
554    ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_TEST;
555    ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_WRITE;
556    ProgramStore mProgramStore_BLEND_ADD_DEPTH_TEST;
557    ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_DEPTH;
558    ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_TEST;
559    ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_WRITE;
560
561    ProgramRaster mProgramRaster_CULL_BACK;
562    ProgramRaster mProgramRaster_CULL_FRONT;
563    ProgramRaster mProgramRaster_CULL_NONE;
564
565    ///////////////////////////////////////////////////////////////////////////////////
566    //
567
568    public static class RSMessage implements Runnable {
569        protected int[] mData;
570        protected int mID;
571        public void run() {
572        }
573    }
574    public RSMessage mMessageCallback = null;
575
576    public enum Priority {
577        LOW (5),     //ANDROID_PRIORITY_BACKGROUND + 5
578        NORMAL (-4);  //ANDROID_PRIORITY_DISPLAY
579
580        int mID;
581        Priority(int id) {
582            mID = id;
583        }
584    }
585
586    void validate() {
587        if (mContext == 0) {
588            throw new IllegalStateException("Calling RS with no Context active.");
589        }
590    }
591
592    public void contextSetPriority(Priority p) {
593        validate();
594        nContextSetPriority(p.mID);
595    }
596
597    protected static class MessageThread extends Thread {
598        RenderScript mRS;
599        boolean mRun = true;
600
601        MessageThread(RenderScript rs) {
602            super("RSMessageThread");
603            mRS = rs;
604
605        }
606
607        public void run() {
608            // This function is a temporary solution.  The final solution will
609            // used typed allocations where the message id is the type indicator.
610            int[] rbuf = new int[16];
611            mRS.nContextInitToClient(mRS.mContext);
612            while(mRun) {
613                rbuf[0] = 0;
614                int msg = mRS.nContextGetMessage(mRS.mContext, rbuf, true);
615                if ((msg == 0)) {
616                    // Can happen for two reasons
617                    if (rbuf[0] > 0 && mRun) {
618                        // 1: Buffer needs to be enlarged.
619                        rbuf = new int[rbuf[0] + 2];
620                    } else {
621                        // 2: teardown.
622                        // But we want to avoid starving other threads during
623                        // teardown by yielding until the next line in the destructor
624                        // can execute to set mRun = false
625                        try {
626                            sleep(1, 0);
627                        } catch(InterruptedException e) {
628                        }
629                    }
630                    continue;
631                }
632                if(mRS.mMessageCallback != null) {
633                    mRS.mMessageCallback.mData = rbuf;
634                    mRS.mMessageCallback.mID = msg;
635                    mRS.mMessageCallback.run();
636                }
637            }
638            Log.d(LOG_TAG, "MessageThread exiting.");
639        }
640    }
641
642    protected RenderScript() {
643    }
644
645    public static RenderScript create() {
646        RenderScript rs = new RenderScript();
647
648        rs.mDev = rs.nDeviceCreate();
649        rs.mContext = rs.nContextCreate(rs.mDev, 0);
650        rs.mMessageThread = new MessageThread(rs);
651        rs.mMessageThread.start();
652        Element.initPredefined(rs);
653        return rs;
654    }
655
656    public void contextDump(int bits) {
657        validate();
658        nContextDump(bits);
659    }
660
661    public void finish() {
662        nContextFinish();
663    }
664
665    public void destroy() {
666        validate();
667        nContextDeinitToClient(mContext);
668        mMessageThread.mRun = false;
669        try {
670            mMessageThread.join();
671        } catch(InterruptedException e) {
672        }
673
674        nContextDestroy();
675        mContext = 0;
676
677        nDeviceDestroy(mDev);
678        mDev = 0;
679    }
680
681    boolean isAlive() {
682        return mContext != 0;
683    }
684
685    ///////////////////////////////////////////////////////////////////////////////////
686    // Root state
687
688    protected int safeID(BaseObj o) {
689        if(o != null) {
690            return o.mID;
691        }
692        return 0;
693    }
694}
695
696
697
698