Skip to content

Commit

Permalink
8290344: Start/stop displaysync affects performance in metal renderin…
Browse files Browse the repository at this point in the history
…g pipeline

Reviewed-by: aghaisas, jdv
  • Loading branch information
Alexey Ushakov committed Aug 30, 2022
1 parent afa5d4c commit f766d92
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
@property (readwrite, assign) int topInset;
@property (readwrite, assign) int leftInset;
@property (readwrite, assign) CVDisplayLinkRef displayLink;
@property (readwrite, atomic) int displayLinkCount;

- (id) initWithJavaLayer:(jobject)layer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import "LWCToolkit.h"
#import "MTLSurfaceData.h"
#import "JNIUtilities.h"
#define KEEP_ALIVE_INC 4

@implementation MTLLayer

Expand All @@ -42,6 +43,7 @@ @implementation MTLLayer
@synthesize leftInset;
@synthesize nextDrawableCount;
@synthesize displayLink;
@synthesize displayLinkCount;

- (id) initWithJavaLayer:(jobject)layer
{
Expand Down Expand Up @@ -74,12 +76,15 @@ - (id) initWithJavaLayer:(jobject)layer
self.opaque = YES;
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
CVDisplayLinkSetOutputCallback(displayLink, &displayLinkCallback, (__bridge void*)self);
self.displayLinkCount = 0;
return self;
}

- (void) blitTexture {
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];
return;
}
Expand All @@ -100,9 +105,9 @@ - (void) blitTexture {
NSUInteger src_h = self.buffer.height - src_y;

if (src_h <= 0 || src_w <= 0) {
J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: Invalid src width or height.");
[self stopDisplayLink];
return;
J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: Invalid src width or height.");
[self stopDisplayLink];
return;
}

id<MTLCommandBuffer> commandBuf = [self.ctx createBlitCommandBuffer];
Expand Down Expand Up @@ -134,7 +139,11 @@ - (void) blitTexture {
}];

[commandBuf commit];
[self stopDisplayLink];

if (--self.displayLinkCount <= 0) {
self.displayLinkCount = 0;
[self stopDisplayLink];
}
}
}

Expand Down Expand Up @@ -177,13 +186,18 @@ - (void) redraw {
}

- (void) startDisplayLink {
if (!CVDisplayLinkIsRunning(self.displayLink))
if (!CVDisplayLinkIsRunning(self.displayLink)) {
CVDisplayLinkStart(self.displayLink);
J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer_startDisplayLink");
}
displayLinkCount += KEEP_ALIVE_INC; // Keep alive displaylink counter
}

- (void) stopDisplayLink {
if (CVDisplayLinkIsRunning(self.displayLink))
if (CVDisplayLinkIsRunning(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)
Expand Down

1 comment on commit f766d92

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.