M7350v1_en_gpl

This commit is contained in:
T
2024-09-09 08:52:07 +00:00
commit f9cc65cfda
65988 changed files with 26357421 additions and 0 deletions
@@ -0,0 +1,382 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.io.IOException;
import java.io.InputStream;
import android.content.res.Resources;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.util.TypedValue;
/**
* @hide
*
**/
public class Allocation extends BaseObj {
Type mType;
Bitmap mBitmap;
Allocation(int id, RenderScript rs, Type t) {
super(rs);
mID = id;
mType = t;
}
public Type getType() {
return mType;
}
public void uploadToTexture(int baseMipLevel) {
mRS.validate();
mRS.nAllocationUploadToTexture(mID, false, baseMipLevel);
}
public void uploadToTexture(boolean genMips, int baseMipLevel) {
mRS.validate();
mRS.nAllocationUploadToTexture(mID, genMips, baseMipLevel);
}
public void uploadToBufferObject() {
mRS.validate();
mRS.nAllocationUploadToBufferObject(mID);
}
public void data(int[] d) {
mRS.validate();
subData1D(0, mType.getElementCount(), d);
}
public void data(short[] d) {
mRS.validate();
subData1D(0, mType.getElementCount(), d);
}
public void data(byte[] d) {
mRS.validate();
subData1D(0, mType.getElementCount(), d);
}
public void data(float[] d) {
mRS.validate();
subData1D(0, mType.getElementCount(), d);
}
private void data1DChecks(int off, int count, int len, int dataSize) {
mRS.validate();
if((off < 0) || (count < 1) || ((off + count) > mType.getElementCount())) {
throw new IllegalArgumentException("Offset or Count out of bounds.");
}
if((len) < dataSize) {
throw new IllegalArgumentException("Array too small for allocation type.");
}
}
public void subData1D(int off, int count, int[] d) {
int dataSize = mType.mElement.getSizeBytes() * count;
data1DChecks(off, count, d.length * 4, dataSize);
mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
}
public void subData1D(int off, int count, short[] d) {
int dataSize = mType.mElement.getSizeBytes() * count;
data1DChecks(off, count, d.length * 2, dataSize);
mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
}
public void subData1D(int off, int count, byte[] d) {
int dataSize = mType.mElement.getSizeBytes() * count;
data1DChecks(off, count, d.length, dataSize);
mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
}
public void subData1D(int off, int count, float[] d) {
int dataSize = mType.mElement.getSizeBytes() * count;
data1DChecks(off, count, d.length * 4, dataSize);
mRS.nAllocationSubData1D(mID, off, count, d, dataSize);
}
public void subData2D(int xoff, int yoff, int w, int h, int[] d) {
mRS.validate();
mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
}
public void subData2D(int xoff, int yoff, int w, int h, float[] d) {
mRS.validate();
mRS.nAllocationSubData2D(mID, xoff, yoff, w, h, d, d.length * 4);
}
public void readData(int[] d) {
mRS.validate();
mRS.nAllocationRead(mID, d);
}
public void readData(float[] d) {
mRS.validate();
mRS.nAllocationRead(mID, d);
}
public void data(Object o) {
mRS.validate();
mRS.nAllocationSubDataFromObject(mID, mType, 0, o);
}
public void read(Object o) {
mRS.validate();
mRS.nAllocationSubReadFromObject(mID, mType, 0, o);
}
public void subData(int offset, Object o) {
mRS.validate();
mRS.nAllocationSubDataFromObject(mID, mType, offset, o);
}
public class Adapter1D extends BaseObj {
Adapter1D(int id, RenderScript rs) {
super(rs);
mID = id;
}
public void setConstraint(Dimension dim, int value) {
mRS.validate();
mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
}
public void data(int[] d) {
mRS.validate();
mRS.nAdapter1DData(mID, d);
}
public void data(float[] d) {
mRS.validate();
mRS.nAdapter1DData(mID, d);
}
public void subData(int off, int count, int[] d) {
mRS.validate();
mRS.nAdapter1DSubData(mID, off, count, d);
}
public void subData(int off, int count, float[] d) {
mRS.validate();
mRS.nAdapter1DSubData(mID, off, count, d);
}
}
public Adapter1D createAdapter1D() {
mRS.validate();
int id = mRS.nAdapter1DCreate();
if(id == 0) {
throw new IllegalStateException("allocation failed.");
}
mRS.nAdapter1DBindAllocation(id, mID);
return new Adapter1D(id, mRS);
}
public class Adapter2D extends BaseObj {
Adapter2D(int id, RenderScript rs) {
super(rs);
mID = id;
}
public void setConstraint(Dimension dim, int value) {
mRS.validate();
mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
}
public void data(int[] d) {
mRS.validate();
mRS.nAdapter2DData(mID, d);
}
public void data(float[] d) {
mRS.validate();
mRS.nAdapter2DData(mID, d);
}
public void subData(int xoff, int yoff, int w, int h, int[] d) {
mRS.validate();
mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
}
public void subData(int xoff, int yoff, int w, int h, float[] d) {
mRS.validate();
mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
}
}
public Adapter2D createAdapter2D() {
mRS.validate();
int id = mRS.nAdapter2DCreate();
if(id == 0) {
throw new IllegalStateException("allocation failed.");
}
mRS.nAdapter2DBindAllocation(id, mID);
return new Adapter2D(id, mRS);
}
// creation
private static BitmapFactory.Options mBitmapOptions = new BitmapFactory.Options();
static {
mBitmapOptions.inScaled = false;
}
static public Allocation createTyped(RenderScript rs, Type type)
throws IllegalArgumentException {
rs.validate();
if(type.mID == 0) {
throw new IllegalStateException("Bad Type");
}
int id = rs.nAllocationCreateTyped(type.mID);
return new Allocation(id, rs, type);
}
static public Allocation createSized(RenderScript rs, Element e, int count)
throws IllegalArgumentException {
rs.validate();
Type.Builder b = new Type.Builder(rs, e);
b.add(Dimension.X, count);
Type t = b.create();
int id = rs.nAllocationCreateTyped(t.mID);
if(id == 0) {
throw new IllegalStateException("Bad element.");
}
return new Allocation(id, rs, t);
}
static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
final Bitmap.Config bc = b.getConfig();
if (bc == Bitmap.Config.ALPHA_8) {
return Element.A_8(rs);
}
if (bc == Bitmap.Config.ARGB_4444) {
return Element.RGBA_4444(rs);
}
if (bc == Bitmap.Config.ARGB_8888) {
return Element.RGBA_8888(rs);
}
if (bc == Bitmap.Config.RGB_565) {
return Element.RGB_565(rs);
}
throw new IllegalStateException("Bad bitmap type.");
}
static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
Element e = elementFromBitmap(rs, b);
Type.Builder tb = new Type.Builder(rs, e);
tb.add(Dimension.X, b.getWidth());
tb.add(Dimension.Y, b.getHeight());
return tb.create();
}
static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
throws IllegalArgumentException {
rs.validate();
Type t = typeFromBitmap(rs, b);
int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
if(id == 0) {
throw new IllegalStateException("Load failed.");
}
return new Allocation(id, rs, t);
}
static public Allocation createBitmapRef(RenderScript rs, Bitmap b)
throws IllegalArgumentException {
rs.validate();
Type t = typeFromBitmap(rs, b);
int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
if(id == 0) {
throw new IllegalStateException("Load failed.");
}
Allocation a = new Allocation(id, rs, t);
a.mBitmap = b;
return a;
}
static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
throws IllegalArgumentException {
rs.validate();
int id = rs.nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b);
if(id == 0) {
throw new IllegalStateException("Load failed.");
}
return new Allocation(id, rs, null);
}
static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
throws IllegalArgumentException {
rs.validate();
InputStream is = null;
try {
final TypedValue value = new TypedValue();
is = res.openRawResource(id, value);
int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
asset);
if(allocationId == 0) {
throw new IllegalStateException("Load failed.");
}
return new Allocation(allocationId, rs, null);
} catch (Exception e) {
// Ignore
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// Ignore
}
}
}
return null;
}
static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
throws IllegalArgumentException {
mBitmapOptions.inPreferredConfig = null;
if (dstFmt == rs.mElement_RGBA_8888) {
mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
} else if (dstFmt == rs.mElement_RGB_888) {
mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
} else if (dstFmt == rs.mElement_RGBA_4444) {
mBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_4444;
} else if (dstFmt == rs.mElement_RGB_565) {
mBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
}
Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
return createFromBitmapBoxed(rs, b, dstFmt, genMips);
}
}
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import android.util.Log;
/**
* @hide
*
**/
class BaseObj {
BaseObj(RenderScript rs) {
rs.validate();
mRS = rs;
mID = 0;
mDestroyed = false;
}
public int getID() {
return mID;
}
int mID;
boolean mDestroyed;
String mName;
RenderScript mRS;
public void setName(String s) throws IllegalStateException, IllegalArgumentException
{
if(s.length() < 1) {
throw new IllegalArgumentException("setName does not accept a zero length string.");
}
if(mName != null) {
throw new IllegalArgumentException("setName object already has a name.");
}
try {
byte[] bytes = s.getBytes("UTF-8");
mRS.nAssignName(mID, bytes);
mName = s;
} catch (java.io.UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
protected void finalize() throws Throwable
{
if (!mDestroyed) {
if(mID != 0 && mRS.isAlive()) {
mRS.nObjDestroyOOB(mID);
}
mRS = null;
mID = 0;
mDestroyed = true;
//Log.v(RenderScript.LOG_TAG, getClass() +
// " auto finalizing object without having released the RS reference.");
}
super.finalize();
}
public void destroy() {
if(mDestroyed) {
throw new IllegalStateException("Object already destroyed.");
}
mDestroyed = true;
mRS.nObjDestroy(mID);
}
}
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
/**
* @hide
**/
public enum Dimension {
X (0),
Y (1),
Z (2),
LOD (3),
FACE (4),
ARRAY_0 (100);
int mID;
Dimension(int id) {
mID = id;
}
}
@@ -0,0 +1,422 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.lang.reflect.Field;
/**
* @hide
*
**/
public class Element extends BaseObj {
int mSize;
Element[] mElements;
String[] mElementNames;
DataType mType;
DataKind mKind;
boolean mNormalized;
int mVectorSize;
int getSizeBytes() {return mSize;}
public enum DataType {
//FLOAT_16 (1, 2),
FLOAT_32 (2, 4),
//FLOAT_64 (3, 8),
SIGNED_8 (4, 1),
SIGNED_16 (5, 2),
SIGNED_32 (6, 4),
//SIGNED_64 (7, 8),
UNSIGNED_8 (8, 1),
UNSIGNED_16 (9, 2),
UNSIGNED_32 (10, 4),
//UNSIGNED_64 (11, 8),
UNSIGNED_5_6_5 (12, 2),
UNSIGNED_5_5_5_1 (13, 2),
UNSIGNED_4_4_4_4 (14, 2),
RS_ELEMENT (15, 4),
RS_TYPE (16, 4),
RS_ALLOCATION (17, 4),
RS_SAMPLER (18, 4),
RS_SCRIPT (19, 4),
RS_MESH (20, 4),
RS_PROGRAM_FRAGMENT (21, 4),
RS_PROGRAM_VERTEX (22, 4),
RS_PROGRAM_RASTER (23, 4),
RS_PROGRAM_STORE (24, 4);
int mID;
int mSize;
DataType(int id, int size) {
mID = id;
mSize = size;
}
}
public enum DataKind {
USER (0),
COLOR (1),
POSITION (2),
TEXTURE (3),
NORMAL (4),
INDEX (5),
POINT_SIZE(6),
PIXEL_L (7),
PIXEL_A (8),
PIXEL_LA (9),
PIXEL_RGB (10),
PIXEL_RGBA (11);
int mID;
DataKind(int id) {
mID = id;
}
}
public static Element USER_U8(RenderScript rs) {
if(rs.mElement_USER_U8 == null) {
rs.mElement_USER_U8 = createUser(rs, DataType.UNSIGNED_8);
}
return rs.mElement_USER_U8;
}
public static Element USER_I8(RenderScript rs) {
if(rs.mElement_USER_I8 == null) {
rs.mElement_USER_I8 = createUser(rs, DataType.SIGNED_8);
}
return rs.mElement_USER_I8;
}
public static Element USER_U32(RenderScript rs) {
if(rs.mElement_USER_U32 == null) {
rs.mElement_USER_U32 = createUser(rs, DataType.UNSIGNED_32);
}
return rs.mElement_USER_U32;
}
public static Element USER_I32(RenderScript rs) {
if(rs.mElement_USER_I32 == null) {
rs.mElement_USER_I32 = createUser(rs, DataType.SIGNED_32);
}
return rs.mElement_USER_I32;
}
public static Element USER_F32(RenderScript rs) {
if(rs.mElement_USER_F32 == null) {
rs.mElement_USER_F32 = createUser(rs, DataType.FLOAT_32);
}
return rs.mElement_USER_F32;
}
public static Element A_8(RenderScript rs) {
if(rs.mElement_A_8 == null) {
rs.mElement_A_8 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_A);
}
return rs.mElement_A_8;
}
public static Element RGB_565(RenderScript rs) {
if(rs.mElement_RGB_565 == null) {
rs.mElement_RGB_565 = createPixel(rs, DataType.UNSIGNED_5_6_5, DataKind.PIXEL_RGB);
}
return rs.mElement_RGB_565;
}
public static Element RGB_888(RenderScript rs) {
if(rs.mElement_RGB_888 == null) {
rs.mElement_RGB_888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGB);
}
return rs.mElement_RGB_888;
}
public static Element RGBA_5551(RenderScript rs) {
if(rs.mElement_RGBA_5551 == null) {
rs.mElement_RGBA_5551 = createPixel(rs, DataType.UNSIGNED_5_5_5_1, DataKind.PIXEL_RGBA);
}
return rs.mElement_RGBA_5551;
}
public static Element RGBA_4444(RenderScript rs) {
if(rs.mElement_RGBA_4444 == null) {
rs.mElement_RGBA_4444 = createPixel(rs, DataType.UNSIGNED_4_4_4_4, DataKind.PIXEL_RGBA);
}
return rs.mElement_RGBA_4444;
}
public static Element RGBA_8888(RenderScript rs) {
if(rs.mElement_RGBA_8888 == null) {
rs.mElement_RGBA_8888 = createPixel(rs, DataType.UNSIGNED_8, DataKind.PIXEL_RGBA);
}
return rs.mElement_RGBA_8888;
}
public static Element INDEX_16(RenderScript rs) {
if(rs.mElement_INDEX_16 == null) {
rs.mElement_INDEX_16 = createIndex(rs);
}
return rs.mElement_INDEX_16;
}
public static Element ATTRIB_POSITION_2(RenderScript rs) {
if(rs.mElement_POSITION_2 == null) {
rs.mElement_POSITION_2 = createAttrib(rs, DataType.FLOAT_32, DataKind.POSITION, 2);
}
return rs.mElement_POSITION_2;
}
public static Element ATTRIB_POSITION_3(RenderScript rs) {
if(rs.mElement_POSITION_3 == null) {
rs.mElement_POSITION_3 = createAttrib(rs, DataType.FLOAT_32, DataKind.POSITION, 3);
}
return rs.mElement_POSITION_3;
}
public static Element ATTRIB_TEXTURE_2(RenderScript rs) {
if(rs.mElement_TEXTURE_2 == null) {
rs.mElement_TEXTURE_2 = createAttrib(rs, DataType.FLOAT_32, DataKind.TEXTURE, 2);
}
return rs.mElement_TEXTURE_2;
}
public static Element ATTRIB_NORMAL_3(RenderScript rs) {
if(rs.mElement_NORMAL_3 == null) {
rs.mElement_NORMAL_3 = createAttrib(rs, DataType.FLOAT_32, DataKind.NORMAL, 3);
}
return rs.mElement_NORMAL_3;
}
public static Element ATTRIB_COLOR_U8_4(RenderScript rs) {
if(rs.mElement_COLOR_U8_4 == null) {
rs.mElement_COLOR_U8_4 = createAttrib(rs, DataType.UNSIGNED_8, DataKind.COLOR, 4);
}
return rs.mElement_COLOR_U8_4;
}
public static Element ATTRIB_COLOR_F32_4(RenderScript rs) {
if(rs.mElement_COLOR_F32_4 == null) {
rs.mElement_COLOR_F32_4 = createAttrib(rs, DataType.FLOAT_32, DataKind.COLOR, 4);
}
return rs.mElement_COLOR_F32_4;
}
Element(RenderScript rs, Element[] e, String[] n) {
super(rs);
mSize = 0;
mElements = e;
mElementNames = n;
int[] ids = new int[mElements.length];
for (int ct = 0; ct < mElements.length; ct++ ) {
mSize += mElements[ct].mSize;
ids[ct] = mElements[ct].mID;
}
mID = rs.nElementCreate2(ids, mElementNames);
}
Element(RenderScript rs, DataType dt, DataKind dk, boolean norm, int size) {
super(rs);
mSize = dt.mSize * size;
mType = dt;
mKind = dk;
mNormalized = norm;
mVectorSize = size;
mID = rs.nElementCreate(dt.mID, dk.mID, norm, size);
}
public void destroy() throws IllegalStateException {
super.destroy();
}
public static Element createFromClass(RenderScript rs, Class c) {
rs.validate();
Field[] fields = c.getFields();
Builder b = new Builder(rs);
for(Field f: fields) {
Class fc = f.getType();
if(fc == int.class) {
b.add(createUser(rs, DataType.SIGNED_32), f.getName());
} else if(fc == short.class) {
b.add(createUser(rs, DataType.SIGNED_16), f.getName());
} else if(fc == byte.class) {
b.add(createUser(rs, DataType.SIGNED_8), f.getName());
} else if(fc == float.class) {
b.add(createUser(rs, DataType.FLOAT_32), f.getName());
} else {
throw new IllegalArgumentException("Unkown field type");
}
}
return b.create();
}
/////////////////////////////////////////
public static Element createUser(RenderScript rs, DataType dt) {
return new Element(rs, dt, DataKind.USER, false, 1);
}
public static Element createVector(RenderScript rs, DataType dt, int size) {
if (size < 2 || size > 4) {
throw new IllegalArgumentException("Bad size");
}
return new Element(rs, dt, DataKind.USER, false, size);
}
public static Element createIndex(RenderScript rs) {
return new Element(rs, DataType.UNSIGNED_16, DataKind.INDEX, false, 1);
}
public static Element createAttrib(RenderScript rs, DataType dt, DataKind dk, int size) {
if (!(dt == DataType.FLOAT_32 ||
dt == DataType.UNSIGNED_8 ||
dt == DataType.UNSIGNED_16 ||
dt == DataType.UNSIGNED_32 ||
dt == DataType.SIGNED_8 ||
dt == DataType.SIGNED_16 ||
dt == DataType.SIGNED_32)) {
throw new IllegalArgumentException("Unsupported DataType");
}
if (!(dk == DataKind.COLOR ||
dk == DataKind.POSITION ||
dk == DataKind.TEXTURE ||
dk == DataKind.NORMAL ||
dk == DataKind.POINT_SIZE ||
dk == DataKind.USER)) {
throw new IllegalArgumentException("Unsupported DataKind");
}
if (dk == DataKind.COLOR &&
((dt != DataType.FLOAT_32 && dt != DataType.UNSIGNED_8) ||
size < 3 || size > 4)) {
throw new IllegalArgumentException("Bad combo");
}
if (dk == DataKind.POSITION && (size < 1 || size > 4)) {
throw new IllegalArgumentException("Bad combo");
}
if (dk == DataKind.TEXTURE &&
(dt != DataType.FLOAT_32 || size < 1 || size > 4)) {
throw new IllegalArgumentException("Bad combo");
}
if (dk == DataKind.NORMAL &&
(dt != DataType.FLOAT_32 || size != 3)) {
throw new IllegalArgumentException("Bad combo");
}
if (dk == DataKind.POINT_SIZE &&
(dt != DataType.FLOAT_32 || size != 1)) {
throw new IllegalArgumentException("Bad combo");
}
boolean norm = false;
if (dk == DataKind.COLOR && dt == DataType.UNSIGNED_8) {
norm = true;
}
return new Element(rs, dt, dk, norm, size);
}
public static Element createPixel(RenderScript rs, DataType dt, DataKind dk) {
if (!(dk == DataKind.PIXEL_L ||
dk == DataKind.PIXEL_A ||
dk == DataKind.PIXEL_LA ||
dk == DataKind.PIXEL_RGB ||
dk == DataKind.PIXEL_RGBA)) {
throw new IllegalArgumentException("Unsupported DataKind");
}
if (!(dt == DataType.UNSIGNED_8 ||
dt == DataType.UNSIGNED_5_6_5 ||
dt == DataType.UNSIGNED_4_4_4_4 ||
dt == DataType.UNSIGNED_5_5_5_1)) {
throw new IllegalArgumentException("Unsupported DataType");
}
if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
throw new IllegalArgumentException("Bad kind and type combo");
}
if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
throw new IllegalArgumentException("Bad kind and type combo");
}
if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
throw new IllegalArgumentException("Bad kind and type combo");
}
int size = 1;
if (dk == DataKind.PIXEL_LA) {
size = 2;
}
if (dk == DataKind.PIXEL_RGB) {
size = 3;
}
if (dk == DataKind.PIXEL_RGBA) {
size = 4;
}
return new Element(rs, dt, dk, true, size);
}
public static class Builder {
RenderScript mRS;
Element[] mElements;
String[] mElementNames;
int mCount;
public Builder(RenderScript rs) {
mRS = rs;
mCount = 0;
mElements = new Element[8];
mElementNames = new String[8];
}
public void add(Element element, String name) {
if(mCount == mElements.length) {
Element[] e = new Element[mCount + 8];
String[] s = new String[mCount + 8];
System.arraycopy(mElements, 0, e, 0, mCount);
System.arraycopy(mElementNames, 0, s, 0, mCount);
mElements = e;
mElementNames = s;
}
mElements[mCount] = element;
mElementNames[mCount] = name;
mCount++;
}
public Element create() {
mRS.validate();
Element[] ein = new Element[mCount];
String[] sin = new String[mCount];
java.lang.System.arraycopy(mElements, 0, ein, 0, mCount);
java.lang.System.arraycopy(mElementNames, 0, sin, 0, mCount);
return new Element(mRS, ein, sin);
}
}
static void initPredefined(RenderScript rs) {
int a8 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
DataKind.PIXEL_A.mID, true, 1);
int rgba4444 = rs.nElementCreate(DataType.UNSIGNED_4_4_4_4.mID,
DataKind.PIXEL_RGBA.mID, true, 4);
int rgba8888 = rs.nElementCreate(DataType.UNSIGNED_8.mID,
DataKind.PIXEL_RGBA.mID, true, 4);
int rgb565 = rs.nElementCreate(DataType.UNSIGNED_5_6_5.mID,
DataKind.PIXEL_RGB.mID, true, 3);
rs.nInitElements(a8, rgba4444, rgba8888, rgb565);
}
}
@@ -0,0 +1,129 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
/**
* @hide
*
**/
public class FieldPacker {
public FieldPacker(int len) {
mPos = 0;
mData = new byte[len];
}
public void align(int v) {
while ((mPos & (v - 1)) != 0) {
mData[mPos++] = 0;
}
}
void reset() {
mPos = 0;
}
void addI8(byte v) {
mData[mPos++] = v;
}
void addI16(short v) {
align(2);
mData[mPos++] = (byte)(v & 0xff);
mData[mPos++] = (byte)(v >> 8);
}
void addI32(int v) {
align(4);
mData[mPos++] = (byte)(v & 0xff);
mData[mPos++] = (byte)((v >> 8) & 0xff);
mData[mPos++] = (byte)((v >> 16) & 0xff);
mData[mPos++] = (byte)((v >> 24) & 0xff);
}
void addI64(long v) {
align(8);
mData[mPos++] = (byte)(v & 0xff);
mData[mPos++] = (byte)((v >> 8) & 0xff);
mData[mPos++] = (byte)((v >> 16) & 0xff);
mData[mPos++] = (byte)((v >> 24) & 0xff);
mData[mPos++] = (byte)((v >> 32) & 0xff);
mData[mPos++] = (byte)((v >> 40) & 0xff);
mData[mPos++] = (byte)((v >> 48) & 0xff);
mData[mPos++] = (byte)((v >> 56) & 0xff);
}
void addU8(short v) {
if ((v < 0) || (v > 0xff)) {
throw new IllegalArgumentException("Saving value out of range for type");
}
mData[mPos++] = (byte)v;
}
void addU16(int v) {
if ((v < 0) || (v > 0xffff)) {
throw new IllegalArgumentException("Saving value out of range for type");
}
align(2);
mData[mPos++] = (byte)(v & 0xff);
mData[mPos++] = (byte)(v >> 8);
}
void addU32(long v) {
if ((v < 0) || (v > 0xffffffff)) {
throw new IllegalArgumentException("Saving value out of range for type");
}
align(4);
mData[mPos++] = (byte)(v & 0xff);
mData[mPos++] = (byte)((v >> 8) & 0xff);
mData[mPos++] = (byte)((v >> 16) & 0xff);
mData[mPos++] = (byte)((v >> 24) & 0xff);
}
void addU64(long v) {
if (v < 0) {
throw new IllegalArgumentException("Saving value out of range for type");
}
align(8);
mData[mPos++] = (byte)(v & 0xff);
mData[mPos++] = (byte)((v >> 8) & 0xff);
mData[mPos++] = (byte)((v >> 16) & 0xff);
mData[mPos++] = (byte)((v >> 24) & 0xff);
mData[mPos++] = (byte)((v >> 32) & 0xff);
mData[mPos++] = (byte)((v >> 40) & 0xff);
mData[mPos++] = (byte)((v >> 48) & 0xff);
mData[mPos++] = (byte)((v >> 56) & 0xff);
}
void addF32(float v) {
addI32(Float.floatToRawIntBits(v));
}
void addF64(float v) {
addI64(Double.doubleToRawLongBits(v));
}
final byte[] getData() {
return mData;
}
private final byte mData[];
private int mPos;
}
@@ -0,0 +1,76 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import android.util.Config;
import android.util.Log;
/**
* @hide
*
**/
public class Light extends BaseObj {
Light(int id, RenderScript rs) {
super(rs);
mID = id;
}
public void setColor(float r, float g, float b) {
mRS.validate();
mRS.nLightSetColor(mID, r, g, b);
}
public void setPosition(float x, float y, float z) {
mRS.validate();
mRS.nLightSetPosition(mID, x, y, z);
}
public static class Builder {
RenderScript mRS;
boolean mIsMono;
boolean mIsLocal;
public Builder(RenderScript rs) {
mRS = rs;
mIsMono = false;
mIsLocal = false;
}
public void lightSetIsMono(boolean isMono) {
mIsMono = isMono;
}
public void lightSetIsLocal(boolean isLocal) {
mIsLocal = isLocal;
}
static synchronized Light internalCreate(RenderScript rs, Builder b) {
rs.nSamplerBegin();
rs.nLightSetIsMono(b.mIsMono);
rs.nLightSetIsLocal(b.mIsLocal);
int id = rs.nLightCreate();
return new Light(id, rs);
}
public Light create() {
mRS.validate();
return internalCreate(mRS, this);
}
}
}
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.lang.Math;
import android.util.Log;
/**
* @hide
*
**/
public class Matrix2f {
public Matrix2f() {
mMat = new float[4];
loadIdentity();
}
public float get(int i, int j) {
return mMat[i*2 + j];
}
public void set(int i, int j, float v) {
mMat[i*2 + j] = v;
}
public void loadIdentity() {
mMat[0] = 1;
mMat[1] = 0;
mMat[2] = 0;
mMat[3] = 1;
}
public void load(Matrix2f src) {
System.arraycopy(mMat, 0, src, 0, 4);
}
final float[] mMat;
}
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.lang.Math;
import android.util.Log;
/**
* @hide
*
**/
public class Matrix3f {
public Matrix3f() {
mMat = new float[9];
loadIdentity();
}
public float get(int i, int j) {
return mMat[i*3 + j];
}
public void set(int i, int j, float v) {
mMat[i*3 + j] = v;
}
public void loadIdentity() {
mMat[0] = 1;
mMat[1] = 0;
mMat[2] = 0;
mMat[3] = 0;
mMat[4] = 1;
mMat[5] = 0;
mMat[6] = 0;
mMat[7] = 0;
mMat[8] = 1;
}
public void load(Matrix3f src) {
System.arraycopy(mMat, 0, src, 0, 9);
}
final float[] mMat;
}
@@ -0,0 +1,189 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.lang.Math;
import android.util.Log;
/**
* @hide
*
**/
public class Matrix4f {
public Matrix4f() {
mMat = new float[16];
loadIdentity();
}
public float get(int i, int j) {
return mMat[i*4 + j];
}
public void set(int i, int j, float v) {
mMat[i*4 + j] = v;
}
public void loadIdentity() {
mMat[0] = 1;
mMat[1] = 0;
mMat[2] = 0;
mMat[3] = 0;
mMat[4] = 0;
mMat[5] = 1;
mMat[6] = 0;
mMat[7] = 0;
mMat[8] = 0;
mMat[9] = 0;
mMat[10] = 1;
mMat[11] = 0;
mMat[12] = 0;
mMat[13] = 0;
mMat[14] = 0;
mMat[15] = 1;
}
public void load(Matrix4f src) {
System.arraycopy(mMat, 0, src, 0, 16);
}
public void loadRotate(float rot, float x, float y, float z) {
float c, s;
mMat[3] = 0;
mMat[7] = 0;
mMat[11]= 0;
mMat[12]= 0;
mMat[13]= 0;
mMat[14]= 0;
mMat[15]= 1;
rot *= (float)(java.lang.Math.PI / 180.0f);
c = (float)java.lang.Math.cos(rot);
s = (float)java.lang.Math.sin(rot);
float len = (float)java.lang.Math.sqrt(x*x + y*y + z*z);
if (!(len != 1)) {
float recipLen = 1.f / len;
x *= recipLen;
y *= recipLen;
z *= recipLen;
}
float nc = 1.0f - c;
float xy = x * y;
float yz = y * z;
float zx = z * x;
float xs = x * s;
float ys = y * s;
float zs = z * s;
mMat[ 0] = x*x*nc + c;
mMat[ 4] = xy*nc - zs;
mMat[ 8] = zx*nc + ys;
mMat[ 1] = xy*nc + zs;
mMat[ 5] = y*y*nc + c;
mMat[ 9] = yz*nc - xs;
mMat[ 2] = zx*nc - ys;
mMat[ 6] = yz*nc + xs;
mMat[10] = z*z*nc + c;
}
public void loadScale(float x, float y, float z) {
loadIdentity();
mMat[0] = x;
mMat[5] = y;
mMat[10] = z;
}
public void loadTranslate(float x, float y, float z) {
loadIdentity();
mMat[12] = x;
mMat[13] = y;
mMat[14] = z;
}
public void loadMultiply(Matrix4f lhs, Matrix4f rhs) {
for (int i=0 ; i<4 ; i++) {
float ri0 = 0;
float ri1 = 0;
float ri2 = 0;
float ri3 = 0;
for (int j=0 ; j<4 ; j++) {
float rhs_ij = rhs.get(i,j);
ri0 += lhs.get(j,0) * rhs_ij;
ri1 += lhs.get(j,1) * rhs_ij;
ri2 += lhs.get(j,2) * rhs_ij;
ri3 += lhs.get(j,3) * rhs_ij;
}
set(i,0, ri0);
set(i,1, ri1);
set(i,2, ri2);
set(i,3, ri3);
}
}
public void loadOrtho(float l, float r, float b, float t, float n, float f) {
loadIdentity();
mMat[0] = 2 / (r - l);
mMat[5] = 2 / (t - b);
mMat[10]= -2 / (f - n);
mMat[12]= -(r + l) / (r - l);
mMat[13]= -(t + b) / (t - b);
mMat[14]= -(f + n) / (f - n);
}
public void loadFrustum(float l, float r, float b, float t, float n, float f) {
loadIdentity();
mMat[0] = 2 * n / (r - l);
mMat[5] = 2 * n / (t - b);
mMat[8] = (r + l) / (r - l);
mMat[9] = (t + b) / (t - b);
mMat[10]= -(f + n) / (f - n);
mMat[11]= -1;
mMat[14]= -2*f*n / (f - n);
mMat[15]= 0;
}
public void multiply(Matrix4f rhs) {
Matrix4f tmp = new Matrix4f();
tmp.loadMultiply(this, rhs);
load(tmp);
}
public void rotate(float rot, float x, float y, float z) {
Matrix4f tmp = new Matrix4f();
tmp.loadRotate(rot, x, y, z);
multiply(tmp);
}
public void scale(float x, float y, float z) {
Matrix4f tmp = new Matrix4f();
tmp.loadScale(x, y, z);
multiply(tmp);
}
public void translate(float x, float y, float z) {
Matrix4f tmp = new Matrix4f();
tmp.loadTranslate(x, y, z);
multiply(tmp);
}
final float[] mMat;
}
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
/**
* @hide
**/
public enum Primitive {
POINT (0),
LINE (1),
LINE_STRIP (2),
TRIANGLE (3),
TRIANGLE_STRIP (4),
TRIANGLE_FAN (5);
int mID;
Primitive(int id) {
mID = id;
}
}
@@ -0,0 +1,144 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import android.util.Config;
import android.util.Log;
/**
* @hide
*
**/
public class Program extends BaseObj {
public static final int MAX_INPUT = 8;
public static final int MAX_OUTPUT = 8;
public static final int MAX_CONSTANT = 8;
public static final int MAX_TEXTURE = 8;
Element mInputs[];
Element mOutputs[];
Type mConstants[];
int mTextureCount;
String mShader;
Program(int id, RenderScript rs) {
super(rs);
mID = id;
}
public void bindConstants(Allocation a, int slot) {
mRS.nProgramBindConstants(mID, slot, a.mID);
}
public void bindTexture(Allocation va, int slot)
throws IllegalArgumentException {
mRS.validate();
if((slot < 0) || (slot >= mTextureCount)) {
throw new IllegalArgumentException("Slot ID out of range.");
}
mRS.nProgramBindTexture(mID, slot, va.mID);
}
public void bindSampler(Sampler vs, int slot)
throws IllegalArgumentException {
mRS.validate();
if((slot < 0) || (slot >= mTextureCount)) {
throw new IllegalArgumentException("Slot ID out of range.");
}
mRS.nProgramBindSampler(mID, slot, vs.mID);
}
public static class BaseProgramBuilder {
RenderScript mRS;
Element mInputs[];
Element mOutputs[];
Type mConstants[];
Type mTextures[];
int mInputCount;
int mOutputCount;
int mConstantCount;
int mTextureCount;
String mShader;
protected BaseProgramBuilder(RenderScript rs) {
mRS = rs;
mInputs = new Element[MAX_INPUT];
mOutputs = new Element[MAX_OUTPUT];
mConstants = new Type[MAX_CONSTANT];
mInputCount = 0;
mOutputCount = 0;
mConstantCount = 0;
mTextureCount = 0;
}
public void setShader(String s) {
mShader = s;
}
public void addInput(Element e) throws IllegalStateException {
// Should check for consistant and non-conflicting names...
if(mInputCount >= MAX_INPUT) {
throw new IllegalArgumentException("Max input count exceeded.");
}
mInputs[mInputCount++] = e;
}
public void addOutput(Element e) throws IllegalStateException {
// Should check for consistant and non-conflicting names...
if(mOutputCount >= MAX_OUTPUT) {
throw new IllegalArgumentException("Max output count exceeded.");
}
mOutputs[mOutputCount++] = e;
}
public int addConstant(Type t) throws IllegalStateException {
// Should check for consistant and non-conflicting names...
if(mConstantCount >= MAX_CONSTANT) {
throw new IllegalArgumentException("Max input count exceeded.");
}
mConstants[mConstantCount] = t;
return mConstantCount++;
}
public void setTextureCount(int count) throws IllegalArgumentException {
// Should check for consistant and non-conflicting names...
if(count >= MAX_CONSTANT) {
throw new IllegalArgumentException("Max texture count exceeded.");
}
mTextureCount = count;
}
protected void initProgram(Program p) {
p.mInputs = new Element[mInputCount];
System.arraycopy(mInputs, 0, p.mInputs, 0, mInputCount);
p.mOutputs = new Element[mOutputCount];
System.arraycopy(mOutputs, 0, p.mOutputs, 0, mOutputCount);
p.mConstants = new Type[mConstantCount];
System.arraycopy(mConstants, 0, p.mConstants, 0, mConstantCount);
p.mTextureCount = mTextureCount;
}
}
}
@@ -0,0 +1,142 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import android.util.Config;
import android.util.Log;
/**
* @hide
*
**/
public class ProgramFragment extends Program {
ProgramFragment(int id, RenderScript rs) {
super(id, rs);
}
public static class ShaderBuilder extends BaseProgramBuilder {
public ShaderBuilder(RenderScript rs) {
super(rs);
}
public ProgramFragment create() {
mRS.validate();
int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount + 1) * 2];
int idx = 0;
for (int i=0; i < mInputCount; i++) {
tmp[idx++] = 0;
tmp[idx++] = mInputs[i].mID;
}
for (int i=0; i < mOutputCount; i++) {
tmp[idx++] = 1;
tmp[idx++] = mOutputs[i].mID;
}
for (int i=0; i < mConstantCount; i++) {
tmp[idx++] = 2;
tmp[idx++] = mConstants[i].mID;
}
tmp[idx++] = 3;
tmp[idx++] = mTextureCount;
int id = mRS.nProgramFragmentCreate2(mShader, tmp);
ProgramFragment pf = new ProgramFragment(id, mRS);
initProgram(pf);
return pf;
}
}
public static class Builder {
public static final int MAX_TEXTURE = 2;
RenderScript mRS;
boolean mPointSpriteEnable;
public enum EnvMode {
REPLACE (1),
MODULATE (2),
DECAL (3);
int mID;
EnvMode(int id) {
mID = id;
}
}
public enum Format {
ALPHA (1),
LUMINANCE_ALPHA (2),
RGB (3),
RGBA (4);
int mID;
Format(int id) {
mID = id;
}
}
private class Slot {
EnvMode env;
Format format;
Slot(EnvMode _env, Format _fmt) {
env = _env;
format = _fmt;
}
}
Slot[] mSlots;
public Builder(RenderScript rs) {
mRS = rs;
mSlots = new Slot[MAX_TEXTURE];
mPointSpriteEnable = false;
}
public void setTexture(EnvMode env, Format fmt, int slot)
throws IllegalArgumentException {
if((slot < 0) || (slot >= MAX_TEXTURE)) {
throw new IllegalArgumentException("MAX_TEXTURE exceeded.");
}
mSlots[slot] = new Slot(env, fmt);
}
public void setPointSpriteTexCoordinateReplacement(boolean enable) {
mPointSpriteEnable = enable;
}
public ProgramFragment create() {
mRS.validate();
int[] tmp = new int[MAX_TEXTURE * 2 + 1];
if (mSlots[0] != null) {
tmp[0] = mSlots[0].env.mID;
tmp[1] = mSlots[0].format.mID;
}
if (mSlots[1] != null) {
tmp[2] = mSlots[1].env.mID;
tmp[3] = mSlots[1].format.mID;
}
tmp[4] = mPointSpriteEnable ? 1 : 0;
int id = mRS.nProgramFragmentCreate(tmp);
ProgramFragment pf = new ProgramFragment(id, mRS);
pf.mTextureCount = MAX_TEXTURE;
return pf;
}
}
}
@@ -0,0 +1,113 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import android.util.Config;
import android.util.Log;
/**
* @hide
*
**/
public class ProgramRaster extends BaseObj {
boolean mPointSmooth;
boolean mLineSmooth;
boolean mPointSprite;
float mPointSize;
float mLineWidth;
Element mIn;
Element mOut;
ProgramRaster(int id, RenderScript rs) {
super(rs);
mID = id;
mPointSize = 1.0f;
mLineWidth = 1.0f;
mPointSmooth = false;
mLineSmooth = false;
mPointSprite = false;
}
public void setLineWidth(float w) {
mRS.validate();
mLineWidth = w;
mRS.nProgramRasterSetLineWidth(mID, w);
}
public void setPointSize(float s) {
mRS.validate();
mPointSize = s;
mRS.nProgramRasterSetPointSize(mID, s);
}
void internalInit() {
int inID = 0;
int outID = 0;
if (mIn != null) {
inID = mIn.mID;
}
if (mOut != null) {
outID = mOut.mID;
}
mID = mRS.nProgramRasterCreate(inID, outID, mPointSmooth, mLineSmooth, mPointSprite);
}
public static class Builder {
RenderScript mRS;
ProgramRaster mPR;
public Builder(RenderScript rs, Element in, Element out) {
mRS = rs;
mPR = new ProgramRaster(0, rs);
}
public void setPointSpriteEnable(boolean enable) {
mPR.mPointSprite = enable;
}
public void setPointSmoothEnable(boolean enable) {
mPR.mPointSmooth = enable;
}
public void setLineSmoothEnable(boolean enable) {
mPR.mLineSmooth = enable;
}
static synchronized ProgramRaster internalCreate(RenderScript rs, Builder b) {
b.mPR.internalInit();
ProgramRaster pr = b.mPR;
b.mPR = new ProgramRaster(0, b.mRS);
return pr;
}
public ProgramRaster create() {
mRS.validate();
return internalCreate(mRS, this);
}
}
}
@@ -0,0 +1,174 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import android.util.Config;
import android.util.Log;
/**
* @hide
*
**/
public class ProgramStore extends BaseObj {
public enum DepthFunc {
ALWAYS (0),
LESS (1),
LEQUAL (2),
GREATER (3),
GEQUAL (4),
EQUAL (5),
NOTEQUAL (6);
int mID;
DepthFunc(int id) {
mID = id;
}
}
public enum BlendSrcFunc {
ZERO (0),
ONE (1),
DST_COLOR (2),
ONE_MINUS_DST_COLOR (3),
SRC_ALPHA (4),
ONE_MINUS_SRC_ALPHA (5),
DST_ALPHA (6),
ONE_MINUS_DST_ALPHA (7),
SRC_ALPHA_SATURATE (8);
int mID;
BlendSrcFunc(int id) {
mID = id;
}
}
public enum BlendDstFunc {
ZERO (0),
ONE (1),
SRC_COLOR (2),
ONE_MINUS_SRC_COLOR (3),
SRC_ALPHA (4),
ONE_MINUS_SRC_ALPHA (5),
DST_ALPHA (6),
ONE_MINUS_DST_ALPHA (7);
int mID;
BlendDstFunc(int id) {
mID = id;
}
}
ProgramStore(int id, RenderScript rs) {
super(rs);
mID = id;
}
public static class Builder {
RenderScript mRS;
Element mIn;
Element mOut;
DepthFunc mDepthFunc;
boolean mDepthMask;
boolean mColorMaskR;
boolean mColorMaskG;
boolean mColorMaskB;
boolean mColorMaskA;
BlendSrcFunc mBlendSrc;
BlendDstFunc mBlendDst;
boolean mDither;
public Builder(RenderScript rs, Element in, Element out) {
mRS = rs;
mIn = in;
mOut = out;
mDepthFunc = DepthFunc.ALWAYS;
mDepthMask = false;
mColorMaskR = true;
mColorMaskG = true;
mColorMaskB = true;
mColorMaskA = true;
mBlendSrc = BlendSrcFunc.ONE;
mBlendDst = BlendDstFunc.ZERO;
}
public void setDepthFunc(DepthFunc func) {
mDepthFunc = func;
}
public void setDepthMask(boolean enable) {
mDepthMask = enable;
}
public void setColorMask(boolean r, boolean g, boolean b, boolean a) {
mColorMaskR = r;
mColorMaskG = g;
mColorMaskB = b;
mColorMaskA = a;
}
public void setBlendFunc(BlendSrcFunc src, BlendDstFunc dst) {
mBlendSrc = src;
mBlendDst = dst;
}
public void setDitherEnable(boolean enable) {
mDither = enable;
}
static synchronized ProgramStore internalCreate(RenderScript rs, Builder b) {
int inID = 0;
int outID = 0;
if (b.mIn != null) {
inID = b.mIn.mID;
}
if (b.mOut != null) {
outID = b.mOut.mID;
}
rs.nProgramFragmentStoreBegin(inID, outID);
rs.nProgramFragmentStoreDepthFunc(b.mDepthFunc.mID);
rs.nProgramFragmentStoreDepthMask(b.mDepthMask);
rs.nProgramFragmentStoreColorMask(b.mColorMaskR,
b.mColorMaskG,
b.mColorMaskB,
b.mColorMaskA);
rs.nProgramFragmentStoreBlendFunc(b.mBlendSrc.mID, b.mBlendDst.mID);
rs.nProgramFragmentStoreDither(b.mDither);
int id = rs.nProgramFragmentStoreCreate();
return new ProgramStore(id, rs);
}
public ProgramStore create() {
mRS.validate();
return internalCreate(mRS, this);
}
}
}
@@ -0,0 +1,182 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import android.util.Config;
import android.util.Log;
/**
* @hide
*
**/
public class ProgramVertex extends Program {
public static final int MAX_LIGHT = 8;
ProgramVertex(int id, RenderScript rs) {
super(id, rs);
}
public void bindAllocation(MatrixAllocation va) {
mRS.validate();
bindConstants(va.mAlloc, 0);
}
public static class Builder {
RenderScript mRS;
boolean mTextureMatrixEnable;
public Builder(RenderScript rs, Element in, Element out) {
mRS = rs;
}
public void setTextureMatrixEnable(boolean enable) {
mTextureMatrixEnable = enable;
}
public ProgramVertex create() {
int id = mRS.nProgramVertexCreate(mTextureMatrixEnable);
return new ProgramVertex(id, mRS);
}
}
public static class ShaderBuilder extends BaseProgramBuilder {
public ShaderBuilder(RenderScript rs) {
super(rs);
}
public ProgramVertex create() {
mRS.validate();
int[] tmp = new int[(mInputCount + mOutputCount + mConstantCount +1) * 2];
int idx = 0;
for (int i=0; i < mInputCount; i++) {
tmp[idx++] = 0;
tmp[idx++] = mInputs[i].mID;
}
for (int i=0; i < mOutputCount; i++) {
tmp[idx++] = 1;
tmp[idx++] = mOutputs[i].mID;
}
for (int i=0; i < mConstantCount; i++) {
tmp[idx++] = 2;
tmp[idx++] = mConstants[i].mID;
}
tmp[idx++] = 3;
tmp[idx++] = mTextureCount;
int id = mRS.nProgramVertexCreate2(mShader, tmp);
ProgramVertex pv = new ProgramVertex(id, mRS);
initProgram(pv);
return pv;
}
}
public static class MatrixAllocation {
static final int MODELVIEW_OFFSET = 0;
static final int PROJECTION_OFFSET = 16;
static final int TEXTURE_OFFSET = 32;
Matrix4f mModel;
Matrix4f mProjection;
Matrix4f mTexture;
public Allocation mAlloc;
public MatrixAllocation(RenderScript rs) {
mModel = new Matrix4f();
mProjection = new Matrix4f();
mTexture = new Matrix4f();
mAlloc = Allocation.createSized(rs, Element.createUser(rs, Element.DataType.FLOAT_32), 48);
mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat);
mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat);
}
public void destroy() {
mAlloc.destroy();
mAlloc = null;
}
public void loadModelview(Matrix4f m) {
mModel = m;
mAlloc.subData1D(MODELVIEW_OFFSET, 16, m.mMat);
}
public void loadProjection(Matrix4f m) {
mProjection = m;
mAlloc.subData1D(PROJECTION_OFFSET, 16, m.mMat);
}
public void loadTexture(Matrix4f m) {
mTexture = m;
mAlloc.subData1D(TEXTURE_OFFSET, 16, m.mMat);
}
public void setupOrthoWindow(int w, int h) {
mProjection.loadOrtho(0,w, h,0, -1,1);
mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
}
public void setupOrthoNormalized(int w, int h) {
// range -1,1 in the narrow axis.
if(w > h) {
float aspect = ((float)w) / h;
mProjection.loadOrtho(-aspect,aspect, -1,1, -1,1);
} else {
float aspect = ((float)h) / w;
mProjection.loadOrtho(-1,1, -aspect,aspect, -1,1);
}
mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
}
public void setupProjectionNormalized(int w, int h) {
// range -1,1 in the narrow axis at z = 0.
Matrix4f m1 = new Matrix4f();
Matrix4f m2 = new Matrix4f();
if(w > h) {
float aspect = ((float)w) / h;
m1.loadFrustum(-aspect,aspect, -1,1, 1,100);
} else {
float aspect = ((float)h) / w;
m1.loadFrustum(-1,1, -aspect,aspect, 1,100);
}
m2.loadRotate(180, 0, 1, 0);
m1.loadMultiply(m1, m2);
m2.loadScale(-2, 2, 1);
m1.loadMultiply(m1, m2);
m2.loadTranslate(0, 0, 2);
m1.loadMultiply(m1, m2);
mProjection = m1;
mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
}
}
}
@@ -0,0 +1,169 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.io.Writer;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* @hide
*
**/
public class RSSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mSurfaceHolder;
private RenderScriptGL mRS;
/**
* Standard View constructor. In order to render something, you
* must call {@link #setRenderer} to register a renderer.
*/
public RSSurfaceView(Context context) {
super(context);
init();
//Log.v(RenderScript.LOG_TAG, "RSSurfaceView");
}
/**
* Standard View constructor. In order to render something, you
* must call {@link #setRenderer} to register a renderer.
*/
public RSSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
//Log.v(RenderScript.LOG_TAG, "RSSurfaceView");
}
private void init() {
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed
SurfaceHolder holder = getHolder();
holder.addCallback(this);
}
/**
* This method is part of the SurfaceHolder.Callback interface, and is
* not normally called or subclassed by clients of RSSurfaceView.
*/
public void surfaceCreated(SurfaceHolder holder) {
Log.v(RenderScript.LOG_TAG, "surfaceCreated");
mSurfaceHolder = holder;
}
/**
* This method is part of the SurfaceHolder.Callback interface, and is
* not normally called or subclassed by clients of RSSurfaceView.
*/
public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return
Log.v(RenderScript.LOG_TAG, "surfaceDestroyed");
if (mRS != null) {
mRS.contextSetSurface(0, 0, null);
}
}
/**
* This method is part of the SurfaceHolder.Callback interface, and is
* not normally called or subclassed by clients of RSSurfaceView.
*/
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Log.v(RenderScript.LOG_TAG, "surfaceChanged");
if (mRS != null) {
mRS.contextSetSurface(w, h, holder.getSurface());
}
}
/**
* Inform the view that the activity is paused. The owner of this view must
* call this method when the activity is paused. Calling this method will
* pause the rendering thread.
* Must not be called before a renderer has been set.
*/
public void onPause() {
if(mRS != null) {
mRS.pause();
}
//Log.v(RenderScript.LOG_TAG, "onPause");
}
/**
* Inform the view that the activity is resumed. The owner of this view must
* call this method when the activity is resumed. Calling this method will
* recreate the OpenGL display and resume the rendering
* thread.
* Must not be called before a renderer has been set.
*/
public void onResume() {
if(mRS != null) {
mRS.resume();
}
//Log.v(RenderScript.LOG_TAG, "onResume");
}
/**
* Queue a runnable to be run on the GL rendering thread. This can be used
* to communicate with the Renderer on the rendering thread.
* Must not be called before a renderer has been set.
* @param r the runnable to be run on the GL rendering thread.
*/
public void queueEvent(Runnable r) {
//Log.v(RenderScript.LOG_TAG, "queueEvent");
}
/**
* This method is used as part of the View class and is not normally
* called or subclassed by clients of RSSurfaceView.
* Must not be called before a renderer has been set.
*/
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
}
// ----------------------------------------------------------------------
public RenderScriptGL createRenderScript(boolean useDepth, boolean forceSW) {
Log.v(RenderScript.LOG_TAG, "createRenderScript");
mRS = new RenderScriptGL(useDepth, forceSW);
return mRS;
}
public RenderScriptGL createRenderScript(boolean useDepth) {
return createRenderScript(useDepth, false);
}
public void destroyRenderScript() {
Log.v(RenderScript.LOG_TAG, "destroyRenderScript");
mRS.destroy();
mRS = null;
}
public void createRenderScript(RenderScriptGL rs) {
mRS = rs;
}
}
@@ -0,0 +1,337 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.lang.reflect.Field;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Config;
import android.util.Log;
import android.view.Surface;
/**
* @hide
*
**/
public class RenderScript {
static final String LOG_TAG = "RenderScript_jni";
protected static final boolean DEBUG = false;
@SuppressWarnings({"UnusedDeclaration", "deprecation"})
protected static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
/*
* We use a class initializer to allow the native code to cache some
* field offsets.
*/
@SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
protected static boolean sInitialized;
native protected static void _nInit();
static {
sInitialized = false;
try {
System.loadLibrary("rs_jni");
_nInit();
sInitialized = true;
} catch (UnsatisfiedLinkError e) {
Log.d(LOG_TAG, "RenderScript JNI library not found!");
}
}
native void nInitElements(int a8, int rgba4444, int rgba8888, int rgb565);
native int nDeviceCreate();
native void nDeviceDestroy(int dev);
native void nDeviceSetConfig(int dev, int param, int value);
native int nContextCreateGL(int dev, int ver, boolean useDepth);
native int nContextCreate(int dev, int ver);
native void nContextDestroy(int con);
native void nContextSetSurface(int w, int h, Surface sur);
native void nContextSetPriority(int p);
native void nContextDump(int bits);
native void nContextBindRootScript(int script);
native void nContextBindSampler(int sampler, int slot);
native void nContextBindProgramFragmentStore(int pfs);
native void nContextBindProgramFragment(int pf);
native void nContextBindProgramVertex(int pf);
native void nContextBindProgramRaster(int pr);
native void nContextPause();
native void nContextResume();
native int nContextGetMessage(int[] data, boolean wait);
native void nContextInitToClient();
native void nContextDeinitToClient();
native void nAssignName(int obj, byte[] name);
native void nObjDestroy(int id);
native void nObjDestroyOOB(int id);
native int nFileOpen(byte[] name);
native int nElementCreate(int type, int kind, boolean norm, int vecSize);
native int nElementCreate2(int[] elements, String[] names);
native void nTypeBegin(int elementID);
native void nTypeAdd(int dim, int val);
native int nTypeCreate();
native void nTypeFinalDestroy(Type t);
native void nTypeSetupFields(Type t, int[] types, int[] bits, Field[] IDs);
native int nAllocationCreateTyped(int type);
native int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
native int nAllocationCreateBitmapRef(int type, Bitmap bmp);
native int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp);
native int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream);
native void nAllocationUploadToTexture(int alloc, boolean genMips, int baseMioLevel);
native void nAllocationUploadToBufferObject(int alloc);
native void nAllocationSubData1D(int id, int off, int count, int[] d, int sizeBytes);
native void nAllocationSubData1D(int id, int off, int count, short[] d, int sizeBytes);
native void nAllocationSubData1D(int id, int off, int count, byte[] d, int sizeBytes);
native void nAllocationSubData1D(int id, int off, int count, float[] d, int sizeBytes);
native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d, int sizeBytes);
native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d, int sizeBytes);
native void nAllocationRead(int id, int[] d);
native void nAllocationRead(int id, float[] d);
native void nAllocationSubDataFromObject(int id, Type t, int offset, Object o);
native void nAllocationSubReadFromObject(int id, Type t, int offset, Object o);
native void nAdapter1DBindAllocation(int ad, int alloc);
native void nAdapter1DSetConstraint(int ad, int dim, int value);
native void nAdapter1DData(int ad, int[] d);
native void nAdapter1DData(int ad, float[] d);
native void nAdapter1DSubData(int ad, int off, int count, int[] d);
native void nAdapter1DSubData(int ad, int off, int count, float[] d);
native int nAdapter1DCreate();
native void nAdapter2DBindAllocation(int ad, int alloc);
native void nAdapter2DSetConstraint(int ad, int dim, int value);
native void nAdapter2DData(int ad, int[] d);
native void nAdapter2DData(int ad, float[] d);
native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, int[] d);
native void nAdapter2DSubData(int ad, int xoff, int yoff, int w, int h, float[] d);
native int nAdapter2DCreate();
native void nScriptBindAllocation(int script, int alloc, int slot);
native void nScriptSetClearColor(int script, float r, float g, float b, float a);
native void nScriptSetClearDepth(int script, float depth);
native void nScriptSetClearStencil(int script, int stencil);
native void nScriptSetTimeZone(int script, byte[] timeZone);
native void nScriptSetType(int type, boolean writable, String name, int slot);
native void nScriptSetRoot(boolean isRoot);
native void nScriptSetInvokable(String name, int slot);
native void nScriptInvoke(int id, int slot);
native void nScriptCBegin();
native void nScriptCSetScript(byte[] script, int offset, int length);
native int nScriptCCreate();
native void nScriptCAddDefineI32(String name, int value);
native void nScriptCAddDefineF(String name, float value);
native void nSamplerBegin();
native void nSamplerSet(int param, int value);
native int nSamplerCreate();
native void nProgramFragmentStoreBegin(int in, int out);
native void nProgramFragmentStoreDepthFunc(int func);
native void nProgramFragmentStoreDepthMask(boolean enable);
native void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a);
native void nProgramFragmentStoreBlendFunc(int src, int dst);
native void nProgramFragmentStoreDither(boolean enable);
native int nProgramFragmentStoreCreate();
native int nProgramRasterCreate(int in, int out, boolean pointSmooth, boolean lineSmooth, boolean pointSprite);
native void nProgramRasterSetLineWidth(int pr, float v);
native void nProgramRasterSetPointSize(int pr, float v);
native void nProgramBindConstants(int pv, int slot, int mID);
native void nProgramBindTexture(int vpf, int slot, int a);
native void nProgramBindSampler(int vpf, int slot, int s);
native int nProgramFragmentCreate(int[] params);
native int nProgramFragmentCreate2(String shader, int[] params);
native int nProgramVertexCreate(boolean texMat);
native int nProgramVertexCreate2(String shader, int[] params);
native void nLightBegin();
native void nLightSetIsMono(boolean isMono);
native void nLightSetIsLocal(boolean isLocal);
native int nLightCreate();
native void nLightSetColor(int l, float r, float g, float b);
native void nLightSetPosition(int l, float x, float y, float z);
native int nSimpleMeshCreate(int batchID, int idxID, int[] vtxID, int prim);
native void nSimpleMeshBindVertex(int id, int alloc, int slot);
native void nSimpleMeshBindIndex(int id, int alloc);
native void nAnimationBegin(int attribCount, int keyframeCount);
native void nAnimationAdd(float time, float[] attribs);
native int nAnimationCreate();
protected int mDev;
protected int mContext;
@SuppressWarnings({"FieldCanBeLocal"})
protected MessageThread mMessageThread;
Element mElement_USER_U8;
Element mElement_USER_I8;
Element mElement_USER_U16;
Element mElement_USER_I16;
Element mElement_USER_U32;
Element mElement_USER_I32;
Element mElement_USER_F32;
Element mElement_A_8;
Element mElement_RGB_565;
Element mElement_RGB_888;
Element mElement_RGBA_5551;
Element mElement_RGBA_4444;
Element mElement_RGBA_8888;
Element mElement_INDEX_16;
Element mElement_POSITION_2;
Element mElement_POSITION_3;
Element mElement_TEXTURE_2;
Element mElement_NORMAL_3;
Element mElement_COLOR_U8_4;
Element mElement_COLOR_F32_4;
///////////////////////////////////////////////////////////////////////////////////
//
public static class RSMessage implements Runnable {
protected int[] mData;
protected int mID;
public void run() {
}
}
public RSMessage mMessageCallback = null;
public enum Priority {
LOW (5), //ANDROID_PRIORITY_BACKGROUND + 5
NORMAL (-4); //ANDROID_PRIORITY_DISPLAY
int mID;
Priority(int id) {
mID = id;
}
}
void validate() {
if (mContext == 0) {
throw new IllegalStateException("Calling RS with no Context active.");
}
}
public void contextSetPriority(Priority p) {
validate();
nContextSetPriority(p.mID);
}
protected static class MessageThread extends Thread {
RenderScript mRS;
boolean mRun = true;
MessageThread(RenderScript rs) {
super("RSMessageThread");
mRS = rs;
}
public void run() {
// This function is a temporary solution. The final solution will
// used typed allocations where the message id is the type indicator.
int[] rbuf = new int[16];
mRS.nContextInitToClient();
while(mRun) {
int msg = mRS.nContextGetMessage(rbuf, true);
if (msg == 0) {
// Should only happen during teardown.
// But we want to avoid starving other threads during
// teardown by yielding until the next line in the destructor
// can execute to set mRun = false
try {
sleep(1, 0);
} catch(InterruptedException e) {
}
}
if(mRS.mMessageCallback != null) {
mRS.mMessageCallback.mData = rbuf;
mRS.mMessageCallback.mID = msg;
mRS.mMessageCallback.run();
}
//Log.d(LOG_TAG, "MessageThread msg " + msg + " v1 " + rbuf[0] + " v2 " + rbuf[1] + " v3 " +rbuf[2]);
}
Log.d(LOG_TAG, "MessageThread exiting.");
}
}
protected RenderScript() {
}
public static RenderScript create() {
RenderScript rs = new RenderScript();
rs.mDev = rs.nDeviceCreate();
rs.mContext = rs.nContextCreate(rs.mDev, 0);
rs.mMessageThread = new MessageThread(rs);
rs.mMessageThread.start();
Element.initPredefined(rs);
return rs;
}
public void contextDump(int bits) {
validate();
nContextDump(bits);
}
public void destroy() {
validate();
nContextDeinitToClient();
mMessageThread.mRun = false;
nContextDestroy(mContext);
mContext = 0;
nDeviceDestroy(mDev);
mDev = 0;
}
boolean isAlive() {
return mContext != 0;
}
///////////////////////////////////////////////////////////////////////////////////
// Root state
protected int safeID(BaseObj o) {
if(o != null) {
return o.mID;
}
return 0;
}
}
@@ -0,0 +1,127 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.lang.reflect.Field;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Config;
import android.util.Log;
import android.view.Surface;
/**
* @hide
*
**/
public class RenderScriptGL extends RenderScript {
private Surface mSurface;
int mWidth;
int mHeight;
public RenderScriptGL(boolean useDepth, boolean forceSW) {
mSurface = null;
mWidth = 0;
mHeight = 0;
mDev = nDeviceCreate();
if(forceSW) {
nDeviceSetConfig(mDev, 0, 1);
}
mContext = nContextCreateGL(mDev, 0, useDepth);
mMessageThread = new MessageThread(this);
mMessageThread.start();
Element.initPredefined(this);
}
public void contextSetSurface(int w, int h, Surface sur) {
mSurface = sur;
mWidth = w;
mHeight = h;
validate();
nContextSetSurface(w, h, mSurface);
}
void pause() {
validate();
nContextPause();
}
void resume() {
validate();
nContextResume();
}
public void contextBindRootScript(Script s) {
validate();
nContextBindRootScript(safeID(s));
}
public void contextBindProgramFragmentStore(ProgramStore p) {
validate();
nContextBindProgramFragmentStore(safeID(p));
}
public void contextBindProgramFragment(ProgramFragment p) {
validate();
nContextBindProgramFragment(safeID(p));
}
public void contextBindProgramRaster(ProgramRaster p) {
validate();
nContextBindProgramRaster(safeID(p));
}
public void contextBindProgramVertex(ProgramVertex p) {
validate();
nContextBindProgramVertex(safeID(p));
}
//////////////////////////////////////////////////////////////////////////////////
// File
public class File extends BaseObj {
File(int id) {
super(RenderScriptGL.this);
mID = id;
}
}
public File fileOpen(String s) throws IllegalStateException, IllegalArgumentException
{
if(s.length() < 1) {
throw new IllegalArgumentException("fileOpen does not accept a zero length string.");
}
try {
byte[] bytes = s.getBytes("UTF-8");
int id = nFileOpen(bytes);
return new File(id);
} catch (java.io.UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,131 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.io.IOException;
import java.io.InputStream;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
/**
* @hide
*
**/
public class Sampler extends BaseObj {
public enum Value {
NEAREST (0),
LINEAR (1),
LINEAR_MIP_LINEAR (2),
WRAP (3),
CLAMP (4);
int mID;
Value(int id) {
mID = id;
}
}
Sampler(int id, RenderScript rs) {
super(rs);
mID = id;
}
public static class Builder {
RenderScript mRS;
Value mMin;
Value mMag;
Value mWrapS;
Value mWrapT;
Value mWrapR;
public Builder(RenderScript rs) {
mRS = rs;
mMin = Value.NEAREST;
mMag = Value.NEAREST;
mWrapS = Value.WRAP;
mWrapT = Value.WRAP;
mWrapR = Value.WRAP;
}
public void setMin(Value v) {
if (v == Value.NEAREST ||
v == Value.LINEAR ||
v == Value.LINEAR_MIP_LINEAR) {
mMin = v;
} else {
throw new IllegalArgumentException("Invalid value");
}
}
public void setMag(Value v) {
if (v == Value.NEAREST || v == Value.LINEAR) {
mMag = v;
} else {
throw new IllegalArgumentException("Invalid value");
}
}
public void setWrapS(Value v) {
if (v == Value.WRAP || v == Value.CLAMP) {
mWrapS = v;
} else {
throw new IllegalArgumentException("Invalid value");
}
}
public void setWrapT(Value v) {
if (v == Value.WRAP || v == Value.CLAMP) {
mWrapT = v;
} else {
throw new IllegalArgumentException("Invalid value");
}
}
public void setWrapR(Value v) {
if (v == Value.WRAP || v == Value.CLAMP) {
mWrapR = v;
} else {
throw new IllegalArgumentException("Invalid value");
}
}
static synchronized Sampler internalCreate(RenderScript rs, Builder b) {
rs.nSamplerBegin();
rs.nSamplerSet(0, b.mMin.mID);
rs.nSamplerSet(1, b.mMag.mID);
rs.nSamplerSet(2, b.mWrapS.mID);
rs.nSamplerSet(3, b.mWrapT.mID);
rs.nSamplerSet(4, b.mWrapR.mID);
int id = rs.nSamplerCreate();
return new Sampler(id, rs);
}
public Sampler create() {
mRS.validate();
return internalCreate(mRS, this);
}
}
}
@@ -0,0 +1,149 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
/**
* @hide
**/
public class Script extends BaseObj {
public static final int MAX_SLOT = 16;
boolean mIsRoot;
Type[] mTypes;
boolean[] mWritable;
Invokable[] mInvokables;
public static class Invokable {
RenderScript mRS;
Script mScript;
int mSlot;
String mName;
Invokable() {
mSlot = -1;
}
public void execute() {
mRS.nScriptInvoke(mScript.mID, mSlot);
}
}
Script(int id, RenderScript rs) {
super(rs);
mID = id;
}
public void bindAllocation(Allocation va, int slot) {
mRS.validate();
mRS.nScriptBindAllocation(mID, va.mID, slot);
}
public void setClearColor(float r, float g, float b, float a) {
mRS.validate();
mRS.nScriptSetClearColor(mID, r, g, b, a);
}
public void setClearDepth(float d) {
mRS.validate();
mRS.nScriptSetClearDepth(mID, d);
}
public void setClearStencil(int stencil) {
mRS.validate();
mRS.nScriptSetClearStencil(mID, stencil);
}
public void setTimeZone(String timeZone) {
mRS.validate();
try {
mRS.nScriptSetTimeZone(mID, timeZone.getBytes("UTF-8"));
} catch (java.io.UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static class Builder {
RenderScript mRS;
boolean mIsRoot = false;
Type[] mTypes;
String[] mNames;
boolean[] mWritable;
int mInvokableCount = 0;
Invokable[] mInvokables;
Builder(RenderScript rs) {
mRS = rs;
mTypes = new Type[MAX_SLOT];
mNames = new String[MAX_SLOT];
mWritable = new boolean[MAX_SLOT];
mInvokables = new Invokable[MAX_SLOT];
}
public void setType(Type t, int slot) {
mTypes[slot] = t;
mNames[slot] = null;
}
public void setType(Type t, String name, int slot) {
mTypes[slot] = t;
mNames[slot] = name;
}
public Invokable addInvokable(String func) {
Invokable i = new Invokable();
i.mName = func;
i.mRS = mRS;
i.mSlot = mInvokableCount;
mInvokables[mInvokableCount++] = i;
return i;
}
public void setType(boolean writable, int slot) {
mWritable[slot] = writable;
}
void transferCreate() {
mRS.nScriptSetRoot(mIsRoot);
for(int ct=0; ct < mTypes.length; ct++) {
if(mTypes[ct] != null) {
mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct);
}
}
for(int ct=0; ct < mInvokableCount; ct++) {
mRS.nScriptSetInvokable(mInvokables[ct].mName, ct);
}
}
void transferObject(Script s) {
s.mIsRoot = mIsRoot;
s.mTypes = mTypes;
s.mInvokables = new Invokable[mInvokableCount];
for(int ct=0; ct < mInvokableCount; ct++) {
s.mInvokables[ct] = mInvokables[ct];
s.mInvokables[ct].mScript = s;
}
s.mInvokables = null;
}
public void setRoot(boolean r) {
mIsRoot = r;
}
}
}
@@ -0,0 +1,161 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import android.content.res.Resources;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map.Entry;
import java.util.HashMap;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* @hide
**/
public class ScriptC extends Script {
private static final String TAG = "ScriptC";
ScriptC(int id, RenderScript rs) {
super(id, rs);
}
public static class Builder extends Script.Builder {
byte[] mProgram;
int mProgramLength;
HashMap<String,Integer> mIntDefines = new HashMap();
HashMap<String,Float> mFloatDefines = new HashMap();
public Builder(RenderScript rs) {
super(rs);
}
public void setScript(String s) {
try {
mProgram = s.getBytes("UTF-8");
mProgramLength = mProgram.length;
} catch (java.io.UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public void setScript(Resources resources, int id) {
InputStream is = resources.openRawResource(id);
try {
try {
setScript(is);
} finally {
is.close();
}
} catch(IOException e) {
throw new Resources.NotFoundException();
}
}
public void setScript(InputStream is) throws IOException {
byte[] buf = new byte[1024];
int currentPos = 0;
while(true) {
int bytesLeft = buf.length - currentPos;
if (bytesLeft == 0) {
byte[] buf2 = new byte[buf.length * 2];
System.arraycopy(buf, 0, buf2, 0, buf.length);
buf = buf2;
bytesLeft = buf.length - currentPos;
}
int bytesRead = is.read(buf, currentPos, bytesLeft);
if (bytesRead <= 0) {
break;
}
currentPos += bytesRead;
}
mProgram = buf;
mProgramLength = currentPos;
}
static synchronized ScriptC internalCreate(Builder b) {
b.mRS.nScriptCBegin();
b.transferCreate();
for (Entry<String,Integer> e: b.mIntDefines.entrySet()) {
b.mRS.nScriptCAddDefineI32(e.getKey(), e.getValue().intValue());
}
for (Entry<String,Float> e: b.mFloatDefines.entrySet()) {
b.mRS.nScriptCAddDefineF(e.getKey(), e.getValue().floatValue());
}
b.mRS.nScriptCSetScript(b.mProgram, 0, b.mProgramLength);
int id = b.mRS.nScriptCCreate();
ScriptC obj = new ScriptC(id, b.mRS);
b.transferObject(obj);
return obj;
}
public void addDefine(String name, int value) {
mIntDefines.put(name, value);
}
public void addDefine(String name, float value) {
mFloatDefines.put(name, value);
}
/**
* Takes the all public static final fields for a class, and adds defines
* for them, using the name of the field as the name of the define.
*/
public void addDefines(Class cl) {
addDefines(cl.getFields(), (Modifier.STATIC | Modifier.FINAL | Modifier.PUBLIC), null);
}
/**
* Takes the all public fields for an object, and adds defines
* for them, using the name of the field as the name of the define.
*/
public void addDefines(Object o) {
addDefines(o.getClass().getFields(), Modifier.PUBLIC, o);
}
void addDefines(Field[] fields, int mask, Object o) {
for (Field f: fields) {
try {
if ((f.getModifiers() & mask) == mask) {
Class t = f.getType();
if (t == int.class) {
mIntDefines.put(f.getName(), f.getInt(o));
}
else if (t == float.class) {
mFloatDefines.put(f.getName(), f.getFloat(o));
}
}
} catch (IllegalAccessException ex) {
// TODO: Do we want this log?
Log.d(TAG, "addDefines skipping field " + f.getName());
}
}
}
public ScriptC create() {
return internalCreate(this);
}
}
}
@@ -0,0 +1,364 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import android.util.Config;
import android.util.Log;
/**
* @hide
*
**/
public class SimpleMesh extends BaseObj {
Type[] mVertexTypes;
Type mIndexType;
//Type mBatcheType;
Primitive mPrimitive;
SimpleMesh(int id, RenderScript rs) {
super(rs);
mID = id;
}
public void bindVertexAllocation(Allocation a, int slot) {
mRS.validate();
mRS.nSimpleMeshBindVertex(mID, a.mID, slot);
}
public void bindIndexAllocation(Allocation a) {
mRS.validate();
mRS.nSimpleMeshBindIndex(mID, a.mID);
}
public Allocation createVertexAllocation(int slot) {
mRS.validate();
return Allocation.createTyped(mRS, mVertexTypes[slot]);
}
public Allocation createIndexAllocation() {
mRS.validate();
return Allocation.createTyped(mRS, mIndexType);
}
public Type getVertexType(int slot) {
return mVertexTypes[slot];
}
public Type getIndexType() {
return mIndexType;
}
public static class Builder {
RenderScript mRS;
class Entry {
Type t;
Element e;
int size;
}
int mVertexTypeCount;
Entry[] mVertexTypes;
Entry mIndexType;
//Entry mBatchType;
Primitive mPrimitive;
public Builder(RenderScript rs) {
mRS = rs;
mVertexTypeCount = 0;
mVertexTypes = new Entry[16];
mIndexType = new Entry();
}
public int addVertexType(Type t) throws IllegalStateException {
if (mVertexTypeCount >= mVertexTypes.length) {
throw new IllegalStateException("Max vertex types exceeded.");
}
int addedIndex = mVertexTypeCount;
mVertexTypes[mVertexTypeCount] = new Entry();
mVertexTypes[mVertexTypeCount].t = t;
mVertexTypeCount++;
return addedIndex;
}
public int addVertexType(Element e, int size) throws IllegalStateException {
if (mVertexTypeCount >= mVertexTypes.length) {
throw new IllegalStateException("Max vertex types exceeded.");
}
int addedIndex = mVertexTypeCount;
mVertexTypes[mVertexTypeCount] = new Entry();
mVertexTypes[mVertexTypeCount].e = e;
mVertexTypes[mVertexTypeCount].size = size;
mVertexTypeCount++;
return addedIndex;
}
public void setIndexType(Type t) {
mIndexType.t = t;
mIndexType.e = null;
mIndexType.size = 0;
}
public void setIndexType(Element e, int size) {
mIndexType.t = null;
mIndexType.e = e;
mIndexType.size = size;
}
public void setPrimitive(Primitive p) {
mPrimitive = p;
}
Type newType(Element e, int size) {
Type.Builder tb = new Type.Builder(mRS, e);
tb.add(Dimension.X, size);
return tb.create();
}
static synchronized SimpleMesh internalCreate(RenderScript rs, Builder b) {
Type[] toDestroy = new Type[18];
int toDestroyCount = 0;
int indexID = 0;
if (b.mIndexType.t != null) {
indexID = b.mIndexType.t.mID;
} else if (b.mIndexType.size != 0) {
b.mIndexType.t = b.newType(b.mIndexType.e, b.mIndexType.size);
indexID = b.mIndexType.t.mID;
toDestroy[toDestroyCount++] = b.mIndexType.t;
}
int[] IDs = new int[b.mVertexTypeCount];
for(int ct=0; ct < b.mVertexTypeCount; ct++) {
if (b.mVertexTypes[ct].t != null) {
IDs[ct] = b.mVertexTypes[ct].t.mID;
} else {
b.mVertexTypes[ct].t = b.newType(b.mVertexTypes[ct].e, b.mVertexTypes[ct].size);
IDs[ct] = b.mVertexTypes[ct].t.mID;
toDestroy[toDestroyCount++] = b.mVertexTypes[ct].t;
}
}
int id = rs.nSimpleMeshCreate(0, indexID, IDs, b.mPrimitive.mID);
for(int ct=0; ct < toDestroyCount; ct++) {
toDestroy[ct].destroy();
}
return new SimpleMesh(id, rs);
}
public SimpleMesh create() {
mRS.validate();
SimpleMesh sm = internalCreate(mRS, this);
sm.mVertexTypes = new Type[mVertexTypeCount];
for(int ct=0; ct < mVertexTypeCount; ct++) {
sm.mVertexTypes[ct] = mVertexTypes[ct].t;
}
sm.mIndexType = mIndexType.t;
sm.mPrimitive = mPrimitive;
return sm;
}
}
public static class TriangleMeshBuilder {
float mVtxData[];
int mVtxCount;
short mIndexData[];
int mIndexCount;
RenderScript mRS;
Element mElement;
float mNX = 0;
float mNY = 0;
float mNZ = -1;
float mS0 = 0;
float mT0 = 0;
float mR = 1;
float mG = 1;
float mB = 1;
float mA = 1;
int mVtxSize;
int mFlags;
public static final int COLOR = 0x0001;
public static final int NORMAL = 0x0002;
public static final int TEXTURE_0 = 0x0100;
public TriangleMeshBuilder(RenderScript rs, int vtxSize, int flags) {
mRS = rs;
mVtxCount = 0;
mIndexCount = 0;
mVtxData = new float[128];
mIndexData = new short[128];
mVtxSize = vtxSize;
mFlags = flags;
if (vtxSize < 2 || vtxSize > 3) {
throw new IllegalArgumentException("Vertex size out of range.");
}
}
private void makeSpace(int count) {
if ((mVtxCount + count) >= mVtxData.length) {
float t[] = new float[mVtxData.length * 2];
System.arraycopy(mVtxData, 0, t, 0, mVtxData.length);
mVtxData = t;
}
}
private void latch() {
if ((mFlags & COLOR) != 0) {
makeSpace(4);
mVtxData[mVtxCount++] = mR;
mVtxData[mVtxCount++] = mG;
mVtxData[mVtxCount++] = mB;
mVtxData[mVtxCount++] = mA;
}
if ((mFlags & TEXTURE_0) != 0) {
makeSpace(2);
mVtxData[mVtxCount++] = mS0;
mVtxData[mVtxCount++] = mT0;
}
if ((mFlags & NORMAL) != 0) {
makeSpace(3);
mVtxData[mVtxCount++] = mNX;
mVtxData[mVtxCount++] = mNY;
mVtxData[mVtxCount++] = mNZ;
}
}
public void addVertex(float x, float y) {
if (mVtxSize != 2) {
throw new IllegalStateException("add mistmatch with declared components.");
}
makeSpace(2);
mVtxData[mVtxCount++] = x;
mVtxData[mVtxCount++] = y;
latch();
}
public void addVertex(float x, float y, float z) {
if (mVtxSize != 3) {
throw new IllegalStateException("add mistmatch with declared components.");
}
makeSpace(3);
mVtxData[mVtxCount++] = x;
mVtxData[mVtxCount++] = y;
mVtxData[mVtxCount++] = z;
latch();
}
public void setTexture(float s, float t) {
if ((mFlags & TEXTURE_0) == 0) {
throw new IllegalStateException("add mistmatch with declared components.");
}
mS0 = s;
mT0 = t;
}
public void setNormal(float x, float y, float z) {
if ((mFlags & NORMAL) == 0) {
throw new IllegalStateException("add mistmatch with declared components.");
}
mNX = x;
mNY = y;
mNZ = z;
}
public void setColor(float r, float g, float b, float a) {
if ((mFlags & COLOR) == 0) {
throw new IllegalStateException("add mistmatch with declared components.");
}
mR = r;
mG = g;
mB = b;
mA = a;
}
public void addTriangle(int idx1, int idx2, int idx3) {
if((idx1 >= mVtxCount) || (idx1 < 0) ||
(idx2 >= mVtxCount) || (idx2 < 0) ||
(idx3 >= mVtxCount) || (idx3 < 0)) {
throw new IllegalStateException("Index provided greater than vertex count.");
}
if ((mIndexCount + 3) >= mIndexData.length) {
short t[] = new short[mIndexData.length * 2];
System.arraycopy(mIndexData, 0, t, 0, mIndexData.length);
mIndexData = t;
}
mIndexData[mIndexCount++] = (short)idx1;
mIndexData[mIndexCount++] = (short)idx2;
mIndexData[mIndexCount++] = (short)idx3;
}
public SimpleMesh create() {
Element.Builder b = new Element.Builder(mRS);
int floatCount = mVtxSize;
b.add(Element.createAttrib(mRS,
Element.DataType.FLOAT_32,
Element.DataKind.POSITION,
mVtxSize), "position");
if ((mFlags & COLOR) != 0) {
floatCount += 4;
b.add(Element.createAttrib(mRS,
Element.DataType.FLOAT_32,
Element.DataKind.COLOR,
4), "color");
}
if ((mFlags & TEXTURE_0) != 0) {
floatCount += 2;
b.add(Element.createAttrib(mRS,
Element.DataType.FLOAT_32,
Element.DataKind.TEXTURE,
2), "texture");
}
if ((mFlags & NORMAL) != 0) {
floatCount += 3;
b.add(Element.createAttrib(mRS,
Element.DataType.FLOAT_32,
Element.DataKind.NORMAL,
3), "normal");
}
mElement = b.create();
Builder smb = new Builder(mRS);
smb.addVertexType(mElement, mVtxCount / floatCount);
smb.setIndexType(Element.createIndex(mRS), mIndexCount);
smb.setPrimitive(Primitive.TRIANGLE);
SimpleMesh sm = smb.create();
Allocation vertexAlloc = sm.createVertexAllocation(0);
Allocation indexAlloc = sm.createIndexAllocation();
sm.bindVertexAllocation(vertexAlloc, 0);
sm.bindIndexAllocation(indexAlloc);
vertexAlloc.data(mVtxData);
vertexAlloc.uploadToBufferObject();
indexAlloc.data(mIndexData);
indexAlloc.uploadToBufferObject();
return sm;
}
}
}
@@ -0,0 +1,228 @@
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.lang.reflect.Field;
/**
* @hide
*
**/
public class Type extends BaseObj {
int mDimX;
int mDimY;
int mDimZ;
boolean mDimLOD;
boolean mDimFaces;
int mElementCount;
Element mElement;
private int mNativeCache;
Class mJavaClass;
public Element getElement() {
return mElement;
}
public int getX() {
return mDimX;
}
public int getY() {
return mDimY;
}
public int getZ() {
return mDimZ;
}
public boolean getLOD() {
return mDimLOD;
}
public boolean getFaces() {
return mDimFaces;
}
public int getElementCount() {
return mElementCount;
}
void calcElementCount() {
boolean hasLod = getLOD();
int x = getX();
int y = getY();
int z = getZ();
int faces = 1;
if(getFaces()) {
faces = 6;
}
if(x == 0) {
x = 1;
}
if(y == 0) {
y = 1;
}
if(z == 0) {
z = 1;
}
int count = x * y * z * faces;
if(hasLod && (x > 1) && (y > 1) && (z > 1)) {
if(x > 1) {
x >>= 1;
}
if(y > 1) {
y >>= 1;
}
if(z > 1) {
z >>= 1;
}
count += x * y * z * faces;
}
mElementCount = count;
}
Type(int id, RenderScript rs) {
super(rs);
mID = id;
mNativeCache = 0;
}
protected void finalize() throws Throwable {
if(mNativeCache != 0) {
mRS.nTypeFinalDestroy(this);
mNativeCache = 0;
}
super.finalize();
}
public static Type createFromClass(RenderScript rs, Class c, int size) {
Element e = Element.createFromClass(rs, c);
Builder b = new Builder(rs, e);
b.add(Dimension.X, size);
Type t = b.create();
e.destroy();
// native fields
{
Field[] fields = c.getFields();
int[] arTypes = new int[fields.length];
int[] arBits = new int[fields.length];
for(int ct=0; ct < fields.length; ct++) {
Field f = fields[ct];
Class fc = f.getType();
if(fc == int.class) {
arTypes[ct] = Element.DataType.SIGNED_32.mID;
arBits[ct] = 32;
} else if(fc == short.class) {
arTypes[ct] = Element.DataType.SIGNED_16.mID;
arBits[ct] = 16;
} else if(fc == byte.class) {
arTypes[ct] = Element.DataType.SIGNED_8.mID;
arBits[ct] = 8;
} else if(fc == float.class) {
arTypes[ct] = Element.DataType.FLOAT_32.mID;
arBits[ct] = 32;
} else {
throw new IllegalArgumentException("Unkown field type");
}
}
rs.nTypeSetupFields(t, arTypes, arBits, fields);
}
t.mJavaClass = c;
return t;
}
public static Type createFromClass(RenderScript rs, Class c, int size, String scriptName) {
Type t = createFromClass(rs, c, size);
t.setName(scriptName);
return t;
}
public static class Builder {
RenderScript mRS;
Entry[] mEntries;
int mEntryCount;
Element mElement;
class Entry {
Dimension mDim;
int mValue;
}
public Builder(RenderScript rs, Element e) {
if(e.mID == 0) {
throw new IllegalArgumentException("Invalid element.");
}
mRS = rs;
mEntries = new Entry[4];
mElement = e;
}
public void add(Dimension d, int value) {
if(value < 1) {
throw new IllegalArgumentException("Values of less than 1 for Dimensions are not valid.");
}
if(mEntries.length >= mEntryCount) {
Entry[] en = new Entry[mEntryCount + 8];
System.arraycopy(mEntries, 0, en, 0, mEntries.length);
mEntries = en;
}
mEntries[mEntryCount] = new Entry();
mEntries[mEntryCount].mDim = d;
mEntries[mEntryCount].mValue = value;
mEntryCount++;
}
static synchronized Type internalCreate(RenderScript rs, Builder b) {
rs.nTypeBegin(b.mElement.mID);
for (int ct=0; ct < b.mEntryCount; ct++) {
Entry en = b.mEntries[ct];
rs.nTypeAdd(en.mDim.mID, en.mValue);
}
int id = rs.nTypeCreate();
return new Type(id, rs);
}
public Type create() {
Type t = internalCreate(mRS, this);
t.mElement = mElement;
for(int ct=0; ct < mEntryCount; ct++) {
if(mEntries[ct].mDim == Dimension.X) {
t.mDimX = mEntries[ct].mValue;
}
if(mEntries[ct].mDim == Dimension.Y) {
t.mDimY = mEntries[ct].mValue;
}
if(mEntries[ct].mDim == Dimension.Z) {
t.mDimZ = mEntries[ct].mValue;
}
if(mEntries[ct].mDim == Dimension.LOD) {
t.mDimLOD = mEntries[ct].mValue != 0;
}
if(mEntries[ct].mDim == Dimension.FACE) {
t.mDimFaces = mEntries[ct].mValue != 0;
}
}
t.calcElementCount();
return t;
}
}
}
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.lang.Math;
import android.util.Log;
/**
* @hide
*
**/
public class Vector2f {
public Vector2f() {
}
public float x;
public float y;
}
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.lang.Math;
import android.util.Log;
/**
* @hide
*
**/
public class Vector3f {
public Vector3f() {
}
public float x;
public float y;
public float z;
}
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.renderscript;
import java.lang.Math;
import android.util.Log;
/**
* @hide
*
**/
public class Vector4f {
public Vector4f() {
}
public float x;
public float y;
public float z;
public float w;
}