編輯:關於 Android 程式設計
函式會被反射到位於 project_root/gen/package/name/ScriptC_renderscript_filename 的腳本類別中。若你在 Renderscript 中宣告:
void touch(float x, float y, float pressure, int id) {
if (id >= 10) {
return;
}
touchPos[id].x = x;
touchPos[id].y = y;
touchPressure[id] = pressure;
}
腳本類別中就會自動產生對應的呼叫方法:
public void invoke_touch(float x, float y, float pressure, int id) {
FieldPacker touch_fp = new FieldPacker(16);
touch_fp.addF32(x);
touch_fp.addF32(y);
touch_fp.addF32(pressure);
touch_fp.addI32(id);
invoke(mExportFuncIdx_touch, touch_fp);
}
注意:Renderscript 函式不能有回傳值,因為整個系統被設計成非同步的。當框架層呼叫進入 Renderscript 時,該呼叫會被放進佇列,等到適合時機才執行。這樣設計讓 Renderscript 得以在不被任意中斷的前提下最佳化並提升效率;反過來,若允許回傳值,呼叫就會被阻斷直到函式回傳為止。若確實需要把值送回框架層,請使用 rsSendToClient()。
支援的變數型別同樣會被反射到腳本類別中,並為每個變數產生一組存取器。例如宣告:
uint32_t unsignedInteger = 1;
就會產生:
private long mExportVar_unsignedInteger;
public void set_unsignedInteger(long v) {
mExportVar_unsignedInteger = v;
setVar(mExportVarIdx_unsignedInteger, v);
}
public long get_unsignedInteger() {
return mExportVar_unsignedInteger;
}
結構會被反射成自己專屬的類別,位於 <project_root>/gen/com/example/renderscript/ScriptField_struct_name。這個類別代表一個結構陣列,並負責為結果成員配置記憶體。若宣告:
typedef struct Point {
float2 position;
float size;
} Point_t;
便會在 ScriptField_Point.java 中產生大量樣板程式碼:
package com.example.android.rs.hellocompute;
import android.renderscript.*;
import android.content.res.Resources;
/** @hide */
public class ScriptField_Point extends android.renderscript.Script.FieldBase {
static public class Item {
public static final int sizeof = 12;
Float2 position;
float size;
Item() {
position = new Float2();
}
}
private Item mItemArray[];
private FieldPacker mIOBuffer;
public static Element createElement(RenderScript rs) {
Element.Builder eb = new Element.Builder(rs);
eb.add(Element.F32_2(rs), "position");
eb.add(Element.F32(rs), "size");
return eb.create();
}
public ScriptField_Point(RenderScript rs, int count) {
mItemArray = null;
mIOBuffer = null;
mElement = createElement(rs);
init(rs, count);
}
public ScriptField_Point(RenderScript rs, int count, int usages) {
mItemArray = null;
mIOBuffer = null;
mElement = createElement(rs);
init(rs, count, usages);
}
private void copyToArray(Item i, int index) {
if (mIOBuffer == null) mIOBuffer = new FieldPacker(Item.sizeof * getType().getX());
mIOBuffer.reset(index * Item.sizeof);
mIOBuffer.addF32(i.position);
mIOBuffer.addF32(i.size);
}
public void set(Item i, int index, boolean copyNow) {
if (mItemArray == null) mItemArray = new Item[getType().getX()];
mItemArray[index] = i;
if (copyNow) {
copyToArray(i, index);
mAllocation.setFromFieldPacker(index, mIOBuffer);
}
}
public Item get(int index) {
if (mItemArray == null) return null;
return mItemArray[index];
}
public void copyAll() {
for (int ct = 0; ct < mItemArray.length; ct++) copyToArray(mItemArray[ct], ct);
mAllocation.setFromFieldPacker(0, mIOBuffer);
}
public void resize(int newSize) {
if (mItemArray != null) {
int oldSize = mItemArray.length;
int copySize = Math.min(oldSize, newSize);
if (newSize == oldSize) return;
Item ni[] = new Item[newSize];
System.arraycopy(mItemArray, 0, ni, 0, copySize);
mItemArray = ni;
}
mAllocation.resize(newSize);
if (mIOBuffer != null) mIOBuffer = new FieldPacker(Item.sizeof * getType().getX());
}
}
除此之外還會為每個欄位產生 set_xxx(int index, T v, boolean copyNow) 與 get_xxx(int index) 一組方法,以及 set_position、set_size 等專用 setter,讓你能只更新結構中的單一欄位而不必重送整個結構。
ScriptField_struct_name(RenderScript rs, int count) 用 count 指定要配置的結構數量。多參數版本 (RenderScript rs, int count, int usages) 則額外指定記憶體要對應的記憶體空間,共有四種:
A. USAGE_SCRIPT:配置在腳本記憶體空間,不指定時這就是預設值。
B. USAGE_GRAPHICS_TEXTURE:配置在 GPU 的紋理記憶體空間。
C. USAGE_GRAPHICS_VERTEX:配置在 GPU 的 Vertex 記憶體空間。
D. USAGE_GRAPHICS_CONSTANTS:配置在 GPU 的固定記憶體空間,供不同程式物件共用。
多種空間可用位元 OR 合併指定,目的是告知 Renderscript 執行期你打算存取哪些空間的資料:
ScriptField_Point touchPoints = new ScriptField_Point(myRenderscript, 2,
Allocation.USAGE_SCRIPT | Allocation.USAGE_GRAPHICS_VERTEX);
靜態巢狀類別 Item 讓你能用物件形式建立結構實例,在 Java 端操作起來自然得多。改完物件後呼叫 set(Item i, int index, boolean copyNow),就能把它寫入配置好的記憶體並放到陣列的指定位置,之後 Renderscript 執行期即可讀到最新值。
每個欄位都有 getter/setter,並帶一個 index 參數用來指定要讀寫陣列中的哪個結構。setter 另有 copyNow 參數,用來決定是否立刻把該筆記憶體同步給 Renderscript 執行期。
建立記憶體中該結構的 Element 描述,配置時會依此決定配置方式。
行為類似 C 語言的 realloc(),可以擴展先前配置的空間,並保留其中已有的值。
把框架層設定過的記憶體一次同步到 Renderscript 執行期。當你在多個 setter 中都傳入 copyNow=false 以批量修改時,最後呼叫一次 copyAll() 就能統一批送,這在大量資料寫入時能明顯減少來回次數。
作者:FireOfStar
手機鎖屏密碼隨時間變化:動態密碼不怕被偷看
如今移動支付日益便捷,為了防止他人隨意使用,很多人都會啟用鎖屏功能。但傳統鎖屏多採用固定密碼,被破解的風險相對較高。那麼有沒有辦法把固定密碼改成隨時間變化的動態密碼呢?以
滑動掛斷:自定義 View 的簡單實現
「滑動解鎖」「滑動掛斷」這類控件在通話界面裡非常常見。它的好處是防誤觸——相比一個大號的紅色掛斷按鈕,需要滑動到底才能確認的操作,能有效避免通話中臉頰誤觸。 實現它只需要
Android ListView 性能優化全攻略:複用、ViewHolder 與異步亂序
前面示範 ListView 時,每一行只用了一張圖片加一段文字,跑起來毫無問題。但只要把資料量拉到 1000、10000、100000 條,打開頁面的那一刻你就會明白什麼
實作可調透明度的 Android 圖片檢視器與局部放大
這個實例做了一個功能不算多但相當實用的圖片檢視器:可以切換圖片、以按鈕調整透明度,而且手指觸碰主圖時,另一個 ImageView 會即時顯示觸碰位置附近的局部放大——有點