Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8263363: Minor cleanup of Lanai code - unused code removal and comments correction #3357

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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);

@@ -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;

/**
@@ -79,32 +78,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. */
@@ -145,10 +145,6 @@ 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...
MTLContext.invalidateCurrentContext();
cfginfo = getMTLConfigInfo(displayID, mtlShadersLib);
if (cfginfo != 0L) {
textureSize = nativeGetMaxTextureSize();
@@ -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,143 +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<MTLDevice> device = CGDirectDisplayCopyCurrentMetalDevice(displayID);
if (device != nil) {
NSError* error = nil;
id<MTLLibrary> 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;

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

J2dRlsTraceLn(J2D_TRACE_INFO, "MTLGraphicsConfig_getMTLConfigInfo");
__block MTLContext* mtlc = nil;
__block MTLGraphicsConfigInfo* mtlinfo = nil;

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
JNI_COCOA_ENTER(env);

__block NSString* path = NormalizedPathNSStringFromJavaString(env, mtlShadersLib);

Copy link
Member

@mrserb mrserb Apr 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also check how this function is called, looks like previously it was called as a selector+an array as a parameter, and then reworked as a performOnMainThreadWaiting+block, but it still use an array as a parameter. I think it is similar to JDK-8238075.

Copy link
Contributor Author

@aghaisas aghaisas Apr 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent point! Thanks for the pointer to the bug.
A lot of code in this file can be cleaned up. I will update the PR soon.

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) {
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");
free(mtlc);
[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");

[ThreadUtilities performOnMainThreadWaiting:YES block:^() {

BOOL ret = FALSE;;
id<MTLDevice> device = CGDirectDisplayCopyCurrentMetalDevice(displayID);
if (device != nil) {
NSError *error = nil;
id<MTLLibrary> 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, "MTLGraphicsConfig_getMTLConfigInfo: 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)
@@ -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);