jdk/src/share/classes/javax/swing/BufferStrategyPaintManager.java
changeset 23297 2946ef51ee24
parent 18178 ee71c923891d
child 23328 4c53a6ebc779
equal deleted inserted replaced
23296:0acf698eee6a 23297:2946ef51ee24
     1 /*
     1 /*
     2  * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    30 import java.lang.reflect.*;
    30 import java.lang.reflect.*;
    31 import java.lang.ref.WeakReference;
    31 import java.lang.ref.WeakReference;
    32 import java.util.*;
    32 import java.util.*;
    33 
    33 
    34 import com.sun.java.swing.SwingUtilities3;
    34 import com.sun.java.swing.SwingUtilities3;
       
    35 import java.util.logging.Level;
       
    36 import java.util.logging.Logger;
       
    37 import sun.awt.AWTAccessor;
    35 
    38 
    36 import sun.awt.SubRegionShowable;
    39 import sun.awt.SubRegionShowable;
    37 import sun.java2d.SunGraphics2D;
    40 import sun.java2d.SunGraphics2D;
    38 import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
    41 import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
    39 import sun.awt.SunToolkit;
    42 import sun.awt.SunToolkit;
    67     // If we're using a flip strategy the contents of the backbuffer may
    70     // If we're using a flip strategy the contents of the backbuffer may
    68     // have changed and so show only attempts to show from the backbuffer
    71     // have changed and so show only attempts to show from the backbuffer
    69     // if we get a blit strategy.
    72     // if we get a blit strategy.
    70     //
    73     //
    71 
    74 
    72     //
       
    73     // Methods used to create BufferStrategy for Applets.
       
    74     //
       
    75     private static Method COMPONENT_CREATE_BUFFER_STRATEGY_METHOD;
       
    76     private static Method COMPONENT_GET_BUFFER_STRATEGY_METHOD;
       
    77 
       
    78     private static final PlatformLogger LOGGER = PlatformLogger.getLogger(
    75     private static final PlatformLogger LOGGER = PlatformLogger.getLogger(
    79                            "javax.swing.BufferStrategyPaintManager");
    76                            "javax.swing.BufferStrategyPaintManager");
    80 
    77 
    81     /**
    78     /**
    82      * List of BufferInfos.  We don't use a Map primarily because
    79      * List of BufferInfos.  We don't use a Map primarily because
   140     /**
   137     /**
   141      * Set to true if the bufferInfo needs to be disposed when current
   138      * Set to true if the bufferInfo needs to be disposed when current
   142      * paint loop is done.
   139      * paint loop is done.
   143      */
   140      */
   144     private boolean disposeBufferOnEnd;
   141     private boolean disposeBufferOnEnd;
   145 
       
   146     private static Method getGetBufferStrategyMethod() {
       
   147         if (COMPONENT_GET_BUFFER_STRATEGY_METHOD == null) {
       
   148             getMethods();
       
   149         }
       
   150         return COMPONENT_GET_BUFFER_STRATEGY_METHOD;
       
   151     }
       
   152 
       
   153     private static Method getCreateBufferStrategyMethod() {
       
   154         if (COMPONENT_CREATE_BUFFER_STRATEGY_METHOD == null) {
       
   155             getMethods();
       
   156         }
       
   157         return COMPONENT_CREATE_BUFFER_STRATEGY_METHOD;
       
   158     }
       
   159 
       
   160     private static void getMethods() {
       
   161         java.security.AccessController.doPrivileged(
       
   162                             new java.security.PrivilegedAction<Object>() {
       
   163             public Object run() {
       
   164                 try {
       
   165                     COMPONENT_CREATE_BUFFER_STRATEGY_METHOD = Component.class.
       
   166                               getDeclaredMethod("createBufferStrategy",
       
   167                                                 new Class[] { int.class,
       
   168                                                 BufferCapabilities.class });
       
   169                     COMPONENT_CREATE_BUFFER_STRATEGY_METHOD.
       
   170                                             setAccessible(true);
       
   171                     COMPONENT_GET_BUFFER_STRATEGY_METHOD = Component.class.
       
   172                               getDeclaredMethod("getBufferStrategy");
       
   173                     COMPONENT_GET_BUFFER_STRATEGY_METHOD.setAccessible(true);
       
   174                 } catch (SecurityException e) {
       
   175                     assert false;
       
   176                 } catch (NoSuchMethodException nsme) {
       
   177                     assert false;
       
   178                 }
       
   179                 return null;
       
   180             }
       
   181         });
       
   182     }
       
   183 
   142 
   184     BufferStrategyPaintManager() {
   143     BufferStrategyPaintManager() {
   185         bufferInfos = new ArrayList<BufferInfo>(1);
   144         bufferInfos = new ArrayList<BufferInfo>(1);
   186     }
   145     }
   187 
   146 
   764                 ourBS = getBufferStrategy(false);
   723                 ourBS = getBufferStrategy(false);
   765                 if (root instanceof Window) {
   724                 if (root instanceof Window) {
   766                     componentBS = ((Window)root).getBufferStrategy();
   725                     componentBS = ((Window)root).getBufferStrategy();
   767                 }
   726                 }
   768                 else {
   727                 else {
   769                     try {
   728                     componentBS = AWTAccessor.getComponentAccessor().getBufferStrategy(root);
   770                         componentBS = (BufferStrategy)
       
   771                                  getGetBufferStrategyMethod().invoke(root);
       
   772                     } catch (InvocationTargetException ite) {
       
   773                         assert false;
       
   774                     } catch (IllegalArgumentException iae) {
       
   775                         assert false;
       
   776                     } catch (IllegalAccessException iae2) {
       
   777                         assert false;
       
   778                     }
       
   779                 }
   729                 }
   780                 if (componentBS != ourBS) {
   730                 if (componentBS != ourBS) {
   781                     // Component has a different BS, dispose ours.
   731                     // Component has a different BS, dispose ours.
   782                     if (ourBS != null) {
   732                     if (ourBS != null) {
   783                         ourBS.dispose();
   733                         ourBS.dispose();
   837                     null);
   787                     null);
   838             }
   788             }
   839             BufferStrategy bs = null;
   789             BufferStrategy bs = null;
   840             if (SunToolkit.isInstanceOf(root, "java.applet.Applet")) {
   790             if (SunToolkit.isInstanceOf(root, "java.applet.Applet")) {
   841                 try {
   791                 try {
   842                     getCreateBufferStrategyMethod().invoke(root, 2, caps);
   792                     AWTAccessor.ComponentAccessor componentAccessor
   843                     bs = (BufferStrategy)getGetBufferStrategyMethod().
   793                             = AWTAccessor.getComponentAccessor();
   844                                             invoke(root);
   794                     componentAccessor.createBufferStrategy(root, 2, caps);
   845                 } catch (InvocationTargetException ite) {
   795                     bs = componentAccessor.getBufferStrategy(root);
       
   796                 } catch (AWTException e) {
   846                     // Type is not supported
   797                     // Type is not supported
   847                     if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
   798                     if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
   848                         LOGGER.finer("createBufferStratety failed",
   799                         LOGGER.finer("createBufferStratety failed",
   849                                      ite);
   800                                      e);
   850                     }
   801                     }
   851                 } catch (IllegalArgumentException iae) {
       
   852                     assert false;
       
   853                 } catch (IllegalAccessException iae2) {
       
   854                     assert false;
       
   855                 }
   802                 }
   856             }
   803             }
   857             else {
   804             else {
   858                 try {
   805                 try {
   859                     ((Window)root).createBufferStrategy(2, caps);
   806                     ((Window)root).createBufferStrategy(2, caps);