blob: da83d04bb803461531d50153165cad524ef290aa [file] [log] [blame]
Jason Sams0835d422009-08-04 17:58:23 -07001/*
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(rs);
51 mID = id;
52 }
53
Jason Sams4d339932010-05-11 14:03:58 -070054 Sampler mSampler_CLAMP_NEAREST;
55 Sampler mSampler_CLAMP_LINEAR;
56 Sampler mSampler_CLAMP_LINEAR_MIP;
57 Sampler mSampler_WRAP_NEAREST;
58 Sampler mSampler_WRAP_LINEAR;
59 Sampler mSampler_WRAP_LINEAR_MIP;
60
61 public static Sampler CLAMP_NEAREST(RenderScript rs) {
62 if(rs.mSampler_CLAMP_NEAREST == null) {
63 Builder b = new Builder(rs);
64 b.setMin(Value.NEAREST);
65 b.setMag(Value.NEAREST);
66 b.setWrapS(Value.CLAMP);
67 b.setWrapT(Value.CLAMP);
68 rs.mSampler_CLAMP_NEAREST = b.create();
69 }
70 return rs.mSampler_CLAMP_NEAREST;
71 }
72
73 public static Sampler CLAMP_LINEAR(RenderScript rs) {
74 if(rs.mSampler_CLAMP_LINEAR == null) {
75 Builder b = new Builder(rs);
76 b.setMin(Value.LINEAR);
77 b.setMag(Value.LINEAR);
78 b.setWrapS(Value.CLAMP);
79 b.setWrapT(Value.CLAMP);
80 rs.mSampler_CLAMP_LINEAR = b.create();
81 }
82 return rs.mSampler_CLAMP_LINEAR;
83 }
84
85 public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
86 if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
87 Builder b = new Builder(rs);
88 b.setMin(Value.LINEAR_MIP_LINEAR);
89 b.setMag(Value.LINEAR_MIP_LINEAR);
90 b.setWrapS(Value.CLAMP);
91 b.setWrapT(Value.CLAMP);
92 rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
93 }
94 return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
95 }
96
97 public static Sampler WRAP_NEAREST(RenderScript rs) {
98 if(rs.mSampler_WRAP_NEAREST == null) {
99 Builder b = new Builder(rs);
100 b.setMin(Value.NEAREST);
101 b.setMag(Value.NEAREST);
102 b.setWrapS(Value.WRAP);
103 b.setWrapT(Value.WRAP);
104 rs.mSampler_WRAP_NEAREST = b.create();
105 }
106 return rs.mSampler_WRAP_NEAREST;
107 }
108
109 public static Sampler WRAP_LINEAR(RenderScript rs) {
110 if(rs.mSampler_WRAP_LINEAR == null) {
111 Builder b = new Builder(rs);
112 b.setMin(Value.LINEAR);
113 b.setMag(Value.LINEAR);
114 b.setWrapS(Value.WRAP);
115 b.setWrapT(Value.WRAP);
116 rs.mSampler_WRAP_LINEAR = b.create();
117 }
118 return rs.mSampler_WRAP_LINEAR;
119 }
120
121 public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
122 if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
123 Builder b = new Builder(rs);
124 b.setMin(Value.LINEAR_MIP_LINEAR);
125 b.setMag(Value.LINEAR_MIP_LINEAR);
126 b.setWrapS(Value.WRAP);
127 b.setWrapT(Value.WRAP);
128 rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
129 }
130 return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
131 }
132
133
Jason Sams0835d422009-08-04 17:58:23 -0700134 public static class Builder {
135 RenderScript mRS;
136 Value mMin;
137 Value mMag;
138 Value mWrapS;
139 Value mWrapT;
140 Value mWrapR;
141
142 public Builder(RenderScript rs) {
143 mRS = rs;
144 mMin = Value.NEAREST;
145 mMag = Value.NEAREST;
146 mWrapS = Value.WRAP;
147 mWrapT = Value.WRAP;
148 mWrapR = Value.WRAP;
149 }
150
151 public void setMin(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800152 if (v == Value.NEAREST ||
153 v == Value.LINEAR ||
154 v == Value.LINEAR_MIP_LINEAR) {
155 mMin = v;
156 } else {
157 throw new IllegalArgumentException("Invalid value");
158 }
Jason Sams0835d422009-08-04 17:58:23 -0700159 }
160
161 public void setMag(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800162 if (v == Value.NEAREST || v == Value.LINEAR) {
163 mMag = v;
164 } else {
165 throw new IllegalArgumentException("Invalid value");
166 }
Jason Sams0835d422009-08-04 17:58:23 -0700167 }
168
169 public void setWrapS(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800170 if (v == Value.WRAP || v == Value.CLAMP) {
171 mWrapS = v;
172 } else {
173 throw new IllegalArgumentException("Invalid value");
174 }
Jason Sams0835d422009-08-04 17:58:23 -0700175 }
176
177 public void setWrapT(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800178 if (v == Value.WRAP || v == Value.CLAMP) {
179 mWrapT = v;
180 } else {
181 throw new IllegalArgumentException("Invalid value");
182 }
Jason Sams0835d422009-08-04 17:58:23 -0700183 }
184
185 public void setWrapR(Value v) {
Jason Sams8bb41dd2009-12-16 15:59:59 -0800186 if (v == Value.WRAP || v == Value.CLAMP) {
187 mWrapR = v;
188 } else {
189 throw new IllegalArgumentException("Invalid value");
190 }
Jason Sams0835d422009-08-04 17:58:23 -0700191 }
192
193 static synchronized Sampler internalCreate(RenderScript rs, Builder b) {
194 rs.nSamplerBegin();
195 rs.nSamplerSet(0, b.mMin.mID);
196 rs.nSamplerSet(1, b.mMag.mID);
197 rs.nSamplerSet(2, b.mWrapS.mID);
198 rs.nSamplerSet(3, b.mWrapT.mID);
199 rs.nSamplerSet(4, b.mWrapR.mID);
200 int id = rs.nSamplerCreate();
201 return new Sampler(id, rs);
202 }
203
204 public Sampler create() {
Jason Sams771bebb2009-12-07 12:40:12 -0800205 mRS.validate();
Jason Sams0835d422009-08-04 17:58:23 -0700206 return internalCreate(mRS, this);
207 }
208 }
209
210}
211