RenderScript.java revision b0ec1b46d6f5b5612e33fe43a828abea79b87a00
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * @hide
19 *
20 **/
21package android.renderscript;
22
23import java.io.InputStream;
24import java.io.IOException;
25
26import android.os.Bundle;
27import android.content.res.Resources;
28import android.util.Log;
29import android.util.Config;
30import android.view.Menu;
31import android.view.MenuItem;
32import android.view.Window;
33import android.view.View;
34import android.view.Surface;
35import android.graphics.Bitmap;
36import android.graphics.Color;
37
38/**
39 * @hide
40 *
41 **/
42public class RenderScript {
43    static final String LOG_TAG = "libRS_jni";
44    private static final boolean DEBUG  = false;
45    private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
46
47
48
49     /*
50     * We use a class initializer to allow the native code to cache some
51     * field offsets.
52     */
53    private static boolean sInitialized;
54    native private static void _nInit();
55
56    static {
57        sInitialized = false;
58        try {
59            System.loadLibrary("rs_jni");
60            _nInit();
61            sInitialized = true;
62        } catch (UnsatisfiedLinkError e) {
63            Log.d(LOG_TAG, "RenderScript JNI library not found!");
64        }
65    }
66
67    native private int  nDeviceCreate();
68    native private void nDeviceDestroy(int dev);
69    native private int  nContextCreate(int dev, Surface sur, int ver);
70    native private void nContextDestroy(int con);
71
72    //void rsContextBindSampler (uint32_t slot, RsSampler sampler);
73    //void rsContextBindRootScript (RsScript sampler);
74    native private void nContextBindRootScript(int script);
75    native private void nContextBindSampler(int sampler, int slot);
76    native private void nContextBindProgramFragmentStore(int pfs);
77    native private void nContextBindProgramFragment(int pf);
78    native private void nContextBindProgramVertex(int pf);
79
80    native private void nAssignName(int obj, byte[] name);
81    native private int  nFileOpen(byte[] name);
82
83    native private void nElementBegin();
84    native private void nElementAddPredefined(int predef);
85    native private void nElementAdd(int kind, int type, int norm, int bits);
86    native private int  nElementCreate();
87    native private int  nElementGetPredefined(int predef);
88    native private void nElementDestroy(int obj);
89
90    native private void nTypeBegin(int elementID);
91    native private void nTypeAdd(int dim, int val);
92    native private int  nTypeCreate();
93    native private void nTypeDestroy(int id);
94
95    native private int  nAllocationCreateTyped(int type);
96    native private int  nAllocationCreatePredefSized(int predef, int count);
97    native private int  nAllocationCreateSized(int elem, int count);
98    native private int  nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
99    native private int  nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp);
100
101    native private void nAllocationUploadToTexture(int alloc, int baseMioLevel);
102    native private void nAllocationDestroy(int alloc);
103    native private void nAllocationData(int id, int[] d);
104    native private void nAllocationData(int id, float[] d);
105    native private void nAllocationSubData1D(int id, int off, int count, int[] d);
106    native private void nAllocationSubData1D(int id, int off, int count, float[] d);
107    native private void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d);
108    native private void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d);
109
110    native private void nTriangleMeshDestroy(int id);
111    native private void nTriangleMeshBegin(int vertex, int index);
112    native private void nTriangleMeshAddVertex_XY (float x, float y);
113    native private void nTriangleMeshAddVertex_XYZ (float x, float y, float z);
114    native private void nTriangleMeshAddVertex_XY_ST (float x, float y, float s, float t);
115    native private void nTriangleMeshAddVertex_XYZ_ST (float x, float y, float z, float s, float t);
116    native private void nTriangleMeshAddVertex_XYZ_ST_NORM (float x, float y, float z, float s, float t, float nx, float ny, float nz);
117    native private void nTriangleMeshAddTriangle(int i1, int i2, int i3);
118    native private int  nTriangleMeshCreate();
119
120    native private void nAdapter1DDestroy(int id);
121    native private void nAdapter1DBindAllocation(int ad, int alloc);
122    native private void nAdapter1DSetConstraint(int ad, int dim, int value);
123    native private void nAdapter1DData(int ad, int[] d);
124    native private void nAdapter1DSubData(int ad, int off, int count, int[] d);
125    native private void nAdapter1DData(int ad, float[] d);
126    native private void nAdapter1DSubData(int ad, int off, int count, float[] d);
127    native private int  nAdapter1DCreate();
128
129    native private void nScriptDestroy(int script);
130    native private void nScriptBindAllocation(int vtm, int alloc, int slot);
131    native private void nScriptCBegin();
132    native private void nScriptCSetClearColor(float r, float g, float b, float a);
133    native private void nScriptCSetClearDepth(float depth);
134    native private void nScriptCSetClearStencil(int stencil);
135    native private void nScriptCAddType(int type);
136    native private void nScriptCSetRoot(boolean isRoot);
137    native private void nScriptCSetScript(byte[] script, int offset, int length);
138    native private int  nScriptCCreate();
139
140    native private void nSamplerDestroy(int sampler);
141    native private void nSamplerBegin();
142    native private void nSamplerSet(int param, int value);
143    native private int  nSamplerCreate();
144
145    native private void nProgramFragmentStoreBegin(int in, int out);
146    native private void nProgramFragmentStoreDepthFunc(int func);
147    native private void nProgramFragmentStoreDepthMask(boolean enable);
148    native private void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a);
149    native private void nProgramFragmentStoreBlendFunc(int src, int dst);
150    native private void nProgramFragmentStoreDither(boolean enable);
151    native private int  nProgramFragmentStoreCreate();
152    native private void nProgramFragmentStoreDestroy(int pgm);
153
154    native private void nProgramFragmentBegin(int in, int out);
155    native private void nProgramFragmentBindTexture(int vpf, int slot, int a);
156    native private void nProgramFragmentBindSampler(int vpf, int slot, int s);
157    native private void nProgramFragmentSetType(int slot, int vt);
158    native private void nProgramFragmentSetEnvMode(int slot, int env);
159    native private void nProgramFragmentSetTexEnable(int slot, boolean enable);
160    native private int  nProgramFragmentCreate();
161    native private void nProgramFragmentDestroy(int pgm);
162
163    native private void nProgramVertexDestroy(int pv);
164    native private void nProgramVertexBindAllocation(int pv, int slot, int mID);
165    native private void nProgramVertexBegin(int inID, int outID);
166    native private void nProgramVertexSetType(int slot, int mID);
167    native private void nProgramVertexSetTextureMatrixEnable(boolean enable);
168    native private void nProgramVertexAddLight(int id);
169    native private int  nProgramVertexCreate();
170
171    native private void nLightBegin();
172    native private void nLightSetIsMono(boolean isMono);
173    native private void nLightSetIsLocal(boolean isLocal);
174    native private int  nLightCreate();
175    native private void nLightDestroy(int l);
176    native private void nLightSetColor(int l, float r, float g, float b);
177    native private void nLightSetPosition(int l, float x, float y, float z);
178
179
180    private int     mDev;
181    private int     mContext;
182    private Surface mSurface;
183
184
185
186    ///////////////////////////////////////////////////////////////////////////////////
187    //
188
189    RenderScript(Surface sur) {
190        mSurface = sur;
191        mDev = nDeviceCreate();
192        mContext = nContextCreate(mDev, mSurface, 0);
193    }
194
195    private class BaseObj {
196        BaseObj() {
197            mID = 0;
198        }
199
200        public int getID() {
201            return mID;
202        }
203
204        int mID;
205        String mName;
206
207        public void setName(String s) throws IllegalStateException, IllegalArgumentException
208        {
209            if(s.length() < 1) {
210                throw new IllegalArgumentException("setName does not accept a zero length string.");
211            }
212            if(mName != null) {
213                throw new IllegalArgumentException("setName object already has a name.");
214            }
215
216            try {
217                byte[] bytes = s.getBytes("UTF-8");
218                nAssignName(mID, bytes);
219                mName = s;
220            } catch (java.io.UnsupportedEncodingException e) {
221                throw new RuntimeException(e);
222            }
223        }
224
225        protected void finalize() throws Throwable
226        {
227            if (mID != 0) {
228                Log.v(LOG_TAG,
229                      "Element finalized without having released the RS reference.");
230            }
231            super.finalize();
232        }
233    }
234
235
236    //////////////////////////////////////////////////////////////////////////////////
237    // Element
238
239    public enum ElementPredefined {
240        USER_U8 (0),
241        USER_I8 (1),
242        USER_U16 (2),
243        USER_I16 (3),
244        USER_U32 (4),
245        USER_I32 (5),
246        USER_FLOAT (6),
247
248        A_8                (7),
249        RGB_565            (8),
250        RGB_888            (11),
251        RGBA_5551          (9),
252        RGBA_4444          (10),
253        RGBA_8888          (12),
254
255        INDEX_16           (13),
256        INDEX_32           (14),
257        XY_F32             (15),
258        XYZ_F32            (16),
259        ST_XY_F32          (17),
260        ST_XYZ_F32         (18),
261        NORM_XYZ_F32       (19),
262        NORM_ST_XYZ_F32    (20);
263
264        int mID;
265        ElementPredefined(int id) {
266            mID = id;
267        }
268    }
269
270    public enum DataType {
271        FLOAT (0),
272        UNSIGNED (1),
273        SIGNED (2);
274
275        int mID;
276        DataType(int id) {
277            mID = id;
278        }
279    }
280
281    public enum DataKind {
282        USER (0),
283        RED (1),
284        GREEN (2),
285        BLUE (3),
286        ALPHA (4),
287        LUMINANCE (5),
288        INTENSITY (6),
289        X (7),
290        Y (8),
291        Z (9),
292        W (10),
293        S (11),
294        T (12),
295        Q (13),
296        R (14),
297        NX (15),
298        NY (16),
299        NZ (17),
300        INDEX (18);
301
302        int mID;
303        DataKind(int id) {
304            mID = id;
305        }
306    }
307
308    public enum DepthFunc {
309        ALWAYS (0),
310        LESS (1),
311        LEQUAL (2),
312        GREATER (3),
313        GEQUAL (4),
314        EQUAL (5),
315        NOTEQUAL (6);
316
317        int mID;
318        DepthFunc(int id) {
319            mID = id;
320        }
321    }
322
323    public enum BlendSrcFunc {
324        ZERO (0),
325        ONE (1),
326        DST_COLOR (2),
327        ONE_MINUS_DST_COLOR (3),
328        SRC_ALPHA (4),
329        ONE_MINUS_SRC_ALPHA (5),
330        DST_ALPHA (6),
331        ONE_MINUS_DST_ALPA (7),
332        SRC_ALPHA_SATURATE (8);
333
334        int mID;
335        BlendSrcFunc(int id) {
336            mID = id;
337        }
338    }
339
340    public enum BlendDstFunc {
341        ZERO (0),
342        ONE (1),
343        SRC_COLOR (2),
344        ONE_MINUS_SRC_COLOR (3),
345        SRC_ALPHA (4),
346        ONE_MINUS_SRC_ALPHA (5),
347        DST_ALPHA (6),
348        ONE_MINUS_DST_ALPA (7);
349
350        int mID;
351        BlendDstFunc(int id) {
352            mID = id;
353        }
354    }
355
356    public enum EnvMode {
357        REPLACE (0),
358        MODULATE (1),
359        DECAL (2);
360
361        int mID;
362        EnvMode(int id) {
363            mID = id;
364        }
365    }
366
367    public enum SamplerParam {
368        FILTER_MIN (0),
369        FILTER_MAG (1),
370        WRAP_MODE_S (2),
371        WRAP_MODE_T (3),
372        WRAP_MODE_R (4);
373
374        int mID;
375        SamplerParam(int id) {
376            mID = id;
377        }
378    }
379
380    public enum SamplerValue {
381        NEAREST (0),
382        LINEAR (1),
383        LINEAR_MIP_LINEAR (2),
384        WRAP (3),
385        CLAMP (4);
386
387        int mID;
388        SamplerValue(int id) {
389            mID = id;
390        }
391    }
392
393
394
395    public class Element extends BaseObj {
396        Element(int id) {
397            mID = id;
398        }
399
400        public void estroy() {
401            nElementDestroy(mID);
402            mID = 0;
403        }
404    }
405
406    public void elementBegin() {
407        nElementBegin();
408    }
409
410    public void elementAddPredefined(ElementPredefined e) {
411        nElementAddPredefined(e.mID);
412    }
413
414    public void elementAdd(DataType dt, DataKind dk, boolean isNormalized, int bits) {
415        int norm = 0;
416        if (isNormalized) {
417            norm = 1;
418        }
419        nElementAdd(dt.mID, dk.mID, norm, bits);
420    }
421
422    public Element elementCreate() {
423        int id = nElementCreate();
424        return new Element(id);
425    }
426
427    public Element elementGetPredefined(ElementPredefined predef) {
428        int id = nElementGetPredefined(predef.mID);
429        return new Element(id);
430    }
431
432
433    //////////////////////////////////////////////////////////////////////////////////
434    // Type
435
436    public enum Dimension {
437        X (0),
438        Y (1),
439        Z (2),
440        LOD (3),
441        FACE (4),
442        ARRAY_0 (100);
443
444        int mID;
445        Dimension(int id) {
446            mID = id;
447        }
448    }
449
450    public class Type extends BaseObj {
451        Type(int id) {
452            mID = id;
453        }
454
455        public void destroy() {
456            nTypeDestroy(mID);
457            mID = 0;
458        }
459    }
460
461    public void typeBegin(Element e) {
462        nTypeBegin(e.mID);
463    }
464
465    public void typeAdd(Dimension d, int value) {
466        nTypeAdd(d.mID, value);
467    }
468
469    public Type typeCreate() {
470        int id = nTypeCreate();
471        return new Type(id);
472    }
473
474
475    //////////////////////////////////////////////////////////////////////////////////
476    // Allocation
477
478    public class Allocation extends BaseObj {
479        Allocation(int id) {
480            mID = id;
481        }
482
483        public void uploadToTexture(int baseMipLevel) {
484            nAllocationUploadToTexture(mID, baseMipLevel);
485        }
486
487        public void destroy() {
488            nAllocationDestroy(mID);
489            mID = 0;
490        }
491
492        public void data(int[] d) {
493            nAllocationData(mID, d);
494        }
495
496        public void data(float[] d) {
497            nAllocationData(mID, d);
498        }
499
500        public void subData1D(int off, int count, int[] d) {
501            nAllocationSubData1D(mID, off, count, d);
502        }
503
504        public void subData1D(int off, int count, float[] d) {
505            nAllocationSubData1D(mID, off, count, d);
506        }
507
508        public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
509            nAllocationSubData2D(mID, xoff, yoff, w, h, d);
510        }
511
512        public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
513            nAllocationSubData2D(mID, xoff, yoff, w, h, d);
514        }
515    }
516
517    public Allocation allocationCreateTyped(Type type) {
518        int id = nAllocationCreateTyped(type.mID);
519        return new Allocation(id);
520    }
521
522    public Allocation allocationCreatePredefSized(ElementPredefined e, int count) {
523        int id = nAllocationCreatePredefSized(e.mID, count);
524        return new Allocation(id);
525    }
526
527    public Allocation allocationCreateSized(Element e, int count) {
528        int id = nAllocationCreateSized(e.mID, count);
529        return new Allocation(id);
530    }
531
532    public Allocation allocationCreateFromBitmap(Bitmap b, ElementPredefined dstFmt, boolean genMips) {
533        int id = nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
534        return new Allocation(id);
535    }
536
537    public Allocation allocationCreateFromBitmapBoxed(Bitmap b, ElementPredefined dstFmt, boolean genMips) {
538        int id = nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
539        return new Allocation(id);
540    }
541
542    //////////////////////////////////////////////////////////////////////////////////
543    // Adapter1D
544
545    public class Adapter1D extends BaseObj {
546        Adapter1D(int id) {
547            mID = id;
548        }
549
550        public void destroy() {
551            nAdapter1DDestroy(mID);
552            mID = 0;
553        }
554
555        public void bindAllocation(Allocation a) {
556            nAdapter1DBindAllocation(mID, a.mID);
557        }
558
559        public void setConstraint(Dimension dim, int value) {
560            nAdapter1DSetConstraint(mID, dim.mID, value);
561        }
562
563        public void data(int[] d) {
564            nAdapter1DData(mID, d);
565        }
566
567        public void subData(int off, int count, int[] d) {
568            nAdapter1DSubData(mID, off, count, d);
569        }
570
571        public void data(float[] d) {
572            nAdapter1DData(mID, d);
573        }
574
575        public void subData(int off, int count, float[] d) {
576            nAdapter1DSubData(mID, off, count, d);
577        }
578    }
579
580    public Adapter1D adapter1DCreate() {
581        int id = nAdapter1DCreate();
582        return new Adapter1D(id);
583    }
584
585
586    //////////////////////////////////////////////////////////////////////////////////
587    // Triangle Mesh
588
589    public class TriangleMesh extends BaseObj {
590        TriangleMesh(int id) {
591            mID = id;
592        }
593
594        public void destroy() {
595            nTriangleMeshDestroy(mID);
596            mID = 0;
597        }
598    }
599
600    public void triangleMeshBegin(Element vertex, Element index) {
601        nTriangleMeshBegin(vertex.mID, index.mID);
602    }
603
604    public void triangleMeshAddVertex_XY(float x, float y) {
605        nTriangleMeshAddVertex_XY(x, y);
606    }
607
608    public void triangleMeshAddVertex_XYZ(float x, float y, float z) {
609        nTriangleMeshAddVertex_XYZ(x, y, z);
610    }
611
612    public void triangleMeshAddVertex_XY_ST(float x, float y, float s, float t) {
613        nTriangleMeshAddVertex_XY_ST(x, y, s, t);
614    }
615
616    public void triangleMeshAddVertex_XYZ_ST(float x, float y, float z, float s, float t) {
617        nTriangleMeshAddVertex_XYZ_ST(x, y, z, s, t);
618    }
619
620    public void triangleMeshAddVertex_XYZ_ST_NORM(float x, float y, float z, float s, float t, float nx, float ny, float nz) {
621        nTriangleMeshAddVertex_XYZ_ST_NORM(x, y, z, s, t, nx, ny, nz);
622    }
623
624    public void triangleMeshAddTriangle(int i1, int i2, int i3) {
625        nTriangleMeshAddTriangle(i1, i2, i3);
626    }
627
628    public TriangleMesh triangleMeshCreate() {
629        int id = nTriangleMeshCreate();
630        return new TriangleMesh(id);
631    }
632
633    //////////////////////////////////////////////////////////////////////////////////
634    // Script
635
636    public class Script extends BaseObj {
637        Script(int id) {
638            mID = id;
639        }
640
641        public void destroy() {
642            nScriptDestroy(mID);
643            mID = 0;
644        }
645
646        public void bindAllocation(Allocation va, int slot) {
647            nScriptBindAllocation(mID, va.mID, slot);
648        }
649    }
650
651    public void scriptCBegin() {
652        nScriptCBegin();
653    }
654
655    public void scriptCSetClearColor(float r, float g, float b, float a) {
656        nScriptCSetClearColor(r, g, b, a);
657    }
658
659    public void scriptCSetClearDepth(float d) {
660        nScriptCSetClearDepth(d);
661    }
662
663    public void scriptCSetClearStencil(int stencil) {
664        nScriptCSetClearStencil(stencil);
665    }
666
667    public void scriptCAddType(Type t) {
668        nScriptCAddType(t.mID);
669    }
670
671    public void scriptCSetRoot(boolean r) {
672        nScriptCSetRoot(r);
673    }
674
675    public void scriptCSetScript(String s) {
676        try {
677            byte[] bytes = s.getBytes("UTF-8");
678            nScriptCSetScript(bytes, 0, bytes.length);
679        } catch (java.io.UnsupportedEncodingException e) {
680            throw new RuntimeException(e);
681        }
682    }
683
684    public void scriptCSetScript(Resources resources, int id) {
685        InputStream is = resources.openRawResource(id);
686        try {
687            try {
688                scriptCSetScript(is);
689            } finally {
690                is.close();
691            }
692        } catch(IOException e) {
693            throw new Resources.NotFoundException();
694        }
695    }
696
697    public void  scriptCSetScript(InputStream is) throws IOException {
698        byte[] buf = new byte[1024];
699        int currentPos = 0;
700        while(true) {
701            int bytesLeft = buf.length - currentPos;
702            if (bytesLeft == 0) {
703                byte[] buf2 = new byte[buf.length * 2];
704                System.arraycopy(buf, 0, buf2, 0, buf.length);
705                buf = buf2;
706                bytesLeft = buf.length - currentPos;
707            }
708            int bytesRead = is.read(buf, currentPos, bytesLeft);
709            if (bytesRead <= 0) {
710                break;
711            }
712            currentPos += bytesRead;
713        }
714        nScriptCSetScript(buf, 0, currentPos);
715    }
716
717    public Script scriptCCreate() {
718        int id = nScriptCCreate();
719        return new Script(id);
720    }
721
722    //////////////////////////////////////////////////////////////////////////////////
723    // ProgramVertex
724
725    public class ProgramVertex extends BaseObj {
726        ProgramVertex(int id) {
727            mID = id;
728        }
729
730        public void destroy() {
731            nProgramVertexDestroy(mID);
732            mID = 0;
733        }
734
735        public void bindAllocation(int slot, Allocation va) {
736            nProgramVertexBindAllocation(mID, slot, va.mID);
737        }
738    }
739
740    public void programVertexBegin(Element in, Element out) {
741        int inID = 0;
742        int outID = 0;
743        if (in != null) {
744            inID = in.mID;
745        }
746        if (out != null) {
747            outID = out.mID;
748        }
749        nProgramVertexBegin(inID, outID);
750    }
751
752    public void programVertexSetType(int slot, Type t) {
753        nProgramVertexSetType(slot, t.mID);
754    }
755
756    public void programVertexSetTextureMatrixEnable(boolean enable) {
757        nProgramVertexSetTextureMatrixEnable(enable);
758    }
759
760    public void programVertexAddLight(Light l) {
761        nProgramVertexAddLight(l.mID);
762    }
763
764    public ProgramVertex programVertexCreate() {
765        int id = nProgramVertexCreate();
766        return new ProgramVertex(id);
767    }
768
769
770    //////////////////////////////////////////////////////////////////////////////////
771    // ProgramFragmentStore
772
773    public class ProgramFragmentStore extends BaseObj {
774        ProgramFragmentStore(int id) {
775            mID = id;
776        }
777
778        public void destroy() {
779            nProgramFragmentStoreDestroy(mID);
780            mID = 0;
781        }
782    }
783
784    public void programFragmentStoreBegin(Element in, Element out) {
785        int inID = 0;
786        int outID = 0;
787        if (in != null) {
788            inID = in.mID;
789        }
790        if (out != null) {
791            outID = out.mID;
792        }
793        nProgramFragmentStoreBegin(inID, outID);
794    }
795
796    public void programFragmentStoreDepthFunc(DepthFunc func) {
797        nProgramFragmentStoreDepthFunc(func.mID);
798    }
799
800    public void programFragmentStoreDepthMask(boolean enable) {
801        nProgramFragmentStoreDepthMask(enable);
802    }
803
804    public void programFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
805        nProgramFragmentStoreColorMask(r,g,b,a);
806    }
807
808    public void programFragmentStoreBlendFunc(BlendSrcFunc src, BlendDstFunc dst) {
809        nProgramFragmentStoreBlendFunc(src.mID, dst.mID);
810    }
811
812    public void programFragmentStoreDitherEnable(boolean enable) {
813        nProgramFragmentStoreDither(enable);
814    }
815
816    public ProgramFragmentStore programFragmentStoreCreate() {
817        int id = nProgramFragmentStoreCreate();
818        return new ProgramFragmentStore(id);
819    }
820
821    //////////////////////////////////////////////////////////////////////////////////
822    // ProgramFragment
823
824    public class ProgramFragment extends BaseObj {
825        ProgramFragment(int id) {
826            mID = id;
827        }
828
829        public void destroy() {
830            nProgramFragmentDestroy(mID);
831            mID = 0;
832        }
833
834        public void bindTexture(Allocation va, int slot) {
835            nProgramFragmentBindTexture(mID, slot, va.mID);
836        }
837
838        public void bindSampler(Sampler vs, int slot) {
839            nProgramFragmentBindSampler(mID, slot, vs.mID);
840        }
841    }
842
843    public void programFragmentBegin(Element in, Element out) {
844        int inID = 0;
845        int outID = 0;
846        if (in != null) {
847            inID = in.mID;
848        }
849        if (out != null) {
850            outID = out.mID;
851        }
852        nProgramFragmentBegin(inID, outID);
853    }
854
855    public void programFragmentSetType(int slot, Type t) {
856        nProgramFragmentSetType(slot, t.mID);
857    }
858
859    public void programFragmentSetType(int slot, EnvMode t) {
860        nProgramFragmentSetEnvMode(slot, t.mID);
861    }
862
863    public void programFragmentSetTexEnable(int slot, boolean enable) {
864        nProgramFragmentSetTexEnable(slot, enable);
865    }
866
867    public void programFragmentSetTexEnvMode(int slot, EnvMode env) {
868        nProgramFragmentSetEnvMode(slot, env.mID);
869    }
870
871    public ProgramFragment programFragmentCreate() {
872        int id = nProgramFragmentCreate();
873        return new ProgramFragment(id);
874    }
875
876    //////////////////////////////////////////////////////////////////////////////////
877    // Sampler
878
879    public class Sampler extends BaseObj {
880        Sampler(int id) {
881            mID = id;
882        }
883
884        public void destroy() {
885            nSamplerDestroy(mID);
886            mID = 0;
887        }
888    }
889
890    public void samplerBegin() {
891        nSamplerBegin();
892    }
893
894    public void samplerSet(SamplerParam p, SamplerValue v) {
895        nSamplerSet(p.mID, v.mID);
896    }
897
898    public Sampler samplerCreate() {
899        int id = nSamplerCreate();
900        return new Sampler(id);
901    }
902
903    //////////////////////////////////////////////////////////////////////////////////
904    // Light
905
906    public class Light extends BaseObj {
907        Light(int id) {
908            mID = id;
909        }
910
911        public void destroy() {
912            nLightDestroy(mID);
913            mID = 0;
914        }
915
916        public void setColor(float r, float g, float b) {
917            nLightSetColor(mID, r, g, b);
918        }
919
920        public void setPosition(float x, float y, float z) {
921            nLightSetPosition(mID, x, y, z);
922        }
923    }
924
925    public void lightBegin() {
926        nLightBegin();
927    }
928
929    public void lightSetIsMono(boolean isMono) {
930        nLightSetIsMono(isMono);
931    }
932
933    public void lightSetIsLocal(boolean isLocal) {
934        nLightSetIsLocal(isLocal);
935    }
936
937    public Light lightCreate() {
938        int id = nLightCreate();
939        return new Light(id);
940    }
941
942    //////////////////////////////////////////////////////////////////////////////////
943    // File
944
945    public class File extends BaseObj {
946        File(int id) {
947            mID = id;
948        }
949
950        public void destroy() {
951            //nLightDestroy(mID);
952            mID = 0;
953        }
954    }
955
956    public File fileOpen(String s) throws IllegalStateException, IllegalArgumentException
957    {
958        if(s.length() < 1) {
959            throw new IllegalArgumentException("fileOpen does not accept a zero length string.");
960        }
961
962        try {
963            byte[] bytes = s.getBytes("UTF-8");
964            int id = nFileOpen(bytes);
965            return new File(id);
966        } catch (java.io.UnsupportedEncodingException e) {
967            throw new RuntimeException(e);
968        }
969    }
970
971
972    ///////////////////////////////////////////////////////////////////////////////////
973    // Root state
974
975    public void contextBindRootScript(Script s) {
976        nContextBindRootScript(s.mID);
977    }
978
979    //public void contextBindSampler(Sampler s, int slot) {
980        //nContextBindSampler(s.mID);
981    //}
982
983    public void contextBindProgramFragmentStore(ProgramFragmentStore pfs) {
984        nContextBindProgramFragmentStore(pfs.mID);
985    }
986
987    public void contextBindProgramFragment(ProgramFragment pf) {
988        nContextBindProgramFragment(pf.mID);
989    }
990
991    public void contextBindProgramVertex(ProgramVertex pf) {
992        nContextBindProgramVertex(pf.mID);
993    }
994
995/*
996    RsAdapter2D rsAdapter2DCreate ();
997    void rsAdapter2DBindAllocation (RsAdapter2D adapt, RsAllocation alloc);
998    void rsAdapter2DDestroy (RsAdapter2D adapter);
999    void rsAdapter2DSetConstraint (RsAdapter2D adapter, RsDimension dim, uint32_t value);
1000    void rsAdapter2DData (RsAdapter2D adapter, const void * data);
1001    void rsAdapter2DSubData (RsAdapter2D adapter, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void * data);
1002    void rsSamplerBegin ();
1003    void rsSamplerSet (RsSamplerParam p, RsSamplerValue value);
1004    RsSampler rsSamplerCreate ();
1005    void rsSamplerBind (RsSampler sampler, RsAllocation alloc);
1006*/
1007
1008}
1009
1010