Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/swing/CachedPainter.java
38829 views
/*1* Copyright (c) 2004, 2015, 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 java.awt.*;27import java.awt.image.*;28import java.util.*;2930/**31* A base class used for icons or images that are expensive to paint.32* A subclass will do the following:33* <ol>34* <li>Invoke <code>paint</code> when you want to paint the image,35* if you are implementing <code>Icon</code> you'll invoke this from36* <code>paintIcon</code>.37* The args argument is useful when additional state is needed.38* <li>Override <code>paintToImage</code> to render the image. The code that39* lives here is equivalent to what previously would go in40* <code>paintIcon</code>, for an <code>Icon</code>.41* </ol>42* The two ways to use this class are:43* <ol>44* <li>Invoke <code>paint</code> to draw the cached reprensentation at45* the specified location.46* <li>Invoke <code>getImage</code> to get the cached reprensentation and47* draw the image yourself. This is primarly useful when you are not48* using <code>VolatileImage</code>.49* </ol>50*51*52*/53public abstract class CachedPainter {54// CacheMap maps from class to ImageCache.55private static final Map<Object,ImageCache> cacheMap = new HashMap<>();5657private static ImageCache getCache(Object key) {58synchronized(CachedPainter.class) {59ImageCache cache = cacheMap.get(key);60if (cache == null) {61cache = new ImageCache(1);62cacheMap.put(key, cache);63}64return cache;65}66}6768/**69* Creates an instance of <code>CachedPainter</code> that will cache up70* to <code>cacheCount</code> images of this class.71*72* @param cacheCount Max number of images to cache73*/74public CachedPainter(int cacheCount) {75getCache(getClass()).setMaxCount(cacheCount);76}7778/**79* Renders the cached image to the the passed in <code>Graphic</code>.80* If there is no cached image <code>paintToImage</code> will be invoked.81* <code>paintImage</code> is invoked to paint the cached image.82*83* @param c Component rendering to, this may be null.84* @param g Graphics to paint to85* @param x X-coordinate to render to86* @param y Y-coordinate to render to87* @param w Width to render in88* @param h Height to render in89* @param arg Variable arguments that will be passed to paintToImage90*/91public void paint(Component c, Graphics g, int x,92int y, int w, int h, Object... args) {93if (w <= 0 || h <= 0) {94return;95}96synchronized (CachedPainter.class) {97paint0(c, g, x, y, w, h, args);98}99}100101private void paint0(Component c, Graphics g, int x,102int y, int w, int h, Object... args) {103Object key = getClass();104GraphicsConfiguration config = getGraphicsConfiguration(c);105ImageCache cache = getCache(key);106Image image = cache.getImage(key, config, w, h, args);107int attempts = 0;108do {109boolean draw = false;110if (image instanceof VolatileImage) {111// See if we need to recreate the image112switch (((VolatileImage)image).validate(config)) {113case VolatileImage.IMAGE_INCOMPATIBLE:114((VolatileImage)image).flush();115image = null;116break;117case VolatileImage.IMAGE_RESTORED:118draw = true;119break;120}121}122if (image == null) {123// Recreate the image124image = createImage(c, w, h, config, args);125cache.setImage(key, config, w, h, args, image);126draw = true;127}128if (draw) {129// Render to the Image130Graphics g2 = image.getGraphics();131paintToImage(c, image, g2, w, h, args);132g2.dispose();133}134135// Render to the passed in Graphics136paintImage(c, g, x, y, w, h, image, args);137138// If we did this 3 times and the contents are still lost139// assume we're painting to a VolatileImage that is bogus and140// give up. Presumably we'll be called again to paint.141} while ((image instanceof VolatileImage) &&142((VolatileImage)image).contentsLost() && ++attempts < 3);143}144145/**146* Paints the representation to cache to the supplied Graphics.147*148* @param c Component painting to, may be null.149* @param image Image to paint to150* @param g Graphics to paint to, obtained from the passed in Image.151* @param w Width to paint to152* @param h Height to paint to153* @param args Arguments supplied to <code>paint</code>154*/155protected abstract void paintToImage(Component c, Image image, Graphics g,156int w, int h, Object[] args);157158159/**160* Paints the image to the specified location.161*162* @param c Component painting to163* @param g Graphics to paint to164* @param x X coordinate to paint to165* @param y Y coordinate to paint to166* @param w Width to paint to167* @param h Height to paint to168* @param image Image to paint169* @param args Arguments supplied to <code>paint</code>170*/171protected void paintImage(Component c, Graphics g,172int x, int y, int w, int h, Image image,173Object[] args) {174g.drawImage(image, x, y, null);175}176177/**178* Creates the image to cache. This returns an opaque image, subclasses179* that require translucency or transparency will need to override this180* method.181*182* @param c Component painting to183* @param w Width of image to create184* @param h Height to image to create185* @param config GraphicsConfiguration that will be186* rendered to, this may be null.187* @param args Arguments passed to paint188*/189protected Image createImage(Component c, int w, int h,190GraphicsConfiguration config, Object[] args) {191if (config == null) {192return new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);193}194return config.createCompatibleVolatileImage(w, h);195}196197/**198* Clear the image cache199*/200protected void flush() {201synchronized(CachedPainter.class) {202getCache(getClass()).flush();203}204}205206private GraphicsConfiguration getGraphicsConfiguration(Component c) {207if (c == null) {208return null;209}210return c.getGraphicsConfiguration();211}212}213214215