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

Commit 650a3eb7 authored by Romain Guy's avatar Romain Guy
Browse files

Load bitmap resources entirely from native code.

Change-Id: I6660baec241794c40611bce79b7f9ce9479e52ba
parent de5cd3d8
Loading
Loading
Loading
Loading
+11 −4
Original line number Original line Diff line number Diff line
@@ -337,19 +337,26 @@ public class BitmapFactory {
     */
     */
    public static Bitmap decodeResource(Resources res, int id, Options opts) {
    public static Bitmap decodeResource(Resources res, int id, Options opts) {
        Bitmap bm = null;
        Bitmap bm = null;
        InputStream is = null; 
        
        
        try {
        try {
            final TypedValue value = new TypedValue();
            final TypedValue value = new TypedValue();
            final InputStream is = res.openRawResource(id, value);
            is = res.openRawResource(id, value);


            bm = decodeResourceStream(res, value, is, null, opts);
            bm = decodeResourceStream(res, value, is, null, opts);
            is.close();
        } catch (Exception e) {
        } catch (java.io.IOException e) {
            /*  do nothing.
            /*  do nothing.
                If the exception happened on open, bm will be null.
                If the exception happened on open, bm will be null.
                If it happened on close, bm is still valid.
                If it happened on close, bm is still valid.
            */
            */
        } finally {
            try {
                if (is != null) is.close();
            } catch (IOException e) {
                // Ignore
            }
        }
        }

        return bm;
        return bm;
    }
    }


+27 −10
Original line number Original line Diff line number Diff line
@@ -16,19 +16,15 @@


package android.renderscript;
package android.renderscript;


import java.lang.reflect.Field;
import java.lang.reflect.Array;

import java.io.IOException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStream;


import android.content.res.Resources;
import android.content.res.Resources;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.renderscript.Type;
import android.util.Config;
import android.util.Log;
import android.util.Log;
import android.util.TypedValue;


/**
/**
 * @hide
 * @hide
@@ -52,7 +48,7 @@ public class Allocation extends BaseObj {
    }
    }


    public void data(int[] d) {
    public void data(int[] d) {
        int size = 0;
        int size;
        if(mType != null && mType.mElement != null) {
        if(mType != null && mType.mElement != null) {
            size = mType.mElement.mSize;
            size = mType.mElement.mSize;
            for(int ct=0; ct < mType.mValues.length; ct++) {
            for(int ct=0; ct < mType.mValues.length; ct++) {
@@ -71,7 +67,7 @@ public class Allocation extends BaseObj {
    }
    }


    public void data(float[] d) {
    public void data(float[] d) {
        int size = 0;
        int size;
        if(mType != null && mType.mElement != null) {
        if(mType != null && mType.mElement != null) {
            size = mType.mElement.mSize;
            size = mType.mElement.mSize;
            for(int ct=0; ct < mType.mValues.length; ct++) {
            for(int ct=0; ct < mType.mValues.length; ct++) {
@@ -245,8 +241,29 @@ public class Allocation extends BaseObj {
    static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
    static public Allocation createFromBitmapResource(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
        throws IllegalArgumentException {
        throws IllegalArgumentException {


        Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
        InputStream is = null;
        return createFromBitmap(rs, b, dstFmt, genMips);
        try {
            final TypedValue value = new TypedValue();
            is = res.openRawResource(id, value);

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

            return new Allocation(allocationId, rs, null);            
        } catch (Exception e) {
            // Ignore
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
        }

        return null;
    }
    }


    static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
    static public Allocation createFromBitmapResourceBoxed(RenderScript rs, Resources res, int id, Element dstFmt, boolean genMips)
+5 −4
Original line number Original line Diff line number Diff line
@@ -16,13 +16,10 @@


package android.renderscript;
package android.renderscript;


import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Field;


import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap;
import android.renderscript.Type;
import android.graphics.BitmapFactory;
import android.util.Config;
import android.util.Config;
import android.util.Log;
import android.util.Log;
import android.view.Surface;
import android.view.Surface;
@@ -35,6 +32,7 @@ import android.view.Surface;
public class RenderScript {
public class RenderScript {
    static final String LOG_TAG = "libRS_jni";
    static final String LOG_TAG = "libRS_jni";
    private static final boolean DEBUG  = false;
    private static final boolean DEBUG  = false;
    @SuppressWarnings({"UnusedDeclaration", "deprecation"})
    private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;
    private static final boolean LOG_ENABLED = DEBUG ? Config.LOGD : Config.LOGV;




@@ -43,6 +41,7 @@ public class RenderScript {
     * We use a class initializer to allow the native code to cache some
     * We use a class initializer to allow the native code to cache some
     * field offsets.
     * field offsets.
     */
     */
    @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
    private static boolean sInitialized;
    private static boolean sInitialized;
    native private static void _nInit();
    native private static void _nInit();


@@ -95,6 +94,7 @@ public class RenderScript {
    native int  nAllocationCreateSized(int elem, int count);
    native int  nAllocationCreateSized(int elem, int count);
    native int  nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
    native int  nAllocationCreateFromBitmap(int dstFmt, boolean genMips, Bitmap bmp);
    native int  nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp);
    native int  nAllocationCreateFromBitmapBoxed(int dstFmt, boolean genMips, Bitmap bmp);
    native int  nAllocationCreateFromAssetStream(int dstFmt, boolean genMips, int assetStream);


    native void nAllocationUploadToTexture(int alloc, int baseMioLevel);
    native void nAllocationUploadToTexture(int alloc, int baseMioLevel);
    native void nAllocationUploadToBufferObject(int alloc);
    native void nAllocationUploadToBufferObject(int alloc);
@@ -188,6 +188,7 @@ public class RenderScript {


    private int     mDev;
    private int     mDev;
    private int     mContext;
    private int     mContext;
    @SuppressWarnings({"FieldCanBeLocal"})
    private Surface mSurface;
    private Surface mSurface;


    private static boolean mElementsInitialized = false;
    private static boolean mElementsInitialized = false;
+33 −0
Original line number Original line Diff line number Diff line
@@ -26,7 +26,13 @@
#include <ui/Surface.h>
#include <ui/Surface.h>


#include <core/SkBitmap.h>
#include <core/SkBitmap.h>
#include <core/SkPixelRef.h>
#include <core/SkStream.h>
#include <core/SkTemplates.h>
#include <images/SkImageDecoder.h>


#include <utils/Asset.h>
#include <utils/ResourceTypes.h>


#include "jni.h"
#include "jni.h"
#include "JNIHelp.h"
#include "JNIHelp.h"
@@ -396,6 +402,32 @@ nAllocationCreateFromBitmap(JNIEnv *_env, jobject _this, jint dstFmt, jboolean g
    return 0;
    return 0;
}
}


static int
nAllocationCreateFromAssetStream(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jint native_asset)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));

    Asset* asset = reinterpret_cast<Asset*>(native_asset);
    SkBitmap bitmap;
    SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
            &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);

    SkBitmap::Config config = bitmap.getConfig();

    RsElementPredefined e = SkBitmapToPredefined(config);

    if (e != RS_ELEMENT_USER_U8) {
        bitmap.lockPixels();
        const int w = bitmap.width();
        const int h = bitmap.height();
        const void* ptr = bitmap.getPixels();
        jint id = (jint)rsAllocationCreateFromBitmap(con, w, h, (RsElementPredefined)dstFmt, e, genMips, ptr);
        bitmap.unlockPixels();
        return id;
    }
    return 0;
}

