Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/sun/java2d/opengl/CGLGraphicsConfig.m
38829 views
1
/*
2
* Copyright (c) 2011, 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
#import <stdlib.h>
27
#import <string.h>
28
#import <ApplicationServices/ApplicationServices.h>
29
#import <JavaNativeFoundation/JavaNativeFoundation.h>
30
31
#import "sun_java2d_opengl_CGLGraphicsConfig.h"
32
33
#import "jni.h"
34
#import "jni_util.h"
35
#import "CGLGraphicsConfig.h"
36
#import "CGLSurfaceData.h"
37
#import "LWCToolkit.h"
38
#import "ThreadUtilities.h"
39
40
#pragma mark -
41
#pragma mark "--- Mac OS X specific methods for GL pipeline ---"
42
43
/**
44
* Disposes all memory and resources associated with the given
45
* CGLGraphicsConfigInfo (including its native OGLContext data).
46
*/
47
void
48
OGLGC_DestroyOGLGraphicsConfig(jlong pConfigInfo)
49
{
50
J2dTraceLn(J2D_TRACE_INFO, "OGLGC_DestroyOGLGraphicsConfig");
51
52
CGLGraphicsConfigInfo *cglinfo =
53
(CGLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
54
if (cglinfo == NULL) {
55
J2dRlsTraceLn(J2D_TRACE_ERROR,
56
"OGLGC_DestroyOGLGraphicsConfig: info is null");
57
return;
58
}
59
60
OGLContext *oglc = (OGLContext*)cglinfo->context;
61
if (oglc != NULL) {
62
OGLContext_DestroyContextResources(oglc);
63
64
CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
65
if (ctxinfo != NULL) {
66
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
67
[NSOpenGLContext clearCurrentContext];
68
[ctxinfo->context clearDrawable];
69
[ctxinfo->context release];
70
if (ctxinfo->scratchSurface != 0) {
71
[ctxinfo->scratchSurface release];
72
}
73
[pool drain];
74
free(ctxinfo);
75
oglc->ctxInfo = NULL;
76
}
77
cglinfo->context = NULL;
78
}
79
80
free(cglinfo);
81
}
82
83
#pragma mark -
84
#pragma mark "--- CGLGraphicsConfig methods ---"
85
86
#ifdef REMOTELAYER
87
mach_port_t JRSRemotePort;
88
int remoteSocketFD = -1;
89
90
static void *JRSRemoteThreadFn(void *data) {
91
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
92
93
// Negotiate a unix domain socket to communicate the
94
// out of band data: to read the mach port server name, and
95
// subsequently write out the layer ID.
96
static char* sock_path = "/tmp/JRSRemoteDemoSocket";
97
struct sockaddr_un address;
98
int socket_fd, nbytes;
99
int BUFLEN = 256;
100
char buffer[BUFLEN];
101
102
remoteSocketFD = socket(PF_LOCAL, SOCK_STREAM, 0);
103
if (remoteSocketFD < 0) {
104
NSLog(@"socket() failed");
105
return NULL;
106
}
107
memset(&address, 0, sizeof(struct sockaddr_un));
108
address.sun_family = AF_UNIX;
109
memcpy(address.sun_path, sock_path, strlen(sock_path)+1);
110
int tries=0, status=-1;
111
while (status !=0 && tries<600) {
112
status = connect(remoteSocketFD, (struct sockaddr *) &address,
113
sizeof(struct sockaddr_un));
114
if (status != 0) {
115
tries++;
116
NSLog(@"connection attempt %d failed.", tries);
117
usleep(5000000);
118
}
119
}
120
if (status != 0) {
121
NSLog(@"failed to connect");
122
return NULL;
123
}
124
nbytes = read(remoteSocketFD, buffer, BUFLEN);
125
NSString* serverString = [[NSString alloc] initWithUTF8String:buffer];
126
CFRetain(serverString);
127
NSLog(@"Read server name %@", serverString);
128
JRSRemotePort = [JRSRenderServer recieveRenderServer:serverString];
129
NSLog(@"Read server port %d", JRSRemotePort);
130
131
[pool drain];
132
return NULL;
133
}
134
135
void sendLayerID(int layerID) {
136
if (JRSRemotePort == 0 || remoteSocketFD < 0) {
137
NSLog(@"No connection to send ID");
138
return;
139
}
140
int BUFLEN = 256;
141
char buffer[BUFLEN];
142
snprintf(buffer, BUFLEN, "%d", layerID);
143
write(remoteSocketFD, buffer, BUFLEN);
144
}
145
#endif /* REMOTELAYER */
146
147
/**
148
* This is a globally shared context used when creating textures. When any
149
* new contexts are created, they specify this context as the "share list"
150
* context, which means any texture objects created when this shared context
151
* is current will be available to any other context in any other thread.
152
*/
153
NSOpenGLContext *sharedContext = NULL;
154
NSOpenGLPixelFormat *sharedPixelFormat = NULL;
155
156
/**
157
* Attempts to initialize CGL and the core OpenGL library.
158
*/
159
JNIEXPORT jboolean JNICALL
160
Java_sun_java2d_opengl_CGLGraphicsConfig_initCGL
161
(JNIEnv *env, jclass cglgc)
162
{
163
J2dRlsTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_initCGL");
164
165
if (!OGLFuncs_OpenLibrary()) {
166
return JNI_FALSE;
167
}
168
169
if (!OGLFuncs_InitPlatformFuncs() ||
170
!OGLFuncs_InitBaseFuncs() ||
171
!OGLFuncs_InitExtFuncs())
172
{
173
OGLFuncs_CloseLibrary();
174
return JNI_FALSE;
175
}
176
#ifdef REMOTELAYER
177
pthread_t jrsRemoteThread;
178
pthread_create(&jrsRemoteThread, NULL, JRSRemoteThreadFn, NULL);
179
#endif
180
return JNI_TRUE;
181
}
182
183
184
/**
185
* Determines whether the CGL pipeline can be used for a given GraphicsConfig
186
* provided its screen number and visual ID. If the minimum requirements are
187
* met, the native CGLGraphicsConfigInfo structure is initialized for this
188
* GraphicsConfig with the necessary information (pixel format, etc.)
189
* and a pointer to this structure is returned as a jlong. If
190
* initialization fails at any point, zero is returned, indicating that CGL
191
* cannot be used for this GraphicsConfig (we should fallback on an existing
192
* 2D pipeline).
193
*/
194
JNIEXPORT jlong JNICALL
195
Java_sun_java2d_opengl_CGLGraphicsConfig_getCGLConfigInfo
196
(JNIEnv *env, jclass cglgc,
197
jint displayID, jint pixfmt, jint swapInterval)
198
{
199
jlong ret = 0L;
200
JNF_COCOA_ENTER(env);
201
NSMutableArray * retArray = [NSMutableArray arrayWithCapacity:3];
202
[retArray addObject: [NSNumber numberWithInt: (int)displayID]];
203
[retArray addObject: [NSNumber numberWithInt: (int)pixfmt]];
204
[retArray addObject: [NSNumber numberWithInt: (int)swapInterval]];
205
if ([NSThread isMainThread]) {
206
[GraphicsConfigUtil _getCGLConfigInfo: retArray];
207
} else {
208
[GraphicsConfigUtil performSelectorOnMainThread: @selector(_getCGLConfigInfo:) withObject: retArray waitUntilDone: YES];
209
}
210
NSNumber * num = (NSNumber *)[retArray objectAtIndex: 0];
211
ret = (jlong)[num longValue];
212
JNF_COCOA_EXIT(env);
213
return ret;
214
}
215
216
217
218
@implementation GraphicsConfigUtil
219
+ (void) _getCGLConfigInfo: (NSMutableArray *)argValue {
220
AWT_ASSERT_APPKIT_THREAD;
221
222
jint displayID = (jint)[(NSNumber *)[argValue objectAtIndex: 0] intValue];
223
jint swapInterval = (jint)[(NSNumber *)[argValue objectAtIndex: 2] intValue];
224
JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
225
[argValue removeAllObjects];
226
227
J2dRlsTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_getCGLConfigInfo");
228
229
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
230
231
if (sharedContext == NULL) {
232
233
NSOpenGLPixelFormatAttribute attrs[] = {
234
NSOpenGLPFAAllowOfflineRenderers,
235
NSOpenGLPFAClosestPolicy,
236
NSOpenGLPFAWindow,
237
NSOpenGLPFAPixelBuffer,
238
NSOpenGLPFADoubleBuffer,
239
NSOpenGLPFAColorSize, 32,
240
NSOpenGLPFAAlphaSize, 8,
241
NSOpenGLPFADepthSize, 16,
242
0
243
};
244
245
sharedPixelFormat =
246
[[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
247
if (sharedPixelFormat == nil) {
248
J2dRlsTraceLn(J2D_TRACE_ERROR,
249
"CGLGraphicsConfig_getCGLConfigInfo: shared NSOpenGLPixelFormat is NULL");
250
251
[argValue addObject: [NSNumber numberWithLong: 0L]];
252
return;
253
}
254
255
sharedContext =
256
[[NSOpenGLContext alloc]
257
initWithFormat:sharedPixelFormat
258
shareContext: NULL];
259
if (sharedContext == nil) {
260
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: shared NSOpenGLContext is NULL");
261
[argValue addObject: [NSNumber numberWithLong: 0L]];
262
return;
263
}
264
}
265
266
#if USE_NSVIEW_FOR_SCRATCH
267
NSRect contentRect = NSMakeRect(0, 0, 64, 64);
268
NSWindow *window =
269
[[NSWindow alloc]
270
initWithContentRect: contentRect
271
styleMask: NSBorderlessWindowMask
272
backing: NSBackingStoreBuffered
273
defer: false];
274
if (window == nil) {
275
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: NSWindow is NULL");
276
[argValue addObject: [NSNumber numberWithLong: 0L]];
277
return;
278
}
279
280
NSView *scratchSurface =
281
[[NSView alloc]
282
initWithFrame: contentRect];
283
if (scratchSurface == nil) {
284
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: NSView is NULL");
285
[argValue addObject: [NSNumber numberWithLong: 0L]];
286
return;
287
}
288
[window setContentView: scratchSurface];
289
#else
290
NSOpenGLPixelBuffer *scratchSurface =
291
[[NSOpenGLPixelBuffer alloc]
292
initWithTextureTarget:GL_TEXTURE_2D
293
textureInternalFormat:GL_RGB
294
textureMaxMipMapLevel:0
295
pixelsWide:64
296
pixelsHigh:64];
297
#endif
298
299
NSOpenGLContext *context =
300
[[NSOpenGLContext alloc]
301
initWithFormat: sharedPixelFormat
302
shareContext: sharedContext];
303
if (context == nil) {
304
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: NSOpenGLContext is NULL");
305
[argValue addObject: [NSNumber numberWithLong: 0L]];
306
return;
307
}
308
309
GLint contextVirtualScreen = [context currentVirtualScreen];
310
#if USE_NSVIEW_FOR_SCRATCH
311
[context setView: scratchSurface];
312
#else
313
[context
314
setPixelBuffer: scratchSurface
315
cubeMapFace:0
316
mipMapLevel:0
317
currentVirtualScreen: contextVirtualScreen];
318
#endif
319
[context makeCurrentContext];
320
321
// get version and extension strings
322
const unsigned char *versionstr = j2d_glGetString(GL_VERSION);
323
if (!OGLContext_IsVersionSupported(versionstr)) {
324
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: OpenGL 1.2 is required");
325
[NSOpenGLContext clearCurrentContext];
326
[argValue addObject: [NSNumber numberWithLong: 0L]];
327
return;
328
}
329
J2dRlsTraceLn1(J2D_TRACE_INFO, "CGLGraphicsConfig_getCGLConfigInfo: OpenGL version=%s", versionstr);
330
331
jint caps = CAPS_EMPTY;
332
OGLContext_GetExtensionInfo(env, &caps);
333
334
GLint value = 0;
335
[sharedPixelFormat
336
getValues: &value
337
forAttribute: NSOpenGLPFADoubleBuffer
338
forVirtualScreen: contextVirtualScreen];
339
if (value != 0) {
340
caps |= CAPS_DOUBLEBUFFERED;
341
}
342
[sharedPixelFormat
343
getValues: &value
344
forAttribute: NSOpenGLPFAAlphaSize
345
forVirtualScreen: contextVirtualScreen];
346
if (value != 0) {
347
caps |= CAPS_STORED_ALPHA;
348
}
349
350
J2dRlsTraceLn2(J2D_TRACE_INFO,
351
"CGLGraphicsConfig_getCGLConfigInfo: db=%d alpha=%d",
352
(caps & CAPS_DOUBLEBUFFERED) != 0,
353
(caps & CAPS_STORED_ALPHA) != 0);
354
355
// remove before shipping (?)
356
#if 1
357
[sharedPixelFormat
358
getValues: &value
359
forAttribute: NSOpenGLPFAAccelerated
360
forVirtualScreen: contextVirtualScreen];
361
if (value == 0) {
362
[sharedPixelFormat
363
getValues: &value
364
forAttribute: NSOpenGLPFARendererID
365
forVirtualScreen: contextVirtualScreen];
366
fprintf(stderr, "WARNING: GL pipe is running in software mode (Renderer ID=0x%x)\n", (int)value);
367
}
368
#endif
369
370
// 0: the buffers are swapped with no regard to the vertical refresh rate
371
// 1: the buffers are swapped only during the vertical retrace
372
GLint params = swapInterval;
373
[context setValues: &params forParameter: NSOpenGLCPSwapInterval];
374
375
CGLCtxInfo *ctxinfo = (CGLCtxInfo *)malloc(sizeof(CGLCtxInfo));
376
if (ctxinfo == NULL) {
377
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGC_InitOGLContext: could not allocate memory for ctxinfo");
378
[NSOpenGLContext clearCurrentContext];
379
[argValue addObject: [NSNumber numberWithLong: 0L]];
380
return;
381
}
382
memset(ctxinfo, 0, sizeof(CGLCtxInfo));
383
ctxinfo->context = context;
384
ctxinfo->scratchSurface = scratchSurface;
385
386
OGLContext *oglc = (OGLContext *)malloc(sizeof(OGLContext));
387
if (oglc == 0L) {
388
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGC_InitOGLContext: could not allocate memory for oglc");
389
[NSOpenGLContext clearCurrentContext];
390
free(ctxinfo);
391
[argValue addObject: [NSNumber numberWithLong: 0L]];
392
return;
393
}
394
memset(oglc, 0, sizeof(OGLContext));
395
oglc->ctxInfo = ctxinfo;
396
oglc->caps = caps;
397
398
// create the CGLGraphicsConfigInfo record for this config
399
CGLGraphicsConfigInfo *cglinfo = (CGLGraphicsConfigInfo *)malloc(sizeof(CGLGraphicsConfigInfo));
400
if (cglinfo == NULL) {
401
J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: could not allocate memory for cglinfo");
402
[NSOpenGLContext clearCurrentContext];
403
free(oglc);
404
free(ctxinfo);
405
[argValue addObject: [NSNumber numberWithLong: 0L]];
406
return;
407
}
408
memset(cglinfo, 0, sizeof(CGLGraphicsConfigInfo));
409
cglinfo->screen = displayID;
410
cglinfo->pixfmt = sharedPixelFormat;
411
cglinfo->context = oglc;
412
413
[NSOpenGLContext clearCurrentContext];
414
[argValue addObject: [NSNumber numberWithLong:ptr_to_jlong(cglinfo)]];
415
[pool drain];
416
}
417
@end //GraphicsConfigUtil
418
419
JNIEXPORT jint JNICALL
420
Java_sun_java2d_opengl_CGLGraphicsConfig_getOGLCapabilities
421
(JNIEnv *env, jclass cglgc, jlong configInfo)
422
{
423
J2dTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_getOGLCapabilities");
424
425
CGLGraphicsConfigInfo *cglinfo =
426
(CGLGraphicsConfigInfo *)jlong_to_ptr(configInfo);
427
if ((cglinfo == NULL) || (cglinfo->context == NULL)) {
428
return CAPS_EMPTY;
429
} else {
430
return cglinfo->context->caps;
431
}
432
}
433
434
JNIEXPORT jint JNICALL
435
Java_sun_java2d_opengl_CGLGraphicsConfig_nativeGetMaxTextureSize
436
(JNIEnv *env, jclass cglgc)
437
{
438
J2dTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_nativeGetMaxTextureSize");
439
440
__block int max = 0;
441
442
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
443
[sharedContext makeCurrentContext];
444
j2d_glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
445
[NSOpenGLContext clearCurrentContext];
446
}];
447
448
return (jint)max;
449
}
450
451