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;
|
2010-03-01 15:31:04 -08:00
|
|
|
Bitmap mBitmap;
|
2009-08-12 17:54:11 -07:00
|
|
|
|
|
|
|
Allocation(int id, RenderScript rs, Type t) {
|
2010-08-11 14:41:28 -07:00
|
|
|
super(id, rs);
|
2009-08-12 17:54:11 -07:00
|
|
|
mType = t;
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2010-07-12 15:50:32 -07:00
|
|
|
Allocation(int id, RenderScript rs) {
|
2010-08-11 14:41:28 -07:00
|
|
|
super(id, rs);
|
2010-07-12 15:50:32 -07:00
|
|
|
}
|
|
|
|
|
2010-07-15 11:33:03 -07:00
|
|
|
@Override
|
|
|
|
void updateFromNative() {
|
2010-11-09 17:11:40 -08:00
|
|
|
super.updateFromNative();
|
|
|
|
int typeID = mRS.nAllocationGetType(getID());
|
2010-07-15 11:33:03 -07:00
|
|
|
if(typeID != 0) {
|
|
|
|
mType = new Type(typeID, mRS);
|
|
|
|
mType.updateFromNative();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationUploadToTexture(getID(), false, baseMipLevel);
|
2010-02-23 17:44:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void uploadToTexture(boolean genMips, int baseMipLevel) {
|
|
|
|
mRS.validate();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationUploadToTexture(getID(), 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();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationUploadToBufferObject(getID());
|
2009-08-27 20:23:34 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
public void updateFromBitmap(Bitmap b) {
|
2010-10-11 12:35:15 -07:00
|
|
|
|
|
|
|
mRS.validate();
|
|
|
|
if(mType.getX() != b.getWidth() ||
|
|
|
|
mType.getY() != b.getHeight()) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
|
2010-10-11 12:35:15 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationUpdateFromBitmap(getID(), b);
|
2010-10-11 12:35:15 -07:00
|
|
|
}
|
|
|
|
|
2010-08-31 13:50:42 -07:00
|
|
|
public void subData(int xoff, FieldPacker fp) {
|
2010-03-26 15:33:42 -07:00
|
|
|
int eSize = mType.mElement.getSizeBytes();
|
|
|
|
final byte[] data = fp.getData();
|
|
|
|
|
|
|
|
int count = data.length / eSize;
|
|
|
|
if ((eSize * count) != data.length) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSIllegalArgumentException("Field packer length " + data.length +
|
2010-03-26 15:33:42 -07:00
|
|
|
" not divisible by element size " + eSize + ".");
|
|
|
|
}
|
2010-08-31 13:50:42 -07:00
|
|
|
data1DChecks(xoff, count, data.length, data.length);
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationSubData1D(getID(), xoff, count, data, data.length);
|
2010-08-31 13:50:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void subElementData(int xoff, int component_number, FieldPacker fp) {
|
|
|
|
if (component_number >= mType.mElement.mElements.length) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
|
2010-08-31 13:50:42 -07:00
|
|
|
}
|
|
|
|
if(xoff < 0) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSIllegalArgumentException("Offset must be >= 0.");
|
2010-08-31 13:50:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
final byte[] data = fp.getData();
|
|
|
|
int eSize = mType.mElement.mElements[component_number].getSizeBytes();
|
|
|
|
|
|
|
|
if (data.length != eSize) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
|
2010-08-31 13:50:42 -07:00
|
|
|
" does not match component size " + eSize + ".");
|
|
|
|
}
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationSubElementData1D(getID(), xoff, component_number, data, data.length);
|
2010-03-26 15:33:42 -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();
|
2010-03-26 15:33:42 -07:00
|
|
|
if(off < 0) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSIllegalArgumentException("Offset must be >= 0.");
|
2010-03-26 15:33:42 -07:00
|
|
|
}
|
|
|
|
if(count < 1) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSIllegalArgumentException("Count must be >= 1.");
|
2010-03-26 15:33:42 -07:00
|
|
|
}
|
|
|
|
if((off + count) > mType.getElementCount()) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSIllegalArgumentException("Overflow, Available count " + mType.getElementCount() +
|
2010-03-26 15:33:42 -07:00
|
|
|
", got " + count + " at offset " + off + ".");
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
|
|
|
if((len) < dataSize) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSIllegalArgumentException("Array too small for allocation type.");
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
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);
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
|
|
|
public void subData1D(int off, int count, short[] d) {
|
|
|
|
int dataSize = mType.mElement.getSizeBytes() * count;
|
|
|
|
data1DChecks(off, count, d.length * 2, dataSize);
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
|
|
|
public void subData1D(int off, int count, byte[] d) {
|
|
|
|
int dataSize = mType.mElement.getSizeBytes() * count;
|
|
|
|
data1DChecks(off, count, d.length, dataSize);
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationSubData1D(getID(), off, count, d, dataSize);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
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);
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationSubData1D(getID(), 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();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationSubData2D(getID(), 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();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationSubData2D(getID(), 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();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationRead(getID(), d);
|
2009-08-10 14:55:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void readData(float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationRead(getID(), d);
|
2009-08-10 14:55:26 -07:00
|
|
|
}
|
|
|
|
|
2010-10-26 13:09:17 -07:00
|
|
|
public synchronized void resize(int dimX) {
|
2010-10-05 13:32:49 -07:00
|
|
|
if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
|
2010-10-05 13:32:49 -07:00
|
|
|
}
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationResize1D(getID(), dimX);
|
2010-11-01 16:08:59 -07:00
|
|
|
mRS.finish(); // Necessary because resize is fifoed and update is async.
|
2010-10-26 13:09:17 -07:00
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
int typeID = mRS.nAllocationGetType(getID());
|
2010-10-26 13:09:17 -07:00
|
|
|
mType = new Type(typeID, mRS);
|
|
|
|
mType.updateFromNative();
|
2010-10-05 13:32:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
public void resize(int dimX, int dimY) {
|
|
|
|
if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
|
2010-10-05 13:32:49 -07:00
|
|
|
}
|
|
|
|
if (mType.getY() == 0) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
|
2010-10-05 13:32:49 -07:00
|
|
|
}
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAllocationResize2D(getID(), dimX, dimY);
|
2010-10-05 13:32:49 -07:00
|
|
|
}
|
|
|
|
*/
|
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) {
|
2010-08-11 14:41:28 -07:00
|
|
|
super(id, rs);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setConstraint(Dimension dim, int value) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAdapter1DSetConstraint(getID(), dim.mID, value);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void data(int[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAdapter1DData(getID(), d);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void data(float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAdapter1DData(getID(), d);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
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();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAdapter1DSubData(getID(), off, count, d);
|
2009-08-03 16:03:08 -07:00
|
|
|
}
|
|
|
|
|
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();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAdapter1DSubData(getID(), off, count, d);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSRuntimeException("Adapter creation failed.");
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAdapter1DBindAllocation(id, getID());
|
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) {
|
2010-08-11 14:41:28 -07:00
|
|
|
super(id, rs);
|
2009-08-03 16:03:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setConstraint(Dimension dim, int value) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAdapter2DSetConstraint(getID(), dim.mID, value);
|
2009-08-03 16:03:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void data(int[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAdapter2DData(getID(), d);
|
2009-08-03 16:03:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void data(float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAdapter2DData(getID(), d);
|
2009-08-03 16:03:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void subData(int xoff, int yoff, int w, int h, int[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAdapter2DSubData(getID(), xoff, yoff, w, h, d);
|
2009-08-03 16:03:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void subData(int xoff, int yoff, int w, int h, float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2010-11-09 17:11:40 -08:00
|
|
|
mRS.nAdapter2DSubData(getID(), xoff, yoff, w, h, d);
|
2009-08-03 16:03:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSRuntimeException("allocation failed.");
|
|
|
|
}
|
|
|
|
mRS.nAdapter2DBindAllocation(id, getID());
|
|
|
|
if(id == 0) {
|
|
|
|
throw new RSRuntimeException("Adapter creation failed.");
|
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;
|
|
|
|
}
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
static public Allocation createTyped(RenderScript rs, Type type) {
|
2009-08-09 17:01:55 -07:00
|
|
|
|
2009-12-07 12:40:12 -08:00
|
|
|
rs.validate();
|
2010-11-09 17:11:40 -08:00
|
|
|
if(type.getID() == 0) {
|
|
|
|
throw new RSInvalidStateException("Bad Type");
|
|
|
|
}
|
|
|
|
int id = rs.nAllocationCreateTyped(type.getID());
|
|
|
|
if(id == 0) {
|
|
|
|
throw new RSRuntimeException("Allocation creation failed.");
|
2009-08-09 17:01:55 -07:00
|
|
|
}
|
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();
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
int id = rs.nAllocationCreateTyped(t.getID());
|
2009-09-04 14:42:41 -07:00
|
|
|
if(id == 0) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSRuntimeException("Allocation creation failed.");
|
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
|
|
|
}
|
|
|
|
|
2010-03-01 15:31:04 -08:00
|
|
|
static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
|
|
|
|
final Bitmap.Config bc = b.getConfig();
|
|
|
|
if (bc == Bitmap.Config.ALPHA_8) {
|
|
|
|
return Element.A_8(rs);
|
|
|
|
}
|
|
|
|
if (bc == Bitmap.Config.ARGB_4444) {
|
|
|
|
return Element.RGBA_4444(rs);
|
|
|
|
}
|
|
|
|
if (bc == Bitmap.Config.ARGB_8888) {
|
|
|
|
return Element.RGBA_8888(rs);
|
|
|
|
}
|
|
|
|
if (bc == Bitmap.Config.RGB_565) {
|
|
|
|
return Element.RGB_565(rs);
|
|
|
|
}
|
2010-11-16 13:46:34 -08:00
|
|
|
throw new RSInvalidStateException("Bad bitmap type: " + bc);
|
2010-03-01 15:31:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
|
|
|
|
Element e = elementFromBitmap(rs, b);
|
|
|
|
Type.Builder tb = new Type.Builder(rs, e);
|
|
|
|
tb.add(Dimension.X, b.getWidth());
|
|
|
|
tb.add(Dimension.Y, b.getHeight());
|
|
|
|
return tb.create();
|
|
|
|
}
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) {
|
2009-07-31 20:40:47 -07:00
|
|
|
|
2009-12-07 12:40:12 -08:00
|
|
|
rs.validate();
|
2010-03-01 15:31:04 -08:00
|
|
|
Type t = typeFromBitmap(rs, b);
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
int id = rs.nAllocationCreateFromBitmap(dstFmt.getID(), genMips, b);
|
2009-12-23 14:35:29 -08:00
|
|
|
if(id == 0) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSRuntimeException("Load failed.");
|
2009-12-23 14:35:29 -08:00
|
|
|
}
|
2010-03-01 15:31:04 -08:00
|
|
|
return new Allocation(id, rs, t);
|
|
|
|
}
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
static public Allocation createBitmapRef(RenderScript rs, Bitmap b) {
|
2010-03-01 15:31:04 -08:00
|
|
|
|
|
|
|
rs.validate();
|
|
|
|
Type t = typeFromBitmap(rs, b);
|
|
|
|
|
|
|
|
int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
|
|
|
|
if(id == 0) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSRuntimeException("Load failed.");
|
2010-03-01 15:31:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Allocation a = new Allocation(id, rs, t);
|
|
|
|
a.mBitmap = b;
|
|
|
|
return a;
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) {
|
2009-07-31 20:40:47 -07:00
|
|
|
|
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();
|
2010-11-09 17:11:40 -08:00
|
|
|
int aId = rs.nAllocationCreateFromAssetStream(dstFmt.getID(), genMips, asset);
|
2009-08-31 14:06:43 -07:00
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
if (aId == 0) {
|
|
|
|
throw new RSRuntimeException("Load failed.");
|
2009-12-23 14:35:29 -08:00
|
|
|
}
|
2010-11-09 17:11:40 -08:00
|
|
|
return new Allocation(aId, rs, null);
|
2009-08-31 14:06:43 -07:00
|
|
|
} finally {
|
|
|
|
if (is != null) {
|
|
|
|
try {
|
|
|
|
is.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2010-11-09 17:11:40 -08:00
|
|
|
static public Allocation createFromString(RenderScript rs, String str) {
|
2010-06-24 17:15:34 -07:00
|
|
|
byte[] allocArray = null;
|
|
|
|
try {
|
|
|
|
allocArray = str.getBytes("UTF-8");
|
|
|
|
Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length);
|
|
|
|
alloc.data(allocArray);
|
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSRuntimeException("Could not convert string to utf-8.");
|
2010-06-24 17:15:34 -07:00
|
|
|
}
|
|
|
|
}
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|