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 mEnvironment.cache(buffer.getProducer()); // the producer bitmap isn't needed anymore
353 Bitmap result = mCachedProcessing.process(mOriginalBitmap, filters, mEnvironment);
354 buffer.setProducer(result);
357 public synchronized void computeOld(SharedBuffer buffer, ImagePreset preset, int type) {
358 synchronized (CachingPipeline.class) {
359 if (getRenderScriptContext() == null) {
363 Log.v(LOGTAG, "compute preset " + preset);
364 preset.showFilters();
367 String thread = Thread.currentThread().getName();
368 long time = System.currentTimeMillis();
369 setupEnvironment(preset, false);
370 mFiltersManager.freeFilterResources(preset);
372 Bitmap resizedOriginalBitmap = mResizedOriginalBitmap;
373 if (updateOriginalAllocation(preset) || buffer.getProducer() == null) {
374 resizedOriginalBitmap = mResizedOriginalBitmap;
375 buffer.setProducer(resizedOriginalBitmap);
376 mEnvironment.cache(buffer.getProducer());
379 Bitmap bitmap = buffer.getProducer().getBitmap();
380 long time2 = System.currentTimeMillis();
382 if (bitmap == null || (bitmap.getWidth() != resizedOriginalBitmap.getWidth())
383 || (bitmap.getHeight() != resizedOriginalBitmap.getHeight())) {
384 mEnvironment.cache(buffer.getProducer());
385 buffer.setProducer(resizedOriginalBitmap);
386 bitmap = buffer.getProducer().getBitmap();
388 mOriginalAllocation.copyTo(bitmap);
390 Bitmap tmpbitmap = preset.apply(bitmap, mEnvironment);
391 if (tmpbitmap != bitmap) {
392 mEnvironment.cache(buffer.getProducer());
393 buffer.setProducer(tmpbitmap);
396 mFiltersManager.freeFilterResources(preset);
398 time = System.currentTimeMillis() - time;
399 time2 = System.currentTimeMillis() - time2;
401 Log.v(LOGTAG, "Applying type " + type + " filters to bitmap "
402 + bitmap + " (" + bitmap.getWidth() + " x " + bitmap.getHeight()
403 + ") took " + time + " ms, " + time2 + " ms for the filter, on thread " + thread);
408 public boolean needsRepaint() {
409 SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
410 return buffer.checkRepaintNeeded();
413 public void setPreviewScaleFactor(float previewScaleFactor) {
414 mPreviewScaleFactor = previewScaleFactor;
417 public void setHighResPreviewScaleFactor(float highResPreviewScaleFactor) {
418 mHighResPreviewScaleFactor = highResPreviewScaleFactor;
421 public synchronized boolean isInitialized() {
422 return getRenderScriptContext() != null && mOriginalBitmap != null;
425 public boolean prepareRenderscriptAllocations(Bitmap bitmap) {
426 RenderScript RS = getRenderScriptContext();
427 boolean needsUpdate = false;
428 if (mOutPixelsAllocation == null || mInPixelsAllocation == null ||
429 bitmap.getWidth() != mWidth || bitmap.getHeight() != mHeight) {
430 destroyPixelAllocations();
431 Bitmap bitmapBuffer = bitmap;
432 if (bitmap.getConfig() == null || bitmap.getConfig() != BITMAP_CONFIG) {
433 bitmapBuffer = bitmap.copy(BITMAP_CONFIG, true);
435 mOutPixelsAllocation = Allocation.createFromBitmap(RS, bitmapBuffer,
436 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
437 mInPixelsAllocation = Allocation.createTyped(RS,
438 mOutPixelsAllocation.getType());
442 mInPixelsAllocation.copyFrom(bitmap);
444 if (bitmap.getWidth() != mWidth
445 || bitmap.getHeight() != mHeight) {
446 mWidth = bitmap.getWidth();
447 mHeight = bitmap.getHeight();
451 Log.v(LOGTAG, "prepareRenderscriptAllocations: " + needsUpdate + " in " + getName());
456 public synchronized Allocation getInPixelsAllocation() {
457 return mInPixelsAllocation;
460 public synchronized Allocation getOutPixelsAllocation() {
461 return mOutPixelsAllocation;
464 public String getName() {
468 public RenderScript getRSContext() {
469 return CachingPipeline.getRenderScriptContext();