ProgramFragment.java revision 1bada8cd6e4f340de93cff4a2439835fc3b1456c
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.renderscript;
18
19
20import android.util.Config;
21import android.util.Log;
22
23
24/**
25 * @hide
26 *
27 **/
28public class ProgramFragment extends BaseObj {
29    public static final int MAX_SLOT = 2;
30
31    public enum EnvMode {
32        REPLACE (0),
33        MODULATE (1),
34        DECAL (2);
35
36        int mID;
37        EnvMode(int id) {
38            mID = id;
39        }
40    }
41
42
43    ProgramFragment(int id, RenderScript rs) {
44        super(rs);
45        mID = id;
46    }
47
48    public void destroy() {
49        if(mDestroyed) {
50            throw new IllegalStateException("Object already destroyed.");
51        }
52        mDestroyed = true;
53        mRS.nProgramFragmentStoreDestroy(mID);
54    }
55
56    public void bindTexture(Allocation va, int slot)
57        throws IllegalArgumentException {
58        if((slot < 0) || (slot >= MAX_SLOT)) {
59            throw new IllegalArgumentException("Slot ID out of range.");
60        }
61
62        mRS.nProgramFragmentBindTexture(mID, slot, va.mID);
63    }
64
65    public void bindSampler(Sampler vs, int slot)
66        throws IllegalArgumentException {
67        if((slot < 0) || (slot >= MAX_SLOT)) {
68            throw new IllegalArgumentException("Slot ID out of range.");
69        }
70
71        mRS.nProgramFragmentBindSampler(mID, slot, vs.mID);
72    }
73
74
75    public static class Builder {
76        RenderScript mRS;
77        Element mIn;
78        Element mOut;
79
80        private class Slot {
81            Type mType;
82            EnvMode mEnv;
83            boolean mTexEnable;
84
85            Slot() {
86                mTexEnable = false;
87            }
88        }
89        Slot[] mSlots;
90
91        public Builder(RenderScript rs, Element in, Element out) {
92            mRS = rs;
93            mIn = in;
94            mOut = out;
95            mSlots = new Slot[MAX_SLOT];
96            for(int ct=0; ct < MAX_SLOT; ct++) {
97                mSlots[ct] = new Slot();
98            }
99        }
100
101        public void setType(int slot, Type t)
102            throws IllegalArgumentException {
103            if((slot < 0) || (slot >= MAX_SLOT)) {
104                throw new IllegalArgumentException("Slot ID out of range.");
105            }
106
107            mSlots[slot].mType = t;
108        }
109
110        public void setTexEnable(boolean enable, int slot)
111            throws IllegalArgumentException {
112            if((slot < 0) || (slot >= MAX_SLOT)) {
113                throw new IllegalArgumentException("Slot ID out of range.");
114            }
115
116            mSlots[slot].mTexEnable = enable;
117        }
118
119        public void setTexEnvMode(EnvMode env, int slot)
120            throws IllegalArgumentException {
121            if((slot < 0) || (slot >= MAX_SLOT)) {
122                throw new IllegalArgumentException("Slot ID out of range.");
123            }
124
125            mSlots[slot].mEnv = env;
126        }
127
128
129        static synchronized ProgramFragment internalCreate(RenderScript rs, Builder b) {
130            int inID = 0;
131            int outID = 0;
132            if (b.mIn != null) {
133                inID = b.mIn.mID;
134            }
135            if (b.mOut != null) {
136                outID = b.mOut.mID;
137            }
138            rs.nProgramFragmentBegin(inID, outID);
139            for(int ct=0; ct < MAX_SLOT; ct++) {
140                if(b.mSlots[ct].mTexEnable) {
141                    Slot s = b.mSlots[ct];
142                    if(s.mType != null) {
143                        rs.nProgramFragmentSetType(ct, s.mType.mID);
144                    }
145                    rs.nProgramFragmentSetTexEnable(ct, true);
146                    if(s.mEnv != null) {
147                        rs.nProgramFragmentSetEnvMode(ct, s.mEnv.mID);
148                    }
149                }
150            }
151
152
153            int id = rs.nProgramFragmentCreate();
154            return new ProgramFragment(id, rs);
155        }
156
157        public ProgramFragment create() {
158            return internalCreate(mRS, this);
159        }
160    }
161}
162
163
164
165