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 synchronized void render(RenderingRequest request) {
206 synchronized (CachingPipeline.class) {
207 if (getRenderScriptContext() == null) {
210 if (((request.getType() != RenderingRequest.PARTIAL_RENDERING
211 && request.getType() != RenderingRequest.HIGHRES_RENDERING)
212 && request.getBitmap() == null)
213 || request.getImagePreset() == null) {
218 Log.v(LOGTAG, "render image of type " + getType(request));
221 Bitmap bitmap = request.getBitmap();
222 ImagePreset preset = request.getImagePreset();
223 setupEnvironment(preset,
224 request.getType() != RenderingRequest.HIGHRES_RENDERING);
225 mFiltersManager.freeFilterResources(preset);
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));
238 if (request.getType() == RenderingRequest.HIGHRES_RENDERING) {
239 bitmap = MasterImage.getImage().getOriginalBitmapHighres();
240 if (bitmap != null) {
241 bitmap = preset.applyGeometry(bitmap, mEnvironment);
245 if (request.getType() == RenderingRequest.FULL_RENDERING
246 || request.getType() == RenderingRequest.GEOMETRY_RENDERING
247 || request.getType() == RenderingRequest.FILTERS_RENDERING) {
248 updateOriginalAllocation(preset);
252 Log.v(LOGTAG, "after update, req bitmap (" + bitmap.getWidth() + "x" + bitmap.getHeight()
253 + " ? resizeOriginal (" + mResizedOriginalBitmap.getWidth() + "x"
254 + mResizedOriginalBitmap.getHeight());
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);
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) {
271 if (request.getType() == RenderingRequest.ICON_RENDERING) {
272 mEnvironment.setQuality(FilterEnvironment.QUALITY_ICON);
274 mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
277 Bitmap bmp = preset.apply(bitmap, mEnvironment);
278 if (!mEnvironment.needsStop()) {
279 request.setBitmap(bmp);
281 mFiltersManager.freeFilterResources(preset);
286 public synchronized void renderImage(ImagePreset preset, Allocation in, Allocation out) {
287 synchronized (CachingPipeline.class) {
288 if (getRenderScriptContext() == null) {
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) {
298 preset.applyBorder(in, out, copyOut, mEnvironment);
302 public synchronized Bitmap renderFinalImage(Bitmap bitmap, ImagePreset preset) {
303 synchronized (CachingPipeline.class) {
304 if (getRenderScriptContext() == null) {
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);
317 public Bitmap renderGeometryIcon(Bitmap bitmap, ImagePreset preset) {
318 return GeometryMathUtils.applyGeometryRepresentations(preset.getGeometryFilters(), bitmap);
321 public void compute(SharedBuffer buffer, ImagePreset preset, int type) {
322 if (getRenderScriptContext() == null) {
325 setupEnvironment(preset, false);
326 Vector<FilterRepresentation> filters = preset.getFilters();
327 Bitmap result = mCachedProcessing.process(mOriginalBitmap, filters, mEnvironment);
328 buffer.setProducer(result);
331 public synchronized void computeOld(SharedBuffer buffer, ImagePreset preset, int type) {
332 synchronized (CachingPipeline.class) {
333 if (getRenderScriptContext() == null) {
337 Log.v(LOGTAG, "compute preset " + preset);
338 preset.showFilters();
341 String thread = Thread.currentThread().getName();
342 long time = System.currentTimeMillis();
343 setupEnvironment(preset, false);
344 mFiltersManager.freeFilterResources(preset);
346 Bitmap resizedOriginalBitmap = mResizedOriginalBitmap;
347 if (updateOriginalAllocation(preset) || buffer.getProducer() == null) {
348 resizedOriginalBitmap = mResizedOriginalBitmap;
349 buffer.setProducer(resizedOriginalBitmap);
350 mEnvironment.cache(buffer.getProducer());
353 Bitmap bitmap = buffer.getProducer().getBitmap();
354 long time2 = System.currentTimeMillis();
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();
362 mOriginalAllocation.copyTo(bitmap);
364 Bitmap tmpbitmap = preset.apply(bitmap, mEnvironment);
365 if (tmpbitmap != bitmap) {
366 mEnvironment.cache(buffer.getProducer());
367 buffer.setProducer(tmpbitmap);
370 mFiltersManager.freeFilterResources(preset);
372 time = System.currentTimeMillis() - time;
373 time2 = System.currentTimeMillis() - time2;
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);
382 public boolean needsRepaint() {
383 SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
384 return buffer.checkRepaintNeeded();
387 public void setPreviewScaleFactor(float previewScaleFactor) {
388 mPreviewScaleFactor = previewScaleFactor;
391 public void setHighResPreviewScaleFactor(float highResPreviewScaleFactor) {
392 mHighResPreviewScaleFactor = highResPreviewScaleFactor;
395 public synchronized boolean isInitialized() {
396 return getRenderScriptContext() != null && mOriginalBitmap != null;
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);
409 mOutPixelsAllocation = Allocation.createFromBitmap(RS, bitmapBuffer,
410 Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
411 mInPixelsAllocation = Allocation.createTyped(RS,
412 mOutPixelsAllocation.getType());
416 mInPixelsAllocation.copyFrom(bitmap);
418 if (bitmap.getWidth() != mWidth
419 || bitmap.getHeight() != mHeight) {
420 mWidth = bitmap.getWidth();
421 mHeight = bitmap.getHeight();
425 Log.v(LOGTAG, "prepareRenderscriptAllocations: " + needsUpdate + " in " + getName());
430 public synchronized Allocation getInPixelsAllocation() {
431 return mInPixelsAllocation;
434 public synchronized Allocation getOutPixelsAllocation() {
435 return mOutPixelsAllocation;
438 public String getName() {
442 public RenderScript getRSContext() {
443 return CachingPipeline.getRenderScriptContext();