• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.renderscript;
18 
19 import java.io.File;
20 import java.lang.reflect.Field;
21 
22 import android.content.Context;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.PackageManager;
25 import android.content.res.AssetManager;
26 import android.graphics.Bitmap;
27 import android.graphics.BitmapFactory;
28 import android.graphics.SurfaceTexture;
29 import android.os.Process;
30 import android.util.Log;
31 import 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  **/
47 public 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;
_nInit()61     native static void _nInit();
62 
63 
64     static {
65         sInitialized = false;
66         try {
67             System.loadLibrary("rs_jni");
_nInit()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.
nDeviceCreate()77     native int  nDeviceCreate();
nDeviceDestroy(int dev)78     native void nDeviceDestroy(int dev);
nDeviceSetConfig(int dev, int param, int value)79     native void nDeviceSetConfig(int dev, int param, int value);
nContextGetUserMessage(int con, int[] data)80     native int nContextGetUserMessage(int con, int[] data);
nContextGetErrorMessage(int con)81     native String nContextGetErrorMessage(int con);
nContextPeekMessage(int con, int[] subID)82     native int  nContextPeekMessage(int con, int[] subID);
nContextInitToClient(int con)83     native void nContextInitToClient(int con);
nContextDeinitToClient(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      */
setupDiskCache(File cacheDir)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.
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)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);
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)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     }
rsnContextCreate(int dev, int ver, int sdkVer)125     native int  rsnContextCreate(int dev, int ver, int sdkVer);
nContextCreate(int dev, int ver, int sdkVer)126     synchronized int nContextCreate(int dev, int ver, int sdkVer) {
127         return rsnContextCreate(dev, ver, sdkVer);
128     }
rsnContextDestroy(int con)129     native void rsnContextDestroy(int con);
nContextDestroy()130     synchronized void nContextDestroy() {
131         validate();
132         rsnContextDestroy(mContext);
133     }
rsnContextSetSurface(int con, int w, int h, Surface sur)134     native void rsnContextSetSurface(int con, int w, int h, Surface sur);
nContextSetSurface(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     }
rsnContextSetSurfaceTexture(int con, int w, int h, SurfaceTexture sur)139     native void rsnContextSetSurfaceTexture(int con, int w, int h, SurfaceTexture sur);
nContextSetSurfaceTexture(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     }
rsnContextSetPriority(int con, int p)144     native void rsnContextSetPriority(int con, int p);
nContextSetPriority(int p)145     synchronized void nContextSetPriority(int p) {
146         validate();
147         rsnContextSetPriority(mContext, p);
148     }
rsnContextDump(int con, int bits)149     native void rsnContextDump(int con, int bits);
nContextDump(int bits)150     synchronized void nContextDump(int bits) {
151         validate();
152         rsnContextDump(mContext, bits);
153     }
rsnContextFinish(int con)154     native void rsnContextFinish(int con);
nContextFinish()155     synchronized void nContextFinish() {
156         validate();
157         rsnContextFinish(mContext);
158     }
159 
rsnContextBindRootScript(int con, int script)160     native void rsnContextBindRootScript(int con, int script);
nContextBindRootScript(int script)161     synchronized void nContextBindRootScript(int script) {
162         validate();
163         rsnContextBindRootScript(mContext, script);
164     }
rsnContextBindSampler(int con, int sampler, int slot)165     native void rsnContextBindSampler(int con, int sampler, int slot);
nContextBindSampler(int sampler, int slot)166     synchronized void nContextBindSampler(int sampler, int slot) {
167         validate();
168         rsnContextBindSampler(mContext, sampler, slot);
169     }
rsnContextBindProgramStore(int con, int pfs)170     native void rsnContextBindProgramStore(int con, int pfs);
nContextBindProgramStore(int pfs)171     synchronized void nContextBindProgramStore(int pfs) {
172         validate();
173         rsnContextBindProgramStore(mContext, pfs);
174     }
rsnContextBindProgramFragment(int con, int pf)175     native void rsnContextBindProgramFragment(int con, int pf);
nContextBindProgramFragment(int pf)176     synchronized void nContextBindProgramFragment(int pf) {
177         validate();
178         rsnContextBindProgramFragment(mContext, pf);
179     }
rsnContextBindProgramVertex(int con, int pv)180     native void rsnContextBindProgramVertex(int con, int pv);
nContextBindProgramVertex(int pv)181     synchronized void nContextBindProgramVertex(int pv) {
182         validate();
183         rsnContextBindProgramVertex(mContext, pv);
184     }
rsnContextBindProgramRaster(int con, int pr)185     native void rsnContextBindProgramRaster(int con, int pr);
nContextBindProgramRaster(int pr)186     synchronized void nContextBindProgramRaster(int pr) {
187         validate();
188         rsnContextBindProgramRaster(mContext, pr);
189     }
rsnContextPause(int con)190     native void rsnContextPause(int con);
nContextPause()191     synchronized void nContextPause() {
192         validate();
193         rsnContextPause(mContext);
194     }
rsnContextResume(int con)195     native void rsnContextResume(int con);
nContextResume()196     synchronized void nContextResume() {
197         validate();
198         rsnContextResume(mContext);
199     }
200 
rsnAssignName(int con, int obj, byte[] name)201     native void rsnAssignName(int con, int obj, byte[] name);
nAssignName(int obj, byte[] name)202     synchronized void nAssignName(int obj, byte[] name) {
203         validate();
204         rsnAssignName(mContext, obj, name);
205     }
rsnGetName(int con, int obj)206     native String rsnGetName(int con, int obj);
nGetName(int obj)207     synchronized String nGetName(int obj) {
208         validate();
209         return rsnGetName(mContext, obj);
210     }
rsnObjDestroy(int con, int id)211     native void rsnObjDestroy(int con, int id);
nObjDestroy(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 
rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize)221     native int  rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
nElementCreate(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     }
rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes)226     native int  rsnElementCreate2(int con, int[] elements, String[] names, int[] arraySizes);
nElementCreate2(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     }
rsnElementGetNativeData(int con, int id, int[] elementData)231     native void rsnElementGetNativeData(int con, int id, int[] elementData);
nElementGetNativeData(int id, int[] elementData)232     synchronized void nElementGetNativeData(int id, int[] elementData) {
233         validate();
234         rsnElementGetNativeData(mContext, id, elementData);
235     }
rsnElementGetSubElements(int con, int id, int[] IDs, String[] names, int[] arraySizes)236     native void rsnElementGetSubElements(int con, int id,
237                                          int[] IDs, String[] names, int[] arraySizes);
nElementGetSubElements(int id, 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 
rsnTypeCreate(int con, int eid, int x, int y, int z, boolean mips, boolean faces)243     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)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     }
rsnTypeGetNativeData(int con, int id, int[] typeData)248     native void rsnTypeGetNativeData(int con, int id, int[] typeData);
nTypeGetNativeData(int id, int[] typeData)249     synchronized void nTypeGetNativeData(int id, int[] typeData) {
250         validate();
251         rsnTypeGetNativeData(mContext, id, typeData);
252     }
253 
rsnAllocationCreateTyped(int con, int type, int mip, int usage, int pointer)254     native int  rsnAllocationCreateTyped(int con, int type, int mip, int usage, int pointer);
nAllocationCreateTyped(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     }
rsnAllocationCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage)259     native int  rsnAllocationCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
nAllocationCreateFromBitmap(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     }
rsnAllocationCubeCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage)264     native int  rsnAllocationCubeCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
nAllocationCubeCreateFromBitmap(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     }
rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp)269     native int  rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
nAllocationCreateBitmapRef(int type, Bitmap bmp)270     synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
271         validate();
272         return rsnAllocationCreateBitmapRef(mContext, type, bmp);
273     }
rsnAllocationCreateFromAssetStream(int con, int mips, int assetStream, int usage)274     native int  rsnAllocationCreateFromAssetStream(int con, int mips, int assetStream, int usage);
nAllocationCreateFromAssetStream(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 
rsnAllocationCopyToBitmap(int con, int alloc, Bitmap bmp)280     native void  rsnAllocationCopyToBitmap(int con, int alloc, Bitmap bmp);
nAllocationCopyToBitmap(int alloc, Bitmap bmp)281     synchronized void nAllocationCopyToBitmap(int alloc, Bitmap bmp) {
282         validate();
283         rsnAllocationCopyToBitmap(mContext, alloc, bmp);
284     }
285 
286 
rsnAllocationSyncAll(int con, int alloc, int src)287     native void rsnAllocationSyncAll(int con, int alloc, int src);
nAllocationSyncAll(int alloc, int src)288     synchronized void nAllocationSyncAll(int alloc, int src) {
289         validate();
290         rsnAllocationSyncAll(mContext, alloc, src);
291     }
rsnAllocationGetSurfaceTextureID(int con, int alloc)292     native int rsnAllocationGetSurfaceTextureID(int con, int alloc);
nAllocationGetSurfaceTextureID(int alloc)293     synchronized int nAllocationGetSurfaceTextureID(int alloc) {
294         validate();
295         return rsnAllocationGetSurfaceTextureID(mContext, alloc);
296     }
rsnAllocationGetSurfaceTextureID2(int con, int alloc, SurfaceTexture st)297     native void rsnAllocationGetSurfaceTextureID2(int con, int alloc, SurfaceTexture st);
nAllocationGetSurfaceTextureID2(int alloc, SurfaceTexture st)298     synchronized void nAllocationGetSurfaceTextureID2(int alloc, SurfaceTexture st) {
299         validate();
300         rsnAllocationGetSurfaceTextureID2(mContext, alloc, st);
301     }
rsnAllocationSetSurface(int con, int alloc, Surface sur)302     native void rsnAllocationSetSurface(int con, int alloc, Surface sur);
nAllocationSetSurface(int alloc, Surface sur)303     synchronized void nAllocationSetSurface(int alloc, Surface sur) {
304         validate();
305         rsnAllocationSetSurface(mContext, alloc, sur);
306     }
rsnAllocationIoSend(int con, int alloc)307     native void rsnAllocationIoSend(int con, int alloc);
nAllocationIoSend(int alloc)308     synchronized void nAllocationIoSend(int alloc) {
309         validate();
310         rsnAllocationIoSend(mContext, alloc);
311     }
rsnAllocationIoReceive(int con, int alloc)312     native void rsnAllocationIoReceive(int con, int alloc);
nAllocationIoReceive(int alloc)313     synchronized void nAllocationIoReceive(int alloc) {
314         validate();
315         rsnAllocationIoReceive(mContext, alloc);
316     }
317 
318 
rsnAllocationGenerateMipmaps(int con, int alloc)319     native void rsnAllocationGenerateMipmaps(int con, int alloc);
nAllocationGenerateMipmaps(int alloc)320     synchronized void nAllocationGenerateMipmaps(int alloc) {
321         validate();
322         rsnAllocationGenerateMipmaps(mContext, alloc);
323     }
rsnAllocationCopyFromBitmap(int con, int alloc, Bitmap bmp)324     native void  rsnAllocationCopyFromBitmap(int con, int alloc, Bitmap bmp);
nAllocationCopyFromBitmap(int alloc, Bitmap bmp)325     synchronized void nAllocationCopyFromBitmap(int alloc, Bitmap bmp) {
326         validate();
327         rsnAllocationCopyFromBitmap(mContext, alloc, bmp);
328     }
329 
330 
rsnAllocationData1D(int con, int id, int off, int mip, int count, int[] d, int sizeBytes)331     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)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     }
rsnAllocationData1D(int con, int id, int off, int mip, int count, short[] d, int sizeBytes)336     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)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     }
rsnAllocationData1D(int con, int id, int off, int mip, int count, byte[] d, int sizeBytes)341     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)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     }
rsnAllocationData1D(int con, int id, int off, int mip, int count, float[] d, int sizeBytes)346     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)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 
rsnAllocationElementData1D(int con, int id, int xoff, int mip, int compIdx, byte[] d, int sizeBytes)352     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)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 
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)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);
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)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 
rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, byte[] d, int sizeBytes)378     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)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     }
rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, short[] d, int sizeBytes)383     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)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     }
rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, int[] d, int sizeBytes)388     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)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     }
rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, int w, int h, float[] d, int sizeBytes)393     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)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     }
rsnAllocationData2D(int con, int id, int xoff, int yoff, int mip, int face, Bitmap b)398     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)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 
rsnAllocationRead(int con, int id, byte[] d)404     native void rsnAllocationRead(int con, int id, byte[] d);
nAllocationRead(int id, byte[] d)405     synchronized void nAllocationRead(int id, byte[] d) {
406         validate();
407         rsnAllocationRead(mContext, id, d);
408     }
rsnAllocationRead(int con, int id, short[] d)409     native void rsnAllocationRead(int con, int id, short[] d);
nAllocationRead(int id, short[] d)410     synchronized void nAllocationRead(int id, short[] d) {
411         validate();
412         rsnAllocationRead(mContext, id, d);
413     }
rsnAllocationRead(int con, int id, int[] d)414     native void rsnAllocationRead(int con, int id, int[] d);
nAllocationRead(int id, int[] d)415     synchronized void nAllocationRead(int id, int[] d) {
416         validate();
417         rsnAllocationRead(mContext, id, d);
418     }
rsnAllocationRead(int con, int id, float[] d)419     native void rsnAllocationRead(int con, int id, float[] d);
nAllocationRead(int id, float[] d)420     synchronized void nAllocationRead(int id, float[] d) {
421         validate();
422         rsnAllocationRead(mContext, id, d);
423     }
rsnAllocationGetType(int con, int id)424     native int  rsnAllocationGetType(int con, int id);
nAllocationGetType(int id)425     synchronized int nAllocationGetType(int id) {
426         validate();
427         return rsnAllocationGetType(mContext, id);
428     }
429 
rsnAllocationResize1D(int con, int id, int dimX)430     native void rsnAllocationResize1D(int con, int id, int dimX);
nAllocationResize1D(int id, int dimX)431     synchronized void nAllocationResize1D(int id, int dimX) {
432         validate();
433         rsnAllocationResize1D(mContext, id, dimX);
434     }
rsnAllocationResize2D(int con, int id, int dimX, int dimY)435     native void rsnAllocationResize2D(int con, int id, int dimX, int dimY);
nAllocationResize2D(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 
rsnFileA3DCreateFromAssetStream(int con, int assetStream)441     native int  rsnFileA3DCreateFromAssetStream(int con, int assetStream);
nFileA3DCreateFromAssetStream(int assetStream)442     synchronized int nFileA3DCreateFromAssetStream(int assetStream) {
443         validate();
444         return rsnFileA3DCreateFromAssetStream(mContext, assetStream);
445     }
rsnFileA3DCreateFromFile(int con, String path)446     native int  rsnFileA3DCreateFromFile(int con, String path);
nFileA3DCreateFromFile(String path)447     synchronized int nFileA3DCreateFromFile(String path) {
448         validate();
449         return rsnFileA3DCreateFromFile(mContext, path);
450     }
rsnFileA3DCreateFromAsset(int con, AssetManager mgr, String path)451     native int  rsnFileA3DCreateFromAsset(int con, AssetManager mgr, String path);
nFileA3DCreateFromAsset(AssetManager mgr, String path)452     synchronized int nFileA3DCreateFromAsset(AssetManager mgr, String path) {
453         validate();
454         return rsnFileA3DCreateFromAsset(mContext, mgr, path);
455     }
rsnFileA3DGetNumIndexEntries(int con, int fileA3D)456     native int  rsnFileA3DGetNumIndexEntries(int con, int fileA3D);
nFileA3DGetNumIndexEntries(int fileA3D)457     synchronized int nFileA3DGetNumIndexEntries(int fileA3D) {
458         validate();
459         return rsnFileA3DGetNumIndexEntries(mContext, fileA3D);
460     }
rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names)461     native void rsnFileA3DGetIndexEntries(int con, int fileA3D, int numEntries, int[] IDs, String[] names);
nFileA3DGetIndexEntries(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     }
rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index)466     native int  rsnFileA3DGetEntryByIndex(int con, int fileA3D, int index);
nFileA3DGetEntryByIndex(int fileA3D, int index)467     synchronized int nFileA3DGetEntryByIndex(int fileA3D, int index) {
468         validate();
469         return rsnFileA3DGetEntryByIndex(mContext, fileA3D, index);
470     }
471 
rsnFontCreateFromFile(int con, String fileName, float size, int dpi)472     native int  rsnFontCreateFromFile(int con, String fileName, float size, int dpi);
nFontCreateFromFile(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     }
rsnFontCreateFromAssetStream(int con, String name, float size, int dpi, int assetStream)477     native int  rsnFontCreateFromAssetStream(int con, String name, float size, int dpi, int assetStream);
nFontCreateFromAssetStream(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     }
rsnFontCreateFromAsset(int con, AssetManager mgr, String path, float size, int dpi)482     native int  rsnFontCreateFromAsset(int con, AssetManager mgr, String path, float size, int dpi);
nFontCreateFromAsset(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 
rsnScriptBindAllocation(int con, int script, int alloc, int slot)489     native void rsnScriptBindAllocation(int con, int script, int alloc, int slot);
nScriptBindAllocation(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     }
rsnScriptSetTimeZone(int con, int script, byte[] timeZone)494     native void rsnScriptSetTimeZone(int con, int script, byte[] timeZone);
nScriptSetTimeZone(int script, byte[] timeZone)495     synchronized void nScriptSetTimeZone(int script, byte[] timeZone) {
496         validate();
497         rsnScriptSetTimeZone(mContext, script, timeZone);
498     }
rsnScriptInvoke(int con, int id, int slot)499     native void rsnScriptInvoke(int con, int id, int slot);
nScriptInvoke(int id, int slot)500     synchronized void nScriptInvoke(int id, int slot) {
501         validate();
502         rsnScriptInvoke(mContext, id, slot);
503     }
rsnScriptForEach(int con, int id, int slot, int ain, int aout, byte[] params)504     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)505     native void rsnScriptForEach(int con, int id, int slot, int ain, int aout);
nScriptForEach(int id, int slot, int ain, int aout, byte[] params)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     }
rsnScriptInvokeV(int con, int id, int slot, byte[] params)514     native void rsnScriptInvokeV(int con, int id, int slot, byte[] params);
nScriptInvokeV(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     }
rsnScriptSetVarI(int con, int id, int slot, int val)519     native void rsnScriptSetVarI(int con, int id, int slot, int val);
nScriptSetVarI(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     }
rsnScriptSetVarJ(int con, int id, int slot, long val)524     native void rsnScriptSetVarJ(int con, int id, int slot, long val);
nScriptSetVarJ(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     }
rsnScriptSetVarF(int con, int id, int slot, float val)529     native void rsnScriptSetVarF(int con, int id, int slot, float val);
nScriptSetVarF(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     }
rsnScriptSetVarD(int con, int id, int slot, double val)534     native void rsnScriptSetVarD(int con, int id, int slot, double val);
nScriptSetVarD(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     }
rsnScriptSetVarV(int con, int id, int slot, byte[] val)539     native void rsnScriptSetVarV(int con, int id, int slot, byte[] val);
nScriptSetVarV(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     }
rsnScriptSetVarVE(int con, int id, int slot, byte[] val, int e, int[] dims)544     native void rsnScriptSetVarVE(int con, int id, int slot, byte[] val,
545                                   int e, int[] dims);
nScriptSetVarVE(int id, int slot, byte[] val, int e, int[] dims)546     synchronized void nScriptSetVarVE(int id, int slot, byte[] val,
547                                       int e, int[] dims) {
548         validate();
549         rsnScriptSetVarVE(mContext, id, slot, val, e, dims);
550     }
rsnScriptSetVarObj(int con, int id, int slot, int val)551     native void rsnScriptSetVarObj(int con, int id, int slot, int val);
nScriptSetVarObj(int id, int slot, int val)552     synchronized void nScriptSetVarObj(int id, int slot, int val) {
553         validate();
554         rsnScriptSetVarObj(mContext, id, slot, val);
555     }
556 
rsnScriptCCreate(int con, String resName, String cacheDir, byte[] script, int length)557     native int  rsnScriptCCreate(int con, String resName, String cacheDir,
558                                  byte[] script, int length);
nScriptCCreate(String resName, String cacheDir, byte[] script, int length)559     synchronized int nScriptCCreate(String resName, String cacheDir, byte[] script, int length) {
560         validate();
561         return rsnScriptCCreate(mContext, resName, cacheDir, script, length);
562     }
563 
rsnSamplerCreate(int con, int magFilter, int minFilter, int wrapS, int wrapT, int wrapR, float aniso)564     native int  rsnSamplerCreate(int con, int magFilter, int minFilter,
565                                  int wrapS, int wrapT, int wrapR, float aniso);
nSamplerCreate(int magFilter, int minFilter, int wrapS, int wrapT, int wrapR, float aniso)566     synchronized int nSamplerCreate(int magFilter, int minFilter,
567                                  int wrapS, int wrapT, int wrapR, float aniso) {
568         validate();
569         return rsnSamplerCreate(mContext, magFilter, minFilter, wrapS, wrapT, wrapR, aniso);
570     }
571 
rsnProgramStoreCreate(int con, boolean r, boolean g, boolean b, boolean a, boolean depthMask, boolean dither, int srcMode, int dstMode, int depthFunc)572     native int  rsnProgramStoreCreate(int con, boolean r, boolean g, boolean b, boolean a,
573                                       boolean depthMask, boolean dither,
574                                       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)575     synchronized int nProgramStoreCreate(boolean r, boolean g, boolean b, boolean a,
576                                          boolean depthMask, boolean dither,
577                                          int srcMode, int dstMode, int depthFunc) {
578         validate();
579         return rsnProgramStoreCreate(mContext, r, g, b, a, depthMask, dither, srcMode,
580                                      dstMode, depthFunc);
581     }
582 
rsnProgramRasterCreate(int con, boolean pointSprite, int cullMode)583     native int  rsnProgramRasterCreate(int con, boolean pointSprite, int cullMode);
nProgramRasterCreate(boolean pointSprite, int cullMode)584     synchronized int nProgramRasterCreate(boolean pointSprite, int cullMode) {
585         validate();
586         return rsnProgramRasterCreate(mContext, pointSprite, cullMode);
587     }
588 
rsnProgramBindConstants(int con, int pv, int slot, int mID)589     native void rsnProgramBindConstants(int con, int pv, int slot, int mID);
nProgramBindConstants(int pv, int slot, int mID)590     synchronized void nProgramBindConstants(int pv, int slot, int mID) {
591         validate();
592         rsnProgramBindConstants(mContext, pv, slot, mID);
593     }
rsnProgramBindTexture(int con, int vpf, int slot, int a)594     native void rsnProgramBindTexture(int con, int vpf, int slot, int a);
nProgramBindTexture(int vpf, int slot, int a)595     synchronized void nProgramBindTexture(int vpf, int slot, int a) {
596         validate();
597         rsnProgramBindTexture(mContext, vpf, slot, a);
598     }
rsnProgramBindSampler(int con, int vpf, int slot, int s)599     native void rsnProgramBindSampler(int con, int vpf, int slot, int s);
nProgramBindSampler(int vpf, int slot, int s)600     synchronized void nProgramBindSampler(int vpf, int slot, int s) {
601         validate();
602         rsnProgramBindSampler(mContext, vpf, slot, s);
603     }
rsnProgramFragmentCreate(int con, String shader, String[] texNames, int[] params)604     native int  rsnProgramFragmentCreate(int con, String shader, String[] texNames, int[] params);
nProgramFragmentCreate(String shader, String[] texNames, int[] params)605     synchronized int nProgramFragmentCreate(String shader, String[] texNames, int[] params) {
606         validate();
607         return rsnProgramFragmentCreate(mContext, shader, texNames, params);
608     }
rsnProgramVertexCreate(int con, String shader, String[] texNames, int[] params)609     native int  rsnProgramVertexCreate(int con, String shader, String[] texNames, int[] params);
nProgramVertexCreate(String shader, String[] texNames, int[] params)610     synchronized int nProgramVertexCreate(String shader, String[] texNames, int[] params) {
611         validate();
612         return rsnProgramVertexCreate(mContext, shader, texNames, params);
613     }
614 
rsnMeshCreate(int con, int[] vtx, int[] idx, int[] prim)615     native int  rsnMeshCreate(int con, int[] vtx, int[] idx, int[] prim);
nMeshCreate(int[] vtx, int[] idx, int[] prim)616     synchronized int nMeshCreate(int[] vtx, int[] idx, int[] prim) {
617         validate();
618         return rsnMeshCreate(mContext, vtx, idx, prim);
619     }
rsnMeshGetVertexBufferCount(int con, int id)620     native int  rsnMeshGetVertexBufferCount(int con, int id);
nMeshGetVertexBufferCount(int id)621     synchronized int nMeshGetVertexBufferCount(int id) {
622         validate();
623         return rsnMeshGetVertexBufferCount(mContext, id);
624     }
rsnMeshGetIndexCount(int con, int id)625     native int  rsnMeshGetIndexCount(int con, int id);
nMeshGetIndexCount(int id)626     synchronized int nMeshGetIndexCount(int id) {
627         validate();
628         return rsnMeshGetIndexCount(mContext, id);
629     }
rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount)630     native void rsnMeshGetVertices(int con, int id, int[] vtxIds, int vtxIdCount);
nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount)631     synchronized void nMeshGetVertices(int id, int[] vtxIds, int vtxIdCount) {
632         validate();
633         rsnMeshGetVertices(mContext, id, vtxIds, vtxIdCount);
634     }
rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount)635     native void rsnMeshGetIndices(int con, int id, int[] idxIds, int[] primitives, int vtxIdCount);
nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount)636     synchronized void nMeshGetIndices(int id, int[] idxIds, int[] primitives, int vtxIdCount) {
637         validate();
638         rsnMeshGetIndices(mContext, id, idxIds, primitives, vtxIdCount);
639     }
640 
rsnPathCreate(int con, int prim, boolean isStatic, int vtx, int loop, float q)641     native int  rsnPathCreate(int con, int prim, boolean isStatic, int vtx, int loop, float q);
nPathCreate(int prim, boolean isStatic, int vtx, int loop, float q)642     synchronized int nPathCreate(int prim, boolean isStatic, int vtx, int loop, float q) {
643         validate();
644         return rsnPathCreate(mContext, prim, isStatic, vtx, loop, q);
645     }
646 
647     int     mDev;
648     int     mContext;
649     @SuppressWarnings({"FieldCanBeLocal"})
650     MessageThread mMessageThread;
651 
652     Element mElement_U8;
653     Element mElement_I8;
654     Element mElement_U16;
655     Element mElement_I16;
656     Element mElement_U32;
657     Element mElement_I32;
658     Element mElement_U64;
659     Element mElement_I64;
660     Element mElement_F32;
661     Element mElement_F64;
662     Element mElement_BOOLEAN;
663 
664     Element mElement_ELEMENT;
665     Element mElement_TYPE;
666     Element mElement_ALLOCATION;
667     Element mElement_SAMPLER;
668     Element mElement_SCRIPT;
669     Element mElement_MESH;
670     Element mElement_PROGRAM_FRAGMENT;
671     Element mElement_PROGRAM_VERTEX;
672     Element mElement_PROGRAM_RASTER;
673     Element mElement_PROGRAM_STORE;
674     Element mElement_FONT;
675 
676     Element mElement_A_8;
677     Element mElement_RGB_565;
678     Element mElement_RGB_888;
679     Element mElement_RGBA_5551;
680     Element mElement_RGBA_4444;
681     Element mElement_RGBA_8888;
682 
683     Element mElement_FLOAT_2;
684     Element mElement_FLOAT_3;
685     Element mElement_FLOAT_4;
686 
687     Element mElement_DOUBLE_2;
688     Element mElement_DOUBLE_3;
689     Element mElement_DOUBLE_4;
690 
691     Element mElement_UCHAR_2;
692     Element mElement_UCHAR_3;
693     Element mElement_UCHAR_4;
694 
695     Element mElement_CHAR_2;
696     Element mElement_CHAR_3;
697     Element mElement_CHAR_4;
698 
699     Element mElement_USHORT_2;
700     Element mElement_USHORT_3;
701     Element mElement_USHORT_4;
702 
703     Element mElement_SHORT_2;
704     Element mElement_SHORT_3;
705     Element mElement_SHORT_4;
706 
707     Element mElement_UINT_2;
708     Element mElement_UINT_3;
709     Element mElement_UINT_4;
710 
711     Element mElement_INT_2;
712     Element mElement_INT_3;
713     Element mElement_INT_4;
714 
715     Element mElement_ULONG_2;
716     Element mElement_ULONG_3;
717     Element mElement_ULONG_4;
718 
719     Element mElement_LONG_2;
720     Element mElement_LONG_3;
721     Element mElement_LONG_4;
722 
723     Element mElement_MATRIX_4X4;
724     Element mElement_MATRIX_3X3;
725     Element mElement_MATRIX_2X2;
726 
727     Sampler mSampler_CLAMP_NEAREST;
728     Sampler mSampler_CLAMP_LINEAR;
729     Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
730     Sampler mSampler_WRAP_NEAREST;
731     Sampler mSampler_WRAP_LINEAR;
732     Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;
733 
734     ProgramStore mProgramStore_BLEND_NONE_DEPTH_TEST;
735     ProgramStore mProgramStore_BLEND_NONE_DEPTH_NO_DEPTH;
736     ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_TEST;
737     ProgramStore mProgramStore_BLEND_ALPHA_DEPTH_NO_DEPTH;
738 
739     ProgramRaster mProgramRaster_CULL_BACK;
740     ProgramRaster mProgramRaster_CULL_FRONT;
741     ProgramRaster mProgramRaster_CULL_NONE;
742 
743     ///////////////////////////////////////////////////////////////////////////////////
744     //
745 
746     /**
747      * Base class application should derive from for handling RS messages
748      * coming from their scripts.  When a script calls sendToClient the data
749      * fields will be filled in and then the run method called by a message
750      * handling thread.  This will occur some time after sendToClient completes
751      * in the script.
752      *
753      */
754     public static class RSMessageHandler implements Runnable {
755         protected int[] mData;
756         protected int mID;
757         protected int mLength;
run()758         public void run() {
759         }
760     }
761     /**
762      * If an application is expecting messages it should set this field to an
763      * instance of RSMessage.  This instance will receive all the user messages
764      * sent from sendToClient by scripts from this context.
765      *
766      */
767     RSMessageHandler mMessageCallback = null;
768 
setMessageHandler(RSMessageHandler msg)769     public void setMessageHandler(RSMessageHandler msg) {
770         mMessageCallback = msg;
771     }
getMessageHandler()772     public RSMessageHandler getMessageHandler() {
773         return mMessageCallback;
774     }
775 
776     /**
777      * Runtime error base class.  An application should derive from this class
778      * if it wishes to install an error handler.  When errors occur at runtime
779      * the fields in this class will be filled and the run method called.
780      *
781      */
782     public static class RSErrorHandler implements Runnable {
783         protected String mErrorMessage;
784         protected int mErrorNum;
run()785         public void run() {
786         }
787     }
788 
789     /**
790      * Application Error handler.  All runtime errors will be dispatched to the
791      * instance of RSAsyncError set here.  If this field is null a
792      * RSRuntimeException will instead be thrown with details about the error.
793      * This will cause program termaination.
794      *
795      */
796     RSErrorHandler mErrorCallback = null;
797 
setErrorHandler(RSErrorHandler msg)798     public void setErrorHandler(RSErrorHandler msg) {
799         mErrorCallback = msg;
800     }
getErrorHandler()801     public RSErrorHandler getErrorHandler() {
802         return mErrorCallback;
803     }
804 
805     /**
806      * RenderScript worker threads priority enumeration.  The default value is
807      * NORMAL.  Applications wishing to do background processing such as
808      * wallpapers should set their priority to LOW to avoid starving forground
809      * processes.
810      */
811     public enum Priority {
812         LOW (Process.THREAD_PRIORITY_BACKGROUND + (5 * Process.THREAD_PRIORITY_LESS_FAVORABLE)),
813         NORMAL (Process.THREAD_PRIORITY_DISPLAY);
814 
815         int mID;
Priority(int id)816         Priority(int id) {
817             mID = id;
818         }
819     }
820 
validate()821     void validate() {
822         if (mContext == 0) {
823             throw new RSInvalidStateException("Calling RS with no Context active.");
824         }
825     }
826 
827 
828     /**
829      * Change the priority of the worker threads for this context.
830      *
831      * @param p New priority to be set.
832      */
setPriority(Priority p)833     public void setPriority(Priority p) {
834         validate();
835         nContextSetPriority(p.mID);
836     }
837 
838     static class MessageThread extends Thread {
839         RenderScript mRS;
840         boolean mRun = true;
841         int[] mAuxData = new int[2];
842 
843         static final int RS_MESSAGE_TO_CLIENT_NONE = 0;
844         static final int RS_MESSAGE_TO_CLIENT_EXCEPTION = 1;
845         static final int RS_MESSAGE_TO_CLIENT_RESIZE = 2;
846         static final int RS_MESSAGE_TO_CLIENT_ERROR = 3;
847         static final int RS_MESSAGE_TO_CLIENT_USER = 4;
848 
849         static final int RS_ERROR_FATAL_UNKNOWN = 0x1000;
850 
MessageThread(RenderScript rs)851         MessageThread(RenderScript rs) {
852             super("RSMessageThread");
853             mRS = rs;
854 
855         }
856 
run()857         public void run() {
858             // This function is a temporary solution.  The final solution will
859             // used typed allocations where the message id is the type indicator.
860             int[] rbuf = new int[16];
861             mRS.nContextInitToClient(mRS.mContext);
862             while(mRun) {
863                 rbuf[0] = 0;
864                 int msg = mRS.nContextPeekMessage(mRS.mContext, mAuxData);
865                 int size = mAuxData[1];
866                 int subID = mAuxData[0];
867 
868                 if (msg == RS_MESSAGE_TO_CLIENT_USER) {
869                     if ((size>>2) >= rbuf.length) {
870                         rbuf = new int[(size + 3) >> 2];
871                     }
872                     if (mRS.nContextGetUserMessage(mRS.mContext, rbuf) !=
873                         RS_MESSAGE_TO_CLIENT_USER) {
874                         throw new RSDriverException("Error processing message from Renderscript.");
875                     }
876 
877                     if(mRS.mMessageCallback != null) {
878                         mRS.mMessageCallback.mData = rbuf;
879                         mRS.mMessageCallback.mID = subID;
880                         mRS.mMessageCallback.mLength = size;
881                         mRS.mMessageCallback.run();
882                     } else {
883                         throw new RSInvalidStateException("Received a message from the script with no message handler installed.");
884                     }
885                     continue;
886                 }
887 
888                 if (msg == RS_MESSAGE_TO_CLIENT_ERROR) {
889                     String e = mRS.nContextGetErrorMessage(mRS.mContext);
890 
891                     if (subID >= RS_ERROR_FATAL_UNKNOWN) {
892                         throw new RSRuntimeException("Fatal error " + subID + ", details: " + e);
893                     }
894 
895                     if(mRS.mErrorCallback != null) {
896                         mRS.mErrorCallback.mErrorMessage = e;
897                         mRS.mErrorCallback.mErrorNum = subID;
898                         mRS.mErrorCallback.run();
899                     } else {
900                         // Do not throw here. In these cases, we do not have
901                         // a fatal error.
902                     }
903                     continue;
904                 }
905 
906                 // 2: teardown.
907                 // But we want to avoid starving other threads during
908                 // teardown by yielding until the next line in the destructor
909                 // can execute to set mRun = false
910                 try {
911                     sleep(1, 0);
912                 } catch(InterruptedException e) {
913                 }
914             }
915             Log.d(LOG_TAG, "MessageThread exiting.");
916         }
917     }
918 
RenderScript(Context ctx)919     RenderScript(Context ctx) {
920         if (ctx != null) {
921             mApplicationContext = ctx.getApplicationContext();
922         }
923     }
924 
925     /**
926      * Gets the application context associated with the RenderScript context.
927      *
928      * @return The application context.
929      */
getApplicationContext()930     public final Context getApplicationContext() {
931         return mApplicationContext;
932     }
933 
934     /**
935      * Create a basic RenderScript context.
936      *
937      * @hide
938      * @param ctx The context.
939      * @return RenderScript
940      */
create(Context ctx, int sdkVersion)941     public static RenderScript create(Context ctx, int sdkVersion) {
942         RenderScript rs = new RenderScript(ctx);
943 
944         rs.mDev = rs.nDeviceCreate();
945         rs.mContext = rs.nContextCreate(rs.mDev, 0, sdkVersion);
946         if (rs.mContext == 0) {
947             throw new RSDriverException("Failed to create RS context.");
948         }
949         rs.mMessageThread = new MessageThread(rs);
950         rs.mMessageThread.start();
951         return rs;
952     }
953 
954     /**
955      * Create a basic RenderScript context.
956      *
957      * @param ctx The context.
958      * @return RenderScript
959      */
create(Context ctx)960     public static RenderScript create(Context ctx) {
961         int v = ctx.getApplicationInfo().targetSdkVersion;
962         return create(ctx, v);
963     }
964 
965     /**
966      * Print the currently available debugging information about the state of
967      * the RS context to the log.
968      *
969      */
contextDump()970     public void contextDump() {
971         validate();
972         nContextDump(0);
973     }
974 
975     /**
976      * Wait for any commands in the fifo between the java bindings and native to
977      * be processed.
978      *
979      */
finish()980     public void finish() {
981         nContextFinish();
982     }
983 
984     /**
985      * Destroy this renderscript context.  Once this function is called its no
986      * longer legal to use this or any objects created by this context.
987      *
988      */
destroy()989     public void destroy() {
990         validate();
991         nContextDeinitToClient(mContext);
992         mMessageThread.mRun = false;
993         try {
994             mMessageThread.join();
995         } catch(InterruptedException e) {
996         }
997 
998         nContextDestroy();
999         mContext = 0;
1000 
1001         nDeviceDestroy(mDev);
1002         mDev = 0;
1003     }
1004 
isAlive()1005     boolean isAlive() {
1006         return mContext != 0;
1007     }
1008 
safeID(BaseObj o)1009     int safeID(BaseObj o) {
1010         if(o != null) {
1011             return o.getID(this);
1012         }
1013         return 0;
1014     }
1015 }
1016