Renderscript ScriptIntrinsicYuvToRGB and Bitmap allocation

旧巷老猫 提交于 2019-12-25 03:34:25

问题


I want to make my own SurfaceView and send there a frames which I've obtained from onPreviewFrame(byte[] data, Camera camera) method. To do it, I need to conver frames from Yuv to ARGB and to draw them to a Canvas to my SurfaceView.

Here is my onPreviewFrame:

        public void onPreviewFrame(byte[] data, Camera camera) {

        if (camera != null) {
            camera.addCallbackBuffer(data);
        }

        // using RenderScript
        Bitmap bitmap = RenderScriptFilter.convertYuvToRgbIntrinsic(rs, data, PREVIEW_WIDTH, PREVIEW_HEIGHT);

        if (mFilterSurfaceView != null) {
            SurfaceHolder holder = mFilterSurfaceView.getHolder();
            Canvas canvas = holder.lockCanvas();

            // draw bitmap to the canvas
            canvas.drawBitmap(bitmap, 0, 0, mPaint);

            holder.unlockCanvasAndPost(canvas);
            mFrames++;
            System.gc();
        }
    }
};

Here is convertYuvToRgbIntrinsic method:

    public static Bitmap convertYuvToRgbIntrinsic(RenderScript rs, byte[] data, int w, int h) {

    int imageWidth = w ;
    int imageHeight = h ;

    ScriptIntrinsicYuvToRGB yuvToRgbIntrinsic = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(rs));

    // Create the input allocation  memory for Renderscript to work with
    Type.Builder yuvType = new Type.Builder(rs, Element.U8(rs))
            .setX(imageWidth)
            .setY(imageHeight)
            .setYuvFormat(android.graphics.ImageFormat.NV21);

    Allocation aIn = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
    // Set the YUV frame data into the input allocation
    aIn.copyFrom(data);


    // Create the output allocation
    Type.Builder rgbType = new Type.Builder(rs, Element.RGBA_8888(rs))
            .setX(imageWidth)
            .setY(imageHeight);

    Allocation aOut = Allocation.createTyped(rs, rgbType.create(), Allocation.USAGE_SCRIPT);



    yuvToRgbIntrinsic.setInput(aIn);
    // Run the script for every pixel on the input allocation and put the result in aOut
    yuvToRgbIntrinsic.forEach(aOut);

    // Create an output bitmap and copy the result into that bitmap
    Bitmap outBitmap = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
    aOut.copyTo(outBitmap);

    return outBitmap ;

}

I think the problem appears because of System.gc() method. Because when I try with 640x480 all okay, but with 1280x720 I obtain some errors:

12-30 13:06:59.063 18034-18178/youten.redo.y2ndkyuv420sp E/RenderScript: rsAssert failed: cmd->cmdID < (sizeof(gPlaybackFuncs) / sizeof(void *)), in frameworks/rs/rsThreadIO.cpp at 156 12-30 13:06:59.063 18034-18178/youten.redo.y2ndkyuv420sp E/RenderScript: playCoreCommands error con 0x78fb79a0, cmd 2072915616 12-30 13:06:59.063 18034-18178/youten.redo.y2ndkyuv420sp A/libc: Fatal signal 11 (SIGSEGV) at 0x2fa27a84 (code=1), thread 18178 (o.y2ndkyuv420sp)

I understood that System.gc() is a bad practice, but what to do here to allocate a memory? bitmap.recycle(); bitmap = null; didn't fixes the problem.


回答1:


I don't know what triggers your error, and how this could be related to garbage collector. At any rate, 1280x720 bitmaps can exhaust your memory quickly.

It is counter-productive to recreate all renderscript allocations for each frame. The whole point is to make all preparations once, and reuse.

Attached find the utility function I use:

public Bitmap convertYuvImageToBitmap(Context context, YuvImage yuvImage) {

    int w = yuvImage.getWidth();
    int h = yuvImage.getHeight();

    if (rs == null) {
        // once
        rs = RenderScript.create(context);
        yuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
    }

    if (yuvAllocation == null || yuvAllocation.getBytesSize() < yuvImage.getYuvData().length) {
        yuvType = new Type.Builder(rs, Element.U8(rs)).setX(yuvImage.getYuvData().length);
        yuvAllocation = Allocation.createTyped(rs, yuvType.create(), Allocation.USAGE_SCRIPT);
        Log.w(TAG, "allocate in " + yuvAllocation.getBytesSize() + " " + w + "x" + h);
    }

    if (rgbaAllocation == null || 
            rgbaAllocation.getBytesSize() < rgbaAllocation.getElement().getBytesSize()*w*h) {
        rgbaType = new Type.Builder(rs, Element.RGBA_8888(rs)).setX(w).setY(h);
        rgbaAllocation = Allocation.createTyped(rs, rgbaType.create(), Allocation.USAGE_SCRIPT);
        Log.w(TAG, "allocate out " + rgbaAllocation.getBytesSize() + " " + w + "x" + h);
    }

    yuvAllocation.copyFrom(yuvImage.getYuvData());

    yuvToRgb.setInput(yuvAllocation);
    yuvToRgb.forEach(rgbaAllocation);

    if (bmpout == null) {
        bmpout = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    }
    rgbaAllocation.copyTo(bmpout);
    return bmpout;
}


来源:https://stackoverflow.com/questions/34453174/renderscript-scriptintrinsicyuvtorgb-and-bitmap-allocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!