RenderScript.java revision 69f0d31e576c0110c2cbbafc3b9bd46e73fc1afc
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.io.IOException;
20import java.io.InputStream;
21
22import android.content.res.Resources;
23import android.graphics.Bitmap;
24import android.util.Config;
25import android.util.Log;
26import android.view.Surface;
27
28
29/**
30 * @hide
31 *
32 **/
33public class RenderScript {
34    static final String LOG_TAG = "libRS_jni";
35    private static final boolean DEBUG  = false;
36    private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
37
38
39
40     /*
41     * We use a class initializer to allow the native code to cache some
42     * field offsets.
43     */
44    private static boolean sInitialized;
45    native private static void _nInit();
46
47
48    static {
49        sInitialized = false;
50        try {
51            System.loadLibrary("rs_jni");
52            _nInit();
53            sInitialized = true;
54        } catch (UnsatisfiedLinkError e) {
55            Log.d(LOG_TAG, "RenderScript JNI library not found!");
56        }
57    }
58
59    native int  nDeviceCreate();
60    native void nDeviceDestroy(int dev);
61    native int  nContextCreate(int dev, Surface sur, int ver);
62    native void nContextDestroy(int con);
63
64    //void rsContextBindSampler (uint32_t slot, RsSampler sampler);
65    //void rsContextBindRootScript (RsScript sampler);
66    native void nContextBindRootScript(int script);
67    native void nContextBindSampler(int sampler, int slot);
68    native void nContextBindProgramFragmentStore(int pfs);
69    native void nContextBindProgramFragment(int pf);
70    native void nContextBindProgramVertex(int pf);
71
72    native void nAssignName(int obj, byte[] name);
73    native int  nFileOpen(byte[] name);
74
75    native void nElementBegin();
76    native void nElementAddPredefined(int predef);
77    native void nElementAdd(int kind, int type, int norm, int bits);
78    native int  nElementCreate();
79    native int  nElementGetPredefined(int predef);
80    native void nElementDestroy(int obj);
81
82    native void nTypeBegin(int elementID);
83    native void nTypeAdd(int dim, int val);
84    native int  nTypeCreate();
85    native void nTypeDestroy(int id);
86
87    native int  nAllocationCreateTyped(int type);
88    native int  nAllocationCreatePredefSized(int predef, int count);
89    native int  nAllocationCreateSized(int elem, int count);
90    native int  nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
91    native int  nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp);
92
93    native void nAllocationUploadToTexture(int alloc, int baseMioLevel);
94    native void nAllocationDestroy(int alloc);
95    native void nAllocationData(int id, int[] d);
96    native void nAllocationData(int id, float[] d);
97    native void nAllocationSubData1D(int id, int off, int count, int[] d);
98    native void nAllocationSubData1D(int id, int off, int count, float[] d);
99    native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d);
100    native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d);
101
102    native void nTriangleMeshDestroy(int id);
103    native void nTriangleMeshBegin(int vertex, int index);
104    native void nTriangleMeshAddVertex_XY (float x, float y);
105    native void nTriangleMeshAddVertex_XYZ (float x, float y, float z);
106    native void nTriangleMeshAddVertex_XY_ST (float x, float y, float s, float t);
107    native void nTriangleMeshAddVertex_XYZ_ST (float x, float y, float z, float s, float t);
108    native void nTriangleMeshAddVertex_XYZ_ST_NORM (float x, float y, float z, float s, float t, float nx, float ny, float nz);
109    native void nTriangleMeshAddTriangle(int i1, int i2, int i3);
110    native int  nTriangleMeshCreate();
111
112    native void nAdapter1DDestroy(int id);
113    native void nAdapter1DBindAllocation(int ad, int alloc);
114    native void nAdapter1DSetConstraint(int ad, int dim, int value);
115    native void nAdapter1DData(int ad, int[] d);
116    native void nAdapter1DData(int ad, float[] d);
117    native void nAdapter1DSubData(int ad, int off, int count, int[] d);
118    native void nAdapter1DSubData(int ad, int off, int count, float[] d);
119    native int  nAdapter1DCreate();
120
121    native void nAdapter2DDestroy(int id);
122    native void nAdapter2DBindAllocation(int ad, int alloc);
123    native void nAdapter2DSetConstraint(int ad, int dim, int value);
124    native void nAdapter2DData(int ad, int[] d);
125    native void nAdapter2DData(int ad, float[] d);
126    native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d);
127    native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d);
128    native int  nAdapter2DCreate();
129
130    native void nScriptDestroy(int script);
131    native void nScriptBindAllocation(int vtm, int alloc, int slot);
132    native void nScriptCBegin();
133    native void nScriptCSetClearColor(float r, float g, float b, float a);
134    native void nScriptCSetClearDepth(float depth);
135    native void nScriptCSetClearStencil(int stencil);
136    native void nScriptCSetTimeZone(byte[] timeZone);
137    native void nScriptCAddType(int type);
138    native void nScriptCSetRoot(boolean isRoot);
139    native void nScriptCSetScript(byte[] script, int offset, int length);
140    native int  nScriptCCreate();
141
142    native void nSamplerDestroy(int sampler);
143    native void nSamplerBegin();
144    native void nSamplerSet(int param, int value);
145    native int  nSamplerCreate();
146
147    native void nProgramFragmentStoreBegin(int in, int out);
148    native void nProgramFragmentStoreDepthFunc(int func);
149    native void nProgramFragmentStoreDepthMask(boolean enable);
150    native void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a);
151    native void nProgramFragmentStoreBlendFunc(int src, int dst);
152    native void nProgramFragmentStoreDither(boolean enable);
153    native int  nProgramFragmentStoreCreate();
154    native void nProgramFragmentStoreDestroy(int pgm);
155
156    native void nProgramFragmentBegin(int in, int out);
157    native void nProgramFragmentBindTexture(int vpf, int slot, int a);
158    native void nProgramFragmentBindSampler(int vpf, int slot, int s);
159    native void nProgramFragmentSetType(int slot, int vt);
160    native void nProgramFragmentSetEnvMode(int slot, int env);
161    native void nProgramFragmentSetTexEnable(int slot, boolean enable);
162    native int  nProgramFragmentCreate();
163    native void nProgramFragmentDestroy(int pgm);
164
165    native void nProgramVertexDestroy(int pv);
166    native void nProgramVertexBindAllocation(int pv, int slot, int mID);
167    native void nProgramVertexBegin(int inID, int outID);
168    native void nProgramVertexSetType(int slot, int mID);
169    native void nProgramVertexSetTextureMatrixEnable(boolean enable);
170    native void nProgramVertexAddLight(int id);
171    native int  nProgramVertexCreate();
172
173    native void nLightBegin();
174    native void nLightSetIsMono(boolean isMono);
175    native void nLightSetIsLocal(boolean isLocal);
176    native int  nLightCreate();
177    native void nLightDestroy(int l);
178    native void nLightSetColor(int l, float r, float g, float b);
179    native void nLightSetPosition(int l, float x, float y, float z);
180
181
182    private int     mDev;
183    private int     mContext;
184    private Surface mSurface;
185
186    private static boolean mElementsInitialized = false;
187
188    ///////////////////////////////////////////////////////////////////////////////////
189    //
190
191    RenderScript(Surface sur) {
192        mSurface = sur;
193        mDev = nDeviceCreate();
194        mContext = nContextCreate(mDev, mSurface, 0);
195
196        // TODO: This should be protected by a lock
197        if(!mElementsInitialized) {
198            Element.init(this);
199            mElementsInitialized = true;
200        }
201    }
202
203    //////////////////////////////////////////////////////////////////////////////////
204    // Element
205
206    Element.Builder mElementBuilder = new Element.Builder(this);
207    public Element.Builder elementBuilderCreate() throws IllegalStateException {
208        mElementBuilder.begin();
209        return mElementBuilder;
210    }
211
212    Type.Builder mTypeBuilder = new Type.Builder(this);
213    public Type.Builder typeBuilderCreate(Element e) throws IllegalStateException {
214        mTypeBuilder.begin(e);
215        return mTypeBuilder;
216    }
217
218
219    public enum DepthFunc {
220        ALWAYS (0),
221        LESS (1),
222        LEQUAL (2),
223        GREATER (3),
224        GEQUAL (4),
225        EQUAL (5),
226        NOTEQUAL (6);
227
228        int mID;
229        DepthFunc(int id) {
230            mID = id;
231        }
232    }
233
234    public enum BlendSrcFunc {
235        ZERO (0),
236        ONE (1),
237        DST_COLOR (2),
238        ONE_MINUS_DST_COLOR (3),
239        SRC_ALPHA (4),
240        ONE_MINUS_SRC_ALPHA (5),
241        DST_ALPHA (6),
242        ONE_MINUS_DST_ALPA (7),
243        SRC_ALPHA_SATURATE (8);
244
245        int mID;
246        BlendSrcFunc(int id) {
247            mID = id;
248        }
249    }
250
251    public enum BlendDstFunc {
252        ZERO (0),
253        ONE (1),
254        SRC_COLOR (2),
255        ONE_MINUS_SRC_COLOR (3),
256        SRC_ALPHA (4),
257        ONE_MINUS_SRC_ALPHA (5),
258        DST_ALPHA (6),
259        ONE_MINUS_DST_ALPA (7);
260
261        int mID;
262        BlendDstFunc(int id) {
263            mID = id;
264        }
265    }
266
267    public enum EnvMode {
268        REPLACE (0),
269        MODULATE (1),
270        DECAL (2);
271
272        int mID;
273        EnvMode(int id) {
274            mID = id;
275        }
276    }
277
278    public enum SamplerParam {
279        FILTER_MIN (0),
280        FILTER_MAG (1),
281        WRAP_MODE_S (2),
282        WRAP_MODE_T (3),
283        WRAP_MODE_R (4);
284
285        int mID;
286        SamplerParam(int id) {
287            mID = id;
288        }
289    }
290
291    public enum SamplerValue {
292        NEAREST (0),
293        LINEAR (1),
294        LINEAR_MIP_LINEAR (2),
295        WRAP (3),
296        CLAMP (4);
297
298        int mID;
299        SamplerValue(int id) {
300            mID = id;
301        }
302    }
303
304    //////////////////////////////////////////////////////////////////////////////////
305    // Triangle Mesh
306
307    public class TriangleMesh extends BaseObj {
308        TriangleMesh(int id) {
309            super(RenderScript.this);
310            mID = id;
311        }
312
313        public void destroy() {
314            nTriangleMeshDestroy(mID);
315            mID = 0;
316        }
317    }
318
319    public void triangleMeshBegin(Element vertex, Element index) {
320        Log.e("rs", "vtx " + vertex.toString() + "  " + vertex.mID + "  " + vertex.mPredefinedID);
321        nTriangleMeshBegin(vertex.mID, index.mID);
322    }
323
324    public void triangleMeshAddVertex_XY(float x, float y) {
325        nTriangleMeshAddVertex_XY(x, y);
326    }
327
328    public void triangleMeshAddVertex_XYZ(float x, float y, float z) {
329        nTriangleMeshAddVertex_XYZ(x, y, z);
330    }
331
332    public void triangleMeshAddVertex_XY_ST(float x, float y, float s, float t) {
333        nTriangleMeshAddVertex_XY_ST(x, y, s, t);
334    }
335
336    public void triangleMeshAddVertex_XYZ_ST(float x, float y, float z, float s, float t) {
337        nTriangleMeshAddVertex_XYZ_ST(x, y, z, s, t);
338    }
339
340    public void triangleMeshAddVertex_XYZ_ST_NORM(float x, float y, float z, float s, float t, float nx, float ny, float nz) {
341        nTriangleMeshAddVertex_XYZ_ST_NORM(x, y, z, s, t, nx, ny, nz);
342    }
343
344    public void triangleMeshAddTriangle(int i1, int i2, int i3) {
345        nTriangleMeshAddTriangle(i1, i2, i3);
346    }
347
348    public TriangleMesh triangleMeshCreate() {
349        int id = nTriangleMeshCreate();
350        return new TriangleMesh(id);
351    }
352
353    //////////////////////////////////////////////////////////////////////////////////
354    // ProgramVertex
355
356    public class ProgramVertex extends BaseObj {
357        ProgramVertex(int id) {
358            super(RenderScript.this);
359            mID = id;
360        }
361
362        public void destroy() {
363            nProgramVertexDestroy(mID);
364            mID = 0;
365        }
366
367        public void bindAllocation(int slot, Allocation va) {
368            nProgramVertexBindAllocation(mID, slot, va.mID);
369        }
370    }
371
372    public void programVertexBegin(Element in, Element out) {
373        int inID = 0;
374        int outID = 0;
375        if (in != null) {
376            inID = in.mID;
377        }
378        if (out != null) {
379            outID = out.mID;
380        }
381        nProgramVertexBegin(inID, outID);
382    }
383
384    public void programVertexSetType(int slot, Type t) {
385        nProgramVertexSetType(slot, t.mID);
386    }
387
388    public void programVertexSetTextureMatrixEnable(boolean enable) {
389        nProgramVertexSetTextureMatrixEnable(enable);
390    }
391
392    public void programVertexAddLight(Light l) {
393        nProgramVertexAddLight(l.mID);
394    }
395
396    public ProgramVertex programVertexCreate() {
397        int id = nProgramVertexCreate();
398        return new ProgramVertex(id);
399    }
400
401
402    //////////////////////////////////////////////////////////////////////////////////
403    // ProgramFragmentStore
404
405    public class ProgramFragmentStore extends BaseObj {
406        ProgramFragmentStore(int id) {
407            super(RenderScript.this);
408            mID = id;
409        }
410
411        public void destroy() {
412            nProgramFragmentStoreDestroy(mID);
413            mID = 0;
414        }
415    }
416
417    public void programFragmentStoreBegin(Element in, Element out) {
418        int inID = 0;
419        int outID = 0;
420        if (in != null) {
421            inID = in.mID;
422        }
423        if (out != null) {
424            outID = out.mID;
425        }
426        nProgramFragmentStoreBegin(inID, outID);
427    }
428
429    public void programFragmentStoreDepthFunc(DepthFunc func) {
430        nProgramFragmentStoreDepthFunc(func.mID);
431    }
432
433    public void programFragmentStoreDepthMask(boolean enable) {
434        nProgramFragmentStoreDepthMask(enable);
435    }
436
437    public void programFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
438        nProgramFragmentStoreColorMask(r,g,b,a);
439    }
440
441    public void programFragmentStoreBlendFunc(BlendSrcFunc src, BlendDstFunc dst) {
442        nProgramFragmentStoreBlendFunc(src.mID, dst.mID);
443    }
444
445    public void programFragmentStoreDitherEnable(boolean enable) {
446        nProgramFragmentStoreDither(enable);
447    }
448
449    public ProgramFragmentStore programFragmentStoreCreate() {
450        int id = nProgramFragmentStoreCreate();
451        return new ProgramFragmentStore(id);
452    }
453
454    //////////////////////////////////////////////////////////////////////////////////
455    // ProgramFragment
456
457    public class ProgramFragment extends BaseObj {
458        ProgramFragment(int id) {
459            super(RenderScript.this);
460            mID = id;
461        }
462
463        public void destroy() {
464            nProgramFragmentDestroy(mID);
465            mID = 0;
466        }
467
468        public void bindTexture(Allocation va, int slot) {
469            nProgramFragmentBindTexture(mID, slot, va.mID);
470        }
471
472        public void bindSampler(Sampler vs, int slot) {
473            nProgramFragmentBindSampler(mID, slot, vs.mID);
474        }
475    }
476
477    public void programFragmentBegin(Element in, Element out) {
478        int inID = 0;
479        int outID = 0;
480        if (in != null) {
481            inID = in.mID;
482        }
483        if (out != null) {
484            outID = out.mID;
485        }
486        nProgramFragmentBegin(inID, outID);
487    }
488
489    public void programFragmentSetType(int slot, Type t) {
490        nProgramFragmentSetType(slot, t.mID);
491    }
492
493    public void programFragmentSetType(int slot, EnvMode t) {
494        nProgramFragmentSetEnvMode(slot, t.mID);
495    }
496
497    public void programFragmentSetTexEnable(int slot, boolean enable) {
498        nProgramFragmentSetTexEnable(slot, enable);
499    }
500
501    public void programFragmentSetTexEnvMode(int slot, EnvMode env) {
502        nProgramFragmentSetEnvMode(slot, env.mID);
503    }
504
505    public ProgramFragment programFragmentCreate() {
506        int id = nProgramFragmentCreate();
507        return new ProgramFragment(id);
508    }
509
510    //////////////////////////////////////////////////////////////////////////////////
511    // Sampler
512
513    public class Sampler extends BaseObj {
514        Sampler(int id) {
515            super(RenderScript.this);
516            mID = id;
517        }
518
519        public void destroy() {
520            nSamplerDestroy(mID);
521            mID = 0;
522        }
523    }
524
525    public void samplerBegin() {
526        nSamplerBegin();
527    }
528
529    public void samplerSet(SamplerParam p, SamplerValue v) {
530        nSamplerSet(p.mID, v.mID);
531    }
532
533    public Sampler samplerCreate() {
534        int id = nSamplerCreate();
535        return new Sampler(id);
536    }
537
538    //////////////////////////////////////////////////////////////////////////////////
539    // Light
540
541    public class Light extends BaseObj {
542        Light(int id) {
543            super(RenderScript.this);
544            mID = id;
545        }
546
547        public void destroy() {
548            nLightDestroy(mID);
549            mID = 0;
550        }
551
552        public void setColor(float r, float g, float b) {
553            nLightSetColor(mID, r, g, b);
554        }
555
556        public void setPosition(float x, float y, float z) {
557            nLightSetPosition(mID, x, y, z);
558        }
559    }
560
561    public void lightBegin() {
562        nLightBegin();
563    }
564
565    public void lightSetIsMono(boolean isMono) {
566        nLightSetIsMono(isMono);
567    }
568
569    public void lightSetIsLocal(boolean isLocal) {
570        nLightSetIsLocal(isLocal);
571    }
572
573    public Light lightCreate() {
574        int id = nLightCreate();
575        return new Light(id);
576    }
577
578    //////////////////////////////////////////////////////////////////////////////////
579    // File
580
581    public class File extends BaseObj {
582        File(int id) {
583            super(RenderScript.this);
584            mID = id;
585        }
586
587        public void destroy() {
588            //nLightDestroy(mID);
589            mID = 0;
590        }
591    }
592
593    public File fileOpen(String s) throws IllegalStateException, IllegalArgumentException
594    {
595        if(s.length() < 1) {
596            throw new IllegalArgumentException("fileOpen does not accept a zero length string.");
597        }
598
599        try {
600            byte[] bytes = s.getBytes("UTF-8");
601            int id = nFileOpen(bytes);
602            return new File(id);
603        } catch (java.io.UnsupportedEncodingException e) {
604            throw new RuntimeException(e);
605        }
606    }
607
608
609    ///////////////////////////////////////////////////////////////////////////////////
610    // Root state
611
612    public void contextBindRootScript(Script s) {
613        nContextBindRootScript(s.mID);
614    }
615
616    //public void contextBindSampler(Sampler s, int slot) {
617        //nContextBindSampler(s.mID);
618    //}
619
620    public void contextBindProgramFragmentStore(ProgramFragmentStore pfs) {
621        nContextBindProgramFragmentStore(pfs.mID);
622    }
623
624    public void contextBindProgramFragment(ProgramFragment pf) {
625        nContextBindProgramFragment(pf.mID);
626    }
627
628    public void contextBindProgramVertex(ProgramVertex pf) {
629        nContextBindProgramVertex(pf.mID);
630    }
631
632}
633
634
635