Ignore:
Timestamp:
2020-11-27T22:45:55+01:00 (3 years ago)
Author:
simon04
Message:

see #20141 - Revert "ImageProvider: cache rendered SVG images using JCS"

Location:
trunk/src/org/openstreetmap/josm/tools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/ImageProvider.java

    r17364 r17369  
    954954                        continue;
    955955                    }
    956                     ir = getIfAvailableLocalURL(subdir + name, path, type);
     956                    ir = getIfAvailableLocalURL(path, type);
    957957                    if (ir != null) {
    958958                        cache.put(cacheName, ir);
     
    984984                    svg = getSvgUniverse().getDiagram(uri);
    985985                }
    986                 return svg == null ? null : new ImageResource(url, svg);
     986                return svg == null ? null : new ImageResource(svg);
    987987            case OTHER:
    988988                BufferedImage img = null;
     
    992992                    Logging.log(Logging.LEVEL_WARN, "Exception while reading HTTP image:", e);
    993993                }
    994                 return img == null ? null : new ImageResource(url, img);
     994                return img == null ? null : new ImageResource(img);
    995995            default:
    996996                throw new AssertionError("Unsupported type: " + type);
     
    10411041                    return null;
    10421042                }
    1043                 return new ImageResource(url, svg);
     1043                return new ImageResource(svg);
    10441044            } else {
    10451045                try {
     
    10501050                    // CHECKSTYLE.ON: LineLength
    10511051                    Image img = read(new ByteArrayInputStream(bytes), false, true);
    1052                     return img == null ? null : new ImageResource(url, img);
     1052                    return img == null ? null : new ImageResource(img);
    10531053                } catch (IOException | UnsatisfiedLinkError e) {
    10541054                    Logging.log(Logging.LEVEL_WARN, "Exception while reading image:", e);
     
    11251125                            svg = getSvgUniverse().getDiagram(uri);
    11261126                        }
    1127                         return svg == null ? null : new ImageResource(fullName, svg);
     1127                        return svg == null ? null : new ImageResource(svg);
    11281128                    case OTHER:
    11291129                        while (size > 0) {
     
    11381138                            Logging.warn(e);
    11391139                        }
    1140                         return img == null ? null : new ImageResource(fullName, img);
     1140                        return img == null ? null : new ImageResource(img);
    11411141                    default:
    11421142                        throw new AssertionError("Unknown ImageType: "+type);
     
    11571157     * @return the requested image or null if the request failed
    11581158     */
    1159     private static ImageResource getIfAvailableLocalURL(String cacheKey, URL path, ImageType type) {
     1159    private static ImageResource getIfAvailableLocalURL(URL path, ImageType type) {
    11601160        switch (type) {
    11611161        case SVG:
    1162             return new ImageResource(cacheKey, () -> {
    1163                 synchronized (getSvgUniverse()) {
     1162            SVGDiagram svg = null;
     1163            synchronized (getSvgUniverse()) {
     1164                try {
     1165                    URI uri = null;
    11641166                    try {
    1165                         URI uri = null;
    1166                         try {
    1167                             uri = getSvgUniverse().loadSVG(path);
    1168                         } catch (InvalidPathException e) {
    1169                             Logging.error("Cannot open {0}: {1}", path, e.getMessage());
    1170                             Logging.trace(e);
     1167                        uri = getSvgUniverse().loadSVG(path);
     1168                    } catch (InvalidPathException e) {
     1169                        Logging.error("Cannot open {0}: {1}", path, e.getMessage());
     1170                        Logging.trace(e);
     1171                    }
     1172                    if (uri == null && "jar".equals(path.getProtocol())) {
     1173                        URL betterPath = Utils.betterJarUrl(path);
     1174                        if (betterPath != null) {
     1175                            uri = getSvgUniverse().loadSVG(betterPath);
    11711176                        }
    1172                         if (uri == null && "jar".equals(path.getProtocol())) {
    1173                             URL betterPath = Utils.betterJarUrl(path);
    1174                             if (betterPath != null) {
    1175                                 uri = getSvgUniverse().loadSVG(betterPath);
    1176                             }
    1177                         }
    1178                         return getSvgUniverse().getDiagram(uri);
    1179                     } catch (SecurityException | IOException e) {
    1180                         Logging.log(Logging.LEVEL_WARN, "Unable to read SVG", e);
    11811177                    }
    1182                 }
    1183                 return null;
    1184             });
     1178                    svg = getSvgUniverse().getDiagram(uri);
     1179                } catch (SecurityException | IOException e) {
     1180                    Logging.log(Logging.LEVEL_WARN, "Unable to read SVG", e);
     1181                }
     1182            }
     1183            return svg == null ? null : new ImageResource(svg);
    11851184        case OTHER:
    11861185            BufferedImage img = null;
     
    11971196                Logging.debug(e);
    11981197            }
    1199             return img == null ? null : new ImageResource(path.toString(), img);
     1198            return img == null ? null : new ImageResource(img);
    12001199        default:
    12011200            throw new AssertionError();
     
    13951394     */
    13961395    public static Image createBoundedImage(Image img, int maxSize) {
    1397         return new ImageResource(img.toString(), img).getImageIconBounded(new Dimension(maxSize, maxSize)).getImage();
     1396        return new ImageResource(img).getImageIconBounded(new Dimension(maxSize, maxSize)).getImage();
    13981397    }
    13991398
     
    19341933    }
    19351934
    1936     /**
    1937      * Prints statistics concerning the image loading caches.
    1938      */
    1939     public static void printStatistics() {
    1940         Logging.info(getSvgUniverse().statistics());
    1941         Logging.info(ImageResource.statistics());
    1942     }
    1943 
    19441935    @Override
    19451936    public String toString() {
  • trunk/src/org/openstreetmap/josm/tools/ImageResource.java

    r17364 r17369  
    55import java.awt.Image;
    66import java.awt.image.BufferedImage;
    7 import java.io.IOException;
    8 import java.io.UncheckedIOException;
    97import java.util.List;
    10 import java.util.Locale;
    11 import java.util.Objects;
    12 import java.util.function.Supplier;
     8import java.util.Map;
     9import java.util.concurrent.ConcurrentHashMap;
    1310
    1411import javax.swing.AbstractAction;
     
    1815import javax.swing.JPanel;
    1916import javax.swing.UIManager;
    20 
    21 import org.apache.commons.jcs3.access.behavior.ICacheAccess;
    22 import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry;
    23 import org.openstreetmap.josm.data.cache.JCSCacheManager;
    2417
    2518import com.kitfox.svg.SVGDiagram;
     
    3629
    3730    /**
    38      * Caches the image data for resized versions of the same image.
    39      * Depending on {@link JCSCacheManager#USE_IMAGE_RESOURCE_CACHE}, a combined memory/disk, or an in-memory-cache is used.
    40      */
    41     private static final ICacheAccess<String, BufferedImageCacheEntry> imgCache = JCSCacheManager.getImageResourceCache();
    42 
    43     private final String cacheKey;
     31     * Caches the image data for resized versions of the same image. The key is obtained using {@link ImageResizeMode#cacheKey(Dimension)}.
     32     */
     33    private final Map<Integer, BufferedImage> imgCache = new ConcurrentHashMap<>(4);
    4434    /**
    4535     * SVG diagram information in case of SVG vector image.
     
    4737    private SVGDiagram svg;
    4838    /**
    49      * Supplier for SVG diagram information in case of possibly cached SVG vector image.
    50      */
    51     private Supplier<SVGDiagram> svgSupplier;
    52     /**
    5339     * Use this dimension to request original file dimension.
    5440     */
     
    6955    /**
    7056     * Constructs a new {@code ImageResource} from an image.
    71      * @param cacheKey the caching identifier of the image
    7257     * @param img the image
    7358     */
    74     public ImageResource(String cacheKey, Image img) {
    75         this.cacheKey = Objects.requireNonNull(cacheKey);
    76         this.baseImage = Objects.requireNonNull(img);
     59    public ImageResource(Image img) {
     60        CheckParameterUtil.ensureParameterNotNull(img);
     61        baseImage = img;
    7762    }
    7863
    7964    /**
    8065     * Constructs a new {@code ImageResource} from SVG data.
    81      * @param cacheKey the caching identifier of the image
    8266     * @param svg SVG data
    8367     */
    84     public ImageResource(String cacheKey, SVGDiagram svg) {
    85         this.cacheKey = Objects.requireNonNull(cacheKey);
    86         this.svg = Objects.requireNonNull(svg);
    87     }
    88 
    89     public ImageResource(String cacheKey, Supplier<SVGDiagram> svgSupplier) {
    90         this.cacheKey = Objects.requireNonNull(cacheKey);
    91         this.svgSupplier = Objects.requireNonNull(svgSupplier);
     68    public ImageResource(SVGDiagram svg) {
     69        CheckParameterUtil.ensureParameterNotNull(svg);
     70        this.svg = svg;
    9271    }
    9372
     
    9978     */
    10079    public ImageResource(ImageResource res, List<ImageOverlay> overlayInfo) {
    101         this.cacheKey = res.cacheKey;
    10280        this.svg = res.svg;
    103         this.svgSupplier = res.svgSupplier;
    10481        this.baseImage = res.baseImage;
    10582        this.overlayInfo = overlayInfo;
     
    180157    ImageIcon getImageIcon(Dimension dim, boolean multiResolution, ImageResizeMode resizeMode) {
    181158        return getImageIconAlreadyScaled(GuiSizesHelper.getDimensionDpiAdjusted(dim), multiResolution, false, resizeMode);
    182     }
    183 
    184     private BufferedImage getImageFromCache(String cacheKey) {
    185         try {
    186             BufferedImageCacheEntry image = imgCache.get(cacheKey);
    187             if (image == null || image.getImage() == null) {
    188                 return null;
    189             }
    190             Logging.trace("{0} is in cache :-)", cacheKey);
    191             return image.getImage();
    192         } catch (IOException e) {
    193             throw new UncheckedIOException(e);
    194         }
    195159    }
    196160
     
    217181            resizeMode = ImageResizeMode.BOUNDED;
    218182        }
    219         final String cacheKey = String.format(Locale.ROOT, "%s--%s--%d--%d",
    220                 this.cacheKey, resizeMode.name(), dim.width, dim.height);
    221         BufferedImage img = getImageFromCache(cacheKey);
     183        final int cacheKey = resizeMode.cacheKey(dim);
     184        BufferedImage img = imgCache.get(cacheKey);
    222185        if (img == null) {
    223             if (svgSupplier != null) {
    224                 svg = svgSupplier.get();
    225                 Logging.trace("{0} is not in cache :-(", cacheKey);
    226                 svgSupplier = null;
    227             }
    228186            if (svg != null) {
    229187                img = ImageProvider.createImageFromSvg(svg, dim, resizeMode);
     
    253211                disabledIcon.paintIcon(new JPanel(), img.getGraphics(), 0, 0);
    254212            }
    255             if (img == null) {
    256                 return null;
    257             }
    258             BufferedImageCacheEntry cacheEntry = BufferedImageCacheEntry.pngEncoded(img);
    259             Logging.trace("Storing {0} ({1} bytes) in cache...", cacheKey, cacheEntry.getContent().length);
    260             imgCache.put(cacheKey, cacheEntry);
     213            imgCache.put(cacheKey, img);
    261214        }
    262215
     
    298251    }
    299252
    300     static String statistics() {
    301         return String.format("ImageResource cache: [%s]", imgCache.getStatistics());
    302     }
    303 
    304253    @Override
    305254    public String toString() {
Note: See TracChangeset for help on using the changeset viewer.