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

Commit 6521ff09 authored by Omprakash Dhyade c_odhyad@quicinc.com's avatar Omprakash Dhyade c_odhyad@quicinc.com Committed by Steve Kondik
Browse files

frameworks/base: Provide APIs for storing allocations.

Current implementation does not allow applications to re-use already
created allocations. This feature is useful in case of orientation
change, where we might like to re-use the memory created earlier.
For example, Launcher2 changes orientation upon key board event,
it will re-create the icons if it were not allowed to keep them.
This change facilitates this requirement.
parent 1dfad01c
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -261,6 +261,29 @@ public class Allocation extends BaseObj {
        return new Allocation(id, rs, null);
    }

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

        rs.validate();
        int id = rs.nAllocationCreateFromBitmap1(index, dstFmt.mID, genMips, b);
        return new Allocation(id, rs, null);
    }

    static public void addToAllocation(RenderScript rs, int index)
        throws IllegalArgumentException {
	rs.nAllocationAddToAllocationList(index);
    }

    static public void removeFromAllocation(RenderScript rs, int index)
        throws IllegalArgumentException {
	rs.nAllocationRemoveFromAllocationList(index);
    }

    static public void createAllocationList(RenderScript rs, int count)
        throws IllegalArgumentException {
	rs.nAllocationCreateAllocationList(count);
    }

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

+4 −0
Original line number Diff line number Diff line
@@ -101,6 +101,10 @@ public class RenderScript {

    native int  nAllocationCreateTyped(int type);
    native int  nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
    native int  nAllocationCreateFromBitmap1(int index, int dstFmt, boolean genMips, Bitmap bmp);
    native int  nAllocationAddToAllocationList(int index);
    native int  nAllocationRemoveFromAllocationList(int index);
    native int  nAllocationCreateAllocationList(int count);
    native int  nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp);
    native int  nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream);

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

static int
nAllocationCreateFromBitmap1(JNIEnv *_env, jobject _this, jint index, jint dstFmt, jboolean genMips, jobject jbitmap)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    SkBitmap const * nativeBitmap =
            (SkBitmap const *)_env->GetIntField(jbitmap, gNativeBitmapID);
    const SkBitmap& bitmap(*nativeBitmap);
    SkBitmap::Config config = bitmap.getConfig();

    RsElement e = SkBitmapToPredefined(config);
    if (e) {
        bitmap.lockPixels();
        const int w = bitmap.width();
        const int h = bitmap.height();
        const void* ptr = bitmap.getPixels();
        jint id = (jint)rsAllocationCreateFromBitmap1(con, w, h, (RsElement)dstFmt, e, genMips, ptr, index);
        bitmap.unlockPixels();
        return id;
    }
    return 0;
}

static int
nAllocationAddToAllocationList(JNIEnv *_env, jobject _this, jint index)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    rsAllocationAddToAllocationList(con, index);
    return 0;
}

static int
nAllocationRemoveFromAllocationList(JNIEnv *_env, jobject _this, jint index)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    rsAllocationRemoveFromAllocationList(con, index);
    return 0;
}

static int
nAllocationCreateAllocationList(JNIEnv *_env, jobject _this, jint index)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    rsAllocationCreateAllocationList(con, index);
    return 0;
}

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

