Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 510884fc authored by Eric Rahm's avatar Eric Rahm Committed by Michael Bestas
Browse files

Remove RenderScript from Gallery2

RenderScript usage is now deprecated. This removes all usage in Gallery2
where currently it is essentially unused.

Fixes: 211520324
Test: m Gallery2
Change-Id: I6d9584ab5eeda0e22b0b7c9b816c91266a4003f6
parent 681cbae1
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -1884,10 +1884,6 @@ public class FilterShowActivity extends AbstractPermissionActivity implements On
                return;
            }

            if (null == CachingPipeline.getRenderScriptContext()) {
                Log.v(LOGTAG, "RenderScript context destroyed during load");
                return;
            }
            final View imageShow = findViewById(R.id.imageShow);
            imageShow.setVisibility(View.VISIBLE);

+0 −7
Original line number Diff line number Diff line
@@ -19,7 +19,6 @@ package com.android.gallery3d.filtershow.filters;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.renderscript.Allocation;
import android.widget.Toast;

import com.android.gallery3d.app.GalleryAppImpl;
@@ -88,12 +87,6 @@ public abstract class ImageFilter implements Cloneable {
        return mName;
    }

    public boolean supportsAllocationInput() { return false; }

    public void apply(Allocation in, Allocation out) {
        setGeneralParameters();
    }

    public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
        // do nothing here, subclasses will implement filtering here
        setGeneralParameters();
+1 −19
Original line number Diff line number Diff line
@@ -19,31 +19,20 @@ package com.android.gallery3d.filtershow.pipeline;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.renderscript.Allocation;
import android.renderscript.RenderScript;

import android.util.Log;
import com.android.gallery3d.filtershow.cache.BitmapCache;
import com.android.gallery3d.filtershow.imageshow.PrimaryImage;

public class Buffer {
    private static final String LOGTAG = "Buffer";
    private Bitmap mBitmap;
    private Allocation mAllocation;
    private boolean mUseAllocation = false;
    private ImagePreset mPreset;

    public Buffer(Bitmap bitmap) {
        RenderScript rs = CachingPipeline.getRenderScriptContext();
        if (bitmap != null) {
            BitmapCache cache = PrimaryImage.getImage().getBitmapCache();
            mBitmap = cache.getBitmapCopy(bitmap, BitmapCache.PREVIEW_CACHE);
        }
        if (mUseAllocation) {
            // TODO: recreate the allocation when the RS context changes
            mAllocation = Allocation.createFromBitmap(rs, mBitmap,
                    Allocation.MipmapControl.MIPMAP_NONE,
                    Allocation.USAGE_SHARED | Allocation.USAGE_SCRIPT);
        }
    }

