java – Renderscript支持库的问题
|
我试图在API 16及更高版本的设备上使用 Android Renderscript支持库,按照步骤 described here.到目前为止,事情并不顺利. 我的Renderscript代码如下所示. #pragma version(1)
#pragma rs java_package_name(com.xxx.renderscript.test)
#include "rs_time.rsh"
rs_script flipScript;
rs_allocation gIn;
rs_allocation gOut;
int width;
int height;
int direction = 0;
void root(uchar4 *v_out,const void *usrData,uint32_t x,uint32_t y) {
if(direction == 0) { // flip horizontally
const uchar4 *element = rsGetElementAt(gIn,width - x,y);
float4 color = rsUnpackColor8888(*element);
float4 output = {color.r,color.g,color.b};
*v_out = rsPackColorTo8888(output);
}
else if(direction == 1) { // flip vertically
const uchar4 *element = rsGetElementAt(gIn,x,height - y);
float4 color = rsUnpackColor8888(*element);
float4 output = {color.r,color.b};
*v_out = rsPackColorTo8888(output);
}
else if(direction == 2) { // rotate left
const uchar4 *element = rsGetElementAt(gIn,width - y,x);
float4 color = rsUnpackColor8888(*element);
float4 output = {color.r,color.b};
*v_out = rsPackColorTo8888(output);
}
else if(direction == 3) { // rotate right
const uchar4 *element = rsGetElementAt(gIn,y,height - x);
float4 color = rsUnpackColor8888(*element);
float4 output = {color.r,color.b};
*v_out = rsPackColorTo8888(output);
}
}
void flip(int testIdx) {
int64_t t0,t1;
int64_t t;
t0 = rsUptimeNanos();
rsForEach(flipScript,gIn,gOut);
t1 = rsUptimeNanos();
t = t1 - t0;
rsDebug(" flip: timer on RS side: ",t);
}
首先我将我的Android SDK工具升级到22.3,将我的SDK平台工具升级到19.如果我单击ADT / Help / About ADT / Installation Details,我看到所有组件都已升级到22.3.0,除了Developer Tools,仍然是21.0.1: Android DDMS 22.3.0.v201310242005-887826 com.android.ide.eclipse.ddms.feature.group The Android Open Source Project Android Developer Tools 21.0.1.201212060302 com.android.ide.eclipse.adt.package.product null Android Development Tools 22.3.0.v201310242005-887826 com.android.ide.eclipse.adt.feature.group The Android Open Source Project Android Hierarchy Viewer 22.3.0.v201310242005-887826 com.android.ide.eclipse.hierarchyviewer.feature.group The Android Open Source Project Android Native Development Tools 22.3.0.v201310242005-887826 com.android.ide.eclipse.ndk.feature.group The Android Open Source Project Android Traceview 22.3.0.v201310242005-887826 com.android.ide.eclipse.traceview.feature.group The Android Open Source Project Tracer for OpenGL ES 22.3.0.v201310242005-887826 com.android.ide.eclipse.gldebugger.feature.group The Android Open Source Project 然后我按照上述步骤中的建议编辑了我的“project.properties”文件.我也做了修改,加入了Tim Murray的评论. proguard.config=C:/android/adt-bundle-windows-x86_64/sdk/tools/proguard/proguard-android.txt:proguard-project.txt # Project target. target=android-18 renderscript.target=18 renderscript.support.mode=true #sdk.buildtools=19.0.0 在我的Java代码中,我确保导入了支持库中的包.下面列出的是完整的Java文件: package com.xxx.renderscript.test;
import android.support.v8.renderscript.*;
//import android.renderscript.RenderScript;
//import android.renderscript.Allocation;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class RenderScriptTestActivity extends Activity {
private final String mLog = this.getClass().getName();
private static final int TESTS_PER_GROUP = 12;
private Bitmap mBitmapIn;
private Bitmap mBitmapOut;
private ImageView mDisplayView;
private RenderScript mRS = null;
private ScriptC_flip mScriptFlip = null;
private Allocation mInPixelsAllocation = null;
private Allocation mOutPixelsAllocation = null;
//use aSyncTask so we display the image while rendering it.
private ImageRenderTask mImageRenderTask = null;
private boolean isRsBenchDone = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(mLog,"onCreate() ");
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_rs);
mBitmapIn = loadBitmap(R.drawable.city);
mBitmapOut = loadBitmap(R.drawable.city);
mDisplayView = (ImageView) findViewById(R.id.rs_display);
mDisplayView.setImageBitmap(mBitmapOut);
// RS instances
mRS = RenderScript.create(this);
mScriptFlip = new ScriptC_flip(mRS,getResources(),R.raw.flip);
mInPixelsAllocation = Allocation.createFromBitmap(mRS,mBitmapIn,Allocation.MipmapControl.MIPMAP_NONE,Allocation.USAGE_SCRIPT);
mOutPixelsAllocation = Allocation.createFromBitmap(mRS,mBitmapOut,Allocation.USAGE_SCRIPT);
}
@Override
protected void onResume() {
Log.i(mLog,"onResume ");
super.onResume();
// for thread & display control
isRsBenchDone = true;
}
// initialize flip.rs
private void setupRS() {
Log.i(mLog,"Initializing flip.rs...");
mScriptFlip.set_flipScript(mScriptFlip);
mScriptFlip.set_width(mBitmapIn.getWidth());
mScriptFlip.set_height(mBitmapIn.getHeight());
mScriptFlip.set_gIn(mInPixelsAllocation);
mScriptFlip.set_gOut(mOutPixelsAllocation);
}
@Override
protected void onStop() {
Log.i(mLog,"onStop ");
super.onStop();
}
private void resetRS() {
mBitmapIn = null;
mBitmapOut = null;
mRS = null;
mScriptFlip = null;
mInPixelsAllocation = null;
mOutPixelsAllocation = null;
}
private Bitmap loadBitmap(int resource) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap b_temp = BitmapFactory.decodeResource(getResources(),resource,options);
int w = b_temp.getWidth();
int h = b_temp.getHeight();
Config cfg = b_temp.getConfig();
Bitmap b_keep = Bitmap.createBitmap(w,h,cfg);
Canvas c = new Canvas(b_keep);
c.drawBitmap(b_temp,null);
b_temp.recycle();
if (true) {
Log.d(mLog,"loadBitmap(): width = " + w + " loadBitmap(): height = " + h);
}
return b_keep;
}
// press the button to invoke this function
public void benchmark(View v) {
if (false == isRsBenchDone) { //do nothing if it's not done yet
return;
}
if (mImageRenderTask != null) {
if (mImageRenderTask.getStatus() == AsyncTask.Status.RUNNING) {
return;
}
if (mImageRenderTask.getStatus() == AsyncTask.Status.PENDING) {
mImageRenderTask.execute();
return;
}
}
// instantiate AsyncTask if not already existed & running
Log.i(mLog,"User pressed Start");
SystemClock.sleep(2000);
isRsBenchDone = false;
setupRS();
mImageRenderTask = new ImageRenderTask();
mImageRenderTask.execute();
}
// user presses this button when the test is done and all results have been
// recorded.
public void pressedRsExit(View view) {
Log.i(mLog,"User pressed Exit");
if (isRsBenchDone) { // only exit when we are finished
resetRS();
RenderScriptTestActivity.this.finish();
}
}
/**************************************************************************
**
* Since the display must be updated in the main UI thread,and the main
* UI thread cannot be put to sleep,we have to use an AsyncTask to update
* the display after the image is rendered,and then sleep some time
* so that the tester can view the images being displayed.
**
**************************************************************************/
private class ImageRenderTask extends AsyncTask<Void,Drawable,Void> {
// runs in the UI (main) thread in correspondence to the AsyncTask
// updating the drawable.
protected void onProgressUpdate(Drawable... values) {
Log.i(mLog,"onProgressUpdate...");
super.onProgressUpdate(values);
Drawable draw_tmp = values[0];
mDisplayView.setImageDrawable(draw_tmp);
mDisplayView.invalidate();
}
// runs on the UI (main) thread after the AsyncTask is finished
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// reset the flag to exit
isRsBenchDone = true;
}
@Override
protected Void doInBackground(Void... params) {
int group_id;
int flip_way;
//------------------------------------------------------------
// group 4: flip horizontally
//------------------------------------------------------------
flip_way = 0;
mScriptFlip.set_direction(flip_way);
Log.i(mLog,"starting horizontal flipping ... ... ... ...");
// measure group 4
group_id = 4; // used as a switch for render script
measureRS(group_id);
Log.i(mLog," horizontal done");
// sleep cannot happen in the main UI thread
SystemClock.sleep(2500);
updateDisplay();
SystemClock.sleep(2500);
//------------------------------------------------------------
// group 5: flip vertically
//------------------------------------------------------------
mScriptFlip.set_gIn(mInPixelsAllocation);
flip_way = 1;
mScriptFlip.set_direction(flip_way);
Log.i(mLog,"starting vertical flipping ... ... ... ...");
// measure group 5
group_id = 5; // used as a switch for render script
measureRS(group_id);
Log.i(mLog," vertical done");
// sleep cannot happen in the main UI thread
SystemClock.sleep(2500);
updateDisplay();
SystemClock.sleep(2500);
return null;
}
// run and measure Render Script instances
private void measureRS(int group_id) {
if ((0==group_id) || (1 == group_id) || (2 == group_id)) {
//other stuff
}
if (3 == group_id) {
//other stuff
}
if ((4==group_id) || (5 == group_id)) {
for (int i = 0; i < TESTS_PER_GROUP; i++) {
mScriptFlip.invoke_flip(i);
}
}
}
// update the displayed image
private void updateDisplay() {
Log.i(mLog,"updateDisplay...");
//mOutPixelsAllocation.copyTo(mBitmapIn); // this to verify the display.
mOutPixelsAllocation.copyTo(mBitmapOut);
Bitmap b_temp = Bitmap.createBitmap(mBitmapOut.getWidth(),mBitmapOut.getHeight(),Bitmap.Config.ARGB_8888);
Canvas c_temp = new Canvas(b_temp);
c_temp.drawBitmap(mBitmapOut,null);
Drawable d_temp = new BitmapDrawable(getResources(),b_temp);
publishProgress(d_temp);
}
}
}
最后,在我的项目属性中,我将外部JAR添加到Java Build Path中: renderscript-v8.jar - C:androidadt-bundle-windows-x86_64sdkbuild-tools19.0.0renderscriptlib 代码构建没有任何问题.然后我在几台设备上测试了它.以下是测试结果. Nexus 7(2012版,升级到Android 4.4) (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
