2009-07-31 20:40:47 -07:00
|
|
|
/*
|
2012-02-13 18:25:54 -08:00
|
|
|
* Copyright (C) 2008-2012 The Android Open Source Project
|
2009-07-31 20:40:47 -07:00
|
|
|
*
|
|
|
|
* 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;
|
2013-04-11 18:07:52 -07:00
|
|
|
import java.util.HashMap;
|
2009-07-31 20:40:47 -07:00
|
|
|
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;
|
2012-03-28 15:30:07 -07:00
|
|
|
import android.view.Surface;
|
2009-07-31 20:40:47 -07:00
|
|
|
import android.util.Log;
|
2009-08-31 14:06:43 -07:00
|
|
|
import android.util.TypedValue;
|
2013-02-28 11:45:22 -08:00
|
|
|
import android.graphics.Canvas;
|
2013-05-23 16:59:23 -07:00
|
|
|
import android.os.Trace;
|
2009-07-31 20:40:47 -07:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* <p> This class provides the primary method through which data is passed to
|
|
|
|
* and from RenderScript kernels. An Allocation provides the backing store for
|
|
|
|
* a given {@link android.renderscript.Type}. </p>
|
2011-01-04 18:59:12 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* <p>An Allocation also contains a set of usage flags that denote how the
|
|
|
|
* Allocation could be used. For example, an Allocation may have usage flags
|
|
|
|
* specifying that it can be used from a script as well as input to a {@link
|
|
|
|
* android.renderscript.Sampler}. A developer must synchronize across these
|
|
|
|
* different usages using {@link android.renderscript.Allocation#syncAll} in
|
|
|
|
* order to ensure that different users of the Allocation have a consistent view
|
|
|
|
* of memory. For example, in the case where an Allocation is used as the output
|
|
|
|
* of one kernel and as Sampler input in a later kernel, a developer must call
|
|
|
|
* {@link #syncAll syncAll(Allocation.USAGE_SCRIPT)} prior to launching the
|
|
|
|
* second kernel to ensure correctness.
|
2011-01-04 18:59:12 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* <p>An Allocation can be populated with the {@link #copyFrom} routines. For
|
|
|
|
* more complex Element types, the {@link #copyFromUnchecked} methods can be
|
|
|
|
* used to copy from byte arrays or similar constructs.</p>
|
2009-07-31 20:40:47 -07:00
|
|
|
*
|
2011-12-20 10:38:34 -08:00
|
|
|
* <div class="special reference">
|
|
|
|
* <h3>Developer Guides</h3>
|
2013-04-09 11:01:01 -07:00
|
|
|
* <p>For more information about creating an application that uses RenderScript, read the
|
|
|
|
* <a href="{@docRoot}guide/topics/renderscript/index.html">RenderScript</a> developer guide.</p>
|
2011-12-20 10:38:34 -08:00
|
|
|
* </div>
|
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-07-07 15:24:42 -07:00
|
|
|
Allocation mAdaptedAllocation;
|
2013-08-22 14:55:26 -07:00
|
|
|
int mSize;
|
2011-07-07 15:24:42 -07:00
|
|
|
|
|
|
|
boolean mConstrainedLOD;
|
|
|
|
boolean mConstrainedFace;
|
|
|
|
boolean mConstrainedY;
|
|
|
|
boolean mConstrainedZ;
|
2012-01-13 14:01:20 -08:00
|
|
|
boolean mReadAllowed = true;
|
|
|
|
boolean mWriteAllowed = true;
|
2011-07-07 15:24:42 -07:00
|
|
|
int mSelectedY;
|
|
|
|
int mSelectedZ;
|
|
|
|
int mSelectedLOD;
|
|
|
|
Type.CubemapFace mSelectedFace = Type.CubemapFace.POSITIVE_X;
|
|
|
|
|
|
|
|
int mCurrentDimX;
|
|
|
|
int mCurrentDimY;
|
|
|
|
int mCurrentDimZ;
|
|
|
|
int mCurrentCount;
|
2013-11-19 12:45:54 -08:00
|
|
|
static HashMap<Long, Allocation> mAllocationMap =
|
|
|
|
new HashMap<Long, Allocation>();
|
2013-08-29 13:30:59 -07:00
|
|
|
OnBufferAvailableListener mBufferNotifier;
|
2011-07-07 15:24:42 -07:00
|
|
|
|
2013-11-25 18:28:33 -08:00
|
|
|
private Element.DataType validateObjectIsPrimitiveArray(Object d, boolean checkType) {
|
|
|
|
final Class c = d.getClass();
|
|
|
|
if (!c.isArray()) {
|
|
|
|
throw new RSIllegalArgumentException("Object passed is not an array of primitives.");
|
|
|
|
}
|
|
|
|
final Class cmp = c.getComponentType();
|
|
|
|
if (!cmp.isPrimitive()) {
|
|
|
|
throw new RSIllegalArgumentException("Object passed is not an Array of primitives.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmp == Long.TYPE) {
|
|
|
|
if (checkType) {
|
|
|
|
validateIsInt64();
|
|
|
|
return mType.mElement.mType;
|
|
|
|
}
|
|
|
|
return Element.DataType.SIGNED_64;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmp == Integer.TYPE) {
|
|
|
|
if (checkType) {
|
|
|
|
validateIsInt32();
|
|
|
|
return mType.mElement.mType;
|
|
|
|
}
|
|
|
|
return Element.DataType.SIGNED_32;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmp == Short.TYPE) {
|
|
|
|
if (checkType) {
|
|
|
|
validateIsInt16();
|
|
|
|
return mType.mElement.mType;
|
|
|
|
}
|
|
|
|
return Element.DataType.SIGNED_16;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmp == Byte.TYPE) {
|
|
|
|
if (checkType) {
|
|
|
|
validateIsInt8();
|
|
|
|
return mType.mElement.mType;
|
|
|
|
}
|
|
|
|
return Element.DataType.SIGNED_8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmp == Float.TYPE) {
|
|
|
|
if (checkType) {
|
|
|
|
validateIsFloat32();
|
|
|
|
}
|
|
|
|
return Element.DataType.FLOAT_32;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmp == Double.TYPE) {
|
|
|
|
if (checkType) {
|
|
|
|
validateIsFloat64();
|
|
|
|
}
|
|
|
|
return Element.DataType.FLOAT_64;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* The usage of the Allocation. These signal to RenderScript where to place
|
|
|
|
* the Allocation in memory.
|
2011-01-12 13:28:37 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Allocation will be bound to and accessed by scripts.
|
2011-01-12 13:28:37 -08:00
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
public static final int USAGE_SCRIPT = 0x0001;
|
2011-01-12 13:28:37 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* The Allocation will be used as a texture source by one or more graphics
|
|
|
|
* programs.
|
2011-01-12 13:28:37 -08:00
|
|
|
*
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
|
2011-01-12 13:28:37 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* The Allocation will be used as a graphics mesh.
|
|
|
|
*
|
|
|
|
* This was deprecated in API level 16.
|
2011-01-12 13:28:37 -08:00
|
|
|
*
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
|
2011-01-12 13:28:37 -08:00
|
|
|
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* The Allocation will be used as the source of shader constants by one or
|
|
|
|
* more programs.
|
|
|
|
*
|
|
|
|
* This was deprecated in API level 16.
|
2011-01-12 13:28:37 -08:00
|
|
|
*
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* The Allocation will be used as a target for offscreen rendering
|
|
|
|
*
|
|
|
|
* This was deprecated in API level 16.
|
2011-04-01 14:19:01 -07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static final int USAGE_GRAPHICS_RENDER_TARGET = 0x0010;
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-09-24 15:18:52 -07:00
|
|
|
* The Allocation will be used as a {@link android.view.Surface}
|
|
|
|
* consumer. This usage will cause the Allocation to be created
|
|
|
|
* as read-only.
|
2012-01-13 14:01:20 -08:00
|
|
|
*
|
|
|
|
*/
|
2012-03-23 11:47:26 -07:00
|
|
|
public static final int USAGE_IO_INPUT = 0x0020;
|
2012-02-13 18:25:54 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-09-24 15:18:52 -07:00
|
|
|
* The Allocation will be used as a {@link android.view.Surface}
|
2013-04-09 11:01:01 -07:00
|
|
|
* producer. The dimensions and format of the {@link
|
2013-09-24 15:18:52 -07:00
|
|
|
* android.view.Surface} will be forced to those of the
|
2013-04-09 11:01:01 -07:00
|
|
|
* Allocation.
|
2012-01-13 14:01:20 -08:00
|
|
|
*
|
|
|
|
*/
|
2012-03-23 11:47:26 -07:00
|
|
|
public static final int USAGE_IO_OUTPUT = 0x0040;
|
2009-08-12 17:54:11 -07:00
|
|
|
|
2012-12-17 16:35:06 -08:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* The Allocation's backing store will be inherited from another object
|
|
|
|
* (usually a {@link android.graphics.Bitmap}); copying to or from the
|
|
|
|
* original source Bitmap will cause a synchronization rather than a full
|
|
|
|
* copy. {@link #syncAll} may also be used to synchronize the Allocation
|
|
|
|
* and the source Bitmap.
|
2012-12-17 16:35:06 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* <p>This is set by default for allocations created with {@link
|
|
|
|
* #createFromBitmap} in API version 18 and higher.</p>
|
2012-12-17 16:35:06 -08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static final int USAGE_SHARED = 0x0080;
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Controls mipmap behavior when using the bitmap creation and update
|
|
|
|
* functions.
|
2011-01-12 13:28:37 -08:00
|
|
|
*/
|
2010-12-10 16:03:15 -08:00
|
|
|
public enum MipmapControl {
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* No mipmaps will be generated and the type generated from the incoming
|
|
|
|
* bitmap will not contain additional LODs.
|
2011-01-12 13:28:37 -08:00
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
MIPMAP_NONE(0),
|
2011-01-12 13:28:37 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* A full mipmap chain will be created in script memory. The Type of
|
|
|
|
* the Allocation will contain a full mipmap chain. On upload, the full
|
|
|
|
* chain will be transferred.
|
2011-01-12 13:28:37 -08:00
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
MIPMAP_FULL(1),
|
2011-01-12 13:28:37 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* The Type of the Allocation will be the same as MIPMAP_NONE. It will
|
|
|
|
* not contain mipmaps. On upload, the allocation data will contain a
|
|
|
|
* full mipmap chain generated from the top level in script memory.
|
2011-01-12 13:28:37 -08:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2011-07-08 13:52:30 -07:00
|
|
|
|
2013-11-19 12:45:54 -08:00
|
|
|
private long getIDSafe() {
|
2011-07-08 13:52:30 -07:00
|
|
|
if (mAdaptedAllocation != null) {
|
2012-04-03 15:36:36 -07:00
|
|
|
return mAdaptedAllocation.getID(mRS);
|
2011-07-08 13:52:30 -07:00
|
|
|
}
|
2012-04-03 15:36:36 -07:00
|
|
|
return getID(mRS);
|
2011-07-08 13:52:30 -07:00
|
|
|
}
|
|
|
|
|
2012-03-23 13:51:56 -07:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Get the {@link android.renderscript.Element} of the {@link
|
|
|
|
* android.renderscript.Type} of the Allocation.
|
2012-03-23 13:51:56 -07:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @return Element
|
2012-03-23 13:51:56 -07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public Element getElement() {
|
|
|
|
return mType.getElement();
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2012-03-23 13:51:56 -07:00
|
|
|
* Get the usage flags of the Allocation.
|
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @return usage this Allocation's set of the USAGE_* flags OR'd together
|
2012-03-23 13:51:56 -07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public int getUsage() {
|
|
|
|
return mUsage;
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2012-03-23 15:48:37 -07:00
|
|
|
* Get the size of the Allocation in bytes.
|
|
|
|
*
|
2012-04-11 14:04:23 -07:00
|
|
|
* @return size of the Allocation in bytes.
|
2012-03-23 15:48:37 -07:00
|
|
|
*
|
|
|
|
*/
|
2012-04-11 14:04:23 -07:00
|
|
|
public int getBytesSize() {
|
2013-12-17 17:15:25 -08:00
|
|
|
if (mType.mDimYuv != 0) {
|
|
|
|
return (int)Math.ceil(mType.getCount() * mType.getElement().getBytesSize() * 1.5);
|
|
|
|
}
|
2012-04-11 14:04:23 -07:00
|
|
|
return mType.getCount() * mType.getElement().getBytesSize();
|
2012-03-23 15:48:37 -07:00
|
|
|
}
|
|
|
|
|
2011-07-07 16:05:18 -07:00
|
|
|
private void updateCacheInfo(Type t) {
|
|
|
|
mCurrentDimX = t.getX();
|
|
|
|
mCurrentDimY = t.getY();
|
|
|
|
mCurrentDimZ = t.getZ();
|
|
|
|
mCurrentCount = mCurrentDimX;
|
|
|
|
if (mCurrentDimY > 1) {
|
|
|
|
mCurrentCount *= mCurrentDimY;
|
|
|
|
}
|
|
|
|
if (mCurrentDimZ > 1) {
|
|
|
|
mCurrentCount *= mCurrentDimZ;
|
|
|
|
}
|
|
|
|
}
|
2011-07-07 15:24:42 -07:00
|
|
|
|
2012-12-04 17:59:29 -08:00
|
|
|
private void setBitmap(Bitmap b) {
|
|
|
|
mBitmap = b;
|
|
|
|
}
|
|
|
|
|
2013-11-19 12:45:54 -08:00
|
|
|
Allocation(long 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 |
|
2011-04-01 14:19:01 -07:00
|
|
|
USAGE_GRAPHICS_CONSTANTS |
|
2012-01-13 14:01:20 -08:00
|
|
|
USAGE_GRAPHICS_RENDER_TARGET |
|
|
|
|
USAGE_IO_INPUT |
|
2012-12-17 16:35:06 -08:00
|
|
|
USAGE_IO_OUTPUT |
|
|
|
|
USAGE_SHARED)) != 0) {
|
2010-12-08 16:14:36 -08:00
|
|
|
throw new RSIllegalArgumentException("Unknown usage specified.");
|
|
|
|
}
|
2012-01-13 14:01:20 -08:00
|
|
|
|
2012-03-23 11:47:26 -07:00
|
|
|
if ((usage & USAGE_IO_INPUT) != 0) {
|
2012-01-13 14:01:20 -08:00
|
|
|
mWriteAllowed = false;
|
|
|
|
|
2012-03-23 11:47:26 -07:00
|
|
|
if ((usage & ~(USAGE_IO_INPUT |
|
2012-01-13 14:01:20 -08:00
|
|
|
USAGE_GRAPHICS_TEXTURE |
|
|
|
|
USAGE_SCRIPT)) != 0) {
|
|
|
|
throw new RSIllegalArgumentException("Invalid usage combination.");
|
|
|
|
}
|
|
|
|
}
|
2013-04-13 19:48:36 -07:00
|
|
|
|
2010-12-08 16:14:36 -08:00
|
|
|
mType = t;
|
2012-01-13 14:01:20 -08:00
|
|
|
mUsage = usage;
|
2011-07-07 15:24:42 -07:00
|
|
|
|
2011-07-07 16:05:18 -07:00
|
|
|
if (t != null) {
|
2013-09-09 17:56:07 -07:00
|
|
|
// TODO: A3D doesn't have Type info during creation, so we can't
|
|
|
|
// calculate the size ahead of time. We can possibly add a method
|
|
|
|
// to update the size in the future if it seems reasonable.
|
|
|
|
mSize = mType.getCount() * mType.getElement().getBytesSize();
|
2011-07-07 16:05:18 -07:00
|
|
|
updateCacheInfo(t);
|
2011-07-07 15:24:42 -07:00
|
|
|
}
|
2013-08-22 14:55:26 -07:00
|
|
|
try {
|
|
|
|
RenderScript.registerNativeAllocation.invoke(RenderScript.sRuntime, mSize);
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(RenderScript.LOG_TAG, "Couldn't invoke registerNativeAllocation:" + e);
|
|
|
|
throw new RSRuntimeException("Couldn't invoke registerNativeAllocation:" + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void finalize() throws Throwable {
|
|
|
|
RenderScript.registerNativeFree.invoke(RenderScript.sRuntime, mSize);
|
|
|
|
super.finalize();
|
2010-07-12 15:50:32 -07:00
|
|
|
}
|
|
|
|
|
2013-11-25 18:28:33 -08:00
|
|
|
private void validateIsInt64() {
|
|
|
|
if ((mType.mElement.mType == Element.DataType.SIGNED_64) ||
|
|
|
|
(mType.mElement.mType == Element.DataType.UNSIGNED_64)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new RSIllegalArgumentException(
|
|
|
|
"64 bit integer source does not match allocation type " + mType.mElement.mType);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-11-25 18:28:33 -08:00
|
|
|
private void validateIsFloat64() {
|
|
|
|
if (mType.mElement.mType == Element.DataType.FLOAT_64) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new RSIllegalArgumentException(
|
|
|
|
"64 bit float source does not match allocation type " + mType.mElement.mType);
|
|
|
|
}
|
|
|
|
|
2011-01-16 15:04:08 -08:00
|
|
|
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();
|
2013-11-19 12:45:54 -08:00
|
|
|
long typeID = mRS.nAllocationGetType(getID(mRS));
|
2010-07-15 11:33:03 -07:00
|
|
|
if(typeID != 0) {
|
|
|
|
mType = new Type(typeID, mRS);
|
|
|
|
mType.updateFromNative();
|
2011-07-07 16:17:36 -07:00
|
|
|
updateCacheInfo(mType);
|
2010-07-15 11:33:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Get the {@link android.renderscript.Type} of the Allocation.
|
2012-03-23 13:51:56 -07:00
|
|
|
*
|
|
|
|
* @return Type
|
|
|
|
*
|
|
|
|
*/
|
2010-01-12 12:12:28 -08:00
|
|
|
public Type getType() {
|
|
|
|
return mType;
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Propagate changes from one usage of the Allocation to the
|
|
|
|
* other usages of the Allocation.
|
2012-03-23 13:51:56 -07:00
|
|
|
*
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
public void syncAll(int srcLocation) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "syncAll");
|
2010-12-08 16:14:36 -08:00
|
|
|
switch (srcLocation) {
|
2013-04-09 17:28:56 -07:00
|
|
|
case USAGE_GRAPHICS_TEXTURE:
|
2010-12-08 16:14:36 -08:00
|
|
|
case USAGE_SCRIPT:
|
2013-04-09 17:28:56 -07:00
|
|
|
if ((mUsage & USAGE_SHARED) != 0) {
|
|
|
|
copyFrom(mBitmap);
|
|
|
|
}
|
|
|
|
break;
|
2010-12-08 16:14:36 -08:00
|
|
|
case USAGE_GRAPHICS_CONSTANTS:
|
|
|
|
case USAGE_GRAPHICS_VERTEX:
|
|
|
|
break;
|
2013-04-09 17:28:56 -07:00
|
|
|
case USAGE_SHARED:
|
|
|
|
if ((mUsage & USAGE_SHARED) != 0) {
|
|
|
|
copyTo(mBitmap);
|
|
|
|
}
|
|
|
|
break;
|
2010-12-08 16:14:36 -08:00
|
|
|
default:
|
|
|
|
throw new RSIllegalArgumentException("Source must be exactly one usage type.");
|
|
|
|
}
|
|
|
|
mRS.validate();
|
2011-07-08 13:52:30 -07:00
|
|
|
mRS.nAllocationSyncAll(getIDSafe(), srcLocation);
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2010-12-08 16:14:36 -08:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Send a buffer to the output stream. The contents of the Allocation will
|
|
|
|
* be undefined after this operation. This operation is only valid if {@link
|
|
|
|
* #USAGE_IO_OUTPUT} is set on the Allocation.
|
|
|
|
*
|
2012-02-15 12:04:24 -08:00
|
|
|
*
|
|
|
|
*/
|
2012-03-29 17:58:15 -07:00
|
|
|
public void ioSend() {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "ioSend");
|
2012-02-15 12:04:24 -08:00
|
|
|
if ((mUsage & USAGE_IO_OUTPUT) == 0) {
|
|
|
|
throw new RSIllegalArgumentException(
|
|
|
|
"Can only send buffer if IO_OUTPUT usage specified.");
|
|
|
|
}
|
|
|
|
mRS.validate();
|
2012-04-03 15:36:36 -07:00
|
|
|
mRS.nAllocationIoSend(getID(mRS));
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2012-02-15 12:04:24 -08:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Receive the latest input into the Allocation. This operation
|
|
|
|
* is only valid if {@link #USAGE_IO_INPUT} is set on the Allocation.
|
2012-02-15 12:04:24 -08:00
|
|
|
*
|
|
|
|
*/
|
2012-03-29 17:58:15 -07:00
|
|
|
public void ioReceive() {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "ioReceive");
|
2012-02-15 12:04:24 -08:00
|
|
|
if ((mUsage & USAGE_IO_INPUT) == 0) {
|
|
|
|
throw new RSIllegalArgumentException(
|
2012-03-23 11:47:26 -07:00
|
|
|
"Can only receive if IO_INPUT usage specified.");
|
2012-02-15 12:04:24 -08:00
|
|
|
}
|
|
|
|
mRS.validate();
|
2012-04-03 15:36:36 -07:00
|
|
|
mRS.nAllocationIoReceive(getID(mRS));
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2012-02-15 12:04:24 -08:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy an array of RS objects to the Allocation.
|
2012-03-23 13:51:56 -07:00
|
|
|
*
|
|
|
|
* @param d Source array.
|
|
|
|
*/
|
2010-12-06 15:59:59 -08:00
|
|
|
public void copyFrom(BaseObj[] d) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
|
2010-12-06 15:59:59 -08:00
|
|
|
mRS.validate();
|
2011-01-16 15:04:08 -08:00
|
|
|
validateIsObject();
|
2011-07-07 15:24:42 -07:00
|
|
|
if (d.length != mCurrentCount) {
|
2010-12-06 15:59:59 -08:00
|
|
|
throw new RSIllegalArgumentException("Array size mismatch, allocation sizeX = " +
|
2011-07-07 15:24:42 -07:00
|
|
|
mCurrentCount + ", array length = " + d.length);
|
2010-12-06 15:59:59 -08:00
|
|
|
}
|
2013-11-19 12:45:54 -08:00
|
|
|
// FIXME: requires 64-bit path
|
|
|
|
|
2010-12-06 15:59:59 -08:00
|
|
|
int i[] = new int[d.length];
|
|
|
|
for (int ct=0; ct < d.length; ct++) {
|
2013-11-19 12:45:54 -08:00
|
|
|
i[ct] = (int)d[ct].getID(mRS);
|
2010-12-06 15:59:59 -08:00
|
|
|
}
|
2011-07-07 15:24:42 -07:00
|
|
|
copy1DRangeFromUnchecked(0, mCurrentCount, i);
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
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();
|
2013-02-28 11:45:22 -08:00
|
|
|
if (bc == null) {
|
|
|
|
throw new RSIllegalArgumentException("Bitmap has an unsupported format for this operation");
|
|
|
|
}
|
2011-01-11 17:42:52 -08:00
|
|
|
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 +
|
2012-04-11 14:04:23 -07:00
|
|
|
" of " + mType.getElement().getBytesSize() +
|
2011-01-11 17:42:52 -08:00
|
|
|
" bytes, passed bitmap was " + bc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ARGB_8888:
|
|
|
|
if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
|
2012-04-11 14:04:23 -07:00
|
|
|
(mType.getElement().getBytesSize() != 4)) {
|
2011-01-11 17:42:52 -08:00
|
|
|
throw new RSIllegalArgumentException("Allocation kind is " +
|
|
|
|
mType.getElement().mKind + ", type " +
|
|
|
|
mType.getElement().mType +
|
2012-04-11 14:04:23 -07:00
|
|
|
" of " + mType.getElement().getBytesSize() +
|
2011-01-11 17:42:52 -08:00
|
|
|
" bytes, passed bitmap was " + bc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RGB_565:
|
|
|
|
if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGB) ||
|
2012-04-11 14:04:23 -07:00
|
|
|
(mType.getElement().getBytesSize() != 2)) {
|
2011-01-11 17:42:52 -08:00
|
|
|
throw new RSIllegalArgumentException("Allocation kind is " +
|
|
|
|
mType.getElement().mKind + ", type " +
|
|
|
|
mType.getElement().mType +
|
2012-04-11 14:04:23 -07:00
|
|
|
" of " + mType.getElement().getBytesSize() +
|
2011-01-11 17:42:52 -08:00
|
|
|
" bytes, passed bitmap was " + bc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ARGB_4444:
|
|
|
|
if ((mType.getElement().mKind != Element.DataKind.PIXEL_RGBA) ||
|
2012-04-11 14:04:23 -07:00
|
|
|
(mType.getElement().getBytesSize() != 2)) {
|
2011-01-11 17:42:52 -08:00
|
|
|
throw new RSIllegalArgumentException("Allocation kind is " +
|
|
|
|
mType.getElement().mKind + ", type " +
|
|
|
|
mType.getElement().mType +
|
2012-04-11 14:04:23 -07:00
|
|
|
" of " + mType.getElement().getBytesSize() +
|
2011-01-11 17:42:52 -08:00
|
|
|
" 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) {
|
2011-07-07 15:24:42 -07:00
|
|
|
if((mCurrentDimX != b.getWidth()) || (mCurrentDimY != b.getHeight())) {
|
2011-01-12 14:53:25 -08:00
|
|
|
throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 18:28:33 -08:00
|
|
|
private void copyFromUnchecked(Object array, Element.DataType dt, int arrayLen) {
|
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
|
|
|
|
mRS.validate();
|
|
|
|
if (mCurrentDimZ > 0) {
|
|
|
|
copy3DRangeFromUnchecked(0, 0, 0, mCurrentDimX, mCurrentDimY, mCurrentDimZ, array, dt, arrayLen);
|
|
|
|
} else if (mCurrentDimY > 0) {
|
|
|
|
copy2DRangeFromUnchecked(0, 0, mCurrentDimX, mCurrentDimY, array, dt, arrayLen);
|
|
|
|
} else {
|
|
|
|
copy1DRangeFromUnchecked(0, mCurrentCount, array, dt, arrayLen);
|
|
|
|
}
|
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy into this Allocation from an array. This method does not guarantee
|
|
|
|
* that the Allocation is compatible with the input buffer; it copies memory
|
|
|
|
* without reinterpretation.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
2013-11-25 18:28:33 -08:00
|
|
|
* @param array The source data array
|
|
|
|
* @hide
|
2011-01-19 15:44:38 -08:00
|
|
|
*/
|
2013-11-25 18:28:33 -08:00
|
|
|
public void copyFromUnchecked(Object array) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copyFromUnchecked");
|
2013-11-25 18:28:33 -08:00
|
|
|
copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, false),
|
|
|
|
java.lang.reflect.Array.getLength(array));
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2011-01-19 15:44:38 -08:00
|
|
|
}
|
2013-05-23 16:59:23 -07:00
|
|
|
|
2013-11-25 18:28:33 -08:00
|
|
|
/**
|
|
|
|
* Copy into this Allocation from an array. This method does not guarantee
|
|
|
|
* that the Allocation is compatible with the input buffer; it copies memory
|
|
|
|
* without reinterpretation.
|
|
|
|
*
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
|
|
|
public void copyFromUnchecked(int[] d) {
|
|
|
|
copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy into this Allocation from an array. This method does not guarantee
|
|
|
|
* that the Allocation is compatible with the input buffer; it copies memory
|
|
|
|
* without reinterpretation.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
|
|
|
public void copyFromUnchecked(short[] d) {
|
2013-11-25 18:28:33 -08:00
|
|
|
copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
|
2011-01-19 15:44:38 -08:00
|
|
|
}
|
2013-05-23 16:59:23 -07:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy into this Allocation from an array. This method does not guarantee
|
|
|
|
* that the Allocation is compatible with the input buffer; it copies memory
|
|
|
|
* without reinterpretation.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
|
|
|
public void copyFromUnchecked(byte[] d) {
|
2013-11-25 18:28:33 -08:00
|
|
|
copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
|
2011-01-19 15:44:38 -08:00
|
|
|
}
|
2013-05-23 16:59:23 -07:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy into this Allocation from an array. This method does not guarantee
|
|
|
|
* that the Allocation is compatible with the input buffer; it copies memory
|
|
|
|
* without reinterpretation.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
|
|
|
public void copyFromUnchecked(float[] d) {
|
2013-11-25 18:28:33 -08:00
|
|
|
copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
|
2011-01-19 15:44:38 -08:00
|
|
|
}
|
|
|
|
|
2013-05-23 16:59:23 -07:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy into this Allocation from an array. This variant is type checked
|
|
|
|
* and will generate exceptions if the Allocation's {@link
|
2013-11-25 18:28:33 -08:00
|
|
|
* android.renderscript.Element} does not match the array's
|
|
|
|
* primitive type.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param d the source data array
|
2013-11-25 18:28:33 -08:00
|
|
|
* @hide
|
2011-01-19 15:44:38 -08:00
|
|
|
*/
|
2013-11-25 18:28:33 -08:00
|
|
|
public void copyFrom(Object array) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
|
2013-11-25 18:28:33 -08:00
|
|
|
copyFromUnchecked(array, validateObjectIsPrimitiveArray(array, true),
|
|
|
|
java.lang.reflect.Array.getLength(array));
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
2011-01-19 15:44:38 -08:00
|
|
|
|
2013-11-25 18:28:33 -08:00
|
|
|
/**
|
|
|
|
* Copy into this Allocation from an array. This variant is type checked
|
|
|
|
* and will generate exceptions if the Allocation's {@link
|
|
|
|
* android.renderscript.Element} is not a 32 bit integer type.
|
|
|
|
*
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
|
|
|
public void copyFrom(int[] d) {
|
|
|
|
validateIsInt32();
|
|
|
|
copyFromUnchecked(d, Element.DataType.SIGNED_32, d.length);
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy into this Allocation from an array. This variant is type checked
|
|
|
|
* and will generate exceptions if the Allocation's {@link
|
|
|
|
* android.renderscript.Element} is not a 16 bit integer type.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
2010-12-06 15:59:59 -08:00
|
|
|
public void copyFrom(short[] d) {
|
2013-11-25 18:28:33 -08:00
|
|
|
validateIsInt16();
|
|
|
|
copyFromUnchecked(d, Element.DataType.SIGNED_16, d.length);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
2011-01-19 15:44:38 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy into this Allocation from an array. This variant is type checked
|
|
|
|
* and will generate exceptions if the Allocation's {@link
|
|
|
|
* android.renderscript.Element} is not an 8 bit integer type.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
2010-12-06 15:59:59 -08:00
|
|
|
public void copyFrom(byte[] d) {
|
2013-11-25 18:28:33 -08:00
|
|
|
validateIsInt8();
|
|
|
|
copyFromUnchecked(d, Element.DataType.SIGNED_8, d.length);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
2011-01-19 15:44:38 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy into this Allocation from an array. This variant is type checked
|
|
|
|
* and will generate exceptions if the Allocation's {@link
|
|
|
|
* android.renderscript.Element} is not a 32 bit float type.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
2010-12-06 15:59:59 -08:00
|
|
|
public void copyFrom(float[] d) {
|
2013-11-25 18:28:33 -08:00
|
|
|
validateIsFloat32();
|
|
|
|
copyFromUnchecked(d, Element.DataType.FLOAT_32, d.length);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
2011-01-19 15:44:38 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy into an Allocation from a {@link android.graphics.Bitmap}. The
|
|
|
|
* height, width, and format of the bitmap must match the existing
|
|
|
|
* allocation.
|
|
|
|
*
|
|
|
|
* <p>If the {@link android.graphics.Bitmap} is the same as the {@link
|
|
|
|
* android.graphics.Bitmap} used to create the Allocation with {@link
|
|
|
|
* #createFromBitmap} and {@link #USAGE_SHARED} is set on the Allocation,
|
|
|
|
* this will synchronize the Allocation with the latest data from the {@link
|
|
|
|
* android.graphics.Bitmap}, potentially avoiding the actual copy.</p>
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param b the source bitmap
|
|
|
|
*/
|
2010-12-06 15:59:59 -08:00
|
|
|
public void copyFrom(Bitmap b) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
|
2011-01-12 14:53:25 -08:00
|
|
|
mRS.validate();
|
2013-02-28 11:45:22 -08:00
|
|
|
if (b.getConfig() == null) {
|
|
|
|
Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
|
|
|
|
Canvas c = new Canvas(newBitmap);
|
|
|
|
c.drawBitmap(b, 0, 0, null);
|
|
|
|
copyFrom(newBitmap);
|
|
|
|
return;
|
|
|
|
}
|
2011-01-12 14:53:25 -08:00
|
|
|
validateBitmapSize(b);
|
|
|
|
validateBitmapFormat(b);
|
2012-04-03 15:36:36 -07:00
|
|
|
mRS.nAllocationCopyFromBitmap(getID(mRS), b);
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2010-12-10 16:03:15 -08:00
|
|
|
}
|
2010-10-11 12:35:15 -07:00
|
|
|
|
2012-10-03 13:50:05 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy an Allocation from an Allocation. The types of both allocations
|
2012-10-03 13:50:05 -07:00
|
|
|
* must be identical.
|
|
|
|
*
|
|
|
|
* @param a the source allocation
|
|
|
|
*/
|
|
|
|
public void copyFrom(Allocation a) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copyFrom");
|
2012-10-03 13:50:05 -07:00
|
|
|
mRS.validate();
|
|
|
|
if (!mType.equals(a.getType())) {
|
|
|
|
throw new RSIllegalArgumentException("Types of allocations must match.");
|
|
|
|
}
|
|
|
|
copy2DRangeFrom(0, 0, mCurrentDimX, mCurrentDimY, a, 0, 0);
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2012-10-03 13:50:05 -07:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* This is only intended to be used by auto-generated code reflected from
|
|
|
|
* the RenderScript script files and should not be used by developers.
|
2011-01-07 17:00:07 -08:00
|
|
|
*
|
|
|
|
* @param xoff
|
|
|
|
* @param fp
|
|
|
|
*/
|
2011-01-16 15:05:41 -08:00
|
|
|
public void setFromFieldPacker(int xoff, FieldPacker fp) {
|
2012-02-22 15:22:41 -08:00
|
|
|
mRS.validate();
|
2012-04-11 14:04:23 -07:00
|
|
|
int eSize = mType.mElement.getBytesSize();
|
2010-03-26 15:33:42 -07:00
|
|
|
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 + ".");
|
|
|
|
}
|
2011-07-07 15:24:42 -07:00
|
|
|
copy1DRangeFromUnchecked(xoff, count, data);
|
2010-08-31 13:50:42 -07:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* This is only intended to be used by auto-generated code reflected from
|
|
|
|
* the RenderScript script files.
|
2011-01-07 17:00:07 -08:00
|
|
|
*
|
|
|
|
* @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) {
|
2012-02-22 15:22:41 -08:00
|
|
|
mRS.validate();
|
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();
|
2012-04-11 14:04:23 -07:00
|
|
|
int eSize = mType.mElement.mElements[component_number].getBytesSize();
|
2012-02-02 09:47:26 -08:00
|
|
|
eSize *= mType.mElement.mArraySizes[component_number];
|
2010-08-31 13:50:42 -07:00
|
|
|
|
|
|
|
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 + ".");
|
|
|
|
}
|
|
|
|
|
2011-07-08 13:52:30 -07:00
|
|
|
mRS.nAllocationElementData1D(getIDSafe(), xoff, mSelectedLOD,
|
2011-07-07 15:24:42 -07:00
|
|
|
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
|
|
|
}
|
2011-07-07 15:24:42 -07:00
|
|
|
if((off + count) > mCurrentCount) {
|
|
|
|
throw new RSIllegalArgumentException("Overflow, Available count " + mCurrentCount +
|
2010-03-26 15:33:42 -07:00
|
|
|
", got " + count + " at offset " + off + ".");
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
2011-07-07 15:24:42 -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
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Generate a mipmap chain. This is only valid if the Type of the Allocation
|
|
|
|
* includes mipmaps.
|
2011-01-12 13:28:37 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* <p>This function will generate a complete set of mipmaps from the top
|
|
|
|
* level LOD and place them into the script memory space.</p>
|
2011-01-12 13:28:37 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* <p>If the Allocation is also using other memory spaces, a call to {@link
|
|
|
|
* #syncAll syncAll(Allocation.USAGE_SCRIPT)} is required.</p>
|
2011-01-12 13:28:37 -08:00
|
|
|
*/
|
|
|
|
public void generateMipmaps() {
|
2012-04-03 15:36:36 -07:00
|
|
|
mRS.nAllocationGenerateMipmaps(getID(mRS));
|
2011-01-12 13:28:37 -08:00
|
|
|
}
|
|
|
|
|
2013-11-25 18:28:33 -08:00
|
|
|
private void copy1DRangeFromUnchecked(int off, int count, Object array,
|
|
|
|
Element.DataType dt, int arrayLen) {
|
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFromUnchecked");
|
|
|
|
final int dataSize = mType.mElement.getBytesSize() * count;
|
|
|
|
data1DChecks(off, count, arrayLen * dt.mSize, dataSize);
|
|
|
|
mRS.nAllocationData1D(getIDSafe(), off, mSelectedLOD, count, array, dataSize, dt);
|
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy an array into part of this Allocation. This method does not
|
|
|
|
* guarantee that the Allocation is compatible with the input buffer.
|
|
|
|
*
|
|
|
|
* @param off The offset of the first element to be copied.
|
|
|
|
* @param count The number of elements to be copied.
|
|
|
|
* @param array The source data array
|
|
|
|
* @hide
|
|
|
|
*/
|
|
|
|
public void copy1DRangeFromUnchecked(int off, int count, Object array) {
|
|
|
|
copy1DRangeFromUnchecked(off, count, array,
|
|
|
|
validateObjectIsPrimitiveArray(array, false),
|
|
|
|
java.lang.reflect.Array.getLength(array));
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy an array into part of this Allocation. This method does not
|
|
|
|
* guarantee that the Allocation is compatible with the input buffer.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param off The offset of the first element to be copied.
|
|
|
|
* @param count The number of elements to be copied.
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
|
|
|
public void copy1DRangeFromUnchecked(int off, int count, int[] d) {
|
2013-11-25 18:28:33 -08:00
|
|
|
copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_32, d.length);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
2013-05-23 16:59:23 -07:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy an array into part of this Allocation. This method does not
|
|
|
|
* guarantee that the Allocation is compatible with the input buffer.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param off The offset of the first element to be copied.
|
|
|
|
* @param count The number of elements to be copied.
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
|
|
|
public void copy1DRangeFromUnchecked(int off, int count, short[] d) {
|
2013-11-25 18:28:33 -08:00
|
|
|
copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_16, d.length);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
2013-05-23 16:59:23 -07:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy an array into part of this Allocation. This method does not
|
|
|
|
* guarantee that the Allocation is compatible with the input buffer.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param off The offset of the first element to be copied.
|
|
|
|
* @param count The number of elements to be copied.
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
|
|
|
public void copy1DRangeFromUnchecked(int off, int count, byte[] d) {
|
2013-11-25 18:28:33 -08:00
|
|
|
copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.SIGNED_8, d.length);
|
2009-09-21 19:41:04 -07:00
|
|
|
}
|
2013-05-23 16:59:23 -07:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy an array into part of this Allocation. This method does not
|
|
|
|
* guarantee that the Allocation is compatible with the input buffer.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param off The offset of the first element to be copied.
|
|
|
|
* @param count The number of elements to be copied.
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
|
|
|
public void copy1DRangeFromUnchecked(int off, int count, float[] d) {
|
2013-11-25 18:28:33 -08:00
|
|
|
copy1DRangeFromUnchecked(off, count, (Object)d, Element.DataType.FLOAT_32, d.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy an array into part of this Allocation. This variant is type checked
|
|
|
|
* and will generate exceptions if the Allocation type does not
|
|
|
|
* match the component type of the array passed in.
|
|
|
|
*
|
|
|
|
* @param off The offset of the first element to be copied.
|
|
|
|
* @param count The number of elements to be copied.
|
|
|
|
* @param array The source data array.
|
|
|
|
* @hide
|
|
|
|
*/
|
|
|
|
public void copy1DRangeFrom(int off, int count, Object array) {
|
|
|
|
copy1DRangeFromUnchecked(off, count, array,
|
|
|
|
validateObjectIsPrimitiveArray(array, true),
|
|
|
|
java.lang.reflect.Array.getLength(array));
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy an array into part of this Allocation. This variant is type checked
|
|
|
|
* and will generate exceptions if the Allocation type is not a 32 bit
|
|
|
|
* integer type.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param off The offset of the first element to be copied.
|
|
|
|
* @param count The number of elements to be copied.
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
2011-01-16 15:04:08 -08:00
|
|
|
public void copy1DRangeFrom(int off, int count, int[] d) {
|
|
|
|
validateIsInt32();
|
2013-11-25 18:28:33 -08:00
|
|
|
copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_32, d.length);
|
2011-01-16 15:04:08 -08:00
|
|
|
}
|
2011-01-19 15:44:38 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy an array into part of this Allocation. This variant is type checked
|
|
|
|
* and will generate exceptions if the Allocation type is not a 16 bit
|
|
|
|
* integer type.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param off The offset of the first element to be copied.
|
|
|
|
* @param count The number of elements to be copied.
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
2011-01-16 15:04:08 -08:00
|
|
|
public void copy1DRangeFrom(int off, int count, short[] d) {
|
|
|
|
validateIsInt16();
|
2013-11-25 18:28:33 -08:00
|
|
|
copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_16, d.length);
|
2011-01-16 15:04:08 -08:00
|
|
|
}
|
2011-01-19 15:44:38 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy an array into part of this Allocation. This variant is type checked
|
|
|
|
* and will generate exceptions if the Allocation type is not an 8 bit
|
|
|
|
* integer type.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param off The offset of the first element to be copied.
|
|
|
|
* @param count The number of elements to be copied.
|
|
|
|
* @param d the source data array
|
|
|
|
*/
|
2011-01-16 15:04:08 -08:00
|
|
|
public void copy1DRangeFrom(int off, int count, byte[] d) {
|
|
|
|
validateIsInt8();
|
2013-11-25 18:28:33 -08:00
|
|
|
copy1DRangeFromUnchecked(off, count, d, Element.DataType.SIGNED_8, d.length);
|
2011-01-16 15:04:08 -08:00
|
|
|
}
|
2011-01-19 15:44:38 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy an array into part of this Allocation. This variant is type checked
|
|
|
|
* and will generate exceptions if the Allocation type is not a 32 bit float
|
|
|
|
* type.
|
2011-01-19 15:44:38 -08:00
|
|
|
*
|
|
|
|
* @param off The offset of the first element to be copied.
|
|
|
|
* @param count The number of elements to be copied.
|
2011-06-14 11:13:19 -07:00
|
|
|
* @param d the source data array.
|
2011-01-19 15:44:38 -08:00
|
|
|
*/
|
2011-01-16 15:04:08 -08:00
|
|
|
public void copy1DRangeFrom(int off, int count, float[] d) {
|
|
|
|
validateIsFloat32();
|
2013-11-25 18:28:33 -08:00
|
|
|
copy1DRangeFromUnchecked(off, count, d, Element.DataType.FLOAT_32, d.length);
|
2011-01-16 15:04:08 -08:00
|
|
|
}
|
2013-11-25 18:28:33 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy part of an Allocation into this Allocation.
|
2011-06-14 11:13:19 -07:00
|
|
|
*
|
|
|
|
* @param off The offset of the first element to be copied.
|
|
|
|
* @param count The number of elements to be copied.
|
|
|
|
* @param data the source data allocation.
|
|
|
|
* @param dataOff off The offset of the first element in data to
|
|
|
|
* be copied.
|
|
|
|
*/
|
|
|
|
public void copy1DRangeFrom(int off, int count, Allocation data, int dataOff) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copy1DRangeFrom");
|
2011-07-08 13:52:30 -07:00
|
|
|
mRS.nAllocationData2D(getIDSafe(), off, 0,
|
2011-07-07 15:24:42 -07:00
|
|
|
mSelectedLOD, mSelectedFace.mID,
|
2012-04-03 15:36:36 -07:00
|
|
|
count, 1, data.getID(mRS), dataOff, 0,
|
2011-07-07 15:24:42 -07:00
|
|
|
data.mSelectedLOD, data.mSelectedFace.mID);
|
2011-06-14 11:13:19 -07:00
|
|
|
}
|
|
|
|
|
2011-01-12 14:53:25 -08:00
|
|
|
private void validate2DRange(int xoff, int yoff, int w, int h) {
|
2011-07-07 15:24:42 -07:00
|
|
|
if (mAdaptedAllocation != null) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
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) > mCurrentDimX) || ((yoff + h) > mCurrentDimY)) {
|
|
|
|
throw new RSIllegalArgumentException("Updated region larger than allocation.");
|
|
|
|
}
|
2011-01-12 14:53:25 -08:00
|
|
|
}
|
|
|
|
}
|
2009-09-21 19:41:04 -07:00
|
|
|
|
2013-11-25 18:28:33 -08:00
|
|
|
void copy2DRangeFromUnchecked(int xoff, int yoff, int w, int h, Object array,
|
|
|
|
Element.DataType dt, int arrayLen) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFromUnchecked");
|
2011-01-07 17:00:07 -08:00
|
|
|
mRS.validate();
|
2011-01-12 14:53:25 -08:00
|
|
|
validate2DRange(xoff, yoff, w, h);
|
2013-11-25 18:28:33 -08:00
|
|
|
mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, w, h,
|
|
|
|
array, arrayLen * dt.mSize, dt);
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2011-01-07 17:00:07 -08:00
|
|
|
}
|
|
|
|
|
2013-11-25 18:28:33 -08:00
|
|
|
/**
|
|
|
|
* Copy from an array into a rectangular region in this Allocation. The
|
|
|
|
* array is assumed to be tightly packed.
|
|
|
|
*
|
|
|
|
* @param xoff X offset of the region to update in this Allocation
|
|
|
|
* @param yoff Y offset of the region to update in this Allocation
|
|
|
|
* @param w Width of the region to update
|
|
|
|
* @param h Height of the region to update
|
|
|
|
* @param data to be placed into the Allocation
|
|
|
|
* @hide
|
|
|
|
*/
|
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, Object array) {
|
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
|
|
|
|
copy2DRangeFromUnchecked(xoff, yoff, w, h, array,
|
|
|
|
validateObjectIsPrimitiveArray(array, true),
|
|
|
|
java.lang.reflect.Array.getLength(array));
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|
2013-02-08 17:11:31 -08:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy from an array into a rectangular region in this Allocation. The
|
|
|
|
* array is assumed to be tightly packed.
|
2013-02-08 17:11:31 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @param xoff X offset of the region to update in this Allocation
|
|
|
|
* @param yoff Y offset of the region to update in this Allocation
|
|
|
|
* @param w Width of the region to update
|
|
|
|
* @param h Height of the region to update
|
|
|
|
* @param data to be placed into the Allocation
|
2013-02-08 17:11:31 -08:00
|
|
|
*/
|
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, byte[] data) {
|
2013-02-08 21:03:51 -08:00
|
|
|
validateIsInt8();
|
2013-11-25 18:28:33 -08:00
|
|
|
copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
|
|
|
|
Element.DataType.SIGNED_8, data.length);
|
2013-02-08 17:11:31 -08:00
|
|
|
}
|
|
|
|
|
2013-04-09 11:01:01 -07:00
|
|
|
/**
|
|
|
|
* Copy from an array into a rectangular region in this Allocation. The
|
|
|
|
* array is assumed to be tightly packed.
|
|
|
|
*
|
|
|
|
* @param xoff X offset of the region to update in this Allocation
|
|
|
|
* @param yoff Y offset of the region to update in this Allocation
|
|
|
|
* @param w Width of the region to update
|
|
|
|
* @param h Height of the region to update
|
|
|
|
* @param data to be placed into the Allocation
|
|
|
|
*/
|
2013-02-08 17:11:31 -08:00
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, short[] data) {
|
2013-02-08 21:03:51 -08:00
|
|
|
validateIsInt16();
|
2013-11-25 18:28:33 -08:00
|
|
|
copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
|
|
|
|
Element.DataType.SIGNED_16, data.length);
|
2013-02-08 17:11:31 -08:00
|
|
|
}
|
|
|
|
|
2013-04-09 11:01:01 -07:00
|
|
|
/**
|
|
|
|
* Copy from an array into a rectangular region in this Allocation. The
|
|
|
|
* array is assumed to be tightly packed.
|
|
|
|
*
|
|
|
|
* @param xoff X offset of the region to update in this Allocation
|
|
|
|
* @param yoff Y offset of the region to update in this Allocation
|
|
|
|
* @param w Width of the region to update
|
|
|
|
* @param h Height of the region to update
|
|
|
|
* @param data to be placed into the Allocation
|
|
|
|
*/
|
2013-02-08 17:11:31 -08:00
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, int[] data) {
|
2013-02-08 21:03:51 -08:00
|
|
|
validateIsInt32();
|
2013-11-25 18:28:33 -08:00
|
|
|
copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
|
|
|
|
Element.DataType.SIGNED_32, data.length);
|
2013-02-08 17:11:31 -08:00
|
|
|
}
|
|
|
|
|
2013-04-09 11:01:01 -07:00
|
|
|
/**
|
|
|
|
* Copy from an array into a rectangular region in this Allocation. The
|
|
|
|
* array is assumed to be tightly packed.
|
|
|
|
*
|
|
|
|
* @param xoff X offset of the region to update in this Allocation
|
|
|
|
* @param yoff Y offset of the region to update in this Allocation
|
|
|
|
* @param w Width of the region to update
|
|
|
|
* @param h Height of the region to update
|
|
|
|
* @param data to be placed into the Allocation
|
|
|
|
*/
|
2013-02-08 17:11:31 -08:00
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h, float[] data) {
|
2013-02-08 21:03:51 -08:00
|
|
|
validateIsFloat32();
|
2013-11-25 18:28:33 -08:00
|
|
|
copy2DRangeFromUnchecked(xoff, yoff, w, h, data,
|
|
|
|
Element.DataType.FLOAT_32, data.length);
|
2013-02-08 17:11:31 -08:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy a rectangular region from an Allocation into a rectangular region in
|
|
|
|
* this Allocation.
|
2011-06-14 11:13:19 -07:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @param xoff X offset of the region in this Allocation
|
|
|
|
* @param yoff Y offset of the region in this Allocation
|
|
|
|
* @param w Width of the region to update.
|
|
|
|
* @param h Height of the region to update.
|
|
|
|
* @param data source Allocation.
|
|
|
|
* @param dataXoff X offset in source Allocation
|
|
|
|
* @param dataYoff Y offset in source Allocation
|
2011-06-14 11:13:19 -07:00
|
|
|
*/
|
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, int w, int h,
|
|
|
|
Allocation data, int dataXoff, int dataYoff) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
|
2011-06-14 11:13:19 -07:00
|
|
|
mRS.validate();
|
|
|
|
validate2DRange(xoff, yoff, w, h);
|
2011-07-08 13:52:30 -07:00
|
|
|
mRS.nAllocationData2D(getIDSafe(), xoff, yoff,
|
2011-07-07 15:24:42 -07:00
|
|
|
mSelectedLOD, mSelectedFace.mID,
|
2012-04-03 15:36:36 -07:00
|
|
|
w, h, data.getID(mRS), dataXoff, dataYoff,
|
2011-07-07 15:24:42 -07:00
|
|
|
data.mSelectedLOD, data.mSelectedFace.mID);
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2011-06-14 11:13:19 -07:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy a {@link android.graphics.Bitmap} into an Allocation. The height
|
|
|
|
* and width of the update will use the height and width of the {@link
|
|
|
|
* android.graphics.Bitmap}.
|
2011-01-12 13:28:37 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @param xoff X offset of the region to update in this Allocation
|
|
|
|
* @param yoff Y offset of the region to update in this Allocation
|
|
|
|
* @param data the Bitmap to be copied
|
2011-01-12 13:28:37 -08:00
|
|
|
*/
|
|
|
|
public void copy2DRangeFrom(int xoff, int yoff, Bitmap data) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copy2DRangeFrom");
|
2011-01-07 17:00:07 -08:00
|
|
|
mRS.validate();
|
2013-02-28 11:45:22 -08:00
|
|
|
if (data.getConfig() == null) {
|
|
|
|
Bitmap newBitmap = Bitmap.createBitmap(data.getWidth(), data.getHeight(), Bitmap.Config.ARGB_8888);
|
|
|
|
Canvas c = new Canvas(newBitmap);
|
|
|
|
c.drawBitmap(data, 0, 0, null);
|
|
|
|
copy2DRangeFrom(xoff, yoff, newBitmap);
|
2013-04-09 15:59:24 -07:00
|
|
|
return;
|
2013-02-28 11:45:22 -08:00
|
|
|
}
|
2011-01-12 14:53:25 -08:00
|
|
|
validateBitmapFormat(data);
|
|
|
|
validate2DRange(xoff, yoff, data.getWidth(), data.getHeight());
|
2011-07-08 13:52:30 -07:00
|
|
|
mRS.nAllocationData2D(getIDSafe(), xoff, yoff, mSelectedLOD, mSelectedFace.mID, data);
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2011-01-07 17:00:07 -08:00
|
|
|
}
|
|
|
|
|
2013-04-09 15:59:24 -07:00
|
|
|
private void validate3DRange(int xoff, int yoff, int zoff, int w, int h, int d) {
|
|
|
|
if (mAdaptedAllocation != null) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (xoff < 0 || yoff < 0 || zoff < 0) {
|
|
|
|
throw new RSIllegalArgumentException("Offset cannot be negative.");
|
|
|
|
}
|
|
|
|
if (h < 0 || w < 0 || d < 0) {
|
|
|
|
throw new RSIllegalArgumentException("Height or width cannot be negative.");
|
|
|
|
}
|
|
|
|
if (((xoff + w) > mCurrentDimX) || ((yoff + h) > mCurrentDimY) || ((zoff + d) > mCurrentDimZ)) {
|
|
|
|
throw new RSIllegalArgumentException("Updated region larger than allocation.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @hide
|
|
|
|
*
|
|
|
|
*/
|
2013-11-25 18:28:33 -08:00
|
|
|
private void copy3DRangeFromUnchecked(int xoff, int yoff, int zoff, int w, int h, int d,
|
|
|
|
Object array, Element.DataType dt, int arrayLen) {
|
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFromUnchecked");
|
2013-04-09 15:59:24 -07:00
|
|
|
mRS.validate();
|
|
|
|
validate3DRange(xoff, yoff, zoff, w, h, d);
|
2013-11-25 18:28:33 -08:00
|
|
|
mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD, w, h, d,
|
|
|
|
array, arrayLen * dt.mSize, dt);
|
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2013-04-09 15:59:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @hide
|
|
|
|
* Copy a rectangular region from the array into the allocation.
|
2013-04-09 11:01:01 -07:00
|
|
|
* The array is assumed to be tightly packed.
|
|
|
|
*
|
|
|
|
* @param xoff X offset of the region to update in this Allocation
|
|
|
|
* @param yoff Y offset of the region to update in this Allocation
|
|
|
|
* @param zoff Z offset of the region to update in this Allocation
|
|
|
|
* @param w Width of the region to update
|
|
|
|
* @param h Height of the region to update
|
|
|
|
* @param d Depth of the region to update
|
2013-04-09 15:59:24 -07:00
|
|
|
* @param data to be placed into the allocation
|
|
|
|
*/
|
2013-11-25 18:28:33 -08:00
|
|
|
public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d, Object array) {
|
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copy3DRangeFrom");
|
|
|
|
copy3DRangeFromUnchecked(xoff, yoff, zoff, w, h, d, array,
|
|
|
|
validateObjectIsPrimitiveArray(array, true),
|
|
|
|
java.lang.reflect.Array.getLength(array));
|
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2013-04-09 15:59:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @hide
|
|
|
|
* Copy a rectangular region into the allocation from another
|
|
|
|
* allocation.
|
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @param xoff X offset of the region to update in this Allocation
|
|
|
|
* @param yoff Y offset of the region to update in this Allocation
|
|
|
|
* @param zoff Z offset of the region to update in this Allocation
|
|
|
|
* @param w Width of the region to update.
|
|
|
|
* @param h Height of the region to update.
|
|
|
|
* @param d Depth of the region to update.
|
2013-04-09 15:59:24 -07:00
|
|
|
* @param data source allocation.
|
2013-04-09 11:01:01 -07:00
|
|
|
* @param dataXoff X offset of the region in the source Allocation
|
|
|
|
* @param dataYoff Y offset of the region in the source Allocation
|
|
|
|
* @param dataZoff Z offset of the region in the source Allocation
|
2013-04-09 15:59:24 -07:00
|
|
|
*/
|
|
|
|
public void copy3DRangeFrom(int xoff, int yoff, int zoff, int w, int h, int d,
|
|
|
|
Allocation data, int dataXoff, int dataYoff, int dataZoff) {
|
|
|
|
mRS.validate();
|
|
|
|
validate3DRange(xoff, yoff, zoff, w, h, d);
|
|
|
|
mRS.nAllocationData3D(getIDSafe(), xoff, yoff, zoff, mSelectedLOD,
|
|
|
|
w, h, d, data.getID(mRS), dataXoff, dataYoff, dataZoff,
|
|
|
|
data.mSelectedLOD);
|
|
|
|
}
|
|
|
|
|
2011-01-07 17:00:07 -08:00
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy from the Allocation into a {@link android.graphics.Bitmap}. The
|
|
|
|
* bitmap must match the dimensions of the Allocation.
|
2011-07-08 13:52:30 -07:00
|
|
|
*
|
|
|
|
* @param b The bitmap to be set from the Allocation.
|
|
|
|
*/
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copyTo(Bitmap b) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
|
2011-01-12 14:53:25 -08:00
|
|
|
mRS.validate();
|
|
|
|
validateBitmapFormat(b);
|
|
|
|
validateBitmapSize(b);
|
2012-04-03 15:36:36 -07:00
|
|
|
mRS.nAllocationCopyToBitmap(getID(mRS), b);
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2011-01-07 17:00:07 -08:00
|
|
|
}
|
|
|
|
|
2013-11-25 18:28:33 -08:00
|
|
|
private void copyTo(Object array, Element.DataType dt, int arrayLen) {
|
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "copyTo");
|
|
|
|
mRS.validate();
|
|
|
|
mRS.nAllocationRead(getID(mRS), array, dt);
|
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy from the Allocation into an array. The array must be at
|
|
|
|
* least as large as the Allocation. The
|
|
|
|
* {@link android.renderscript.Element} must match the component
|
|
|
|
* type of the array passed in.
|
|
|
|
*
|
|
|
|
* @param array The array to be set from the Allocation.
|
|
|
|
* @hide
|
|
|
|
*/
|
|
|
|
public void copyTo(Object array) {
|
|
|
|
copyTo(array, validateObjectIsPrimitiveArray(array, true),
|
|
|
|
java.lang.reflect.Array.getLength(array));
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy from the Allocation into a byte array. The array must be at least
|
|
|
|
* as large as the Allocation. The allocation must be of an 8 bit integer
|
|
|
|
* {@link android.renderscript.Element} type.
|
2011-07-08 13:52:30 -07:00
|
|
|
*
|
|
|
|
* @param d The array to be set from the Allocation.
|
|
|
|
*/
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copyTo(byte[] d) {
|
2011-01-16 15:04:08 -08:00
|
|
|
validateIsInt8();
|
2013-11-25 18:28:33 -08:00
|
|
|
copyTo(d, Element.DataType.SIGNED_8, d.length);
|
2011-01-07 17:00:07 -08:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy from the Allocation into a short array. The array must be at least
|
|
|
|
* as large as the Allocation. The allocation must be of an 16 bit integer
|
|
|
|
* {@link android.renderscript.Element} type.
|
2011-07-08 13:52:30 -07:00
|
|
|
*
|
|
|
|
* @param d The array to be set from the Allocation.
|
|
|
|
*/
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copyTo(short[] d) {
|
2011-01-16 15:04:08 -08:00
|
|
|
validateIsInt16();
|
2013-11-25 18:28:33 -08:00
|
|
|
copyTo(d, Element.DataType.SIGNED_16, d.length);
|
2011-01-07 17:00:07 -08:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy from the Allocation into a int array. The array must be at least as
|
|
|
|
* large as the Allocation. The allocation must be of an 32 bit integer
|
|
|
|
* {@link android.renderscript.Element} type.
|
2011-07-08 13:52:30 -07:00
|
|
|
*
|
|
|
|
* @param d The array to be set from the Allocation.
|
|
|
|
*/
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copyTo(int[] d) {
|
2011-01-16 15:04:08 -08:00
|
|
|
validateIsInt32();
|
2013-11-25 18:28:33 -08:00
|
|
|
copyTo(d, Element.DataType.SIGNED_32, d.length);
|
2009-08-10 14:55:26 -07:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Copy from the Allocation into a float array. The array must be at least
|
|
|
|
* as large as the Allocation. The allocation must be of an 32 bit float
|
|
|
|
* {@link android.renderscript.Element} type.
|
2011-07-08 13:52:30 -07:00
|
|
|
*
|
|
|
|
* @param d The array to be set from the Allocation.
|
|
|
|
*/
|
2011-01-07 17:00:07 -08:00
|
|
|
public void copyTo(float[] d) {
|
2011-01-16 15:04:08 -08:00
|
|
|
validateIsFloat32();
|
2013-11-25 18:28:33 -08:00
|
|
|
copyTo(d, Element.DataType.FLOAT_32, d.length);
|
2009-08-10 14:55:26 -07:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07: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.
|
2011-01-12 13:28:37 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* <p>If the new region is smaller the references of any objects outside the
|
|
|
|
* new region will be released.</p>
|
2011-01-12 13:28:37 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* <p>A new type will be created with the new dimension.</p>
|
2011-01-12 13:28:37 -08:00
|
|
|
*
|
|
|
|
* @param dimX The new size of the allocation.
|
2013-04-09 15:59:24 -07:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @deprecated RenderScript objects should be immutable once created. The
|
|
|
|
* replacement is to create a new allocation and copy the contents.
|
2011-01-12 13:28:37 -08: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
|
|
|
}
|
2012-04-03 15:36:36 -07:00
|
|
|
mRS.nAllocationResize1D(getID(mRS), 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
|
|
|
|
2013-11-19 12:45:54 -08:00
|
|
|
long typeID = mRS.nAllocationGetType(getID(mRS));
|
2010-10-26 13:09:17 -07:00
|
|
|
mType = new Type(typeID, mRS);
|
|
|
|
mType.updateFromNative();
|
2011-07-07 16:05:18 -07:00
|
|
|
updateCacheInfo(mType);
|
2010-10-05 13:32:49 -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;
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates a new Allocation with the given {@link
|
|
|
|
* android.renderscript.Type}, mipmap flag, and usage flags.
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @param type RenderScript type describing data layout
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param mips specifies desired mipmap behaviour for the
|
|
|
|
* allocation
|
2013-04-09 11:01:01 -07:00
|
|
|
* @param usage bit field specifying how the Allocation is
|
2011-01-12 17:32:36 -08:00
|
|
|
* utilized
|
|
|
|
*/
|
|
|
|
static public Allocation createTyped(RenderScript rs, Type type, MipmapControl mips, int usage) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "createTyped");
|
2009-12-07 12:40:12 -08:00
|
|
|
rs.validate();
|
2012-04-03 15:36:36 -07:00
|
|
|
if (type.getID(rs) == 0) {
|
2010-11-09 17:11:40 -08:00
|
|
|
throw new RSInvalidStateException("Bad Type");
|
|
|
|
}
|
2013-11-19 12:45:54 -08:00
|
|
|
long id = rs.nAllocationCreateTyped(type.getID(rs), mips.mID, usage, 0);
|
2011-11-23 15:02:15 -08:00
|
|
|
if (id == 0) {
|
|
|
|
throw new RSRuntimeException("Allocation creation failed.");
|
|
|
|
}
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2011-11-23 15:02:15 -08:00
|
|
|
return new Allocation(id, rs, type, usage);
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates an Allocation with the size specified by the type and no mipmaps
|
|
|
|
* generated by default
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
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);
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates an Allocation for use by scripts with a given {@link
|
|
|
|
* android.renderscript.Type} and no mipmaps
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @param rs Context to which the Allocation will belong.
|
|
|
|
* @param type RenderScript Type describing data layout
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates an Allocation with a specified number of given elements
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @param rs Context to which the Allocation will belong.
|
|
|
|
* @param e Element to use in the Allocation
|
|
|
|
* @param count the number of Elements in the Allocation
|
|
|
|
* @param usage bit field specifying how the Allocation is
|
2011-01-12 17:32:36 -08:00
|
|
|
* utilized
|
|
|
|
*
|
|
|
|
* @return allocation
|
|
|
|
*/
|
2010-12-08 16:14:36 -08:00
|
|
|
static public Allocation createSized(RenderScript rs, Element e,
|
|
|
|
int count, int usage) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "createSized");
|
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();
|
|
|
|
|
2013-11-19 12:45:54 -08:00
|
|
|
long id = rs.nAllocationCreateTyped(t.getID(rs), MipmapControl.MIPMAP_NONE.mID, usage, 0);
|
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
|
|
|
}
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2010-12-08 16:14:36 -08:00
|
|
|
return new Allocation(id, rs, t, usage);
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates an Allocation with a specified number of given elements
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @param rs Context to which the Allocation will belong.
|
|
|
|
* @param e Element to use in the Allocation
|
|
|
|
* @param count the number of Elements in the Allocation
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
|
|
|
* @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();
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates an Allocation from a {@link android.graphics.Bitmap}.
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2013-04-09 11:01:01 -07:00
|
|
|
* @param b Bitmap source for the allocation data
|
2011-01-12 17:32:36 -08:00
|
|
|
* @param mips specifies desired mipmap behaviour for the
|
|
|
|
* allocation
|
|
|
|
* @param usage bit field specifying how the allocation is
|
|
|
|
* utilized
|
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @return Allocation containing bitmap data
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
|
|
|
*/
|
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) {
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceBegin(RenderScript.TRACE_TAG, "createFromBitmap");
|
2009-12-07 12:40:12 -08:00
|
|
|
rs.validate();
|
2013-02-28 11:45:22 -08:00
|
|
|
|
|
|
|
// WAR undocumented color formats
|
|
|
|
if (b.getConfig() == null) {
|
|
|
|
if ((usage & USAGE_SHARED) != 0) {
|
|
|
|
throw new RSIllegalArgumentException("USAGE_SHARED cannot be used with a Bitmap that has a null config.");
|
|
|
|
}
|
|
|
|
Bitmap newBitmap = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ARGB_8888);
|
|
|
|
Canvas c = new Canvas(newBitmap);
|
|
|
|
c.drawBitmap(b, 0, 0, null);
|
|
|
|
return createFromBitmap(rs, newBitmap, mips, usage);
|
|
|
|
}
|
|
|
|
|
2010-12-08 16:14:36 -08:00
|
|
|
Type t = typeFromBitmap(rs, b, mips);
|
2010-03-01 15:31:04 -08:00
|
|
|
|
2012-12-04 17:59:29 -08:00
|
|
|
// enable optimized bitmap path only with no mipmap and script-only usage
|
|
|
|
if (mips == MipmapControl.MIPMAP_NONE &&
|
|
|
|
t.getElement().isCompatible(Element.RGBA_8888(rs)) &&
|
2013-04-09 17:28:56 -07:00
|
|
|
usage == (USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE)) {
|
2013-11-19 12:45:54 -08:00
|
|
|
long id = rs.nAllocationCreateBitmapBackedAllocation(t.getID(rs), mips.mID, b, usage);
|
2012-12-04 17:59:29 -08:00
|
|
|
if (id == 0) {
|
|
|
|
throw new RSRuntimeException("Load failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep a reference to the Bitmap around to prevent GC
|
|
|
|
Allocation alloc = new Allocation(id, rs, t, usage);
|
|
|
|
alloc.setBitmap(b);
|
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
|
2013-04-13 19:48:36 -07:00
|
|
|
|
2013-11-19 12:45:54 -08:00
|
|
|
long id = rs.nAllocationCreateFromBitmap(t.getID(rs), mips.mID, b, usage);
|
2010-12-08 16:14:36 -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
|
|
|
}
|
2013-05-23 16:59:23 -07:00
|
|
|
Trace.traceEnd(RenderScript.TRACE_TAG);
|
2010-12-08 16:14:36 -08:00
|
|
|
return new Allocation(id, rs, t, usage);
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Returns the handle to a raw buffer that is being managed by the screen
|
|
|
|
* compositor. This operation is only valid for Allocations with {@link
|
|
|
|
* #USAGE_IO_INPUT}.
|
2012-03-28 15:30:07 -07:00
|
|
|
*
|
2012-04-11 14:04:23 -07:00
|
|
|
* @return Surface object associated with allocation
|
2012-03-28 15:30:07 -07:00
|
|
|
*
|
2012-02-15 12:04:24 -08:00
|
|
|
*/
|
2012-03-28 15:30:07 -07:00
|
|
|
public Surface getSurface() {
|
2013-02-22 12:45:54 -08:00
|
|
|
if ((mUsage & USAGE_IO_INPUT) == 0) {
|
|
|
|
throw new RSInvalidStateException("Allocation is not a surface texture.");
|
|
|
|
}
|
|
|
|
return mRS.nAllocationGetSurface(getID(mRS));
|
2012-03-28 15:30:07 -07:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Associate a {@link android.view.Surface} with this Allocation. This
|
|
|
|
* operation is only valid for Allocations with {@link #USAGE_IO_OUTPUT}.
|
2012-04-11 14:04:23 -07:00
|
|
|
*
|
|
|
|
* @param sur Surface to associate with allocation
|
2012-03-28 15:30:07 -07:00
|
|
|
*/
|
|
|
|
public void setSurface(Surface sur) {
|
|
|
|
mRS.validate();
|
2012-02-15 12:04:24 -08:00
|
|
|
if ((mUsage & USAGE_IO_OUTPUT) == 0) {
|
|
|
|
throw new RSInvalidStateException("Allocation is not USAGE_IO_OUTPUT.");
|
|
|
|
}
|
|
|
|
|
2012-04-03 15:36:36 -07:00
|
|
|
mRS.nAllocationSetSurface(getID(mRS), sur);
|
2012-02-15 12:04:24 -08:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates an Allocation from a {@link android.graphics.Bitmap}.
|
2012-12-17 16:35:06 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* <p>With target API version 18 or greater, this Allocation will be created
|
|
|
|
* with {@link #USAGE_SHARED}, {@link #USAGE_SCRIPT}, and {@link
|
|
|
|
* #USAGE_GRAPHICS_TEXTURE}. With target API version 17 or lower, this
|
|
|
|
* Allocation will be created with {@link #USAGE_GRAPHICS_TEXTURE}.</p>
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
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
|
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @return Allocation containing bitmap data
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
|
|
|
*/
|
2010-12-15 01:41:00 -08:00
|
|
|
static public Allocation createFromBitmap(RenderScript rs, Bitmap b) {
|
2012-12-17 16:35:06 -08:00
|
|
|
if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
|
|
|
|
return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
|
2013-04-09 17:28:56 -07:00
|
|
|
USAGE_SHARED | USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
|
2012-12-17 16:35:06 -08:00
|
|
|
}
|
2010-12-15 01:41:00 -08:00
|
|
|
return createFromBitmap(rs, b, MipmapControl.MIPMAP_NONE,
|
|
|
|
USAGE_GRAPHICS_TEXTURE);
|
2010-03-01 15:31:04 -08:00
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates a cubemap Allocation from a {@link android.graphics.Bitmap}
|
|
|
|
* containing the horizontal list of cube faces. Each face must be a square,
|
|
|
|
* have the same size as all other faces, and have a width that is a power
|
|
|
|
* of 2.
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
2011-01-13 14:53:43 -08:00
|
|
|
* @param rs Context to which the allocation will belong.
|
2013-04-09 11:01:01 -07:00
|
|
|
* @param b Bitmap with cubemap faces layed out in the following
|
2011-01-12 17:32:36 -08:00
|
|
|
* 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();
|
|
|
|
|
2013-11-19 12:45:54 -08:00
|
|
|
long id = rs.nAllocationCubeCreateFromBitmap(t.getID(rs), 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);
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates a non-mipmapped cubemap Allocation for use as a graphics texture
|
|
|
|
* from a {@link android.graphics.Bitmap} containing the horizontal list of
|
|
|
|
* cube faces. Each face must be a square, have the same size as all other
|
|
|
|
* faces, and have a width that is a power of 2.
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
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
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates a cubemap Allocation from 6 {@link android.graphics.Bitmap}
|
|
|
|
* objects containing the cube faces. Each face must be a square, have the
|
|
|
|
* same size as all other faces, and have a width that is a power of 2.
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
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);
|
2011-06-16 17:44:53 -07:00
|
|
|
adapter.setFace(Type.CubemapFace.POSITIVE_X);
|
2011-01-11 14:47:44 -08:00
|
|
|
adapter.copyFrom(xpos);
|
|
|
|
adapter.setFace(Type.CubemapFace.NEGATIVE_X);
|
|
|
|
adapter.copyFrom(xneg);
|
2011-06-16 17:44:53 -07:00
|
|
|
adapter.setFace(Type.CubemapFace.POSITIVE_Y);
|
2011-01-11 14:47:44 -08:00
|
|
|
adapter.copyFrom(ypos);
|
|
|
|
adapter.setFace(Type.CubemapFace.NEGATIVE_Y);
|
|
|
|
adapter.copyFrom(yneg);
|
2011-06-16 17:44:53 -07:00
|
|
|
adapter.setFace(Type.CubemapFace.POSITIVE_Z);
|
2011-01-11 14:47:44 -08:00
|
|
|
adapter.copyFrom(zpos);
|
|
|
|
adapter.setFace(Type.CubemapFace.NEGATIVE_Z);
|
|
|
|
adapter.copyFrom(zneg);
|
|
|
|
|
|
|
|
return cubemap;
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates a non-mipmapped cubemap Allocation for use as a sampler input
|
|
|
|
* from 6 {@link android.graphics.Bitmap} objects containing the cube
|
|
|
|
* faces. Each face must be a square, have the same size as all other faces,
|
|
|
|
* and have a width that is a power of 2.
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
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);
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates an Allocation from the Bitmap referenced
|
|
|
|
* by resource ID.
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
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
|
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @return Allocation containing resource data
|
2011-01-12 17:32:36 -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();
|
2013-05-31 14:00:46 -07:00
|
|
|
if ((usage & (USAGE_SHARED | USAGE_IO_INPUT | USAGE_IO_OUTPUT)) != 0) {
|
|
|
|
throw new RSIllegalArgumentException("Unsupported usage specified.");
|
|
|
|
}
|
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
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates a non-mipmapped Allocation to use as a graphics texture from the
|
|
|
|
* {@link android.graphics.Bitmap} referenced by resource ID.
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* <p>With target API version 18 or greater, this allocation will be created
|
|
|
|
* with {@link #USAGE_SCRIPT} and {@link #USAGE_GRAPHICS_TEXTURE}. With
|
|
|
|
* target API version 17 or lower, this allocation will be created with
|
|
|
|
* {@link #USAGE_GRAPHICS_TEXTURE}.</p>
|
2013-02-05 19:20:18 -08:00
|
|
|
*
|
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
|
|
|
|
*
|
2013-04-09 11:01:01 -07:00
|
|
|
* @return Allocation containing resource data
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
|
|
|
*/
|
2010-12-15 01:41:00 -08:00
|
|
|
static public Allocation createFromBitmapResource(RenderScript rs,
|
|
|
|
Resources res,
|
|
|
|
int id) {
|
2013-02-05 19:20:18 -08:00
|
|
|
if (rs.getApplicationContext().getApplicationInfo().targetSdkVersion >= 18) {
|
|
|
|
return createFromBitmapResource(rs, res, id,
|
|
|
|
MipmapControl.MIPMAP_NONE,
|
2013-05-31 14:00:46 -07:00
|
|
|
USAGE_SCRIPT | USAGE_GRAPHICS_TEXTURE);
|
2013-02-05 19:20:18 -08:00
|
|
|
}
|
2010-12-15 01:41:00 -08:00
|
|
|
return createFromBitmapResource(rs, res, id,
|
|
|
|
MipmapControl.MIPMAP_NONE,
|
|
|
|
USAGE_GRAPHICS_TEXTURE);
|
|
|
|
}
|
|
|
|
|
2012-05-07 15:34:29 -07:00
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Creates an Allocation containing string data encoded in UTF-8 format.
|
2011-01-12 17:32:36 -08:00
|
|
|
*
|
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
|
|
|
}
|
|
|
|
}
|
2013-04-11 18:07:52 -07:00
|
|
|
|
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Interface to handle notification when new buffers are available via
|
|
|
|
* {@link #USAGE_IO_INPUT}. An application will receive one notification
|
|
|
|
* when a buffer is available. Additional buffers will not trigger new
|
|
|
|
* notifications until a buffer is processed.
|
2013-04-11 18:07:52 -07:00
|
|
|
*/
|
2013-08-29 13:30:59 -07:00
|
|
|
public interface OnBufferAvailableListener {
|
2013-04-11 18:07:52 -07:00
|
|
|
public void onBufferAvailable(Allocation a);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-04-09 11:01:01 -07:00
|
|
|
* Set a notification handler for {@link #USAGE_IO_INPUT}.
|
2013-04-11 18:07:52 -07:00
|
|
|
*
|
2013-08-29 13:30:59 -07:00
|
|
|
* @param callback instance of the OnBufferAvailableListener
|
|
|
|
* class to be called when buffer arrive.
|
2013-04-11 18:07:52 -07:00
|
|
|
*/
|
2013-08-29 13:30:59 -07:00
|
|
|
public void setOnBufferAvailableListener(OnBufferAvailableListener callback) {
|
2013-04-11 18:07:52 -07:00
|
|
|
synchronized(mAllocationMap) {
|
2013-11-19 12:45:54 -08:00
|
|
|
mAllocationMap.put(new Long(getID(mRS)), this);
|
2013-04-11 18:07:52 -07:00
|
|
|
mBufferNotifier = callback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sendBufferNotification(int id) {
|
|
|
|
synchronized(mAllocationMap) {
|
2013-11-19 12:45:54 -08:00
|
|
|
Allocation a = mAllocationMap.get(new Long(id));
|
2013-04-11 18:07:52 -07:00
|
|
|
|
|
|
|
if ((a != null) && (a.mBufferNotifier != null)) {
|
|
|
|
a.mBufferNotifier.onBufferAvailable(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-31 20:40:47 -07:00
|
|
|
}
|
|
|
|
|