Home | History | Annotate | Download | only in imagepool
      1 /*
      2  * Copyright (C) 2018 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 android.util.imagepool;
     18 
     19 import com.android.tools.layoutlib.annotations.Nullable;
     20 import com.android.tools.layoutlib.annotations.VisibleForTesting;
     21 
     22 import android.util.imagepool.ImagePool.Image.Orientation;
     23 
     24 import java.awt.image.BufferedImage;
     25 import java.lang.ref.SoftReference;
     26 import java.util.LinkedList;
     27 import java.util.Queue;
     28 
     29 /**
     30  * Data model for image pool. Bucket contains the list of same sized buffered image in soft ref.
     31  */
     32 /* private package */ class Bucket {
     33 
     34     @VisibleForTesting final Queue<SoftReference<BufferedImage>> mBufferedImageRef = new LinkedList<>();
     35 
     36     public boolean isEmpty() {
     37         return mBufferedImageRef.isEmpty();
     38     }
     39 
     40     @Nullable
     41     public BufferedImage remove() {
     42         if (mBufferedImageRef.isEmpty()) {
     43             return null;
     44         }
     45 
     46         SoftReference<BufferedImage> reference = mBufferedImageRef.remove();
     47         return reference == null ? null : reference.get();
     48     }
     49 
     50     public void offer(BufferedImage img) {
     51         mBufferedImageRef.offer(new SoftReference<>(img));
     52     }
     53 
     54     public void clear() {
     55         mBufferedImageRef.clear();
     56     }
     57 
     58     static class BucketCreationMetaData {
     59         public final int mWidth;
     60         public final int mHeight;
     61         public final int mType;
     62         public final int mNumberOfCopies;
     63         public final Orientation mOrientation;
     64         public final long mMaxCacheSize;
     65 
     66         BucketCreationMetaData(int width, int height, int type, int numberOfCopies,
     67                 Orientation orientation, long maxCacheSize) {
     68             mWidth = width;
     69             mHeight = height;
     70             mType = type;
     71             mNumberOfCopies = numberOfCopies;
     72             mOrientation = orientation;
     73             mMaxCacheSize = maxCacheSize;
     74         }
     75     }
     76 }