1 /*
   2  * Copyright (c) 2003, 2010, 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 #include <awt.h>
  27 #include "Trace.h"
  28 #include "WindowsFlags.h"
  29 
  30 BOOL      accelReset;         // reset registry 2d acceleration settings
  31 BOOL      useD3D = TRUE;      // d3d enabled flag
  32                               // initially is TRUE to allow D3D preloading
  33 BOOL      forceD3DUsage;      // force d3d on or off
  34 jboolean  g_offscreenSharing; // JAWT accelerated surface sharing
  35 BOOL      checkRegistry;      // Diagnostic tool: outputs 2d registry settings
  36 BOOL      disableRegistry;    // Diagnostic tool: disables registry interaction
  37 BOOL      setHighDPIAware;    // Whether to set the high-DPI awareness flag
  38 
  39 extern WCHAR *j2dAccelKey;       // Name of java2d root key
  40 extern WCHAR *j2dAccelDriverKey; // Name of j2d per-device key
  41 
  42 static jfieldID d3dEnabledID;
  43 static jfieldID d3dSetID;
  44 static jclass   wFlagsClassID;
  45 
  46 void SetIDs(JNIEnv *env, jclass wFlagsClass)
  47 {
  48     wFlagsClassID = (jclass)env->NewGlobalRef(wFlagsClass);
  49     d3dEnabledID = env->GetStaticFieldID(wFlagsClass, "d3dEnabled", "Z");
  50     d3dSetID = env->GetStaticFieldID(wFlagsClass, "d3dSet", "Z");
  51 }
  52 
  53 BOOL GetStaticBoolean(JNIEnv *env, jclass wfClass, const char *fieldName)
  54 {
  55     jfieldID fieldID = env->GetStaticFieldID(wfClass, fieldName, "Z");
  56     return env->GetStaticBooleanField(wfClass, fieldID);
  57 }
  58 
  59 jobject GetStaticObject(JNIEnv *env, jclass wfClass, const char *fieldName,
  60                         const char *signature)
  61 {
  62     jfieldID fieldID = env->GetStaticFieldID(wfClass, fieldName, signature);
  63     return env->GetStaticObjectField(wfClass, fieldID);
  64 }
  65 
  66 void GetFlagValues(JNIEnv *env, jclass wFlagsClass)
  67 {
  68     jboolean d3dEnabled = env->GetStaticBooleanField(wFlagsClass, d3dEnabledID);
  69     jboolean d3dSet = env->GetStaticBooleanField(wFlagsClass, d3dSetID);
  70     if (!d3dSet) {
  71         // Only check environment variable if user did not set Java
  72         // command-line parameter; values of sun.java2d.d3d override
  73         // any setting of J2D_D3D environment variable.
  74         char *d3dEnv = getenv("J2D_D3D");
  75         if (d3dEnv) {
  76             if (strcmp(d3dEnv, "false") == 0) {
  77                 // printf("Java2D Direct3D usage disabled by J2D_D3D env\n");
  78                 d3dEnabled = FALSE;
  79                 d3dSet = TRUE;
  80                 SetD3DEnabledFlag(env, d3dEnabled, d3dSet);
  81             } else if (strcmp(d3dEnv, "true") == 0) {
  82                 // printf("Java2D Direct3D usage forced on by J2D_D3D env\n");
  83                 d3dEnabled = TRUE;
  84                 d3dSet = TRUE;
  85                 SetD3DEnabledFlag(env, d3dEnabled, d3dSet);
  86             }
  87         }
  88     }
  89     useD3D = d3dEnabled;
  90     forceD3DUsage = d3dSet;
  91     g_offscreenSharing = GetStaticBoolean(env, wFlagsClass,
  92                                           "offscreenSharingEnabled");
  93     accelReset = GetStaticBoolean(env, wFlagsClass, "accelReset");
  94     checkRegistry = GetStaticBoolean(env, wFlagsClass, "checkRegistry");
  95     disableRegistry = GetStaticBoolean(env, wFlagsClass, "disableRegistry");
  96     jstring javaVersionString = (jstring)GetStaticObject(env, wFlagsClass,
  97                                                          "javaVersion",
  98                                                          "Ljava/lang/String;");
  99 
 100     setHighDPIAware =
 101         (IS_WINVISTA && GetStaticBoolean(env, wFlagsClass, "setHighDPIAware"));
 102 
 103     J2dTraceLn(J2D_TRACE_INFO, "WindowsFlags (native):");
 104     J2dTraceLn1(J2D_TRACE_INFO, "  d3dEnabled = %s",
 105                 (useD3D ? "true" : "false"));
 106     J2dTraceLn1(J2D_TRACE_INFO, "  d3dSet = %s",
 107                 (forceD3DUsage ? "true" : "false"));
 108     J2dTraceLn1(J2D_TRACE_INFO, "  offscreenSharing = %s",
 109                 (g_offscreenSharing ? "true" : "false"));
 110     J2dTraceLn1(J2D_TRACE_INFO, "  accelReset = %s",
 111                 (accelReset ? "true" : "false"));
 112     J2dTraceLn1(J2D_TRACE_INFO, "  checkRegistry = %s",
 113                 (checkRegistry ? "true" : "false"));
 114     J2dTraceLn1(J2D_TRACE_INFO, "  disableRegistry = %s",
 115                 (disableRegistry ? "true" : "false"));
 116     J2dTraceLn1(J2D_TRACE_INFO, "  setHighDPIAware = %s",
 117                 (setHighDPIAware ? "true" : "false"));
 118 }
 119 
 120 void SetD3DEnabledFlag(JNIEnv *env, BOOL d3dEnabled, BOOL d3dSet)
 121 {
 122     useD3D = d3dEnabled;
 123     forceD3DUsage = d3dSet;
 124     if (env == NULL) {
 125         env = (JNIEnv * ) JNU_GetEnv(jvm, JNI_VERSION_1_2);
 126     }
 127     env->SetStaticBooleanField(wFlagsClassID, d3dEnabledID, d3dEnabled);
 128     if (d3dSet) {
 129         env->SetStaticBooleanField(wFlagsClassID, d3dSetID, d3dSet);
 130     }
 131 }
 132 
 133 BOOL IsD3DEnabled() {
 134     return useD3D;
 135 }
 136 
 137 BOOL IsD3DForced() {
 138     return forceD3DUsage;
 139 }
 140 
 141 extern "C" {
 142 
 143 /**
 144  * This function is called from WindowsFlags.initFlags() and initializes
 145  * the native side of our runtime flags.  There are a couple of important
 146  * things that happen at the native level after we set the Java flags:
 147  * - set native variables based on the java flag settings (such as useDD
 148  * based on whether ddraw was enabled by a runtime flag)
 149  * - override java level settings if there user has set an environment
 150  * variable but not a runtime flag.  For example, if the user runs
 151  * with sun.java2d.d3d=true but also uses the J2D_D3D=false environment
 152  * variable, then we use the java-level true value.  But if they do
 153  * not use the runtime flag, then the env variable will force d3d to
 154  * be disabled.  Any native env variable overriding must up-call to
 155  * Java to change the java level flag settings.
 156  * - A later error in initialization may result in disabling some
 157  * native property that must be propagated to the Java level.  For
 158  * example, d3d is enabled by default, but we may find later that
 159  * we must disable it do to some runtime configuration problem (such as
 160  * a bad video card).  This will happen through mechanisms in this native
 161  * file to change the value of the known Java flags (in this d3d example,
 162  * we would up-call to set the value of d3dEnabled to Boolean.FALSE).
 163  */
 164 JNIEXPORT void JNICALL
 165 Java_sun_java2d_windows_WindowsFlags_initNativeFlags(JNIEnv *env,
 166                                                      jclass wFlagsClass)
 167 {
 168     SetIDs(env, wFlagsClass);
 169     GetFlagValues(env, wFlagsClass);
 170 }
 171 
 172 } // extern "C"