Fixed bug 4484 - use SIMD aligned memory for SDL_Surface
authorSylvain Becker <sylvain.becker@gmail.com>
Mon, 04 Feb 2019 09:11:07 +0100
changeset 1259166cd8731c3b1
parent 12590 b50bedb4bca0
child 12592 10722106b650
Fixed bug 4484 - use SIMD aligned memory for SDL_Surface

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().
src/video/SDL_RLEaccel.c
src/video/SDL_surface.c
     1.1 --- a/src/video/SDL_RLEaccel.c	Mon Feb 04 08:43:37 2019 +0100
     1.2 +++ b/src/video/SDL_RLEaccel.c	Mon Feb 04 09:11:07 2019 +0100
     1.3 @@ -89,6 +89,7 @@
     1.4  #include "SDL_sysvideo.h"
     1.5  #include "SDL_blit.h"
     1.6  #include "SDL_RLEaccel_c.h"
     1.7 +#include "../cpuinfo/SDL_simd.h"
     1.8  
     1.9  #ifndef MIN
    1.10  #define MIN(a, b) ((a) < (b) ? (a) : (b))
    1.11 @@ -1220,8 +1221,9 @@
    1.12  
    1.13      /* Now that we have it encoded, release the original pixels */
    1.14      if (!(surface->flags & SDL_PREALLOC)) {
    1.15 -        SDL_free(surface->pixels);
    1.16 +        SDL_SIMDFree(surface->pixels);
    1.17          surface->pixels = NULL;
    1.18 +        surface->flags &= ~SDL_SIMD_ALIGNED;
    1.19      }
    1.20  
    1.21      /* realloc the buffer to release unused memory */
    1.22 @@ -1383,8 +1385,9 @@
    1.23  
    1.24      /* Now that we have it encoded, release the original pixels */
    1.25      if (!(surface->flags & SDL_PREALLOC)) {
    1.26 -        SDL_free(surface->pixels);
    1.27 +        SDL_SIMDFree(surface->pixels);
    1.28          surface->pixels = NULL;
    1.29 +        surface->flags &= ~SDL_SIMD_ALIGNED;
    1.30      }
    1.31  
    1.32      /* realloc the buffer to release unused memory */
    1.33 @@ -1484,10 +1487,11 @@
    1.34          uncopy_opaque = uncopy_transl = uncopy_32;
    1.35      }
    1.36  
    1.37 -    surface->pixels = SDL_malloc(surface->h * surface->pitch);
    1.38 +    surface->pixels = SDL_SIMDAlloc(surface->h * surface->pitch);
    1.39      if (!surface->pixels) {
    1.40          return (SDL_FALSE);
    1.41      }
    1.42 +    surface->flags |= SDL_SIMD_ALIGNED;
    1.43      /* fill background with transparent pixels */
    1.44      SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
    1.45  
    1.46 @@ -1549,12 +1553,13 @@
    1.47                  SDL_Rect full;
    1.48  
    1.49                  /* re-create the original surface */
    1.50 -                surface->pixels = SDL_malloc(surface->h * surface->pitch);
    1.51 +                surface->pixels = SDL_SIMDAlloc(surface->h * surface->pitch);
    1.52                  if (!surface->pixels) {
    1.53                      /* Oh crap... */
    1.54                      surface->flags |= SDL_RLEACCEL;
    1.55                      return;
    1.56                  }
    1.57 +                surface->flags |= SDL_SIMD_ALIGNED;
    1.58  
    1.59                  /* fill it with the background color */
    1.60                  SDL_FillRect(surface, NULL, surface->map->info.colorkey);
     2.1 --- a/src/video/SDL_surface.c	Mon Feb 04 08:43:37 2019 +0100
     2.2 +++ b/src/video/SDL_surface.c	Mon Feb 04 09:11:07 2019 +0100
     2.3 @@ -120,12 +120,13 @@
     2.4              return NULL;
     2.5          }
     2.6  
     2.7 -        surface->pixels = SDL_malloc((size_t)size);
     2.8 +        surface->pixels = SDL_SIMDAlloc((size_t)size);
     2.9          if (!surface->pixels) {
    2.10              SDL_FreeSurface(surface);
    2.11              SDL_OutOfMemory();
    2.12              return NULL;
    2.13          }
    2.14 +        surface->flags |= SDL_SIMD_ALIGNED;
    2.15          /* This is important for bitmaps */
    2.16          SDL_memset(surface->pixels, 0, surface->h * surface->pitch);
    2.17      }