2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.gallery3d.filtershow.pipeline;
19 import android.graphics.Bitmap;
20 import android.graphics.Canvas;
21 import android.support.v8.renderscript.Allocation;
23 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
24 import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation;
25 import com.android.gallery3d.filtershow.filters.FiltersManagerInterface;
26 import com.android.gallery3d.filtershow.filters.ImageFilter;
28 import java.lang.ref.WeakReference;
29 import java.util.HashMap;
31 public class FilterEnvironment {
32 private static final String LOGTAG = "FilterEnvironment";
33 private ImagePreset mImagePreset;
34 private float mScaleFactor;
36 private FiltersManagerInterface mFiltersManager;
37 private PipelineInterface mPipeline;
38 private volatile boolean mStop = false;
40 public static final int QUALITY_ICON = 0;
41 public static final int QUALITY_PREVIEW = 1;
42 public static final int QUALITY_FINAL = 2;
44 public synchronized boolean needsStop() {
48 public synchronized void setStop(boolean stop) {
52 private HashMap<Long, WeakReference<Bitmap>>
53 bitmapCach = new HashMap<Long, WeakReference<Bitmap>>();
55 private HashMap<Integer, Integer>
56 generalParameters = new HashMap<Integer, Integer>();
58 public void cache(Buffer buffer) {
62 Bitmap bitmap = buffer.getBitmap();
66 public void cache(Bitmap bitmap) {
70 Long key = calcKey(bitmap.getWidth(), bitmap.getHeight());
71 bitmapCach.put(key, new WeakReference<Bitmap>(bitmap));
74 public Bitmap getBitmap(int w, int h) {
75 Long key = calcKey(w, h);
76 WeakReference<Bitmap> ref = bitmapCach.remove(key);
82 || bitmap.getWidth() != w
83 || bitmap.getHeight() != h) {
84 bitmap = Bitmap.createBitmap(
85 w, h, Bitmap.Config.ARGB_8888);
90 public Bitmap getBitmapCopy(Bitmap source) {
91 Bitmap bitmap = getBitmap(source.getWidth(), source.getHeight());
92 Canvas canvas = new Canvas(bitmap);
93 canvas.drawBitmap(source, 0, 0, null);
97 private Long calcKey(long w, long h) {
101 public void setImagePreset(ImagePreset imagePreset) {
102 mImagePreset = imagePreset;
105 public ImagePreset getImagePreset() {
109 public void setScaleFactor(float scaleFactor) {
110 mScaleFactor = scaleFactor;
113 public float getScaleFactor() {
117 public void setQuality(int quality) {
121 public int getQuality() {
125 public void setFiltersManager(FiltersManagerInterface filtersManager) {
126 mFiltersManager = filtersManager;
129 public FiltersManagerInterface getFiltersManager() {
130 return mFiltersManager;
133 public void applyRepresentation(FilterRepresentation representation,
134 Allocation in, Allocation out) {
135 ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
136 filter.useRepresentation(representation);
137 filter.setEnvironment(this);
138 if (filter.supportsAllocationInput()) {
139 filter.apply(in, out);
141 filter.setGeneralParameters();
142 filter.setEnvironment(null);
145 public Bitmap applyRepresentation(FilterRepresentation representation, Bitmap bitmap) {
146 if (representation instanceof FilterUserPresetRepresentation) {
147 // we allow instances of FilterUserPresetRepresentation in a preset only to know if one
148 // has been applied (so we can show this in the UI). But as all the filters in them are
149 // applied directly they do not themselves need to do any kind of filtering.
152 ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
153 filter.useRepresentation(representation);
154 filter.setEnvironment(this);
155 Bitmap ret = filter.apply(bitmap, mScaleFactor, mQuality);
156 filter.setGeneralParameters();
157 filter.setEnvironment(null);
161 public PipelineInterface getPipeline() {
165 public void setPipeline(PipelineInterface cachingPipeline) {
166 mPipeline = cachingPipeline;
169 public synchronized void clearGeneralParameters() {
170 generalParameters = null;
173 public synchronized Integer getGeneralParameter(int id) {
174 if (generalParameters == null || !generalParameters.containsKey(id)) {
177 return generalParameters.get(id);
180 public synchronized void setGeneralParameter(int id, int value) {
181 if (generalParameters == null) {
182 generalParameters = new HashMap<Integer, Integer>();
185 generalParameters.put(id, value);