    public boolean isSameSize(Bitmap bitmap) {
@@ -67,14 +56,7 @@ public class Buffer {
        return mBitmap;
    }

    public Allocation getAllocation() {
        return mAllocation;
    }

    public void sync() {
        if (mUseAllocation) {
            mAllocation.copyTo(mBitmap);
        }
    }

    public ImagePreset getPreset() {
+6 −156
Original line number Diff line number Diff line
@@ -24,8 +24,6 @@ import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.renderscript.Allocation;
import android.renderscript.RenderScript;
import android.util.Log;

import com.android.gallery3d.filtershow.cache.BitmapCache;
@@ -43,8 +41,6 @@ public class CachingPipeline implements PipelineInterface {

    private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;

    private static volatile RenderScript sRS = null;

    private FiltersManager mFiltersManager = null;
    private volatile Bitmap mOriginalBitmap = null;
    private volatile Bitmap mResizedOriginalBitmap = null;
@@ -52,12 +48,6 @@ public class CachingPipeline implements PipelineInterface {
    private FilterEnvironment mEnvironment = new FilterEnvironment();
    private CacheProcessing mCachedProcessing = new CacheProcessing();


    private volatile Allocation mOriginalAllocation = null;
    private volatile Allocation mFiltersOnlyOriginalAllocation =  null;

    protected volatile Allocation mInPixelsAllocation;
    protected volatile Allocation mOutPixelsAllocation;
    private volatile int mWidth = 0;
    private volatile int mHeight = 0;

@@ -70,73 +60,25 @@ public class CachingPipeline implements PipelineInterface {
        mName = name;
    }

    public static synchronized RenderScript getRenderScriptContext() {
        return sRS;
    }

    public static synchronized void createRenderscriptContext(Context context) {
        if (sRS != null) {
            Log.w(LOGTAG, "A prior RS context exists when calling setRenderScriptContext");
            destroyRenderScriptContext();
        }
        sRS = RenderScript.create(context);
    }

    public static synchronized void destroyRenderScriptContext() {
        if (sRS != null) {
            sRS.destroy();
        }
        sRS = null;
    }

    public void stop() {
        mEnvironment.setStop(true);
    }

    public synchronized void reset() {
        synchronized (CachingPipeline.class) {
            if (getRenderScriptContext() == null) {
                return;
            }
            mOriginalBitmap = null; // just a reference to the bitmap in ImageLoader
            if (mResizedOriginalBitmap != null) {
                mResizedOriginalBitmap.recycle();
                mResizedOriginalBitmap = null;
            }
            if (mOriginalAllocation != null) {
                mOriginalAllocation.destroy();
                mOriginalAllocation = null;
            }
            if (mFiltersOnlyOriginalAllocation != null) {
                mFiltersOnlyOriginalAllocation.destroy();
                mFiltersOnlyOriginalAllocation = null;
            }

            mPreviewScaleFactor = 1.0f;
            mHighResPreviewScaleFactor = 1.0f;

            destroyPixelAllocations();
        }
    }

    public Resources getResources() {
        return sRS.getApplicationContext().getResources();
    }

    private synchronized void destroyPixelAllocations() {
        if (DEBUG) {
            Log.v(LOGTAG, "destroyPixelAllocations in " + getName());
        }
        if (mInPixelsAllocation != null) {
            mInPixelsAllocation.destroy();
            mInPixelsAllocation = null;
        }
        if (mOutPixelsAllocation != null) {
            mOutPixelsAllocation.destroy();
            mOutPixelsAllocation = null;
        }
            mWidth = 0;
            mHeight = 0;
        }
    }

    private String getType(RenderingRequest request) {
        if (request.getType() == RenderingRequest.ICON_RENDERING) {
@@ -192,31 +134,13 @@ public class CachingPipeline implements PipelineInterface {
            return false;
        }

        RenderScript RS = getRenderScriptContext();

        Allocation filtersOnlyOriginalAllocation = mFiltersOnlyOriginalAllocation;
        mFiltersOnlyOriginalAllocation = Allocation.createFromBitmap(RS, originalBitmap,
                Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
        if (filtersOnlyOriginalAllocation != null) {
            filtersOnlyOriginalAllocation.destroy();
        }

        Allocation originalAllocation = mOriginalAllocation;
        mResizedOriginalBitmap = preset.applyGeometry(originalBitmap, mEnvironment);
        mOriginalAllocation = Allocation.createFromBitmap(RS, mResizedOriginalBitmap,
                Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
        if (originalAllocation != null) {
            originalAllocation.destroy();
        }

        return true;
    }

    public void renderHighres(RenderingRequest request) {
        synchronized (CachingPipeline.class) {
            if (getRenderScriptContext() == null) {
                return;
            }
            ImagePreset preset = request.getImagePreset();
            setupEnvironment(preset, false);
            Bitmap bitmap = PrimaryImage.getImage().getOriginalBitmapHighres();
@@ -239,9 +163,6 @@ public class CachingPipeline implements PipelineInterface {

    public void renderGeometry(RenderingRequest request) {
        synchronized (CachingPipeline.class) {
            if (getRenderScriptContext() == null) {
                return;
            }
            ImagePreset preset = request.getImagePreset();
            setupEnvironment(preset, false);
            Bitmap bitmap = PrimaryImage.getImage().getOriginalBitmapHighres();
@@ -261,9 +182,6 @@ public class CachingPipeline implements PipelineInterface {

    public void renderFilters(RenderingRequest request) {
        synchronized (CachingPipeline.class) {
            if (getRenderScriptContext() == null) {
                return;
            }
            ImagePreset preset = request.getImagePreset();
            setupEnvironment(preset, false);
            Bitmap bitmap = PrimaryImage.getImage().getOriginalBitmapHighres();
@@ -284,9 +202,6 @@ public class CachingPipeline implements PipelineInterface {
    public synchronized void render(RenderingRequest request) {
        // TODO: cleanup/remove GEOMETRY / FILTERS paths
        synchronized (CachingPipeline.class) {
            if (getRenderScriptContext() == null) {
                return;
            }
            if ((request.getType() != RenderingRequest.PARTIAL_RENDERING
                  && request.getType() != RenderingRequest.ICON_RENDERING
                    && request.getBitmap() == null)
@@ -330,9 +245,9 @@ public class CachingPipeline implements PipelineInterface {

            if (request.getType() == RenderingRequest.FULL_RENDERING
                    || request.getType() == RenderingRequest.GEOMETRY_RENDERING) {
                mOriginalAllocation.copyTo(bitmap);
                bitmap = mResizedOriginalBitmap;
            } else if (request.getType() == RenderingRequest.FILTERS_RENDERING) {
                mFiltersOnlyOriginalAllocation.copyTo(bitmap);
                bitmap = mOriginalBitmap;
            }

            if (request.getType() == RenderingRequest.FULL_RENDERING
@@ -379,27 +294,8 @@ public class CachingPipeline implements PipelineInterface {
        }
    }

    public synchronized void renderImage(ImagePreset preset, Allocation in, Allocation out) {
        synchronized (CachingPipeline.class) {
            if (getRenderScriptContext() == null) {
                return;
            }
            setupEnvironment(preset, false);
            mFiltersManager.freeFilterResources(preset);
            preset.applyFilters(-1, -1, in, out, mEnvironment);
            boolean copyOut = false;
            if (preset.nbFilters() > 0) {
                copyOut = true;
            }
            preset.applyBorder(in, out, copyOut, mEnvironment);
        }
    }

    public synchronized Bitmap renderFinalImage(Bitmap bitmap, ImagePreset preset) {
        synchronized (CachingPipeline.class) {
            if (getRenderScriptContext() == null) {
                return bitmap;
            }
            setupEnvironment(preset, false);
            mEnvironment.setQuality(FilterEnvironment.QUALITY_FINAL);
            mEnvironment.setScaleFactor(1.0f);
@@ -415,9 +311,6 @@ public class CachingPipeline implements PipelineInterface {
    }

    public void compute(SharedBuffer buffer, ImagePreset preset, int type) {
        if (getRenderScriptContext() == null) {
            return;
        }
        setupEnvironment(preset, false);
        Vector<FilterRepresentation> filters = preset.getFilters();
        Bitmap result = mCachedProcessing.process(mOriginalBitmap, filters, mEnvironment);
@@ -439,53 +332,10 @@ public class CachingPipeline implements PipelineInterface {
    }

    public synchronized boolean isInitialized() {
        return getRenderScriptContext() != null && mOriginalBitmap != null;
    }

    public boolean prepareRenderscriptAllocations(Bitmap bitmap) {
        RenderScript RS = getRenderScriptContext();
        boolean needsUpdate = false;
        if (mOutPixelsAllocation == null || mInPixelsAllocation == null ||
                bitmap.getWidth() != mWidth || bitmap.getHeight() != mHeight) {
            destroyPixelAllocations();
            Bitmap bitmapBuffer = bitmap;
            if (bitmap.getConfig() == null || bitmap.getConfig() != BITMAP_CONFIG) {
                bitmapBuffer = bitmap.copy(BITMAP_CONFIG, true);
            }
            mOutPixelsAllocation = Allocation.createFromBitmap(RS, bitmapBuffer,
                    Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
            mInPixelsAllocation = Allocation.createTyped(RS,
                    mOutPixelsAllocation.getType());
            needsUpdate = true;
        }
        if (RS != null) {
            mInPixelsAllocation.copyFrom(bitmap);
        }
        if (bitmap.getWidth() != mWidth
                || bitmap.getHeight() != mHeight) {
            mWidth = bitmap.getWidth();
            mHeight = bitmap.getHeight();
            needsUpdate = true;
        }
        if (DEBUG) {
            Log.v(LOGTAG, "prepareRenderscriptAllocations: " + needsUpdate + " in " + getName());
        }
        return needsUpdate;
    }

    public synchronized Allocation getInPixelsAllocation() {
        return mInPixelsAllocation;
    }

    public synchronized Allocation getOutPixelsAllocation() {
        return mOutPixelsAllocation;
        return mOriginalBitmap != null;
    }

    public String getName() {
        return mName;
    }

    public RenderScript getRSContext() {
        return CachingPipeline.getRenderScriptContext();
    }
}
+0 −13
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.gallery3d.filtershow.pipeline;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.renderscript.Allocation;

import com.android.gallery3d.app.Log;
import com.android.gallery3d.filtershow.cache.BitmapCache;
@@ -107,18 +106,6 @@ public class FilterEnvironment {
        return mFiltersManager;
    }

    public void applyRepresentation(FilterRepresentation representation,
                                    Allocation in, Allocation out) {
        ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
        filter.useRepresentation(representation);
        filter.setEnvironment(this);
        if (filter.supportsAllocationInput()) {
            filter.apply(in, out);
        }
        filter.setGeneralParameters();
        filter.setEnvironment(null);
    }

    public Bitmap applyRepresentation(FilterRepresentation representation, Bitmap bitmap) {
        if (representation instanceof FilterUserPresetRepresentation) {
            // we allow instances of FilterUserPresetRepresentation in a preset only to know if one
Loading