即使大小应该足够,RenderScript 的分配也会出错

Allocation for RenderScript gives error even though the size should be adequate

我正在尝试使用内置的 RenderScript 脚本将 NV21 转换为 RGBA8888,但即使我检查了分配对象中缓冲区的大小,我仍然遇到以下错误: Fatal signal 11 (SIGSEGV) at 0x4eb30000 (code=1), thread 18458 (epthsyncexample)

我的代码:

 rs = RenderScript.create(context);
    yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));


    Type.Builder yuvType = new Type.Builder(rs, Element.U8_4(rs))
            .setX(1280).setY(720)
            .setYuvFormat(android.graphics.ImageFormat.NV21);
    Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
    in.copyFrom(YUVArray); //<<<<<<<<<<<<<<<<<<<<<<<<<<


    Type.Builder rgbaType = new Type.Builder(rs, Element.U8_4(rs))
            .setX(W).setY(H);
    Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);

    byte[] RGBOut = new byte[W * H * 4];

    //

    yuvToRgbIntrinsic.setInput(in);
    yuvToRgbIntrinsic.forEach(out);

    out.copyTo(RGBOut);
    return RGBOut;

错误本身很容易理解,但我不知道为什么会这样。我用来表示 NV21 图像的字节数组大小为 1382400 字节。分配缓冲区为 1280*720*1.5 = 1382400 字节。我不明白为什么标记的代码行会导致分段错误。

有什么提示吗?

我读过一些帖子,例如 this and , but they are about completely different problems. The only question that might have something to do with it is this one。我在哪里可以找到有关此限制的信息?

在大量修改代码之后,我意识到是什么导致了问题。当我通过代码进行调试时,我一定导致了一些竞争条件或其他原因,因为每次我向单元加载数据或从单元加载数据时,我都有很高的机会出现段错误。

如果允许 运行 通过 RenderScript 段,我最终得到的代码是:

public byte[] convertYUV2RGB(byte[] YUVArray, int H, int W){//W: 1280, H: 720

    //Convert using the premade script here.
    rs = RenderScript.create(context);
    yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));

    Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs)).setX(YUVArray.length);

    Allocation in = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);

    Type.Builder rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(W).setY(H);

    Allocation out = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
    in.copyFrom(YUVArray);
    byte[] RGBOut = new byte[W * H * 4];

    yuvToRgbIntrinsic.setInput(in);
    yuvToRgbIntrinsic.forEach(out);

    out.copyTo(RGBOut);
    return RGBOut;
}