Changeset 31466 in osm
- Timestamp:
- 2015-08-08T12:30:05+02:00 (9 years ago)
- Location:
- applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/MemoryTileCache.java
r31434 r31466 23 23 * Default cache size 24 24 */ 25 protected int cacheSize = 200;25 protected int cacheSize; 26 26 27 27 protected final Map<String, CacheEntry> hash; … … 36 36 */ 37 37 public MemoryTileCache() { 38 this(200); 39 } 40 41 /** 42 * Constructs a new {@code MemoryTileCache}. 43 * @param cacheSize size of the cache 44 */ 45 public MemoryTileCache(int cacheSize) { 46 this.cacheSize = cacheSize; 38 47 hash = new HashMap<>(cacheSize); 39 48 lruTiles = new CacheLinkedListElement(); 40 49 } 50 41 51 42 52 @Override -
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Tile.java
r31439 r31466 10 10 import java.util.HashMap; 11 11 import java.util.Map; 12 import java.util.concurrent.Callable; 12 13 13 14 import javax.imageio.ImageIO; … … 87 88 } 88 89 90 private static class CachedCallable<V> implements Callable<V> { 91 private V result = null; 92 private Callable<V> callable; 93 94 /** 95 * Wraps callable so it is evaluated only once 96 * @param callable to cache 97 */ 98 public CachedCallable(Callable<V> callable) { 99 this.callable = callable; 100 } 101 102 @Override 103 public synchronized V call() { 104 try { 105 if (result == null) { 106 result = callable.call(); 107 } 108 return result; 109 } catch (Exception e) { 110 // this should not happen here 111 throw new RuntimeException(e); 112 } 113 } 114 } 115 89 116 /** 90 117 * Tries to get tiles of a lower or higher zoom level (one or two level … … 93 120 */ 94 121 public void loadPlaceholderFromCache(TileCache cache) { 95 BufferedImage tmpImage = new BufferedImage(source.getTileSize(), source.getTileSize(), BufferedImage.TYPE_INT_RGB); 96 Graphics2D g = (Graphics2D) tmpImage.getGraphics(); 122 /* 123 * use LazyTask as creation of BufferedImage is very expensive 124 * this way we can avoid object creation until we're sure it's needed 125 */ 126 final CachedCallable<BufferedImage> tmpImage = new CachedCallable<>(new Callable<BufferedImage>() { 127 @Override 128 public BufferedImage call() throws Exception { 129 return new BufferedImage(source.getTileSize(), source.getTileSize(), BufferedImage.TYPE_INT_RGB); 130 } 131 }); 132 97 133 for (int zoomDiff = 1; zoomDiff < 5; zoomDiff++) { 98 134 // first we check if there are already the 2^x tiles … … 103 139 int xtileHigh = xtile << zoomDiff; 104 140 int ytileHigh = ytile << zoomDiff; 105 double scale = 1.0 / factor; 106 g.setTransform(AffineTransform.getScaleInstance(scale, scale)); 141 final double scale = 1.0 / factor; 142 143 /* 144 * use LazyTask for graphics to avoid evaluation of tmpImage, until we have 145 * something to draw 146 */ 147 CachedCallable<Graphics2D> graphics = new CachedCallable<>(new Callable<Graphics2D>() { 148 @Override 149 public Graphics2D call() throws Exception { 150 Graphics2D g = (Graphics2D) tmpImage.call().getGraphics(); 151 g.setTransform(AffineTransform.getScaleInstance(scale, scale)); 152 return g; 153 } 154 }); 155 107 156 int paintedTileCount = 0; 108 157 for (int x = 0; x < factor; x++) { … … 111 160 if (tile != null && tile.isLoaded()) { 112 161 paintedTileCount++; 113 tile.paint(g , x * source.getTileSize(), y * source.getTileSize());162 tile.paint(graphics.call(), x * source.getTileSize(), y * source.getTileSize()); 114 163 } 115 164 } 116 165 } 117 166 if (paintedTileCount == factor * factor) { 118 image = tmpImage ;167 image = tmpImage.call(); 119 168 return; 120 169 } … … 125 174 int xtileLow = xtile >> zoomDiff; 126 175 int ytileLow = ytile >> zoomDiff; 127 int factor = 1 << zoomDiff; 128 double scale = factor; 129 AffineTransform at = new AffineTransform(); 130 int translateX = (xtile % factor) * source.getTileSize(); 131 int translateY = (ytile % factor) * source.getTileSize(); 132 at.setTransform(scale, 0, 0, scale, -translateX, -translateY); 133 g.setTransform(at); 176 final int factor = 1 << zoomDiff; 177 final double scale = factor; 178 CachedCallable<Graphics2D> graphics = new CachedCallable<>(new Callable<Graphics2D>() { 179 @Override 180 public Graphics2D call() throws Exception { 181 Graphics2D g = (Graphics2D) tmpImage.call().getGraphics(); 182 AffineTransform at = new AffineTransform(); 183 int translateX = (xtile % factor) * source.getTileSize(); 184 int translateY = (ytile % factor) * source.getTileSize(); 185 at.setTransform(scale, 0, 0, scale, -translateX, -translateY); 186 g.setTransform(at); 187 return g; 188 } 189 190 }); 191 134 192 Tile tile = cache.getTile(source, xtileLow, ytileLow, zoomLow); 135 193 if (tile != null && tile.isLoaded()) { 136 tile.paint(g , 0, 0);137 image = tmpImage ;194 tile.paint(graphics.call(), 0, 0); 195 image = tmpImage.call(); 138 196 return; 139 197 }
Note:
See TracChangeset
for help on using the changeset viewer.