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