author | tdv |
Tue, 22 Jul 2008 11:24:32 -0700 | |
changeset 888 | c7009cf0001f |
parent 887 | 0aab8d3fa11a |
child 3105 | b5931be76fbc |
child 3084 | 67ca55732362 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
888
c7009cf0001f
6728492: typo in copyrights in some files touched by the d3d pipeline port
tdv
parents:
887
diff
changeset
|
2 |
* Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
package javax.swing; |
|
26 |
||
27 |
import java.applet.Applet; |
|
28 |
import java.awt.*; |
|
29 |
import java.awt.event.*; |
|
30 |
import java.awt.image.*; |
|
31 |
import java.lang.reflect.*; |
|
32 |
import java.lang.ref.WeakReference; |
|
33 |
import java.security.AccessController; |
|
34 |
import java.util.*; |
|
35 |
import java.util.logging.*; |
|
36 |
import sun.awt.SubRegionShowable; |
|
37 |
import sun.java2d.SunGraphics2D; |
|
38 |
import sun.security.action.GetPropertyAction; |
|
39 |
||
40 |
/** |
|
41 |
* A PaintManager implementation that uses a BufferStrategy for |
|
42 |
* rendering. |
|
43 |
* |
|
44 |
* @author Scott Violet |
|
45 |
*/ |
|
46 |
class BufferStrategyPaintManager extends RepaintManager.PaintManager { |
|
47 |
// |
|
48 |
// All drawing is done to a BufferStrategy. At the end of painting |
|
49 |
// (endPaint) the region that was painted is flushed to the screen |
|
50 |
// (using BufferStrategy.show). |
|
51 |
// |
|
52 |
// PaintManager.show is overriden to show directly from the |
|
53 |
// BufferStrategy (when using blit), if successful true is |
|
54 |
// returned and a paint event will not be generated. To avoid |
|
55 |
// showing from the buffer while painting a locking scheme is |
|
56 |
// implemented. When beginPaint is invoked the field painting is |
|
57 |
// set to true. If painting is true and show is invoked we |
|
58 |
// immediately return false. This is done to avoid blocking the |
|
59 |
// toolkit thread while painting happens. In a similar way when |
|
60 |
// show is invoked the field showing is set to true, beginPaint |
|
61 |
// will then block until showing is true. This scheme ensures we |
|
62 |
// only ever have one thread using the BufferStrategy and it also |
|
63 |
// ensures the toolkit thread remains as responsive as possible. |
|
64 |
// |
|
65 |
// If we're using a flip strategy the contents of the backbuffer may |
|
66 |
// have changed and so show only attempts to show from the backbuffer |
|
67 |
// if we get a blit strategy. |
|
68 |
// |
|
69 |
||
70 |
// |
|
71 |
// Methods used to create BufferStrategy for Applets. |
|
72 |
// |
|
73 |
private static Method COMPONENT_CREATE_BUFFER_STRATEGY_METHOD; |
|
74 |
private static Method COMPONENT_GET_BUFFER_STRATEGY_METHOD; |
|
75 |
||
76 |
/** |
|
77 |
* Indicates whether or not we should try and get a flip buffer strategy |
|
78 |
* first, default is false. |
|
79 |
*/ |
|
80 |
private static boolean TRY_FLIP; |
|
81 |
||
82 |
private static final Logger LOGGER = Logger.getLogger( |
|
83 |
"javax.swing.BufferStrategyPaintManager"); |
|
84 |
||
85 |
/** |
|
86 |
* List of BufferInfos. We don't use a Map primarily because |
|
87 |
* there are typically only a handful of top level components making |
|
88 |
* a Map overkill. |
|
89 |
*/ |
|
90 |
private ArrayList<BufferInfo> bufferInfos; |
|
91 |
||
92 |
/** |
|
93 |
* Indicates <code>beginPaint</code> has been invoked. This is |
|
94 |
* set to true for the life of beginPaint/endPaint pair. |
|
95 |
*/ |
|
96 |
private boolean painting; |
|
97 |
/** |
|
98 |
* Indicates we're in the process of showing. All painting, on the EDT, |
|
99 |
* is blocked while this is true. |
|
100 |
*/ |
|
101 |
private boolean showing; |
|
102 |
||
103 |
// |
|
104 |
// Region that we need to flush. When beginPaint is called these are |
|
105 |
// reset and any subsequent calls to paint/copyArea then update these |
|
106 |
// fields accordingly. When endPaint is called we then try and show |
|
107 |
// the accumulated region. |
|
108 |
// These fields are in the coordinate system of the root. |
|
109 |
// |
|
110 |
private int accumulatedX; |
|
111 |
private int accumulatedY; |
|
112 |
private int accumulatedMaxX; |
|
113 |
private int accumulatedMaxY; |
|
114 |
||
115 |
// |
|
116 |
// The following fields are set by prepare |
|
117 |
// |
|
118 |
||
119 |
/** |
|
120 |
* Farthest JComponent ancestor for the current paint/copyArea. |
|
121 |
*/ |
|
122 |
private JComponent rootJ; |
|
123 |
/** |
|
124 |
* Parent Applet/Window for the current paint/copyArea |
|
125 |
*/ |
|
126 |
private Container root; |
|
127 |
/** |
|
128 |
* Location of component being painted relative to root. |
|
129 |
*/ |
|
130 |
private int xOffset; |
|
131 |
/** |
|
132 |
* Location of component being painted relative to root. |
|
133 |
*/ |
|
134 |
private int yOffset; |
|
135 |
/** |
|
136 |
* Graphics from the BufferStrategy. |
|
137 |
*/ |
|
138 |
private Graphics bsg; |
|
139 |
/** |
|
140 |
* BufferStrategy currently being used. |
|
141 |
*/ |
|
142 |
private BufferStrategy bufferStrategy; |
|
143 |
/** |
|
144 |
* BufferInfo corresponding to root. |
|
145 |
*/ |
|
146 |
private BufferInfo bufferInfo; |
|
147 |
||
148 |
/** |
|
149 |
* Set to true if the bufferInfo needs to be disposed when current |
|
150 |
* paint loop is done. |
|
151 |
*/ |
|
152 |
private boolean disposeBufferOnEnd; |
|
153 |
||
154 |
||
155 |
static { |
|
156 |
TRY_FLIP = "true".equals(AccessController.doPrivileged( |
|
157 |
new GetPropertyAction("swing.useFlipBufferStrategy", "false"))); |
|
158 |
} |
|
159 |
||
160 |
private static Method getGetBufferStrategyMethod() { |
|
161 |
if (COMPONENT_GET_BUFFER_STRATEGY_METHOD == null) { |
|
162 |
getMethods(); |
|
163 |
} |
|
164 |
return COMPONENT_GET_BUFFER_STRATEGY_METHOD; |
|
165 |
} |
|
166 |
||
167 |
private static Method getCreateBufferStrategyMethod() { |
|
168 |
if (COMPONENT_CREATE_BUFFER_STRATEGY_METHOD == null) { |
|
169 |
getMethods(); |
|
170 |
} |
|
171 |
return COMPONENT_CREATE_BUFFER_STRATEGY_METHOD; |
|
172 |
} |
|
173 |
||
174 |
private static void getMethods() { |
|
175 |
java.security.AccessController.doPrivileged( |
|
176 |
new java.security.PrivilegedAction<Object>() { |
|
177 |
public Object run() { |
|
178 |
try { |
|
179 |
COMPONENT_CREATE_BUFFER_STRATEGY_METHOD = Component.class. |
|
180 |
getDeclaredMethod("createBufferStrategy", |
|
181 |
new Class[] { int.class, |
|
182 |
BufferCapabilities.class }); |
|
183 |
COMPONENT_CREATE_BUFFER_STRATEGY_METHOD. |
|
184 |
setAccessible(true); |
|
185 |
COMPONENT_GET_BUFFER_STRATEGY_METHOD = Component.class. |
|
186 |
getDeclaredMethod("getBufferStrategy"); |
|
187 |
COMPONENT_GET_BUFFER_STRATEGY_METHOD.setAccessible(true); |
|
188 |
} catch (SecurityException e) { |
|
189 |
assert false; |
|
190 |
} catch (NoSuchMethodException nsme) { |
|
191 |
assert false; |
|
192 |
} |
|
193 |
return null; |
|
194 |
} |
|
195 |
}); |
|
196 |
} |
|
197 |
||
198 |
BufferStrategyPaintManager() { |
|
199 |
bufferInfos = new ArrayList<BufferInfo>(1); |
|
200 |
} |
|
201 |
||
202 |
// |
|
203 |
// PaintManager methods |
|
204 |
// |
|
205 |
||
206 |
/** |
|
207 |
* Cleans up any created BufferStrategies. |
|
208 |
*/ |
|
209 |
protected void dispose() { |
|
210 |
// dipose can be invoked at any random time. To avoid |
|
211 |
// threading dependancies we do the actual diposing via an |
|
212 |
// invokeLater. |
|
213 |
SwingUtilities.invokeLater(new Runnable() { |
|
214 |
public void run() { |
|
215 |
java.util.List<BufferInfo> bufferInfos; |
|
216 |
synchronized(BufferStrategyPaintManager.this) { |
|
217 |
while (showing) { |
|
218 |
try { |
|
219 |
wait(); |
|
220 |
} catch (InterruptedException ie) { |
|
221 |
} |
|
222 |
} |
|
223 |
bufferInfos = BufferStrategyPaintManager.this.bufferInfos; |
|
224 |
BufferStrategyPaintManager.this.bufferInfos = null; |
|
225 |
} |
|
226 |
dispose(bufferInfos); |
|
227 |
} |
|
228 |
}); |
|
229 |
} |
|
230 |
||
231 |
private void dispose(java.util.List<BufferInfo> bufferInfos) { |
|
232 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
233 |
LOGGER.log(Level.FINER, "BufferStrategyPaintManager disposed", |
|
234 |
new RuntimeException()); |
|
235 |
} |
|
236 |
if (bufferInfos != null) { |
|
237 |
for (BufferInfo bufferInfo : bufferInfos) { |
|
238 |
bufferInfo.dispose(); |
|
239 |
} |
|
240 |
} |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* Shows the specified region of the back buffer. This will return |
|
245 |
* true if successful, false otherwise. This is invoked on the |
|
246 |
* toolkit thread in response to an expose event. |
|
247 |
*/ |
|
248 |
public boolean show(Container c, int x, int y, int w, int h) { |
|
249 |
synchronized(this) { |
|
250 |
if (painting) { |
|
251 |
// Don't show from backbuffer while in the process of |
|
252 |
// painting. |
|
253 |
return false; |
|
254 |
} |
|
255 |
showing = true; |
|
256 |
} |
|
257 |
try { |
|
258 |
BufferInfo info = getBufferInfo(c); |
|
259 |
BufferStrategy bufferStrategy; |
|
260 |
if (info != null && !info.usingFlip && info.isInSync() && |
|
261 |
(bufferStrategy = info.getBufferStrategy(false)) != null) { |
|
262 |
SubRegionShowable bsSubRegion = |
|
263 |
(SubRegionShowable)bufferStrategy; |
|
264 |
boolean paintAllOnExpose = info.getPaintAllOnExpose(); |
|
265 |
info.setPaintAllOnExpose(false); |
|
887 | 266 |
if (bsSubRegion.showIfNotLost(x, y, (x + w), (y + h))) { |
2 | 267 |
return !paintAllOnExpose; |
268 |
} |
|
269 |
// Mark the buffer as needing to be repainted. We don't |
|
270 |
// immediately do a repaint as this method will return false |
|
271 |
// indicating a PaintEvent should be generated which will |
|
272 |
// trigger a complete repaint. |
|
273 |
bufferInfo.setContentsLostDuringExpose(true); |
|
274 |
} |
|
275 |
} |
|
276 |
finally { |
|
277 |
synchronized(this) { |
|
278 |
showing = false; |
|
279 |
notifyAll(); |
|
280 |
} |
|
281 |
} |
|
282 |
return false; |
|
283 |
} |
|
284 |
||
285 |
public boolean paint(JComponent paintingComponent, |
|
286 |
JComponent bufferComponent, Graphics g, |
|
287 |
int x, int y, int w, int h) { |
|
288 |
if (prepare(paintingComponent, true, x, y, w, h)) { |
|
289 |
if ((g instanceof SunGraphics2D) && |
|
290 |
((SunGraphics2D)g).getDestination() == root) { |
|
291 |
// BufferStrategy may have already constrained the Graphics. To |
|
292 |
// account for that we revert the constrain, then apply a |
|
293 |
// constrain for Swing on top of that. |
|
294 |
int cx = ((SunGraphics2D)bsg).constrainX; |
|
295 |
int cy = ((SunGraphics2D)bsg).constrainY; |
|
296 |
if (cx != 0 || cy != 0) { |
|
297 |
bsg.translate(-cx, -cy); |
|
298 |
} |
|
299 |
((SunGraphics2D)bsg).constrain(xOffset + cx, yOffset + cy, |
|
300 |
x + w, y + h); |
|
301 |
bsg.setClip(x, y, w, h); |
|
302 |
paintingComponent.paintToOffscreen(bsg, x, y, w, h, |
|
303 |
x + w, y + h); |
|
304 |
accumulate(xOffset + x, yOffset + y, w, h); |
|
305 |
return true; |
|
306 |
} else { |
|
307 |
// Assume they are going to eventually render to the screen. |
|
308 |
// This disables showing from backbuffer until a complete |
|
309 |
// repaint occurs. |
|
310 |
bufferInfo.setInSync(false); |
|
311 |
// Fall through to old rendering. |
|
312 |
} |
|
313 |
} |
|
314 |
// Invalid root, do what Swing has always done. |
|
315 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
316 |
LOGGER.finer("prepare failed"); |
|
317 |
} |
|
318 |
return super.paint(paintingComponent, bufferComponent, g, x, y, w, h); |
|
319 |
} |
|
320 |
||
321 |
public void copyArea(JComponent c, Graphics g, int x, int y, int w, int h, |
|
322 |
int deltaX, int deltaY, boolean clip) { |
|
323 |
// Note: this method is only called internally and we know that |
|
324 |
// g is from a heavyweight Component, so no check is necessary as |
|
325 |
// it is in paint() above. |
|
326 |
// |
|
327 |
// If the buffer isn't in sync there is no point in doing a copyArea, |
|
328 |
// it has garbage. |
|
329 |
if (prepare(c, false, 0, 0, 0, 0) && bufferInfo.isInSync()) { |
|
330 |
if (clip) { |
|
331 |
Rectangle cBounds = c.getVisibleRect(); |
|
332 |
int relX = xOffset + x; |
|
333 |
int relY = yOffset + y; |
|
334 |
bsg.clipRect(xOffset + cBounds.x, |
|
335 |
yOffset + cBounds.y, |
|
336 |
cBounds.width, cBounds.height); |
|
337 |
bsg.copyArea(relX, relY, w, h, deltaX, deltaY); |
|
338 |
} |
|
339 |
else { |
|
340 |
bsg.copyArea(xOffset + x, yOffset + y, w, h, deltaX, |
|
341 |
deltaY); |
|
342 |
} |
|
343 |
accumulate(x + xOffset + deltaX, y + yOffset + deltaY, w, h); |
|
344 |
} else { |
|
345 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
346 |
LOGGER.finer("copyArea: prepare failed or not in sync"); |
|
347 |
} |
|
348 |
// Prepare failed, or not in sync. By calling super.copyArea |
|
349 |
// we'll copy on screen. We need to flush any pending paint to |
|
350 |
// the screen otherwise we'll do a copyArea on the wrong thing. |
|
351 |
if (!flushAccumulatedRegion()) { |
|
352 |
// Flush failed, copyArea will be copying garbage, |
|
353 |
// force repaint of all. |
|
354 |
rootJ.repaint(); |
|
355 |
} else { |
|
356 |
super.copyArea(c, g, x, y, w, h, deltaX, deltaY, clip); |
|
357 |
} |
|
358 |
} |
|
359 |
} |
|
360 |
||
361 |
public void beginPaint() { |
|
362 |
synchronized(this) { |
|
363 |
painting = true; |
|
364 |
// Make sure another thread isn't attempting to show from |
|
365 |
// the back buffer. |
|
366 |
while(showing) { |
|
367 |
try { |
|
368 |
wait(); |
|
369 |
} catch (InterruptedException ie) { |
|
370 |
} |
|
371 |
} |
|
372 |
} |
|
373 |
if (LOGGER.isLoggable(Level.FINEST)) { |
|
374 |
LOGGER.finest("beginPaint"); |
|
375 |
} |
|
376 |
// Reset the area that needs to be painted. |
|
377 |
resetAccumulated(); |
|
378 |
} |
|
379 |
||
380 |
public void endPaint() { |
|
381 |
if (LOGGER.isLoggable(Level.FINEST)) { |
|
382 |
LOGGER.finest("endPaint: region " + accumulatedX + " " + |
|
383 |
accumulatedY + " " + accumulatedMaxX + " " + |
|
384 |
accumulatedMaxY); |
|
385 |
} |
|
386 |
if (painting) { |
|
387 |
if (!flushAccumulatedRegion()) { |
|
388 |
if (!isRepaintingRoot()) { |
|
389 |
repaintRoot(rootJ); |
|
390 |
} |
|
391 |
else { |
|
392 |
// Contents lost twice in a row, punt. |
|
393 |
resetDoubleBufferPerWindow(); |
|
394 |
// In case we've left junk on the screen, force a repaint. |
|
395 |
rootJ.repaint(); |
|
396 |
} |
|
397 |
} |
|
398 |
} |
|
399 |
||
400 |
BufferInfo toDispose = null; |
|
401 |
synchronized(this) { |
|
402 |
painting = false; |
|
403 |
if (disposeBufferOnEnd) { |
|
404 |
disposeBufferOnEnd = false; |
|
405 |
toDispose = bufferInfo; |
|
406 |
bufferInfos.remove(toDispose); |
|
407 |
} |
|
408 |
} |
|
409 |
if (toDispose != null) { |
|
410 |
toDispose.dispose(); |
|
411 |
} |
|
412 |
} |
|
413 |
||
414 |
/** |
|
415 |
* Renders the BufferStrategy to the screen. |
|
416 |
* |
|
417 |
* @return true if successful, false otherwise. |
|
418 |
*/ |
|
419 |
private boolean flushAccumulatedRegion() { |
|
420 |
boolean success = true; |
|
421 |
if (accumulatedX != Integer.MAX_VALUE) { |
|
422 |
SubRegionShowable bsSubRegion = (SubRegionShowable)bufferStrategy; |
|
423 |
boolean contentsLost = bufferStrategy.contentsLost(); |
|
424 |
if (!contentsLost) { |
|
425 |
bsSubRegion.show(accumulatedX, accumulatedY, |
|
426 |
accumulatedMaxX, accumulatedMaxY); |
|
427 |
contentsLost = bufferStrategy.contentsLost(); |
|
428 |
} |
|
429 |
if (contentsLost) { |
|
430 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
431 |
LOGGER.finer("endPaint: contents lost"); |
|
432 |
} |
|
433 |
// Shown region was bogus, mark buffer as out of sync. |
|
434 |
bufferInfo.setInSync(false); |
|
435 |
success = false; |
|
436 |
} |
|
437 |
} |
|
438 |
resetAccumulated(); |
|
439 |
return success; |
|
440 |
} |
|
441 |
||
442 |
private void resetAccumulated() { |
|
443 |
accumulatedX = Integer.MAX_VALUE; |
|
444 |
accumulatedY = Integer.MAX_VALUE; |
|
445 |
accumulatedMaxX = 0; |
|
446 |
accumulatedMaxY = 0; |
|
447 |
} |
|
448 |
||
449 |
/** |
|
450 |
* Invoked when the double buffering or useTrueDoubleBuffering |
|
451 |
* changes for a JRootPane. If the rootpane is not double |
|
452 |
* buffered, or true double buffering changes we throw out any |
|
453 |
* cache we may have. |
|
454 |
*/ |
|
455 |
public void doubleBufferingChanged(final JRootPane rootPane) { |
|
456 |
if ((!rootPane.isDoubleBuffered() || |
|
457 |
!rootPane.getUseTrueDoubleBuffering()) && |
|
458 |
rootPane.getParent() != null) { |
|
459 |
if (!SwingUtilities.isEventDispatchThread()) { |
|
460 |
Runnable updater = new Runnable() { |
|
461 |
public void run() { |
|
462 |
doubleBufferingChanged0(rootPane); |
|
463 |
} |
|
464 |
}; |
|
465 |
SwingUtilities.invokeLater(updater); |
|
466 |
} |
|
467 |
else { |
|
468 |
doubleBufferingChanged0(rootPane); |
|
469 |
} |
|
470 |
} |
|
471 |
} |
|
472 |
||
473 |
/** |
|
474 |
* Does the work for doubleBufferingChanged. |
|
475 |
*/ |
|
476 |
private void doubleBufferingChanged0(JRootPane rootPane) { |
|
477 |
// This will only happen on the EDT. |
|
478 |
BufferInfo info; |
|
479 |
synchronized(this) { |
|
480 |
// Make sure another thread isn't attempting to show from |
|
481 |
// the back buffer. |
|
482 |
while(showing) { |
|
483 |
try { |
|
484 |
wait(); |
|
485 |
} catch (InterruptedException ie) { |
|
486 |
} |
|
487 |
} |
|
488 |
info = getBufferInfo(rootPane.getParent()); |
|
489 |
if (painting && bufferInfo == info) { |
|
490 |
// We're in the process of painting and the user grabbed |
|
491 |
// the Graphics. If we dispose now, endPaint will attempt |
|
492 |
// to show a bogus BufferStrategy. Set a flag so that |
|
493 |
// endPaint knows it needs to dispose this buffer. |
|
494 |
disposeBufferOnEnd = true; |
|
495 |
info = null; |
|
496 |
} else if (info != null) { |
|
497 |
bufferInfos.remove(info); |
|
498 |
} |
|
499 |
} |
|
500 |
if (info != null) { |
|
501 |
info.dispose(); |
|
502 |
} |
|
503 |
} |
|
504 |
||
505 |
/** |
|
506 |
* Calculates information common to paint/copyArea. |
|
507 |
* |
|
508 |
* @return true if should use buffering per window in painting. |
|
509 |
*/ |
|
510 |
private boolean prepare(JComponent c, boolean isPaint, int x, int y, |
|
511 |
int w, int h) { |
|
512 |
if (bsg != null) { |
|
513 |
bsg.dispose(); |
|
514 |
bsg = null; |
|
515 |
} |
|
516 |
bufferStrategy = null; |
|
517 |
if (fetchRoot(c)) { |
|
518 |
boolean contentsLost = false; |
|
519 |
BufferInfo bufferInfo = getBufferInfo(root); |
|
520 |
if (bufferInfo == null) { |
|
521 |
contentsLost = true; |
|
522 |
bufferInfo = new BufferInfo(root); |
|
523 |
bufferInfos.add(bufferInfo); |
|
524 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
525 |
LOGGER.finer("prepare: new BufferInfo: " + root); |
|
526 |
} |
|
527 |
} |
|
528 |
this.bufferInfo = bufferInfo; |
|
529 |
if (!bufferInfo.hasBufferStrategyChanged()) { |
|
530 |
bufferStrategy = bufferInfo.getBufferStrategy(true); |
|
531 |
if (bufferStrategy != null) { |
|
532 |
bsg = bufferStrategy.getDrawGraphics(); |
|
533 |
if (bufferStrategy.contentsRestored()) { |
|
534 |
contentsLost = true; |
|
535 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
536 |
LOGGER.finer( |
|
537 |
"prepare: contents restored in prepare"); |
|
538 |
} |
|
539 |
} |
|
540 |
} |
|
541 |
else { |
|
542 |
// Couldn't create BufferStrategy, fallback to normal |
|
543 |
// painting. |
|
544 |
return false; |
|
545 |
} |
|
546 |
if (bufferInfo.getContentsLostDuringExpose()) { |
|
547 |
contentsLost = true; |
|
548 |
bufferInfo.setContentsLostDuringExpose(false); |
|
549 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
550 |
LOGGER.finer("prepare: contents lost on expose"); |
|
551 |
} |
|
552 |
} |
|
553 |
if (isPaint && c == rootJ && x == 0 && y == 0 && |
|
554 |
c.getWidth() == w && c.getHeight() == h) { |
|
555 |
bufferInfo.setInSync(true); |
|
556 |
} |
|
557 |
else if (contentsLost) { |
|
558 |
// We either recreated the BufferStrategy, or the contents |
|
559 |
// of the buffer strategy were restored. We need to |
|
560 |
// repaint the root pane so that the back buffer is in sync |
|
561 |
// again. |
|
562 |
bufferInfo.setInSync(false); |
|
563 |
if (!isRepaintingRoot()) { |
|
564 |
repaintRoot(rootJ); |
|
565 |
} |
|
566 |
else { |
|
567 |
// Contents lost twice in a row, punt |
|
568 |
resetDoubleBufferPerWindow(); |
|
569 |
} |
|
570 |
} |
|
571 |
return (bufferInfos != null); |
|
572 |
} |
|
573 |
} |
|
574 |
return false; |
|
575 |
} |
|
576 |
||
577 |
private boolean fetchRoot(JComponent c) { |
|
578 |
boolean encounteredHW = false; |
|
579 |
rootJ = c; |
|
580 |
root = c; |
|
581 |
xOffset = yOffset = 0; |
|
582 |
while (root != null && (!(root instanceof Window) && |
|
583 |
!(root instanceof Applet))) { |
|
584 |
xOffset += root.getX(); |
|
585 |
yOffset += root.getY(); |
|
586 |
root = root.getParent(); |
|
587 |
if (root != null) { |
|
588 |
if (root instanceof JComponent) { |
|
589 |
rootJ = (JComponent)root; |
|
590 |
} |
|
591 |
else if (!root.isLightweight()) { |
|
592 |
if (!encounteredHW) { |
|
593 |
encounteredHW = true; |
|
594 |
} |
|
595 |
else { |
|
596 |
// We've encountered two hws now and may have |
|
597 |
// a containment hierarchy with lightweights containing |
|
598 |
// heavyweights containing other lightweights. |
|
599 |
// Heavyweights poke holes in lightweight |
|
600 |
// rendering so that if we call show on the BS |
|
601 |
// (which is associated with the Window) you will |
|
602 |
// not see the contents over any child |
|
603 |
// heavyweights. If we didn't do this when we |
|
604 |
// went to show the descendants of the nested hw |
|
605 |
// you would see nothing, so, we bail out here. |
|
606 |
return false; |
|
607 |
} |
|
608 |
} |
|
609 |
} |
|
610 |
} |
|
611 |
if ((root instanceof RootPaneContainer) && |
|
612 |
(rootJ instanceof JRootPane)) { |
|
613 |
// We're in a Swing heavyeight (JFrame/JWindow...), use double |
|
614 |
// buffering if double buffering enabled on the JRootPane and |
|
615 |
// the JRootPane wants true double buffering. |
|
616 |
if (rootJ.isDoubleBuffered() && |
|
617 |
((JRootPane)rootJ).getUseTrueDoubleBuffering()) { |
|
618 |
// Whether or not a component is double buffered is a |
|
619 |
// bit tricky with Swing. This gives a good approximation |
|
620 |
// of the various ways to turn on double buffering for |
|
621 |
// components. |
|
622 |
return true; |
|
623 |
} |
|
624 |
} |
|
625 |
// Don't do true double buffering. |
|
626 |
return false; |
|
627 |
} |
|
628 |
||
629 |
/** |
|
630 |
* Turns off double buffering per window. |
|
631 |
*/ |
|
632 |
private void resetDoubleBufferPerWindow() { |
|
633 |
if (bufferInfos != null) { |
|
634 |
dispose(bufferInfos); |
|
635 |
bufferInfos = null; |
|
636 |
repaintManager.setPaintManager(null); |
|
637 |
} |
|
638 |
} |
|
639 |
||
640 |
/** |
|
641 |
* Returns the BufferInfo for the specified root or null if one |
|
642 |
* hasn't been created yet. |
|
643 |
*/ |
|
644 |
private BufferInfo getBufferInfo(Container root) { |
|
645 |
for (int counter = bufferInfos.size() - 1; counter >= 0; counter--) { |
|
646 |
BufferInfo bufferInfo = bufferInfos.get(counter); |
|
647 |
Container biRoot = bufferInfo.getRoot(); |
|
648 |
if (biRoot == null) { |
|
649 |
// Window gc'ed |
|
650 |
bufferInfos.remove(counter); |
|
651 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
652 |
LOGGER.finer("BufferInfo pruned, root null"); |
|
653 |
} |
|
654 |
} |
|
655 |
else if (biRoot == root) { |
|
656 |
return bufferInfo; |
|
657 |
} |
|
658 |
} |
|
659 |
return null; |
|
660 |
} |
|
661 |
||
662 |
private void accumulate(int x, int y, int w, int h) { |
|
663 |
accumulatedX = Math.min(x, accumulatedX); |
|
664 |
accumulatedY = Math.min(y, accumulatedY); |
|
665 |
accumulatedMaxX = Math.max(accumulatedMaxX, x + w); |
|
666 |
accumulatedMaxY = Math.max(accumulatedMaxY, y + h); |
|
667 |
} |
|
668 |
||
669 |
||
670 |
||
671 |
/** |
|
672 |
* BufferInfo is used to track the BufferStrategy being used for |
|
673 |
* a particular Component. In addition to tracking the BufferStrategy |
|
674 |
* it will install a WindowListener and ComponentListener. When the |
|
675 |
* component is hidden/iconified the buffer is marked as needing to be |
|
676 |
* completely repainted. |
|
677 |
*/ |
|
678 |
private class BufferInfo extends ComponentAdapter implements |
|
679 |
WindowListener { |
|
680 |
// NOTE: This class does NOT hold a direct reference to the root, if it |
|
681 |
// did there would be a cycle between the BufferPerWindowPaintManager |
|
682 |
// and the Window so that it could never be GC'ed |
|
683 |
// |
|
684 |
// Reference to BufferStrategy is referenced via WeakReference for |
|
685 |
// same reason. |
|
686 |
private WeakReference<BufferStrategy> weakBS; |
|
687 |
private WeakReference<Container> root; |
|
688 |
// Whether or not we're using flip bs or blit. |
|
689 |
private boolean usingFlip; |
|
690 |
// Indicates whether or not the backbuffer and display are in sync. |
|
691 |
// This is set to true when a full repaint on the rootpane is done. |
|
692 |
private boolean inSync; |
|
693 |
// Indicates the contents were lost during and expose event. |
|
694 |
private boolean contentsLostDuringExpose; |
|
695 |
// Indicates we need to generate a paint event on expose. |
|
696 |
private boolean paintAllOnExpose; |
|
697 |
||
698 |
||
699 |
public BufferInfo(Container root) { |
|
700 |
this.root = new WeakReference<Container>(root); |
|
701 |
root.addComponentListener(this); |
|
702 |
if (root instanceof Window) { |
|
703 |
((Window)root).addWindowListener(this); |
|
704 |
} |
|
705 |
} |
|
706 |
||
707 |
public void setPaintAllOnExpose(boolean paintAllOnExpose) { |
|
708 |
this.paintAllOnExpose = paintAllOnExpose; |
|
709 |
} |
|
710 |
||
711 |
public boolean getPaintAllOnExpose() { |
|
712 |
return paintAllOnExpose; |
|
713 |
} |
|
714 |
||
715 |
public void setContentsLostDuringExpose(boolean value) { |
|
716 |
contentsLostDuringExpose = value; |
|
717 |
} |
|
718 |
||
719 |
public boolean getContentsLostDuringExpose() { |
|
720 |
return contentsLostDuringExpose; |
|
721 |
} |
|
722 |
||
723 |
public void setInSync(boolean inSync) { |
|
724 |
this.inSync = inSync; |
|
725 |
} |
|
726 |
||
727 |
/** |
|
728 |
* Whether or not the contents of the buffer strategy |
|
729 |
* is in sync with the window. This is set to true when the root |
|
730 |
* pane paints all, and false when contents are lost/restored. |
|
731 |
*/ |
|
732 |
public boolean isInSync() { |
|
733 |
return inSync; |
|
734 |
} |
|
735 |
||
736 |
/** |
|
737 |
* Returns the Root (Window or Applet) that this BufferInfo references. |
|
738 |
*/ |
|
739 |
public Container getRoot() { |
|
740 |
return (root == null) ? null : root.get(); |
|
741 |
} |
|
742 |
||
743 |
/** |
|
744 |
* Returns the BufferStartegy. This will return null if |
|
745 |
* the BufferStartegy hasn't been created and <code>create</code> is |
|
746 |
* false, or if there is a problem in creating the |
|
747 |
* <code>BufferStartegy</code>. |
|
748 |
* |
|
749 |
* @param create If true, and the BufferStartegy is currently null, |
|
750 |
* one will be created. |
|
751 |
*/ |
|
752 |
public BufferStrategy getBufferStrategy(boolean create) { |
|
753 |
BufferStrategy bs = (weakBS == null) ? null : weakBS.get(); |
|
754 |
if (bs == null && create) { |
|
755 |
bs = createBufferStrategy(); |
|
756 |
if (bs != null) { |
|
757 |
weakBS = new WeakReference<BufferStrategy>(bs); |
|
758 |
} |
|
759 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
760 |
LOGGER.finer("getBufferStrategy: created bs: " + bs); |
|
761 |
} |
|
762 |
} |
|
763 |
return bs; |
|
764 |
} |
|
765 |
||
766 |
/** |
|
767 |
* Returns true if using a flip buffer strategy. |
|
768 |
*/ |
|
769 |
public boolean usingFlip() { |
|
770 |
return usingFlip; |
|
771 |
} |
|
772 |
||
773 |
/** |
|
774 |
* Returns true if the buffer strategy of the component differs |
|
775 |
* from current buffer strategy. |
|
776 |
*/ |
|
777 |
public boolean hasBufferStrategyChanged() { |
|
778 |
Container root = getRoot(); |
|
779 |
if (root != null) { |
|
780 |
BufferStrategy ourBS = null; |
|
781 |
BufferStrategy componentBS = null; |
|
782 |
||
783 |
ourBS = getBufferStrategy(false); |
|
784 |
if (root instanceof Window) { |
|
785 |
componentBS = ((Window)root).getBufferStrategy(); |
|
786 |
} |
|
787 |
else { |
|
788 |
try { |
|
789 |
componentBS = (BufferStrategy) |
|
790 |
getGetBufferStrategyMethod().invoke(root); |
|
791 |
} catch (InvocationTargetException ite) { |
|
792 |
assert false; |
|
793 |
} catch (IllegalArgumentException iae) { |
|
794 |
assert false; |
|
795 |
} catch (IllegalAccessException iae2) { |
|
796 |
assert false; |
|
797 |
} |
|
798 |
} |
|
799 |
if (componentBS != ourBS) { |
|
800 |
// Component has a different BS, dispose ours. |
|
801 |
if (ourBS != null) { |
|
802 |
ourBS.dispose(); |
|
803 |
} |
|
804 |
weakBS = null; |
|
805 |
return true; |
|
806 |
} |
|
807 |
} |
|
808 |
return false; |
|
809 |
} |
|
810 |
||
811 |
/** |
|
812 |
* Creates the BufferStrategy. If the appropriate system property |
|
813 |
* has been set we'll try for flip first and then we'll try for |
|
814 |
* blit. |
|
815 |
*/ |
|
816 |
private BufferStrategy createBufferStrategy() { |
|
817 |
BufferCapabilities caps; |
|
818 |
Container root = getRoot(); |
|
819 |
if (root == null) { |
|
820 |
return null; |
|
821 |
} |
|
822 |
BufferStrategy bs = null; |
|
823 |
if (TRY_FLIP) { |
|
824 |
bs = createBufferStrategy(root,BufferCapabilities.FlipContents. |
|
825 |
COPIED); |
|
826 |
usingFlip = true; |
|
827 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
828 |
LOGGER.finer("createBufferStrategy: using flip strategy"); |
|
829 |
} |
|
830 |
} |
|
831 |
if (bs == null) { |
|
832 |
bs = createBufferStrategy(root, null); |
|
833 |
usingFlip = false; |
|
834 |
} |
|
835 |
if (!(bs instanceof SubRegionShowable)) { |
|
836 |
// We do this for two reasons: |
|
837 |
// 1. So that we know we can cast to SubRegionShowable and |
|
838 |
// invoke show with the minimal region to update |
|
839 |
// 2. To avoid the possibility of invoking client code |
|
840 |
// on the toolkit thread. |
|
841 |
bs = null; |
|
842 |
} |
|
843 |
return bs; |
|
844 |
} |
|
845 |
||
846 |
// Creates and returns a buffer strategy of the requested type. If |
|
847 |
// there is a problem creating the buffer strategy this will |
|
848 |
// eat the exception and return null. |
|
849 |
private BufferStrategy createBufferStrategy(Container root, |
|
850 |
BufferCapabilities.FlipContents type) { |
|
851 |
BufferCapabilities caps = new BufferCapabilities( |
|
852 |
new ImageCapabilities(true), |
|
853 |
new ImageCapabilities(true), |
|
854 |
type); |
|
855 |
BufferStrategy bs = null; |
|
856 |
if (root instanceof Applet) { |
|
857 |
try { |
|
858 |
getCreateBufferStrategyMethod().invoke(root, 2, caps); |
|
859 |
bs = (BufferStrategy)getGetBufferStrategyMethod(). |
|
860 |
invoke(root); |
|
861 |
} catch (InvocationTargetException ite) { |
|
862 |
// Type is not supported |
|
863 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
864 |
LOGGER.log(Level.FINER, "createBufferStratety failed", |
|
865 |
ite); |
|
866 |
} |
|
867 |
} catch (IllegalArgumentException iae) { |
|
868 |
assert false; |
|
869 |
} catch (IllegalAccessException iae2) { |
|
870 |
assert false; |
|
871 |
} |
|
872 |
} |
|
873 |
else { |
|
874 |
try { |
|
875 |
((Window)root).createBufferStrategy(2, caps); |
|
876 |
bs = ((Window)root).getBufferStrategy(); |
|
877 |
} catch (AWTException e) { |
|
878 |
// Type not supported |
|
879 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
880 |
LOGGER.log(Level.FINER, "createBufferStratety failed", |
|
881 |
e); |
|
882 |
} |
|
883 |
} |
|
884 |
} |
|
885 |
return bs; |
|
886 |
} |
|
887 |
||
888 |
/** |
|
889 |
* Cleans up and removes any references. |
|
890 |
*/ |
|
891 |
public void dispose() { |
|
892 |
Container root = getRoot(); |
|
893 |
if (LOGGER.isLoggable(Level.FINER)) { |
|
894 |
LOGGER.log(Level.FINER, "disposed BufferInfo for: " + root); |
|
895 |
} |
|
896 |
if (root != null) { |
|
897 |
root.removeComponentListener(this); |
|
898 |
if (root instanceof Window) { |
|
899 |
((Window)root).removeWindowListener(this); |
|
900 |
} |
|
901 |
BufferStrategy bs = getBufferStrategy(false); |
|
902 |
if (bs != null) { |
|
903 |
bs.dispose(); |
|
904 |
} |
|
905 |
} |
|
906 |
this.root = null; |
|
907 |
weakBS = null; |
|
908 |
} |
|
909 |
||
910 |
// We mark the buffer as needing to be painted on a hide/iconify |
|
911 |
// because the developer may have conditionalized painting based on |
|
912 |
// visibility. |
|
913 |
// Ideally we would also move to having the BufferStrategy being |
|
914 |
// a SoftReference in Component here, but that requires changes to |
|
915 |
// Component and the like. |
|
916 |
public void componentHidden(ComponentEvent e) { |
|
917 |
Container root = getRoot(); |
|
918 |
if (root != null && root.isVisible()) { |
|
919 |
// This case will only happen if a developer calls |
|
920 |
// hide immediately followed by show. In this case |
|
921 |
// the event is delivered after show and the window |
|
922 |
// will still be visible. If a developer altered the |
|
923 |
// contents of the window between the hide/show |
|
924 |
// invocations we won't recognize we need to paint and |
|
925 |
// the contents would be bogus. Calling repaint here |
|
926 |
// fixs everything up. |
|
927 |
root.repaint(); |
|
928 |
} |
|
929 |
else { |
|
930 |
setPaintAllOnExpose(true); |
|
931 |
} |
|
932 |
} |
|
933 |
||
934 |
public void windowIconified(WindowEvent e) { |
|
935 |
setPaintAllOnExpose(true); |
|
936 |
} |
|
937 |
||
938 |
// On a dispose we chuck everything. |
|
939 |
public void windowClosed(WindowEvent e) { |
|
940 |
// Make sure we're not showing. |
|
941 |
synchronized(BufferStrategyPaintManager.this) { |
|
942 |
while (showing) { |
|
943 |
try { |
|
944 |
BufferStrategyPaintManager.this.wait(); |
|
945 |
} catch (InterruptedException ie) { |
|
946 |
} |
|
947 |
} |
|
948 |
bufferInfos.remove(this); |
|
949 |
} |
|
950 |
dispose(); |
|
951 |
} |
|
952 |
||
953 |
public void windowOpened(WindowEvent e) { |
|
954 |
} |
|
955 |
||
956 |
public void windowClosing(WindowEvent e) { |
|
957 |
} |
|
958 |
||
959 |
public void windowDeiconified(WindowEvent e) { |
|
960 |
} |
|
961 |
||
962 |
public void windowActivated(WindowEvent e) { |
|
963 |
} |
|
964 |
||
965 |
public void windowDeactivated(WindowEvent e) { |
|
966 |
} |
|
967 |
} |
|
968 |
} |