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