8290344: Start/stop displaysync affects performance in metal rendering pipeline

Reviewed-by: aghaisas, jdv
This commit is contained in:
Alexey Ushakov 2022-08-30 08:23:58 +00:00
parent afa5d4ced3
commit f766d92755
2 changed files with 22 additions and 7 deletions
src/java.desktop/macosx/native/libawt_lwawt/java2d/metal

@ -55,6 +55,7 @@
@property (readwrite, assign) int topInset; @property (readwrite, assign) int topInset;
@property (readwrite, assign) int leftInset; @property (readwrite, assign) int leftInset;
@property (readwrite, assign) CVDisplayLinkRef displayLink; @property (readwrite, assign) CVDisplayLinkRef displayLink;
@property (readwrite, atomic) int displayLinkCount;
- (id) initWithJavaLayer:(jobject)layer; - (id) initWithJavaLayer:(jobject)layer;

@ -29,6 +29,7 @@
#import "LWCToolkit.h" #import "LWCToolkit.h"
#import "MTLSurfaceData.h" #import "MTLSurfaceData.h"
#import "JNIUtilities.h" #import "JNIUtilities.h"
#define KEEP_ALIVE_INC 4
@implementation MTLLayer @implementation MTLLayer
@ -42,6 +43,7 @@
@synthesize leftInset; @synthesize leftInset;
@synthesize nextDrawableCount; @synthesize nextDrawableCount;
@synthesize displayLink; @synthesize displayLink;
@synthesize displayLinkCount;
- (id) initWithJavaLayer:(jobject)layer - (id) initWithJavaLayer:(jobject)layer
{ {
@ -74,12 +76,15 @@
self.opaque = YES; self.opaque = YES;
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink); CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
CVDisplayLinkSetOutputCallback(displayLink, &displayLinkCallback, (__bridge void*)self); CVDisplayLinkSetOutputCallback(displayLink, &displayLinkCallback, (__bridge void*)self);
self.displayLinkCount = 0;
return self; return self;
} }
- (void) blitTexture { - (void) blitTexture {
if (self.ctx == NULL || self.javaLayer == NULL || self.buffer == nil || self.ctx.device == nil) { if (self.ctx == NULL || self.javaLayer == NULL || self.buffer == nil || self.ctx.device == nil) {
J2dTraceLn4(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: uninitialized (mtlc=%p, javaLayer=%p, buffer=%p, devide=%p)", self.ctx, self.javaLayer, self.buffer, ctx.device); J2dTraceLn4(J2D_TRACE_VERBOSE,
"MTLLayer.blitTexture: uninitialized (mtlc=%p, javaLayer=%p, buffer=%p, devide=%p)", self.ctx,
self.javaLayer, self.buffer, ctx.device);
[self stopDisplayLink]; [self stopDisplayLink];
return; return;
} }
@ -100,9 +105,9 @@
NSUInteger src_h = self.buffer.height - src_y; NSUInteger src_h = self.buffer.height - src_y;
if (src_h <= 0 || src_w <= 0) { if (src_h <= 0 || src_w <= 0) {
J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: Invalid src width or height."); J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: Invalid src width or height.");
[self stopDisplayLink]; [self stopDisplayLink];
return; return;
} }
id<MTLCommandBuffer> commandBuf = [self.ctx createBlitCommandBuffer]; id<MTLCommandBuffer> commandBuf = [self.ctx createBlitCommandBuffer];
@ -134,7 +139,11 @@
}]; }];
[commandBuf commit]; [commandBuf commit];
[self stopDisplayLink];
if (--self.displayLinkCount <= 0) {
self.displayLinkCount = 0;
[self stopDisplayLink];
}
} }
} }
@ -177,13 +186,18 @@
} }
- (void) startDisplayLink { - (void) startDisplayLink {
if (!CVDisplayLinkIsRunning(self.displayLink)) if (!CVDisplayLinkIsRunning(self.displayLink)) {
CVDisplayLinkStart(self.displayLink); CVDisplayLinkStart(self.displayLink);
J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer_startDisplayLink");
}
displayLinkCount += KEEP_ALIVE_INC; // Keep alive displaylink counter
} }
- (void) stopDisplayLink { - (void) stopDisplayLink {
if (CVDisplayLinkIsRunning(self.displayLink)) if (CVDisplayLinkIsRunning(self.displayLink)) {
CVDisplayLinkStop(self.displayLink); CVDisplayLinkStop(self.displayLink);
J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer_stopDisplayLink");
}
} }
CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext) CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext)