1 18 package org.apache.batik.ext.awt.image.rendered; 19 20 import java.awt.Point ; 21 import java.awt.image.Raster ; 22 import java.lang.ref.SoftReference ; 23 import java.util.HashMap ; 24 25 import org.apache.batik.util.CleanerThread; 26 import org.apache.batik.util.HaltingThread; 27 28 public class TileMap implements TileStore { 29 private static final boolean DEBUG = false; 30 private static final boolean COUNT = false; 31 32 private HashMap rasters=new HashMap (); 33 34 static class TileMapLRUMember extends TileLRUMember { 35 public Point pt; 36 public SoftReference parent; 37 38 class RasterSoftRef extends CleanerThread.SoftReferenceCleared { 39 RasterSoftRef(Object o) { super(o); } 40 public void cleared() { 41 if (DEBUG) System.err.println("Cleaned: " + this); 42 TileMap tm = (TileMap)parent.get(); 43 if (tm != null) 44 tm.rasters.remove(pt); 45 } 46 } 47 48 TileMapLRUMember(TileMap parent, Point pt, Raster ras) { 49 super(ras); 50 this.parent = new SoftReference (parent); 51 this.pt = pt; 52 } 53 54 public void setRaster(Raster ras) { 55 hRaster = ras; 56 wRaster = new RasterSoftRef(ras); 57 } 58 } 59 60 private TileGenerator source = null; 61 private LRUCache cache = null; 62 63 public TileMap(TileGenerator source, 64 LRUCache cache) { 65 this.cache = cache; 66 this.source = source; 67 } 68 69 public void setTile(int x, int y, Raster ras) { 70 Point pt = new Point (x, y); 71 72 if (ras == null) { 73 Object o = rasters.remove(pt); 75 if (o != null) 76 cache.remove((TileMapLRUMember)o); 77 return; 78 } 79 80 Object o = rasters.get(pt); 81 TileMapLRUMember item; 82 if (o == null) { 83 item = new TileMapLRUMember(this, pt, ras); 84 rasters.put(pt, item); 85 } else { 86 item = (TileMapLRUMember)o; 87 item.setRaster(ras); 88 } 89 90 cache.add(item); 91 if (DEBUG) System.out.println("Setting: (" + x + ", " + y + ")"); 92 } 93 94 public Raster getTileNoCompute(int x, int y) { 97 Point pt = new Point (x, y); 98 Object o = rasters.get(pt); 99 if (o == null) 100 return null; 101 102 TileMapLRUMember item = (TileMapLRUMember)o; 103 Raster ret = item.retrieveRaster(); 104 if (ret != null) 105 cache.add(item); 106 return ret; 107 } 108 109 public Raster getTile(int x, int y) { 110 if (DEBUG) System.out.println("Fetching: (" + (x) + ", " + 111 (y) + ")"); 112 if (COUNT) synchronized (TileMap.class) { requests++; } 113 114 Raster ras = null; 115 Point pt = new Point (x, y); 116 Object o = rasters.get(pt); 117 TileMapLRUMember item = null; 118 if (o != null) { 119 item = (TileMapLRUMember)o; 120 ras = item.retrieveRaster(); 121 } 122 123 if (ras == null) { 124 if (DEBUG) System.out.println("Generating: ("+(x)+", "+ 125 (y) + ")"); 126 if (COUNT) synchronized (TileMap.class) { misses++; } 127 ras = source.genTile(x, y); 128 129 if (HaltingThread.hasBeenHalted()) 133 return ras; 134 135 if (item != null) 136 item.setRaster(ras); 137 else { 138 item = new TileMapLRUMember(this, pt, ras); 139 rasters.put(pt, item); 140 } 141 } 142 143 cache.add(item); 145 146 return ras; 147 } 148 149 static int requests; 150 static int misses; 151 } 152 | Popular Tags |