Changeset 10588 in josm for trunk/src


Ignore:
Timestamp:
2016-07-22T23:03:22+02:00 (8 years ago)
Author:
Don-vip
Message:

see #11390, fix #13120 - Use a new memory manager for imagery layers (patch by michael2402) - gsoc-core - requires java 8

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
2 edited

Legend:

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

    r10576 r10588  
    9090import org.openstreetmap.josm.io.WMSLayerImporter;
    9191import org.openstreetmap.josm.tools.GBC;
     92import org.openstreetmap.josm.tools.MemoryManager;
     93import org.openstreetmap.josm.tools.MemoryManager.MemoryHandle;
     94import org.openstreetmap.josm.tools.MemoryManager.NotEnoughMemoryException;
    9295
    9396/**
     
    694697                throw new IllegalArgumentException(tr("Failed to create tile source"));
    695698            }
    696             checkLayerMemoryDoesNotExceedMaximum();
    697699            // check if projection is supported
    698700            projectionChanged(null, Main.getProjection());
     
    703705    @Override
    704706    protected LayerPainter createMapViewPainter(MapViewEvent event) {
    705         return new CompatibilityModeLayerPainter() {
    706             @Override
    707             public void detachFromMapView(MapViewEvent event) {
    708                 event.getMapView().removeMouseListener(adapter);
    709                 MapView.removeZoomChangeListener(AbstractTileSourceLayer.this);
    710                 super.detachFromMapView(event);
    711             }
    712         };
     707        return new TileSourcePainter();
    713708    }
    714709
     
    732727            add(new JMenuItem(new ShowTileInfoAction()));
    733728        }
    734     }
    735 
    736     @Override
    737     protected long estimateMemoryUsage() {
    738         return 4L * tileSource.getTileSize() * tileSource.getTileSize() * estimateTileCacheSize();
    739729    }
    740730
     
    19401930        adjustAction.destroy();
    19411931    }
     1932
     1933    private class TileSourcePainter extends CompatibilityModeLayerPainter {
     1934        /**
     1935         * The memory handle that will hold our tile source.
     1936         */
     1937        private MemoryHandle<?> memory;
     1938
     1939        @Override
     1940        public void paint(MapViewGraphics graphics) {
     1941            allocateCacheMemory();
     1942            if (memory != null) {
     1943                super.paint(graphics);
     1944            }
     1945        }
     1946
     1947        private void allocateCacheMemory() {
     1948            if (memory == null) {
     1949                MemoryManager manager = MemoryManager.getInstance();
     1950                if (manager.isAvailable(getEstimatedCacheSize())) {
     1951                    try {
     1952                        memory = manager.allocateMemory("tile source layer", getEstimatedCacheSize(), () -> new Object());
     1953                    } catch (NotEnoughMemoryException e) {
     1954                        Main.warn("Could not allocate tile source memory", e);
     1955                    }
     1956                }
     1957            }
     1958        }
     1959
     1960        protected long getEstimatedCacheSize() {
     1961            return 4L * tileSource.getTileSize() * tileSource.getTileSize() * estimateTileCacheSize();
     1962        }
     1963
     1964        @Override
     1965        public void detachFromMapView(MapViewEvent event) {
     1966            event.getMapView().removeMouseListener(adapter);
     1967            MapView.removeZoomChangeListener(AbstractTileSourceLayer.this);
     1968            super.detachFromMapView(event);
     1969            if (memory != null) {
     1970                memory.free();
     1971            }
     1972        }
     1973    }
    19421974}
  • trunk/src/org/openstreetmap/josm/gui/layer/Layer.java

    r10458 r10588  
    159159     * Note that Main.map is null as long as no layer has been added, so do
    160160     * not execute code in the constructor, that assumes Main.map.mapView is
    161      * not null. Instead override this method.
    162      *
    163      * This implementation provides check, if JOSM will be able to use Layer. Layers
    164      * using a lot of memory, which do know in advance, how much memory they use, should
    165      * override {@link #estimateMemoryUsage() estimateMemoryUsage} method and give a hint.
    166      *
    167      * This allows for preemptive warning message for user, instead of failing later on
    168      *
    169      * Remember to call {@code super.hookUpMapView()} when overriding this method
     161     * not null.
     162     *
     163     * If you need to execute code when this layer is added to the map view, use
     164     * {@link #attachToMapView(org.openstreetmap.josm.gui.layer.MapViewPaintable.MapViewEvent)}
    170165     */
    171166    public void hookUpMapView() {
    172         checkLayerMemoryDoesNotExceedMaximum();
    173     }
    174 
    175     /**
    176      * Checks that the memory required for the layers is no greather than the max memory.
    177      */
    178     protected static void checkLayerMemoryDoesNotExceedMaximum() {
    179         // calculate total memory needed for all layers
    180         long memoryBytesRequired = 50L * 1024L * 1024L; // assumed minimum JOSM memory footprint
    181         for (Layer layer: Main.getLayerManager().getLayers()) {
    182             memoryBytesRequired += layer.estimateMemoryUsage();
    183         }
    184         if (memoryBytesRequired > Runtime.getRuntime().maxMemory()) {
    185             throw new IllegalArgumentException(
    186                     tr("To add another layer you need to allocate at least {0,number,#}MB memory to JOSM using -Xmx{0,number,#}M "
    187                             + "option (see http://forum.openstreetmap.org/viewtopic.php?id=25677).\n"
    188                             + "Currently you have {1,number,#}MB memory allocated for JOSM",
    189                             memoryBytesRequired / 1024 / 1024, Runtime.getRuntime().maxMemory() / 1024 / 1024));
    190         }
    191167    }
    192168
     
    589565    /**
    590566     * @return bytes that the tile will use. Needed for resource management
    591      */
     567     * @deprecated Not used any more.
     568     */
     569    @Deprecated
    592570    protected long estimateMemoryUsage() {
    593571        return 0;
Note: See TracChangeset for help on using the changeset viewer.