aboutsummaryrefslogtreecommitdiff
path: root/src/macosx/native/sun/java2d/opengl/CGLGraphicsConfig.m
blob: 647042f578f0d8ad87e66144bc1b1c8f3be82cfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

#import <stdlib.h>
#import <string.h>
#import <ApplicationServices/ApplicationServices.h>
#import <JavaNativeFoundation/JavaNativeFoundation.h>

#import "sun_java2d_opengl_CGLGraphicsConfig.h"

#import "jni.h"
#import "jni_util.h"
#import "CGLGraphicsConfig.h"
#import "CGLSurfaceData.h"
#import "LWCToolkit.h"
#import "ThreadUtilities.h"

#pragma mark -
#pragma mark "--- Mac OS X specific methods for GL pipeline ---"

/**
 * Disposes all memory and resources associated with the given
 * CGLGraphicsConfigInfo (including its native OGLContext data).
 */
void
OGLGC_DestroyOGLGraphicsConfig(jlong pConfigInfo)
{
    J2dTraceLn(J2D_TRACE_INFO, "OGLGC_DestroyOGLGraphicsConfig");

    CGLGraphicsConfigInfo *cglinfo =
        (CGLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
    if (cglinfo == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR,
                      "OGLGC_DestroyOGLGraphicsConfig: info is null");
        return;
    }

    OGLContext *oglc = (OGLContext*)cglinfo->context;
    if (oglc != NULL) {
        OGLContext_DestroyContextResources(oglc);

        CGLCtxInfo *ctxinfo = (CGLCtxInfo *)oglc->ctxInfo;
        if (ctxinfo != NULL) {
            NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];        
            [NSOpenGLContext clearCurrentContext];
            [ctxinfo->context clearDrawable];
            [ctxinfo->context release];
            if (ctxinfo->scratchSurface != 0) {
                [ctxinfo->scratchSurface release];
            }
            [pool drain];
            free(ctxinfo);
        }
    }

    free(cglinfo);
}

#pragma mark -
#pragma mark "--- CGLGraphicsConfig methods ---"

#ifdef REMOTELAYER
mach_port_t JRSRemotePort;
int remoteSocketFD = -1;

static void *JRSRemoteThreadFn(void *data) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // Negotiate a unix domain socket to communicate the
    // out of band data: to read the mach port server name, and
    // subsequently write out the layer ID.
    static char* sock_path = "/tmp/JRSRemoteDemoSocket";
    struct sockaddr_un address;
    int  socket_fd, nbytes;
    int BUFLEN = 256;
    char buffer[BUFLEN];

    remoteSocketFD = socket(PF_LOCAL, SOCK_STREAM, 0);
    if (remoteSocketFD < 0) {
        NSLog(@"socket() failed");
        return NULL;
    }
    memset(&address, 0, sizeof(struct sockaddr_un));
    address.sun_family = AF_UNIX;
    memcpy(address.sun_path, sock_path, strlen(sock_path)+1);
    int tries=0, status=-1;
    while (status !=0 && tries<600) {
        status = connect(remoteSocketFD, (struct sockaddr *) &address,
                         sizeof(struct sockaddr_un));
        if (status != 0) {
            tries++;
            NSLog(@"connection attempt %d failed.", tries);
            usleep(5000000);
        }
    }
    if (status != 0) {
        NSLog(@"failed to connect");
        return NULL;
    }
    nbytes = read(remoteSocketFD, buffer, BUFLEN);
    NSString* serverString = [[NSString alloc] initWithUTF8String:buffer];
    CFRetain(serverString);
    NSLog(@"Read server name %@", serverString);
    JRSRemotePort = [JRSRenderServer recieveRenderServer:serverString];
    NSLog(@"Read server port %d", JRSRemotePort);

    [pool drain];
    return NULL;
}

void sendLayerID(int layerID) {
    if (JRSRemotePort == 0 || remoteSocketFD < 0) {
        NSLog(@"No connection to send ID");
        return;
    }
    int BUFLEN = 256;
    char buffer[BUFLEN];
    snprintf(buffer, BUFLEN, "%d", layerID);
    write(remoteSocketFD, buffer, BUFLEN);
}
#endif  /* REMOTELAYER */

