如何让Android Render Script Group工作?

Xia*_*ang 6 android renderscript

我可以让两个独立的内在函数工作,但不能在ScriptGroup中一起工作.我发现有关如何使用Script Group的文档非常稀疏.

这是我的代码:

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);
Run Code Online (Sandbox Code Playgroud)

灰色和模糊都有效.然后我尝试将它们放在一起,结果是空白的.码:

// 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);
Run Code Online (Sandbox Code Playgroud)

mah*_*ren 5

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

-1-如果要使用内部函数,请使用以下序列。

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();
Run Code Online (Sandbox Code Playgroud)

-2-注意输入分配的传递方式。输入分配将传递给mBlur.setInput()而不是sGroup.setInput()。如果使用sGroup.setInput(),则该组正确找不到输入,并导致以下错误,当然,我也看不到屏幕上的转换图像。

E/RenderScript(12023): rsAssert failed: !"ScriptGroup:setInput kid not found", in frameworks/rs/rsScriptGroup.cpp at 267
Run Code Online (Sandbox Code Playgroud)

在-1的特定示例中,在使用sGroup.setInput()而不是mBlur.setInput()的那一刻,我也收到以下错误

E/RenderScript(12023): Blur executed without input, skipping
Run Code Online (Sandbox Code Playgroud)

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

-3-具体地说,在您要使用ScriptIntrinsicBlur依次执行ScriptIntrinsicColorMatrix的情况下,存在另一个问题(不一定是bug)。虽然模糊固有函数具有setInput函数,但colorMatrix确实具有setInput函数。因此,您也不能使用-1作为解决方法。

-4-我认为在renderscript中正确的解决方法是像在ScriptIntrinsicColorMatrix中那样,普遍地弃用internal.setInput,并在脚本组中使用内部函数时使ScriptGroup.setInput正常工作。

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

  • ScriptGroup将在某个时候进行全面改革,它比现在的许多其他事情要重要得多。不过,我将看看是否可以在此期间为这种内在情况添加解决方案。 (2认同)