OSDN Git Service

0ff8bfbadc340685eb5698d96c25e6875065ba68
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / pipeline / CachingPipeline.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.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.Bitmap;
22 import android.support.v8.renderscript.Allocation;
23 import android.support.v8.renderscript.RenderScript;
24 import android.util.Log;
25
26 import com.android.gallery3d.filtershow.cache.ImageLoader;
27 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
28 import com.android.gallery3d.filtershow.filters.FiltersManager;
29 import com.android.gallery3d.filtershow.imageshow.GeometryMathUtils;
30 import com.android.gallery3d.filtershow.imageshow.MasterImage;
31
32 import java.util.Vector;
33
34 public class CachingPipeline implements PipelineInterface {
35     private static final String LOGTAG = "CachingPipeline";
36     private boolean DEBUG = false;
37
38     private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
39
40     private static volatile RenderScript sRS = null;
41
42     private FiltersManager mFiltersManager = null;
43     private volatile Bitmap mOriginalBitmap = null;
44     private volatile Bitmap mResizedOriginalBitmap = null;
45
46     private FilterEnvironment mEnvironment = new FilterEnvironment();
47     private CacheProcessing mCachedProcessing = new CacheProcessing();
48
49
50     private volatile Allocation mOriginalAllocation = null;
51     private volatile Allocation mFiltersOnlyOriginalAllocation =  null;
52
53     protected volatile Allocation mInPixelsAllocation;
54     protected volatile Allocation mOutPixelsAllocation;
55     private volatile int mWidth = 0;
56     private volatile int mHeight = 0;
57
58     private volatile float mPreviewScaleFactor = 1.0f;
59     private volatile float mHighResPreviewScaleFactor = 1.0f;
60     private volatile String mName = "";
61
62     public CachingPipeline(FiltersManager filtersManager, String name) {
63         mFiltersManager = filtersManager;
64         mName = name;
65     }
66
67     public static synchronized RenderScript getRenderScriptContext() {
68         return sRS;
69     }
70
71     public static synchronized void createRenderscriptContext(Context context) {
72         if (sRS != null) {
73             Log.w(LOGTAG, "A prior RS context exists when calling setRenderScriptContext");
74             destroyRenderScriptContext();
75         }
76         sRS = RenderScript.create(context);
77     }
78
79     public static synchronized void destroyRenderScriptContext() {
80         if (sRS != null) {
81             sRS.destroy();
82         }
83         sRS = null;
84     }
85
86     public void stop() {
87         mEnvironment.setStop(true);
88     }
89
90     public synchronized void reset() {
91         synchronized (CachingPipeline.class) {
92             if (getRenderScriptContext() == null) {
93                 return;
94             }
95             mOriginalBitmap = null; // just a reference to the bitmap in ImageLoader
96             if (mResizedOriginalBitmap != null) {
97                 mResizedOriginalBitmap.recycle();
98                 mResizedOriginalBitmap = null;
99             }
100             if (mOriginalAllocation != null) {
101                 mOriginalAllocation.destroy();
102                 mOriginalAllocation = null;
103             }
104             if (mFiltersOnlyOriginalAllocation != null) {
105                 mFiltersOnlyOriginalAllocation.destroy();
106                 mFiltersOnlyOriginalAllocation = null;
107             }
108             mPreviewScaleFactor = 1.0f;
109             mHighResPreviewScaleFactor = 1.0f;
110
111             destroyPixelAllocations();
112         }
113     }
114
115     public Resources getResources() {
116         return sRS.getApplicationContext().getResources();
117     }
118
119     private synchronized void destroyPixelAllocations() {
120         if (DEBUG) {
121             Log.v(LOGTAG, "destroyPixelAllocations in " + getName());
122         }
123         if (mInPixelsAllocation != null) {
124             mInPixelsAllocation.destroy();
125             mInPixelsAllocation = null;
126         }
127         if (mOutPixelsAllocation != null) {
128             mOutPixelsAllocation.destroy();
129             mOutPixelsAllocation = null;
130         }
131         mWidth = 0;
132         mHeight = 0;
133     }
134
135     private String getType(RenderingRequest request) {
136         if (request.getType() == RenderingRequest.ICON_RENDERING) {
137             return "ICON_RENDERING";
138         }
139         if (request.getType() == RenderingRequest.FILTERS_RENDERING) {
140             return "FILTERS_RENDERING";
141         }
142         if (request.getType() == RenderingRequest.FULL_RENDERING) {
143             return "FULL_RENDERING";
144         }
145         if (request.getType() == RenderingRequest.GEOMETRY_RENDERING) {
146             return "GEOMETRY_RENDERING";
147         }
148         if (request.getType() == RenderingRequest.PARTIAL_RENDERING) {
149             return "PARTIAL_RENDERING";
150         }
151         if (request.getType() == RenderingRequest.HIGHRES_RENDERING) {
152             return "HIGHRES_RENDERING";
153         }
154         return "UNKNOWN TYPE!";
155     }
156
157     private void setupEnvironment(ImagePreset preset, boolean highResPreview) {
158         mEnvironment.setPipeline(this);
159         mEnvironment.setFiltersManager(mFiltersManager);
160         mEnvironment.setBitmapCache(MasterImage.getImage().getBitmapCache());
161         if (highResPreview) {
162             mEnvironment.setScaleFactor(mHighResPreviewScaleFactor);
163         } else {
164             mEnvironment.setScaleFactor(mPreviewScaleFactor);
165         }
166         mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
167         mEnvironment.setImagePreset(preset);
168         mEnvironment.setStop(false);
169     }
170
171     public void setOriginal(Bitmap bitmap) {
172         mOriginalBitmap = bitmap;
173         Log.v(LOGTAG,"setOriginal, size " + bitmap.getWidth() + " x " + bitmap.getHeight());
174         ImagePreset preset = MasterImage.getImage().getPreset();
175         setupEnvironment(preset, false);
176         updateOriginalAllocation(preset);
177     }
178
179     private synchronized boolean updateOriginalAllocation(ImagePreset preset) {
180         Bitmap originalBitmap = mOriginalBitmap;
181
182         if (originalBitmap == null) {
183             return false;
184         }
185
186         RenderScript RS = getRenderScriptContext();
187
188         Allocation filtersOnlyOriginalAllocation = mFiltersOnlyOriginalAllocation;
189         mFiltersOnlyOriginalAllocation = Allocation.createFromBitmap(RS, originalBitmap,
190                 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
191         if (filtersOnlyOriginalAllocation != null) {
192             filtersOnlyOriginalAllocation.destroy();
193         }
194
195         Allocation originalAllocation = mOriginalAllocation;
196         mResizedOriginalBitmap = preset.applyGeometry(originalBitmap, mEnvironment);
197         mOriginalAllocation = Allocation.createFromBitmap(RS, mResizedOriginalBitmap,
198                 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
199         if (originalAllocation != null) {
200             originalAllocation.destroy();
201         }
202
203         return true;
204     }
205
206     public void renderHighres(RenderingRequest request) {
207         synchronized (CachingPipeline.class) {
208             if (getRenderScriptContext() == null) {
209                 return;
210             }
211             ImagePreset preset = request.getImagePreset();
212             setupEnvironment(preset, false);
213             Bitmap bitmap = MasterImage.getImage().getOriginalBitmapHighres();
214             if (bitmap == null) {
215                 return;
216             }
217             bitmap = mEnvironment.getBitmapCopy(bitmap);
218             bitmap = preset.applyGeometry(bitmap, mEnvironment);
219
220             mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
221             Bitmap bmp = preset.apply(bitmap, mEnvironment);
222             if (!mEnvironment.needsStop()) {
223                 request.setBitmap(bmp);
224             }
225             mFiltersManager.freeFilterResources(preset);
226         }
227     }
228
229     public synchronized void render(RenderingRequest request) {
230         synchronized (CachingPipeline.class) {
231             if (getRenderScriptContext() == null) {
232                 return;
233             }
234             if (((request.getType() != RenderingRequest.PARTIAL_RENDERING
235                     && request.getType() != RenderingRequest.HIGHRES_RENDERING)
236                     && request.getBitmap() == null)
237                     || request.getImagePreset() == null) {
238                 return;
239             }
240
241             if (DEBUG) {
242                 Log.v(LOGTAG, "render image of type " + getType(request));
243             }
244
245             Bitmap bitmap = request.getBitmap();
246             ImagePreset preset = request.getImagePreset();
247             setupEnvironment(preset,
248                     request.getType() != RenderingRequest.HIGHRES_RENDERING);
249             mFiltersManager.freeFilterResources(preset);
250
251             if (request.getType() == RenderingRequest.PARTIAL_RENDERING) {
252                 MasterImage master = MasterImage.getImage();
253                 bitmap = ImageLoader.getScaleOneImageForPreset(master.getActivity(),
254                         mEnvironment,
255                         master.getUri(), request.getBounds(),
256                         request.getDestination());
257                 if (bitmap == null) {
258                     Log.w(LOGTAG, "could not get bitmap for: " + getType(request));
259                     return;
260                 }
261             }
262
263             if (request.getType() == RenderingRequest.HIGHRES_RENDERING) {
264                 bitmap = MasterImage.getImage().getOriginalBitmapHighres();
265                 if (bitmap != null) {
266                     bitmap = preset.applyGeometry(bitmap, mEnvironment);
267                 }
268             }
269
270             if (request.getType() == RenderingRequest.FULL_RENDERING
271                     || request.getType() == RenderingRequest.GEOMETRY_RENDERING
272                     || request.getType() == RenderingRequest.FILTERS_RENDERING) {
273                 updateOriginalAllocation(preset);
274             }
275
276             if (DEBUG) {
277                 Log.v(LOGTAG, "after update, req bitmap (" + bitmap.getWidth() + "x" + bitmap.getHeight()
278                         + " ? resizeOriginal (" + mResizedOriginalBitmap.getWidth() + "x"
279                         + mResizedOriginalBitmap.getHeight());
280             }
281
282             if (request.getType() == RenderingRequest.FULL_RENDERING
283                     || request.getType() == RenderingRequest.GEOMETRY_RENDERING) {
284                 mOriginalAllocation.copyTo(bitmap);
285             } else if (request.getType() == RenderingRequest.FILTERS_RENDERING) {
286                 mFiltersOnlyOriginalAllocation.copyTo(bitmap);
287             }
288
289             if (request.getType() == RenderingRequest.FULL_RENDERING
290                     || request.getType() == RenderingRequest.FILTERS_RENDERING
291                     || request.getType() == RenderingRequest.ICON_RENDERING
292                     || request.getType() == RenderingRequest.PARTIAL_RENDERING
293                     || request.getType() == RenderingRequest.HIGHRES_RENDERING
294                     || request.getType() == RenderingRequest.STYLE_ICON_RENDERING) {
295
296                 if (request.getType() == RenderingRequest.ICON_RENDERING) {
297                     mEnvironment.setQuality(FilterEnvironment.QUALITY_ICON);
298                 } else {
299                     mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
300                 }
301
302                 Bitmap bmp = preset.apply(bitmap, mEnvironment);
303                 if (!mEnvironment.needsStop()) {
304                     request.setBitmap(bmp);
305                 }
306                 mFiltersManager.freeFilterResources(preset);
307             }
308         }
309     }
310
311     public synchronized void renderImage(ImagePreset preset, Allocation in, Allocation out) {
312         synchronized (CachingPipeline.class) {
313             if (getRenderScriptContext() == null) {
314                 return;
315             }
316             setupEnvironment(preset, false);
317             mFiltersManager.freeFilterResources(preset);
318             preset.applyFilters(-1, -1, in, out, mEnvironment);
319             boolean copyOut = false;
320             if (preset.nbFilters() > 0) {
321                 copyOut = true;
322             }
323             preset.applyBorder(in, out, copyOut, mEnvironment);
324         }
325     }
326
327     public synchronized Bitmap renderFinalImage(Bitmap bitmap, ImagePreset preset) {
328         synchronized (CachingPipeline.class) {
329             if (getRenderScriptContext() == null) {
330                 return bitmap;
331             }
332             setupEnvironment(preset, false);
333             mEnvironment.setQuality(FilterEnvironment.QUALITY_FINAL);
334             mEnvironment.setScaleFactor(1.0f);
335             mFiltersManager.freeFilterResources(preset);
336             bitmap = preset.applyGeometry(bitmap, mEnvironment);
337             bitmap = preset.apply(bitmap, mEnvironment);
338             return bitmap;
339         }
340     }
341
342     public Bitmap renderGeometryIcon(Bitmap bitmap, ImagePreset preset) {
343         return GeometryMathUtils.applyGeometryRepresentations(preset.getGeometryFilters(), bitmap);
344     }
345
346     public void compute(SharedBuffer buffer, ImagePreset preset, int type) {
347         if (getRenderScriptContext() == null) {
348             return;
349         }
350         setupEnvironment(preset, false);
351         Vector<FilterRepresentation> filters = preset.getFilters();
352         Bitmap result = mCachedProcessing.process(mOriginalBitmap, filters, mEnvironment);
353         buffer.setProducer(result);
354     }
355
356     public synchronized void computeOld(SharedBuffer buffer, ImagePreset preset, int type) {
357         synchronized (CachingPipeline.class) {
358             if (getRenderScriptContext() == null) {
359                 return;
360             }
361             if (DEBUG) {
362                 Log.v(LOGTAG, "compute preset " + preset);
363                 preset.showFilters();
364             }
365
366             String thread = Thread.currentThread().getName();
367             long time = System.currentTimeMillis();
368             setupEnvironment(preset, false);
369             mFiltersManager.freeFilterResources(preset);
370
371             Bitmap resizedOriginalBitmap = mResizedOriginalBitmap;
372             if (updateOriginalAllocation(preset) || buffer.getProducer() == null) {
373                 resizedOriginalBitmap = mResizedOriginalBitmap;
374                 buffer.setProducer(resizedOriginalBitmap);
375                 mEnvironment.cache(buffer.getProducer());
376             }
377
378             Bitmap bitmap = buffer.getProducer().getBitmap();
379             long time2 = System.currentTimeMillis();
380
381             if (bitmap == null || (bitmap.getWidth() != resizedOriginalBitmap.getWidth())
382                     || (bitmap.getHeight() != resizedOriginalBitmap.getHeight())) {
383                 mEnvironment.cache(buffer.getProducer());
384                 buffer.setProducer(resizedOriginalBitmap);
385                 bitmap = buffer.getProducer().getBitmap();
386             }
387             mOriginalAllocation.copyTo(bitmap);
388
389             Bitmap tmpbitmap = preset.apply(bitmap, mEnvironment);
390             if (tmpbitmap != bitmap) {
391                 mEnvironment.cache(buffer.getProducer());
392                 buffer.setProducer(tmpbitmap);
393             }
394
395             mFiltersManager.freeFilterResources(preset);
396
397             time = System.currentTimeMillis() - time;
398             time2 = System.currentTimeMillis() - time2;
399             if (DEBUG) {
400                 Log.v(LOGTAG, "Applying type " + type + " filters to bitmap "
401                         + bitmap + " (" + bitmap.getWidth() + " x " + bitmap.getHeight()
402                         + ") took " + time + " ms, " + time2 + " ms for the filter, on thread " + thread);
403             }
404         }
405     }
406
407     public boolean needsRepaint() {
408         SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
409         return buffer.checkRepaintNeeded();
410     }
411
412     public void setPreviewScaleFactor(float previewScaleFactor) {
413         mPreviewScaleFactor = previewScaleFactor;
414     }
415
416     public void setHighResPreviewScaleFactor(float highResPreviewScaleFactor) {
417         mHighResPreviewScaleFactor = highResPreviewScaleFactor;
418     }
419
420     public synchronized boolean isInitialized() {
421         return getRenderScriptContext() != null && mOriginalBitmap != null;
422     }
423
424     public boolean prepareRenderscriptAllocations(Bitmap bitmap) {
425         RenderScript RS = getRenderScriptContext();
426         boolean needsUpdate = false;
427         if (mOutPixelsAllocation == null || mInPixelsAllocation == null ||
428                 bitmap.getWidth() != mWidth || bitmap.getHeight() != mHeight) {
429             destroyPixelAllocations();
430             Bitmap bitmapBuffer = bitmap;
431             if (bitmap.getConfig() == null || bitmap.getConfig() != BITMAP_CONFIG) {
432                 bitmapBuffer = bitmap.copy(BITMAP_CONFIG, true);
433             }
434             mOutPixelsAllocation = Allocation.createFromBitmap(RS, bitmapBuffer,
435                     Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
436             mInPixelsAllocation = Allocation.createTyped(RS,
437                     mOutPixelsAllocation.getType());
438             needsUpdate = true;
439         }
440         if (RS != null) {
441             mInPixelsAllocation.copyFrom(bitmap);
442         }
443         if (bitmap.getWidth() != mWidth
444                 || bitmap.getHeight() != mHeight) {
445             mWidth = bitmap.getWidth();
446             mHeight = bitmap.getHeight();
447             needsUpdate = true;
448         }
449         if (DEBUG) {
450             Log.v(LOGTAG, "prepareRenderscriptAllocations: " + needsUpdate + " in " + getName());
451         }
452         return needsUpdate;
453     }
454
455     public synchronized Allocation getInPixelsAllocation() {
456         return mInPixelsAllocation;
457     }
458
459     public synchronized Allocation getOutPixelsAllocation() {
460         return mOutPixelsAllocation;
461     }
462
463     public String getName() {
464         return mName;
465     }
466
467     public RenderScript getRSContext() {
468         return CachingPipeline.getRenderScriptContext();
469     }
470 }