testautomation_render.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  1. /**
  2. * Original code: automated SDL platform test written by Edgar Simo "bobbens"
  3. * Extended and extensively updated by aschiffler at ferzkopp dot net
  4. */
  5. #include <SDL3/SDL.h>
  6. #include <SDL3/SDL_test.h>
  7. #include "testautomation_images.h"
  8. #include "testautomation_suites.h"
  9. /* ================= Test Case Implementation ================== */
  10. #define TESTRENDER_WINDOW_W 320
  11. #define TESTRENDER_WINDOW_H 240
  12. #define TESTRENDER_SCREEN_W 80
  13. #define TESTRENDER_SCREEN_H 60
  14. #define RENDER_COMPARE_FORMAT SDL_PIXELFORMAT_ARGB8888
  15. #define RENDER_COLOR_CLEAR 0xFF000000
  16. #define RENDER_COLOR_GREEN 0xFF00FF00
  17. #define ALLOWABLE_ERROR_OPAQUE 0
  18. #define ALLOWABLE_ERROR_BLENDED 0
  19. #define CHECK_FUNC(FUNC, PARAMS) \
  20. { \
  21. bool result = FUNC PARAMS; \
  22. if (!result) { \
  23. SDLTest_AssertCheck(result, "Validate result from %s, expected: true, got: false, %s", #FUNC, SDL_GetError()); \
  24. } \
  25. }
  26. /* Test window and renderer */
  27. static SDL_Window *window = NULL;
  28. static SDL_Renderer *renderer = NULL;
  29. /* Prototypes for helper functions */
  30. static int clearScreen(void);
  31. static void compare(SDL_Surface *reference, int allowable_error);
  32. static void compare2x(SDL_Surface *reference, int allowable_error);
  33. static SDL_Texture *loadTestFace(void);
  34. static bool isSupported(int code);
  35. static bool hasDrawColor(void);
  36. /**
  37. * Create software renderer for tests
  38. */
  39. static void SDLCALL InitCreateRenderer(void **arg)
  40. {
  41. const char *renderer_name = NULL;
  42. renderer = NULL;
  43. window = SDL_CreateWindow("render_testCreateRenderer", TESTRENDER_WINDOW_W, TESTRENDER_WINDOW_H, 0);
  44. SDLTest_AssertPass("SDL_CreateWindow()");
  45. SDLTest_AssertCheck(window != NULL, "Check SDL_CreateWindow result");
  46. if (window == NULL) {
  47. return;
  48. }
  49. renderer = SDL_CreateRenderer(window, renderer_name);
  50. SDLTest_AssertPass("SDL_CreateRenderer()");
  51. SDLTest_AssertCheck(renderer != NULL, "Check SDL_CreateRenderer result: %s", renderer != NULL ? "success" : SDL_GetError());
  52. if (renderer == NULL) {
  53. SDL_DestroyWindow(window);
  54. return;
  55. }
  56. }
  57. /**
  58. * Destroy renderer for tests
  59. */
  60. static void SDLCALL CleanupDestroyRenderer(void *arg)
  61. {
  62. if (renderer) {
  63. SDL_DestroyRenderer(renderer);
  64. renderer = NULL;
  65. SDLTest_AssertPass("SDL_DestroyRenderer()");
  66. }
  67. if (window) {
  68. SDL_DestroyWindow(window);
  69. window = NULL;
  70. SDLTest_AssertPass("SDL_DestroyWindow");
  71. }
  72. }
  73. /**
  74. * Tests call to SDL_GetNumRenderDrivers
  75. *
  76. * \sa SDL_GetNumRenderDrivers
  77. */
  78. static int SDLCALL render_testGetNumRenderDrivers(void *arg)
  79. {
  80. int n;
  81. n = SDL_GetNumRenderDrivers();
  82. SDLTest_AssertCheck(n >= 1, "Number of renderers >= 1, reported as %i", n);
  83. return TEST_COMPLETED;
  84. }
  85. /**
  86. * Tests the SDL primitives for rendering.
  87. *
  88. * \sa SDL_SetRenderDrawColor
  89. * \sa SDL_RenderFillRect
  90. * \sa SDL_RenderLine
  91. *
  92. */
  93. static int SDLCALL render_testPrimitives(void *arg)
  94. {
  95. int ret;
  96. int x, y;
  97. SDL_FRect rect;
  98. SDL_Surface *referenceSurface = NULL;
  99. int checkFailCount1;
  100. int checkFailCount2;
  101. /* Clear surface. */
  102. clearScreen();
  103. /* Need drawcolor or just skip test. */
  104. SDLTest_AssertCheck(hasDrawColor(), "hasDrawColor");
  105. /* Draw a rectangle. */
  106. rect.x = 40.0f;
  107. rect.y = 0.0f;
  108. rect.w = 40.0f;
  109. rect.h = 80.0f;
  110. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 13, 73, 200, SDL_ALPHA_OPAQUE))
  111. CHECK_FUNC(SDL_RenderFillRect, (renderer, &rect))
  112. /* Draw a rectangle with negative width and height. */
  113. rect.x = 10.0f + 60.0f;
  114. rect.y = 10.0f + 40.0f;
  115. rect.w = -60.0f;
  116. rect.h = -40.0f;
  117. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 200, 0, 100, SDL_ALPHA_OPAQUE))
  118. CHECK_FUNC(SDL_RenderFillRect, (renderer, &rect))
  119. /* Draw a rectangle with zero width and height. */
  120. rect.x = 10.0f;
  121. rect.y = 10.0f;
  122. rect.w = 0.0f;
  123. rect.h = 0.0f;
  124. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 255, 0, 0, SDL_ALPHA_OPAQUE))
  125. CHECK_FUNC(SDL_RenderFillRect, (renderer, &rect))
  126. /* Draw some points like so:
  127. * X.X.X.X..
  128. * .X.X.X.X.
  129. * X.X.X.X.. */
  130. checkFailCount1 = 0;
  131. checkFailCount2 = 0;
  132. for (y = 0; y < 3; y++) {
  133. for (x = y % 2; x < TESTRENDER_SCREEN_W; x += 2) {
  134. ret = SDL_SetRenderDrawColor(renderer, (Uint8)(x * y), (Uint8)(x * y / 2), (Uint8)(x * y / 3), SDL_ALPHA_OPAQUE);
  135. if (!ret) {
  136. checkFailCount1++;
  137. }
  138. ret = SDL_RenderPoint(renderer, (float)x, (float)y);
  139. if (!ret) {
  140. checkFailCount2++;
  141. }
  142. }
  143. }
  144. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetRenderDrawColor, expected: 0, got: %i", checkFailCount1);
  145. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderPoint, expected: 0, got: %i", checkFailCount2);
  146. /* Draw some lines. */
  147. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  148. CHECK_FUNC(SDL_RenderLine, (renderer, 0.0f, 30.0f, TESTRENDER_SCREEN_W, 30.0f))
  149. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 55, 55, 5, SDL_ALPHA_OPAQUE))
  150. CHECK_FUNC(SDL_RenderLine, (renderer, 40.0f, 30.0f, 40.0f, 60.0f))
  151. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 5, 105, 105, SDL_ALPHA_OPAQUE))
  152. CHECK_FUNC(SDL_RenderLine, (renderer, 0.0f, 0.0f, 29.0f, 29.0f))
  153. CHECK_FUNC(SDL_RenderLine, (renderer, 29.0f, 30.0f, 0.0f, 59.0f))
  154. CHECK_FUNC(SDL_RenderLine, (renderer, 79.0f, 0.0f, 50.0f, 29.0f))
  155. CHECK_FUNC(SDL_RenderLine, (renderer, 79.0f, 59.0f, 50.0f, 30.0f))
  156. /* See if it's the same. */
  157. referenceSurface = SDLTest_ImagePrimitives();
  158. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  159. /* Make current */
  160. SDL_RenderPresent(renderer);
  161. /* Clean up. */
  162. SDL_DestroySurface(referenceSurface);
  163. referenceSurface = NULL;
  164. return TEST_COMPLETED;
  165. }
  166. /**
  167. * Tests the SDL primitives for rendering within a viewport.
  168. *
  169. * \sa SDL_SetRenderDrawColor
  170. * \sa SDL_RenderFillRect
  171. * \sa SDL_RenderLine
  172. *
  173. */
  174. static int SDLCALL render_testPrimitivesWithViewport(void *arg)
  175. {
  176. SDL_Rect viewport;
  177. SDL_Surface *surface;
  178. /* Clear surface. */
  179. clearScreen();
  180. viewport.x = 2;
  181. viewport.y = 2;
  182. viewport.w = 2;
  183. viewport.h = 2;
  184. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport));
  185. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 255, 255, 255, SDL_ALPHA_OPAQUE))
  186. CHECK_FUNC(SDL_RenderLine, (renderer, 0.0f, 0.0f, 1.0f, 1.0f));
  187. viewport.x = 3;
  188. viewport.y = 3;
  189. viewport.w = 1;
  190. viewport.h = 1;
  191. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport));
  192. surface = SDL_RenderReadPixels(renderer, NULL);
  193. if (surface) {
  194. Uint8 r, g, b, a;
  195. CHECK_FUNC(SDL_ReadSurfacePixel, (surface, 0, 0, &r, &g, &b, &a));
  196. SDLTest_AssertCheck(r == 0xFF && g == 0xFF && b == 0xFF && a == 0xFF, "Validate diagonal line drawing with viewport, expected 0xFFFFFFFF, got 0x%.2x%.2x%.2x%.2x", r, g, b, a);
  197. SDL_DestroySurface(surface);
  198. } else {
  199. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  200. }
  201. return TEST_COMPLETED;
  202. }
  203. /**
  204. * Tests some blitting routines.
  205. *
  206. * \sa SDL_RenderTexture
  207. * \sa SDL_DestroyTexture
  208. */
  209. static int SDLCALL render_testBlit(void *arg)
  210. {
  211. int ret;
  212. SDL_FRect rect;
  213. SDL_Texture *tface;
  214. SDL_Surface *referenceSurface = NULL;
  215. int i, j, ni, nj;
  216. int checkFailCount1;
  217. /* Clear surface. */
  218. clearScreen();
  219. /* Need drawcolor or just skip test. */
  220. SDLTest_AssertCheck(hasDrawColor(), "hasDrawColor)");
  221. /* Create face surface. */
  222. tface = loadTestFace();
  223. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  224. if (tface == NULL) {
  225. return TEST_ABORTED;
  226. }
  227. /* Constant values. */
  228. rect.w = (float)tface->w;
  229. rect.h = (float)tface->h;
  230. ni = TESTRENDER_SCREEN_W - tface->w;
  231. nj = TESTRENDER_SCREEN_H - tface->h;
  232. /* Loop blit. */
  233. checkFailCount1 = 0;
  234. for (j = 0; j <= nj; j += 4) {
  235. for (i = 0; i <= ni; i += 4) {
  236. /* Blitting. */
  237. rect.x = (float)i;
  238. rect.y = (float)j;
  239. ret = SDL_RenderTexture(renderer, tface, NULL, &rect);
  240. if (!ret) {
  241. checkFailCount1++;
  242. }
  243. }
  244. }
  245. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_RenderTexture, expected: 0, got: %i", checkFailCount1);
  246. /* See if it's the same */
  247. referenceSurface = SDLTest_ImageBlit();
  248. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  249. /* Make current */
  250. SDL_RenderPresent(renderer);
  251. /* Clean up. */
  252. SDL_DestroyTexture(tface);
  253. SDL_DestroySurface(referenceSurface);
  254. referenceSurface = NULL;
  255. return TEST_COMPLETED;
  256. }
  257. /**
  258. * Tests tiled blitting routines.
  259. */
  260. static int SDLCALL render_testBlitTiled(void *arg)
  261. {
  262. int ret;
  263. SDL_FRect rect;
  264. SDL_Texture *tface;
  265. SDL_Surface *referenceSurface = NULL;
  266. SDL_Surface *referenceSurface2x = NULL;
  267. /* Create face surface. */
  268. tface = loadTestFace();
  269. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  270. if (tface == NULL) {
  271. return TEST_ABORTED;
  272. }
  273. SDL_SetTextureScaleMode(tface, SDL_SCALEMODE_NEAREST); /* So 2x scaling is pixel perfect */
  274. /* Tiled blit - 1.0 scale */
  275. {
  276. /* Clear surface. */
  277. clearScreen();
  278. /* Tiled blit. */
  279. rect.x = 0.0f;
  280. rect.y = 0.0f;
  281. rect.w = (float)TESTRENDER_SCREEN_W;
  282. rect.h = (float)TESTRENDER_SCREEN_H;
  283. ret = SDL_RenderTextureTiled(renderer, tface, NULL, 1.0f, &rect);
  284. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTextureTiled, expected: true, got: %i", ret);
  285. /* See if it's the same */
  286. referenceSurface = SDLTest_ImageBlitTiled();
  287. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  288. /* Make current */
  289. SDL_RenderPresent(renderer);
  290. }
  291. /* Tiled blit - 2.0 scale */
  292. {
  293. /* Clear surface. */
  294. clearScreen();
  295. /* Tiled blit. */
  296. rect.x = 0.0f;
  297. rect.y = 0.0f;
  298. rect.w = (float)TESTRENDER_SCREEN_W * 2;
  299. rect.h = (float)TESTRENDER_SCREEN_H * 2;
  300. ret = SDL_RenderTextureTiled(renderer, tface, NULL, 2.0f, &rect);
  301. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTextureTiled, expected: true, got: %i", ret);
  302. /* See if it's the same */
  303. referenceSurface2x = SDL_CreateSurface(referenceSurface->w * 2, referenceSurface->h * 2, referenceSurface->format);
  304. SDL_BlitSurfaceScaled(referenceSurface, NULL, referenceSurface2x, NULL, SDL_SCALEMODE_NEAREST);
  305. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_BlitSurfaceScaled, expected: 0, got: %i", ret);
  306. compare2x(referenceSurface2x, ALLOWABLE_ERROR_OPAQUE);
  307. /* Make current */
  308. SDL_RenderPresent(renderer);
  309. }
  310. /* Clean up. */
  311. SDL_DestroyTexture(tface);
  312. SDL_DestroySurface(referenceSurface);
  313. SDL_DestroySurface(referenceSurface2x);
  314. referenceSurface = NULL;
  315. return TEST_COMPLETED;
  316. }
  317. static const Uint8 COLOR_SEPARATION = 85;
  318. static void Fill9GridReferenceSurface(SDL_Surface *surface, int left_width, int right_width, int top_height, int bottom_height)
  319. {
  320. SDL_Rect rect;
  321. // Upper left
  322. rect.x = 0;
  323. rect.y = 0;
  324. rect.w = left_width;
  325. rect.h = top_height;
  326. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  327. // Top
  328. rect.x = left_width;
  329. rect.y = 0;
  330. rect.w = surface->w - left_width - right_width;
  331. rect.h = top_height;
  332. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  333. // Upper right
  334. rect.x = surface->w - right_width;
  335. rect.y = 0;
  336. rect.w = right_width;
  337. rect.h = top_height;
  338. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 1 * COLOR_SEPARATION, 0));
  339. // Left
  340. rect.x = 0;
  341. rect.y = top_height;
  342. rect.w = left_width;
  343. rect.h = surface->h - top_height - bottom_height;
  344. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  345. // Center
  346. rect.x = left_width;
  347. rect.y = top_height;
  348. rect.w = surface->w - right_width - left_width;
  349. rect.h = surface->h - top_height - bottom_height;
  350. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  351. // Right
  352. rect.x = surface->w - right_width;
  353. rect.y = top_height;
  354. rect.w = right_width;
  355. rect.h = surface->h - top_height - bottom_height;
  356. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 2 * COLOR_SEPARATION, 0));
  357. // Lower left
  358. rect.x = 0;
  359. rect.y = surface->h - bottom_height;
  360. rect.w = left_width;
  361. rect.h = bottom_height;
  362. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 1 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  363. // Bottom
  364. rect.x = left_width;
  365. rect.y = surface->h - bottom_height;
  366. rect.w = surface->w - left_width - right_width;
  367. rect.h = bottom_height;
  368. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 2 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  369. // Lower right
  370. rect.x = surface->w - right_width;
  371. rect.y = surface->h - bottom_height;
  372. rect.w = right_width;
  373. rect.h = bottom_height;
  374. SDL_FillSurfaceRect(surface, &rect, SDL_MapSurfaceRGB(surface, 3 * COLOR_SEPARATION, 3 * COLOR_SEPARATION, 0));
  375. }
  376. /**
  377. * Tests 9-grid blitting.
  378. */
  379. static int SDLCALL render_testBlit9Grid(void *arg)
  380. {
  381. SDL_Surface *referenceSurface = NULL;
  382. SDL_Surface *source = NULL;
  383. SDL_Texture *texture;
  384. int x, y;
  385. SDL_FRect rect;
  386. int ret = 0;
  387. /* Create source surface */
  388. source = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGBA32);
  389. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  390. for (y = 0; y < 3; ++y) {
  391. for (x = 0; x < 3; ++x) {
  392. SDL_WriteSurfacePixel(source, x, y, (Uint8)((1 + x) * COLOR_SEPARATION), (Uint8)((1 + y) * COLOR_SEPARATION), 0, 255);
  393. }
  394. }
  395. texture = SDL_CreateTextureFromSurface(renderer, source);
  396. SDLTest_AssertCheck(texture != NULL, "Verify source texture is not NULL");
  397. ret = SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  398. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_SetTextureScaleMode, expected: true, got: %i", ret);
  399. /* 9-grid blit - 1.0 scale */
  400. {
  401. SDLTest_Log("9-grid blit - 1.0 scale");
  402. /* Create reference surface */
  403. SDL_DestroySurface(referenceSurface);
  404. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  405. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  406. Fill9GridReferenceSurface(referenceSurface, 1, 1, 1, 1);
  407. /* Clear surface. */
  408. clearScreen();
  409. /* Tiled blit. */
  410. rect.x = 0.0f;
  411. rect.y = 0.0f;
  412. rect.w = (float)TESTRENDER_SCREEN_W;
  413. rect.h = (float)TESTRENDER_SCREEN_H;
  414. ret = SDL_RenderTexture9Grid(renderer, texture, NULL, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, &rect);
  415. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9Grid, expected: true, got: %i", ret);
  416. /* See if it's the same */
  417. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  418. /* Make current */
  419. SDL_RenderPresent(renderer);
  420. }
  421. /* 9-grid blit - 2.0 scale */
  422. {
  423. SDLTest_Log("9-grid blit - 2.0 scale");
  424. /* Create reference surface */
  425. SDL_DestroySurface(referenceSurface);
  426. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  427. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  428. Fill9GridReferenceSurface(referenceSurface, 2, 2, 2, 2);
  429. /* Clear surface. */
  430. clearScreen();
  431. /* Tiled blit. */
  432. rect.x = 0.0f;
  433. rect.y = 0.0f;
  434. rect.w = (float)TESTRENDER_SCREEN_W;
  435. rect.h = (float)TESTRENDER_SCREEN_H;
  436. ret = SDL_RenderTexture9Grid(renderer, texture, NULL, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, &rect);
  437. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9Grid, expected: true, got: %i", ret);
  438. /* See if it's the same */
  439. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  440. /* Make current */
  441. SDL_RenderPresent(renderer);
  442. }
  443. /* Clean up. */
  444. SDL_DestroySurface(source);
  445. SDL_DestroyTexture(texture);
  446. /* Create complex source surface */
  447. source = SDL_CreateSurface(5, 5, SDL_PIXELFORMAT_RGBA32);
  448. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  449. SDL_WriteSurfacePixel(source, 0, 0, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  450. SDL_WriteSurfacePixel(source, 1, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  451. SDL_WriteSurfacePixel(source, 2, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  452. SDL_WriteSurfacePixel(source, 3, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  453. SDL_WriteSurfacePixel(source, 4, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  454. SDL_WriteSurfacePixel(source, 0, 1, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  455. SDL_WriteSurfacePixel(source, 1, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  456. SDL_WriteSurfacePixel(source, 2, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  457. SDL_WriteSurfacePixel(source, 3, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  458. SDL_WriteSurfacePixel(source, 4, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  459. SDL_WriteSurfacePixel(source, 0, 2, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  460. SDL_WriteSurfacePixel(source, 1, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  461. SDL_WriteSurfacePixel(source, 2, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  462. SDL_WriteSurfacePixel(source, 3, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  463. SDL_WriteSurfacePixel(source, 4, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  464. SDL_WriteSurfacePixel(source, 0, 3, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  465. SDL_WriteSurfacePixel(source, 1, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  466. SDL_WriteSurfacePixel(source, 2, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  467. SDL_WriteSurfacePixel(source, 3, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  468. SDL_WriteSurfacePixel(source, 4, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  469. SDL_WriteSurfacePixel(source, 0, 4, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  470. SDL_WriteSurfacePixel(source, 1, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  471. SDL_WriteSurfacePixel(source, 2, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  472. SDL_WriteSurfacePixel(source, 3, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  473. SDL_WriteSurfacePixel(source, 4, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  474. texture = SDL_CreateTextureFromSurface(renderer, source);
  475. SDLTest_AssertCheck(texture != NULL, "Verify source texture is not NULL");
  476. ret = SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  477. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_SetTextureScaleMode, expected: true, got: %i", ret);
  478. /* complex 9-grid blit - 1.0 scale */
  479. {
  480. SDLTest_Log("complex 9-grid blit - 1.0 scale");
  481. /* Create reference surface */
  482. SDL_DestroySurface(referenceSurface);
  483. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  484. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  485. Fill9GridReferenceSurface(referenceSurface, 1, 2, 1, 2);
  486. /* Clear surface. */
  487. clearScreen();
  488. /* Tiled blit. */
  489. rect.x = 0.0f;
  490. rect.y = 0.0f;
  491. rect.w = (float)TESTRENDER_SCREEN_W;
  492. rect.h = (float)TESTRENDER_SCREEN_H;
  493. ret = SDL_RenderTexture9Grid(renderer, texture, NULL, 1.0f, 2.0f, 1.0f, 2.0f, 1.0f, &rect);
  494. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9Grid, expected: true, got: %i", ret);
  495. /* See if it's the same */
  496. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  497. /* Make current */
  498. SDL_RenderPresent(renderer);
  499. }
  500. /* complex 9-grid blit - 2.0 scale */
  501. {
  502. SDLTest_Log("complex 9-grid blit - 2.0 scale");
  503. /* Create reference surface */
  504. SDL_DestroySurface(referenceSurface);
  505. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  506. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  507. Fill9GridReferenceSurface(referenceSurface, 2, 4, 2, 4);
  508. /* Clear surface. */
  509. clearScreen();
  510. /* Tiled blit. */
  511. rect.x = 0.0f;
  512. rect.y = 0.0f;
  513. rect.w = (float)TESTRENDER_SCREEN_W;
  514. rect.h = (float)TESTRENDER_SCREEN_H;
  515. ret = SDL_RenderTexture9Grid(renderer, texture, NULL, 1.0f, 2.0f, 1.0f, 2.0f, 2.0f, &rect);
  516. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9Grid, expected: true, got: %i", ret);
  517. /* See if it's the same */
  518. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  519. /* Make current */
  520. SDL_RenderPresent(renderer);
  521. }
  522. /* Clean up. */
  523. SDL_DestroySurface(referenceSurface);
  524. SDL_DestroySurface(source);
  525. SDL_DestroyTexture(texture);
  526. return TEST_COMPLETED;
  527. }
  528. /**
  529. * Tests tiled 9-grid blitting.
  530. */
  531. static int SDLCALL render_testBlit9GridTiled(void *arg)
  532. {
  533. SDL_Surface *referenceSurface = NULL;
  534. SDL_Surface *source = NULL;
  535. SDL_Texture *texture;
  536. int x, y;
  537. SDL_FRect rect;
  538. int ret = 0;
  539. /* Create source surface */
  540. source = SDL_CreateSurface(3, 3, SDL_PIXELFORMAT_RGBA32);
  541. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  542. for (y = 0; y < 3; ++y) {
  543. for (x = 0; x < 3; ++x) {
  544. SDL_WriteSurfacePixel(source, x, y, (Uint8)((1 + x) * COLOR_SEPARATION), (Uint8)((1 + y) * COLOR_SEPARATION), 0, 255);
  545. }
  546. }
  547. texture = SDL_CreateTextureFromSurface(renderer, source);
  548. SDLTest_AssertCheck(texture != NULL, "Verify source texture is not NULL");
  549. ret = SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  550. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_SetTextureScaleMode, expected: true, got: %i", ret);
  551. /* Tiled 9-grid blit - 1.0 scale */
  552. {
  553. SDLTest_Log("tiled 9-grid blit - 1.0 scale");
  554. /* Create reference surface */
  555. SDL_DestroySurface(referenceSurface);
  556. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  557. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  558. Fill9GridReferenceSurface(referenceSurface, 1, 1, 1, 1);
  559. /* Clear surface. */
  560. clearScreen();
  561. /* Tiled blit. */
  562. rect.x = 0.0f;
  563. rect.y = 0.0f;
  564. rect.w = (float)TESTRENDER_SCREEN_W;
  565. rect.h = (float)TESTRENDER_SCREEN_H;
  566. ret = SDL_RenderTexture9GridTiled(renderer, texture, NULL, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, &rect, 1.0f);
  567. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9GridTiled, expected: true, got: %i", ret);
  568. /* See if it's the same */
  569. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  570. /* Make current */
  571. SDL_RenderPresent(renderer);
  572. }
  573. /* Tiled 9-grid blit - 2.0 scale */
  574. {
  575. SDLTest_Log("tiled 9-grid blit - 2.0 scale");
  576. /* Create reference surface */
  577. SDL_DestroySurface(referenceSurface);
  578. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  579. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  580. Fill9GridReferenceSurface(referenceSurface, 2, 2, 2, 2);
  581. /* Clear surface. */
  582. clearScreen();
  583. /* Tiled blit. */
  584. rect.x = 0.0f;
  585. rect.y = 0.0f;
  586. rect.w = (float)TESTRENDER_SCREEN_W;
  587. rect.h = (float)TESTRENDER_SCREEN_H;
  588. ret = SDL_RenderTexture9GridTiled(renderer, texture, NULL, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, &rect, 2.0f);
  589. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9GridTiled, expected: true, got: %i", ret);
  590. /* See if it's the same */
  591. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  592. /* Make current */
  593. SDL_RenderPresent(renderer);
  594. }
  595. /* Clean up. */
  596. SDL_DestroySurface(source);
  597. SDL_DestroyTexture(texture);
  598. /* Create complex source surface */
  599. source = SDL_CreateSurface(5, 5, SDL_PIXELFORMAT_RGBA32);
  600. SDLTest_AssertCheck(source != NULL, "Verify source surface is not NULL");
  601. SDL_WriteSurfacePixel(source, 0, 0, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  602. SDL_WriteSurfacePixel(source, 1, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  603. SDL_WriteSurfacePixel(source, 2, 0, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  604. SDL_WriteSurfacePixel(source, 3, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  605. SDL_WriteSurfacePixel(source, 4, 0, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((1) * COLOR_SEPARATION), 0, 255);
  606. SDL_WriteSurfacePixel(source, 0, 1, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  607. SDL_WriteSurfacePixel(source, 1, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  608. SDL_WriteSurfacePixel(source, 2, 1, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  609. SDL_WriteSurfacePixel(source, 3, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  610. SDL_WriteSurfacePixel(source, 4, 1, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  611. SDL_WriteSurfacePixel(source, 0, 2, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  612. SDL_WriteSurfacePixel(source, 1, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  613. SDL_WriteSurfacePixel(source, 2, 2, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  614. SDL_WriteSurfacePixel(source, 3, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  615. SDL_WriteSurfacePixel(source, 4, 2, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((2) * COLOR_SEPARATION), 0, 255);
  616. SDL_WriteSurfacePixel(source, 0, 3, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  617. SDL_WriteSurfacePixel(source, 1, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  618. SDL_WriteSurfacePixel(source, 2, 3, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  619. SDL_WriteSurfacePixel(source, 3, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  620. SDL_WriteSurfacePixel(source, 4, 3, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  621. SDL_WriteSurfacePixel(source, 0, 4, (Uint8)((1) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  622. SDL_WriteSurfacePixel(source, 1, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  623. SDL_WriteSurfacePixel(source, 2, 4, (Uint8)((2) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  624. SDL_WriteSurfacePixel(source, 3, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  625. SDL_WriteSurfacePixel(source, 4, 4, (Uint8)((3) * COLOR_SEPARATION), (Uint8)((3) * COLOR_SEPARATION), 0, 255);
  626. texture = SDL_CreateTextureFromSurface(renderer, source);
  627. SDLTest_AssertCheck(texture != NULL, "Verify source texture is not NULL");
  628. ret = SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  629. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_SetTextureScaleMode, expected: true, got: %i", ret);
  630. /* complex tiled 9-grid blit - 1.0 scale */
  631. {
  632. SDLTest_Log("complex tiled 9-grid blit - 1.0 scale");
  633. /* Create reference surface */
  634. SDL_DestroySurface(referenceSurface);
  635. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  636. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  637. Fill9GridReferenceSurface(referenceSurface, 1, 2, 1, 2);
  638. /* Clear surface. */
  639. clearScreen();
  640. /* Tiled blit. */
  641. rect.x = 0.0f;
  642. rect.y = 0.0f;
  643. rect.w = (float)TESTRENDER_SCREEN_W;
  644. rect.h = (float)TESTRENDER_SCREEN_H;
  645. ret = SDL_RenderTexture9GridTiled(renderer, texture, NULL, 1.0f, 2.0f, 1.0f, 2.0f, 1.0f, &rect, 1.0f);
  646. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9GridTiled, expected: true, got: %i", ret);
  647. /* See if it's the same */
  648. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  649. /* Make current */
  650. SDL_RenderPresent(renderer);
  651. }
  652. /* complex tiled 9-grid blit - 2.0 scale */
  653. {
  654. SDLTest_Log("complex tiled 9-grid blit - 2.0 scale");
  655. /* Create reference surface */
  656. SDL_DestroySurface(referenceSurface);
  657. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, SDL_PIXELFORMAT_RGBA32);
  658. SDLTest_AssertCheck(referenceSurface != NULL, "Verify reference surface is not NULL");
  659. Fill9GridReferenceSurface(referenceSurface, 2, 4, 2, 4);
  660. /* Clear surface. */
  661. clearScreen();
  662. /* Tiled blit. */
  663. rect.x = 0.0f;
  664. rect.y = 0.0f;
  665. rect.w = (float)TESTRENDER_SCREEN_W;
  666. rect.h = (float)TESTRENDER_SCREEN_H;
  667. ret = SDL_RenderTexture9GridTiled(renderer, texture, NULL, 1.0f, 2.0f, 1.0f, 2.0f, 2.0f, &rect, 2.0f);
  668. SDLTest_AssertCheck(ret == true, "Validate results from call to SDL_RenderTexture9GridTiled, expected: true, got: %i", ret);
  669. /* See if it's the same */
  670. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  671. /* Make current */
  672. SDL_RenderPresent(renderer);
  673. }
  674. /* Clean up. */
  675. SDL_DestroySurface(referenceSurface);
  676. SDL_DestroySurface(source);
  677. SDL_DestroyTexture(texture);
  678. return TEST_COMPLETED;
  679. }
  680. /**
  681. * Blits doing color tests.
  682. *
  683. * \sa SDL_SetTextureColorMod
  684. * \sa SDL_RenderTexture
  685. * \sa SDL_DestroyTexture
  686. */
  687. static int SDLCALL render_testBlitColor(void *arg)
  688. {
  689. int ret;
  690. SDL_FRect rect;
  691. SDL_Texture *tface;
  692. SDL_Surface *referenceSurface = NULL;
  693. int i, j, ni, nj;
  694. int checkFailCount1;
  695. int checkFailCount2;
  696. /* Clear surface. */
  697. clearScreen();
  698. /* Create face surface. */
  699. tface = loadTestFace();
  700. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  701. if (tface == NULL) {
  702. return TEST_ABORTED;
  703. }
  704. /* Constant values. */
  705. rect.w = (float)tface->w;
  706. rect.h = (float)tface->h;
  707. ni = TESTRENDER_SCREEN_W - tface->w;
  708. nj = TESTRENDER_SCREEN_H - tface->h;
  709. /* Test blitting with color mod. */
  710. checkFailCount1 = 0;
  711. checkFailCount2 = 0;
  712. for (j = 0; j <= nj; j += 4) {
  713. for (i = 0; i <= ni; i += 4) {
  714. /* Set color mod. */
  715. ret = SDL_SetTextureColorMod(tface, (Uint8)((255 / nj) * j), (Uint8)((255 / ni) * i), (Uint8)((255 / nj) * j));
  716. if (!ret) {
  717. checkFailCount1++;
  718. }
  719. /* Blitting. */
  720. rect.x = (float)i;
  721. rect.y = (float)j;
  722. ret = SDL_RenderTexture(renderer, tface, NULL, &rect);
  723. if (!ret) {
  724. checkFailCount2++;
  725. }
  726. }
  727. }
  728. SDLTest_AssertCheck(checkFailCount1 == 0, "Validate results from calls to SDL_SetTextureColorMod, expected: 0, got: %i", checkFailCount1);
  729. SDLTest_AssertCheck(checkFailCount2 == 0, "Validate results from calls to SDL_RenderTexture, expected: 0, got: %i", checkFailCount2);
  730. /* See if it's the same. */
  731. referenceSurface = SDLTest_ImageBlitColor();
  732. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  733. /* Make current */
  734. SDL_RenderPresent(renderer);
  735. /* Clean up. */
  736. SDL_DestroyTexture(tface);
  737. SDL_DestroySurface(referenceSurface);
  738. referenceSurface = NULL;
  739. return TEST_COMPLETED;
  740. }
  741. typedef enum TestRenderOperation
  742. {
  743. TEST_RENDER_POINT,
  744. TEST_RENDER_LINE,
  745. TEST_RENDER_RECT,
  746. TEST_RENDER_COPY_XRGB,
  747. TEST_RENDER_COPY_ARGB,
  748. } TestRenderOperation;
  749. /**
  750. * Helper that tests a specific operation and blend mode, -1 for color mod, -2 for alpha mod
  751. */
  752. static void testBlendModeOperation(TestRenderOperation op, int mode, SDL_PixelFormat dst_format)
  753. {
  754. /* Allow up to 2 delta from theoretical value to account for rounding error.
  755. * We allow 2 rounding errors because the software renderer breaks drawing operations into alpha multiplication and a separate blend operation.
  756. */
  757. const int MAXIMUM_ERROR = 2;
  758. int ret;
  759. SDL_Texture *src = NULL;
  760. SDL_Texture *dst;
  761. SDL_Surface *result;
  762. Uint8 srcR = 10, srcG = 128, srcB = 240, srcA = 100;
  763. Uint8 dstR = 128, dstG = 128, dstB = 128, dstA = 128;
  764. Uint8 expectedR, expectedG, expectedB, expectedA;
  765. Uint8 actualR, actualG, actualB, actualA;
  766. int deltaR, deltaG, deltaB, deltaA;
  767. const char *operation = "UNKNOWN";
  768. const char *mode_name = "UNKNOWN";
  769. /* Create dst surface */
  770. dst = SDL_CreateTexture(renderer, dst_format, SDL_TEXTUREACCESS_TARGET, 3, 3);
  771. SDLTest_AssertCheck(dst != NULL, "Verify dst surface is not NULL");
  772. if (dst == NULL) {
  773. return;
  774. }
  775. if (SDL_ISPIXELFORMAT_ALPHA(dst_format)) {
  776. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  777. ret = SDL_GetTextureBlendMode(dst, &blendMode);
  778. SDLTest_AssertCheck(ret == true, "Verify result from SDL_GetTextureBlendMode(), expected: true, got: %i", ret);
  779. SDLTest_AssertCheck(blendMode == SDL_BLENDMODE_BLEND, "Verify alpha texture blend mode, expected %d, got %" SDL_PRIu32, SDL_BLENDMODE_BLEND, blendMode);
  780. }
  781. /* Set as render target */
  782. SDL_SetRenderTarget(renderer, dst);
  783. /* Clear surface. */
  784. if (!SDL_ISPIXELFORMAT_ALPHA(dst_format)) {
  785. dstA = 255;
  786. }
  787. ret = SDL_SetRenderDrawColor(renderer, dstR, dstG, dstB, dstA);
  788. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawColor(), expected: true, got: %i", ret);
  789. ret = SDL_RenderClear(renderer);
  790. SDLTest_AssertPass("Call to SDL_RenderClear()");
  791. SDLTest_AssertCheck(ret == true, "Verify result from SDL_RenderClear, expected: true, got: %i", ret);
  792. if (op == TEST_RENDER_COPY_XRGB || op == TEST_RENDER_COPY_ARGB) {
  793. Uint8 pixels[4];
  794. /* Create src surface */
  795. src = SDL_CreateTexture(renderer, op == TEST_RENDER_COPY_XRGB ? SDL_PIXELFORMAT_RGBX32 : SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 1, 1);
  796. SDLTest_AssertCheck(src != NULL, "Verify src surface is not NULL");
  797. if (src == NULL) {
  798. return;
  799. }
  800. /* Clear surface. */
  801. if (op == TEST_RENDER_COPY_XRGB) {
  802. srcA = 255;
  803. }
  804. pixels[0] = srcR;
  805. pixels[1] = srcG;
  806. pixels[2] = srcB;
  807. pixels[3] = srcA;
  808. SDL_UpdateTexture(src, NULL, pixels, sizeof(pixels));
  809. /* Set blend mode. */
  810. if (mode >= 0) {
  811. ret = SDL_SetTextureBlendMode(src, (SDL_BlendMode)mode);
  812. SDLTest_AssertPass("Call to SDL_SetTextureBlendMode()");
  813. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetTextureBlendMode(..., %i), expected: true, got: %i", mode, ret);
  814. } else {
  815. ret = SDL_SetTextureBlendMode(src, SDL_BLENDMODE_BLEND);
  816. SDLTest_AssertPass("Call to SDL_SetTextureBlendMode()");
  817. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetTextureBlendMode(..., %i), expected: true, got: %i", mode, ret);
  818. }
  819. } else {
  820. /* Set draw color */
  821. ret = SDL_SetRenderDrawColor(renderer, srcR, srcG, srcB, srcA);
  822. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawColor(), expected: true, got: %i", ret);
  823. /* Set blend mode. */
  824. if (mode >= 0) {
  825. ret = SDL_SetRenderDrawBlendMode(renderer, (SDL_BlendMode)mode);
  826. SDLTest_AssertPass("Call to SDL_SetRenderDrawBlendMode()");
  827. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawBlendMode(..., %i), expected: true, got: %i", mode, ret);
  828. } else {
  829. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  830. SDLTest_AssertPass("Call to SDL_SetRenderDrawBlendMode()");
  831. SDLTest_AssertCheck(ret == true, "Verify result from SDL_SetRenderDrawBlendMode(..., %i), expected: true, got: %i", mode, ret);
  832. }
  833. }
  834. /* Test blend mode. */
  835. #define FLOAT(X) ((float)X / 255.0f)
  836. switch (mode) {
  837. case -1:
  838. mode_name = "color modulation";
  839. ret = SDL_SetTextureColorMod(src, srcR, srcG, srcB);
  840. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetTextureColorMod, expected: true, got: %i", ret);
  841. expectedR = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcR) * FLOAT(srcR)) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  842. expectedG = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcG) * FLOAT(srcG)) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  843. expectedB = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcB) * FLOAT(srcB)) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  844. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  845. break;
  846. case -2:
  847. mode_name = "alpha modulation";
  848. ret = SDL_SetTextureAlphaMod(src, srcA);
  849. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_SetTextureAlphaMod, expected: true, got: %i", ret);
  850. 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);
  851. 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);
  852. 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);
  853. expectedA = (Uint8)SDL_roundf(SDL_clamp((FLOAT(srcA) * FLOAT(srcA)) + FLOAT(dstA) * (1.0f - (FLOAT(srcA) * FLOAT(srcA))), 0.0f, 1.0f) * 255.0f);
  854. break;
  855. case SDL_BLENDMODE_NONE:
  856. mode_name = "SDL_BLENDMODE_NONE";
  857. expectedR = srcR;
  858. expectedG = srcG;
  859. expectedB = srcB;
  860. expectedA = SDL_ISPIXELFORMAT_ALPHA(dst_format) ? srcA : 255;
  861. break;
  862. case SDL_BLENDMODE_BLEND:
  863. mode_name = "SDL_BLENDMODE_BLEND";
  864. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  865. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  866. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  867. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  868. break;
  869. case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
  870. mode_name = "SDL_BLENDMODE_BLEND_PREMULTIPLIED";
  871. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  872. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  873. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  874. expectedA = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcA) + FLOAT(dstA) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  875. break;
  876. case SDL_BLENDMODE_ADD:
  877. mode_name = "SDL_BLENDMODE_ADD";
  878. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(srcA) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  879. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(srcA) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  880. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(srcA) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  881. expectedA = dstA;
  882. break;
  883. case SDL_BLENDMODE_ADD_PREMULTIPLIED:
  884. mode_name = "SDL_BLENDMODE_ADD_PREMULTIPLIED";
  885. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) + FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  886. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) + FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  887. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) + FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  888. expectedA = dstA;
  889. break;
  890. case SDL_BLENDMODE_MOD:
  891. mode_name = "SDL_BLENDMODE_MOD";
  892. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR), 0.0f, 1.0f) * 255.0f);
  893. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG), 0.0f, 1.0f) * 255.0f);
  894. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB), 0.0f, 1.0f) * 255.0f);
  895. expectedA = dstA;
  896. break;
  897. case SDL_BLENDMODE_MUL:
  898. mode_name = "SDL_BLENDMODE_MUL";
  899. expectedR = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcR) * FLOAT(dstR) + FLOAT(dstR) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  900. expectedG = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcG) * FLOAT(dstG) + FLOAT(dstG) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  901. expectedB = (Uint8)SDL_roundf(SDL_clamp(FLOAT(srcB) * FLOAT(dstB) + FLOAT(dstB) * (1.0f - FLOAT(srcA)), 0.0f, 1.0f) * 255.0f);
  902. expectedA = dstA;
  903. break;
  904. default:
  905. SDLTest_LogError("Invalid blending mode: %d", mode);
  906. return;
  907. }
  908. switch (op) {
  909. case TEST_RENDER_POINT:
  910. operation = "render point";
  911. ret = SDL_RenderPoint(renderer, 0.0f, 0.0f);
  912. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderPoint, expected: 0, got: %i", ret);
  913. break;
  914. case TEST_RENDER_LINE:
  915. operation = "render line";
  916. ret = SDL_RenderLine(renderer, 0.0f, 0.0f, 2.0f, 2.0f);
  917. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderLine, expected: true, got: %i", ret);
  918. break;
  919. case TEST_RENDER_RECT:
  920. operation = "render rect";
  921. ret = SDL_RenderFillRect(renderer, NULL);
  922. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderFillRect, expected: 0, got: %i", ret);
  923. break;
  924. case TEST_RENDER_COPY_XRGB:
  925. case TEST_RENDER_COPY_ARGB:
  926. operation = (op == TEST_RENDER_COPY_XRGB) ? "render XRGB" : "render ARGB";
  927. ret = SDL_RenderTexture(renderer, src, NULL, NULL);
  928. SDLTest_AssertCheck(ret == true, "Validate results from calls to SDL_RenderTexture, expected: true, got: %i", ret);
  929. break;
  930. default:
  931. SDLTest_LogError("Invalid blending operation: %d", op);
  932. return;
  933. }
  934. result = SDL_RenderReadPixels(renderer, NULL);
  935. SDL_ReadSurfacePixel(result, 0, 0, &actualR, &actualG, &actualB, &actualA);
  936. deltaR = SDL_abs((int)actualR - expectedR);
  937. deltaG = SDL_abs((int)actualG - expectedG);
  938. deltaB = SDL_abs((int)actualB - expectedB);
  939. deltaA = SDL_abs((int)actualA - expectedA);
  940. SDLTest_AssertCheck(
  941. deltaR <= MAXIMUM_ERROR &&
  942. deltaG <= MAXIMUM_ERROR &&
  943. deltaB <= MAXIMUM_ERROR &&
  944. deltaA <= MAXIMUM_ERROR,
  945. "Checking %s %s operation results, expected %d,%d,%d,%d, got %d,%d,%d,%d",
  946. operation, mode_name,
  947. expectedR, expectedG, expectedB, expectedA, actualR, actualG, actualB, actualA);
  948. /* Clean up */
  949. SDL_DestroySurface(result);
  950. SDL_DestroyTexture(src);
  951. SDL_DestroyTexture(dst);
  952. }
  953. static void testBlendMode(int mode)
  954. {
  955. const TestRenderOperation operations[] = {
  956. TEST_RENDER_POINT,
  957. TEST_RENDER_LINE,
  958. TEST_RENDER_RECT,
  959. TEST_RENDER_COPY_XRGB,
  960. TEST_RENDER_COPY_ARGB
  961. };
  962. const SDL_PixelFormat dst_formats[] = {
  963. SDL_PIXELFORMAT_XRGB8888, SDL_PIXELFORMAT_ARGB8888
  964. };
  965. int i, j;
  966. for (i = 0; i < SDL_arraysize(operations); ++i) {
  967. for (j = 0; j < SDL_arraysize(dst_formats); ++j) {
  968. TestRenderOperation op = operations[i];
  969. if (mode < 0) {
  970. if (op != TEST_RENDER_COPY_XRGB && op != TEST_RENDER_COPY_ARGB) {
  971. /* Unsupported mode for this operation */
  972. continue;
  973. }
  974. }
  975. testBlendModeOperation(op, mode, dst_formats[j]);
  976. }
  977. }
  978. }
  979. /**
  980. * Tests render operations with blend modes
  981. */
  982. static int SDLCALL render_testBlendModes(void *arg)
  983. {
  984. testBlendMode(-1);
  985. testBlendMode(-2);
  986. testBlendMode(SDL_BLENDMODE_NONE);
  987. testBlendMode(SDL_BLENDMODE_BLEND);
  988. testBlendMode(SDL_BLENDMODE_BLEND_PREMULTIPLIED);
  989. testBlendMode(SDL_BLENDMODE_ADD);
  990. testBlendMode(SDL_BLENDMODE_ADD_PREMULTIPLIED);
  991. testBlendMode(SDL_BLENDMODE_MOD);
  992. testBlendMode(SDL_BLENDMODE_MUL);
  993. return TEST_COMPLETED;
  994. }
  995. /**
  996. * Test viewport
  997. */
  998. static int SDLCALL render_testViewport(void *arg)
  999. {
  1000. SDL_Surface *referenceSurface;
  1001. SDL_Rect viewport;
  1002. viewport.x = TESTRENDER_SCREEN_W / 3;
  1003. viewport.y = TESTRENDER_SCREEN_H / 3;
  1004. viewport.w = TESTRENDER_SCREEN_W / 2;
  1005. viewport.h = TESTRENDER_SCREEN_H / 2;
  1006. /* Create expected result */
  1007. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  1008. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1009. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  1010. /* Clear surface. */
  1011. clearScreen();
  1012. /* Set the viewport and do a fill operation */
  1013. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  1014. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1015. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1016. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  1017. /* Check to see if final image matches. */
  1018. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1019. /*
  1020. * Verify that clear ignores the viewport
  1021. */
  1022. /* Create expected result */
  1023. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_GREEN))
  1024. /* Clear surface. */
  1025. clearScreen();
  1026. /* Set the viewport and do a clear operation */
  1027. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  1028. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1029. CHECK_FUNC(SDL_RenderClear, (renderer))
  1030. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  1031. /* Check to see if final image matches. */
  1032. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1033. /* Make current */
  1034. SDL_RenderPresent(renderer);
  1035. SDL_DestroySurface(referenceSurface);
  1036. return TEST_COMPLETED;
  1037. }
  1038. static int SDLCALL render_testRGBSurfaceNoAlpha(void* arg)
  1039. {
  1040. SDL_Surface *surface;
  1041. SDL_Renderer *software_renderer;
  1042. SDL_Surface *surface2;
  1043. SDL_Texture *texture2;
  1044. bool result;
  1045. SDL_FRect dest_rect;
  1046. SDL_FPoint point;
  1047. const SDL_PixelFormatDetails *format_details;
  1048. Uint8 r, g, b, a;
  1049. SDLTest_AssertPass("About to call SDL_CreateSurface(128, 128, SDL_PIXELFORMAT_RGBX32)");
  1050. surface = SDL_CreateSurface(128, 128, SDL_PIXELFORMAT_RGBX32);
  1051. SDLTest_AssertCheck(surface != NULL, "Returned surface must be not NULL");
  1052. if (surface == NULL) {
  1053. return TEST_ABORTED;
  1054. }
  1055. SDLTest_AssertPass("About to call SDL_GetPixelFormatDetails(surface->format)");
  1056. format_details = SDL_GetPixelFormatDetails(surface->format);
  1057. SDLTest_AssertCheck(format_details != NULL, "Result must be non-NULL, is %p", format_details);
  1058. if (format_details == NULL) {
  1059. SDL_DestroySurface(surface);
  1060. return TEST_ABORTED;
  1061. }
  1062. SDLTest_AssertCheck(format_details->bits_per_pixel == 32, "format_details->bits_per_pixel is %d, should be %d", format_details->bits_per_pixel, 32);
  1063. SDLTest_AssertCheck(format_details->bytes_per_pixel == 4, "format_details->bytes_per_pixel is %d, should be %d", format_details->bytes_per_pixel, 4);
  1064. SDLTest_AssertPass("About to call SDL_CreateSoftwareRenderer(surface)");
  1065. software_renderer = SDL_CreateSoftwareRenderer(surface);
  1066. SDLTest_AssertCheck(software_renderer != NULL, "Returned renderer must be not NULL");
  1067. if (software_renderer == NULL) {
  1068. SDL_DestroySurface(surface);
  1069. return TEST_ABORTED;
  1070. }
  1071. SDLTest_AssertPass("About to call SDL_CreateSurface(16, 16, SDL_PIXELFORMAT_RGBX32)");
  1072. surface2 = SDL_CreateSurface(16, 16, SDL_PIXELFORMAT_RGBX32);
  1073. SDLTest_AssertCheck(surface2 != NULL, "Returned surface must be not NULL");
  1074. if (surface2 == NULL) {
  1075. SDL_DestroySurface(surface);
  1076. SDL_DestroyRenderer(software_renderer);
  1077. return TEST_ABORTED;
  1078. }
  1079. SDLTest_AssertPass("About to call SDL_FillRect(surface2, NULL, 0)");
  1080. result = SDL_FillSurfaceRect(surface2, NULL, SDL_MapRGB(SDL_GetPixelFormatDetails(surface2->format), NULL, 0, 0, 0));
  1081. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1082. SDLTest_AssertPass("About to call SDL_CreateTextureFromSurface(software_renderer, surface2)");
  1083. texture2 = SDL_CreateTextureFromSurface(software_renderer, surface2);
  1084. SDLTest_AssertCheck(texture2 != NULL, "Returned texture is not NULL");
  1085. SDLTest_AssertPass("About to call SDL_SetRenderDrawColor(renderer, 0xaa, 0xbb, 0xcc, 0x0)");
  1086. result = SDL_SetRenderDrawColor(software_renderer, 0xaa, 0xbb, 0xcc, 0x0);
  1087. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1088. SDLTest_AssertPass("About to call SDL_RenderClear(renderer)");
  1089. result = SDL_RenderClear(software_renderer);
  1090. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1091. SDLTest_AssertPass("About to call SDL_SetRenderDrawColor(renderer, 0x0, 0x0, 0x0, 0x0)");
  1092. result = SDL_SetRenderDrawColor(software_renderer, 0x0, 0x0, 0x0, 0x0);
  1093. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1094. dest_rect.x = 32;
  1095. dest_rect.y = 32;
  1096. dest_rect.w = (float)surface2->w;
  1097. dest_rect.h = (float)surface2->h;
  1098. point.x = 0;
  1099. point.y = 0;
  1100. SDLTest_AssertPass("About to call SDL_RenderCopy(software_renderer, texture, NULL, &{%g, %g, %g, %g})",
  1101. dest_rect.x, dest_rect.h, dest_rect.w, dest_rect.h);
  1102. result = SDL_RenderTextureRotated(software_renderer, texture2, NULL, &dest_rect, 180, &point, SDL_FLIP_NONE);
  1103. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1104. SDLTest_AssertPass("About to call SDL_RenderPresent(software_renderer)");
  1105. SDL_RenderPresent(software_renderer);
  1106. SDLTest_AssertPass("About to call SDL_ReadSurfacePixel(0, 0)");
  1107. result = SDL_ReadSurfacePixel(surface, 0, 0, &r, &g, &b, &a);
  1108. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1109. SDLTest_AssertCheck(r == 0xaa && g == 0xbb && b == 0xcc && a == SDL_ALPHA_OPAQUE,
  1110. "Pixel at (0, 0) is {0x%02x,0x%02x,0x%02x,0x%02x}, should be {0x%02x,0x%02x,0x%02x,0x%02x}",
  1111. r, g, b, a, 0xaa, 0xbb, 0xcc, SDL_ALPHA_OPAQUE);
  1112. SDLTest_AssertPass("About to call SDL_ReadSurfacePixel(15, 15)");
  1113. result = SDL_ReadSurfacePixel(surface, 15, 15, &r, &g, &b, &a);
  1114. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1115. SDLTest_AssertCheck(r == 0xaa && g == 0xbb && b == 0xcc && a == SDL_ALPHA_OPAQUE,
  1116. "Pixel at (0, 0) is {0x%02x,0x%02x,0x%02x,0x%02x}, should be {0x%02x,0x%02x,0x%02x,0x%02x}",
  1117. r, g, b, a, 0xaa, 0xbb, 0xcc, SDL_ALPHA_OPAQUE);
  1118. SDLTest_AssertPass("About to call SDL_ReadSurfacePixel(16, 16)");
  1119. result = SDL_ReadSurfacePixel(surface, 16, 16, &r, &g, &b, &a);
  1120. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1121. SDLTest_AssertCheck(r == 0x00 && g == 0x00 && b == 0x00 && a == SDL_ALPHA_OPAQUE,
  1122. "Pixel at (0, 0) is {0x%02x,0x%02x,0x%02x,0x%02x}, should be {0x%02x,0x%02x,0x%02x,0x%02x}",
  1123. r, g, b, a, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  1124. SDLTest_AssertPass("About to call SDL_ReadSurfacePixel(31, 31)");
  1125. result = SDL_ReadSurfacePixel(surface, 31, 31, &r, &g, &b, &a);
  1126. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1127. SDLTest_AssertCheck(r == 0x00 && g == 0x00 && b == 0x00 && a == SDL_ALPHA_OPAQUE,
  1128. "Pixel at (0, 0) is {0x%02x,0x%02x,0x%02x,0x%02x}, should be {0x%02x,0x%02x,0x%02x,0x%02x}",
  1129. r, g, b, a, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE);
  1130. SDLTest_AssertPass("About to call SDL_ReadSurfacePixel(32, 32)");
  1131. result = SDL_ReadSurfacePixel(surface, 32, 32, &r, &g, &b, &a);
  1132. SDLTest_AssertCheck(result == true, "Result is %d, should be %d", result, true);
  1133. SDLTest_AssertCheck(r == 0xaa && g == 0xbb && b == 0xcc && a == SDL_ALPHA_OPAQUE,
  1134. "Pixel at (0, 0) is {0x%02x,0x%02x,0x%02x,0x%02x}, should be {0x%02x,0x%02x,0x%02x,0x%02x}",
  1135. r, g, b, a, 0xaa, 0xbb, 0xcc, SDL_ALPHA_OPAQUE);
  1136. SDL_DestroyTexture(texture2);
  1137. SDL_DestroySurface(surface2);
  1138. SDL_DestroyRenderer(software_renderer);
  1139. SDL_DestroySurface(surface);
  1140. return TEST_COMPLETED;
  1141. }
  1142. /**
  1143. * Test clip rect
  1144. */
  1145. static int SDLCALL render_testClipRect(void *arg)
  1146. {
  1147. SDL_Surface *referenceSurface;
  1148. SDL_Rect cliprect;
  1149. cliprect.x = TESTRENDER_SCREEN_W / 3;
  1150. cliprect.y = TESTRENDER_SCREEN_H / 3;
  1151. cliprect.w = TESTRENDER_SCREEN_W / 2;
  1152. cliprect.h = TESTRENDER_SCREEN_H / 2;
  1153. /* Create expected result */
  1154. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  1155. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1156. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &cliprect, RENDER_COLOR_GREEN))
  1157. /* Clear surface. */
  1158. clearScreen();
  1159. /* Set the cliprect and do a fill operation */
  1160. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, &cliprect))
  1161. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1162. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1163. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, NULL))
  1164. /* Check to see if final image matches. */
  1165. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1166. /*
  1167. * Verify that clear ignores the cliprect
  1168. */
  1169. /* Create expected result */
  1170. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_GREEN))
  1171. /* Clear surface. */
  1172. clearScreen();
  1173. /* Set the cliprect and do a clear operation */
  1174. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, &cliprect))
  1175. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1176. CHECK_FUNC(SDL_RenderClear, (renderer))
  1177. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, NULL))
  1178. /* Check to see if final image matches. */
  1179. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1180. /* Make current */
  1181. SDL_RenderPresent(renderer);
  1182. SDL_DestroySurface(referenceSurface);
  1183. return TEST_COMPLETED;
  1184. }
  1185. /**
  1186. * Test logical size
  1187. */
  1188. static int SDLCALL render_testLogicalSize(void *arg)
  1189. {
  1190. SDL_Surface *referenceSurface;
  1191. SDL_Rect viewport;
  1192. SDL_FRect rect;
  1193. int w = 0, h = 0;
  1194. int set_w, set_h;
  1195. SDL_RendererLogicalPresentation set_presentation_mode;
  1196. SDL_FRect set_rect;
  1197. const int factor = 2;
  1198. SDL_GetWindowSize(window, &w, &h);
  1199. if (w != TESTRENDER_WINDOW_W || h != TESTRENDER_WINDOW_H) {
  1200. SDLTest_Log("Skipping test render_testLogicalSize: expected window %dx%d, got %dx%d", TESTRENDER_WINDOW_W, TESTRENDER_WINDOW_H, w, h);
  1201. return TEST_SKIPPED;
  1202. }
  1203. viewport.x = ((TESTRENDER_SCREEN_W / 4) / factor) * factor;
  1204. viewport.y = ((TESTRENDER_SCREEN_H / 4) / factor) * factor;
  1205. viewport.w = ((TESTRENDER_SCREEN_W / 2) / factor) * factor;
  1206. viewport.h = ((TESTRENDER_SCREEN_H / 2) / factor) * factor;
  1207. /* Create expected result */
  1208. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  1209. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1210. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  1211. /* Clear surface. */
  1212. clearScreen();
  1213. /* Set the logical size and do a fill operation */
  1214. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  1215. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, w / factor, h / factor, SDL_LOGICAL_PRESENTATION_LETTERBOX))
  1216. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1217. SDLTest_AssertCheck(
  1218. set_w == (w / factor) &&
  1219. set_h == (h / factor) &&
  1220. set_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX,
  1221. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1222. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1223. SDLTest_AssertCheck(
  1224. set_rect.x == 0.0f &&
  1225. set_rect.y == 0.0f &&
  1226. set_rect.w == (float)w &&
  1227. set_rect.h == (float)h,
  1228. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1229. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1230. rect.x = (float)viewport.x / factor;
  1231. rect.y = (float)viewport.y / factor;
  1232. rect.w = (float)viewport.w / factor;
  1233. rect.h = (float)viewport.h / factor;
  1234. CHECK_FUNC(SDL_RenderFillRect, (renderer, &rect))
  1235. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  1236. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1237. SDLTest_AssertCheck(
  1238. set_w == 0 &&
  1239. set_h == 0 &&
  1240. set_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED,
  1241. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1242. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1243. SDLTest_AssertCheck(
  1244. set_rect.x == 0.0f &&
  1245. set_rect.y == 0.0f &&
  1246. set_rect.w == (float)w &&
  1247. set_rect.h == (float)h,
  1248. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1249. /* Check to see if final image matches. */
  1250. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1251. /* Clear surface. */
  1252. clearScreen();
  1253. /* Set the logical size and viewport and do a fill operation */
  1254. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  1255. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, w / factor, h / factor, SDL_LOGICAL_PRESENTATION_LETTERBOX))
  1256. viewport.x = (TESTRENDER_SCREEN_W / 4) / factor;
  1257. viewport.y = (TESTRENDER_SCREEN_H / 4) / factor;
  1258. viewport.w = (TESTRENDER_SCREEN_W / 2) / factor;
  1259. viewport.h = (TESTRENDER_SCREEN_H / 2) / factor;
  1260. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  1261. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1262. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1263. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  1264. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  1265. /* Check to see if final image matches. */
  1266. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1267. /*
  1268. * Test a logical size that isn't the same aspect ratio as the window
  1269. */
  1270. viewport.x = (TESTRENDER_SCREEN_W / 4);
  1271. viewport.y = 0;
  1272. viewport.w = TESTRENDER_SCREEN_W;
  1273. viewport.h = TESTRENDER_SCREEN_H;
  1274. /* Create expected result */
  1275. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1276. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  1277. /* Clear surface. */
  1278. clearScreen();
  1279. /* Set the logical size and do a fill operation */
  1280. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  1281. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer,
  1282. w - 2 * (TESTRENDER_SCREEN_W / 4),
  1283. h,
  1284. SDL_LOGICAL_PRESENTATION_LETTERBOX))
  1285. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1286. SDLTest_AssertCheck(
  1287. set_w == w - 2 * (TESTRENDER_SCREEN_W / 4) &&
  1288. set_h == h &&
  1289. set_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX,
  1290. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1291. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1292. SDLTest_AssertCheck(
  1293. set_rect.x == 20.0f &&
  1294. set_rect.y == 0.0f &&
  1295. set_rect.w == 280.0f &&
  1296. set_rect.h == 240.0f,
  1297. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1298. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1299. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1300. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  1301. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1302. SDLTest_AssertCheck(
  1303. set_w == 0 &&
  1304. set_h == 0 &&
  1305. set_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED,
  1306. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1307. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1308. SDLTest_AssertCheck(
  1309. set_rect.x == 0.0f &&
  1310. set_rect.y == 0.0f &&
  1311. set_rect.w == 320.0f &&
  1312. set_rect.h == 240.0f,
  1313. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1314. /* Check to see if final image matches. */
  1315. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1316. /* Clear surface. */
  1317. clearScreen();
  1318. /* Make current */
  1319. SDL_RenderPresent(renderer);
  1320. SDL_DestroySurface(referenceSurface);
  1321. return TEST_COMPLETED;
  1322. }
  1323. /**
  1324. * @brief Tests setting and getting texture scale mode.
  1325. *
  1326. * \sa
  1327. * http://wiki.libsdl.org/SDL2/SDL_SetTextureScaleMode
  1328. * http://wiki.libsdl.org/SDL2/SDL_GetTextureScaleMode
  1329. */
  1330. static int SDLCALL render_testGetSetTextureScaleMode(void *arg)
  1331. {
  1332. const struct {
  1333. const char *name;
  1334. SDL_ScaleMode mode;
  1335. } modes[] = {
  1336. { "SDL_SCALEMODE_NEAREST", SDL_SCALEMODE_NEAREST },
  1337. { "SDL_SCALEMODE_LINEAR", SDL_SCALEMODE_LINEAR },
  1338. { "SDL_SCALEMODE_PIXELART", SDL_SCALEMODE_PIXELART },
  1339. };
  1340. size_t i;
  1341. for (i = 0; i < SDL_arraysize(modes); i++) {
  1342. SDL_Texture *texture;
  1343. bool result;
  1344. SDL_ScaleMode actual_mode = SDL_SCALEMODE_NEAREST;
  1345. SDL_ClearError();
  1346. SDLTest_AssertPass("About to call SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 16, 16)");
  1347. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 16, 16);
  1348. SDLTest_AssertCheck(texture != NULL, "SDL_CreateTexture must return a non-NULL texture");
  1349. SDLTest_AssertPass("About to call SDL_SetTextureScaleMode(texture, %s)", modes[i].name);
  1350. result = SDL_SetTextureScaleMode(texture, modes[i].mode);
  1351. SDLTest_AssertCheck(result == true, "SDL_SetTextureScaleMode returns %d, expected %d", result, true);
  1352. SDLTest_AssertPass("About to call SDL_GetTextureScaleMode(texture)");
  1353. result = SDL_GetTextureScaleMode(texture, &actual_mode);
  1354. SDLTest_AssertCheck(result == true, "SDL_SetTextureScaleMode returns %d, expected %d", result, true);
  1355. SDLTest_AssertCheck(actual_mode == modes[i].mode, "SDL_GetTextureScaleMode must return %s (%d), actual=%d",
  1356. modes[i].name, modes[i].mode, actual_mode);
  1357. }
  1358. return TEST_COMPLETED;
  1359. }
  1360. /* Helper functions */
  1361. /**
  1362. * Checks to see if functionality is supported. Helper function.
  1363. */
  1364. static bool isSupported(int code)
  1365. {
  1366. return (code != false);
  1367. }
  1368. /**
  1369. * Test to see if we can vary the draw color. Helper function.
  1370. *
  1371. * \sa SDL_SetRenderDrawColor
  1372. * \sa SDL_GetRenderDrawColor
  1373. */
  1374. static bool hasDrawColor(void)
  1375. {
  1376. int ret, fail;
  1377. Uint8 r, g, b, a;
  1378. fail = 0;
  1379. /* Set color. */
  1380. ret = SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100);
  1381. if (!isSupported(ret)) {
  1382. fail = 1;
  1383. }
  1384. ret = SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
  1385. if (!isSupported(ret)) {
  1386. fail = 1;
  1387. }
  1388. /* Restore natural. */
  1389. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  1390. if (!isSupported(ret)) {
  1391. fail = 1;
  1392. }
  1393. /* Something failed, consider not available. */
  1394. if (fail) {
  1395. return false;
  1396. }
  1397. /* Not set properly, consider failed. */
  1398. else if ((r != 100) || (g != 100) || (b != 100) || (a != 100)) {
  1399. return false;
  1400. }
  1401. return true;
  1402. }
  1403. /**
  1404. * Loads the test image 'Face' as texture. Helper function.
  1405. *
  1406. * \sa SDL_CreateTextureFromSurface
  1407. */
  1408. static SDL_Texture *
  1409. loadTestFace(void)
  1410. {
  1411. SDL_Surface *face;
  1412. SDL_Texture *tface;
  1413. face = SDLTest_ImageFace();
  1414. if (!face) {
  1415. return NULL;
  1416. }
  1417. tface = SDL_CreateTextureFromSurface(renderer, face);
  1418. if (!tface) {
  1419. SDLTest_LogError("SDL_CreateTextureFromSurface() failed with error: %s", SDL_GetError());
  1420. }
  1421. SDL_DestroySurface(face);
  1422. return tface;
  1423. }
  1424. /**
  1425. * Compares screen pixels with image pixels. Helper function.
  1426. *
  1427. * \param referenceSurface Image to compare against.
  1428. * \param allowable_error allowed difference from the reference image
  1429. *
  1430. * \sa SDL_RenderReadPixels
  1431. * \sa SDL_CreateSurfaceFrom
  1432. * \sa SDL_DestroySurface
  1433. */
  1434. static void compare(SDL_Surface *referenceSurface, int allowable_error)
  1435. {
  1436. int ret;
  1437. SDL_Rect rect;
  1438. SDL_Surface *surface, *testSurface;
  1439. /* Explicitly specify the rect in case the window isn't the expected size... */
  1440. rect.x = 0;
  1441. rect.y = 0;
  1442. rect.w = TESTRENDER_SCREEN_W;
  1443. rect.h = TESTRENDER_SCREEN_H;
  1444. surface = SDL_RenderReadPixels(renderer, &rect);
  1445. if (!surface) {
  1446. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1447. return;
  1448. }
  1449. testSurface = SDL_ConvertSurface(surface, RENDER_COMPARE_FORMAT);
  1450. SDL_DestroySurface(surface);
  1451. if (!testSurface) {
  1452. SDLTest_AssertCheck(testSurface != NULL, "Validate result from SDL_ConvertSurface, got NULL, %s", SDL_GetError());
  1453. return;
  1454. }
  1455. /* Compare surface. */
  1456. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  1457. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1458. /* Clean up. */
  1459. SDL_DestroySurface(testSurface);
  1460. }
  1461. static void compare2x(SDL_Surface *referenceSurface, int allowable_error)
  1462. {
  1463. int ret;
  1464. SDL_Rect rect;
  1465. SDL_Surface *surface, *testSurface;
  1466. /* Explicitly specify the rect in case the window isn't the expected size... */
  1467. rect.x = 0;
  1468. rect.y = 0;
  1469. rect.w = TESTRENDER_SCREEN_W * 2;
  1470. rect.h = TESTRENDER_SCREEN_H * 2;
  1471. surface = SDL_RenderReadPixels(renderer, &rect);
  1472. if (!surface) {
  1473. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1474. return;
  1475. }
  1476. testSurface = SDL_ConvertSurface(surface, RENDER_COMPARE_FORMAT);
  1477. SDL_DestroySurface(surface);
  1478. if (!testSurface) {
  1479. SDLTest_AssertCheck(testSurface != NULL, "Validate result from SDL_ConvertSurface, got NULL, %s", SDL_GetError());
  1480. return;
  1481. }
  1482. /* Compare surface. */
  1483. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  1484. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1485. /* Clean up. */
  1486. SDL_DestroySurface(testSurface);
  1487. }
  1488. /**
  1489. * Clears the screen. Helper function.
  1490. *
  1491. * \sa SDL_SetRenderDrawColor
  1492. * \sa SDL_RenderClear
  1493. * \sa SDL_RenderPresent
  1494. * \sa SDL_SetRenderDrawBlendMode
  1495. */
  1496. static int
  1497. clearScreen(void)
  1498. {
  1499. int ret;
  1500. /* Make current */
  1501. SDL_RenderPresent(renderer);
  1502. /* Set color. */
  1503. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  1504. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawColor, expected: true, got: %i", ret);
  1505. /* Clear screen. */
  1506. ret = SDL_RenderClear(renderer);
  1507. SDLTest_AssertCheck(ret == true, "Validate result from SDL_RenderClear, expected: true, got: %i", ret);
  1508. /* Set defaults. */
  1509. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  1510. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawBlendMode, expected: true, got: %i", ret);
  1511. ret = SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
  1512. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawColor, expected: true, got: %i", ret);
  1513. return 0;
  1514. }
  1515. /**
  1516. * Tests geometry UV clamping
  1517. */
  1518. static int SDLCALL render_testUVClamping(void *arg)
  1519. {
  1520. SDL_Vertex vertices[6];
  1521. SDL_Vertex *verts = vertices;
  1522. SDL_FColor color = { 1.0f, 1.0f, 1.0f, 1.0f };
  1523. SDL_FRect rect;
  1524. float min_U = -0.5f;
  1525. float max_U = 1.5f;
  1526. float min_V = -0.5f;
  1527. float max_V = 1.5f;
  1528. SDL_Texture *tface;
  1529. SDL_Surface *referenceSurface = NULL;
  1530. /* Clear surface. */
  1531. clearScreen();
  1532. /* Create face surface. */
  1533. tface = loadTestFace();
  1534. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  1535. if (tface == NULL) {
  1536. return TEST_ABORTED;
  1537. }
  1538. rect.w = (float)tface->w * 2;
  1539. rect.h = (float)tface->h * 2;
  1540. rect.x = (TESTRENDER_SCREEN_W - rect.w) / 2;
  1541. rect.y = (TESTRENDER_SCREEN_H - rect.h) / 2;
  1542. /*
  1543. * 0--1
  1544. * | /|
  1545. * |/ |
  1546. * 3--2
  1547. *
  1548. * Draw sprite2 as triangles that can be recombined as rect by software renderer
  1549. */
  1550. /* 0 */
  1551. verts->position.x = rect.x;
  1552. verts->position.y = rect.y;
  1553. verts->color = color;
  1554. verts->tex_coord.x = min_U;
  1555. verts->tex_coord.y = min_V;
  1556. verts++;
  1557. /* 1 */
  1558. verts->position.x = rect.x + rect.w;
  1559. verts->position.y = rect.y;
  1560. verts->color = color;
  1561. verts->tex_coord.x = max_U;
  1562. verts->tex_coord.y = min_V;
  1563. verts++;
  1564. /* 2 */
  1565. verts->position.x = rect.x + rect.w;
  1566. verts->position.y = rect.y + rect.h;
  1567. verts->color = color;
  1568. verts->tex_coord.x = max_U;
  1569. verts->tex_coord.y = max_V;
  1570. verts++;
  1571. /* 0 */
  1572. verts->position.x = rect.x;
  1573. verts->position.y = rect.y;
  1574. verts->color = color;
  1575. verts->tex_coord.x = min_U;
  1576. verts->tex_coord.y = min_V;
  1577. verts++;
  1578. /* 2 */
  1579. verts->position.x = rect.x + rect.w;
  1580. verts->position.y = rect.y + rect.h;
  1581. verts->color = color;
  1582. verts->tex_coord.x = max_U;
  1583. verts->tex_coord.y = max_V;
  1584. verts++;
  1585. /* 3 */
  1586. verts->position.x = rect.x;
  1587. verts->position.y = rect.y + rect.h;
  1588. verts->color = color;
  1589. verts->tex_coord.x = min_U;
  1590. verts->tex_coord.y = max_V;
  1591. verts++;
  1592. /* Set texture address mode to clamp */
  1593. SDL_SetRenderTextureAddressMode(renderer, SDL_TEXTURE_ADDRESS_CLAMP, SDL_TEXTURE_ADDRESS_CLAMP);
  1594. /* Blit sprites as triangles onto the screen */
  1595. SDL_RenderGeometry(renderer, tface, vertices, 6, NULL, 0);
  1596. /* See if it's the same */
  1597. referenceSurface = SDLTest_ImageClampedSprite();
  1598. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1599. /* Make current */
  1600. SDL_RenderPresent(renderer);
  1601. /* Clean up. */
  1602. SDL_DestroyTexture(tface);
  1603. SDL_DestroySurface(referenceSurface);
  1604. referenceSurface = NULL;
  1605. return TEST_COMPLETED;
  1606. }
  1607. /**
  1608. * Tests geometry UV wrapping
  1609. */
  1610. static int SDLCALL render_testUVWrapping(void *arg)
  1611. {
  1612. SDL_Vertex vertices[6];
  1613. SDL_Vertex *verts = vertices;
  1614. SDL_FColor color = { 1.0f, 1.0f, 1.0f, 1.0f };
  1615. SDL_FRect rect;
  1616. float min_U = -0.5f;
  1617. float max_U = 1.5f;
  1618. float min_V = -0.5f;
  1619. float max_V = 1.5f;
  1620. SDL_Texture *tface;
  1621. SDL_Surface *referenceSurface = NULL;
  1622. /* Clear surface. */
  1623. clearScreen();
  1624. /* Create face surface. */
  1625. tface = loadTestFace();
  1626. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  1627. if (tface == NULL) {
  1628. return TEST_ABORTED;
  1629. }
  1630. rect.w = (float)tface->w * 2;
  1631. rect.h = (float)tface->h * 2;
  1632. rect.x = (TESTRENDER_SCREEN_W - rect.w) / 2;
  1633. rect.y = (TESTRENDER_SCREEN_H - rect.h) / 2;
  1634. /*
  1635. * 0--1
  1636. * | /|
  1637. * |/ |
  1638. * 3--2
  1639. *
  1640. * Draw sprite2 as triangles that can be recombined as rect by software renderer
  1641. */
  1642. /* 0 */
  1643. verts->position.x = rect.x;
  1644. verts->position.y = rect.y;
  1645. verts->color = color;
  1646. verts->tex_coord.x = min_U;
  1647. verts->tex_coord.y = min_V;
  1648. verts++;
  1649. /* 1 */
  1650. verts->position.x = rect.x + rect.w;
  1651. verts->position.y = rect.y;
  1652. verts->color = color;
  1653. verts->tex_coord.x = max_U;
  1654. verts->tex_coord.y = min_V;
  1655. verts++;
  1656. /* 2 */
  1657. verts->position.x = rect.x + rect.w;
  1658. verts->position.y = rect.y + rect.h;
  1659. verts->color = color;
  1660. verts->tex_coord.x = max_U;
  1661. verts->tex_coord.y = max_V;
  1662. verts++;
  1663. /* 0 */
  1664. verts->position.x = rect.x;
  1665. verts->position.y = rect.y;
  1666. verts->color = color;
  1667. verts->tex_coord.x = min_U;
  1668. verts->tex_coord.y = min_V;
  1669. verts++;
  1670. /* 2 */
  1671. verts->position.x = rect.x + rect.w;
  1672. verts->position.y = rect.y + rect.h;
  1673. verts->color = color;
  1674. verts->tex_coord.x = max_U;
  1675. verts->tex_coord.y = max_V;
  1676. verts++;
  1677. /* 3 */
  1678. verts->position.x = rect.x;
  1679. verts->position.y = rect.y + rect.h;
  1680. verts->color = color;
  1681. verts->tex_coord.x = min_U;
  1682. verts->tex_coord.y = max_V;
  1683. verts++;
  1684. /* Blit sprites as triangles onto the screen */
  1685. SDL_RenderGeometry(renderer, tface, vertices, 6, NULL, 0);
  1686. /* See if it's the same */
  1687. referenceSurface = SDLTest_ImageWrappingSprite();
  1688. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1689. /* Make current */
  1690. SDL_RenderPresent(renderer);
  1691. /* Clean up. */
  1692. SDL_DestroyTexture(tface);
  1693. SDL_DestroySurface(referenceSurface);
  1694. referenceSurface = NULL;
  1695. return TEST_COMPLETED;
  1696. }
  1697. /**
  1698. * Tests texture state changes
  1699. */
  1700. static int SDLCALL render_testTextureState(void *arg)
  1701. {
  1702. const Uint8 pixels[8] = {
  1703. 0x00, 0x00, 0x00, 0xFF,
  1704. 0xFF, 0xFF, 0xFF, 0xFF
  1705. };
  1706. const SDL_Color expected[] = {
  1707. /* Step 0: plain copy */
  1708. { 0x00, 0x00, 0x00, 0xFF },
  1709. { 0xFF, 0xFF, 0xFF, 0xFF },
  1710. /* Step 1: color mod to red */
  1711. { 0x00, 0x00, 0x00, 0xFF },
  1712. { 0xFF, 0x00, 0x00, 0xFF },
  1713. /* Step 2: alpha mod to 128 (cleared to green) */
  1714. { 0x00, 0x7F, 0x00, 0xFF },
  1715. { 0x80, 0xFF, 0x80, 0xFF },
  1716. /* Step 3: nearest stretch */
  1717. { 0xFF, 0xFF, 0xFF, 0xFF },
  1718. { 0x00, 0xFF, 0x00, 0xFF },
  1719. /* Step 4: linear stretch */
  1720. { 0x80, 0x80, 0x80, 0xFF },
  1721. { 0x00, 0xFF, 0x00, 0xFF },
  1722. };
  1723. SDL_Texture *texture;
  1724. SDL_Rect rect;
  1725. SDL_FRect dst;
  1726. int i;
  1727. /* Clear surface to green */
  1728. SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE);
  1729. SDL_RenderClear(renderer);
  1730. /* Create 2-pixel surface. */
  1731. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 2, 1);
  1732. SDLTest_AssertCheck(texture != NULL, "Verify SDL_CreateTexture() result");
  1733. if (texture == NULL) {
  1734. return TEST_ABORTED;
  1735. }
  1736. SDL_UpdateTexture(texture, NULL, pixels, sizeof(pixels));
  1737. dst.x = 0.0f;
  1738. dst.y = 0.0f;
  1739. dst.w = 2.0f;
  1740. dst.h = 1.0f;
  1741. /* Step 0: plain copy */
  1742. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1743. dst.y += 1;
  1744. /* Step 1: color mod to red */
  1745. SDL_SetTextureColorMod(texture, 0xFF, 0x00, 0x00);
  1746. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1747. SDL_SetTextureColorMod(texture, 0xFF, 0xFF, 0xFF);
  1748. dst.y += 1;
  1749. /* Step 2: alpha mod to 128 */
  1750. SDL_SetTextureAlphaMod(texture, 0x80);
  1751. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1752. SDL_SetTextureAlphaMod(texture, 0xFF);
  1753. dst.y += 1;
  1754. /* Step 3: nearest stretch */
  1755. dst.w = 1;
  1756. SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  1757. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1758. dst.y += 1;
  1759. /* Step 4: linear stretch */
  1760. dst.w = 1;
  1761. SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_LINEAR);
  1762. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1763. dst.y += 1;
  1764. /* Verify results */
  1765. rect.x = 0;
  1766. rect.y = 0;
  1767. rect.w = 2;
  1768. rect.h = 1;
  1769. for (i = 0; i < SDL_arraysize(expected); ) {
  1770. const int MAX_DELTA = 1;
  1771. SDL_Color actual;
  1772. int deltaR, deltaG, deltaB, deltaA;
  1773. SDL_Surface *surface = SDL_RenderReadPixels(renderer, &rect);
  1774. SDL_ReadSurfacePixel(surface, 0, 0, &actual.r, &actual.g, &actual.b, &actual.a);
  1775. deltaR = (actual.r - expected[i].r);
  1776. deltaG = (actual.g - expected[i].g);
  1777. deltaB = (actual.b - expected[i].b);
  1778. deltaA = (actual.a - expected[i].a);
  1779. SDLTest_AssertCheck(SDL_abs(deltaR) <= MAX_DELTA &&
  1780. SDL_abs(deltaG) <= MAX_DELTA &&
  1781. SDL_abs(deltaB) <= MAX_DELTA &&
  1782. SDL_abs(deltaA) <= MAX_DELTA,
  1783. "Validate left pixel at step %d, expected %d,%d,%d,%d, got %d,%d,%d,%d", i/2,
  1784. expected[i].r, expected[i].g, expected[i].b, expected[i].a,
  1785. actual.r, actual.g, actual.b, actual.a);
  1786. ++i;
  1787. SDL_ReadSurfacePixel(surface, 1, 0, &actual.r, &actual.g, &actual.b, &actual.a);
  1788. deltaR = (actual.r - expected[i].r);
  1789. deltaG = (actual.g - expected[i].g);
  1790. deltaB = (actual.b - expected[i].b);
  1791. deltaA = (actual.a - expected[i].a);
  1792. SDLTest_AssertCheck(SDL_abs(deltaR) <= MAX_DELTA &&
  1793. SDL_abs(deltaG) <= MAX_DELTA &&
  1794. SDL_abs(deltaB) <= MAX_DELTA &&
  1795. SDL_abs(deltaA) <= MAX_DELTA,
  1796. "Validate right pixel at step %d, expected %d,%d,%d,%d, got %d,%d,%d,%d", i/2,
  1797. expected[i].r, expected[i].g, expected[i].b, expected[i].a,
  1798. actual.r, actual.g, actual.b, actual.a);
  1799. ++i;
  1800. SDL_DestroySurface(surface);
  1801. rect.y += 1;
  1802. }
  1803. /* Clean up. */
  1804. SDL_DestroyTexture(texture);
  1805. return TEST_COMPLETED;
  1806. }
  1807. static void CheckUniformColor(float expected)
  1808. {
  1809. SDL_Surface *surface = SDL_RenderReadPixels(renderer, NULL);
  1810. if (surface) {
  1811. const float epsilon = 0.001f;
  1812. float r, g, b, a;
  1813. CHECK_FUNC(SDL_ReadSurfacePixelFloat, (surface, 0, 0, &r, &g, &b, &a));
  1814. SDLTest_AssertCheck(
  1815. SDL_fabs(r - expected) <= epsilon &&
  1816. SDL_fabs(g - expected) <= epsilon &&
  1817. SDL_fabs(b - expected) <= epsilon &&
  1818. a == 1.0f,
  1819. "Check color, expected %g,%g,%g,%g, got %g,%g,%g,%g",
  1820. expected, expected, expected, 1.0f, r, g, b, a);
  1821. SDL_DestroySurface(surface);
  1822. } else {
  1823. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1824. }
  1825. }
  1826. /**
  1827. * Tests colorspace support (sRGB -> linear)
  1828. */
  1829. static int SDLCALL render_testColorspaceLinear(void *arg)
  1830. {
  1831. SDL_PropertiesID props;
  1832. SDL_Texture *texture;
  1833. Uint32 pixel = 0xFF404040;
  1834. SDL_DestroyRenderer(renderer);
  1835. props = SDL_CreateProperties();
  1836. #ifdef SDL_PLATFORM_EMSCRIPTEN
  1837. // Something about falling back to the software renderer and failing causes window surface updates to fail in video_getWindowSurface()
  1838. // Adding this as a workaround to prevent software fallback until we figure this out.
  1839. SDL_SetStringProperty(props, SDL_PROP_RENDERER_CREATE_NAME_STRING, "opengles2");
  1840. #endif
  1841. SDL_SetPointerProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window);
  1842. SDL_SetNumberProperty(props, SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB_LINEAR);
  1843. renderer = SDL_CreateRendererWithProperties(props);
  1844. SDL_DestroyProperties(props);
  1845. if (!renderer) {
  1846. SDLTest_Log("Skipping test render_testColorspaceLinear, couldn't create a linear colorspace renderer");
  1847. return TEST_SKIPPED;
  1848. }
  1849. /* Verify conversion between sRGB and linear colorspaces */
  1850. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 1, 1);
  1851. SDLTest_AssertPass("Create texture");
  1852. SDLTest_AssertCheck(texture != NULL, "Check SDL_CreateTexture result");
  1853. CHECK_FUNC(SDL_UpdateTexture, (texture, NULL, &pixel, sizeof(pixel)));
  1854. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1855. CHECK_FUNC(SDL_RenderClear, (renderer));
  1856. CheckUniformColor(0.0f);
  1857. SDLTest_AssertPass("Checking sRGB clear 0x40");
  1858. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1859. CHECK_FUNC(SDL_RenderClear, (renderer));
  1860. CheckUniformColor(0.0512695f);
  1861. /* Clear target to 0 */
  1862. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1863. CHECK_FUNC(SDL_RenderClear, (renderer));
  1864. SDLTest_AssertPass("Checking sRGB draw 0x40");
  1865. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1866. CHECK_FUNC(SDL_RenderPoint, (renderer, 0.0f, 0.0f));
  1867. CheckUniformColor(0.0512695f);
  1868. /* Clear target to 0 */
  1869. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1870. CHECK_FUNC(SDL_RenderClear, (renderer));
  1871. SDLTest_AssertPass("Checking sRGB texture 0x40");
  1872. CHECK_FUNC(SDL_RenderTexture, (renderer, texture, NULL, NULL));
  1873. CheckUniformColor(0.0512695f);
  1874. /* Clear target to 0 */
  1875. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1876. CHECK_FUNC(SDL_RenderClear, (renderer));
  1877. SDL_SetRenderColorScale(renderer, 2.0f);
  1878. SDLTest_AssertPass("Checking sRGB clear 0x40 with color scale 2.0");
  1879. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1880. CHECK_FUNC(SDL_RenderClear, (renderer));
  1881. CheckUniformColor(0.102478f);
  1882. /* Clear target to 0 */
  1883. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1884. CHECK_FUNC(SDL_RenderClear, (renderer));
  1885. SDLTest_AssertPass("Checking sRGB draw 0x40 with color scale 2.0f");
  1886. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1887. CHECK_FUNC(SDL_RenderPoint, (renderer, 0.0f, 0.0f));
  1888. CheckUniformColor(0.102478f);
  1889. /* Clear target to 0 */
  1890. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1891. CHECK_FUNC(SDL_RenderClear, (renderer));
  1892. SDLTest_AssertPass("Checking sRGB texture 0x40 with color scale 2.0f");
  1893. CHECK_FUNC(SDL_RenderTexture, (renderer, texture, NULL, NULL));
  1894. CheckUniformColor(0.102478f);
  1895. SDL_DestroyTexture(texture);
  1896. return TEST_COMPLETED;
  1897. }
  1898. /**
  1899. * Tests colorspace support (linear -> sRGB)
  1900. */
  1901. static int SDLCALL render_testColorspaceSRGB(void *arg)
  1902. {
  1903. bool supports_float_textures;
  1904. const SDL_PixelFormat *texture_formats;
  1905. int i;
  1906. SDL_Texture *texture;
  1907. float pixel[4] = { 0.25f, 0.25f, 0.25f, 1.0f };
  1908. supports_float_textures = false;
  1909. texture_formats = (const SDL_PixelFormat *)SDL_GetPointerProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_TEXTURE_FORMATS_POINTER, NULL);
  1910. if (texture_formats) {
  1911. for (i = 0; texture_formats[i] != SDL_PIXELFORMAT_UNKNOWN; ++i) {
  1912. if (SDL_ISPIXELFORMAT_FLOAT(texture_formats[i])) {
  1913. supports_float_textures = true;
  1914. break;
  1915. }
  1916. }
  1917. }
  1918. if (!supports_float_textures) {
  1919. SDLTest_Log("Skipping test render_testColorspaceSRGB, renderer doesn't support float textures");
  1920. return TEST_SKIPPED;
  1921. }
  1922. /* Verify conversion between sRGB and linear colorspaces */
  1923. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA128_FLOAT, SDL_TEXTUREACCESS_STATIC, 1, 1);
  1924. SDLTest_AssertPass("Create texture");
  1925. SDLTest_AssertCheck(texture != NULL, "Check SDL_CreateTexture result");
  1926. CHECK_FUNC(SDL_UpdateTexture, (texture, NULL, &pixel, sizeof(pixel)));
  1927. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1928. CHECK_FUNC(SDL_RenderClear, (renderer));
  1929. CheckUniformColor(0.0f);
  1930. SDLTest_AssertPass("Checking sRGB clear 0x40");
  1931. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1932. CHECK_FUNC(SDL_RenderClear, (renderer));
  1933. CheckUniformColor(0.25098f);
  1934. /* Clear target to 0 */
  1935. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1936. CHECK_FUNC(SDL_RenderClear, (renderer));
  1937. SDLTest_AssertPass("Checking sRGB draw 0x40");
  1938. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1939. CHECK_FUNC(SDL_RenderPoint, (renderer, 0.0f, 0.0f));
  1940. CheckUniformColor(0.25098f);
  1941. /* Clear target to 0 */
  1942. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1943. CHECK_FUNC(SDL_RenderClear, (renderer));
  1944. SDLTest_AssertPass("Checking linear texture 0.25");
  1945. CHECK_FUNC(SDL_RenderTexture, (renderer, texture, NULL, NULL));
  1946. CheckUniformColor(0.537255f);
  1947. /* Clear target to 0 */
  1948. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1949. CHECK_FUNC(SDL_RenderClear, (renderer));
  1950. SDL_SetRenderColorScale(renderer, 2.0f);
  1951. SDLTest_AssertPass("Checking sRGB clear 0x40 with color scale 2.0");
  1952. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1953. CHECK_FUNC(SDL_RenderClear, (renderer));
  1954. CheckUniformColor(0.501961f);
  1955. /* Clear target to 0 */
  1956. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1957. CHECK_FUNC(SDL_RenderClear, (renderer));
  1958. SDLTest_AssertPass("Checking sRGB draw 0x40 with color scale 2.0f");
  1959. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1960. CHECK_FUNC(SDL_RenderPoint, (renderer, 0.0f, 0.0f));
  1961. CheckUniformColor(0.501961f);
  1962. /* Clear target to 0 */
  1963. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1964. CHECK_FUNC(SDL_RenderClear, (renderer));
  1965. SDLTest_AssertPass("Checking linear texture 0.25 with color scale 2.0f");
  1966. CHECK_FUNC(SDL_RenderTexture, (renderer, texture, NULL, NULL));
  1967. CheckUniformColor(0.737255f);
  1968. SDL_DestroyTexture(texture);
  1969. return TEST_COMPLETED;
  1970. }
  1971. /* ================= Test References ================== */
  1972. /* Render test cases */
  1973. static const SDLTest_TestCaseReference renderTestGetNumRenderDrivers = {
  1974. render_testGetNumRenderDrivers, "render_testGetNumRenderDrivers", "Tests call to SDL_GetNumRenderDrivers", TEST_ENABLED
  1975. };
  1976. static const SDLTest_TestCaseReference renderTestPrimitives = {
  1977. render_testPrimitives, "render_testPrimitives", "Tests rendering primitives", TEST_ENABLED
  1978. };
  1979. static const SDLTest_TestCaseReference renderTestPrimitivesWithViewport = {
  1980. render_testPrimitivesWithViewport, "render_testPrimitivesWithViewport", "Tests rendering primitives within a viewport", TEST_ENABLED
  1981. };
  1982. static const SDLTest_TestCaseReference renderTestBlit = {
  1983. render_testBlit, "render_testBlit", "Tests blitting", TEST_ENABLED
  1984. };
  1985. static const SDLTest_TestCaseReference renderTestBlitTiled = {
  1986. render_testBlitTiled, "render_testBlitTiled", "Tests tiled blitting", TEST_ENABLED
  1987. };
  1988. static const SDLTest_TestCaseReference renderTestBlit9Grid = {
  1989. render_testBlit9Grid, "render_testBlit9Grid", "Tests 9-grid blitting", TEST_ENABLED
  1990. };
  1991. static const SDLTest_TestCaseReference renderTestBlit9GridTiled = {
  1992. render_testBlit9GridTiled, "render_testBlit9GridTiled", "Tests tiled 9-grid blitting", TEST_ENABLED
  1993. };
  1994. static const SDLTest_TestCaseReference renderTestBlitColor = {
  1995. render_testBlitColor, "render_testBlitColor", "Tests blitting with color", TEST_ENABLED
  1996. };
  1997. static const SDLTest_TestCaseReference renderTestBlendModes = {
  1998. render_testBlendModes, "render_testBlendModes", "Tests rendering blend modes", TEST_ENABLED
  1999. };
  2000. static const SDLTest_TestCaseReference renderTestViewport = {
  2001. render_testViewport, "render_testViewport", "Tests viewport", TEST_ENABLED
  2002. };
  2003. static const SDLTest_TestCaseReference renderTestClipRect = {
  2004. render_testClipRect, "render_testClipRect", "Tests clip rect", TEST_ENABLED
  2005. };
  2006. static const SDLTest_TestCaseReference renderTestLogicalSize = {
  2007. render_testLogicalSize, "render_testLogicalSize", "Tests logical size", TEST_ENABLED
  2008. };
  2009. static const SDLTest_TestCaseReference renderTestUVClamping = {
  2010. render_testUVClamping, "render_testUVClamping", "Tests geometry UV clamping", TEST_ENABLED
  2011. };
  2012. static const SDLTest_TestCaseReference renderTestUVWrapping = {
  2013. render_testUVWrapping, "render_testUVWrapping", "Tests geometry UV wrapping", TEST_ENABLED
  2014. };
  2015. static const SDLTest_TestCaseReference renderTestTextureState = {
  2016. render_testTextureState, "render_testTextureState", "Tests texture state changes", TEST_ENABLED
  2017. };
  2018. static const SDLTest_TestCaseReference renderTestGetSetTextureScaleMode = {
  2019. render_testGetSetTextureScaleMode, "render_testGetSetTextureScaleMode", "Tests setting/getting texture scale mode", TEST_ENABLED
  2020. };
  2021. static const SDLTest_TestCaseReference renderTestRGBSurfaceNoAlpha = {
  2022. render_testRGBSurfaceNoAlpha, "render_testRGBSurfaceNoAlpha", "Tests RGB surface with no alpha using software renderer", TEST_ENABLED
  2023. };
  2024. static const SDLTest_TestCaseReference renderTestColorspaceLinear = {
  2025. render_testColorspaceLinear, "render_testColorspaceLinear", "Tests colorspace support (sRGB -> linear)", TEST_ENABLED
  2026. };
  2027. static const SDLTest_TestCaseReference renderTestColorspaceSRGB = {
  2028. render_testColorspaceSRGB, "render_testColorspaceSRGB", "Tests colorspace support (linear -> sRGB)", TEST_ENABLED
  2029. };
  2030. /* Sequence of Render test cases */
  2031. static const SDLTest_TestCaseReference *renderTests[] = {
  2032. &renderTestGetNumRenderDrivers,
  2033. &renderTestPrimitives,
  2034. &renderTestPrimitivesWithViewport,
  2035. &renderTestBlit,
  2036. &renderTestBlitTiled,
  2037. &renderTestBlit9Grid,
  2038. &renderTestBlit9GridTiled,
  2039. &renderTestBlitColor,
  2040. &renderTestBlendModes,
  2041. &renderTestViewport,
  2042. &renderTestClipRect,
  2043. &renderTestLogicalSize,
  2044. &renderTestUVClamping,
  2045. &renderTestUVWrapping,
  2046. &renderTestTextureState,
  2047. &renderTestGetSetTextureScaleMode,
  2048. &renderTestRGBSurfaceNoAlpha,
  2049. &renderTestColorspaceLinear,
  2050. &renderTestColorspaceSRGB,
  2051. NULL
  2052. };
  2053. /* Render test suite (global) */
  2054. SDLTest_TestSuiteReference renderTestSuite = {
  2055. "Render",
  2056. InitCreateRenderer,
  2057. renderTests,
  2058. CleanupDestroyRenderer
  2059. };