2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.filtershow.pipeline;
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;
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;
32 import java.util.Vector;
34 public class CachingPipeline implements PipelineInterface {
35 private static final String LOGTAG = "CachingPipeline";
36 private boolean DEBUG = false;
38 private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
40 private static volatile RenderScript sRS = null;
42 private FiltersManager mFiltersManager = null;
43 private volatile Bitmap mOriginalBitmap = null;
44 private volatile Bitmap mResizedOriginalBitmap = null;
46 private FilterEnvironment mEnvironment = new FilterEnvironment();
47 private CacheProcessing mCachedProcessing = new CacheProcessing();
50 private volatile Allocation mOriginalAllocation = null;
51 private volatile Allocation mFiltersOnlyOriginalAllocation = null;
53 protected volatile Allocation mInPixelsAllocation;
54 protected volatile Allocation mOutPixelsAllocation;
55 private volatile int mWidth = 0;
56 private volatile int mHeight = 0;
58 private volatile float mPreviewScaleFactor = 1.0f;
59 private volatile float mHighResPreviewScaleFactor = 1.0f;
60 private volatile String mName = "";
62 public CachingPipeline(FiltersManager filtersManager, String name) {
63 mFiltersManager = filtersManager;
67 public static synchronized RenderScript getRenderScriptContext() {
71 public static synchronized void createRenderscriptContext(Context context) {
73 Log.w(LOGTAG, "A prior RS context exists when calling setRenderScriptContext");
74 destroyRenderScriptContext();
76 sRS = RenderScript.create(context);
79 public static synchronized void destroyRenderScriptContext() {
87 mEnvironment.setStop(true);
90 public synchronized void reset() {
91 synchronized (CachingPipeline.class) {
92 if (getRenderScriptContext() == null) {
95 mOriginalBitmap = null; // just a reference to the bitmap in ImageLoader
96 if (mResizedOriginalBitmap != null) {
97 mResizedOriginalBitmap.recycle();
98 mResizedOriginalBitmap = null;
100 if (mOriginalAllocation != null) {
101 mOriginalAllocation.destroy();
102 mOriginalAllocation = null;
104 if (mFiltersOnlyOriginalAllocation != null) {
105 mFiltersOnlyOriginalAllocation.destroy();
106 mFiltersOnlyOriginalAllocation = null;
108 mPreviewScaleFactor = 1.0f;
109 mHighResPreviewScaleFactor = 1.0f;
111 destroyPixelAllocations();
115 public Resources getResources() {
116 return sRS.getApplicationContext().getResources();
119 private synchronized void destroyPixelAllocations() {
121 Log.v(LOGTAG, "destroyPixelAllocations in " + getName());
123 if (mInPixelsAllocation != null) {
124 mInPixelsAllocation.destroy();
125 mInPixelsAllocation = null;
127 if (mOutPixelsAllocation != null) {
128 mOutPixelsAllocation.destroy();
129 mOutPixelsAllocation = null;
135 private String getType(RenderingRequest request) {
136 if (request.getType() == RenderingRequest.ICON_RENDERING) {
137 return "ICON_RENDERING";
139 if (request.getType() == RenderingRequest.FILTERS_RENDERING) {
140 return "FILTERS_RENDERING";
142 if (request.getType() == RenderingRequest.FULL_RENDERING) {
143 return "FULL_RENDERING";
145 if (request.getType() == RenderingRequest.GEOMETRY_RENDERING) {
146 return "GEOMETRY_RENDERING";
148 if (request.getType() == RenderingRequest.PARTIAL_RENDERING) {
149 return "PARTIAL_RENDERING";
151 if (request.getType() == RenderingRequest.HIGHRES_RENDERING) {
152 return "HIGHRES_RENDERING";
154 return "UNKNOWN TYPE!";
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);
164 mEnvironment.setScaleFactor(mPreviewScaleFactor);
166 mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
167 mEnvironment.setImagePreset(preset);
168 mEnvironment.setStop(false);
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);
179 private synchronized boolean updateOriginalAllocation(ImagePreset preset) {
180 Bitmap originalBitmap = mOriginalBitmap;
182 if (originalBitmap == null) {
186 RenderScript RS = getRenderScriptContext();
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();
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();
206 public void renderHighres(RenderingRequest request) {
207 synchronized (CachingPipeline.class) {
208 if (getRenderScriptContext() == null) {
211 ImagePreset preset = request.getImagePreset();
212 setupEnvironment(preset, false);
213 Bitmap bitmap = MasterImage.getImage().getOriginalBitmapHighres();
214 if (bitmap == null) {
217 bitmap = mEnvironment.getBitmapCopy(bitmap);
218 bitmap = preset.applyGeometry(bitmap, mEnvironment);
220 mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
221 Bitmap bmp = preset.apply(bitmap, mEnvironment);
222 if (!mEnvironment.needsStop()) {
223 request.setBitmap(bmp);
225 mFiltersManager.freeFilterResources(preset);
229 public synchronized void render(RenderingRequest request) {
230 synchronized (CachingPipeline.class) {
231 if (getRenderScriptContext() == null) {
234 if (((request.getType() != RenderingRequest.PARTIAL_RENDERING
235 && request.getType() != RenderingRequest.HIGHRES_RENDERING)
236 && request.getBitmap() == null)
237 || request.getImagePreset() == null) {
242 Log.v(LOGTAG, "render image of type " + getType(request));
245 Bitmap bitmap = request.getBitmap();
246 ImagePreset preset = request.getImagePreset();
247 setupEnvironment(preset,
248 request.getType() != RenderingRequest.HIGHRES_RENDERING);
249 mFiltersManager.freeFilterResources(preset);
251 if (request.getType() == RenderingRequest.PARTIAL_RENDERING) {
252 MasterImage master = MasterImage.getImage();
253 bitmap = ImageLoader.getScaleOneImageForPreset(master.getActivity(),
255 master.getUri(), request.getBounds(),
256 request.getDestination());
257 if (bitmap == null) {
258 Log.w(LOGTAG, "could not get bitmap for: " + getType(request));
263 if (request.getType() == RenderingRequest.HIGHRES_RENDERING) {
264 bitmap = MasterImage.getImage().getOriginalBitmapHighres();
265 if (bitmap != null) {
266 bitmap = preset.applyGeometry(bitmap, mEnvironment);
270 if (request.getType() == RenderingRequest.FULL_RENDERING
271 || request.getType() == RenderingRequest.GEOMETRY_RENDERING
272 || request.getType() == RenderingRequest.FILTERS_RENDERING) {
273 updateOriginalAllocation(preset);
277 Log.v(LOGTAG, "after update, req bitmap (" + bitmap.getWidth() + "x" + bitmap.getHeight()
278 + " ? resizeOriginal (" + mResizedOriginalBitmap.getWidth() + "x"
279 + mResizedOriginalBitmap.getHeight());
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);
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) {
296 if (request.getType() == RenderingRequest.ICON_RENDERING) {
297 mEnvironment.setQuality(FilterEnvironment.QUALITY_ICON);
299 mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
302 Bitmap bmp = preset.apply(bitmap, mEnvironment);
303 if (!mEnvironment.needsStop()) {
304 request.setBitmap(bmp);
306 mFiltersManager.freeFilterResources(preset);
311 public synchronized void renderImage(ImagePreset preset, Allocation in, Allocation out) {
312 synchronized (CachingPipeline.class) {
313 if (getRenderScriptContext() == null) {
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) {
323 preset.applyBorder(in, out, copyOut, mEnvironment);
327 public synchronized Bitmap renderFinalImage(Bitmap bitmap, ImagePreset preset) {
328 synchronized (CachingPipeline.class) {
329 if (getRenderScriptContext() == null) {
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);
342 public Bitmap renderGeometryIcon(Bitmap bitmap, ImagePreset preset) {
343 return GeometryMathUtils.applyGeometryRepresentations(preset.getGeometryFilters(), bitmap);
346 public void compute(SharedBuffer buffer, ImagePreset preset, int type) {
347 if (getRenderScriptContext() == null) {
350 setupEnvironment(preset, false);
351 Vector<FilterRepresentation> filters = preset.getFilters();
352 Bitmap result = mCachedProcessing.process(mOriginalBitmap, filters, mEnvironment);
353 buffer.setProducer(result);
356 public synchronized void computeOld(SharedBuffer buffer, ImagePreset preset, int type) {
357 synchronized (CachingPipeline.class) {
358 if (getRenderScriptContext() == null) {
362 Log.v(LOGTAG, "compute preset " + preset);
363 preset.showFilters();
366 String thread = Thread.currentThread().getName();
367 long time = System.currentTimeMillis();
368 setupEnvironment(preset, false);
369 mFiltersManager.freeFilterResources(preset);
371 Bitmap resizedOriginalBitmap = mResizedOriginalBitmap;
372 if (updateOriginalAllocation(preset) || buffer.getProducer() == null) {
373 resizedOriginalBitmap = mResizedOriginalBitmap;
374 buffer.setProducer(resizedOriginalBitmap);
375 mEnvironment.cache(buffer.getProducer());
378 Bitmap bitmap = buffer.getProducer().getBitmap();
379 long time2 = System.currentTimeMillis();
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();
387 mOriginalAllocation.copyTo(bitmap);
389 Bitmap tmpbitmap = preset.apply(bitmap, mEnvironment);
390 if (tmpbitmap != bitmap) {
391 mEnvironment.cache(buffer.getProducer());
392 buffer.setProducer(tmpbitmap);
395 mFiltersManager.freeFilterResources(preset);
397 time = System.currentTimeMillis() - time;
398 time2 = System.currentTimeMillis() - time2;
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);
407 public boolean needsRepaint() {
408 SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
409 return buffer.checkRepaintNeeded();
412 public void setPreviewScaleFactor(float previewScaleFactor) {
413 mPreviewScaleFactor = previewScaleFactor;
416 public void setHighResPreviewScaleFactor(float highResPreviewScaleFactor) {
417 mHighResPreviewScaleFactor = highResPreviewScaleFactor;
420 public synchronized boolean isInitialized() {
421 return getRenderScriptContext() != null && mOriginalBitmap != null;
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);
434 mOutPixelsAllocation = Allocation.createFromBitmap(RS, bitmapBuffer,
435 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
436 mInPixelsAllocation = Allocation.createTyped(RS,
437 mOutPixelsAllocation.getType());
441 mInPixelsAllocation.copyFrom(bitmap);
443 if (bitmap.getWidth() != mWidth
444 || bitmap.getHeight() != mHeight) {
445 mWidth = bitmap.getWidth();
446 mHeight = bitmap.getHeight();
450 Log.v(LOGTAG, "prepareRenderscriptAllocations: " + needsUpdate + " in " + getName());
455 public synchronized Allocation getInPixelsAllocation() {
456 return mInPixelsAllocation;
459 public synchronized Allocation getOutPixelsAllocation() {
460 return mOutPixelsAllocation;
463 public String getName() {
467 public RenderScript getRSContext() {
468 return CachingPipeline.getRenderScriptContext();