--- old/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLContext.h 2019-11-20 15:55:38.000000000 +0530 +++ new/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLContext.h 2019-11-20 15:55:37.000000000 +0530 @@ -100,7 +100,7 @@ @property (strong)MTLPipelineStatesStorage* pipelineStateStorage; @property (strong)MTLTexturePool* texturePool; -- (void)releaseCommandBuffer; +- (void)deallocCommandBuffer; /** * Fetches the MTLContext associated with the given destination surface, * makes the context current for those surfaces, updates the destination --- old/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLContext.m 2019-11-20 15:55:39.000000000 +0530 +++ new/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLContext.m 2019-11-20 15:55:38.000000000 +0530 @@ -120,8 +120,7 @@ return _commandBuffer; } -- (void)releaseCommandBuffer { - [_commandBuffer release]; +- (void)deallocCommandBuffer { _commandBuffer = nil; } --- old/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLLayer.m 2019-11-20 15:55:40.000000000 +0530 +++ new/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLLayer.m 2019-11-20 15:55:40.000000000 +0530 @@ -87,52 +87,43 @@ } if (self.nextDrawableCount == 0) { - @autoreleasepool { - - if ((self.buffer.width == 0) || (self.buffer.height == 0)) { - J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: cannot create drawable of size 0"); - - [self.ctx releaseCommandBuffer]; - [self.ctx.texturePool markAllTexturesFree]; - return; - } - - if (@available(macOS 10.13, *)) { - self.displaySyncEnabled = NO; - } - id mtlDrawable = [self nextDrawable]; - if (mtlDrawable == nil) { - J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: nextDrawable is null)"); - - [self.ctx releaseCommandBuffer]; - [self.ctx.texturePool markAllTexturesFree]; - return; - } - self.nextDrawableCount++; - J2dTraceLn6(J2D_TRACE_INFO, "MTLLayer.blitTexture: src tex=%p (w=%d, h=%d), dst tex=%p (w=%d, h=%d)", self.buffer, self.buffer.width, self.buffer.height, mtlDrawable.texture, mtlDrawable.texture.width, mtlDrawable.texture.height); - id blitEncoder = [commandBuf blitCommandEncoder]; - [blitEncoder + @autoreleasepool { + if ((self.buffer.width == 0) || (self.buffer.height == 0)) { + J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: cannot create drawable of size 0"); + + [self.ctx deallocCommandBuffer]; + [commandBuf release]; + return; + } + + id mtlDrawable = [self nextDrawable]; + if (mtlDrawable == nil) { + J2dTraceLn(J2D_TRACE_VERBOSE, "MTLLayer.blitTexture: nextDrawable is null)"); + + [self.ctx deallocCommandBuffer]; + [commandBuf release]; + return; + } + self.nextDrawableCount++; + J2dTraceLn6(J2D_TRACE_INFO, "MTLLayer.blitTexture: src tex=%p (w=%d, h=%d), dst tex=%p (w=%d, h=%d)", self.buffer, self.buffer.width, self.buffer.height, mtlDrawable.texture, mtlDrawable.texture.width, mtlDrawable.texture.height); + id blitEncoder = [commandBuf blitCommandEncoder]; + [blitEncoder copyFromTexture:self.buffer sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake((jint)(self.leftInset*self.contentsScale), (jint)(self.topInset*self.contentsScale), 0) sourceSize:MTLSizeMake(self.buffer.width, self.buffer.height, 1) toTexture:mtlDrawable.texture destinationSlice:0 destinationLevel:0 destinationOrigin:MTLOriginMake(0, 0, 0)]; - [blitEncoder endEncoding]; + [blitEncoder endEncoding]; - [commandBuf presentDrawable:mtlDrawable]; + [commandBuf presentDrawable:mtlDrawable]; - [commandBuf addCompletedHandler:^(id cmdBuff) { - [self.ctx.texturePool markAllTexturesFree]; - [self.ctx releaseCommandBuffer]; + [commandBuf addCompletedHandler:^(id cmdBuff) { + [commandBuf release]; self.nextDrawableCount--; - if (@available(macOS 10.13, *)) { - self.displaySyncEnabled = YES; - } - }]; - - [commandBuf commit]; - [commandBuf waitUntilCompleted]; - [self.ctx releaseCommandBuffer]; - } + }]; + + [commandBuf commit]; + [self.ctx deallocCommandBuffer]; + } } } @@ -251,6 +242,4 @@ } [layer blitTexture]; - - [ctx releaseCommandBuffer]; } --- old/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.m 2019-11-20 15:55:41.000000000 +0530 +++ new/src/java.desktop/macosx/native/libawt_lwawt/java2d/metal/MTLRenderQueue.m 2019-11-20 15:55:41.000000000 +0530 @@ -960,6 +960,16 @@ if (mtlc != NULL) { RESET_PREVIOUS_OP(); [mtlc endCommonRenderEncoder]; + id commandBuffer = mtlc.commandBuffer; + [commandBuffer addCompletedHandler:^(id commandBuffer) { + [mtlc.texturePool markAllTexturesFree]; + [commandBuffer release]; + }]; + [commandBuffer commit]; + [mtlc deallocCommandBuffer]; + if (sync) { + [commandBuffer waitUntilCompleted]; + } BMTLSDOps *dstOps = MTLRenderQueue_GetCurrentDestination(); if (dstOps != NULL) { MTLSDOps *dstMTLOps = (MTLSDOps *)dstOps->privOps;