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

545 views Asked by At

I am trying to use the built in RenderScript script for converting NV21 to RGBA8888, but even thoguh I checked the size of the buffer in the Allocation object I still got the following error: Fatal signal 11 (SIGSEGV) at 0x4eb30000 (code=1), thread 18458 (epthsyncexample)

My code:

 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;

The error itself is easy to understand, but why it happens I do not know. The byte array I use to represent the NV21 image is of size 1382400 bytes. The Allocation buffer is of 1280*720*1.5 = 1382400 bytes. I dont understand why the marked line of code caused a segmentation fault.

Any hints?

I have read some posts like this and this, but they are about completely different problems. The only question that might have something to do with it is this one. Where can I find out about this limit?

1

There are 1 answers

0
Noobs DeSroobs On BEST ANSWER

After a lot of meddling with the code I realized what was causing the problem. When I was debugging through the code I must have caused some race conditions or something because every time I load data to and from the unit I had a high chance of getting a seg fault.

The code that I ended up with that works IF ALLOWED TO RUN passed the RenderScript segment was:

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;
}