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/opengl/CGLGraphicsConfig.m
41159 views
1
/*
2
* Copyright (c) 2011, 2020, 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_opengl_CGLGraphicsConfig.h"
27
28
#import "CGLGraphicsConfig.h"
29
#import "CGLSurfaceData.h"
30
#import "ThreadUtilities.h"
31
#import "JNIUtilities.h"
32
33
#import <stdlib.h>
34
#import <string.h>
35
#import <ApplicationServices/ApplicationServices.h>
36
37
/**
38
* Disposes all memory and resources associated with the given
39
* CGLGraphicsConfigInfo (including its native OGLContext data).
40
*/
41
void
42
OGLGC_DestroyOGLGraphicsConfig(jlong pConfigInfo)
43
{
44
J2dTraceLn(J2D_TRACE_INFO, "OGLGC_DestroyOGLGraphicsConfig");
45
46
CGLGraphicsConfigInfo *cglinfo =
47
(CGLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
48
if (cglinfo == NULL) {
49
J2dRlsTraceLn(J2D_TRACE_ERROR,
50
"OGLGC_DestroyOGLGraphicsConfig: info is null");
51
return;
52
}
53
54
OGLContext *oglc = (OGLContext*)cglinfo->context;
55
if (oglc != NULL) {
56
OGLContext_DestroyContextResources(oglc);
57
58
CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
59
if (ctxinfo != NULL) {
60
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
61
[NSOpenGLContext clearCurrentContext];
62
[ctxinfo->context clearDrawable];
63
[ctxinfo->context release];
64
if (ctxinfo->scratchSurface != 0) {
65
[ctxinfo->scratchSurface release];
66
}
67
[pool drain];
68
free(ctxinfo);
69
oglc->ctxInfo = NULL;
70
}
71
cglinfo->context = NULL;
72
}
73
74
free(cglinfo);
75
}
76
77
/**
78
* This is a globally shared context used when creating textures. When any
79
* new contexts are created, they specify this context as the "share list"
80
* context, which means any texture objects created when this shared context
81
* is current will be available to any other context in any other thread.
82
*/
83
NSOpenGLContext *sharedContext = NULL;
84
NSOpenGLPixelFormat *sharedPixelFormat = NULL;
85
86
/**
87
* Attempts to initialize CGL and the core OpenGL library.
88
*/
89
JNIEXPORT jboolean JNICALL
90
Java_sun_java2d_opengl_CGLGraphicsConfig_initCGL
91
(JNIEnv *env, jclass cglgc)
92
{
93
J2dRlsTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_initCGL");
94
95
if (!OGLFuncs_OpenLibrary()) {
96
return JNI_FALSE;
97
}
98
99
if (!OGLFuncs_InitPlatformFuncs() ||
100
!OGLFuncs_InitBaseFuncs() ||
101
!OGLFuncs_InitExtFuncs())
102
{
103
OGLFuncs_CloseLibrary();
104
return JNI_FALSE;
105
}
106
return JNI_TRUE;
107
}
108
109
/**
110
* Determines whether the CGL pipeline can be used for a given GraphicsConfig.
111
* If the minimum requirements are met, the native CGLGraphicsConfigInfo
112
* structure is initialized for this GraphicsConfig with the necessary
113
* information and a pointer to this structure is returned as a jlong. If
114
* initialization fails at any point, zero is returned, indicating that CGL
115
* cannot be used for this GraphicsConfig (we should fallback on an existing 2D
116
* pipeline).
117
*/
118
JNIEXPORT jlong JNICALL
119
Java_sun_java2d_opengl_CGLGraphicsConfig_getCGLConfigInfo
120
(JNIEnv *env, jclass cglgc)
121
{
122
__block jlong ret = 0L;
123
JNI_COCOA_ENTER(env);
124
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
125
126
JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
127
128
J2dRlsTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_getCGLConfigInfo");
129
130
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
131
132
if (sharedContext == NULL) {
133
134
NSOpenGLPixelFormatAttribute attrs[] = {
135
NSOpenGLPFAAllowOfflineRenderers,
136
NSOpenGLPFAClosestPolicy,
137
NSOpenGLPFAWindow,
138
NSOpenGLPFAPixelBuffer,
139
NSOpenGLPFADoubleBuffer,
140
NSOpenGLPFAColorSize, 32,
141
NSOpenGLPFAAlphaSize, 8,
142
NSOpenGLPFADepthSize, 16,
143
0
144
};
145
146
sharedPixelFormat =
147
[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
148
if (sharedPixelFormat == nil) {
149
J2dRlsTraceLn(J2D_TRACE_ERROR,
150
"CGLGraphicsConfig_getCGLConfigInfo: shared NSOpenGLPixelFormat is NULL");
151
return;
152
}
153
154
sharedContext =
155
[[NSOpenGLContext alloc]
156
initWithFormat:sharedPixelFormat
157
shareContext: NULL];
158
if (sharedContext == nil) {
159
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: shared NSOpenGLContext is NULL");
160
return;
161
}
162
}
163
164
#if USE_NSVIEW_FOR_SCRATCH
165
NSRect contentRect = NSMakeRect(0, 0, 64, 64);
166
NSWindow *window =
167
[[NSWindow alloc]
168
initWithContentRect: contentRect
169
styleMask: NSBorderlessWindowMask
170
backing: NSBackingStoreBuffered
171
defer: false];
172
if (window == nil) {
173
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: NSWindow is NULL");
174
return;
175
}
176
177
NSView *scratchSurface =
178
[[NSView alloc]
179
initWithFrame: contentRect];
180
if (scratchSurface == nil) {
181
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: NSView is NULL");
182
return;
183
}
184
[window setContentView: scratchSurface];
185
#else
186
NSOpenGLPixelBuffer *scratchSurface =
187
[[NSOpenGLPixelBuffer alloc]
188
initWithTextureTarget:GL_TEXTURE_2D
189
textureInternalFormat:GL_RGB
190
textureMaxMipMapLevel:0
191
pixelsWide:64
192
pixelsHigh:64];
193
#endif
194
195
NSOpenGLContext *context =
196
[[NSOpenGLContext alloc]
197
initWithFormat: sharedPixelFormat
198
shareContext: sharedContext];
199
if (context == nil) {
200
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: NSOpenGLContext is NULL");
201
return;
202
}
203
204
GLint contextVirtualScreen = [context currentVirtualScreen];
205
#if USE_NSVIEW_FOR_SCRATCH
206
[context setView: scratchSurface];
207
#else
208
[context
209
setPixelBuffer: scratchSurface
210
cubeMapFace:0
211
mipMapLevel:0
212
currentVirtualScreen: contextVirtualScreen];
213
#endif
214
[context makeCurrentContext];
215
216
// get version and extension strings
217
const unsigned char *versionstr = j2d_glGetString(GL_VERSION);
218
if (!OGLContext_IsVersionSupported(versionstr)) {
219
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: OpenGL 1.2 is required");
220
[NSOpenGLContext clearCurrentContext];
221
return;
222
}
223
J2dRlsTraceLn1(J2D_TRACE_INFO, "CGLGraphicsConfig_getCGLConfigInfo: OpenGL version=%s", versionstr);
224
225
jint caps = CAPS_EMPTY;
226
OGLContext_GetExtensionInfo(env, &caps);
227
228
GLint value = 0;
229
[sharedPixelFormat
230
getValues: &value
231
forAttribute: NSOpenGLPFADoubleBuffer
232
forVirtualScreen: contextVirtualScreen];
233
if (value != 0) {
234
caps |= CAPS_DOUBLEBUFFERED;
235
}
236
237
J2dRlsTraceLn1(J2D_TRACE_INFO,
238
"CGLGraphicsConfig_getCGLConfigInfo: db=%d",
239
(caps & CAPS_DOUBLEBUFFERED) != 0);
240
241
// remove before shipping (?)
242
#if 1
243
[sharedPixelFormat
244
getValues: &value
245
forAttribute: NSOpenGLPFAAccelerated
246
forVirtualScreen: contextVirtualScreen];
247
if (value == 0) {
248
[sharedPixelFormat
249
getValues: &value
250
forAttribute: NSOpenGLPFARendererID
251
forVirtualScreen: contextVirtualScreen];
252
fprintf(stderr, "WARNING: GL pipe is running in software mode (Renderer ID=0x%x)\n", (int)value);
253
}
254
#endif
255
CGLCtxInfo *ctxinfo = (CGLCtxInfo *)malloc(sizeof(CGLCtxInfo));
256
if (ctxinfo == NULL) {
257
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGC_InitOGLContext: could not allocate memory for ctxinfo");
258
[NSOpenGLContext clearCurrentContext];
259
return;
260
}
261
memset(ctxinfo, 0, sizeof(CGLCtxInfo));
262
ctxinfo->context = context;
263
ctxinfo->scratchSurface = scratchSurface;
264
265
OGLContext *oglc = (OGLContext *)malloc(sizeof(OGLContext));
266
if (oglc == 0L) {
267
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGC_InitOGLContext: could not allocate memory for oglc");
268
[NSOpenGLContext clearCurrentContext];
269
free(ctxinfo);
270
return;
271
}
272
memset(oglc, 0, sizeof(OGLContext));
273
oglc->ctxInfo = ctxinfo;
274
oglc->caps = caps;
275
276
// create the CGLGraphicsConfigInfo record for this config
277
CGLGraphicsConfigInfo *cglinfo = (CGLGraphicsConfigInfo *)malloc(sizeof(CGLGraphicsConfigInfo));
278
if (cglinfo == NULL) {
279
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: could not allocate memory for cglinfo");
280
[NSOpenGLContext clearCurrentContext];
281
free(oglc);
282
free(ctxinfo);
283
return;
284
}
285
memset(cglinfo, 0, sizeof(CGLGraphicsConfigInfo));
286
cglinfo->context = oglc;
287
288
[NSOpenGLContext clearCurrentContext];
289
ret = ptr_to_jlong(cglinfo);
290
[pool drain];
291
292
}];
293
JNI_COCOA_EXIT(env);
294
return ret;
295
}
296
297
JNIEXPORT jint JNICALL
298
Java_sun_java2d_opengl_CGLGraphicsConfig_getOGLCapabilities
299
(JNIEnv *env, jclass cglgc, jlong configInfo)
300
{
301
J2dTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_getOGLCapabilities");
302
303
CGLGraphicsConfigInfo *cglinfo =
304
(CGLGraphicsConfigInfo *)jlong_to_ptr(configInfo);
305
if ((cglinfo == NULL) || (cglinfo->context == NULL)) {
306
return CAPS_EMPTY;
307
} else {
308
return cglinfo->context->caps;
309
}
310
}
311
312
JNIEXPORT jint JNICALL
313
Java_sun_java2d_opengl_CGLGraphicsConfig_nativeGetMaxTextureSize
314
(JNIEnv *env, jclass cglgc)
315
{
316
J2dTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_nativeGetMaxTextureSize");
317
318
__block int max = 0;
319
320
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
321
[sharedContext makeCurrentContext];
322
j2d_glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
323
[NSOpenGLContext clearCurrentContext];
324
}];
325
326
return (jint)max;
327
}
328
329