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