Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cf68d2f5 authored by nicolasroard's avatar nicolasroard
Browse files

Pipeline refactoring

Add SharedBuffer and Buffer class

Change-Id: I823a9520541e3be5321a3deb5e58a358588b6cf3
parent 43cd49f3
Loading
Loading
Loading
Loading
+11 −6
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import com.android.gallery3d.filtershow.filters.FiltersManager;
import com.android.gallery3d.filtershow.filters.ImageFilterGeometry;
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.presets.FilterEnvironment;
import com.android.gallery3d.filtershow.presets.ImagePreset;
import com.android.gallery3d.filtershow.presets.PipelineInterface;
@@ -342,7 +343,7 @@ public class CachingPipeline implements PipelineInterface {
                FilterEnvironment.QUALITY_PREVIEW);
    }

    public synchronized void compute(TripleBufferBitmap buffer, ImagePreset preset, int type) {
    public synchronized void compute(SharedBuffer buffer, ImagePreset preset, int type) {
        synchronized (CachingPipeline.class) {
            if (getRenderScriptContext() == null) {
                return;
@@ -361,16 +362,20 @@ public class CachingPipeline implements PipelineInterface {
            if (updateOriginalAllocation(preset)) {
                resizedOriginalBitmap = mResizedOriginalBitmap;
                mEnvironment.cache(buffer.getProducer());
                buffer.updateProducerBitmap(resizedOriginalBitmap);
                buffer.setProducer(resizedOriginalBitmap);
            }

            Bitmap bitmap = null;
            if (buffer.getProducer() != null) {
                bitmap = buffer.getProducer().getBitmap();
            }
            Bitmap bitmap = buffer.getProducer();
            long time2 = System.currentTimeMillis();

            if (bitmap == null || (bitmap.getWidth() != resizedOriginalBitmap.getWidth())
                    || (bitmap.getHeight() != resizedOriginalBitmap.getHeight())) {
                mEnvironment.cache(buffer.getProducer());
                buffer.updateProducerBitmap(resizedOriginalBitmap);
                bitmap = buffer.getProducer();
                buffer.setProducer(resizedOriginalBitmap);
                bitmap = buffer.getProducer().getBitmap();
            }
            mOriginalAllocation.copyTo(bitmap);

@@ -393,7 +398,7 @@ public class CachingPipeline implements PipelineInterface {
    }

    public boolean needsRepaint() {
        TripleBufferBitmap buffer = MasterImage.getImage().getDoubleBuffer();
        SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
        return buffer.checkRepaintNeeded();
    }

+3 −2
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import com.android.gallery3d.filtershow.filters.FiltersManager;
import com.android.gallery3d.filtershow.filters.ImageFilterRS;
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.presets.ImagePreset;

public class FilteringPipeline implements Handler.Callback {
@@ -71,7 +72,7 @@ public class FilteringPipeline implements Handler.Callback {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case NEW_PRESET: {
                    TripleBufferBitmap buffer = MasterImage.getImage().getDoubleBuffer();
                    SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
                    buffer.swapConsumer();
                    MasterImage.getImage().notifyObservers();
                    if (mHasUnhandledPreviewRequest) {
@@ -96,7 +97,7 @@ public class FilteringPipeline implements Handler.Callback {
        switch (msg.what) {
            case COMPUTE_PRESET: {
                ImagePreset preset = (ImagePreset) msg.obj;
                TripleBufferBitmap buffer = MasterImage.getImage().getDoubleBuffer();
                SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
                mPreviewPipeline.compute(buffer, preset, COMPUTE_PRESET);
                buffer.swapProducer();
                Message uimsg = mUIHandler.obtainMessage(NEW_PRESET);
+12 −7
Original line number Diff line number Diff line
@@ -31,9 +31,10 @@ import com.android.gallery3d.filtershow.cache.FilteringPipeline;
import com.android.gallery3d.filtershow.cache.ImageLoader;
import com.android.gallery3d.filtershow.cache.RenderingRequest;
import com.android.gallery3d.filtershow.cache.RenderingRequestCaller;
import com.android.gallery3d.filtershow.cache.TripleBufferBitmap;
import com.android.gallery3d.filtershow.filters.FilterRepresentation;
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.presets.ImagePreset;
import com.android.gallery3d.filtershow.state.StateAdapter;

@@ -56,7 +57,7 @@ public class MasterImage implements RenderingRequestCaller {
    private ImagePreset mGeometryOnlyPreset = null;
    private ImagePreset mFiltersOnlyPreset = null;

    private TripleBufferBitmap mFilteredPreview = new TripleBufferBitmap();
    private SharedBuffer mPreviewBuffer = new SharedBuffer();

    private Bitmap mGeometryOnlyBitmap = null;
    private Bitmap mFiltersOnlyBitmap = null;
@@ -250,8 +251,8 @@ public class MasterImage implements RenderingRequestCaller {
        }
    }

    public TripleBufferBitmap getDoubleBuffer() {
        return mFilteredPreview;
    public SharedBuffer getPreviewBuffer() {
        return mPreviewBuffer;
    }

    public void setOriginalGeometry(Bitmap originalBitmapLarge) {
@@ -265,7 +266,11 @@ public class MasterImage implements RenderingRequestCaller {
    }

    public Bitmap getFilteredImage() {
        return mFilteredPreview.getConsumer();
        Buffer consumer = mPreviewBuffer.getConsumer();
        if (consumer != null) {
            return consumer.getBitmap();
        }
        return null;
    }

    public Bitmap getFiltersOnlyImage() {
@@ -344,7 +349,7 @@ public class MasterImage implements RenderingRequestCaller {
    }

    public void invalidatePreview() {
        mFilteredPreview.invalidate();
        mPreviewBuffer.invalidate();
        invalidatePartialPreview();
        invalidateHighresPreview();
        needsUpdatePartialPreview();
+56 −0
Original line number Diff line number Diff line
/*
 * 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 android.graphics.Bitmap;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.RenderScript;
import com.android.gallery3d.filtershow.cache.CachingPipeline;

public class Buffer {
    private Bitmap mBitmap;
    private Allocation mAllocation;
    private boolean mUseAllocation = false;
    private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;

    public Buffer(Bitmap bitmap) {
        RenderScript rs = CachingPipeline.getRenderScriptContext();
        mBitmap = bitmap.copy(BITMAP_CONFIG, true);
        if (mUseAllocation) {
            // TODO: recreate the allocation when the RS context changes
            mAllocation = Allocation.createFromBitmap(rs, mBitmap,
                    Allocation.MipmapControl.MIPMAP_NONE,
                    Allocation.USAGE_SHARED | Allocation.USAGE_SCRIPT);
        }
    }

    public Bitmap getBitmap() {
        return mBitmap;
    }

    public Allocation getAllocation() {
        return mAllocation;
    }

    public void sync() {
        if (mUseAllocation) {
            mAllocation.copyTo(mBitmap);
        }
    }

}
+20 −28
Original line number Diff line number Diff line
@@ -14,54 +14,42 @@
 * limitations under the License.
 */

package com.android.gallery3d.filtershow.cache;
package com.android.gallery3d.filtershow.pipeline;

import android.graphics.Bitmap;
import android.util.Log;

public class TripleBufferBitmap {
public class SharedBuffer {

    private static String LOGTAG = "TripleBufferBitmap";
    private static final String LOGTAG = "SharedBuffer";

    private volatile Bitmap mBitmaps[] = new Bitmap[3];
    private volatile Bitmap mProducer = null;
    private volatile Bitmap mConsumer = null;
    private volatile Bitmap mIntermediate = null;
    private volatile Buffer mProducer = null;
    private volatile Buffer mConsumer = null;
    private volatile Buffer mIntermediate = null;
    private volatile boolean mNeedsSwap = false;

    private final Bitmap.Config mBitmapConfig = Bitmap.Config.ARGB_8888;
    private volatile boolean mNeedsRepaint = true;

    public TripleBufferBitmap() {

    }

    public synchronized void updateBitmaps(Bitmap bitmap) {
        mBitmaps[0] = bitmap.copy(mBitmapConfig, true);
        mBitmaps[1] = bitmap.copy(mBitmapConfig, true);
        mBitmaps[2] = bitmap.copy(mBitmapConfig, true);
        mProducer = mBitmaps[0];
        mConsumer = mBitmaps[1];
        mIntermediate = mBitmaps[2];
    }

    public synchronized void updateProducerBitmap(Bitmap bitmap) {
        mProducer = bitmap.copy(mBitmapConfig, true);
    public SharedBuffer() {
    }

    public synchronized void setProducer(Bitmap producer) {
        mProducer = producer;
        mProducer = new Buffer(producer);
    }

    public synchronized Bitmap getProducer() {
    public synchronized Buffer getProducer() {
        return mProducer;
    }

    public synchronized Bitmap getConsumer() {
    public synchronized Buffer getConsumer() {
        return mConsumer;
    }

    public synchronized void swapProducer() {
        Bitmap intermediate = mIntermediate;
        if (mProducer != null) {
            mProducer.sync();
        }
        Buffer intermediate = mIntermediate;
        mIntermediate = mProducer;
        mProducer = intermediate;
        mNeedsSwap = true;
@@ -71,7 +59,10 @@ public class TripleBufferBitmap {
        if (!mNeedsSwap) {
            return;
        }
        Bitmap intermediate = mIntermediate;
        if (mConsumer != null) {
            mConsumer.sync();
        }
        Buffer intermediate = mIntermediate;
        mIntermediate = mConsumer;
        mConsumer = intermediate;
        mNeedsSwap = false;
@@ -90,3 +81,4 @@ public class TripleBufferBitmap {
    }

}
Loading