testautomation_render.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430
  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 empty cliprect clips all drawing
  1168. */
  1169. /* Set the cliprect and do a fill operation */
  1170. cliprect.h = 0;
  1171. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, &cliprect))
  1172. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 255, 0, 0, SDL_ALPHA_OPAQUE))
  1173. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1174. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, NULL))
  1175. /* Check to see if final image matches. */
  1176. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1177. /*
  1178. * Verify that clear ignores the cliprect
  1179. */
  1180. /* Create expected result */
  1181. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_GREEN))
  1182. /* Clear surface. */
  1183. clearScreen();
  1184. /* Set the cliprect and do a clear operation */
  1185. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, &cliprect))
  1186. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1187. CHECK_FUNC(SDL_RenderClear, (renderer))
  1188. CHECK_FUNC(SDL_SetRenderClipRect, (renderer, NULL))
  1189. /* Check to see if final image matches. */
  1190. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1191. /* Make current */
  1192. SDL_RenderPresent(renderer);
  1193. SDL_DestroySurface(referenceSurface);
  1194. return TEST_COMPLETED;
  1195. }
  1196. /**
  1197. * Test logical size
  1198. */
  1199. static int SDLCALL render_testLogicalSize(void *arg)
  1200. {
  1201. SDL_Surface *referenceSurface;
  1202. SDL_Rect viewport;
  1203. SDL_FRect rect;
  1204. int w = 0, h = 0;
  1205. int set_w, set_h;
  1206. SDL_RendererLogicalPresentation set_presentation_mode;
  1207. SDL_FRect set_rect;
  1208. const int factor = 2;
  1209. SDL_GetWindowSize(window, &w, &h);
  1210. if (w != TESTRENDER_WINDOW_W || h != TESTRENDER_WINDOW_H) {
  1211. SDLTest_Log("Skipping test render_testLogicalSize: expected window %dx%d, got %dx%d", TESTRENDER_WINDOW_W, TESTRENDER_WINDOW_H, w, h);
  1212. return TEST_SKIPPED;
  1213. }
  1214. viewport.x = ((TESTRENDER_SCREEN_W / 4) / factor) * factor;
  1215. viewport.y = ((TESTRENDER_SCREEN_H / 4) / factor) * factor;
  1216. viewport.w = ((TESTRENDER_SCREEN_W / 2) / factor) * factor;
  1217. viewport.h = ((TESTRENDER_SCREEN_H / 2) / factor) * factor;
  1218. /* Create expected result */
  1219. referenceSurface = SDL_CreateSurface(TESTRENDER_SCREEN_W, TESTRENDER_SCREEN_H, RENDER_COMPARE_FORMAT);
  1220. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1221. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  1222. /* Clear surface. */
  1223. clearScreen();
  1224. /* Set the logical size and do a fill operation */
  1225. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  1226. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, w / factor, h / factor, SDL_LOGICAL_PRESENTATION_LETTERBOX))
  1227. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1228. SDLTest_AssertCheck(
  1229. set_w == (w / factor) &&
  1230. set_h == (h / factor) &&
  1231. set_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX,
  1232. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1233. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1234. SDLTest_AssertCheck(
  1235. set_rect.x == 0.0f &&
  1236. set_rect.y == 0.0f &&
  1237. set_rect.w == (float)w &&
  1238. set_rect.h == (float)h,
  1239. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1240. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1241. rect.x = (float)viewport.x / factor;
  1242. rect.y = (float)viewport.y / factor;
  1243. rect.w = (float)viewport.w / factor;
  1244. rect.h = (float)viewport.h / factor;
  1245. CHECK_FUNC(SDL_RenderFillRect, (renderer, &rect))
  1246. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  1247. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1248. SDLTest_AssertCheck(
  1249. set_w == 0 &&
  1250. set_h == 0 &&
  1251. set_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED,
  1252. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1253. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1254. SDLTest_AssertCheck(
  1255. set_rect.x == 0.0f &&
  1256. set_rect.y == 0.0f &&
  1257. set_rect.w == (float)w &&
  1258. set_rect.h == (float)h,
  1259. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1260. /* Check to see if final image matches. */
  1261. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1262. /* Clear surface. */
  1263. clearScreen();
  1264. /* Set the logical size and viewport and do a fill operation */
  1265. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  1266. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, w / factor, h / factor, SDL_LOGICAL_PRESENTATION_LETTERBOX))
  1267. viewport.x = (TESTRENDER_SCREEN_W / 4) / factor;
  1268. viewport.y = (TESTRENDER_SCREEN_H / 4) / factor;
  1269. viewport.w = (TESTRENDER_SCREEN_W / 2) / factor;
  1270. viewport.h = (TESTRENDER_SCREEN_H / 2) / factor;
  1271. CHECK_FUNC(SDL_SetRenderViewport, (renderer, &viewport))
  1272. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1273. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1274. CHECK_FUNC(SDL_SetRenderViewport, (renderer, NULL))
  1275. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  1276. /* Check to see if final image matches. */
  1277. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1278. /*
  1279. * Test a logical size that isn't the same aspect ratio as the window
  1280. */
  1281. viewport.x = (TESTRENDER_SCREEN_W / 4);
  1282. viewport.y = 0;
  1283. viewport.w = TESTRENDER_SCREEN_W;
  1284. viewport.h = TESTRENDER_SCREEN_H;
  1285. /* Create expected result */
  1286. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, NULL, RENDER_COLOR_CLEAR))
  1287. CHECK_FUNC(SDL_FillSurfaceRect, (referenceSurface, &viewport, RENDER_COLOR_GREEN))
  1288. /* Clear surface. */
  1289. clearScreen();
  1290. /* Set the logical size and do a fill operation */
  1291. CHECK_FUNC(SDL_GetCurrentRenderOutputSize, (renderer, &w, &h))
  1292. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer,
  1293. w - 2 * (TESTRENDER_SCREEN_W / 4),
  1294. h,
  1295. SDL_LOGICAL_PRESENTATION_LETTERBOX))
  1296. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1297. SDLTest_AssertCheck(
  1298. set_w == w - 2 * (TESTRENDER_SCREEN_W / 4) &&
  1299. set_h == h &&
  1300. set_presentation_mode == SDL_LOGICAL_PRESENTATION_LETTERBOX,
  1301. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1302. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1303. SDLTest_AssertCheck(
  1304. set_rect.x == 20.0f &&
  1305. set_rect.y == 0.0f &&
  1306. set_rect.w == 280.0f &&
  1307. set_rect.h == 240.0f,
  1308. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1309. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 255, 0, SDL_ALPHA_OPAQUE))
  1310. CHECK_FUNC(SDL_RenderFillRect, (renderer, NULL))
  1311. CHECK_FUNC(SDL_SetRenderLogicalPresentation, (renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED))
  1312. CHECK_FUNC(SDL_GetRenderLogicalPresentation, (renderer, &set_w, &set_h, &set_presentation_mode))
  1313. SDLTest_AssertCheck(
  1314. set_w == 0 &&
  1315. set_h == 0 &&
  1316. set_presentation_mode == SDL_LOGICAL_PRESENTATION_DISABLED,
  1317. "Validate result from SDL_GetRenderLogicalPresentation, got %d, %d, %d", set_w, set_h, set_presentation_mode);
  1318. CHECK_FUNC(SDL_GetRenderLogicalPresentationRect, (renderer, &set_rect))
  1319. SDLTest_AssertCheck(
  1320. set_rect.x == 0.0f &&
  1321. set_rect.y == 0.0f &&
  1322. set_rect.w == 320.0f &&
  1323. set_rect.h == 240.0f,
  1324. "Validate result from SDL_GetRenderLogicalPresentationRect, got {%g, %g, %gx%g}", set_rect.x, set_rect.y, set_rect.w, set_rect.h);
  1325. /* Check to see if final image matches. */
  1326. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1327. /* Clear surface. */
  1328. clearScreen();
  1329. /* Make current */
  1330. SDL_RenderPresent(renderer);
  1331. SDL_DestroySurface(referenceSurface);
  1332. return TEST_COMPLETED;
  1333. }
  1334. /**
  1335. * @brief Tests setting and getting texture scale mode.
  1336. *
  1337. * \sa
  1338. * http://wiki.libsdl.org/SDL2/SDL_SetTextureScaleMode
  1339. * http://wiki.libsdl.org/SDL2/SDL_GetTextureScaleMode
  1340. */
  1341. static int SDLCALL render_testGetSetTextureScaleMode(void *arg)
  1342. {
  1343. const struct {
  1344. const char *name;
  1345. SDL_ScaleMode mode;
  1346. } modes[] = {
  1347. { "SDL_SCALEMODE_NEAREST", SDL_SCALEMODE_NEAREST },
  1348. { "SDL_SCALEMODE_LINEAR", SDL_SCALEMODE_LINEAR },
  1349. { "SDL_SCALEMODE_PIXELART", SDL_SCALEMODE_PIXELART },
  1350. };
  1351. size_t i;
  1352. for (i = 0; i < SDL_arraysize(modes); i++) {
  1353. SDL_Texture *texture;
  1354. bool result;
  1355. SDL_ScaleMode actual_mode = SDL_SCALEMODE_NEAREST;
  1356. SDL_ClearError();
  1357. SDLTest_AssertPass("About to call SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 16, 16)");
  1358. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 16, 16);
  1359. SDLTest_AssertCheck(texture != NULL, "SDL_CreateTexture must return a non-NULL texture");
  1360. SDLTest_AssertPass("About to call SDL_SetTextureScaleMode(texture, %s)", modes[i].name);
  1361. result = SDL_SetTextureScaleMode(texture, modes[i].mode);
  1362. SDLTest_AssertCheck(result == true, "SDL_SetTextureScaleMode returns %d, expected %d", result, true);
  1363. SDLTest_AssertPass("About to call SDL_GetTextureScaleMode(texture)");
  1364. result = SDL_GetTextureScaleMode(texture, &actual_mode);
  1365. SDLTest_AssertCheck(result == true, "SDL_SetTextureScaleMode returns %d, expected %d", result, true);
  1366. SDLTest_AssertCheck(actual_mode == modes[i].mode, "SDL_GetTextureScaleMode must return %s (%d), actual=%d",
  1367. modes[i].name, modes[i].mode, actual_mode);
  1368. }
  1369. return TEST_COMPLETED;
  1370. }
  1371. /* Helper functions */
  1372. /**
  1373. * Checks to see if functionality is supported. Helper function.
  1374. */
  1375. static bool isSupported(int code)
  1376. {
  1377. return (code != false);
  1378. }
  1379. /**
  1380. * Test to see if we can vary the draw color. Helper function.
  1381. *
  1382. * \sa SDL_SetRenderDrawColor
  1383. * \sa SDL_GetRenderDrawColor
  1384. */
  1385. static bool hasDrawColor(void)
  1386. {
  1387. int ret, fail;
  1388. Uint8 r, g, b, a;
  1389. fail = 0;
  1390. /* Set color. */
  1391. ret = SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100);
  1392. if (!isSupported(ret)) {
  1393. fail = 1;
  1394. }
  1395. ret = SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
  1396. if (!isSupported(ret)) {
  1397. fail = 1;
  1398. }
  1399. /* Restore natural. */
  1400. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  1401. if (!isSupported(ret)) {
  1402. fail = 1;
  1403. }
  1404. /* Something failed, consider not available. */
  1405. if (fail) {
  1406. return false;
  1407. }
  1408. /* Not set properly, consider failed. */
  1409. else if ((r != 100) || (g != 100) || (b != 100) || (a != 100)) {
  1410. return false;
  1411. }
  1412. return true;
  1413. }
  1414. /**
  1415. * Loads the test image 'Face' as texture. Helper function.
  1416. *
  1417. * \sa SDL_CreateTextureFromSurface
  1418. */
  1419. static SDL_Texture *
  1420. loadTestFace(void)
  1421. {
  1422. SDL_Surface *face;
  1423. SDL_Texture *tface;
  1424. face = SDLTest_ImageFace();
  1425. if (!face) {
  1426. return NULL;
  1427. }
  1428. tface = SDL_CreateTextureFromSurface(renderer, face);
  1429. if (!tface) {
  1430. SDLTest_LogError("SDL_CreateTextureFromSurface() failed with error: %s", SDL_GetError());
  1431. }
  1432. SDL_DestroySurface(face);
  1433. return tface;
  1434. }
  1435. /**
  1436. * Compares screen pixels with image pixels. Helper function.
  1437. *
  1438. * \param referenceSurface Image to compare against.
  1439. * \param allowable_error allowed difference from the reference image
  1440. *
  1441. * \sa SDL_RenderReadPixels
  1442. * \sa SDL_CreateSurfaceFrom
  1443. * \sa SDL_DestroySurface
  1444. */
  1445. static void compare(SDL_Surface *referenceSurface, int allowable_error)
  1446. {
  1447. int ret;
  1448. SDL_Rect rect;
  1449. SDL_Surface *surface, *testSurface;
  1450. /* Explicitly specify the rect in case the window isn't the expected size... */
  1451. rect.x = 0;
  1452. rect.y = 0;
  1453. rect.w = TESTRENDER_SCREEN_W;
  1454. rect.h = TESTRENDER_SCREEN_H;
  1455. surface = SDL_RenderReadPixels(renderer, &rect);
  1456. if (!surface) {
  1457. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1458. return;
  1459. }
  1460. testSurface = SDL_ConvertSurface(surface, RENDER_COMPARE_FORMAT);
  1461. SDL_DestroySurface(surface);
  1462. if (!testSurface) {
  1463. SDLTest_AssertCheck(testSurface != NULL, "Validate result from SDL_ConvertSurface, got NULL, %s", SDL_GetError());
  1464. return;
  1465. }
  1466. /* Compare surface. */
  1467. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  1468. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1469. /* Clean up. */
  1470. SDL_DestroySurface(testSurface);
  1471. }
  1472. static void compare2x(SDL_Surface *referenceSurface, int allowable_error)
  1473. {
  1474. int ret;
  1475. SDL_Rect rect;
  1476. SDL_Surface *surface, *testSurface;
  1477. /* Explicitly specify the rect in case the window isn't the expected size... */
  1478. rect.x = 0;
  1479. rect.y = 0;
  1480. rect.w = TESTRENDER_SCREEN_W * 2;
  1481. rect.h = TESTRENDER_SCREEN_H * 2;
  1482. surface = SDL_RenderReadPixels(renderer, &rect);
  1483. if (!surface) {
  1484. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1485. return;
  1486. }
  1487. testSurface = SDL_ConvertSurface(surface, RENDER_COMPARE_FORMAT);
  1488. SDL_DestroySurface(surface);
  1489. if (!testSurface) {
  1490. SDLTest_AssertCheck(testSurface != NULL, "Validate result from SDL_ConvertSurface, got NULL, %s", SDL_GetError());
  1491. return;
  1492. }
  1493. /* Compare surface. */
  1494. ret = SDLTest_CompareSurfaces(testSurface, referenceSurface, allowable_error);
  1495. SDLTest_AssertCheck(ret == 0, "Validate result from SDLTest_CompareSurfaces, expected: 0, got: %i", ret);
  1496. /* Clean up. */
  1497. SDL_DestroySurface(testSurface);
  1498. }
  1499. /**
  1500. * Clears the screen. Helper function.
  1501. *
  1502. * \sa SDL_SetRenderDrawColor
  1503. * \sa SDL_RenderClear
  1504. * \sa SDL_RenderPresent
  1505. * \sa SDL_SetRenderDrawBlendMode
  1506. */
  1507. static int
  1508. clearScreen(void)
  1509. {
  1510. int ret;
  1511. /* Make current */
  1512. SDL_RenderPresent(renderer);
  1513. /* Set color. */
  1514. ret = SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  1515. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawColor, expected: true, got: %i", ret);
  1516. /* Clear screen. */
  1517. ret = SDL_RenderClear(renderer);
  1518. SDLTest_AssertCheck(ret == true, "Validate result from SDL_RenderClear, expected: true, got: %i", ret);
  1519. /* Set defaults. */
  1520. ret = SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  1521. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawBlendMode, expected: true, got: %i", ret);
  1522. ret = SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
  1523. SDLTest_AssertCheck(ret == true, "Validate result from SDL_SetRenderDrawColor, expected: true, got: %i", ret);
  1524. return 0;
  1525. }
  1526. /**
  1527. * Tests geometry UV clamping
  1528. */
  1529. static int SDLCALL render_testUVClamping(void *arg)
  1530. {
  1531. SDL_Vertex vertices[6];
  1532. SDL_Vertex *verts = vertices;
  1533. SDL_FColor color = { 1.0f, 1.0f, 1.0f, 1.0f };
  1534. SDL_FRect rect;
  1535. float min_U = -0.5f;
  1536. float max_U = 1.5f;
  1537. float min_V = -0.5f;
  1538. float max_V = 1.5f;
  1539. SDL_Texture *tface;
  1540. SDL_Surface *referenceSurface = NULL;
  1541. /* Clear surface. */
  1542. clearScreen();
  1543. /* Create face surface. */
  1544. tface = loadTestFace();
  1545. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  1546. if (tface == NULL) {
  1547. return TEST_ABORTED;
  1548. }
  1549. rect.w = (float)tface->w * 2;
  1550. rect.h = (float)tface->h * 2;
  1551. rect.x = (TESTRENDER_SCREEN_W - rect.w) / 2;
  1552. rect.y = (TESTRENDER_SCREEN_H - rect.h) / 2;
  1553. /*
  1554. * 0--1
  1555. * | /|
  1556. * |/ |
  1557. * 3--2
  1558. *
  1559. * Draw sprite2 as triangles that can be recombined as rect by software renderer
  1560. */
  1561. /* 0 */
  1562. verts->position.x = rect.x;
  1563. verts->position.y = rect.y;
  1564. verts->color = color;
  1565. verts->tex_coord.x = min_U;
  1566. verts->tex_coord.y = min_V;
  1567. verts++;
  1568. /* 1 */
  1569. verts->position.x = rect.x + rect.w;
  1570. verts->position.y = rect.y;
  1571. verts->color = color;
  1572. verts->tex_coord.x = max_U;
  1573. verts->tex_coord.y = min_V;
  1574. verts++;
  1575. /* 2 */
  1576. verts->position.x = rect.x + rect.w;
  1577. verts->position.y = rect.y + rect.h;
  1578. verts->color = color;
  1579. verts->tex_coord.x = max_U;
  1580. verts->tex_coord.y = max_V;
  1581. verts++;
  1582. /* 0 */
  1583. verts->position.x = rect.x;
  1584. verts->position.y = rect.y;
  1585. verts->color = color;
  1586. verts->tex_coord.x = min_U;
  1587. verts->tex_coord.y = min_V;
  1588. verts++;
  1589. /* 2 */
  1590. verts->position.x = rect.x + rect.w;
  1591. verts->position.y = rect.y + rect.h;
  1592. verts->color = color;
  1593. verts->tex_coord.x = max_U;
  1594. verts->tex_coord.y = max_V;
  1595. verts++;
  1596. /* 3 */
  1597. verts->position.x = rect.x;
  1598. verts->position.y = rect.y + rect.h;
  1599. verts->color = color;
  1600. verts->tex_coord.x = min_U;
  1601. verts->tex_coord.y = max_V;
  1602. verts++;
  1603. /* Set texture address mode to clamp */
  1604. SDL_SetRenderTextureAddressMode(renderer, SDL_TEXTURE_ADDRESS_CLAMP, SDL_TEXTURE_ADDRESS_CLAMP);
  1605. /* Blit sprites as triangles onto the screen */
  1606. SDL_RenderGeometry(renderer, tface, vertices, 6, NULL, 0);
  1607. /* See if it's the same */
  1608. referenceSurface = SDLTest_ImageClampedSprite();
  1609. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1610. /* Make current */
  1611. SDL_RenderPresent(renderer);
  1612. /* Clean up. */
  1613. SDL_DestroyTexture(tface);
  1614. SDL_DestroySurface(referenceSurface);
  1615. referenceSurface = NULL;
  1616. return TEST_COMPLETED;
  1617. }
  1618. /**
  1619. * Tests geometry UV wrapping
  1620. */
  1621. static int SDLCALL render_testUVWrapping(void *arg)
  1622. {
  1623. SDL_Vertex vertices[6];
  1624. SDL_Vertex *verts = vertices;
  1625. SDL_FColor color = { 1.0f, 1.0f, 1.0f, 1.0f };
  1626. SDL_FRect rect;
  1627. float min_U = -0.5f;
  1628. float max_U = 1.5f;
  1629. float min_V = -0.5f;
  1630. float max_V = 1.5f;
  1631. SDL_Texture *tface;
  1632. SDL_Surface *referenceSurface = NULL;
  1633. /* Clear surface. */
  1634. clearScreen();
  1635. /* Create face surface. */
  1636. tface = loadTestFace();
  1637. SDLTest_AssertCheck(tface != NULL, "Verify loadTestFace() result");
  1638. if (tface == NULL) {
  1639. return TEST_ABORTED;
  1640. }
  1641. rect.w = (float)tface->w * 2;
  1642. rect.h = (float)tface->h * 2;
  1643. rect.x = (TESTRENDER_SCREEN_W - rect.w) / 2;
  1644. rect.y = (TESTRENDER_SCREEN_H - rect.h) / 2;
  1645. /*
  1646. * 0--1
  1647. * | /|
  1648. * |/ |
  1649. * 3--2
  1650. *
  1651. * Draw sprite2 as triangles that can be recombined as rect by software renderer
  1652. */
  1653. /* 0 */
  1654. verts->position.x = rect.x;
  1655. verts->position.y = rect.y;
  1656. verts->color = color;
  1657. verts->tex_coord.x = min_U;
  1658. verts->tex_coord.y = min_V;
  1659. verts++;
  1660. /* 1 */
  1661. verts->position.x = rect.x + rect.w;
  1662. verts->position.y = rect.y;
  1663. verts->color = color;
  1664. verts->tex_coord.x = max_U;
  1665. verts->tex_coord.y = min_V;
  1666. verts++;
  1667. /* 2 */
  1668. verts->position.x = rect.x + rect.w;
  1669. verts->position.y = rect.y + rect.h;
  1670. verts->color = color;
  1671. verts->tex_coord.x = max_U;
  1672. verts->tex_coord.y = max_V;
  1673. verts++;
  1674. /* 0 */
  1675. verts->position.x = rect.x;
  1676. verts->position.y = rect.y;
  1677. verts->color = color;
  1678. verts->tex_coord.x = min_U;
  1679. verts->tex_coord.y = min_V;
  1680. verts++;
  1681. /* 2 */
  1682. verts->position.x = rect.x + rect.w;
  1683. verts->position.y = rect.y + rect.h;
  1684. verts->color = color;
  1685. verts->tex_coord.x = max_U;
  1686. verts->tex_coord.y = max_V;
  1687. verts++;
  1688. /* 3 */
  1689. verts->position.x = rect.x;
  1690. verts->position.y = rect.y + rect.h;
  1691. verts->color = color;
  1692. verts->tex_coord.x = min_U;
  1693. verts->tex_coord.y = max_V;
  1694. verts++;
  1695. /* Blit sprites as triangles onto the screen */
  1696. SDL_RenderGeometry(renderer, tface, vertices, 6, NULL, 0);
  1697. /* See if it's the same */
  1698. referenceSurface = SDLTest_ImageWrappingSprite();
  1699. compare(referenceSurface, ALLOWABLE_ERROR_OPAQUE);
  1700. /* Make current */
  1701. SDL_RenderPresent(renderer);
  1702. /* Clean up. */
  1703. SDL_DestroyTexture(tface);
  1704. SDL_DestroySurface(referenceSurface);
  1705. referenceSurface = NULL;
  1706. return TEST_COMPLETED;
  1707. }
  1708. /**
  1709. * Tests texture state changes
  1710. */
  1711. static int SDLCALL render_testTextureState(void *arg)
  1712. {
  1713. const Uint8 pixels[8] = {
  1714. 0x00, 0x00, 0x00, 0xFF,
  1715. 0xFF, 0xFF, 0xFF, 0xFF
  1716. };
  1717. const SDL_Color expected[] = {
  1718. /* Step 0: plain copy */
  1719. { 0x00, 0x00, 0x00, 0xFF },
  1720. { 0xFF, 0xFF, 0xFF, 0xFF },
  1721. /* Step 1: color mod to red */
  1722. { 0x00, 0x00, 0x00, 0xFF },
  1723. { 0xFF, 0x00, 0x00, 0xFF },
  1724. /* Step 2: alpha mod to 128 (cleared to green) */
  1725. { 0x00, 0x7F, 0x00, 0xFF },
  1726. { 0x80, 0xFF, 0x80, 0xFF },
  1727. /* Step 3: nearest stretch */
  1728. { 0xFF, 0xFF, 0xFF, 0xFF },
  1729. { 0x00, 0xFF, 0x00, 0xFF },
  1730. /* Step 4: linear stretch */
  1731. { 0x80, 0x80, 0x80, 0xFF },
  1732. { 0x00, 0xFF, 0x00, 0xFF },
  1733. };
  1734. SDL_Texture *texture;
  1735. SDL_Rect rect;
  1736. SDL_FRect dst;
  1737. int i;
  1738. /* Clear surface to green */
  1739. SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE);
  1740. SDL_RenderClear(renderer);
  1741. /* Create 2-pixel surface. */
  1742. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 2, 1);
  1743. SDLTest_AssertCheck(texture != NULL, "Verify SDL_CreateTexture() result");
  1744. if (texture == NULL) {
  1745. return TEST_ABORTED;
  1746. }
  1747. SDL_UpdateTexture(texture, NULL, pixels, sizeof(pixels));
  1748. dst.x = 0.0f;
  1749. dst.y = 0.0f;
  1750. dst.w = 2.0f;
  1751. dst.h = 1.0f;
  1752. /* Step 0: plain copy */
  1753. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1754. dst.y += 1;
  1755. /* Step 1: color mod to red */
  1756. SDL_SetTextureColorMod(texture, 0xFF, 0x00, 0x00);
  1757. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1758. SDL_SetTextureColorMod(texture, 0xFF, 0xFF, 0xFF);
  1759. dst.y += 1;
  1760. /* Step 2: alpha mod to 128 */
  1761. SDL_SetTextureAlphaMod(texture, 0x80);
  1762. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1763. SDL_SetTextureAlphaMod(texture, 0xFF);
  1764. dst.y += 1;
  1765. /* Step 3: nearest stretch */
  1766. dst.w = 1;
  1767. SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
  1768. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1769. dst.y += 1;
  1770. /* Step 4: linear stretch */
  1771. dst.w = 1;
  1772. SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_LINEAR);
  1773. SDL_RenderTexture(renderer, texture, NULL, &dst);
  1774. dst.y += 1;
  1775. /* Verify results */
  1776. rect.x = 0;
  1777. rect.y = 0;
  1778. rect.w = 2;
  1779. rect.h = 1;
  1780. for (i = 0; i < SDL_arraysize(expected); ) {
  1781. const int MAX_DELTA = 1;
  1782. SDL_Color actual;
  1783. int deltaR, deltaG, deltaB, deltaA;
  1784. SDL_Surface *surface = SDL_RenderReadPixels(renderer, &rect);
  1785. SDL_ReadSurfacePixel(surface, 0, 0, &actual.r, &actual.g, &actual.b, &actual.a);
  1786. deltaR = (actual.r - expected[i].r);
  1787. deltaG = (actual.g - expected[i].g);
  1788. deltaB = (actual.b - expected[i].b);
  1789. deltaA = (actual.a - expected[i].a);
  1790. SDLTest_AssertCheck(SDL_abs(deltaR) <= MAX_DELTA &&
  1791. SDL_abs(deltaG) <= MAX_DELTA &&
  1792. SDL_abs(deltaB) <= MAX_DELTA &&
  1793. SDL_abs(deltaA) <= MAX_DELTA,
  1794. "Validate left pixel at step %d, expected %d,%d,%d,%d, got %d,%d,%d,%d", i/2,
  1795. expected[i].r, expected[i].g, expected[i].b, expected[i].a,
  1796. actual.r, actual.g, actual.b, actual.a);
  1797. ++i;
  1798. SDL_ReadSurfacePixel(surface, 1, 0, &actual.r, &actual.g, &actual.b, &actual.a);
  1799. deltaR = (actual.r - expected[i].r);
  1800. deltaG = (actual.g - expected[i].g);
  1801. deltaB = (actual.b - expected[i].b);
  1802. deltaA = (actual.a - expected[i].a);
  1803. SDLTest_AssertCheck(SDL_abs(deltaR) <= MAX_DELTA &&
  1804. SDL_abs(deltaG) <= MAX_DELTA &&
  1805. SDL_abs(deltaB) <= MAX_DELTA &&
  1806. SDL_abs(deltaA) <= MAX_DELTA,
  1807. "Validate right pixel at step %d, expected %d,%d,%d,%d, got %d,%d,%d,%d", i/2,
  1808. expected[i].r, expected[i].g, expected[i].b, expected[i].a,
  1809. actual.r, actual.g, actual.b, actual.a);
  1810. ++i;
  1811. SDL_DestroySurface(surface);
  1812. rect.y += 1;
  1813. }
  1814. /* Clean up. */
  1815. SDL_DestroyTexture(texture);
  1816. return TEST_COMPLETED;
  1817. }
  1818. static void CheckUniformColor(float expected)
  1819. {
  1820. SDL_Surface *surface = SDL_RenderReadPixels(renderer, NULL);
  1821. if (surface) {
  1822. const float epsilon = 0.001f;
  1823. float r, g, b, a;
  1824. CHECK_FUNC(SDL_ReadSurfacePixelFloat, (surface, 0, 0, &r, &g, &b, &a));
  1825. SDLTest_AssertCheck(
  1826. SDL_fabs(r - expected) <= epsilon &&
  1827. SDL_fabs(g - expected) <= epsilon &&
  1828. SDL_fabs(b - expected) <= epsilon &&
  1829. a == 1.0f,
  1830. "Check color, expected %g,%g,%g,%g, got %g,%g,%g,%g",
  1831. expected, expected, expected, 1.0f, r, g, b, a);
  1832. SDL_DestroySurface(surface);
  1833. } else {
  1834. SDLTest_AssertCheck(surface != NULL, "Validate result from SDL_RenderReadPixels, got NULL, %s", SDL_GetError());
  1835. }
  1836. }
  1837. /**
  1838. * Tests colorspace support (sRGB -> linear)
  1839. */
  1840. static int SDLCALL render_testColorspaceLinear(void *arg)
  1841. {
  1842. SDL_PropertiesID props;
  1843. SDL_Texture *texture;
  1844. Uint32 pixel = 0xFF404040;
  1845. SDL_DestroyRenderer(renderer);
  1846. props = SDL_CreateProperties();
  1847. #ifdef SDL_PLATFORM_EMSCRIPTEN
  1848. // Something about falling back to the software renderer and failing causes window surface updates to fail in video_getWindowSurface()
  1849. // Adding this as a workaround to prevent software fallback until we figure this out.
  1850. SDL_SetStringProperty(props, SDL_PROP_RENDERER_CREATE_NAME_STRING, "opengles2");
  1851. #endif
  1852. SDL_SetPointerProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window);
  1853. SDL_SetNumberProperty(props, SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB_LINEAR);
  1854. renderer = SDL_CreateRendererWithProperties(props);
  1855. SDL_DestroyProperties(props);
  1856. if (!renderer) {
  1857. SDLTest_Log("Skipping test render_testColorspaceLinear, couldn't create a linear colorspace renderer");
  1858. return TEST_SKIPPED;
  1859. }
  1860. /* Verify conversion between sRGB and linear colorspaces */
  1861. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, 1, 1);
  1862. SDLTest_AssertPass("Create texture");
  1863. SDLTest_AssertCheck(texture != NULL, "Check SDL_CreateTexture result");
  1864. CHECK_FUNC(SDL_UpdateTexture, (texture, NULL, &pixel, sizeof(pixel)));
  1865. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1866. CHECK_FUNC(SDL_RenderClear, (renderer));
  1867. CheckUniformColor(0.0f);
  1868. SDLTest_AssertPass("Checking sRGB clear 0x40");
  1869. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1870. CHECK_FUNC(SDL_RenderClear, (renderer));
  1871. CheckUniformColor(0.0512695f);
  1872. /* Clear target to 0 */
  1873. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1874. CHECK_FUNC(SDL_RenderClear, (renderer));
  1875. SDLTest_AssertPass("Checking sRGB draw 0x40");
  1876. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1877. CHECK_FUNC(SDL_RenderPoint, (renderer, 0.0f, 0.0f));
  1878. CheckUniformColor(0.0512695f);
  1879. /* Clear target to 0 */
  1880. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1881. CHECK_FUNC(SDL_RenderClear, (renderer));
  1882. SDLTest_AssertPass("Checking sRGB texture 0x40");
  1883. CHECK_FUNC(SDL_RenderTexture, (renderer, texture, NULL, NULL));
  1884. CheckUniformColor(0.0512695f);
  1885. /* Clear target to 0 */
  1886. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1887. CHECK_FUNC(SDL_RenderClear, (renderer));
  1888. SDL_SetRenderColorScale(renderer, 2.0f);
  1889. SDLTest_AssertPass("Checking sRGB clear 0x40 with color scale 2.0");
  1890. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1891. CHECK_FUNC(SDL_RenderClear, (renderer));
  1892. CheckUniformColor(0.102478f);
  1893. /* Clear target to 0 */
  1894. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1895. CHECK_FUNC(SDL_RenderClear, (renderer));
  1896. SDLTest_AssertPass("Checking sRGB draw 0x40 with color scale 2.0f");
  1897. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1898. CHECK_FUNC(SDL_RenderPoint, (renderer, 0.0f, 0.0f));
  1899. CheckUniformColor(0.102478f);
  1900. /* Clear target to 0 */
  1901. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1902. CHECK_FUNC(SDL_RenderClear, (renderer));
  1903. SDLTest_AssertPass("Checking sRGB texture 0x40 with color scale 2.0f");
  1904. CHECK_FUNC(SDL_RenderTexture, (renderer, texture, NULL, NULL));
  1905. CheckUniformColor(0.102478f);
  1906. SDL_DestroyTexture(texture);
  1907. return TEST_COMPLETED;
  1908. }
  1909. /**
  1910. * Tests colorspace support (linear -> sRGB)
  1911. */
  1912. static int SDLCALL render_testColorspaceSRGB(void *arg)
  1913. {
  1914. bool supports_float_textures;
  1915. const SDL_PixelFormat *texture_formats;
  1916. int i;
  1917. SDL_Texture *texture;
  1918. float pixel[4] = { 0.25f, 0.25f, 0.25f, 1.0f };
  1919. supports_float_textures = false;
  1920. texture_formats = (const SDL_PixelFormat *)SDL_GetPointerProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_TEXTURE_FORMATS_POINTER, NULL);
  1921. if (texture_formats) {
  1922. for (i = 0; texture_formats[i] != SDL_PIXELFORMAT_UNKNOWN; ++i) {
  1923. if (SDL_ISPIXELFORMAT_FLOAT(texture_formats[i])) {
  1924. supports_float_textures = true;
  1925. break;
  1926. }
  1927. }
  1928. }
  1929. if (!supports_float_textures) {
  1930. SDLTest_Log("Skipping test render_testColorspaceSRGB, renderer doesn't support float textures");
  1931. return TEST_SKIPPED;
  1932. }
  1933. /* Verify conversion between sRGB and linear colorspaces */
  1934. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA128_FLOAT, SDL_TEXTUREACCESS_STATIC, 1, 1);
  1935. SDLTest_AssertPass("Create texture");
  1936. SDLTest_AssertCheck(texture != NULL, "Check SDL_CreateTexture result");
  1937. CHECK_FUNC(SDL_UpdateTexture, (texture, NULL, &pixel, sizeof(pixel)));
  1938. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1939. CHECK_FUNC(SDL_RenderClear, (renderer));
  1940. CheckUniformColor(0.0f);
  1941. SDLTest_AssertPass("Checking sRGB clear 0x40");
  1942. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1943. CHECK_FUNC(SDL_RenderClear, (renderer));
  1944. CheckUniformColor(0.25098f);
  1945. /* Clear target to 0 */
  1946. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1947. CHECK_FUNC(SDL_RenderClear, (renderer));
  1948. SDLTest_AssertPass("Checking sRGB draw 0x40");
  1949. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1950. CHECK_FUNC(SDL_RenderPoint, (renderer, 0.0f, 0.0f));
  1951. CheckUniformColor(0.25098f);
  1952. /* Clear target to 0 */
  1953. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1954. CHECK_FUNC(SDL_RenderClear, (renderer));
  1955. SDLTest_AssertPass("Checking linear texture 0.25");
  1956. CHECK_FUNC(SDL_RenderTexture, (renderer, texture, NULL, NULL));
  1957. CheckUniformColor(0.537255f);
  1958. /* Clear target to 0 */
  1959. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1960. CHECK_FUNC(SDL_RenderClear, (renderer));
  1961. SDL_SetRenderColorScale(renderer, 2.0f);
  1962. SDLTest_AssertPass("Checking sRGB clear 0x40 with color scale 2.0");
  1963. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1964. CHECK_FUNC(SDL_RenderClear, (renderer));
  1965. CheckUniformColor(0.501961f);
  1966. /* Clear target to 0 */
  1967. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1968. CHECK_FUNC(SDL_RenderClear, (renderer));
  1969. SDLTest_AssertPass("Checking sRGB draw 0x40 with color scale 2.0f");
  1970. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0x40, 0x40, 0x40, 255));
  1971. CHECK_FUNC(SDL_RenderPoint, (renderer, 0.0f, 0.0f));
  1972. CheckUniformColor(0.501961f);
  1973. /* Clear target to 0 */
  1974. CHECK_FUNC(SDL_SetRenderDrawColor, (renderer, 0, 0, 0, 255));
  1975. CHECK_FUNC(SDL_RenderClear, (renderer));
  1976. SDLTest_AssertPass("Checking linear texture 0.25 with color scale 2.0f");
  1977. CHECK_FUNC(SDL_RenderTexture, (renderer, texture, NULL, NULL));
  1978. CheckUniformColor(0.737255f);
  1979. SDL_DestroyTexture(texture);
  1980. return TEST_COMPLETED;
  1981. }
  1982. /* ================= Test References ================== */
  1983. /* Render test cases */
  1984. static const SDLTest_TestCaseReference renderTestGetNumRenderDrivers = {
  1985. render_testGetNumRenderDrivers, "render_testGetNumRenderDrivers", "Tests call to SDL_GetNumRenderDrivers", TEST_ENABLED
  1986. };
  1987. static const SDLTest_TestCaseReference renderTestPrimitives = {
  1988. render_testPrimitives, "render_testPrimitives", "Tests rendering primitives", TEST_ENABLED
  1989. };
  1990. static const SDLTest_TestCaseReference renderTestPrimitivesWithViewport = {
  1991. render_testPrimitivesWithViewport, "render_testPrimitivesWithViewport", "Tests rendering primitives within a viewport", TEST_ENABLED
  1992. };
  1993. static const SDLTest_TestCaseReference renderTestBlit = {
  1994. render_testBlit, "render_testBlit", "Tests blitting", TEST_ENABLED
  1995. };
  1996. static const SDLTest_TestCaseReference renderTestBlitTiled = {
  1997. render_testBlitTiled, "render_testBlitTiled", "Tests tiled blitting", TEST_ENABLED
  1998. };
  1999. static const SDLTest_TestCaseReference renderTestBlit9Grid = {
  2000. render_testBlit9Grid, "render_testBlit9Grid", "Tests 9-grid blitting", TEST_ENABLED
  2001. };
  2002. static const SDLTest_TestCaseReference renderTestBlit9GridTiled = {
  2003. render_testBlit9GridTiled, "render_testBlit9GridTiled", "Tests tiled 9-grid blitting", TEST_ENABLED
  2004. };
  2005. static const SDLTest_TestCaseReference renderTestBlitColor = {
  2006. render_testBlitColor, "render_testBlitColor", "Tests blitting with color", TEST_ENABLED
  2007. };
  2008. static const SDLTest_TestCaseReference renderTestBlendModes = {
  2009. render_testBlendModes, "render_testBlendModes", "Tests rendering blend modes", TEST_ENABLED
  2010. };
  2011. static const SDLTest_TestCaseReference renderTestViewport = {
  2012. render_testViewport, "render_testViewport", "Tests viewport", TEST_ENABLED
  2013. };
  2014. static const SDLTest_TestCaseReference renderTestClipRect = {
  2015. render_testClipRect, "render_testClipRect", "Tests clip rect", TEST_ENABLED
  2016. };
  2017. static const SDLTest_TestCaseReference renderTestLogicalSize = {
  2018. render_testLogicalSize, "render_testLogicalSize", "Tests logical size", TEST_ENABLED
  2019. };
  2020. static const SDLTest_TestCaseReference renderTestUVClamping = {
  2021. render_testUVClamping, "render_testUVClamping", "Tests geometry UV clamping", TEST_ENABLED
  2022. };
  2023. static const SDLTest_TestCaseReference renderTestUVWrapping = {
  2024. render_testUVWrapping, "render_testUVWrapping", "Tests geometry UV wrapping", TEST_ENABLED
  2025. };
  2026. static const SDLTest_TestCaseReference renderTestTextureState = {
  2027. render_testTextureState, "render_testTextureState", "Tests texture state changes", TEST_ENABLED
  2028. };
  2029. static const SDLTest_TestCaseReference renderTestGetSetTextureScaleMode = {
  2030. render_testGetSetTextureScaleMode, "render_testGetSetTextureScaleMode", "Tests setting/getting texture scale mode", TEST_ENABLED
  2031. };
  2032. static const SDLTest_TestCaseReference renderTestRGBSurfaceNoAlpha = {
  2033. render_testRGBSurfaceNoAlpha, "render_testRGBSurfaceNoAlpha", "Tests RGB surface with no alpha using software renderer", TEST_ENABLED
  2034. };
  2035. static const SDLTest_TestCaseReference renderTestColorspaceLinear = {
  2036. render_testColorspaceLinear, "render_testColorspaceLinear", "Tests colorspace support (sRGB -> linear)", TEST_ENABLED
  2037. };
  2038. static const SDLTest_TestCaseReference renderTestColorspaceSRGB = {
  2039. render_testColorspaceSRGB, "render_testColorspaceSRGB", "Tests colorspace support (linear -> sRGB)", TEST_ENABLED
  2040. };
  2041. /* Sequence of Render test cases */
  2042. static const SDLTest_TestCaseReference *renderTests[] = {
  2043. &renderTestGetNumRenderDrivers,
  2044. &renderTestPrimitives,
  2045. &renderTestPrimitivesWithViewport,
  2046. &renderTestBlit,
  2047. &renderTestBlitTiled,
  2048. &renderTestBlit9Grid,
  2049. &renderTestBlit9GridTiled,
  2050. &renderTestBlitColor,
  2051. &renderTestBlendModes,
  2052. &renderTestViewport,
  2053. &renderTestClipRect,
  2054. &renderTestLogicalSize,
  2055. &renderTestUVClamping,
  2056. &renderTestUVWrapping,
  2057. &renderTestTextureState,
  2058. &renderTestGetSetTextureScaleMode,
  2059. &renderTestRGBSurfaceNoAlpha,
  2060. &renderTestColorspaceLinear,
  2061. &renderTestColorspaceSRGB,
  2062. NULL
  2063. };
  2064. /* Render test suite (global) */
  2065. SDLTest_TestSuiteReference renderTestSuite = {
  2066. "Render",
  2067. InitCreateRenderer,
  2068. renderTests,
  2069. CleanupDestroyRenderer
  2070. };