static int
static int
nAllocationCreateFromBitmapBoxed(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jobject jbitmap)
nAllocationCreateFromBitmapBoxed(JNIEnv *_env, jobject _this, jint dstFmt, jboolean genMips, jobject jbitmap)
{
{
@@ -1239,6 +1271,7 @@ static JNINativeMethod methods[] = {
{"nAllocationCreateSized",         "(II)I",                                (void*)nAllocationCreateSized },
{"nAllocationCreateSized",         "(II)I",                                (void*)nAllocationCreateSized },
{"nAllocationCreateFromBitmap",    "(IZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
{"nAllocationCreateFromBitmap",    "(IZLandroid/graphics/Bitmap;)I",       (void*)nAllocationCreateFromBitmap },
{"nAllocationCreateFromBitmapBoxed","(IZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
{"nAllocationCreateFromBitmapBoxed","(IZLandroid/graphics/Bitmap;)I",      (void*)nAllocationCreateFromBitmapBoxed },
{"nAllocationCreateFromAssetStream","(IZI)I",                              (void*)nAllocationCreateFromAssetStream },
{"nAllocationUploadToTexture",     "(II)V",                                (void*)nAllocationUploadToTexture },
{"nAllocationUploadToTexture",     "(II)V",                                (void*)nAllocationUploadToTexture },
{"nAllocationUploadToBufferObject","(I)V",                                 (void*)nAllocationUploadToBufferObject },
{"nAllocationUploadToBufferObject","(I)V",                                 (void*)nAllocationUploadToBufferObject },
{"nAllocationData",                "(I[II)V",                              (void*)nAllocationData_i },
{"nAllocationData",                "(I[II)V",                              (void*)nAllocationData_i },