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