RenderScript.java revision 7e5ab3b177b10fee304d011b3a4b9ee03e2b18b5
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
19import java.lang.reflect.Field;
20
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23import android.util.Config;
24import android.util.Log;
25import android.view.Surface;
26
27
28/**
29 * @hide
30 *
31 **/
32public class RenderScript {
33    static final String LOG_TAG = "RenderScript_jni";
34    private static final boolean DEBUG  = false;
35    @SuppressWarnings({"UnusedDeclaration", "deprecation"})
36    private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
37    int mWidth;
38    int mHeight;
39
40
41
42     /*
43     * We use a class initializer to allow the native code to cache some
44     * field offsets.
45     */
46    @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
47    private static boolean sInitialized;
48    native private static void _nInit();
49
50
51    static {
52        sInitialized = false;
53        try {
54            System.loadLibrary("rs_jni");
55            _nInit();
56            sInitialized = true;
57        } catch (UnsatisfiedLinkError e) {
58            Log.d(LOG_TAG, "RenderScript JNI library not found!");
59        }
60    }
61
62    native void nInitElements(int a8, int rgba4444, int rgba8888, int rgb565);
63
64    native int  nDeviceCreate();
65    native void nDeviceDestroy(int dev);
66    native void nDeviceSetConfig(int dev, int param, int value);
67    native int  nContextCreate(int dev, int ver, boolean useDepth);
68    native void nContextDestroy(int con);
69    native void nContextSetSurface(int w, int h, Surface sur);
70    native void nContextSetPriority(int p);
71    native void nContextDump(int bits);
72
73    native void nContextBindRootScript(int script);
74    native void nContextBindSampler(int sampler, int slot);
75    native void nContextBindProgramFragmentStore(int pfs);
76    native void nContextBindProgramFragment(int pf);
77    native void nContextBindProgramVertex(int pf);
78    native void nContextBindProgramRaster(int pr);
79    native void nContextAddDefineI32(String name, int value);
80    native void nContextAddDefineF(String name, float value);
81    native void nContextPause();
82    native void nContextResume();
83    native int nContextGetMessage(int[] data, boolean wait);
84    native void nContextInitToClient();
85    native void nContextDeinitToClient();
86
87    native void nAssignName(int obj, byte[] name);
88    native void nObjDestroy(int id);
89    native void nObjDestroyOOB(int id);
90    native int  nFileOpen(byte[] name);
91
92    native void nElementBegin();
93    native void nElementAdd(int kind, int type, boolean norm, int bits, String s);
94    native int  nElementCreate();
95
96    native void nTypeBegin(int elementID);
97    native void nTypeAdd(int dim, int val);
98    native int  nTypeCreate();
99    native void nTypeFinalDestroy(Type t);
100    native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs);
101
102    native int  nAllocationCreateTyped(int type);
103    native int  nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
104    native int  nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp);
105    native int  nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream);
106
107    native void nAllocationUploadToTexture(int alloc, int baseMioLevel);
108    native void nAllocationUploadToBufferObject(int alloc);
109
110    native void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes);
111    native void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes);
112    native void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes);
113    native void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes);
114
115    native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
116    native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
117    native void nAllocationRead(int id, int[] d);
118    native void nAllocationRead(int id, float[] d);
119    native void nAllocationSubDataFromObject(int id, Type t, int offset, Object o);
120    native void nAllocationSubReadFromObject(int id, Type t, int offset, Object o);
121
122    native void nAdapter1DBindAllocation(int ad, int alloc);
123    native void nAdapter1DSetConstraint(int ad, int dim, int value);
124    native void nAdapter1DData(int ad, int[] d);
125    native void nAdapter1DData(int ad, float[] d);
126    native void nAdapter1DSubData(int ad, int off, int count, int[] d);
127    native void nAdapter1DSubData(int ad, int off, int count, float[] d);
128    native int  nAdapter1DCreate();
129
130    native void nAdapter2DBindAllocation(int ad, int alloc);
131    native void nAdapter2DSetConstraint(int ad, int dim, int value);
132    native void nAdapter2DData(int ad, int[] d);
133    native void nAdapter2DData(int ad, float[] d);
134    native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d);
135    native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d);
136    native int  nAdapter2DCreate();
137
138    native void nScriptBindAllocation(int script, int alloc, int slot);
139    native void nScriptSetClearColor(int script, float r, float g, float b, float a);
140    native void nScriptSetClearDepth(int script, float depth);
141    native void nScriptSetClearStencil(int script, int stencil);
142    native void nScriptSetTimeZone(int script, byte[] timeZone);
143    native void nScriptSetType(int type, boolean writable, String name, int slot);
144    native void nScriptSetRoot(boolean isRoot);
145    native void nScriptSetInvokable(String name, int slot);
146    native void nScriptInvoke(int id, int slot);
147
148    native void nScriptCBegin();
149    native void nScriptCSetScript(byte[] script, int offset, int length);
150    native int  nScriptCCreate();
151    native void nScriptCAddDefineI32(String name, int value);
152    native void nScriptCAddDefineF(String name, float value);
153
154    native void nSamplerBegin();
155    native void nSamplerSet(int param, int value);
156    native int  nSamplerCreate();
157
158    native void nProgramFragmentStoreBegin(int in, int out);
159    native void nProgramFragmentStoreDepthFunc(int func);
160    native void nProgramFragmentStoreDepthMask(boolean enable);
161    native void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a);
162    native void nProgramFragmentStoreBlendFunc(int src, int dst);
163    native void nProgramFragmentStoreDither(boolean enable);
164    native int  nProgramFragmentStoreCreate();
165
166    native int  nProgramRasterCreate(int in, int out, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
167    native void nProgramRasterSetLineWidth(int pr, float v);
168    native void nProgramRasterSetPointSize(int pr, float v);
169
170    native void nProgramBindConstants(int pv, int slot, int mID);
171
172    native void nProgramFragmentBegin(int in, int out, boolean pointSpriteEnable);
173    native void nProgramFragmentBindTexture(int vpf, int slot, int a);
174    native void nProgramFragmentBindSampler(int vpf, int slot, int s);
175    native void nProgramFragmentSetSlot(int slot, boolean enable, int env, int vt);
176    native void nProgramFragmentSetShader(String txt);
177    native int  nProgramFragmentCreate();
178    native int  nProgramFragmentCreate2(String shader, int[] params);
179
180    native int  nProgramVertexCreate(boolean texMat);
181    native int  nProgramVertexCreate2(String shader, int[] params);
182
183    native void nLightBegin();
184    native void nLightSetIsMono(boolean isMono);
185    native void nLightSetIsLocal(boolean isLocal);
186    native int  nLightCreate();
187    native void nLightSetColor(int l, float r, float g, float b);
188    native void nLightSetPosition(int l, float x, float y, float z);
189
190    native int  nSimpleMeshCreate(int batchID, int idxID, int[] vtxID, int prim);
191    native void nSimpleMeshBindVertex(int id, int alloc, int slot);
192    native void nSimpleMeshBindIndex(int id, int alloc);
193
194    native void nAnimationBegin(int attribCount, int keyframeCount);
195    native void nAnimationAdd(float time, float[] attribs);
196    native int  nAnimationCreate();
197
198    private int     mDev;
199    private int     mContext;
200    @SuppressWarnings({"FieldCanBeLocal"})
201    private Surface mSurface;
202    private MessageThread mMessageThread;
203
204
205    Element mElement_USER_U8;
206    Element mElement_USER_I8;
207    Element mElement_USER_U16;
208    Element mElement_USER_I16;
209    Element mElement_USER_U32;
210    Element mElement_USER_I32;
211    Element mElement_USER_FLOAT;
212
213    Element mElement_A_8;
214    Element mElement_RGB_565;
215    Element mElement_RGB_888;
216    Element mElement_RGBA_5551;
217    Element mElement_RGBA_4444;
218    Element mElement_RGBA_8888;
219
220    Element mElement_INDEX_16;
221    Element mElement_XY_F32;
222    Element mElement_XYZ_F32;
223
224
225    ///////////////////////////////////////////////////////////////////////////////////
226    //
227
228    public static class RSMessage implements Runnable {
229        protected int[] mData;
230        protected int mID;
231        public void run() {
232        }
233    }
234    public RSMessage mMessageCallback = null;
235
236    public enum Priority {
237        LOW (5),     //ANDROID_PRIORITY_BACKGROUND + 5
238        NORMAL (-4);  //ANDROID_PRIORITY_DISPLAY
239
240        int mID;
241        Priority(int id) {
242            mID = id;
243        }
244    }
245
246    void validate() {
247        if (mContext == 0) {
248            throw new IllegalStateException("Calling RS with no Context active.");
249        }
250    }
251
252    void validateSurface() {
253        //if (mSurface == null) {
254            //throw new IllegalStateException("Uploading data to GL with no surface.");
255        //}
256    }
257
258    public void contextSetPriority(Priority p) {
259        nContextSetPriority(p.mID);
260    }
261
262    private static class MessageThread extends Thread {
263        RenderScript mRS;
264        boolean mRun = true;
265
266        MessageThread(RenderScript rs) {
267            super("RSMessageThread");
268            mRS = rs;
269
270        }
271
272        public void run() {
273            // This function is a temporary solution.  The final solution will
274            // used typed allocations where the message id is the type indicator.
275            int[] rbuf = new int[16];
276            mRS.nContextInitToClient();
277            while(mRun) {
278                int msg = mRS.nContextGetMessage(rbuf, true);
279                if (msg == 0) {
280                    // Should only happen during teardown.
281                    // But we want to avoid starving other threads during
282                    // teardown by yielding until the next line in the destructor
283                    // can execute to set mRun = false
284                    try {
285                        sleep(1, 0);
286                    } catch(InterruptedException e) {
287                    }
288                }
289                if(mRS.mMessageCallback != null) {
290                    mRS.mMessageCallback.mData = rbuf;
291                    mRS.mMessageCallback.mID = msg;
292                    mRS.mMessageCallback.run();
293                }
294                //Log.d(LOG_TAG, "MessageThread msg " + msg + " v1 " + rbuf[0] + " v2 " + rbuf[1] + " v3 " +rbuf[2]);
295            }
296            Log.d(LOG_TAG, "MessageThread exiting.");
297        }
298    }
299
300    public RenderScript(boolean useDepth, boolean forceSW) {
301        mSurface = null;
302        mWidth = 0;
303        mHeight = 0;
304        mDev = nDeviceCreate();
305        if(forceSW) {
306            nDeviceSetConfig(mDev, 0, 1);
307        }
308        mContext = nContextCreate(mDev, 0, useDepth);
309        Element.initPredefined(this);
310        mMessageThread = new MessageThread(this);
311        mMessageThread.start();
312    }
313
314    public void contextSetSurface(int w, int h, Surface sur) {
315        mSurface = sur;
316        mWidth = w;
317        mHeight = h;
318        nContextSetSurface(w, h, mSurface);
319    }
320
321    public void contextDump(int bits) {
322        nContextDump(bits);
323    }
324
325    public void destroy() {
326        nContextDeinitToClient();
327        mMessageThread.mRun = false;
328
329        nContextDestroy(mContext);
330        mContext = 0;
331
332        nDeviceDestroy(mDev);
333        mDev = 0;
334    }
335
336    boolean isAlive() {
337        return mContext != 0;
338    }
339
340    void pause() {
341        nContextPause();
342    }
343
344    void resume() {
345        nContextResume();
346    }
347
348    //////////////////////////////////////////////////////////////////////////////////
349    // File
350
351    public class File extends BaseObj {
352        File(int id) {
353            super(RenderScript.this);
354            mID = id;
355        }
356    }
357
358    public File fileOpen(String s) throws IllegalStateException, IllegalArgumentException
359    {
360        if(s.length() < 1) {
361            throw new IllegalArgumentException("fileOpen does not accept a zero length string.");
362        }
363
364        try {
365            byte[] bytes = s.getBytes("UTF-8");
366            int id = nFileOpen(bytes);
367            return new File(id);
368        } catch (java.io.UnsupportedEncodingException e) {
369            throw new RuntimeException(e);
370        }
371    }
372
373
374    ///////////////////////////////////////////////////////////////////////////////////
375    // Root state
376
377    private int safeID(BaseObj o) {
378        if(o != null) {
379            return o.mID;
380        }
381        return 0;
382    }
383
384    public void contextBindRootScript(Script s) {
385        nContextBindRootScript(safeID(s));
386    }
387
388    public void contextBindProgramFragmentStore(ProgramStore p) {
389        nContextBindProgramFragmentStore(safeID(p));
390    }
391
392    public void contextBindProgramFragment(ProgramFragment p) {
393        nContextBindProgramFragment(safeID(p));
394    }
395
396    public void contextBindProgramRaster(ProgramRaster p) {
397        nContextBindProgramRaster(safeID(p));
398    }
399
400    public void contextBindProgramVertex(ProgramVertex p) {
401        nContextBindProgramVertex(safeID(p));
402    }
403
404}
405
406
407