Skip to content

Commit 7b30135

Browse files
jayathirthraoarapte
authored andcommittedSep 3, 2024
8339457: Memory leak of MTLTextureDescriptor and MTLComputePipelineState
1 parent 18e237f commit 7b30135

File tree

2 files changed

+70
-60
lines changed

2 files changed

+70
-60
lines changed
 

‎modules/javafx.graphics/src/main/native-glass/mac/GlassFrameBufferObject.m

+11-9
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,20 @@ - (void)_createFboIfNeededForWidth:(unsigned int)width andHeight:(unsigned int)h
5656
}
5757

5858
if (self->_texture == nil) {
59-
// Create a texture ----------
60-
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
59+
@autoreleasepool {
60+
// Create a texture ----------
61+
id<MTLDevice> device = MTLCreateSystemDefaultDevice();
6162

62-
MTLTextureDescriptor *texDescriptor =
63-
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat: MTLPixelFormatBGRA8Unorm
64-
width:width
65-
height:height
66-
mipmapped:false];
63+
MTLTextureDescriptor *texDescriptor =
64+
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat: MTLPixelFormatBGRA8Unorm
65+
width:width
66+
height:height
67+
mipmapped:false];
6768

68-
texDescriptor.usage = MTLTextureUsageRenderTarget;
69+
texDescriptor.usage = MTLTextureUsageRenderTarget;
6970

70-
self->_texture = [device newTextureWithDescriptor:texDescriptor];
71+
self->_texture = [device newTextureWithDescriptor:texDescriptor];
72+
}
7173
}
7274

7375
self->_width = width;

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

+59-51
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,17 @@ - (MetalTexture*) createTexture : (MetalContext*) ctx
7070
mipmapped = false;
7171
}
7272
TEX_LOG(@"useMipMap : %d", useMipMap);
73-
MTLTextureDescriptor *texDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pixelFormat
73+
@autoreleasepool {
74+
MTLTextureDescriptor *texDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:pixelFormat
7475
width:width
7576
height:height
7677
mipmapped:mipmapped];
77-
texDescriptor.usage = usage;
78+
texDescriptor.usage = usage;
7879

79-
// Create buffer to store pixel data and then a texture using that buffer
80-
id<MTLDevice> device = [context getDevice];
81-
texture = [device newTextureWithDescriptor:texDescriptor];
80+
// Create buffer to store pixel data and then a texture using that buffer
81+
id<MTLDevice> device = [context getDevice];
82+
texture = [device newTextureWithDescriptor:texDescriptor];
83+
}
8284

