SDL: blit_testRandomToRandomSVGAMultipleIterations: avoid 250000 allocations

From 36b84e7e0047b7f237bf6303bd1a56d857fb97a9 Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Fri, 30 Aug 2024 22:22:49 +0200
Subject: [PATCH] blit_testRandomToRandomSVGAMultipleIterations: avoid 250000
 allocations

---
 test/testautomation_blit.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)

diff --git a/test/testautomation_blit.c b/test/testautomation_blit.c
index 2eec3b19c4c6a..f13d9abf95f80 100644
--- a/test/testautomation_blit.c
+++ b/test/testautomation_blit.c
@@ -45,10 +45,9 @@ void blitSetUp(void *arg) {
     rngState[1] = 2;
 }
 /*
- * Generates a stream of PRNG pixel data given length
+ * Fill buffer with stream of PRNG pixel data given size
  */
-Uint32 *getNextRandomBuffer(const int width, const int height) {
-    Uint32* buf = SDL_malloc(sizeof(Uint32) * width * height);
+static Uint32 *fillNextRandomBuffer(Uint32 *buf, const int width, const int height) {
     int i;
     for (i = 0; i < width * height; i++) {
         buf[i] = getRandomUint32();
@@ -56,10 +55,12 @@ Uint32 *getNextRandomBuffer(const int width, const int height) {
     return buf;
 }
 /*
- * Generates a small 15 x 15px surface of PRNG pixel data
+ * Generates a stream of PRNG pixel data given length
  */
-SDL_Surface* getRandomBlitChunk(Uint32 *pixels, SDL_PixelFormat format) {
-    return SDL_CreateSurfaceFrom(15, 15, format, pixels, 15 * 4);
+static Uint32 *getNextRandomBuffer(const int width, const int height) {
+    Uint32* buf = SDL_malloc(sizeof(Uint32) * width * height);
+    fillNextRandomBuffer(buf, width, height);
+    return buf;
 }
 /*
  * Generates a 800 x 600 surface of PRNG pixel data
@@ -154,15 +155,22 @@ int blit_testRandomToRandomSVGA(void *arg) {
 int blit_testRandomToRandomSVGAMultipleIterations(void *arg) {
     const int width = 800;
     const int height = 600;
+    const int blit_width = 15;
+    const int blit_height = 15;
     int i;
     const Uint32 correct_hash = 0x5d26be78;
+    Uint32 *buf = SDL_malloc(blit_width * blit_height * sizeof(Uint32));
     // Create blank source surface
+    SDL_Surface *sourceSurface = SDL_CreateSurface(blit_width, blit_height, SDL_PIXELFORMAT_RGBA8888);
+    // Create blank destination surface
     SDL_Surface* dest_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_ABGR8888);
 
     // Perform 250k random blits into random areas of the blank surface
     for (i = 0; i < 250000; i++) {
-        Uint32 *buf = getNextRandomBuffer(15, 15);
-        SDL_Surface *sourceSurface = getRandomBlitChunk(buf, SDL_PIXELFORMAT_RGBA8888);
+        fillNextRandomBuffer(buf, blit_width, blit_height);
+        SDL_LockSurface(sourceSurface);
+        SDL_memcpy(sourceSurface->pixels, buf, blit_width * blit_height * sizeof(Uint32));
+        SDL_UnlockSurface(sourceSurface);
 
         SDL_Rect dest_rect;
         int location = (int)getRandomUint32();
@@ -170,9 +178,6 @@ int blit_testRandomToRandomSVGAMultipleIterations(void *arg) {
         dest_rect.y = location % (height - 15 - 1);
 
         SDL_BlitSurface(sourceSurface, NULL, dest_surface, &dest_rect);
-
-        SDL_DestroySurface(sourceSurface);
-        SDL_free(buf);
     }
     // Check result
     const Uint32 hash = hashSurfacePixels(dest_surface);
@@ -181,6 +186,8 @@ int blit_testRandomToRandomSVGAMultipleIterations(void *arg) {
     SDLTest_AssertCheck(hash == correct_hash,
                         "Should render identically, expected hash 0x%" SDL_PRIx32 ", got 0x%" SDL_PRIx32,
                         correct_hash, hash);
+    SDL_DestroySurface(sourceSurface);
+    SDL_free(buf);
     return TEST_COMPLETED;
 }