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      *      translAccelEnabled: usage: "-Dsun.java2d.translaccel=true"
  59      *               equivalent to sun.java2d.d3d=true
  60      *      offscreenSharingEnabled: usage: "-Dsun.java2d.offscreenSharing=true"
  61      *               Turns on the ability to share a hardware-accelerated
  62      *               offscreen surface through the JAWT interface.  See
  63      *               src/windows/native/sun/windows/awt_DrawingSurface.* for
  64      *               more information.  This capability is disabled by default
  65      *               pending more testing and time to work out the right
  66      *               solution; we do not want to expose more public JAWT api
  67      *               without being very sure that we will be willing to support
  68      *               that API in the future regardless of other native
  69      *               rendering pipeline changes.
  70      *      accelReset: usage: "-Dsun.java2d.accelReset"
  71      *               This flag tells us to reset any persistent information
  72      *               the display device acceleration characteristics so that
  73      *               we are forced to retest these characteristics.  This flag
  74      *               is primarily used for debugging purposes (to allow testing
  75      *               of the persistent storage mechanisms) but may also be
  76      *               needed by some users if, for example, a driver upgrade
  77      *               may change the runtime characteristics and they want the
  78      *               tests to be re-run.
  79      *      checkRegistry: usage: "-Dsun.java2d.checkRegistry"
  80      *               This flag tells us to output the current registry settings
  81      *               (after our initialization) to the console.
  82      *      disableRegistry: usage: "-Dsun.java2d.disableRegistry"
  83      *               This flag tells us to disable all registry-related
  84      *               activities.  It is mainly here for debugging purposes,
  85      *               to allow us to see whether any runtime bugs are caused
  86      *               by or related to registry problems.
  87      *      magPresent: usage: "-Djavax.accessibility.screen_magnifier_present"
  88      *               This flag is set either on the command line or in the
  89      *               properties file.  It tells Swing whether the user is
  90      *               currently using a screen magnifying application.  These
  91      *               applications tend to conflict with ddraw (which assumes
  92      *               it owns the entire display), so the presence of these
  93      *               applications implies that we should disable ddraw.
  94      *               So if magPresent is true, we set ddEnabled and associated
  95      *               variables to false and do not initialize the native
  96      *               hardware acceleration for these properties.
  97      *      opengl: usage: "-Dsun.java2d.opengl=[true|True]"
  98      *               Enables the use of the OpenGL-pipeline.  If the
  99      *               OpenGL flag is specified and WGL initialization is
 100      *               successful, we implicitly disable the use of DirectDraw
 101      *               and Direct3D, as those pipelines may interfere with the
 102      *               OGL pipeline.  (If "True" is specified, a message will
 103      *               appear on the console stating whether or not the OGL
 104      *               was successfully initialized.)
 105      * setHighDPIAware: Property usage: "-Dsun.java2d.dpiaware=[true|false]"
 106      *               This property flag "sun.java2d.dpiaware" is used to
 107      *               override the default behavior, which is:
 108      *               On Windows Vista, if the java process is launched from a
 109      *               known launcher (java, javaw, javaws, etc) - which is
 110      *               determined by whether a -Dsun.java.launcher property is set
 111      *               to "SUN_STANDARD" - the "high-DPI aware" property will be
 112      *               set on the native level prior to initializing the display.
 113      *
 114      */
 115 
 116     private static boolean gdiBlitEnabled;
 117     private static boolean d3dEnabled;
 118     private static boolean d3dVerbose;
 119     private static boolean d3dSet;
 120     private static boolean d3dOnScreenEnabled;
 121     private static boolean oglEnabled;
 122     private static boolean oglVerbose;
 123     private static boolean offscreenSharingEnabled;
 124     private static boolean accelReset;
 125     private static boolean checkRegistry;
 126     private static boolean disableRegistry;
 127     private static boolean magPresent;
 128     private static boolean setHighDPIAware;
 129     // TODO: other flags, including nopixfmt
 130 
 131     static {
 132         // Ensure awt is loaded already.  Also, this forces static init
 133         // of WToolkit and Toolkit, which we depend upon.
 134         WToolkit.loadLibraries();
 135         // First, init all Java level flags
 136         initJavaFlags();
 137         // Now, init things on the native side.  This may call up through
 138         // JNI to get/set the Java level flags based on native capabilities
 139         // and environment variables
 140         initNativeFlags();
 141     }
 142 
 143     private static native boolean initNativeFlags();
 144 
 145     // Noop: this method is just here as a convenient calling place when
 146     // we are initialized by Win32GraphicsEnv.  Calling this will force
 147     // us to run through the static block below, which is where the
 148     // real work occurs.
 149     public static void initFlags() {}
 150 
 151     private static boolean getBooleanProp(String p, boolean defaultVal) {
 152         String propString = System.getProperty(p);
 153         boolean returnVal = defaultVal;
 154         if (propString != null) {
 155             if (propString.equals("true") ||
 156                 propString.equals("t") ||
 157                 propString.equals("True") ||
 158                 propString.equals("T") ||
 159                 propString.equals("")) // having the prop name alone
 160             {                          // is equivalent to true
 161                 returnVal = true;
 162             } else if (propString.equals("false") ||
 163                        propString.equals("f") ||
 164                        propString.equals("False") ||
 165                        propString.equals("F"))
 166             {
 167                 returnVal = false;
 168             }
 169         }
 170         return returnVal;
 171     }
 172 
 173     private static boolean isBooleanPropTrueVerbose(String p) {
 174         String propString = System.getProperty(p);
 175         if (propString != null) {
 176             if (propString.equals("True") ||
 177                 propString.equals("T"))
 178             {
 179                 return true;
 180             }
 181         }
 182         return false;
 183     }
 184 
 185     private static int getIntProp(String p, int defaultVal) {
 186         String propString = System.getProperty(p);
 187         int returnVal = defaultVal;
 188         if (propString != null) {
 189             try {
 190                 returnVal = Integer.parseInt(propString);
 191             } catch (NumberFormatException e) {}
 192         }
 193         return returnVal;
 194     }
 195 
 196     private static boolean getPropertySet(String p) {
 197         String propString = System.getProperty(p);
 198         return (propString != null) ? true : false;
 199     }
 200 
 201     private static void initJavaFlags() {
 202         java.security.AccessController.doPrivileged(
 203             new java.security.PrivilegedAction<Object>()
 204         {
 205             public Object run() {
 206                 magPresent = getBooleanProp(
 207                     "javax.accessibility.screen_magnifier_present", false);
 208                 boolean ddEnabled =
 209                     !getBooleanProp("sun.java2d.noddraw", magPresent);
 210                 boolean ddOffscreenEnabled =
 211                     getBooleanProp("sun.java2d.ddoffscreen", ddEnabled);
 212                 d3dEnabled = getBooleanProp("sun.java2d.d3d",
 213                     ddEnabled && ddOffscreenEnabled);
 214                 d3dOnScreenEnabled =
 215                     getBooleanProp("sun.java2d.d3d.onscreen", d3dEnabled);
 216                 oglEnabled = getBooleanProp("sun.java2d.opengl", false);
 217                 if (oglEnabled) {
 218                     oglVerbose = isBooleanPropTrueVerbose("sun.java2d.opengl");
 219                     if (WGLGraphicsConfig.isWGLAvailable()) {
 220                         d3dEnabled = false;
 221                     } else {
 222                         if (oglVerbose) {
 223                             System.out.println(
 224                                 "Could not enable OpenGL pipeline " +
 225                                 "(WGL not available)");
 226                         }
 227                         oglEnabled = false;
 228                     }
 229                 }
 230                 gdiBlitEnabled = getBooleanProp("sun.java2d.gdiBlit", true);
 231                 d3dSet = getPropertySet("sun.java2d.d3d");
 232                 if (d3dSet) {
 233                     d3dVerbose = isBooleanPropTrueVerbose("sun.java2d.d3d");
 234                 }
 235                 offscreenSharingEnabled =
 236                     getBooleanProp("sun.java2d.offscreenSharing", false);
 237                 accelReset = getBooleanProp("sun.java2d.accelReset", false);
 238                 checkRegistry =
 239                     getBooleanProp("sun.java2d.checkRegistry", false);
 240                 disableRegistry =
 241                     getBooleanProp("sun.java2d.disableRegistry", false);
 242                 String dpiOverride = System.getProperty("sun.java2d.dpiaware");
 243                 if (dpiOverride != null) {
 244                     setHighDPIAware = dpiOverride.equalsIgnoreCase("true");
 245                 } else {
 246                     String sunLauncherProperty =
 247                         System.getProperty("sun.java.launcher", "unknown");
 248                     setHighDPIAware =
 249                         sunLauncherProperty.equalsIgnoreCase("SUN_STANDARD");
 250                 }
 251                 /*
 252                 // Output info based on some non-default flags:
 253                 if (offscreenSharingEnabled) {
 254                     System.out.println(
 255                         "Warning: offscreenSharing has been enabled. " +
 256                         "The use of this capability will change in future " +
 257                         "releases and applications that depend on it " +
 258                         "may not work correctly");
 259                 }
 260                 */
 261                 return null;
 262             }
 263         });
 264         /*
 265         System.out.println("WindowsFlags (Java):");
 266         System.out.println("  ddEnabled: " + ddEnabled + "\n" +
 267                            "  ddOffscreenEnabled: " + ddOffscreenEnabled + "\n" +
 268                            "  ddVramForced: " + ddVramForced + "\n" +
 269                            "  ddLockEnabled: " + ddLockEnabled + "\n" +
 270                            "  ddLockSet: " + ddLockSet + "\n" +
 271                            "  ddBlitEnabled: " + ddBlitEnabled + "\n" +
 272                            "  ddScaleEnabled: " + ddScaleEnabled + "\n" +
 273                            "  d3dEnabled: " + d3dEnabled + "\n" +
 274                            "  d3dSet: " + d3dSet + "\n" +
 275                            "  oglEnabled: " + oglEnabled + "\n" +
 276                            "  oglVerbose: " + oglVerbose + "\n" +
 277                            "  gdiBlitEnabled: " + gdiBlitEnabled + "\n" +
 278                            "  translAccelEnabled: " + translAccelEnabled + "\n" +
 279                            "  offscreenSharingEnabled: " + offscreenSharingEnabled + "\n" +
 280                            "  accelReset: " + accelReset + "\n" +
 281                            "  checkRegistry: " + checkRegistry + "\n" +
 282                            "  disableRegistry: " + disableRegistry + "\n" +
 283                            "  d3dTexBPP: " + d3dTexBpp);
 284         */
 285     }
 286 
 287     public static boolean isD3DEnabled() {
 288         return d3dEnabled;
 289     }
 290 
 291     public static boolean isD3DSet() {
 292         return d3dSet;
 293     }
 294 
 295     public static boolean isD3DOnScreenEnabled() {
 296         return d3dOnScreenEnabled;
 297     }
 298 
 299     public static boolean isD3DVerbose() {
 300         return d3dVerbose;
 301     }
 302 
 303     public static boolean isGdiBlitEnabled() {
 304         return gdiBlitEnabled;
 305     }
 306 
 307     public static boolean isTranslucentAccelerationEnabled() {
 308         return d3dEnabled;
 309     }
 310 
 311     public static boolean isOffscreenSharingEnabled() {
 312         return offscreenSharingEnabled;
 313     }
 314 
 315     public static boolean isMagPresent() {
 316         return magPresent;
 317     }
 318 
 319     public static boolean isOGLEnabled() {
 320         return oglEnabled;
 321     }
 322 
 323     public static boolean isOGLVerbose() {
 324         return oglVerbose;
 325     }
 326 }