Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 06d69de7 authored by Jason Sams's avatar Jason Sams
Browse files

Continue error check improvements and write some docs.

Change-Id: I345b08490da21d9ee715ff1ddc80c1a816b579f4
parent 641b0628
Loading
Loading
Loading
Loading
+71 −78
Original line number Diff line number Diff line
@@ -45,9 +45,8 @@ public class Allocation extends BaseObj {

    @Override
    void updateFromNative() {
        mRS.validate();
        mName = mRS.nGetName(mID);
        int typeID = mRS.nAllocationGetType(mID);
        super.updateFromNative();
        int typeID = mRS.nAllocationGetType(getID());
        if(typeID != 0) {
            mType = new Type(typeID, mRS);
            mType.updateFromNative();
@@ -60,17 +59,17 @@ public class Allocation extends BaseObj {

    public void uploadToTexture(int baseMipLevel) {
        mRS.validate();
        mRS.nAllocationUploadToTexture(mID, false, baseMipLevel);
        mRS.nAllocationUploadToTexture(getID(), false, baseMipLevel);
    }

    public void uploadToTexture(boolean genMips, int baseMipLevel) {
        mRS.validate();
        mRS.nAllocationUploadToTexture(mID, genMips, baseMipLevel);
        mRS.nAllocationUploadToTexture(getID(), genMips, baseMipLevel);
    }

    public void uploadToBufferObject() {
        mRS.validate();
        mRS.nAllocationUploadToBufferObject(mID);
        mRS.nAllocationUploadToBufferObject(getID());
    }

    public void data(int[] d) {
@@ -90,16 +89,15 @@ public class Allocation extends BaseObj {
        subData1D(0, mType.getElementCount(), d);
    }

    public void updateFromBitmap(Bitmap b)
        throws IllegalArgumentException {
    public void updateFromBitmap(Bitmap b) {

        mRS.validate();
        if(mType.getX() != b.getWidth() ||
           mType.getY() != b.getHeight()) {
            throw new IllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
            throw new RSIllegalArgumentException("Cannot update allocation from bitmap, sizes mismatch");
        }

        mRS.nAllocationUpdateFromBitmap(mID, b);
        mRS.nAllocationUpdateFromBitmap(getID(), b);
    }

    public void subData(int xoff, FieldPacker fp) {
@@ -108,100 +106,100 @@ public class Allocation extends BaseObj {

        int count = data.length / eSize;
        if ((eSize * count) != data.length) {
            throw new IllegalArgumentException("Field packer length " + data.length +
            throw new RSIllegalArgumentException("Field packer length " + data.length +
                                               " not divisible by element size " + eSize + ".");
        }
        data1DChecks(xoff, count, data.length, data.length);
        mRS.nAllocationSubData1D(mID, xoff, count, data, data.length);
        mRS.nAllocationSubData1D(getID(), xoff, count, data, data.length);
    }


    public void subElementData(int xoff, int component_number, FieldPacker fp) {
        if (component_number >= mType.mElement.mElements.length) {
            throw new IllegalArgumentException("Component_number " + component_number + " out of range.");
            throw new RSIllegalArgumentException("Component_number " + component_number + " out of range.");
        }
        if(xoff < 0) {
            throw new IllegalArgumentException("Offset must be >= 0.");
            throw new RSIllegalArgumentException("Offset must be >= 0.");
        }

        final byte[] data = fp.getData();
        int eSize = mType.mElement.mElements[component_number].getSizeBytes();

        if (data.length != eSize) {
            throw new IllegalArgumentException("Field packer sizelength " + data.length +
            throw new RSIllegalArgumentException("Field packer sizelength " + data.length +
                                               " does not match component size " + eSize + ".");
        }

        mRS.nAllocationSubElementData1D(mID, xoff, component_number, data, data.length);
        mRS.nAllocationSubElementData1D(getID(), xoff, component_number, data, data.length);
    }

    private void data1DChecks(int off, int count, int len, int dataSize) {
        mRS.validate();
        if(off < 0) {
            throw new IllegalArgumentException("Offset must be >= 0.");
            throw new RSIllegalArgumentException("Offset must be >= 0.");
        }
        if(count < 1) {
            throw new IllegalArgumentException("Count must be >= 1.");
            throw new RSIllegalArgumentException("Count must be >= 1.");
        }
        if((off + count) > mType.getElementCount()) {
            throw new IllegalArgumentException("Overflow, Available count " + mType.getElementCount() +
            throw new RSIllegalArgumentException("Overflow, Available count " + mType.getElementCount() +
                                               ", got " + count + " at offset " + off + ".");
        }
        if((len) < dataSize) {
            throw new IllegalArgumentException("Array too small for allocation type.");
            throw new RSIllegalArgumentException("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);
        mRS.nAllocationSubData1D(getID(), 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);
        mRS.nAllocationSubData1D(getID(), 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);
        mRS.nAllocationSubData1D(getID(), 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);
        mRS.nAllocationSubData1D(getID(), 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);
        mRS.nAllocationSubData2D(getID(), 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);
        mRS.nAllocationSubData2D(getID(), xoff, yoff, w, h, d, d.length * 4);
    }

    public void readData(int[] d) {
        mRS.validate();
        mRS.nAllocationRead(mID, d);
        mRS.nAllocationRead(getID(), d);
    }

    public void readData(float[] d) {
        mRS.validate();
        mRS.nAllocationRead(mID, d);
        mRS.nAllocationRead(getID(), d);
    }

    public synchronized void resize(int dimX) {
        if ((mType.getY() > 0)|| (mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
            throw new IllegalStateException("Resize only support for 1D allocations at this time.");
            throw new RSInvalidStateException("Resize only support for 1D allocations at this time.");
        }
        mRS.nAllocationResize1D(mID, dimX);
        mRS.nAllocationResize1D(getID(), dimX);
        mRS.finish();  // Necessary because resize is fifoed and update is async.

        int typeID = mRS.nAllocationGetType(mID);
        int typeID = mRS.nAllocationGetType(getID());
        mType = new Type(typeID, mRS);
        mType.updateFromNative();
    }
@@ -209,12 +207,12 @@ public class Allocation extends BaseObj {
    /*
    public void resize(int dimX, int dimY) {
        if ((mType.getZ() > 0) || mType.getFaces() || mType.getLOD()) {
            throw new IllegalStateException("Resize only support for 2D allocations at this time.");
            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
        }
        if (mType.getY() == 0) {
            throw new IllegalStateException("Resize only support for 2D allocations at this time.");
            throw new RSIllegalStateException("Resize only support for 2D allocations at this time.");
        }
        mRS.nAllocationResize2D(mID, dimX, dimY);
        mRS.nAllocationResize2D(getID(), dimX, dimY);
    }
    */

@@ -225,27 +223,27 @@ public class Allocation extends BaseObj {

        public void setConstraint(Dimension dim, int value) {
            mRS.validate();
            mRS.nAdapter1DSetConstraint(mID, dim.mID, value);
            mRS.nAdapter1DSetConstraint(getID(), dim.mID, value);
        }

        public void data(int[] d) {
            mRS.validate();
            mRS.nAdapter1DData(mID, d);
            mRS.nAdapter1DData(getID(), d);
        }

        public void data(float[] d) {
            mRS.validate();
            mRS.nAdapter1DData(mID, d);
            mRS.nAdapter1DData(getID(), d);
        }

        public void subData(int off, int count, int[] d) {
            mRS.validate();
            mRS.nAdapter1DSubData(mID, off, count, d);
            mRS.nAdapter1DSubData(getID(), off, count, d);
        }

        public void subData(int off, int count, float[] d) {
            mRS.validate();
            mRS.nAdapter1DSubData(mID, off, count, d);
            mRS.nAdapter1DSubData(getID(), off, count, d);
        }
    }

@@ -253,9 +251,9 @@ public class Allocation extends BaseObj {
        mRS.validate();
        int id = mRS.nAdapter1DCreate();
        if(id == 0) {
            throw new IllegalStateException("allocation failed.");
            throw new RSRuntimeException("Adapter creation failed.");
        }
        mRS.nAdapter1DBindAllocation(id, mID);
        mRS.nAdapter1DBindAllocation(id, getID());
        return new Adapter1D(id, mRS);
    }

@@ -267,27 +265,27 @@ public class Allocation extends BaseObj {

        public void setConstraint(Dimension dim, int value) {
            mRS.validate();
            mRS.nAdapter2DSetConstraint(mID, dim.mID, value);
            mRS.nAdapter2DSetConstraint(getID(), dim.mID, value);
        }

        public void data(int[] d) {
            mRS.validate();
            mRS.nAdapter2DData(mID, d);
            mRS.nAdapter2DData(getID(), d);
        }

        public void data(float[] d) {
            mRS.validate();
            mRS.nAdapter2DData(mID, d);
            mRS.nAdapter2DData(getID(), d);
        }

        public void subData(int xoff, int yoff, int w, int h, int[] d) {
            mRS.validate();
            mRS.nAdapter2DSubData(mID, xoff, yoff, w, h, d);
            mRS.nAdapter2DSubData(getID(), 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);
            mRS.nAdapter2DSubData(getID(), xoff, yoff, w, h, d);
        }
    }

@@ -295,9 +293,12 @@ public class Allocation extends BaseObj {
        mRS.validate();
        int id = mRS.nAdapter2DCreate();
        if(id == 0) {
            throw new IllegalStateException("allocation failed.");
            throw new RSRuntimeException("allocation failed.");
        }
        mRS.nAdapter2DBindAllocation(id, getID());
        if(id == 0) {
            throw new RSRuntimeException("Adapter creation failed.");
        }
        mRS.nAdapter2DBindAllocation(id, mID);
        return new Adapter2D(id, mRS);
    }

@@ -309,14 +310,16 @@ public class Allocation extends BaseObj {
        mBitmapOptions.inScaled = false;
    }

    static public Allocation createTyped(RenderScript rs, Type type)
        throws IllegalArgumentException {
    static public Allocation createTyped(RenderScript rs, Type type) {

        rs.validate();
        if(type.mID == 0) {
            throw new IllegalStateException("Bad Type");
        if(type.getID() == 0) {
            throw new RSInvalidStateException("Bad Type");
        }
        int id = rs.nAllocationCreateTyped(type.getID());
        if(id == 0) {
            throw new RSRuntimeException("Allocation creation failed.");
        }
        int id = rs.nAllocationCreateTyped(type.mID);
        return new Allocation(id, rs, type);
    }

@@ -328,9 +331,9 @@ public class Allocation extends BaseObj {
        b.add(Dimension.X, count);
        Type t = b.create();

        int id = rs.nAllocationCreateTyped(t.mID);
        int id = rs.nAllocationCreateTyped(t.getID());
        if(id == 0) {
            throw new IllegalStateException("Bad element.");
            throw new RSRuntimeException("Allocation creation failed.");
        }
        return new Allocation(id, rs, t);
    }
@@ -349,7 +352,7 @@ public class Allocation extends BaseObj {
        if (bc == Bitmap.Config.RGB_565) {
            return Element.RGB_565(rs);
        }
        throw new IllegalStateException("Bad bitmap type.");
        throw new RSInvalidStateException("Bad bitmap type.");
    }

    static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
@@ -360,28 +363,26 @@ public class Allocation extends BaseObj {
        return tb.create();
    }

    static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
        throws IllegalArgumentException {
    static public Allocation createFromBitmap(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips) {

        rs.validate();
        Type t = typeFromBitmap(rs, b);

        int id = rs.nAllocationCreateFromBitmap(dstFmt.mID, genMips, b);
        int id = rs.nAllocationCreateFromBitmap(dstFmt.getID(), genMips, b);
        if(id == 0) {
            throw new IllegalStateException("Load failed.");
            throw new RSRuntimeException("Load failed.");
        }
        return new Allocation(id, rs, t);
    }

    static public Allocation createBitmapRef(RenderScript rs, Bitmap b)
        throws IllegalArgumentException {
    static public Allocation createBitmapRef(RenderScript rs, Bitmap b) {

        rs.validate();
        Type t = typeFromBitmap(rs, b);

        int id = rs.nAllocationCreateBitmapRef(t.getID(), b);
        if(id == 0) {
            throw new IllegalStateException("Load failed.");
            throw new RSRuntimeException("Load failed.");
        }

        Allocation a = new Allocation(id, rs, t);
@@ -389,8 +390,7 @@ public class Allocation extends BaseObj {
        return a;
    }

    static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
        throws IllegalArgumentException {
    static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) {

        rs.validate();
        InputStream is = null;
@@ -399,15 +399,12 @@ public class Allocation extends BaseObj {
            is = res.openRawResource(id, value);

            int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
            int allocationId = rs.nAllocationCreateFromAssetStream(dstFmt.mID, genMips,
                    asset);
            int aId = rs.nAllocationCreateFromAssetStream(dstFmt.getID(), genMips, asset);

            if(allocationId == 0) {
                throw new IllegalStateException("Load failed.");
            if (aId == 0) {
                throw new RSRuntimeException("Load failed.");
            }
            return new Allocation(allocationId, rs, null);
        } catch (Exception e) {
            // Ignore
            return new Allocation(aId, rs, null);
        } finally {
            if (is != null) {
                try {
@@ -417,12 +414,9 @@ public class Allocation extends BaseObj {
                }
            }
        }

        return null;
    }

    static public Allocation createFromString(RenderScript rs, String str)
        throws IllegalArgumentException {
    static public Allocation createFromString(RenderScript rs, String str) {
        byte[] allocArray = null;
        try {
            allocArray = str.getBytes("UTF-8");
@@ -431,9 +425,8 @@ public class Allocation extends BaseObj {
            return alloc;
        }
        catch (Exception e) {
            Log.e("rs", "could not convert string to utf-8");
            throw new RSRuntimeException("Could not convert string to utf-8.");
        }
        return null;
    }
}

+17 −5
Original line number Diff line number Diff line
@@ -21,9 +21,12 @@ import android.util.Log;
/**
 * @hide
 *
 * BaseObj is the base class for interfacing with native renderscript objects.
 * It primarly contains code for tracking the native object ID and forcably
 * disconecting the object from the native allocation for early cleanup.
 *
 **/
class BaseObj {

    BaseObj(int id, RenderScript rs) {
        rs.validate();
        mRS = rs;
@@ -31,6 +34,13 @@ class BaseObj {
        mDestroyed = false;
    }

    void setID(int id) {
        if (mID != 0) {
            throw new RSRuntimeException("Internal Error, reset of object ID.");
        }
        mID = id;
    }

    public int getID() {
        if (mDestroyed) {
            throw new RSInvalidStateException("using a destroyed object.");
@@ -38,9 +48,9 @@ class BaseObj {
        return mID;
    }

    int mID;
    boolean mDestroyed;
    String mName;
    private int mID;
    private boolean mDestroyed;
    private String mName;
    RenderScript mRS;

    public void setName(String s) {
@@ -74,7 +84,7 @@ class BaseObj {
        super.finalize();
    }

    public void destroy() {
    synchronized public void destroy() {
        if(mDestroyed) {
            throw new RSInvalidStateException("Object already destroyed.");
        }
@@ -85,6 +95,8 @@ class BaseObj {
    // If an object came from an a3d file, java fields need to be
    // created with objects from the native layer
    void updateFromNative() {
        mRS.validate();
        mName = mRS.nGetName(getID());
    }

}
+4 −3
Original line number Diff line number Diff line
@@ -373,10 +373,11 @@ public class Element extends BaseObj {

    @Override
    void updateFromNative() {
        super.updateFromNative();

        // we will pack mType; mKind; mNormalized; mVectorSize; NumSubElements
        int[] dataBuffer = new int[5];
        mRS.nElementGetNativeData(mID, dataBuffer);
        mRS.nElementGetNativeData(getID(), dataBuffer);

        mNormalized = dataBuffer[2] == 1 ? true : false;
        mVectorSize = dataBuffer[3];
@@ -399,7 +400,7 @@ public class Element extends BaseObj {
            mElementNames = new String[numSubElements];

            int[] subElementIds = new int[numSubElements];
            mRS.nElementGetSubElements(mID, subElementIds, mElementNames);
            mRS.nElementGetSubElements(getID(), subElementIds, mElementNames);
            for(int i = 0; i < numSubElements; i ++) {
                mElements[i] = new Element(subElementIds[i], mRS);
                mElements[i].updateFromNative();
@@ -523,7 +524,7 @@ public class Element extends BaseObj {

            int[] ids = new int[ein.length];
            for (int ct = 0; ct < ein.length; ct++ ) {
                ids[ct] = ein[ct].mID;
                ids[ct] = ein[ct].getID();
            }
            int id = mRS.nElementCreate2(ids, sin, asin);
            return new Element(id, mRS, ein, sin, asin);
+3 −3
Original line number Diff line number Diff line
@@ -149,7 +149,7 @@ public class FileA3D extends BaseObj {
    }

    private void initEntries() {
        int numFileEntries = mRS.nFileA3DGetNumIndexEntries(mID);
        int numFileEntries = mRS.nFileA3DGetNumIndexEntries(getID());
        if(numFileEntries <= 0) {
            return;
        }
@@ -158,10 +158,10 @@ public class FileA3D extends BaseObj {
        int[] ids = new int[numFileEntries];
        String[] names = new String[numFileEntries];

        mRS.nFileA3DGetIndexEntries(mID, numFileEntries, ids, names);
        mRS.nFileA3DGetIndexEntries(getID(), numFileEntries, ids, names);

        for(int i = 0; i < numFileEntries; i ++) {
            mFileEntries[i] = new IndexEntry(mRS, i, mID, names[i], ClassID.toClassID(ids[i]));
            mFileEntries[i] = new IndexEntry(mRS, i, getID(), names[i], ClassID.toClassID(ids[i]));
        }
    }

+6 −6
Original line number Diff line number Diff line
@@ -60,16 +60,16 @@ public class Mesh extends BaseObj {

    @Override
    void updateFromNative() {
        mName = mRS.nGetName(mID);
        int vtxCount = mRS.nMeshGetVertexBufferCount(mID);
        int idxCount = mRS.nMeshGetIndexCount(mID);
        super.updateFromNative();
        int vtxCount = mRS.nMeshGetVertexBufferCount(getID());
        int idxCount = mRS.nMeshGetIndexCount(getID());

        int[] vtxIDs = new int[vtxCount];
        int[] idxIDs = new int[idxCount];
        int[] primitives = new int[idxCount];

        mRS.nMeshGetVertices(mID, vtxIDs, vtxCount);
        mRS.nMeshGetIndices(mID, idxIDs, primitives, idxCount);
        mRS.nMeshGetVertices(getID(), vtxIDs, vtxCount);
        mRS.nMeshGetIndices(getID(), idxIDs, primitives, idxCount);

        mVertexBuffers = new Allocation[vtxCount];
        mIndexBuffers = new Allocation[idxCount];
@@ -292,7 +292,7 @@ public class Mesh extends BaseObj {

            for(int ct = 0; ct < b.mVertexTypeCount; ct ++) {
                Entry entry = b.mVertexTypes[ct];
                rs.nMeshBindVertex(id, entry.a.mID, ct);
                rs.nMeshBindVertex(id, entry.a.getID(), ct);
                newMesh.mVertexBuffers[ct] = entry.a;
            }
            rs.nMeshInitVertexAttribs(id);
Loading