1 /*
   2  * Copyright (c) 2003, 2008, 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  */
  25 
  26 package sun.java2d.windows;
  27 
  28 import sun.awt.windows.WToolkit;
  29 import sun.java2d.opengl.WGLGraphicsConfig;
  30 
  31 public class WindowsFlags {
  32 
  33     /**
  34      * Description of command-line flags.  All flags with [true|false]
  35      * values (where both have possible meanings, such as with ddlock)
  36      * have an associated variable that indicates whether this flag
  37      * was set by the user.  For example, d3d is on by default, but
  38      * may be disabled at runtime by internal settings unless the user
  39      * has forced it on with d3d=true.  These associated variables have
  40      * the same base (eg, d3d) but end in "Set" (eg, d3dEnabled and
  41      * d3dSet).
  42      *      ddEnabled: usage: "-Dsun.java2d.noddraw[=false|true]"
  43      *               turns on/off all usage of Direct3D
  44      *      ddOffscreenEnabled: equivalent of sun.java2d.noddraw
  45      *      gdiBlitEnabled: usage: "-Dsun.java2d.gdiblit=false"
  46      *               turns off Blit loops that use GDI for copying to
  47      *               the screen from certain image types.  Copies will,
  48      *               instead, happen via ddraw locking or temporary GDI DIB
  49      *               creation/copying (depending on OS and other flags)
  50      *      d3dEnabled: usage: "-Dsun.java2d.d3d=[true|false]"
  51      *               Forces our use of Direct3D on or off.  Direct3D is on
  52      *               by default, but may be disabled in some situations, such
  53      *               as on a card with bad d3d line quality, or on a video card
  54      *               that we have had bad experience with (e.g., Trident).
  55      *               This flag can force us to use d3d
  56      *               anyway in these situations.  Or, this flag can force us to
  57      *               not use d3d in a situation where we would use it otherwise.
  58      *      offscreenSharingEnabled: usage: "-Dsun.java2d.offscreenSharing=true"
  59      *               Turns on the ability to share a hardware-accelerated
  60      *               offscreen surface through the JAWT interface.  See
  61      *               src/windows/native/sun/windows/awt_DrawingSurface.* for
  62      *               more information.  This capability is disabled by default
  63      *               pending more testing and time to work out the right
  64      *               solution; we do not want to expose more public JAWT api
  65      *               without being very sure that we will be willing to support
  66      *               that API in the future regardless of other native
  67      *               rendering pipeline changes.
  68      *      magPresent: usage: "-Djavax.accessibility.screen_magnifier_present"
  69      *               This flag is set either on the command line or in the
  70      *               properties file.  It tells Swing whether the user is
  71      *               currently using a screen magnifying application.  These
  72      *               applications tend to conflict with ddraw (which assumes
  73      *               it owns the entire display), so the presence of these
  74      *               applications implies that we should disable ddraw.
  75      *               So if magPresent is true, we set ddEnabled and associated
  76      *               variables to false and do not initialize the native
  77      *               hardware acceleration for these properties.
  78      *      opengl: usage: "-Dsun.java2d.opengl=[true|True]"
  79      *               Enables the use of the OpenGL-pipeline.  If the
  80      *               OpenGL flag is specified and WGL initialization is
  81      *               successful, we implicitly disable the use of DirectDraw
  82      *               and Direct3D, as those pipelines may interfere with the
  83      *               OGL pipeline.  (If "True" is specified, a message will
  84      *               appear on the console stating whether or not the OGL
  85      *               was successfully initialized.)
  86      * setHighDPIAware: Property usage: "-Dsun.java2d.dpiaware=[true|false]"
  87      *               This property flag "sun.java2d.dpiaware" is used to
  88      *               override the default behavior, which is:
  89      *               On Windows Vista, if the java process is launched from a
  90      *               known launcher (java, javaw, javaws, etc) - which is
  91      *               determined by whether a -Dsun.java.launcher property is set
  92      *               to "SUN_STANDARD" - the "high-DPI aware" property will be
  93      *               set on the native level prior to initializing the display.
  94      *
  95      */
  96 
  97     private static boolean gdiBlitEnabled;
  98     private static boolean d3dEnabled;
  99     private static boolean d3dVerbose;
 100     private static boolean d3dSet;
 101     private static boolean d3dOnScreenEnabled;
 102     private static boolean oglEnabled;
 103     private static boolean oglVerbose;
 104     private static boolean offscreenSharingEnabled;
 105     private static boolean magPresent;
 106     private static boolean setHighDPIAware;
 107     // TODO: other flags, including nopixfmt
 108 
 109     static {
 110         // Ensure awt is loaded already.  Also, this forces static init
 111         // of WToolkit and Toolkit, which we depend upon.
 112         WToolkit.loadLibraries();
 113         // First, init all Java level flags
 114         initJavaFlags();
 115         // Now, init things on the native side.  This may call up through
 116         // JNI to get/set the Java level flags based on native capabilities
 117         // and environment variables
 118         initNativeFlags();
 119     }
 120 
 121     private static native boolean initNativeFlags();
 122 
 123     // Noop: this method is just here as a convenient calling place when
 124     // we are initialized by Win32GraphicsEnv.  Calling this will force
 125     // us to run through the static block below, which is where the
 126     // real work occurs.
 127     public static void initFlags() {}
 128 
 129     private static boolean getBooleanProp(String p, boolean defaultVal) {
 130         String propString = System.getProperty(p);
 131         boolean returnVal = defaultVal;
 132         if (propString != null) {
 133             if (propString.equals("true") ||
 134                 propString.equals("t") ||
 135                 propString.equals("True") ||
 136                 propString.equals("T") ||
 137                 propString.equals("")) // having the prop name alone
 138             {                          // is equivalent to true
 139                 returnVal = true;
 140             } else if (propString.equals("false") ||
 141                        propString.equals("f") ||
 142                        propString.equals("False") ||
 143                        propString.equals("F"))
 144             {
 145                 returnVal = false;
 146             }
 147         }
 148         return returnVal;
 149     }
 150 
 151     private static boolean isBooleanPropTrueVerbose(String p) {
 152         String propString = System.getProperty(p);
 153         if (propString != null) {
 154             if (propString.equals("True") ||
 155                 propString.equals("T"))
 156             {
 157                 return true;
 158             }
 159         }
 160         return false;
 161     }
 162 
 163     private static int getIntProp(String p, int defaultVal) {
 164         String propString = System.getProperty(p);
 165         int returnVal = defaultVal;
 166         if (propString != null) {
 167             try {
 168                 returnVal = Integer.parseInt(propString);
 169             } catch (NumberFormatException e) {}
 170         }
 171         return returnVal;
 172     }
 173 
 174     private static boolean getPropertySet(String p) {
 175         String propString = System.getProperty(p);
 176         return (propString != null) ? true : false;
 177     }
 178 
 179     private static void initJavaFlags() {
 180         java.security.AccessController.doPrivileged(
 181             new java.security.PrivilegedAction<Object>()
 182         {
 183             public Object run() {
 184                 magPresent = getBooleanProp(
 185                     "javax.accessibility.screen_magnifier_present", false);
 186                 boolean ddEnabled =
 187                     !getBooleanProp("sun.java2d.noddraw", magPresent);
 188                 boolean ddOffscreenEnabled =
 189                     getBooleanProp("sun.java2d.ddoffscreen", ddEnabled);
 190                 d3dEnabled = getBooleanProp("sun.java2d.d3d",
 191                     ddEnabled && ddOffscreenEnabled);
 192                 d3dOnScreenEnabled =
 193                     getBooleanProp("sun.java2d.d3d.onscreen", d3dEnabled);
 194                 oglEnabled = getBooleanProp("sun.java2d.opengl", false);
 195                 if (oglEnabled) {
 196                     oglVerbose = isBooleanPropTrueVerbose("sun.java2d.opengl");
 197                     if (WGLGraphicsConfig.isWGLAvailable()) {
 198                         d3dEnabled = false;
 199                     } else {
 200                         if (oglVerbose) {
 201                             System.out.println(
 202                                 "Could not enable OpenGL pipeline " +
 203                                 "(WGL not available)");
 204                         }
 205                         oglEnabled = false;
 206                     }
 207                 }
 208                 gdiBlitEnabled = getBooleanProp("sun.java2d.gdiBlit", true);
 209                 d3dSet = getPropertySet("sun.java2d.d3d");
 210                 if (d3dSet) {
 211                     d3dVerbose = isBooleanPropTrueVerbose("sun.java2d.d3d");
 212                 }
 213                 offscreenSharingEnabled =
 214                     getBooleanProp("sun.java2d.offscreenSharing", false);
 215                 String dpiOverride = System.getProperty("sun.java2d.dpiaware");
 216                 if (dpiOverride != null) {
 217                     setHighDPIAware = dpiOverride.equalsIgnoreCase("true");
 218                 } else {
 219                     String sunLauncherProperty =
 220                         System.getProperty("sun.java.launcher", "unknown");
 221                     setHighDPIAware =
 222                         sunLauncherProperty.equalsIgnoreCase("SUN_STANDARD");
 223                 }
 224                 /*
 225                 // Output info based on some non-default flags:
 226                 if (offscreenSharingEnabled) {
 227                     System.out.println(
 228                         "Warning: offscreenSharing has been enabled. " +
 229                         "The use of this capability will change in future " +
 230                         "releases and applications that depend on it " +
 231                         "may not work correctly");
 232                 }
 233                 */
 234                 return null;
 235             }
 236         });
 237         /*
 238         System.out.println("WindowsFlags (Java):");
 239         System.out.println("  ddEnabled: " + ddEnabled + "\n" +
 240                            "  ddOffscreenEnabled: " + ddOffscreenEnabled + "\n" +
 241                            "  d3dEnabled: " + d3dEnabled + "\n" +
 242                            "  d3dSet: " + d3dSet + "\n" +
 243                            "  oglEnabled: " + oglEnabled + "\n" +
 244                            "  oglVerbose: " + oglVerbose + "\n" +
 245                            "  gdiBlitEnabled: " + gdiBlitEnabled + "\n" +
 246                            "  offscreenSharingEnabled: " + offscreenSharingEnabled);
 247         */
 248     }
 249 
 250     public static boolean isD3DEnabled() {
 251         return d3dEnabled;
 252     }
 253 
 254     public static boolean isD3DSet() {
 255         return d3dSet;
 256     }
 257 
 258     public static boolean isD3DOnScreenEnabled() {
 259         return d3dOnScreenEnabled;
 260     }
 261 
 262     public static boolean isD3DVerbose() {
 263         return d3dVerbose;
 264     }
 265 
 266     public static boolean isGdiBlitEnabled() {
 267         return gdiBlitEnabled;
 268     }
 269 
 270     public static boolean isOffscreenSharingEnabled() {
 271         return offscreenSharingEnabled;
 272     }
 273 
 274     public static boolean isMagPresent() {
 275         return magPresent;
 276     }
 277 
 278     public static boolean isOGLEnabled() {
 279         return oglEnabled;
 280     }
 281 
 282     public static boolean isOGLVerbose() {
 283         return oglVerbose;
 284     }
 285 }