[英]How to pass a void pointer to a struct pointer and to resolve the read memory error
[英]How to make Renderscript update a pointer to struct?
所以我在Renderscript代碼中有一個帶有指針的結構:
typedef struct __attribute__((packed, aligned(4))) RGB {
float red;
float green;
float blue;
} RGB_t;
RGB_t *rgb;
這就是我將內存分配和綁定到此指針的方式:
ScriptField_RGB rgbs = new ScriptField_RGB(mRS, bitmap.getWidth()*bitmap.getHeight());
function_script.bind_rgb(rgbs);
這是我設置初始值的方式:
int index = 0;
for(int y = 0; y < bitmap.getHeight(); ++y) {
for(int x = 0; x < bitmap.getWidth(); ++x) {
int color = bitmap.getPixel(x, y);
ScriptField_RGB.Item i = new ScriptField_RGB.Item();
i.red = Color.red(color) / 255.0f;
i.green = Color.green(color) / 255.0f;
i.blue = Color.blue(color) / 255.0f;
rgbs.set(i, index, false);
index++;
}
}
rgbs.copyAll();
“ function_script”是一個帶有內核的腳本,用於填充“ RGB_t *”,就像這樣:
#pragma version(1)
#pragma rs java_package_name(my.package)
#include "rgb.rs"
void __attribute__((kernel)) root(RGB_t pixel, uint32_t x) {
//rsDebug("before", rgb[x].red,rgb[x].green,rgb[x].blue);
rgb[x].red = pixel.red * 255.0f;
rgb[x].green = pixel.green * 255.0f;
rgb[x].blue = pixel.blue * 255.0f;
//rsDebug("after", rgb[x].red,rgb[x].green,rgb[x].blue);
}
使用以下命令運行該內核:
function_script.forEach_root(arrayOperational);
不會在Android Layer更新我的ScriptField值。 我仍然有我以前確定的舊價值觀。
因此,如果我致電:
rgbs.get(index)
當然,在“ foreach root”調用之后,我將得到我原來的值。
如何在Android Layer中反映我的更改?
由於您正在處理Bitmap
例如數據(RGB值),因此應考慮使用輸入和輸出Allocation
對象,而不是您自己的綁定。 Rendescript將為您有效地解決此問題,而不必像這樣手動綁定字段。
但是,如果你必須這樣做綁定,你需要安裝現場的使用情況,包括Allocation.USAGE_SHARED
(使用不同,重載的構造)。通過在地方,你應該能夠調用updateAllocation()
上Java中的field對象以從腳本中獲取最新數據。 如果這不起作用(取決於自動生成的代碼),則可以嘗試調用getAllocation()
然后調用Allocation.syncAll(Allocation.USAGE_SHARED)
以使用腳本中的任何更改來更新Allocation
。 此時,您的字段訪問方法應提供更新的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.