Java Code Examples for android.support.v8.renderscript.RenderScript#destroy()

The following examples show how to use android.support.v8.renderscript.RenderScript#destroy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RenderscriptBlurringMachine.java    From fogger with Apache License 2.0 6 votes vote down vote up
@Override
protected Bitmap blur(Context context, Bitmap bitmapToBlur, int radius) {
    Log.i(TAG, "Current build version sdk " + Build.VERSION.SDK_INT);
    Bitmap bitmap = bitmapToBlur.copy(bitmapToBlur.getConfig(), true);

    final RenderScript renderScript = RenderScript.create(context, Build.VERSION.SDK_INT);
    final Allocation input = Allocation.createFromBitmap(renderScript, bitmapToBlur,
                                                            Allocation.MipmapControl.MIPMAP_NONE,
                                                            Allocation.USAGE_SCRIPT);
    final Allocation output = Allocation.createTyped(renderScript, input.getType());
    try {
        final ScriptIntrinsicBlur script = createBlurringScript(radius, renderScript, input);
        script.forEach(output);
        renderScript.finish();
        output.copyTo(bitmap);
    } finally {
        input.destroy();
        output.destroy();
        bitmapToBlur.recycle();
        renderScript.destroy();
    }
    return bitmap;
}
 
Example 2
Source File: RenderScriptBlur.java    From EtsyBlur with Apache License 2.0 6 votes vote down vote up
public synchronized static boolean isAvailable(@NonNull Context context) {
    if (!isAvailabilityChecked) {
        boolean available = true;
        RenderScript rs = null;
        try {
            rs = RenderScript.create(context);
        } catch (RSRuntimeException e) {
            Log.w(TAG, "Renderscript is not available on this device.");
            available = false;
        } finally {
            if (rs != null) {
                rs.destroy();
            }
            isAvailabilityChecked = true;
            isAvailable = available;
        }
    }
    return isAvailable;
}
 
Example 3
Source File: BlurTransformation.java    From Dota2Helper with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();

    int width = source.getWidth();
    int height = source.getHeight();
    int scaledWidth = width / mSampling;
    int scaledHeight = height / mSampling;

    Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(source, 0, 0, paint);

    RenderScript rs = RenderScript.create(mContext);
    Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
            Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, input.getType());
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    blur.setInput(input);
    blur.setRadius(mRadius);
    blur.forEach(output);
    output.copyTo(bitmap);

    rs.destroy();

    return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 4
Source File: BlurBuilder.java    From talk-android with MIT License 5 votes vote down vote up
public static void RsBlur(RenderScript rs, Bitmap original) {
    // use this constructor for best performance, because it uses USAGE_SHARED mode which
    // reuses memory
    final Allocation input = Allocation.createFromBitmap(rs, original);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(8f);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(original);
    rs.destroy();
}
 
Example 5
Source File: BlurBuilder.java    From talk-android with MIT License 5 votes vote down vote up
public static void RsBlur(RenderScript rs, Bitmap original, float radius) {
    // use this constructor for best performance, because it uses USAGE_SHARED mode which
    // reuses memory
    final Allocation input = Allocation.createFromBitmap(rs, original);
    final Allocation output = Allocation.createTyped(rs, input.getType());
    final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setRadius(radius);
    script.setInput(input);
    script.forEach(output);
    output.copyTo(original);
    rs.destroy();
}
 
Example 6
Source File: Blur.java    From AllAngleExpandableButton with Apache License 2.0 5 votes vote down vote up
@WorkerThread
private Bitmap getBlurBitmap(Context context, Bitmap inBitmap, float radius) {
    if (context == null || inBitmap == null) {
        throw new IllegalArgumentException("have not called setParams() before call execute()");
    }

    int width = Math.round(inBitmap.getWidth() * SCALE);
    int height = Math.round(inBitmap.getHeight() * SCALE);

    Bitmap in = Bitmap.createScaledBitmap(inBitmap, width, height, false);
    Bitmap out = Bitmap.createBitmap(in);

    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    Allocation allocationIn = Allocation.createFromBitmap(rs, in);
    Allocation allocationOut = Allocation.createFromBitmap(rs, out);

    blurScript.setRadius(radius);
    blurScript.setInput(allocationIn);
    blurScript.forEach(allocationOut);
    allocationOut.copyTo(out);

    allocationIn.destroy();
    allocationOut.destroy();
    blurScript.destroy();
    rs.destroy();

    return out;
}
 
