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.renderscript.Allocation;
23 import com.android.gallery3d.app.Log;
24 import com.android.gallery3d.filtershow.cache.BitmapCache;
25 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
26 import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation;
27 import com.android.gallery3d.filtershow.filters.FiltersManagerInterface;
28 import com.android.gallery3d.filtershow.filters.ImageFilter;
30 import java.lang.ref.WeakReference;
31 import java.util.HashMap;
33 public class FilterEnvironment {
34 private static final String LOGTAG = "FilterEnvironment";
35 private ImagePreset mImagePreset;
36 private float mScaleFactor;
38 private FiltersManagerInterface mFiltersManager;
39 private PipelineInterface mPipeline;
40 private volatile boolean mStop = false;
41 private BitmapCache mBitmapCache;
43 public static final int QUALITY_ICON = 0;
44 public static final int QUALITY_PREVIEW = 1;
45 public static final int QUALITY_FINAL = 2;
47 public synchronized boolean needsStop() {
51 public synchronized void setStop(boolean stop) {
55 private HashMap<Integer, Integer>
56 generalParameters = new HashMap<Integer, Integer>();
58 public void setBitmapCache(BitmapCache cache) {
62 public void cache(Buffer buffer) {
63 mBitmapCache.cache(buffer);
66 public void cache(Bitmap bitmap) {
67 mBitmapCache.cache(bitmap);
70 public Bitmap getBitmap(int w, int h, int type) {
71 return mBitmapCache.getBitmap(w, h, type);
74 public Bitmap getBitmapCopy(Bitmap source, int type) {
75 return mBitmapCache.getBitmapCopy(source, type);
78 public void setImagePreset(ImagePreset imagePreset) {
79 mImagePreset = imagePreset;
82 public ImagePreset getImagePreset() {
86 public void setScaleFactor(float scaleFactor) {
87 mScaleFactor = scaleFactor;
90 public float getScaleFactor() {
94 public void setQuality(int quality) {
98 public int getQuality() {
102 public void setFiltersManager(FiltersManagerInterface filtersManager) {
103 mFiltersManager = filtersManager;
106 public FiltersManagerInterface getFiltersManager() {
107 return mFiltersManager;
110 public void applyRepresentation(FilterRepresentation representation,
111 Allocation in, Allocation out) {
112 ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
113 filter.useRepresentation(representation);
114 filter.setEnvironment(this);
115 if (filter.supportsAllocationInput()) {
116 filter.apply(in, out);
118 filter.setGeneralParameters();
119 filter.setEnvironment(null);
122 public Bitmap applyRepresentation(FilterRepresentation representation, Bitmap bitmap) {
123 if (representation instanceof FilterUserPresetRepresentation) {
124 // we allow instances of FilterUserPresetRepresentation in a preset only to know if one
125 // has been applied (so we can show this in the UI). But as all the filters in them are
126 // applied directly they do not themselves need to do any kind of filtering.
129 ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
131 Log.e(LOGTAG,"No ImageFilter for "+representation.getSerializationName());
133 filter.useRepresentation(representation);
134 filter.setEnvironment(this);
135 Bitmap ret = filter.apply(bitmap, mScaleFactor, mQuality);
139 filter.setGeneralParameters();
140 filter.setEnvironment(null);
144 public PipelineInterface getPipeline() {
148 public void setPipeline(PipelineInterface cachingPipeline) {
149 mPipeline = cachingPipeline;
152 public synchronized void clearGeneralParameters() {
153 generalParameters = null;
156 public synchronized Integer getGeneralParameter(int id) {
157 if (generalParameters == null || !generalParameters.containsKey(id)) {
160 return generalParameters.get(id);
163 public synchronized void setGeneralParameter(int id, int value) {
164 if (generalParameters == null) {
165 generalParameters = new HashMap<Integer, Integer>();
168 generalParameters.put(id, value);
171 public BitmapCache getBimapCache() {