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