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

Commit 79f4bd3d authored by Koushik K. Dutta's avatar Koushik K. Dutta
Browse files

Bitmap allocations must be stretched to 2^n dimensions. To support this, there...

Bitmap allocations must be stretched to 2^n dimensions. To support this, there is a new call in the framework. Perhaps CreateFromBitmapBoxed was meant to do this? I am not sure about the original developers intent.
parent 6882974a
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -268,7 +268,7 @@ public class Allocation extends BaseObj {
        return new Allocation(id, rs, null);
    }

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

        rs.validate();
@@ -279,6 +279,17 @@ public class Allocation extends BaseObj {
        return new Allocation(id, rs, null);
    }

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

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

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

+1 −0
Original line number Diff line number Diff line
@@ -100,6 +100,7 @@ public class RenderScript {
    native int  nAllocationCreateTyped(int type);
    native int  nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
    native int  nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp);
    native int  nAllocationCreateFromBitmapStretched(int dstFmt, boolean genMips, Bitmap bmp);
    native int  nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream);

    native void nAllocationUploadToTexture(int alloc, int baseMioLevel);
+46 −1
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@
#include <ui/Surface.h>

#include <core/SkBitmap.h>
#include <core/SkCanvas.h>
#include <core/SkPixelRef.h>
#include <core/SkStream.h>
#include <core/SkTemplates.h>
@@ -40,6 +41,7 @@

#include <RenderScript.h>
#include <RenderScriptEnv.h>
#include <rsUtils.h>

//#define LOG_API LOGE
#define LOG_API(...)
@@ -520,6 +522,49 @@ nAllocationCreateFromAssetStream(JNIEnv *_env, jobject _this, jint dstFmt, jbool
    return 0;
}

static int
nAllocationCreateFromBitmapStretched(JNIEnv *_env, jobject _this, 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) {
        const int w = bitmap.width();
        const int h = bitmap.height();
        const int w2 = android::renderscript::rsHigherPow2(w);
        const int h2 = android::renderscript::rsHigherPow2(h);

        if ((w2 != w) || (h2 != h)) {
            SkBitmap resized;
            resized.setConfig(config, w2, h2);
            resized.allocPixels();
            SkCanvas canvas(resized);
            canvas.drawColor(0, SkXfermode::kClear_Mode);
            SkRect rect;
            rect.set(0, 0, w2, h2);
            canvas.drawBitmapRect(bitmap, NULL, rect);
            resized.lockPixels();
            const void* ptr = resized.getPixels();
            jint id = (jint)rsAllocationCreateFromBitmap(con, w2, h2, (RsElement)dstFmt, e, genMips, ptr);
            resized.unlockPixels();
            return id;
        }
        else {
            bitmap.lockPixels();
            const void* ptr = bitmap.getPixels();
            jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElement)dstFmt, e, genMips, ptr);
            bitmap.unlockPixels();
            return id;
        }
    }
    return 0;
}

static int
nAllocationCreateFromBitmapBoxed(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jobject jbitmap)
{
@@ -543,7 +588,6 @@ nAllocationCreateFromBitmapBoxed(JNIEnv *_env, jobject _this, jint dstFmt, jbool
    return 0;
}


static void
nAllocationSubData1D_i(JNIEnv *_env, jobject _this, jint alloc, jint offset, jint count, jintArray data, int sizeBytes)
{
@@ -1360,6 +1404,7 @@ static JNINativeMethod methods[] = {
{"nAllocationCreateTyped",         "(I)I",                                 (void*)nAllocationCreateTyped },
{"nAllocationCreateFromBitmap",    "(IZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
{"nAllocationCreateFromBitmapBoxed","(IZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
{"nAllocationCreateFromBitmapStretched","(IZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapStretched },
{"nAllocationCreateFromAssetStream","(IZI)I",                              (void*)nAllocationCreateFromAssetStream },
{"nAllocationUploadToTexture",     "(II)V",                                (void*)nAllocationUploadToTexture },
{"nAllocationUploadToBufferObject","(I)V",                                 (void*)nAllocationUploadToBufferObject },