- Timestamp:
- 2016-07-22T23:03:22+02:00 (8 years ago)
- 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 90 90 import org.openstreetmap.josm.io.WMSLayerImporter; 91 91 import org.openstreetmap.josm.tools.GBC; 92 import org.openstreetmap.josm.tools.MemoryManager; 93 import org.openstreetmap.josm.tools.MemoryManager.MemoryHandle; 94 import org.openstreetmap.josm.tools.MemoryManager.NotEnoughMemoryException; 92 95 93 96 /** … … 694 697 throw new IllegalArgumentException(tr("Failed to create tile source")); 695 698 } 696 checkLayerMemoryDoesNotExceedMaximum();697 699 // check if projection is supported 698 700 projectionChanged(null, Main.getProjection()); … … 703 705 @Override 704 706 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(); 713 708 } 714 709 … … 732 727 add(new JMenuItem(new ShowTileInfoAction())); 733 728 } 734 }735 736 @Override737 protected long estimateMemoryUsage() {738 return 4L * tileSource.getTileSize() * tileSource.getTileSize() * estimateTileCacheSize();739 729 } 740 730 … … 1940 1930 adjustAction.destroy(); 1941 1931 } 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 } 1942 1974 } -
trunk/src/org/openstreetmap/josm/gui/layer/Layer.java
r10458 r10588 159 159 * Note that Main.map is null as long as no layer has been added, so do 160 160 * 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)} 170 165 */ 171 166 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 layers180 long memoryBytesRequired = 50L * 1024L * 1024L; // assumed minimum JOSM memory footprint181 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 }191 167 } 192 168 … … 589 565 /** 590 566 * @return bytes that the tile will use. Needed for resource management 591 */ 567 * @deprecated Not used any more. 568 */ 569 @Deprecated 592 570 protected long estimateMemoryUsage() { 593 571 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.