OSDN Git Service

Refactoring Geometry handling.
[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         if (highResPreview) {
161             mEnvironment.setScaleFactor(mHighResPreviewScaleFactor);
162         } else {
163             mEnvironment.setScaleFactor(mPreviewScaleFactor);
164         }
165         mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
166         mEnvironment.setImagePreset(preset);
167         mEnvironment.setStop(false);
168     }
169
170     public void setOriginal(Bitmap bitmap) {
171         mOriginalBitmap = bitmap;
172         Log.v(LOGTAG,"setOriginal, size " + bitmap.getWidth() + " x " + bitmap.getHeight());
173         ImagePreset preset = MasterImage.getImage().getPreset();
174         setupEnvironment(preset, false);
175         updateOriginalAllocation(preset);
176     }
177
178     private synchronized boolean updateOriginalAllocation(ImagePreset preset) {
179         Bitmap originalBitmap = mOriginalBitmap;
180
181         if (originalBitmap == null) {
182             return false;
183         }
184
185         RenderScript RS = getRenderScriptContext();
186
187         Allocation filtersOnlyOriginalAllocation = mFiltersOnlyOriginalAllocation;
188         mFiltersOnlyOriginalAllocation = Allocation.createFromBitmap(RS, originalBitmap,
189                 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
190         if (filtersOnlyOriginalAllocation != null) {
191             filtersOnlyOriginalAllocation.destroy();
192         }
193
194         Allocation originalAllocation = mOriginalAllocation;
195         mResizedOriginalBitmap = preset.applyGeometry(originalBitmap, mEnvironment);
196         mOriginalAllocation = Allocation.createFromBitmap(RS, mResizedOriginalBitmap,
197                 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
198         if (originalAllocation != null) {
199             originalAllocation.destroy();
200         }
201
202         return true;
203     }
204
205     public synchronized void render(RenderingRequest request) {
206         synchronized (CachingPipeline.class) {
207             if (getRenderScriptContext() == null) {
208                 return;
209             }
210             if (((request.getType() != RenderingRequest.PARTIAL_RENDERING
211                     && request.getType() != RenderingRequest.HIGHRES_RENDERING)
212                     && request.getBitmap() == null)
213                     || request.getImagePreset() == null) {
214                 return;
215             }
216
217             if (DEBUG) {
218                 Log.v(LOGTAG, "render image of type " + getType(request));
219             }
220
221             Bitmap bitmap = request.getBitmap();
222             ImagePreset preset = request.getImagePreset();
223             setupEnvironment(preset,
224                     request.getType() != RenderingRequest.HIGHRES_RENDERING);
225             mFiltersManager.freeFilterResources(preset);
226
227             if (request.getType() == RenderingRequest.PARTIAL_RENDERING) {
228                 MasterImage master = MasterImage.getImage();
229                 bitmap = ImageLoader.getScaleOneImageForPreset(master.getActivity(),
230                         master.getUri(), request.getBounds(),
231                         request.getDestination());
232                 if (bitmap == null) {
233                     Log.w(LOGTAG, "could not get bitmap for: " + getType(request));
234                     return;
235                 }
236             }
237
238             if (request.getType() == RenderingRequest.HIGHRES_RENDERING) {
239                 bitmap = MasterImage.getImage().getOriginalBitmapHighres();
240                 if (bitmap != null) {
241                     bitmap = preset.applyGeometry(bitmap, mEnvironment);
242                 }
243             }
244
245             if (request.getType() == RenderingRequest.FULL_RENDERING
246                     || request.getType() == RenderingRequest.GEOMETRY_RENDERING
247                     || request.getType() == RenderingRequest.FILTERS_RENDERING) {
248                 updateOriginalAllocation(preset);
249             }
250
251             if (DEBUG) {
252                 Log.v(LOGTAG, "after update, req bitmap (" + bitmap.getWidth() + "x" + bitmap.getHeight()
253                         + " ? resizeOriginal (" + mResizedOriginalBitmap.getWidth() + "x"
254                         + mResizedOriginalBitmap.getHeight());
255             }
256
257             if (request.getType() == RenderingRequest.FULL_RENDERING
258                     || request.getType() == RenderingRequest.GEOMETRY_RENDERING) {
259                 mOriginalAllocation.copyTo(bitmap);
260             } else if (request.getType() == RenderingRequest.FILTERS_RENDERING) {
261                 mFiltersOnlyOriginalAllocation.copyTo(bitmap);
262             }
263
264             if (request.getType() == RenderingRequest.FULL_RENDERING
265                     || request.getType() == RenderingRequest.FILTERS_RENDERING
266                     || request.getType() == RenderingRequest.ICON_RENDERING
267                     || request.getType() == RenderingRequest.PARTIAL_RENDERING
268                     || request.getType() == RenderingRequest.HIGHRES_RENDERING
269                     || request.getType() == RenderingRequest.STYLE_ICON_RENDERING) {
270
271                 if (request.getType() == RenderingRequest.ICON_RENDERING) {
272                     mEnvironment.setQuality(FilterEnvironment.QUALITY_ICON);
273                 } else {
274                     mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
275                 }
276
277                 Bitmap bmp = preset.apply(bitmap, mEnvironment);
278                 if (!mEnvironment.needsStop()) {
279                     request.setBitmap(bmp);
280                 }
281                 mFiltersManager.freeFilterResources(preset);
282             }
283         }
284     }
285
286     public synchronized void renderImage(ImagePreset preset, Allocation in, Allocation out) {
287         synchronized (CachingPipeline.class) {
288             if (getRenderScriptContext() == null) {
289                 return;
290             }
291             setupEnvironment(preset, false);
292             mFiltersManager.freeFilterResources(preset);
293             preset.applyFilters(-1, -1, in, out, mEnvironment);
294             boolean copyOut = false;
295             if (preset.nbFilters() > 0) {
296                 copyOut = true;
297             }
298             preset.applyBorder(in, out, copyOut, mEnvironment);
299         }
300     }
301
302     public synchronized Bitmap renderFinalImage(Bitmap bitmap, ImagePreset preset) {
303         synchronized (CachingPipeline.class) {
304             if (getRenderScriptContext() == null) {
305                 return bitmap;
306             }
307             setupEnvironment(preset, false);
308             mEnvironment.setQuality(FilterEnvironment.QUALITY_FINAL);
309             mEnvironment.setScaleFactor(1.0f);
310             mFiltersManager.freeFilterResources(preset);
311             bitmap = preset.applyGeometry(bitmap, mEnvironment);
312             bitmap = preset.apply(bitmap, mEnvironment);
313             return bitmap;
314         }
315     }
316
317     public Bitmap renderGeometryIcon(Bitmap bitmap, ImagePreset preset) {
318         return GeometryMathUtils.applyGeometryRepresentations(preset.getGeometryFilters(), bitmap);
319     }
320
321     public void compute(SharedBuffer buffer, ImagePreset preset, int type) {
322         if (getRenderScriptContext() == null) {
323             return;
324         }
325         setupEnvironment(preset, false);
326         Vector<FilterRepresentation> filters = preset.getFilters();
327         Bitmap result = mCachedProcessing.process(mOriginalBitmap, filters, mEnvironment);
328         buffer.setProducer(result);
329     }
330
331     public synchronized void computeOld(SharedBuffer buffer, ImagePreset preset, int type) {
332         synchronized (CachingPipeline.class) {
333             if (getRenderScriptContext() == null) {
334                 return;
335             }
336             if (DEBUG) {
337                 Log.v(LOGTAG, "compute preset " + preset);
338                 preset.showFilters();
339             }
340
341             String thread = Thread.currentThread().getName();
342             long time = System.currentTimeMillis();
343             setupEnvironment(preset, false);
344             mFiltersManager.freeFilterResources(preset);
345
346             Bitmap resizedOriginalBitmap = mResizedOriginalBitmap;
347             if (updateOriginalAllocation(preset) || buffer.getProducer() == null) {
348                 resizedOriginalBitmap = mResizedOriginalBitmap;
349                 buffer.setProducer(resizedOriginalBitmap);
350                 mEnvironment.cache(buffer.getProducer());
351             }
352
353             Bitmap bitmap = buffer.getProducer().getBitmap();
354             long time2 = System.currentTimeMillis();
355
356             if (bitmap == null || (bitmap.getWidth() != resizedOriginalBitmap.getWidth())
357                     || (bitmap.getHeight() != resizedOriginalBitmap.getHeight())) {
358                 mEnvironment.cache(buffer.getProducer());
359                 buffer.setProducer(resizedOriginalBitmap);
360                 bitmap = buffer.getProducer().getBitmap();
361             }
362             mOriginalAllocation.copyTo(bitmap);
363
364             Bitmap tmpbitmap = preset.apply(bitmap, mEnvironment);
365             if (tmpbitmap != bitmap) {
366                 mEnvironment.cache(buffer.getProducer());
367                 buffer.setProducer(tmpbitmap);
368             }
369
370             mFiltersManager.freeFilterResources(preset);
371
372             time = System.currentTimeMillis() - time;
373             time2 = System.currentTimeMillis() - time2;
374             if (DEBUG) {
375                 Log.v(LOGTAG, "Applying type " + type + " filters to bitmap "
376                         + bitmap + " (" + bitmap.getWidth() + " x " + bitmap.getHeight()
377                         + ") took " + time + " ms, " + time2 + " ms for the filter, on thread " + thread);
378             }
379         }
380     }
381
382     public boolean needsRepaint() {
383         SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
384         return buffer.checkRepaintNeeded();
385     }
386
387     public void setPreviewScaleFactor(float previewScaleFactor) {
388         mPreviewScaleFactor = previewScaleFactor;
389     }
390
391     public void setHighResPreviewScaleFactor(float highResPreviewScaleFactor) {
392         mHighResPreviewScaleFactor = highResPreviewScaleFactor;
393     }
394
395     public synchronized boolean isInitialized() {
396         return getRenderScriptContext() != null && mOriginalBitmap != null;
397     }
398
399     public boolean prepareRenderscriptAllocations(Bitmap bitmap) {
400         RenderScript RS = getRenderScriptContext();
401         boolean needsUpdate = false;
402         if (mOutPixelsAllocation == null || mInPixelsAllocation == null ||
403                 bitmap.getWidth() != mWidth || bitmap.getHeight() != mHeight) {
404             destroyPixelAllocations();
405             Bitmap bitmapBuffer = bitmap;
406             if (bitmap.getConfig() == null || bitmap.getConfig() != BITMAP_CONFIG) {
407                 bitmapBuffer = bitmap.copy(BITMAP_CONFIG, true);
408             }
409             mOutPixelsAllocation = Allocation.createFromBitmap(RS, bitmapBuffer,
410                     Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
411             mInPixelsAllocation = Allocation.createTyped(RS,
412                     mOutPixelsAllocation.getType());
413             needsUpdate = true;
414         }
415         if (RS != null) {
416             mInPixelsAllocation.copyFrom(bitmap);
417         }
418         if (bitmap.getWidth() != mWidth
419                 || bitmap.getHeight() != mHeight) {
420             mWidth = bitmap.getWidth();
421             mHeight = bitmap.getHeight();
422             needsUpdate = true;
423         }
424         if (DEBUG) {
425             Log.v(LOGTAG, "prepareRenderscriptAllocations: " + needsUpdate + " in " + getName());
426         }
427         return needsUpdate;
428     }
429
430     public synchronized Allocation getInPixelsAllocation() {
431         return mInPixelsAllocation;
432     }
433
434     public synchronized Allocation getOutPixelsAllocation() {
435         return mOutPixelsAllocation;
436     }
437
438     public String getName() {
439         return mName;
440     }
441
442     public RenderScript getRSContext() {
443         return CachingPipeline.getRenderScriptContext();
444     }
445 }