From 36e612a488511940b61f09803b270aa1c61b68e0 Mon Sep 17 00:00:00 2001 From: Jason Sams Date: Fri, 31 Jul 2009 16:26:13 -0700 Subject: [PATCH] Begin splitting up RenderScript.java into seperate classes. First piece split off Element. --- .../java/android/renderscript/BaseObj.java | 67 +++ .../java/android/renderscript/Element.java | 205 +++++++++ .../renderscript/ProgramVertexAlloc.java | 5 +- .../android/renderscript/RenderScript.java | 427 +++++++----------- .../Film/src/com/android/film/FilmRS.java | 55 +-- .../src/com/android/fountain/FountainRS.java | 10 +- .../src/com/android/grass/rs/GrassRS.java | 13 +- libs/rs/java/Rollo/res/raw/rollo.c | 3 - .../src/com/android/rollo/RolloMesh.java | 90 ---- .../Rollo/src/com/android/rollo/RolloRS.java | 31 +- 10 files changed, 473 insertions(+), 433 deletions(-) create mode 100644 graphics/java/android/renderscript/BaseObj.java create mode 100644 graphics/java/android/renderscript/Element.java delete mode 100644 libs/rs/java/Rollo/src/com/android/rollo/RolloMesh.java diff --git a/graphics/java/android/renderscript/BaseObj.java b/graphics/java/android/renderscript/BaseObj.java new file mode 100644 index 0000000000000..f70aee5089e50 --- /dev/null +++ b/graphics/java/android/renderscript/BaseObj.java @@ -0,0 +1,67 @@ +/* + * 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) { + mRS = rs; + mID = 0; + } + + public int getID() { + return mID; + } + + int mID; + 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 (mID != 0) { + Log.v(RenderScript.LOG_TAG, + "Element finalized without having released the RS reference."); + } + super.finalize(); + } +} + diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java new file mode 100644 index 0000000000000..8e0a7a11fac7f --- /dev/null +++ b/graphics/java/android/renderscript/Element.java @@ -0,0 +1,205 @@ +/* + * 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 Element extends BaseObj { + final int mPredefinedID; + final boolean mIsPredefined; + + public static final Element USER_U8 = new Element(0); + public static final Element USER_I8 = new Element(1); + public static final Element USER_U16 = new Element(2); + public static final Element USER_I16 = new Element(3); + public static final Element USER_U32 = new Element(4); + public static final Element USER_I32 = new Element(5); + public static final Element USER_FLOAT = new Element(6); + + public static final Element A_8 = new Element(7); + public static final Element RGB_565 = new Element(8); + public static final Element RGB_888 = new Element(11); + public static final Element RGBA_5551 = new Element(9); + public static final Element RGBA_4444 = new Element(10); + public static final Element RGBA_8888 = new Element(12); + + public static final Element INDEX_16 = new Element(13); + public static final Element INDEX_32 = new Element(14); + public static final Element XY_F32 = new Element(15); + public static final Element XYZ_F32 = new Element(16); + public static final Element ST_XY_F32 = new Element(17); + public static final Element ST_XYZ_F32 = new Element(18); + public static final Element NORM_XYZ_F32 = new Element(19); + public static final Element NORM_ST_XYZ_F32 = new Element(20); + + void initPredef(RenderScript rs) { + mID = rs.nElementGetPredefined(mPredefinedID); + } + + static void init(RenderScript rs) { + USER_U8.initPredef(rs); + USER_I8.initPredef(rs); + USER_U16.initPredef(rs); + USER_I16.initPredef(rs); + USER_U32.initPredef(rs); + USER_I32.initPredef(rs); + USER_FLOAT.initPredef(rs); + + A_8.initPredef(rs); + RGB_565.initPredef(rs); + RGB_888.initPredef(rs); + RGBA_5551.initPredef(rs); + RGBA_4444.initPredef(rs); + RGBA_8888.initPredef(rs); + + INDEX_16.initPredef(rs); + INDEX_32.initPredef(rs); + XY_F32.initPredef(rs); + XYZ_F32.initPredef(rs); + ST_XY_F32.initPredef(rs); + ST_XYZ_F32.initPredef(rs); + NORM_XYZ_F32.initPredef(rs); + NORM_ST_XYZ_F32.initPredef(rs); + } + + + public enum DataType { + FLOAT (0), + UNSIGNED (1), + SIGNED (2); + + int mID; + DataType(int id) { + mID = id; + } + } + + public enum DataKind { + USER (0), + RED (1), + GREEN (2), + BLUE (3), + ALPHA (4), + LUMINANCE (5), + INTENSITY (6), + X (7), + Y (8), + Z (9), + W (10), + S (11), + T (12), + Q (13), + R (14), + NX (15), + NY (16), + NZ (17), + INDEX (18); + + int mID; + DataKind(int id) { + mID = id; + } + } + + + Element(int predef) { + super(null); + mID = 0; + mPredefinedID = predef; + mIsPredefined = true; + } + + Element(int id, RenderScript rs) { + super(rs); + mID = id; + mPredefinedID = 0; + mIsPredefined = false; + } + + public void destroy() throws IllegalStateException { + if(mIsPredefined) { + throw new IllegalStateException("Attempting to destroy a predefined Element."); + } + mRS.nElementDestroy(mID); + mID = 0; + } + + + + + public static class Builder { + RenderScript mRS; + boolean mActive = true; + + Builder(RenderScript rs) { + mRS = rs; + } + + void begin() throws IllegalStateException { + if (mActive) { + throw new IllegalStateException("Element builder already active."); + } + mRS.nElementBegin(); + mActive = true; + } + + public Builder add(Element e) throws IllegalArgumentException, IllegalStateException { + if(!mActive) { + throw new IllegalStateException("Element builder not active."); + } + if(!e.mIsPredefined) { + throw new IllegalArgumentException("add requires a predefined Element."); + } + mRS.nElementAddPredefined(e.mID); + return this; + } + + public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) + throws IllegalStateException { + if(!mActive) { + throw new IllegalStateException("Element builder not active."); + } + int norm = 0; + if (isNormalized) { + norm = 1; + } + mRS.nElementAdd(dt.mID, dk.mID, norm, bits); + return this; + } + + public void abort() throws IllegalStateException { + if(!mActive) { + throw new IllegalStateException("Element builder not active."); + } + mActive = false; + } + + public Element create() throws IllegalStateException { + if(!mActive) { + throw new IllegalStateException("Element builder not active."); + } + int id = mRS.nElementCreate(); + mActive = false; + return new Element(id, mRS); + } + } + +} + diff --git a/graphics/java/android/renderscript/ProgramVertexAlloc.java b/graphics/java/android/renderscript/ProgramVertexAlloc.java index 82bcc305c1837..424e0ad33b161 100644 --- a/graphics/java/android/renderscript/ProgramVertexAlloc.java +++ b/graphics/java/android/renderscript/ProgramVertexAlloc.java @@ -40,10 +40,7 @@ public class ProgramVertexAlloc { mProjection = new Matrix(); mTexture = new Matrix(); - mAlloc = rs.allocationCreatePredefSized( - RenderScript.ElementPredefined.USER_FLOAT, - 48); - + mAlloc = rs.allocationCreateSized(Element.USER_FLOAT, 48); mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat); mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat); mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat); diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java index 904361fb88b3d..3d4f33335c646 100644 --- a/graphics/java/android/renderscript/RenderScript.java +++ b/graphics/java/android/renderscript/RenderScript.java @@ -14,30 +14,21 @@ * limitations under the License. */ -/** - * @hide - * - **/ package android.renderscript; -import java.io.InputStream; 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.view.Surface; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; import android.graphics.Color; -import android.os.Bundle; -import android.content.res.Resources; -import android.util.Log; -import android.util.Config; -import android.view.Menu; -import android.view.MenuItem; -import android.view.Window; -import android.view.View; -import android.view.Surface; /** * @hide @@ -71,125 +62,125 @@ public class RenderScript { mBitmapOptions.inScaled = false; } - native private int nDeviceCreate(); - native private void nDeviceDestroy(int dev); - native private int nContextCreate(int dev, Surface sur, int ver); - native private void nContextDestroy(int con); + native int nDeviceCreate(); + native void nDeviceDestroy(int dev); + native int nContextCreate(int dev, Surface sur, int ver); + native void nContextDestroy(int con); //void rsContextBindSampler (uint32_t slot, RsSampler sampler); //void rsContextBindRootScript (RsScript sampler); - native private void nContextBindRootScript(int script); - native private void nContextBindSampler(int sampler, int slot); - native private void nContextBindProgramFragmentStore(int pfs); - native private void nContextBindProgramFragment(int pf); - native private void nContextBindProgramVertex(int pf); + 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 private void nAssignName(int obj, byte[] name); - native private int nFileOpen(byte[] name); + native void nAssignName(int obj, byte[] name); + native int nFileOpen(byte[] name); - native private void nElementBegin(); - native private void nElementAddPredefined(int predef); - native private void nElementAdd(int kind, int type, int norm, int bits); - native private int nElementCreate(); - native private int nElementGetPredefined(int predef); - native private void nElementDestroy(int obj); + native void nElementBegin(); + native void nElementAddPredefined(int predef); + native void nElementAdd(int kind, int type, int norm, int bits); + native int nElementCreate(); + native int nElementGetPredefined(int predef); + native void nElementDestroy(int obj); - native private void nTypeBegin(int elementID); - native private void nTypeAdd(int dim, int val); - native private int nTypeCreate(); - native private void nTypeDestroy(int id); + native void nTypeBegin(int elementID); + native void nTypeAdd(int dim, int val); + native int nTypeCreate(); + native void nTypeDestroy(int id); - native private int nAllocationCreateTyped(int type); - native private int nAllocationCreatePredefSized(int predef, int count); - native private int nAllocationCreateSized(int elem, int count); - native private int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp); - native private int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp); + native int nAllocationCreateTyped(int type); + native int nAllocationCreatePredefSized(int predef, int count); + native int nAllocationCreateSized(int elem, int count); + native int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp); + native int nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp); - native private void nAllocationUploadToTexture(int alloc, int baseMioLevel); - native private void nAllocationDestroy(int alloc); - native private void nAllocationData(int id, int[] d); - native private void nAllocationData(int id, float[] d); - native private void nAllocationSubData1D(int id, int off, int count, int[] d); - native private void nAllocationSubData1D(int id, int off, int count, float[] d); - native private void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d); - native private void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d); + native void nAllocationUploadToTexture(int alloc, int baseMioLevel); + native void nAllocationDestroy(int alloc); + native void nAllocationData(int id, int[] d); + native void nAllocationData(int id, float[] d); + native void nAllocationSubData1D(int id, int off, int count, int[] d); + native void nAllocationSubData1D(int id, int off, int count, float[] d); + native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, int[] d); + native void nAllocationSubData2D(int id, int xoff, int yoff, int w, int h, float[] d); - native private void nTriangleMeshDestroy(int id); - native private void nTriangleMeshBegin(int vertex, int index); - native private void nTriangleMeshAddVertex_XY (float x, float y); - native private void nTriangleMeshAddVertex_XYZ (float x, float y, float z); - native private void nTriangleMeshAddVertex_XY_ST (float x, float y, float s, float t); - native private void nTriangleMeshAddVertex_XYZ_ST (float x, float y, float z, float s, float t); - native private void nTriangleMeshAddVertex_XYZ_ST_NORM (float x, float y, float z, float s, float t, float nx, float ny, float nz); - native private void nTriangleMeshAddTriangle(int i1, int i2, int i3); - native private int nTriangleMeshCreate(); + native void nTriangleMeshDestroy(int id); + native void nTriangleMeshBegin(int vertex, int index); + native void nTriangleMeshAddVertex_XY (float x, float y); + native void nTriangleMeshAddVertex_XYZ (float x, float y, float z); + native void nTriangleMeshAddVertex_XY_ST (float x, float y, float s, float t); + native void nTriangleMeshAddVertex_XYZ_ST (float x, float y, float z, float s, float t); + native void nTriangleMeshAddVertex_XYZ_ST_NORM (float x, float y, float z, float s, float t, float nx, float ny, float nz); + native void nTriangleMeshAddTriangle(int i1, int i2, int i3); + native int nTriangleMeshCreate(); - native private void nAdapter1DDestroy(int id); - native private void nAdapter1DBindAllocation(int ad, int alloc); - native private void nAdapter1DSetConstraint(int ad, int dim, int value); - native private void nAdapter1DData(int ad, int[] d); - native private void nAdapter1DSubData(int ad, int off, int count, int[] d); - native private void nAdapter1DData(int ad, float[] d); - native private void nAdapter1DSubData(int ad, int off, int count, float[] d); - native private int nAdapter1DCreate(); + native void nAdapter1DDestroy(int id); + 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 nAdapter1DSubData(int ad, int off, int count, int[] d); + native void nAdapter1DData(int ad, float[] d); + native void nAdapter1DSubData(int ad, int off, int count, float[] d); + native int nAdapter1DCreate(); - native private void nScriptDestroy(int script); - native private void nScriptBindAllocation(int vtm, int alloc, int slot); - native private void nScriptCBegin(); - native private void nScriptCSetClearColor(float r, float g, float b, float a); - native private void nScriptCSetClearDepth(float depth); - native private void nScriptCSetClearStencil(int stencil); - native private void nScriptCSetTimeZone(byte[] timeZone); - native private void nScriptCAddType(int type); - native private void nScriptCSetRoot(boolean isRoot); - native private void nScriptCSetScript(byte[] script, int offset, int length); - native private int nScriptCCreate(); + native void nScriptDestroy(int script); + native void nScriptBindAllocation(int vtm, int alloc, int slot); + native void nScriptCBegin(); + native void nScriptCSetClearColor(float r, float g, float b, float a); + native void nScriptCSetClearDepth(float depth); + native void nScriptCSetClearStencil(int stencil); + native void nScriptCSetTimeZone(byte[] timeZone); + native void nScriptCAddType(int type); + native void nScriptCSetRoot(boolean isRoot); + native void nScriptCSetScript(byte[] script, int offset, int length); + native int nScriptCCreate(); - native private void nSamplerDestroy(int sampler); - native private void nSamplerBegin(); - native private void nSamplerSet(int param, int value); - native private int nSamplerCreate(); + native void nSamplerDestroy(int sampler); + native void nSamplerBegin(); + native void nSamplerSet(int param, int value); + native int nSamplerCreate(); - native private void nProgramFragmentStoreBegin(int in, int out); - native private void nProgramFragmentStoreDepthFunc(int func); - native private void nProgramFragmentStoreDepthMask(boolean enable); - native private void nProgramFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a); - native private void nProgramFragmentStoreBlendFunc(int src, int dst); - native private void nProgramFragmentStoreDither(boolean enable); - native private int nProgramFragmentStoreCreate(); - native private void nProgramFragmentStoreDestroy(int pgm); + 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 void nProgramFragmentStoreDestroy(int pgm); - native private void nProgramFragmentBegin(int in, int out); - native private void nProgramFragmentBindTexture(int vpf, int slot, int a); - native private void nProgramFragmentBindSampler(int vpf, int slot, int s); - native private void nProgramFragmentSetType(int slot, int vt); - native private void nProgramFragmentSetEnvMode(int slot, int env); - native private void nProgramFragmentSetTexEnable(int slot, boolean enable); - native private int nProgramFragmentCreate(); - native private void nProgramFragmentDestroy(int pgm); + native void nProgramFragmentBegin(int in, int out); + native void nProgramFragmentBindTexture(int vpf, int slot, int a); + native void nProgramFragmentBindSampler(int vpf, int slot, int s); + native void nProgramFragmentSetType(int slot, int vt); + native void nProgramFragmentSetEnvMode(int slot, int env); + native void nProgramFragmentSetTexEnable(int slot, boolean enable); + native int nProgramFragmentCreate(); + native void nProgramFragmentDestroy(int pgm); - native private void nProgramVertexDestroy(int pv); - native private void nProgramVertexBindAllocation(int pv, int slot, int mID); - native private void nProgramVertexBegin(int inID, int outID); - native private void nProgramVertexSetType(int slot, int mID); - native private void nProgramVertexSetTextureMatrixEnable(boolean enable); - native private void nProgramVertexAddLight(int id); - native private int nProgramVertexCreate(); + native void nProgramVertexDestroy(int pv); + native void nProgramVertexBindAllocation(int pv, int slot, int mID); + native void nProgramVertexBegin(int inID, int outID); + native void nProgramVertexSetType(int slot, int mID); + native void nProgramVertexSetTextureMatrixEnable(boolean enable); + native void nProgramVertexAddLight(int id); + native int nProgramVertexCreate(); - native private void nLightBegin(); - native private void nLightSetIsMono(boolean isMono); - native private void nLightSetIsLocal(boolean isLocal); - native private int nLightCreate(); - native private void nLightDestroy(int l); - native private void nLightSetColor(int l, float r, float g, float b); - native private void nLightSetPosition(int l, float x, float y, float z); + native void nLightBegin(); + native void nLightSetIsMono(boolean isMono); + native void nLightSetIsLocal(boolean isLocal); + native int nLightCreate(); + native void nLightDestroy(int l); + native void nLightSetColor(int l, float r, float g, float b); + native void nLightSetPosition(int l, float x, float y, float z); private int mDev; private int mContext; private Surface mSurface; - + private static boolean mElementsInitialized = false; /////////////////////////////////////////////////////////////////////////////////// // @@ -198,120 +189,25 @@ public class RenderScript { mSurface = sur; mDev = nDeviceCreate(); mContext = nContextCreate(mDev, mSurface, 0); - } - private class BaseObj { - BaseObj() { - mID = 0; - } - - public int getID() { - return mID; - } - - int mID; - String mName; - - 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"); - nAssignName(mID, bytes); - mName = s; - } catch (java.io.UnsupportedEncodingException e) { - throw new RuntimeException(e); - } - } - - protected void finalize() throws Throwable - { - if (mID != 0) { - Log.v(LOG_TAG, - "Element finalized without having released the RS reference."); - } - super.finalize(); + // TODO: This should be protected by a lock + if(!mElementsInitialized) { + Element.init(this); + mElementsInitialized = true; } } - ////////////////////////////////////////////////////////////////////////////////// // Element - public enum ElementPredefined { - USER_U8 (0), - USER_I8 (1), - USER_U16 (2), - USER_I16 (3), - USER_U32 (4), - USER_I32 (5), - USER_FLOAT (6), - - A_8 (7), - RGB_565 (8), - RGB_888 (11), - RGBA_5551 (9), - RGBA_4444 (10), - RGBA_8888 (12), - - INDEX_16 (13), - INDEX_32 (14), - XY_F32 (15), - XYZ_F32 (16), - ST_XY_F32 (17), - ST_XYZ_F32 (18), - NORM_XYZ_F32 (19), - NORM_ST_XYZ_F32 (20); - - int mID; - ElementPredefined(int id) { - mID = id; - } + Element.Builder mElementBuilder = new Element.Builder(this); + public Element.Builder elementBuilderCreate() throws IllegalStateException { + mElementBuilder.begin(); + return mElementBuilder; } - public enum DataType { - FLOAT (0), - UNSIGNED (1), - SIGNED (2); - int mID; - DataType(int id) { - mID = id; - } - } - public enum DataKind { - USER (0), - RED (1), - GREEN (2), - BLUE (3), - ALPHA (4), - LUMINANCE (5), - INTENSITY (6), - X (7), - Y (8), - Z (9), - W (10), - S (11), - T (12), - Q (13), - R (14), - NX (15), - NY (16), - NZ (17), - INDEX (18); - - int mID; - DataKind(int id) { - mID = id; - } - } public enum DepthFunc { ALWAYS (0), @@ -398,46 +294,6 @@ public class RenderScript { } } - - - public class Element extends BaseObj { - Element(int id) { - mID = id; - } - - public void estroy() { - nElementDestroy(mID); - mID = 0; - } - } - - public void elementBegin() { - nElementBegin(); - } - - public void elementAddPredefined(ElementPredefined e) { - nElementAddPredefined(e.mID); - } - - public void elementAdd(DataType dt, DataKind dk, boolean isNormalized, int bits) { - int norm = 0; - if (isNormalized) { - norm = 1; - } - nElementAdd(dt.mID, dk.mID, norm, bits); - } - - public Element elementCreate() { - int id = nElementCreate(); - return new Element(id); - } - - public Element elementGetPredefined(ElementPredefined predef) { - int id = nElementGetPredefined(predef.mID); - return new Element(id); - } - - ////////////////////////////////////////////////////////////////////////////////// // Type @@ -457,6 +313,7 @@ public class RenderScript { public class Type extends BaseObj { Type(int id) { + super(RenderScript.this); mID = id; } @@ -485,6 +342,7 @@ public class RenderScript { public class Allocation extends BaseObj { Allocation(int id) { + super(RenderScript.this); mID = id; } @@ -527,34 +385,48 @@ public class RenderScript { return new Allocation(id); } - public Allocation allocationCreatePredefSized(ElementPredefined e, int count) { - int id = nAllocationCreatePredefSized(e.mID, count); - return new Allocation(id); - } - public Allocation allocationCreateSized(Element e, int count) { - int id = nAllocationCreateSized(e.mID, count); + int id; + if(e.mIsPredefined) { + id = nAllocationCreatePredefSized(e.mPredefinedID, count); + } else { + id = nAllocationCreateSized(e.mID, count); + } return new Allocation(id); } - public Allocation allocationCreateFromBitmap(Bitmap b, ElementPredefined dstFmt, boolean genMips) { - int id = nAllocationCreateFromBitmap(dstFmt.mID, genMips, b); + public Allocation allocationCreateFromBitmap(Bitmap b, Element dstFmt, boolean genMips) + throws IllegalArgumentException { + if(!dstFmt.mIsPredefined) { + throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element."); + } + + int id = nAllocationCreateFromBitmap(dstFmt.mPredefinedID, genMips, b); return new Allocation(id); } - public Allocation allocationCreateFromBitmapBoxed(Bitmap b, ElementPredefined dstFmt, boolean genMips) { - int id = nAllocationCreateFromBitmapBoxed(dstFmt.mID, genMips, b); + public Allocation allocationCreateFromBitmapBoxed(Bitmap b, Element dstFmt, boolean genMips) + throws IllegalArgumentException { + if(!dstFmt.mIsPredefined) { + throw new IllegalStateException("Attempting to allocate a bitmap with a non-static element."); + } + + int id = nAllocationCreateFromBitmapBoxed(dstFmt.mPredefinedID, genMips, b); return new Allocation(id); } - public Allocation allocationCreateFromBitmapResource(Resources res, int id, ElementPredefined internalElement, boolean genMips) { + public Allocation allocationCreateFromBitmapResource(Resources res, int id, Element dstFmt, boolean genMips) + throws IllegalArgumentException { + Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions); - return allocationCreateFromBitmap(b, internalElement, genMips); + return allocationCreateFromBitmap(b, dstFmt, genMips); } - public Allocation allocationCreateFromBitmapResourceBoxed(Resources res, int id, ElementPredefined internalElement, boolean genMips) { + public Allocation allocationCreateFromBitmapResourceBoxed(Resources res, int id, Element dstFmt, boolean genMips) + throws IllegalArgumentException { + Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions); - return allocationCreateFromBitmapBoxed(b, internalElement, genMips); + return allocationCreateFromBitmapBoxed(b, dstFmt, genMips); } @@ -563,6 +435,7 @@ public class RenderScript { public class Adapter1D extends BaseObj { Adapter1D(int id) { + super(RenderScript.this); mID = id; } @@ -607,6 +480,7 @@ public class RenderScript { public class TriangleMesh extends BaseObj { TriangleMesh(int id) { + super(RenderScript.this); mID = id; } @@ -617,6 +491,7 @@ public class RenderScript { } public void triangleMeshBegin(Element vertex, Element index) { + Log.e("rs", "vtx " + vertex.toString() + " " + vertex.mID + " " + vertex.mPredefinedID); nTriangleMeshBegin(vertex.mID, index.mID); } @@ -654,6 +529,7 @@ public class RenderScript { public class Script extends BaseObj { Script(int id) { + super(RenderScript.this); mID = id; } @@ -674,12 +550,12 @@ public class RenderScript { public void scriptCSetTimeZone(String timeZone) { try { byte[] bytes = timeZone.getBytes("UTF-8"); - nScriptCSetTimeZone(bytes); + nScriptCSetTimeZone(bytes); } catch (java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } } - + public void scriptCSetClearColor(float r, float g, float b, float a) { nScriptCSetClearColor(r, g, b, a); } @@ -752,6 +628,7 @@ public class RenderScript { public class ProgramVertex extends BaseObj { ProgramVertex(int id) { + super(RenderScript.this); mID = id; } @@ -800,6 +677,7 @@ public class RenderScript { public class ProgramFragmentStore extends BaseObj { ProgramFragmentStore(int id) { + super(RenderScript.this); mID = id; } @@ -851,6 +729,7 @@ public class RenderScript { public class ProgramFragment extends BaseObj { ProgramFragment(int id) { + super(RenderScript.this); mID = id; } @@ -906,6 +785,7 @@ public class RenderScript { public class Sampler extends BaseObj { Sampler(int id) { + super(RenderScript.this); mID = id; } @@ -933,6 +813,7 @@ public class RenderScript { public class Light extends BaseObj { Light(int id) { + super(RenderScript.this); mID = id; } @@ -972,6 +853,7 @@ public class RenderScript { public class File extends BaseObj { File(int id) { + super(RenderScript.this); mID = id; } @@ -1035,3 +917,4 @@ public class RenderScript { } + diff --git a/libs/rs/java/Film/src/com/android/film/FilmRS.java b/libs/rs/java/Film/src/com/android/film/FilmRS.java index 12a14e3dbef14..0e5b1bd28e585 100644 --- a/libs/rs/java/Film/src/com/android/film/FilmRS.java +++ b/libs/rs/java/Film/src/com/android/film/FilmRS.java @@ -21,22 +21,12 @@ import java.io.Writer; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.drawable.BitmapDrawable; -import android.graphics.drawable.Drawable; -import android.os.Handler; -import android.os.Message; +import android.util.Log; + import android.renderscript.Matrix; import android.renderscript.ProgramVertexAlloc; import android.renderscript.RenderScript; -import android.renderscript.RenderScript.ElementPredefined; -import android.util.AttributeSet; -import android.util.Log; -import android.view.KeyEvent; -import android.view.MotionEvent; -import android.view.Surface; -import android.view.SurfaceHolder; -import android.view.SurfaceView; +import android.renderscript.Element; public class FilmRS { private final int POS_TRANSLATE = 0; @@ -76,8 +66,8 @@ public class FilmRS { private RenderScript mRS; private RenderScript.Script mScriptStrip; private RenderScript.Script mScriptImage; - private RenderScript.Element mElementVertex; - private RenderScript.Element mElementIndex; + private Element mElementVertex; + private Element mElementIndex; private RenderScript.Sampler mSampler; private RenderScript.ProgramFragmentStore mPFSBackground; private RenderScript.ProgramFragmentStore mPFSImages; @@ -166,13 +156,10 @@ public class FilmRS { private void loadImages() { mBufferIDs = new int[13]; mImages = new RenderScript.Allocation[13]; - mAllocIDs = mRS.allocationCreatePredefSized( - RenderScript.ElementPredefined.USER_FLOAT, - mBufferIDs.length); - - RenderScript.ElementPredefined ie = - RenderScript.ElementPredefined.RGB_565; + mAllocIDs = mRS.allocationCreateSized( + Element.USER_FLOAT, mBufferIDs.length); + Element ie = Element.RGB_565; mImages[0] = mRS.allocationCreateFromBitmapResourceBoxed(mRes, R.drawable.p01, ie, true); mImages[1] = mRS.allocationCreateFromBitmapResourceBoxed(mRes, R.drawable.p02, ie, true); mImages[2] = mRS.allocationCreateFromBitmapResourceBoxed(mRes, R.drawable.p03, ie, true); @@ -197,9 +184,8 @@ public class FilmRS { private void initState() { mBufferState = new int[10]; - mAllocState = mRS.allocationCreatePredefSized( - RenderScript.ElementPredefined.USER_FLOAT, - mBufferState.length); + mAllocState = mRS.allocationCreateSized( + Element.USER_FLOAT, mBufferState.length); mBufferState[STATE_TRIANGLE_OFFSET_COUNT] = mFSM.mTriangleOffsetsCount; mBufferState[STATE_LAST_FOCUS] = -1; @@ -208,10 +194,8 @@ public class FilmRS { } private void initRS() { - mElementVertex = mRS.elementGetPredefined( - RenderScript.ElementPredefined.NORM_ST_XYZ_F32); - mElementIndex = mRS.elementGetPredefined( - RenderScript.ElementPredefined.INDEX_16); + mElementVertex = Element.NORM_ST_XYZ_F32; + mElementIndex = Element.INDEX_16; mRS.triangleMeshBegin(mElementVertex, mElementIndex); mFSM = new FilmStripMesh(); @@ -231,9 +215,8 @@ public class FilmRS { mRS.scriptCSetRoot(true); mScriptStrip = mRS.scriptCCreate(); - mAllocPos = mRS.allocationCreatePredefSized( - RenderScript.ElementPredefined.USER_FLOAT, - mBufferPos.length); + mAllocPos = mRS.allocationCreateSized( + Element.USER_FLOAT, mBufferPos.length); loadImages(); initState(); @@ -250,15 +233,13 @@ public class FilmRS { mScriptStrip.bindAllocation(mPVA.mAlloc, 3); - mAllocOffsets = mRS.allocationCreatePredefSized( - RenderScript.ElementPredefined.USER_I32, - mFSM.mTriangleOffsets.length); + mAllocOffsets = mRS.allocationCreateSized( + Element.USER_I32, mFSM.mTriangleOffsets.length); mAllocOffsets.data(mFSM.mTriangleOffsets); mScriptStrip.bindAllocation(mAllocOffsets, 4); - mAllocOffsetsTex = mRS.allocationCreatePredefSized( - RenderScript.ElementPredefined.USER_FLOAT, - mFSM.mTriangleOffsetsTex.length); + mAllocOffsetsTex = mRS.allocationCreateSized( + Element.USER_FLOAT, mFSM.mTriangleOffsetsTex.length); mAllocOffsetsTex.data(mFSM.mTriangleOffsetsTex); mScriptStrip.bindAllocation(mAllocOffsetsTex, 5); diff --git a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java index c8e9a1ed9a7f7..654d6cf12ab05 100644 --- a/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java +++ b/libs/rs/java/Fountain/src/com/android/fountain/FountainRS.java @@ -23,9 +23,11 @@ import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; +import android.util.Log; + import android.renderscript.RenderScript; import android.renderscript.ProgramVertexAlloc; -import android.util.Log; +import android.renderscript.Element; public class FountainRS { @@ -65,10 +67,10 @@ public class FountainRS { private void initRS() { int partCount = 1024; - mIntAlloc = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, 10); - mPartAlloc = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, partCount * 3 * 3); + mIntAlloc = mRS.allocationCreateSized(Element.USER_I32, 10); + mPartAlloc = mRS.allocationCreateSized(Element.USER_I32, partCount * 3 * 3); mPartAlloc.setName("PartBuffer"); - mVertAlloc = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, partCount * 5 + 1); + mVertAlloc = mRS.allocationCreateSized(Element.USER_I32, partCount * 5 + 1); mRS.programFragmentStoreBegin(null, null); mRS.programFragmentStoreBlendFunc(RenderScript.BlendSrcFunc.SRC_ALPHA, RenderScript.BlendDstFunc.ONE); diff --git a/libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java b/libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java index bb13051edc727..f5737da3a1c24 100644 --- a/libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java +++ b/libs/rs/java/Grass/src/com/android/grass/rs/GrassRS.java @@ -17,8 +17,6 @@ package com.android.grass.rs; import android.content.res.Resources; -import android.renderscript.RenderScript; -import static android.renderscript.RenderScript.ElementPredefined.*; import static android.renderscript.RenderScript.SamplerParam.*; import static android.renderscript.RenderScript.SamplerValue.*; import static android.renderscript.RenderScript.EnvMode.*; @@ -26,6 +24,9 @@ import static android.renderscript.RenderScript.DepthFunc.*; import static android.renderscript.RenderScript.BlendSrcFunc; import static android.renderscript.RenderScript.BlendDstFunc; +import android.renderscript.RenderScript; +import android.renderscript.Element; + import java.util.TimeZone; class GrassRS { @@ -85,15 +86,15 @@ class GrassRS { } private void createScriptStructures() { - mState = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, 1); + mState = mRS.allocationCreateSized(Element.USER_I32, 1); mState.data(new int[1]); } private void loadSkyTextures() { mSkyBufferIDs = new int[SKY_TEXTURES_COUNT]; mSkyTextures = new RenderScript.Allocation[SKY_TEXTURES_COUNT]; - mSkyTexturesIDs = mRS.allocationCreatePredefSized( - USER_FLOAT, SKY_TEXTURES_COUNT); + mSkyTexturesIDs = mRS.allocationCreateSized( + Element.USER_FLOAT, SKY_TEXTURES_COUNT); final RenderScript.Allocation[] textures = mSkyTextures; textures[0] = loadTexture(R.drawable.night, "night"); @@ -115,7 +116,7 @@ class GrassRS { private RenderScript.Allocation loadTexture(int id, String name) { RenderScript.Allocation allocation = mRS.allocationCreateFromBitmapResource(mResources, id, - RGB_565, false); + Element.RGB_565, false); allocation.setName(name); return allocation; } diff --git a/libs/rs/java/Rollo/res/raw/rollo.c b/libs/rs/java/Rollo/res/raw/rollo.c index 8763a34c664d0..a5275888363a9 100644 --- a/libs/rs/java/Rollo/res/raw/rollo.c +++ b/libs/rs/java/Rollo/res/raw/rollo.c @@ -90,9 +90,6 @@ int main(void* con, int ft, int launchID) float ty1 = ((y * 3.1f) - 5.f) * scale; float ty2 = ty1 + scale * 1.8f; bindTexture(NAMED_PF, 0, loadI32(1, index)); - //if (done && (index != selectedID)) { - //color(0.4f, 0.4f, 0.4f, 1.0f); - //} drawQuad(tx1, ty1, tz1, tx2, ty1, tz2, tx2, ty2, tz2, diff --git a/libs/rs/java/Rollo/src/com/android/rollo/RolloMesh.java b/libs/rs/java/Rollo/src/com/android/rollo/RolloMesh.java deleted file mode 100644 index d7252fbb40ff0..0000000000000 --- a/libs/rs/java/Rollo/src/com/android/rollo/RolloMesh.java +++ /dev/null @@ -1,90 +0,0 @@ - /* - * 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 com.android.rollo; - -import java.io.Writer; -import java.lang.Math; - -import android.renderscript.RenderScript; - - -class RolloMesh { - static public final float mCardHeight = 1.2f; - static public final float mCardWidth = 1.8f; - static public final float mTabHeight = 0.2f; - static public final float mTabs = 3; - static public final float mTabGap = 0.1f; - - static RenderScript.TriangleMesh createCard(RenderScript rs) { - RenderScript.Element vtx = rs.elementGetPredefined( - RenderScript.ElementPredefined.ST_XYZ_F32); - RenderScript.Element idx = rs.elementGetPredefined( - RenderScript.ElementPredefined.INDEX_16); - - float w = mCardWidth / 2; - float h = mCardHeight; - float z = 0; - - rs.triangleMeshBegin(vtx, idx); - rs.triangleMeshAddVertex_XYZ_ST(-w, 0, z, 0, 0); - rs.triangleMeshAddVertex_XYZ_ST(-w, h, z, 0, 1); - rs.triangleMeshAddVertex_XYZ_ST( w, h, z, 1, 1); - rs.triangleMeshAddVertex_XYZ_ST( w, 0, z, 1, 0); - rs.triangleMeshAddTriangle(0,1,2); - rs.triangleMeshAddTriangle(0,2,3); - return rs.triangleMeshCreate(); - } - - static RenderScript.TriangleMesh createTab(RenderScript rs) { - RenderScript.Element vtx = rs.elementGetPredefined( - RenderScript.ElementPredefined.ST_XYZ_F32); - RenderScript.Element idx = rs.elementGetPredefined( - RenderScript.ElementPredefined.INDEX_16); - - - float tabSlope = 0.1f; - float num = 0; - - float w = (mCardWidth - ((mTabs - 1) * mTabGap)) / mTabs; - float w1 = -(mCardWidth / 2) + ((w + mTabGap) * num); - float w2 = w1 + (w * tabSlope); - float w3 = w1 + w - (w * tabSlope); - float w4 = w1 + w; - float h1 = mCardHeight; - float h2 = h1 + mTabHeight; - float z = 0; - - float stScale = w / mTabHeight / 2; - float stScale2 = stScale * (tabSlope / w); - - - rs.triangleMeshBegin(vtx, idx); - rs.triangleMeshAddVertex_XYZ_ST(w1, h1, z, -stScale, 0); - rs.triangleMeshAddVertex_XYZ_ST(w2, h2, z, -stScale2, 1); - rs.triangleMeshAddVertex_XYZ_ST(w3, h2, z, stScale2, 1); - rs.triangleMeshAddVertex_XYZ_ST(w4, h1, z, stScale, 0); - rs.triangleMeshAddTriangle(0,1,2); - rs.triangleMeshAddTriangle(0,2,3); - return rs.triangleMeshCreate(); - } - - - -} - - diff --git a/libs/rs/java/Rollo/src/com/android/rollo/RolloRS.java b/libs/rs/java/Rollo/src/com/android/rollo/RolloRS.java index 039d4e91af9ef..7f9727c0ef174 100644 --- a/libs/rs/java/Rollo/src/com/android/rollo/RolloRS.java +++ b/libs/rs/java/Rollo/src/com/android/rollo/RolloRS.java @@ -20,9 +20,8 @@ import java.io.Writer; import android.renderscript.RenderScript; import android.renderscript.ProgramVertexAlloc; +import android.renderscript.Element; -import android.content.Context; -import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; @@ -30,8 +29,9 @@ import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.Typeface; -import android.os.Handler; -import android.os.Message; + +import android.content.Context; +import android.content.res.Resources; import android.util.Log; public class RolloRS { @@ -190,8 +190,8 @@ public class RolloRS { mRS.contextBindProgramVertex(mPV); mAllocScratchBuf = new int[32]; - mAllocScratch = mRS.allocationCreatePredefSized( - RenderScript.ElementPredefined.USER_I32, mAllocScratchBuf.length); + mAllocScratch = mRS.allocationCreateSized( + Element.USER_I32, mAllocScratchBuf.length); mAllocScratch.data(mAllocScratchBuf); Log.e("rs", "Done loading named"); @@ -201,18 +201,15 @@ public class RolloRS { { mIcons = new RenderScript.Allocation[29]; mAllocIconIDBuf = new int[mIcons.length]; - mAllocIconID = mRS.allocationCreatePredefSized( - RenderScript.ElementPredefined.USER_I32, mAllocIconIDBuf.length); + mAllocIconID = mRS.allocationCreateSized( + Element.USER_I32, mAllocIconIDBuf.length); mLabels = new RenderScript.Allocation[29]; mAllocLabelIDBuf = new int[mLabels.length]; - mAllocLabelID = mRS.allocationCreatePredefSized( - RenderScript.ElementPredefined.USER_I32, mLabels.length); + mAllocLabelID = mRS.allocationCreateSized( + Element.USER_I32, mLabels.length); - RenderScript.ElementPredefined ie565 = - RenderScript.ElementPredefined.RGB_565; - RenderScript.ElementPredefined ie8888 = - RenderScript.ElementPredefined.RGBA_8888; + Element ie8888 = Element.RGBA_8888; mIcons[0] = mRS.allocationCreateFromBitmapResource(mRes, R.raw.browser, ie8888, true); mIcons[1] = mRS.allocationCreateFromBitmapResource(mRes, R.raw.market, ie8888, true); @@ -310,7 +307,7 @@ public class RolloRS { p.setTextSize(20); p.setColor(0xffffffff); c.drawText(t, 2, 26, p); - return mRS.allocationCreateFromBitmap(b, RenderScript.ElementPredefined.RGBA_8888, true); + return mRS.allocationCreateFromBitmap(b, Element.RGBA_8888, true); } @@ -323,8 +320,8 @@ public class RolloRS { mScript = mRS.scriptCCreate(); mAllocStateBuf = new int[] {0, 0, 0, 8, 0, 0, -1, 0, mAllocIconIDBuf.length, 0, 0}; - mAllocState = mRS.allocationCreatePredefSized( - RenderScript.ElementPredefined.USER_I32, mAllocStateBuf.length); + mAllocState = mRS.allocationCreateSized( + Element.USER_I32, mAllocStateBuf.length); mScript.bindAllocation(mAllocState, 0); mScript.bindAllocation(mAllocIconID, 1); mScript.bindAllocation(mAllocScratch, 2);