testautomation_blit.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * SDL_BlitSurface bit-perfect rendering test suite written by Isaac Aronson
  3. */
  4. /* Suppress C4996 VS compiler warnings for unlink() */
  5. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
  6. #define _CRT_SECURE_NO_DEPRECATE
  7. #endif
  8. #if defined(_MSC_VER) && !defined(_CRT_NONSTDC_NO_DEPRECATE)
  9. #define _CRT_NONSTDC_NO_DEPRECATE
  10. #endif
  11. #include <stdio.h>
  12. #ifndef _MSC_VER
  13. #include <unistd.h>
  14. #else
  15. /* Suppress uint64 to uint32 conversion warning within the PRNG engine */
  16. #pragma warning( disable : 4244 )
  17. #endif
  18. #include <sys/stat.h>
  19. #include <SDL3/SDL.h>
  20. #include <SDL3/SDL_test.h>
  21. #include "testautomation_images.h"
  22. /* ====== xoroshiro128+ PRNG engine for deterministic blit input ===== */
  23. Uint64 rotl(Uint64 x, int k) { return (x << k) | (x >> (-k & 63)); }
  24. Uint64 next(Uint64 state[2]) {
  25. Uint64 s0 = state[0], s1 = state[1];
  26. Uint64 result = rotl((s0 + s1) * 9, 29) + s0;
  27. state[0] = s0 ^ rotl(s1, 29);
  28. state[1] = s0 ^ s1 << 9;
  29. return result;
  30. }
  31. static Uint64 rngState[2] = {1, 2};
  32. Uint32 getRandomUint32(void) {
  33. return (Uint32)next(rngState);
  34. }
  35. /* ================= Test Case Helper Functions ================== */
  36. /*
  37. * Resets PRNG state to initialize tests using PRNG
  38. */
  39. void SDLCALL blitSetUp(void **arg) {
  40. rngState[0] = 1;
  41. rngState[1] = 2;
  42. }
  43. /*
  44. * Fill buffer with stream of PRNG pixel data given size
  45. */
  46. static Uint32 *fillNextRandomBuffer(Uint32 *buf, const int width, const int height) {
  47. int i;
  48. for (i = 0; i < width * height; i++) {
  49. buf[i] = getRandomUint32();
  50. }
  51. return buf;
  52. }
  53. /*
  54. * Generates a stream of PRNG pixel data given length
  55. */
  56. static Uint32 *getNextRandomBuffer(const int width, const int height) {
  57. Uint32* buf = SDL_malloc(sizeof(Uint32) * width * height);
  58. fillNextRandomBuffer(buf, width, height);
  59. return buf;
  60. }
  61. /*
  62. * Generates a 800 x 600 surface of PRNG pixel data
  63. */
  64. SDL_Surface* getRandomSVGASurface(Uint32 *pixels, SDL_PixelFormat format) {
  65. return SDL_CreateSurfaceFrom(800, 600, format, pixels, 800 * 4);
  66. }
  67. /*
  68. * Calculates the FNV-1a hash of input pixel data
  69. */
  70. Uint32 FNVHash(Uint32* buf, int length) {
  71. const Uint32 fnv_prime = 0x811C9DC5;
  72. Uint32 hash = 0;
  73. int i;
  74. for (i = 0; i < length; buf++, i++)
  75. {
  76. hash *= fnv_prime;
  77. hash ^= (*buf);
  78. }
  79. return hash;
  80. }
  81. /*
  82. * Wraps the FNV-1a hash for an input surface's pixels
  83. */
  84. Uint32 hashSurfacePixels(SDL_Surface * surface) {
  85. Uint64 buffer_size = surface->w * surface->h;
  86. return FNVHash(surface->pixels, buffer_size);
  87. }
  88. /* ================= Test Case Implementation ================== */
  89. /**
  90. * Tests rendering a rainbow gradient background onto a blank surface, then rendering a sprite with complex geometry and
  91. * transparency on top of said surface, and comparing the result to known accurate renders with a hash.
  92. */
  93. static int SDLCALL blit_testExampleApplicationRender(void *arg) {
  94. const int width = 32;
  95. const int height = 32;
  96. const Uint32 correct_hash = 0xe345d7a7;
  97. SDL_Surface* dest_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_ARGB8888);
  98. SDL_Surface* rainbow_background = SDLTest_ImageBlendingBackground();
  99. SDL_Surface* gearbrain_sprite = SDLTest_ImageBlendingSprite();
  100. // Blit background into "screen"
  101. SDL_BlitSurface(rainbow_background, NULL, dest_surface, NULL);
  102. // Blit example game sprite onto "screen"
  103. SDL_BlitSurface(gearbrain_sprite, NULL, dest_surface, NULL);
  104. // Check result
  105. const Uint32 hash = hashSurfacePixels(dest_surface);
  106. SDLTest_AssertCheck(hash == correct_hash,
  107. "Should render identically, expected hash 0x%" SDL_PRIx32 ", got 0x%" SDL_PRIx32,
  108. correct_hash, hash);
  109. // Clean up
  110. SDL_DestroySurface(rainbow_background);
  111. SDL_DestroySurface(gearbrain_sprite);
  112. SDL_DestroySurface(dest_surface);
  113. return TEST_COMPLETED;
  114. }
  115. /**
  116. * Tests rendering PRNG noise onto a surface of PRNG noise, while also testing color shift operations between the
  117. * different source and destination pixel formats, without an alpha shuffle, at SVGA resolution. Compares to known
  118. * accurate renders with a hash.
  119. */
  120. static int SDLCALL blit_testRandomToRandomSVGA(void *arg) {
  121. const int width = 800;
  122. const int height = 600;
  123. const Uint32 correct_hash = 0x42140c5f;
  124. // Allocate random buffers
  125. Uint32 *dest_pixels = getNextRandomBuffer(width, height);
  126. Uint32 *src_pixels = getNextRandomBuffer(width, height);
  127. // Create surfaces of different pixel formats
  128. SDL_Surface* dest_surface = getRandomSVGASurface(dest_pixels, SDL_PIXELFORMAT_BGRA8888);
  129. SDL_Surface* src_surface = getRandomSVGASurface(src_pixels, SDL_PIXELFORMAT_RGBA8888);
  130. // Blit surfaces
  131. SDL_BlitSurface(src_surface, NULL, dest_surface, NULL);
  132. // Check result
  133. const Uint32 hash = hashSurfacePixels(dest_surface);
  134. SDLTest_AssertCheck(hash == correct_hash,
  135. "Should render identically, expected hash 0x%" SDL_PRIx32 ", got 0x%" SDL_PRIx32,
  136. correct_hash, hash);
  137. // Clean up
  138. SDL_DestroySurface(dest_surface);
  139. SDL_DestroySurface(src_surface);
  140. SDL_free(dest_pixels);
  141. SDL_free(src_pixels);
  142. return TEST_COMPLETED;
  143. }
  144. /**
  145. * Tests rendering small chunks of 15 by 15px PRNG noise onto an initially blank SVGA surface, while also testing color
  146. * shift operations between the different source and destination pixel formats, including an alpha shuffle. Compares to
  147. * known accurate renders with a hash.
  148. */
  149. static int SDLCALL blit_testRandomToRandomSVGAMultipleIterations(void *arg) {
  150. const int width = 800;
  151. const int height = 600;
  152. const int blit_width = 15;
  153. const int blit_height = 15;
  154. int i;
  155. const Uint32 correct_hash = 0x5d26be78;
  156. Uint32 *buf = SDL_malloc(blit_width * blit_height * sizeof(Uint32));
  157. // Create blank source surface
  158. SDL_Surface *sourceSurface = SDL_CreateSurface(blit_width, blit_height, SDL_PIXELFORMAT_RGBA8888);
  159. // Create blank destination surface
  160. SDL_Surface* dest_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_ABGR8888);
  161. // Perform 250k random blits into random areas of the blank surface
  162. for (i = 0; i < 250000; i++) {
  163. fillNextRandomBuffer(buf, blit_width, blit_height);
  164. SDL_LockSurface(sourceSurface);
  165. SDL_memcpy(sourceSurface->pixels, buf, blit_width * blit_height * sizeof(Uint32));
  166. SDL_UnlockSurface(sourceSurface);
  167. SDL_Rect dest_rect;
  168. int location = (int)getRandomUint32();
  169. dest_rect.x = location % (width - 15 - 1);
  170. dest_rect.y = location % (height - 15 - 1);
  171. SDL_BlitSurface(sourceSurface, NULL, dest_surface, &dest_rect);
  172. }
  173. // Check result
  174. const Uint32 hash = hashSurfacePixels(dest_surface);
  175. // Clean up
  176. SDL_DestroySurface(dest_surface);
  177. SDLTest_AssertCheck(hash == correct_hash,
  178. "Should render identically, expected hash 0x%" SDL_PRIx32 ", got 0x%" SDL_PRIx32,
  179. correct_hash, hash);
  180. SDL_DestroySurface(sourceSurface);
  181. SDL_free(buf);
  182. return TEST_COMPLETED;
  183. }
  184. static const SDLTest_TestCaseReference blitTest1 = {
  185. blit_testExampleApplicationRender, "blit_testExampleApplicationRender",
  186. "Test example application render.", TEST_ENABLED
  187. };
  188. static const SDLTest_TestCaseReference blitTest2 = {
  189. blit_testRandomToRandomSVGA, "blit_testRandomToRandomSVGA",
  190. "Test SVGA noise render.", TEST_ENABLED
  191. };
  192. static const SDLTest_TestCaseReference blitTest3 = {
  193. blit_testRandomToRandomSVGAMultipleIterations, "blit_testRandomToRandomSVGAMultipleIterations",
  194. "Test SVGA noise render (250k iterations).", TEST_ENABLED
  195. };
  196. static const SDLTest_TestCaseReference *blitTests[] = {
  197. &blitTest1, &blitTest2, &blitTest3, NULL
  198. };
  199. SDLTest_TestSuiteReference blitTestSuite = {
  200. "Blending",
  201. blitSetUp,
  202. blitTests,
  203. NULL
  204. };