2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.renderscript;
19 import java.io.IOException;
20 import java.io.InputStream;
22 import android.content.res.Resources;
23 import android.content.res.AssetManager;
24 import android.graphics.Bitmap;
25 import android.graphics.BitmapFactory;
26 import android.util.Log;
27 import android.util.TypedValue;
33 public class Allocation extends BaseObj {
37 Allocation(int id, RenderScript rs, Type t) {
43 public Type getType() {
47 public void uploadToTexture(int baseMipLevel) {
49 mRS.nAllocationUploadToTexture(mID, false, baseMipLevel);
52 public void uploadToTexture(boolean genMips, int baseMipLevel) {
54 mRS.nAllocationUploadToTexture(mID, genMips, baseMipLevel);
57 public void uploadToBufferObject() {
59 mRS.nAllocationUploadToBufferObject(mID);
62 public void data(int[] d) {
64 subData1D(0, mType.getElementCount(), d);
66 public void data(short[] d) {
68 subData1D(0, mType.getElementCount(), d);
70 public void data(byte[] d) {
72 subData1D(0, mType.getElementCount(), d);
74 public void data(float[] d) {
76 subData1D(0, mType.getElementCount(), d);
79 private void data1DChecks(int off, int count, int len, int dataSize) {
81 if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) {
82 throw new IllegalArgumentException("Offset or Count out of bounds.");
84 if((len) < dataSize) {
85 throw new IllegalArgumentException("Array too small for allocation type.");
89 public void subData1D(int off, int count, int[] d) {
90 int dataSize = mType.mElement.getSizeBytes() * count;
91 data1DChecks(off, count, d.length * 4, dataSize);
92 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
94 public void subData1D(int off, int count, short[] d) {
95 int dataSize = mType.mElement.getSizeBytes() * count;
96 data1DChecks(off, count, d.length * 2, dataSize);
97 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
99 public void subData1D(int off, int count, byte[] d) {
100 int dataSize = mType.mElement.getSizeBytes() * count;
101 data1DChecks(off, count, d.length, dataSize);
102 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
104 public void subData1D(int off, int count, float[] d) {
105 int dataSize = mType.mElement.getSizeBytes() * count;
106 data1DChecks(off, count, d.length * 4, dataSize);
107 mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
112 public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
114 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
117 public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
119 mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
122 public void readData(int[] d) {
124 mRS.nAllocationRead(mID, d);
127 public void readData(float[] d) {
129 mRS.nAllocationRead(mID, d);
132 public void data(Object o) {
134 mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
137 public void read(Object o) {
139 mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
142 public void subData(int offset, Object o) {
144 mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
147 public class Adapter1D extends BaseObj {
148 Adapter1D(int id, RenderScript rs) {
153 public void setConstraint(Dimension dim, int value) {
155 mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
158 public void data(int[] d) {
160 mRS.nAdapter1DData(mID, d);
163 public void data(float[] d) {
165 mRS.nAdapter1DData(mID, d);
168 public void subData(int off, int count, int[] d) {
170 mRS.nAdapter1DSubData(mID, off, count, d);
173 public void subData(int off, int count, float[] d) {
175 mRS.nAdapter1DSubData(mID, off, count, d);
179 public Adapter1D createAdapter1D() {
181 int id = mRS.nAdapter1DCreate();
183 throw new IllegalStateException("allocation failed.");
185 mRS.nAdapter1DBindAllocation(id, mID);
186 return new Adapter1D(id, mRS);
190 public class Adapter2D extends BaseObj {
191 Adapter2D(int id, RenderScript rs) {
196 public void setConstraint(Dimension dim, int value) {
198 mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
201 public void data(int[] d) {
203 mRS.nAdapter2DData(mID, d);
206 public void data(float[] d) {
208 mRS.nAdapter2DData(mID, d);
211 public void subData(int xoff, int yoff, int w, int h, int[] d) {
213 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
216 public void subData(int xoff, int yoff, int w, int h, float[] d) {
218 mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
222 public Adapter2D createAdapter2D() {
224 int id = mRS.nAdapter2DCreate();
226 throw new IllegalStateException("allocation failed.");
228 mRS.nAdapter2DBindAllocation(id, mID);
229 return new Adapter2D(id, mRS);
235 private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
237 mBitmapOptions.inScaled = false;
240 static public Allocation createTyped(RenderScript rs, Type type)
241 throws IllegalArgumentException {
245 throw new IllegalStateException("Bad Type");
247 int id = rs.nAllocationCreateTyped(type.mID);
248 return new Allocation(id, rs, type);
251 static public Allocation createSized(RenderScript rs, Element e, int count)
252 throws IllegalArgumentException {
255 Type.Builder b = new Type.Builder(rs, e);
256 b.add(Dimension.X, count);
259 int id = rs.nAllocationCreateTyped(t.mID);
261 throw new IllegalStateException("Bad element.");
263 return new Allocation(id, rs, t);
266 static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
267 final Bitmap.Config bc = b.getConfig();
268 if (bc == Bitmap.Config.ALPHA_8) {
269 return Element.A_8(rs);
271 if (bc == Bitmap.Config.ARGB_4444) {
272 return Element.RGBA_4444(rs);
274 if (bc == Bitmap.Config.ARGB_8888) {
275 return Element.RGBA_8888(rs);
277 if (bc == Bitmap.Config.RGB_565) {
278 return Element.RGB_565(rs);
280 throw new IllegalStateException("Bad bitmap type.");
283 static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
284 Element e = elementFromBitmap(rs, b);
285 Type.Builder tb = new Type.Builder(rs, e);
286 tb.add(Dimension.X, b.getWidth());
287 tb.add(Dimension.Y, b.getHeight());
291 static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
292 throws IllegalArgumentException {
295 Type t = typeFromBitmap(rs, b);
297 int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
299 throw new IllegalStateException("Load failed.");
301 return new Allocation(id, rs, t);
304 static public Allocation createBitmapRef(RenderScript rs, Bitmap b)
305 throws IllegalArgumentException {
308 Type t = typeFromBitmap(rs, b);
310 int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
312 throw new IllegalStateException("Load failed.");
315 Allocation a = new Allocation(id, rs, t);
320 static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
321 throws IllegalArgumentException {
324 int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
326 throw new IllegalStateException("Load failed.");
328 return new Allocation(id, rs, null);
331 static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
332 throws IllegalArgumentException {
335 InputStream is = null;
337 final TypedValue value = new TypedValue();
338 is = res.openRawResource(id, value);
340 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
341 int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
344 if(allocationId == 0) {
345 throw new IllegalStateException("Load failed.");
347 return new Allocation(allocationId, rs, null);
348 } catch (Exception e) {
354 } catch (IOException e) {
363 static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
364 throws IllegalArgumentException {
366 mBitmapOptions.inPreferredConfig = null;
367 if (dstFmt == rs.mElement_RGBA_8888) {
368 mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
369 } else if (dstFmt == rs.mElement_RGB_888) {
370 mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
371 } else if (dstFmt == rs.mElement_RGBA_4444) {
372 mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_4444;
373 } else if (dstFmt == rs.mElement_RGB_565) {
374 mBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
377 Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
378 return createFromBitmapBoxed(rs, b, dstFmt, genMips);