我想使用以下RenderScript代码计算位图的像素
文件名:counter.rs
#pragma version(1)
#pragma rs java_package_name(com.mypackage)
#pragma rs_fp_relaxed
uint count; // initialized in Java
void countPixels(uchar4* unused, uint x, uint y) {
rsAtomicInc(&count);
}
Application context = ...; // The application context
RenderScript rs = RenderScript.create(applicationContext);
Bitmap bitmap = ...; // A random bitmap
Allocation allocation = Allocation.createFromBitmap(rs, bitmap);
ScriptC_Counter script = new ScriptC_Counter(rs);
script.set_count(0);
script.forEach_countPixels(allocation);
allocation.syncAll(Allocation.USAGE_SCRIPT);
long count = script.get_count();
这是我收到的错误消息:
错误:未找到计数地址
答案 0 :(得分:1)
作为旁注,除非必须,否则在并行计算中使用原子操作通常不是一个好习惯。 RenderScript实际上为这种应用程序提供了reduction kernel。也许你可以尝试一下。
代码有几个问题:
如果你必须使用rsAtomicInc,一个很好的例子实际上是RenderScript CTS测试:
答案 1 :(得分:1)
这是我的工作解决方案。
文件名:counter.rs
#pragma version(1)
#pragma rs java_package_name(com.mypackage)
#pragma rs_fp_relaxed
int32_t count = 0;
rs_allocation rsAllocationCount;
void countPixels(uchar4* unused, uint x, uint y) {
rsAtomicInc(&count);
rsSetElementAt_int(rsAllocationCount, count, 0);
}
Context context = ...;
RenderScript renderScript = RenderScript.create(context);
Bitmap bitmap = ...; // A random bitmap
Allocation allocationBitmap = Allocation.createFromBitmap(renderScript, bitmap);
Allocation allocationCount = Allocation.createTyped(renderScript, Type.createX(renderScript, Element.I32(renderScript), 1));
ScriptC_Counter script = new ScriptC_Counter(renderScript);
script.set_rsAllocationCount(allocationCount);
script.forEach_countPixels(allocationBitmap);
int[] count = new int[1];
allocationBitmap.syncAll(Allocation.USAGE_SCRIPT);
allocationCount.copyTo(count);
// The count can now be accessed via
count[0];