RenderScript.java revision fe1d5ffd1b6c4977a9f301997d2ad90e5b049a3b
1/*
2 * Copyright (C) 2008-2012 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.io.File;
20import java.lang.reflect.Field;
21
22import android.content.Context;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageManager;
25import android.content.res.AssetManager;
26import android.graphics.Bitmap;
27import android.graphics.BitmapFactory;
28import android.graphics.SurfaceTexture;
29import android.os.Process;
30import android.util.Log;
31import android.view.Surface;
32
33
34
35/**
36 * Renderscript base master class.  An instance of this class creates native
37 * worker threads for processing commands from this object.  This base class
38 * does not provide any extended capabilities beyond simple data processing.
39 * For extended capabilities use derived classes such as RenderScriptGL.
40 *
41 * <div class="special reference">
42 * <h3>Developer Guides</h3>
43 * <p>For more information about creating an application that uses Renderscript, read the
44 * <a href="{@docRoot}guide/topics/graphics/renderscript.html">Renderscript</a> developer guide.</p>
45 * </div>
46 **/
47public class RenderScript {
48    static final String LOG_TAG = "RenderScript_jni";
49    static final boolean DEBUG  = false;
50    @SuppressWarnings({"UnusedDeclaration", "deprecation"})
51    static final boolean LOG_ENABLED = false;
52
53    private Context mApplicationContext;
54
55    /*
56     * We use a class initializer to allow the native code to cache some
57     * field offsets.
58     */
59    @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
60    static boolean sInitialized;
61    native static void _nInit();
62
63
64    static {
65        sInitialized = false;
66        try {
67            System.loadLibrary("rs_jni");
68            _nInit();
69            sInitialized = true;
70        } catch (UnsatisfiedLinkError e) {
71            Log.e(LOG_TAG, "Error loading RS jni library: " + e);
72            throw new RSRuntimeException("Error loading RS jni library: " + e);
73        }
74    }
75
76    // Non-threadsafe functions.
77    native int  nDeviceCreate();
78    native void nDeviceDestroy(int dev);
79    native void nDeviceSetConfig(int dev, int param, int value);
80    native int nContextGetUserMessage(int con, int[] data);
81    native String nContextGetErrorMessage(int con);
82    native int  nContextPeekMessage(int con, int[] subID);
83    native void nContextInitToClient(int con);
84    native void nContextDeinitToClient(int con);
85
86    /**
87     * Name of the file that holds the object cache.
88     */
89    private static final String CACHE_PATH = "com.android.renderscript.cache";
90    static String mCachePath;
91
92     /**
93     * Sets the directory to use as a persistent storage for the
94     * renderscript object file cache.
95     *
96     * @hide
97     * @param cacheDir A directory the current process can write to
98     */
99    public static void setupDiskCache(File cacheDir) {
100        File f = new File(cacheDir, CACHE_PATH);
101        mCachePath = f.getAbsolutePath();
102        f.mkdirs();
103    }
104
105
106    // Methods below are wrapped to protect the non-threadsafe
107    // lockless fifo.
108    native int  rsnContextCreateGL(int dev, int ver, int sdkVer,
109                 int colorMin, int colorPref,
110                 int alphaMin, int alphaPref,
111                 int depthMin, int depthPref,
112                 int stencilMin, int stencilPref,
113                 int samplesMin, int samplesPref, float samplesQ, int dpi);
114    synchronized int nContextCreateGL(int dev, int ver, int sdkVer,
115                 int colorMin, int colorPref,
116                 int alphaMin, int alphaPref,
117                 int depthMin, int depthPref,
118                 int stencilMin, int stencilPref,
119                 int samplesMin, int samplesPref, float samplesQ, int dpi) {
120        return rsnContextCreateGL(dev, ver, sdkVer, colorMin, colorPref,
121                                  alphaMin, alphaPref, depthMin, depthPref,
122                                  stencilMin, stencilPref,
123                                  samplesMin, samplesPref, samplesQ, dpi);
124    }
125    native int  rsnContextCreate(int dev, int ver, int sdkVer);
126    synchronized int nContextCreate(int dev, int ver, int sdkVer) {
127        return rsnContextCreate(dev, ver, sdkVer);
128    }
129    native void rsnContextDestroy(int con);
130    synchronized void nContextDestroy() {
131        validate();
132        rsnContextDestroy(mContext);
133    }
134    native void rsnContextSetSurface(int con, int w, int h, Surface sur);
135    synchronized void nContextSetSurface(int w, int h, Surface sur) {
136        validate();
137        rsnContextSetSurface(mContext, w, h, sur);
138    }
139    native void rsnContextSetSurfaceTexture(int con, int w, int h, SurfaceTexture sur);
140    synchronized void nContextSetSurfaceTexture(int w, int h, SurfaceTexture sur) {
141        validate();
142        rsnContextSetSurfaceTexture(mContext, w, h, sur);
143    }
144    native void rsnContextSetPriority(int con, int p);
145    synchronized void nContextSetPriority(int p) {
146        validate();
147        rsnContextSetPriority(mContext, p);
148    }
149    native void rsnContextDump(int con, int bits);
150    synchronized void nContextDump(int bits) {
151        validate();
152        rsnContextDump(mContext, bits);
153    }
154    native void rsnContextFinish(int con);
155    synchronized void nContextFinish() {
156        validate();
157        rsnContextFinish(mContext);
158    }
159
160    native void rsnContextBindRootScript(int con, int script);
161    synchronized void nContextBindRootScript(int script) {
162        validate();
163        rsnContextBindRootScript(mContext, script);
164    }
165    native void rsnContextBindSampler(int con, int sampler, int slot);
166    synchronized void nContextBindSampler(int sampler, int slot) {
167        validate();
168        rsnContextBindSampler(mContext, sampler, slot);
169    }
170    native void rsnContextBindProgramStore(int con, int pfs);
171    synchronized void nContextBindProgramStore(int pfs) {
172        validate();
173        rsnContextBindProgramStore(mContext, pfs);
174    }
175    native void rsnContextBindProgramFragment(int con, int pf);
176    synchronized void nContextBindProgramFragment(int pf) {
177        validate();
178        rsnContextBindProgramFragment(mContext, pf);
179    }
180    native void rsnContextBindProgramVertex(int con, int pv);
181    synchronized void nContextBindProgramVertex(int pv) {
182        validate();
183        rsnContextBindProgramVertex(mContext, pv);
184    }
185    native void rsnContextBindProgramRaster(int con, int pr);
186    synchronized void nContextBindProgramRaster(int pr) {
187        validate();
188        rsnContextBindProgramRaster(mContext, pr);
189    }
190    native void rsnContextPause(int con);
191    synchronized void nContextPause() {
192        validate();
193        rsnContextPause(mContext);
194    }
195    native void rsnContextResume(int con);
196    synchronized void nContextResume() {
197        validate();
198        rsnContextResume(mContext);
199    }
200
201    native void rsnAssignName(int con, int obj, byte[] name);
202    synchronized void nAssignName(int obj, byte[] name) {
203        validate();
204        rsnAssignName(mContext, obj, name);
205    }
206    native String rsnGetName(int con, int obj);
207    synchronized String nGetName(int obj) {
208        validate();
209        return rsnGetName(mContext, obj);
210    }
211    native void rsnObjDestroy(int con, int id);
212    synchronized void nObjDestroy(int id) {
213        // There is a race condition here.  The calling code may be run
214        // by the gc while teardown is occuring.  This protects againts
215        // deleting dead objects.
216        if (mContext != 0) {
217            rsnObjDestroy(mContext, id);
218        }
219    }
220
221    native int  rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
222    synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
223        validate();
224        return rsnElementCreate(mContext, type, kind, norm, vecSize);
225    }
226    native int  rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
227    synchronized int nElementCreate2(int[] elements, String[] names, int[] arraySizes) {
228        validate();
229        return rsnElementCreate2(mContext, elements, names, arraySizes);
230    }
231    native void rsnElementGetNativeData(int con, int id, int[] elementData);
232    synchronized void nElementGetNativeData(int id, int[] elementData) {
233        validate();
234        rsnElementGetNativeData(mContext, id, elementData);
235    }
236    native void rsnElementGetSubElements(int con, int id,
237                                         int[] IDs, String[] names, int[] arraySizes);
238    synchronized void nElementGetSubElements(int id, int[] IDs, String[] names, int[] arraySizes) {
239        validate();
240        rsnElementGetSubElements(mContext, id, IDs, names, arraySizes);
241    }
242
243    native int rsnTypeCreate(int con, int eid, int x, int y, int z, boolean mips, boolean faces);
244    synchronized int nTypeCreate(int eid, int x, int y, int z, boolean mips, boolean faces) {
245        validate();
246        return rsnTypeCreate(mContext, eid, x, y, z, mips, faces);
247    }
248    native void rsnTypeGetNativeData(int con, int id, int[] typeData);
249    synchronized void nTypeGetNativeData(int id, int[] typeData) {
250        validate();
251        rsnTypeGetNativeData(mContext, id, typeData);
252    }
253
254    native int  rsnAllocationCreateTyped(int con, int type, int mip, int usage, int pointer);
255    synchronized int nAllocationCreateTyped(int type, int mip, int usage, int pointer) {
256        validate();
257        return rsnAllocationCreateTyped(mContext, type, mip, usage, pointer);
258    }
259    native int  rsnAllocationCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
260    synchronized int nAllocationCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
261        validate();
262        return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage);
263    }
264    native int  rsnAllocationCubeCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
265    synchronized int nAllocationCubeCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
266        validate();
267        return rsnAllocationCubeCreateFromBitmap(mContext, type, mip, bmp, usage);
268    }
269    native int  rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
270    synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
271        validate();
272        return rsnAllocationCreateBitmapRef(mContext, type, bmp);
273    }
274    native int  rsnAllocationCreateFromAssetStream(int con, int mips, int assetStream, int usage);
275    synchronized int nAllocationCreateFromAssetStream(int mips, int assetStream, int usage) {
276        validate();
277        return rsnAllocationCreateFromAssetStream(mContext, mips, assetStream, usage);
278    }
279
280    native void  rsnAllocationCopyToBitmap(int con, int alloc, Bitmap bmp);
281    synchronized void nAllocationCopyToBitmap(int alloc, Bitmap bmp) {
282        validate();
283        rsnAllocationCopyToBitmap(mContext, alloc, bmp);
284    }
285
286
287    native void rsnAllocationSyncAll(int con, int alloc, int src);
288    synchronized void nAllocationSyncAll(int alloc, int src) {
289        validate();
290        rsnAllocationSyncAll(mContext, alloc, src);
291    }
292    native int rsnAllocationGetSurfaceTextureID(int con, int alloc);
293    synchronized int nAllocationGetSurfaceTextureID(int alloc) {
294        validate();
295        return rsnAllocationGetSurfaceTextureID(mContext, alloc);
296    }
297    native void rsnAllocationGetSurfaceTextureID2(int con, int alloc, SurfaceTexture st);
298    synchronized void nAllocationGetSurfaceTextureID2(int alloc, SurfaceTexture st) {
299        validate();
300        rsnAllocationGetSurfaceTextureID2(mContext, alloc, st);
301    }
302    native void rsnAllocationSetSurfaceTexture(int con, int alloc, SurfaceTexture sur);
303    synchronized void nAllocationSetSurfaceTexture(int alloc, SurfaceTexture sur) {
304        validate();
305        rsnAllocationSetSurfaceTexture(mContext, alloc, sur);
306    }
307    native void rsnAllocationIoSend(int con, int alloc);
308    synchronized void nAllocationIoSend(int alloc) {
309        validate();
310        rsnAllocationIoSend(mContext, alloc);
311    }
312    native void rsnAllocationIoReceive(int con, int alloc);
313    synchronized void nAllocationIoReceive(int alloc) {
314        validate();
315        rsnAllocationIoReceive(mContext, alloc);
316    }
317
318
319    native void rsnAllocationGenerateMipmaps(int con, int alloc);
320    synchronized void nAllocationGenerateMipmaps(int alloc) {
321        validate();
322        rsnAllocationGenerateMipmaps(mContext, alloc);
323    }
324    native void  rsnAllocationCopyFromBitmap(int con, int alloc, Bitmap bmp);
325    synchronized void nAllocationCopyFromBitmap(int alloc, Bitmap bmp) {
326        validate();
327        rsnAllocationCopyFromBitmap(mContext, alloc, bmp);
328    }
329
330
331    native void rsnAllocationData1D(int con, int id, int off, int mip, int count, int[] d, int sizeBytes);
332    synchronized void nAllocationData1D(int id, int off, int mip, int count, int[] d, int sizeBytes) {
333        validate();
334        rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
335    }
336    native void rsnAllocationData1D(int con, int id, int off, int mip, int count, short[] d, int sizeBytes);
337    synchronized void nAllocationData1D(int id, int off, int mip, int count, short[] d, int sizeBytes) {
338        validate();
339        rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
340    }
341    native void rsnAllocationData1D(int con, int id, int off, int mip, int count, byte[] d, int sizeBytes);
342    synchronized void nAllocationData1D(int id, int off, int mip, int count, byte[] d, int sizeBytes) {
343        validate();
344        rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
345    }
346    native void rsnAllocationData1D(int con, int id, int off, int mip, int count, float[] d, int sizeBytes);
347    synchronized void nAllocationData1D(int id, int off, int mip, int count, float[] d, int sizeBytes) {
348        validate();
349        rsnAllocationData1D(mContext, id, off, mip, count, d, sizeBytes);
350    }
351
352    native void rsnAllocationElementData1D(int con, int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes);
353    synchronized void nAllocationElementData1D(int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes) {
354        validate();
355        rsnAllocationElementData1D(mContext, id, xoff, mip, compIdx, d, sizeBytes);
356    }
357
358    native void rsnAllocationData2D(int con,
359                                    int dstAlloc, int dstXoff, int dstYoff,
360                                    int dstMip, int dstFace,
361                                    int width, int height,
362                                    int srcAlloc, int srcXoff, int srcYoff,
363                                    int srcMip, int srcFace);
364    synchronized void nAllocationData2D(int dstAlloc, int dstXoff, int dstYoff,
365                                        int dstMip, int dstFace,
366                                        int width, int height,
367                                        int srcAlloc, int srcXoff, int srcYoff,
368                                        int srcMip, int srcFace) {
369        validate();
370        rsnAllocationData2D(mContext,
371                            dstAlloc, dstXoff, dstYoff,
372                            dstMip, dstFace,
373                            width, height,
374                            srcAlloc, srcXoff, srcYoff,
375                            srcMip, srcFace);
376    }
377
378    native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, byte[] d, int sizeBytes);
379    synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, byte[] d, int sizeBytes) {
380        validate();
381        rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
382    }
383    native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, short[] d, int sizeBytes);
384    synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, short[] d, int sizeBytes) {
385        validate();
386        rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
387    }
388    native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, int[] d, int sizeBytes);
389    synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, int[] d, int sizeBytes) {
390        validate();
391        rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
392    }
393    native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, float[] d, int sizeBytes);
394    synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, int w, int h, float[] d, int sizeBytes) {
395        validate();
396        rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, w, h, d, sizeBytes);
397    }
398    native void rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, Bitmap b);
399    synchronized void nAllocationData2D(int id, int xoff, int yoff, int mip, int face, Bitmap b) {
400        validate();
401        rsnAllocationData2D(mContext, id, xoff, yoff, mip, face, b);
402    }
403
404    native void rsnAllocationRead(int con, int id, byte[] d);
405    synchronized void nAllocationRead(int id, byte[] d) {
406        validate();
407        rsnAllocationRead(mContext, id, d);
408    }
409    native void rsnAllocationRead(int con, int id, short[] d);
410    synchronized void nAllocationRead(int id, short[] d) {
411        validate();
412        rsnAllocationRead(mContext, id, d);
413    }
414    native void rsnAllocationRead(int con, int id, int[] d);
415    synchronized void nAllocationRead(int id, int[] d) {
416        validate();
417        rsnAllocationRead(mContext, id, d);
418    }
419    native void rsnAllocationRead(int con, int id, float[] d);
420    synchronized void nAllocationRead(int id, float[] d) {
421        validate();
422        rsnAllocationRead(mContext, id, d);
423    }
424    native int  rsnAllocationGetType(int con, int id);
425    synchronized int nAllocationGetType(int id) {
426        validate();
427        return rsnAllocationGetType(mContext, id);
428    }
429
430    native void rsnAllocationResize1D(int con, int id, int dimX);
431    synchronized void nAllocationResize1D(int id, int dimX) {
432        validate();
433        rsnAllocationResize1D(mContext, id, dimX);
434    }
435    native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
436    synchronized void nAllocationResize2D(int id, int dimX, int dimY) {
437        validate();
438        rsnAllocationResize2D(mContext, id, dimX, dimY);
439    }
440
441    native int  rsnFileA3DCreateFromAssetStream(int con, int assetStream);
442    synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
443        validate();
444        return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
445    }
446    native int  rsnFileA3DCreateFromFile(int con, String path);
447    synchronized int nFileA3DCreateFromFile(String path) {
448        validate();
449        return rsnFileA3DCreateFromFile(mContext, path);
450    }
451    native int  rsnFileA3DCreateFromAsset(int con, AssetManager mgr, String path);
452    synchronized int nFileA3DCreateFromAsset(AssetManager mgr, String path) {
453        validate();
454        return rsnFileA3DCreateFromAsset(mContext, mgr, path);
455    }
456    native int  rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
457    synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
458        validate();
459        return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
460    }
461    native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
462    synchronized void nFileA3DGetIndexEntries(int fileA3D, int numEntries, int[] IDs, String[] names) {
463        validate();
464        rsnFileA3DGetIndexEntries(mContext, fileA3D, numEntries, IDs, names);
465    }
466    native int  rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
467    synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
468        validate();
469        return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
470    }
471
472    native int  rsnFontCreateFromFile(int con, String fileName, float size, int dpi);
473    synchronized int nFontCreateFromFile(String fileName, float size, int dpi) {
474        validate();
475        return rsnFontCreateFromFile(mContext, fileName, size, dpi);
476    }
477    native int  rsnFontCreateFromAssetStream(int con, String name, float size, int dpi, int assetStream);
478    synchronized int nFontCreateFromAssetStream(String name, float size, int dpi, int assetStream) {
479        validate();
480        return rsnFontCreateFromAssetStream(mContext, name, size, dpi, assetStream);
481    }
482    native int  rsnFontCreateFromAsset(int con, AssetManager mgr, String path, float size, int dpi);
483    synchronized int nFontCreateFromAsset(AssetManager mgr, String path, float size, int dpi) {
484        validate();
485        return rsnFontCreateFromAsset(mContext, mgr, path, size, dpi);
486    }
487
488
489    native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
490    synchronized void nScriptBindAllocation(int script, int alloc, int slot) {
491        validate();
492        rsnScriptBindAllocation(mContext, script, alloc, slot);
493    }
494    native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
495    synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
496        validate();
497        rsnScriptSetTimeZone(mContext, script, timeZone);
498    }
499    native void rsnScriptInvoke(int con, int id, int slot);
500    synchronized void nScriptInvoke(int id, int slot) {
501        validate();
502        rsnScriptInvoke(mContext, id, slot);
503    }
504    native void rsnScriptForEach(int con, int id, int slot, int ain, int aout, byte[] params);
505    native void rsnScriptForEach(int con, int id, int slot, int ain, int aout);
506    synchronized void nScriptForEach(int id, int slot, int ain, int aout, byte[] params) {
507        validate();
508        if (params == null) {
509            rsnScriptForEach(mContext, id, slot, ain, aout);
510        } else {
511            rsnScriptForEach(mContext, id, slot, ain, aout, params);
512        }
513    }
514    native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
515    synchronized void nScriptInvokeV(int id, int slot, byte[] params) {
516        validate();
517        rsnScriptInvokeV(mContext, id, slot, params);
518    }
519    native void rsnScriptSetVarI(int con, int id, int slot, int val);
520    synchronized void nScriptSetVarI(int id, int slot, int val) {
521        validate();
522        rsnScriptSetVarI(mContext, id, slot, val);
523    }
524    native void rsnScriptSetVarJ(int con, int id, int slot, long val);
525    synchronized void nScriptSetVarJ(int id, int slot, long val) {
526        validate();
527        rsnScriptSetVarJ(mContext, id, slot, val);
528    }
529    native void rsnScriptSetVarF(int con, int id, int slot, float val);
530    synchronized void nScriptSetVarF(int id, int slot, float val) {
531        validate();
532        rsnScriptSetVarF(mContext, id, slot, val);
533    }
534    native void rsnScriptSetVarD(int con, int id, int slot, double val);
535    synchronized void nScriptSetVarD(int id, int slot, double val) {
536        validate();
537        rsnScriptSetVarD(mContext, id, slot, val);
538    }
539    native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
540    synchronized void nScriptSetVarV(int id, int slot, byte[] val) {
541        validate();
542        rsnScriptSetVarV(mContext, id, slot, val);
543    }
544    native void rsnScriptSetVarObj(int con, int id, int slot, int val);
545    synchronized void nScriptSetVarObj(int id, int slot, int val) {
546        validate();
547        rsnScriptSetVarObj(mContext, id, slot, val);
548    }
549
550    native int  rsnScriptCCreate(int con, String resName, String cacheDir,
551                                 byte[] script, int length);
552    synchronized int nScriptCCreate(String resName, String cacheDir, byte[] script, int length) {
553        validate();
554        return rsnScriptCCreate(mContext, resName, cacheDir, script, length);
555    }
556
557    native int  rsnSamplerCreate(int con, int magFilter, int minFilter,
558                                 int wrapS, int wrapT, int wrapR, float aniso);
559    synchronized int nSamplerCreate(int magFilter, int minFilter,
560                                 int wrapS, int wrapT, int wrapR, float aniso) {
561        validate();
562        return rsnSamplerCreate(mContext, magFilter, minFilter, wrapS, wrapT, wrapR, aniso);
563    }
564
565    native int  rsnProgramStoreCreate(int con, boolean r, boolean g, boolean b, boolean a,
566                                      boolean depthMask, boolean dither,
567                                      int srcMode, int dstMode, int depthFunc);
568    synchronized int nProgramStoreCreate(boolean r, boolean g, boolean b, boolean a,
569                                         boolean depthMask, boolean dither,
570                                         int srcMode, int dstMode, int depthFunc) {
571        validate();
572        return rsnProgramStoreCreate(mContext, r, g, b, a, depthMask, dither, srcMode,
573                                     dstMode, depthFunc);
574    }
575
576    native int  rsnProgramRasterCreate(int con, boolean pointSprite, int cullMode);
577    synchronized int nProgramRasterCreate(boolean pointSprite, int cullMode) {
578        validate();
579        return rsnProgramRasterCreate(mContext, pointSprite, cullMode);
580    }
581
582    native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
583    synchronized void nProgramBindConstants(int pv, int slot, int mID) {
584        validate();
585        rsnProgramBindConstants(mContext, pv, slot, mID);
586    }
587    native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
588    synchronized void nProgramBindTexture(int vpf, int slot, int a) {
589        validate();
590        rsnProgramBindTexture(mContext, vpf, slot, a);
591    }
592    native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
593    synchronized void nProgramBindSampler(int vpf, int slot, int s) {
594        validate();
595        rsnProgramBindSampler(mContext, vpf, slot, s);
596    }
597    native int  rsnProgramFragmentCreate(int con, String shader, String[] texNames, int[] params);
598    synchronized int nProgramFragmentCreate(String shader, String[] texNames, int[] params) {
599        validate();
600        return rsnProgramFragmentCreate(mContext, shader, texNames, params);
601    }
602    native int  rsnProgramVertexCreate(int con, String shader, String[] texNames, int[] params);
603    synchronized int nProgramVertexCreate(String shader, String[] texNames, int[] params) {
604        validate();
605        return rsnProgramVertexCreate(mContext, shader, texNames, params);
606    }
607
608    native int  rsnMeshCreate(int con, int[] vtx, int[] idx, int[] prim);
609    synchronized int nMeshCreate(int[] vtx, int[] idx, int[] prim) {
610        validate();
611        return rsnMeshCreate(mContext, vtx, idx, prim);
612    }
613    native int  rsnMeshGetVertexBufferCount(int con, int id);
614    synchronized int nMeshGetVertexBufferCount(int id) {
615        validate();
616        return rsnMeshGetVertexBufferCount(mContext, id);
617    }
618    native int  rsnMeshGetIndexCount(int con, int id);
619    synchronized int nMeshGetIndexCount(int id) {
620        validate();
621        return rsnMeshGetIndexCount(mContext, id);
622    }
623    native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
624    synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
625        validate();
626        rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
627    }
628    native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
629    synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
630        validate();
631        rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
632    }
633
634    native int  rsnPathCreate(int con, int prim, boolean isStatic, int vtx, int loop, float q);
635    synchronized int nPathCreate(int prim, boolean isStatic, int vtx, int loop, float q) {
636        validate();
637        return rsnPathCreate(mContext, prim, isStatic, vtx, loop, q);
638    }
639
640    int     mDev;
641    int     mContext;
642    @SuppressWarnings({"FieldCanBeLocal"})
643    MessageThread mMessageThread;
644
645    Element mElement_U8;
646    Element mElement_I8;
647    Element mElement_U16;
648    Element mElement_I16;
649    Element mElement_U32;
650    Element mElement_I32;
651    Element mElement_U64;
652    Element mElement_I64;
653    Element mElement_F32;
654    Element mElement_F64;
655    Element mElement_BOOLEAN;
656
657    Element mElement_ELEMENT;
658    Element mElement_TYPE;
659    Element mElement_ALLOCATION;
660    Element mElement_SAMPLER;
661    Element mElement_SCRIPT;
662    Element mElement_MESH;
663    Element mElement_PROGRAM_FRAGMENT;
664    Element mElement_PROGRAM_VERTEX;
665    Element mElement_PROGRAM_RASTER;
666    Element mElement_PROGRAM_STORE;
667
668    Element mElement_A_8;
669    Element mElement_RGB_565;
670    Element mElement_RGB_888;
671    Element mElement_RGBA_5551;
672    Element mElement_RGBA_4444;
673    Element mElement_RGBA_8888;
674
675    Element mElement_FLOAT_2;
676    Element mElement_FLOAT_3;
677    Element mElement_FLOAT_4;
678
679    Element mElement_DOUBLE_2;
680    Element mElement_DOUBLE_3;
681    Element mElement_DOUBLE_4;
682
683    Element mElement_UCHAR_2;
684    Element mElement_UCHAR_3;
685    Element mElement_UCHAR_4;
686
687    Element mElement_CHAR_2;
688    Element mElement_CHAR_3;
689    Element mElement_CHAR_4;
690
691    Element mElement_USHORT_2;
692    Element mElement_USHORT_3;
693    Element mElement_USHORT_4;
694
695    Element mElement_SHORT_2;
696    Element mElement_SHORT_3;
697    Element mElement_SHORT_4;
698
699    Element mElement_UINT_2;
700    Element mElement_UINT_3;
701    Element mElement_UINT_4;
702
703    Element mElement_INT_2;
704    Element mElement_INT_3;
705    Element mElement_INT_4;
706
707    Element mElement_ULONG_2;
708    Element mElement_ULONG_3;
709    Element mElement_ULONG_4;
710
711    Element mElement_LONG_2;
712    Element mElement_LONG_3;
713    Element mElement_LONG_4;
714
715    Element mElement_MATRIX_4X4;
716    Element mElement_MATRIX_3X3;
717    Element mElement_MATRIX_2X2;
718
719    Sampler mSampler_CLAMP_NEAREST;
720    Sampler mSampler_CLAMP_LINEAR;
721    Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
722    Sampler mSampler_WRAP_NEAREST;
723    Sampler mSampler_WRAP_LINEAR;
724    Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
725
726    ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
727    ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
728    ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
729    ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
730
731    ProgramRaster mProgramRaster_CULL_BACK;
732    ProgramRaster mProgramRaster_CULL_FRONT;
733    ProgramRaster mProgramRaster_CULL_NONE;
734
735    ///////////////////////////////////////////////////////////////////////////////////
736    //
737
738    /**
739     * Base class application should derive from for handling RS messages
740     * coming from their scripts.  When a script calls sendToClient the data
741     * fields will be filled in and then the run method called by a message
742     * handling thread.  This will occur some time after sendToClient completes
743     * in the script.
744     *
745     */
746    public static class RSMessageHandler implements Runnable {
747        protected int[] mData;
748        protected int mID;
749        protected int mLength;
750        public void run() {
751        }
752    }
753    /**
754     * If an application is expecting messages it should set this field to an
755     * instance of RSMessage.  This instance will receive all the user messages
756     * sent from sendToClient by scripts from this context.
757     *
758     */
759    RSMessageHandler mMessageCallback = null;
760
761    public void setMessageHandler(RSMessageHandler msg) {
762        mMessageCallback = msg;
763    }
764    public RSMessageHandler getMessageHandler() {
765        return mMessageCallback;
766    }
767
768    /**
769     * Runtime error base class.  An application should derive from this class
770     * if it wishes to install an error handler.  When errors occur at runtime
771     * the fields in this class will be filled and the run method called.
772     *
773     */
774    public static class RSErrorHandler implements Runnable {
775        protected String mErrorMessage;
776        protected int mErrorNum;
777        public void run() {
778        }
779    }
780
781    /**
782     * Application Error handler.  All runtime errors will be dispatched to the
783     * instance of RSAsyncError set here.  If this field is null a
784     * RSRuntimeException will instead be thrown with details about the error.
785     * This will cause program termaination.
786     *
787     */
788    RSErrorHandler mErrorCallback = null;
789
790    public void setErrorHandler(RSErrorHandler msg) {
791        mErrorCallback = msg;
792    }
793    public RSErrorHandler getErrorHandler() {
794        return mErrorCallback;
795    }
796
797    /**
798     * RenderScript worker threads priority enumeration.  The default value is
799     * NORMAL.  Applications wishing to do background processing such as
800     * wallpapers should set their priority to LOW to avoid starving forground
801     * processes.
802     */
803    public enum Priority {
804        LOW (Process.THREAD_PRIORITY_BACKGROUND + (5 * Process.THREAD_PRIORITY_LESS_FAVORABLE)),
805        NORMAL (Process.THREAD_PRIORITY_DISPLAY);
806
807        int mID;
808        Priority(int id) {
809            mID = id;
810        }
811    }
812
813    void validate() {
814        if (mContext == 0) {
815            throw new RSInvalidStateException("Calling RS with no Context active.");
816        }
817    }
818
819
820    /**
821     * Change the priority of the worker threads for this context.
822     *
823     * @param p New priority to be set.
824     */
825    public void setPriority(Priority p) {
826        validate();
827        nContextSetPriority(p.mID);
828    }
829
830    static class MessageThread extends Thread {
831        RenderScript mRS;
832        boolean mRun = true;
833        int[] mAuxData = new int[2];
834
835        static final int RS_MESSAGE_TO_CLIENT_NONE = 0;
836        static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1;
837        static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
838        static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
839        static final int RS_MESSAGE_TO_CLIENT_USER = 4;
840
841        static final int RS_ERROR_FATAL_UNKNOWN = 0x1000;
842
843        MessageThread(RenderScript rs) {
844            super("RSMessageThread");
845            mRS = rs;
846
847        }
848
849        public void run() {
850            // This function is a temporary solution.  The final solution will
851            // used typed allocations where the message id is the type indicator.
852            int[] rbuf = new int[16];
853            mRS.nContextInitToClient(mRS.mContext);
854            while(mRun) {
855                rbuf[0] = 0;
856                int msg = mRS.nContextPeekMessage(mRS.mContext, mAuxData);
857                int size = mAuxData[1];
858                int subID = mAuxData[0];
859
860                if (msg == RS_MESSAGE_TO_CLIENT_USER) {
861                    if ((size>>2) >= rbuf.length) {
862                        rbuf = new int[(size + 3) >> 2];
863                    }
864                    if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) !=
865                        RS_MESSAGE_TO_CLIENT_USER) {
866                        throw new RSDriverException("Error processing message from Renderscript.");
867                    }
868
869                    if(mRS.mMessageCallback != null) {
870                        mRS.mMessageCallback.mData = rbuf;
871                        mRS.mMessageCallback.mID = subID;
872                        mRS.mMessageCallback.mLength = size;
873                        mRS.mMessageCallback.run();
874                    } else {
875                        throw new RSInvalidStateException("Received a message from the script with no message handler installed.");
876                    }
877                    continue;
878                }
879
880                if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
881                    String e = mRS.nContextGetErrorMessage(mRS.mContext);
882
883                    if (subID >= RS_ERROR_FATAL_UNKNOWN) {
884                        throw new RSRuntimeException("Fatal error " + subID + ", details: " + e);
885                    }
886
887                    if(mRS.mErrorCallback != null) {
888                        mRS.mErrorCallback.mErrorMessage = e;
889                        mRS.mErrorCallback.mErrorNum = subID;
890                        mRS.mErrorCallback.run();
891                    } else {
892                        // Do not throw here. In these cases, we do not have
893                        // a fatal error.
894                    }
895                    continue;
896                }
897
898                // 2: teardown.
899                // But we want to avoid starving other threads during
900                // teardown by yielding until the next line in the destructor
901                // can execute to set mRun = false
902                try {
903                    sleep(1, 0);
904                } catch(InterruptedException e) {
905                }
906            }
907            Log.d(LOG_TAG, "MessageThread exiting.");
908        }
909    }
910
911    RenderScript(Context ctx) {
912        if (ctx != null) {
913            mApplicationContext = ctx.getApplicationContext();
914        }
915    }
916
917    /**
918     * Gets the application context associated with the RenderScript context.
919     *
920     * @return The application context.
921     */
922    public final Context getApplicationContext() {
923        return mApplicationContext;
924    }
925
926    /**
927     * Create a basic RenderScript context.
928     *
929     * @hide
930     * @param ctx The context.
931     * @return RenderScript
932     */
933    public static RenderScript create(Context ctx, int sdkVersion) {
934        RenderScript rs = new RenderScript(ctx);
935
936        rs.mDev = rs.nDeviceCreate();
937        rs.mContext = rs.nContextCreate(rs.mDev, 0, sdkVersion);
938        if (rs.mContext == 0) {
939            throw new RSDriverException("Failed to create RS context.");
940        }
941        rs.mMessageThread = new MessageThread(rs);
942        rs.mMessageThread.start();
943        return rs;
944    }
945
946    /**
947     * Create a basic RenderScript context.
948     *
949     * @param ctx The context.
950     * @return RenderScript
951     */
952    public static RenderScript create(Context ctx) {
953        int v = ctx.getApplicationInfo().targetSdkVersion;
954        return create(ctx, v);
955    }
956
957    /**
958     * Print the currently available debugging information about the state of
959     * the RS context to the log.
960     *
961     */
962    public void contextDump() {
963        validate();
964        nContextDump(0);
965    }
966
967    /**
968     * Wait for any commands in the fifo between the java bindings and native to
969     * be processed.
970     *
971     */
972    public void finish() {
973        nContextFinish();
974    }
975
976    /**
977     * Destroy this renderscript context.  Once this function is called its no
978     * longer legal to use this or any objects created by this context.
979     *
980     */
981    public void destroy() {
982        validate();
983        nContextDeinitToClient(mContext);
984        mMessageThread.mRun = false;
985        try {
986            mMessageThread.join();
987        } catch(InterruptedException e) {
988        }
989
990        nContextDestroy();
991        mContext = 0;
992
993        nDeviceDestroy(mDev);
994        mDev = 0;
995    }
996
997    boolean isAlive() {
998        return mContext != 0;
999    }
1000
1001    int safeID(BaseObj o) {
1002        if(o != null) {
1003            return o.getID();
1004        }
1005        return 0;
1006    }
1007}
1008