rsForEach使用null out allocation调用

时间:2014-06-18 09:34:51

标签: android allocation renderscript

我是renderscript的新手,正在尝试一些事情。这是我正在尝试的一个例子。一切似乎都运作良好。

public class RsYuv implements TextureView.SurfaceTextureListener
{
     private int mHeight;
     private int mWidth;
     private RenderScript mRS;
     private Allocation mAllocationOut;
     private Allocation mAllocationIn;
     private ScriptIntrinsicYuvToRGB mYuv;
     private boolean mHaveSurface;
     private Surface mSurface;

    RsYuv(RenderScript rs) {
        mRS = rs;
        mYuv = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(mRS));
    }


     public void reset(int mWidth, int mHeight) {
        Type.Builder tb = new Type.Builder(mRS, Element.RGBA_8888(mRS));
        tb.setX(mWidth);
        tb.setY(mHeight);
        Type t = tb.create();
        mAllocationOut = Allocation.createTyped(mRS, t, Allocation.USAGE_SCRIPT |
                                                        Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SHARED);

        tb = new Type.Builder(mRS, Element.createPixel(mRS, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV));
        tb.setX(mWidth);
        tb.setY(mHeight);
        tb.setYuvFormat(android.graphics.ImageFormat.NV21);
        mAllocationIn = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
        mYuv.setInput(mAllocationIn);

        setupSurface();
    }

    void execute(byte[] yuv) {
         mAllocationIn.copyFrom(yuv);
        if (mHaveSurface) {
            mYuv.forEach(mAllocationOut);
            mAllocationOut.ioSend();
        }
    }

    void setupSurface() {
        if (mAllocationOut != null) {

            mAllocationOut.setSurface(mSurface);
        }
        if (mSurface != null) {
            mHaveSurface = true;
        } else {
            mHaveSurface = false;
        }
    }
}

这似乎运作良好。但是当我引入另一个分配时,它总是给我

non fatal RS error, rsForEach called with null out allocations

以下是我在声明后初始化另一个分配的方法。

public void reset(int mWidth, mHeight) {
    // after initializing mAllocationOut

     mAllocationOut1 = Allocation.createTyped(mRS, mAllocationOut.getType(), Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SHARED);

    // further code
}

void execute(data[] yuv) {
     if (mHaveSurface) {
        mYuv.forEach(mAllocationOut);
        mYuv.forEach(mAllocationOut1); // Here it gives the error
        mAllocationOut.ioSend();
     }
}

可能是什么问题?我想复制mAllocationOut。我尝试使用copyFrom,但每次都会导致崩溃而不会提供太多详细错误。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

上面的评论警告说“有可能在没有绑定表面的情况下发生IO_INPUT / IO_INPUT”,所以我猜这就是你所看到的。你有没有打电话给mAllocationOut1.setSurface?使用IO_OUTPUT创建分配不会创建任何后备存储;后备存储来自与之相关的任何Surface。这将解释警告和copyTo崩溃。 (我会查看是否需要在某处添加更多错误检查以使其更加明显)