blob: aad09f6df9d0845df1189c04d09eca3317d42912 [file] [log] [blame]
Jason Sams22534172009-08-04 16:58:20 -07001/*
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 {
Jason Sams110195f2009-08-04 18:47:46 -070029 public static final int MAX_SLOT = 2;
30
Jason Sams22534172009-08-04 16:58:20 -070031 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
Jason Sams110195f2009-08-04 18:47:46 -070048 public void bindTexture(Allocation va, int slot)
49 throws IllegalArgumentException {
50 if((slot < 0) || (slot >= MAX_SLOT)) {
51 throw new IllegalArgumentException("Slot ID out of range.");
52 }
53
Jason Sams22534172009-08-04 16:58:20 -070054 mRS.nProgramFragmentBindTexture(mID, slot, va.mID);
55 }
56
Jason Sams110195f2009-08-04 18:47:46 -070057 public void bindSampler(Sampler vs, int slot)
58 throws IllegalArgumentException {
59 if((slot < 0) || (slot >= MAX_SLOT)) {
60 throw new IllegalArgumentException("Slot ID out of range.");
61 }
62
Jason Sams22534172009-08-04 16:58:20 -070063 mRS.nProgramFragmentBindSampler(mID, slot, vs.mID);
64 }
65
66
67 public static class Builder {
Jason Sams22534172009-08-04 16:58:20 -070068 RenderScript mRS;
69 Element mIn;
70 Element mOut;
71
72 private class Slot {
73 Type mType;
74 EnvMode mEnv;
75 boolean mTexEnable;
76
77 Slot() {
78 mTexEnable = false;
79 }
80 }
81 Slot[] mSlots;
82
83 public Builder(RenderScript rs, Element in, Element out) {
84 mRS = rs;
85 mIn = in;
86 mOut = out;
87 mSlots = new Slot[MAX_SLOT];
88 for(int ct=0; ct < MAX_SLOT; ct++) {
89 mSlots[ct] = new Slot();
90 }
91 }
92
93 public void setType(int slot, Type t)
94 throws IllegalArgumentException {
95 if((slot < 0) || (slot >= MAX_SLOT)) {
96 throw new IllegalArgumentException("Slot ID out of range.");
97 }
98
99 mSlots[slot].mType = t;
100 }
101
102 public void setTexEnable(boolean enable, int slot)
103 throws IllegalArgumentException {
104 if((slot < 0) || (slot >= MAX_SLOT)) {
105 throw new IllegalArgumentException("Slot ID out of range.");
106 }
107
108 mSlots[slot].mTexEnable = enable;
109 }
110
111 public void setTexEnvMode(EnvMode env, int slot)
112 throws IllegalArgumentException {
113 if((slot < 0) || (slot >= MAX_SLOT)) {
114 throw new IllegalArgumentException("Slot ID out of range.");
115 }
116
117 mSlots[slot].mEnv = env;
118 }
119
120
121 static synchronized ProgramFragment internalCreate(RenderScript rs, Builder b) {
122 int inID = 0;
123 int outID = 0;
124 if (b.mIn != null) {
125 inID = b.mIn.mID;
126 }
127 if (b.mOut != null) {
128 outID = b.mOut.mID;
129 }
130 rs.nProgramFragmentBegin(inID, outID);
131 for(int ct=0; ct < MAX_SLOT; ct++) {
132 if(b.mSlots[ct].mTexEnable) {
133 Slot s = b.mSlots[ct];
134 if(s.mType != null) {
135 rs.nProgramFragmentSetType(ct, s.mType.mID);
136 }
137 rs.nProgramFragmentSetTexEnable(ct, true);
138 if(s.mEnv != null) {
139 rs.nProgramFragmentSetEnvMode(ct, s.mEnv.mID);
140 }
141 }
142 }
143
144
145 int id = rs.nProgramFragmentCreate();
146 return new ProgramFragment(id, rs);
147 }
148
149 public ProgramFragment create() {
150 return internalCreate(mRS, this);
151 }
152 }
153}
154
155
156