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

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

This experimental feature is disabled by default. Set the advanced preference jcs.cache.use_image_resource_cache=true to enable. No cache eviction for altered images is implemented at the moment.

File:
1 edited

Legend:

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

    r17144 r17364  
    954954                        continue;
    955955                    }
    956                     ir = getIfAvailableLocalURL(path, type);
     956                    ir = getIfAvailableLocalURL(subdir + name, path, type);
    957957                    if (ir != null) {
    958958                        cache.put(cacheName, ir);
     
    984984                    svg = getSvgUniverse().getDiagram(uri);
    985985                }
    986                 return svg == null ? null : new ImageResource(svg);
     986                return svg == null ? null : new ImageResource(url, 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(img);
     994                return img == null ? null : new ImageResource(url, img);
    995995            default:
    996996                throw new AssertionError("Unsupported type: " + type);
     
    10411041                    return null;
    10421042                }
    1043                 return new ImageResource(svg);
     1043                return new ImageResource(url, 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(img);
     1052                    return img == null ? null : new ImageResource(url, 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(svg);
     1127                        return svg == null ? null : new ImageResource(fullName, svg);
    11281128                    case OTHER:
    11291129                        while (size > 0) {
     
    11381138                            Logging.warn(e);
    11391139                        }
    1140                         return img == null ? null : new ImageResource(img);
     1140                        return img == null ? null : new ImageResource(fullName, 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(URL path, ImageType type) {
     1159    private static ImageResource getIfAvailableLocalURL(String cacheKey, URL path, ImageType type) {
    11601160        switch (type) {
    11611161        case SVG:
    1162             SVGDiagram svg = null;
    1163             synchronized (getSvgUniverse()) {
    1164                 try {
    1165                     URI uri = null;
     1162            return new ImageResource(cacheKey, () -> {
     1163                synchronized (getSvgUniverse()) {
    11661164                    try {
    1167                         uri = getSvgUniverse().loadSVG(path);
    1168                     } catch (InvalidPathException e) {
    1169                         Logging.error("Cannot open {0}: {1}", path, e.getMessage());
    1170                         Logging.trace(e);
     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);
     1171                        }
     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);
    11711181                    }
    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                     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);
     1182                }
     1183                return null;
     1184            });
    11841185        case OTHER:
    11851186            BufferedImage img = null;
     
    11961197                Logging.debug(e);
    11971198            }
    1198             return img == null ? null : new ImageResource(img);
     1199            return img == null ? null : new ImageResource(path.toString(), img);
    11991200        default:
    12001201            throw new AssertionError();
     
    13941395     */
    13951396    public static Image createBoundedImage(Image img, int maxSize) {
    1396         return new ImageResource(img).getImageIconBounded(new Dimension(maxSize, maxSize)).getImage();
     1397        return new ImageResource(img.toString(), img).getImageIconBounded(new Dimension(maxSize, maxSize)).getImage();
    13971398    }
    13981399
     
    19331934    }
    19341935
     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
    19351944    @Override
    19361945    public String toString() {
Note: See TracChangeset for help on using the changeset viewer.