Load bitmap resources entirely from native code.

Change-Id: I6660baec241794c40611bce79b7f9ce9479e52ba
This commit is contained in:
Romain Guy
2009-08-31 14:06:43 -07:00
parent de5cd3d864
commit 650a3eb7d6
4 changed files with 76 additions and 18 deletions

View File

@ -16,19 +16,15 @@
package android.renderscript;
import java.lang.reflect.Field;
import java.lang.reflect.Array;
import java.io.IOException;
import java.io.InputStream;
import android.content.res.Resources;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.renderscript.Type;
import android.util.Config;
import android.util.Log;
import android.util.TypedValue;
/**
* @hide
@ -52,7 +48,7 @@ public class Allocation extends BaseObj {
}
public void data(int[] d) {
int size = 0;
int size;
if(mType != null && mType.mElement != null) {
size = mType.mElement.mSize;
for(int ct=0; ct < mType.mValues.length; ct++) {
@ -71,7 +67,7 @@ public class Allocation extends BaseObj {
}
public void data(float[] d) {
int size = 0;
int size;
if(mType != null && mType.mElement != null) {
size = mType.mElement.mSize;
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)
throws IllegalArgumentException {
Bitmap b = BitmapFactory.decodeResource(res, id, mBitmapOptions);
return createFromBitmap(rs, b, dstFmt, genMips);
InputStream is = null;
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)

View File

@ -16,13 +16,10 @@
package android.renderscript;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.renderscript.Type;
import android.graphics.BitmapFactory;
import android.util.Config;
import android.util.Log;
import android.view.Surface;
@ -35,6 +32,7 @@ import android.view.Surface;
public class RenderScript {
static final String LOG_TAG = "libRS_jni";
private static final boolean DEBUG = false;
@SuppressWarnings({"UnusedDeclaration", "deprecation"})
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
* field offsets.
*/
@SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
private static boolean sInitialized;
native private static void _nInit();
@ -95,6 +94,7 @@ public class RenderScript {
native int nAllocationCreateSized(int elem, int count);
native int nAllocationCreateFromBitmap(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 nAllocationUploadToBufferObject(int alloc);
@ -188,6 +188,7 @@ public class RenderScript {
private int mDev;
private int mContext;
@SuppressWarnings({"FieldCanBeLocal"})
private Surface mSurface;
private static boolean mElementsInitialized = false;