1 18 package org.apache.batik.ext.awt.image.rendered; 19 20 import java.awt.image.Raster ; 21 22 import org.apache.batik.util.HaltingThread; 23 24 29 30 public class TileGrid implements TileStore { 31 private static final boolean DEBUG = false; 32 private static final boolean COUNT = false; 33 34 private int xSz, ySz; 35 private int minTileX, minTileY; 36 private TileLRUMember [][] rasters=null; 37 private TileGenerator source = null; 38 private LRUCache cache = null; 39 40 public TileGrid(int minTileX, int minTileY, 41 int xSz, int ySz, 42 TileGenerator source, 43 LRUCache cache) { 44 this.cache = cache; 45 this.source = source; 46 this.minTileX = minTileX; 47 this.minTileY = minTileY; 48 this.xSz = xSz; 49 this.ySz = ySz; 50 51 rasters = new TileLRUMember[ySz][]; 52 } 53 54 public void setTile(int x, int y, Raster ras) { 55 x-= minTileX; 56 y-= minTileY; 57 if ((x<0) || (x>=xSz)) return; 58 if ((y<0) || (y>=ySz)) return; 59 60 TileLRUMember [] row = rasters[y]; 61 TileLRUMember item; 62 if (ras == null) { 63 if (row == null) return; 65 item = row[x]; 66 if (item == null) return; 67 68 row[x] = null; 69 cache.remove(item); 70 return; 71 } 72 73 if (row != null) { 74 item = row[x]; 75 if (item == null) { 76 item = new TileLRUMember(); 77 row[x] = item; 78 } 79 } else { 80 row = new TileLRUMember[xSz]; 81 item = new TileLRUMember(); 82 row[x] = item; 83 rasters[y] = row; 84 } 85 item.setRaster(ras); 86 87 cache.add(item); 88 89 if (DEBUG) System.out.println("Setting: (" + (x+minTileX) + ", " + 90 (y+minTileY) + ")"); 91 } 92 93 public Raster getTileNoCompute(int x, int y) { 96 x-=minTileX; 97 y-=minTileY; 98 if ((x<0) || (x>=xSz)) return null; 99 if ((y<0) || (y>=ySz)) return null; 100 101 TileLRUMember [] row = rasters[y]; 102 if (row == null) 103 return null; 104 TileLRUMember item = row[x]; 105 if (item == null) 106 return null; 107 Raster ret = item.retrieveRaster(); 108 if (ret != null) 109 cache.add(item); 110 return ret; 111 } 112 113 public Raster getTile(int x, int y) { 114 x-=minTileX; 115 y-=minTileY; 116 if ((x<0) || (x>=xSz)) return null; 117 if ((y<0) || (y>=ySz)) return null; 118 119 if (DEBUG) System.out.println("Fetching: (" + (x+minTileX) + ", " + 120 (y+minTileY) + ")"); 121 if (COUNT) synchronized (TileGrid.class) { requests++; } 122 123 Raster ras = null; 124 TileLRUMember [] row = rasters[y]; 125 TileLRUMember item = null; 126 if (row != null) { 127 item = row[x]; 128 if (item != null) 129 ras = item.retrieveRaster(); 130 else { 131 item = new TileLRUMember(); 132 row[x] = item; 133 } 134 } else { 135 row = new TileLRUMember[xSz]; 136 rasters[y] = row; 137 item = new TileLRUMember(); 138 row[x] = item; 139 } 140 141 if (ras == null) { 142 if (DEBUG) System.out.println("Generating: ("+(x+minTileX)+", "+ 143 (y+minTileY) + ")"); 144 if (COUNT) synchronized (TileGrid.class) { misses++; } 145 ras = source.genTile(x+minTileX, y+minTileY); 146 147 if (HaltingThread.hasBeenHalted()) 151 return ras; 152 153 item.setRaster(ras); 154 } 155 156 cache.add(item); 158 159 return ras; 160 } 161 162 static int requests; 163 static int misses; 164 } 165 | Popular Tags |