2014-01-18 66 views
4

我可以得到兩個獨立的內部函數,但不能在ScriptGroup中一起工作。我發現如何使用Script Group的文檔非常稀疏。如何讓Android Render 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); 

灰色和模糊的工作。然後我試着把它們放在一起,結果是空白的。代碼:

 // 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); 

回答

4

我能夠重現您所看到的問題,並與之前使用intrinsics進行實驗的筆記進行交叉檢查。我認爲在renderscript內在函數代碼中存在一些錯誤。

-1- 如果你想得到一個scripgroup與intrinsics工作,下面的序列的作品。

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-該具體示例中,我得到以下錯誤以及用於mBlur.setInput(),而不是瞬間sGroup.setInput()

E/RenderScript(12023): Blur executed without input, skipping 

這似乎是在渲染器中的錯誤

-3- 具體來說,在你的情況下,如果你想在一個序列中使用ScriptIntrinsicColorMatrix和ScriptIntrinsicBlur,還有另一個問題(不一定是bug)。雖然模糊內在有一個setInput函數,但colorMatrix確實有一個setInput函數。所以你也不能使用-1-作爲解決方法。

-4- 我認爲的renderScript正確的修復將是普遍地貶低 intrinsic.setInput只是因爲它是爲ScriptIntrinsicColorMatrix完成並獲得ScriptGroup.setInput而在腳本中使用組工作的內部函數。

-5- 我沒有看到使用scriptgroup的任何問題,當我有我自己的內核。換句話說,scriptGroup.setInput()和scriptGroup.setOutput()工作得很好

+0

我試過了,這沒有什麼區別。我的意思是將兩個rs內聯添加到腳本組中,然後在圖像視圖中不顯示輸出。我在這裏上傳了我的測試項目:https://github.com/laoyang/rstest,如果你想看看。謝謝! –

+0

細化我的分析。往上看。 –

+0

謝謝!你是對的,我改變了順序,讓模糊成爲第一個,並設定了它的輸入。腳本組正在工作。所以我認爲渲染腳本仍然不成熟... –