/**
 * This is a globally shared context used when creating textures.  When any
 * new contexts are created, they specify this context as the "share list"
 * context, which means any texture objects created when this shared context
 * is current will be available to any other context in any other thread.
 */
NSOpenGLContext *sharedContext = NULL;
NSOpenGLPixelFormat *sharedPixelFormat = NULL;

/**
 * Attempts to initialize CGL and the core OpenGL library.
 */
JNIEXPORT jboolean JNICALL
Java_sun_java2d_opengl_CGLGraphicsConfig_initCGL
    (JNIEnv *env, jclass cglgc)
{
    J2dRlsTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_initCGL");

    if (!OGLFuncs_OpenLibrary()) {
        return JNI_FALSE;
    }

    if (!OGLFuncs_InitPlatformFuncs() ||
        !OGLFuncs_InitBaseFuncs() ||
        !OGLFuncs_InitExtFuncs())
    {
        OGLFuncs_CloseLibrary();
        return JNI_FALSE;
    }
#ifdef REMOTELAYER
    pthread_t jrsRemoteThread;
    pthread_create(&jrsRemoteThread, NULL, JRSRemoteThreadFn, NULL);
#endif
    return JNI_TRUE;
}


/**
 * Determines whether the CGL pipeline can be used for a given GraphicsConfig
 * provided its screen number and visual ID.  If the minimum requirements are
 * met, the native CGLGraphicsConfigInfo structure is initialized for this
 * GraphicsConfig with the necessary information (pixel format, etc.)
 * and a pointer to this structure is returned as a jlong.  If
 * initialization fails at any point, zero is returned, indicating that CGL
 * cannot be used for this GraphicsConfig (we should fallback on an existing
 * 2D pipeline).
 */
JNIEXPORT jlong JNICALL
Java_sun_java2d_opengl_CGLGraphicsConfig_getCGLConfigInfo
    (JNIEnv *env, jclass cglgc,
     jint displayID, jint pixfmt, jint swapInterval)
{
  jlong ret = 0L;
  JNF_COCOA_ENTER(env);
  NSMutableArray * retArray = [NSMutableArray arrayWithCapacity:3];
  [retArray addObject: [NSNumber numberWithInt: (int)displayID]];
  [retArray addObject: [NSNumber numberWithInt: (int)pixfmt]];
  [retArray addObject: [NSNumber numberWithInt: (int)swapInterval]];
  if ([NSThread isMainThread]) {
      [GraphicsConfigUtil _getCGLConfigInfo: retArray];
  } else {
      [GraphicsConfigUtil performSelectorOnMainThread: @selector(_getCGLConfigInfo:) withObject: retArray waitUntilDone: YES];
  }
  NSNumber * num = (NSNumber *)[retArray objectAtIndex: 0];
  ret = (jlong)[num longValue];
  JNF_COCOA_EXIT(env);
  return ret;
}



