我可以使用两个单独的内在函数,但不能在ScriptGroup中一起工作。我发现有关如何使用脚本组的文档极为稀疏。

这是我的代码:

mRS = RenderScript.create(getActivity());

mInAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,
        Allocation.MipmapControl.MIPMAP_NONE,
        Allocation.USAGE_SCRIPT |
                Allocation.USAGE_GRAPHICS_TEXTURE |
                Allocation.USAGE_SHARED);

mOutAllocation = Allocation.createFromBitmap(mRS, mBitmapOut,
        Allocation.MipmapControl.MIPMAP_NONE,
        Allocation.USAGE_SCRIPT |
                Allocation.USAGE_SHARED);

ScriptIntrinsicColorMatrix gray = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS));
gray.setGreyscale();

ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS));
blur.setRadius(20.f);
blur.setInput(mInAllocation);

//gray.forEach(mInAllocation, mOutAllocation);
blur.forEach(mOutAllocation);

mOutAllocation.copyTo(mBitmapOut);

灰色和模糊效果均有效。然后我尝试将它们放在一起,结果是空白。代码:
// gray.forEach(mInAllocation, mOutAllocation);
// blur.forEach(mOutAllocation);
// mOutAllocation.copyTo(mBitmapOut);

ScriptGroup.Builder builder = new ScriptGroup.Builder(mRS);
builder.addKernel(gray.getKernelID());
builder.addKernel(blur.getKernelID());
builder.addConnection(mInAllocation.getType(), gray.getKernelID(), blur.getKernelID());

ScriptGroup group = builder.create();
group.setInput(gray.getKernelID(), mInAllocation);
group.setOutput(blur.getKernelID(), mOutAllocation);
group.execute();

mOutAllocation.copyTo(mBitmapOut);

最佳答案

我能够重现您所看到的问题,并通过我之前对内在函数进行的实验中的注释进行了交叉检查。我认为renderscript内在代码中存在一些错误。

-1-
如果您希望使用内部函数的Scripgroup,则下面的序列适用。

mBlur.setInput(mInAllocation);
sBuilder = new ScriptGroup.Builder(mRS);
sBuilder.addKernel(mBlur.getKernelID());
sBuilder.addKernel(mColor.getKernelID());
sBuilder.addConnection(connect, mBlur.getKernelID(), mColor.getKernelID());

sGroup = sBuilder.create();
//  sGroup.setInput(mBlur.getKernelID(), mInAllocation); //See point 2
sGroup.setOutput(mColor.getKernelID(), mOutAllocation);
sGroup.execute();

mOutAllocation.copyTo(outBitmap);
mRS.finish();

-2-
注意输入分配的传递方式。输入分配将传递给mBlur.setInput()而不是sGroup.setInput()。如果使用sGroup.setInput(),则该组正确找不到输入,并且会导致以下错误,当然,我也看不到屏幕上的转换图像。
E/RenderScript(12023): rsAssert failed: !"ScriptGroup:setInput kid not found", in frameworks/rs/rsScriptGroup.cpp at 267

在-1的特定示例中,在使用sGroup.setInput()而不是mBlur.setInput()的那一刻,我也遇到了以下错误。
E/RenderScript(12023): Blur executed without input, skipping

这似乎是渲染脚本中的错误

-3-
具体来说,在您的情况下,如果您想按顺序使用ScriptIntrinsicBlur进行ScriptIntrinsicColorMatrix,则会出现另一个问题(不一定是bug)。虽然模糊固有函数具有setInput函数,但colorMatrix确实具有setInput函数。因此,您也不能使用-1作为解决方法。

-4-
我认为renderscript的正确修复方法是不赞成使用
就像对ScriptIntrinsicColorMatrix所做的一样,native.setInput可以使ScriptGroup.setInput在脚本组中使用内部函数时正常工作。

-5-
当我有自己的内核时,使用脚本组没有发现任何问题。换句话说,scriptGroup.setInput()和scriptGroup.setOutput()可以正常工作

10-08 07:08