RenderScript.java revision 5edc608a0749ed4b7074b5c1243043eb722c3c31
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  rsnAllocationCreateFromBitmapBoxed(int con, int dstFmt, boolean genMips, Bitmap bmp);
198    synchronized int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp) {
199        return rsnAllocationCreateFromBitmapBoxed(mContext, dstFmt, genMips, bmp);
200    }
201    native int  rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream);
202    synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) {
203        return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream);
204    }
205
206    native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
207    synchronized void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel) {
208        rsnAllocationUploadToTexture(mContext, alloc, genMips, baseMioLevel);
209    }
210    native void rsnAllocationUploadToBufferObject(int con, int alloc);
211    synchronized void nAllocationUploadToBufferObject(int alloc) {
212        rsnAllocationUploadToBufferObject(mContext, alloc);
213    }
214
215    native void rsnAllocationSubData1D(int con, int id, int off, int count, int[] d, int sizeBytes);
216    synchronized void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes) {
217        rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
218    }
219    native void rsnAllocationSubData1D(int con, int id, int off, int count, short[] d, int sizeBytes);
220    synchronized void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes) {
221        rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
222    }
223    native void rsnAllocationSubData1D(int con, int id, int off, int count, byte[] d, int sizeBytes);
224    synchronized void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes) {
225        rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
226    }
227    native void rsnAllocationSubElementData1D(int con, int id, int xoff, int compIdx, byte[] d, int sizeBytes);
228    synchronized void nAllocationSubElementData1D(int id, int xoff, int compIdx, byte[] d, int sizeBytes) {
229        rsnAllocationSubElementData1D(mContext, id, xoff, compIdx, d, sizeBytes);
230    }
231    native void rsnAllocationSubData1D(int con, int id, int off, int count, float[] d, int sizeBytes);
232    synchronized void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes) {
233        rsnAllocationSubData1D(mContext, id, off, count, d, sizeBytes);
234    }
235
236    native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
237    synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes) {
238        rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
239    }
240    native void rsnAllocationSubData2D(int con, int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
241    synchronized void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes) {
242        rsnAllocationSubData2D(mContext, id, xoff, yoff, w, h, d, sizeBytes);
243    }
244    native void rsnAllocationRead(int con, int id, int[] d);
245    synchronized void nAllocationRead(int id, int[] d) {
246        rsnAllocationRead(mContext, id, d);
247    }
248    native void rsnAllocationRead(int con, int id, float[] d);
249    synchronized void nAllocationRead(int id, float[] d) {
250        rsnAllocationRead(mContext, id, d);
251    }
252    native int  rsnAllocationGetType(int con, int id);
253    synchronized int nAllocationGetType(int id) {
254        return rsnAllocationGetType(mContext, id);
255    }
256
257    native void rsnAllocationResize1D(int con, int id, int dimX);
258    synchronized void nAllocationResize1D(int id, int dimX) {
259        rsnAllocationResize1D(mContext, id, dimX);
260    }
261    native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
262    synchronized void nAllocationResize2D(int id, int dimX, int dimY) {
263        rsnAllocationResize2D(mContext, id, dimX, dimY);
264    }
265
266    native int  rsnFileA3DCreateFromAssetStream(int con, int assetStream);
267    synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
268        return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
269    }
270    native int  rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
271    synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
272        return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
273    }
274    native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
275    synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
276        rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
277    }
278    native int  rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
279    synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
280        return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
281    }
282
283    native int  rsnFontCreateFromFile(int con, String fileName, int size, int dpi);
284    synchronized int nFontCreateFromFile(String fileName, int size, int dpi) {
285        return rsnFontCreateFromFile(mContext, fileName, size, dpi);
286    }
287
288    native void rsnAdapter1DBindAllocation(int con, int ad, int alloc);
289    synchronized void nAdapter1DBindAllocation(int ad, int alloc) {
290        rsnAdapter1DBindAllocation(mContext, ad, alloc);
291    }
292    native void rsnAdapter1DSetConstraint(int con, int ad, int dim, int value);
293    synchronized void nAdapter1DSetConstraint(int ad, int dim, int value) {
294        rsnAdapter1DSetConstraint(mContext, ad, dim, value);
295    }
296    native void rsnAdapter1DData(int con, int ad, int[] d);
297    synchronized void nAdapter1DData(int ad, int[] d) {
298        rsnAdapter1DData(mContext, ad, d);
299    }
300    native void rsnAdapter1DData(int con, int ad, float[] d);
301    synchronized void nAdapter1DData(int ad, float[] d) {
302        rsnAdapter1DData(mContext, ad, d);
303    }
304    native void rsnAdapter1DSubData(int con, int ad, int off, int count, int[] d);
305    synchronized void nAdapter1DSubData(int ad, int off, int count, int[] d) {
306        rsnAdapter1DSubData(mContext, ad, off, count, d);
307    }
308    native void rsnAdapter1DSubData(int con, int ad, int off, int count, float[] d);
309    synchronized void nAdapter1DSubData(int ad, int off, int count, float[] d) {
310        rsnAdapter1DSubData(mContext, ad, off, count, d);
311    }
312    native int  rsnAdapter1DCreate(int con);
313    synchronized int nAdapter1DCreate() {
314        return rsnAdapter1DCreate(mContext);
315    }
316
317    native void rsnAdapter2DBindAllocation(int con, int ad, int alloc);
318    synchronized void nAdapter2DBindAllocation(int ad, int alloc) {
319        rsnAdapter2DBindAllocation(mContext, ad, alloc);
320    }
321    native void rsnAdapter2DSetConstraint(int con, int ad, int dim, int value);
322    synchronized void nAdapter2DSetConstraint(int ad, int dim, int value) {
323        rsnAdapter2DSetConstraint(mContext, ad, dim, value);
324    }
325    native void rsnAdapter2DData(int con, int ad, int[] d);
326    synchronized void nAdapter2DData(int ad, int[] d) {
327        rsnAdapter2DData(mContext, ad, d);
328    }
329    native void rsnAdapter2DData(int con, int ad, float[] d);
330    synchronized void nAdapter2DData(int ad, float[] d) {
331        rsnAdapter2DData(mContext, ad, d);
332    }
333    native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, int[] d);
334    synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d) {
335        rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
336    }
337    native void rsnAdapter2DSubData(int con, int ad, int xoff, int yoff, int w, int h, float[] d);
338    synchronized void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d) {
339        rsnAdapter2DSubData(mContext, ad, xoff, yoff, w, h, d);
340    }
341    native int  rsnAdapter2DCreate(int con);
342    synchronized int nAdapter2DCreate() {
343        return rsnAdapter2DCreate(mContext);
344    }
345
346    native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
347    synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
348        rsnScriptBindAllocation(mContext, script, alloc, slot);
349    }
350    native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
351    synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
352        rsnScriptSetTimeZone(mContext, script, timeZone);
353    }
354    native void rsnScriptInvoke(int con, int id, int slot);
355    synchronized void nScriptInvoke(int id, int slot) {
356        rsnScriptInvoke(mContext, id, slot);
357    }
358    native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
359    synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
360        rsnScriptInvokeV(mContext, id, slot, params);
361    }
362    native void rsnScriptSetVarI(int con, int id, int slot, int val);
363    synchronized void nScriptSetVarI(int id, int slot, int val) {
364        rsnScriptSetVarI(mContext, id, slot, val);
365    }
366    native void rsnScriptSetVarF(int con, int id, int slot, float val);
367    synchronized void nScriptSetVarF(int id, int slot, float val) {
368        rsnScriptSetVarF(mContext, id, slot, val);
369    }
370    native void rsnScriptSetVarD(int con, int id, int slot, double val);
371    synchronized void nScriptSetVarD(int id, int slot, double val) {
372        rsnScriptSetVarD(mContext, id, slot, val);
373    }
374    native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
375    synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
376        rsnScriptSetVarV(mContext, id, slot, val);
377    }
378
379    native void rsnScriptCBegin(int con);
380    synchronized void nScriptCBegin() {
381        rsnScriptCBegin(mContext);
382    }
383    native void rsnScriptCSetScript(int con, byte[] script, int offset, int length);
384    synchronized void nScriptCSetScript(byte[] script, int offset, int length) {
385        rsnScriptCSetScript(mContext, script, offset, length);
386    }
387    native int  rsnScriptCCreate(int con);
388    synchronized int nScriptCCreate() {
389        return rsnScriptCCreate(mContext);
390    }
391
392    native void rsnSamplerBegin(int con);
393    synchronized void nSamplerBegin() {
394        rsnSamplerBegin(mContext);
395    }
396    native void rsnSamplerSet(int con, int param, int value);
397    synchronized void nSamplerSet(int param, int value) {
398        rsnSamplerSet(mContext, param, value);
399    }
400    native void rsnSamplerSet2(int con, int param, float value);
401    synchronized void nSamplerSet2(int param, float value) {
402        rsnSamplerSet2(mContext, param, value);
403    }
404    native int  rsnSamplerCreate(int con);
405    synchronized int nSamplerCreate() {
406        return rsnSamplerCreate(mContext);
407    }
408
409    native void rsnProgramStoreBegin(int con, int in, int out);
410    synchronized void nProgramStoreBegin(int in, int out) {
411        rsnProgramStoreBegin(mContext, in, out);
412    }
413    native void rsnProgramStoreDepthFunc(int con, int func);
414    synchronized void nProgramStoreDepthFunc(int func) {
415        rsnProgramStoreDepthFunc(mContext, func);
416    }
417    native void rsnProgramStoreDepthMask(int con, boolean enable);
418    synchronized void nProgramStoreDepthMask(boolean enable) {
419        rsnProgramStoreDepthMask(mContext, enable);
420    }
421    native void rsnProgramStoreColorMask(int con, boolean r, boolean g, boolean b, boolean a);
422    synchronized void nProgramStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
423        rsnProgramStoreColorMask(mContext, r, g, b, a);
424    }
425    native void rsnProgramStoreBlendFunc(int con, int src, int dst);
426    synchronized void nProgramStoreBlendFunc(int src, int dst) {
427        rsnProgramStoreBlendFunc(mContext, src, dst);
428    }
429    native void rsnProgramStoreDither(int con, boolean enable);
430    synchronized void nProgramStoreDither(boolean enable) {
431        rsnProgramStoreDither(mContext, enable);
432    }
433    native int  rsnProgramStoreCreate(int con);
434    synchronized int nProgramStoreCreate() {
435        return rsnProgramStoreCreate(mContext);
436    }
437
438    native int  rsnProgramRasterCreate(int con, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
439    synchronized int nProgramRasterCreate(boolean pointSmooth, boolean lineSmooth, boolean pointSprite) {
440        return rsnProgramRasterCreate(mContext, pointSmooth, lineSmooth, pointSprite);
441    }
442    native void rsnProgramRasterSetLineWidth(int con, int pr, float v);
443    synchronized void nProgramRasterSetLineWidth(int pr, float v) {
444        rsnProgramRasterSetLineWidth(mContext, pr, v);
445    }
446    native void rsnProgramRasterSetCullMode(int con, int pr, int mode);
447    synchronized void nProgramRasterSetCullMode(int pr, int mode) {
448        rsnProgramRasterSetCullMode(mContext, pr, mode);
449    }
450
451    native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
452    synchronized void nProgramBindConstants(int pv, int slot, int mID) {
453        rsnProgramBindConstants(mContext, pv, slot, mID);
454    }
455    native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
456    synchronized void nProgramBindTexture(int vpf, int slot, int a) {
457        rsnProgramBindTexture(mContext, vpf, slot, a);
458    }
459    native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
460    synchronized void nProgramBindSampler(int vpf, int slot, int s) {
461        rsnProgramBindSampler(mContext, vpf, slot, s);
462    }
463    native int  rsnProgramFragmentCreate(int con, String shader, int[] params);
464    synchronized int nProgramFragmentCreate(String shader, int[] params) {
465        return rsnProgramFragmentCreate(mContext, shader, params);
466    }
467    native int  rsnProgramVertexCreate(int con, String shader, int[] params);
468    synchronized int nProgramVertexCreate(String shader, int[] params) {
469        return rsnProgramVertexCreate(mContext, shader, params);
470    }
471
472    native int  rsnMeshCreate(int con, int vtxCount, int indexCount);
473    synchronized int nMeshCreate(int vtxCount, int indexCount) {
474        return rsnMeshCreate(mContext, vtxCount, indexCount);
475    }
476    native void rsnMeshBindVertex(int con, int id, int alloc, int slot);
477    synchronized void nMeshBindVertex(int id, int alloc, int slot) {
478        rsnMeshBindVertex(mContext, id, alloc, slot);
479    }
480    native void rsnMeshBindIndex(int con, int id, int alloc, int prim, int slot);
481    synchronized void nMeshBindIndex(int id, int alloc, int prim, int slot) {
482        rsnMeshBindIndex(mContext, id, alloc, prim, slot);
483    }
484    native int  rsnMeshGetVertexBufferCount(int con, int id);
485    synchronized int nMeshGetVertexBufferCount(int id) {
486        return rsnMeshGetVertexBufferCount(mContext, id);
487    }
488    native int  rsnMeshGetIndexCount(int con, int id);
489    synchronized int nMeshGetIndexCount(int id) {
490        return rsnMeshGetIndexCount(mContext, id);
491    }
492    native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
493    synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
494        rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
495    }
496    native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
497    synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
498        rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
499    }
500
501
502    protected int     mDev;
503    protected int     mContext;
504    @SuppressWarnings({"FieldCanBeLocal"})
505    protected MessageThread mMessageThread;
506
507    Element mElement_U8;
508    Element mElement_I8;
509    Element mElement_U16;
510    Element mElement_I16;
511    Element mElement_U32;
512    Element mElement_I32;
513    Element mElement_I64;
514    Element mElement_F32;
515    Element mElement_F64;
516    Element mElement_BOOLEAN;
517
518    Element mElement_ELEMENT;
519    Element mElement_TYPE;
520    Element mElement_ALLOCATION;
521    Element mElement_SAMPLER;
522    Element mElement_SCRIPT;
523    Element mElement_MESH;
524    Element mElement_PROGRAM_FRAGMENT;
525    Element mElement_PROGRAM_VERTEX;
526    Element mElement_PROGRAM_RASTER;
527    Element mElement_PROGRAM_STORE;
528
529    Element mElement_A_8;
530    Element mElement_RGB_565;
531    Element mElement_RGB_888;
532    Element mElement_RGBA_5551;
533    Element mElement_RGBA_4444;
534    Element mElement_RGBA_8888;
535
536    Element mElement_FLOAT_2;
537    Element mElement_FLOAT_3;
538    Element mElement_FLOAT_4;
539    Element mElement_UCHAR_4;
540
541    Element mElement_MATRIX_4X4;
542    Element mElement_MATRIX_3X3;
543    Element mElement_MATRIX_2X2;
544
545    Sampler mSampler_CLAMP_NEAREST;
546    Sampler mSampler_CLAMP_LINEAR;
547    Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
548    Sampler mSampler_WRAP_NEAREST;
549    Sampler mSampler_WRAP_LINEAR;
550    Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
551
552    ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
553    ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
554    ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_TEST;
555    ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_WRITE;
556    ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
557    ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
558    ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_TEST;
559    ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_WRITE;
560    ProgramStore mProgramStore_BLEND_ADD_DEPTH_TEST;
561    ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_DEPTH;
562    ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_TEST;
563    ProgramStore mProgramStore_BLEND_ADD_DEPTH_NO_WRITE;
564
565    ProgramRaster mProgramRaster_CULL_BACK;
566    ProgramRaster mProgramRaster_CULL_FRONT;
567    ProgramRaster mProgramRaster_CULL_NONE;
568
569    ///////////////////////////////////////////////////////////////////////////////////
570    //
571
572    public static class RSMessage implements Runnable {
573        protected int[] mData;
574        protected int mID;
575        public void run() {
576        }
577    }
578    public RSMessage mMessageCallback = null;
579
580    public enum Priority {
581        LOW (5),     //ANDROID_PRIORITY_BACKGROUND + 5
582        NORMAL (-4);  //ANDROID_PRIORITY_DISPLAY
583
584        int mID;
585        Priority(int id) {
586            mID = id;
587        }
588    }
589
590    void validate() {
591        if (mContext == 0) {
592            throw new IllegalStateException("Calling RS with no Context active.");
593        }
594    }
595
596    public void contextSetPriority(Priority p) {
597        validate();
598        nContextSetPriority(p.mID);
599    }
600
601    protected static class MessageThread extends Thread {
602        RenderScript mRS;
603        boolean mRun = true;
604
605        MessageThread(RenderScript rs) {
606            super("RSMessageThread");
607            mRS = rs;
608
609        }
610
611        public void run() {
612            // This function is a temporary solution.  The final solution will
613            // used typed allocations where the message id is the type indicator.
614            int[] rbuf = new int[16];
615            mRS.nContextInitToClient(mRS.mContext);
616            while(mRun) {
617                rbuf[0] = 0;
618                int msg = mRS.nContextGetMessage(mRS.mContext, rbuf, true);
619                if ((msg == 0)) {
620                    // Can happen for two reasons
621                    if (rbuf[0] > 0 && mRun) {
622                        // 1: Buffer needs to be enlarged.
623                        rbuf = new int[rbuf[0] + 2];
624                    } else {
625                        // 2: teardown.
626                        // But we want to avoid starving other threads during
627                        // teardown by yielding until the next line in the destructor
628                        // can execute to set mRun = false
629                        try {
630                            sleep(1, 0);
631                        } catch(InterruptedException e) {
632                        }
633                    }
634                    continue;
635                }
636                if(mRS.mMessageCallback != null) {
637                    mRS.mMessageCallback.mData = rbuf;
638                    mRS.mMessageCallback.mID = msg;
639                    mRS.mMessageCallback.run();
640                }
641            }
642            Log.d(LOG_TAG, "MessageThread exiting.");
643        }
644    }
645
646    protected RenderScript() {
647    }
648
649    public static RenderScript create() {
650        RenderScript rs = new RenderScript();
651
652        rs.mDev = rs.nDeviceCreate();
653        rs.mContext = rs.nContextCreate(rs.mDev, 0);
654        rs.mMessageThread = new MessageThread(rs);
655        rs.mMessageThread.start();
656        Element.initPredefined(rs);
657        return rs;
658    }
659
660    public void contextDump(int bits) {
661        validate();
662        nContextDump(bits);
663    }
664
665    public void finish() {
666        nContextFinish();
667    }
668
669    public void destroy() {
670        validate();
671        nContextDeinitToClient(mContext);
672        mMessageThread.mRun = false;
673        try {
674            mMessageThread.join();
675        } catch(InterruptedException e) {
676        }
677
678        nContextDestroy();
679        mContext = 0;
680
681        nDeviceDestroy(mDev);
682        mDev = 0;
683    }
684
685    boolean isAlive() {
686        return mContext != 0;
687    }
688
689    ///////////////////////////////////////////////////////////////////////////////////
690    // Root state
691
692    protected int safeID(BaseObj o) {
693        if(o != null) {
694            return o.mID;
695        }
696        return 0;
697    }
698}
699
700
701
702