Changeset 16186 in josm


Ignore:
Timestamp:
2020-03-21T20:25:18+01:00 (4 years ago)
Author:
Don-vip
Message:

fix #18871 - fix performance regression - padded icons requests made unnecessary costly rescale operations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/MapImage.java

    r16153 r16186  
    1212import java.util.concurrent.CompletableFuture;
    1313import java.util.concurrent.ExecutionException;
     14import java.util.function.Consumer;
    1415
    1516import javax.swing.ImageIcon;
     
    118119    /**
    119120     * Get the image resource associated with this MapImage object.
    120      * This method blocks until the image has been loaded.
     121     * This method blocks until the image resource has been loaded.
    121122     * @return the image resource
    122123     */
     
    124125        if (imageResource == null) {
    125126            try {
    126                 // load and wait for the image
    127                 loadImage().get();
     127                // load and wait for the image resource
     128                loadImageResource().get();
    128129            } catch (ExecutionException | InterruptedException e) {
    129130                Logging.warn(e);
     
    165166    }
    166167
    167     private CompletableFuture<Void> loadImage() {
     168    private CompletableFuture<Void> load(Consumer<? super ImageResource> action) {
    168169        return new ImageProvider(name)
    169170                .setDirs(MapPaintStyles.getIconSourceDirs(source))
     
    172173                .setInArchiveDir(source.getZipEntryDirName())
    173174                .setOptional(true)
    174                 .getResourceAsync(result -> {
    175                     synchronized (this) {
    176                         imageResource = result;
    177                         if (result == null) {
    178                             source.logWarning(tr("Failed to locate image ''{0}''", name));
    179                             ImageIcon noIcon = MapPaintStyles.getNoIconIcon(source);
    180                             img = noIcon == null ? null : noIcon.getImage();
    181                         } else {
    182                             img = rescale(result.getImageIcon(new Dimension(width, height)).getImage());
    183                         }
    184                         if (temporary) {
    185                             disabledImgCache = null;
    186                             MapView mapView = MainApplication.getMap().mapView;
    187                             mapView.preferenceChanged(null); // otherwise repaint is ignored, because layer hasn't changed
    188                             mapView.repaint();
    189                         }
    190                         temporary = false;
    191                     }
     175                .getResourceAsync(action);
     176    }
     177
     178    /**
     179     * Loads image resource and actual rescaled image.
     180     * @return the future of the requested image
     181     * @see #loadImageResource
     182     */
     183    private CompletableFuture<Void> loadImage() {
     184        return load(result -> {
     185            synchronized (this) {
     186                imageResource = result;
     187                if (result == null) {
     188                    source.logWarning(tr("Failed to locate image ''{0}''", name));
     189                    ImageIcon noIcon = MapPaintStyles.getNoIconIcon(source);
     190                    img = noIcon == null ? null : noIcon.getImage();
     191                } else {
     192                    img = rescale(result.getImageIcon(new Dimension(width, height)).getImage());
    192193                }
    193         );
     194                if (temporary) {
     195                    disabledImgCache = null;
     196                    MapView mapView = MainApplication.getMap().mapView;
     197                    mapView.preferenceChanged(null); // otherwise repaint is ignored, because layer hasn't changed
     198                    mapView.repaint();
     199                }
     200                temporary = false;
     201            }
     202        });
     203    }
     204
     205    /**
     206     * Loads image resource only.
     207     * @return the future of the requested image resource
     208     * @see #loadImage
     209     */
     210    private CompletableFuture<Void> loadImageResource() {
     211        return load(result -> {
     212            synchronized (this) {
     213                imageResource = result;
     214                if (result == null) {
     215                    source.logWarning(tr("Failed to locate image ''{0}''", name));
     216                }
     217            }
     218        });
    194219    }
    195220
Note: See TracChangeset for help on using the changeset viewer.