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 if (highResPreview) {
161 mEnvironment.setScaleFactor(mHighResPreviewScaleFactor);
163 mEnvironment.setScaleFactor(mPreviewScaleFactor);
165 mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
166 mEnvironment.setImagePreset(preset);
167 mEnvironment.setStop(false);
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);
178 private synchronized boolean updateOriginalAllocation(ImagePreset preset) {
179 Bitmap originalBitmap = mOriginalBitmap;
181 if (originalBitmap == null) {
185 RenderScript RS = getRenderScriptContext();
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();
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();
205 public void renderHighres(RenderingRequest request) {
206 synchronized (CachingPipeline.class) {
207 if (getRenderScriptContext() == null) {
210 ImagePreset preset = request.getImagePreset();
211 setupEnvironment(preset, false);
212 Bitmap bitmap = MasterImage.getImage().getOriginalBitmapHighres();
213 if (bitmap == null) {
216 // TODO: use a cache of bitmaps
217 bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
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(),
254 master.getUri(), request.getBounds(),
255 request.getDestination());
256 if (bitmap == null) {
257 Log.w(LOGTAG, "could not get bitmap for: " + getType(request));
262 if (request.getType() == RenderingRequest.HIGHRES_RENDERING) {
263 bitmap = MasterImage.getImage().getOriginalBitmapHighres();
264 if (bitmap != null) {
265 bitmap = preset.applyGeometry(bitmap, mEnvironment);
269 if (request.getType() == RenderingRequest.FULL_RENDERING
270 || request.getType() == RenderingRequest.GEOMETRY_RENDERING
271 || request.getType() == RenderingRequest.FILTERS_RENDERING) {
272 updateOriginalAllocation(preset);
276 Log.v(LOGTAG, "after update, req bitmap (" + bitmap.getWidth() + "x" + bitmap.getHeight()
277 + " ? resizeOriginal (" + mResizedOriginalBitmap.getWidth() + "x"
278 + mResizedOriginalBitmap.getHeight());
281 if (request.getType() == RenderingRequest.FULL_RENDERING
282 || request.getType() == RenderingRequest.GEOMETRY_RENDERING) {
283 mOriginalAllocation.copyTo(bitmap);
284 } else if (request.getType() == RenderingRequest.FILTERS_RENDERING) {
285 mFiltersOnlyOriginalAllocation.copyTo(bitmap);
288 if (request.getType() == RenderingRequest.FULL_RENDERING
289 || request.getType() == RenderingRequest.FILTERS_RENDERING
290 || request.getType() == RenderingRequest.ICON_RENDERING
291 || request.getType() == RenderingRequest.PARTIAL_RENDERING
292 || request.getType() == RenderingRequest.HIGHRES_RENDERING
293 || request.getType() == RenderingRequest.STYLE_ICON_RENDERING) {
295 if (request.getType() == RenderingRequest.ICON_RENDERING) {
296 mEnvironment.setQuality(FilterEnvironment.QUALITY_ICON);
298 mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
301 Bitmap bmp = preset.apply(bitmap, mEnvironment);
302 if (!mEnvironment.needsStop()) {
303 request.setBitmap(bmp);
305 mFiltersManager.freeFilterResources(preset);
310 public synchronized void renderImage(ImagePreset preset, Allocation in, Allocation out) {
311 synchronized (CachingPipeline.class) {
312 if (getRenderScriptContext() == null) {
315 setupEnvironment(preset, false);
316 mFiltersManager.freeFilterResources(preset);
317 preset.applyFilters(-1, -1, in, out, mEnvironment);
318 boolean copyOut = false;
319 if (preset.nbFilters() > 0) {
322 preset.applyBorder(in, out, copyOut, mEnvironment);
326 public synchronized Bitmap renderFinalImage(Bitmap bitmap, ImagePreset preset) {
327 synchronized (CachingPipeline.class) {
328 if (getRenderScriptContext() == null) {
331 setupEnvironment(preset, false);
332 mEnvironment.setQuality(FilterEnvironment.QUALITY_FINAL);
333 mEnvironment.setScaleFactor(1.0f);
334 mFiltersManager.freeFilterResources(preset);
335 bitmap = preset.applyGeometry(bitmap, mEnvironment);
336 bitmap = preset.apply(bitmap, mEnvironment);
341 public Bitmap renderGeometryIcon(Bitmap bitmap, ImagePreset preset) {
342 return GeometryMathUtils.applyGeometryRepresentations(preset.getGeometryFilters(), bitmap);
345 public void compute(SharedBuffer buffer, ImagePreset preset, int type) {
346 if (getRenderScriptContext() == null) {
349 setupEnvironment(preset, false);
350 Vector<FilterRepresentation> filters = preset.getFilters();
351 Bitmap result = mCachedProcessing.process(mOriginalBitmap, filters, mEnvironment);
352 buffer.setProducer(result);
355 public synchronized void computeOld(SharedBuffer buffer, ImagePreset preset, int type) {
356 synchronized (CachingPipeline.class) {
357 if (getRenderScriptContext() == null) {
361 Log.v(LOGTAG, "compute preset " + preset);
362 preset.showFilters();
365 String thread = Thread.currentThread().getName();
366 long time = System.currentTimeMillis();
367 setupEnvironment(preset, false);
368 mFiltersManager.freeFilterResources(preset);
370 Bitmap resizedOriginalBitmap = mResizedOriginalBitmap;
371 if (updateOriginalAllocation(preset) || buffer.getProducer() == null) {
372 resizedOriginalBitmap = mResizedOriginalBitmap;
373 buffer.setProducer(resizedOriginalBitmap);
374 mEnvironment.cache(buffer.getProducer());
377 Bitmap bitmap = buffer.getProducer().getBitmap();
378 long time2 = System.currentTimeMillis();
380 if (bitmap == null || (bitmap.getWidth() != resizedOriginalBitmap.getWidth())
381 || (bitmap.getHeight() != resizedOriginalBitmap.getHeight())) {
382 mEnvironment.cache(buffer.getProducer());
383 buffer.setProducer(resizedOriginalBitmap);
384 bitmap = buffer.getProducer().getBitmap();
386 mOriginalAllocation.copyTo(bitmap);
388 Bitmap tmpbitmap = preset.apply(bitmap, mEnvironment);
389 if (tmpbitmap != bitmap) {
390 mEnvironment.cache(buffer.getProducer());
391 buffer.setProducer(tmpbitmap);
394 mFiltersManager.freeFilterResources(preset);
396 time = System.currentTimeMillis() - time;
397 time2 = System.currentTimeMillis() - time2;
399 Log.v(LOGTAG, "Applying type " + type + " filters to bitmap "
400 + bitmap + " (" + bitmap.getWidth() + " x " + bitmap.getHeight()
401 + ") took " + time + " ms, " + time2 + " ms for the filter, on thread " + thread);
406 public boolean needsRepaint() {
407 SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
408 return buffer.checkRepaintNeeded();
411 public void setPreviewScaleFactor(float previewScaleFactor) {
412 mPreviewScaleFactor = previewScaleFactor;
415 public void setHighResPreviewScaleFactor(float highResPreviewScaleFactor) {
416 mHighResPreviewScaleFactor = highResPreviewScaleFactor;
419 public synchronized boolean isInitialized() {
420 return getRenderScriptContext() != null && mOriginalBitmap != null;
423 public boolean prepareRenderscriptAllocations(Bitmap bitmap) {
424 RenderScript RS = getRenderScriptContext();
425 boolean needsUpdate = false;
426 if (mOutPixelsAllocation == null || mInPixelsAllocation == null ||
427 bitmap.getWidth() != mWidth || bitmap.getHeight() != mHeight) {
428 destroyPixelAllocations();
429 Bitmap bitmapBuffer = bitmap;
430 if (bitmap.getConfig() == null || bitmap.getConfig() != BITMAP_CONFIG) {
431 bitmapBuffer = bitmap.copy(BITMAP_CONFIG, true);
433 mOutPixelsAllocation = Allocation.createFromBitmap(RS, bitmapBuffer,
434 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
435 mInPixelsAllocation = Allocation.createTyped(RS,
436 mOutPixelsAllocation.getType());
440 mInPixelsAllocation.copyFrom(bitmap);
442 if (bitmap.getWidth() != mWidth
443 || bitmap.getHeight() != mHeight) {
444 mWidth = bitmap.getWidth();
445 mHeight = bitmap.getHeight();
449 Log.v(LOGTAG, "prepareRenderscriptAllocations: " + needsUpdate + " in " + getName());
454 public synchronized Allocation getInPixelsAllocation() {
455 return mInPixelsAllocation;
458 public synchronized Allocation getOutPixelsAllocation() {
459 return mOutPixelsAllocation;
462 public String getName() {
466 public RenderScript getRSContext() {
467 return CachingPipeline.getRenderScriptContext();