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.NotNull;
     20 import com.android.tools.layoutlib.annotations.Nullable;
     21 import com.android.tools.layoutlib.annotations.VisibleForTesting;
     22 import java.awt.Graphics2D;
     23 import java.awt.image.BufferedImage;
     24 import java.awt.image.ImageObserver;
     25 import java.util.concurrent.locks.ReadWriteLock;
     26 import java.util.concurrent.locks.ReentrantReadWriteLock;
     27 
     28 /**
     29  * Representation of buffered image. When used with ImagePool, it provides 2 features
     30  * (last one not yet impl'd):
     31  *
     32  * <ul>
     33  *   <li>Automatic recycle of BufferedImage through use of reference counter</li>
     34  *   <li>Able to re-use the image for similar size of buffered image</li>
     35  *   <li>TODO: Able to re-use the iamge for different orientation (not yet impl'd)</li>
     36  * </ul>
     37  */
     38 /* private package */ class ImageImpl implements ImagePool.Image {
     39 
     40     private final ReadWriteLock mLock = new ReentrantReadWriteLock();
     41 
     42     private final int mWidth;
     43     private final int mHeight;
     44     private final Orientation mOrientation;
     45 
     46     @VisibleForTesting final BufferedImage mImg;
     47 
     48     ImageImpl(
     49             int width,
     50             int height,
     51             @NotNull BufferedImage img,
     52             Orientation orientation) {
     53         mImg = img;
     54         mWidth = width;
     55         mHeight = height;
     56         mOrientation = orientation;
     57     }
     58 
     59     @Override
     60     public int getWidth() {
     61         return mWidth;
     62     }
     63 
     64     @Override
     65     public int getHeight() {
     66         return mHeight;
     67     }
     68 
     69     @Override
     70     public void setRGB(int x, int y, int width, int height, int[] colors, int offset, int stride) {
     71         mLock.readLock().lock();
     72         try {
     73             // TODO: Apply orientation.
     74             mImg.setRGB(x, y, width, height, colors, offset, stride);
     75         } finally {
     76             mLock.readLock().unlock();
     77         }
     78     }
     79 
     80     @Override
     81     public void drawImage(Graphics2D graphics, int x, int y, @Nullable ImageObserver o) {
     82         mLock.readLock().lock();
     83         try {
     84             // TODO: Apply orientation.
     85             graphics.drawImage(mImg, x, y, mWidth, mHeight, o);
     86         } finally {
     87             mLock.readLock().unlock();
     88         }
     89     }
     90 }