testautomation_surface.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221
  1. /**
  2. * Original code: automated SDL surface test written by Edgar Simo "bobbens"
  3. * Adapted/rewritten for test lib by Andreas Schiffler
  4. */
  5. #include <stdio.h>
  6. #ifndef _MSC_VER
  7. #include <unistd.h>
  8. #endif
  9. #include <sys/stat.h>
  10. #include <SDL3/SDL.h>
  11. #include <SDL3/SDL_test.h>
  12. #include "testautomation_suites.h"
  13. #include "testautomation_images.h"
  14. #define CHECK_FUNC(FUNC, PARAMS) \
  15. { \
  16. bool result = FUNC PARAMS; \
  17. if (!result) { \
  18. SDLTest_AssertCheck(result, "Validate result from %s, expected: true, got: false, %s", #FUNC, SDL_GetError()); \
  19. } \
  20. }
  21. /* ================= Test Case Implementation ================== */
  22. /* Shared test surface */
  23. static SDL_Surface *referenceSurface = NULL;
  24. static SDL_Surface *testSurface = NULL;
  25. /* Fixture */
  26. /* Create a 32-bit writable surface for blitting tests */
  27. static void SDLCALL surfaceSetUp(void **arg)
  28. {
  29. int result;
  30. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  31. SDL_BlendMode currentBlendMode;
  32. referenceSurface = SDLTest_ImageBlit(); /* For size info */
  33. testSurface = SDL_CreateSurface(referenceSurface->w, referenceSurface->h, SDL_PIXELFORMAT_RGBA32);
  34. SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface is not NULL");
  35. if (testSurface != NULL) {
  36. /* Disable blend mode for target surface */
  37. result = SDL_SetSurfaceBlendMode(testSurface, blendMode);
  38. SDLTest_AssertCheck(result == true, "Validate result from SDL_SetSurfaceBlendMode, expected: true, got: %i", result);
  39. result = SDL_GetSurfaceBlendMode(testSurface, &currentBlendMode);
  40. SDLTest_AssertCheck(result == true, "Validate result from SDL_GetSurfaceBlendMode, expected: true, got: %i", result);
  41. SDLTest_AssertCheck(currentBlendMode == blendMode, "Validate blendMode, expected: %" SDL_PRIu32 ", got: %" SDL_PRIu32, blendMode, currentBlendMode);
  42. /* Clear the target surface */
  43. result = SDL_FillSurfaceRect(testSurface, NULL, SDL_MapSurfaceRGBA(testSurface, 0, 0, 0, 255));
  44. SDLTest_AssertCheck(result == true, "Validate result from SDL_FillSurfaceRect, expected: true, got: %i", result);
  45. }
  46. }
  47. static void SDLCALL surfaceTearDown(void *arg)
  48. {
  49. SDL_DestroySurface(referenceSurface);
  50. referenceSurface = NULL;
  51. SDL_DestroySurface(testSurface);
  52. testSurface = NULL;
  53. }
  54. static void DitherPalette(SDL_Palette *palette)
  55. {
  56. int i;
  57. for (i = 0; i < palette->ncolors; i++) {
  58. int r, g, b;
  59. /* map each bit field to the full [0, 255] interval,
  60. so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
  61. r = i & 0xe0;
  62. r |= r >> 3 | r >> 6;
  63. palette->colors[i].r = (Uint8)r;
  64. g = (i << 3) & 0xe0;
  65. g |= g >> 3 | g >> 6;
  66. palette->colors[i].g = (Uint8)g;
  67. b = i & 0x3;
  68. b |= b << 2;
  69. b |= b << 4;
  70. palette->colors[i].b = (Uint8)b;
  71. palette->colors[i].a = SDL_ALPHA_OPAQUE;
  72. }
  73. }
  74. /**
  75. * Helper that blits in a specific blend mode, -1 for color mod, -2 for alpha mod
  76. */
  77. static void testBlitBlendModeWithFormats(int mode, SDL_PixelFormat src_format, SDL_PixelFormat dst_format)
  78. {
  79. /* Allow up to 1 delta from theoretical value to account for rounding error */
  80. const int MAXIMUM_ERROR = 1;
  81. int ret;
  82. SDL_Surface *src;
  83. SDL_Surface *dst;
  84. Uint32 color;
  85. Uint8 srcR = 10, srcG = 128, srcB = 240, srcA = 100;
  86. Uint8 dstR = 128, dstG = 128, dstB = 128, dstA = 128;
  87. Uint8 expectedR, expectedG, expectedB, expectedA;
  88. Uint8 actualR, actualG, actualB, actualA;
  89. int deltaR, deltaG, deltaB, deltaA;
  90. /* Create dst surface */
  91. dst = SDL_CreateSurface(9, 1, dst_format);
  92. SDLTest_AssertCheck(dst != NULL, "Verify dst surface is not NULL");
  93. if (dst == NULL) {
  94. return;
  95. }
  96. /* Clear surface. */
  97. if (SDL_ISPIXELFORMAT_INDEXED(dst_format)) {
  98. SDL_Palette *palette = SDL_CreateSurfacePalette(dst);
  99. DitherPalette(palette);
  100. palette->colors[0].r = dstR;
  101. palette->colors[0].g = dstG;
  102. palette->colors[0].b = dstB;
  103. palette->colors[0].a = dstA;
  104. color = 0;
  105. } else {
  106. color = SDL_MapSurfaceRGBA(dst, dstR, dstG, dstB, dstA);
  107. SDLTest_AssertPass("Call to SDL_MapSurfaceRGBA()");
  108. }
  109. ret = SDL_FillSurfaceRect(dst, NULL, color);
  110. SDLTest_AssertPass("Call to SDL_FillSurfaceRect()");
  111. SDLTest_AssertCheck(ret == true, "Verify result from SDL_FillSurfaceRect, expected: true, got: %i", ret);
  112. SDL_GetRGBA(color, SDL_GetPixelFormatDetails(dst->format), SDL_GetSurfacePalette(dst), &dstR, &dstG, &dstB, &dstA);
  113. /* Create src surface */
  114. src = SDL_CreateSurface(9, 1, src_format);
  115. SDLTest_AssertCheck(src != NULL, "Verify src surface is not NULL");
  116. if (src == NULL) {
  117. return;
  118. }
  119. if (SDL_ISPIXELFORMAT_INDEXED(src_format)) {
  120. SDL_Palette *palette = SDL_CreateSurfacePalette(src);
  121. palette->colors[0].r = srcR;
  122. palette->colors[0].g = srcG;
  123. palette->colors[0].b = srcB;
  124. palette->colors[0].a = srcA;
  125. }
  126. /* Reset alpha modulation */
  127. ret = SDL_SetSurfaceAlphaMod(src, 255);
  128. SDLTest_AssertPass("Call to SDL_SetSurfaceAlphaMod()");
  129. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceAlphaMod(), expected: true, got: %i", ret);
  130. /* Reset color modulation */
  131. ret = SDL_SetSurfaceColorMod(src, 255, 255, 255);
  132. SDLTest_AssertPass("Call to SDL_SetSurfaceColorMod()");
  133. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorMod(), expected: true, got: %i", ret);
  134. /* Reset color key */
  135. ret = SDL_SetSurfaceColorKey(src, false, 0);
  136. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  137. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey(), expected: true, got: %i", ret);
  138. /* Clear surface. */
  139. color = SDL_MapSurfaceRGBA(src, srcR, srcG, srcB, srcA);
  140. SDLTest_AssertPass("Call to SDL_MapSurfaceRGBA()");
  141. ret = SDL_FillSurfaceRect(src, NULL, color);
  142. SDLTest_AssertPass("Call to SDL_FillSurfaceRect()");
  143. SDLTest_AssertCheck(ret == true, "Verify result from SDL_FillSurfaceRect, expected: true, got: %i", ret);
  144. SDL_GetRGBA(color, SDL_GetPixelFormatDetails(src->format), SDL_GetSurfacePalette(src), &srcR, &srcG, &srcB, &srcA);
  145. /* Set blend mode. */
  146. if (mode >= 0) {
  147. ret = SDL_SetSurfaceBlendMode(src, (SDL_BlendMode)mode);
  148. SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
  149. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: true, got: %i", mode, ret);
  150. } else {
  151. ret = SDL_SetSurfaceBlendMode(src, SDL_BLENDMODE_BLEND);
  152. SDLTest_AssertPass("Call to SDL_SetSurfaceBlendMode()");
  153. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceBlendMode(..., %i), expected: true, got: %i", mode, ret);
  154. }
  155. /* Test blend mode. */
  156. #define FLOAT(X) ((float)X / 255.0f)
  157. switch (mode) {
  158. case -1:
  159. /* Set color mod. */
  160. ret = SDL_SetSurfaceColorMod(src, srcR, srcG, srcB);
  161. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetSurfaceColorMod, expected: true, got: %i", ret);
  162. expectedR = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcR) * FLOAT(srcR)) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  163. expectedG = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcG) * FLOAT(srcG)) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  164. expectedB = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcB) * FLOAT(srcB)) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  165. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  166. break;
  167. case -2:
  168. /* Set alpha mod. */
  169. ret = SDL_SetSurfaceAlphaMod(src, srcA);
  170. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetSurfaceAlphaMod, expected: true, got: %i", ret);
  171. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * (FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstR) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  172. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * (FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstG) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  173. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * (FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstB) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  174. expectedA = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstA) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  175. break;
  176. case SDL_BLENDMODE_NONE:
  177. expectedR = srcR;
  178. expectedG = srcG;
  179. expectedB = srcB;
  180. expectedA = SDL_ISPIXELFORMAT_ALPHA(dst_format) ? srcA : 255;
  181. break;
  182. case SDL_BLENDMODE_BLEND:
  183. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  184. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  185. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  186. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  187. break;
  188. case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
  189. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  190. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  191. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  192. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  193. break;
  194. case SDL_BLENDMODE_ADD:
  195. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  196. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  197. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  198. expectedA = dstA;
  199. break;
  200. case SDL_BLENDMODE_ADD_PREMULTIPLIED:
  201. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  202. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  203. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  204. expectedA = dstA;
  205. break;
  206. case SDL_BLENDMODE_MOD:
  207. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  208. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  209. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  210. expectedA = dstA;
  211. break;
  212. case SDL_BLENDMODE_MUL:
  213. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  214. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  215. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  216. expectedA = dstA;
  217. break;
  218. default:
  219. SDLTest_LogError("Invalid blending mode: %d", mode);
  220. return;
  221. }
  222. if (SDL_ISPIXELFORMAT_INDEXED(dst_format)) {
  223. SDL_Palette *palette = SDL_GetSurfacePalette(dst);
  224. palette->colors[1].r = expectedR;
  225. palette->colors[1].g = expectedG;
  226. palette->colors[1].b = expectedB;
  227. palette->colors[1].a = expectedA;
  228. }
  229. /* Blitting. */
  230. ret = SDL_BlitSurface(src, NULL, dst, NULL);
  231. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_BlitSurface, expected: true, got: %i: %s", ret, !ret ? SDL_GetError() : "success");
  232. if (ret) {
  233. SDL_ReadSurfacePixel(dst, 0, 0, &actualR, &actualG, &actualB, &actualA);
  234. deltaR = SDL_abs((int)actualR - expectedR);
  235. deltaG = SDL_abs((int)actualG - expectedG);
  236. deltaB = SDL_abs((int)actualB - expectedB);
  237. deltaA = SDL_abs((int)actualA - expectedA);
  238. SDLTest_AssertCheck(
  239. deltaR <= MAXIMUM_ERROR &&
  240. deltaG <= MAXIMUM_ERROR &&
  241. deltaB <= MAXIMUM_ERROR &&
  242. deltaA <= MAXIMUM_ERROR,
  243. "Checking %s -> %s blit results, expected %d,%d,%d,%d, got %d,%d,%d,%d",
  244. SDL_GetPixelFormatName(src_format),
  245. SDL_GetPixelFormatName(dst_format),
  246. expectedR, expectedG, expectedB, expectedA, actualR, actualG, actualB, actualA);
  247. }
  248. /* Clean up */
  249. SDL_DestroySurface(src);
  250. SDL_DestroySurface(dst);
  251. }
  252. static void testBlitBlendMode(int mode)
  253. {
  254. const SDL_PixelFormat src_formats[] = {
  255. SDL_PIXELFORMAT_INDEX8, SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  256. };
  257. const SDL_PixelFormat dst_formats[] = {
  258. SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  259. };
  260. int i, j;
  261. for (i = 0; i < SDL_arraysize(src_formats); ++i) {
  262. for (j = 0; j < SDL_arraysize(dst_formats); ++j) {
  263. testBlitBlendModeWithFormats(mode, src_formats[i], dst_formats[j]);
  264. }
  265. }
  266. }
  267. /* Helper to check that a file exists */
  268. static void AssertFileExist(const char *filename)
  269. {
  270. SDLTest_AssertCheck(SDL_GetPathInfo(filename, NULL), "Verify file '%s' exists", filename);
  271. }
  272. /* Test case functions */
  273. /**
  274. * Tests creating surface with invalid format
  275. */
  276. static int SDLCALL surface_testInvalidFormat(void *arg)
  277. {
  278. SDL_Surface *surface;
  279. surface = SDL_CreateSurface(32, 32, SDL_PIXELFORMAT_UNKNOWN);
  280. SDLTest_AssertCheck(surface == NULL, "Verify SDL_CreateSurface(SDL_PIXELFORMAT_UNKNOWN) returned NULL");
  281. SDL_DestroySurface(surface);
  282. surface = SDL_CreateSurfaceFrom(32, 32, SDL_PIXELFORMAT_UNKNOWN, NULL, 0);
  283. SDLTest_AssertCheck(surface == NULL, "Verify SDL_CreateSurfaceFrom(SDL_PIXELFORMAT_UNKNOWN) returned NULL");
  284. SDL_DestroySurface(surface);
  285. return TEST_COMPLETED;
  286. }
  287. /**
  288. * Tests sprite saving and loading
  289. */
  290. static int SDLCALL surface_testSaveLoad(void *arg)
  291. {
  292. int ret;
  293. const char *sampleFilename = "testSaveLoad.tmp";
  294. SDL_Surface *face;
  295. SDL_Surface *rface;
  296. SDL_Palette *palette;
  297. SDL_Color colors[] = {
  298. { 255, 0, 0, SDL_ALPHA_OPAQUE }, /* Red */
  299. { 0, 255, 0, SDL_ALPHA_OPAQUE } /* Green */
  300. };
  301. SDL_IOStream *stream;
  302. Uint8 r, g, b, a;
  303. /* Create sample surface */
  304. face = SDLTest_ImageFace();
  305. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  306. if (face == NULL) {
  307. return TEST_ABORTED;
  308. }
  309. /* Delete test file; ignore errors */
  310. SDL_RemovePath(sampleFilename);
  311. /* Save a BMP surface */
  312. ret = SDL_SaveBMP(face, sampleFilename);
  313. SDLTest_AssertPass("Call to SDL_SaveBMP()");
  314. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SaveBMP, expected: true, got: %i", ret);
  315. AssertFileExist(sampleFilename);
  316. /* Load a BMP surface */
  317. rface = SDL_LoadBMP(sampleFilename);
  318. SDLTest_AssertPass("Call to SDL_LoadBMP()");
  319. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
  320. if (rface != NULL) {
  321. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  322. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  323. SDL_DestroySurface(rface);
  324. rface = NULL;
  325. }
  326. /* Delete test file; ignore errors */
  327. SDL_RemovePath(sampleFilename);
  328. /* Save a PNG surface */
  329. ret = SDL_SavePNG(face, sampleFilename);
  330. SDLTest_AssertPass("Call to SDL_SavePNG()");
  331. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SavePNG, expected: true, got: %i", ret);
  332. AssertFileExist(sampleFilename);
  333. /* Load a PNG surface */
  334. rface = SDL_LoadPNG(sampleFilename);
  335. SDLTest_AssertPass("Call to SDL_LoadPNG()");
  336. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadPNG is not NULL");
  337. if (rface != NULL) {
  338. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  339. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  340. SDL_DestroySurface(rface);
  341. rface = NULL;
  342. }
  343. /* Delete test file; ignore errors */
  344. SDL_RemovePath(sampleFilename);
  345. /* Clean up */
  346. SDL_DestroySurface(face);
  347. face = NULL;
  348. /* Create an 8-bit image */
  349. face = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  350. SDLTest_AssertCheck(face != NULL, "Verify 8-bit surface is not NULL");
  351. if (face == NULL) {
  352. return TEST_ABORTED;
  353. }
  354. palette = SDL_CreatePalette(2);
  355. SDLTest_AssertCheck(palette != NULL, "Verify palette is not NULL");
  356. if (palette == NULL) {
  357. return TEST_ABORTED;
  358. }
  359. SDL_SetPaletteColors(palette, colors, 0, SDL_arraysize(colors));
  360. SDL_SetSurfacePalette(face, palette);
  361. SDL_DestroyPalette(palette);
  362. /* Set a green pixel */
  363. *(Uint8 *)face->pixels = 1;
  364. /* Save and reload as a BMP */
  365. stream = SDL_IOFromDynamicMem();
  366. SDLTest_AssertCheck(stream != NULL, "Verify iostream is not NULL");
  367. if (stream == NULL) {
  368. return TEST_ABORTED;
  369. }
  370. ret = SDL_SaveBMP_IO(face, stream, false);
  371. SDLTest_AssertPass("Call to SDL_SaveBMP()");
  372. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SaveBMP, expected: true, got: %i", ret);
  373. SDL_SeekIO(stream, 0, SDL_IO_SEEK_SET);
  374. rface = SDL_LoadBMP_IO(stream, false);
  375. SDLTest_AssertPass("Call to SDL_LoadBMP()");
  376. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadBMP is not NULL");
  377. if (rface != NULL) {
  378. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  379. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  380. SDLTest_AssertCheck(rface->format == SDL_PIXELFORMAT_INDEX8, "Verify format of loaded surface, expected: %s, got: %s", SDL_GetPixelFormatName(face->format), SDL_GetPixelFormatName(rface->format));
  381. SDL_ReadSurfacePixel(rface, 0, 0, &r, &g, &b, &a);
  382. SDLTest_AssertCheck(r == colors[1].r &&
  383. g == colors[1].g &&
  384. b == colors[1].b &&
  385. a == colors[1].a,
  386. "Verify color of loaded surface, expected: %d,%d,%d,%d, got: %d,%d,%d,%d",
  387. r, g, b, a,
  388. colors[1].r, colors[1].g, colors[1].b, colors[1].a);
  389. SDL_DestroySurface(rface);
  390. rface = NULL;
  391. }
  392. SDL_CloseIO(stream);
  393. stream = NULL;
  394. /* Save and reload as a PNG */
  395. stream = SDL_IOFromDynamicMem();
  396. SDLTest_AssertCheck(stream != NULL, "Verify iostream is not NULL");
  397. if (stream == NULL) {
  398. return TEST_ABORTED;
  399. }
  400. ret = SDL_SavePNG_IO(face, stream, false);
  401. SDLTest_AssertPass("Call to SDL_SavePNG()");
  402. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SavePNG, expected: true, got: %i", ret);
  403. SDL_SeekIO(stream, 0, SDL_IO_SEEK_SET);
  404. rface = SDL_LoadPNG_IO(stream, false);
  405. SDLTest_AssertPass("Call to SDL_LoadPNG()");
  406. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_LoadPNG is not NULL");
  407. if (rface != NULL) {
  408. SDLTest_AssertCheck(face->w == rface->w, "Verify width of loaded surface, expected: %i, got: %i", face->w, rface->w);
  409. SDLTest_AssertCheck(face->h == rface->h, "Verify height of loaded surface, expected: %i, got: %i", face->h, rface->h);
  410. SDLTest_AssertCheck(rface->format == SDL_PIXELFORMAT_INDEX8, "Verify format of loaded surface, expected: %s, got: %s", SDL_GetPixelFormatName(face->format), SDL_GetPixelFormatName(rface->format));
  411. SDL_ReadSurfacePixel(rface, 0, 0, &r, &g, &b, &a);
  412. SDLTest_AssertCheck(r == colors[1].r &&
  413. g == colors[1].g &&
  414. b == colors[1].b &&
  415. a == colors[1].a,
  416. "Verify color of loaded surface, expected: %d,%d,%d,%d, got: %d,%d,%d,%d",
  417. r, g, b, a,
  418. colors[1].r, colors[1].g, colors[1].b, colors[1].a);
  419. SDL_DestroySurface(rface);
  420. rface = NULL;
  421. }
  422. SDL_CloseIO(stream);
  423. stream = NULL;
  424. SDL_DestroySurface(face);
  425. return TEST_COMPLETED;
  426. }
  427. /**
  428. * Tests tiled blitting.
  429. */
  430. static int SDLCALL surface_testBlitTiled(void *arg)
  431. {
  432. SDL_Surface *face = NULL;
  433. SDL_Surface *testSurface2x = NULL;
  434. SDL_Surface *referenceSurface2x = NULL;
  435. int ret = 0;
  436. /* Create sample surface */
  437. face = SDLTest_ImageFace();
  438. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  439. if (face == NULL) {
  440. return TEST_ABORTED;
  441. }
  442. /* Tiled blit - 1.0 scale */
  443. {
  444. ret = SDL_BlitSurfaceTiled(face, NULL, testSurface, NULL);
  445. SDLTest_AssertCheck(ret == true, "Verify result from SDL_BlitSurfaceTiled expected: true, got: %i", ret);
  446. /* See if it's the same */
  447. SDL_DestroySurface(referenceSurface);
  448. referenceSurface = SDLTest_ImageBlitTiled();
  449. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  450. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  451. }
  452. /* Tiled blit - 2.0 scale */
  453. {
  454. testSurface2x = SDL_CreateSurface(testSurface->w * 2, testSurface->h * 2, testSurface->format);
  455. SDLTest_AssertCheck(testSurface != NULL, "Check that testSurface2x is not NULL");
  456. ret = SDL_FillSurfaceRect(testSurface2x, NULL, SDL_MapSurfaceRGBA(testSurface2x, 0, 0, 0, 255));
  457. SDLTest_AssertCheck(ret == true, "Validate result from SDL_FillSurfaceRect, expected: true, got: %i", ret);
  458. ret = SDL_BlitSurfaceTiledWithScale(face, NULL, 2.0f, SDL_SCALEMODE_NEAREST, testSurface2x, NULL);
  459. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_BlitSurfaceTiledWithScale, expected: true, got: %i", ret);
  460. /* See if it's the same */
  461. referenceSurface2x = SDL_CreateSurface(referenceSurface->w * 2, referenceSurface->h * 2, referenceSurface->format);
  462. SDL_BlitSurfaceScaled(referenceSurface, NULL, referenceSurface2x, NULL, SDL_SCALEMODE_NEAREST);
  463. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_BlitSurfaceScaled, expected: true, got: %i", ret);
  464. ret = SDLTest_CompareSurfaces(testSurface2x, referenceSurface2x, 0);
  465. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  466. }
  467. /* Tiled blit - very small scale */
  468. {
  469. float tiny_scale = 0.01f;
  470. ret = SDL_BlitSurfaceTiledWithScale(face, NULL, tiny_scale, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  471. SDLTest_AssertCheck(ret == true, "Expected SDL_BlitSurfaceTiledWithScale to succeed with very small scale: %f, got: %i", tiny_scale, ret);
  472. }
  473. /* Clean up. */
  474. SDL_DestroySurface(face);
  475. SDL_DestroySurface(testSurface2x);
  476. SDL_DestroySurface(referenceSurface2x);
  477. return TEST_COMPLETED;
  478. }
  479. static const Uint8 COLOR_SEPARATION = 85;
  480. static void Fill9GridReferenceSurface(SDL_Surface *surface, int left_width, int right_width, int top_height, int bottom_height)
  481. {
  482. SDL_Rect rect;
  483. // Upper left
  484. rect.x = 0;
  485. rect.y = 0;
  486. rect.w = left_width;
  487. rect.h = top_height;
  488. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  489. // Top
  490. rect.x = left_width;
  491. rect.y = 0;
  492. rect.w = surface->w - left_width - right_width;
  493. rect.h = top_height;
  494. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  495. // Upper right
  496. rect.x = surface->w - right_width;
  497. rect.y = 0;
  498. rect.w = right_width;
  499. rect.h = top_height;
  500. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  501. // Left
  502. rect.x = 0;
  503. rect.y = top_height;
  504. rect.w = left_width;
  505. rect.h = surface->h - top_height - bottom_height;
  506. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  507. // Center
  508. rect.x = left_width;
  509. rect.y = top_height;
  510. rect.w = surface->w - right_width - left_width;
  511. rect.h = surface->h - top_height - bottom_height;
  512. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  513. // Right
  514. rect.x = surface->w - right_width;
  515. rect.y = top_height;
  516. rect.w = right_width;
  517. rect.h = surface->h - top_height - bottom_height;
  518. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  519. // Lower left
  520. rect.x = 0;
  521. rect.y = surface->h - bottom_height;
  522. rect.w = left_width;
  523. rect.h = bottom_height;
  524. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  525. // Bottom
  526. rect.x = left_width;
  527. rect.y = surface->h - bottom_height;
  528. rect.w = surface->w - left_width - right_width;
  529. rect.h = bottom_height;
  530. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  531. // Lower right
  532. rect.x = surface->w - right_width;
  533. rect.y = surface->h - bottom_height;
  534. rect.w = right_width;
  535. rect.h = bottom_height;
  536. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  537. }
  538. /**
  539. * Tests 9-grid blitting.
  540. */
  541. static int SDLCALL surface_testBlit9Grid(void *arg)
  542. {
  543. SDL_Surface *source = NULL;
  544. int x, y;
  545. int ret = 0;
  546. /* Create source surface */
  547. source = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGBA32);
  548. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  549. for (y = 0; y < 3; ++y) {
  550. for (x = 0; x < 3; ++x) {
  551. SDL_WriteSurfacePixel(source, x, y, (Uint8)((1 + x) * COLOR_SEPARATION), (Uint8)((1 + y) * COLOR_SEPARATION), 0, 255);
  552. }
  553. }
  554. /* 9-grid blit - 1.0 scale */
  555. {
  556. /* Create reference surface */
  557. SDL_DestroySurface(referenceSurface);
  558. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  559. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  560. Fill9GridReferenceSurface(referenceSurface, 1, 1, 1, 1);
  561. ret = SDL_BlitSurface9Grid(source, NULL, 1, 1, 1, 1, 0.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  562. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  563. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  564. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  565. }
  566. /* 9-grid blit - 2.0 scale */
  567. {
  568. /* Create reference surface */
  569. SDL_DestroySurface(referenceSurface);
  570. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  571. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  572. Fill9GridReferenceSurface(referenceSurface, 2, 2, 2, 2);
  573. ret = SDL_BlitSurface9Grid(source, NULL, 1, 1, 1, 1, 2.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  574. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  575. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  576. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  577. }
  578. /* Clean up. */
  579. SDL_DestroySurface(source);
  580. /* Create complex source surface */
  581. source = SDL_CreateSurface(5, 5, SDL_PIXELFORMAT_RGBA32);
  582. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  583. SDL_WriteSurfacePixel(source, 0, 0, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  584. SDL_WriteSurfacePixel(source, 1, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  585. SDL_WriteSurfacePixel(source, 2, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  586. SDL_WriteSurfacePixel(source, 3, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  587. SDL_WriteSurfacePixel(source, 4, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  588. SDL_WriteSurfacePixel(source, 0, 1, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  589. SDL_WriteSurfacePixel(source, 1, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  590. SDL_WriteSurfacePixel(source, 2, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  591. SDL_WriteSurfacePixel(source, 3, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  592. SDL_WriteSurfacePixel(source, 4, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  593. SDL_WriteSurfacePixel(source, 0, 2, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  594. SDL_WriteSurfacePixel(source, 1, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  595. SDL_WriteSurfacePixel(source, 2, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  596. SDL_WriteSurfacePixel(source, 3, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  597. SDL_WriteSurfacePixel(source, 4, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  598. SDL_WriteSurfacePixel(source, 0, 3, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  599. SDL_WriteSurfacePixel(source, 1, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  600. SDL_WriteSurfacePixel(source, 2, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  601. SDL_WriteSurfacePixel(source, 3, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  602. SDL_WriteSurfacePixel(source, 4, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  603. SDL_WriteSurfacePixel(source, 0, 4, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  604. SDL_WriteSurfacePixel(source, 1, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  605. SDL_WriteSurfacePixel(source, 2, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  606. SDL_WriteSurfacePixel(source, 3, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  607. SDL_WriteSurfacePixel(source, 4, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  608. /* complex 9-grid blit - 1.0 scale */
  609. {
  610. SDLTest_Log("complex 9-grid blit - 1.0 scale");
  611. /* Create reference surface */
  612. SDL_DestroySurface(referenceSurface);
  613. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  614. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  615. Fill9GridReferenceSurface(referenceSurface, 1, 2, 1, 2);
  616. ret = SDL_BlitSurface9Grid(source, NULL, 1, 2, 1, 2, 0.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  617. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  618. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  619. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  620. }
  621. /* complex 9-grid blit - 2.0 scale */
  622. {
  623. SDLTest_Log("complex 9-grid blit - 2.0 scale");
  624. /* Create reference surface */
  625. SDL_DestroySurface(referenceSurface);
  626. referenceSurface = SDL_CreateSurface(testSurface->w, testSurface->h, testSurface->format);
  627. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  628. Fill9GridReferenceSurface(referenceSurface, 2, 4, 2, 4);
  629. ret = SDL_BlitSurface9Grid(source, NULL, 1, 2, 1, 2, 2.0f, SDL_SCALEMODE_NEAREST, testSurface, NULL);
  630. SDLTest_AssertCheck(ret == true, "Validate result from SDL_BlitSurface9Grid, expected: true, got: %i", ret);
  631. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, 0);
  632. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  633. }
  634. /* Clean up. */
  635. SDL_DestroySurface(source);
  636. return TEST_COMPLETED;
  637. }
  638. /**
  639. * Tests blitting between multiple surfaces of the same format
  640. */
  641. static int SDLCALL surface_testBlitMultiple(void *arg)
  642. {
  643. SDL_Surface *source, *surface;
  644. SDL_Palette *palette;
  645. Uint8 *pixels;
  646. palette = SDL_CreatePalette(2);
  647. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  648. palette->colors[0].r = 0;
  649. palette->colors[0].g = 0;
  650. palette->colors[0].b = 0;
  651. palette->colors[1].r = 0xFF;
  652. palette->colors[1].g = 0;
  653. palette->colors[1].b = 0;
  654. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  655. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  656. SDL_SetSurfacePalette(source, palette);
  657. *(Uint8 *)source->pixels = 1;
  658. /* Set up a blit to a surface using the palette */
  659. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  660. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  661. SDL_SetSurfacePalette(surface, palette);
  662. pixels = (Uint8 *)surface->pixels;
  663. *pixels = 0;
  664. SDL_BlitSurface(source, NULL, surface, NULL);
  665. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  666. /* Set up a blit to another surface using the same palette */
  667. SDL_DestroySurface(surface);
  668. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  669. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  670. SDL_SetSurfacePalette(surface, palette);
  671. pixels = (Uint8 *)surface->pixels;
  672. *pixels = 0;
  673. SDL_BlitSurface(source, NULL, surface, NULL);
  674. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  675. /* Set up a blit to new surface with a different format */
  676. SDL_DestroySurface(surface);
  677. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  678. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  679. pixels = (Uint8 *)surface->pixels;
  680. SDL_BlitSurface(source, NULL, surface, NULL);
  681. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  682. /* Set up a blit to another surface with the same format */
  683. SDL_DestroySurface(surface);
  684. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  685. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  686. pixels = (Uint8 *)surface->pixels;
  687. SDL_BlitSurface(source, NULL, surface, NULL);
  688. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  689. SDL_DestroyPalette(palette);
  690. SDL_DestroySurface(source);
  691. SDL_DestroySurface(surface);
  692. return TEST_COMPLETED;
  693. }
  694. /**
  695. * Tests operations on surfaces with NULL pixels
  696. */
  697. static int SDLCALL surface_testSurfaceNULLPixels(void *arg)
  698. {
  699. SDL_Surface *a, *b, *face;
  700. bool result;
  701. face = SDLTest_ImageFace();
  702. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  703. if (face == NULL) {
  704. return TEST_ABORTED;
  705. }
  706. /* Test blitting with NULL pixels */
  707. a = SDL_CreateSurfaceFrom(face->w, face->h, SDL_PIXELFORMAT_ARGB8888, NULL, 0);
  708. SDLTest_AssertCheck(a != NULL, "Verify result from SDL_CreateSurfaceFrom() with NULL pixels is not NULL");
  709. result = SDL_BlitSurface(a, NULL, face, NULL);
  710. SDLTest_AssertCheck(!result, "Verify result from SDL_BlitSurface() with src having NULL pixels is false");
  711. result = SDL_BlitSurface(face, NULL, a, NULL);
  712. SDLTest_AssertCheck(!result, "Verify result from SDL_BlitSurface() with dst having NULL pixels is false");
  713. b = SDL_CreateSurfaceFrom(face->w * 2, face->h * 2, SDL_PIXELFORMAT_ARGB8888, NULL, 0);
  714. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_CreateSurfaceFrom() with NULL pixels is not NULL");
  715. result = SDL_BlitSurfaceScaled(b, NULL, face, NULL, SDL_SCALEMODE_NEAREST);
  716. SDLTest_AssertCheck(!result, "Verify result from SDL_BlitSurfaceScaled() with src having NULL pixels is false");
  717. result = SDL_BlitSurfaceScaled(face, NULL, b, NULL, SDL_SCALEMODE_NEAREST);
  718. SDLTest_AssertCheck(!result, "Verify result from SDL_BlitSurfaceScaled() with dst having NULL pixels is false");
  719. SDL_DestroySurface(b);
  720. b = NULL;
  721. /* Test conversion with NULL pixels */
  722. b = SDL_ConvertSurfaceAndColorspace(a, SDL_PIXELFORMAT_ABGR8888, NULL, SDL_COLORSPACE_UNKNOWN, 0);
  723. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ConvertSurfaceAndColorspace() with NULL pixels is not NULL");
  724. SDL_DestroySurface(b);
  725. b = NULL;
  726. /* Test duplication with NULL pixels */
  727. b = SDL_DuplicateSurface(a);
  728. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_DuplicateSurface() with NULL pixels is not NULL");
  729. SDL_DestroySurface(b);
  730. b = NULL;
  731. /* Test scaling with NULL pixels */
  732. b = SDL_ScaleSurface(a, a->w * 2, a->h * 2, SDL_SCALEMODE_NEAREST);
  733. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ScaleSurface() with NULL pixels is not NULL");
  734. SDLTest_AssertCheck(b->pixels == NULL, "Verify pixels from SDL_ScaleSurface() is NULL");
  735. SDL_DestroySurface(b);
  736. b = NULL;
  737. /* Test filling surface with NULL pixels */
  738. result = SDL_FillSurfaceRect(a, NULL, 0);
  739. SDLTest_AssertCheck(result, "Verify result from SDL_FillSurfaceRect() with dst having NULL pixels is true");
  740. /* Clean up. */
  741. SDL_DestroySurface(face);
  742. SDL_DestroySurface(a);
  743. SDL_DestroySurface(b);
  744. return TEST_COMPLETED;
  745. }
  746. /**
  747. * Tests operations on surfaces with RLE pixels
  748. */
  749. static int SDLCALL surface_testSurfaceRLEPixels(void *arg)
  750. {
  751. SDL_Surface *face, *a, *b, *tmp;
  752. int ret;
  753. bool result;
  754. face = SDLTest_ImageFace();
  755. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  756. if (face == NULL) {
  757. return TEST_ABORTED;
  758. }
  759. /* RLE encoding only works for 32-bit surfaces with alpha in the high bits */
  760. if (face->format != SDL_PIXELFORMAT_ARGB8888) {
  761. tmp = SDL_ConvertSurface(face, SDL_PIXELFORMAT_ARGB8888);
  762. SDLTest_AssertCheck(tmp != NULL, "Verify tmp surface is not NULL");
  763. if (tmp == NULL) {
  764. return TEST_ABORTED;
  765. }
  766. SDL_DestroySurface(face);
  767. face = tmp;
  768. }
  769. /* Create a temporary surface to trigger RLE encoding during blit */
  770. tmp = SDL_DuplicateSurface(face);
  771. SDLTest_AssertCheck(tmp != NULL, "Verify result from SDL_DuplicateSurface() with RLE pixels is not NULL");
  772. result = SDL_SetSurfaceRLE(face, true);
  773. SDLTest_AssertCheck(result, "Verify result from SDL_SetSurfaceRLE() is true");
  774. /* Test duplication with RLE pixels */
  775. a = SDL_DuplicateSurface(face);
  776. SDLTest_AssertCheck(a != NULL, "Verify result from SDL_DuplicateSurface() with RLE pixels is not NULL");
  777. SDLTest_AssertCheck(SDL_SurfaceHasRLE(a), "Verify result from SDL_DuplicateSurface() with RLE pixels has RLE set");
  778. ret = SDLTest_CompareSurfaces(a, face, 0);
  779. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  780. /* Verify that blitting from an RLE surface does RLE encode it */
  781. SDLTest_AssertCheck(!SDL_MUSTLOCK(a), "Verify initial RLE surface does not need to be locked");
  782. SDLTest_AssertCheck(a->pixels != NULL, "Verify initial RLE surface has pixels available");
  783. result = SDL_BlitSurface(a, NULL, tmp, NULL);
  784. SDLTest_AssertCheck(result, "Verify result from SDL_BlitSurface() with RLE surface is true");
  785. SDLTest_AssertCheck(SDL_MUSTLOCK(a), "Verify RLE surface after blit needs to be locked");
  786. SDLTest_AssertCheck(a->pixels == NULL, "Verify RLE surface after blit does not have pixels available");
  787. ret = SDLTest_CompareSurfaces(tmp, face, 0);
  788. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  789. /* Test scaling with RLE pixels */
  790. b = SDL_ScaleSurface(a, a->w * 2, a->h * 2, SDL_SCALEMODE_NEAREST);
  791. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ScaleSurface() is not NULL");
  792. SDLTest_AssertCheck(SDL_SurfaceHasRLE(b), "Verify result from SDL_ScaleSurface() with RLE pixels has RLE set");
  793. /* Test scaling blitting with RLE pixels */
  794. result = SDL_BlitSurfaceScaled(a, NULL, b, NULL, SDL_SCALEMODE_NEAREST);
  795. SDLTest_AssertCheck(result, "Verify result from SDL_BlitSurfaceScaled() with src having RLE pixels is true");
  796. SDL_BlitSurface(a, NULL, tmp, NULL);
  797. SDL_DestroySurface(b);
  798. b = NULL;
  799. /* Test conversion with RLE pixels */
  800. b = SDL_ConvertSurfaceAndColorspace(a, SDL_PIXELFORMAT_ABGR8888, NULL, SDL_COLORSPACE_UNKNOWN, 0);
  801. SDLTest_AssertCheck(b != NULL, "Verify result from SDL_ConvertSurfaceAndColorspace() with RLE pixels is not NULL");
  802. SDLTest_AssertCheck(SDL_SurfaceHasRLE(b), "Verify result from SDL_ConvertSurfaceAndColorspace() with RLE pixels has RLE set");
  803. ret = SDLTest_CompareSurfacesIgnoreTransparentPixels(b, face, 0);
  804. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  805. SDL_BlitSurface(a, NULL, tmp, NULL);
  806. SDL_DestroySurface(b);
  807. b = NULL;
  808. #if 0 /* This will currently fail, you must lock the surface first */
  809. /* Test filling surface with RLE pixels */
  810. result = SDL_FillSurfaceRect(a, NULL, 0);
  811. SDLTest_AssertCheck(result, "Verify result from SDL_FillSurfaceRect() with dst having RLE pixels is true");
  812. #endif
  813. /* Make sure the RLE surface still needs to be locked after surface operations */
  814. SDLTest_AssertCheck(a->pixels == NULL, "Verify RLE surface after operations does not have pixels available");
  815. /* Clean up. */
  816. SDL_DestroySurface(face);
  817. SDL_DestroySurface(a);
  818. SDL_DestroySurface(b);
  819. SDL_DestroySurface(tmp);
  820. return TEST_COMPLETED;
  821. }
  822. /**
  823. * Tests surface conversion.
  824. */
  825. static int SDLCALL surface_testSurfaceConversion(void *arg)
  826. {
  827. SDL_Surface *rface = NULL, *face = NULL;
  828. int ret = 0;
  829. /* Create sample surface */
  830. face = SDLTest_ImageFace();
  831. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  832. if (face == NULL) {
  833. return TEST_ABORTED;
  834. }
  835. /* Set transparent pixel as the pixel at (0,0) */
  836. if (SDL_GetSurfacePalette(face)) {
  837. ret = SDL_SetSurfaceColorKey(face, true, *(Uint8 *)face->pixels);
  838. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  839. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey, expected: true, got: %i", ret);
  840. }
  841. /* Convert to 32 bit to compare. */
  842. rface = SDL_ConvertSurface(face, testSurface->format);
  843. SDLTest_AssertPass("Call to SDL_ConvertSurface()");
  844. SDLTest_AssertCheck(rface != NULL, "Verify result from SDL_ConvertSurface is not NULL");
  845. /* Compare surface. */
  846. ret = SDLTest_CompareSurfaces(rface, face, 0);
  847. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  848. /* Clean up. */
  849. SDL_DestroySurface(face);
  850. SDL_DestroySurface(rface);
  851. return TEST_COMPLETED;
  852. }
  853. /**
  854. * Tests surface conversion across all pixel formats.
  855. */
  856. static int SDLCALL surface_testCompleteSurfaceConversion(void *arg)
  857. {
  858. Uint32 pixel_formats[] = {
  859. SDL_PIXELFORMAT_INDEX8,
  860. SDL_PIXELFORMAT_RGB332,
  861. SDL_PIXELFORMAT_XRGB4444,
  862. SDL_PIXELFORMAT_XBGR4444,
  863. SDL_PIXELFORMAT_XRGB1555,
  864. SDL_PIXELFORMAT_XBGR1555,
  865. SDL_PIXELFORMAT_ARGB4444,
  866. SDL_PIXELFORMAT_RGBA4444,
  867. SDL_PIXELFORMAT_ABGR4444,
  868. SDL_PIXELFORMAT_BGRA4444,
  869. SDL_PIXELFORMAT_ARGB1555,
  870. SDL_PIXELFORMAT_RGBA5551,
  871. SDL_PIXELFORMAT_ABGR1555,
  872. SDL_PIXELFORMAT_BGRA5551,
  873. SDL_PIXELFORMAT_RGB565,
  874. SDL_PIXELFORMAT_BGR565,
  875. SDL_PIXELFORMAT_RGB24,
  876. SDL_PIXELFORMAT_BGR24,
  877. SDL_PIXELFORMAT_XRGB8888,
  878. SDL_PIXELFORMAT_RGBX8888,
  879. SDL_PIXELFORMAT_XBGR8888,
  880. SDL_PIXELFORMAT_BGRX8888,
  881. SDL_PIXELFORMAT_ARGB8888,
  882. SDL_PIXELFORMAT_RGBA8888,
  883. SDL_PIXELFORMAT_ABGR8888,
  884. SDL_PIXELFORMAT_BGRA8888,
  885. #if 0 /* We aren't testing HDR10 colorspace conversion */
  886. SDL_PIXELFORMAT_XRGB2101010,
  887. SDL_PIXELFORMAT_XBGR2101010,
  888. SDL_PIXELFORMAT_ARGB2101010,
  889. SDL_PIXELFORMAT_ABGR2101010,
  890. #endif
  891. };
  892. SDL_Surface *face = NULL, *cvt1, *cvt2, *final;
  893. const SDL_PixelFormatDetails *fmt1, *fmt2;
  894. int i, j, ret = 0;
  895. /* Create sample surface */
  896. face = SDLTest_ImageFace();
  897. SDLTest_AssertCheck(face != NULL, "Verify face surface is not NULL");
  898. if (face == NULL) {
  899. return TEST_ABORTED;
  900. }
  901. /* Set transparent pixel as the pixel at (0,0) */
  902. if (SDL_GetSurfacePalette(face)) {
  903. ret = SDL_SetSurfaceColorKey(face, true, *(Uint8 *)face->pixels);
  904. SDLTest_AssertPass("Call to SDL_SetSurfaceColorKey()");
  905. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetSurfaceColorKey, expected: true, got: %i", ret);
  906. }
  907. for (i = 0; i < SDL_arraysize(pixel_formats); ++i) {
  908. for (j = 0; j < SDL_arraysize(pixel_formats); ++j) {
  909. fmt1 = SDL_GetPixelFormatDetails(pixel_formats[i]);
  910. SDLTest_AssertCheck(fmt1 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  911. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  912. cvt1 = SDL_ConvertSurface(face, fmt1->format);
  913. SDLTest_AssertCheck(cvt1 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  914. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  915. fmt2 = SDL_GetPixelFormatDetails(pixel_formats[j]);
  916. SDLTest_AssertCheck(fmt2 != NULL, "SDL_GetPixelFormatDetails(%s[0x%08" SDL_PRIx32 "]) should return a non-null pixel format",
  917. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  918. cvt2 = SDL_ConvertSurface(cvt1, fmt2->format);
  919. SDLTest_AssertCheck(cvt2 != NULL, "SDL_ConvertSurface(..., %s[0x%08" SDL_PRIx32 "]) should return a non-null surface",
  920. SDL_GetPixelFormatName(pixel_formats[i]), pixel_formats[i]);
  921. if (fmt1 && fmt2 &&
  922. fmt1->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  923. fmt2->bytes_per_pixel == SDL_BYTESPERPIXEL(face->format) &&
  924. SDL_ISPIXELFORMAT_ALPHA(fmt1->format) == SDL_ISPIXELFORMAT_ALPHA(face->format) &&
  925. SDL_ISPIXELFORMAT_ALPHA(fmt2->format) == SDL_ISPIXELFORMAT_ALPHA(face->format)) {
  926. final = SDL_ConvertSurface(cvt2, face->format);
  927. SDL_assert(final != NULL);
  928. /* Compare surface. */
  929. ret = SDLTest_CompareSurfaces(face, final, 0);
  930. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  931. SDL_DestroySurface(final);
  932. }
  933. SDL_DestroySurface(cvt1);
  934. SDL_DestroySurface(cvt2);
  935. }
  936. }
  937. /* Clean up. */
  938. SDL_DestroySurface(face);
  939. return TEST_COMPLETED;
  940. }
  941. /**
  942. * Tests sprite loading. A failure case.
  943. */
  944. static int SDLCALL surface_testLoadFailure(void *arg)
  945. {
  946. SDL_Surface *face = SDL_LoadBMP("nonexistant.bmp");
  947. SDLTest_AssertCheck(face == NULL, "SDL_CreateLoadBmp");
  948. return TEST_COMPLETED;
  949. }
  950. /**
  951. * Tests blitting from a zero sized source rectangle
  952. */
  953. static int SDLCALL surface_testBlitZeroSource(void *arg)
  954. {
  955. SDL_Surface *src = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  956. SDL_Surface *dst = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  957. SDL_Rect srcrect = { 0, 0, 0, 0 };
  958. int ret;
  959. SDLTest_AssertPass("Call to SDL_BlitSurfaceScaled() with zero sized source rectangle");
  960. SDL_FillSurfaceRect(src, NULL, SDL_MapSurfaceRGB(src, 255, 255, 255));
  961. SDL_BlitSurfaceScaled(src, &srcrect, dst, NULL, SDL_SCALEMODE_NEAREST);
  962. ret = SDLTest_CompareSurfaces(dst, src, 0);
  963. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  964. SDL_DestroySurface(src);
  965. SDL_DestroySurface(dst);
  966. return TEST_COMPLETED;
  967. }
  968. /**
  969. * Tests some blitting routines.
  970. */
  971. static int SDLCALL surface_testBlit(void *arg)
  972. {
  973. /* Basic blitting */
  974. testBlitBlendMode(SDL_BLENDMODE_NONE);
  975. return TEST_COMPLETED;
  976. }
  977. /**
  978. * Tests some blitting routines with color mod
  979. */
  980. static int SDLCALL surface_testBlitColorMod(void *arg)
  981. {
  982. /* Basic blitting with color mod */
  983. testBlitBlendMode(-1);
  984. return TEST_COMPLETED;
  985. }
  986. /**
  987. * Tests some blitting routines with alpha mod
  988. */
  989. static int SDLCALL surface_testBlitAlphaMod(void *arg)
  990. {
  991. /* Basic blitting with alpha mod */
  992. testBlitBlendMode(-2);
  993. return TEST_COMPLETED;
  994. }
  995. /**
  996. * Tests some more blitting routines.
  997. */
  998. static int SDLCALL surface_testBlitBlendBlend(void *arg)
  999. {
  1000. /* Blend blitting */
  1001. testBlitBlendMode(SDL_BLENDMODE_BLEND);
  1002. return TEST_COMPLETED;
  1003. }
  1004. /**
  1005. * @brief Tests some more blitting routines.
  1006. */
  1007. static int SDLCALL surface_testBlitBlendPremultiplied(void *arg)
  1008. {
  1009. /* Blend premultiplied blitting */
  1010. testBlitBlendMode(SDL_BLENDMODE_BLEND_PREMULTIPLIED);
  1011. return TEST_COMPLETED;
  1012. }
  1013. /**
  1014. * Tests some more blitting routines.
  1015. */
  1016. static int SDLCALL surface_testBlitBlendAdd(void *arg)
  1017. {
  1018. /* Add blitting */
  1019. testBlitBlendMode(SDL_BLENDMODE_ADD);
  1020. return TEST_COMPLETED;
  1021. }
  1022. /**
  1023. * Tests some more blitting routines.
  1024. */
  1025. static int SDLCALL surface_testBlitBlendAddPremultiplied(void *arg)
  1026. {
  1027. /* Add premultiplied blitting */
  1028. testBlitBlendMode(SDL_BLENDMODE_ADD_PREMULTIPLIED);
  1029. return TEST_COMPLETED;
  1030. }
  1031. /**
  1032. * Tests some more blitting routines.
  1033. */
  1034. static int SDLCALL surface_testBlitBlendMod(void *arg)
  1035. {
  1036. /* Mod blitting */
  1037. testBlitBlendMode(SDL_BLENDMODE_MOD);
  1038. return TEST_COMPLETED;
  1039. }
  1040. /**
  1041. * Tests some more blitting routines.
  1042. */
  1043. static int SDLCALL surface_testBlitBlendMul(void *arg)
  1044. {
  1045. /* Mod blitting */
  1046. testBlitBlendMode(SDL_BLENDMODE_MUL);
  1047. return TEST_COMPLETED;
  1048. }
  1049. /**
  1050. * Tests blitting bitmaps
  1051. */
  1052. static int SDLCALL surface_testBlitBitmap(void *arg)
  1053. {
  1054. const SDL_PixelFormat formats[] = {
  1055. SDL_PIXELFORMAT_INDEX1LSB,
  1056. SDL_PIXELFORMAT_INDEX1MSB,
  1057. SDL_PIXELFORMAT_INDEX2LSB,
  1058. SDL_PIXELFORMAT_INDEX2MSB,
  1059. SDL_PIXELFORMAT_INDEX4LSB,
  1060. SDL_PIXELFORMAT_INDEX4MSB
  1061. };
  1062. Uint8 pixel;
  1063. int i, j;
  1064. bool result;
  1065. SDL_Surface *dst = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_ARGB8888);
  1066. SDL_Color colors[] = {
  1067. { 0x00, 0x00, 0x00, 0xFF },
  1068. { 0xFF, 0xFF, 0xFF, 0xFF }
  1069. };
  1070. SDL_Palette *palette;
  1071. Uint32 value, expected = 0xFFFFFFFF;
  1072. palette = SDL_CreatePalette(SDL_arraysize(colors));
  1073. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette() != NULL, result = %p", palette);
  1074. result = SDL_SetPaletteColors(palette, colors, 0, SDL_arraysize(colors));
  1075. SDLTest_AssertCheck(result, "SDL_SetPaletteColors, result = %s", result ? "true" : "false");
  1076. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1077. SDL_PixelFormat format = formats[i];
  1078. int bpp = SDL_BITSPERPIXEL(format);
  1079. int width = (8 / bpp);
  1080. if (SDL_PIXELORDER(format) == SDL_BITMAPORDER_1234) {
  1081. switch (bpp) {
  1082. case 1:
  1083. pixel = 0x80;
  1084. break;
  1085. case 2:
  1086. pixel = 0x40;
  1087. break;
  1088. case 4:
  1089. pixel = 0x10;
  1090. break;
  1091. default:
  1092. SDL_assert(!"Unexpected bpp");
  1093. break;
  1094. }
  1095. } else {
  1096. pixel = 0x01;
  1097. }
  1098. for (j = 0; j < width; ++j) {
  1099. SDL_Rect rect = { j, 0, 1, 1 };
  1100. SDL_Surface *src = SDL_CreateSurfaceFrom(width, 1, format, &pixel, 1);
  1101. SDL_SetSurfacePalette(src, palette);
  1102. *(Uint32 *)dst->pixels = 0;
  1103. result = SDL_BlitSurface(src, &rect, dst, NULL);
  1104. SDLTest_AssertCheck(result, "SDL_BlitSurface(%s pixel %d), result = %s", SDL_GetPixelFormatName(format), j, result ? "true" : "false");
  1105. value = *(Uint32 *)dst->pixels;
  1106. SDLTest_AssertCheck(value == expected, "Expected value == 0x%" SDL_PRIx32 ", actually = 0x%" SDL_PRIx32, expected, value);
  1107. SDL_DestroySurface(src);
  1108. if (SDL_PIXELORDER(format) == SDL_BITMAPORDER_1234) {
  1109. pixel >>= bpp;
  1110. } else {
  1111. pixel <<= bpp;
  1112. }
  1113. }
  1114. }
  1115. SDL_DestroyPalette(palette);
  1116. SDL_DestroySurface(dst);
  1117. return TEST_COMPLETED;
  1118. }
  1119. /**
  1120. * Tests blitting invalid surfaces.
  1121. */
  1122. static int SDLCALL surface_testBlitInvalid(void *arg)
  1123. {
  1124. SDL_Surface *valid, *invalid;
  1125. bool result;
  1126. valid = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  1127. SDLTest_AssertCheck(valid != NULL, "Check surface creation");
  1128. invalid = SDL_CreateSurface(0, 0, SDL_PIXELFORMAT_RGBA8888);
  1129. SDLTest_AssertCheck(invalid != NULL, "Check surface creation");
  1130. SDLTest_AssertCheck(invalid->pixels == NULL, "Check surface pixels are NULL");
  1131. result = SDL_BlitSurface(invalid, NULL, valid, NULL);
  1132. SDLTest_AssertCheck(result == false, "SDL_BlitSurface(invalid, NULL, valid, NULL), result = %s", result ? "true" : "false");
  1133. result = SDL_BlitSurface(valid, NULL, invalid, NULL);
  1134. SDLTest_AssertCheck(result == false, "SDL_BlitSurface(valid, NULL, invalid, NULL), result = %s", result ? "true" : "false");
  1135. result = SDL_BlitSurfaceScaled(invalid, NULL, valid, NULL, SDL_SCALEMODE_NEAREST);
  1136. SDLTest_AssertCheck(result == false, "SDL_BlitSurfaceScaled(invalid, NULL, valid, NULL, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1137. result = SDL_BlitSurfaceScaled(valid, NULL, invalid, NULL, SDL_SCALEMODE_NEAREST);
  1138. SDLTest_AssertCheck(result == false, "SDL_BlitSurfaceScaled(valid, NULL, invalid, NULL, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1139. SDL_DestroySurface(valid);
  1140. SDL_DestroySurface(invalid);
  1141. return TEST_COMPLETED;
  1142. }
  1143. static int SDLCALL surface_testBlitsWithBadCoordinates(void *arg)
  1144. {
  1145. const SDL_Rect rect[8] = {
  1146. { SDL_MAX_SINT32, 0, 2, 2 },
  1147. { 0, SDL_MAX_SINT32, 2, 2 },
  1148. { 0, 0, SDL_MAX_SINT32, 2 },
  1149. { 0, 0, 2, SDL_MAX_SINT32 },
  1150. { SDL_MIN_SINT32, 0, 2, 2 },
  1151. { 0, SDL_MIN_SINT32, 2, 2 },
  1152. { 0, 0, SDL_MIN_SINT32, 2 },
  1153. { 0, 0, 2, SDL_MIN_SINT32 }
  1154. };
  1155. SDL_Surface *s;
  1156. bool result;
  1157. int i;
  1158. s = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA8888);
  1159. SDLTest_AssertCheck(s != NULL, "Check surface creation");
  1160. for (i = 0; i < 8; i++) {
  1161. result = SDL_BlitSurface(s, NULL, s, &rect[i]);
  1162. SDLTest_AssertCheck(result == true, "SDL_BlitSurface(valid, NULL, valid, &rect), result = %s", result ? "true" : "false");
  1163. result = SDL_BlitSurface(s, &rect[i], s, NULL);
  1164. SDLTest_AssertCheck(result == true, "SDL_BlitSurface(valid, &rect, valid, NULL), result = %s", result ? "true" : "false");
  1165. result = SDL_BlitSurfaceScaled(s, NULL, s, &rect[i], SDL_SCALEMODE_NEAREST);
  1166. SDLTest_AssertCheck(result == true, "SDL_BlitSurfaceScaled(valid, NULL, valid, &rect, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1167. result = SDL_BlitSurfaceScaled(s, &rect[i], s, NULL, SDL_SCALEMODE_NEAREST);
  1168. SDLTest_AssertCheck(result == true, "SDL_BlitSurfaceScaled(valid, &rect, valid, NULL, SDL_SCALEMODE_NEAREST), result = %s", result ? "true" : "false");
  1169. }
  1170. SDL_DestroySurface(s);
  1171. return TEST_COMPLETED;
  1172. }
  1173. static int SDLCALL surface_testOverflow(void *arg)
  1174. {
  1175. char buf[1024];
  1176. const char *expectedError;
  1177. SDL_Surface *surface;
  1178. SDL_memset(buf, '\0', sizeof(buf));
  1179. expectedError = "Parameter 'width' is invalid";
  1180. surface = SDL_CreateSurface(-3, 100, SDL_PIXELFORMAT_INDEX8);
  1181. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  1182. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1183. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1184. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  1185. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  1186. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1187. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1188. surface = SDL_CreateSurfaceFrom(-1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  1189. SDLTest_AssertCheck(surface == NULL, "Should detect negative width");
  1190. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1191. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1192. expectedError = "Parameter 'height' is invalid";
  1193. surface = SDL_CreateSurface(100, -3, SDL_PIXELFORMAT_INDEX8);
  1194. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  1195. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1196. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1197. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_INDEX8, buf, 4);
  1198. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  1199. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1200. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1201. surface = SDL_CreateSurfaceFrom(1, -1, SDL_PIXELFORMAT_RGBA8888, buf, 4);
  1202. SDLTest_AssertCheck(surface == NULL, "Should detect negative height");
  1203. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1204. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1205. expectedError = "Parameter 'pitch' is invalid";
  1206. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_INDEX8, buf, -1);
  1207. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  1208. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1209. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1210. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, -1);
  1211. SDLTest_AssertCheck(surface == NULL, "Should detect negative pitch");
  1212. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1213. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1214. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, buf, 0);
  1215. SDLTest_AssertCheck(surface == NULL, "Should detect zero pitch");
  1216. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1217. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1218. surface = SDL_CreateSurfaceFrom(1, 1, SDL_PIXELFORMAT_RGBA8888, NULL, 0);
  1219. SDLTest_AssertCheck(surface != NULL, "Allow zero pitch for partially set up surfaces: %s",
  1220. surface != NULL ? "(success)" : SDL_GetError());
  1221. SDL_DestroySurface(surface);
  1222. /* Less than 1 byte per pixel: the pitch can legitimately be less than
  1223. * the width, but it must be enough to hold the appropriate number of
  1224. * bits per pixel. SDL_PIXELFORMAT_INDEX4* needs 1 byte per 2 pixels. */
  1225. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  1226. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  1227. surface != NULL ? "(success)" : SDL_GetError());
  1228. SDL_DestroySurface(surface);
  1229. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  1230. SDLTest_AssertCheck(surface != NULL, "6px * 4 bits per px fits in 3 bytes: %s",
  1231. surface != NULL ? "(success)" : SDL_GetError());
  1232. SDL_DestroySurface(surface);
  1233. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 3);
  1234. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1235. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1236. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1237. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 3);
  1238. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1239. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1240. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1241. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4LSB, buf, 4);
  1242. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  1243. surface != NULL ? "(success)" : SDL_GetError());
  1244. SDL_DestroySurface(surface);
  1245. surface = SDL_CreateSurfaceFrom(7, 1, SDL_PIXELFORMAT_INDEX4MSB, buf, 4);
  1246. SDLTest_AssertCheck(surface != NULL, "7px * 4 bits per px fits in 4 bytes: %s",
  1247. surface != NULL ? "(success)" : SDL_GetError());
  1248. SDL_DestroySurface(surface);
  1249. /* SDL_PIXELFORMAT_INDEX2* needs 1 byte per 4 pixels. */
  1250. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  1251. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  1252. surface != NULL ? "(success)" : SDL_GetError());
  1253. SDL_DestroySurface(surface);
  1254. surface = SDL_CreateSurfaceFrom(12, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  1255. SDLTest_AssertCheck(surface != NULL, "12px * 2 bits per px fits in 3 bytes: %s",
  1256. surface != NULL ? "(success)" : SDL_GetError());
  1257. SDL_DestroySurface(surface);
  1258. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 3);
  1259. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp (%d)", surface ? surface->pitch : 0);
  1260. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1261. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1262. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 3);
  1263. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1264. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1265. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1266. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2LSB, buf, 4);
  1267. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  1268. surface != NULL ? "(success)" : SDL_GetError());
  1269. SDL_DestroySurface(surface);
  1270. surface = SDL_CreateSurfaceFrom(13, 1, SDL_PIXELFORMAT_INDEX2MSB, buf, 4);
  1271. SDLTest_AssertCheck(surface != NULL, "13px * 2 bits per px fits in 4 bytes: %s",
  1272. surface != NULL ? "(success)" : SDL_GetError());
  1273. SDL_DestroySurface(surface);
  1274. /* SDL_PIXELFORMAT_INDEX1* needs 1 byte per 8 pixels. */
  1275. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  1276. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  1277. surface != NULL ? "(success)" : SDL_GetError());
  1278. SDL_DestroySurface(surface);
  1279. surface = SDL_CreateSurfaceFrom(16, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  1280. SDLTest_AssertCheck(surface != NULL, "16px * 1 bit per px fits in 2 bytes: %s",
  1281. surface != NULL ? "(success)" : SDL_GetError());
  1282. SDL_DestroySurface(surface);
  1283. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 2);
  1284. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1285. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1286. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1287. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 2);
  1288. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1289. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1290. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1291. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1LSB, buf, 3);
  1292. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  1293. surface != NULL ? "(success)" : SDL_GetError());
  1294. SDL_DestroySurface(surface);
  1295. surface = SDL_CreateSurfaceFrom(17, 1, SDL_PIXELFORMAT_INDEX1MSB, buf, 3);
  1296. SDLTest_AssertCheck(surface != NULL, "17px * 1 bit per px fits in 3 bytes: %s",
  1297. surface != NULL ? "(success)" : SDL_GetError());
  1298. SDL_DestroySurface(surface);
  1299. /* SDL_PIXELFORMAT_INDEX8 and SDL_PIXELFORMAT_RGB332 require 1 byte per pixel. */
  1300. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  1301. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  1302. surface != NULL ? "(success)" : SDL_GetError());
  1303. SDL_DestroySurface(surface);
  1304. surface = SDL_CreateSurfaceFrom(5, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  1305. SDLTest_AssertCheck(surface != NULL, "5px * 8 bits per px fits in 5 bytes: %s",
  1306. surface != NULL ? "(success)" : SDL_GetError());
  1307. SDL_DestroySurface(surface);
  1308. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_RGB332, buf, 5);
  1309. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1310. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1311. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1312. surface = SDL_CreateSurfaceFrom(6, 1, SDL_PIXELFORMAT_INDEX8, buf, 5);
  1313. SDLTest_AssertCheck(surface == NULL, "Should detect pitch < width * bpp");
  1314. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1315. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1316. /* Everything else requires more than 1 byte per pixel, and rounds up
  1317. * each pixel to an integer number of bytes (e.g. RGB555 is really
  1318. * XRGB1555, with 1 bit per pixel wasted). */
  1319. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1320. SDLTest_AssertCheck(surface != NULL, "3px * 15 (really 16) bits per px fits in 6 bytes: %s",
  1321. surface != NULL ? "(success)" : SDL_GetError());
  1322. SDL_DestroySurface(surface);
  1323. surface = SDL_CreateSurfaceFrom(3, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1324. SDLTest_AssertCheck(surface != NULL, "5px * 15 (really 16) bits per px fits in 6 bytes: %s",
  1325. surface != NULL ? "(success)" : SDL_GetError());
  1326. SDL_DestroySurface(surface);
  1327. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1328. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  1329. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1330. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1331. surface = SDL_CreateSurfaceFrom(4, 1, SDL_PIXELFORMAT_XRGB1555, buf, 6);
  1332. SDLTest_AssertCheck(surface == NULL, "4px * 15 (really 16) bits per px doesn't fit in 6 bytes");
  1333. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1334. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1335. if (sizeof(size_t) == 4 && sizeof(int) >= 4) {
  1336. SDL_ClearError();
  1337. expectedError = "aligning pitch would overflow";
  1338. /* 0x5555'5555 * 3bpp = 0xffff'ffff which fits in size_t, but adding
  1339. * alignment padding makes it overflow */
  1340. surface = SDL_CreateSurface(0x55555555, 1, SDL_PIXELFORMAT_RGB24);
  1341. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in pitch + alignment");
  1342. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1343. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1344. SDL_ClearError();
  1345. expectedError = "width * bpp would overflow";
  1346. /* 0x4000'0000 * 4bpp = 0x1'0000'0000 which (just) overflows */
  1347. surface = SDL_CreateSurface(0x40000000, 1, SDL_PIXELFORMAT_ARGB8888);
  1348. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * bytes per pixel");
  1349. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1350. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1351. SDL_ClearError();
  1352. expectedError = "height * pitch would overflow";
  1353. surface = SDL_CreateSurface((1 << 29) - 1, (1 << 29) - 1, SDL_PIXELFORMAT_INDEX8);
  1354. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height");
  1355. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1356. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1357. SDL_ClearError();
  1358. expectedError = "height * pitch would overflow";
  1359. surface = SDL_CreateSurface((1 << 15) + 1, (1 << 15) + 1, SDL_PIXELFORMAT_ARGB8888);
  1360. SDLTest_AssertCheck(surface == NULL, "Should detect overflow in width * height * bytes per pixel");
  1361. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1362. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1363. } else {
  1364. SDLTest_Log("Can't easily overflow size_t on this platform");
  1365. }
  1366. return TEST_COMPLETED;
  1367. }
  1368. static int SDLCALL surface_testFlip(void *arg)
  1369. {
  1370. SDL_Surface *surface;
  1371. Uint8 *pixels;
  1372. int offset;
  1373. const char *expectedError;
  1374. surface = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGB24);
  1375. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1376. SDL_ClearError();
  1377. expectedError = "Parameter 'surface' is invalid";
  1378. SDL_FlipSurface(NULL, SDL_FLIP_HORIZONTAL);
  1379. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1380. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1381. SDL_ClearError();
  1382. expectedError = "Parameter 'flip' is invalid";
  1383. SDL_FlipSurface(surface, SDL_FLIP_NONE);
  1384. SDLTest_AssertCheck(SDL_strcmp(SDL_GetError(), expectedError) == 0,
  1385. "Expected \"%s\", got \"%s\"", expectedError, SDL_GetError());
  1386. pixels = (Uint8 *)surface->pixels;
  1387. *pixels = 0xFF;
  1388. offset = 0;
  1389. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_VERTICAL)");
  1390. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_VERTICAL));
  1391. SDLTest_AssertCheck(pixels[offset] == 0x00,
  1392. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  1393. offset = 2 * surface->pitch;
  1394. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  1395. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  1396. SDLTest_AssertPass("Call to SDL_FlipSurface(surface, SDL_FLIP_HORIZONTAL)");
  1397. CHECK_FUNC(SDL_FlipSurface, (surface, SDL_FLIP_HORIZONTAL));
  1398. SDLTest_AssertCheck(pixels[offset] == 0x00,
  1399. "Expected pixels[%d] == 0x00 got 0x%.2X", offset, pixels[offset]);
  1400. offset += (surface->w - 1) * SDL_BYTESPERPIXEL(surface->format);
  1401. SDLTest_AssertCheck(pixels[offset] == 0xFF,
  1402. "Expected pixels[%d] == 0xFF got 0x%.2X", offset, pixels[offset]);
  1403. SDL_DestroySurface(surface);
  1404. return TEST_COMPLETED;
  1405. }
  1406. static int SDLCALL surface_testPalette(void *arg)
  1407. {
  1408. SDL_Surface *source, *surface, *output;
  1409. SDL_Palette *palette;
  1410. Uint8 *pixels;
  1411. palette = SDL_CreatePalette(2);
  1412. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  1413. source = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  1414. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  1415. SDLTest_AssertCheck(SDL_GetSurfacePalette(source) == NULL, "SDL_GetSurfacePalette(source)");
  1416. surface = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_INDEX8);
  1417. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1418. SDLTest_AssertCheck(SDL_GetSurfacePalette(surface) == NULL, "SDL_GetSurfacePalette(surface)");
  1419. pixels = (Uint8 *)surface->pixels;
  1420. SDLTest_AssertCheck(*pixels == 0, "Expected *pixels == 0 got %u", *pixels);
  1421. /* Identity copy between indexed surfaces without a palette */
  1422. *(Uint8 *)source->pixels = 1;
  1423. SDL_BlitSurface(source, NULL, surface, NULL);
  1424. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1425. /* Identity copy between indexed surfaces where the source has a palette */
  1426. palette->colors[0].r = 0;
  1427. palette->colors[0].g = 0;
  1428. palette->colors[0].b = 0;
  1429. palette->colors[1].r = 0xFF;
  1430. palette->colors[1].g = 0;
  1431. palette->colors[1].b = 0;
  1432. SDL_SetSurfacePalette(source, palette);
  1433. *pixels = 0;
  1434. SDL_BlitSurface(source, NULL, surface, NULL);
  1435. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1436. /* Identity copy between indexed surfaces where the destination has a palette */
  1437. palette->colors[0].r = 0;
  1438. palette->colors[0].g = 0;
  1439. palette->colors[0].b = 0;
  1440. palette->colors[1].r = 0xFF;
  1441. palette->colors[1].g = 0;
  1442. palette->colors[1].b = 0;
  1443. SDL_SetSurfacePalette(source, NULL);
  1444. SDL_SetSurfacePalette(surface, palette);
  1445. *pixels = 0;
  1446. SDL_BlitSurface(source, NULL, surface, NULL);
  1447. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1448. /* Identity copy between indexed surfaces where the source and destination share a palette */
  1449. palette->colors[0].r = 0;
  1450. palette->colors[0].g = 0;
  1451. palette->colors[0].b = 0;
  1452. palette->colors[1].r = 0xFF;
  1453. palette->colors[1].g = 0;
  1454. palette->colors[1].b = 0;
  1455. SDL_SetSurfacePalette(source, palette);
  1456. SDL_SetSurfacePalette(surface, palette);
  1457. *pixels = 0;
  1458. SDL_BlitSurface(source, NULL, surface, NULL);
  1459. SDLTest_AssertCheck(*pixels == 1, "Expected *pixels == 1 got %u", *pixels);
  1460. output = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA32);
  1461. SDLTest_AssertCheck(output != NULL, "SDL_CreateSurface()");
  1462. pixels = (Uint8 *)output->pixels;
  1463. SDL_BlitSurface(surface, NULL, output, NULL);
  1464. SDLTest_AssertCheck(*pixels == 0xFF, "Expected *pixels == 0xFF got 0x%.2X", *pixels);
  1465. /* Set the palette color and blit again */
  1466. palette->colors[1].r = 0xAA;
  1467. SDL_SetSurfacePalette(surface, palette);
  1468. SDL_BlitSurface(surface, NULL, output, NULL);
  1469. SDLTest_AssertCheck(*pixels == 0xAA, "Expected *pixels == 0xAA got 0x%.2X", *pixels);
  1470. SDL_DestroyPalette(palette);
  1471. SDL_DestroySurface(source);
  1472. SDL_DestroySurface(surface);
  1473. SDL_DestroySurface(output);
  1474. return TEST_COMPLETED;
  1475. }
  1476. static int SDLCALL surface_testPalettization(void *arg)
  1477. {
  1478. const SDL_Color palette_colors[] = {
  1479. { 0x80, 0x00, 0x00, 0xff },
  1480. { 0x00, 0x80, 0x00, 0xff },
  1481. { 0x00, 0x00, 0x80, 0xff },
  1482. { 0x40, 0x00, 0x00, 0xff },
  1483. { 0x00, 0x40, 0x00, 0xff },
  1484. { 0x00, 0x00, 0x40, 0xff },
  1485. { 0x00, 0x00, 0x00, 0xff },
  1486. { 0xff, 0x00, 0x00, 0xff },
  1487. { 0x00, 0xff, 0x00, 0xff },
  1488. { 0x00, 0x00, 0xff, 0xff },
  1489. { 0xff, 0xff, 0x00, 0xff },
  1490. { 0x00, 0xff, 0xff, 0xff },
  1491. { 0xff, 0x00, 0xff, 0xff },
  1492. };
  1493. const struct {
  1494. SDL_Color c;
  1495. Uint8 e;
  1496. } colors[] = {
  1497. { { 0xff, 0x00, 0x00, 0xff }, 7 },
  1498. { { 0xfe, 0x00, 0x00, 0xff }, 7 },
  1499. { { 0xfd, 0x00, 0x00, 0xff }, 7 },
  1500. { { 0xf0, 0x00, 0x00, 0xff }, 7 },
  1501. { { 0xd0, 0x00, 0x00, 0xff }, 7 },
  1502. { { 0xb0, 0x00, 0x00, 0xff }, 0 },
  1503. { { 0xa0, 0x00, 0x00, 0xff }, 0 },
  1504. { { 0xff, 0x00, 0x00, 0x00 }, 7 },
  1505. { { 0x00, 0x10, 0x21, 0xff }, 5 },
  1506. { { 0x00, 0x10, 0x19, 0xff }, 6 },
  1507. { { 0x81, 0x00, 0x41, 0xff }, 0 },
  1508. { { 0x80, 0xf0, 0xf0, 0x7f }, 11 },
  1509. { { 0x00, 0x00, 0x00, 0xff }, 6 },
  1510. { { 0x00, 0x00, 0x00, 0x01 }, 6 },
  1511. };
  1512. int i;
  1513. int result;
  1514. SDL_Surface *source, *output;
  1515. SDL_Palette *palette;
  1516. Uint8 *pixels;
  1517. palette = SDL_CreatePalette(SDL_arraysize(palette_colors));
  1518. SDLTest_AssertCheck(palette != NULL, "SDL_CreatePalette()");
  1519. result = SDL_SetPaletteColors(palette, palette_colors, 0, SDL_arraysize(palette_colors));
  1520. SDLTest_AssertCheck(result, "SDL_SetPaletteColors()");
  1521. source = SDL_CreateSurface(SDL_arraysize(palette_colors) + SDL_arraysize(colors), 1, SDL_PIXELFORMAT_RGBA8888);
  1522. SDLTest_AssertCheck(source != NULL, "SDL_CreateSurface()");
  1523. SDLTest_AssertCheck(source->w == SDL_arraysize(palette_colors) + SDL_arraysize(colors), "Expected source->w == %d, got %d", (int)(SDL_arraysize(palette_colors) + SDL_arraysize(colors)), source->w);
  1524. SDLTest_AssertCheck(source->h == 1, "Expected source->h == %d, got %d", 1, source->h);
  1525. SDLTest_AssertCheck(source->format == SDL_PIXELFORMAT_RGBA8888, "Expected source->format == SDL_PIXELFORMAT_RGBA8888, got 0x%x (%s)", source->format, SDL_GetPixelFormatName(source->format));
  1526. for (i = 0; i < SDL_arraysize(colors); i++) {
  1527. result = SDL_WriteSurfacePixel(source, i, 0, colors[i].c.r, colors[i].c.g, colors[i].c.b, colors[i].c.a);
  1528. SDLTest_AssertCheck(result == true, "SDL_WriteSurfacePixel");
  1529. }
  1530. for (i = 0; i < SDL_arraysize(palette_colors); i++) {
  1531. result = SDL_WriteSurfacePixel(source, SDL_arraysize(colors) + i, 0, palette_colors[i].r, palette_colors[i].g, palette_colors[i].b, palette_colors[i].a);
  1532. SDLTest_AssertCheck(result == true, "SDL_WriteSurfacePixel");
  1533. }
  1534. output = SDL_ConvertSurfaceAndColorspace(source, SDL_PIXELFORMAT_INDEX8, palette, SDL_COLORSPACE_UNKNOWN, 0);
  1535. SDLTest_AssertCheck(output != NULL, "SDL_ConvertSurfaceAndColorspace()");
  1536. SDLTest_AssertCheck(output->w == source->w, "Expected output->w == %d, got %d", source->w, output->w);
  1537. SDLTest_AssertCheck(output->h == source->h, "Expected output->h == %d, got %d", source->h, output->h);
  1538. SDLTest_AssertCheck(output->format == SDL_PIXELFORMAT_INDEX8, "Expected output->format == SDL_PIXELFORMAT_INDEX8, got 0x%x (%s)", output->format, SDL_GetPixelFormatName(output->format));
  1539. pixels = output->pixels;
  1540. for (i = 0; i < SDL_arraysize(colors); i++) {
  1541. int idx = i;
  1542. Uint8 actual = pixels[idx];
  1543. Uint8 expected = colors[i].e;
  1544. SDLTest_AssertCheck(actual < SDL_arraysize(palette_colors), "output->pixels[%d] < %d", idx, (int)SDL_arraysize(palette_colors));
  1545. SDLTest_AssertCheck(actual == expected, "Expected output->pixels[%d] == %u, got %u", idx, expected, actual);
  1546. }
  1547. SDLTest_AssertPass("Check palette 1:1 mapping");
  1548. for (i = 0; i < SDL_arraysize(palette_colors); i++) {
  1549. int idx = SDL_arraysize(colors) + i;
  1550. Uint8 actual = pixels[idx];
  1551. Uint8 expected = i;
  1552. SDLTest_AssertCheck(actual < SDL_arraysize(palette_colors), "output->pixels[%d] < %d", idx, (int)SDL_arraysize(palette_colors));
  1553. SDLTest_AssertCheck(actual == expected, "Expected output->pixels[%d] == %u, got %u", idx, expected, actual);
  1554. }
  1555. SDL_DestroyPalette(palette);
  1556. SDL_DestroySurface(source);
  1557. SDL_DestroySurface(output);
  1558. return TEST_COMPLETED;
  1559. }
  1560. static int SDLCALL surface_testClearSurface(void *arg)
  1561. {
  1562. SDL_PixelFormat formats[] = {
  1563. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1564. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1565. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1566. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1567. SDL_PIXELFORMAT_YV12, SDL_PIXELFORMAT_UYVY, SDL_PIXELFORMAT_NV12
  1568. };
  1569. SDL_Surface *surface;
  1570. SDL_PixelFormat format;
  1571. const float MAXIMUM_ERROR_RGB = 0.0001f;
  1572. const float MAXIMUM_ERROR_YUV = 0.01f;
  1573. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 1.0f;
  1574. float actualR, actualG, actualB, actualA;
  1575. float deltaR, deltaG, deltaB, deltaA;
  1576. int i, ret;
  1577. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1578. const float MAXIMUM_ERROR = SDL_ISPIXELFORMAT_FOURCC(formats[i]) ? MAXIMUM_ERROR_YUV : MAXIMUM_ERROR_RGB;
  1579. format = formats[i];
  1580. surface = SDL_CreateSurface(1, 1, format);
  1581. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1582. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1583. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1584. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, &actualA);
  1585. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1586. deltaR = SDL_fabsf(actualR - srcR);
  1587. deltaG = SDL_fabsf(actualG - srcG);
  1588. deltaB = SDL_fabsf(actualB - srcB);
  1589. deltaA = SDL_fabsf(actualA - srcA);
  1590. SDLTest_AssertCheck(
  1591. deltaR <= MAXIMUM_ERROR &&
  1592. deltaG <= MAXIMUM_ERROR &&
  1593. deltaB <= MAXIMUM_ERROR &&
  1594. deltaA <= MAXIMUM_ERROR,
  1595. "Checking %s surface clear results, expected %.4f,%.4f,%.4f,%.4f, got %.4f,%.4f,%.4f,%.4f",
  1596. SDL_GetPixelFormatName(format),
  1597. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  1598. SDL_DestroySurface(surface);
  1599. }
  1600. return TEST_COMPLETED;
  1601. }
  1602. static int SDLCALL surface_testPremultiplyAlpha(void *arg)
  1603. {
  1604. SDL_PixelFormat formats[] = {
  1605. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1606. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1607. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1608. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1609. };
  1610. SDL_Surface *surface;
  1611. SDL_PixelFormat format;
  1612. const float MAXIMUM_ERROR_LOW_PRECISION = 1 / 255.0f;
  1613. const float MAXIMUM_ERROR_HIGH_PRECISION = 0.0001f;
  1614. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1615. float expectedR = srcR * srcA;
  1616. float expectedG = srcG * srcA;
  1617. float expectedB = srcB * srcA;
  1618. float actualR, actualG, actualB;
  1619. float deltaR, deltaG, deltaB;
  1620. int i, ret;
  1621. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1622. const float MAXIMUM_ERROR = (SDL_BITSPERPIXEL(formats[i]) > 32) ? MAXIMUM_ERROR_HIGH_PRECISION : MAXIMUM_ERROR_LOW_PRECISION;
  1623. format = formats[i];
  1624. surface = SDL_CreateSurface(1, 1, format);
  1625. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1626. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1627. SDLTest_AssertCheck(ret == true, "SDL_SetSurfaceColorspace()");
  1628. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1629. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1630. ret = SDL_PremultiplySurfaceAlpha(surface, false);
  1631. SDLTest_AssertCheck(ret == true, "SDL_PremultiplySurfaceAlpha()");
  1632. ret = SDL_ReadSurfacePixelFloat(surface, 0, 0, &actualR, &actualG, &actualB, NULL);
  1633. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1634. deltaR = SDL_fabsf(actualR - expectedR);
  1635. deltaG = SDL_fabsf(actualG - expectedG);
  1636. deltaB = SDL_fabsf(actualB - expectedB);
  1637. SDLTest_AssertCheck(
  1638. deltaR <= MAXIMUM_ERROR &&
  1639. deltaG <= MAXIMUM_ERROR &&
  1640. deltaB <= MAXIMUM_ERROR,
  1641. "Checking %s alpha premultiply results, expected %.4f,%.4f,%.4f, got %.4f,%.4f,%.4f",
  1642. SDL_GetPixelFormatName(format),
  1643. expectedR, expectedG, expectedB, actualR, actualG, actualB);
  1644. SDL_DestroySurface(surface);
  1645. }
  1646. return TEST_COMPLETED;
  1647. }
  1648. static int SDLCALL surface_testScale(void *arg)
  1649. {
  1650. SDL_PixelFormat formats[] = {
  1651. SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_RGBA8888,
  1652. SDL_PIXELFORMAT_ARGB2101010, SDL_PIXELFORMAT_ABGR2101010,
  1653. SDL_PIXELFORMAT_ARGB64, SDL_PIXELFORMAT_RGBA64,
  1654. SDL_PIXELFORMAT_ARGB128_FLOAT, SDL_PIXELFORMAT_RGBA128_FLOAT,
  1655. };
  1656. SDL_ScaleMode modes[] = {
  1657. SDL_SCALEMODE_NEAREST, SDL_SCALEMODE_LINEAR, SDL_SCALEMODE_PIXELART
  1658. };
  1659. SDL_Surface *surface, *result;
  1660. SDL_PixelFormat format;
  1661. SDL_ScaleMode mode;
  1662. const float MAXIMUM_ERROR = 0.0001f;
  1663. float srcR = 10 / 255.0f, srcG = 128 / 255.0f, srcB = 240 / 255.0f, srcA = 170 / 255.0f;
  1664. float actualR, actualG, actualB, actualA;
  1665. float deltaR, deltaG, deltaB, deltaA;
  1666. int i, j, ret;
  1667. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1668. for (j = 0; j < SDL_arraysize(modes); ++j) {
  1669. format = formats[i];
  1670. mode = modes[j];
  1671. surface = SDL_CreateSurface(1, 1, format);
  1672. SDLTest_AssertCheck(surface != NULL, "SDL_CreateSurface()");
  1673. ret = SDL_SetSurfaceColorspace(surface, SDL_COLORSPACE_SRGB);
  1674. SDLTest_AssertCheck(ret == true, "SDL_SetSurfaceColorspace()");
  1675. ret = SDL_ClearSurface(surface, srcR, srcG, srcB, srcA);
  1676. SDLTest_AssertCheck(ret == true, "SDL_ClearSurface()");
  1677. result = SDL_ScaleSurface(surface, 2, 2, mode);
  1678. SDLTest_AssertCheck(ret == true, "SDL_PremultiplySurfaceAlpha()");
  1679. ret = SDL_ReadSurfacePixelFloat(result, 1, 1, &actualR, &actualG, &actualB, &actualA);
  1680. SDLTest_AssertCheck(ret == true, "SDL_ReadSurfacePixelFloat()");
  1681. deltaR = SDL_fabsf(actualR - srcR);
  1682. deltaG = SDL_fabsf(actualG - srcG);
  1683. deltaB = SDL_fabsf(actualB - srcB);
  1684. deltaA = SDL_fabsf(actualA - srcA);
  1685. SDLTest_AssertCheck(
  1686. deltaR <= MAXIMUM_ERROR &&
  1687. deltaG <= MAXIMUM_ERROR &&
  1688. deltaB <= MAXIMUM_ERROR &&
  1689. deltaA <= MAXIMUM_ERROR,
  1690. "Checking %s %s scaling results, expected %.4f,%.4f,%.4f,%.4f got %.4f,%.4f,%.4f,%.4f",
  1691. SDL_GetPixelFormatName(format),
  1692. mode == SDL_SCALEMODE_NEAREST ? "nearest" :
  1693. mode == SDL_SCALEMODE_LINEAR ? "linear" :
  1694. mode == SDL_SCALEMODE_PIXELART ? "pixelart" : "unknown",
  1695. srcR, srcG, srcB, srcA, actualR, actualG, actualB, actualA);
  1696. SDL_DestroySurface(surface);
  1697. SDL_DestroySurface(result);
  1698. }
  1699. }
  1700. return TEST_COMPLETED;
  1701. }
  1702. #define GENERATE_SHIFTS
  1703. static Uint32 Calculate(int v, int bits, int vmax, int shift)
  1704. {
  1705. #if defined(GENERATE_FLOOR)
  1706. return (Uint32)SDL_floor(v * 255.0f / vmax) << shift;
  1707. #elif defined(GENERATE_ROUND)
  1708. return (Uint32)SDL_roundf(v * 255.0f / vmax) << shift;
  1709. #elif defined(GENERATE_SHIFTS)
  1710. switch (bits) {
  1711. case 1:
  1712. v = (v << 7) | (v << 6) | (v << 5) | (v << 4) | (v << 3) | (v << 2) | (v << 1) | v;
  1713. break;
  1714. case 2:
  1715. v = (v << 6) | (v << 4) | (v << 2) | v;
  1716. break;
  1717. case 3:
  1718. v = (v << 5) | (v << 2) | (v >> 1);
  1719. break;
  1720. case 4:
  1721. v = (v << 4) | v;
  1722. break;
  1723. case 5:
  1724. v = (v << 3) | (v >> 2);
  1725. break;
  1726. case 6:
  1727. v = (v << 2) | (v >> 4);
  1728. break;
  1729. case 7:
  1730. v = (v << 1) | (v >> 6);
  1731. break;
  1732. case 8:
  1733. break;
  1734. }
  1735. return (Uint32)v << shift;
  1736. #endif
  1737. }
  1738. static Uint32 Calculate565toARGB(int v, const SDL_PixelFormatDetails *fmt)
  1739. {
  1740. Uint8 r = (v & 0xF800) >> 11;
  1741. Uint8 g = (v & 0x07E0) >> 5;
  1742. Uint8 b = (v & 0x001F);
  1743. return fmt->Amask |
  1744. Calculate(r, 5, 31, fmt->Rshift) |
  1745. Calculate(g, 6, 63, fmt->Gshift) |
  1746. Calculate(b, 5, 31, fmt->Bshift);
  1747. }
  1748. static int SDLCALL surface_test16BitTo32Bit(void *arg)
  1749. {
  1750. static const SDL_PixelFormat formats[] = {
  1751. SDL_PIXELFORMAT_ARGB8888,
  1752. SDL_PIXELFORMAT_ABGR8888,
  1753. SDL_PIXELFORMAT_RGBA8888,
  1754. SDL_PIXELFORMAT_BGRA8888
  1755. };
  1756. static Uint16 pixels[1 << 16];
  1757. static Uint32 expected[1 << 16];
  1758. int i, p, ret;
  1759. SDL_Surface *surface16;
  1760. SDL_Surface *surface32;
  1761. SDL_Surface *expected32;
  1762. for (p = 0; p < SDL_arraysize(pixels); ++p) {
  1763. pixels[p] = p;
  1764. }
  1765. surface16 = SDL_CreateSurfaceFrom(SDL_arraysize(pixels), 1, SDL_PIXELFORMAT_RGB565, pixels, sizeof(pixels));
  1766. for (i = 0; i < SDL_arraysize(formats); ++i) {
  1767. SDL_PixelFormat format = formats[i];
  1768. const SDL_PixelFormatDetails *fmt = SDL_GetPixelFormatDetails(format);
  1769. SDLTest_Log("Checking conversion from SDL_PIXELFORMAT_RGB565 to %s", SDL_GetPixelFormatName(format));
  1770. surface32 = SDL_ConvertSurface(surface16, format);
  1771. for (p = 0; p < SDL_arraysize(pixels); ++p) {
  1772. expected[p] = Calculate565toARGB(p, fmt);
  1773. }
  1774. expected32 = SDL_CreateSurfaceFrom(SDL_arraysize(expected), 1, format, expected, sizeof(expected));
  1775. ret = SDLTest_CompareSurfaces(surface32, expected32, 0);
  1776. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1777. SDL_DestroySurface(surface32);
  1778. SDL_DestroySurface(expected32);
  1779. }
  1780. SDL_DestroySurface(surface16);
  1781. return TEST_COMPLETED;
  1782. }
  1783. /* ================= Test References ================== */
  1784. /* Surface test cases */
  1785. static const SDLTest_TestCaseReference surfaceTestInvalidFormat = {
  1786. surface_testInvalidFormat, "surface_testInvalidFormat", "Tests creating surface with invalid format", TEST_ENABLED
  1787. };
  1788. static const SDLTest_TestCaseReference surfaceTestSaveLoad = {
  1789. surface_testSaveLoad, "surface_testSaveLoad", "Tests sprite saving and loading.", TEST_ENABLED
  1790. };
  1791. static const SDLTest_TestCaseReference surfaceTestBlitZeroSource = {
  1792. surface_testBlitZeroSource, "surface_testBlitZeroSource", "Tests blitting from a zero sized source rectangle", TEST_ENABLED
  1793. };
  1794. static const SDLTest_TestCaseReference surfaceTestBlit = {
  1795. surface_testBlit, "surface_testBlit", "Tests basic blitting.", TEST_ENABLED
  1796. };
  1797. static const SDLTest_TestCaseReference surfaceTestBlitTiled = {
  1798. surface_testBlitTiled, "surface_testBlitTiled", "Tests tiled blitting.", TEST_ENABLED
  1799. };
  1800. static const SDLTest_TestCaseReference surfaceTestBlit9Grid = {
  1801. surface_testBlit9Grid, "surface_testBlit9Grid", "Tests 9-grid blitting.", TEST_ENABLED
  1802. };
  1803. static const SDLTest_TestCaseReference surfaceTestBlitMultiple = {
  1804. surface_testBlitMultiple, "surface_testBlitMultiple", "Tests blitting between multiple surfaces of the same format.", TEST_ENABLED
  1805. };
  1806. static const SDLTest_TestCaseReference surfaceTestLoadFailure = {
  1807. surface_testLoadFailure, "surface_testLoadFailure", "Tests sprite loading. A failure case.", TEST_ENABLED
  1808. };
  1809. static const SDLTest_TestCaseReference surfaceTestNULLPixels = {
  1810. surface_testSurfaceNULLPixels, "surface_testSurfaceNULLPixels", "Tests surface operations with NULL pixels.", TEST_ENABLED
  1811. };
  1812. static const SDLTest_TestCaseReference surfaceTestRLEPixels = {
  1813. surface_testSurfaceRLEPixels, "surface_testSurfaceRLEPixels", "Tests surface operations with RLE surfaces.", TEST_ENABLED
  1814. };
  1815. static const SDLTest_TestCaseReference surfaceTestSurfaceConversion = {
  1816. surface_testSurfaceConversion, "surface_testSurfaceConversion", "Tests surface conversion.", TEST_ENABLED
  1817. };
  1818. static const SDLTest_TestCaseReference surfaceTestCompleteSurfaceConversion = {
  1819. surface_testCompleteSurfaceConversion, "surface_testCompleteSurfaceConversion", "Tests surface conversion across all pixel formats", TEST_ENABLED
  1820. };
  1821. static const SDLTest_TestCaseReference surfaceTestBlitColorMod = {
  1822. surface_testBlitColorMod, "surface_testBlitColorMod", "Tests some blitting routines with color mod.", TEST_ENABLED
  1823. };
  1824. static const SDLTest_TestCaseReference surfaceTestBlitAlphaMod = {
  1825. surface_testBlitAlphaMod, "surface_testBlitAlphaMod", "Tests some blitting routines with alpha mod.", TEST_ENABLED
  1826. };
  1827. static const SDLTest_TestCaseReference surfaceTestBlitBlendBlend = {
  1828. surface_testBlitBlendBlend, "surface_testBlitBlendBlend", "Tests blitting routines with blend blending mode.", TEST_ENABLED
  1829. };
  1830. static const SDLTest_TestCaseReference surfaceTestBlitBlendPremultiplied = {
  1831. surface_testBlitBlendPremultiplied, "surface_testBlitBlendPremultiplied", "Tests blitting routines with premultiplied blending mode.", TEST_ENABLED
  1832. };
  1833. static const SDLTest_TestCaseReference surfaceTestBlitBlendAdd = {
  1834. surface_testBlitBlendAdd, "surface_testBlitBlendAdd", "Tests blitting routines with add blending mode.", TEST_ENABLED
  1835. };
  1836. static const SDLTest_TestCaseReference surfaceTestBlitBlendAddPremultiplied = {
  1837. surface_testBlitBlendAddPremultiplied, "surface_testBlitBlendAddPremultiplied", "Tests blitting routines with premultiplied add blending mode.", TEST_ENABLED
  1838. };
  1839. static const SDLTest_TestCaseReference surfaceTestBlitBlendMod = {
  1840. surface_testBlitBlendMod, "surface_testBlitBlendMod", "Tests blitting routines with mod blending mode.", TEST_ENABLED
  1841. };
  1842. static const SDLTest_TestCaseReference surfaceTestBlitBlendMul = {
  1843. surface_testBlitBlendMul, "surface_testBlitBlendMul", "Tests blitting routines with mul blending mode.", TEST_ENABLED
  1844. };
  1845. static const SDLTest_TestCaseReference surfaceTestBlitBitmap = {
  1846. surface_testBlitBitmap, "surface_testBlitBitmap", "Tests blitting routines with bitmap surfaces.", TEST_ENABLED
  1847. };
  1848. static const SDLTest_TestCaseReference surfaceTestBlitInvalid = {
  1849. surface_testBlitInvalid, "surface_testBlitInvalid", "Tests blitting routines with invalid surfaces.", TEST_ENABLED
  1850. };
  1851. static const SDLTest_TestCaseReference surfaceTestBlitsWithBadCoordinates = {
  1852. surface_testBlitsWithBadCoordinates, "surface_testBlitsWithBadCoordinates", "Test blitting routines with bad coordinates.", TEST_ENABLED
  1853. };
  1854. static const SDLTest_TestCaseReference surfaceTestOverflow = {
  1855. surface_testOverflow, "surface_testOverflow", "Test overflow detection.", TEST_ENABLED
  1856. };
  1857. static const SDLTest_TestCaseReference surfaceTestFlip = {
  1858. surface_testFlip, "surface_testFlip", "Test surface flipping.", TEST_ENABLED
  1859. };
  1860. static const SDLTest_TestCaseReference surfaceTestPalette = {
  1861. surface_testPalette, "surface_testPalette", "Test surface palette operations.", TEST_ENABLED
  1862. };
  1863. static const SDLTest_TestCaseReference surfaceTestPalettization = {
  1864. surface_testPalettization, "surface_testPalettization", "Test surface palettization.", TEST_ENABLED
  1865. };
  1866. static const SDLTest_TestCaseReference surfaceTestClearSurface = {
  1867. surface_testClearSurface, "surface_testClearSurface", "Test clear surface operations.", TEST_ENABLED
  1868. };
  1869. static const SDLTest_TestCaseReference surfaceTestPremultiplyAlpha = {
  1870. surface_testPremultiplyAlpha, "surface_testPremultiplyAlpha", "Test alpha premultiply operations.", TEST_ENABLED
  1871. };
  1872. static const SDLTest_TestCaseReference surfaceTestScale = {
  1873. surface_testScale, "surface_testScale", "Test scaling operations.", TEST_ENABLED
  1874. };
  1875. static const SDLTest_TestCaseReference surfaceTest16BitTo32Bit = {
  1876. surface_test16BitTo32Bit, "surface_test16BitTo32Bit", "Test conversion from 16-bit to 32-bit pixels.", TEST_ENABLED
  1877. };
  1878. /* Sequence of Surface test cases */
  1879. static const SDLTest_TestCaseReference *surfaceTests[] = {
  1880. &surfaceTestInvalidFormat,
  1881. &surfaceTestSaveLoad,
  1882. &surfaceTestBlitZeroSource,
  1883. &surfaceTestBlit,
  1884. &surfaceTestBlitTiled,
  1885. &surfaceTestBlit9Grid,
  1886. &surfaceTestBlitMultiple,
  1887. &surfaceTestLoadFailure,
  1888. &surfaceTestNULLPixels,
  1889. &surfaceTestRLEPixels,
  1890. &surfaceTestSurfaceConversion,
  1891. &surfaceTestCompleteSurfaceConversion,
  1892. &surfaceTestBlitColorMod,
  1893. &surfaceTestBlitAlphaMod,
  1894. &surfaceTestBlitBlendBlend,
  1895. &surfaceTestBlitBlendPremultiplied,
  1896. &surfaceTestBlitBlendAdd,
  1897. &surfaceTestBlitBlendAddPremultiplied,
  1898. &surfaceTestBlitBlendMod,
  1899. &surfaceTestBlitBlendMul,
  1900. &surfaceTestBlitBitmap,
  1901. &surfaceTestBlitInvalid,
  1902. &surfaceTestBlitsWithBadCoordinates,
  1903. &surfaceTestOverflow,
  1904. &surfaceTestFlip,
  1905. &surfaceTestPalette,
  1906. &surfaceTestPalettization,
  1907. &surfaceTestClearSurface,
  1908. &surfaceTestPremultiplyAlpha,
  1909. &surfaceTestScale,
  1910. &surfaceTest16BitTo32Bit,
  1911. NULL
  1912. };
  1913. /* Surface test suite (global) */
  1914. SDLTest_TestSuiteReference surfaceTestSuite = {
  1915. "Surface",
  1916. surfaceSetUp,
  1917. surfaceTests,
  1918. surfaceTearDown
  1919. };