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.presets;
19 import android.graphics.Bitmap;
20 import android.support.v8.renderscript.Allocation;
22 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
23 import com.android.gallery3d.filtershow.filters.FiltersManagerInterface;
24 import com.android.gallery3d.filtershow.filters.ImageFilter;
25 import com.android.gallery3d.filtershow.pipeline.Buffer;
27 import java.lang.ref.WeakReference;
28 import java.util.HashMap;
30 public class FilterEnvironment {
31 private static final String LOGTAG = "FilterEnvironment";
32 private ImagePreset mImagePreset;
33 private float mScaleFactor;
35 private FiltersManagerInterface mFiltersManager;
36 private PipelineInterface mPipeline;
37 private volatile boolean mStop = false;
39 public static final int QUALITY_ICON = 0;
40 public static final int QUALITY_PREVIEW = 1;
41 public static final int QUALITY_FINAL = 2;
43 public synchronized boolean needsStop() {
47 public synchronized void setStop(boolean stop) {
51 private HashMap<Long, WeakReference<Bitmap>>
52 bitmapCach = new HashMap<Long, WeakReference<Bitmap>>();
54 private HashMap<Integer, Integer>
55 generalParameters = new HashMap<Integer, Integer>();
57 public void cache(Buffer buffer) {
61 Bitmap bitmap = buffer.getBitmap();
65 Long key = calcKey(bitmap.getWidth(), bitmap.getHeight());
66 bitmapCach.put(key, new WeakReference<Bitmap>(bitmap));
69 public Bitmap getBitmap(int w, int h) {
70 Long key = calcKey(w, h);
71 WeakReference<Bitmap> ref = bitmapCach.remove(key);
77 bitmap = Bitmap.createBitmap(
78 w, h, Bitmap.Config.ARGB_8888);
83 private Long calcKey(long w, long h) {
84 return (w << 32) | (h << 32);
87 public void setImagePreset(ImagePreset imagePreset) {
88 mImagePreset = imagePreset;
91 public ImagePreset getImagePreset() {
95 public void setScaleFactor(float scaleFactor) {
96 mScaleFactor = scaleFactor;
99 public float getScaleFactor() {
103 public void setQuality(int quality) {
107 public int getQuality() {
111 public void setFiltersManager(FiltersManagerInterface filtersManager) {
112 mFiltersManager = filtersManager;
115 public FiltersManagerInterface getFiltersManager() {
116 return mFiltersManager;
119 public void applyRepresentation(FilterRepresentation representation,
120 Allocation in, Allocation out) {
121 ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
122 filter.useRepresentation(representation);
123 filter.setEnvironment(this);
124 if (filter.supportsAllocationInput()) {
125 filter.apply(in, out);
127 filter.setGeneralParameters();
128 filter.setEnvironment(null);
131 public Bitmap applyRepresentation(FilterRepresentation representation, Bitmap bitmap) {
132 ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
133 filter.useRepresentation(representation);
134 filter.setEnvironment(this);
135 Bitmap ret = filter.apply(bitmap, mScaleFactor, mQuality);
136 filter.setGeneralParameters();
137 filter.setEnvironment(null);
141 public PipelineInterface getPipeline() {
145 public void setPipeline(PipelineInterface cachingPipeline) {
146 mPipeline = cachingPipeline;
149 public synchronized void clearGeneralParameters() {
150 generalParameters = null;
153 public synchronized Integer getGeneralParameter(int id) {
154 if (generalParameters == null || !generalParameters.containsKey(id)) {
157 return generalParameters.get(id);
160 public synchronized void setGeneralParameter(int id, int value) {
161 if (generalParameters == null) {
162 generalParameters = new HashMap<Integer, Integer>();
165 generalParameters.put(id, value);