jdk/src/share/classes/javax/swing/plaf/nimbus/ImageCache.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2658 43e06bc950ec
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
     2
 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     4
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    10
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    15
 * accompanied this code).
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    16
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2658
diff changeset
    23
 * questions.
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    24
 */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    25
package javax.swing.plaf.nimbus;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    26
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    27
import java.awt.GraphicsConfiguration;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    28
import java.awt.Image;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    29
import java.lang.ref.ReferenceQueue;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    30
import java.lang.ref.SoftReference;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    31
import java.util.Arrays;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    32
import java.util.Iterator;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    33
import java.util.LinkedHashMap;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    34
import java.util.Map;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    35
import java.util.concurrent.locks.ReadWriteLock;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    36
import java.util.concurrent.locks.ReentrantReadWriteLock;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    37
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    38
/**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    39
 * ImageCache - A fixed pixel count sized cache of Images keyed by arbitrary set of arguments. All images are held with
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    40
 * SoftReferences so they will be dropped by the GC if heap memory gets tight. When our size hits max pixel count least
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    41
 * recently requested images are removed first.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    42
 *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    43
 * @author Created by Jasper Potts (Aug 7, 2007)
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    44
 */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    45
class ImageCache {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    46
    // Ordered Map keyed by args hash, ordered by most recent accessed entry.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    47
    private final LinkedHashMap<Integer, PixelCountSoftReference> map =
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    48
            new LinkedHashMap<Integer, PixelCountSoftReference>(16, 0.75f, true);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    49
    // Maximum number of pixels to cache, this is used if maxCount
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    50
    private final int maxPixelCount;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    51
    // Maximum cached image size in pxiels
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    52
    private final int maxSingleImagePixelSize;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    53
    // The current number of pixels stored in the cache
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    54
    private int currentPixelCount = 0;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    55
    // Lock for concurrent access to map
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    56
    private ReadWriteLock lock = new ReentrantReadWriteLock();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    57
    // Reference queue for tracking lost softreferences to images in the cache
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    58
    private ReferenceQueue<Image> referenceQueue = new ReferenceQueue<Image>();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    59
    // Singleton Instance
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    60
    private static final ImageCache instance = new ImageCache();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    61
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    62
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    63
    /** Get static singleton instance */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    64
    static ImageCache getInstance() {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    65
        return instance;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    66
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    67
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    68
    public ImageCache() {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    69
        this.maxPixelCount = (8 * 1024 * 1024) / 4; // 8Mb of pixels
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    70
        this.maxSingleImagePixelSize = 300 * 300;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    71
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    72
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    73
    public ImageCache(int maxPixelCount, int maxSingleImagePixelSize) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    74
        this.maxPixelCount = maxPixelCount;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    75
        this.maxSingleImagePixelSize = maxSingleImagePixelSize;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    76
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    77
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    78
    /** Clear the cache */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    79
    public void flush() {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    80
        lock.readLock().lock();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    81
        try {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    82
            map.clear();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    83
        } finally {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    84
            lock.readLock().unlock();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    85
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    86
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    87
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    88
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    89
     * Check if the image size is to big to be stored in the cache
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    90
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    91
     * @param w The image width
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    92
     * @param h The image height
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    93
     * @return True if the image size is less than max
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    94
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    95
    public boolean isImageCachable(int w, int h) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    96
        return (w * h) < maxSingleImagePixelSize;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    97
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    98
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
    99
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   100
     * Get the cached image for given keys
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   101
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   102
     * @param config The graphics configuration, needed if cached image is a Volatile Image. Used as part of cache key
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   103
     * @param w      The image width, used as part of cache key
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   104
     * @param h      The image height, used as part of cache key
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   105
     * @param args   Other arguments to use as part of the cache key
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   106
     * @return Returns the cached Image, or null there is no cached image for key
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   107
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   108
    public Image getImage(GraphicsConfiguration config, int w, int h, Object... args) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   109
        lock.readLock().lock();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   110
        try {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   111
            PixelCountSoftReference ref = map.get(hash(config, w, h, args));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   112
            // check reference has not been lost and the key truly matches, in case of false positive hash match
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   113
            if (ref != null && ref.equals(config,w, h, args)) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   114
                return ref.get();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   115
            } else {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   116
                return null;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   117
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   118
        } finally {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   119
            lock.readLock().unlock();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   120
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   121
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   122
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   123
    /**
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   124
     * Sets the cached image for the specified constraints.
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   125
     *
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   126
     * @param image  The image to store in cache
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   127
     * @param config The graphics configuration, needed if cached image is a Volatile Image. Used as part of cache key
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   128
     * @param w      The image width, used as part of cache key
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   129
     * @param h      The image height, used as part of cache key
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   130
     * @param args   Other arguments to use as part of the cache key
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   131
     * @return true if the image could be cached or false if the image is too big
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   132
     */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   133
    public boolean setImage(Image image, GraphicsConfiguration config, int w, int h, Object... args) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   134
        if (!isImageCachable(w, h)) return false;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   135
        int hash = hash(config, w, h, args);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   136
        lock.writeLock().lock();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   137
        try {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   138
            PixelCountSoftReference ref = map.get(hash);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   139
            // check if currently in map
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   140
            if (ref != null && ref.get() == image) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   141
                return true;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   142
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   143
            // clear out old
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   144
            if (ref != null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   145
                currentPixelCount -= ref.pixelCount;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   146
                map.remove(hash);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   147
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   148
            // add new image to pixel count
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   149
            int newPixelCount = image.getWidth(null) * image.getHeight(null);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   150
            currentPixelCount += newPixelCount;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   151
            // clean out lost references if not enough space
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   152
            if (currentPixelCount > maxPixelCount) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   153
                while ((ref = (PixelCountSoftReference)referenceQueue.poll()) != null){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   154
                    //reference lost
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   155
                    map.remove(ref.hash);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   156
                    currentPixelCount -= ref.pixelCount;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   157
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   158
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   159
            // remove old items till there is enough free space
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   160
            if (currentPixelCount > maxPixelCount) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   161
                Iterator<Map.Entry<Integer, PixelCountSoftReference>> mapIter = map.entrySet().iterator();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   162
                while ((currentPixelCount > maxPixelCount) && mapIter.hasNext()) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   163
                    Map.Entry<Integer, PixelCountSoftReference> entry = mapIter.next();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   164
                    mapIter.remove();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   165
                    Image img = entry.getValue().get();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   166
                    if (img != null) img.flush();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   167
                    currentPixelCount -= entry.getValue().pixelCount;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   168
                }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   169
            }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   170
            // finaly put new in map
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   171
            map.put(hash, new PixelCountSoftReference(image, referenceQueue, newPixelCount,hash, config, w, h, args));
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   172
            return true;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   173
        } finally {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   174
            lock.writeLock().unlock();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   175
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   176
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   177
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   178
    /** Create a unique hash from all the input */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   179
    private int hash(GraphicsConfiguration config, int w, int h, Object ... args) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   180
        int hash;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   181
        hash = (config != null ? config.hashCode() : 0);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   182
        hash = 31 * hash + w;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   183
        hash = 31 * hash + h;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   184
        hash = 31 * hash + Arrays.deepHashCode(args);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   185
        return hash;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   186
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   187
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   188
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   189
    /** Extended SoftReference that stores the pixel count even after the image is lost */
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   190
    private static class PixelCountSoftReference extends SoftReference<Image> {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   191
        private final int pixelCount;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   192
        private final int hash;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   193
        // key parts
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   194
        private final GraphicsConfiguration config;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   195
        private final int w;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   196
        private final int h;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   197
        private final Object[] args;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   198
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   199
        public PixelCountSoftReference(Image referent, ReferenceQueue<? super Image> q, int pixelCount, int hash,
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   200
                                       GraphicsConfiguration config, int w, int h, Object[] args) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   201
            super(referent, q);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   202
            this.pixelCount = pixelCount;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   203
            this.hash = hash;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   204
            this.config = config;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   205
            this.w = w;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   206
            this.h = h;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   207
            this.args = args;
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   208
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   209
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   210
        public boolean equals (GraphicsConfiguration config, int w, int h, Object[] args){
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   211
            return config == this.config &&
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   212
                            w == this.w &&
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   213
                            h == this.h &&
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   214
                            Arrays.equals(args, this.args);
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   215
        }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   216
    }
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents:
diff changeset
   217
}