Skip to content

Commit

Permalink
Fixed bug 4484 - use SIMD aligned memory for SDL_Surface
Browse files Browse the repository at this point in the history
Surfaces are allocated using SDL_SIMDAlloc()
They are marked with SDL_SIMD_ALIGNED flag to appropriatly free them with SDL_SIMDFree()
(Flag is cleared when pixels is free'd in RLE, in case user would hijack the pixels ptr)

When providing its own memory pointer (SDL_CreateRGBSurfaceFrom()) and clearing
SDL_PREALLOC  to delegate to SDL the memory free, it's the responsability of the user
to add SDL_SIMD_ALIGNED or not, whether the pointer has been allocated with SDL_malloc() or
SDL_SIMDAlloc().
  • Loading branch information
1bsyl committed Feb 4, 2019
1 parent 9292dc7 commit 670f3d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/video/SDL_RLEaccel.c
Expand Up @@ -89,6 +89,7 @@
#include "SDL_sysvideo.h"
#include "SDL_blit.h"
#include "SDL_RLEaccel_c.h"
#include "../cpuinfo/SDL_simd.h"

#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
Expand Down Expand Up @@ -1220,8 +1221,9 @@ RLEAlphaSurface(SDL_Surface * surface)

/* Now that we have it encoded, release the original pixels */
if (!(surface->flags & SDL_PREALLOC)) {
SDL_free(surface->pixels);
SDL_SIMDFree(surface->pixels);
surface->pixels = NULL;
surface->flags &= ~SDL_SIMD_ALIGNED;
}

/* realloc the buffer to release unused memory */
Expand Down Expand Up @@ -1383,8 +1385,9 @@ RLEColorkeySurface(SDL_Surface * surface)

/* Now that we have it encoded, release the original pixels */
if (!(surface->flags & SDL_PREALLOC)) {
SDL_free(surface->pixels);
SDL_SIMDFree(surface->pixels);
surface->pixels = NULL;
surface->flags &= ~SDL_SIMD_ALIGNED;
}

/* realloc the buffer to release unused memory */
Expand Down Expand Up @@ -1484,10 +1487,11 @@ UnRLEAlpha(SDL_Surface * surface)
uncopy_opaque = uncopy_transl = uncopy_32;
}

surface->pixels = SDL_malloc(surface->h * surface->pitch);
surface->pixels = SDL_SIMDAlloc(surface->h * surface->pitch);
if (!surface->pixels) {
return (SDL_FALSE);
}
surface->flags |= SDL_SIMD_ALIGNED;
/* fill background with transparent pixels */
SDL_memset(surface->pixels, 0, surface->h * surface->pitch);

Expand Down Expand Up @@ -1549,12 +1553,13 @@ SDL_UnRLESurface(SDL_Surface * surface, int recode)
SDL_Rect full;

/* re-create the original surface */
surface->pixels = SDL_malloc(surface->h * surface->pitch);
surface->pixels = SDL_SIMDAlloc(surface->h * surface->pitch);
if (!surface->pixels) {
/* Oh crap... */
surface->flags |= SDL_RLEACCEL;
return;
}
surface->flags |= SDL_SIMD_ALIGNED;

/* fill it with the background color */
SDL_FillRect(surface, NULL, surface->map->info.colorkey);
Expand Down
3 changes: 2 additions & 1 deletion src/video/SDL_surface.c
Expand Up @@ -120,12 +120,13 @@ SDL_CreateRGBSurfaceWithFormat(Uint32 flags, int width, int height, int depth,
return NULL;
}

surface->pixels = SDL_malloc((size_t)size);
surface->pixels = SDL_SIMDAlloc((size_t)size);
if (!surface->pixels) {
SDL_FreeSurface(surface);
SDL_OutOfMemory();
return NULL;
}
surface->flags |= SDL_SIMD_ALIGNED;
/* This is important for bitmaps */
SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
}
Expand Down

0 comments on commit 670f3d3

Please sign in to comment.