1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17package com.android.gallery3d.filtershow.pipeline;
18
19import android.graphics.Bitmap;
20
21public class SharedBuffer {
22
23    private static final String LOGTAG = "SharedBuffer";
24
25    private volatile Buffer mProducer = null;
26    private volatile Buffer mConsumer = null;
27    private volatile Buffer mIntermediate = null;
28
29    private volatile boolean mNeedsSwap = false;
30    private volatile boolean mNeedsRepaint = true;
31
32    public synchronized void setProducer(Bitmap producer) {
33        if (mProducer != null
34                && !mProducer.isSameSize(producer)) {
35            mProducer.remove();
36            mProducer = null;
37        }
38        if (mProducer == null) {
39            mProducer = new Buffer(producer);
40        } else {
41            mProducer.useBitmap(producer);
42        }
43    }
44
45    public synchronized Buffer getProducer() {
46        return mProducer;
47    }
48
49    public synchronized Buffer getConsumer() {
50        return mConsumer;
51    }
52
53    public synchronized void swapProducer() {
54        Buffer intermediate = mIntermediate;
55        mIntermediate = mProducer;
56        mProducer = intermediate;
57        mNeedsSwap = true;
58    }
59
60    public synchronized void swapConsumerIfNeeded() {
61        if (!mNeedsSwap) {
62            return;
63        }
64        Buffer intermediate = mIntermediate;
65        mIntermediate = mConsumer;
66        mConsumer = intermediate;
67        mNeedsSwap = false;
68    }
69
70    public synchronized void invalidate() {
71        mNeedsRepaint = true;
72    }
73
74    public synchronized boolean checkRepaintNeeded() {
75        if (mNeedsRepaint) {
76            mNeedsRepaint = false;
77            return true;
78        }
79        return false;
80    }
81
82}
83
84