OSDN Git Service

6a130a3e36863782d483f907fd404a099d41ba76
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / presets / 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.presets;
18
19 import android.graphics.Bitmap;
20 import android.support.v8.renderscript.Allocation;
21
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;
26
27 import java.lang.ref.WeakReference;
28 import java.util.HashMap;
29
30 public class FilterEnvironment {
31     private static final String LOGTAG = "FilterEnvironment";
32     private ImagePreset mImagePreset;
33     private float mScaleFactor;
34     private int mQuality;
35     private FiltersManagerInterface mFiltersManager;
36     private PipelineInterface mPipeline;
37     private volatile boolean mStop = false;
38
39     public static final int QUALITY_ICON = 0;
40     public static final int QUALITY_PREVIEW = 1;
41     public static final int QUALITY_FINAL = 2;
42
43     public synchronized boolean needsStop() {
44         return mStop;
45     }
46
47     public synchronized void setStop(boolean stop) {
48         this.mStop = stop;
49     }
50
51     private HashMap<Long, WeakReference<Bitmap>>
52             bitmapCach = new HashMap<Long, WeakReference<Bitmap>>();
53
54     private HashMap<Integer, Integer>
55                     generalParameters = new HashMap<Integer, Integer>();
56
57     public void cache(Buffer buffer) {
58         if (buffer == null) {
59             return;
60         }
61         Bitmap bitmap = buffer.getBitmap();
62         if (bitmap == null) {
63             return;
64         }
65         Long key = calcKey(bitmap.getWidth(), bitmap.getHeight());
66         bitmapCach.put(key, new WeakReference<Bitmap>(bitmap));
67     }
68
69     public Bitmap getBitmap(int w, int h) {
70         Long key = calcKey(w, h);
71         WeakReference<Bitmap> ref = bitmapCach.remove(key);
72         Bitmap bitmap = null;
73         if (ref != null) {
74             bitmap = ref.get();
75         }
76         if (bitmap == null) {
77             bitmap = Bitmap.createBitmap(
78                     w, h, Bitmap.Config.ARGB_8888);
79         }
80         return bitmap;
81     }
82
83     private Long calcKey(long w, long h) {
84         return (w << 32) | (h << 32);
85     }
86
87     public void setImagePreset(ImagePreset imagePreset) {
88         mImagePreset = imagePreset;
89     }
90
91     public ImagePreset getImagePreset() {
92         return mImagePreset;
93     }
94
95     public void setScaleFactor(float scaleFactor) {
96         mScaleFactor = scaleFactor;
97     }
98
99     public float getScaleFactor() {
100         return mScaleFactor;
101     }
102
103     public void setQuality(int quality) {
104         mQuality = quality;
105     }
106
107     public int getQuality() {
108         return mQuality;
109     }
110
111     public void setFiltersManager(FiltersManagerInterface filtersManager) {
112         mFiltersManager = filtersManager;
113     }
114
115     public FiltersManagerInterface getFiltersManager() {
116         return mFiltersManager;
117     }
118
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);
126         }
127         filter.setGeneralParameters();
128         filter.setEnvironment(null);
129     }
130
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);
138         return ret;
139     }
140
141     public PipelineInterface getPipeline() {
142         return mPipeline;
143     }
144
145     public void setPipeline(PipelineInterface cachingPipeline) {
146         mPipeline = cachingPipeline;
147     }
148
149     public synchronized void clearGeneralParameters() {
150         generalParameters = null;
151     }
152
153     public synchronized Integer getGeneralParameter(int id) {
154         if (generalParameters == null || !generalParameters.containsKey(id)) {
155             return null;
156         }
157         return generalParameters.get(id);
158     }
159
160     public synchronized void setGeneralParameter(int id, int value) {
161         if (generalParameters == null) {
162             generalParameters = new HashMap<Integer, Integer>();
163         }
164
165         generalParameters.put(id, value);
166     }
167
168 }