@implementation GraphicsConfigUtil
+ (void) _getCGLConfigInfo: (NSMutableArray *)argValue {
    AWT_ASSERT_APPKIT_THREAD;

    jint displayID = (jint)[(NSNumber *)[argValue objectAtIndex: 0] intValue];
    jint pixfmt = (jint)[(NSNumber *)[argValue objectAtIndex: 1] intValue];
    jint swapInterval = (jint)[(NSNumber *)[argValue objectAtIndex: 2] intValue];
    JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
    [argValue removeAllObjects];

    J2dRlsTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_getCGLConfigInfo");

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    CGOpenGLDisplayMask glMask = (CGOpenGLDisplayMask)pixfmt;
    if (sharedContext == NULL) {
        if (glMask == 0) {
            glMask = CGDisplayIDToOpenGLDisplayMask(displayID);
        }

        NSOpenGLPixelFormatAttribute attrs[] = {
            NSOpenGLPFAClosestPolicy,
            NSOpenGLPFAWindow,
            NSOpenGLPFAPixelBuffer,
            NSOpenGLPFADoubleBuffer,
            NSOpenGLPFAColorSize, 32,
            NSOpenGLPFAAlphaSize, 8,
            NSOpenGLPFADepthSize, 16,
            NSOpenGLPFAScreenMask, glMask,
            0
        };

        sharedPixelFormat =
            [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
        if (sharedPixelFormat == nil) {
            J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: shared NSOpenGLPixelFormat is NULL");
            [argValue addObject: [NSNumber numberWithLong: 0L]];
            return;
        }

        sharedContext =
            [[NSOpenGLContext alloc]
                initWithFormat:sharedPixelFormat
                shareContext: NULL];
        if (sharedContext == nil) {
            J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: shared NSOpenGLContext is NULL");
            [argValue addObject: [NSNumber numberWithLong: 0L]];
            return;
        }
    }

#if USE_NSVIEW_FOR_SCRATCH
    NSRect contentRect = NSMakeRect(0, 0, 64, 64);
    NSWindow *window =
        [[NSWindow alloc]
            initWithContentRect: contentRect
            styleMask: NSBorderlessWindowMask
            backing: NSBackingStoreBuffered
            defer: false];
    if (window == nil) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: NSWindow is NULL");
        [argValue addObject: [NSNumber numberWithLong: 0L]];
        return;
    }

    NSView *scratchSurface =
        [[NSView alloc]
            initWithFrame: contentRect];
    if (scratchSurface == nil) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: NSView is NULL");
        [argValue addObject: [NSNumber numberWithLong: 0L]];
        return;
    }
    [window setContentView: scratchSurface];
#else
    NSOpenGLPixelBuffer *scratchSurface =
        [[NSOpenGLPixelBuffer alloc]
            initWithTextureTarget:GL_TEXTURE_2D
            textureInternalFormat:GL_RGB
            textureMaxMipMapLevel:0
            pixelsWide:64
            pixelsHigh:64];
#endif

    NSOpenGLContext *context =
        [[NSOpenGLContext alloc]
            initWithFormat: sharedPixelFormat
            shareContext: sharedContext];
    if (context == nil) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: NSOpenGLContext is NULL");
        [argValue addObject: [NSNumber numberWithLong: 0L]];
        return;
    }

    GLint contextVirtualScreen = [context currentVirtualScreen];
#if USE_NSVIEW_FOR_SCRATCH
    [context setView: scratchSurface];
#else
    [context
        setPixelBuffer: scratchSurface
        cubeMapFace:0
        mipMapLevel:0
        currentVirtualScreen: contextVirtualScreen];
#endif
    [context makeCurrentContext];

    // get version and extension strings
    const unsigned char *versionstr = j2d_glGetString(GL_VERSION);
    if (!OGLContext_IsVersionSupported(versionstr)) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: OpenGL 1.2 is required");
        [NSOpenGLContext clearCurrentContext];
        [argValue addObject: [NSNumber numberWithLong: 0L]];
        return;
    }
    J2dRlsTraceLn1(J2D_TRACE_INFO, "CGLGraphicsConfig_getCGLConfigInfo: OpenGL version=%s", versionstr);

    jint caps = CAPS_EMPTY;
    OGLContext_GetExtensionInfo(env, &caps);

    GLint value = 0;
    [sharedPixelFormat
        getValues: &value
        forAttribute: NSOpenGLPFADoubleBuffer
        forVirtualScreen: contextVirtualScreen];
    if (value != 0) {
        caps |= CAPS_DOUBLEBUFFERED;
    }
    [sharedPixelFormat
        getValues: &value
        forAttribute: NSOpenGLPFAAlphaSize
        forVirtualScreen: contextVirtualScreen];
    if (value != 0) {
        caps |= CAPS_STORED_ALPHA;
    }

    J2dRlsTraceLn2(J2D_TRACE_INFO,
                   "CGLGraphicsConfig_getCGLConfigInfo: db=%d alpha=%d",
                   (caps & CAPS_DOUBLEBUFFERED) != 0,
                   (caps & CAPS_STORED_ALPHA) != 0);

    // remove before shipping (?)
