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; |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
| 27 | import android.graphics.Bitmap; |
| 28 | import android.graphics.BitmapFactory; |
| 29 | |
| 30 | /** |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 31 | * Sampler object which defines how data is extracted from textures. Samplers |
| 32 | * are attached to Program objects (currently only ProgramFragment) when those objects |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 33 | * need to access texture data. |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 34 | **/ |
| 35 | public class Sampler extends BaseObj { |
| 36 | public enum Value { |
| 37 | NEAREST (0), |
| 38 | LINEAR (1), |
| 39 | LINEAR_MIP_LINEAR (2), |
Alex Sakhartchouk | 0857196 | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 40 | LINEAR_MIP_NEAREST (5), |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 41 | WRAP (3), |
| 42 | CLAMP (4); |
| 43 | |
| 44 | int mID; |
| 45 | Value(int id) { |
| 46 | mID = id; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | Sampler(int id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 51 | super(id, rs); |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 54 | /** |
| 55 | * Retrieve a sampler with min and mag set to nearest and wrap modes set to |
| 56 | * clamp. |
| 57 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 58 | * @param rs Context to which the sampler will belong. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 59 | * |
| 60 | * @return Sampler |
| 61 | */ |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 62 | public static Sampler CLAMP_NEAREST(RenderScript rs) { |
| 63 | if(rs.mSampler_CLAMP_NEAREST == null) { |
| 64 | Builder b = new Builder(rs); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 65 | b.setMinification(Value.NEAREST); |
| 66 | b.setMagnification(Value.NEAREST); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 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 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 74 | /** |
| 75 | * Retrieve a sampler with min and mag set to linear and wrap modes set to |
| 76 | * clamp. |
| 77 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 78 | * @param rs Context to which the sampler will belong. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 79 | * |
| 80 | * @return Sampler |
| 81 | */ |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 82 | public static Sampler CLAMP_LINEAR(RenderScript rs) { |
| 83 | if(rs.mSampler_CLAMP_LINEAR == null) { |
| 84 | Builder b = new Builder(rs); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 85 | b.setMinification(Value.LINEAR); |
| 86 | b.setMagnification(Value.LINEAR); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 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 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Retrieve a sampler with ag set to linear, min linear mipmap linear, and |
| 96 | * to and wrap modes set to clamp. |
| 97 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 98 | * @param rs Context to which the sampler will belong. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 99 | * |
| 100 | * @return Sampler |
| 101 | */ |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 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); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 105 | b.setMinification(Value.LINEAR_MIP_LINEAR); |
| 106 | b.setMagnification(Value.LINEAR); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 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 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 114 | /** |
| 115 | * Retrieve a sampler with min and mag set to nearest and wrap modes set to |
| 116 | * wrap. |
| 117 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 118 | * @param rs Context to which the sampler will belong. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 119 | * |
| 120 | * @return Sampler |
| 121 | */ |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 122 | public static Sampler WRAP_NEAREST(RenderScript rs) { |
| 123 | if(rs.mSampler_WRAP_NEAREST == null) { |
| 124 | Builder b = new Builder(rs); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 125 | b.setMinification(Value.NEAREST); |
| 126 | b.setMagnification(Value.NEAREST); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 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 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 134 | /** |
| 135 | * Retrieve a sampler with min and mag set to nearest and wrap modes set to |
| 136 | * wrap. |
| 137 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 138 | * @param rs Context to which the sampler will belong. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 139 | * |
| 140 | * @return Sampler |
| 141 | */ |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 142 | public static Sampler WRAP_LINEAR(RenderScript rs) { |
| 143 | if(rs.mSampler_WRAP_LINEAR == null) { |
| 144 | Builder b = new Builder(rs); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 145 | b.setMinification(Value.LINEAR); |
| 146 | b.setMagnification(Value.LINEAR); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 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 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 154 | /** |
| 155 | * Retrieve a sampler with ag set to linear, min linear mipmap linear, and |
| 156 | * to and wrap modes set to wrap. |
| 157 | * |
Alex Sakhartchouk | f5c876e | 2011-01-13 14:53:43 -0800 | [diff] [blame] | 158 | * @param rs Context to which the sampler will belong. |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 159 | * |
| 160 | * @return Sampler |
| 161 | */ |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 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); |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 165 | b.setMinification(Value.LINEAR_MIP_LINEAR); |
| 166 | b.setMagnification(Value.LINEAR); |
Jason Sams | 4d33993 | 2010-05-11 14:03:58 -0700 | [diff] [blame] | 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 | |
Jason Sams | bf6ef8d | 2010-12-06 15:59:59 -0800 | [diff] [blame] | 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 | */ |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 180 | public static class Builder { |
| 181 | RenderScript mRS; |
| 182 | Value mMin; |
| 183 | Value mMag; |
| 184 | Value mWrapS; |
| 185 | Value mWrapT; |
| 186 | Value mWrapR; |
Alex Sakhartchouk | f5b3510 | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 187 | float mAniso; |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 188 | |
| 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; |
Alex Sakhartchouk | f5b3510 | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 196 | mAniso = 1.0f; |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 199 | public void setMinification(Value v) { |
Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 200 | if (v == Value.NEAREST || |
| 201 | v == Value.LINEAR || |
Alex Sakhartchouk | 0857196 | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 202 | v == Value.LINEAR_MIP_LINEAR || |
| 203 | v == Value.LINEAR_MIP_NEAREST) { |
Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 204 | mMin = v; |
| 205 | } else { |
| 206 | throw new IllegalArgumentException("Invalid value"); |
| 207 | } |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Alex Sakhartchouk | b4d7bb6 | 2010-12-21 14:42:26 -0800 | [diff] [blame] | 210 | public void setMagnification(Value v) { |
Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 211 | if (v == Value.NEAREST || v == Value.LINEAR) { |
| 212 | mMag = v; |
| 213 | } else { |
| 214 | throw new IllegalArgumentException("Invalid value"); |
| 215 | } |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | public void setWrapS(Value v) { |
Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 219 | if (v == Value.WRAP || v == Value.CLAMP) { |
| 220 | mWrapS = v; |
| 221 | } else { |
| 222 | throw new IllegalArgumentException("Invalid value"); |
| 223 | } |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | public void setWrapT(Value v) { |
Jason Sams | 8bb41dd | 2009-12-16 15:59:59 -0800 | [diff] [blame] | 227 | if (v == Value.WRAP || v == Value.CLAMP) { |
| 228 | mWrapT = v; |
| 229 | } else { |
| 230 | throw new IllegalArgumentException("Invalid value"); |
| 231 | } |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Alex Sakhartchouk | f5b3510 | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 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 | |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 242 | public Sampler create() { |
Jason Sams | 771bebb | 2009-12-07 12:40:12 -0800 | [diff] [blame] | 243 | mRS.validate(); |
Alex Sakhartchouk | a89094a | 2011-05-04 17:45:36 -0700 | [diff] [blame] | 244 | int id = mRS.nSamplerCreate(mMag.mID, mMin.mID, mWrapS.mID, mWrapT.mID, mWrapR.mID, mAniso); |
| 245 | return new Sampler(id, mRS); |
Jason Sams | 0835d42 | 2009-08-04 17:58:23 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | } |
| 250 | |