Skip to content

Commit 6905805

Browse files
jayathirthraoarapte
authored andcommittedOct 15, 2024
8342146: Use dealloc method in MetalShader and fix leak
1 parent 6aedc7e commit 6905805

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed
 

‎modules/javafx.graphics/src/main/java/com/sun/prism/mtl/MTLContext.java

+5
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ private static native void nBlit(long pContext, long nSrcRTT, long nDstRTT,
421421

422422
private static native void nRelease(long pContext);
423423

424+
private static native void nDisposeShader(long nMetalShaderRef);
425+
424426
@Override
425427
public void setDeviceParametersFor2D() {
426428
if (checkDisposed()) return;
@@ -622,6 +624,9 @@ private void updateRawMatrix(GeneralTransform3D src) {
622624
rawMatrix[15] = (float)src.get(15);
623625
}
624626

627+
public void disposeShader(long nMetalShaderRef) {
628+
nDisposeShader(nMetalShaderRef);
629+
}
625630

626631
@Override
627632
public void dispose() {

‎modules/javafx.graphics/src/main/java/com/sun/prism/mtl/MTLShader.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ native private static long nSetTexture(long nMetalShader, int texID, int uniform
6262

6363
native private static long nSetConstants(long nMetalShader, int uniformID, float[] values, int size);
6464

65-
native private static void nDispose(long nMetalShader);
66-
6765
private MTLShader(MTLContext context, String fragmentFunctionName) {
6866
MTLLog.Debug(">>> MTLShader(): fragFuncName = " + fragmentFunctionName);
6967

@@ -138,6 +136,10 @@ public void enable() {
138136
@Override
139137
public void disable() {
140138
MTLLog.Debug("MTLShader.disable() fragFuncName = " + fragmentFunctionName);
139+
// TODO: MTL: There are no disable calls coming from BaseShaderContext.
140+
// So this is a no-op. We can call disable on lastShader in
141+
// BaseShaderContext.checkState() but that will be a common change for
142+
// all pipelines.
141143
nDisable(nMetalShaderRef);
142144
}
143145

@@ -237,6 +239,6 @@ public void setConstants(String name, FloatBuffer buf, int off, int count) {
237239
@Override
238240
public void dispose() {
239241
MTLLog.Debug(">>> MTLShader.dispose() : fragmentFunctionName : " + this.fragmentFunctionName);
240-
nDispose(nMetalShaderRef);
242+
context.disposeShader(nMetalShaderRef);
241243
}
242244
}

‎modules/javafx.graphics/src/main/native-prism-mtl/MetalContext.m

+17
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ - (void) commitCurrentCommandBuffer:(bool)waitUntilCompleted
290290
for (MetalShader* shader in shadersUsedInCB) {
291291
[shader setArgsUpdated:true];
292292
}
293+
[shadersUsedInCB removeAllObjects];
293294

294295
unsigned int rbid = [MetalRingBuffer getCurrentBufferIndex];
295296
[currentCommandBuffer addCompletedHandler:^(id<MTLCommandBuffer> cb) {
@@ -899,6 +900,11 @@ - (void)dealloc
899900
transientBuffersForCB = nil;
900901
}
901902

903+
if (shadersUsedInCB != nil) {
904+
[shadersUsedInCB removeAllObjects];
905+
[shadersUsedInCB release];
906+
shadersUsedInCB = nil;
907+
}
902908
if (identityMatrixBuf != nil) {
903909
[identityMatrixBuf release];
904910
identityMatrixBuf = nil;
@@ -969,6 +975,17 @@ - (void)dealloc
969975
return jContextPtr;
970976
}
971977

978+
JNIEXPORT void JNICALL Java_com_sun_prism_mtl_MTLContext_nDisposeShader
979+
(JNIEnv *env, jclass jClass, jlong shaderRef)
980+
{
981+
CTX_LOG(@">>>> MTLContext_nDisposeShader");
982+
983+
MetalShader *shaderPtr = (MetalShader *)jlong_to_ptr(shaderRef);
984+
if (shaderPtr != NULL) {
985+
[shaderPtr release];
986+
}
987+
CTX_LOG(@"<<<< MTLContext_nDisposeShader");
988+
}
972989

973990
JNIEXPORT void JNICALL Java_com_sun_prism_mtl_MTLContext_nRelease
974991
(JNIEnv *env, jclass jClass, jlong context)

‎modules/javafx.graphics/src/main/native-prism-mtl/MetalShader.h

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272

7373
- (NSUInteger) getArgumentID:(NSString*) name;
7474
- (void) enable;
75+
- (void) disable;
7576

7677
- (void) setTexture:(int)texID
7778
uniformID:(int)uniformID
@@ -87,6 +88,7 @@
8788
- (void) setFloat4:(int)uniformID f0:(float) f0 f1:(float) f1 f2:(float) f2 f3:(float) f3;
8889

8990
- (void) setConstants:(int)uniformID values:(float[]) values size:(int) size;
91+
- (void) dealloc;
9092

9193
@end
9294

‎modules/javafx.graphics/src/main/native-prism-mtl/MetalShader.m

+10-12
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ - (void) enable
159159
SHADER_LOG(@"<<<< MetalShader.enable()\n");
160160
}
161161

162+
- (void) disable
163+
{
164+
SHADER_LOG(@"\n");
165+
SHADER_LOG(@">>>> MetalShader.disable()----> fragFuncName: %@", fragFuncName);
166+
[context setCurrentShader:NULL];
167+
SHADER_LOG(@"<<<< MetalShader.disable()\n");
168+
}
169+
162170
- (id<MTLRenderPipelineState>) getPipelineState:(bool)isMSAA
163171
compositeMode:(int)compositeMode;
164172
{
@@ -413,7 +421,7 @@ - (void) setConstants:(int)uniformID values:(float[]) values size:(int) size
413421
SHADER_LOG(@"<<<< MetalShader.setConstants()");
414422
}
415423

416-
- (void) dispose
424+
- (void) dealloc
417425
{
418426
SHADER_LOG(@"\n");
419427
SHADER_LOG(@">>>> MetalShader.dispose()----> fragFuncName: %@", fragFuncName);
@@ -490,7 +498,7 @@ - (void) dispose
490498
SHADER_LOG(@"\n");
491499
SHADER_LOG(@"-> JNICALL Native: MTLShader_nDisable");
492500
MetalShader *mtlShader = (MetalShader *)jlong_to_ptr(shader);
493-
//[mtlShader disable];
501+
[mtlShader disable];
494502
return 1;
495503
}
496504

@@ -572,13 +580,3 @@ - (void) dispose
572580
(*env)->ReleaseFloatArrayElements(env, valuesArray, values, 0);
573581
return 1;
574582
}
575-
576-
JNIEXPORT void JNICALL Java_com_sun_prism_mtl_MTLShader_nDispose
577-
(JNIEnv *env, jclass jClass, jlong shader)
578-
{
579-
SHADER_LOG(@"\n");
580-
SHADER_LOG(@"-> JNICALL Native: MTLShader_nDispose");
581-
MetalShader *mtlShader = (MetalShader *)jlong_to_ptr(shader);
582-
[mtlShader dispose];
583-
return;
584-
}

0 commit comments

Comments
 (0)
Please sign in to comment.