Android Renderscript計算(三)

來源:互聯網
上載者:User

調用Renderscript代碼

你能夠通過由執行個體化的類(ScriptC_script_name)來建立一個Renderscript對象從Android架構代碼中調用Renderscript。這個類包含了一個forEach_root()方法,它會調用rsForeach()方法。你能夠傳遞給它與Renderscript運行時層級調用相同的參數。這種技術允許你的Android應用程式把高精度的數學計算轉交給Renderscript。

在Android架構層次調用Renderscript的方法:

1.  在你的Android架構代碼中分配Renderscript所需要的記憶體。對於Androi3.2(API Level 13)以前的版本,需要分配輸入和輸出記憶體。Android4.0(API Level 14)以後的平台版本只需要分配其中之一的記憶體或兩個都分配。

2.  建立ScriptC_scritp_name類的一個執行個體。

3.  調用forEach_root()方法,並傳入分配的記憶體、Renderscript和其他的可選的使用者定義的資料。輸出記憶體中將會包含Renderscript的輸出結果。

以下樣本來自HellCompute樣本,它處理一張位元影像,並輸出它的黑白版本。CreateScript()方法安裝前面描述的步驟來執行。這個方法調用Renderscript對象,執行mono.rs指令碼,把最終的處理結果位元影像儲存在輸出的記憶體中,然後把處理後的位元影像顯示在螢幕上:

package com.example.android.rs.hellocompute;

 

import android.app.Activity;

import android.os.Bundle;

import android.graphics.BitmapFactory;

import android.graphics.Bitmap;

import android.renderscript.RenderScript;

import android.renderscript.Allocation;

import android.widget.ImageView;

 

public class HelloCompute extends Activity {

 
private Bitmap mBitmapIn;

 
private Bitmap mBitmapOut;

 

 
private RenderScript mRS;

 
private Allocation mInAllocation;

 
private Allocation mOutAllocation;

 
private ScriptC_mono mScript;

 

 
@Override

 
protected void onCreate(Bundle savedInstanceState) {

     
super.onCreate(savedInstanceState);

     
setContentView(R.layout.main);

 

     
mBitmapIn = loadBitmap(R.drawable.data);

     
mBitmapOut = Bitmap.createBitmap(mBitmapIn.getWidth(), mBitmapIn.getHeight(),

                                      
mBitmapIn.getConfig());

 

     
ImageView in = (ImageView) findViewById(R.id.displayin);

     
in.setImageBitmap(mBitmapIn);

 

     
ImageView out = (ImageView) findViewById(R.id.displayout);

     
out.setImageBitmap(mBitmapOut);

 

     
createScript();

 
}

 
private void createScript() {

     
mRS = RenderScript.create(this);

     
mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,

         
Allocation.MipmapControl.MIPMAP_NONE,

         
Allocation.USAGE_SCRIPT);

     
mOutAllocation = Allocation.createTyped(mRS, mInAllocation.getType());

     
mScript = new ScriptC_mono(mRS, getResources(), R.raw.mono);

     
mScript.forEach_root(mInAllocation, mOutAllocation);

     
mOutAllocation.copyTo(mBitmapOut);

 
}

 

 
private Bitmap loadBitmap(int resource) {

     
final BitmapFactory.Options options = new BitmapFactory.Options();

     
options.inPreferredConfig = Bitmap.Config.ARGB_8888;

     
return BitmapFactory.decodeResource(getResources(), resource, options);

 
}

}

以下是從另一個Renderscript檔案中調用Renderscript的方法:

1. 在Android架構代碼中分配由Renderscript所需要的記憶體。對於Android3.2平台和之前的版本,要同時分配輸入和輸出記憶體。Android4.0平台版本之後可以根據需要來分配輸入和輸出記憶體。

2. 調用rsForEach()方法,並傳入分配的記憶體和可選的使用者定義的資料。輸出記憶體中會包含Renderscript的輸出結果。

rs_script script;
rs_allocation in_allocation;
rs_allocation out_allocation;
UserData_t data;
...

rsForEach(script,
in_allocation, out_allocation,&data,sizeof(data));

在這個例子中,假定在Android架構層指令碼和記憶體已經被分配和綁定,並且UserData_t是一個被事前聲明的結構。把這個結構的指標和它的大小傳遞一個rsForEach()方法,這是一個可選的參數。如果你的Renderscript需要一些輸入記憶體中之外的資訊,就可以使用這個參數。

設定浮點精度

你能夠定義計算規則所需要的浮點精度。如果你需要比IEEE 754-2008標準(預設使用的標準)更小的精度,使用這個定義是有用的。你能夠使用下列編譯指令來定義指令碼的浮點精度層級:

1. #pragma rs_fp_full(如果沒指定,這是預設的精度層級):指示應用程式需要由IEEE 754-2008標準所描述的浮點精度。

2. #pragma rs_fp_relaxed:對於不需要嚴格遵從IEEE 754-2008標準要求精度的應用程式可以使用這種編譯指令,對於de-norms(去模)計算這種模式啟用了flush-to-zero(清零),並且round-towards-zero(向零方向舍入)。

3. #pragma rs_fp_imprecise:對於沒有嚴格精度要求的應用程式使用這種模式。這種模式沿用了rs_fp_relaxed模式以下的所有規則:

   操作結果-0.0會使用+0.0來代替返回;

   沒有定義有關INF和NAN的操作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.