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 * 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 * 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 * 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 * 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 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 // Ordered Map keyed by args hash, ordered by most recent accessed entry. 4632N/A // Maximum number of pixels to cache, this is used if maxCount 4632N/A // The current number of pixels stored in the cache 1674N/A // Lock for concurrent access to map 1674N/A // Reference queue for tracking lost softreferences to images in the cache 1674N/A this((
8 *
1024 *
1024) /
4);
// 8Mb of pixels 1674N/A // check reference has not been lost and the key truly matches, 1674N/A // in case of false positive hash match 1674N/A * Sets the cached image for the specified constraints. 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 // check if currently in map 1674N/A // add new image to pixel count 1674N/A // clean out lost references if not enough space 1674N/A // remove old items till there is enough free space 1674N/A * Extended SoftReference that stores the pixel count even after the image // default access, because access to these fields shouldn't be emulated // by a synthetic accessor. // /** 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 // //validate the buffer so we can check for surface loss // int bufferStatus = VolatileImage.IMAGE_INCOMPATIBLE; // 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.getWidth() != w || buffer.getHeight() != h || bufferStatus == VolatileImage.IMAGE_INCOMPATIBLE) { // // clear any resources related to the old back buffer // // 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 // } while (buffer.contentsLost() && renderCounter++ < 3); // if (renderCounter >= 3) return null;