2009-07-31 20:40:47 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package android.renderscript;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import android.content.res.Resources;
|
2009-08-31 14:06:43 -07:00
|
|
|
import android.content.res.AssetManager;
|
2009-07-31 20:40:47 -07:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.util.Log;
|
2009-08-31 14:06:43 -07:00
|
|
|
import android.util.TypedValue;
|
2009-07-31 20:40:47 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @hide
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
public class Allocation extends BaseObj {
|
2009-08-12 17:54:11 -07:00
|
|
|
Type mType;
|
|
|
|
|
|
|
|
Allocation(int id, RenderScript rs, Type t) {
|
2009-07-31 20:40:47 -07:00
|
|
|
super(rs);
|
|
|
|
mID = id;
|
2009-08-12 17:54:11 -07:00
|
|
|
mType = t;
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2010-01-12 12:12:28 -08:00
|
|
|
public Type getType() {
|
|
|
|
return mType;
|
|
|
|
}
|
|
|
|
|
2009-07-31 20:40:47 -07:00
|
|
|
public void uploadToTexture(int baseMipLevel) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2010-02-23 17:44:28 -08:00
|
|
|
mRS.nAllocationUploadToTexture(mID, false, baseMipLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void uploadToTexture(boolean genMips, int baseMipLevel) {
|
|
|
|
mRS.validate();
|
|
|
|
mRS.nAllocationUploadToTexture(mID, genMips, baseMipLevel);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2009-08-27 20:23:34 -07:00
|
|
|
public void uploadToBufferObject() {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-08-27 20:23:34 -07:00
|
|
|
mRS.nAllocationUploadToBufferObject(mID);
|
|
|
|
}
|
|
|
|
|
2009-07-31 20:40:47 -07:00
|
|
|
public void data(int[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-09-21 19:41:04 -07:00
|
|
|
subData1D(0, mType.getElementCount(), d);
|
|
|
|
}
|
|
|
|
public void data(short[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-09-21 19:41:04 -07:00
|
|
|
subData1D(0, mType.getElementCount(), d);
|
|
|
|
}
|
|
|
|
public void data(byte[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-09-21 19:41:04 -07:00
|
|
|
subData1D(0, mType.getElementCount(), d);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
public void data(float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-09-21 19:41:04 -07:00
|
|
|
subData1D(0, mType.getElementCount(), d);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2009-09-21 19:41:04 -07:00
|
|
|
private void data1DChecks(int off, int count, int len, int dataSize) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-09-21 19:41:04 -07:00
|
|
|
if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) {
|
|
|
|
throw new IllegalArgumentException("Offset or Count out of bounds.");
|
|
|
|
}
|
|
|
|
if((len) < dataSize) {
|
|
|
|
throw new IllegalArgumentException("Array too small for allocation type.");
|
|
|
|
}
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2009-09-21 19:41:04 -07:00
|
|
|
public void subData1D(int off, int count, int[] d) {
|
|
|
|
int dataSize = mType.mElement.getSizeBytes() * count;
|
|
|
|
data1DChecks(off, count, d.length * 4, dataSize);
|
|
|
|
mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
|
|
|
|
}
|
|
|
|
public void subData1D(int off, int count, short[] d) {
|
|
|
|
int dataSize = mType.mElement.getSizeBytes() * count;
|
|
|
|
data1DChecks(off, count, d.length * 2, dataSize);
|
|
|
|
mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
|
|
|
|
}
|
|
|
|
public void subData1D(int off, int count, byte[] d) {
|
|
|
|
int dataSize = mType.mElement.getSizeBytes() * count;
|
|
|
|
data1DChecks(off, count, d.length, dataSize);
|
|
|
|
mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
|
|
|
|
}
|
2009-07-31 20:40:47 -07:00
|
|
|
public void subData1D(int off, int count, float[] d) {
|
2009-09-21 19:41:04 -07:00
|
|
|
int dataSize = mType.mElement.getSizeBytes() * count;
|
|
|
|
data1DChecks(off, count, d.length * 4, dataSize);
|
|
|
|
mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2009-09-21 19:41:04 -07:00
|
|
|
|
|
|
|
|
2009-07-31 20:40:47 -07:00
|
|
|
public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-08-27 20:23:34 -07:00
|
|
|
mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-08-27 20:23:34 -07:00
|
|
|
mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2009-08-10 14:55:26 -07:00
|
|
|
public void readData(int[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-08-10 14:55:26 -07:00
|
|
|
mRS.nAllocationRead(mID, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void readData(float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-08-10 14:55:26 -07:00
|
|
|
mRS.nAllocationRead(mID, d);
|
|
|
|
}
|
|
|
|
|
2009-08-12 17:54:11 -07:00
|
|
|
public void data(Object o) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-09-03 15:43:13 -07:00
|
|
|
mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
|
2009-08-12 17:54:11 -07:00
|
|
|
}
|
|
|
|
|
2009-09-15 12:39:22 -07:00
|
|
|
public void read(Object o) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-09-15 12:39:22 -07:00
|
|
|
mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
|
|
|
|
}
|
|
|
|
|
2009-09-03 15:43:13 -07:00
|
|
|
public void subData(int offset, Object o) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-09-03 15:43:13 -07:00
|
|
|
mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
|
|
|
|
}
|
2009-08-10 14:55:26 -07:00
|
|
|
|
2009-07-31 20:40:47 -07:00
|
|
|
public class Adapter1D extends BaseObj {
|
|
|
|
Adapter1D(int id, RenderScript rs) {
|
|
|
|
super(rs);
|
|
|
|
mID = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setConstraint(Dimension dim, int value) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-07-31 20:40:47 -07:00
|
|
|
mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void data(int[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-07-31 20:40:47 -07:00
|
|
|
mRS.nAdapter1DData(mID, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void data(float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-07-31 20:40:47 -07:00
|
|
|
mRS.nAdapter1DData(mID, d);
|
|
|
|
}
|
|
|
|
|
2009-08-03 16:03:08 -07:00
|
|
|
public void subData(int off, int count, int[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-08-03 16:03:08 -07:00
|
|
|
mRS.nAdapter1DSubData(mID, off, count, d);
|
|
|
|
}
|
|
|
|
|
2009-07-31 20:40:47 -07:00
|
|
|
public void subData(int off, int count, float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-07-31 20:40:47 -07:00
|
|
|
mRS.nAdapter1DSubData(mID, off, count, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Adapter1D createAdapter1D() {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-07-31 20:40:47 -07:00
|
|
|
int id = mRS.nAdapter1DCreate();
|
2009-12-23 14:35:29 -08:00
|
|
|
if(id == 0) {
|
|
|
|
throw new IllegalStateException("allocation failed.");
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
2009-12-23 14:35:29 -08:00
|
|
|
mRS.nAdapter1DBindAllocation(id, mID);
|
2009-07-31 20:40:47 -07:00
|
|
|
return new Adapter1D(id, mRS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-03 16:03:08 -07:00
|
|
|
public class Adapter2D extends BaseObj {
|
|
|
|
Adapter2D(int id, RenderScript rs) {
|
|
|
|
super(rs);
|
|
|
|
mID = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setConstraint(Dimension dim, int value) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-08-03 16:03:08 -07:00
|
|
|
mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void data(int[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-08-03 16:03:08 -07:00
|
|
|
mRS.nAdapter2DData(mID, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void data(float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-08-03 16:03:08 -07:00
|
|
|
mRS.nAdapter2DData(mID, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void subData(int xoff, int yoff, int w, int h, int[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-08-03 16:03:08 -07:00
|
|
|
mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void subData(int xoff, int yoff, int w, int h, float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-08-03 16:03:08 -07:00
|
|
|
mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Adapter2D createAdapter2D() {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2009-08-03 16:03:08 -07:00
|
|
|
int id = mRS.nAdapter2DCreate();
|
2009-12-23 14:35:29 -08:00
|
|
|
if(id == 0) {
|
|
|
|
throw new IllegalStateException("allocation failed.");
|
2009-08-03 16:03:08 -07:00
|
|
|
}
|
2009-12-23 14:35:29 -08:00
|
|
|
mRS.nAdapter2DBindAllocation(id, mID);
|
2009-08-03 16:03:08 -07:00
|
|
|
return new Adapter2D(id, mRS);
|
|
|
|
}
|
|
|
|
|
2009-07-31 20:40:47 -07:00
|
|
|
|
|
|
|
// creation
|
|
|
|
|
|
|
|
private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
|
|
|
|
static {
|
|
|
|
mBitmapOptions.inScaled = false;
|
|
|
|
}
|
|
|
|
|
2009-08-09 17:01:55 -07:00
|
|
|
static public Allocation createTyped(RenderScript rs, Type type)
|
|
|
|
throws IllegalArgumentException {
|
|
|
|
|
2009-12-07 12:40:12 -08:00
|
|
|
rs.validate();
|
2009-08-09 17:01:55 -07:00
|
|
|
if(type.mID == 0) {
|
|
|
|
throw new IllegalStateException("Bad Type");
|
|
|
|
}
|
2009-07-31 20:40:47 -07:00
|
|
|
int id = rs.nAllocationCreateTyped(type.mID);
|
2009-08-12 17:54:11 -07:00
|
|
|
return new Allocation(id, rs, type);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2009-08-09 17:01:55 -07:00
|
|
|
static public Allocation createSized(RenderScript rs, Element e, int count)
|
|
|
|
throws IllegalArgumentException {
|
|
|
|
|
2009-12-07 12:40:12 -08:00
|
|
|
rs.validate();
|
2009-09-21 19:41:04 -07:00
|
|
|
Type.Builder b = new Type.Builder(rs, e);
|
|
|
|
b.add(Dimension.X, count);
|
|
|
|
Type t = b.create();
|
|
|
|
|
|
|
|
int id = rs.nAllocationCreateTyped(t.mID);
|
2009-09-04 14:42:41 -07:00
|
|
|
if(id == 0) {
|
|
|
|
throw new IllegalStateException("Bad element.");
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
2009-09-21 19:41:04 -07:00
|
|
|
return new Allocation(id, rs, t);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
|
|
|
|
throws IllegalArgumentException {
|
|
|
|
|
2009-12-07 12:40:12 -08:00
|
|
|
rs.validate();
|
2009-09-04 14:42:41 -07:00
|
|
|
int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
|
2009-12-23 14:35:29 -08:00
|
|
|
if(id == 0) {
|
|
|
|
throw new IllegalStateException("Load failed.");
|
|
|
|
}
|
2009-08-12 17:54:11 -07:00
|
|
|
return new Allocation(id, rs, null);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2010-01-06 15:10:29 -08:00
|
|
|
static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
|
2009-07-31 20:40:47 -07:00
|
|
|
throws IllegalArgumentException {
|
|
|
|
|
2009-12-07 12:40:12 -08:00
|
|
|
rs.validate();
|
2009-09-04 14:42:41 -07:00
|
|
|
int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
|
2009-12-23 14:35:29 -08:00
|
|
|
if(id == 0) {
|
|
|
|
throw new IllegalStateException("Load failed.");
|
|
|
|
}
|
2009-08-12 17:54:11 -07:00
|
|
|
return new Allocation(id, rs, null);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
|
|
|
|
throws IllegalArgumentException {
|
|
|
|
|
2009-12-07 12:40:12 -08:00
|
|
|
rs.validate();
|
2009-08-31 14:06:43 -07:00
|
|
|
InputStream is = null;
|
|
|
|
try {
|
|
|
|
final TypedValue value = new TypedValue();
|
|
|
|
is = res.openRawResource(id, value);
|
|
|
|
|
|
|
|
int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
|
2009-09-04 14:42:41 -07:00
|
|
|
int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
|
2009-08-31 14:06:43 -07:00
|
|
|
asset);
|
|
|
|
|
2009-12-23 14:35:29 -08:00
|
|
|
if(allocationId == 0) {
|
|
|
|
throw new IllegalStateException("Load failed.");
|
|
|
|
}
|
2009-09-04 14:42:41 -07:00
|
|
|
return new Allocation(allocationId, rs, null);
|
2009-08-31 14:06:43 -07:00
|
|
|
} catch (Exception e) {
|
|
|
|
// Ignore
|
|
|
|
} finally {
|
|
|
|
if (is != null) {
|
|
|
|
try {
|
|
|
|
is.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
|
|
|
|
throws IllegalArgumentException {
|
|
|
|
|
|
|
|
Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
|
|
|
|
return createFromBitmapBoxed(rs, b, dstFmt, genMips);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|