#if 1
    [sharedPixelFormat
        getValues: &value
        forAttribute: NSOpenGLPFAAccelerated
        forVirtualScreen: contextVirtualScreen];
    if (value == 0) {
        [sharedPixelFormat
            getValues: &value
            forAttribute: NSOpenGLPFARendererID
            forVirtualScreen: contextVirtualScreen];
        fprintf(stderr, "WARNING: GL pipe is running in software mode (Renderer ID=0x%x)\n", (int)value);
    }
#endif

    // 0: the buffers are swapped with no regard to the vertical refresh rate
    // 1: the buffers are swapped only during the vertical retrace
    GLint params = swapInterval;
    [context setValues: &params forParameter: NSOpenGLCPSwapInterval];

    CGLCtxInfo *ctxinfo = (CGLCtxInfo *)malloc(sizeof(CGLCtxInfo));
    if (ctxinfo == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGC_InitOGLContext: could not allocate memory for ctxinfo");
        [NSOpenGLContext clearCurrentContext];
        [argValue addObject: [NSNumber numberWithLong: 0L]];
        return;
    }
    memset(ctxinfo, 0, sizeof(CGLCtxInfo));
    ctxinfo->context = context;
    ctxinfo->scratchSurface = scratchSurface;

    OGLContext *oglc = (OGLContext *)malloc(sizeof(OGLContext));
    if (oglc == 0L) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGC_InitOGLContext: could not allocate memory for oglc");
        [NSOpenGLContext clearCurrentContext];
        free(ctxinfo);
        [argValue addObject: [NSNumber numberWithLong: 0L]];
        return;
    }
    memset(oglc, 0, sizeof(OGLContext));
    oglc->ctxInfo = ctxinfo;
    oglc->caps = caps;

    // create the CGLGraphicsConfigInfo record for this config
    CGLGraphicsConfigInfo *cglinfo = (CGLGraphicsConfigInfo *)malloc(sizeof(CGLGraphicsConfigInfo));
    if (cglinfo == NULL) {
        J2dRlsTraceLn(J2D_TRACE_ERROR, "CGLGraphicsConfig_getCGLConfigInfo: could not allocate memory for cglinfo");
        [NSOpenGLContext clearCurrentContext];
        free(oglc);
        free(ctxinfo);
        [argValue addObject: [NSNumber numberWithLong: 0L]];
        return;
    }
    memset(cglinfo, 0, sizeof(CGLGraphicsConfigInfo));
    cglinfo->screen = displayID;
    cglinfo->pixfmt = sharedPixelFormat;
    cglinfo->context = oglc;

    [NSOpenGLContext clearCurrentContext];
    [argValue addObject: [NSNumber numberWithLong:ptr_to_jlong(cglinfo)]];
    [pool drain];
}
@end //GraphicsConfigUtil

JNIEXPORT jint JNICALL
Java_sun_java2d_opengl_CGLGraphicsConfig_getOGLCapabilities
    (JNIEnv *env, jclass cglgc, jlong configInfo)
{
    J2dTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_getOGLCapabilities");

    CGLGraphicsConfigInfo *cglinfo =
        (CGLGraphicsConfigInfo *)jlong_to_ptr(configInfo);
    if ((cglinfo == NULL) || (cglinfo->context == NULL)) {
        return CAPS_EMPTY;
    } else {
        return cglinfo->context->caps;
    }
}

JNIEXPORT jint JNICALL
Java_sun_java2d_opengl_CGLGraphicsConfig_nativeGetMaxTextureSize
    (JNIEnv *env, jclass cglgc)
{
    J2dTraceLn(J2D_TRACE_INFO, "CGLGraphicsConfig_nativeGetMaxTextureSize");

    __block int max = 0;

    [ThreadUtilities performOnMainThreadWaiting:YES block:^(){
        [sharedContext makeCurrentContext];
        j2d_glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
        [NSOpenGLContext clearCurrentContext];
    }];

    return (jint)max;
}