简体   繁体   English

Android RenderScript与不确定元素一起使用

[英]Android RenderScript use with indeterminate elements

I would like to know if RenderScript engine can be used to optimize an algorithm. 我想知道RenderScript引擎是否可以用来优化算法。

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). 但是我只看到RenderScript示例可以在具有N个元素的良好定义的分配上工作(典型示例是内核对位图中的每个像素执行一个内核)。

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. 一种解决方案是创建一个Java“ while”周期,并在每次迭代时调用RenderScript代码(它将只有一个Element,一个内核周期),并在算法完成时从java中进行检查。 Would this be performant? 这会表现吗? I mean, I will have to pass a bitmap to RenderScript, would this cause an entire copy to be made? 我的意思是,我将必须将位图传递给RenderScript,这会导致整个副本被制作吗? Wouldnt be NDK a better solution in this case? 在这种情况下,NDK会不会是更好的解决方案?

Which would be the best performant approach to solve this? 哪种方法是解决此问题的最佳方法?

Thanks! 谢谢! Juan Ignacio 胡安·伊格纳西奥(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. 您可以在每次迭代中都使用Java进行流控制,但这并不是最好的方法,并且需要大量额外的东西来确保您不会阻塞UI线程和类似的事情。

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. 做到这一点的好方法是创建一个单线程RS函数(而不是使用__attribute((kernel))或类似的东西创建的函数),并使用rsForEach生成每次迭代。 when that iteration is done, rsForEach will return, you can check your termination conditions via script globals, and launch another iteration if necessary. 迭代完成后,rsForEach将返回,您可以通过脚本全局变量检查终止条件,并在必要时启动另一个迭代。 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). 这也将开销降至最低,因为您将在整个迭代过程的运行时都位于RS环境中(较少的JNI转换,较少的CPU内核开/关转换,诸如此类)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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