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

Commit 5476b450 authored by Jason Sams's avatar Jason Sams
Browse files

Allocation API update.

Change-Id: I9b4a71f9e94c7d3978f06b7971051ab4f8472503
parent af8962e4
Loading
Loading
Loading
Loading
+119 −54
Original line number Original line Diff line number Diff line
@@ -33,6 +33,15 @@ import android.util.TypedValue;
public class Allocation extends BaseObj {
public class Allocation extends BaseObj {
    Type mType;
    Type mType;
    Bitmap mBitmap;
    Bitmap mBitmap;
    int mUsage;

    public static final int USAGE_SCRIPT = 0x0001;
    public static final int USAGE_GRAPHICS_TEXTURE = 0x0002;
    public static final int USAGE_GRAPHICS_VERTEX = 0x0004;
    public static final int USAGE_GRAPHICS_CONSTANTS = 0x0008;

    private static final int USAGE_ALL = 0x000F;



    public enum CubemapLayout {
    public enum CubemapLayout {
        VERTICAL_FACE_LIST (0),
        VERTICAL_FACE_LIST (0),
@@ -46,13 +55,23 @@ public class Allocation extends BaseObj {
        }
        }
    }
    }


    Allocation(int id, RenderScript rs, Type t) {
    public enum MipmapGenerationControl {
        super(id, rs);
        MIPMAP_NONE(0),
        mType = t;
        MIPMAP_FULL(1),
        MIPMAP_ON_SYNC_TO_TEXTURE(2);

        int mID;
        MipmapGenerationControl(int id) {
            mID = id;
        }
    }
    }


    Allocation(int id, RenderScript rs) {
    Allocation(int id, RenderScript rs, Type t, int usage) {
        super(id, rs);
        super(id, rs);
        if (usage > USAGE_ALL) {
            throw new RSIllegalArgumentException("Unknown usage specified.");
        }
        mType = t;
    }
    }


