KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > gvt > renderer > DynamicRenderer


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.gvt.renderer;
19
20 import java.awt.Point JavaDoc;
21 import java.awt.Rectangle JavaDoc;
22 import java.awt.RenderingHints JavaDoc;
23 import java.awt.Shape JavaDoc;
24 import java.awt.geom.AffineTransform JavaDoc;
25 import java.awt.geom.Rectangle2D JavaDoc;
26 import java.awt.image.BufferedImage JavaDoc;
27 import java.awt.image.Raster JavaDoc;
28 import java.awt.image.SampleModel JavaDoc;
29 import java.awt.image.WritableRaster JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Collection JavaDoc;
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 /**
41  * Simple implementation of the Renderer that supports dynamic updates.
42  *
43  * @author <a HREF="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
44  * @version $Id: DynamicRenderer.java,v 1.23 2005/03/27 08:58:34 cam Exp $
45  */

46 public class DynamicRenderer extends StaticRenderer {
47
48     final static int COPY_OVERHEAD = 1000;
49     final static int COPY_LINE_OVERHEAD = 10;
50
51     /**
52      * Constructs a new dynamic renderer with the specified buffer image.
53      */

54     public DynamicRenderer() {
55         super();
56     }
57
58     public DynamicRenderer(RenderingHints JavaDoc rh,
59                            AffineTransform JavaDoc at){
60         super(rh, at);
61     }
62
63     RectListManager damagedAreas;
64
65     protected CachableRed setupCache(CachableRed img) {
66         // Don't do any caching of content for dynamic case
67
return img;
68     }
69
70     public void flush(Rectangle JavaDoc r) {
71         // Since we don't cache we don't need to flush
72
return;
73     }
74
75     /**
76      * Flush a list of rectangles of cached image data.
77      */

78     public void flush(Collection JavaDoc areas) {
79         // Since we don't cache we don't need to flush
80
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             // No image to display so clear everything out...
92
workingRaster = null;
93             workingOffScreen = null;
94             workingBaseRaster = null;
95             
96             currentOffScreen = null;
97             currentBaseRaster = null;
98             currentRaster = null;
99             return;
100         }
101
102         SampleModel JavaDoc 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 JavaDoc(0,0));
114
115             workingRaster = workingBaseRaster.createWritableChild
116                 (0, 0, w, h, 0, 0, null);
117
118             workingOffScreen = new BufferedImage JavaDoc
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     /**
132      * Repaints the associated GVT tree under the list of <tt>areas</tt>.
133      *
134      * If double buffered is true and this method completes cleanly it
135      * will set the result of the repaint as the image returned by
136      * getOffscreen otherwise the old image will still be returned.
137      * If double buffered is false it is possible some effects of
138      * the failed rendering will be visible in the image returned
139      * by getOffscreen.
140      *
141      * @param devRLM regions to be repainted, in the current
142      * user space coordinate system.
143      */

144     // long lastFrame = -1;
145
public void repaint(RectListManager devRLM) {
146         if (devRLM == null)
147             return;
148
149         // long t0 = System.currentTimeMillis();
150
// if (lastFrame != -1) {
151
// System.out.println("InterFrame time: " + (t0-lastFrame));
152
// }
153
// lastFrame = t0;
154

155         CachableRed cr;
156         WritableRaster JavaDoc syncRaster;
157         WritableRaster JavaDoc copyRaster;
158
159         updateWorkingBuffers();
160         if ((rootCR == null) ||
161             (workingBaseRaster == null)) {
162             // System.err.println("RootCR: " + rootCR);
163
// System.err.println("wrkBaseRaster: " + workingBaseRaster);
164
return;
165         }
166         cr = rootCR;
167         syncRaster = workingBaseRaster;
168         copyRaster = workingRaster;
169
170         Rectangle JavaDoc srcR = rootCR.getBounds();
171         // System.out.println("RootCR: " + srcR);
172
Rectangle JavaDoc 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 JavaDoc dr = copyRaster.getBounds();
182
183         // Ensure only one thread works on baseRaster at a time...
184
synchronized (syncRaster) {
185             // System.out.println("Dynamic:");
186
if (repaintAll) {
187                 // System.out.println("Repainting All");
188
cr.copyData(copyRaster);
189             } else {
190                 java.awt.Graphics2D JavaDoc g2d = null;
191                 if (false) {
192                     BufferedImage JavaDoc tmpBI = new BufferedImage JavaDoc
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 JavaDoc iter = damagedAreas.iterator();
211                     while (iter.hasNext()) {
212                         Rectangle JavaDoc r = (Rectangle JavaDoc)iter.next();
213                         if (!dr.intersects(r)) continue;
214                         r = dr.intersection(r);
215                         // System.err.println("Copy: " + r);
216
Raster JavaDoc 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 JavaDoc(0,0,255,50));
221                             g2d.fill(r);
222                             g2d.setPaint(new java.awt.Color JavaDoc(0,0,0,50));
223                             g2d.draw(r);
224                         }
225                     }
226                 }
227
228                 Iterator JavaDoc iter = devRLM.iterator();
229                 while (iter.hasNext()) {
230                     Rectangle JavaDoc r = (Rectangle JavaDoc)iter.next();
231                     if (!dr.intersects(r)) continue;
232                     r = dr.intersection(r);
233                     
234                     // System.err.println("Render: " + r);
235
WritableRaster JavaDoc 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 JavaDoc(255,0,0,50));
240                         g2d.fill(r);
241                         g2d.setPaint(new java.awt.Color JavaDoc(0,0,0,50));
242                         g2d.draw(r);
243                     }
244                 }
245             }
246         }
247
248         if (HaltingThread.hasBeenHalted())
249             return;
250
251         // System.out.println("Dmg: " + damagedAreas);
252
// System.out.println("Areas: " + devRects);
253

254         // Swap the buffers if the rendering completed cleanly.
255
BufferedImage JavaDoc 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