OSDN Git Service

Make Gallery2 use platform RenderScript
[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.renderscript.Allocation;
22
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;
29
30 import java.lang.ref.WeakReference;
31 import java.util.HashMap;
32
33 public class FilterEnvironment {
34     private static final String LOGTAG = "FilterEnvironment";
35     private ImagePreset mImagePreset;
36     private float mScaleFactor;
37     private int mQuality;
38     private FiltersManagerInterface mFiltersManager;
39     private PipelineInterface mPipeline;
40     private volatile boolean mStop = false;
41     private BitmapCache mBitmapCache;
42
43     public static final int QUALITY_ICON = 0;
44     public static final int QUALITY_PREVIEW = 1;
45     public static final int QUALITY_FINAL = 2;
46
47     public synchronized boolean needsStop() {
48         return mStop;
49     }
50
51     public synchronized void setStop(boolean stop) {
52         this.mStop = stop;
53     }
54
55     private HashMap<Integer, Integer>
56                     generalParameters = new HashMap<Integer, Integer>();
57
58     public void setBitmapCache(BitmapCache cache) {
59         mBitmapCache = cache;
60     }
61
62     public void cache(Buffer buffer) {
63         mBitmapCache.cache(buffer);
64     }
65
66     public void cache(Bitmap bitmap) {
67         mBitmapCache.cache(bitmap);
68     }
69
70     public Bitmap getBitmap(int w, int h, int type) {
71         return mBitmapCache.getBitmap(w, h, type);
72     }
73
74     public Bitmap getBitmapCopy(Bitmap source, int type) {
75         return mBitmapCache.getBitmapCopy(source, type);
76     }
77
78     public void setImagePreset(ImagePreset imagePreset) {
79         mImagePreset = imagePreset;
80     }
81
82     public ImagePreset getImagePreset() {
83         return mImagePreset;
84     }
85
86     public void setScaleFactor(float scaleFactor) {
87         mScaleFactor = scaleFactor;
88     }
89
90     public float getScaleFactor() {
91         return mScaleFactor;
92     }
93
94     public void setQuality(int quality) {
95         mQuality = quality;
96     }
97
98     public int getQuality() {
99         return mQuality;
100     }
101
102     public void setFiltersManager(FiltersManagerInterface filtersManager) {
103         mFiltersManager = filtersManager;
104     }
105
106     public FiltersManagerInterface getFiltersManager() {
107         return mFiltersManager;
108     }
109
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);
117         }
118         filter.setGeneralParameters();
119         filter.setEnvironment(null);
120     }
121
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.
127             return bitmap;
128         }
129         ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
130         if (filter == null){
131             Log.e(LOGTAG,"No ImageFilter for "+representation.getSerializationName());
132         }
133         filter.useRepresentation(representation);
134         filter.setEnvironment(this);
135         Bitmap ret = filter.apply(bitmap, mScaleFactor, mQuality);
136         if (bitmap != ret) {
137             cache(bitmap);
138         }
139         filter.setGeneralParameters();
140         filter.setEnvironment(null);
141         return ret;
142     }
143
144     public PipelineInterface getPipeline() {
145         return mPipeline;
146     }
147
148     public void setPipeline(PipelineInterface cachingPipeline) {
149         mPipeline = cachingPipeline;
150     }
151
152     public synchronized void clearGeneralParameters() {
153         generalParameters = null;
154     }
155
156     public synchronized Integer getGeneralParameter(int id) {
157         if (generalParameters == null || !generalParameters.containsKey(id)) {
158             return null;
159         }
160         return generalParameters.get(id);
161     }
162
163     public synchronized void setGeneralParameter(int id, int value) {
164         if (generalParameters == null) {
165             generalParameters = new HashMap<Integer, Integer>();
166         }
167
168         generalParameters.put(id, value);
169     }
170
171     public BitmapCache getBimapCache() {
172         return mBitmapCache;
173     }
174 }