Path: blob/master/src/java.desktop/share/classes/sun/swing/CachedPainter.java
41153 views
/*1* Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.swing;2526import sun.awt.image.SurfaceManager;27import sun.java2d.SurfaceData;28import java.awt.Component;29import java.awt.Graphics;30import java.awt.Graphics2D;31import java.awt.GraphicsConfiguration;32import java.awt.Image;33import java.awt.geom.AffineTransform;34import java.awt.image.AbstractMultiResolutionImage;35import java.awt.image.BufferedImage;36import java.awt.image.ImageObserver;37import java.awt.image.VolatileImage;38import java.util.Arrays;39import java.util.HashMap;40import java.util.Map;4142/**43* A base class used for icons or images that are expensive to paint.44* A subclass will do the following:45* <ol>46* <li>Invoke <code>paint</code> when you want to paint the image,47* if you are implementing <code>Icon</code> you'll invoke this from48* <code>paintIcon</code>.49* The args argument is useful when additional state is needed.50* <li>Override <code>paintToImage</code> to render the image. The code that51* lives here is equivalent to what previously would go in52* <code>paintIcon</code>, for an <code>Icon</code>.53* </ol>54* The two ways to use this class are:55* <ol>56* <li>Invoke <code>paint</code> to draw the cached reprensentation at57* the specified location.58* <li>Invoke <code>getImage</code> to get the cached reprensentation and59* draw the image yourself. This is primarly useful when you are not60* using <code>VolatileImage</code>.61* </ol>62*63*64*/65public abstract class CachedPainter {66// CacheMap maps from class to ImageCache.67private static final Map<Object,ImageCache> cacheMap = new HashMap<>();6869private static ImageCache getCache(Object key) {70synchronized(CachedPainter.class) {71ImageCache cache = cacheMap.get(key);72if (cache == null) {73if (key == PainterMultiResolutionCachedImage.class) {74cache = new ImageCache(32);75} else {76cache = new ImageCache(1);77}78cacheMap.put(key, cache);79}80return cache;81}82}8384/**85* Creates an instance of <code>CachedPainter</code> that will cache up86* to <code>cacheCount</code> images of this class.87*88* @param cacheCount Max number of images to cache89*/90public CachedPainter(int cacheCount) {91getCache(getClass()).setMaxCount(cacheCount);92}9394/**95* Renders the cached image to the passed in <code>Graphic</code>.96* If there is no cached image <code>paintToImage</code> will be invoked.97* <code>paintImage</code> is invoked to paint the cached image.98*99* @param c Component rendering to, this may be null.100* @param g Graphics to paint to101* @param x X-coordinate to render to102* @param y Y-coordinate to render to103* @param w Width to render in104* @param h Height to render in105* @param args Variable arguments that will be passed to paintToImage106*/107public void paint(Component c, Graphics g, int x,108int y, int w, int h, Object... args) {109if (w <= 0 || h <= 0) {110return;111}112synchronized (CachedPainter.class) {113paint0(c, g, x, y, w, h, args);114}115}116117private Image getImage(Object key, Component c,118int baseWidth, int baseHeight,119int w, int h, Object... args) {120GraphicsConfiguration config = getGraphicsConfiguration(c);121ImageCache cache = getCache(key);122Image image = cache.getImage(key, config, w, h, args);123int attempts = 0;124VolatileImage volatileImage = (image instanceof VolatileImage)125? (VolatileImage) image126: null;127do {128boolean draw = false;129if (volatileImage != null) {130// See if we need to recreate the image131switch (volatileImage.validate(config)) {132case VolatileImage.IMAGE_INCOMPATIBLE:133volatileImage.flush();134image = null;135break;136case VolatileImage.IMAGE_RESTORED:137draw = true;138break;139}140}141if (image == null) {142// Recreate the image143if( config != null && (w != baseHeight || h != baseWidth)) {144AffineTransform tx = config.getDefaultTransform();145double sx = tx.getScaleX();146double sy = tx.getScaleY();147if ( Double.compare(sx, 1) != 0 ||148Double.compare(sy, 1) != 0) {149if (Math.abs(sx * baseWidth - w) < 1 &&150Math.abs(sy * baseHeight - h) < 1) {151w = baseWidth;152h = baseHeight;153} else {154w = (int)Math.ceil(w / sx);155h = (int)Math.ceil(w / sy);156}157}158}159image = createImage(c, w, h, config, args);160cache.setImage(key, config, w, h, args, image);161draw = true;162volatileImage = (image instanceof VolatileImage)163? (VolatileImage) image164: null;165}166if (draw) {167// Render to the Image168Graphics2D g2 = (Graphics2D) image.getGraphics();169if (volatileImage == null) {170if ((w != baseWidth || h != baseHeight)) {171g2.scale((double) w / baseWidth,172(double) h / baseHeight);173}174paintToImage(c, image, g2, baseWidth, baseHeight, args);175} else {176SurfaceData sd = SurfaceManager.getManager(volatileImage)177.getPrimarySurfaceData();178double sx = sd.getDefaultScaleX();179double sy = sd.getDefaultScaleY();180if ( Double.compare(sx, 1) != 0 ||181Double.compare(sy, 1) != 0) {182g2.scale(1 / sx, 1 / sy);183}184paintToImage(c, image, g2, (int)Math.ceil(w * sx),185(int)Math.ceil(h * sy), args);186}187g2.dispose();188}189190// If we did this 3 times and the contents are still lost191// assume we're painting to a VolatileImage that is bogus and192// give up. Presumably we'll be called again to paint.193} while ((volatileImage != null) &&194volatileImage.contentsLost() && ++attempts < 3);195196return image;197}198199private void paint0(Component c, Graphics g, int x,200int y, int w, int h, Object... args) {201Object key = getClass();202GraphicsConfiguration config = getGraphicsConfiguration(c);203ImageCache cache = getCache(key);204Image image = cache.getImage(key, config, w, h, args);205206if (image == null) {207image = new PainterMultiResolutionCachedImage(w, h);208cache.setImage(key, config, w, h, args, image);209}210211if (image instanceof PainterMultiResolutionCachedImage) {212((PainterMultiResolutionCachedImage) image).setParams(c, args);213}214215// Render to the passed in Graphics216paintImage(c, g, x, y, w, h, image, args);217}218219/**220* Paints the representation to cache to the supplied Graphics.221*222* @param c Component painting to, may be null.223* @param image Image to paint to224* @param g Graphics to paint to, obtained from the passed in Image.225* @param w Width to paint to226* @param h Height to paint to227* @param args Arguments supplied to <code>paint</code>228*/229protected abstract void paintToImage(Component c, Image image, Graphics g,230int w, int h, Object[] args);231232233/**234* Paints the image to the specified location.235*236* @param c Component painting to237* @param g Graphics to paint to238* @param x X coordinate to paint to239* @param y Y coordinate to paint to240* @param w Width to paint to241* @param h Height to paint to242* @param image Image to paint243* @param args Arguments supplied to <code>paint</code>244*/245protected void paintImage(Component c, Graphics g,246int x, int y, int w, int h, Image image,247Object[] args) {248g.drawImage(image, x, y, null);249}250251/**252* Creates the image to cache. This returns an opaque image, subclasses253* that require translucency or transparency will need to override this254* method.255*256* @param c Component painting to257* @param w Width of image to create258* @param h Height to image to create259* @param config GraphicsConfiguration that will be260* rendered to, this may be null.261* @param args Arguments passed to paint262*/263protected Image createImage(Component c, int w, int h,264GraphicsConfiguration config, Object[] args) {265if (config == null) {266return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);267}268return config.createCompatibleVolatileImage(w, h);269}270271/**272* Clear the image cache273*/274protected void flush() {275synchronized(CachedPainter.class) {276getCache(getClass()).flush();277}278}279280private GraphicsConfiguration getGraphicsConfiguration(Component c) {281if (c == null) {282return null;283}284return c.getGraphicsConfiguration();285}286287class PainterMultiResolutionCachedImage extends AbstractMultiResolutionImage {288289private final int baseWidth;290private final int baseHeight;291private Component c;292private Object[] args;293294public PainterMultiResolutionCachedImage(int baseWidth, int baseHeight) {295this.baseWidth = baseWidth;296this.baseHeight = baseHeight;297}298299public void setParams(Component c, Object[] args) {300this.c = c;301this.args = args;302}303304@Override305public int getWidth(ImageObserver observer) {306return baseWidth;307}308309@Override310public int getHeight(ImageObserver observer) {311return baseHeight;312}313314@Override315public Image getResolutionVariant(double destWidth, double destHeight) {316int w = (int) Math.ceil(destWidth);317int h = (int) Math.ceil(destHeight);318return getImage(PainterMultiResolutionCachedImage.class,319c, baseWidth, baseHeight, w, h, args);320}321322@Override323protected Image getBaseImage() {324return getResolutionVariant(baseWidth, baseHeight);325}326327@Override328public java.util.List<Image> getResolutionVariants() {329return Arrays.asList(getResolutionVariant(baseWidth, baseHeight));330}331}332}333334335