android - “Renderscript Allocation copyto”耗时很多,如何解决?

标签 android renderscript

void sobel(const uchar * v_in , uchar4 *v_out, const void * userData , uint32_t x , uint32_t y)
{
    if ((x==0)||(x==width-1)||(y==0)||(y==height-1)) return;

    uchar a00 = rsGetElementAt_uchar(gIn,x-1,y-1); 
    uchar a01 = rsGetElementAt_uchar(gIn,x,y-1);
    uchar a02 = rsGetElementAt_uchar(gIn,x+1,y-1);
    uchar a10 = rsGetElementAt_uchar(gIn,x-1,y);
    uchar a11 = rsGetElementAt_uchar(gIn,x,y);
    uchar a12 = rsGetElementAt_uchar(gIn,x+1,y);
    uchar a20 = rsGetElementAt_uchar(gIn,x-1,y + 1);
    uchar a21 = rsGetElementAt_uchar(gIn,x,y + 1);
    uchar a22 = rsGetElementAt_uchar(gIn,x+1,y + 1);


    short ux = ((short)((a20) * (1) + (a21) * (2) + (a22) * (1) + (a00) * (-1) + (a01) * (-2) + (a02) * (-1)));
    short uy = ((short)((a02) * (1) + (a12) * (2) + (a22) * (1) + (a00) * (-1) + (a10) * (-2) + (a20) * (-1)));

    uchar resx = (NDA_CAST_8U(ux));
    uchar resy = (NDA_CAST_8U(uy));

    //outdata[y*width + x] = resx;

    rsSetElementAt_uchar(gUx,resx,x,y);
    rsSetElementAt_uchar(gUy,resy,x,y);

    uchar res  = 255 * (uchar)(ux > 10 || uy > 10 );
    *v_out = (uchar4){res,res,res,255};
}

我对渲染脚本感到困惑。我只是想用它来做Imageprocess sobel (image size : 640 * 480; 我试着用JNI做sobel,但是需要3.5ms,太长了. 逐帧,使用我从相机 surfaceview 获取的字节数据,我发现,无论我编码还是使用 api ScriptIntrinsicConvolve3x3,copyto 和 copyfrom 操作都需要很多时间(大约 15 毫秒)。我想知道为什么分配这么慢以及我能做些什么。

最佳答案

这实际上是您的计算花费的时间,而不是 copyTo。 计算在后台执行,只有当您尝试访问结果(使用 copyTo)时,您才需要等待计算完成。

当您调用 script.forEach_sobel 时,它几乎立即返回,但这并不意味着计算已经完成,它在后台运行。

您可以通过注释掉 script.forEach_sobel 调用来检查 copyTo 的实际时间,如果您不调用此方法而只是尝试使用 copyTo 那么它会快得多。

关于android - “Renderscript Allocation copyto”耗时很多,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23588714/

相关文章:

android - 没有 rsDebug,RenderScript 代码无法工作

android - 在 Android 中收听传出的短信或已发送的邮箱

java - RxJava : Range operator is not working

java - RenderScript 支持库中缺少 Mesh 类

android - 库项目中的 Renderscript 将导致 findViewById() 返回 null

Android RenderScript Matrix_2x2 元素数据类型

gpu - 渲染脚本和 GPU

java - Android avd Manager 和 Sdk.exe 未打开

java - 如何将新的依赖项插入 Gradle 项目?

将服务绑定(bind)到 android.app.Activity 与将其绑定(bind)到 android.app.Application