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