ProgramFragment.java revision 22534176fb5c1257130ef4ee589739ca42766a32
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 enum EnvMode { 30 REPLACE (0), 31 MODULATE (1), 32 DECAL (2); 33 34 int mID; 35 EnvMode(int id) { 36 mID = id; 37 } 38 } 39 40 41 ProgramFragment(int id, RenderScript rs) { 42 super(rs); 43 mID = id; 44 } 45 46 public void destroy() { 47 mRS.nProgramFragmentStoreDestroy(mID); 48 mID = 0; 49 } 50 51 public void bindTexture(Allocation va, int slot) { 52 mRS.nProgramFragmentBindTexture(mID, slot, va.mID); 53 } 54 55 public void bindSampler(RenderScript.Sampler vs, int slot) { 56 mRS.nProgramFragmentBindSampler(mID, slot, vs.mID); 57 } 58 59 60 public static class Builder { 61 public static final int MAX_SLOT = 2; 62 RenderScript mRS; 63 Element mIn; 64 Element mOut; 65 66 private class Slot { 67 Type mType; 68 EnvMode mEnv; 69 boolean mTexEnable; 70 71 Slot() { 72 mTexEnable = false; 73 } 74 } 75 Slot[] mSlots; 76 77 public Builder(RenderScript rs, Element in, Element out) { 78 mRS = rs; 79 mIn = in; 80 mOut = out; 81 mSlots = new Slot[MAX_SLOT]; 82 for(int ct=0; ct < MAX_SLOT; ct++) { 83 mSlots[ct] = new Slot(); 84 } 85 } 86 87 public void setType(int slot, Type t) 88 throws IllegalArgumentException { 89 if((slot < 0) || (slot >= MAX_SLOT)) { 90 throw new IllegalArgumentException("Slot ID out of range."); 91 } 92 93 mSlots[slot].mType = t; 94 } 95 96 public void setTexEnable(boolean enable, int slot) 97 throws IllegalArgumentException { 98 if((slot < 0) || (slot >= MAX_SLOT)) { 99 throw new IllegalArgumentException("Slot ID out of range."); 100 } 101 102 mSlots[slot].mTexEnable = enable; 103 } 104 105 public void setTexEnvMode(EnvMode env, int slot) 106 throws IllegalArgumentException { 107 if((slot < 0) || (slot >= MAX_SLOT)) { 108 throw new IllegalArgumentException("Slot ID out of range."); 109 } 110 111 mSlots[slot].mEnv = env; 112 } 113 114 115 static synchronized ProgramFragment internalCreate(RenderScript rs, Builder b) { 116 int inID = 0; 117 int outID = 0; 118 if (b.mIn != null) { 119 inID = b.mIn.mID; 120 } 121 if (b.mOut != null) { 122 outID = b.mOut.mID; 123 } 124 rs.nProgramFragmentBegin(inID, outID); 125 for(int ct=0; ct < MAX_SLOT; ct++) { 126 if(b.mSlots[ct].mTexEnable) { 127 Slot s = b.mSlots[ct]; 128 if(s.mType != null) { 129 rs.nProgramFragmentSetType(ct, s.mType.mID); 130 } 131 rs.nProgramFragmentSetTexEnable(ct, true); 132 if(s.mEnv != null) { 133 rs.nProgramFragmentSetEnvMode(ct, s.mEnv.mID); 134 } 135 } 136 } 137 138 139 int id = rs.nProgramFragmentCreate(); 140 return new ProgramFragment(id, rs); 141 } 142 143 public ProgramFragment create() { 144 return internalCreate(mRS, this); 145 } 146 } 147} 148 149 150 151