Example 7
Source File: BitmapUtils.java    From Rocko-Android-Demos with Apache License 2.0 5 votes vote down vote up
public static Bitmap blurBitmap(Context applicationContext, Bitmap bitmap, float radius) {

        //Let's create an empty bitmap with the same size of the bitmap we want to blur
        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

        //Instantiate a new Renderscript
        RenderScript rs = RenderScript.create(applicationContext);

        //Create an Intrinsic Blur Script using the Renderscript
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

        //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
        Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
        //Set the radius of the blur
        blurScript.setRadius(radius);
        //Perform the Renderscript
        blurScript.setInput(allIn);
        blurScript.forEach(allOut);

        //Copy the final bitmap created by the out Allocation to the outBitmap
        allOut.copyTo(outBitmap);

        //recycle the original bitmap
        bitmap.recycle();

        //After finishing everything, we destroy the Renderscript.
        rs.destroy();

        return outBitmap;
    }
 
Example 8
Source File: BlurTransformation.java    From GracefulMovies with Apache License 2.0 4 votes vote down vote up
/**
     * Transforms the given {@link Bitmap} based on the given dimensions and returns the transformed
     * result.
     * <p/>
     * <p>
     * The provided Bitmap, toTransform, should not be recycled or returned to the pool. Glide will automatically
     * recycle and/or reuse toTransform if the transformation returns a different Bitmap. Similarly implementations
     * should never recycle or return Bitmaps that are returned as the result of this method. Recycling or returning
     * the provided and/or the returned Bitmap to the pool will lead to a variety of runtime exceptions and drawing
     * errors. See #408 for an example. If the implementation obtains and discards intermediate Bitmaps, they may
     * safely be returned to the BitmapPool and/or recycled.
     * </p>
     * <p/>
     * <p>
     * outWidth and outHeight will never be {Target.SIZE_ORIGINAL}, this
     * class converts them to be the size of the Bitmap we're going to transform before calling this method.
     * </p>
     *
     * @param pool        A {@link BitmapPool} that can be used to obtain and
     *                    return intermediate {@link Bitmap}s used in this transformation. For every
     *                    {@link Bitmap} obtained from the pool during this transformation, a
     *                    {@link Bitmap} must also be returned.
     * @param toTransform The {@link Bitmap} to transform.
     * @param outWidth    The ideal width of the transformed bitmap (the transformed width does not need to match exactly).
     * @param outHeight   The ideal height of the transformed bitmap (the transformed heightdoes not need to match
     */
    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        boolean needScaled = mSampling == DEFAULT_SAMPLING;
        int originWidth = toTransform.getWidth();
        int originHeight = toTransform.getHeight();
        int width, height;
        if (needScaled) {
            width = originWidth;
            height = originHeight;
        } else {
            width = (int) (originWidth / mSampling);
            height = (int) (originHeight / mSampling);
        }
        //find a re-use bitmap
        Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
        if (bitmap == null) {
            bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        if (mSampling != DEFAULT_SAMPLING) {
            canvas.scale(1 / mSampling, 1 / mSampling);
        }
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
        PorterDuffColorFilter filter =
                new PorterDuffColorFilter(mColor, PorterDuff.Mode.SRC_ATOP);
        paint.setColorFilter(filter);
        canvas.drawBitmap(toTransform, 0, 0, paint);
// TIPS: Glide will take care of returning our original Bitmap to the BitmapPool for us,
// we needn't to recycle it. 
//        toTransform.recycle();  <--- Just for tips. by Ligboy

        RenderScript rs = RenderScript.create(mContext);
        Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT);
        Allocation output = Allocation.createTyped(rs, input.getType());
        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

        blur.setInput(input);
        blur.setRadius(mRadius);
        blur.forEach(output);
        output.copyTo(bitmap);

        rs.destroy();

        if (needScaled) {
            return bitmap;
        } else {
            Bitmap scaled = Bitmap.createScaledBitmap(bitmap, originWidth, originHeight, true);
            bitmap.recycle();
            return scaled;
        }
    }