Sampler.java revision 0857196107d55bae312c12b72b115d25d5fee4f9
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 java.io.IOException; 21import java.io.InputStream; 22 23import android.content.res.Resources; 24import android.os.Bundle; 25import android.util.Config; 26import android.util.Log; 27 28import android.graphics.Bitmap; 29import android.graphics.BitmapFactory; 30 31/** 32 * @hide 33 * 34 * Sampler object which defines how data is extracted from textures. Samplers 35 * are attached to Program objects (currently only fragment) when those objects 36 * need to access texture data. 37 **/ 38public class Sampler extends BaseObj { 39 public enum Value { 40 NEAREST (0), 41 LINEAR (1), 42 LINEAR_MIP_LINEAR (2), 43 LINEAR_MIP_NEAREST (5), 44 WRAP (3), 45 CLAMP (4); 46 47 int mID; 48 Value(int id) { 49 mID = id; 50 } 51 } 52 53 Sampler(int id, RenderScript rs) { 54 super(id, rs); 55 } 56 57 /** 58 * Retrieve a sampler with min and mag set to nearest and wrap modes set to 59 * clamp. 60 * 61 * @param rs 62 * 63 * @return Sampler 64 */ 65 public static Sampler CLAMP_NEAREST(RenderScript rs) { 66 if(rs.mSampler_CLAMP_NEAREST == null) { 67 Builder b = new Builder(rs); 68 b.setMin(Value.NEAREST); 69 b.setMag(Value.NEAREST); 70 b.setWrapS(Value.CLAMP); 71 b.setWrapT(Value.CLAMP); 72 rs.mSampler_CLAMP_NEAREST = b.create(); 73 } 74 return rs.mSampler_CLAMP_NEAREST; 75 } 76 77 /** 78 * Retrieve a sampler with min and mag set to linear and wrap modes set to 79 * clamp. 80 * 81 * @param rs 82 * 83 * @return Sampler 84 */ 85 public static Sampler CLAMP_LINEAR(RenderScript rs) { 86 if(rs.mSampler_CLAMP_LINEAR == null) { 87 Builder b = new Builder(rs); 88 b.setMin(Value.LINEAR); 89 b.setMag(Value.LINEAR); 90 b.setWrapS(Value.CLAMP); 91 b.setWrapT(Value.CLAMP); 92 rs.mSampler_CLAMP_LINEAR = b.create(); 93 } 94 return rs.mSampler_CLAMP_LINEAR; 95 } 96 97 /** 98 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and 99 * to and wrap modes set to clamp. 100 * 101 * @param rs 102 * 103 * @return Sampler 104 */ 105 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) { 106 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) { 107 Builder b = new Builder(rs); 108 b.setMin(Value.LINEAR_MIP_LINEAR); 109 b.setMag(Value.LINEAR); 110 b.setWrapS(Value.CLAMP); 111 b.setWrapT(Value.CLAMP); 112 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create(); 113 } 114 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR; 115 } 116 117 /** 118 * Retrieve a sampler with min and mag set to nearest and wrap modes set to 119 * wrap. 120 * 121 * @param rs 122 * 123 * @return Sampler 124 */ 125 public static Sampler WRAP_NEAREST(RenderScript rs) { 126 if(rs.mSampler_WRAP_NEAREST == null) { 127 Builder b = new Builder(rs); 128 b.setMin(Value.NEAREST); 129 b.setMag(Value.NEAREST); 130 b.setWrapS(Value.WRAP); 131 b.setWrapT(Value.WRAP); 132 rs.mSampler_WRAP_NEAREST = b.create(); 133 } 134 return rs.mSampler_WRAP_NEAREST; 135 } 136 137 /** 138 * Retrieve a sampler with min and mag set to nearest and wrap modes set to 139 * wrap. 140 * 141 * @param rs 142 * 143 * @return Sampler 144 */ 145 public static Sampler WRAP_LINEAR(RenderScript rs) { 146 if(rs.mSampler_WRAP_LINEAR == null) { 147 Builder b = new Builder(rs); 148 b.setMin(Value.LINEAR); 149 b.setMag(Value.LINEAR); 150 b.setWrapS(Value.WRAP); 151 b.setWrapT(Value.WRAP); 152 rs.mSampler_WRAP_LINEAR = b.create(); 153 } 154 return rs.mSampler_WRAP_LINEAR; 155 } 156 157 /** 158 * Retrieve a sampler with ag set to linear, min linear mipmap linear, and 159 * to and wrap modes set to wrap. 160 * 161 * @param rs 162 * 163 * @return Sampler 164 */ 165 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) { 166 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) { 167 Builder b = new Builder(rs); 168 b.setMin(Value.LINEAR_MIP_LINEAR); 169 b.setMag(Value.LINEAR); 170 b.setWrapS(Value.WRAP); 171 b.setWrapT(Value.WRAP); 172 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create(); 173 } 174 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR; 175 } 176 177 178 /** 179 * Builder for creating non-standard samplers. Usefull if mix and match of 180 * wrap modes is necesary or if anisotropic filtering is desired. 181 * 182 */ 183 public static class Builder { 184 RenderScript mRS; 185 Value mMin; 186 Value mMag; 187 Value mWrapS; 188 Value mWrapT; 189 Value mWrapR; 190 float mAniso; 191 192 public Builder(RenderScript rs) { 193 mRS = rs; 194 mMin = Value.NEAREST; 195 mMag = Value.NEAREST; 196 mWrapS = Value.WRAP; 197 mWrapT = Value.WRAP; 198 mWrapR = Value.WRAP; 199 mAniso = 1.0f; 200 } 201 202 public void setMin(Value v) { 203 if (v == Value.NEAREST || 204 v == Value.LINEAR || 205 v == Value.LINEAR_MIP_LINEAR || 206 v == Value.LINEAR_MIP_NEAREST) { 207 mMin = v; 208 } else { 209 throw new IllegalArgumentException("Invalid value"); 210 } 211 } 212 213 public void setMag(Value v) { 214 if (v == Value.NEAREST || v == Value.LINEAR) { 215 mMag = v; 216 } else { 217 throw new IllegalArgumentException("Invalid value"); 218 } 219 } 220 221 public void setWrapS(Value v) { 222 if (v == Value.WRAP || v == Value.CLAMP) { 223 mWrapS = v; 224 } else { 225 throw new IllegalArgumentException("Invalid value"); 226 } 227 } 228 229 public void setWrapT(Value v) { 230 if (v == Value.WRAP || v == Value.CLAMP) { 231 mWrapT = v; 232 } else { 233 throw new IllegalArgumentException("Invalid value"); 234 } 235 } 236 237 public void setWrapR(Value v) { 238 if (v == Value.WRAP || v == Value.CLAMP) { 239 mWrapR = v; 240 } else { 241 throw new IllegalArgumentException("Invalid value"); 242 } 243 } 244 245 public void setAnisotropy(float v) { 246 if(v >= 0.0f) { 247 mAniso = v; 248 } else { 249 throw new IllegalArgumentException("Invalid value"); 250 } 251 } 252 253 static synchronized Sampler internalCreate(RenderScript rs, Builder b) { 254 rs.nSamplerBegin(); 255 rs.nSamplerSet(0, b.mMin.mID); 256 rs.nSamplerSet(1, b.mMag.mID); 257 rs.nSamplerSet(2, b.mWrapS.mID); 258 rs.nSamplerSet(3, b.mWrapT.mID); 259 rs.nSamplerSet(4, b.mWrapR.mID); 260 rs.nSamplerSet2(5, b.mAniso); 261 int id = rs.nSamplerCreate(); 262 return new Sampler(id, rs); 263 } 264 265 public Sampler create() { 266 mRS.validate(); 267 return internalCreate(mRS, this); 268 } 269 } 270 271} 272 273