英文:
Renderscript Error: no memory object set to this allocation
问题
我正在为 Android 的 camera2 编写一个 YUV 到 RGBA 的转换器,它通过 GPU 纹理获取输入,并返回一个 byte[]
(以及图像大小)。
在我测试的所有设备上都能正常工作,但在三星 Galaxy S9+ 上,首次运行脚本时我也会收到以下错误消息:
E/RenderScript(14384): 错误:此分配没有设置内存对象:0x72072c2c80
D/RenderScript(14384): [RS-DIAG] 不支持在 GPU 上使用已使用的分配
D/RenderScript(14384): [RS-DIAG] 正在启动 CPU 脚本:slot(1) 46 0x71cad8a000
然后在以后的运行中:
D/RenderScript(14384): [RS-DIAG] 由于先前的错误而禁用 GPU
D/RenderScript(14384): [RS-DIAG] 正在启动 CPU 脚本:slot(1) 47 0x71cad8a000
我找不到关于上述两个错误消息的任何信息。我注意到尽管仍然会报告错误,但脚本似乎仍按预期工作,因为生成了正确的输出。
我对脚本在 GPU 上无法运行感到满意,但事实上,renderscript 报告了错误而不仅仅是警告,这让我感到困惑。
我猜我真正的问题是:是否可以安全地忽略此错误,或者在其他设备/更新的版本上会失败?
仅供参考,以下是我的代码:
import android.graphics.ImageFormat;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.Type;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
public class YUV420Converter implements Allocation.OnBufferAvailableListener {
private static String TAG = "YUV420Converter";
private Allocation mYUVInputAllocation;
private Allocation mOutputAllocation;
private Allocation mDummyInputAllocation;
private Size mPreviewSize;
private ScriptC_yuv2rgb mScriptC;
private BGRA8888Listener mBGRA8888Listener;
public interface BGRA8888Listener {
public void newByteArray(byte[] bytes, Size size);
}
YUV420Converter(RenderScript rs, Size dimensions, BGRA8888Listener mBGRA8888Listener) {
mPreviewSize = dimensions;
this.mBGRA8888Listener = mBGRA8888Listener;
createAllocation(rs, dimensions);
mScriptC = new ScriptC_yuv2rgb(rs);
mScriptC.set_gCurrentFrame(mYUVInputAllocation);
mYUVInputAllocation.setOnBufferAvailableListener(this);
}
private void createAllocation(RenderScript rs, Size dimensions) {
Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.YUV(rs));
yuvTypeBuilder.setX(dimensions.getWidth());
yuvTypeBuilder.setY(dimensions.getHeight());
yuvTypeBuilder.setYuvFormat(ImageFormat.YUV_420_888);
mYUVInputAllocation = Allocation.createTyped(rs, yuvTypeBuilder.create(),
Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT);
Type.Builder rgbTypeBuilder = new Type.Builder(rs, Element.RGBA_8888(rs));
rgbTypeBuilder.setX(dimensions.getWidth());
rgbTypeBuilder.setY(dimensions.getHeight());
mDummyInputAllocation = Allocation.createTyped(rs, rgbTypeBuilder.create(),
Allocation.USAGE_SCRIPT);
mOutputAllocation = Allocation.createTyped(rs, rgbTypeBuilder.create(),
Allocation.USAGE_SCRIPT);
}
public Surface getInputNormalSurface() {
return mYUVInputAllocation.getSurface();
}
@Override
public void onBufferAvailable(Allocation a) {
Log.d(TAG, "开始转换");
long start = System.currentTimeMillis();
// 获取最新的输入
mYUVInputAllocation.ioReceive();
// 运行处理过程
mScriptC.forEach_yuv2rgb(mDummyInputAllocation, mOutputAllocation);
byte[] b = new byte[mPreviewSize.getWidth() * mPreviewSize.getHeight() * 4];
mOutputAllocation.copyTo(b);
Log.d(TAG, "转换耗时:" + (System.currentTimeMillis() - start) + " 毫秒");
mBGRA8888Listener.newByteArray(b, mPreviewSize);
}
}
此代码的使用类似于以下伪代码:
renderScript = RenderScript.create(activity);
yuvConverter = new YUV420Converter(renderScript, yuvCaptureSize, bgra8888Listener);
List<Surface> surfaces = new ArrayList<>();
surfaces.add(yuvConverter.getInputNormalSurface());
cameraDevice.createCaptureSession(surfaces, ... );
captureRequestBuilder.addTarget(yuvConverter.getInputNormalSurface());
cameraCaptureSession.capture(captureRequestBuilder.build(), ...);
(以上内容仅为翻译结果,不包含问题回答部分)
英文:
I am working on a YUV->RGBA converter for android's camera2 that gets its input via GPU texture and returns a byte[]
(and the image size).
This works fine on all devices that I tested but on Samsung Galaxy S9+ I also get the following error messages on the first run of the script:
E/RenderScript(14384): Error: no memory object set to this allocation: 0x72072c2c80
D/RenderScript(14384): [RS-DIAG] A used allocation is not supported on the GPU
D/RenderScript(14384): [RS-DIAG] Launching CPU script : slot(1) 46 0x71cad8a000
and then on later runs:
D/RenderScript(14384): [RS-DIAG] GPU disabled due to earlier error
D/RenderScript(14384): [RS-DIAG] Launching CPU script : slot(1) 47 0x71cad8a000
I could not find anything about the two error messages on top. I noticed that the script still seems to work as expected since the correct output is generated.
I am fine with the script not running on the GPU but the fact that renderscript reports an error and not just a warning throws me off.
I guess my real question is: Is this error safe to ignore or will it fail on other devices/newer versions?
Just for reference, here is my code:
import android.graphics.ImageFormat;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.Type;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
public class YUV420Converter implements Allocation.OnBufferAvailableListener {
private static String TAG = "YUV420Converter";
private Allocation mYUVInputAllocation;
private Allocation mOutputAllocation;
private Allocation mDummyInputAllocation;
private Size mPreviewSize;
private ScriptC_yuv2rgb mScriptC;
private BGRA8888Listener mBGRA8888Listener;
public interface BGRA8888Listener {
public void newByteArray(byte[] bytes, Size size);
}
YUV420Converter(RenderScript rs, Size dimensions, BGRA8888Listener mBGRA8888Listener) {
mPreviewSize = dimensions;
this.mBGRA8888Listener = mBGRA8888Listener;
createAllocation(rs, dimensions);
mScriptC = new ScriptC_yuv2rgb(rs);
mScriptC.set_gCurrentFrame(mYUVInputAllocation);
mYUVInputAllocation.setOnBufferAvailableListener(this);
}
private void createAllocation(RenderScript rs, Size dimensions) {
Type.Builder yuvTypeBuilder = new Type.Builder(rs, Element.YUV(rs));
yuvTypeBuilder.setX(dimensions.getWidth());
yuvTypeBuilder.setY(dimensions.getHeight());
yuvTypeBuilder.setYuvFormat(ImageFormat.YUV_420_888);
mYUVInputAllocation = Allocation.createTyped(rs, yuvTypeBuilder.create(),
Allocation.USAGE_IO_INPUT | Allocation.USAGE_SCRIPT);
Type.Builder rgbTypeBuilder = new Type.Builder(rs, Element.RGBA_8888(rs));
rgbTypeBuilder.setX(dimensions.getWidth());
rgbTypeBuilder.setY(dimensions.getHeight());
mDummyInputAllocation = Allocation.createTyped(rs, rgbTypeBuilder.create(),
Allocation.USAGE_SCRIPT);
mOutputAllocation = Allocation.createTyped(rs, rgbTypeBuilder.create(),
Allocation.USAGE_SCRIPT);
}
public Surface getInputNormalSurface() {
return mYUVInputAllocation.getSurface();
}
@Override
public void onBufferAvailable(Allocation a) {
Log.d(TAG, "Beginning conversion");
long start = System.currentTimeMillis();
// Get to newest input
mYUVInputAllocation.ioReceive();
// Run processing pass
mScriptC.forEach_yuv2rgb(mDummyInputAllocation, mOutputAllocation);
byte[] b = new byte[mPreviewSize.getWidth() * mPreviewSize.getHeight() * 4];
mOutputAllocation.copyTo(b);
Log.d(TAG, "Conversion took " + (System.currentTimeMillis() - start) + " ms");
mBGRA8888Listener.newByteArray(b, mPreviewSize);
}
}
Which is used like in this pseudocode:
renderScript = RenderScript.create(activity);
yuvConverter = new YUV420Converter(renderScript, yuvCaptureSize, bgra8888Listener);
List<Surface> surfaces = new ArrayList<>();
surfaces.add(yuvConverter.getInputNormalSurface());
cameraDevice.createCaptureSession(surfaces, ... );
captureRequestBuilder.addTarget(yuvConverter.getInputNormalSurface());
cameraCaptureSession.capture(captureRequestBuilder.build(), ...);
答案1
得分: 1
不清楚实际错误,但你可以尝试使用 Android 内置的 intrinisc 进行处理(是的,输出是 RGBA):
https://developer.android.com/reference/android/renderscript/ScriptIntrinsicYuvToRGB
英文:
No idea about the actual error, but you could try using the android intrinisc for that (yes, the output is RGBA):
https://developer.android.com/reference/android/renderscript/ScriptIntrinsicYuvToRGB
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论