[Commits] SDL: Fixed Metal renderer memory leak

libsdl.org revision control commits-owner at libsdl.org
Fri Dec 8 13:20:24 PST 2017


details:   https://hg.libsdl.org/SDL/rev/3af68ade4d30
changeset: 11743:3af68ade4d30
user:      Sam Lantinga <slouken at libsdl.org>
date:      Fri Dec 08 13:20:20 2017 -0800
description:
Fixed Metal renderer memory leak

diffstat:

 src/render/metal/SDL_render_metal.m |  58 ++++++++++++++++++------------------
 1 files changed, 29 insertions(+), 29 deletions(-)

diffs (220 lines):

diff -r 971cd689bd96 -r 3af68ade4d30 src/render/metal/SDL_render_metal.m
--- a/src/render/metal/SDL_render_metal.m	Fri Dec 08 14:03:36 2017 -0500
+++ b/src/render/metal/SDL_render_metal.m	Fri Dec 08 13:20:20 2017 -0800
@@ -370,17 +370,17 @@
 
 static int
 METAL_GetOutputSize(SDL_Renderer * renderer, int *w, int *h)
-{
+{ @autoreleasepool {
     METAL_ActivateRenderer(renderer);
     METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
     *w = (int) data.mtlbackbuffer.texture.width;
     *h = (int) data.mtlbackbuffer.texture.height;
     return 0;
-}
+}}
 
 static int
 METAL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
-{
+{ @autoreleasepool {
     METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
     MTLPixelFormat mtlpixfmt;
 
@@ -405,12 +405,12 @@
     texture->driverdata = (void*)CFBridgingRetain(mtltexture);
 
     return 0;
-}
+}}
 
 static int
 METAL_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
                  const SDL_Rect * rect, const void *pixels, int pitch)
-{
+{ @autoreleasepool {
     // !!! FIXME: this is a synchronous call; it doesn't return until data is uploaded in some form.
     // !!! FIXME:  Maybe move this off to a thread that marks the texture as uploaded and only stall the main thread if we try to
     // !!! FIXME:  use this texture before the marking is done? Is it worth it? Or will we basically always be uploading a bunch of
@@ -418,7 +418,7 @@
     id<MTLTexture> mtltexture = (__bridge id<MTLTexture>) texture->driverdata;
     [mtltexture replaceRegion:MTLRegionMake2D(rect->x, rect->y, rect->w, rect->h) mipmapLevel:0 withBytes:pixels bytesPerRow:pitch];
     return 0;
-}
+}}
 
 static int
 METAL_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
@@ -445,17 +445,17 @@
 
 static int
 METAL_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
-{
+{ @autoreleasepool {
     METAL_ActivateRenderer(renderer);
     METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
     id<MTLTexture> mtltexture = texture ? (__bridge id<MTLTexture>) texture->driverdata : nil;
     data.mtlpassdesc.colorAttachments[0].texture = mtltexture;
     return 0;
-}
+}}
 
 static int
 METAL_UpdateViewport(SDL_Renderer * renderer)
-{
+{ @autoreleasepool {
     METAL_ActivateRenderer(renderer);
     METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
     MTLViewport viewport;
@@ -467,15 +467,15 @@
     viewport.zfar = 1.0;
     [data.mtlcmdencoder setViewport:viewport];
     return 0;
-}
+}}
 
 static int
 METAL_UpdateClipRect(SDL_Renderer * renderer)
-{
-    // !!! FIXME: should this care about the viewport?
+{ @autoreleasepool {
     METAL_ActivateRenderer(renderer);
     METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
     MTLScissorRect mtlrect;
+    // !!! FIXME: should this care about the viewport?
     if (renderer->clipping_enabled) {
         const SDL_Rect *rect = &renderer->clip_rect;
         mtlrect.x = renderer->viewport.x + rect->x;
@@ -492,11 +492,11 @@
         [data.mtlcmdencoder setScissorRect:mtlrect];
     }
     return 0;
-}
+}}
 
 static int
 METAL_RenderClear(SDL_Renderer * renderer)
-{
+{ @autoreleasepool {
     // We could dump the command buffer and force a clear on a new one, but this will respect the scissor state.
     METAL_ActivateRenderer(renderer);
     METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
@@ -528,7 +528,7 @@
     [data.mtlcmdencoder setViewport:viewport];
 
     return 0;
-}
+}}
 
 // normalize a value from 0.0f to len into -1.0f to 1.0f.
 static inline float
@@ -556,7 +556,7 @@
 static int
 DrawVerts(SDL_Renderer * renderer, const SDL_FPoint * points, int count,
           const MTLPrimitiveType primtype)
-{
+{ @autoreleasepool {
     METAL_ActivateRenderer(renderer);
 
     const size_t vertlen = (sizeof (float) * 2) * count;
@@ -588,7 +588,7 @@
 
     SDL_free(verts);
     return 0;
-}
+}}
 
 static int
 METAL_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points, int count)
@@ -604,7 +604,7 @@
 
 static int
 METAL_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects, int count)
-{
+{ @autoreleasepool {
     METAL_ActivateRenderer(renderer);
     METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
 
@@ -633,12 +633,12 @@
     }
 
     return 0;
-}
+}}
 
 static int
 METAL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
               const SDL_Rect * srcrect, const SDL_FRect * dstrect)
-{
+{ @autoreleasepool {
     METAL_ActivateRenderer(renderer);
     METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
     id<MTLTexture> mtltexture = (__bridge id<MTLTexture>) texture->driverdata;
@@ -679,7 +679,7 @@
     [data.mtlcmdencoder drawPrimitives:MTLPrimitiveTypeTriangleStrip vertexStart:0 vertexCount:5];
 
     return 0;
-}
+}}
 
 static int
 METAL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
@@ -692,7 +692,7 @@
 static int
 METAL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
                     Uint32 pixel_format, void * pixels, int pitch)
-{
+{ @autoreleasepool {
     METAL_ActivateRenderer(renderer);
     METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
     MTLRenderPassColorAttachmentDescriptor *colorAttachment = data.mtlpassdesc.colorAttachments[0];
@@ -719,11 +719,11 @@
     const int status = SDL_ConvertPixels(rect->w, rect->h, temp_format, temp_pixels, temp_pitch, pixel_format, pixels, pitch);
     SDL_free(temp_pixels);
     return status;
-}
+}}
 
 static void
 METAL_RenderPresent(SDL_Renderer * renderer)
-{
+{ @autoreleasepool {
     METAL_ActivateRenderer(renderer);
     METAL_RenderData *data = (__bridge METAL_RenderData *) renderer->driverdata;
 
@@ -739,21 +739,21 @@
     data.mtlcmdbuffer = nil;
     data.mtlbackbuffer = nil;
     data.beginScene = YES;
-}
+}}
 
 static void
 METAL_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
-{
+{ @autoreleasepool {
     id<MTLTexture> mtltexture = CFBridgingRelease(texture->driverdata);
 #if !__has_feature(objc_arc)
     [mtltexture release];
 #endif
     texture->driverdata = NULL;
-}
+}}
 
 static void
 METAL_DestroyRenderer(SDL_Renderer * renderer)
-{
+{ @autoreleasepool {
     if (renderer->driverdata) {
         METAL_RenderData *data = CFBridgingRelease(renderer->driverdata);
 
@@ -784,7 +784,7 @@
 #endif
     }
     SDL_free(renderer);
-}
+}}
 
 #endif /* SDL_VIDEO_RENDER_METAL && !SDL_RENDER_DISABLED */
 


More information about the commits mailing list