1/* 2 * Copyright (C) 2013 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.support.v8.renderscript; 18 19import android.content.Context; 20import android.content.res.Resources; 21import android.util.Log; 22import android.os.Bundle; 23 24import java.io.IOException; 25import java.io.InputStream; 26 27import android.graphics.Bitmap; 28import android.graphics.BitmapFactory; 29 30/** 31 * 32 **/ 33class SamplerThunker extends Sampler { 34 android.renderscript.Sampler mN; 35 36 protected SamplerThunker(int id, RenderScript rs) { 37 super(id, rs); 38 } 39 40 android.renderscript.BaseObj getNObj() { 41 return mN; 42 } 43 44 static android.renderscript.Sampler.Value convertValue (Value v) { 45 switch (v) { 46 case NEAREST: 47 return android.renderscript.Sampler.Value.NEAREST; 48 case LINEAR: 49 return android.renderscript.Sampler.Value.LINEAR; 50 case LINEAR_MIP_LINEAR: 51 return android.renderscript.Sampler.Value.LINEAR_MIP_LINEAR; 52 case LINEAR_MIP_NEAREST: 53 return android.renderscript.Sampler.Value.LINEAR_MIP_NEAREST; 54 case WRAP: 55 return android.renderscript.Sampler.Value.WRAP; 56 case CLAMP: 57 return android.renderscript.Sampler.Value.CLAMP; 58 case MIRRORED_REPEAT: 59 return android.renderscript.Sampler.Value.MIRRORED_REPEAT; 60 } 61 return null; 62 } 63 64 /** 65 * Builder for creating non-standard samplers. Useful if mix and match of 66 * wrap modes is necesary or if anisotropic filtering is desired. 67 * 68 */ 69 public static class Builder { 70 RenderScriptThunker mRS; 71 Value mMin; 72 Value mMag; 73 Value mWrapS; 74 Value mWrapT; 75 Value mWrapR; 76 float mAniso; 77 78 public Builder(RenderScriptThunker rs) { 79 mRS = rs; 80 mMin = Value.NEAREST; 81 mMag = Value.NEAREST; 82 mWrapS = Value.WRAP; 83 mWrapT = Value.WRAP; 84 mWrapR = Value.WRAP; 85 } 86 87 public void setMinification(Value v) { 88 if (v == Value.NEAREST || 89 v == Value.LINEAR || 90 v == Value.LINEAR_MIP_LINEAR || 91 v == Value.LINEAR_MIP_NEAREST) { 92 mMin = v; 93 } else { 94 throw new IllegalArgumentException("Invalid value"); 95 } 96 } 97 98 public void setMagnification(Value v) { 99 if (v == Value.NEAREST || v == Value.LINEAR) { 100 mMag = v; 101 } else { 102 throw new IllegalArgumentException("Invalid value"); 103 } 104 } 105 106 public void setWrapS(Value v) { 107 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) { 108 mWrapS = v; 109 } else { 110 throw new IllegalArgumentException("Invalid value"); 111 } 112 } 113 114 public void setWrapT(Value v) { 115 if (v == Value.WRAP || v == Value.CLAMP || v == Value.MIRRORED_REPEAT) { 116 mWrapT = v; 117 } else { 118 throw new IllegalArgumentException("Invalid value"); 119 } 120 } 121 122 public void setAnisotropy(float v) { 123 if(v >= 0.0f) { 124 mAniso = v; 125 } else { 126 throw new IllegalArgumentException("Invalid value"); 127 } 128 } 129 130 public Sampler create() { 131 mRS.validate(); 132 try { 133 android.renderscript.Sampler.Builder b = new android.renderscript.Sampler.Builder(mRS.mN); 134 b.setMinification(convertValue(mMin)); 135 b.setMagnification(convertValue(mMag)); 136 b.setWrapS(convertValue(mWrapS)); 137 b.setWrapT(convertValue(mWrapT)); 138 b.setAnisotropy(mAniso); 139 android.renderscript.Sampler s = b.create(); 140 141 SamplerThunker sampler = new SamplerThunker(0, mRS); 142 sampler.mMin = mMin; 143 sampler.mMag = mMag; 144 sampler.mWrapS = mWrapS; 145 sampler.mWrapT = mWrapT; 146 sampler.mWrapR = mWrapR; 147 sampler.mAniso = mAniso; 148 sampler.mN = s; 149 150 return sampler; 151 } catch (android.renderscript.RSRuntimeException e) { 152 throw ExceptionThunker.convertException(e); 153 } 154 } 155 } 156 157 158}