Home | History | Annotate | Download | only in cache
      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 
     17 package com.android.gallery3d.filtershow.cache;
     18 
     19 import android.graphics.Bitmap;
     20 
     21 public class TripleBufferBitmap {
     22 
     23     private static String LOGTAG = "TripleBufferBitmap";
     24 
     25     private volatile Bitmap mBitmaps[] = new Bitmap[3];
     26     private volatile Bitmap mProducer = null;
     27     private volatile Bitmap mConsumer = null;
     28     private volatile Bitmap mIntermediate = null;
     29     private volatile boolean mNeedsSwap = false;
     30 
     31     private final Bitmap.Config mBitmapConfig = Bitmap.Config.ARGB_8888;
     32     private volatile boolean mNeedsRepaint = true;
     33 
     34     public TripleBufferBitmap() {
     35 
     36     }
     37 
     38     public synchronized void updateBitmaps(Bitmap bitmap) {
     39         mBitmaps[0] = bitmap.copy(mBitmapConfig, true);
     40         mBitmaps[1] = bitmap.copy(mBitmapConfig, true);
     41         mBitmaps[2] = bitmap.copy(mBitmapConfig, true);
     42         mProducer = mBitmaps[0];
     43         mConsumer = mBitmaps[1];
     44         mIntermediate = mBitmaps[2];
     45     }
     46 
     47     public synchronized void updateProducerBitmap(Bitmap bitmap) {
     48         mProducer = bitmap.copy(mBitmapConfig, true);
     49     }
     50 
     51     public synchronized void setProducer(Bitmap producer) {
     52         mProducer = producer;
     53     }
     54 
     55     public synchronized Bitmap getProducer() {
     56         return mProducer;
     57     }
     58 
     59     public synchronized Bitmap getConsumer() {
     60         return mConsumer;
     61     }
     62 
     63     public synchronized void swapProducer() {
     64         Bitmap intermediate = mIntermediate;
     65         mIntermediate = mProducer;
     66         mProducer = intermediate;
     67         mNeedsSwap = true;
     68     }
     69 
     70     public synchronized void swapConsumer() {
     71         if (!mNeedsSwap) {
     72             return;
     73         }
     74         Bitmap intermediate = mIntermediate;
     75         mIntermediate = mConsumer;
     76         mConsumer = intermediate;
     77         mNeedsSwap = false;
     78     }
     79 
     80     public synchronized void invalidate() {
     81         mNeedsRepaint = true;
     82     }
     83 
     84     public synchronized boolean checkRepaintNeeded() {
     85         if (mNeedsRepaint) {
     86             mNeedsRepaint = false;
     87             return true;
     88         }
     89         return false;
     90     }
     91 
     92 }
     93