8385
/*
8486
// for testing purpose
@@ -129,37 +131,39 @@ - (MetalTexture*) createTexture : (MetalContext*) ctx
129131
pixelFormat = MTLPixelFormatBGRA8Unorm;
130132
storageMode = MTLResourceStorageModeShared;
131133

132-
MTLTextureDescriptor *texDescriptor = [[MTLTextureDescriptor new] autorelease];
133-
texDescriptor.usage = usage;
134-
texDescriptor.width = width;
135-
texDescriptor.height = height;
136-
texDescriptor.textureType = type;
137-
texDescriptor.pixelFormat = pixelFormat;
138-
texDescriptor.sampleCount = 1;
139-
texDescriptor.hazardTrackingMode = MTLHazardTrackingModeTracked;
140-
141-
id<MTLDevice> device = [context getDevice];
142-
143-
texture = [device newTextureWithDescriptor: texDescriptor];
144-
if (msaa) {
145-
TEX_LOG(@">>>> MetalTexture.createTexture()2 msaa texture");
146-
MTLTextureDescriptor *msaaTexDescriptor = [[MTLTextureDescriptor new] autorelease];
147-
msaaTexDescriptor.storageMode = MTLStorageModePrivate;
148-
msaaTexDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
149-
msaaTexDescriptor.width = width;
150-
msaaTexDescriptor.height = height;
151-
msaaTexDescriptor.textureType = MTLTextureType2DMultisample;
152-
msaaTexDescriptor.pixelFormat = pixelFormat;
153-
//By default all SoC's on macOS support 4 sample count
154-
msaaTexDescriptor.sampleCount = 4;
155-
msaaTexture = [device newTextureWithDescriptor: msaaTexDescriptor];
156-
} else {
157-
msaaTexture = nil;
134+
@autoreleasepool {
135+
MTLTextureDescriptor *texDescriptor = [MTLTextureDescriptor new];
136+
texDescriptor.usage = usage;
137+
texDescriptor.width = width;
138+
texDescriptor.height = height;
139+
texDescriptor.textureType = type;
140+
texDescriptor.pixelFormat = pixelFormat;
141+
texDescriptor.sampleCount = 1;
142+
texDescriptor.hazardTrackingMode = MTLHazardTrackingModeTracked;
143+
144+
id<MTLDevice> device = [context getDevice];
145+
146+
texture = [device newTextureWithDescriptor: texDescriptor];
147+
if (msaa) {
148+
TEX_LOG(@">>>> MetalTexture.createTexture()2 msaa texture");
149+
MTLTextureDescriptor *msaaTexDescriptor = [MTLTextureDescriptor new];
150+
msaaTexDescriptor.storageMode = MTLStorageModePrivate;
151+
msaaTexDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
152+
msaaTexDescriptor.width = width;
153+
msaaTexDescriptor.height = height;
154+
msaaTexDescriptor.textureType = MTLTextureType2DMultisample;
155+
msaaTexDescriptor.pixelFormat = pixelFormat;
156+
//By default all SoC's on macOS support 4 sample count
157+
msaaTexDescriptor.sampleCount = 4;
158+
msaaTexture = [device newTextureWithDescriptor: msaaTexDescriptor];
159+
} else {
160+
msaaTexture = nil;
161+
}
162+
isMSAA = msaa;
163+
164+
depthTexture = nil;
165+
depthMSAATexture = nil;
158166
}
159-
isMSAA = msaa;
160-
161-
depthTexture = nil;
162-
depthMSAATexture = nil;
163167
}
164168
TEX_LOG(@">>>> MetalTexture.createTexture()2 (buffer backed texture) -- width = %lu, height = %lu", width, height);
165169
TEX_LOG(@">>>> MetalTexture.createTexture()2 created MetalTexture = %p", texture);
@@ -195,22 +199,24 @@ - (void) createDepthTexture
195199
depthTexture.height != height ||
196200
lastDepthMSAA != isMSAA) {
197201
lastDepthMSAA = isMSAA;
198-
MTLTextureDescriptor *depthDesc = [[MTLTextureDescriptor new] autorelease];
199-
depthDesc.width = width;
200-
depthDesc.height = height;
201-
depthDesc.pixelFormat = MTLPixelFormatDepth32Float;
202-
depthDesc.textureType = MTLTextureType2D;
203-
depthDesc.sampleCount = 1;
204-
depthDesc.usage = MTLTextureUsageRenderTarget;
205-
depthDesc.storageMode = MTLStorageModePrivate;
206-
depthTexture = [device newTextureWithDescriptor: depthDesc];
207-
if (isMSAA) {
208-
TEX_LOG(@">>>> MetalTexture.createDepthMSAATexture()");
209-
depthDesc.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
210-
depthDesc.textureType = MTLTextureType2DMultisample;
211-
//By default all SoC's on macOS support 4 sample count
212-
depthDesc.sampleCount = 4;
213-
depthMSAATexture = [device newTextureWithDescriptor: depthDesc];
202+
@autoreleasepool {
203+
MTLTextureDescriptor *depthDesc = [MTLTextureDescriptor new];
204+
depthDesc.width = width;
205+
depthDesc.height = height;
206+
depthDesc.pixelFormat = MTLPixelFormatDepth32Float;
207+
depthDesc.textureType = MTLTextureType2D;
208+
depthDesc.sampleCount = 1;
209+
depthDesc.usage = MTLTextureUsageRenderTarget;
210+
depthDesc.storageMode = MTLStorageModePrivate;
211+
depthTexture = [device newTextureWithDescriptor: depthDesc];
212+
if (isMSAA) {
213+
TEX_LOG(@">>>> MetalTexture.createDepthMSAATexture()");
214+
depthDesc.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
215+
depthDesc.textureType = MTLTextureType2DMultisample;
216+
//By default all SoC's on macOS support 4 sample count
217+
depthDesc.sampleCount = 4;
218+
depthMSAATexture = [device newTextureWithDescriptor: depthDesc];
219+
}
214220
}
215221
}
216222
}
@@ -564,6 +570,8 @@ static int copyPixelDataToRingBuffer(MetalContext* context, void* pixels, unsign
564570

565571
[computeEncoder endEncoding];
566572

573+
[_computePipelineState release];
574+
567575
[context commitCurrentCommandBuffer];
568576
}
569577

0 commit comments

Comments
 (0)
Please sign in to comment.