| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 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 | package android.renderscript; |
| 18 | |
| 19 | |
| 20 | import android.util.Config; |
| 21 | import android.util.Log; |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * @hide |
| 26 | * |
| 27 | **/ |
| 28 | public class ProgramFragment extends BaseObj { |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame^] | 29 | public static final int MAX_SLOT = 2; |
| 30 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 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 | mRS.nProgramFragmentStoreDestroy(mID); |
| 50 | mID = 0; |
| 51 | } |
| 52 | |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame^] | 53 | public void bindTexture(Allocation va, int slot) |
| 54 | throws IllegalArgumentException { |
| 55 | if((slot < 0) || (slot >= MAX_SLOT)) { |
| 56 | throw new IllegalArgumentException("Slot ID out of range."); |
| 57 | } |
| 58 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 59 | mRS.nProgramFragmentBindTexture(mID, slot, va.mID); |
| 60 | } |
| 61 | |
| Jason Sams | 110195f | 2009-08-04 18:47:46 -0700 | [diff] [blame^] | 62 | public void bindSampler(Sampler vs, int slot) |
| 63 | throws IllegalArgumentException { |
| 64 | if((slot < 0) || (slot >= MAX_SLOT)) { |
| 65 | throw new IllegalArgumentException("Slot ID out of range."); |
| 66 | } |
| 67 | |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 68 | mRS.nProgramFragmentBindSampler(mID, slot, vs.mID); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | public static class Builder { |
| Jason Sams | 2253417 | 2009-08-04 16:58:20 -0700 | [diff] [blame] | 73 | RenderScript mRS; |
| 74 | Element mIn; |
| 75 | Element mOut; |
| 76 | |
| 77 | private class Slot { |
| 78 | Type mType; |
| 79 | EnvMode mEnv; |
| 80 | boolean mTexEnable; |
| 81 | |
| 82 | Slot() { |
| 83 | mTexEnable = false; |
| 84 | } |
| 85 | } |
| 86 | Slot[] mSlots; |
| 87 | |
| 88 | public Builder(RenderScript rs, Element in, Element out) { |
| 89 | mRS = rs; |
| 90 | mIn = in; |
| 91 | mOut = out; |
| 92 | mSlots = new Slot[MAX_SLOT]; |
| 93 | for(int ct=0; ct < MAX_SLOT; ct++) { |
| 94 | mSlots[ct] = new Slot(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public void setType(int slot, Type t) |
| 99 | throws IllegalArgumentException { |
| 100 | if((slot < 0) || (slot >= MAX_SLOT)) { |
| 101 | throw new IllegalArgumentException("Slot ID out of range."); |
| 102 | } |
| 103 | |
| 104 | mSlots[slot].mType = t; |
| 105 | } |
| 106 | |
| 107 | public void setTexEnable(boolean enable, int slot) |
| 108 | throws IllegalArgumentException { |
| 109 | if((slot < 0) || (slot >= MAX_SLOT)) { |
| 110 | throw new IllegalArgumentException("Slot ID out of range."); |
| 111 | } |
| 112 | |
| 113 | mSlots[slot].mTexEnable = enable; |
| 114 | } |
| 115 | |
| 116 | public void setTexEnvMode(EnvMode env, int slot) |
| 117 | throws IllegalArgumentException { |
| 118 | if((slot < 0) || (slot >= MAX_SLOT)) { |
| 119 | throw new IllegalArgumentException("Slot ID out of range."); |
| 120 | } |
| 121 | |
| 122 | mSlots[slot].mEnv = env; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | static synchronized ProgramFragment internalCreate(RenderScript rs, Builder b) { |
| 127 | int inID = 0; |
| 128 | int outID = 0; |
| 129 | if (b.mIn != null) { |
| 130 | inID = b.mIn.mID; |
| 131 | } |
| 132 | if (b.mOut != null) { |
| 133 | outID = b.mOut.mID; |
| 134 | } |
| 135 | rs.nProgramFragmentBegin(inID, outID); |
| 136 | for(int ct=0; ct < MAX_SLOT; ct++) { |
| 137 | if(b.mSlots[ct].mTexEnable) { |
| 138 | Slot s = b.mSlots[ct]; |
| 139 | if(s.mType != null) { |
| 140 | rs.nProgramFragmentSetType(ct, s.mType.mID); |
| 141 | } |
| 142 | rs.nProgramFragmentSetTexEnable(ct, true); |
| 143 | if(s.mEnv != null) { |
| 144 | rs.nProgramFragmentSetEnvMode(ct, s.mEnv.mID); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | |
| 150 | int id = rs.nProgramFragmentCreate(); |
| 151 | return new ProgramFragment(id, rs); |
| 152 | } |
| 153 | |
| 154 | public ProgramFragment create() { |
| 155 | return internalCreate(mRS, this); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | |