这是argout范例在RenderScript中的良好实践吗?

时间:2013-11-14 00:29:35

标签: renderscript

RenderScript中的反射类包含执行内核的函数。这些函数遵循out参数范例 - 它们的一个参数是存储输出的Allocation。

这是否有理由比返回输出分配更好? (我应该跟随并在我的与RenderScript相关的函数中使用参数?)

例如,我实现了以下帮助器类,它包装了ScriptC_gradient并计算了Bitmap的渐变。 可以从输入分配推断输出分配应具有的类型,从而隐藏设置目标分配所需的样板。是否有理由更喜欢compute()的一个实现而不是另一个?

public class Gradient {
    private RenderScript mRS;
    private ScriptC_gradient mScript;

    public Gradient(RenderScript RS) {
        mRS = RS;
        mScript = new ScriptC_gradient(mRS);
    }
    /* Out-argument implementation
     * 
     * This closely mirrors RenderScript's kernel functions, but
     * it requires the caller to write boilerplate to set up the 
     * destination Allocation.
     */
    public void compute(Allocation elevation, Allocation gradient) {
        mScript.invoke_setDimensions(elevation);
        mScript.forEach_root(elevation, gradient);
    }
    /* Allocation-returning implementation
     * 
     * This hides the boilerplate. 
     */
    public Allocation compute(Allocation elevation) {
        Allocation gradient = Allocation.createTyped(mRS, 
                new Type.Builder(mRS,Element.F32_2(mRS))
                   .setX(elevation.getType().getX())
                   .setY(elevation.getType().getY())
                   .create(),
                Allocation.USAGE_SCRIPT);
        mScript.invoke_setDimensions(elevation);
        mScript.forEach_root(elevation, gradient);
        return gradient;
    }

1 个答案:

答案 0 :(得分:3)

是的,更喜欢使用传入分配输出方法的原因是内存重用。创建分配是昂贵的,不应该超出必要的范围。

第二种方法也会导致“平铺”问题,你可以在每次启动多次内核时填写输出分配的一部分。因为每次先前的内容丢失(或必须复制)时都会重新分配输出。

相关问题