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