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