@Override public final void destroy() { blurScript.destroy(); renderScript.destroy(); if (outAllocation != null) { outAllocation.destroy(); } }
/** * @param bitmap bitmap to blur * @param blurRadius blur radius (1..25) * @return blurred bitmap */ @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) @Override public final Bitmap blur(Bitmap bitmap, float blurRadius) { //Allocation will use the same backing array of pixels as bitmap if created with USAGE_SHARED flag Allocation inAllocation = Allocation.createFromBitmap(renderScript, bitmap); if (!canReuseAllocation(bitmap)) { if (outAllocation != null) { outAllocation.destroy(); } outAllocation = Allocation.createTyped(renderScript, inAllocation.getType()); lastBitmapWidth = bitmap.getWidth(); lastBitmapHeight = bitmap.getHeight(); } blurScript.setRadius(blurRadius); blurScript.setInput(inAllocation); //do not use inAllocation in forEach. it will cause visual artifacts on blurred Bitmap blurScript.forEach(outAllocation); outAllocation.copyTo(bitmap); inAllocation.destroy(); return bitmap; }
private void releaseBitmap() { if (mBlurInput != null) { mBlurInput.destroy(); mBlurInput = null; } if (mBlurOutput != null) { mBlurOutput.destroy(); mBlurOutput = null; } if (mBitmapToBlur != null) { mBitmapToBlur.recycle(); mBitmapToBlur = null; } if (mBlurredBitmap != null) { mBlurredBitmap.recycle(); mBlurredBitmap = null; } }
@Override public final void destroy() { blurScript.destroy(); renderScript.destroy(); if (outAllocation != null) { outAllocation.destroy(); } }
/** * @param bitmap bitmap to blur * @param blurRadius blur radius (1..25) * @return blurred bitmap */ @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1) @Override public final Bitmap blur(Bitmap bitmap, float blurRadius) { //Allocation will use the same backing array of pixels as bitmap if created with USAGE_SHARED flag Allocation inAllocation = Allocation.createFromBitmap(renderScript, bitmap); if (!canReuseAllocation(bitmap)) { if (outAllocation != null) { outAllocation.destroy(); } outAllocation = Allocation.createTyped(renderScript, inAllocation.getType()); lastBitmapWidth = bitmap.getWidth(); lastBitmapHeight = bitmap.getHeight(); } blurScript.setRadius(blurRadius); blurScript.setInput(inAllocation); //do not use inAllocation in forEach. it will cause visual artifacts on blurred Bitmap blurScript.forEach(outAllocation); outAllocation.copyTo(bitmap); inAllocation.destroy(); return bitmap; }
protected void allocFeatureMapNoPad() { Type.Builder outputType = new Type.Builder(renderScript, Element.F32(renderScript)); outputType.setZ(outputShape[0]); outputType.setY(outputShape[1] * outputShape[2]); outputType.setX(outputShape[3]); Allocation outAllocation = Allocation.createTyped(renderScript, outputType.create()); FeatureMap output = new FeatureMap(); output.setFeatureMap(outAllocation); output.setN(outputShape[0]); output.setH(outputShape[1]); output.setW(outputShape[2]); output.setC(outputShape[3]); output.setPad4(false); if(this.featureMapOutput!=null){ ((FeatureMap)featureMapOutput).getFeatureMap().destroy(); } this.featureMapOutput = output; }
protected void allocFeatureMap() { Type.Builder outputType = new Type.Builder(renderScript, Element.F32(renderScript)); outputType.setZ(outputShape[0]); outputType.setY(outputShape[1] * outputShape[2]); outputType.setX(getOutputChannelAligned()); Allocation outAllocation = Allocation.createTyped(renderScript, outputType.create()); FeatureMap output = new FeatureMap(); output.setFeatureMap(outAllocation); output.setN(outputShape[0]); output.setH(outputShape[1]); output.setW(outputShape[2]); output.setC(outputShape[3]); output.setPad4(true); if(this.featureMapOutput!=null){ ((FeatureMap)featureMapOutput).getFeatureMap().destroy(); } this.featureMapOutput = output; }
1.f, colAllocation, kernelAllocation, 0.f, outputFeatureMap); colAllocation.destroy();