    @Override
    @Override
@@ -69,6 +88,20 @@ public class Allocation extends BaseObj {
        return mType;
        return mType;
    }
    }


    public void syncAll(int srcLocation) {
        switch (srcLocation) {
        case USAGE_SCRIPT:
        case USAGE_GRAPHICS_CONSTANTS:
        case USAGE_GRAPHICS_TEXTURE:
        case USAGE_GRAPHICS_VERTEX:
            break;
        default:
            throw new RSIllegalArgumentException("Source must be exactly one usage type.");
        }
        mRS.validate();
        mRS.nAllocationSyncAll(getID(), srcLocation);
    }

    public void uploadToTexture(int baseMipLevel) {
    public void uploadToTexture(int baseMipLevel) {
        mRS.validate();
        mRS.validate();
        mRS.nAllocationUploadToTexture(getID(), false, baseMipLevel);
        mRS.nAllocationUploadToTexture(getID(), false, baseMipLevel);
@@ -242,6 +275,7 @@ public class Allocation extends BaseObj {
    }
    }
    */
    */


    /*
    public class Adapter1D extends BaseObj {
    public class Adapter1D extends BaseObj {
        Adapter1D(int id, RenderScript rs) {
        Adapter1D(int id, RenderScript rs) {
            super(id, rs);
            super(id, rs);
@@ -282,6 +316,7 @@ public class Allocation extends BaseObj {
        mRS.nAdapter1DBindAllocation(id, getID());
        mRS.nAdapter1DBindAllocation(id, getID());
        return new Adapter1D(id, mRS);
        return new Adapter1D(id, mRS);
    }
    }
    */




    public class Adapter2D extends BaseObj {
    public class Adapter2D extends BaseObj {
@@ -336,32 +371,38 @@ public class Allocation extends BaseObj {
        mBitmapOptions.inScaled = false;
        mBitmapOptions.inScaled = false;
    }
    }


    static public Allocation createTyped(RenderScript rs, Type type) {
    static public Allocation createTyped(RenderScript rs, Type type, int usage) {

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


    static public Allocation createSized(RenderScript rs, Element e, int count)
    static public Allocation createTyped(RenderScript rs, Type type) {
        throws IllegalArgumentException {
        return createTyped(rs, type, USAGE_ALL);
    }


    static public Allocation createSized(RenderScript rs, Element e,
                                         int count, int usage) {
        rs.validate();
        rs.validate();
        Type.Builder b = new Type.Builder(rs, e);
        Type.Builder b = new Type.Builder(rs, e);
        b.setX(count);
        b.setX(count);
        Type t = b.create();
        Type t = b.create();


        int id = rs.nAllocationCreateTyped(t.getID());
        int id = rs.nAllocationCreateTyped(t.getID(), usage);
        if (id == 0) {
        if (id == 0) {
            throw new RSRuntimeException("Allocation creation failed.");
            throw new RSRuntimeException("Allocation creation failed.");
        }
        }
        return new Allocation(id, rs, t);
        return new Allocation(id, rs, t, usage);
    }

    static public Allocation createSized(RenderScript rs, Element e, int count) {
        return createSized(rs, e, count, USAGE_ALL);
    }
    }


    static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
    static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
@@ -381,32 +422,44 @@ public class Allocation extends BaseObj {
        throw new RSInvalidStateException("Bad bitmap type: " + bc);
        throw new RSInvalidStateException("Bad bitmap type: " + bc);
    }
    }


    static private Type typeFromBitmap(RenderScript rs, Bitmap b, boolean mip) {
    static private Type typeFromBitmap(RenderScript rs, Bitmap b,
                                       MipmapGenerationControl mip) {
        Element e = elementFromBitmap(rs, b);
        Element e = elementFromBitmap(rs, b);
        Type.Builder tb = new Type.Builder(rs, e);
        Type.Builder tb = new Type.Builder(rs, e);
        tb.setX(b.getWidth());
        tb.setX(b.getWidth());
        tb.setY(b.getHeight());
        tb.setY(b.getHeight());
        tb.setMipmaps(mip);
        tb.setMipmaps(mip == MipmapGenerationControl.MIPMAP_FULL);
        return tb.create();
        return tb.create();
    }
    }


    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
                                              Element dstFmt, boolean genMips) {
                                              MipmapGenerationControl mips,
                                              int usage) {
        rs.validate();
        rs.validate();
        Type t = typeFromBitmap(rs, b, genMips);
        Type t = typeFromBitmap(rs, b, mips);


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

    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
                                              Element dstFmt, boolean genMips) {
        MipmapGenerationControl mc = MipmapGenerationControl.MIPMAP_NONE;
        if (genMips) {
            mc = MipmapGenerationControl.MIPMAP_ON_SYNC_TO_TEXTURE;
        }
        return createFromBitmap(rs, b, mc, USAGE_ALL);
    }
    }


    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
                                                     Element dstFmt,
                                                     MipmapGenerationControl mips,
                                                     boolean genMips,
                                                     CubemapLayout layout,
                                                     CubemapLayout layout) {
                                                     int usage) {
        rs.validate();
        rs.validate();

        int height = b.getHeight();
        int height = b.getHeight();
        int width = b.getWidth();
        int width = b.getWidth();


@@ -429,64 +482,76 @@ public class Allocation extends BaseObj {
        tb.setX(width);
        tb.setX(width);
        tb.setY(width);
        tb.setY(width);
        tb.setFaces(true);
        tb.setFaces(true);
        tb.setMipmaps(genMips);
        tb.setMipmaps(mips == MipmapGenerationControl.MIPMAP_FULL);
        Type t = tb.create();
        Type t = tb.create();


        int id = rs.nAllocationCubeCreateFromBitmap(dstFmt.getID(), genMips, b);
        int id = rs.nAllocationCubeCreateFromBitmap(t.getID(), mips.mID, b, usage);
        if(id == 0) {
        if(id == 0) {
            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
            throw new RSRuntimeException("Load failed for bitmap " + b + " element " + e);
        }
        }
        return new Allocation(id, rs, t);
        return new Allocation(id, rs, t, usage);
    }
    }


    static public Allocation createCubemapFromBitmap(RenderScript rs, Bitmap b,
                                                     Element dstFmt,
                                                     boolean genMips,
                                                     CubemapLayout layout) {
        MipmapGenerationControl mc = MipmapGenerationControl.MIPMAP_NONE;
        if (genMips) {
            mc = MipmapGenerationControl.MIPMAP_ON_SYNC_TO_TEXTURE;
        }
        return createCubemapFromBitmap(rs, b, mc, layout, USAGE_ALL);
    }


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


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


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


        Allocation a = new Allocation(id, rs, t);
        Allocation a = new Allocation(id, rs, t, USAGE_SCRIPT);
        a.mBitmap = b;
        a.mBitmap = b;
        return a;
        return a;
    }
    }


    static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips) {
    static public Allocation createFromBitmapResource(RenderScript rs,
                                                      Resources res,
                                                      int id,
                                                      MipmapGenerationControl mips,
                                                      int usage) {


        rs.validate();
        rs.validate();
        InputStream is = null;
        Bitmap b = BitmapFactory.decodeResource(res, id);
        try {
        Allocation alloc = createFromBitmap(rs, b, mips, usage);
            final TypedValue value = new TypedValue();
        b.recycle();
            is = res.openRawResource(id, value);

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

            if (aId == 0) {
                throw new RSRuntimeException("Load failed.");
            }
            Allocation alloc = new Allocation(aId, rs, null);
            alloc.updateFromNative();
        return alloc;
        return alloc;
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // Ignore
                }
    }
    }

    static public Allocation createFromBitmapResource(RenderScript rs,
                                                      Resources res,
                                                      int id,
                                                      Element dstFmt,
                                                      boolean genMips) {
        MipmapGenerationControl mc = MipmapGenerationControl.MIPMAP_NONE;
        if (genMips) {
            mc = MipmapGenerationControl.MIPMAP_ON_SYNC_TO_TEXTURE;
        }
        }
        return createFromBitmapResource(rs, res, id, mc, USAGE_ALL);
    }
    }


    static public Allocation createFromString(RenderScript rs, String str) {
    static public Allocation createFromString(RenderScript rs,
                                              String str,
                                              int usage) {
        rs.validate();
        byte[] allocArray = null;
        byte[] allocArray = null;
        try {
        try {
            allocArray = str.getBytes("UTF-8");
            allocArray = str.getBytes("UTF-8");
            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length);
            Allocation alloc = Allocation.createSized(rs, Element.U8(rs), allocArray.length, usage);
            alloc.copyFrom(allocArray);
            alloc.copyFrom(allocArray);
            return alloc;
            return alloc;
        }
        }
+3 −0
Original line number Original line Diff line number Diff line
@@ -52,6 +52,9 @@ class BaseObj {
        if (mDestroyed) {
        if (mDestroyed) {
            throw new RSInvalidStateException("using a destroyed object.");
            throw new RSInvalidStateException("using a destroyed object.");
        }
        }
        if (mID == 0) {
            throw new RSRuntimeException("Internal error: Object id 0.");
        }
        return mID;
        return mID;
    }
    }


