Path: blob/master/src/java.desktop/macosx/classes/sun/java2d/MacOSFlags.java
41152 views
/*1* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d;2627import java.security.PrivilegedAction;28import sun.java2d.metal.MTLGraphicsConfig;29import sun.java2d.opengl.CGLGraphicsConfig;303132public class MacOSFlags {3334/**35* Description of command-line flags. All flags with [true|false]36* values37* metalEnabled: usage: "-Dsun.java2d.metal=[true|false]"38*/3940private static boolean oglEnabled;41private static boolean oglVerbose;42private static boolean metalEnabled;43private static boolean metalVerbose;4445private enum PropertyState {ENABLED, DISABLED, UNSPECIFIED};4647static {48initJavaFlags();49}5051private static PropertyState getBooleanProp(String p, PropertyState defaultVal) {52String propString = System.getProperty(p);53PropertyState returnVal = defaultVal;54if (propString != null) {55if (propString.equals("true") ||56propString.equals("t") ||57propString.equals("True") ||58propString.equals("T") ||59propString.equals("")) // having the prop name alone60{ // is equivalent to true61returnVal = PropertyState.ENABLED;62} else if (propString.equals("false") ||63propString.equals("f") ||64propString.equals("False") ||65propString.equals("F"))66{67returnVal = PropertyState.DISABLED;68}69}70return returnVal;71}7273private static boolean isBooleanPropTrueVerbose(String p) {74String propString = System.getProperty(p);75if (propString != null) {76if (propString.equals("True") ||77propString.equals("T"))78{79return true;80}81}82return false;83}8485@SuppressWarnings("removal")86private static void initJavaFlags() {87java.security.AccessController.doPrivileged(88(PrivilegedAction<Object>) () -> {89PropertyState oglState = getBooleanProp("sun.java2d.opengl", PropertyState.UNSPECIFIED);90PropertyState metalState = getBooleanProp("sun.java2d.metal", PropertyState.UNSPECIFIED);9192// Handle invalid combinations to use the default rendering pipeline93// Current default rendering pipeline is OpenGL94// (The default can be changed to Metal in future just by toggling two states in this if condition block)95if ((oglState == PropertyState.UNSPECIFIED && metalState == PropertyState.UNSPECIFIED) ||96(oglState == PropertyState.DISABLED && metalState == PropertyState.DISABLED) ||97(oglState == PropertyState.ENABLED && metalState == PropertyState.ENABLED)) {98oglState = PropertyState.ENABLED; // Enable default pipeline99metalState = PropertyState.DISABLED; // Disable non-default pipeline100}101102if (metalState == PropertyState.UNSPECIFIED) {103if (oglState == PropertyState.DISABLED) {104oglEnabled = false;105metalEnabled = true;106} else {107oglEnabled = true;108metalEnabled = false;109}110} else if (metalState == PropertyState.ENABLED) {111oglEnabled = false;112metalEnabled = true;113} else if (metalState == PropertyState.DISABLED) {114oglEnabled = true;115metalEnabled = false;116}117118oglVerbose = isBooleanPropTrueVerbose("sun.java2d.opengl");119metalVerbose = isBooleanPropTrueVerbose("sun.java2d.metal");120121if (oglEnabled && !metalEnabled) {122// Check whether OGL is available123if (!CGLGraphicsConfig.isCGLAvailable()) {124if (oglVerbose) {125System.out.println("Could not enable OpenGL pipeline (CGL not available)");126}127oglEnabled = false;128metalEnabled = MTLGraphicsConfig.isMetalAvailable();129}130} else if (metalEnabled && !oglEnabled) {131// Check whether Metal framework is available132if (!MTLGraphicsConfig.isMetalAvailable()) {133if (metalVerbose) {134System.out.println("Could not enable Metal pipeline (Metal framework not available)");135}136metalEnabled = false;137oglEnabled = CGLGraphicsConfig.isCGLAvailable();138}139}140141// At this point one of the rendering pipeline must be enabled.142if (!metalEnabled && !oglEnabled) {143throw new InternalError("Error - unable to initialize any rendering pipeline.");144}145146return null;147});148}149150public static boolean isMetalEnabled() {151return metalEnabled;152}153154public static boolean isMetalVerbose() {155return metalVerbose;156}157158public static boolean isOGLEnabled() {159return oglEnabled;160}161162public static boolean isOGLVerbose() {163return oglVerbose;164}165}166167168