RenderScript自定义ScriptC比Intrinsics慢

时间:2015-07-30 15:40:16

标签: android renderscript

我创建了一个非常简单的灰度脚本,它需要比内在模糊渲染更多的时间。为什么?

grayscale.rs:

#pragma version(1)
#pragma rs java_package_name(com.nanotapsoft.cartooncamera)

#pragma rs_fp_relaxed

uchar4 __attribute__((kernel)) grayscale(uchar4 pixelIn, uint32_t x, uint32_t y) {
    uchar grayscale = (pixelIn.r + pixelIn.g + pixelIn.b) / 3;  // simple average
    //uchar grayscale = pixelIn.r * 0.299 + pixelIn.g * 0.587 + pixelIn.b * 0.114;

    uchar4 pixelOut;
    pixelOut.a = pixelIn.a;
    pixelOut.r = grayscale;
    pixelOut.g = grayscale;
    pixelOut.b = grayscale;

    return pixelOut;
}

在java中调用:

    t1 = System.currentTimeMillis();

    iBmp.copyFrom(tmpbmp);

    blurscript.setInput(iBmp);
    blurscript.forEach(oBmp);

    oBmp.copyTo(tmpbmp);

    t2 = System.currentTimeMillis();
    System.out.println("Blur: " + (t2-t1));

    t1 = System.currentTimeMillis();

    iBmp.copyFrom(tmpbmp);
    grayscaleScript.forEach_grayscale(iBmp, oBmp);
    oBmp.copyTo(tmpbmp);

    t2 = System.currentTimeMillis();
    System.out.println("Grayscale: " + (t2-t1));

我得到平均12毫秒的模糊和28毫秒的灰度

1 个答案:

答案 0 :(得分:0)

对于那些想要更快灰度级渲染的用户,如果使用YUV数据,最多可以提高5倍:

#pragma version(1)
#pragma rs java_package_name(com.nanotapsoft.cartooncamera)

#pragma rs_fp_relaxed

rs_allocation ain;
rs_allocation aout;

rs_script grayscaleYUVScript;

void root(const uchar *v_in, uchar4 *v_out, const void *usrData, uint32_t x, uint32_t y) {

    uchar yp = rsGetElementAtYuv_uchar_Y(ain, x, y) & 0xff;
    v_out->r = yp;
    v_out->g = yp;
    v_out->b = yp;
    v_out->a = 0xff;
}

void filter()
{
    rs_allocation ignored;
    #if !defined(RS_VERSION) || (RS_VERSION < 14)
        rsForEach(grayscaleYUVScript, ignored, aout, 0);
    #else
        rsForEach(grayscaleYUVScript, ignored, aout);
    #endif
}

对于我使用的第一个脚本的相同输入大小,此脚本的平均执行时间约为7毫秒。