History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg/javax/swing/BufferStrategyPaintManager.java (1 of 18352 files). Show all

Unified · Split

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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t