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
|
|
|
|
|
|
|
/**
|
2011-01-04 18:59:12 -08:00
|
|
|
* Memory allocation class for renderscript. An allocation combines a Type with
|
|
|
|
* memory to provide storage for user data and objects.
|
|
|
|
*
|
|
|
|
* Allocations may exist in one or more memory spaces. Currently those are
|
|
|
|
* Script: accessable by RS scripts.
|
|
|
|
* Graphics Texture: accessable as a graphics texture.
|
|
|
|
* Graphics Vertex: accessable as graphical vertex data.
|
|
|
|
* Graphics Constants: Accessable as constants in user shaders
|
|
|
|
*
|
|
|
|
* By default java side updates are always applied to the script accessable
|
|
|
|
* memory. If this is not present they are then applied to the various HW
|
|
|
|
* memory types. A syncAll call is necessary after the script data is update to
|
|
|
|
* keep the other memory spaces in sync.
|
2009-07-31 20:40:47 -07:00
|
|
|
*
|
|
|
|
**/
|
|
|
|
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;
|
2010-12-08 16:14:36 -08:00
|
|
|
int mUsage;
|
|
|
|
|
|
|
|
public static final int USAGE_SCRIPT = 0x0001;
|
|
|
|
public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
|
|
|
|
public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
|
|
|
|
public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
|
|
|
|
|
2009-08-12 17:54:11 -07:00
|
|
|
|
2010-11-18 15:22:43 -08:00
|
|
|
public enum CubemapLayout {
|
|
|
|
VERTICAL_FACE_LIST (0),
|
|
|
|
HORIZONTAL_FACE_LIST (1),
|
|
|
|
VERTICAL_CROSS (2),
|
|
|
|
HORIZONTAL_CROSS (3);
|
|
|
|
|
|
|
|
int mID;
|
|
|
|
CubemapLayout(int id) {
|
|
|
|
mID = id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-29 14:31:29 -08:00
|
|
|
|
2010-12-10 16:03:15 -08:00
|
|
|
public enum MipmapControl {
|
2010-12-08 16:14:36 -08:00
|
|
|
MIPMAP_NONE(0),
|
|
|
|
MIPMAP_FULL(1),
|
|
|
|
MIPMAP_ON_SYNC_TO_TEXTURE(2);
|
|
|
|
|
|
|
|
int mID;
|
2010-12-10 16:03:15 -08:00
|
|
|
MipmapControl(int id) {
|
2010-12-08 16:14:36 -08:00
|
|
|
mID = id;
|
|
|
|
}
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2010-12-08 16:14:36 -08:00
|
|
|
Allocation(int id, RenderScript rs, Type t, int usage) {
|
2010-08-11 14:41:28 -07:00
|
|
|
super(id, rs);
|
2010-12-29 14:31:29 -08:00
|
|
|
if ((usage & ~(USAGE_SCRIPT |
|
|
|
|
USAGE_GRAPHICS_TEXTURE |
|
|
|
|
USAGE_GRAPHICS_VERTEX |
|
|
|
|
USAGE_GRAPHICS_CONSTANTS)) != 0) {
|
2010-12-08 16:14:36 -08:00
|
|
|
throw new RSIllegalArgumentException("Unknown usage specified.");
|
|
|
|
}
|
|
|
|
mType = t;
|
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;
|
|
|
|
}
|
|
|
|
|
2010-12-08 16:14:36 -08:00
|
|
|
public void syncAll(int srcLocation) {
|
|
|
|
switch (srcLocation) {
|
|
|
|
case USAGE_SCRIPT:
|
|
|
|
case USAGE_GRAPHICS_CONSTANTS:
|
|
|
|
case USAGE_GRAPHICS_TEXTURE:
|
|
|
|
case USAGE_GRAPHICS_VERTEX:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new RSIllegalArgumentException("Source must be exactly one usage type.");
|
|
|
|
}
|
|
|
|
mRS.validate();
|
|
|
|
mRS.nAllocationSyncAll(getID(), srcLocation);
|
|
|
|
}
|
|
|
|
|
2010-12-06 15:59:59 -08:00
|
|
|
public void copyFrom(BaseObj[] d) {
|
|
|
|
mRS.validate();
|
|
|
|
if (d.length != mType.getCount()) {
|
|
|
|
throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
|
|
|
|
mType.getCount() + ", array length = " + d.length);
|
|
|
|
}
|
|
|
|
int i[] = new int[d.length];
|
|
|
|
for (int ct=0; ct < d.length; ct++) {
|
|
|
|
i[ct] = d[ct].getID();
|
|
|
|
}
|
2011-01-07 17:00:07 -08:00
|
|
|
copy1DRangeFrom(0, mType.getCount(), i);
|
2010-12-06 15:59:59 -08:00
|
|
|
}
|
|
|
|
|
2010-12-10 16:03:15 -08:00
|
|
|
private void validateBitmap(Bitmap b) {
|
|
|
|
mRS.validate();
|
|
|
|
if(mType.getX() != b.getWidth() ||
|
|
|
|
mType.getY() != b.getHeight()) {
|
|
|
|
throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-06 15:59:59 -08:00
|
|
|
public void copyFrom(int[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2011-01-07 17:00:07 -08:00
|
|
|
copy1DRangeFrom(0, mType.getCount(), d);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
2010-12-06 15:59:59 -08:00
|
|
|
public void copyFrom(short[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2011-01-07 17:00:07 -08:00
|
|
|
copy1DRangeFrom(0, mType.getCount(), d);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
2010-12-06 15:59:59 -08:00
|
|
|
public void copyFrom(byte[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2011-01-07 17:00:07 -08:00
|
|
|
copy1DRangeFrom(0, mType.getCount(), d);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
2010-12-06 15:59:59 -08:00
|
|
|
public void copyFrom(float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2011-01-07 17:00:07 -08:00
|
|
|
copy1DRangeFrom(0, mType.getCount(), d);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
2010-12-06 15:59:59 -08:00
|
|
|
public void copyFrom(Bitmap b) {
|
2010-12-10 16:03:15 -08:00
|
|
|
validateBitmap(b);
|
|
|
|
mRS.nAllocationCopyFromBitmap(getID(), b);
|
|
|
|
}
|
2010-10-11 12:35:15 -07:00
|
|
|
|
2011-01-07 17:00:07 -08:00
|
|
|
/**
|
|
|
|
* @hide
|
|
|
|
*
|
|
|
|
* This is only intended to be used by auto-generate code reflected from the
|
|
|
|
* renderscript script files.
|
|
|
|
*
|
|
|
|
* @param xoff
|
|
|
|
* @param fp
|
|
|
|
*/
|
|
|
|
public void setOneElement(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-12-29 14:31:29 -08:00
|
|
|
mRS.nAllocationData1D(getID(), xoff, 0, count, data, data.length);
|
2010-08-31 13:50:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-07 17:00:07 -08:00
|
|
|
/**
|
|
|
|
* @hide
|
|
|
|
*
|
|
|
|
* This is only intended to be used by auto-generate code reflected from the
|
|
|
|
* renderscript script files.
|
|
|
|
*
|
|
|
|
* @param xoff
|
|
|
|
* @param component_number
|
|
|
|
* @param fp
|
|
|
|
*/
|
|
|
|
public void setOneComponent(int xoff, int component_number, FieldPacker fp) {
|
2010-08-31 13:50:42 -07:00
|
|
|
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-12-29 14:31:29 -08:00
|
|
|
mRS.nAllocationElementData1D(getID(), xoff, 0, 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
|
|
|
}
|
2010-12-06 15:59:59 -08:00
|
|
|
if((off + count) > mType.getCount()) {
|
|
|
|
throw new RSIllegalArgumentException("Overflow, Available count " + mType.getCount() +
|
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
|
|
|
}
|
|
|
|
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copy1DRangeFrom(int off, int count, int[] d) {
|
2009-09-21 19:41:04 -07:00
|
|
|
int dataSize = mType.mElement.getSizeBytes() * count;
|
|
|
|
data1DChecks(off, count, d.length * 4, dataSize);
|
2010-12-29 14:31:29 -08:00
|
|
|
mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copy1DRangeFrom(int off, int count, short[] d) {
|
2009-09-21 19:41:04 -07:00
|
|
|
int dataSize = mType.mElement.getSizeBytes() * count;
|
|
|
|
data1DChecks(off, count, d.length * 2, dataSize);
|
2010-12-29 14:31:29 -08:00
|
|
|
mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copy1DRangeFrom(int off, int count, byte[] d) {
|
2009-09-21 19:41:04 -07:00
|
|
|
int dataSize = mType.mElement.getSizeBytes() * count;
|
|
|
|
data1DChecks(off, count, d.length, dataSize);
|
2010-12-29 14:31:29 -08:00
|
|
|
mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copy1DRangeFrom(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-12-29 14:31:29 -08:00
|
|
|
mRS.nAllocationData1D(getID(), off, 0, count, d, dataSize);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2009-09-21 19:41:04 -07:00
|
|
|
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] d) {
|
|
|
|
mRS.validate();
|
|
|
|
mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] d) {
|
|
|
|
mRS.validate();
|
|
|
|
mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2010-12-29 14:31:29 -08:00
|
|
|
mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 4);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] d) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2010-12-29 14:31:29 -08:00
|
|
|
mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, d, d.length * 4);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, Bitmap b) {
|
|
|
|
mRS.validate();
|
|
|
|
mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void copyTo(Bitmap b) {
|
|
|
|
validateBitmap(b);
|
|
|
|
mRS.nAllocationCopyToBitmap(getID(), b);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void copyTo(byte[] d) {
|
|
|
|
mRS.validate();
|
|
|
|
mRS.nAllocationRead(getID(), d);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void copyTo(short[] d) {
|
|
|
|
mRS.validate();
|
|
|
|
mRS.nAllocationRead(getID(), d);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void copyTo(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
|
|
|
}
|
|
|
|
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copyTo(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-12-06 15:59:59 -08:00
|
|
|
if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.hasFaces() || mType.hasMipmaps()) {
|
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-08-03 16:03:08 -07:00
|
|
|
|
2009-07-31 20:40:47 -07:00
|
|
|
|
|
|
|
// creation
|
|
|
|
|
2010-12-29 14:31:29 -08:00
|
|
|
static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
|
2009-07-31 20:40:47 -07:00
|
|
|
static {
|
|
|
|
mBitmapOptions.inScaled = false;
|
|
|
|
}
|
|
|
|
|
2010-12-13 15:32:35 -08:00
|
|
|
static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mc, int usage) {
|
2009-12-07 12:40:12 -08:00
|
|
|
rs.validate();
|
2010-12-08 16:14:36 -08:00
|
|
|
if (type.getID() == 0) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSInvalidStateException("Bad Type");
|
|
|
|
}
|
2010-12-13 15:32:35 -08:00
|
|
|
int id = rs.nAllocationCreateTyped(type.getID(), mc.mID, usage);
|
2010-12-08 16:14:36 -08:00
|
|
|
if (id == 0) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSRuntimeException("Allocation creation failed.");
|
2009-08-09 17:01:55 -07:00
|
|
|
}
|
2010-12-08 16:14:36 -08:00
|
|
|
return new Allocation(id, rs, type, usage);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2010-12-16 00:33:33 -08:00
|
|
|
static public Allocation createTyped(RenderScript rs, Type type, int usage) {
|
|
|
|
return createTyped(rs, type, MipmapControl.MIPMAP_NONE, usage);
|
|
|
|
}
|
|
|
|
|
2010-12-08 16:14:36 -08:00
|
|
|
static public Allocation createTyped(RenderScript rs, Type type) {
|
2010-12-13 15:32:35 -08:00
|
|
|
return createTyped(rs, type, MipmapControl.MIPMAP_NONE, USAGE_SCRIPT);
|
2010-12-08 16:14:36 -08:00
|
|
|
}
|
2009-08-09 17:01:55 -07:00
|
|
|
|
2010-12-08 16:14:36 -08:00
|
|
|
static public Allocation createSized(RenderScript rs, Element e,
|
|
|
|
int count, int usage) {
|
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);
|
2010-12-06 15:59:59 -08:00
|
|
|
b.setX(count);
|
2009-09-21 19:41:04 -07:00
|
|
|
Type t = b.create();
|
|
|
|
|
2010-12-13 15:32:35 -08:00
|
|
|
int id = rs.nAllocationCreateTyped(t.getID(), MipmapControl.MIPMAP_NONE.mID, usage);
|
2010-12-08 16:14:36 -08: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
|
|
|
}
|
2010-12-08 16:14:36 -08:00
|
|
|
return new Allocation(id, rs, t, usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public Allocation createSized(RenderScript rs, Element e, int count) {
|
2010-12-13 15:32:35 -08:00
|
|
|
return createSized(rs, e, count, USAGE_SCRIPT);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2010-12-29 14:31:29 -08:00
|
|
|
static Element elementFromBitmap(RenderScript rs, Bitmap b) {
|
2010-03-01 15:31:04 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-12-29 14:31:29 -08:00
|
|
|
static Type typeFromBitmap(RenderScript rs, Bitmap b,
|
2010-12-10 16:03:15 -08:00
|
|
|
MipmapControl mip) {
|
2010-03-01 15:31:04 -08:00
|
|
|
Element e = elementFromBitmap(rs, b);
|
|
|
|
Type.Builder tb = new Type.Builder(rs, e);
|
2010-12-06 15:59:59 -08:00
|
|
|
tb.setX(b.getWidth());
|
|
|
|
tb.setY(b.getHeight());
|
2010-12-10 16:03:15 -08:00
|
|
|
tb.setMipmaps(mip == MipmapControl.MIPMAP_FULL);
|
2010-03-01 15:31:04 -08:00
|
|
|
return tb.create();
|
|
|
|
}
|
|
|
|
|
2010-11-18 15:22:43 -08:00
|
|
|
static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
|
2010-12-10 16:03:15 -08:00
|
|
|
MipmapControl mips,
|
2010-12-08 16:14:36 -08:00
|
|
|
int usage) {
|
2009-12-07 12:40:12 -08:00
|
|
|
rs.validate();
|
2010-12-08 16:14:36 -08:00
|
|
|
Type t = typeFromBitmap(rs, b, mips);
|
2010-03-01 15:31:04 -08:00
|
|
|
|
2010-12-08 16:14:36 -08:00
|
|
|
int id = rs.nAllocationCreateFromBitmap(t.getID(), mips.mID, b, usage);
|
|
|
|
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-12-08 16:14:36 -08:00
|
|
|
return new Allocation(id, rs, t, usage);
|
|
|
|
}
|
|
|
|
|
2010-12-15 01:41:00 -08:00
|
|
|
static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
|
|
|
|
return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
|
|
|
|
USAGE_GRAPHICS_TEXTURE);
|
2010-03-01 15:31:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-18 15:22:43 -08:00
|
|
|
static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
|
2010-12-10 16:03:15 -08:00
|
|
|
MipmapControl mips,
|
2010-12-08 16:14:36 -08:00
|
|
|
CubemapLayout layout,
|
|
|
|
int usage) {
|
2010-11-18 15:22:43 -08:00
|
|
|
rs.validate();
|
2010-12-08 16:14:36 -08:00
|
|
|
|
2010-11-18 15:22:43 -08:00
|
|
|
int height = b.getHeight();
|
|
|
|
int width = b.getWidth();
|
|
|
|
|
|
|
|
if (layout != CubemapLayout.VERTICAL_FACE_LIST) {
|
|
|
|
throw new RSIllegalArgumentException("Only vertical face list supported");
|
|
|
|
}
|
|
|
|
if (height % 6 != 0) {
|
|
|
|
throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
|
|
|
|
}
|
|
|
|
if (height / 6 != width) {
|
|
|
|
throw new RSIllegalArgumentException("Only square cobe map faces supported");
|
|
|
|
}
|
|
|
|
boolean isPow2 = (width & (width - 1)) == 0;
|
|
|
|
if (!isPow2) {
|
|
|
|
throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
Element e = elementFromBitmap(rs, b);
|
|
|
|
Type.Builder tb = new Type.Builder(rs, e);
|
2010-12-06 15:59:59 -08:00
|
|
|
tb.setX(width);
|
|
|
|
tb.setY(width);
|
|
|
|
tb.setFaces(true);
|
2010-12-10 16:03:15 -08:00
|
|
|
tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
|
2010-11-18 15:22:43 -08:00
|
|
|
Type t = tb.create();
|
|
|
|
|
2010-12-08 16:14:36 -08:00
|
|
|
int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
|
2010-11-18 15:22:43 -08:00
|
|
|
if(id == 0) {
|
|
|
|
throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
|
|
|
|
}
|
2010-12-08 16:14:36 -08:00
|
|
|
return new Allocation(id, rs, t, usage);
|
|
|
|
}
|
|
|
|
|
|
|
|
static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
|
|
|
|
CubemapLayout layout) {
|
2010-12-15 01:41:00 -08:00
|
|
|
return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
|
|
|
|
layout, USAGE_GRAPHICS_TEXTURE);
|
2010-11-18 15:22:43 -08:00
|
|
|
}
|
|
|
|
|
2010-12-08 16:14:36 -08:00
|
|
|
static public Allocation createFromBitmapResource(RenderScript rs,
|
|
|
|
Resources res,
|
|
|
|
int id,
|
2010-12-10 16:03:15 -08:00
|
|
|
MipmapControl mips,
|
2010-12-08 16:14:36 -08:00
|
|
|
int usage) {
|
2009-07-31 20:40:47 -07:00
|
|
|
|
2009-12-07 12:40:12 -08:00
|
|
|
rs.validate();
|
2010-12-08 16:14:36 -08:00
|
|
|
Bitmap b = BitmapFactory.decodeResource(res, id);
|
|
|
|
Allocation alloc = createFromBitmap(rs, b, mips, usage);
|
|
|
|
b.recycle();
|
|
|
|
return alloc;
|
|
|
|
}
|
2009-08-31 14:06:43 -07:00
|
|
|
|
2010-12-15 01:41:00 -08:00
|
|
|
static public Allocation createFromBitmapResource(RenderScript rs,
|
|
|
|
Resources res,
|
|
|
|
int id) {
|
|
|
|
return createFromBitmapResource(rs, res, id,
|
|
|
|
MipmapControl.MIPMAP_NONE,
|
|
|
|
USAGE_GRAPHICS_TEXTURE);
|
|
|
|
}
|
|
|
|
|
2010-12-08 16:14:36 -08:00
|
|
|
static public Allocation createFromString(RenderScript rs,
|
|
|
|
String str,
|
|
|
|
int usage) {
|
|
|
|
rs.validate();
|
2010-06-24 17:15:34 -07:00
|
|
|
byte[] allocArray = null;
|
|
|
|
try {
|
|
|
|
allocArray = str.getBytes("UTF-8");
|
2010-12-08 16:14:36 -08:00
|
|
|
Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
|
2010-12-06 15:59:59 -08:00
|
|
|
alloc.copyFrom(allocArray);
|
2010-06-24 17:15:34 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|