OSDN Git Service

Merge remote-tracking branch 'origin/klp-dev' into klp-dev
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / pipeline / FilterEnvironment.java
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.gallery3d.filtershow.pipeline;
18
19 import android.graphics.Bitmap;
20 import android.graphics.Canvas;
21 import android.support.v8.renderscript.Allocation;
22
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;
27
28 import java.lang.ref.WeakReference;
29 import java.util.HashMap;
30
31 public class FilterEnvironment {
32     private static final String LOGTAG = "FilterEnvironment";
33     private ImagePreset mImagePreset;
34     private float mScaleFactor;
35     private int mQuality;
36     private FiltersManagerInterface mFiltersManager;
37     private PipelineInterface mPipeline;
38     private volatile boolean mStop = false;
39
40     public static final int QUALITY_ICON = 0;
41     public static final int QUALITY_PREVIEW = 1;
42     public static final int QUALITY_FINAL = 2;
43
44     public synchronized boolean needsStop() {
45         return mStop;
46     }
47
48     public synchronized void setStop(boolean stop) {
49         this.mStop = stop;
50     }
51
52     private HashMap<Long, WeakReference<Bitmap>>
53             bitmapCach = new HashMap<Long, WeakReference<Bitmap>>();
54
55     private HashMap<Integer, Integer>
56                     generalParameters = new HashMap<Integer, Integer>();
57
58     public void cache(Buffer buffer) {
59         if (buffer == null) {
60             return;
61         }
62         Bitmap bitmap = buffer.getBitmap();
63         cache(bitmap);
64     }
65
66     public void cache(Bitmap bitmap) {
67         if (bitmap == null) {
68             return;
69         }
70         Long key = calcKey(bitmap.getWidth(), bitmap.getHeight());
71         bitmapCach.put(key, new WeakReference<Bitmap>(bitmap));
72     }
73
74     public Bitmap getBitmap(int w, int h) {
75         Long key = calcKey(w, h);
76         WeakReference<Bitmap> ref = bitmapCach.remove(key);
77         Bitmap bitmap = null;
78         if (ref != null) {
79             bitmap = ref.get();
80         }
81         if (bitmap == null
82                 || bitmap.getWidth() != w
83                 || bitmap.getHeight() != h) {
84             bitmap = Bitmap.createBitmap(
85                     w, h, Bitmap.Config.ARGB_8888);
86         }
87         return bitmap;
88     }
89
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);
94         return bitmap;
95     }
96
97     private Long calcKey(long w, long h) {
98         return (w << 32) | h;
99     }
100
101     public void setImagePreset(ImagePreset imagePreset) {
102         mImagePreset = imagePreset;
103     }
104
105     public ImagePreset getImagePreset() {
106         return mImagePreset;
107     }
108
109     public void setScaleFactor(float scaleFactor) {
110         mScaleFactor = scaleFactor;
111     }
112
113     public float getScaleFactor() {
114         return mScaleFactor;
115     }
116
117     public void setQuality(int quality) {
118         mQuality = quality;
119     }
120
121     public int getQuality() {
122         return mQuality;
123     }
124
125     public void setFiltersManager(FiltersManagerInterface filtersManager) {
126         mFiltersManager = filtersManager;
127     }
128
129     public FiltersManagerInterface getFiltersManager() {
130         return mFiltersManager;
131     }
132
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);
140         }
141         filter.setGeneralParameters();
142         filter.setEnvironment(null);
143     }
144
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.
150             return bitmap;
151         }
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);
158         return ret;
159     }
160
161     public PipelineInterface getPipeline() {
162         return mPipeline;
163     }
164
165     public void setPipeline(PipelineInterface cachingPipeline) {
166         mPipeline = cachingPipeline;
167     }
168
169     public synchronized void clearGeneralParameters() {
170         generalParameters = null;
171     }
172
173     public synchronized Integer getGeneralParameter(int id) {
174         if (generalParameters == null || !generalParameters.containsKey(id)) {
175             return null;
176         }
177         return generalParameters.get(id);
178     }
179
180     public synchronized void setGeneralParameter(int id, int value) {
181         if (generalParameters == null) {
182             generalParameters = new HashMap<Integer, Integer>();
183         }
184
185         generalParameters.put(id, value);
186     }
187
188 }