1 18 package org.apache.batik.gvt.renderer; 19 20 import java.awt.Point ; 21 import java.awt.Rectangle ; 22 import java.awt.RenderingHints ; 23 import java.awt.Shape ; 24 import java.awt.geom.AffineTransform ; 25 import java.awt.geom.Rectangle2D ; 26 import java.awt.image.BufferedImage ; 27 import java.awt.image.Raster ; 28 import java.awt.image.SampleModel ; 29 import java.awt.image.WritableRaster ; 30 import java.util.Iterator ; 31 import java.util.Collection ; 32 33 import org.apache.batik.ext.awt.geom.RectListManager; 34 import org.apache.batik.ext.awt.image.GraphicsUtil; 35 import org.apache.batik.ext.awt.image.PadMode; 36 import org.apache.batik.ext.awt.image.rendered.CachableRed; 37 import org.apache.batik.ext.awt.image.rendered.PadRed; 38 import org.apache.batik.util.HaltingThread; 39 40 46 public class DynamicRenderer extends StaticRenderer { 47 48 final static int COPY_OVERHEAD = 1000; 49 final static int COPY_LINE_OVERHEAD = 10; 50 51 54 public DynamicRenderer() { 55 super(); 56 } 57 58 public DynamicRenderer(RenderingHints rh, 59 AffineTransform at){ 60 super(rh, at); 61 } 62 63 RectListManager damagedAreas; 64 65 protected CachableRed setupCache(CachableRed img) { 66 return img; 68 } 69 70 public void flush(Rectangle r) { 71 return; 73 } 74 75 78 public void flush(Collection areas) { 79 return; 81 } 82 83 protected void updateWorkingBuffers() { 84 if (rootFilter == null) { 85 rootFilter = rootGN.getGraphicsNodeRable(true); 86 rootCR = null; 87 } 88 89 rootCR = renderGNR(); 90 if (rootCR == null) { 91 workingRaster = null; 93 workingOffScreen = null; 94 workingBaseRaster = null; 95 96 currentOffScreen = null; 97 currentBaseRaster = null; 98 currentRaster = null; 99 return; 100 } 101 102 SampleModel sm = rootCR.getSampleModel(); 103 int w = offScreenWidth; 104 int h = offScreenHeight; 105 106 if ((workingBaseRaster == null) || 107 (workingBaseRaster.getWidth() < w) || 108 (workingBaseRaster.getHeight() < h)) { 109 110 sm = sm.createCompatibleSampleModel(w, h); 111 112 workingBaseRaster 113 = Raster.createWritableRaster(sm, new Point (0,0)); 114 115 workingRaster = workingBaseRaster.createWritableChild 116 (0, 0, w, h, 0, 0, null); 117 118 workingOffScreen = new BufferedImage 119 (rootCR.getColorModel(), 120 workingRaster, 121 rootCR.getColorModel().isAlphaPremultiplied(), null); 122 } 123 124 if (!isDoubleBuffered) { 125 currentOffScreen = workingOffScreen; 126 currentBaseRaster = workingBaseRaster; 127 currentRaster = workingRaster; 128 } 129 } 130 131 144 public void repaint(RectListManager devRLM) { 146 if (devRLM == null) 147 return; 148 149 155 CachableRed cr; 156 WritableRaster syncRaster; 157 WritableRaster copyRaster; 158 159 updateWorkingBuffers(); 160 if ((rootCR == null) || 161 (workingBaseRaster == null)) { 162 return; 165 } 166 cr = rootCR; 167 syncRaster = workingBaseRaster; 168 copyRaster = workingRaster; 169 170 Rectangle srcR = rootCR.getBounds(); 171 Rectangle dstR = workingRaster.getBounds(); 173 if ((dstR.x < srcR.x) || 174 (dstR.y < srcR.y) || 175 (dstR.x+dstR.width > srcR.x+srcR.width) || 176 (dstR.y+dstR.height > srcR.y+srcR.height)) 177 cr = new PadRed(cr, dstR, PadMode.ZERO_PAD, null); 178 179 boolean repaintAll = false; 180 181 Rectangle dr = copyRaster.getBounds(); 182 183 synchronized (syncRaster) { 185 if (repaintAll) { 187 cr.copyData(copyRaster); 189 } else { 190 java.awt.Graphics2D g2d = null; 191 if (false) { 192 BufferedImage tmpBI = new BufferedImage 193 (workingOffScreen.getColorModel(), 194 copyRaster.createWritableTranslatedChild(0, 0), 195 workingOffScreen.isAlphaPremultiplied(), null); 196 g2d = GraphicsUtil.createGraphics(tmpBI); 197 g2d.translate(-copyRaster.getMinX(), 198 -copyRaster.getMinY()); 199 } 200 201 202 if ((isDoubleBuffered) && 203 (currentRaster != null) && 204 (damagedAreas != null)) { 205 damagedAreas.subtract(devRLM, COPY_OVERHEAD, 206 COPY_LINE_OVERHEAD); 207 damagedAreas.mergeRects(COPY_OVERHEAD, 208 COPY_LINE_OVERHEAD); 209 210 Iterator iter = damagedAreas.iterator(); 211 while (iter.hasNext()) { 212 Rectangle r = (Rectangle )iter.next(); 213 if (!dr.intersects(r)) continue; 214 r = dr.intersection(r); 215 Raster src = currentRaster.createWritableChild 217 (r.x, r.y, r.width, r.height, r.x, r.y, null); 218 GraphicsUtil.copyData(src, copyRaster); 219 if (g2d != null) { 220 g2d.setPaint(new java.awt.Color (0,0,255,50)); 221 g2d.fill(r); 222 g2d.setPaint(new java.awt.Color (0,0,0,50)); 223 g2d.draw(r); 224 } 225 } 226 } 227 228 Iterator iter = devRLM.iterator(); 229 while (iter.hasNext()) { 230 Rectangle r = (Rectangle )iter.next(); 231 if (!dr.intersects(r)) continue; 232 r = dr.intersection(r); 233 234 WritableRaster dst = copyRaster.createWritableChild 236 (r.x, r.y, r.width, r.height, r.x, r.y, null); 237 cr.copyData(dst); 238 if (g2d != null) { 239 g2d.setPaint(new java.awt.Color (255,0,0,50)); 240 g2d.fill(r); 241 g2d.setPaint(new java.awt.Color (0,0,0,50)); 242 g2d.draw(r); 243 } 244 } 245 } 246 } 247 248 if (HaltingThread.hasBeenHalted()) 249 return; 250 251 254 BufferedImage tmpBI = workingOffScreen; 256 257 workingBaseRaster = currentBaseRaster; 258 workingRaster = currentRaster; 259 workingOffScreen = currentOffScreen; 260 261 currentRaster = copyRaster; 262 currentBaseRaster = syncRaster; 263 currentOffScreen = tmpBI; 264 265 damagedAreas = devRLM; 266 } 267 } 268 | Popular Tags |