From ade2381d214fdcddb265629227586d68a21a3490 Mon Sep 17 00:00:00 2001 From: aghaisas Date: Tue, 6 Apr 2021 19:34:40 +0530 Subject: [PATCH 1/5] 8263363 - cleanup --- .../classes/sun/awt/CGraphicsConfig.java | 2 +- .../sun/java2d/metal/MTLGraphicsConfig.java | 5 ++--- .../java2d/metal/MTLGraphicsConfig.m | 18 ++---------------- 3 files changed, 5 insertions(+), 20 deletions(-) diff --git a/src/java.desktop/macosx/classes/sun/awt/CGraphicsConfig.java b/src/java.desktop/macosx/classes/sun/awt/CGraphicsConfig.java index 9e8c043184f85..dd4d17be3a17b 100644 --- a/src/java.desktop/macosx/classes/sun/awt/CGraphicsConfig.java +++ b/src/java.desktop/macosx/classes/sun/awt/CGraphicsConfig.java @@ -78,7 +78,7 @@ public AffineTransform getNormalizingTransform() { /** * Creates a new SurfaceData that will be associated with the given - * CGLLayer. + * layer (CGLLayer/MTLLayer). */ public abstract SurfaceData createSurfaceData(CFRetainedResource layer); diff --git a/src/java.desktop/macosx/classes/sun/java2d/metal/MTLGraphicsConfig.java b/src/java.desktop/macosx/classes/sun/java2d/metal/MTLGraphicsConfig.java index 24602587e17ac..d3eb8ef24a3d2 100644 --- a/src/java.desktop/macosx/classes/sun/java2d/metal/MTLGraphicsConfig.java +++ b/src/java.desktop/macosx/classes/sun/java2d/metal/MTLGraphicsConfig.java @@ -145,9 +145,8 @@ public static MTLGraphicsConfig getConfig(CGraphicsDevice device, MTLRenderQueue rq = MTLRenderQueue.getInstance(); rq.lock(); try { - // getMTLConfigInfo() creates and destroys temporary - // surfaces/contexts, so we should first invalidate the current - // Java-level context and flush the queue... + // getMTLConfigInfo() creates new MTLContext, so we should first + // invalidate the current Java-level context and flush the queue... MTLContext.invalidateCurrentContext(); cfginfo = getMTLConfigInfo(displayID, mtlShadersLib); if (cfginfo != 0L) { diff --git a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m index 8ac597f7c22a8..5add71fc4b14e 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m +++ b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m @@ -149,20 +149,6 @@ + (void) _getMTLConfigInfo: (NSMutableArray *)argValue { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; - - 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, "MTLGraphicsConfig_getMTLConfigInfo: NSWindow is NULL"); - [argValue addObject: [NSNumber numberWithLong: 0L]]; - return; - } - MTLContext *mtlc = [[MTLContext alloc] initWithDevice:CGDirectDisplayCopyCurrentMetalDevice(displayID) shadersLib:mtlShadersLib]; if (mtlc == 0L) { @@ -171,12 +157,12 @@ + (void) _getMTLConfigInfo: (NSMutableArray *)argValue { return; } - // create the MTLGraphicsConfigInfo record for this config MTLGraphicsConfigInfo *mtlinfo = (MTLGraphicsConfigInfo *)malloc(sizeof(MTLGraphicsConfigInfo)); if (mtlinfo == NULL) { J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_getMTLConfigInfo: could not allocate memory for mtlinfo"); - free(mtlc); + [mtlc release]; + mtlc = nil; [argValue addObject: [NSNumber numberWithLong: 0L]]; return; } From 36ed087be24b7342854b442dd4757d656d1690df Mon Sep 17 00:00:00 2001 From: aghaisas Date: Thu, 8 Apr 2021 16:14:27 +0530 Subject: [PATCH 2/5] Review fixes --- .../java2d/metal/MTLGraphicsConfig.m | 155 ++++++------------ 1 file changed, 47 insertions(+), 108 deletions(-) diff --git a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m index 5add71fc4b14e..798bbd5e4dad3 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m +++ b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m @@ -26,13 +26,9 @@ #import "sun_java2d_metal_MTLGraphicsConfig.h" #import "MTLGraphicsConfig.h" -#import "MTLSurfaceData.h" #import "ThreadUtilities.h" #import "awt.h" -#pragma mark - -#pragma mark "--- Mac OS X specific methods for Metal pipeline ---" - /** * Disposes all memory and resources associated with the given * MTLGraphicsConfigInfo (including its native MTLContext data). @@ -58,10 +54,6 @@ free(mtlinfo); } -#pragma mark - -#pragma mark "--- MTLGraphicsConfig methods ---" - - JNIEXPORT jboolean JNICALL Java_sun_java2d_metal_MTLGraphicsConfig_isMetalFrameworkAvailable (JNIEnv *env, jclass mtlgc) @@ -82,129 +74,76 @@ Java_sun_java2d_metal_MTLGraphicsConfig_tryLoadMetalLibrary (JNIEnv *env, jclass mtlgc, jint displayID, jstring shadersLibName) { - jboolean ret = JNI_FALSE; - JNI_COCOA_ENTER(env); - NSMutableArray * retArray = [NSMutableArray arrayWithCapacity:3]; - [retArray addObject: [NSNumber numberWithInt: (int)displayID]]; - char *str = JNU_GetStringPlatformChars(env, shadersLibName, 0); - [retArray addObject: [NSString stringWithUTF8String: str]]; + __block jboolean ret = JNI_FALSE; + +JNI_COCOA_ENTER(env); + + __block NSString* path = NormalizedPathNSStringFromJavaString(env, shadersLibName); [ThreadUtilities performOnMainThreadWaiting:YES block:^() { - [MTLGraphicsConfigUtil _tryLoadMetalLibrary: retArray]; + + id device = CGDirectDisplayCopyCurrentMetalDevice(displayID); + if (device != nil) { + NSError* error = nil; + id lib = [device newLibraryWithFile:path error:&error]; + if (lib != nil) { + ret = JNI_TRUE; + } else { + J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_tryLoadMetalLibrary - Failed to load Metal shader library."); + } + } else { + J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_tryLoadMetalLibrary - Failed to create MTLDevice."); + } }]; - NSNumber * num = (NSNumber *)[retArray objectAtIndex: 0]; - ret = (jboolean)[num boolValue]; - JNU_ReleaseStringPlatformChars(env, shadersLibName, str); - JNI_COCOA_EXIT(env); +JNI_COCOA_EXIT(env); return ret; } - /** - * Determines whether the MTL pipeline can be used for a given GraphicsConfig - * provided its screen number and visual ID. If the minimum requirements are - * met, the native MTLGraphicsConfigInfo 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 MTL - * cannot be used for this GraphicsConfig (we should fallback on an existing - * 2D pipeline). + * Determines whether the Metal pipeline can be used for a given screen number and + * shader library path. A MTLContext is created and the native MTLGraphicsConfigInfo + * structure is initialized for this context. A pointer to this structure is + * returned as a jlong. + * If initialization fails at any point, zero is returned, indicating that Metal pipeline + * cannot be used for this GraphicsConfig (we should fallback on an existing 2D pipeline). */ JNIEXPORT jlong JNICALL Java_sun_java2d_metal_MTLGraphicsConfig_getMTLConfigInfo (JNIEnv *env, jclass mtlgc, jint displayID, jstring mtlShadersLib) { - jlong ret = 0L; - JNI_COCOA_ENTER(env); - NSMutableArray * retArray = [NSMutableArray arrayWithCapacity:3]; - [retArray addObject: [NSNumber numberWithInt: (int)displayID]]; - char *str = JNU_GetStringPlatformChars(env, mtlShadersLib, 0); - [retArray addObject: [NSString stringWithUTF8String: str]]; - - [ThreadUtilities performOnMainThreadWaiting:YES block:^() { - [MTLGraphicsConfigUtil _getMTLConfigInfo: retArray]; - }]; - - NSNumber * num = (NSNumber *)[retArray objectAtIndex: 0]; - ret = (jlong)[num longValue]; - JNU_ReleaseStringPlatformChars(env, mtlShadersLib, str); - JNI_COCOA_EXIT(env); - return ret; -} - - - - -@implementation MTLGraphicsConfigUtil -+ (void) _getMTLConfigInfo: (NSMutableArray *)argValue { - AWT_ASSERT_APPKIT_THREAD; + __block MTLContext* mtlc = nil; + __block MTLGraphicsConfigInfo* mtlinfo = nil; - jint displayID = (jint)[(NSNumber *)[argValue objectAtIndex: 0] intValue]; - NSString *mtlShadersLib = (NSString *)[argValue objectAtIndex: 1]; - JNIEnv *env = [ThreadUtilities getJNIEnvUncached]; - [argValue removeAllObjects]; +JNI_COCOA_ENTER(env); - J2dRlsTraceLn(J2D_TRACE_INFO, "MTLGraphicsConfig_getMTLConfigInfo"); - - NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; - - MTLContext *mtlc = [[MTLContext alloc] initWithDevice:CGDirectDisplayCopyCurrentMetalDevice(displayID) - shadersLib:mtlShadersLib]; - if (mtlc == 0L) { - J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGC_InitMTLContext: could not initialze MTLContext."); - [argValue addObject: [NSNumber numberWithLong: 0L]]; - return; - } - - // create the MTLGraphicsConfigInfo record for this config - MTLGraphicsConfigInfo *mtlinfo = (MTLGraphicsConfigInfo *)malloc(sizeof(MTLGraphicsConfigInfo)); - if (mtlinfo == NULL) { - J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_getMTLConfigInfo: could not allocate memory for mtlinfo"); - [mtlc release]; - mtlc = nil; - [argValue addObject: [NSNumber numberWithLong: 0L]]; - return; - } - memset(mtlinfo, 0, sizeof(MTLGraphicsConfigInfo)); - mtlinfo->context = mtlc; - - [argValue addObject: [NSNumber numberWithLong:ptr_to_jlong(mtlinfo)]]; - [pool drain]; -} - -+ (void) _tryLoadMetalLibrary: (NSMutableArray *)argValue { - AWT_ASSERT_APPKIT_THREAD; - - jint displayID = (jint)[(NSNumber *)[argValue objectAtIndex: 0] intValue]; - NSString *mtlShadersLib = (NSString *)[argValue objectAtIndex: 1]; - JNIEnv *env = [ThreadUtilities getJNIEnvUncached]; - [argValue removeAllObjects]; - - J2dRlsTraceLn(J2D_TRACE_INFO, "MTLGraphicsConfigUtil_tryLoadMTLLibrary"); + __block NSString* path = NormalizedPathNSStringFromJavaString(env, mtlShadersLib); + [ThreadUtilities performOnMainThreadWaiting:YES block:^() { - BOOL ret = FALSE;; - id device = CGDirectDisplayCopyCurrentMetalDevice(displayID); - if (device != nil) { - NSError *error = nil; - id lib = [device newLibraryWithFile:mtlShadersLib error:&error]; - if (lib != nil) { - ret = TRUE; + mtlc = [[MTLContext alloc] initWithDevice:CGDirectDisplayCopyCurrentMetalDevice(displayID) + shadersLib:path]; + if (mtlc != 0L) { + // create the MTLGraphicsConfigInfo record for this context + mtlinfo = (MTLGraphicsConfigInfo *)malloc(sizeof(MTLGraphicsConfigInfo)); + if (mtlinfo != NULL) { + memset(mtlinfo, 0, sizeof(MTLGraphicsConfigInfo)); + mtlinfo->context = mtlc; + } else { + J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_getMTLConfigInfo: could not allocate memory for mtlinfo"); + [mtlc release]; + mtlc = nil; + } } else { - J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_tryLoadMetalLibrary - Failed to load Metal shader library."); + J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGC_InitMTLContext: could not initialze MTLContext."); } - } else { - J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_tryLoadMetalLibrary - Failed to create MTLDevice."); - } + }]; - [argValue addObject: [NSNumber numberWithBool: ret]]; +JNI_COCOA_EXIT(env); + return ptr_to_jlong(mtlinfo); } -@end //GraphicsConfigUtil - - JNIEXPORT jint JNICALL Java_sun_java2d_metal_MTLGraphicsConfig_nativeGetMaxTextureSize (JNIEnv *env, jclass mtlgc) From 45e7aec06c8dd69ec4b6832af6ff7709e16fddbd Mon Sep 17 00:00:00 2001 From: aghaisas Date: Thu, 8 Apr 2021 16:17:59 +0530 Subject: [PATCH 3/5] log message correction --- .../macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m index 798bbd5e4dad3..2df3ea53fbca2 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m +++ b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLGraphicsConfig.m @@ -135,7 +135,7 @@ mtlc = nil; } } else { - J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGC_InitMTLContext: could not initialze MTLContext."); + J2dRlsTraceLn(J2D_TRACE_ERROR, "MTLGraphicsConfig_getMTLConfigInfo: could not initialze MTLContext."); } }]; From 746d791d7242bb2efe8efd2ef067b0838bf5df75 Mon Sep 17 00:00:00 2001 From: aghaisas Date: Tue, 27 Apr 2021 20:05:59 +0530 Subject: [PATCH 4/5] cleanup MTLContext.invalidateCurrentContext() --- .../classes/sun/java2d/metal/MTLContext.java | 26 ------------------- .../sun/java2d/metal/MTLGraphicsConfig.java | 3 --- .../java2d/metal/MTLRenderQueue.m | 19 -------------- 3 files changed, 48 deletions(-) diff --git a/src/java.desktop/macosx/classes/sun/java2d/metal/MTLContext.java b/src/java.desktop/macosx/classes/sun/java2d/metal/MTLContext.java index 86e90770857b3..85c8554defe49 100644 --- a/src/java.desktop/macosx/classes/sun/java2d/metal/MTLContext.java +++ b/src/java.desktop/macosx/classes/sun/java2d/metal/MTLContext.java @@ -79,32 +79,6 @@ public static void setScratchSurface(long pConfigInfo) { buf.putLong(pConfigInfo); } - /** - * Invalidates the currentContext field to ensure that we properly - * revalidate the MTLContext (make it current, etc.) next time through - * the validate() method. This is typically invoked from methods - * that affect the current context state (e.g. disposing a context or - * surface). - */ - public static void invalidateCurrentContext() { - // assert MTLRenderQueue.getInstance().lock.isHeldByCurrentThread(); - - // invalidate the current Java-level context so that we - // revalidate everything the next time around - if (currentContext != null) { - currentContext.invalidateContext(); - currentContext = null; - } - - // invalidate the context reference at the native level, and - // then flush the queue so that we have no pending operations - // dependent on the current context - MTLRenderQueue rq = MTLRenderQueue.getInstance(); - rq.ensureCapacity(4); - rq.getBuffer().putInt(INVALIDATE_CONTEXT); - rq.flushNow(); - } - public static class MTLContextCaps extends ContextCapabilities { /** Indicates that the context is doublebuffered. */ diff --git a/src/java.desktop/macosx/classes/sun/java2d/metal/MTLGraphicsConfig.java b/src/java.desktop/macosx/classes/sun/java2d/metal/MTLGraphicsConfig.java index d3eb8ef24a3d2..658d60b9f3933 100644 --- a/src/java.desktop/macosx/classes/sun/java2d/metal/MTLGraphicsConfig.java +++ b/src/java.desktop/macosx/classes/sun/java2d/metal/MTLGraphicsConfig.java @@ -145,9 +145,6 @@ public static MTLGraphicsConfig getConfig(CGraphicsDevice device, MTLRenderQueue rq = MTLRenderQueue.getInstance(); rq.lock(); try { - // getMTLConfigInfo() creates new MTLContext, so we should first - // invalidate the current Java-level context and flush the queue... - MTLContext.invalidateCurrentContext(); cfginfo = getMTLConfigInfo(displayID, mtlShadersLib); if (cfginfo != 0L) { textureSize = nativeGetMaxTextureSize(); diff --git a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.m b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.m index f708ce22cbfcf..4ea91bc059146 100644 --- a/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.m +++ b/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.m @@ -667,26 +667,7 @@ void MTLRenderQueue_CheckPreviousOp(jint op) { // dstOps = NULL; break; } - case sun_java2d_pipe_BufferedOpCodes_INVALIDATE_CONTEXT: - { - CHECK_PREVIOUS_OP(MTL_OP_OTHER); - // invalidate the references to the current context and - // destination surface that are maintained at the native level - if (mtlc != NULL) { - commitEncodedCommands(); - RESET_PREVIOUS_OP(); - [mtlc reset]; - } - MTLTR_FreeGlyphCaches(); - if (dstOps != NULL) { - MTLSD_Delete(env, dstOps); - } - - mtlc = NULL; - dstOps = NULL; - break; - } case sun_java2d_pipe_BufferedOpCodes_SYNC: { CHECK_PREVIOUS_OP(MTL_OP_SYNC); From 1cf7d1c2fb8bcd44d670ae99a4985879c3d1d337 Mon Sep 17 00:00:00 2001 From: aghaisas Date: Tue, 27 Apr 2021 20:25:55 +0530 Subject: [PATCH 5/5] cleanup unused import --- src/java.desktop/macosx/classes/sun/java2d/metal/MTLContext.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/java.desktop/macosx/classes/sun/java2d/metal/MTLContext.java b/src/java.desktop/macosx/classes/sun/java2d/metal/MTLContext.java index 85c8554defe49..516fe33fd4e0a 100644 --- a/src/java.desktop/macosx/classes/sun/java2d/metal/MTLContext.java +++ b/src/java.desktop/macosx/classes/sun/java2d/metal/MTLContext.java @@ -32,7 +32,6 @@ import java.lang.annotation.Native; -import static sun.java2d.pipe.BufferedOpCodes.INVALIDATE_CONTEXT; import static sun.java2d.pipe.BufferedOpCodes.SET_SCRATCH_SURFACE; /**