SDL_render.c 110 KB

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