简体   繁体   中英

Android RenderScript use with indeterminate elements

I would like to know if RenderScript engine can be used to optimize an algorithm.

The algorithm does iterative processing over an image until some conditions are met. There is no way to know before processing the number of iterations needed. But I only saw RenderScript examples that work over a well defined Allocation that has N Elements (the typical example is that a Kernel executes for every pixels in bitmap).

One solution would be create a Java "while" cycle, and on each iteration call the RenderScript code (it will have just one Element, one single kernel cycle), and check from java when the algorithm finishes. Would this be performant? I mean, I will have to pass a bitmap to RenderScript, would this cause an entire copy to be made? Wouldnt be NDK a better solution in this case?

Which would be the best performant approach to solve this?

Thanks! Juan Ignacio

you could go back to Java for flow control on each iteration, but that's not the best and requires a bunch of extra stuff to make sure you don't block the UI thread and things like that.

the good way to do this is to create a single-threaded RS function (not one created with __attribute((kernel)) or anything like that) and use rsForEach to spawn each iteration. when that iteration is done, rsForEach will return, you can check your termination conditions via script globals, and launch another iteration if necessary. this will keep overhead to a bare minimum, too, since you'll be living within the RS environment for your entire iterative process's runtime (fewer JNI transitions, fewer on/off transitions of CPU cores, things like that).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM