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;
|
|
|
|
|
2011-01-12 13:28:37 -08:00
|
|
|
/**
|
|
|
|
* The usage of the allocation. These signal to renderscript
|
|
|
|
* where to place the allocation in memory.
|
|
|
|
*
|
|
|
|
* SCRIPT The allocation will be bound to and accessed by
|
|
|
|
* scripts.
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
public static final int USAGE_SCRIPT = 0x0001;
|
2011-01-12 13:28:37 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GRAPHICS_TEXTURE The allcation will be used as a texture
|
|
|
|
* source by one or more graphcics programs.
|
|
|
|
*
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
|
2011-01-12 13:28:37 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GRAPHICS_VERTEX The allocation will be used as a graphics
|
|
|
|
* mesh.
|
|
|
|
*
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
|
2011-01-12 13:28:37 -08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GRAPHICS_CONSTANTS The allocation will be used as the source
|
|
|
|
* of shader constants by one or more programs.
|
|
|
|
*
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
|
|
|
|
|
2009-08-12 17:54:11 -07:00
|
|
|
|
2011-01-12 13:28:37 -08:00
|
|
|
/**
|
|
|
|
* Controls mipmap behavior when using the bitmap creation and
|
|
|
|
* update functions.
|
|
|
|
*/
|
2010-12-10 16:03:15 -08:00
|
|
|
public enum MipmapControl {
|
2011-01-12 13:28:37 -08:00
|
|
|
/**
|
|
|
|
* No mipmaps will be generated and the type generated from the
|
|
|
|
* incoming bitmap will not contain additional LODs.
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
MIPMAP_NONE(0),
|
2011-01-12 13:28:37 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A Full mipmap chain will be created in script memory. The
|
|
|
|
* type of the allocation will contain a full mipmap chain. On
|
|
|
|
* upload to graphics the full chain will be transfered.
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
MIPMAP_FULL(1),
|
2011-01-12 13:28:37 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of the allocation will be the same as MIPMAP_NONE.
|
|
|
|
* It will not contain mipmaps. On upload to graphics the
|
|
|
|
* graphics copy of the allocation data will contain a full
|
|
|
|
* mipmap chain generated from the top level in script memory.
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-01-16 15:04:08 -08:00
|
|
|
private void validateIsInt32() {
|
|
|
|
if ((mType.mElement.mType == Element.DataType.SIGNED_32) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.UNSIGNED_32)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new RSIllegalArgumentException(
|
|
|
|
"32 bit integer source does not match allocation type " + mType.mElement.mType);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void validateIsInt16() {
|
|
|
|
if ((mType.mElement.mType == Element.DataType.SIGNED_16) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.UNSIGNED_16)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new RSIllegalArgumentException(
|
|
|
|
"16 bit integer source does not match allocation type " + mType.mElement.mType);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void validateIsInt8() {
|
|
|
|
if ((mType.mElement.mType == Element.DataType.SIGNED_8) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.UNSIGNED_8)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new RSIllegalArgumentException(
|
|
|
|
"8 bit integer source does not match allocation type " + mType.mElement.mType);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void validateIsFloat32() {
|
|
|
|
if (mType.mElement.mType == Element.DataType.FLOAT_32) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new RSIllegalArgumentException(
|
|
|
|
"32 bit float source does not match allocation type " + mType.mElement.mType);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void validateIsObject() {
|
|
|
|
if ((mType.mElement.mType == Element.DataType.RS_ELEMENT) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.RS_TYPE) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.RS_ALLOCATION) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.RS_SAMPLER) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.RS_SCRIPT) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.RS_MESH) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.RS_PROGRAM_FRAGMENT) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.RS_PROGRAM_VERTEX) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.RS_PROGRAM_RASTER) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.RS_PROGRAM_STORE)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new RSIllegalArgumentException(
|
|
|
|
"Object source does not match allocation type " + mType.mElement.mType);
|
|
|
|
}
|
|
|
|
|
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();
|
2011-01-16 15:04:08 -08:00
|
|
|
validateIsObject();
|
2010-12-06 15:59:59 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-01-12 14:53:25 -08:00
|
|
|
private void validateBitmapFormat(Bitmap b) {
|
2011-01-11 17:42:52 -08:00
|
|
|
Bitmap.Config bc = b.getConfig();
|
|
|
|
switch (bc) {
|
|
|
|
case ALPHA_8:
|
|
|
|
if (mType.getElement().mKind != Element.DataKind.PIXEL_A) {
|
|
|
|
throw new RSIllegalArgumentException("Allocation kind is " +
|
|
|
|
mType.getElement().mKind + ", type " +
|
|
|
|
mType.getElement().mType +
|
|
|
|
" of " + mType.getElement().getSizeBytes() +
|
|
|
|
" bytes, passed bitmap was " + bc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ARGB_8888:
|
|
|
|
if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
|
|
|
|
(mType.getElement().getSizeBytes() != 4)) {
|
|
|
|
throw new RSIllegalArgumentException("Allocation kind is " +
|
|
|
|
mType.getElement().mKind + ", type " +
|
|
|
|
mType.getElement().mType +
|
|
|
|
" of " + mType.getElement().getSizeBytes() +
|
|
|
|
" bytes, passed bitmap was " + bc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RGB_565:
|
|
|
|
if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
|
|
|
|
(mType.getElement().getSizeBytes() != 2)) {
|
|
|
|
throw new RSIllegalArgumentException("Allocation kind is " +
|
|
|
|
mType.getElement().mKind + ", type " +
|
|
|
|
mType.getElement().mType +
|
|
|
|
" of " + mType.getElement().getSizeBytes() +
|
|
|
|
" bytes, passed bitmap was " + bc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ARGB_4444:
|
|
|
|
if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
|
|
|
|
(mType.getElement().getSizeBytes() != 2)) {
|
|
|
|
throw new RSIllegalArgumentException("Allocation kind is " +
|
|
|
|
mType.getElement().mKind + ", type " +
|
|
|
|
mType.getElement().mType +
|
|
|
|
" of " + mType.getElement().getSizeBytes() +
|
|
|
|
" bytes, passed bitmap was " + bc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2010-12-10 16:03:15 -08:00
|
|
|
}
|
|
|
|
|
2011-01-12 14:53:25 -08:00
|
|
|
private void validateBitmapSize(Bitmap b) {
|
|
|
|
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) {
|
2011-01-12 14:53:25 -08:00
|
|
|
mRS.validate();
|
|
|
|
validateBitmapSize(b);
|
|
|
|
validateBitmapFormat(b);
|
2010-12-10 16:03:15 -08:00
|
|
|
mRS.nAllocationCopyFromBitmap(getID(), b);
|
|
|
|
}
|
2010-10-11 12:35:15 -07:00
|
|
|
|
2011-01-07 17:00:07 -08:00
|
|
|
/**
|
|
|
|
* This is only intended to be used by auto-generate code reflected from the
|
|
|
|
* renderscript script files.
|
|
|
|
*
|
|
|
|
* @param xoff
|
|
|
|
* @param fp
|
|
|
|
*/
|
2011-01-16 15:05:41 -08:00
|
|
|
public void setFromFieldPacker(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
|
|
|
/**
|
|
|
|
* This is only intended to be used by auto-generate code reflected from the
|
|
|
|
* renderscript script files.
|
|
|
|
*
|
|
|
|
* @param xoff
|
|
|
|
* @param component_number
|
|
|
|
* @param fp
|
|
|
|
*/
|
2011-01-16 15:05:41 -08:00
|
|
|
public void setFromFieldPacker(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-12 13:28:37 -08:00
|
|
|
/**
|
|
|
|
* Generate a mipmap chain. Requires the type of the allocation
|
|
|
|
* include mipmaps.
|
|
|
|
*
|
|
|
|
* This function will generate a complete set of mipmaps from
|
|
|
|
* the top level lod and place them into the script memoryspace.
|
|
|
|
*
|
|
|
|
* If the allocation is also using other memory spaces a
|
|
|
|
* followup sync will be required.
|
|
|
|
*/
|
|
|
|
public void generateMipmaps() {
|
|
|
|
mRS.nAllocationGenerateMipmaps(getID());
|
|
|
|
}
|
|
|
|
|
2011-01-16 15:04:08 -08:00
|
|
|
void copy1DRangeFromUnchecked(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-16 15:04:08 -08:00
|
|
|
void copy1DRangeFromUnchecked(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-16 15:04:08 -08:00
|
|
|
void copy1DRangeFromUnchecked(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-16 15:04:08 -08:00
|
|
|
void copy1DRangeFromUnchecked(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
|
|
|
}
|
|
|
|
|
2011-01-16 15:04:08 -08:00
|
|
|
public void copy1DRangeFrom(int off, int count, int[] d) {
|
|
|
|
validateIsInt32();
|
|
|
|
copy1DRangeFromUnchecked(off, count, d);
|
|
|
|
}
|
|
|
|
public void copy1DRangeFrom(int off, int count, short[] d) {
|
|
|
|
validateIsInt16();
|
|
|
|
copy1DRangeFromUnchecked(off, count, d);
|
|
|
|
}
|
|
|
|
public void copy1DRangeFrom(int off, int count, byte[] d) {
|
|
|
|
validateIsInt8();
|
|
|
|
copy1DRangeFromUnchecked(off, count, d);
|
|
|
|
}
|
|
|
|
public void copy1DRangeFrom(int off, int count, float[] d) {
|
|
|
|
validateIsFloat32();
|
|
|
|
copy1DRangeFromUnchecked(off, count, d);
|
|
|
|
}
|
|
|
|
|
2011-01-12 14:53:25 -08:00
|
|
|
private void validate2DRange(int xoff, int yoff, int w, int h) {
|
|
|
|
if (xoff < 0 || yoff < 0) {
|
|
|
|
throw new RSIllegalArgumentException("Offset cannot be negative.");
|
|
|
|
}
|
|
|
|
if (h < 0 || w < 0) {
|
|
|
|
throw new RSIllegalArgumentException("Height or width cannot be negative.");
|
|
|
|
}
|
|
|
|
if ((xoff + w) > mType.mDimX ||
|
|
|
|
(yoff + h) > mType.mDimY) {
|
|
|
|
throw new RSIllegalArgumentException("Updated region larger than allocation.");
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 19:41:04 -07:00
|
|
|
|
2011-01-12 13:28:37 -08:00
|
|
|
/**
|
|
|
|
* Copy a rectanglular region from the array into the
|
|
|
|
* allocation. The incoming array is assumed to be tightly
|
|
|
|
* packed.
|
|
|
|
*
|
|
|
|
* @param xoff X offset of the region to update
|
|
|
|
* @param yoff Y offset of the region to update
|
|
|
|
* @param w Width of the incoming region to update
|
|
|
|
* @param h Height of the incoming region to update
|
|
|
|
* @param data to be placed into the allocation
|
|
|
|
*/
|
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
|
2011-01-07 17:00:07 -08:00
|
|
|
mRS.validate();
|
2011-01-12 14:53:25 -08:00
|
|
|
validate2DRange(xoff, yoff, w, h);
|
2011-01-12 13:28:37 -08:00
|
|
|
mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length);
|
2011-01-07 17:00:07 -08:00
|
|
|
}
|
|
|
|
|
2011-01-12 13:28:37 -08:00
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
|
2011-01-07 17:00:07 -08:00
|
|
|
mRS.validate();
|
2011-01-12 14:53:25 -08:00
|
|
|
validate2DRange(xoff, yoff, w, h);
|
2011-01-12 13:28:37 -08:00
|
|
|
mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 2);
|
2011-01-07 17:00:07 -08:00
|
|
|
}
|
|
|
|
|
2011-01-12 13:28:37 -08:00
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2011-01-12 14:53:25 -08:00
|
|
|
validate2DRange(xoff, yoff, w, h);
|
2011-01-12 13:28:37 -08:00
|
|
|
mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2011-01-12 13:28:37 -08:00
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
|
2009-12-07 12:40:12 -08:00
|
|
|
mRS.validate();
|
2011-01-12 14:53:25 -08:00
|
|
|
validate2DRange(xoff, yoff, w, h);
|
2011-01-12 13:28:37 -08:00
|
|
|
mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, w, h, data, data.length * 4);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2011-01-12 13:28:37 -08:00
|
|
|
/**
|
|
|
|
* Copy a bitmap into an allocation. The height and width of
|
|
|
|
* the update will use the height and width of the incoming
|
|
|
|
* bitmap.
|
|
|
|
*
|
|
|
|
* @param xoff X offset of the region to update
|
|
|
|
* @param yoff Y offset of the region to update
|
|
|
|
* @param data the bitmap to be copied
|
|
|
|
*/
|
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
|
2011-01-07 17:00:07 -08:00
|
|
|
mRS.validate();
|
2011-01-12 14:53:25 -08:00
|
|
|
validateBitmapFormat(data);
|
|
|
|
validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
|
2011-01-12 13:28:37 -08:00
|
|
|
mRS.nAllocationData2D(getID(), xoff, yoff, 0, 0, data);
|
2011-01-07 17:00:07 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void copyTo(Bitmap b) {
|
2011-01-12 14:53:25 -08:00
|
|
|
mRS.validate();
|
|
|
|
validateBitmapFormat(b);
|
|
|
|
validateBitmapSize(b);
|
2011-01-07 17:00:07 -08:00
|
|
|
mRS.nAllocationCopyToBitmap(getID(), b);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void copyTo(byte[] d) {
|
2011-01-16 15:04:08 -08:00
|
|
|
validateIsInt8();
|
2011-01-07 17:00:07 -08:00
|
|
|
mRS.validate();
|
|
|
|
mRS.nAllocationRead(getID(), d);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void copyTo(short[] d) {
|
2011-01-16 15:04:08 -08:00
|
|
|
validateIsInt16();
|
2011-01-07 17:00:07 -08:00
|
|
|
mRS.validate();
|
|
|
|
mRS.nAllocationRead(getID(), d);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void copyTo(int[] d) {
|
2011-01-16 15:04:08 -08:00
|
|
|
validateIsInt32();
|
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) {
|
2011-01-16 15:04:08 -08:00
|
|
|
validateIsFloat32();
|
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-12 13:28:37 -08:00
|
|
|
/**
|
|
|
|
* Resize a 1D allocation. The contents of the allocation are
|
|
|
|
* preserved. If new elements are allocated objects are created
|
|
|
|
* with null contents and the new region is otherwise undefined.
|
|
|
|
*
|
|
|
|
* If the new region is smaller the references of any objects
|
|
|
|
* outside the new region will be released.
|
|
|
|
*
|
|
|
|
* A new type will be created with the new dimension.
|
|
|
|
*
|
|
|
|
* @param dimX The new size of the allocation.
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param type renderscript type describing data layout
|
|
|
|
* @param mips specifies desired mipmap behaviour for the
|
|
|
|
* allocation
|
|
|
|
* @param usage bit field specifying how the allocation is
|
|
|
|
* utilized
|
|
|
|
*/
|
|
|
|
static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, 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");
|
|
|
|
}
|
2011-01-12 17:32:36 -08:00
|
|
|
int id = rs.nAllocationCreateTyped(type.getID(), mips.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
|
|
|
}
|
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
* Creates a renderscript allocation with the size specified by
|
|
|
|
* the type and no mipmaps generated by default
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param type renderscript type describing data layout
|
|
|
|
* @param usage bit field specifying how the allocation is
|
|
|
|
* utilized
|
|
|
|
*
|
|
|
|
* @return allocation
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
* Creates a renderscript allocation for use by the script with
|
|
|
|
* the size specified by the type and no mipmaps generated by
|
|
|
|
* default
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param type renderscript type describing data layout
|
|
|
|
*
|
|
|
|
* @return allocation
|
|
|
|
*/
|
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
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
* Creates a renderscript allocation with a specified number of
|
|
|
|
* given elements
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param e describes what each element of an allocation is
|
|
|
|
* @param count specifies the number of element in the allocation
|
|
|
|
* @param usage bit field specifying how the allocation is
|
|
|
|
* utilized
|
|
|
|
*
|
|
|
|
* @return allocation
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
* Creates a renderscript allocation with a specified number of
|
|
|
|
* given elements
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param e describes what each element of an allocation is
|
|
|
|
* @param count specifies the number of element in the allocation
|
|
|
|
*
|
|
|
|
* @return allocation
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
* Creates a renderscript allocation from a bitmap
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param b bitmap source for the allocation data
|
|
|
|
* @param mips specifies desired mipmap behaviour for the
|
|
|
|
* allocation
|
|
|
|
* @param usage bit field specifying how the allocation is
|
|
|
|
* utilized
|
|
|
|
*
|
|
|
|
* @return renderscript allocation containing bitmap data
|
|
|
|
*
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
* Creates a non-mipmapped renderscript allocation to use as a
|
|
|
|
* graphics texture
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param b bitmap source for the allocation data
|
|
|
|
*
|
|
|
|
* @return renderscript allocation containing bitmap data
|
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2011-01-10 15:57:57 -08:00
|
|
|
/**
|
2011-01-12 17:32:36 -08:00
|
|
|
* Creates a cubemap allocation from a bitmap containing the
|
|
|
|
* horizontal list of cube faces. Each individual face must be
|
|
|
|
* the same size and power of 2
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param b bitmap with cubemap faces layed out in the following
|
|
|
|
* format: right, left, top, bottom, front, back
|
|
|
|
* @param mips specifies desired mipmap behaviour for the cubemap
|
|
|
|
* @param usage bit field specifying how the cubemap is utilized
|
|
|
|
*
|
|
|
|
* @return allocation containing cubemap data
|
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
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();
|
|
|
|
|
2011-01-10 15:57:57 -08:00
|
|
|
if (width % 6 != 0) {
|
2010-11-18 15:22:43 -08:00
|
|
|
throw new RSIllegalArgumentException("Cubemap height must be multiple of 6");
|
|
|
|
}
|
2011-01-10 15:57:57 -08:00
|
|
|
if (width / 6 != height) {
|
2011-01-11 14:47:44 -08:00
|
|
|
throw new RSIllegalArgumentException("Only square cube map faces supported");
|
2010-11-18 15:22:43 -08:00
|
|
|
}
|
2011-01-10 15:57:57 -08:00
|
|
|
boolean isPow2 = (height & (height - 1)) == 0;
|
2010-11-18 15:22:43 -08:00
|
|
|
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);
|
2011-01-10 15:57:57 -08:00
|
|
|
tb.setX(height);
|
|
|
|
tb.setY(height);
|
2010-12-06 15:59:59 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
* Creates a non-mipmapped cubemap allocation for use as a
|
|
|
|
* graphics texture from a bitmap containing the horizontal list
|
|
|
|
* of cube faces. Each individual face must be the same size and
|
|
|
|
* power of 2
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param b bitmap with cubemap faces layed out in the following
|
|
|
|
* format: right, left, top, bottom, front, back
|
|
|
|
*
|
|
|
|
* @return allocation containing cubemap data
|
|
|
|
*
|
|
|
|
*/
|
2011-01-11 14:47:44 -08:00
|
|
|
static public Allocation createCubemapFromBitmap(RenderScript rs,
|
|
|
|
Bitmap b) {
|
2010-12-15 01:41:00 -08:00
|
|
|
return createCubemapFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
|
2011-01-10 15:57:57 -08:00
|
|
|
USAGE_GRAPHICS_TEXTURE);
|
2010-11-18 15:22:43 -08:00
|
|
|
}
|
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
* Creates a cubemap allocation from 6 bitmaps containing
|
|
|
|
* the cube faces. All the faces must be the same size and
|
|
|
|
* power of 2
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param xpos cubemap face in the positive x direction
|
|
|
|
* @param xneg cubemap face in the negative x direction
|
|
|
|
* @param ypos cubemap face in the positive y direction
|
|
|
|
* @param yneg cubemap face in the negative y direction
|
|
|
|
* @param zpos cubemap face in the positive z direction
|
|
|
|
* @param zneg cubemap face in the negative z direction
|
|
|
|
* @param mips specifies desired mipmap behaviour for the cubemap
|
|
|
|
* @param usage bit field specifying how the cubemap is utilized
|
|
|
|
*
|
|
|
|
* @return allocation containing cubemap data
|
|
|
|
*
|
|
|
|
*/
|
2011-01-11 14:47:44 -08:00
|
|
|
static public Allocation createCubemapFromCubeFaces(RenderScript rs,
|
|
|
|
Bitmap xpos,
|
|
|
|
Bitmap xneg,
|
|
|
|
Bitmap ypos,
|
|
|
|
Bitmap yneg,
|
|
|
|
Bitmap zpos,
|
|
|
|
Bitmap zneg,
|
|
|
|
MipmapControl mips,
|
|
|
|
int usage) {
|
|
|
|
int height = xpos.getHeight();
|
|
|
|
if (xpos.getWidth() != height ||
|
|
|
|
xneg.getWidth() != height || xneg.getHeight() != height ||
|
|
|
|
ypos.getWidth() != height || ypos.getHeight() != height ||
|
|
|
|
yneg.getWidth() != height || yneg.getHeight() != height ||
|
|
|
|
zpos.getWidth() != height || zpos.getHeight() != height ||
|
|
|
|
zneg.getWidth() != height || zneg.getHeight() != height) {
|
|
|
|
throw new RSIllegalArgumentException("Only square cube map faces supported");
|
|
|
|
}
|
|
|
|
boolean isPow2 = (height & (height - 1)) == 0;
|
|
|
|
if (!isPow2) {
|
|
|
|
throw new RSIllegalArgumentException("Only power of 2 cube faces supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
Element e = elementFromBitmap(rs, xpos);
|
|
|
|
Type.Builder tb = new Type.Builder(rs, e);
|
|
|
|
tb.setX(height);
|
|
|
|
tb.setY(height);
|
|
|
|
tb.setFaces(true);
|
|
|
|
tb.setMipmaps(mips == MipmapControl.MIPMAP_FULL);
|
|
|
|
Type t = tb.create();
|
|
|
|
Allocation cubemap = Allocation.createTyped(rs, t, mips, usage);
|
|
|
|
|
|
|
|
AllocationAdapter adapter = AllocationAdapter.create2D(rs, cubemap);
|
|
|
|
adapter.setFace(Type.CubemapFace.POSITVE_X);
|
|
|
|
adapter.copyFrom(xpos);
|
|
|
|
adapter.setFace(Type.CubemapFace.NEGATIVE_X);
|
|
|
|
adapter.copyFrom(xneg);
|
|
|
|
adapter.setFace(Type.CubemapFace.POSITVE_Y);
|
|
|
|
adapter.copyFrom(ypos);
|
|
|
|
adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
|
|
|
|
adapter.copyFrom(yneg);
|
|
|
|
adapter.setFace(Type.CubemapFace.POSITVE_Z);
|
|
|
|
adapter.copyFrom(zpos);
|
|
|
|
adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
|
|
|
|
adapter.copyFrom(zneg);
|
|
|
|
|
|
|
|
return cubemap;
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
* Creates a non-mipmapped cubemap allocation for use as a
|
|
|
|
* graphics texture from 6 bitmaps containing
|
|
|
|
* the cube faces. All the faces must be the same size and
|
|
|
|
* power of 2
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param xpos cubemap face in the positive x direction
|
|
|
|
* @param xneg cubemap face in the negative x direction
|
|
|
|
* @param ypos cubemap face in the positive y direction
|
|
|
|
* @param yneg cubemap face in the negative y direction
|
|
|
|
* @param zpos cubemap face in the positive z direction
|
|
|
|
* @param zneg cubemap face in the negative z direction
|
|
|
|
*
|
|
|
|
* @return allocation containing cubemap data
|
|
|
|
*
|
|
|
|
*/
|
2011-01-11 14:47:44 -08:00
|
|
|
static public Allocation createCubemapFromCubeFaces(RenderScript rs,
|
|
|
|
Bitmap xpos,
|
|
|
|
Bitmap xneg,
|
|
|
|
Bitmap ypos,
|
|
|
|
Bitmap yneg,
|
|
|
|
Bitmap zpos,
|
|
|
|
Bitmap zneg) {
|
|
|
|
return createCubemapFromCubeFaces(rs, xpos, xneg, ypos, yneg,
|
|
|
|
zpos, zneg, MipmapControl.MIPMAP_NONE,
|
|
|
|
USAGE_GRAPHICS_TEXTURE);
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
* Creates a renderscript allocation from the bitmap referenced
|
|
|
|
* by resource id
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param res application resources
|
|
|
|
* @param id resource id to load the data from
|
|
|
|
* @param mips specifies desired mipmap behaviour for the
|
|
|
|
* allocation
|
|
|
|
* @param usage bit field specifying how the allocation is
|
|
|
|
* utilized
|
|
|
|
*
|
|
|
|
* @return renderscript allocation containing resource data
|
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
* Creates a non-mipmapped renderscript allocation to use as a
|
|
|
|
* graphics texture from the bitmap referenced by resource id
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param res application resources
|
|
|
|
* @param id resource id to load the data from
|
|
|
|
*
|
|
|
|
* @return renderscript allocation containing resource data
|
|
|
|
*
|
|
|
|
*/
|
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);
|
|
|
|
}
|
|
|
|
|
2011-01-12 17:32:36 -08:00
|
|
|
/**
|
|
|
|
* Creates a renderscript allocation containing string data
|
|
|
|
* encoded in UTF-8 format
|
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param str string to create the allocation from
|
|
|
|
* @param usage bit field specifying how the allocaiton is
|
|
|
|
* utilized
|
|
|
|
*
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|