Reputation: 31
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};
}
I'm confused about renderscript. I just want use it to do Imageprocess sobel (image size : 640 * 480
; I tried to use JNI to do sobel
, but it takes 3.5ms, which is too long. Frame-per-frame, with the byte data I get from camera surfaceview
, I find that, whether I code or use the api ScriptIntrinsicConvolve3x3
, the copyto and copyfrom operation take a lot of time (about 15ms). I'm wondering why the allocation is so slow and what I can do about this.
Upvotes: 3
Views: 1318
Reputation: 57203
I have found that Allocation.copyTo()
may take significant time if renderscript is scheduled for execution on GPU, and is immediate when scheduled for execution on CPU. But on GPU, the forEach()
was asynchronous (@Larry Schiefer), and at first I thought that GPU execution was even shorter. Luckily, AccoGuy timely corrected me.
Upvotes: 0
Reputation: 41
That's actually your computation that takes this time and not copyTo. Computation is executed in background and only when you try to access the result (using copyTo) you need to wait for the computation to finish.
When you call your script.forEach_sobel it erturns almost instantly, but this doesn't mean that the computation is already completed, it's running in background.
You can check actual time of copyTo by just commenting out script.forEach_sobel call, if you don't call this method and just try to use copyTo then it will be much faster.
Upvotes: 4