Sampler.java revision 0de9444aa6c25d2c586e8204a6168d10e67376e0
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 **/ 35public class Sampler extends BaseObj { 36 public enum Value { 37 NEAREST (0), 38 LINEAR (1), 39 LINEAR_MIP_LINEAR (2), 40 WRAP (3), 41 CLAMP (4); 42 43 int mID; 44 Value(int id) { 45 mID = id; 46 } 47 } 48 49 Sampler(int id, RenderScript rs) { 50 super(id, rs); 51 } 52 53 Sampler mSampler_CLAMP_NEAREST; 54 Sampler mSampler_CLAMP_LINEAR; 55 Sampler mSampler_CLAMP_LINEAR_MIP; 56 Sampler mSampler_WRAP_NEAREST; 57 Sampler mSampler_WRAP_LINEAR; 58 Sampler mSampler_WRAP_LINEAR_MIP; 59 60 public static Sampler CLAMP_NEAREST(RenderScript rs) { 61 if(rs.mSampler_CLAMP_NEAREST == null) { 62 Builder b = new Builder(rs); 63 b.setMin(Value.NEAREST); 64 b.setMag(Value.NEAREST); 65 b.setWrapS(Value.CLAMP); 66 b.setWrapT(Value.CLAMP); 67 rs.mSampler_CLAMP_NEAREST = b.create(); 68 } 69 return rs.mSampler_CLAMP_NEAREST; 70 } 71 72 public static Sampler CLAMP_LINEAR(RenderScript rs) { 73 if(rs.mSampler_CLAMP_LINEAR == null) { 74 Builder b = new Builder(rs); 75 b.setMin(Value.LINEAR); 76 b.setMag(Value.LINEAR); 77 b.setWrapS(Value.CLAMP); 78 b.setWrapT(Value.CLAMP); 79 rs.mSampler_CLAMP_LINEAR = b.create(); 80 } 81 return rs.mSampler_CLAMP_LINEAR; 82 } 83 84 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) { 85 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) { 86 Builder b = new Builder(rs); 87 b.setMin(Value.LINEAR_MIP_LINEAR); 88 b.setMag(Value.LINEAR_MIP_LINEAR); 89 b.setWrapS(Value.CLAMP); 90 b.setWrapT(Value.CLAMP); 91 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create(); 92 } 93 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR; 94 } 95 96 public static Sampler WRAP_NEAREST(RenderScript rs) { 97 if(rs.mSampler_WRAP_NEAREST == null) { 98 Builder b = new Builder(rs); 99 b.setMin(Value.NEAREST); 100 b.setMag(Value.NEAREST); 101 b.setWrapS(Value.WRAP); 102 b.setWrapT(Value.WRAP); 103 rs.mSampler_WRAP_NEAREST = b.create(); 104 } 105 return rs.mSampler_WRAP_NEAREST; 106 } 107 108 public static Sampler WRAP_LINEAR(RenderScript rs) { 109 if(rs.mSampler_WRAP_LINEAR == null) { 110 Builder b = new Builder(rs); 111 b.setMin(Value.LINEAR); 112 b.setMag(Value.LINEAR); 113 b.setWrapS(Value.WRAP); 114 b.setWrapT(Value.WRAP); 115 rs.mSampler_WRAP_LINEAR = b.create(); 116 } 117 return rs.mSampler_WRAP_LINEAR; 118 } 119 120 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) { 121 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) { 122 Builder b = new Builder(rs); 123 b.setMin(Value.LINEAR_MIP_LINEAR); 124 b.setMag(Value.LINEAR_MIP_LINEAR); 125 b.setWrapS(Value.WRAP); 126 b.setWrapT(Value.WRAP); 127 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create(); 128 } 129 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR; 130 } 131 132 133 public static class Builder { 134 RenderScript mRS; 135 Value mMin; 136 Value mMag; 137 Value mWrapS; 138 Value mWrapT; 139 Value mWrapR; 140 141 public Builder(RenderScript rs) { 142 mRS = rs; 143 mMin = Value.NEAREST; 144 mMag = Value.NEAREST; 145 mWrapS = Value.WRAP; 146 mWrapT = Value.WRAP; 147 mWrapR = Value.WRAP; 148 } 149 150 public void setMin(Value v) { 151 if (v == Value.NEAREST || 152 v == Value.LINEAR || 153 v == Value.LINEAR_MIP_LINEAR) { 154 mMin = v; 155 } else { 156 throw new IllegalArgumentException("Invalid value"); 157 } 158 } 159 160 public void setMag(Value v) { 161 if (v == Value.NEAREST || v == Value.LINEAR) { 162 mMag = v; 163 } else { 164 throw new IllegalArgumentException("Invalid value"); 165 } 166 } 167 168 public void setWrapS(Value v) { 169 if (v == Value.WRAP || v == Value.CLAMP) { 170 mWrapS = v; 171 } else { 172 throw new IllegalArgumentException("Invalid value"); 173 } 174 } 175 176 public void setWrapT(Value v) { 177 if (v == Value.WRAP || v == Value.CLAMP) { 178 mWrapT = v; 179 } else { 180 throw new IllegalArgumentException("Invalid value"); 181 } 182 } 183 184 public void setWrapR(Value v) { 185 if (v == Value.WRAP || v == Value.CLAMP) { 186 mWrapR = v; 187 } else { 188 throw new IllegalArgumentException("Invalid value"); 189 } 190 } 191 192 static synchronized Sampler internalCreate(RenderScript rs, Builder b) { 193 rs.nSamplerBegin(); 194 rs.nSamplerSet(0, b.mMin.mID); 195 rs.nSamplerSet(1, b.mMag.mID); 196 rs.nSamplerSet(2, b.mWrapS.mID); 197 rs.nSamplerSet(3, b.mWrapT.mID); 198 rs.nSamplerSet(4, b.mWrapR.mID); 199 int id = rs.nSamplerCreate(); 200 return new Sampler(id, rs); 201 } 202 203 public Sampler create() { 204 mRS.validate(); 205 return internalCreate(mRS, this); 206 } 207 } 208 209} 210 211