Skip to content

Commit

Permalink
JBR-7733 Metal: Properly reset the current context when destroying th…
Browse files Browse the repository at this point in the history
…e surface
  • Loading branch information
YaaZ committed Oct 12, 2024
1 parent ceda379 commit 6454bc7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/java.desktop/macosx/classes/sun/java2d/metal/MTLContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.lang.annotation.Native;

import static sun.java2d.pipe.BufferedOpCodes.DISPOSE_SURFACE;
import static sun.java2d.pipe.BufferedOpCodes.SET_SCRATCH_SURFACE;

/**
Expand Down Expand Up @@ -78,6 +79,20 @@ public static void setScratchSurface(long pConfigInfo) {
buf.putLong(pConfigInfo);
}

public static void disposeSurface(long pData) {
// assert MTLRenderQueue.getInstance().lock.isHeldByCurrentThread();

// invalidate the current context
currentContext = null;

// set the scratch context
MTLRenderQueue rq = MTLRenderQueue.getInstance();
RenderBuffer buf = rq.getBuffer();
rq.ensureCapacityAndAlignment(12, 4);
buf.putInt(DISPOSE_SURFACE);
buf.putLong(pData);
}

public static class MTLContextCaps extends ContextCapabilities {

/** Indicates that the context is doublebuffered. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,7 @@ public static void dispose(long pData) {
MTLRenderQueue rq = MTLRenderQueue.getInstance();
rq.lock();
try {
RenderBuffer buf = rq.getBuffer();
rq.ensureCapacityAndAlignment(12, 4);
buf.putInt(DISPOSE_SURFACE);
buf.putLong(pData);

MTLContext.disposeSurface(pData);
// this call is expected to complete synchronously, so flush now
rq.flushNow();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,9 @@ void MTLRenderQueue_CheckPreviousOp(jint op) {
[mtlc.glyphCacheAA free];
[mtlc.glyphCacheLCD free];
[mtlc commitCommandBuffer:YES display:NO];
mtlc = NULL;
}
dstOps = NULL;
MTLSD_Delete(env, mtlsdo);
}
break;
Expand All @@ -710,7 +712,9 @@ void MTLRenderQueue_CheckPreviousOp(jint op) {
[mtlc.glyphCacheAA free];
[mtlc.glyphCacheLCD free];
[mtlc commitCommandBuffer:YES display:NO];
mtlc = NULL;
}
dstOps = NULL;
MTLSD_Delete(env, mtlsdo);
if (mtlsdo->privOps != NULL) {
free(mtlsdo->privOps);
Expand Down

0 comments on commit 6454bc7

Please sign in to comment.