}
}
+ 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);
+ }
+ }
+
public synchronized Bitmap renderFinalImage(Bitmap bitmap, ImagePreset preset) {
synchronized (CachingPipeline.class) {
if (getRenderScriptContext() == null) {
private int mLastInputWidth = 0;
private int mLastInputHeight = 0;
+ public static boolean PERF_LOGGING = false;
+
private static ScriptC_grey mGreyConvert = null;
private static RenderScript mRScache = null;
protected abstract void createFilter(android.content.res.Resources res,
float scaleFactor, int quality);
+ protected void createFilter(android.content.res.Resources res,
+ float scaleFactor, int quality, Allocation in) {}
+ protected void bindScriptValues(Allocation in) {}
+
protected abstract void runFilter();
protected void update(Bitmap bitmap) {
}
@Override
+ public void apply(Allocation in, Allocation out) {
+ long startOverAll = System.nanoTime();
+ long startFilter = 0;
+ long endFilter = 0;
+ if (!mResourcesLoaded) {
+ CachingPipeline pipeline = getEnvironment().getCachingPipeline();
+ createFilter(pipeline.getResources(), getEnvironment().getScaleFactor(),
+ getEnvironment().getQuality(), in);
+ mResourcesLoaded = true;
+ }
+ startFilter = System.nanoTime();
+ bindScriptValues(in);
+ run(in, out);
+ if (PERF_LOGGING) {
+ getRenderScriptContext().finish();
+ endFilter = System.nanoTime();
+ long endOverAll = System.nanoTime();
+ String msg = String.format("%s; image size %dx%d; ", getName(),
+ in.getType().getX(), in.getType().getY());
+ long timeOverAll = (endOverAll - startOverAll) / 1000;
+ long timeFilter = (endFilter - startFilter) / 1000;
+ msg += String.format("over all %.2f ms (%.2f FPS); ",
+ timeOverAll / 1000.f, 1000000.f / timeOverAll);
+ msg += String.format("run filter %.2f ms (%.2f FPS)",
+ timeFilter / 1000.f, 1000000.f / timeFilter);
+ Log.i(LOGTAG, msg);
+ }
+ }
+
+ protected void run(Allocation in, Allocation out) {}
+
+ @Override
public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) {
if (bitmap == null || bitmap.getWidth() == 0 || bitmap.getHeight() == 0) {
return bitmap;
displayLowMemoryToast();
Log.e(LOGTAG, "not enough memory for filter " + getName(), e);
}
+
return bitmap;
}
return ret;
}
+ public Allocation loadScaledResourceAlpha(int resource, int w, int h, int inSampleSize) {
+ Resources res = CachingPipeline.getResources();
+ final BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inPreferredConfig = Bitmap.Config.ALPHA_8;
+ options.inSampleSize = inSampleSize;
+ Bitmap bitmap = BitmapFactory.decodeResource(
+ res,
+ resource, options);
+ Bitmap resizeBitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
+ Allocation ret = convertRGBAtoA(resizeBitmap);
+ resizeBitmap.recycle();
+ bitmap.recycle();
+ return ret;
+ }
+
public Allocation loadResourceAlpha(int resource) {
return loadScaledResourceAlpha(resource, 1);
}
package com.android.gallery3d.filtershow.presets;
import android.graphics.Bitmap;
+import android.support.v8.renderscript.Allocation;
import com.android.gallery3d.filtershow.cache.CachingPipeline;
import com.android.gallery3d.filtershow.filters.FilterRepresentation;
import com.android.gallery3d.filtershow.filters.FiltersManager;
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.setEnvironment(null);
+ }
+
public Bitmap applyRepresentation(FilterRepresentation representation, Bitmap bitmap) {
ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
filter.useRepresentation(representation);
import android.graphics.Bitmap;
import android.graphics.Rect;
+import android.support.v8.renderscript.Allocation;
import android.util.Log;
import com.android.gallery3d.filtershow.ImageStateAdapter;
return bitmap;
}
+ public void applyFilters(int from, int to, Allocation in, Allocation out, FilterEnvironment environment) {
+ if (mDoApplyFilters) {
+ if (from < 0) {
+ from = 0;
+ }
+ if (to == -1) {
+ to = mFilters.size();
+ }
+ for (int i = from; i < to; i++) {
+ FilterRepresentation representation = null;
+ synchronized (mFilters) {
+ representation = mFilters.elementAt(i);
+ representation.synchronizeRepresentation();
+ }
+ if (i > from) {
+ in.copyFrom(out);
+ }
+ environment.applyRepresentation(representation, in, out);
+ }
+ }
+ }
+
public boolean canDoPartialRendering() {
if (mGeoData.hasModifications()) {
return false;