{"nAllocationCreateTyped",         "(I)I",                                 (void*)nAllocationCreateTyped },
{"nAllocationCreateFromBitmap",    "(IZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
{"nAllocationCreateFromBitmap1",   "(IIZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmap1 },
{"nAllocationAddToAllocationList", "(I)I",                                 (void*)nAllocationAddToAllocationList},
{"nAllocationRemoveFromAllocationList", "(I)I",                            (void*)nAllocationRemoveFromAllocationList},
{"nAllocationCreateAllocationList", "(I)I",                                (void*)nAllocationCreateAllocationList},
{"nAllocationCreateFromBitmapBoxed","(IZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
{"nAllocationCreateFromAssetStream","(IZI)I",                              (void*)nAllocationCreateFromAssetStream },
{"nAllocationUploadToTexture",     "(II)V",                                (void*)nAllocationUploadToTexture },
+23 −0
Original line number Diff line number Diff line
@@ -109,6 +109,29 @@ AllocationCreateFromBitmap {
	ret RsAllocation
	}

AllocationCreateFromBitmap1 {
	param uint32_t width
	param uint32_t height
	param RsElement dstFmt
	param RsElement srcFmt
	param bool genMips
	param const void * data
	param uint32_t index 
	ret RsAllocation
	}

AllocationAddToAllocationList {
	param uint32_t index
	}

AllocationRemoveFromAllocationList {
	param uint32_t index
	}

AllocationCreateAllocationList {
	param uint32_t count 
	}

AllocationCreateFromBitmapBoxed {
	param uint32_t width
	param uint32_t height
+134 −2
Original line number Diff line number Diff line
@@ -22,6 +22,43 @@
using namespace android;
using namespace android::renderscript;

static long *earlierAllocations;
static unsigned int currentNumAllocations;

Allocation::Allocation(Context *rsc, const Type *type, int index) : ObjectBase(rsc)
{
    mAllocFile = __FILE__;
    mAllocLine = __LINE__;
    mPtr = NULL;

    mCpuWrite = false;
    mCpuRead = false;
    mGpuWrite = false;
    mGpuRead = false;

    mReadWriteRatio = 0;
    mUpdateSize = 0;

    mIsTexture = false;
    mTextureID = 0;

    mIsVertexBuffer = false;
    mBufferID = 0;

    mType.set(type);
    rsAssert(type);
    if (!earlierAllocations[index]) {
        mPtr = malloc(mType->getSizeBytes());
        earlierAllocations[index] = (long) mPtr;
    }
    else
        mPtr = (void*) earlierAllocations[index];
    if (!mPtr) {
        LOGE("Allocation::Allocation, alloc failure");
    }
    mUseEarlierAllocation = true;
}

Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc)
{
    mAllocFile = __FILE__;
@@ -32,6 +69,7 @@ Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc)
    mCpuRead = false;
    mGpuWrite = false;
    mGpuRead = false;
    mUseEarlierAllocation = false;

    mReadWriteRatio = 0;
    mUpdateSize = 0;
@@ -52,6 +90,7 @@ Allocation::Allocation(Context *rsc, const Type *type) : ObjectBase(rsc)

Allocation::~Allocation()
{
    if (!mUseEarlierAllocation)
        free(mPtr);
    mPtr = NULL;

@@ -226,6 +265,15 @@ void Allocation::dumpLOGV(const char *prefix) const
namespace android {
namespace renderscript {

RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype, int index)
{
    const Type * type = static_cast<const Type *>(vtype);

    Allocation * alloc = new Allocation(rsc, type, index);
    alloc->incUserRef();
    return alloc;
}

RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype)
{
    const Type * type = static_cast<const Type *>(vtype);
@@ -402,7 +450,6 @@ RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h
        rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
    }
    RsType type = rsi_TypeCreate(rsc);

    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type);
    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
    if (texAlloc == NULL) {
@@ -426,6 +473,91 @@ RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, uint32_t w, uint32_t h
    return texAlloc;
}

RsAllocation rsi_AllocationCreateFromBitmap1(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src,  bool genMips, const void *data, uint32_t index)
{
    const Element *src = static_cast<const Element *>(_src);
    const Element *dst = static_cast<const Element *>(_dst);
    rsAssert(!(w & (w-1)));
    rsAssert(!(h & (h-1)));

    bool useCt = !(earlierAllocations[index]);
    //LOGE("rsi_AllocationCreateFromBitmap %i %i %i %i %i", w, h, dstFmt, srcFmt, genMips);
    rsi_TypeBegin(rsc, _dst);
    rsi_TypeAdd(rsc, RS_DIMENSION_X, w);
    rsi_TypeAdd(rsc, RS_DIMENSION_Y, h);
    if (genMips) {
        rsi_TypeAdd(rsc, RS_DIMENSION_LOD, 1);
    }
    RsType type = rsi_TypeCreate(rsc);

    RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, type, index);
    Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc);
    if (texAlloc == NULL) {
        LOGE("Memory allocation failure");
        return NULL;
    }

    if (useCt) {
        ElementConverter_t cvt = pickConverter(dst, src);
        cvt(texAlloc->getPtr(), data, w * h);
    }

    if (genMips && useCt) {
        Adapter2D adapt(rsc, texAlloc);
        Adapter2D adapt2(rsc, texAlloc);
        for(uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) {
            adapt.setLOD(lod);
            adapt2.setLOD(lod + 1);
            mip(adapt2, adapt);
        }
    }

    return texAlloc;
}

void rsi_AllocationAddToAllocationList(Context *rsc, uint32_t index)
{
    unsigned int temp = currentNumAllocations;
    currentNumAllocations++;
    earlierAllocations = (long*) realloc(earlierAllocations, (sizeof(long) * currentNumAllocations));
    memmove(earlierAllocations + index + 1, earlierAllocations + index, (temp - index) * sizeof(long)); 
    earlierAllocations[index] = 0;
}

void rsi_AllocationRemoveFromAllocationList(Context *rsc, uint32_t index)
{
    if (index >= currentNumAllocations || !earlierAllocations || !earlierAllocations[index])
        return;

    if (earlierAllocations[index])
        free((void *) earlierAllocations[index]);

    memmove(earlierAllocations + index, earlierAllocations + index + 1, (currentNumAllocations - index - 1) * sizeof(long)); 
    currentNumAllocations--;
    earlierAllocations = (long*) realloc(earlierAllocations, (sizeof(long) * currentNumAllocations));
}

void rsi_AllocationCreateAllocationList(Context *rsc,  uint32_t count)
{
    if (!earlierAllocations && count > 0) {
        earlierAllocations = (long*) malloc(sizeof(long) * count);
	currentNumAllocations = count;
	for (int i = 0; i < currentNumAllocations; i++)
	    earlierAllocations[i] = 0;
    }
    else if (count > 0) {
	for (int i = 0; i < currentNumAllocations; i++) {
	    if (earlierAllocations[i])
	        free((void*) earlierAllocations[i]);
	    earlierAllocations[i] = 0;
        }
        earlierAllocations = (long*) realloc(earlierAllocations, sizeof(long) * count);
	currentNumAllocations = count;
	for (int i = 0; i < currentNumAllocations; i++)
	    earlierAllocations[i] = 0;
    }
}

RsAllocation rsi_AllocationCreateFromBitmapBoxed(Context *rsc, uint32_t w, uint32_t h, RsElement _dst, RsElement _src, bool genMips, const void *data)
{
    const Element *srcE = static_cast<const Element *>(_src);
Loading