Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m
41159 views
1
/*
2
* Copyright (c) 2019, 2021, 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
#import "sun_java2d_metal_MTLGraphicsConfig.h"
27
28
#import "MTLGraphicsConfig.h"
29
#import "ThreadUtilities.h"
30
#import "awt.h"
31
32
/**
33
* Disposes all memory and resources associated with the given
34
* MTLGraphicsConfigInfo (including its native MTLContext data).
35
*/
36
void
37
MTLGC_DestroyMTLGraphicsConfig(jlong pConfigInfo)
38
{
39
J2dTraceLn(J2D_TRACE_INFO, "MTLGC_DestroyMTLGraphicsConfig");
40
41
MTLGraphicsConfigInfo *mtlinfo =
42
(MTLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
43
if (mtlinfo == NULL) {
44
J2dRlsTraceLn(J2D_TRACE_ERROR,
45
"MTLGC_DestroyMTLGraphicsConfig: info is null");
46
return;
47
}
48
49
MTLContext *mtlc = (MTLContext*)mtlinfo->context;
50
if (mtlc != NULL) {
51
[mtlinfo->context release];
52
mtlinfo->context = nil;
53
}
54
free(mtlinfo);
55
}
56
57
JNIEXPORT jboolean JNICALL
58
Java_sun_java2d_metal_MTLGraphicsConfig_isMetalFrameworkAvailable
59
(JNIEnv *env, jclass mtlgc)
60
{
61
jboolean metalSupported = JNI_FALSE;
62
63
// It is guranteed that metal supported GPU is available macOS 10.14 onwards
64
if (@available(macOS 10.14, *)) {
65
metalSupported = JNI_TRUE;
66
}
67
68
J2dRlsTraceLn1(J2D_TRACE_INFO, "MTLGraphicsConfig_isMetalFrameworkAvailable : %d", metalSupported);
69
70
return metalSupported;
71
}
72
73
JNIEXPORT jboolean JNICALL
74
Java_sun_java2d_metal_MTLGraphicsConfig_tryLoadMetalLibrary
75
(JNIEnv *env, jclass mtlgc, jint displayID, jstring shadersLibName)
76
{
77
__block jboolean ret = JNI_FALSE;
78
79
JNI_COCOA_ENTER(env);
80
81
__block NSString* path = NormalizedPathNSStringFromJavaString(env, shadersLibName);
82
83
[ThreadUtilities performOnMainThreadWaiting:YES block:^() {
84
85
id<MTLDevice> device = CGDirectDisplayCopyCurrentMetalDevice(displayID);
86
if (device != nil) {
87
NSError* error = nil;
88
id<MTLLibrary> lib = [device newLibraryWithFile:path error:&error];
89
if (lib != nil) {
90
ret = JNI_TRUE;
91
} else {
92
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_tryLoadMetalLibrary - Failed to load Metal shader library.");
93
}
94
} else {
95
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_tryLoadMetalLibrary - Failed to create MTLDevice.");
96
}
97
}];
98
99
JNI_COCOA_EXIT(env);
100
return ret;
101
}
102
103
/**
104
* Determines whether the Metal pipeline can be used for a given screen number and
105
* shader library path. A MTLContext is created and the native MTLGraphicsConfigInfo
106
* structure is initialized for this context. A pointer to this structure is
107
* returned as a jlong.
108
* If initialization fails at any point, zero is returned, indicating that Metal pipeline
109
* cannot be used for this GraphicsConfig (we should fallback on an existing 2D pipeline).
110
*/
111
JNIEXPORT jlong JNICALL
112
Java_sun_java2d_metal_MTLGraphicsConfig_getMTLConfigInfo
113
(JNIEnv *env, jclass mtlgc, jint displayID, jstring mtlShadersLib)
114
{
115
__block MTLContext* mtlc = nil;
116
__block MTLGraphicsConfigInfo* mtlinfo = nil;
117
118
JNI_COCOA_ENTER(env);
119
120
__block NSString* path = NormalizedPathNSStringFromJavaString(env, mtlShadersLib);
121
122
[ThreadUtilities performOnMainThreadWaiting:YES block:^() {
123
124
mtlc = [[MTLContext alloc] initWithDevice:CGDirectDisplayCopyCurrentMetalDevice(displayID)
125
shadersLib:path];
126
if (mtlc != 0L) {
127
// create the MTLGraphicsConfigInfo record for this context
128
mtlinfo = (MTLGraphicsConfigInfo *)malloc(sizeof(MTLGraphicsConfigInfo));
129
if (mtlinfo != NULL) {
130
memset(mtlinfo, 0, sizeof(MTLGraphicsConfigInfo));
131
mtlinfo->context = mtlc;
132
} else {
133
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_getMTLConfigInfo: could not allocate memory for mtlinfo");
134
[mtlc release];
135
mtlc = nil;
136
}
137
} else {
138
J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_getMTLConfigInfo: could not initialze MTLContext.");
139
}
140
}];
141
142
JNI_COCOA_EXIT(env);
143
144
return ptr_to_jlong(mtlinfo);
145
}
146
147
JNIEXPORT jint JNICALL
148
Java_sun_java2d_metal_MTLGraphicsConfig_nativeGetMaxTextureSize
149
(JNIEnv *env, jclass mtlgc)
150
{
151
J2dTraceLn(J2D_TRACE_INFO, "MTLGraphicsConfig_nativeGetMaxTextureSize");
152
153
return (jint)MTL_GPU_FAMILY_MAC_TXT_SIZE;
154
}
155
156