Ignore:
Timestamp:
2026-11-22T16:16:43+01:00 (4 hours ago)
Author:
GerdP
Message:

fix #24842: Reuse the previous frame when panning at constant zoom

  • drastically improve performance when panning large datasets (patch by mistenman)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/MapView.java

    r19592 r19701  
    5454import org.openstreetmap.josm.data.osm.visitor.paint.Rendering;
    5555import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
     56import org.openstreetmap.josm.data.projection.Projection;
    5657import org.openstreetmap.josm.data.projection.ProjectionRegistry;
    5758import org.openstreetmap.josm.gui.MapViewState.MapViewRectangle;
    5859import org.openstreetmap.josm.gui.autofilter.AutoFilterManager;
    5960import org.openstreetmap.josm.gui.datatransfer.OsmTransferHandler;
     61import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
    6062import org.openstreetmap.josm.gui.layer.Layer;
    6163import org.openstreetmap.josm.gui.layer.LayerManager;
    … …  
    237239    private transient BufferedImage nonChangedLayersBuffer;
    238240    private transient BufferedImage offscreenBuffer;
    239     // Layers that wasn't changed since last paint
    240     private final transient List<Layer> nonChangedLayers = new ArrayList<>();
    241241    private int lastViewID;
     242    private double lastPaintedScale;
     243    private EastNorth lastPaintedCenter;
     244    private Projection lastPaintedProjection;
     245    private List<Layer> lastPaintedLayers;
     246    // Composite baked without labels during a pan.
     247    private transient boolean panLabelsHidden;
    242248    private final AtomicBoolean paintPreferencesChanged = new AtomicBoolean(true);
    243249    private Rectangle lastClipBounds = new Rectangle();
    … …  
    490496            BugReport.intercept(t).put("layer", layer).warn();
    491497        }
     498    }
     499
     500    /**
     501     * Determines if a pan is in progress.
     502     * @return {@code true} if a pan is in progress
     503     * @since 19701
     504     */
     505    public boolean isPanning() {
     506        return mapMover != null && mapMover.movementInProgress();
    492507    }
    493508
    … …  
    552567        }
    553568
    554         boolean canUseBuffer = !paintPreferencesChanged.getAndSet(false)
    555                 && nonChangedLayers.size() <= nonChangedLayersCount
    556                 && lastViewID == getViewID()
    557                 && lastClipBounds.contains(g.getClipBounds())
    558                 && nonChangedLayers.equals(visibleLayers.subList(0, nonChangedLayers.size()));
    559 
     569        boolean paintPrefsChanged = paintPreferencesChanged.getAndSet(false);
     570        boolean sameLayerSet = lastPaintedLayers != null && lastPaintedLayers.equals(visibleLayers);
     571        int viewID = getViewID();
     572        boolean viewMoved = lastViewID != viewID;
     573        boolean panning = isPanning();
     574        boolean hideLabels = OsmDataLayer.PROPERTY_HIDE_LABELS_WHILE_DRAGGING.get();
     575        EastNorth center = getCenter();
     576        double scale = getScale();
     577        Rectangle clip = g.getClipBounds();
     578        boolean fullClip = clip.contains(0, 0, getWidth(), getHeight());
     579        // Partial clips must not rebuild the buffer or clear the label flag
     580        boolean needLabelRefresh = panLabelsHidden && !viewMoved && fullClip && !panning;
     581        if (needLabelRefresh) {
     582            panLabelsHidden = false;
     583        }
     584        boolean canUseBuffer = !paintPrefsChanged
     585                && !needLabelRefresh
     586                && !viewMoved
     587                && sameLayerSet
     588                && nonChangedLayersCount == visibleLayers.size()
     589                && lastClipBounds.contains(clip);
     590
     591        boolean canReusePan = !paintPrefsChanged
     592                && viewMoved
     593                && sameLayerSet
     594                && Utils.equalsEpsilon(scale, lastPaintedScale)
     595                && lastPaintedProjection == getProjection()
     596                && lastClipBounds.contains(0, 0, getWidth(), getHeight())
     597                && hideLabels
     598                && nonChangedLayersBuffer != null
     599                && nonChangedLayersBuffer.getWidth() == width && nonChangedLayersBuffer.getHeight() == height;
     600        int blitDX = 0;
     601        int blitDY = 0;
     602        if (canReusePan) {
     603            blitDX = (int) Math.round((lastPaintedCenter.east() - center.east()) / lastPaintedScale * uiScaleX);
     604            blitDY = (int) Math.round((center.north() - lastPaintedCenter.north()) / lastPaintedScale * uiScaleY);
     605            // Zero or off-screen blits must fall back to full paint
     606            canReusePan = (blitDX != 0 || blitDY != 0) && Math.abs(blitDX) < width && Math.abs(blitDY) < height;
     607        }
     608        if (hideLabels && (canReusePan || panning)) {
     609            panLabelsHidden = true;
     610        }
    560611        if (null == offscreenBuffer || offscreenBuffer.getWidth() != width || offscreenBuffer.getHeight() != height) {
    561612            offscreenBuffer = getAcceleratedImage(this, width, height);
    562613        }
    563614
    564         if (!canUseBuffer || nonChangedLayersBuffer == null) {
     615        if (!canReusePan && (!canUseBuffer || nonChangedLayersBuffer == null)) {
    565616            if (null == nonChangedLayersBuffer
    566617                    || nonChangedLayersBuffer.getWidth() != width || nonChangedLayersBuffer.getHeight() != height) {
    567618                nonChangedLayersBuffer = getAcceleratedImage(this, width, height);
    568619            }
    569             Graphics2D g2 = nonChangedLayersBuffer.createGraphics();
    570             g2.setClip(scaledClip);
    571             g2.setTransform(trDef);
    572             g2.setColor(PaintColors.getBackgroundColor());
    573             g2.fillRect(0, 0, width, height);
    574 
    575             for (int i = 0; i < nonChangedLayersCount; i++) {
    576                 paintLayer(visibleLayers.get(i), g2);
    577             }
    578         } else {
    579             // Maybe there were more unchanged layers then last time - draw them to buffer
    580             if (nonChangedLayers.size() != nonChangedLayersCount) {
     620            if (fullClip) {
    581621                Graphics2D g2 = nonChangedLayersBuffer.createGraphics();
    582622                g2.setClip(scaledClip);
    583623                g2.setTransform(trDef);
    584                 for (int i = nonChangedLayers.size(); i < nonChangedLayersCount; i++) {
     624                g2.setColor(PaintColors.getBackgroundColor());
     625                g2.fillRect(0, 0, width, height);
     626
     627                for (int i = 0; i < nonChangedLayersCount; i++) {
    585628                    paintLayer(visibleLayers.get(i), g2);
    586629                }
    587             }
    588         }
    589 
    590         nonChangedLayers.clear();
    591         if (nonChangedLayersCount > 0)
    592             nonChangedLayers.addAll(visibleLayers.subList(0, nonChangedLayersCount));
    593         lastViewID = getViewID();
    594         lastClipBounds = g.getClipBounds();
     630                g2.dispose();
     631            }
     632        }
     633
     634        lastViewID = viewID;
     635        lastClipBounds = clip;
     636        lastPaintedScale = scale;
     637        lastPaintedCenter = center;
     638        lastPaintedProjection = getProjection();
     639        lastPaintedLayers = new ArrayList<>(visibleLayers);
    595640
    596641        Graphics2D tempG = offscreenBuffer.createGraphics();
    597642        tempG.setClip(scaledClip);
    598643        tempG.setTransform(new AffineTransform());
    599         tempG.drawImage(nonChangedLayersBuffer, 0, 0, null);
     644        if (canReusePan) {
     645            tempG.drawImage(nonChangedLayersBuffer, blitDX, blitDY, null);
     646            if (blitDX != 0) {
     647                paintPanStrip(tempG, blitDX > 0 ? 0 : width + blitDX, 0,
     648                        Math.abs(blitDX), height, visibleLayers, trDef);
     649            }
     650            if (blitDY != 0) {
     651                paintPanStrip(tempG, 0, blitDY > 0 ? 0 : height + blitDY,
     652                        width, Math.abs(blitDY), visibleLayers, trDef);
     653            }
     654            tempG.setClip(scaledClip);
     655        } else {
     656            tempG.drawImage(nonChangedLayersBuffer, 0, 0, null);
     657        }
    600658        tempG.setTransform(trDef);
    601659
    602         for (int i = nonChangedLayersCount; i < visibleLayers.size(); i++) {
    603             paintLayer(visibleLayers.get(i), tempG);
     660        // Skip during a pan: repainting over the shifted composite would
     661        // duplicate and stack translucent layers. Changes appear after pan stops
     662        if (!canReusePan) {
     663            for (int i = nonChangedLayersCount; i < visibleLayers.size(); i++) {
     664                paintLayer(visibleLayers.get(i), tempG);
     665            }
     666        }
     667        if (fullClip && !canUseBuffer) {
     668            Graphics2D g2 = nonChangedLayersBuffer.createGraphics();
     669            g2.drawImage(offscreenBuffer, 0, 0, null);
     670            g2.dispose();
     671        }
     672
     673        // Viewport-fixed overlay which must not go into the reused composite
     674        for (Layer layer : visibleLayers) {
     675            if (layer instanceof AbstractTileSourceLayer) {
     676                ((AbstractTileSourceLayer<?>) layer).paintAttribution(tempG, this);
     677            }
    604678        }
    605679
    606680        try {
    607681            drawTemporaryLayers(tempG, getLatLonBounds(new Rectangle(
    608                     (int) Math.round(g.getClipBounds().x * uiScaleX),
    609                     (int) Math.round(g.getClipBounds().y * uiScaleY))));
     682                    (int) Math.round(clip.x * uiScaleX),
     683                    (int) Math.round(clip.y * uiScaleY))));
    610684        } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
    611685            BugReport.intercept(e).put("temporaryLayers", temporaryLayers).warn();
    … …  
    630704            playHeadMarker.paint(tempG, this);
    631705        }
     706        tempG.dispose();
    632707
    633708        try {
    … …  
    664739    }
    665740
     741    /** Icon overhang covered by the pan-strip band in logical pixels */
     742    private static final int PAN_STRIP_MARGIN = 16;
     743
     744    private void paintPanStrip(Graphics2D g, int x, int y, int w, int h, List<Layer> layers, AffineTransform trDef) {
     745        // Fill first so translucent content does not double stack
     746        int mx = (int) Math.ceil(PAN_STRIP_MARGIN * trDef.getScaleX());
     747        int my = (int) Math.ceil(PAN_STRIP_MARGIN * trDef.getScaleY());
     748        Rectangle band = new Rectangle(x - mx, y - my, w + 2 * mx, h + 2 * my);
     749        g.setTransform(new AffineTransform());
     750        g.setClip(band);
     751        g.setColor(PaintColors.getBackgroundColor());
     752        g.fillRect(band.x, band.y, band.width, band.height);
     753        g.setTransform(trDef);
     754        for (Layer layer : layers) {
     755            paintLayer(layer, g);
     756        }
     757    }
     758
    666759    private void drawTemporaryLayers(Graphics2D tempG, Bounds box) {
    667760        synchronized (temporaryLayers) {
    … …  
    824917            mapMover.destroy();
    825918        }
    826         nonChangedLayers.clear();
    827919        synchronized (temporaryLayers) {
    828920            temporaryLayers.clear();
    … …  
    830922        nonChangedLayersBuffer = null;
    831923        offscreenBuffer = null;
     924        lastPaintedLayers = null;
     925        lastPaintedCenter = null;
     926        lastPaintedProjection = null;
    832927        setTransferHandler(null);
    833928        GuiHelper.destroyComponents(this, false);
Note: See TracChangeset for help on using the changeset viewer.