SDL_render.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. /* The SDL 2D rendering system */
  20. #include "SDL_assert.h"
  21. #include "SDL_hints.h"
  22. #include "SDL_log.h"
  23. #include "SDL_render.h"
  24. #include "SDL_sysrender.h"
  25. #include "software/SDL_render_sw_c.h"
  26. #define SDL_WINDOWRENDERDATA "_SDL_WindowRenderData"
  27. #define CHECK_RENDERER_MAGIC(renderer, retval) \
  28. SDL_assert(renderer && renderer->magic == &renderer_magic); \
  29. if (!renderer || renderer->magic != &renderer_magic) { \
  30. SDL_SetError("Invalid renderer"); \
  31. return retval; \
  32. }
  33. #define CHECK_TEXTURE_MAGIC(texture, retval) \
  34. SDL_assert(texture && texture->magic == &texture_magic); \
  35. if (!texture || texture->magic != &texture_magic) { \
  36. SDL_SetError("Invalid texture"); \
  37. return retval; \
  38. }
  39. /* Predefined blend modes */
  40. #define SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation, \
  41. srcAlphaFactor, dstAlphaFactor, alphaOperation) \
  42. (SDL_BlendMode)(((Uint32)colorOperation << 0) | \
  43. ((Uint32)srcColorFactor << 4) | \
  44. ((Uint32)dstColorFactor << 8) | \
  45. ((Uint32)alphaOperation << 16) | \
  46. ((Uint32)srcAlphaFactor << 20) | \
  47. ((Uint32)dstAlphaFactor << 24))
  48. #define SDL_BLENDMODE_NONE_FULL \
  49. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD, \
  50. SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD)
  51. #define SDL_BLENDMODE_BLEND_FULL \
  52. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, \
  53. SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD)
  54. #define SDL_BLENDMODE_ADD_FULL \
  55. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, \
  56. SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
  57. #define SDL_BLENDMODE_MOD_FULL \
  58. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_COLOR, SDL_BLENDOPERATION_ADD, \
  59. SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
  60. #if !SDL_RENDER_DISABLED
  61. static const SDL_RenderDriver *render_drivers[] = {
  62. #if SDL_VIDEO_RENDER_D3D
  63. &D3D_RenderDriver,
  64. #endif
  65. #if SDL_VIDEO_RENDER_D3D11
  66. &D3D11_RenderDriver,
  67. #endif
  68. #if SDL_VIDEO_RENDER_METAL
  69. &METAL_RenderDriver,
  70. #endif
  71. #if SDL_VIDEO_RENDER_OGL
  72. &GL_RenderDriver,
  73. #endif
  74. #if SDL_VIDEO_RENDER_OGL_ES2
  75. &GLES2_RenderDriver,
  76. #endif
  77. #if SDL_VIDEO_RENDER_OGL_ES
  78. &GLES_RenderDriver,
  79. #endif
  80. #if SDL_VIDEO_RENDER_DIRECTFB
  81. &DirectFB_RenderDriver,
  82. #endif
  83. #if SDL_VIDEO_RENDER_PSP
  84. &PSP_RenderDriver,
  85. #endif
  86. &SW_RenderDriver
  87. };
  88. #endif /* !SDL_RENDER_DISABLED */
  89. static char renderer_magic;
  90. static char texture_magic;
  91. static SDL_INLINE void
  92. DebugLogRenderCommands(const SDL_RenderCommand *cmd)
  93. {
  94. #if 0
  95. unsigned int i = 1;
  96. SDL_Log("Render commands to flush:");
  97. while (cmd) {
  98. switch (cmd->command) {
  99. case SDL_RENDERCMD_NO_OP:
  100. SDL_Log(" %u. no-op", i++);
  101. break;
  102. case SDL_RENDERCMD_SETVIEWPORT:
  103. SDL_Log(" %u. set viewport (first=%u, rect={(%d, %d), %dx%d})", i++,
  104. (unsigned int) cmd->data.viewport.first,
  105. cmd->data.viewport.rect.x, cmd->data.viewport.rect.y,
  106. cmd->data.viewport.rect.w, cmd->data.viewport.rect.h);
  107. break;
  108. case SDL_RENDERCMD_SETCLIPRECT:
  109. SDL_Log(" %u. set cliprect (enabled=%s, rect={(%d, %d), %dx%d})", i++,
  110. cmd->data.cliprect.enabled ? "true" : "false",
  111. cmd->data.cliprect.rect.x, cmd->data.cliprect.rect.y,
  112. cmd->data.cliprect.rect.w, cmd->data.cliprect.rect.h);
  113. break;
  114. case SDL_RENDERCMD_SETDRAWCOLOR:
  115. SDL_Log(" %u. set draw color (first=%u, r=%d, g=%d, b=%d, a=%d)", i++,
  116. (unsigned int) cmd->data.color.first,
  117. (int) cmd->data.color.r, (int) cmd->data.color.g,
  118. (int) cmd->data.color.b, (int) cmd->data.color.a);
  119. break;
  120. case SDL_RENDERCMD_CLEAR:
  121. SDL_Log(" %u. clear (first=%u, r=%d, g=%d, b=%d, a=%d)", i++,
  122. (unsigned int) cmd->data.color.first,
  123. (int) cmd->data.color.r, (int) cmd->data.color.g,
  124. (int) cmd->data.color.b, (int) cmd->data.color.a);
  125. break;
  126. case SDL_RENDERCMD_DRAW_POINTS:
  127. SDL_Log(" %u. draw points (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
  128. (unsigned int) cmd->data.draw.first,
  129. (unsigned int) cmd->data.draw.count,
  130. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  131. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  132. (int) cmd->data.draw.blend);
  133. break;
  134. case SDL_RENDERCMD_DRAW_LINES:
  135. SDL_Log(" %u. draw lines (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
  136. (unsigned int) cmd->data.draw.first,
  137. (unsigned int) cmd->data.draw.count,
  138. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  139. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  140. (int) cmd->data.draw.blend);
  141. break;
  142. case SDL_RENDERCMD_FILL_RECTS:
  143. SDL_Log(" %u. fill rects (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
  144. (unsigned int) cmd->data.draw.first,
  145. (unsigned int) cmd->data.draw.count,
  146. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  147. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  148. (int) cmd->data.draw.blend);
  149. break;
  150. case SDL_RENDERCMD_COPY:
  151. SDL_Log(" %u. copy (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
  152. (unsigned int) cmd->data.draw.first,
  153. (unsigned int) cmd->data.draw.count,
  154. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  155. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  156. (int) cmd->data.draw.blend, cmd->data.draw.texture);
  157. break;
  158. case SDL_RENDERCMD_COPY_EX:
  159. SDL_Log(" %u. copyex (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
  160. (unsigned int) cmd->data.draw.first,
  161. (unsigned int) cmd->data.draw.count,
  162. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  163. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  164. (int) cmd->data.draw.blend, cmd->data.draw.texture);
  165. break;
  166. }
  167. cmd = cmd->next;
  168. }
  169. #endif
  170. }
  171. static int
  172. FlushRenderCommands(SDL_Renderer *renderer)
  173. {
  174. SDL_AllocVertGap *prevgap = &renderer->vertex_data_gaps;
  175. SDL_AllocVertGap *gap = prevgap;
  176. int retval;
  177. SDL_assert((renderer->render_commands == NULL) == (renderer->render_commands_tail == NULL));
  178. if (renderer->render_commands == NULL) { /* nothing to do! */
  179. SDL_assert(renderer->vertex_data_used == 0);
  180. return 0;
  181. }
  182. DebugLogRenderCommands(renderer->render_commands);
  183. retval = renderer->RunCommandQueue(renderer, renderer->render_commands, renderer->vertex_data, renderer->vertex_data_used);
  184. while (gap) {
  185. prevgap = gap;
  186. gap = gap->next;
  187. }
  188. prevgap->next = renderer->vertex_data_gaps_pool;
  189. renderer->vertex_data_gaps_pool = renderer->vertex_data_gaps.next;
  190. renderer->vertex_data_gaps.next = NULL;
  191. /* Move the whole render command queue to the unused pool so we can reuse them next time. */
  192. if (renderer->render_commands_tail != NULL) {
  193. renderer->render_commands_tail->next = renderer->render_commands_pool;
  194. renderer->render_commands_pool = renderer->render_commands;
  195. renderer->render_commands_tail = NULL;
  196. renderer->render_commands = NULL;
  197. }
  198. renderer->vertex_data_used = 0;
  199. renderer->render_command_generation++;
  200. renderer->color_queued = SDL_FALSE;
  201. renderer->viewport_queued = SDL_FALSE;
  202. renderer->cliprect_queued = SDL_FALSE;
  203. return retval;
  204. }
  205. static int
  206. FlushRenderCommandsIfTextureNeeded(SDL_Texture *texture)
  207. {
  208. SDL_Renderer *renderer = texture->renderer;
  209. if (texture->last_command_generation == renderer->render_command_generation) {
  210. /* the current command queue depends on this texture, flush the queue now before it changes */
  211. return FlushRenderCommands(renderer);
  212. }
  213. return 0;
  214. }
  215. static SDL_INLINE int
  216. FlushRenderCommandsIfNotBatching(SDL_Renderer *renderer)
  217. {
  218. return renderer->batching ? 0 : FlushRenderCommands(renderer);
  219. }
  220. int
  221. SDL_RenderFlush(SDL_Renderer * renderer)
  222. {
  223. return FlushRenderCommands(renderer);
  224. }
  225. static SDL_AllocVertGap *
  226. AllocateVertexGap(SDL_Renderer *renderer)
  227. {
  228. SDL_AllocVertGap *retval = renderer->vertex_data_gaps_pool;
  229. if (retval) {
  230. renderer->vertex_data_gaps_pool = retval->next;
  231. retval->next = NULL;
  232. } else {
  233. retval = (SDL_AllocVertGap *) SDL_malloc(sizeof (SDL_AllocVertGap));
  234. if (!retval) {
  235. SDL_OutOfMemory();
  236. }
  237. }
  238. return retval;
  239. }
  240. void *
  241. SDL_AllocateRenderVertices(SDL_Renderer *renderer, const size_t numbytes, const size_t alignment, size_t *offset)
  242. {
  243. const size_t needed = renderer->vertex_data_used + numbytes + alignment;
  244. size_t aligner, aligned;
  245. void *retval;
  246. SDL_AllocVertGap *prevgap = &renderer->vertex_data_gaps;
  247. SDL_AllocVertGap *gap = prevgap->next;
  248. while (gap) {
  249. const size_t gapoffset = gap->offset;
  250. aligner = (alignment && ((gap->offset % alignment) != 0)) ? (alignment - (gap->offset % alignment)) : 0;
  251. aligned = gapoffset + aligner;
  252. /* Can we use this gap? */
  253. if ((aligner < gap->len) && ((gap->len - aligner) >= numbytes)) {
  254. /* we either finished this gap off, trimmed the left, trimmed the right, or split it into two gaps. */
  255. if (gap->len == numbytes) { /* finished it off, remove it */
  256. SDL_assert(aligned == gapoffset);
  257. prevgap->next = gap->next;
  258. gap->next = renderer->vertex_data_gaps_pool;
  259. renderer->vertex_data_gaps_pool = gap;
  260. } else if (aligned == gapoffset) { /* trimmed the left */
  261. gap->offset += numbytes;
  262. gap->len -= numbytes;
  263. } else if (((aligned - gapoffset) + numbytes) == gap->len) { /* trimmed the right */
  264. gap->len -= numbytes;
  265. } else { /* split into two gaps */
  266. SDL_AllocVertGap *newgap = AllocateVertexGap(renderer);
  267. if (!newgap) {
  268. return NULL;
  269. }
  270. newgap->offset = aligned + numbytes;
  271. newgap->len = gap->len - (aligner + numbytes);
  272. newgap->next = gap->next;
  273. // gap->offset doesn't change.
  274. gap->len = aligner;
  275. gap->next = newgap;
  276. }
  277. if (offset) {
  278. *offset = aligned;
  279. }
  280. return ((Uint8 *) renderer->vertex_data) + aligned;
  281. }
  282. /* Try the next gap */
  283. prevgap = gap;
  284. gap = gap->next;
  285. }
  286. /* no gaps with enough space; get a new piece of the vertex buffer */
  287. while (needed > renderer->vertex_data_allocation) {
  288. const size_t current_allocation = renderer->vertex_data ? renderer->vertex_data_allocation : 1024;
  289. const size_t newsize = current_allocation * 2;
  290. void *ptr = SDL_realloc(renderer->vertex_data, newsize);
  291. if (ptr == NULL) {
  292. SDL_OutOfMemory();
  293. return NULL;
  294. }
  295. renderer->vertex_data = ptr;
  296. renderer->vertex_data_allocation = newsize;
  297. }
  298. aligner = (alignment && ((renderer->vertex_data_used % alignment) != 0)) ? (alignment - (renderer->vertex_data_used % alignment)) : 0;
  299. aligned = renderer->vertex_data_used + aligner;
  300. retval = ((Uint8 *) renderer->vertex_data) + aligned;
  301. if (offset) {
  302. *offset = aligned;
  303. }
  304. if (aligner) { /* made a new gap... */
  305. SDL_AllocVertGap *newgap = AllocateVertexGap(renderer);
  306. if (newgap) { /* just let it slide as lost space if malloc fails. */
  307. newgap->offset = renderer->vertex_data_used;
  308. newgap->len = aligner;
  309. newgap->next = NULL;
  310. prevgap->next = newgap;
  311. }
  312. }
  313. renderer->vertex_data_used += aligner + numbytes;
  314. return retval;
  315. }
  316. static SDL_RenderCommand *
  317. AllocateRenderCommand(SDL_Renderer *renderer)
  318. {
  319. SDL_RenderCommand *retval = NULL;
  320. /* !!! FIXME: are there threading limitations in SDL's render API? If not, we need to mutex this. */
  321. retval = renderer->render_commands_pool;
  322. if (retval != NULL) {
  323. renderer->render_commands_pool = retval->next;
  324. retval->next = NULL;
  325. } else {
  326. retval = SDL_calloc(1, sizeof (*retval));
  327. if (!retval) {
  328. SDL_OutOfMemory();
  329. return NULL;
  330. }
  331. }
  332. SDL_assert((renderer->render_commands == NULL) == (renderer->render_commands_tail == NULL));
  333. if (renderer->render_commands_tail != NULL) {
  334. renderer->render_commands_tail->next = retval;
  335. } else {
  336. renderer->render_commands = retval;
  337. }
  338. renderer->render_commands_tail = retval;
  339. return retval;
  340. }
  341. static int
  342. QueueCmdSetViewport(SDL_Renderer *renderer)
  343. {
  344. int retval = 0;
  345. if (!renderer->viewport_queued || (SDL_memcmp(&renderer->viewport, &renderer->last_queued_viewport, sizeof (SDL_Rect)) != 0)) {
  346. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  347. retval = -1;
  348. if (cmd != NULL) {
  349. cmd->command = SDL_RENDERCMD_SETVIEWPORT;
  350. cmd->data.viewport.first = 0; /* render backend will fill this in. */
  351. SDL_memcpy(&cmd->data.viewport.rect, &renderer->viewport, sizeof (renderer->viewport));
  352. retval = renderer->QueueSetViewport(renderer, cmd);
  353. if (retval < 0) {
  354. cmd->command = SDL_RENDERCMD_NO_OP;
  355. } else {
  356. SDL_memcpy(&renderer->last_queued_viewport, &renderer->viewport, sizeof (SDL_Rect));
  357. renderer->viewport_queued = SDL_TRUE;
  358. }
  359. }
  360. }
  361. return retval;
  362. }
  363. static int
  364. QueueCmdSetClipRect(SDL_Renderer *renderer)
  365. {
  366. int retval = 0;
  367. if ((!renderer->cliprect_queued) ||
  368. (renderer->clipping_enabled != renderer->last_queued_cliprect_enabled) ||
  369. (SDL_memcmp(&renderer->clip_rect, &renderer->last_queued_cliprect, sizeof (SDL_Rect)) != 0)) {
  370. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  371. if (cmd == NULL) {
  372. retval = -1;
  373. } else {
  374. cmd->command = SDL_RENDERCMD_SETCLIPRECT;
  375. cmd->data.cliprect.enabled = renderer->clipping_enabled;
  376. SDL_memcpy(&cmd->data.cliprect.rect, &renderer->clip_rect, sizeof (cmd->data.cliprect.rect));
  377. SDL_memcpy(&renderer->last_queued_cliprect, &renderer->clip_rect, sizeof (SDL_Rect));
  378. renderer->last_queued_cliprect_enabled = renderer->clipping_enabled;
  379. renderer->cliprect_queued = SDL_TRUE;
  380. }
  381. }
  382. return retval;
  383. }
  384. static int
  385. QueueCmdSetDrawColor(SDL_Renderer *renderer, const Uint8 r, const Uint8 g, const Uint8 b, const Uint8 a)
  386. {
  387. const Uint32 color = ((a << 24) | (r << 16) | (g << 8) | b);
  388. int retval = 0;
  389. if (!renderer->color_queued || (color != renderer->last_queued_color)) {
  390. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  391. retval = -1;
  392. if (cmd != NULL) {
  393. cmd->command = SDL_RENDERCMD_SETDRAWCOLOR;
  394. cmd->data.color.first = 0; /* render backend will fill this in. */
  395. cmd->data.color.r = r;
  396. cmd->data.color.g = g;
  397. cmd->data.color.b = b;
  398. cmd->data.color.a = a;
  399. retval = renderer->QueueSetDrawColor(renderer, cmd);
  400. if (retval < 0) {
  401. cmd->command = SDL_RENDERCMD_NO_OP;
  402. } else {
  403. renderer->last_queued_color = color;
  404. renderer->color_queued = SDL_TRUE;
  405. }
  406. }
  407. }
  408. return retval;
  409. }
  410. static int
  411. QueueCmdClear(SDL_Renderer *renderer)
  412. {
  413. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  414. if (cmd == NULL) {
  415. return -1;
  416. }
  417. cmd->command = SDL_RENDERCMD_CLEAR;
  418. cmd->data.color.first = 0;
  419. cmd->data.color.r = renderer->r;
  420. cmd->data.color.g = renderer->g;
  421. cmd->data.color.b = renderer->b;
  422. cmd->data.color.a = renderer->a;
  423. return 0;
  424. }
  425. static int
  426. PrepQueueCmdDraw(SDL_Renderer *renderer, const Uint8 r, const Uint8 g, const Uint8 b, const Uint8 a)
  427. {
  428. int retval = 0;
  429. if (retval == 0) {
  430. retval = QueueCmdSetDrawColor(renderer, r, g, b, a);
  431. }
  432. if (retval == 0) {
  433. retval = QueueCmdSetViewport(renderer);
  434. }
  435. if (retval == 0) {
  436. retval = QueueCmdSetClipRect(renderer);
  437. }
  438. return retval;
  439. }
  440. static SDL_RenderCommand *
  441. PrepQueueCmdDrawSolid(SDL_Renderer *renderer, const SDL_RenderCommandType cmdtype)
  442. {
  443. /* !!! FIXME: drop this draw if viewport w or h is zero. */
  444. SDL_RenderCommand *cmd = NULL;
  445. if (PrepQueueCmdDraw(renderer, renderer->r, renderer->g, renderer->b, renderer->a) == 0) {
  446. cmd = AllocateRenderCommand(renderer);
  447. if (cmd != NULL) {
  448. cmd->command = cmdtype;
  449. cmd->data.draw.first = 0; /* render backend will fill this in. */
  450. cmd->data.draw.count = 0; /* render backend will fill this in. */
  451. cmd->data.draw.r = renderer->r;
  452. cmd->data.draw.g = renderer->g;
  453. cmd->data.draw.b = renderer->b;
  454. cmd->data.draw.a = renderer->a;
  455. cmd->data.draw.blend = renderer->blendMode;
  456. cmd->data.draw.texture = NULL; /* no texture. */
  457. }
  458. }
  459. return cmd;
  460. }
  461. static int
  462. QueueCmdDrawPoints(SDL_Renderer *renderer, const SDL_FPoint * points, const int count)
  463. {
  464. SDL_RenderCommand *cmd = PrepQueueCmdDrawSolid(renderer, SDL_RENDERCMD_DRAW_POINTS);
  465. int retval = -1;
  466. if (cmd != NULL) {
  467. retval = renderer->QueueDrawPoints(renderer, cmd, points, count);
  468. if (retval < 0) {
  469. cmd->command = SDL_RENDERCMD_NO_OP;
  470. }
  471. }
  472. return retval;
  473. }
  474. static int
  475. QueueCmdDrawLines(SDL_Renderer *renderer, const SDL_FPoint * points, const int count)
  476. {
  477. SDL_RenderCommand *cmd = PrepQueueCmdDrawSolid(renderer, SDL_RENDERCMD_DRAW_LINES);
  478. int retval = -1;
  479. if (cmd != NULL) {
  480. retval = renderer->QueueDrawLines(renderer, cmd, points, count);
  481. if (retval < 0) {
  482. cmd->command = SDL_RENDERCMD_NO_OP;
  483. }
  484. }
  485. return retval;
  486. }
  487. static int
  488. QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect * rects, const int count)
  489. {
  490. SDL_RenderCommand *cmd = PrepQueueCmdDrawSolid(renderer, SDL_RENDERCMD_FILL_RECTS);
  491. int retval = -1;
  492. if (cmd != NULL) {
  493. retval = renderer->QueueFillRects(renderer, cmd, rects, count);
  494. if (retval < 0) {
  495. cmd->command = SDL_RENDERCMD_NO_OP;
  496. }
  497. }
  498. return retval;
  499. }
  500. static SDL_RenderCommand *
  501. PrepQueueCmdDrawTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_RenderCommandType cmdtype)
  502. {
  503. /* !!! FIXME: drop this draw if viewport w or h is zero. */
  504. SDL_RenderCommand *cmd = NULL;
  505. if (PrepQueueCmdDraw(renderer, texture->r, texture->g, texture->b, texture->a) == 0) {
  506. cmd = AllocateRenderCommand(renderer);
  507. if (cmd != NULL) {
  508. cmd->command = cmdtype;
  509. cmd->data.draw.first = 0; /* render backend will fill this in. */
  510. cmd->data.draw.count = 0; /* render backend will fill this in. */
  511. cmd->data.draw.r = texture->r;
  512. cmd->data.draw.g = texture->g;
  513. cmd->data.draw.b = texture->b;
  514. cmd->data.draw.a = texture->a;
  515. cmd->data.draw.blend = texture->blendMode;
  516. cmd->data.draw.texture = texture;
  517. }
  518. }
  519. return cmd;
  520. }
  521. static int
  522. QueueCmdCopy(SDL_Renderer *renderer, SDL_Texture * texture, const SDL_Rect * srcrect, const SDL_FRect * dstrect)
  523. {
  524. SDL_RenderCommand *cmd = PrepQueueCmdDrawTexture(renderer, texture, SDL_RENDERCMD_COPY);
  525. int retval = -1;
  526. if (cmd != NULL) {
  527. retval = renderer->QueueCopy(renderer, cmd, texture, srcrect, dstrect);
  528. if (retval < 0) {
  529. cmd->command = SDL_RENDERCMD_NO_OP;
  530. }
  531. }
  532. return retval;
  533. }
  534. static int
  535. QueueCmdCopyEx(SDL_Renderer *renderer, SDL_Texture * texture,
  536. const SDL_Rect * srcquad, const SDL_FRect * dstrect,
  537. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
  538. {
  539. SDL_RenderCommand *cmd = PrepQueueCmdDrawTexture(renderer, texture, SDL_RENDERCMD_COPY_EX);
  540. int retval = -1;
  541. SDL_assert(renderer->QueueCopyEx != NULL); /* should have caught at higher level. */
  542. if (cmd != NULL) {
  543. retval = renderer->QueueCopyEx(renderer, cmd, texture, srcquad, dstrect, angle, center, flip);
  544. if (retval < 0) {
  545. cmd->command = SDL_RENDERCMD_NO_OP;
  546. }
  547. }
  548. return retval;
  549. }
  550. static int UpdateLogicalSize(SDL_Renderer *renderer);
  551. int
  552. SDL_GetNumRenderDrivers(void)
  553. {
  554. #if !SDL_RENDER_DISABLED
  555. return SDL_arraysize(render_drivers);
  556. #else
  557. return 0;
  558. #endif
  559. }
  560. int
  561. SDL_GetRenderDriverInfo(int index, SDL_RendererInfo * info)
  562. {
  563. #if !SDL_RENDER_DISABLED
  564. if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  565. return SDL_SetError("index must be in the range of 0 - %d",
  566. SDL_GetNumRenderDrivers() - 1);
  567. }
  568. *info = render_drivers[index]->info;
  569. return 0;
  570. #else
  571. return SDL_SetError("SDL not built with rendering support");
  572. #endif
  573. }
  574. static void GetWindowViewportValues(SDL_Renderer *renderer, int *logical_w, int *logical_h, SDL_Rect *viewport, SDL_FPoint *scale)
  575. {
  576. SDL_LockMutex(renderer->target_mutex);
  577. *logical_w = renderer->target ? renderer->logical_w_backup : renderer->logical_w;
  578. *logical_h = renderer->target ? renderer->logical_h_backup : renderer->logical_h;
  579. *viewport = renderer->target ? renderer->viewport_backup : renderer->viewport;
  580. *scale = renderer->target ? renderer->scale_backup : renderer->scale;
  581. SDL_UnlockMutex(renderer->target_mutex);
  582. }
  583. static int SDLCALL
  584. SDL_RendererEventWatch(void *userdata, SDL_Event *event)
  585. {
  586. SDL_Renderer *renderer = (SDL_Renderer *)userdata;
  587. if (event->type == SDL_WINDOWEVENT) {
  588. SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
  589. if (window == renderer->window) {
  590. if (renderer->WindowEvent) {
  591. renderer->WindowEvent(renderer, &event->window);
  592. }
  593. if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
  594. /* Make sure we're operating on the default render target */
  595. SDL_Texture *saved_target = SDL_GetRenderTarget(renderer);
  596. if (saved_target) {
  597. SDL_SetRenderTarget(renderer, NULL);
  598. }
  599. if (renderer->logical_w) {
  600. UpdateLogicalSize(renderer);
  601. } else {
  602. /* Window was resized, reset viewport */
  603. int w, h;
  604. if (renderer->GetOutputSize) {
  605. renderer->GetOutputSize(renderer, &w, &h);
  606. } else {
  607. SDL_GetWindowSize(renderer->window, &w, &h);
  608. }
  609. if (renderer->target) {
  610. renderer->viewport_backup.x = 0;
  611. renderer->viewport_backup.y = 0;
  612. renderer->viewport_backup.w = w;
  613. renderer->viewport_backup.h = h;
  614. } else {
  615. renderer->viewport.x = 0;
  616. renderer->viewport.y = 0;
  617. renderer->viewport.w = w;
  618. renderer->viewport.h = h;
  619. QueueCmdSetViewport(renderer);
  620. FlushRenderCommandsIfNotBatching(renderer);
  621. }
  622. }
  623. if (saved_target) {
  624. SDL_SetRenderTarget(renderer, saved_target);
  625. }
  626. } else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
  627. renderer->hidden = SDL_TRUE;
  628. } else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
  629. if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
  630. renderer->hidden = SDL_FALSE;
  631. }
  632. } else if (event->window.event == SDL_WINDOWEVENT_MINIMIZED) {
  633. renderer->hidden = SDL_TRUE;
  634. } else if (event->window.event == SDL_WINDOWEVENT_RESTORED ||
  635. event->window.event == SDL_WINDOWEVENT_MAXIMIZED) {
  636. if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
  637. renderer->hidden = SDL_FALSE;
  638. }
  639. }
  640. }
  641. } else if (event->type == SDL_MOUSEMOTION) {
  642. SDL_Window *window = SDL_GetWindowFromID(event->motion.windowID);
  643. if (window == renderer->window) {
  644. int logical_w, logical_h;
  645. SDL_Rect viewport;
  646. SDL_FPoint scale;
  647. GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
  648. if (logical_w) {
  649. event->motion.x -= (int)(viewport.x * renderer->dpi_scale.x);
  650. event->motion.y -= (int)(viewport.y * renderer->dpi_scale.y);
  651. event->motion.x = (int)(event->motion.x / (scale.x * renderer->dpi_scale.x));
  652. event->motion.y = (int)(event->motion.y / (scale.y * renderer->dpi_scale.y));
  653. if (event->motion.xrel > 0) {
  654. event->motion.xrel = SDL_max(1, (int)(event->motion.xrel / (scale.x * renderer->dpi_scale.x)));
  655. } else if (event->motion.xrel < 0) {
  656. event->motion.xrel = SDL_min(-1, (int)(event->motion.xrel / (scale.x * renderer->dpi_scale.x)));
  657. }
  658. if (event->motion.yrel > 0) {
  659. event->motion.yrel = SDL_max(1, (int)(event->motion.yrel / (scale.y * renderer->dpi_scale.y)));
  660. } else if (event->motion.yrel < 0) {
  661. event->motion.yrel = SDL_min(-1, (int)(event->motion.yrel / (scale.y * renderer->dpi_scale.y)));
  662. }
  663. }
  664. }
  665. } else if (event->type == SDL_MOUSEBUTTONDOWN ||
  666. event->type == SDL_MOUSEBUTTONUP) {
  667. SDL_Window *window = SDL_GetWindowFromID(event->button.windowID);
  668. if (window == renderer->window) {
  669. int logical_w, logical_h;
  670. SDL_Rect viewport;
  671. SDL_FPoint scale;
  672. GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
  673. if (logical_w) {
  674. event->button.x -= (int)(viewport.x * renderer->dpi_scale.x);
  675. event->button.y -= (int)(viewport.y * renderer->dpi_scale.y);
  676. event->button.x = (int)(event->button.x / (scale.x * renderer->dpi_scale.x));
  677. event->button.y = (int)(event->button.y / (scale.y * renderer->dpi_scale.y));
  678. }
  679. }
  680. } else if (event->type == SDL_FINGERDOWN ||
  681. event->type == SDL_FINGERUP ||
  682. event->type == SDL_FINGERMOTION) {
  683. int logical_w, logical_h;
  684. SDL_Rect viewport;
  685. SDL_FPoint scale;
  686. GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
  687. if (logical_w) {
  688. int w = 1;
  689. int h = 1;
  690. SDL_GetRendererOutputSize(renderer, &w, &h);
  691. event->tfinger.x *= (w - 1);
  692. event->tfinger.y *= (h - 1);
  693. event->tfinger.x -= (viewport.x * renderer->dpi_scale.x);
  694. event->tfinger.y -= (viewport.y * renderer->dpi_scale.y);
  695. event->tfinger.x = (event->tfinger.x / (scale.x * renderer->dpi_scale.x));
  696. event->tfinger.y = (event->tfinger.y / (scale.y * renderer->dpi_scale.y));
  697. if (logical_w > 1) {
  698. event->tfinger.x = event->tfinger.x / (logical_w - 1);
  699. } else {
  700. event->tfinger.x = 0.5f;
  701. }
  702. if (logical_h > 1) {
  703. event->tfinger.y = event->tfinger.y / (logical_h - 1);
  704. } else {
  705. event->tfinger.y = 0.5f;
  706. }
  707. }
  708. }
  709. return 0;
  710. }
  711. int
  712. SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags,
  713. SDL_Window **window, SDL_Renderer **renderer)
  714. {
  715. *window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED,
  716. SDL_WINDOWPOS_UNDEFINED,
  717. width, height, window_flags);
  718. if (!*window) {
  719. *renderer = NULL;
  720. return -1;
  721. }
  722. *renderer = SDL_CreateRenderer(*window, -1, 0);
  723. if (!*renderer) {
  724. return -1;
  725. }
  726. return 0;
  727. }
  728. static SDL_INLINE
  729. void VerifyDrawQueueFunctions(const SDL_Renderer *renderer)
  730. {
  731. /* all of these functions are required to be implemented, even as no-ops, so we don't
  732. have to check that they aren't NULL over and over. */
  733. SDL_assert(renderer->QueueSetViewport != NULL);
  734. SDL_assert(renderer->QueueSetDrawColor != NULL);
  735. SDL_assert(renderer->QueueDrawPoints != NULL);
  736. SDL_assert(renderer->QueueDrawLines != NULL);
  737. SDL_assert(renderer->QueueFillRects != NULL);
  738. SDL_assert(renderer->QueueCopy != NULL);
  739. SDL_assert(renderer->RunCommandQueue != NULL);
  740. }
  741. SDL_Renderer *
  742. SDL_CreateRenderer(SDL_Window * window, int index, Uint32 flags)
  743. {
  744. #if !SDL_RENDER_DISABLED
  745. SDL_Renderer *renderer = NULL;
  746. int n = SDL_GetNumRenderDrivers();
  747. SDL_bool batching = SDL_TRUE;
  748. const char *hint;
  749. if (!window) {
  750. SDL_SetError("Invalid window");
  751. goto error;
  752. }
  753. if (SDL_GetRenderer(window)) {
  754. SDL_SetError("Renderer already associated with window");
  755. goto error;
  756. }
  757. if (SDL_GetHint(SDL_HINT_RENDER_VSYNC)) {
  758. if (SDL_GetHintBoolean(SDL_HINT_RENDER_VSYNC, SDL_TRUE)) {
  759. flags |= SDL_RENDERER_PRESENTVSYNC;
  760. } else {
  761. flags &= ~SDL_RENDERER_PRESENTVSYNC;
  762. }
  763. }
  764. if (index < 0) {
  765. hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
  766. if (hint) {
  767. for (index = 0; index < n; ++index) {
  768. const SDL_RenderDriver *driver = render_drivers[index];
  769. if (SDL_strcasecmp(hint, driver->info.name) == 0) {
  770. /* Create a new renderer instance */
  771. renderer = driver->CreateRenderer(window, flags);
  772. if (renderer) {
  773. batching = SDL_FALSE;
  774. }
  775. break;
  776. }
  777. }
  778. }
  779. if (!renderer) {
  780. for (index = 0; index < n; ++index) {
  781. const SDL_RenderDriver *driver = render_drivers[index];
  782. if ((driver->info.flags & flags) == flags) {
  783. /* Create a new renderer instance */
  784. renderer = driver->CreateRenderer(window, flags);
  785. if (renderer) {
  786. /* Yay, we got one! */
  787. break;
  788. }
  789. }
  790. }
  791. }
  792. if (index == n) {
  793. SDL_SetError("Couldn't find matching render driver");
  794. goto error;
  795. }
  796. } else {
  797. if (index >= SDL_GetNumRenderDrivers()) {
  798. SDL_SetError("index must be -1 or in the range of 0 - %d",
  799. SDL_GetNumRenderDrivers() - 1);
  800. goto error;
  801. }
  802. /* Create a new renderer instance */
  803. renderer = render_drivers[index]->CreateRenderer(window, flags);
  804. batching = SDL_FALSE;
  805. }
  806. if (!renderer) {
  807. goto error;
  808. }
  809. VerifyDrawQueueFunctions(renderer);
  810. /* let app/user override batching decisions. */
  811. if (renderer->always_batch) {
  812. batching = SDL_TRUE;
  813. } else if (SDL_GetHint(SDL_HINT_RENDER_BATCHING)) {
  814. batching = SDL_GetHintBoolean(SDL_HINT_RENDER_BATCHING, SDL_TRUE);
  815. }
  816. renderer->batching = batching;
  817. renderer->magic = &renderer_magic;
  818. renderer->window = window;
  819. renderer->target_mutex = SDL_CreateMutex();
  820. renderer->scale.x = 1.0f;
  821. renderer->scale.y = 1.0f;
  822. renderer->dpi_scale.x = 1.0f;
  823. renderer->dpi_scale.y = 1.0f;
  824. /* new textures start at zero, so we start at 1 so first render doesn't flush by accident. */
  825. renderer->render_command_generation = 1;
  826. if (window && renderer->GetOutputSize) {
  827. int window_w, window_h;
  828. int output_w, output_h;
  829. if (renderer->GetOutputSize(renderer, &output_w, &output_h) == 0) {
  830. SDL_GetWindowSize(renderer->window, &window_w, &window_h);
  831. renderer->dpi_scale.x = (float)window_w / output_w;
  832. renderer->dpi_scale.y = (float)window_h / output_h;
  833. }
  834. }
  835. if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN|SDL_WINDOW_MINIMIZED)) {
  836. renderer->hidden = SDL_TRUE;
  837. } else {
  838. renderer->hidden = SDL_FALSE;
  839. }
  840. SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
  841. SDL_RenderSetViewport(renderer, NULL);
  842. SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
  843. SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
  844. "Created renderer: %s", renderer->info.name);
  845. return renderer;
  846. error:
  847. return NULL;
  848. #else
  849. SDL_SetError("SDL not built with rendering support");
  850. return NULL;
  851. #endif
  852. }
  853. SDL_Renderer *
  854. SDL_CreateSoftwareRenderer(SDL_Surface * surface)
  855. {
  856. #if !SDL_RENDER_DISABLED
  857. SDL_Renderer *renderer;
  858. renderer = SW_CreateRendererForSurface(surface);
  859. if (renderer) {
  860. VerifyDrawQueueFunctions(renderer);
  861. renderer->magic = &renderer_magic;
  862. renderer->target_mutex = SDL_CreateMutex();
  863. renderer->scale.x = 1.0f;
  864. renderer->scale.y = 1.0f;
  865. /* new textures start at zero, so we start at 1 so first render doesn't flush by accident. */
  866. renderer->render_command_generation = 1;
  867. SDL_RenderSetViewport(renderer, NULL);
  868. }
  869. return renderer;
  870. #else
  871. SDL_SetError("SDL not built with rendering support");
  872. return NULL;
  873. #endif /* !SDL_RENDER_DISABLED */
  874. }
  875. SDL_Renderer *
  876. SDL_GetRenderer(SDL_Window * window)
  877. {
  878. return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
  879. }
  880. int
  881. SDL_GetRendererInfo(SDL_Renderer * renderer, SDL_RendererInfo * info)
  882. {
  883. CHECK_RENDERER_MAGIC(renderer, -1);
  884. *info = renderer->info;
  885. return 0;
  886. }
  887. int
  888. SDL_GetRendererOutputSize(SDL_Renderer * renderer, int *w, int *h)
  889. {
  890. CHECK_RENDERER_MAGIC(renderer, -1);
  891. if (renderer->target) {
  892. return SDL_QueryTexture(renderer->target, NULL, NULL, w, h);
  893. } else if (renderer->GetOutputSize) {
  894. return renderer->GetOutputSize(renderer, w, h);
  895. } else if (renderer->window) {
  896. SDL_GetWindowSize(renderer->window, w, h);
  897. return 0;
  898. } else {
  899. SDL_assert(0 && "This should never happen");
  900. return SDL_SetError("Renderer doesn't support querying output size");
  901. }
  902. }
  903. static SDL_bool
  904. IsSupportedBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
  905. {
  906. switch (blendMode)
  907. {
  908. /* These are required to be supported by all renderers */
  909. case SDL_BLENDMODE_NONE:
  910. case SDL_BLENDMODE_BLEND:
  911. case SDL_BLENDMODE_ADD:
  912. case SDL_BLENDMODE_MOD:
  913. return SDL_TRUE;
  914. default:
  915. return renderer->SupportsBlendMode && renderer->SupportsBlendMode(renderer, blendMode);
  916. }
  917. }
  918. static SDL_bool
  919. IsSupportedFormat(SDL_Renderer * renderer, Uint32 format)
  920. {
  921. Uint32 i;
  922. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  923. if (renderer->info.texture_formats[i] == format) {
  924. return SDL_TRUE;
  925. }
  926. }
  927. return SDL_FALSE;
  928. }
  929. static Uint32
  930. GetClosestSupportedFormat(SDL_Renderer * renderer, Uint32 format)
  931. {
  932. Uint32 i;
  933. if (SDL_ISPIXELFORMAT_FOURCC(format)) {
  934. /* Look for an exact match */
  935. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  936. if (renderer->info.texture_formats[i] == format) {
  937. return renderer->info.texture_formats[i];
  938. }
  939. }
  940. } else {
  941. SDL_bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
  942. /* We just want to match the first format that has the same channels */
  943. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  944. if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
  945. SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
  946. return renderer->info.texture_formats[i];
  947. }
  948. }
  949. }
  950. return renderer->info.texture_formats[0];
  951. }
  952. static SDL_ScaleMode SDL_GetScaleMode(void)
  953. {
  954. const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
  955. if (!hint || SDL_strcasecmp(hint, "nearest") == 0) {
  956. return SDL_ScaleModeNearest;
  957. } else if (SDL_strcasecmp(hint, "linear") == 0) {
  958. return SDL_ScaleModeLinear;
  959. } else if (SDL_strcasecmp(hint, "best") == 0) {
  960. return SDL_ScaleModeBest;
  961. } else {
  962. return (SDL_ScaleMode)SDL_atoi(hint);
  963. }
  964. }
  965. SDL_Texture *
  966. SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int h)
  967. {
  968. SDL_Texture *texture;
  969. CHECK_RENDERER_MAGIC(renderer, NULL);
  970. if (!format) {
  971. format = renderer->info.texture_formats[0];
  972. }
  973. if (SDL_BYTESPERPIXEL(format) == 0) {
  974. SDL_SetError("Invalid texture format");
  975. return NULL;
  976. }
  977. if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  978. SDL_SetError("Palettized textures are not supported");
  979. return NULL;
  980. }
  981. if (w <= 0 || h <= 0) {
  982. SDL_SetError("Texture dimensions can't be 0");
  983. return NULL;
  984. }
  985. if ((renderer->info.max_texture_width && w > renderer->info.max_texture_width) ||
  986. (renderer->info.max_texture_height && h > renderer->info.max_texture_height)) {
  987. SDL_SetError("Texture dimensions are limited to %dx%d", renderer->info.max_texture_width, renderer->info.max_texture_height);
  988. return NULL;
  989. }
  990. texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));
  991. if (!texture) {
  992. SDL_OutOfMemory();
  993. return NULL;
  994. }
  995. texture->magic = &texture_magic;
  996. texture->format = format;
  997. texture->access = access;
  998. texture->w = w;
  999. texture->h = h;
  1000. texture->r = 255;
  1001. texture->g = 255;
  1002. texture->b = 255;
  1003. texture->a = 255;
  1004. texture->scaleMode = SDL_GetScaleMode();
  1005. texture->renderer = renderer;
  1006. texture->next = renderer->textures;
  1007. if (renderer->textures) {
  1008. renderer->textures->prev = texture;
  1009. }
  1010. renderer->textures = texture;
  1011. if (IsSupportedFormat(renderer, format)) {
  1012. if (renderer->CreateTexture(renderer, texture) < 0) {
  1013. SDL_DestroyTexture(texture);
  1014. return NULL;
  1015. }
  1016. } else {
  1017. texture->native = SDL_CreateTexture(renderer,
  1018. GetClosestSupportedFormat(renderer, format),
  1019. access, w, h);
  1020. if (!texture->native) {
  1021. SDL_DestroyTexture(texture);
  1022. return NULL;
  1023. }
  1024. /* Swap textures to have texture before texture->native in the list */
  1025. texture->native->next = texture->next;
  1026. if (texture->native->next) {
  1027. texture->native->next->prev = texture->native;
  1028. }
  1029. texture->prev = texture->native->prev;
  1030. if (texture->prev) {
  1031. texture->prev->next = texture;
  1032. }
  1033. texture->native->prev = texture;
  1034. texture->next = texture->native;
  1035. renderer->textures = texture;
  1036. if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
  1037. texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
  1038. if (!texture->yuv) {
  1039. SDL_DestroyTexture(texture);
  1040. return NULL;
  1041. }
  1042. } else if (access == SDL_TEXTUREACCESS_STREAMING) {
  1043. /* The pitch is 4 byte aligned */
  1044. texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
  1045. texture->pixels = SDL_calloc(1, texture->pitch * h);
  1046. if (!texture->pixels) {
  1047. SDL_DestroyTexture(texture);
  1048. return NULL;
  1049. }
  1050. }
  1051. }
  1052. return texture;
  1053. }
  1054. SDL_Texture *
  1055. SDL_CreateTextureFromSurface(SDL_Renderer * renderer, SDL_Surface * surface)
  1056. {
  1057. const SDL_PixelFormat *fmt;
  1058. SDL_bool needAlpha;
  1059. SDL_bool direct_update;
  1060. int i;
  1061. Uint32 format = SDL_PIXELFORMAT_UNKNOWN;
  1062. SDL_Texture *texture;
  1063. CHECK_RENDERER_MAGIC(renderer, NULL);
  1064. if (!surface) {
  1065. SDL_SetError("SDL_CreateTextureFromSurface() passed NULL surface");
  1066. return NULL;
  1067. }
  1068. /* See what the best texture format is */
  1069. fmt = surface->format;
  1070. if (fmt->Amask || SDL_HasColorKey(surface)) {
  1071. needAlpha = SDL_TRUE;
  1072. } else {
  1073. needAlpha = SDL_FALSE;
  1074. }
  1075. /* If Palette contains alpha values, promotes to alpha format */
  1076. if (fmt->palette) {
  1077. for (i = 0; i < fmt->palette->ncolors; i++) {
  1078. Uint8 alpha_value = fmt->palette->colors[i].a;
  1079. if (alpha_value != 0 || alpha_value != SDL_ALPHA_OPAQUE) {
  1080. needAlpha = SDL_TRUE;
  1081. break;
  1082. }
  1083. }
  1084. }
  1085. /* Try to have the best pixel format for the texture */
  1086. /* No alpha, but a colorkey => promote to alpha */
  1087. if (!fmt->Amask && SDL_HasColorKey(surface)) {
  1088. if (fmt->format == SDL_PIXELFORMAT_RGB888) {
  1089. for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
  1090. if (renderer->info.texture_formats[i] == SDL_PIXELFORMAT_ARGB8888) {
  1091. format = SDL_PIXELFORMAT_ARGB8888;
  1092. break;
  1093. }
  1094. }
  1095. } else if (fmt->format == SDL_PIXELFORMAT_BGR888) {
  1096. for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
  1097. if (renderer->info.texture_formats[i] == SDL_PIXELFORMAT_ABGR8888) {
  1098. format = SDL_PIXELFORMAT_ABGR8888;
  1099. break;
  1100. }
  1101. }
  1102. }
  1103. } else {
  1104. /* Exact match would be fine */
  1105. for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
  1106. if (renderer->info.texture_formats[i] == fmt->format) {
  1107. format = fmt->format;
  1108. break;
  1109. }
  1110. }
  1111. }
  1112. /* Fallback, choose a valid pixel format */
  1113. if (format == SDL_PIXELFORMAT_UNKNOWN) {
  1114. format = renderer->info.texture_formats[0];
  1115. for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
  1116. if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
  1117. SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
  1118. format = renderer->info.texture_formats[i];
  1119. break;
  1120. }
  1121. }
  1122. }
  1123. texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
  1124. surface->w, surface->h);
  1125. if (!texture) {
  1126. return NULL;
  1127. }
  1128. if (format == surface->format->format) {
  1129. if (surface->format->Amask && SDL_HasColorKey(surface)) {
  1130. /* Surface and Renderer formats are identicals.
  1131. * Intermediate conversion is needed to convert color key to alpha (SDL_ConvertColorkeyToAlpha()). */
  1132. direct_update = SDL_FALSE;
  1133. } else {
  1134. /* Update Texture directly */
  1135. direct_update = SDL_TRUE;
  1136. }
  1137. } else {
  1138. /* Surface and Renderer formats are differents, it needs an intermediate conversion. */
  1139. direct_update = SDL_FALSE;
  1140. }
  1141. if (direct_update) {
  1142. if (SDL_MUSTLOCK(surface)) {
  1143. SDL_LockSurface(surface);
  1144. SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
  1145. SDL_UnlockSurface(surface);
  1146. } else {
  1147. SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
  1148. }
  1149. } else {
  1150. SDL_PixelFormat *dst_fmt;
  1151. SDL_Surface *temp = NULL;
  1152. /* Set up a destination surface for the texture update */
  1153. dst_fmt = SDL_AllocFormat(format);
  1154. if (!dst_fmt) {
  1155. SDL_DestroyTexture(texture);
  1156. return NULL;
  1157. }
  1158. temp = SDL_ConvertSurface(surface, dst_fmt, 0);
  1159. SDL_FreeFormat(dst_fmt);
  1160. if (temp) {
  1161. SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
  1162. SDL_FreeSurface(temp);
  1163. } else {
  1164. SDL_DestroyTexture(texture);
  1165. return NULL;
  1166. }
  1167. }
  1168. {
  1169. Uint8 r, g, b, a;
  1170. SDL_BlendMode blendMode;
  1171. SDL_GetSurfaceColorMod(surface, &r, &g, &b);
  1172. SDL_SetTextureColorMod(texture, r, g, b);
  1173. SDL_GetSurfaceAlphaMod(surface, &a);
  1174. SDL_SetTextureAlphaMod(texture, a);
  1175. if (SDL_HasColorKey(surface)) {
  1176. /* We converted to a texture with alpha format */
  1177. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  1178. } else {
  1179. SDL_GetSurfaceBlendMode(surface, &blendMode);
  1180. SDL_SetTextureBlendMode(texture, blendMode);
  1181. }
  1182. }
  1183. return texture;
  1184. }
  1185. int
  1186. SDL_QueryTexture(SDL_Texture * texture, Uint32 * format, int *access,
  1187. int *w, int *h)
  1188. {
  1189. CHECK_TEXTURE_MAGIC(texture, -1);
  1190. if (format) {
  1191. *format = texture->format;
  1192. }
  1193. if (access) {
  1194. *access = texture->access;
  1195. }
  1196. if (w) {
  1197. *w = texture->w;
  1198. }
  1199. if (h) {
  1200. *h = texture->h;
  1201. }
  1202. return 0;
  1203. }
  1204. int
  1205. SDL_SetTextureColorMod(SDL_Texture * texture, Uint8 r, Uint8 g, Uint8 b)
  1206. {
  1207. CHECK_TEXTURE_MAGIC(texture, -1);
  1208. if (r < 255 || g < 255 || b < 255) {
  1209. texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  1210. } else {
  1211. texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  1212. }
  1213. texture->r = r;
  1214. texture->g = g;
  1215. texture->b = b;
  1216. if (texture->native) {
  1217. return SDL_SetTextureColorMod(texture->native, r, g, b);
  1218. }
  1219. return 0;
  1220. }
  1221. int
  1222. SDL_GetTextureColorMod(SDL_Texture * texture, Uint8 * r, Uint8 * g,
  1223. Uint8 * b)
  1224. {
  1225. CHECK_TEXTURE_MAGIC(texture, -1);
  1226. if (r) {
  1227. *r = texture->r;
  1228. }
  1229. if (g) {
  1230. *g = texture->g;
  1231. }
  1232. if (b) {
  1233. *b = texture->b;
  1234. }
  1235. return 0;
  1236. }
  1237. int
  1238. SDL_SetTextureAlphaMod(SDL_Texture * texture, Uint8 alpha)
  1239. {
  1240. CHECK_TEXTURE_MAGIC(texture, -1);
  1241. if (alpha < 255) {
  1242. texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  1243. } else {
  1244. texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  1245. }
  1246. texture->a = alpha;
  1247. if (texture->native) {
  1248. return SDL_SetTextureAlphaMod(texture->native, alpha);
  1249. }
  1250. return 0;
  1251. }
  1252. int
  1253. SDL_GetTextureAlphaMod(SDL_Texture * texture, Uint8 * alpha)
  1254. {
  1255. CHECK_TEXTURE_MAGIC(texture, -1);
  1256. if (alpha) {
  1257. *alpha = texture->a;
  1258. }
  1259. return 0;
  1260. }
  1261. int
  1262. SDL_SetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode blendMode)
  1263. {
  1264. SDL_Renderer *renderer;
  1265. CHECK_TEXTURE_MAGIC(texture, -1);
  1266. renderer = texture->renderer;
  1267. if (!IsSupportedBlendMode(renderer, blendMode)) {
  1268. return SDL_Unsupported();
  1269. }
  1270. texture->blendMode = blendMode;
  1271. if (texture->native) {
  1272. return SDL_SetTextureBlendMode(texture->native, blendMode);
  1273. }
  1274. return 0;
  1275. }
  1276. int
  1277. SDL_GetTextureBlendMode(SDL_Texture * texture, SDL_BlendMode *blendMode)
  1278. {
  1279. CHECK_TEXTURE_MAGIC(texture, -1);
  1280. if (blendMode) {
  1281. *blendMode = texture->blendMode;
  1282. }
  1283. return 0;
  1284. }
  1285. static int
  1286. SDL_UpdateTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
  1287. const void *pixels, int pitch)
  1288. {
  1289. SDL_Texture *native = texture->native;
  1290. SDL_Rect full_rect;
  1291. if (SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch) < 0) {
  1292. return -1;
  1293. }
  1294. full_rect.x = 0;
  1295. full_rect.y = 0;
  1296. full_rect.w = texture->w;
  1297. full_rect.h = texture->h;
  1298. rect = &full_rect;
  1299. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1300. /* We can lock the texture and copy to it */
  1301. void *native_pixels = NULL;
  1302. int native_pitch = 0;
  1303. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1304. return -1;
  1305. }
  1306. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1307. rect->w, rect->h, native_pixels, native_pitch);
  1308. SDL_UnlockTexture(native);
  1309. } else {
  1310. /* Use a temporary buffer for updating */
  1311. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1312. const size_t alloclen = rect->h * temp_pitch;
  1313. if (alloclen > 0) {
  1314. void *temp_pixels = SDL_malloc(alloclen);
  1315. if (!temp_pixels) {
  1316. return SDL_OutOfMemory();
  1317. }
  1318. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1319. rect->w, rect->h, temp_pixels, temp_pitch);
  1320. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1321. SDL_free(temp_pixels);
  1322. }
  1323. }
  1324. return 0;
  1325. }
  1326. static int
  1327. SDL_UpdateTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
  1328. const void *pixels, int pitch)
  1329. {
  1330. SDL_Texture *native = texture->native;
  1331. if (!rect->w || !rect->h) {
  1332. return 0; /* nothing to do. */
  1333. }
  1334. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1335. /* We can lock the texture and copy to it */
  1336. void *native_pixels = NULL;
  1337. int native_pitch = 0;
  1338. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1339. return -1;
  1340. }
  1341. SDL_ConvertPixels(rect->w, rect->h,
  1342. texture->format, pixels, pitch,
  1343. native->format, native_pixels, native_pitch);
  1344. SDL_UnlockTexture(native);
  1345. } else {
  1346. /* Use a temporary buffer for updating */
  1347. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1348. const size_t alloclen = rect->h * temp_pitch;
  1349. if (alloclen > 0) {
  1350. void *temp_pixels = SDL_malloc(alloclen);
  1351. if (!temp_pixels) {
  1352. return SDL_OutOfMemory();
  1353. }
  1354. SDL_ConvertPixels(rect->w, rect->h,
  1355. texture->format, pixels, pitch,
  1356. native->format, temp_pixels, temp_pitch);
  1357. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1358. SDL_free(temp_pixels);
  1359. }
  1360. }
  1361. return 0;
  1362. }
  1363. int
  1364. SDL_UpdateTexture(SDL_Texture * texture, const SDL_Rect * rect,
  1365. const void *pixels, int pitch)
  1366. {
  1367. SDL_Rect full_rect;
  1368. CHECK_TEXTURE_MAGIC(texture, -1);
  1369. if (!pixels) {
  1370. return SDL_InvalidParamError("pixels");
  1371. }
  1372. if (!pitch) {
  1373. return SDL_InvalidParamError("pitch");
  1374. }
  1375. if (!rect) {
  1376. full_rect.x = 0;
  1377. full_rect.y = 0;
  1378. full_rect.w = texture->w;
  1379. full_rect.h = texture->h;
  1380. rect = &full_rect;
  1381. }
  1382. if ((rect->w == 0) || (rect->h == 0)) {
  1383. return 0; /* nothing to do. */
  1384. } else if (texture->yuv) {
  1385. return SDL_UpdateTextureYUV(texture, rect, pixels, pitch);
  1386. } else if (texture->native) {
  1387. return SDL_UpdateTextureNative(texture, rect, pixels, pitch);
  1388. } else {
  1389. SDL_Renderer *renderer = texture->renderer;
  1390. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1391. return -1;
  1392. }
  1393. return renderer->UpdateTexture(renderer, texture, rect, pixels, pitch);
  1394. }
  1395. }
  1396. static int
  1397. SDL_UpdateTextureYUVPlanar(SDL_Texture * texture, const SDL_Rect * rect,
  1398. const Uint8 *Yplane, int Ypitch,
  1399. const Uint8 *Uplane, int Upitch,
  1400. const Uint8 *Vplane, int Vpitch)
  1401. {
  1402. SDL_Texture *native = texture->native;
  1403. SDL_Rect full_rect;
  1404. if (SDL_SW_UpdateYUVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch) < 0) {
  1405. return -1;
  1406. }
  1407. full_rect.x = 0;
  1408. full_rect.y = 0;
  1409. full_rect.w = texture->w;
  1410. full_rect.h = texture->h;
  1411. rect = &full_rect;
  1412. if (!rect->w || !rect->h) {
  1413. return 0; /* nothing to do. */
  1414. }
  1415. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1416. /* We can lock the texture and copy to it */
  1417. void *native_pixels = NULL;
  1418. int native_pitch = 0;
  1419. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1420. return -1;
  1421. }
  1422. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1423. rect->w, rect->h, native_pixels, native_pitch);
  1424. SDL_UnlockTexture(native);
  1425. } else {
  1426. /* Use a temporary buffer for updating */
  1427. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1428. const size_t alloclen = rect->h * temp_pitch;
  1429. if (alloclen > 0) {
  1430. void *temp_pixels = SDL_malloc(alloclen);
  1431. if (!temp_pixels) {
  1432. return SDL_OutOfMemory();
  1433. }
  1434. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1435. rect->w, rect->h, temp_pixels, temp_pitch);
  1436. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1437. SDL_free(temp_pixels);
  1438. }
  1439. }
  1440. return 0;
  1441. }
  1442. int SDL_UpdateYUVTexture(SDL_Texture * texture, const SDL_Rect * rect,
  1443. const Uint8 *Yplane, int Ypitch,
  1444. const Uint8 *Uplane, int Upitch,
  1445. const Uint8 *Vplane, int Vpitch)
  1446. {
  1447. SDL_Renderer *renderer;
  1448. SDL_Rect full_rect;
  1449. CHECK_TEXTURE_MAGIC(texture, -1);
  1450. if (!Yplane) {
  1451. return SDL_InvalidParamError("Yplane");
  1452. }
  1453. if (!Ypitch) {
  1454. return SDL_InvalidParamError("Ypitch");
  1455. }
  1456. if (!Uplane) {
  1457. return SDL_InvalidParamError("Uplane");
  1458. }
  1459. if (!Upitch) {
  1460. return SDL_InvalidParamError("Upitch");
  1461. }
  1462. if (!Vplane) {
  1463. return SDL_InvalidParamError("Vplane");
  1464. }
  1465. if (!Vpitch) {
  1466. return SDL_InvalidParamError("Vpitch");
  1467. }
  1468. if (texture->format != SDL_PIXELFORMAT_YV12 &&
  1469. texture->format != SDL_PIXELFORMAT_IYUV) {
  1470. return SDL_SetError("Texture format must by YV12 or IYUV");
  1471. }
  1472. if (!rect) {
  1473. full_rect.x = 0;
  1474. full_rect.y = 0;
  1475. full_rect.w = texture->w;
  1476. full_rect.h = texture->h;
  1477. rect = &full_rect;
  1478. }
  1479. if (!rect->w || !rect->h) {
  1480. return 0; /* nothing to do. */
  1481. }
  1482. if (texture->yuv) {
  1483. return SDL_UpdateTextureYUVPlanar(texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
  1484. } else {
  1485. SDL_assert(!texture->native);
  1486. renderer = texture->renderer;
  1487. SDL_assert(renderer->UpdateTextureYUV);
  1488. if (renderer->UpdateTextureYUV) {
  1489. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1490. return -1;
  1491. }
  1492. return renderer->UpdateTextureYUV(renderer, texture, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
  1493. } else {
  1494. return SDL_Unsupported();
  1495. }
  1496. }
  1497. }
  1498. static int
  1499. SDL_LockTextureYUV(SDL_Texture * texture, const SDL_Rect * rect,
  1500. void **pixels, int *pitch)
  1501. {
  1502. return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
  1503. }
  1504. static int
  1505. SDL_LockTextureNative(SDL_Texture * texture, const SDL_Rect * rect,
  1506. void **pixels, int *pitch)
  1507. {
  1508. texture->locked_rect = *rect;
  1509. *pixels = (void *) ((Uint8 *) texture->pixels +
  1510. rect->y * texture->pitch +
  1511. rect->x * SDL_BYTESPERPIXEL(texture->format));
  1512. *pitch = texture->pitch;
  1513. return 0;
  1514. }
  1515. int
  1516. SDL_LockTexture(SDL_Texture * texture, const SDL_Rect * rect,
  1517. void **pixels, int *pitch)
  1518. {
  1519. SDL_Rect full_rect;
  1520. CHECK_TEXTURE_MAGIC(texture, -1);
  1521. if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1522. return SDL_SetError("SDL_LockTexture(): texture must be streaming");
  1523. }
  1524. if (!rect) {
  1525. full_rect.x = 0;
  1526. full_rect.y = 0;
  1527. full_rect.w = texture->w;
  1528. full_rect.h = texture->h;
  1529. rect = &full_rect;
  1530. }
  1531. if (texture->yuv) {
  1532. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1533. return -1;
  1534. }
  1535. return SDL_LockTextureYUV(texture, rect, pixels, pitch);
  1536. } else if (texture->native) {
  1537. /* Calls a real SDL_LockTexture/SDL_UnlockTexture on unlock, flushing then. */
  1538. return SDL_LockTextureNative(texture, rect, pixels, pitch);
  1539. } else {
  1540. SDL_Renderer *renderer = texture->renderer;
  1541. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1542. return -1;
  1543. }
  1544. return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
  1545. }
  1546. }
  1547. static void
  1548. SDL_UnlockTextureYUV(SDL_Texture * texture)
  1549. {
  1550. SDL_Texture *native = texture->native;
  1551. void *native_pixels = NULL;
  1552. int native_pitch = 0;
  1553. SDL_Rect rect;
  1554. rect.x = 0;
  1555. rect.y = 0;
  1556. rect.w = texture->w;
  1557. rect.h = texture->h;
  1558. if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
  1559. return;
  1560. }
  1561. SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
  1562. rect.w, rect.h, native_pixels, native_pitch);
  1563. SDL_UnlockTexture(native);
  1564. }
  1565. static void
  1566. SDL_UnlockTextureNative(SDL_Texture * texture)
  1567. {
  1568. SDL_Texture *native = texture->native;
  1569. void *native_pixels = NULL;
  1570. int native_pitch = 0;
  1571. const SDL_Rect *rect = &texture->locked_rect;
  1572. const void* pixels = (void *) ((Uint8 *) texture->pixels +
  1573. rect->y * texture->pitch +
  1574. rect->x * SDL_BYTESPERPIXEL(texture->format));
  1575. int pitch = texture->pitch;
  1576. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1577. return;
  1578. }
  1579. SDL_ConvertPixels(rect->w, rect->h,
  1580. texture->format, pixels, pitch,
  1581. native->format, native_pixels, native_pitch);
  1582. SDL_UnlockTexture(native);
  1583. }
  1584. void
  1585. SDL_UnlockTexture(SDL_Texture * texture)
  1586. {
  1587. CHECK_TEXTURE_MAGIC(texture, );
  1588. if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1589. return;
  1590. }
  1591. if (texture->yuv) {
  1592. SDL_UnlockTextureYUV(texture);
  1593. } else if (texture->native) {
  1594. SDL_UnlockTextureNative(texture);
  1595. } else {
  1596. SDL_Renderer *renderer = texture->renderer;
  1597. renderer->UnlockTexture(renderer, texture);
  1598. }
  1599. }
  1600. SDL_bool
  1601. SDL_RenderTargetSupported(SDL_Renderer *renderer)
  1602. {
  1603. if (!renderer || !renderer->SetRenderTarget) {
  1604. return SDL_FALSE;
  1605. }
  1606. return (renderer->info.flags & SDL_RENDERER_TARGETTEXTURE) != 0;
  1607. }
  1608. int
  1609. SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
  1610. {
  1611. if (!SDL_RenderTargetSupported(renderer)) {
  1612. return SDL_Unsupported();
  1613. }
  1614. if (texture == renderer->target) {
  1615. /* Nothing to do! */
  1616. return 0;
  1617. }
  1618. FlushRenderCommands(renderer); /* time to send everything to the GPU! */
  1619. /* texture == NULL is valid and means reset the target to the window */
  1620. if (texture) {
  1621. CHECK_TEXTURE_MAGIC(texture, -1);
  1622. if (renderer != texture->renderer) {
  1623. return SDL_SetError("Texture was not created with this renderer");
  1624. }
  1625. if (texture->access != SDL_TEXTUREACCESS_TARGET) {
  1626. return SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
  1627. }
  1628. if (texture->native) {
  1629. /* Always render to the native texture */
  1630. texture = texture->native;
  1631. }
  1632. }
  1633. SDL_LockMutex(renderer->target_mutex);
  1634. if (texture && !renderer->target) {
  1635. /* Make a backup of the viewport */
  1636. renderer->viewport_backup = renderer->viewport;
  1637. renderer->clip_rect_backup = renderer->clip_rect;
  1638. renderer->clipping_enabled_backup = renderer->clipping_enabled;
  1639. renderer->scale_backup = renderer->scale;
  1640. renderer->logical_w_backup = renderer->logical_w;
  1641. renderer->logical_h_backup = renderer->logical_h;
  1642. }
  1643. renderer->target = texture;
  1644. if (renderer->SetRenderTarget(renderer, texture) < 0) {
  1645. SDL_UnlockMutex(renderer->target_mutex);
  1646. return -1;
  1647. }
  1648. if (texture) {
  1649. renderer->viewport.x = 0;
  1650. renderer->viewport.y = 0;
  1651. renderer->viewport.w = texture->w;
  1652. renderer->viewport.h = texture->h;
  1653. SDL_zero(renderer->clip_rect);
  1654. renderer->clipping_enabled = SDL_FALSE;
  1655. renderer->scale.x = 1.0f;
  1656. renderer->scale.y = 1.0f;
  1657. renderer->logical_w = texture->w;
  1658. renderer->logical_h = texture->h;
  1659. } else {
  1660. renderer->viewport = renderer->viewport_backup;
  1661. renderer->clip_rect = renderer->clip_rect_backup;
  1662. renderer->clipping_enabled = renderer->clipping_enabled_backup;
  1663. renderer->scale = renderer->scale_backup;
  1664. renderer->logical_w = renderer->logical_w_backup;
  1665. renderer->logical_h = renderer->logical_h_backup;
  1666. }
  1667. SDL_UnlockMutex(renderer->target_mutex);
  1668. if (QueueCmdSetViewport(renderer) < 0) {
  1669. return -1;
  1670. }
  1671. if (QueueCmdSetClipRect(renderer) < 0) {
  1672. return -1;
  1673. }
  1674. /* All set! */
  1675. return FlushRenderCommandsIfNotBatching(renderer);
  1676. }
  1677. SDL_Texture *
  1678. SDL_GetRenderTarget(SDL_Renderer *renderer)
  1679. {
  1680. return renderer->target;
  1681. }
  1682. static int
  1683. UpdateLogicalSize(SDL_Renderer *renderer)
  1684. {
  1685. int w = 1, h = 1;
  1686. float want_aspect;
  1687. float real_aspect;
  1688. float scale;
  1689. SDL_Rect viewport;
  1690. /* 0 is for letterbox, 1 is for overscan */
  1691. int scale_policy = 0;
  1692. const char *hint;
  1693. if (!renderer->logical_w || !renderer->logical_h) {
  1694. return 0;
  1695. }
  1696. if (SDL_GetRendererOutputSize(renderer, &w, &h) < 0) {
  1697. return -1;
  1698. }
  1699. hint = SDL_GetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE);
  1700. if (hint && (*hint == '1' || SDL_strcasecmp(hint, "overscan") == 0)) {
  1701. #if SDL_VIDEO_RENDER_D3D
  1702. SDL_bool overscan_supported = SDL_TRUE;
  1703. /* Unfortunately, Direct3D 9 doesn't support negative viewport numbers
  1704. which the overscan implementation relies on.
  1705. */
  1706. if (SDL_strcasecmp(SDL_GetCurrentVideoDriver(), "direct3d") == 0) {
  1707. overscan_supported = SDL_FALSE;
  1708. }
  1709. if (overscan_supported) {
  1710. scale_policy = 1;
  1711. }
  1712. #else
  1713. scale_policy = 1;
  1714. #endif
  1715. }
  1716. want_aspect = (float)renderer->logical_w / renderer->logical_h;
  1717. real_aspect = (float)w / h;
  1718. /* Clear the scale because we're setting viewport in output coordinates */
  1719. SDL_RenderSetScale(renderer, 1.0f, 1.0f);
  1720. if (renderer->integer_scale) {
  1721. if (want_aspect > real_aspect) {
  1722. scale = (float)(w / renderer->logical_w);
  1723. } else {
  1724. scale = (float)(h / renderer->logical_h);
  1725. }
  1726. viewport.w = (int)SDL_ceil(renderer->logical_w * scale);
  1727. viewport.x = (w - viewport.w) / 2;
  1728. viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
  1729. viewport.y = (h - viewport.h) / 2;
  1730. SDL_RenderSetViewport(renderer, &viewport);
  1731. } else if (SDL_fabs(want_aspect-real_aspect) < 0.0001) {
  1732. /* The aspect ratios are the same, just scale appropriately */
  1733. scale = (float)w / renderer->logical_w;
  1734. SDL_RenderSetViewport(renderer, NULL);
  1735. } else if (want_aspect > real_aspect) {
  1736. if (scale_policy == 1) {
  1737. /* We want a wider aspect ratio than is available -
  1738. zoom so logical height matches the real height
  1739. and the width will grow off the screen
  1740. */
  1741. scale = (float)h / renderer->logical_h;
  1742. viewport.y = 0;
  1743. viewport.h = h;
  1744. viewport.w = (int)SDL_ceil(renderer->logical_w * scale);
  1745. viewport.x = (w - viewport.w) / 2;
  1746. SDL_RenderSetViewport(renderer, &viewport);
  1747. } else {
  1748. /* We want a wider aspect ratio than is available - letterbox it */
  1749. scale = (float)w / renderer->logical_w;
  1750. viewport.x = 0;
  1751. viewport.w = w;
  1752. viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
  1753. viewport.y = (h - viewport.h) / 2;
  1754. SDL_RenderSetViewport(renderer, &viewport);
  1755. }
  1756. } else {
  1757. if (scale_policy == 1) {
  1758. /* We want a narrower aspect ratio than is available -
  1759. zoom so logical width matches the real width
  1760. and the height will grow off the screen
  1761. */
  1762. scale = (float)w / renderer->logical_w;
  1763. viewport.x = 0;
  1764. viewport.w = w;
  1765. viewport.h = (int)SDL_ceil(renderer->logical_h * scale);
  1766. viewport.y = (h - viewport.h) / 2;
  1767. SDL_RenderSetViewport(renderer, &viewport);
  1768. } else {
  1769. /* We want a narrower aspect ratio than is available - use side-bars */
  1770. scale = (float)h / renderer->logical_h;
  1771. viewport.y = 0;
  1772. viewport.h = h;
  1773. viewport.w = (int)SDL_ceil(renderer->logical_w * scale);
  1774. viewport.x = (w - viewport.w) / 2;
  1775. SDL_RenderSetViewport(renderer, &viewport);
  1776. }
  1777. }
  1778. /* Set the new scale */
  1779. SDL_RenderSetScale(renderer, scale, scale);
  1780. return 0;
  1781. }
  1782. int
  1783. SDL_RenderSetLogicalSize(SDL_Renderer * renderer, int w, int h)
  1784. {
  1785. CHECK_RENDERER_MAGIC(renderer, -1);
  1786. if (!w || !h) {
  1787. /* Clear any previous logical resolution */
  1788. renderer->logical_w = 0;
  1789. renderer->logical_h = 0;
  1790. SDL_RenderSetViewport(renderer, NULL);
  1791. SDL_RenderSetScale(renderer, 1.0f, 1.0f);
  1792. return 0;
  1793. }
  1794. renderer->logical_w = w;
  1795. renderer->logical_h = h;
  1796. return UpdateLogicalSize(renderer);
  1797. }
  1798. void
  1799. SDL_RenderGetLogicalSize(SDL_Renderer * renderer, int *w, int *h)
  1800. {
  1801. CHECK_RENDERER_MAGIC(renderer, );
  1802. if (w) {
  1803. *w = renderer->logical_w;
  1804. }
  1805. if (h) {
  1806. *h = renderer->logical_h;
  1807. }
  1808. }
  1809. int
  1810. SDL_RenderSetIntegerScale(SDL_Renderer * renderer, SDL_bool enable)
  1811. {
  1812. CHECK_RENDERER_MAGIC(renderer, -1);
  1813. renderer->integer_scale = enable;
  1814. return UpdateLogicalSize(renderer);
  1815. }
  1816. SDL_bool
  1817. SDLCALL SDL_RenderGetIntegerScale(SDL_Renderer * renderer)
  1818. {
  1819. CHECK_RENDERER_MAGIC(renderer, SDL_FALSE);
  1820. return renderer->integer_scale;
  1821. }
  1822. int
  1823. SDL_RenderSetViewport(SDL_Renderer * renderer, const SDL_Rect * rect)
  1824. {
  1825. int retval;
  1826. CHECK_RENDERER_MAGIC(renderer, -1);
  1827. if (rect) {
  1828. renderer->viewport.x = (int)SDL_floor(rect->x * renderer->scale.x);
  1829. renderer->viewport.y = (int)SDL_floor(rect->y * renderer->scale.y);
  1830. renderer->viewport.w = (int)SDL_ceil(rect->w * renderer->scale.x);
  1831. renderer->viewport.h = (int)SDL_ceil(rect->h * renderer->scale.y);
  1832. } else {
  1833. renderer->viewport.x = 0;
  1834. renderer->viewport.y = 0;
  1835. if (SDL_GetRendererOutputSize(renderer, &renderer->viewport.w, &renderer->viewport.h) < 0) {
  1836. return -1;
  1837. }
  1838. }
  1839. retval = QueueCmdSetViewport(renderer);
  1840. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  1841. }
  1842. void
  1843. SDL_RenderGetViewport(SDL_Renderer * renderer, SDL_Rect * rect)
  1844. {
  1845. CHECK_RENDERER_MAGIC(renderer, );
  1846. if (rect) {
  1847. rect->x = (int)(renderer->viewport.x / renderer->scale.x);
  1848. rect->y = (int)(renderer->viewport.y / renderer->scale.y);
  1849. rect->w = (int)(renderer->viewport.w / renderer->scale.x);
  1850. rect->h = (int)(renderer->viewport.h / renderer->scale.y);
  1851. }
  1852. }
  1853. int
  1854. SDL_RenderSetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
  1855. {
  1856. int retval;
  1857. CHECK_RENDERER_MAGIC(renderer, -1)
  1858. if (rect) {
  1859. renderer->clipping_enabled = SDL_TRUE;
  1860. renderer->clip_rect.x = (int)SDL_floor(rect->x * renderer->scale.x);
  1861. renderer->clip_rect.y = (int)SDL_floor(rect->y * renderer->scale.y);
  1862. renderer->clip_rect.w = (int)SDL_ceil(rect->w * renderer->scale.x);
  1863. renderer->clip_rect.h = (int)SDL_ceil(rect->h * renderer->scale.y);
  1864. } else {
  1865. renderer->clipping_enabled = SDL_FALSE;
  1866. SDL_zero(renderer->clip_rect);
  1867. }
  1868. retval = QueueCmdSetClipRect(renderer);
  1869. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  1870. }
  1871. void
  1872. SDL_RenderGetClipRect(SDL_Renderer * renderer, SDL_Rect * rect)
  1873. {
  1874. CHECK_RENDERER_MAGIC(renderer, )
  1875. if (rect) {
  1876. rect->x = (int)(renderer->clip_rect.x / renderer->scale.x);
  1877. rect->y = (int)(renderer->clip_rect.y / renderer->scale.y);
  1878. rect->w = (int)(renderer->clip_rect.w / renderer->scale.x);
  1879. rect->h = (int)(renderer->clip_rect.h / renderer->scale.y);
  1880. }
  1881. }
  1882. SDL_bool
  1883. SDL_RenderIsClipEnabled(SDL_Renderer * renderer)
  1884. {
  1885. CHECK_RENDERER_MAGIC(renderer, SDL_FALSE)
  1886. return renderer->clipping_enabled;
  1887. }
  1888. int
  1889. SDL_RenderSetScale(SDL_Renderer * renderer, float scaleX, float scaleY)
  1890. {
  1891. CHECK_RENDERER_MAGIC(renderer, -1);
  1892. renderer->scale.x = scaleX;
  1893. renderer->scale.y = scaleY;
  1894. return 0;
  1895. }
  1896. void
  1897. SDL_RenderGetScale(SDL_Renderer * renderer, float *scaleX, float *scaleY)
  1898. {
  1899. CHECK_RENDERER_MAGIC(renderer, );
  1900. if (scaleX) {
  1901. *scaleX = renderer->scale.x;
  1902. }
  1903. if (scaleY) {
  1904. *scaleY = renderer->scale.y;
  1905. }
  1906. }
  1907. int
  1908. SDL_SetRenderDrawColor(SDL_Renderer * renderer,
  1909. Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1910. {
  1911. CHECK_RENDERER_MAGIC(renderer, -1);
  1912. renderer->r = r;
  1913. renderer->g = g;
  1914. renderer->b = b;
  1915. renderer->a = a;
  1916. return 0;
  1917. }
  1918. int
  1919. SDL_GetRenderDrawColor(SDL_Renderer * renderer,
  1920. Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
  1921. {
  1922. CHECK_RENDERER_MAGIC(renderer, -1);
  1923. if (r) {
  1924. *r = renderer->r;
  1925. }
  1926. if (g) {
  1927. *g = renderer->g;
  1928. }
  1929. if (b) {
  1930. *b = renderer->b;
  1931. }
  1932. if (a) {
  1933. *a = renderer->a;
  1934. }
  1935. return 0;
  1936. }
  1937. int
  1938. SDL_SetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode blendMode)
  1939. {
  1940. CHECK_RENDERER_MAGIC(renderer, -1);
  1941. if (!IsSupportedBlendMode(renderer, blendMode)) {
  1942. return SDL_Unsupported();
  1943. }
  1944. renderer->blendMode = blendMode;
  1945. return 0;
  1946. }
  1947. int
  1948. SDL_GetRenderDrawBlendMode(SDL_Renderer * renderer, SDL_BlendMode *blendMode)
  1949. {
  1950. CHECK_RENDERER_MAGIC(renderer, -1);
  1951. *blendMode = renderer->blendMode;
  1952. return 0;
  1953. }
  1954. int
  1955. SDL_RenderClear(SDL_Renderer * renderer)
  1956. {
  1957. int retval;
  1958. CHECK_RENDERER_MAGIC(renderer, -1);
  1959. retval = QueueCmdClear(renderer);
  1960. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  1961. }
  1962. /* !!! FIXME: delete all the duplicate code for the integer versions in 2.1,
  1963. !!! FIXME: making the floating point versions the only available APIs. */
  1964. int
  1965. SDL_RenderDrawPoint(SDL_Renderer * renderer, int x, int y)
  1966. {
  1967. SDL_FPoint fpoint;
  1968. fpoint.x = (float) x;
  1969. fpoint.y = (float) y;
  1970. return SDL_RenderDrawPointsF(renderer, &fpoint, 1);
  1971. }
  1972. int
  1973. SDL_RenderDrawPointF(SDL_Renderer * renderer, float x, float y)
  1974. {
  1975. SDL_FPoint fpoint;
  1976. fpoint.x = x;
  1977. fpoint.y = y;
  1978. return SDL_RenderDrawPointsF(renderer, &fpoint, 1);
  1979. }
  1980. static int
  1981. RenderDrawPointsWithRects(SDL_Renderer * renderer,
  1982. const SDL_Point * points, const int count)
  1983. {
  1984. int retval = -1;
  1985. SDL_bool isstack;
  1986. SDL_FRect *frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  1987. int i;
  1988. if (!frects) {
  1989. return SDL_OutOfMemory();
  1990. }
  1991. for (i = 0; i < count; ++i) {
  1992. frects[i].x = points[i].x * renderer->scale.x;
  1993. frects[i].y = points[i].y * renderer->scale.y;
  1994. frects[i].w = renderer->scale.x;
  1995. frects[i].h = renderer->scale.y;
  1996. }
  1997. retval = QueueCmdFillRects(renderer, frects, count);
  1998. SDL_small_free(frects, isstack);
  1999. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2000. }
  2001. int
  2002. SDL_RenderDrawPoints(SDL_Renderer * renderer,
  2003. const SDL_Point * points, int count)
  2004. {
  2005. SDL_FPoint *fpoints;
  2006. int i;
  2007. int retval;
  2008. SDL_bool isstack;
  2009. CHECK_RENDERER_MAGIC(renderer, -1);
  2010. if (!points) {
  2011. return SDL_SetError("SDL_RenderDrawPoints(): Passed NULL points");
  2012. }
  2013. if (count < 1) {
  2014. return 0;
  2015. }
  2016. /* Don't draw while we're hidden */
  2017. if (renderer->hidden) {
  2018. return 0;
  2019. }
  2020. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  2021. return RenderDrawPointsWithRects(renderer, points, count);
  2022. }
  2023. fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
  2024. if (!fpoints) {
  2025. return SDL_OutOfMemory();
  2026. }
  2027. for (i = 0; i < count; ++i) {
  2028. fpoints[i].x = points[i].x * renderer->scale.x;
  2029. fpoints[i].y = points[i].y * renderer->scale.y;
  2030. }
  2031. retval = QueueCmdDrawPoints(renderer, fpoints, count);
  2032. SDL_small_free(fpoints, isstack);
  2033. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2034. }
  2035. static int
  2036. RenderDrawPointsWithRectsF(SDL_Renderer * renderer,
  2037. const SDL_FPoint * fpoints, const int count)
  2038. {
  2039. int retval = -1;
  2040. SDL_bool isstack;
  2041. SDL_FRect *frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  2042. int i;
  2043. if (!frects) {
  2044. return SDL_OutOfMemory();
  2045. }
  2046. for (i = 0; i < count; ++i) {
  2047. frects[i].x = fpoints[i].x * renderer->scale.x;
  2048. frects[i].y = fpoints[i].y * renderer->scale.y;
  2049. frects[i].w = renderer->scale.x;
  2050. frects[i].h = renderer->scale.y;
  2051. }
  2052. retval = QueueCmdFillRects(renderer, frects, count);
  2053. SDL_small_free(frects, isstack);
  2054. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2055. }
  2056. int
  2057. SDL_RenderDrawPointsF(SDL_Renderer * renderer,
  2058. const SDL_FPoint * points, int count)
  2059. {
  2060. SDL_FPoint *fpoints;
  2061. int i;
  2062. int retval;
  2063. SDL_bool isstack;
  2064. CHECK_RENDERER_MAGIC(renderer, -1);
  2065. if (!points) {
  2066. return SDL_SetError("SDL_RenderDrawFPoints(): Passed NULL points");
  2067. }
  2068. if (count < 1) {
  2069. return 0;
  2070. }
  2071. /* Don't draw while we're hidden */
  2072. if (renderer->hidden) {
  2073. return 0;
  2074. }
  2075. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  2076. return RenderDrawPointsWithRectsF(renderer, points, count);
  2077. }
  2078. fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
  2079. if (!fpoints) {
  2080. return SDL_OutOfMemory();
  2081. }
  2082. for (i = 0; i < count; ++i) {
  2083. fpoints[i].x = points[i].x * renderer->scale.x;
  2084. fpoints[i].y = points[i].y * renderer->scale.y;
  2085. }
  2086. retval = QueueCmdDrawPoints(renderer, fpoints, count);
  2087. SDL_small_free(fpoints, isstack);
  2088. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2089. }
  2090. int
  2091. SDL_RenderDrawLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
  2092. {
  2093. SDL_FPoint points[2];
  2094. points[0].x = (float) x1;
  2095. points[0].y = (float) y1;
  2096. points[1].x = (float) x2;
  2097. points[1].y = (float) y2;
  2098. return SDL_RenderDrawLinesF(renderer, points, 2);
  2099. }
  2100. int
  2101. SDL_RenderDrawLineF(SDL_Renderer * renderer, float x1, float y1, float x2, float y2)
  2102. {
  2103. SDL_FPoint points[2];
  2104. points[0].x = x1;
  2105. points[0].y = y1;
  2106. points[1].x = x2;
  2107. points[1].y = y2;
  2108. return SDL_RenderDrawLinesF(renderer, points, 2);
  2109. }
  2110. static int
  2111. RenderDrawLinesWithRects(SDL_Renderer * renderer,
  2112. const SDL_Point * points, const int count)
  2113. {
  2114. SDL_FRect *frect;
  2115. SDL_FRect *frects;
  2116. SDL_FPoint fpoints[2];
  2117. int i, nrects = 0;
  2118. int retval = 0;
  2119. SDL_bool isstack;
  2120. frects = SDL_small_alloc(SDL_FRect, count-1, &isstack);
  2121. if (!frects) {
  2122. return SDL_OutOfMemory();
  2123. }
  2124. for (i = 0; i < count-1; ++i) {
  2125. if (points[i].x == points[i+1].x) {
  2126. const int minY = SDL_min(points[i].y, points[i+1].y);
  2127. const int maxY = SDL_max(points[i].y, points[i+1].y);
  2128. frect = &frects[nrects++];
  2129. frect->x = points[i].x * renderer->scale.x;
  2130. frect->y = minY * renderer->scale.y;
  2131. frect->w = renderer->scale.x;
  2132. frect->h = (maxY - minY + 1) * renderer->scale.y;
  2133. } else if (points[i].y == points[i+1].y) {
  2134. const int minX = SDL_min(points[i].x, points[i+1].x);
  2135. const int maxX = SDL_max(points[i].x, points[i+1].x);
  2136. frect = &frects[nrects++];
  2137. frect->x = minX * renderer->scale.x;
  2138. frect->y = points[i].y * renderer->scale.y;
  2139. frect->w = (maxX - minX + 1) * renderer->scale.x;
  2140. frect->h = renderer->scale.y;
  2141. } else {
  2142. /* FIXME: We can't use a rect for this line... */
  2143. fpoints[0].x = points[i].x * renderer->scale.x;
  2144. fpoints[0].y = points[i].y * renderer->scale.y;
  2145. fpoints[1].x = points[i+1].x * renderer->scale.x;
  2146. fpoints[1].y = points[i+1].y * renderer->scale.y;
  2147. retval += QueueCmdDrawLines(renderer, fpoints, 2);
  2148. }
  2149. }
  2150. retval += QueueCmdFillRects(renderer, frects, nrects);
  2151. SDL_small_free(frects, isstack);
  2152. if (retval < 0) {
  2153. retval = -1;
  2154. }
  2155. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2156. }
  2157. static int
  2158. RenderDrawLinesWithRectsF(SDL_Renderer * renderer,
  2159. const SDL_FPoint * points, const int count)
  2160. {
  2161. SDL_FRect *frect;
  2162. SDL_FRect *frects;
  2163. SDL_FPoint fpoints[2];
  2164. int i, nrects = 0;
  2165. int retval = 0;
  2166. SDL_bool isstack;
  2167. frects = SDL_small_alloc(SDL_FRect, count-1, &isstack);
  2168. if (!frects) {
  2169. return SDL_OutOfMemory();
  2170. }
  2171. for (i = 0; i < count-1; ++i) {
  2172. if (points[i].x == points[i+1].x) {
  2173. const int minY = (int)SDL_min(points[i].y, points[i+1].y);
  2174. const int maxY = (int)SDL_max(points[i].y, points[i+1].y);
  2175. frect = &frects[nrects++];
  2176. frect->x = points[i].x * renderer->scale.x;
  2177. frect->y = minY * renderer->scale.y;
  2178. frect->w = renderer->scale.x;
  2179. frect->h = (maxY - minY + 1) * renderer->scale.y;
  2180. } else if (points[i].y == points[i+1].y) {
  2181. const int minX = (int)SDL_min(points[i].x, points[i+1].x);
  2182. const int maxX = (int)SDL_max(points[i].x, points[i+1].x);
  2183. frect = &frects[nrects++];
  2184. frect->x = minX * renderer->scale.x;
  2185. frect->y = points[i].y * renderer->scale.y;
  2186. frect->w = (maxX - minX + 1) * renderer->scale.x;
  2187. frect->h = renderer->scale.y;
  2188. } else {
  2189. /* FIXME: We can't use a rect for this line... */
  2190. fpoints[0].x = points[i].x * renderer->scale.x;
  2191. fpoints[0].y = points[i].y * renderer->scale.y;
  2192. fpoints[1].x = points[i+1].x * renderer->scale.x;
  2193. fpoints[1].y = points[i+1].y * renderer->scale.y;
  2194. retval += QueueCmdDrawLines(renderer, fpoints, 2);
  2195. }
  2196. }
  2197. retval += QueueCmdFillRects(renderer, frects, nrects);
  2198. SDL_small_free(frects, isstack);
  2199. if (retval < 0) {
  2200. retval = -1;
  2201. }
  2202. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2203. }
  2204. int
  2205. SDL_RenderDrawLines(SDL_Renderer * renderer,
  2206. const SDL_Point * points, int count)
  2207. {
  2208. SDL_FPoint *fpoints;
  2209. int i;
  2210. int retval;
  2211. SDL_bool isstack;
  2212. CHECK_RENDERER_MAGIC(renderer, -1);
  2213. if (!points) {
  2214. return SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
  2215. }
  2216. if (count < 2) {
  2217. return 0;
  2218. }
  2219. /* Don't draw while we're hidden */
  2220. if (renderer->hidden) {
  2221. return 0;
  2222. }
  2223. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  2224. return RenderDrawLinesWithRects(renderer, points, count);
  2225. }
  2226. fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
  2227. if (!fpoints) {
  2228. return SDL_OutOfMemory();
  2229. }
  2230. for (i = 0; i < count; ++i) {
  2231. fpoints[i].x = points[i].x * renderer->scale.x;
  2232. fpoints[i].y = points[i].y * renderer->scale.y;
  2233. }
  2234. retval = QueueCmdDrawLines(renderer, fpoints, count);
  2235. SDL_small_free(fpoints, isstack);
  2236. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2237. }
  2238. int
  2239. SDL_RenderDrawLinesF(SDL_Renderer * renderer,
  2240. const SDL_FPoint * points, int count)
  2241. {
  2242. SDL_FPoint *fpoints;
  2243. int i;
  2244. int retval;
  2245. SDL_bool isstack;
  2246. CHECK_RENDERER_MAGIC(renderer, -1);
  2247. if (!points) {
  2248. return SDL_SetError("SDL_RenderDrawLines(): Passed NULL points");
  2249. }
  2250. if (count < 2) {
  2251. return 0;
  2252. }
  2253. /* Don't draw while we're hidden */
  2254. if (renderer->hidden) {
  2255. return 0;
  2256. }
  2257. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  2258. return RenderDrawLinesWithRectsF(renderer, points, count);
  2259. }
  2260. fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
  2261. if (!fpoints) {
  2262. return SDL_OutOfMemory();
  2263. }
  2264. for (i = 0; i < count; ++i) {
  2265. fpoints[i].x = points[i].x * renderer->scale.x;
  2266. fpoints[i].y = points[i].y * renderer->scale.y;
  2267. }
  2268. retval = QueueCmdDrawLines(renderer, fpoints, count);
  2269. SDL_small_free(fpoints, isstack);
  2270. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2271. }
  2272. int
  2273. SDL_RenderDrawRect(SDL_Renderer * renderer, const SDL_Rect * rect)
  2274. {
  2275. SDL_FRect frect;
  2276. SDL_FRect *prect = NULL;
  2277. if (rect) {
  2278. frect.x = (float) rect->x;
  2279. frect.y = (float) rect->y;
  2280. frect.w = (float) rect->w;
  2281. frect.h = (float) rect->h;
  2282. prect = &frect;
  2283. }
  2284. return SDL_RenderDrawRectF(renderer, prect);
  2285. }
  2286. int
  2287. SDL_RenderDrawRectF(SDL_Renderer * renderer, const SDL_FRect * rect)
  2288. {
  2289. SDL_FRect frect;
  2290. SDL_FPoint points[5];
  2291. CHECK_RENDERER_MAGIC(renderer, -1);
  2292. /* If 'rect' == NULL, then outline the whole surface */
  2293. if (!rect) {
  2294. SDL_Rect r;
  2295. SDL_RenderGetViewport(renderer, &r);
  2296. frect.x = 0.0f;
  2297. frect.y = 0.0f;
  2298. frect.w = (float) r.w;
  2299. frect.h = (float) r.h;
  2300. rect = &frect;
  2301. }
  2302. points[0].x = rect->x;
  2303. points[0].y = rect->y;
  2304. points[1].x = rect->x+rect->w-1;
  2305. points[1].y = rect->y;
  2306. points[2].x = rect->x+rect->w-1;
  2307. points[2].y = rect->y+rect->h-1;
  2308. points[3].x = rect->x;
  2309. points[3].y = rect->y+rect->h-1;
  2310. points[4].x = rect->x;
  2311. points[4].y = rect->y;
  2312. return SDL_RenderDrawLinesF(renderer, points, 5);
  2313. }
  2314. int
  2315. SDL_RenderDrawRects(SDL_Renderer * renderer,
  2316. const SDL_Rect * rects, int count)
  2317. {
  2318. int i;
  2319. CHECK_RENDERER_MAGIC(renderer, -1);
  2320. if (!rects) {
  2321. return SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
  2322. }
  2323. if (count < 1) {
  2324. return 0;
  2325. }
  2326. /* Don't draw while we're hidden */
  2327. if (renderer->hidden) {
  2328. return 0;
  2329. }
  2330. for (i = 0; i < count; ++i) {
  2331. if (SDL_RenderDrawRect(renderer, &rects[i]) < 0) {
  2332. return -1;
  2333. }
  2334. }
  2335. return 0;
  2336. }
  2337. int
  2338. SDL_RenderDrawRectsF(SDL_Renderer * renderer,
  2339. const SDL_FRect * rects, int count)
  2340. {
  2341. int i;
  2342. CHECK_RENDERER_MAGIC(renderer, -1);
  2343. if (!rects) {
  2344. return SDL_SetError("SDL_RenderDrawRects(): Passed NULL rects");
  2345. }
  2346. if (count < 1) {
  2347. return 0;
  2348. }
  2349. /* Don't draw while we're hidden */
  2350. if (renderer->hidden) {
  2351. return 0;
  2352. }
  2353. for (i = 0; i < count; ++i) {
  2354. if (SDL_RenderDrawRectF(renderer, &rects[i]) < 0) {
  2355. return -1;
  2356. }
  2357. }
  2358. return 0;
  2359. }
  2360. int
  2361. SDL_RenderFillRect(SDL_Renderer * renderer, const SDL_Rect * rect)
  2362. {
  2363. SDL_FRect frect;
  2364. CHECK_RENDERER_MAGIC(renderer, -1);
  2365. /* If 'rect' == NULL, then outline the whole surface */
  2366. if (rect) {
  2367. frect.x = (float) rect->x;
  2368. frect.y = (float) rect->y;
  2369. frect.w = (float) rect->w;
  2370. frect.h = (float) rect->h;
  2371. } else {
  2372. SDL_Rect r;
  2373. SDL_zero(r);
  2374. SDL_RenderGetViewport(renderer, &r);
  2375. frect.x = 0.0f;
  2376. frect.y = 0.0f;
  2377. frect.w = (float) r.w;
  2378. frect.h = (float) r.h;
  2379. }
  2380. return SDL_RenderFillRectsF(renderer, &frect, 1);
  2381. }
  2382. int
  2383. SDL_RenderFillRectF(SDL_Renderer * renderer, const SDL_FRect * rect)
  2384. {
  2385. SDL_FRect frect;
  2386. CHECK_RENDERER_MAGIC(renderer, -1);
  2387. /* If 'rect' == NULL, then outline the whole surface */
  2388. if (!rect) {
  2389. SDL_Rect r;
  2390. SDL_zero(r);
  2391. SDL_RenderGetViewport(renderer, &r);
  2392. frect.x = 0.0f;
  2393. frect.y = 0.0f;
  2394. frect.w = (float) r.w;
  2395. frect.h = (float) r.h;
  2396. rect = &frect;
  2397. }
  2398. return SDL_RenderFillRectsF(renderer, rect, 1);
  2399. }
  2400. int
  2401. SDL_RenderFillRects(SDL_Renderer * renderer,
  2402. const SDL_Rect * rects, int count)
  2403. {
  2404. SDL_FRect *frects;
  2405. int i;
  2406. int retval;
  2407. SDL_bool isstack;
  2408. CHECK_RENDERER_MAGIC(renderer, -1);
  2409. if (!rects) {
  2410. return SDL_SetError("SDL_RenderFillRects(): Passed NULL rects");
  2411. }
  2412. if (count < 1) {
  2413. return 0;
  2414. }
  2415. /* Don't draw while we're hidden */
  2416. if (renderer->hidden) {
  2417. return 0;
  2418. }
  2419. frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  2420. if (!frects) {
  2421. return SDL_OutOfMemory();
  2422. }
  2423. for (i = 0; i < count; ++i) {
  2424. frects[i].x = rects[i].x * renderer->scale.x;
  2425. frects[i].y = rects[i].y * renderer->scale.y;
  2426. frects[i].w = rects[i].w * renderer->scale.x;
  2427. frects[i].h = rects[i].h * renderer->scale.y;
  2428. }
  2429. retval = QueueCmdFillRects(renderer, frects, count);
  2430. SDL_small_free(frects, isstack);
  2431. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2432. }
  2433. int
  2434. SDL_RenderFillRectsF(SDL_Renderer * renderer,
  2435. const SDL_FRect * rects, int count)
  2436. {
  2437. SDL_FRect *frects;
  2438. int i;
  2439. int retval;
  2440. SDL_bool isstack;
  2441. CHECK_RENDERER_MAGIC(renderer, -1);
  2442. if (!rects) {
  2443. return SDL_SetError("SDL_RenderFillFRects(): Passed NULL rects");
  2444. }
  2445. if (count < 1) {
  2446. return 0;
  2447. }
  2448. /* Don't draw while we're hidden */
  2449. if (renderer->hidden) {
  2450. return 0;
  2451. }
  2452. frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  2453. if (!frects) {
  2454. return SDL_OutOfMemory();
  2455. }
  2456. for (i = 0; i < count; ++i) {
  2457. frects[i].x = rects[i].x * renderer->scale.x;
  2458. frects[i].y = rects[i].y * renderer->scale.y;
  2459. frects[i].w = rects[i].w * renderer->scale.x;
  2460. frects[i].h = rects[i].h * renderer->scale.y;
  2461. }
  2462. retval = QueueCmdFillRects(renderer, frects, count);
  2463. SDL_small_free(frects, isstack);
  2464. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2465. }
  2466. /* !!! FIXME: move this to a public API if we want to do float versions of all of these later */
  2467. SDL_FORCE_INLINE SDL_bool SDL_FRectEmpty(const SDL_FRect *r)
  2468. {
  2469. return ((!r) || (r->w <= 0.0f) || (r->h <= 0.0f)) ? SDL_TRUE : SDL_FALSE;
  2470. }
  2471. /* !!! FIXME: move this to a public API if we want to do float versions of all of these later */
  2472. static SDL_bool
  2473. SDL_HasIntersectionF(const SDL_FRect * A, const SDL_FRect * B)
  2474. {
  2475. float Amin, Amax, Bmin, Bmax;
  2476. if (!A) {
  2477. SDL_InvalidParamError("A");
  2478. return SDL_FALSE;
  2479. }
  2480. if (!B) {
  2481. SDL_InvalidParamError("B");
  2482. return SDL_FALSE;
  2483. }
  2484. /* Special cases for empty rects */
  2485. if (SDL_FRectEmpty(A) || SDL_FRectEmpty(B)) {
  2486. return SDL_FALSE;
  2487. }
  2488. /* Horizontal intersection */
  2489. Amin = A->x;
  2490. Amax = Amin + A->w;
  2491. Bmin = B->x;
  2492. Bmax = Bmin + B->w;
  2493. if (Bmin > Amin)
  2494. Amin = Bmin;
  2495. if (Bmax < Amax)
  2496. Amax = Bmax;
  2497. if (Amax <= Amin)
  2498. return SDL_FALSE;
  2499. /* Vertical intersection */
  2500. Amin = A->y;
  2501. Amax = Amin + A->h;
  2502. Bmin = B->y;
  2503. Bmax = Bmin + B->h;
  2504. if (Bmin > Amin)
  2505. Amin = Bmin;
  2506. if (Bmax < Amax)
  2507. Amax = Bmax;
  2508. if (Amax <= Amin)
  2509. return SDL_FALSE;
  2510. return SDL_TRUE;
  2511. }
  2512. int
  2513. SDL_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
  2514. const SDL_Rect * srcrect, const SDL_Rect * dstrect)
  2515. {
  2516. SDL_FRect dstfrect;
  2517. SDL_FRect *pdstfrect = NULL;
  2518. if (dstrect) {
  2519. dstfrect.x = (float) dstrect->x;
  2520. dstfrect.y = (float) dstrect->y;
  2521. dstfrect.w = (float) dstrect->w;
  2522. dstfrect.h = (float) dstrect->h;
  2523. pdstfrect = &dstfrect;
  2524. }
  2525. return SDL_RenderCopyF(renderer, texture, srcrect, pdstfrect);
  2526. }
  2527. int
  2528. SDL_RenderCopyF(SDL_Renderer * renderer, SDL_Texture * texture,
  2529. const SDL_Rect * srcrect, const SDL_FRect * dstrect)
  2530. {
  2531. SDL_Rect real_srcrect;
  2532. SDL_FRect real_dstrect;
  2533. SDL_Rect r;
  2534. int retval;
  2535. CHECK_RENDERER_MAGIC(renderer, -1);
  2536. CHECK_TEXTURE_MAGIC(texture, -1);
  2537. if (renderer != texture->renderer) {
  2538. return SDL_SetError("Texture was not created with this renderer");
  2539. }
  2540. /* Don't draw while we're hidden */
  2541. if (renderer->hidden) {
  2542. return 0;
  2543. }
  2544. real_srcrect.x = 0;
  2545. real_srcrect.y = 0;
  2546. real_srcrect.w = texture->w;
  2547. real_srcrect.h = texture->h;
  2548. if (srcrect) {
  2549. if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  2550. return 0;
  2551. }
  2552. }
  2553. SDL_zero(r);
  2554. SDL_RenderGetViewport(renderer, &r);
  2555. real_dstrect.x = 0.0f;
  2556. real_dstrect.y = 0.0f;
  2557. real_dstrect.w = (float) r.w;
  2558. real_dstrect.h = (float) r.h;
  2559. if (dstrect) {
  2560. if (!SDL_HasIntersectionF(dstrect, &real_dstrect)) {
  2561. return 0;
  2562. }
  2563. real_dstrect = *dstrect;
  2564. }
  2565. if (texture->native) {
  2566. texture = texture->native;
  2567. }
  2568. real_dstrect.x *= renderer->scale.x;
  2569. real_dstrect.y *= renderer->scale.y;
  2570. real_dstrect.w *= renderer->scale.x;
  2571. real_dstrect.h *= renderer->scale.y;
  2572. texture->last_command_generation = renderer->render_command_generation;
  2573. retval = QueueCmdCopy(renderer, texture, &real_srcrect, &real_dstrect);
  2574. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2575. }
  2576. int
  2577. SDL_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
  2578. const SDL_Rect * srcrect, const SDL_Rect * dstrect,
  2579. const double angle, const SDL_Point *center, const SDL_RendererFlip flip)
  2580. {
  2581. SDL_FRect dstfrect;
  2582. SDL_FRect *pdstfrect = NULL;
  2583. SDL_FPoint fcenter;
  2584. SDL_FPoint *pfcenter = NULL;
  2585. if (dstrect) {
  2586. dstfrect.x = (float) dstrect->x;
  2587. dstfrect.y = (float) dstrect->y;
  2588. dstfrect.w = (float) dstrect->w;
  2589. dstfrect.h = (float) dstrect->h;
  2590. pdstfrect = &dstfrect;
  2591. }
  2592. if (center) {
  2593. fcenter.x = (float) center->x;
  2594. fcenter.y = (float) center->y;
  2595. pfcenter = &fcenter;
  2596. }
  2597. return SDL_RenderCopyExF(renderer, texture, srcrect, pdstfrect, angle, pfcenter, flip);
  2598. }
  2599. int
  2600. SDL_RenderCopyExF(SDL_Renderer * renderer, SDL_Texture * texture,
  2601. const SDL_Rect * srcrect, const SDL_FRect * dstrect,
  2602. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
  2603. {
  2604. SDL_Rect real_srcrect;
  2605. SDL_FRect real_dstrect;
  2606. SDL_FPoint real_center;
  2607. int retval;
  2608. if (flip == SDL_FLIP_NONE && (int)(angle/360) == angle/360) { /* fast path when we don't need rotation or flipping */
  2609. return SDL_RenderCopyF(renderer, texture, srcrect, dstrect);
  2610. }
  2611. CHECK_RENDERER_MAGIC(renderer, -1);
  2612. CHECK_TEXTURE_MAGIC(texture, -1);
  2613. if (renderer != texture->renderer) {
  2614. return SDL_SetError("Texture was not created with this renderer");
  2615. }
  2616. if (!renderer->QueueCopyEx) {
  2617. return SDL_SetError("Renderer does not support RenderCopyEx");
  2618. }
  2619. /* Don't draw while we're hidden */
  2620. if (renderer->hidden) {
  2621. return 0;
  2622. }
  2623. real_srcrect.x = 0;
  2624. real_srcrect.y = 0;
  2625. real_srcrect.w = texture->w;
  2626. real_srcrect.h = texture->h;
  2627. if (srcrect) {
  2628. if (!SDL_IntersectRect(srcrect, &real_srcrect, &real_srcrect)) {
  2629. return 0;
  2630. }
  2631. }
  2632. /* We don't intersect the dstrect with the viewport as RenderCopy does because of potential rotation clipping issues... TODO: should we? */
  2633. if (dstrect) {
  2634. real_dstrect = *dstrect;
  2635. } else {
  2636. SDL_Rect r;
  2637. SDL_zero(r);
  2638. SDL_RenderGetViewport(renderer, &r);
  2639. real_dstrect.x = 0.0f;
  2640. real_dstrect.y = 0.0f;
  2641. real_dstrect.w = (float) r.w;
  2642. real_dstrect.h = (float) r.h;
  2643. }
  2644. if (texture->native) {
  2645. texture = texture->native;
  2646. }
  2647. if (center) {
  2648. real_center = *center;
  2649. } else {
  2650. real_center.x = real_dstrect.w / 2.0f;
  2651. real_center.y = real_dstrect.h / 2.0f;
  2652. }
  2653. real_dstrect.x *= renderer->scale.x;
  2654. real_dstrect.y *= renderer->scale.y;
  2655. real_dstrect.w *= renderer->scale.x;
  2656. real_dstrect.h *= renderer->scale.y;
  2657. real_center.x *= renderer->scale.x;
  2658. real_center.y *= renderer->scale.y;
  2659. texture->last_command_generation = renderer->render_command_generation;
  2660. retval = QueueCmdCopyEx(renderer, texture, &real_srcrect, &real_dstrect, angle, &real_center, flip);
  2661. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2662. }
  2663. int
  2664. SDL_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
  2665. Uint32 format, void * pixels, int pitch)
  2666. {
  2667. SDL_Rect real_rect;
  2668. CHECK_RENDERER_MAGIC(renderer, -1);
  2669. if (!renderer->RenderReadPixels) {
  2670. return SDL_Unsupported();
  2671. }
  2672. FlushRenderCommands(renderer); /* we need to render before we read the results. */
  2673. if (!format) {
  2674. format = SDL_GetWindowPixelFormat(renderer->window);
  2675. }
  2676. real_rect.x = renderer->viewport.x;
  2677. real_rect.y = renderer->viewport.y;
  2678. real_rect.w = renderer->viewport.w;
  2679. real_rect.h = renderer->viewport.h;
  2680. if (rect) {
  2681. if (!SDL_IntersectRect(rect, &real_rect, &real_rect)) {
  2682. return 0;
  2683. }
  2684. if (real_rect.y > rect->y) {
  2685. pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  2686. }
  2687. if (real_rect.x > rect->x) {
  2688. int bpp = SDL_BYTESPERPIXEL(format);
  2689. pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  2690. }
  2691. }
  2692. return renderer->RenderReadPixels(renderer, &real_rect,
  2693. format, pixels, pitch);
  2694. }
  2695. void
  2696. SDL_RenderPresent(SDL_Renderer * renderer)
  2697. {
  2698. CHECK_RENDERER_MAGIC(renderer, );
  2699. FlushRenderCommands(renderer); /* time to send everything to the GPU! */
  2700. /* Don't present while we're hidden */
  2701. if (renderer->hidden) {
  2702. return;
  2703. }
  2704. renderer->RenderPresent(renderer);
  2705. }
  2706. void
  2707. SDL_DestroyTexture(SDL_Texture * texture)
  2708. {
  2709. SDL_Renderer *renderer;
  2710. CHECK_TEXTURE_MAGIC(texture, );
  2711. renderer = texture->renderer;
  2712. if (texture == renderer->target) {
  2713. SDL_SetRenderTarget(renderer, NULL); /* implies command queue flush */
  2714. } else {
  2715. FlushRenderCommandsIfTextureNeeded(texture);
  2716. }
  2717. texture->magic = NULL;
  2718. if (texture->next) {
  2719. texture->next->prev = texture->prev;
  2720. }
  2721. if (texture->prev) {
  2722. texture->prev->next = texture->next;
  2723. } else {
  2724. renderer->textures = texture->next;
  2725. }
  2726. if (texture->native) {
  2727. SDL_DestroyTexture(texture->native);
  2728. }
  2729. if (texture->yuv) {
  2730. SDL_SW_DestroyYUVTexture(texture->yuv);
  2731. }
  2732. SDL_free(texture->pixels);
  2733. renderer->DestroyTexture(renderer, texture);
  2734. SDL_free(texture);
  2735. }
  2736. void
  2737. SDL_DestroyRenderer(SDL_Renderer * renderer)
  2738. {
  2739. SDL_RenderCommand *cmd;
  2740. SDL_AllocVertGap *gap;
  2741. SDL_AllocVertGap *nextgap;
  2742. CHECK_RENDERER_MAGIC(renderer, );
  2743. SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
  2744. if (renderer->render_commands_tail != NULL) {
  2745. renderer->render_commands_tail->next = renderer->render_commands_pool;
  2746. cmd = renderer->render_commands;
  2747. } else {
  2748. cmd = renderer->render_commands_pool;
  2749. }
  2750. renderer->render_commands_pool = NULL;
  2751. renderer->render_commands_tail = NULL;
  2752. renderer->render_commands = NULL;
  2753. while (cmd != NULL) {
  2754. SDL_RenderCommand *next = cmd->next;
  2755. SDL_free(cmd);
  2756. cmd = next;
  2757. }
  2758. SDL_free(renderer->vertex_data);
  2759. for (gap = renderer->vertex_data_gaps.next; gap; gap = nextgap) {
  2760. nextgap = gap->next;
  2761. SDL_free(gap);
  2762. }
  2763. for (gap = renderer->vertex_data_gaps_pool; gap; gap = nextgap) {
  2764. nextgap = gap->next;
  2765. SDL_free(gap);
  2766. }
  2767. /* Free existing textures for this renderer */
  2768. while (renderer->textures) {
  2769. SDL_Texture *tex = renderer->textures; (void) tex;
  2770. SDL_DestroyTexture(renderer->textures);
  2771. SDL_assert(tex != renderer->textures); /* satisfy static analysis. */
  2772. }
  2773. if (renderer->window) {
  2774. SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);
  2775. }
  2776. /* It's no longer magical... */
  2777. renderer->magic = NULL;
  2778. /* Free the target mutex */
  2779. SDL_DestroyMutex(renderer->target_mutex);
  2780. renderer->target_mutex = NULL;
  2781. /* Free the renderer instance */
  2782. renderer->DestroyRenderer(renderer);
  2783. }
  2784. int SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh)
  2785. {
  2786. SDL_Renderer *renderer;
  2787. CHECK_TEXTURE_MAGIC(texture, -1);
  2788. renderer = texture->renderer;
  2789. if (texture->native) {
  2790. return SDL_GL_BindTexture(texture->native, texw, texh);
  2791. } else if (renderer && renderer->GL_BindTexture) {
  2792. FlushRenderCommandsIfTextureNeeded(texture); /* in case the app is going to mess with it. */
  2793. return renderer->GL_BindTexture(renderer, texture, texw, texh);
  2794. } else {
  2795. return SDL_Unsupported();
  2796. }
  2797. }
  2798. int SDL_GL_UnbindTexture(SDL_Texture *texture)
  2799. {
  2800. SDL_Renderer *renderer;
  2801. CHECK_TEXTURE_MAGIC(texture, -1);
  2802. renderer = texture->renderer;
  2803. if (texture->native) {
  2804. return SDL_GL_UnbindTexture(texture->native);
  2805. } else if (renderer && renderer->GL_UnbindTexture) {
  2806. FlushRenderCommandsIfTextureNeeded(texture); /* in case the app messed with it. */
  2807. return renderer->GL_UnbindTexture(renderer, texture);
  2808. }
  2809. return SDL_Unsupported();
  2810. }
  2811. void *
  2812. SDL_RenderGetMetalLayer(SDL_Renderer * renderer)
  2813. {
  2814. CHECK_RENDERER_MAGIC(renderer, NULL);
  2815. if (renderer->GetMetalLayer) {
  2816. FlushRenderCommands(renderer); /* in case the app is going to mess with it. */
  2817. return renderer->GetMetalLayer(renderer);
  2818. }
  2819. return NULL;
  2820. }
  2821. void *
  2822. SDL_RenderGetMetalCommandEncoder(SDL_Renderer * renderer)
  2823. {
  2824. CHECK_RENDERER_MAGIC(renderer, NULL);
  2825. if (renderer->GetMetalCommandEncoder) {
  2826. FlushRenderCommands(renderer); /* in case the app is going to mess with it. */
  2827. return renderer->GetMetalCommandEncoder(renderer);
  2828. }
  2829. return NULL;
  2830. }
  2831. static SDL_BlendMode
  2832. SDL_GetShortBlendMode(SDL_BlendMode blendMode)
  2833. {
  2834. if (blendMode == SDL_BLENDMODE_NONE_FULL) {
  2835. return SDL_BLENDMODE_NONE;
  2836. }
  2837. if (blendMode == SDL_BLENDMODE_BLEND_FULL) {
  2838. return SDL_BLENDMODE_BLEND;
  2839. }
  2840. if (blendMode == SDL_BLENDMODE_ADD_FULL) {
  2841. return SDL_BLENDMODE_ADD;
  2842. }
  2843. if (blendMode == SDL_BLENDMODE_MOD_FULL) {
  2844. return SDL_BLENDMODE_MOD;
  2845. }
  2846. return blendMode;
  2847. }
  2848. static SDL_BlendMode
  2849. SDL_GetLongBlendMode(SDL_BlendMode blendMode)
  2850. {
  2851. if (blendMode == SDL_BLENDMODE_NONE) {
  2852. return SDL_BLENDMODE_NONE_FULL;
  2853. }
  2854. if (blendMode == SDL_BLENDMODE_BLEND) {
  2855. return SDL_BLENDMODE_BLEND_FULL;
  2856. }
  2857. if (blendMode == SDL_BLENDMODE_ADD) {
  2858. return SDL_BLENDMODE_ADD_FULL;
  2859. }
  2860. if (blendMode == SDL_BLENDMODE_MOD) {
  2861. return SDL_BLENDMODE_MOD_FULL;
  2862. }
  2863. return blendMode;
  2864. }
  2865. SDL_BlendMode
  2866. SDL_ComposeCustomBlendMode(SDL_BlendFactor srcColorFactor, SDL_BlendFactor dstColorFactor,
  2867. SDL_BlendOperation colorOperation,
  2868. SDL_BlendFactor srcAlphaFactor, SDL_BlendFactor dstAlphaFactor,
  2869. SDL_BlendOperation alphaOperation)
  2870. {
  2871. SDL_BlendMode blendMode = SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation,
  2872. srcAlphaFactor, dstAlphaFactor, alphaOperation);
  2873. return SDL_GetShortBlendMode(blendMode);
  2874. }
  2875. SDL_BlendFactor
  2876. SDL_GetBlendModeSrcColorFactor(SDL_BlendMode blendMode)
  2877. {
  2878. blendMode = SDL_GetLongBlendMode(blendMode);
  2879. return (SDL_BlendFactor)(((Uint32)blendMode >> 4) & 0xF);
  2880. }
  2881. SDL_BlendFactor
  2882. SDL_GetBlendModeDstColorFactor(SDL_BlendMode blendMode)
  2883. {
  2884. blendMode = SDL_GetLongBlendMode(blendMode);
  2885. return (SDL_BlendFactor)(((Uint32)blendMode >> 8) & 0xF);
  2886. }
  2887. SDL_BlendOperation
  2888. SDL_GetBlendModeColorOperation(SDL_BlendMode blendMode)
  2889. {
  2890. blendMode = SDL_GetLongBlendMode(blendMode);
  2891. return (SDL_BlendOperation)(((Uint32)blendMode >> 0) & 0xF);
  2892. }
  2893. SDL_BlendFactor
  2894. SDL_GetBlendModeSrcAlphaFactor(SDL_BlendMode blendMode)
  2895. {
  2896. blendMode = SDL_GetLongBlendMode(blendMode);
  2897. return (SDL_BlendFactor)(((Uint32)blendMode >> 20) & 0xF);
  2898. }
  2899. SDL_BlendFactor
  2900. SDL_GetBlendModeDstAlphaFactor(SDL_BlendMode blendMode)
  2901. {
  2902. blendMode = SDL_GetLongBlendMode(blendMode);
  2903. return (SDL_BlendFactor)(((Uint32)blendMode >> 24) & 0xF);
  2904. }
  2905. SDL_BlendOperation
  2906. SDL_GetBlendModeAlphaOperation(SDL_BlendMode blendMode)
  2907. {
  2908. blendMode = SDL_GetLongBlendMode(blendMode);
  2909. return (SDL_BlendOperation)(((Uint32)blendMode >> 16) & 0xF);
  2910. }
  2911. /* vi: set ts=4 sw=4 expandtab: */