Changeset 3116 in josm


Ignore:
Timestamp:
2010-03-11T21:01:49+01:00 (15 years ago)
Author:
jttt
Message:

Reuse offscreenBuffer if layers didn't change

Location:
trunk/src/org/openstreetmap/josm
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

    r3108 r3116  
    172172            oldHighlights.add(mouseOnExistingNode);
    173173            if(drawTargetHighlight) {
    174                 mouseOnExistingNode.highlighted = true;
     174                mouseOnExistingNode.setHighlighted(true);
    175175            }
    176176            return;
     
    189189        if (!drawTargetHighlight) return;
    190190        for (Way w : mouseOnExistingWays) {
    191             w.highlighted = true;
     191            w.setHighlighted(true);
    192192        }
    193193    }
     
    198198    private void removeHighlighting() {
    199199        for(OsmPrimitive prim : oldHighlights) {
    200             prim.highlighted = false;
     200            prim.setHighlighted(false);
    201201        }
    202202        oldHighlights = new HashSet<OsmPrimitive>();
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r3102 r3116  
    5858    // Number of open calls to beginUpdate
    5959    private int updateCount;
     60
     61    private int highlightUpdateCount;
     62
     63    /**
     64     * This method can be used to detect changes in highlight state of primitives. If highlighting was changed
     65     * then the method will return different number.
     66     * @return
     67     */
     68    public int getHighlightUpdateCount() {
     69        return highlightUpdateCount;
     70    }
    6071
    6172    /**
     
    314325     * Replies an unmodifiable collection of primitives currently selected
    315326     * in this dataset
    316      * 
     327     *
    317328     * @return unmodifiable collection of primitives
    318329     */
     
    897908    }
    898909
     910    void fireHighlightingChanged(OsmPrimitive primitive) {
     911        highlightUpdateCount++;
     912    }
     913
    899914    public void clenupDeletedPrimitives() {
    900915        if (cleanupDeleted(nodes.iterator())
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r3056 r3116  
    229229     * If allowNegativeId is not set, then id will have to be 0 (in that case new unique id will be generated) or
    230230     * positive number.
    231      * 
     231     *
    232232     * If id is not > 0 version is ignored and set to 0.
    233233     *
     
    475475     * show which ways/nodes will connect
    476476     */
    477     public volatile boolean highlighted = false;
     477    private volatile boolean highlighted = false;
    478478
    479479    private int timestamp;
     
    12881288        return dataSet != null && dataSet.isSelected(this);
    12891289    }
     1290
     1291    public void setHighlighted(boolean highlighted) {
     1292        if (this.highlighted != highlighted) {
     1293            this.highlighted = highlighted;
     1294            if (dataSet != null) {
     1295                dataSet.fireHighlightingChanged(this);
     1296            }
     1297        }
     1298    }
     1299
     1300    public boolean isHighlighted() {
     1301        return highlighted;
     1302    }
    12901303}
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/SimplePaintVisitor.java

    r2725 r3116  
    227227        if (inactive || n.isDisabled()) {
    228228            drawNode(n, inactiveColor, unselectedNodeSize, unselectedNodeRadius, fillUnselectedNode);
    229         } else if (n.highlighted) {
     229        } else if (n.isHighlighted()) {
    230230            drawNode(n, highlightColor, selectedNodeSize, selectedNodeRadius, fillSelectedNode);
    231231        } else if (ds.isSelected(n)) {
     
    304304        if (inactive || w.isDisabled()) {
    305305            wayColor = inactiveColor;
    306         } else if(w.highlighted) {
     306        } else if(w.isHighlighted()) {
    307307            wayColor = highlightColor;
    308308        } else if(ds.isSelected(w)) {
  • trunk/src/org/openstreetmap/josm/gui/MapView.java

    r3078 r3116  
    159159     * A list of all layers currently loaded.
    160160     */
    161     private ArrayList<Layer> layers = new ArrayList<Layer>();
     161    private final List<Layer> layers = new ArrayList<Layer>();
    162162    /**
    163163     * The play head marker: there is only one of these so it isn't in any specific layer
     
    180180
    181181    private BufferedImage offscreenBuffer;
     182    // Layers that wasn't changed since last paint
     183    private final List<Layer> nonChangedLayers = new ArrayList<Layer>();
    182184
    183185    public MapView() {
     
    458460            return; // no data loaded yet.
    459461
    460         // re-create offscreen-buffer if we've been resized, otherwise
    461         // just re-use it.
    462         if (null == offscreenBuffer || offscreenBuffer.getWidth() != getWidth()
    463                 || offscreenBuffer.getHeight() != getHeight()) {
    464             offscreenBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    465         }
    466 
    467         Graphics2D tempG = offscreenBuffer.createGraphics();
    468         tempG.setClip(g.getClip());
    469         tempG.setColor(PaintColors.BACKGROUND.get());
    470         tempG.fillRect(0, 0, getWidth(), getHeight());
    471 
     462        List<Layer> visibleLayers = getVisibleLayersInZOrder();
     463
     464        int nonChangedLayersCount = 0;
     465        for (Layer l: visibleLayers) {
     466            if (l.isChanged()) {
     467                break;
     468            } else {
     469                nonChangedLayersCount++;
     470            }
     471        }
     472
     473        boolean canUseBuffer = nonChangedLayers.size() <= nonChangedLayersCount;
     474        if (canUseBuffer) {
     475            for (int i=0; i<nonChangedLayers.size(); i++) {
     476                if (visibleLayers.get(i) != nonChangedLayers.get(i)) {
     477                    canUseBuffer = false;
     478                    break;
     479                }
     480            }
     481        }
     482
     483        Graphics2D tempG = (Graphics2D) g;
    472484        Bounds box = getLatLonBounds(g.getClipBounds());
    473485
    474         for (Layer l: getVisibleLayersInZOrder()) {
    475             l.paint(tempG, this, box);
    476         }
     486        if (!canUseBuffer || offscreenBuffer == null) {
     487            if (null == offscreenBuffer || offscreenBuffer.getWidth() != getWidth() || offscreenBuffer.getHeight() != getHeight()) {
     488                offscreenBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);
     489            }
     490            Graphics2D g2 = offscreenBuffer.createGraphics();
     491            g2.setColor(PaintColors.BACKGROUND.get());
     492            g2.fillRect(0, 0, getWidth(), getHeight());
     493
     494            for (int i=0; i<nonChangedLayersCount; i++) {
     495                visibleLayers.get(i).paint(g2, this, box);
     496            }
     497        } else {
     498            // Maybe there were more unchanged layers then last time - draw them to buffer
     499            if (nonChangedLayers.size() != nonChangedLayersCount) {
     500                Graphics2D g2 = offscreenBuffer.createGraphics();
     501                for (int i=nonChangedLayers.size(); i<nonChangedLayersCount; i++) {
     502                    visibleLayers.get(i).paint(g2, this, box);
     503                }
     504            }
     505        }
     506
     507        nonChangedLayers.clear();
     508        for (int i=0; i<nonChangedLayersCount; i++) {
     509            nonChangedLayers.add(visibleLayers.get(i));
     510        }
     511
     512        tempG.drawImage(offscreenBuffer, 0, 0, null);
     513
     514        for (int i=nonChangedLayersCount; i<visibleLayers.size(); i++) {
     515            visibleLayers.get(i).paint(tempG, this, box);
     516        }
     517
    477518        for (MapViewPaintable mvp : temporaryLayers) {
    478519            mvp.paint(tempG, this, box);
     
    515556        }
    516557
    517         int w = offscreenBuffer.getWidth();
    518         int h = offscreenBuffer.getHeight();
     558        int w = getWidth();
     559        int h = getHeight();
    519560
    520561        // Work around OpenJDK having problems when drawing out of bounds
     
    530571        }
    531572
    532         g.drawImage(offscreenBuffer, 0, 0, null);
    533573        super.paint(g);
    534574    }
  • trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r2801 r3116  
    679679        return n.substring(n.lastIndexOf('.')+1);
    680680    }
     681
     682    /**
     683     * Return a ID which is unique as long as viewport dimensions are the same
     684     */
     685    public int getViewID() {
     686        String x = center.east() + "_" + center.north() + "_" + scale + "_" +
     687        getWidth() + "_" + getHeight() + "_" + getProjection().toString();
     688        java.util.zip.CRC32 id = new java.util.zip.CRC32();
     689        id.update(x.getBytes());
     690        return (int)id.getValue();
     691    }
    681692}
  • trunk/src/org/openstreetmap/josm/gui/layer/Layer.java

    r2621 r3116  
    229229
    230230    /**
     231     *
     232     *
     233     * @return True if layer was changed since last paint
     234     */
     235    public boolean isChanged() {
     236        return true;
     237    }
     238
     239    /**
    231240     * The action to save a layer
    232241     *
  • trunk/src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r3078 r3116  
    4444import org.openstreetmap.josm.command.PurgePrimitivesCommand;
    4545import org.openstreetmap.josm.data.Bounds;
     46import org.openstreetmap.josm.data.SelectionChangedListener;
    4647import org.openstreetmap.josm.data.conflict.Conflict;
    4748import org.openstreetmap.josm.data.conflict.ConflictCollection;
     
    5960import org.openstreetmap.josm.data.osm.Relation;
    6061import org.openstreetmap.josm.data.osm.Way;
     62import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
     63import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter;
     64import org.openstreetmap.josm.data.osm.event.DataSetListenerAdapter.Listener;
    6165import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
    6266import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
     
    7983 * @author imi
    8084 */
    81 public class OsmDataLayer extends Layer {
     85public class OsmDataLayer extends Layer implements Listener, SelectionChangedListener {
    8286    static public final String REQUIRES_SAVE_TO_DISK_PROP = OsmDataLayer.class.getName() + ".requiresSaveToDisk";
    8387    static public final String REQUIRES_UPLOAD_TO_SERVER_PROP = OsmDataLayer.class.getName() + ".requiresUploadToServer";
     
    8589    private boolean requiresSaveToFile = false;
    8690    private boolean requiresUploadToServer = false;
     91    private boolean isChanged = true;
     92    private int highlightUpdateCount;
     93    private int viewId;
    8794
    8895    protected void setRequiresSaveToFile(boolean newValue) {
     
    199206        this.setAssociatedFile(associatedFile);
    200207        conflicts = new ConflictCollection();
     208        data.addDataSetListener(new DataSetListenerAdapter(this));
     209        DataSet.selListeners.add(this);
    201210    }
    202211
     
    215224     */
    216225    @Override public void paint(final Graphics2D g, final MapView mv, Bounds box) {
     226        isChanged = false;
     227        highlightUpdateCount = data.getHighlightUpdateCount();
     228        viewId = Main.map.mapView.getViewID();
     229
    217230        boolean active = mv.getActiveLayer() == this;
    218231        boolean inactive = !active && Main.pref.getBoolean("draw.data.inactive_color", true);
     
    711724    }
    712725
     726    @Override
     727    public boolean isChanged() {
     728        return isChanged || highlightUpdateCount != data.getHighlightUpdateCount() || viewId != Main.map.mapView.getViewID();
     729    }
     730
    713731    /**
    714732     * Initializes the layer after a successful save of OSM data to a file
     
    752770
    753771    }
     772
     773    public void processDatasetEvent(AbstractDatasetChangedEvent event) {
     774        isChanged = true;
     775    }
     776
     777    public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
     778        isChanged = true;
     779    }
    754780}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LineElemStyle.java

    r2890 r3116  
    177177        }
    178178
    179         if(w.highlighted) {
     179        if(w.isHighlighted()) {
    180180            myColor = paintSettings.getHighlightColor();
    181181        } else if (selected) {
  • trunk/src/org/openstreetmap/josm/gui/mappaint/SimpleNodeElemStyle.java

    r3083 r3116  
    2121        Node n = (Node)primitive;
    2222        String name = painter.isShowNames()?painter.getNodeName(n):null;
    23         if (n.highlighted) {
     23        if (n.isHighlighted()) {
    2424            painter.drawNode(n, settings.getHighlightColor(), settings.getSelectedNodeSize(), settings.isFillSelectedNode(), name);
    2525        } else if (selected) {
Note: See TracChangeset for help on using the changeset viewer.