+6 −2
Original line number Original line Diff line number Diff line
@@ -77,14 +77,18 @@ public class Mesh extends BaseObj {


        for(int i = 0; i < vtxCount; i ++) {
        for(int i = 0; i < vtxCount; i ++) {
            if(vtxIDs[i] != 0) {
            if(vtxIDs[i] != 0) {
                mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS);
                mVertexBuffers[i] = new Allocation(vtxIDs[i], mRS, null,
                                                   Allocation.USAGE_GRAPHICS_VERTEX |
                                                   Allocation.USAGE_SCRIPT);
                mVertexBuffers[i].updateFromNative();
                mVertexBuffers[i].updateFromNative();
            }
            }
        }
        }


        for(int i = 0; i < idxCount; i ++) {
        for(int i = 0; i < idxCount; i ++) {
            if(idxIDs[i] != 0) {
            if(idxIDs[i] != 0) {
                mIndexBuffers[i] = new Allocation(idxIDs[i], mRS);
                mIndexBuffers[i] = new Allocation(idxIDs[i], mRS, null,
                                                  Allocation.USAGE_GRAPHICS_VERTEX |
                                                  Allocation.USAGE_SCRIPT);
                mIndexBuffers[i].updateFromNative();
                mIndexBuffers[i].updateFromNative();
            }
            }
            mPrimitives[i] = Primitive.values()[primitives[i]];
            mPrimitives[i] = Primitive.values()[primitives[i]];
+21 −16
Original line number Original line Diff line number Diff line
@@ -192,29 +192,34 @@ public class RenderScript {
        rsnTypeGetNativeData(mContext, id, typeData);
        rsnTypeGetNativeData(mContext, id, typeData);
    }
    }


    native int  rsnAllocationCreateTyped(int con, int type);
    native int  rsnAllocationCreateTyped(int con, int type, int usage);
    synchronized int nAllocationCreateTyped(int type) {
    synchronized int nAllocationCreateTyped(int type, int usage) {
        return rsnAllocationCreateTyped(mContext, type);
        return rsnAllocationCreateTyped(mContext, type, usage);
    }
    }
    native void  rsnAllocationUpdateFromBitmap(int con, int alloc, Bitmap bmp);
    native int  rsnAllocationCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
    synchronized void nAllocationUpdateFromBitmap(int alloc, Bitmap bmp) {
    synchronized int nAllocationCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
        rsnAllocationUpdateFromBitmap(mContext, alloc, bmp);
        return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage);
    }
    }
    native int  rsnAllocationCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp);
    native int  rsnAllocationCubeCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
    synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
    synchronized int nAllocationCubeCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
        return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp);
        return rsnAllocationCubeCreateFromBitmap(mContext, type, mip, bmp, usage);
    }
    native int  rsnAllocationCubeCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp);
    synchronized int nAllocationCubeCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
        return rsnAllocationCubeCreateFromBitmap(mContext, dstFmt, genMips, bmp);
    }
    }
    native int  rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
    native int  rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
    synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
    synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
        return rsnAllocationCreateBitmapRef(mContext, type, bmp);
        return rsnAllocationCreateBitmapRef(mContext, type, bmp);
    }
    }
    native int  rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream);
    native int  rsnAllocationCreateFromAssetStream(int con, int mips, int assetStream, int usage);
    synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) {
    synchronized int nAllocationCreateFromAssetStream(int mips, int assetStream, int usage) {
        return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream);
        return rsnAllocationCreateFromAssetStream(mContext, mips, assetStream, usage);
    }

    native void rsnAllocationSyncAll(int con, int alloc, int src);
    synchronized void nAllocationSyncAll(int alloc, int src) {
        rsnAllocationSyncAll(mContext, alloc, src);
    }
    native void  rsnAllocationUpdateFromBitmap(int con, int alloc, Bitmap bmp);
    synchronized void nAllocationUpdateFromBitmap(int alloc, Bitmap bmp) {
        rsnAllocationUpdateFromBitmap(mContext, alloc, bmp);
    }
    }


    native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
    native void rsnAllocationUploadToTexture(int con, int alloc, boolean genMips, int baseMioLevel);
+5 −1
Original line number Original line Diff line number Diff line
@@ -119,7 +119,11 @@ public class Script extends BaseObj {
        protected Allocation mAllocation;
        protected Allocation mAllocation;


        protected void init(RenderScript rs, int dimx) {
        protected void init(RenderScript rs, int dimx) {
            mAllocation = Allocation.createSized(rs, mElement, dimx);
            mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT);
        }

        protected void init(RenderScript rs, int dimx, int usages) {
            mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT | usages);
        }
        }


        protected FieldBase() {
        protected FieldBase() {
Loading