Browse Source

Optimized SDL_DuplicatePixels()

Sam Lantinga 1 year ago
parent
commit
c3e821fa80
1 changed files with 15 additions and 9 deletions
  1. 15 9
      src/video/SDL_surface.c

+ 15 - 9
src/video/SDL_surface.c

@@ -1554,17 +1554,23 @@ static void SDL_DestroySurfaceOnStack(SDL_Surface *surface)
 
 SDL_Surface *SDL_DuplicatePixels(int width, int height, Uint32 format, SDL_Colorspace colorspace, void *pixels, int pitch)
 {
-    SDL_Surface surface;
-    SDL_PixelFormat fmt;
-    SDL_BlitMap blitmap;
-    SDL_Surface *result = NULL;
-
-    if (SDL_CreateSurfaceOnStack(width, height, format, colorspace, pixels, pitch, &surface, &fmt, &blitmap)) {
-        result = SDL_DuplicateSurface(&surface);
+    SDL_Surface *surface = SDL_CreateSurface(width, height, format);
+    if (surface) {
+        int length = width * SDL_BYTESPERPIXEL(format);
+        Uint8 *src = (Uint8 *)pixels;
+        Uint8 *dst = (Uint8 *)surface->pixels;
+        int rows = height;
+        while (rows--) {
+            SDL_memcpy(dst, src, length);
+            dst += surface->pitch;
+            src += pitch;
+        }
 
-        SDL_DestroySurfaceOnStack(&surface);
+        if (colorspace != SDL_GetDefaultColorspaceForFormat(format)) {
+            SDL_SetNumberProperty(SDL_GetSurfaceProperties(surface), SDL_PROP_SURFACE_COLORSPACE_NUMBER, colorspace);
+        }
     }
-    return result;
+    return surface;
 }
 
 int SDL_ConvertPixelsAndColorspace(int width, int height,