mFiltersManager.freeFilterResources(preset);
Bitmap resizedOriginalBitmap = mResizedOriginalBitmap;
- if (updateOriginalAllocation(preset)) {
+ if (updateOriginalAllocation(preset) || buffer.getProducer() == null) {
resizedOriginalBitmap = mResizedOriginalBitmap;
- mEnvironment.cache(buffer.getProducer());
buffer.setProducer(resizedOriginalBitmap);
+ mEnvironment.cache(buffer.getProducer());
}
- Bitmap bitmap = null;
- if (buffer.getProducer() != null) {
- bitmap = buffer.getProducer().getBitmap();
- }
+ Bitmap bitmap = buffer.getProducer().getBitmap();
long time2 = System.currentTimeMillis();
if (bitmap == null || (bitmap.getWidth() != resizedOriginalBitmap.getWidth())
import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
import com.android.gallery3d.filtershow.imageshow.MasterImage;
import com.android.gallery3d.filtershow.pipeline.SharedBuffer;
+import com.android.gallery3d.filtershow.pipeline.SharedPreset;
import com.android.gallery3d.filtershow.presets.ImagePreset;
public class FilteringPipeline implements Handler.Callback {
public void handleMessage(Message msg) {
switch (msg.what) {
case NEW_PRESET: {
- SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
- buffer.swapConsumer();
MasterImage.getImage().notifyObservers();
if (mHasUnhandledPreviewRequest) {
updatePreviewBuffer();
}
switch (msg.what) {
case COMPUTE_PRESET: {
- ImagePreset preset = (ImagePreset) msg.obj;
SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
- mPreviewPipeline.compute(buffer, preset, COMPUTE_PRESET);
- buffer.swapProducer();
- Message uimsg = mUIHandler.obtainMessage(NEW_PRESET);
- mUIHandler.sendMessage(uimsg);
+ SharedPreset preset = MasterImage.getImage().getPreviewPreset();
+ ImagePreset renderingPreset = preset.dequeuePreset();
+ if (renderingPreset != null) {
+ mPreviewPipeline.compute(buffer, renderingPreset, COMPUTE_PRESET);
+ // set the preset we used in the buffer for later inspection UI-side
+ buffer.getProducer().setPreset(renderingPreset);
+ buffer.getProducer().sync();
+ buffer.swapProducer(); // push back the result
+ Message uimsg = mUIHandler.obtainMessage(NEW_PRESET);
+ mUIHandler.sendMessage(uimsg);
+ }
break;
}
case COMPUTE_RENDERING_REQUEST:
public static final byte TYPE_TINYPLANET = 6;
protected static final String NAME_TAG = "Name";
- private FilterRepresentation mTempRepresentation = null;
-
public FilterRepresentation(String name) {
mName = name;
}
representation.setShowParameterValue(showParameterValue());
representation.mSerializationName = mSerializationName;
- representation.mTempRepresentation =
- mTempRepresentation != null ? mTempRepresentation.clone() : null;
if (DEBUG) {
Log.v(LOGTAG, "cloning from <" + this + "> to <" + representation + ">");
}
public void useParametersFrom(FilterRepresentation a) {
}
- public void clearTempRepresentation() {
- mTempRepresentation = null;
- }
-
- public synchronized void updateTempParametersFrom(FilterRepresentation representation) {
- if (mTempRepresentation == null) {
- try {
- mTempRepresentation = representation.clone();
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
- } else {
- mTempRepresentation.useParametersFrom(representation);
- }
- }
-
- public synchronized void synchronizeRepresentation() {
- if (mTempRepresentation != null) {
- useParametersFrom(mTempRepresentation);
- }
- }
-
public boolean allowsSingleInstanceOnly() {
return false;
}
import com.android.gallery3d.filtershow.filters.ImageFilter;
import com.android.gallery3d.filtershow.pipeline.Buffer;
import com.android.gallery3d.filtershow.pipeline.SharedBuffer;
+import com.android.gallery3d.filtershow.pipeline.SharedPreset;
import com.android.gallery3d.filtershow.presets.ImagePreset;
import com.android.gallery3d.filtershow.state.StateAdapter;
private ImagePreset mFiltersOnlyPreset = null;
private SharedBuffer mPreviewBuffer = new SharedBuffer();
+ private SharedPreset mPreviewPreset = new SharedPreset();
private Bitmap mGeometryOnlyBitmap = null;
private Bitmap mFiltersOnlyBitmap = null;
return mPreviewBuffer;
}
+ public SharedPreset getPreviewPreset() {
+ return mPreviewPreset;
+ }
+
public void setOriginalGeometry(Bitmap originalBitmapLarge) {
GeometryMetadata geo = getPreset().getGeometry();
float w = originalBitmapLarge.getWidth();
}
public Bitmap getFilteredImage() {
+ mPreviewBuffer.swapConsumerIfNeeded(); // get latest bitmap
Buffer consumer = mPreviewBuffer.getConsumer();
if (consumer != null) {
return consumer.getBitmap();
}
public void invalidatePreview() {
+ mPreviewPreset.enqueuePreset(mPreset);
mPreviewBuffer.invalidate();
invalidatePartialPreview();
invalidateHighresPreview();
public ImagePreset getLoadedPreset() {
return mLoadedPreset;
}
+
}
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.RenderScript;
import com.android.gallery3d.filtershow.cache.CachingPipeline;
+import com.android.gallery3d.filtershow.presets.ImagePreset;
public class Buffer {
+ private static final String LOGTAG = "Buffer";
private Bitmap mBitmap;
private Allocation mAllocation;
private boolean mUseAllocation = false;
private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
+ private ImagePreset mPreset;
public Buffer(Bitmap bitmap) {
RenderScript rs = CachingPipeline.getRenderScriptContext();
- mBitmap = bitmap.copy(BITMAP_CONFIG, true);
+ if (bitmap != null) {
+ mBitmap = bitmap.copy(BITMAP_CONFIG, true);
+ }
if (mUseAllocation) {
// TODO: recreate the allocation when the RS context changes
mAllocation = Allocation.createFromBitmap(rs, mBitmap,
}
}
+ public void setBitmap(Bitmap bitmap) {
+ mBitmap = bitmap.copy(BITMAP_CONFIG, true);
+ }
+
public Bitmap getBitmap() {
return mBitmap;
}
}
}
+ public ImagePreset getPreset() {
+ return mPreset;
+ }
+
+ public void setPreset(ImagePreset preset) {
+ if ((mPreset == null) || (!mPreset.same(preset))) {
+ mPreset = new ImagePreset(preset);
+ } else {
+ mPreset.updateWith(preset);
+ }
+ }
}
package com.android.gallery3d.filtershow.pipeline;
import android.graphics.Bitmap;
-import android.util.Log;
public class SharedBuffer {
private volatile Buffer mProducer = null;
private volatile Buffer mConsumer = null;
private volatile Buffer mIntermediate = null;
- private volatile boolean mNeedsSwap = false;
+ private volatile boolean mNeedsSwap = false;
private volatile boolean mNeedsRepaint = true;
- public SharedBuffer() {
- }
-
- public synchronized void setProducer(Bitmap producer) {
- mProducer = new Buffer(producer);
+ public void setProducer(Bitmap producer) {
+ Buffer buffer = new Buffer(producer);
+ synchronized (this) {
+ mProducer = buffer;
+ }
}
public synchronized Buffer getProducer() {
}
public synchronized void swapProducer() {
- if (mProducer != null) {
- mProducer.sync();
- }
Buffer intermediate = mIntermediate;
mIntermediate = mProducer;
mProducer = intermediate;
mNeedsSwap = true;
}
- public synchronized void swapConsumer() {
+ public synchronized void swapConsumerIfNeeded() {
if (!mNeedsSwap) {
return;
}
- if (mConsumer != null) {
- mConsumer.sync();
- }
Buffer intermediate = mIntermediate;
mIntermediate = mConsumer;
mConsumer = intermediate;
--- /dev/null
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.gallery3d.filtershow.pipeline;
+
+import com.android.gallery3d.filtershow.presets.ImagePreset;
+
+public class SharedPreset {
+
+ private volatile ImagePreset mProducerPreset = null;
+ private volatile ImagePreset mConsumerPreset = null;
+ private volatile ImagePreset mIntermediatePreset = null;
+
+ public synchronized void enqueuePreset(ImagePreset preset) {
+ if (mProducerPreset == null || (!mProducerPreset.same(preset))) {
+ mProducerPreset = new ImagePreset(preset);
+ } else {
+ mProducerPreset.updateWith(preset);
+ }
+ ImagePreset temp = mIntermediatePreset;
+ mIntermediatePreset = mProducerPreset;
+ mProducerPreset = temp;
+ }
+
+ public synchronized ImagePreset dequeuePreset() {
+ ImagePreset temp = mConsumerPreset;
+ mConsumerPreset = mIntermediatePreset;
+ mIntermediatePreset = temp;
+ return mConsumerPreset;
+ }
+}
if (representation == null) {
return;
}
- synchronized (mFilters) {
- if (representation instanceof GeometryMetadata) {
- setGeometry((GeometryMetadata) representation);
- } else {
- int position = getPositionForRepresentation(representation);
- if (position == -1) {
- return;
- }
- FilterRepresentation old = mFilters.elementAt(position);
- old.updateTempParametersFrom(representation);
+ if (representation instanceof GeometryMetadata) {
+ setGeometry((GeometryMetadata) representation);
+ } else {
+ int position = getPositionForRepresentation(representation);
+ if (position == -1) {
+ return;
}
+ FilterRepresentation old = mFilters.elementAt(position);
+ old.useParametersFrom(representation);
}
MasterImage.getImage().invalidatePreview();
fillImageStateAdapter(MasterImage.getImage().getState());
return mDoApplyFilters;
}
- public synchronized GeometryMetadata getGeometry() {
+ public GeometryMetadata getGeometry() {
for (FilterRepresentation representation : mFilters) {
if (representation instanceof GeometryMetadata) {
return (GeometryMetadata) representation;
return true;
}
- public synchronized void setGeometry(GeometryMetadata representation) {
+ public void setGeometry(GeometryMetadata representation) {
GeometryMetadata geoData = getGeometry();
if (geoData != representation) {
geoData.set(representation);
// Returns a new bitmap.
if (mDoApplyGeometry) {
GeometryMetadata geoData = getGeometry();
- geoData.synchronizeRepresentation();
bitmap = environment.applyRepresentation(geoData, bitmap);
}
return bitmap;
FilterRepresentation border = getFilterRepresentationForType(
FilterRepresentation.TYPE_BORDER);
if (border != null && mDoApplyGeometry) {
- border.synchronizeRepresentation();
bitmap = environment.applyRepresentation(border, bitmap);
if (environment.getQuality() == FilterEnvironment.QUALITY_FINAL) {
UsageStatistics.onEvent(UsageStatistics.COMPONENT_EDITOR,
"SaveFilters", "Total", to - from + 1);
}
for (int i = from; i < to; i++) {
- FilterRepresentation representation = null;
- synchronized (mFilters) {
- representation = mFilters.elementAt(i);
- representation.synchronizeRepresentation();
- }
+ FilterRepresentation representation = mFilters.elementAt(i);
if (representation instanceof GeometryMetadata) {
// skip the geometry as it's already applied.
continue;
FilterRepresentation border = getFilterRepresentationForType(
FilterRepresentation.TYPE_BORDER);
if (border != null && mDoApplyGeometry) {
- border.synchronizeRepresentation();
// TODO: should keep the bitmap around
Allocation bitmapIn = in;
if (copyOut) {
to = mFilters.size();
}
for (int i = from; i < to; i++) {
- FilterRepresentation representation = null;
- synchronized (mFilters) {
- representation = mFilters.elementAt(i);
- representation.synchronizeRepresentation();
- }
+ FilterRepresentation representation = mFilters.elementAt(i);
if (representation instanceof GeometryMetadata) {
// skip the geometry as it's already applied.
continue;
return false;
}
for (int i = 0; i < mFilters.size(); i++) {
- FilterRepresentation representation = null;
- synchronized (mFilters) {
- representation = mFilters.elementAt(i);
- }
+ FilterRepresentation representation = mFilters.elementAt(i);
if (representation instanceof GeometryMetadata
&& ((GeometryMetadata) representation).hasModifications()) {
return false;
FiltersManager filtersManager = FiltersManager.getManager();
return filtersManager.createFilterFromName(name);
}
+
+ public void updateWith(ImagePreset preset) {
+ if (preset.mFilters.size() != mFilters.size()) {
+ Log.e(LOGTAG, "Updating a preset with an incompatible one");
+ return;
+ }
+ for (int i = 0; i < mFilters.size(); i++) {
+ FilterRepresentation destRepresentation = mFilters.elementAt(i);
+ FilterRepresentation sourceRepresentation = preset.mFilters.elementAt(i);
+ destRepresentation.useParametersFrom(sourceRepresentation);
+ }
+ }
}