4272N/A/*
4272N/A * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
4272N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4272N/A *
4272N/A * This code is free software; you can redistribute it and/or modify it
4272N/A * under the terms of the GNU General Public License version 2 only, as
4272N/A * published by the Free Software Foundation. Oracle designates this
4272N/A * particular file as subject to the "Classpath" exception as provided
1674N/A * by Oracle in the LICENSE file that accompanied this code.
4272N/A *
4272N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4272N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4272N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1674N/A * version 2 for more details (a copy is included in the LICENSE file that
4272N/A * accompanied this code).
4272N/A *
4272N/A * You should have received a copy of the GNU General Public License version
1674N/A * 2 along with this work; if not, write to the Free Software Foundation,
4272N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4272N/A *
4272N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4272N/A * or visit www.oracle.com if you need additional information or have any
4272N/A * questions.
4272N/A */
1674N/A
1674N/Apackage com.apple.laf;
1674N/A
1674N/Aimport java.awt.*;
1674N/Aimport java.lang.ref.*;
1674N/Aimport java.util.*;
1674N/Aimport java.util.concurrent.locks.*;
1674N/A
1674N/Aimport apple.laf.JRSUIConstants;
1674N/Aimport apple.laf.JRSUIState;
1674N/Aimport com.apple.laf.AquaUtils.RecyclableSingleton;
1674N/A
1674N/A/**
1674N/A * ImageCache - A fixed pixel count sized cache of Images keyed by arbitrary set of arguments. All images are held with
1674N/A * SoftReferences so they will be dropped by the GC if heap memory gets tight. When our size hits max pixel count least
1674N/A * recently requested images are removed first.
1674N/A */
1674N/Afinal class ImageCache {
1674N/A // Ordered Map keyed by args hash, ordered by most recent accessed entry.
1674N/A private final LinkedHashMap<Integer, PixelCountSoftReference> map = new LinkedHashMap<>(16, 0.75f, true);
1674N/A
4632N/A // Maximum number of pixels to cache, this is used if maxCount
1674N/A private final int maxPixelCount;
4632N/A // The current number of pixels stored in the cache
1674N/A private int currentPixelCount = 0;
1674N/A
1674N/A // Lock for concurrent access to map
1674N/A private final ReadWriteLock lock = new ReentrantReadWriteLock();
1674N/A // Reference queue for tracking lost softreferences to images in the cache
1674N/A private final ReferenceQueue<Image> referenceQueue = new ReferenceQueue<>();
1674N/A
1674N/A // Singleton Instance
1674N/A private static final RecyclableSingleton<ImageCache> instance = new RecyclableSingleton<ImageCache>() {
1674N/A @Override
1674N/A protected ImageCache getInstance() {
1674N/A return new ImageCache();
1674N/A }
1674N/A };
1674N/A static ImageCache getInstance() {
1674N/A return instance.get();
1674N/A }
1674N/A
1674N/A ImageCache(final int maxPixelCount) {
1674N/A this.maxPixelCount = maxPixelCount;
1674N/A }
1674N/A
1674N/A ImageCache() {
1674N/A this((8 * 1024 * 1024) / 4); // 8Mb of pixels
1674N/A }
1674N/A
1674N/A public void flush() {
1674N/A lock.writeLock().lock();
1674N/A try {
1674N/A map.clear();
1674N/A } finally {
1674N/A lock.writeLock().unlock();
1674N/A }
1674N/A }
1674N/A
1674N/A public Image getImage(final GraphicsConfiguration config, final int w,
1674N/A final int h, final int scale,
1674N/A final JRSUIState state) {
1674N/A final int hash = hash(config, w, h, scale, state);
1674N/A final PixelCountSoftReference ref;
1674N/A lock.readLock().lock();
1674N/A try {
1674N/A ref = map.get(hash);
1674N/A } finally {
1674N/A lock.readLock().unlock();
1674N/A }
1674N/A // check reference has not been lost and the key truly matches,
1674N/A // in case of false positive hash match
1674N/A if (ref != null && ref.equals(config, w, h, scale, state)) {
1674N/A return ref.get();
1674N/A }
1674N/A return null;
1674N/A }
1674N/A
1674N/A /**
1674N/A * Sets the cached image for the specified constraints.
1674N/A *
1674N/A * @param image The image to store in cache
1674N/A * @param config The graphics configuration, needed if cached image is a Volatile Image. Used as part of cache key
1674N/A * @param w The image width, used as part of cache key
1674N/A * @param h The image height, used as part of cache key
1674N/A * @param scale The image scale factor, used as part of cache key
1674N/A * @return true if the image could be cached, false otherwise.
1674N/A */
1674N/A public boolean setImage(final Image image,
1674N/A final GraphicsConfiguration config, final int w, final int h,
1674N/A final int scale, final JRSUIState state) {
1674N/A if (state.is(JRSUIConstants.Animating.YES)) {
1674N/A return false;
1674N/A }
1674N/A
1674N/A final int hash = hash(config, w, h, scale, state);
1674N/A
1674N/A lock.writeLock().lock();
1674N/A try {
1674N/A PixelCountSoftReference ref = map.get(hash);
1674N/A // check if currently in map
1674N/A if (ref != null && ref.get() == image) return true;
1674N/A
1674N/A // clear out old
1674N/A if (ref != null) {
1674N/A currentPixelCount -= ref.pixelCount;
1674N/A map.remove(hash);
1674N/A }
1674N/A
1674N/A // add new image to pixel count
1674N/A final int newPixelCount = image.getWidth(null) * image.getHeight(null);
1674N/A currentPixelCount += newPixelCount;
1674N/A // clean out lost references if not enough space
1674N/A if (currentPixelCount > maxPixelCount) {
1674N/A while ((ref = (PixelCountSoftReference)referenceQueue.poll()) != null) {
1674N/A //reference lost
1674N/A map.remove(ref.hash);
1674N/A currentPixelCount -= ref.pixelCount;
1674N/A }
1674N/A }
1674N/A
1674N/A // remove old items till there is enough free space
1674N/A if (currentPixelCount > maxPixelCount) {
1674N/A final Iterator<Map.Entry<Integer, PixelCountSoftReference>> mapIter = map.entrySet().iterator();
1674N/A while ((currentPixelCount > maxPixelCount) && mapIter.hasNext()) {
1674N/A final Map.Entry<Integer, PixelCountSoftReference> entry = mapIter.next();
1674N/A mapIter.remove();
1674N/A final Image img = entry.getValue().get();
1674N/A if (img != null) img.flush();
1674N/A currentPixelCount -= entry.getValue().pixelCount;
1674N/A }
1674N/A }
1674N/A // finally put new in map
1674N/A map.put(hash, new PixelCountSoftReference(image, referenceQueue, newPixelCount, hash, config, w, h, scale, state));
1674N/A return true;
1674N/A } finally {
1674N/A lock.writeLock().unlock();
1674N/A }
1674N/A }
1674N/A
1674N/A private static int hash(final GraphicsConfiguration config, final int w,
1674N/A final int h, final int scale,
1674N/A final JRSUIState state) {
1674N/A int hash = config != null ? config.hashCode() : 0;
1674N/A hash = 31 * hash + w;
1674N/A hash = 31 * hash + h;
1674N/A hash = 31 * hash + scale;
1674N/A hash = 31 * hash + state.hashCode();
1674N/A return hash;
1674N/A }
1674N/A
1674N/A /**
1674N/A * Extended SoftReference that stores the pixel count even after the image
1674N/A * is lost.
*/
private static class PixelCountSoftReference extends SoftReference<Image> {
// default access, because access to these fields shouldn't be emulated
// by a synthetic accessor.
final int pixelCount;
final int hash;
// key parts
private final GraphicsConfiguration config;
private final int w;
private final int h;
private final int scale;
private final JRSUIState state;
PixelCountSoftReference(final Image referent,
final ReferenceQueue<? super Image> q, final int pixelCount,
final int hash, final GraphicsConfiguration config, final int w,
final int h, final int scale, final JRSUIState state) {
super(referent, q);
this.pixelCount = pixelCount;
this.hash = hash;
this.config = config;
this.w = w;
this.h = h;
this.scale = scale;
this.state = state;
}
boolean equals(final GraphicsConfiguration config, final int w,
final int h, final int scale, final JRSUIState state) {
return config == this.config && w == this.w && h == this.h
&& scale == this.scale && state.equals(this.state);
}
}
// /** Gets the rendered image for this painter at the requested size, either from cache or create a new one */
// private VolatileImage getImage(GraphicsConfiguration config, JComponent c, int w, int h, Object[] extendedCacheKeys) {
// VolatileImage buffer = (VolatileImage)getImage(config, w, h, this, extendedCacheKeys);
//
// int renderCounter = 0; // to avoid any potential, though unlikely, infinite loop
// do {
// //validate the buffer so we can check for surface loss
// int bufferStatus = VolatileImage.IMAGE_INCOMPATIBLE;
// if (buffer != null) {
// bufferStatus = buffer.validate(config);
// }
//
// //If the buffer status is incompatible or restored, then we need to re-render to the volatile image
// if (bufferStatus == VolatileImage.IMAGE_INCOMPATIBLE || bufferStatus == VolatileImage.IMAGE_RESTORED) {
// // if the buffer isn't the right size, or has lost its contents, then recreate
// if (buffer != null) {
// if (buffer.getWidth() != w || buffer.getHeight() != h || bufferStatus == VolatileImage.IMAGE_INCOMPATIBLE) {
// // clear any resources related to the old back buffer
// buffer.flush();
// buffer = null;
// }
// }
//
// if (buffer == null) {
// // recreate the buffer
// buffer = config.createCompatibleVolatileImage(w, h, Transparency.TRANSLUCENT);
// // put in cache for future
// setImage(buffer, config, w, h, this, extendedCacheKeys);
// }
//
// //create the graphics context with which to paint to the buffer
// Graphics2D bg = buffer.createGraphics();
//
// //clear the background before configuring the graphics
// bg.setComposite(AlphaComposite.Clear);
// bg.fillRect(0, 0, w, h);
// bg.setComposite(AlphaComposite.SrcOver);
// bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//
// // paint the painter into buffer
// paint0(bg, c, w, h, extendedCacheKeys);
// //close buffer graphics
// bg.dispose();
// }
// } while (buffer.contentsLost() && renderCounter++ < 3);
//
// // check if we failed
// if (renderCounter >= 3) return null;
//
// return buffer;
// }
}