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

Commit 3247842c authored by Jason Sams's avatar Jason Sams Committed by Android (Google) Code Review
Browse files

Merge "Allocation API update."

parents 3345f27f 5476b450
Loading
Loading
Loading
Loading
+119 −54
Original line number Diff line number Diff line
@@ -33,6 +33,15 @@ import android.util.TypedValue;
public class Allocation extends BaseObj {
    Type mType;
    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 {
        VERTICAL_FACE_LIST (0),
@@ -46,13 +55,23 @@ public class Allocation extends BaseObj {
        }
    }

    Allocation(int id, RenderScript rs, Type t) {
        super(id, rs);
        mType = t;
    public enum MipmapGenerationControl {
        MIPMAP_NONE(0),
        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);
        if (usage > USAGE_ALL) {
            throw new RSIllegalArgumentException("Unknown usage specified.");
        }
        mType = t;
    }

    @Override
@@ -69,6 +88,20 @@ public class Allocation extends BaseObj {
        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) {
        mRS.validate();
        mRS.nAllocationUploadToTexture(getID(), false, baseMipLevel);
@@ -242,6 +275,7 @@ public class Allocation extends BaseObj {
    }
    */

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


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

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

    static public Allocation createTyped(RenderScript rs, Type type, int usage) {
        rs.validate();
        if (type.getID() == 0) {
            throw new RSInvalidStateException("Bad Type");
        }
        int id = rs.nAllocationCreateTyped(type.getID());
        int id = rs.nAllocationCreateTyped(type.getID(), usage);
        if (id == 0) {
            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)
        throws IllegalArgumentException {
    static public Allocation createTyped(RenderScript rs, Type type) {
        return createTyped(rs, type, USAGE_ALL);
    }

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

        int id = rs.nAllocationCreateTyped(t.getID());
        int id = rs.nAllocationCreateTyped(t.getID(), usage);
        if (id == 0) {
            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) {
@@ -381,32 +422,44 @@ public class Allocation extends BaseObj {
        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);
        Type.Builder tb = new Type.Builder(rs, e);
        tb.setX(b.getWidth());
        tb.setY(b.getHeight());
        tb.setMipmaps(mip);
        tb.setMipmaps(mip == MipmapGenerationControl.MIPMAP_FULL);
        return tb.create();
    }

    static public Allocation createFromBitmap(RenderScript rs, Bitmap b,
                                              Element dstFmt, boolean genMips) {
                                              MipmapGenerationControl mips,
                                              int usage) {
        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) {
            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,
                                                     Element dstFmt,
                                                     boolean genMips,
                                                     CubemapLayout layout) {
                                                     MipmapGenerationControl mips,
                                                     CubemapLayout layout,
                                                     int usage) {
        rs.validate();

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

@@ -429,64 +482,76 @@ public class Allocation extends BaseObj {
        tb.setX(width);
        tb.setY(width);
        tb.setFaces(true);
        tb.setMipmaps(genMips);
        tb.setMipmaps(mips == MipmapGenerationControl.MIPMAP_FULL);
        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) {
            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) {

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

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

        Allocation a = new Allocation(id, rs, t);
        Allocation a = new Allocation(id, rs, t, USAGE_SCRIPT);
        a.mBitmap = b;
        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();
        InputStream is = null;
        try {
            final TypedValue value = new TypedValue();
            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();
        Bitmap b = BitmapFactory.decodeResource(res, id);
        Allocation alloc = createFromBitmap(rs, b, mips, usage);
        b.recycle();
        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;
        try {
            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);
            return alloc;
        }
+3 −0
Original line number Diff line number Diff line
@@ -52,6 +52,9 @@ class BaseObj {
        if (mDestroyed) {
            throw new RSInvalidStateException("using a destroyed object.");
        }
        if (mID == 0) {
            throw new RSRuntimeException("Internal error: Object id 0.");
        }
        return mID;
    }

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

        for(int i = 0; i < vtxCount; i ++) {
            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();
            }
        }

        for(int i = 0; i < idxCount; i ++) {
            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();
            }
            mPrimitives[i] = Primitive.values()[primitives[i]];
+21 −16
Original line number Diff line number Diff line
@@ -192,29 +192,34 @@ public class RenderScript {
        rsnTypeGetNativeData(mContext, id, typeData);
    }

    native int  rsnAllocationCreateTyped(int con, int type);
    synchronized int nAllocationCreateTyped(int type) {
        return rsnAllocationCreateTyped(mContext, type);
    native int  rsnAllocationCreateTyped(int con, int type, int usage);
    synchronized int nAllocationCreateTyped(int type, int usage) {
        return rsnAllocationCreateTyped(mContext, type, usage);
    }
    native void  rsnAllocationUpdateFromBitmap(int con, int alloc, Bitmap bmp);
    synchronized void nAllocationUpdateFromBitmap(int alloc, Bitmap bmp) {
        rsnAllocationUpdateFromBitmap(mContext, alloc, bmp);
    native int  rsnAllocationCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
    synchronized int nAllocationCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
        return rsnAllocationCreateFromBitmap(mContext, type, mip, bmp, usage);
    }
    native int  rsnAllocationCreateFromBitmap(int con, int dstFmt, boolean genMips, Bitmap bmp);
    synchronized int nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp) {
        return rsnAllocationCreateFromBitmap(mContext, dstFmt, genMips, bmp);
    }
    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  rsnAllocationCubeCreateFromBitmap(int con, int type, int mip, Bitmap bmp, int usage);
    synchronized int nAllocationCubeCreateFromBitmap(int type, int mip, Bitmap bmp, int usage) {
        return rsnAllocationCubeCreateFromBitmap(mContext, type, mip, bmp, usage);
    }
    native int  rsnAllocationCreateBitmapRef(int con, int type, Bitmap bmp);
    synchronized int nAllocationCreateBitmapRef(int type, Bitmap bmp) {
        return rsnAllocationCreateBitmapRef(mContext, type, bmp);
    }
    native int  rsnAllocationCreateFromAssetStream(int con, int dstFmt, boolean genMips, int assetStream);
    synchronized int nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream) {
        return rsnAllocationCreateFromAssetStream(mContext, dstFmt, genMips, assetStream);
    native int  rsnAllocationCreateFromAssetStream(int con, int mips, int assetStream, int usage);
    synchronized int nAllocationCreateFromAssetStream(int mips, int assetStream, int usage) {
        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);
+5 −1
Original line number Diff line number Diff line
@@ -119,7 +119,11 @@ public class Script extends BaseObj {
        protected Allocation mAllocation;

        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() {
Loading