Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m
41159 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*/2425#import "sun_java2d_metal_MTLGraphicsConfig.h"2627#import "MTLGraphicsConfig.h"28#import "ThreadUtilities.h"29#import "awt.h"3031/**32* Disposes all memory and resources associated with the given33* MTLGraphicsConfigInfo (including its native MTLContext data).34*/35void36MTLGC_DestroyMTLGraphicsConfig(jlong pConfigInfo)37{38J2dTraceLn(J2D_TRACE_INFO, "MTLGC_DestroyMTLGraphicsConfig");3940MTLGraphicsConfigInfo *mtlinfo =41(MTLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);42if (mtlinfo == NULL) {43J2dRlsTraceLn(J2D_TRACE_ERROR,44"MTLGC_DestroyMTLGraphicsConfig: info is null");45return;46}4748MTLContext *mtlc = (MTLContext*)mtlinfo->context;49if (mtlc != NULL) {50[mtlinfo->context release];51mtlinfo->context = nil;52}53free(mtlinfo);54}5556JNIEXPORT jboolean JNICALL57Java_sun_java2d_metal_MTLGraphicsConfig_isMetalFrameworkAvailable58(JNIEnv *env, jclass mtlgc)59{60jboolean metalSupported = JNI_FALSE;6162// It is guranteed that metal supported GPU is available macOS 10.14 onwards63if (@available(macOS 10.14, *)) {64metalSupported = JNI_TRUE;65}6667J2dRlsTraceLn1(J2D_TRACE_INFO, "MTLGraphicsConfig_isMetalFrameworkAvailable : %d", metalSupported);6869return metalSupported;70}7172JNIEXPORT jboolean JNICALL73Java_sun_java2d_metal_MTLGraphicsConfig_tryLoadMetalLibrary74(JNIEnv *env, jclass mtlgc, jint displayID, jstring shadersLibName)75{76__block jboolean ret = JNI_FALSE;7778JNI_COCOA_ENTER(env);7980__block NSString* path = NormalizedPathNSStringFromJavaString(env, shadersLibName);8182[ThreadUtilities performOnMainThreadWaiting:YES block:^() {8384id<MTLDevice> device = CGDirectDisplayCopyCurrentMetalDevice(displayID);85if (device != nil) {86NSError* error = nil;87id<MTLLibrary> lib = [device newLibraryWithFile:path error:&error];88if (lib != nil) {89ret = JNI_TRUE;90} else {91J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_tryLoadMetalLibrary - Failed to load Metal shader library.");92}93} else {94J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_tryLoadMetalLibrary - Failed to create MTLDevice.");95}96}];9798JNI_COCOA_EXIT(env);99return ret;100}101102/**103* Determines whether the Metal pipeline can be used for a given screen number and104* shader library path. A MTLContext is created and the native MTLGraphicsConfigInfo105* structure is initialized for this context. A pointer to this structure is106* returned as a jlong.107* If initialization fails at any point, zero is returned, indicating that Metal pipeline108* cannot be used for this GraphicsConfig (we should fallback on an existing 2D pipeline).109*/110JNIEXPORT jlong JNICALL111Java_sun_java2d_metal_MTLGraphicsConfig_getMTLConfigInfo112(JNIEnv *env, jclass mtlgc, jint displayID, jstring mtlShadersLib)113{114__block MTLContext* mtlc = nil;115__block MTLGraphicsConfigInfo* mtlinfo = nil;116117JNI_COCOA_ENTER(env);118119__block NSString* path = NormalizedPathNSStringFromJavaString(env, mtlShadersLib);120121[ThreadUtilities performOnMainThreadWaiting:YES block:^() {122123mtlc = [[MTLContext alloc] initWithDevice:CGDirectDisplayCopyCurrentMetalDevice(displayID)124shadersLib:path];125if (mtlc != 0L) {126// create the MTLGraphicsConfigInfo record for this context127mtlinfo = (MTLGraphicsConfigInfo *)malloc(sizeof(MTLGraphicsConfigInfo));128if (mtlinfo != NULL) {129memset(mtlinfo, 0, sizeof(MTLGraphicsConfigInfo));130mtlinfo->context = mtlc;131} else {132J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_getMTLConfigInfo: could not allocate memory for mtlinfo");133[mtlc release];134mtlc = nil;135}136} else {137J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_getMTLConfigInfo: could not initialze MTLContext.");138}139}];140141JNI_COCOA_EXIT(env);142143return ptr_to_jlong(mtlinfo);144}145146JNIEXPORT jint JNICALL147Java_sun_java2d_metal_MTLGraphicsConfig_nativeGetMaxTextureSize148(JNIEnv *env, jclass mtlgc)149{150J2dTraceLn(J2D_TRACE_INFO, "MTLGraphicsConfig_nativeGetMaxTextureSize");151152return (jint)MTL_GPU_FAMILY_MAC_TXT_SIZE;153}154155156