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

Commit 8a64743f authored by Jason Sams's avatar Jason Sams
Browse files

Add support for linking to a skia bitmap rather than always copying the data from the bitmap.

parent c2908e60
Loading
Loading
Loading
Loading
+45 −1
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.util.TypedValue;
 **/
public class Allocation extends BaseObj {
    Type mType;
    Bitmap mBitmap;

    Allocation(int id, RenderScript rs, Type t) {
        super(rs);
@@ -262,15 +263,58 @@ public class Allocation extends BaseObj {
        return new Allocation(id, rs, t);
    }

    static private Element elementFromBitmap(RenderScript rs, Bitmap b) {
        final Bitmap.Config bc = b.getConfig();
        if (bc == Bitmap.Config.ALPHA_8) {
            return Element.A_8(rs);
        }
        if (bc == Bitmap.Config.ARGB_4444) {
            return Element.RGBA_4444(rs);
        }
        if (bc == Bitmap.Config.ARGB_8888) {
            return Element.RGBA_8888(rs);
        }
        if (bc == Bitmap.Config.RGB_565) {
            return Element.RGB_565(rs);
        }
        throw new IllegalStateException("Bad bitmap type.");
    }

    static private Type typeFromBitmap(RenderScript rs, Bitmap b) {
        Element e = elementFromBitmap(rs, b);
        Type.Builder tb = new Type.Builder(rs, e);
        tb.add(Dimension.X, b.getWidth());
        tb.add(Dimension.Y, b.getHeight());
        return tb.create();
    }

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

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

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

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

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

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

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

    static Allocation createFromBitmapBoxed(RenderScript rs, Bitmap b, Element dstFmt, boolean genMips)
+1 −0
Original line number Diff line number Diff line
@@ -98,6 +98,7 @@ public class RenderScript {

    native int  nAllocationCreateTyped(int type);
    native int  nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
    native int  nAllocationCreateBitmapRef(int type, Bitmap bmp);
    native int  nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp);
    native int  nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream);

+21 −0
Original line number Diff line number Diff line
@@ -501,6 +501,26 @@ nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jint dstFmt, jboolean g
    return 0;
}

static void ReleaseBitmapCallback(void *bmp)
{
    SkBitmap const * nativeBitmap = (SkBitmap const *)bmp;
    nativeBitmap->unlockPixels();
}

static int
nAllocationCreateBitmapRef(JNIEnv *_env, jobject _this, jint type, jobject jbitmap)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    SkBitmap * nativeBitmap =
            (SkBitmap *)_env->GetIntField(jbitmap, gNativeBitmapID);


    nativeBitmap->lockPixels();
    void* ptr = nativeBitmap->getPixels();
    jint id = (jint)rsAllocationCreateBitmapRef(con, (RsType)type, ptr, nativeBitmap, ReleaseBitmapCallback);
    return id;
}

static int
nAllocationCreateFromAssetStream(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jint native_asset)
{
@@ -1367,6 +1387,7 @@ static JNINativeMethod methods[] = {

{"nAllocationCreateTyped",         "(I)I",                                 (void*)nAllocationCreateTyped },
{"nAllocationCreateFromBitmap",    "(IZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
{"nAllocationCreateBitmapRef",     "(ILandroid/graphics/Bitmap;)I",        (void*)nAllocationCreateBitmapRef },
{"nAllocationCreateFromBitmapBoxed","(IZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
{"nAllocationCreateFromAssetStream","(IZI)I",                              (void*)nAllocationCreateFromAssetStream },
{"nAllocationUploadToTexture",     "(IZI)V",                               (void*)nAllocationUploadToTexture },
+2 −0
Original line number Diff line number Diff line
@@ -46,6 +46,8 @@ typedef void * RsProgramFragment;
typedef void * RsProgramFragmentStore;
typedef void * RsProgramRaster;

typedef void (* RsBitmapCallback_t)(void *);

enum RsDeviceParam {
    RS_DEVICE_PARAM_FORCE_SOFTWARE_GL,
    RS_DEVICE_PARAM_COUNT
+8 −0
Original line number Diff line number Diff line
@@ -90,6 +90,14 @@ AllocationCreateSized {
	ret RsAllocation
	}

AllocationCreateBitmapRef {
	param RsType type
	param void * bmpPtr
	param void * callbackData
	param RsBitmapCallback_t callback
	ret RsAllocation
	}

AllocationCreateFromBitmap {
	param uint32_t width
	param uint32_t height
Loading