SDL_render.c 142 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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_sysrender.h"
  21. #include "software/SDL_render_sw_c.h"
  22. #include "../video/SDL_pixels_c.h"
  23. #if defined(__ANDROID__)
  24. #include "../core/android/SDL_android.h"
  25. #endif
  26. /* as a courtesy to iOS apps, we don't try to draw when in the background, as
  27. that will crash the app. However, these apps _should_ have used
  28. SDL_AddEventWatch to catch SDL_APP_WILLENTERBACKGROUND events and stopped
  29. drawing themselves. Other platforms still draw, as the compositor can use it,
  30. and more importantly: drawing to render targets isn't lost. But I still think
  31. this should probably be removed at some point in the future. --ryan. */
  32. #if defined(__IOS__) || defined(__TVOS__) || defined(__ANDROID__)
  33. #define DONT_DRAW_WHILE_HIDDEN 1
  34. #else
  35. #define DONT_DRAW_WHILE_HIDDEN 0
  36. #endif
  37. #define SDL_WINDOWRENDERDATA "_SDL_WindowRenderData"
  38. #define CHECK_RENDERER_MAGIC(renderer, retval) \
  39. if (!(renderer) || (renderer)->magic != &renderer_magic) { \
  40. SDL_InvalidParamError("renderer"); \
  41. return retval; \
  42. }
  43. #define CHECK_TEXTURE_MAGIC(texture, retval) \
  44. if (!(texture) || (texture)->magic != &texture_magic) { \
  45. SDL_InvalidParamError("texture"); \
  46. return retval; \
  47. }
  48. /* Predefined blend modes */
  49. #define SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation, \
  50. srcAlphaFactor, dstAlphaFactor, alphaOperation) \
  51. (SDL_BlendMode)(((Uint32)(colorOperation) << 0) | \
  52. ((Uint32)(srcColorFactor) << 4) | \
  53. ((Uint32)(dstColorFactor) << 8) | \
  54. ((Uint32)(alphaOperation) << 16) | \
  55. ((Uint32)(srcAlphaFactor) << 20) | \
  56. ((Uint32)(dstAlphaFactor) << 24))
  57. #define SDL_BLENDMODE_NONE_FULL \
  58. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD, \
  59. SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ZERO, SDL_BLENDOPERATION_ADD)
  60. #define SDL_BLENDMODE_BLEND_FULL \
  61. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, \
  62. SDL_BLENDFACTOR_ONE, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD)
  63. #define SDL_BLENDMODE_ADD_FULL \
  64. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD, \
  65. SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
  66. #define SDL_BLENDMODE_MOD_FULL \
  67. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_SRC_COLOR, SDL_BLENDOPERATION_ADD, \
  68. SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_ADD)
  69. #define SDL_BLENDMODE_MUL_FULL \
  70. SDL_COMPOSE_BLENDMODE(SDL_BLENDFACTOR_DST_COLOR, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD, \
  71. SDL_BLENDFACTOR_DST_ALPHA, SDL_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_BLENDOPERATION_ADD)
  72. #if !SDL_RENDER_DISABLED
  73. static const SDL_RenderDriver *render_drivers[] = {
  74. #if SDL_VIDEO_RENDER_D3D
  75. &D3D_RenderDriver,
  76. #endif
  77. #if SDL_VIDEO_RENDER_D3D11
  78. &D3D11_RenderDriver,
  79. #endif
  80. #if SDL_VIDEO_RENDER_D3D12
  81. &D3D12_RenderDriver,
  82. #endif
  83. #if SDL_VIDEO_RENDER_METAL
  84. &METAL_RenderDriver,
  85. #endif
  86. #if SDL_VIDEO_RENDER_OGL
  87. &GL_RenderDriver,
  88. #endif
  89. #if SDL_VIDEO_RENDER_OGL_ES2
  90. &GLES2_RenderDriver,
  91. #endif
  92. #if SDL_VIDEO_RENDER_PS2 && !SDL_RENDER_DISABLED
  93. &PS2_RenderDriver,
  94. #endif
  95. #if SDL_VIDEO_RENDER_PSP
  96. &PSP_RenderDriver,
  97. #endif
  98. #if SDL_VIDEO_RENDER_VITA_GXM
  99. &VITA_GXM_RenderDriver,
  100. #endif
  101. #if SDL_VIDEO_RENDER_SW
  102. &SW_RenderDriver
  103. #endif
  104. };
  105. #endif /* !SDL_RENDER_DISABLED */
  106. static char renderer_magic;
  107. static char texture_magic;
  108. static SDL_INLINE void DebugLogRenderCommands(const SDL_RenderCommand *cmd)
  109. {
  110. #if 0
  111. unsigned int i = 1;
  112. SDL_Log("Render commands to flush:");
  113. while (cmd) {
  114. switch (cmd->command) {
  115. case SDL_RENDERCMD_NO_OP:
  116. SDL_Log(" %u. no-op", i++);
  117. break;
  118. case SDL_RENDERCMD_SETVIEWPORT:
  119. SDL_Log(" %u. set viewport (first=%u, rect={(%d, %d), %dx%d})", i++,
  120. (unsigned int) cmd->data.viewport.first,
  121. cmd->data.viewport.rect.x, cmd->data.viewport.rect.y,
  122. cmd->data.viewport.rect.w, cmd->data.viewport.rect.h);
  123. break;
  124. case SDL_RENDERCMD_SETCLIPRECT:
  125. SDL_Log(" %u. set cliprect (enabled=%s, rect={(%d, %d), %dx%d})", i++,
  126. cmd->data.cliprect.enabled ? "true" : "false",
  127. cmd->data.cliprect.rect.x, cmd->data.cliprect.rect.y,
  128. cmd->data.cliprect.rect.w, cmd->data.cliprect.rect.h);
  129. break;
  130. case SDL_RENDERCMD_SETDRAWCOLOR:
  131. SDL_Log(" %u. set draw color (first=%u, r=%d, g=%d, b=%d, a=%d)", i++,
  132. (unsigned int) cmd->data.color.first,
  133. (int) cmd->data.color.r, (int) cmd->data.color.g,
  134. (int) cmd->data.color.b, (int) cmd->data.color.a);
  135. break;
  136. case SDL_RENDERCMD_CLEAR:
  137. SDL_Log(" %u. clear (first=%u, r=%d, g=%d, b=%d, a=%d)", i++,
  138. (unsigned int) cmd->data.color.first,
  139. (int) cmd->data.color.r, (int) cmd->data.color.g,
  140. (int) cmd->data.color.b, (int) cmd->data.color.a);
  141. break;
  142. case SDL_RENDERCMD_DRAW_POINTS:
  143. SDL_Log(" %u. draw points (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
  144. (unsigned int) cmd->data.draw.first,
  145. (unsigned int) cmd->data.draw.count,
  146. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  147. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  148. (int) cmd->data.draw.blend);
  149. break;
  150. case SDL_RENDERCMD_DRAW_LINES:
  151. SDL_Log(" %u. draw lines (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
  152. (unsigned int) cmd->data.draw.first,
  153. (unsigned int) cmd->data.draw.count,
  154. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  155. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  156. (int) cmd->data.draw.blend);
  157. break;
  158. case SDL_RENDERCMD_FILL_RECTS:
  159. SDL_Log(" %u. fill rects (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d)", i++,
  160. (unsigned int) cmd->data.draw.first,
  161. (unsigned int) cmd->data.draw.count,
  162. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  163. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  164. (int) cmd->data.draw.blend);
  165. break;
  166. case SDL_RENDERCMD_COPY:
  167. SDL_Log(" %u. copy (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
  168. (unsigned int) cmd->data.draw.first,
  169. (unsigned int) cmd->data.draw.count,
  170. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  171. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  172. (int) cmd->data.draw.blend, cmd->data.draw.texture);
  173. break;
  174. case SDL_RENDERCMD_COPY_EX:
  175. SDL_Log(" %u. copyex (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
  176. (unsigned int) cmd->data.draw.first,
  177. (unsigned int) cmd->data.draw.count,
  178. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  179. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  180. (int) cmd->data.draw.blend, cmd->data.draw.texture);
  181. break;
  182. case SDL_RENDERCMD_GEOMETRY:
  183. SDL_Log(" %u. geometry (first=%u, count=%u, r=%d, g=%d, b=%d, a=%d, blend=%d, tex=%p)", i++,
  184. (unsigned int) cmd->data.draw.first,
  185. (unsigned int) cmd->data.draw.count,
  186. (int) cmd->data.draw.r, (int) cmd->data.draw.g,
  187. (int) cmd->data.draw.b, (int) cmd->data.draw.a,
  188. (int) cmd->data.draw.blend, cmd->data.draw.texture);
  189. break;
  190. }
  191. cmd = cmd->next;
  192. }
  193. #endif
  194. }
  195. static int FlushRenderCommands(SDL_Renderer *renderer)
  196. {
  197. int retval;
  198. SDL_assert((renderer->render_commands == NULL) == (renderer->render_commands_tail == NULL));
  199. if (renderer->render_commands == NULL) { /* nothing to do! */
  200. SDL_assert(renderer->vertex_data_used == 0);
  201. return 0;
  202. }
  203. DebugLogRenderCommands(renderer->render_commands);
  204. retval = renderer->RunCommandQueue(renderer, renderer->render_commands, renderer->vertex_data, renderer->vertex_data_used);
  205. /* Move the whole render command queue to the unused pool so we can reuse them next time. */
  206. if (renderer->render_commands_tail != NULL) {
  207. renderer->render_commands_tail->next = renderer->render_commands_pool;
  208. renderer->render_commands_pool = renderer->render_commands;
  209. renderer->render_commands_tail = NULL;
  210. renderer->render_commands = NULL;
  211. }
  212. renderer->vertex_data_used = 0;
  213. renderer->render_command_generation++;
  214. renderer->color_queued = SDL_FALSE;
  215. renderer->viewport_queued = SDL_FALSE;
  216. renderer->cliprect_queued = SDL_FALSE;
  217. return retval;
  218. }
  219. static int FlushRenderCommandsIfTextureNeeded(SDL_Texture *texture)
  220. {
  221. SDL_Renderer *renderer = texture->renderer;
  222. if (texture->last_command_generation == renderer->render_command_generation) {
  223. /* the current command queue depends on this texture, flush the queue now before it changes */
  224. return FlushRenderCommands(renderer);
  225. }
  226. return 0;
  227. }
  228. static SDL_INLINE int FlushRenderCommandsIfNotBatching(SDL_Renderer *renderer)
  229. {
  230. return renderer->batching ? 0 : FlushRenderCommands(renderer);
  231. }
  232. int SDL_RenderFlush(SDL_Renderer *renderer)
  233. {
  234. return FlushRenderCommands(renderer);
  235. }
  236. void *
  237. SDL_AllocateRenderVertices(SDL_Renderer *renderer, const size_t numbytes, const size_t alignment, size_t *offset)
  238. {
  239. const size_t needed = renderer->vertex_data_used + numbytes + alignment;
  240. const size_t current_offset = renderer->vertex_data_used;
  241. const size_t aligner = (alignment && ((current_offset & (alignment - 1)) != 0)) ? (alignment - (current_offset & (alignment - 1))) : 0;
  242. const size_t aligned = current_offset + aligner;
  243. if (renderer->vertex_data_allocation < needed) {
  244. const size_t current_allocation = renderer->vertex_data ? renderer->vertex_data_allocation : 1024;
  245. size_t newsize = current_allocation * 2;
  246. void *ptr;
  247. while (newsize < needed) {
  248. newsize *= 2;
  249. }
  250. ptr = SDL_realloc(renderer->vertex_data, newsize);
  251. if (ptr == NULL) {
  252. SDL_OutOfMemory();
  253. return NULL;
  254. }
  255. renderer->vertex_data = ptr;
  256. renderer->vertex_data_allocation = newsize;
  257. }
  258. if (offset) {
  259. *offset = aligned;
  260. }
  261. renderer->vertex_data_used += aligner + numbytes;
  262. return ((Uint8 *)renderer->vertex_data) + aligned;
  263. }
  264. static SDL_RenderCommand *AllocateRenderCommand(SDL_Renderer *renderer)
  265. {
  266. SDL_RenderCommand *retval = NULL;
  267. /* !!! FIXME: are there threading limitations in SDL's render API? If not, we need to mutex this. */
  268. retval = renderer->render_commands_pool;
  269. if (retval != NULL) {
  270. renderer->render_commands_pool = retval->next;
  271. retval->next = NULL;
  272. } else {
  273. retval = SDL_calloc(1, sizeof(*retval));
  274. if (retval == NULL) {
  275. SDL_OutOfMemory();
  276. return NULL;
  277. }
  278. }
  279. SDL_assert((renderer->render_commands == NULL) == (renderer->render_commands_tail == NULL));
  280. if (renderer->render_commands_tail != NULL) {
  281. renderer->render_commands_tail->next = retval;
  282. } else {
  283. renderer->render_commands = retval;
  284. }
  285. renderer->render_commands_tail = retval;
  286. return retval;
  287. }
  288. static int QueueCmdSetViewport(SDL_Renderer *renderer)
  289. {
  290. int retval = 0;
  291. if (!renderer->viewport_queued || (SDL_memcmp(&renderer->viewport, &renderer->last_queued_viewport, sizeof(SDL_DRect)) != 0)) {
  292. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  293. retval = -1;
  294. if (cmd != NULL) {
  295. cmd->command = SDL_RENDERCMD_SETVIEWPORT;
  296. cmd->data.viewport.first = 0; /* render backend will fill this in. */
  297. /* Convert SDL_DRect to SDL_Rect */
  298. cmd->data.viewport.rect.x = (int)SDL_floor(renderer->viewport.x);
  299. cmd->data.viewport.rect.y = (int)SDL_floor(renderer->viewport.y);
  300. cmd->data.viewport.rect.w = (int)SDL_floor(renderer->viewport.w);
  301. cmd->data.viewport.rect.h = (int)SDL_floor(renderer->viewport.h);
  302. retval = renderer->QueueSetViewport(renderer, cmd);
  303. if (retval < 0) {
  304. cmd->command = SDL_RENDERCMD_NO_OP;
  305. } else {
  306. SDL_copyp(&renderer->last_queued_viewport, &renderer->viewport);
  307. renderer->viewport_queued = SDL_TRUE;
  308. }
  309. }
  310. }
  311. return retval;
  312. }
  313. static int 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_DRect)) != 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. /* Convert SDL_DRect to SDL_Rect */
  326. cmd->data.cliprect.rect.x = (int)SDL_floor(renderer->clip_rect.x);
  327. cmd->data.cliprect.rect.y = (int)SDL_floor(renderer->clip_rect.y);
  328. cmd->data.cliprect.rect.w = (int)SDL_floor(renderer->clip_rect.w);
  329. cmd->data.cliprect.rect.h = (int)SDL_floor(renderer->clip_rect.h);
  330. SDL_copyp(&renderer->last_queued_cliprect, &renderer->clip_rect);
  331. renderer->last_queued_cliprect_enabled = renderer->clipping_enabled;
  332. renderer->cliprect_queued = SDL_TRUE;
  333. }
  334. }
  335. return retval;
  336. }
  337. static int QueueCmdSetDrawColor(SDL_Renderer *renderer, SDL_Color *col)
  338. {
  339. const Uint32 color = (((Uint32)col->a << 24) | (col->r << 16) | (col->g << 8) | col->b);
  340. int retval = 0;
  341. if (!renderer->color_queued || (color != renderer->last_queued_color)) {
  342. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  343. retval = -1;
  344. if (cmd != NULL) {
  345. cmd->command = SDL_RENDERCMD_SETDRAWCOLOR;
  346. cmd->data.color.first = 0; /* render backend will fill this in. */
  347. cmd->data.color.r = col->r;
  348. cmd->data.color.g = col->g;
  349. cmd->data.color.b = col->b;
  350. cmd->data.color.a = col->a;
  351. retval = renderer->QueueSetDrawColor(renderer, cmd);
  352. if (retval < 0) {
  353. cmd->command = SDL_RENDERCMD_NO_OP;
  354. } else {
  355. renderer->last_queued_color = color;
  356. renderer->color_queued = SDL_TRUE;
  357. }
  358. }
  359. }
  360. return retval;
  361. }
  362. static int QueueCmdClear(SDL_Renderer *renderer)
  363. {
  364. SDL_RenderCommand *cmd = AllocateRenderCommand(renderer);
  365. if (cmd == NULL) {
  366. return -1;
  367. }
  368. cmd->command = SDL_RENDERCMD_CLEAR;
  369. cmd->data.color.first = 0;
  370. cmd->data.color.r = renderer->color.r;
  371. cmd->data.color.g = renderer->color.g;
  372. cmd->data.color.b = renderer->color.b;
  373. cmd->data.color.a = renderer->color.a;
  374. return 0;
  375. }
  376. static SDL_RenderCommand *PrepQueueCmdDraw(SDL_Renderer *renderer, const SDL_RenderCommandType cmdtype, SDL_Texture *texture)
  377. {
  378. SDL_RenderCommand *cmd = NULL;
  379. int retval = 0;
  380. SDL_Color *color;
  381. SDL_BlendMode blendMode;
  382. if (texture) {
  383. color = &texture->color;
  384. blendMode = texture->blendMode;
  385. } else {
  386. color = &renderer->color;
  387. blendMode = renderer->blendMode;
  388. }
  389. if (cmdtype != SDL_RENDERCMD_GEOMETRY) {
  390. /* !!! FIXME: drop this draw if viewport w or h is zero. */
  391. retval = QueueCmdSetDrawColor(renderer, color);
  392. }
  393. /* Set the viewport and clip rect directly before draws, so the backends
  394. * don't have to worry about that state not being valid at draw time. */
  395. if (retval == 0 && !renderer->viewport_queued) {
  396. retval = QueueCmdSetViewport(renderer);
  397. }
  398. if (retval == 0 && !renderer->cliprect_queued) {
  399. retval = QueueCmdSetClipRect(renderer);
  400. }
  401. if (retval == 0) {
  402. cmd = AllocateRenderCommand(renderer);
  403. if (cmd != NULL) {
  404. cmd->command = cmdtype;
  405. cmd->data.draw.first = 0; /* render backend will fill this in. */
  406. cmd->data.draw.count = 0; /* render backend will fill this in. */
  407. cmd->data.draw.r = color->r;
  408. cmd->data.draw.g = color->g;
  409. cmd->data.draw.b = color->b;
  410. cmd->data.draw.a = color->a;
  411. cmd->data.draw.blend = blendMode;
  412. cmd->data.draw.texture = texture;
  413. }
  414. }
  415. return cmd;
  416. }
  417. static int QueueCmdDrawPoints(SDL_Renderer *renderer, const SDL_FPoint *points, const int count)
  418. {
  419. SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_DRAW_POINTS, NULL);
  420. int retval = -1;
  421. if (cmd != NULL) {
  422. retval = renderer->QueueDrawPoints(renderer, cmd, points, count);
  423. if (retval < 0) {
  424. cmd->command = SDL_RENDERCMD_NO_OP;
  425. }
  426. }
  427. return retval;
  428. }
  429. static int QueueCmdDrawLines(SDL_Renderer *renderer, const SDL_FPoint *points, const int count)
  430. {
  431. SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_DRAW_LINES, NULL);
  432. int retval = -1;
  433. if (cmd != NULL) {
  434. retval = renderer->QueueDrawLines(renderer, cmd, points, count);
  435. if (retval < 0) {
  436. cmd->command = SDL_RENDERCMD_NO_OP;
  437. }
  438. }
  439. return retval;
  440. }
  441. static int QueueCmdFillRects(SDL_Renderer *renderer, const SDL_FRect *rects, const int count)
  442. {
  443. SDL_RenderCommand *cmd;
  444. int retval = -1;
  445. const int use_rendergeometry = (renderer->QueueFillRects == NULL);
  446. cmd = PrepQueueCmdDraw(renderer, (use_rendergeometry ? SDL_RENDERCMD_GEOMETRY : SDL_RENDERCMD_FILL_RECTS), NULL);
  447. if (cmd != NULL) {
  448. if (use_rendergeometry) {
  449. SDL_bool isstack1;
  450. SDL_bool isstack2;
  451. float *xy = SDL_small_alloc(float, 4 * 2 * count, &isstack1);
  452. int *indices = SDL_small_alloc(int, 6 * count, &isstack2);
  453. if (xy && indices) {
  454. int i;
  455. float *ptr_xy = xy;
  456. int *ptr_indices = indices;
  457. const int xy_stride = 2 * sizeof(float);
  458. const int num_vertices = 4 * count;
  459. const int num_indices = 6 * count;
  460. const int size_indices = 4;
  461. int cur_index = 0;
  462. const int *rect_index_order = renderer->rect_index_order;
  463. for (i = 0; i < count; ++i) {
  464. float minx, miny, maxx, maxy;
  465. minx = rects[i].x;
  466. miny = rects[i].y;
  467. maxx = rects[i].x + rects[i].w;
  468. maxy = rects[i].y + rects[i].h;
  469. *ptr_xy++ = minx;
  470. *ptr_xy++ = miny;
  471. *ptr_xy++ = maxx;
  472. *ptr_xy++ = miny;
  473. *ptr_xy++ = maxx;
  474. *ptr_xy++ = maxy;
  475. *ptr_xy++ = minx;
  476. *ptr_xy++ = maxy;
  477. *ptr_indices++ = cur_index + rect_index_order[0];
  478. *ptr_indices++ = cur_index + rect_index_order[1];
  479. *ptr_indices++ = cur_index + rect_index_order[2];
  480. *ptr_indices++ = cur_index + rect_index_order[3];
  481. *ptr_indices++ = cur_index + rect_index_order[4];
  482. *ptr_indices++ = cur_index + rect_index_order[5];
  483. cur_index += 4;
  484. }
  485. retval = renderer->QueueGeometry(renderer, cmd, NULL,
  486. xy, xy_stride, &renderer->color, 0 /* color_stride */, NULL, 0,
  487. num_vertices, indices, num_indices, size_indices,
  488. 1.0f, 1.0f);
  489. if (retval < 0) {
  490. cmd->command = SDL_RENDERCMD_NO_OP;
  491. }
  492. }
  493. SDL_small_free(xy, isstack1);
  494. SDL_small_free(indices, isstack2);
  495. } else {
  496. retval = renderer->QueueFillRects(renderer, cmd, rects, count);
  497. if (retval < 0) {
  498. cmd->command = SDL_RENDERCMD_NO_OP;
  499. }
  500. }
  501. }
  502. return retval;
  503. }
  504. static int QueueCmdCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect, const SDL_FRect *dstrect)
  505. {
  506. SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_COPY, texture);
  507. int retval = -1;
  508. if (cmd != NULL) {
  509. retval = renderer->QueueCopy(renderer, cmd, texture, srcrect, dstrect);
  510. if (retval < 0) {
  511. cmd->command = SDL_RENDERCMD_NO_OP;
  512. }
  513. }
  514. return retval;
  515. }
  516. static int QueueCmdCopyEx(SDL_Renderer *renderer, SDL_Texture *texture,
  517. const SDL_Rect *srcquad, const SDL_FRect *dstrect,
  518. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip, float scale_x, float scale_y)
  519. {
  520. SDL_RenderCommand *cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_COPY_EX, texture);
  521. int retval = -1;
  522. if (cmd != NULL) {
  523. retval = renderer->QueueCopyEx(renderer, cmd, texture, srcquad, dstrect, angle, center, flip, scale_x, scale_y);
  524. if (retval < 0) {
  525. cmd->command = SDL_RENDERCMD_NO_OP;
  526. }
  527. }
  528. return retval;
  529. }
  530. static int QueueCmdGeometry(SDL_Renderer *renderer, SDL_Texture *texture,
  531. const float *xy, int xy_stride,
  532. const SDL_Color *color, int color_stride,
  533. const float *uv, int uv_stride,
  534. int num_vertices,
  535. const void *indices, int num_indices, int size_indices,
  536. float scale_x, float scale_y)
  537. {
  538. SDL_RenderCommand *cmd;
  539. int retval = -1;
  540. cmd = PrepQueueCmdDraw(renderer, SDL_RENDERCMD_GEOMETRY, texture);
  541. if (cmd != NULL) {
  542. retval = renderer->QueueGeometry(renderer, cmd, texture,
  543. xy, xy_stride,
  544. color, color_stride, uv, uv_stride,
  545. num_vertices, indices, num_indices, size_indices,
  546. scale_x, scale_y);
  547. if (retval < 0) {
  548. cmd->command = SDL_RENDERCMD_NO_OP;
  549. }
  550. }
  551. return retval;
  552. }
  553. static int UpdateLogicalSize(SDL_Renderer *renderer, SDL_bool flush_viewport_cmd);
  554. int SDL_GetNumRenderDrivers(void)
  555. {
  556. #if !SDL_RENDER_DISABLED
  557. return SDL_arraysize(render_drivers);
  558. #else
  559. return 0;
  560. #endif
  561. }
  562. const char *SDL_GetRenderDriver(int index)
  563. {
  564. #if !SDL_RENDER_DISABLED
  565. if (index < 0 || index >= SDL_GetNumRenderDrivers()) {
  566. SDL_SetError("index must be in the range of 0 - %d",
  567. SDL_GetNumRenderDrivers() - 1);
  568. return NULL;
  569. }
  570. return render_drivers[index]->info.name;
  571. #else
  572. SDL_SetError("SDL not built with rendering support");
  573. return NULL;
  574. #endif
  575. }
  576. static void GetWindowViewportValues(SDL_Renderer *renderer, int *logical_w, int *logical_h, SDL_DRect *viewport, SDL_FPoint *scale)
  577. {
  578. SDL_LockMutex(renderer->target_mutex);
  579. *logical_w = renderer->target ? renderer->logical_w_backup : renderer->logical_w;
  580. *logical_h = renderer->target ? renderer->logical_h_backup : renderer->logical_h;
  581. *viewport = renderer->target ? renderer->viewport_backup : renderer->viewport;
  582. *scale = renderer->target ? renderer->scale_backup : renderer->scale;
  583. SDL_UnlockMutex(renderer->target_mutex);
  584. }
  585. static int SDLCALL SDL_RendererEventWatch(void *userdata, SDL_Event *event)
  586. {
  587. SDL_Renderer *renderer = (SDL_Renderer *)userdata;
  588. if (event->type >= SDL_WINDOWEVENT_FIRST && event->type <= SDL_WINDOWEVENT_LAST) {
  589. SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
  590. if (window == renderer->window) {
  591. if (renderer->WindowEvent) {
  592. renderer->WindowEvent(renderer, &event->window);
  593. }
  594. /* In addition to size changes, we also want to do this block for
  595. * window display changes as well! If the new display has a new DPI,
  596. * we need to update the viewport for the new window/drawable ratio.
  597. */
  598. if (event->type == SDL_WINDOWEVENT_SIZE_CHANGED ||
  599. event->type == SDL_WINDOWEVENT_DISPLAY_CHANGED) {
  600. /* Make sure we're operating on the default render target */
  601. SDL_Texture *saved_target = SDL_GetRenderTarget(renderer);
  602. if (saved_target) {
  603. SDL_SetRenderTarget(renderer, NULL);
  604. }
  605. /* Update the DPI scale if the window has been resized. */
  606. if (window && renderer->GetOutputSize) {
  607. int window_w, window_h;
  608. int output_w, output_h;
  609. if (renderer->GetOutputSize(renderer, &output_w, &output_h) == 0) {
  610. SDL_GetWindowSize(renderer->window, &window_w, &window_h);
  611. renderer->dpi_scale.x = (float)window_w / output_w;
  612. renderer->dpi_scale.y = (float)window_h / output_h;
  613. }
  614. }
  615. if (renderer->logical_w) {
  616. #if defined(__ANDROID__)
  617. /* Don't immediatly flush because the app may be in
  618. * background, and the egl context shouldn't be used. */
  619. SDL_bool flush_viewport_cmd = SDL_FALSE;
  620. #else
  621. SDL_bool flush_viewport_cmd = SDL_TRUE;
  622. #endif
  623. UpdateLogicalSize(renderer, flush_viewport_cmd);
  624. } else {
  625. /* Window was resized, reset viewport */
  626. int w, h;
  627. if (renderer->GetOutputSize) {
  628. renderer->GetOutputSize(renderer, &w, &h);
  629. } else {
  630. SDL_GetWindowSize(renderer->window, &w, &h);
  631. }
  632. renderer->viewport.x = (double)0;
  633. renderer->viewport.y = (double)0;
  634. renderer->viewport.w = (double)w;
  635. renderer->viewport.h = (double)h;
  636. QueueCmdSetViewport(renderer);
  637. #if defined(__ANDROID__)
  638. /* Don't immediatly flush because the app may be in
  639. * background, and the egl context shouldn't be used. */
  640. #else
  641. FlushRenderCommandsIfNotBatching(renderer);
  642. #endif
  643. }
  644. if (saved_target) {
  645. SDL_SetRenderTarget(renderer, saved_target);
  646. }
  647. } else if (event->type == SDL_WINDOWEVENT_HIDDEN) {
  648. renderer->hidden = SDL_TRUE;
  649. } else if (event->type == SDL_WINDOWEVENT_SHOWN) {
  650. if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED)) {
  651. renderer->hidden = SDL_FALSE;
  652. }
  653. } else if (event->type == SDL_WINDOWEVENT_MINIMIZED) {
  654. renderer->hidden = SDL_TRUE;
  655. } else if (event->type == SDL_WINDOWEVENT_RESTORED ||
  656. event->type == SDL_WINDOWEVENT_MAXIMIZED) {
  657. if (!(SDL_GetWindowFlags(window) & SDL_WINDOW_HIDDEN)) {
  658. renderer->hidden = SDL_FALSE;
  659. }
  660. }
  661. }
  662. } else if (event->type == SDL_MOUSEMOTION) {
  663. SDL_Window *window = SDL_GetWindowFromID(event->motion.windowID);
  664. if (window == renderer->window) {
  665. int logical_w, logical_h;
  666. SDL_DRect viewport;
  667. SDL_FPoint scale;
  668. GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
  669. if (logical_w) {
  670. event->motion.x -= (int)(viewport.x * renderer->dpi_scale.x);
  671. event->motion.y -= (int)(viewport.y * renderer->dpi_scale.y);
  672. event->motion.x = (int)(event->motion.x / (scale.x * renderer->dpi_scale.x));
  673. event->motion.y = (int)(event->motion.y / (scale.y * renderer->dpi_scale.y));
  674. if (event->motion.xrel != 0 && renderer->relative_scaling) {
  675. float rel = renderer->xrel + event->motion.xrel / (scale.x * renderer->dpi_scale.x);
  676. float truncated = SDL_truncf(rel);
  677. renderer->xrel = rel - truncated;
  678. event->motion.xrel = (Sint32)truncated;
  679. }
  680. if (event->motion.yrel != 0 && renderer->relative_scaling) {
  681. float rel = renderer->yrel + event->motion.yrel / (scale.y * renderer->dpi_scale.y);
  682. float truncated = SDL_truncf(rel);
  683. renderer->yrel = rel - truncated;
  684. event->motion.yrel = (Sint32)truncated;
  685. }
  686. }
  687. }
  688. } else if (event->type == SDL_MOUSEBUTTONDOWN ||
  689. event->type == SDL_MOUSEBUTTONUP) {
  690. SDL_Window *window = SDL_GetWindowFromID(event->button.windowID);
  691. if (window == renderer->window) {
  692. int logical_w, logical_h;
  693. SDL_DRect viewport;
  694. SDL_FPoint scale;
  695. GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
  696. if (logical_w) {
  697. event->button.x -= (int)(viewport.x * renderer->dpi_scale.x);
  698. event->button.y -= (int)(viewport.y * renderer->dpi_scale.y);
  699. event->button.x = (int)(event->button.x / (scale.x * renderer->dpi_scale.x));
  700. event->button.y = (int)(event->button.y / (scale.y * renderer->dpi_scale.y));
  701. }
  702. }
  703. } else if (event->type == SDL_FINGERDOWN ||
  704. event->type == SDL_FINGERUP ||
  705. event->type == SDL_FINGERMOTION) {
  706. int logical_w, logical_h;
  707. float physical_w, physical_h;
  708. SDL_DRect viewport;
  709. SDL_FPoint scale;
  710. GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
  711. /* !!! FIXME: we probably should drop events that are outside of the
  712. !!! FIXME: viewport, but we can't do that from an event watcher,
  713. !!! FIXME: and we would have to track if a touch happened outside
  714. !!! FIXME: the viewport and then slid into it to insert extra
  715. !!! FIXME: events, which is a mess, so for now we just clamp these
  716. !!! FIXME: events to the edge. */
  717. if (renderer->GetOutputSize) {
  718. int w, h;
  719. renderer->GetOutputSize(renderer, &w, &h);
  720. physical_w = (float)w;
  721. physical_h = (float)h;
  722. } else {
  723. int w, h;
  724. SDL_GetWindowSize(renderer->window, &w, &h);
  725. physical_w = ((float)w) * renderer->dpi_scale.x;
  726. physical_h = ((float)h) * renderer->dpi_scale.y;
  727. }
  728. if (physical_w == 0.0f) { /* nowhere for the touch to go, avoid division by zero and put it dead center. */
  729. event->tfinger.x = 0.5f;
  730. } else {
  731. const float normalized_viewport_x = ((float)viewport.x) / physical_w;
  732. const float normalized_viewport_w = ((float)viewport.w) / physical_w;
  733. if (event->tfinger.x <= normalized_viewport_x) {
  734. event->tfinger.x = 0.0f; /* to the left of the viewport, clamp to the edge. */
  735. } else if (event->tfinger.x >= (normalized_viewport_x + normalized_viewport_w)) {
  736. event->tfinger.x = 1.0f; /* to the right of the viewport, clamp to the edge. */
  737. } else {
  738. event->tfinger.x = (event->tfinger.x - normalized_viewport_x) / normalized_viewport_w;
  739. }
  740. }
  741. if (physical_h == 0.0f) { /* nowhere for the touch to go, avoid division by zero and put it dead center. */
  742. event->tfinger.y = 0.5f;
  743. } else {
  744. const float normalized_viewport_y = ((float)viewport.y) / physical_h;
  745. const float normalized_viewport_h = ((float)viewport.h) / physical_h;
  746. if (event->tfinger.y <= normalized_viewport_y) {
  747. event->tfinger.y = 0.0f; /* to the left of the viewport, clamp to the edge. */
  748. } else if (event->tfinger.y >= (normalized_viewport_y + normalized_viewport_h)) {
  749. event->tfinger.y = 1.0f; /* to the right of the viewport, clamp to the edge. */
  750. } else {
  751. event->tfinger.y = (event->tfinger.y - normalized_viewport_y) / normalized_viewport_h;
  752. }
  753. }
  754. }
  755. return 0;
  756. }
  757. int SDL_CreateWindowAndRenderer(int width, int height, Uint32 window_flags,
  758. SDL_Window **window, SDL_Renderer **renderer)
  759. {
  760. *window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED,
  761. SDL_WINDOWPOS_UNDEFINED,
  762. width, height, window_flags);
  763. if (!*window) {
  764. *renderer = NULL;
  765. return -1;
  766. }
  767. *renderer = SDL_CreateRenderer(*window, NULL, 0);
  768. if (!*renderer) {
  769. return -1;
  770. }
  771. return 0;
  772. }
  773. #if !SDL_RENDER_DISABLED
  774. static SDL_INLINE void VerifyDrawQueueFunctions(const SDL_Renderer *renderer)
  775. {
  776. /* all of these functions are required to be implemented, even as no-ops, so we don't
  777. have to check that they aren't NULL over and over. */
  778. SDL_assert(renderer->QueueSetViewport != NULL);
  779. SDL_assert(renderer->QueueSetDrawColor != NULL);
  780. SDL_assert(renderer->QueueDrawPoints != NULL);
  781. SDL_assert(renderer->QueueDrawLines != NULL || renderer->QueueGeometry != NULL);
  782. SDL_assert(renderer->QueueFillRects != NULL || renderer->QueueGeometry != NULL);
  783. SDL_assert(renderer->QueueCopy != NULL || renderer->QueueGeometry != NULL);
  784. SDL_assert(renderer->RunCommandQueue != NULL);
  785. }
  786. static SDL_RenderLineMethod SDL_GetRenderLineMethod()
  787. {
  788. const char *hint = SDL_GetHint(SDL_HINT_RENDER_LINE_METHOD);
  789. int method = 0;
  790. if (hint) {
  791. method = SDL_atoi(hint);
  792. }
  793. switch (method) {
  794. case 1:
  795. return SDL_RENDERLINEMETHOD_POINTS;
  796. case 2:
  797. return SDL_RENDERLINEMETHOD_LINES;
  798. case 3:
  799. return SDL_RENDERLINEMETHOD_GEOMETRY;
  800. default:
  801. return SDL_RENDERLINEMETHOD_POINTS;
  802. }
  803. }
  804. static void SDL_CalculateSimulatedVSyncInterval(SDL_Renderer *renderer, SDL_Window *window)
  805. {
  806. /* FIXME: SDL refresh rate API should return numerator/denominator */
  807. int refresh_rate = 0;
  808. int display_index = SDL_GetWindowDisplayIndex(window);
  809. SDL_DisplayMode mode;
  810. if (display_index < 0) {
  811. display_index = 0;
  812. }
  813. if (SDL_GetDesktopDisplayMode(display_index, &mode) == 0) {
  814. refresh_rate = mode.refresh_rate;
  815. }
  816. if (!refresh_rate) {
  817. /* Pick a good default refresh rate */
  818. refresh_rate = 60;
  819. }
  820. renderer->simulate_vsync_interval_ns = (SDL_NS_PER_SECOND / refresh_rate);
  821. }
  822. #endif /* !SDL_RENDER_DISABLED */
  823. SDL_Renderer *
  824. SDL_CreateRenderer(SDL_Window *window, const char *name, Uint32 flags)
  825. {
  826. #if !SDL_RENDER_DISABLED
  827. SDL_Renderer *renderer = NULL;
  828. const int n = SDL_GetNumRenderDrivers();
  829. SDL_bool batching = SDL_TRUE;
  830. const char *hint;
  831. int i;
  832. #if defined(__ANDROID__)
  833. Android_ActivityMutex_Lock_Running();
  834. #endif
  835. if (window == NULL) {
  836. SDL_InvalidParamError("window");
  837. goto error;
  838. }
  839. if (SDL_GetRenderer(window)) {
  840. SDL_SetError("Renderer already associated with window");
  841. goto error;
  842. }
  843. hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC);
  844. if (hint && *hint) {
  845. if (SDL_GetHintBoolean(SDL_HINT_RENDER_VSYNC, SDL_TRUE)) {
  846. flags |= SDL_RENDERER_PRESENTVSYNC;
  847. } else {
  848. flags &= ~SDL_RENDERER_PRESENTVSYNC;
  849. }
  850. }
  851. if (!name) {
  852. name = SDL_GetHint(SDL_HINT_RENDER_DRIVER);
  853. }
  854. if (name) {
  855. for (i = 0; i < n; i++) {
  856. const SDL_RenderDriver *driver = render_drivers[i];
  857. if (SDL_strcasecmp(name, driver->info.name) == 0) {
  858. /* Create a new renderer instance */
  859. renderer = driver->CreateRenderer(window, flags);
  860. if (renderer) {
  861. batching = SDL_FALSE;
  862. }
  863. break;
  864. }
  865. }
  866. } else {
  867. for (i = 0; i < n; i++) {
  868. const SDL_RenderDriver *driver = render_drivers[i];
  869. if ((driver->info.flags & flags) == flags) {
  870. /* Create a new renderer instance */
  871. renderer = driver->CreateRenderer(window, flags);
  872. if (renderer) {
  873. /* Yay, we got one! */
  874. break;
  875. }
  876. }
  877. }
  878. }
  879. if (renderer == NULL) {
  880. SDL_SetError("Couldn't find matching render driver");
  881. goto error;
  882. }
  883. if ((flags & SDL_RENDERER_PRESENTVSYNC) != 0) {
  884. renderer->wanted_vsync = SDL_TRUE;
  885. if ((renderer->info.flags & SDL_RENDERER_PRESENTVSYNC) == 0) {
  886. renderer->simulate_vsync = SDL_TRUE;
  887. renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
  888. }
  889. }
  890. SDL_CalculateSimulatedVSyncInterval(renderer, window);
  891. VerifyDrawQueueFunctions(renderer);
  892. /* let app/user override batching decisions. */
  893. if (renderer->always_batch) {
  894. batching = SDL_TRUE;
  895. } else if (SDL_GetHint(SDL_HINT_RENDER_BATCHING)) {
  896. batching = SDL_GetHintBoolean(SDL_HINT_RENDER_BATCHING, SDL_TRUE);
  897. }
  898. renderer->batching = batching;
  899. renderer->magic = &renderer_magic;
  900. renderer->window = window;
  901. renderer->target_mutex = SDL_CreateMutex();
  902. renderer->scale.x = 1.0f;
  903. renderer->scale.y = 1.0f;
  904. renderer->dpi_scale.x = 1.0f;
  905. renderer->dpi_scale.y = 1.0f;
  906. /* Default value, if not specified by the renderer back-end */
  907. if (renderer->rect_index_order[0] == 0 && renderer->rect_index_order[1] == 0) {
  908. renderer->rect_index_order[0] = 0;
  909. renderer->rect_index_order[1] = 1;
  910. renderer->rect_index_order[2] = 2;
  911. renderer->rect_index_order[3] = 0;
  912. renderer->rect_index_order[4] = 2;
  913. renderer->rect_index_order[5] = 3;
  914. }
  915. /* new textures start at zero, so we start at 1 so first render doesn't flush by accident. */
  916. renderer->render_command_generation = 1;
  917. if (renderer->GetOutputSize) {
  918. int window_w, window_h;
  919. int output_w, output_h;
  920. if (renderer->GetOutputSize(renderer, &output_w, &output_h) == 0) {
  921. SDL_GetWindowSize(renderer->window, &window_w, &window_h);
  922. renderer->dpi_scale.x = (float)window_w / output_w;
  923. renderer->dpi_scale.y = (float)window_h / output_h;
  924. }
  925. }
  926. renderer->relative_scaling = SDL_GetHintBoolean(SDL_HINT_MOUSE_RELATIVE_SCALING, SDL_TRUE);
  927. renderer->line_method = SDL_GetRenderLineMethod();
  928. if (SDL_GetWindowFlags(window) & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED)) {
  929. renderer->hidden = SDL_TRUE;
  930. } else {
  931. renderer->hidden = SDL_FALSE;
  932. }
  933. SDL_SetWindowData(window, SDL_WINDOWRENDERDATA, renderer);
  934. SDL_SetRenderViewport(renderer, NULL);
  935. SDL_AddEventWatch(SDL_RendererEventWatch, renderer);
  936. SDL_LogInfo(SDL_LOG_CATEGORY_RENDER,
  937. "Created renderer: %s", renderer->info.name);
  938. #if defined(__ANDROID__)
  939. Android_ActivityMutex_Unlock();
  940. #endif
  941. return renderer;
  942. error:
  943. #if defined(__ANDROID__)
  944. Android_ActivityMutex_Unlock();
  945. #endif
  946. return NULL;
  947. #else
  948. SDL_SetError("SDL not built with rendering support");
  949. return NULL;
  950. #endif
  951. }
  952. SDL_Renderer *
  953. SDL_CreateSoftwareRenderer(SDL_Surface *surface)
  954. {
  955. #if !SDL_RENDER_DISABLED && SDL_VIDEO_RENDER_SW
  956. SDL_Renderer *renderer;
  957. renderer = SW_CreateRendererForSurface(surface);
  958. if (renderer) {
  959. VerifyDrawQueueFunctions(renderer);
  960. renderer->magic = &renderer_magic;
  961. renderer->target_mutex = SDL_CreateMutex();
  962. renderer->scale.x = 1.0f;
  963. renderer->scale.y = 1.0f;
  964. /* new textures start at zero, so we start at 1 so first render doesn't flush by accident. */
  965. renderer->render_command_generation = 1;
  966. /* Software renderer always uses line method, for speed */
  967. renderer->line_method = SDL_RENDERLINEMETHOD_LINES;
  968. SDL_SetRenderViewport(renderer, NULL);
  969. }
  970. return renderer;
  971. #else
  972. SDL_SetError("SDL not built with rendering support");
  973. return NULL;
  974. #endif /* !SDL_RENDER_DISABLED */
  975. }
  976. SDL_Renderer *
  977. SDL_GetRenderer(SDL_Window *window)
  978. {
  979. return (SDL_Renderer *)SDL_GetWindowData(window, SDL_WINDOWRENDERDATA);
  980. }
  981. SDL_Window *
  982. SDL_GetRenderWindow(SDL_Renderer *renderer)
  983. {
  984. CHECK_RENDERER_MAGIC(renderer, NULL);
  985. return renderer->window;
  986. }
  987. int SDL_GetRendererInfo(SDL_Renderer *renderer, SDL_RendererInfo *info)
  988. {
  989. CHECK_RENDERER_MAGIC(renderer, -1);
  990. *info = renderer->info;
  991. return 0;
  992. }
  993. int SDL_GetRendererOutputSize(SDL_Renderer *renderer, int *w, int *h)
  994. {
  995. CHECK_RENDERER_MAGIC(renderer, -1);
  996. if (renderer->target) {
  997. return SDL_QueryTexture(renderer->target, NULL, NULL, w, h);
  998. } else if (renderer->GetOutputSize) {
  999. return renderer->GetOutputSize(renderer, w, h);
  1000. } else if (renderer->window) {
  1001. SDL_GetWindowSize(renderer->window, w, h);
  1002. return 0;
  1003. } else {
  1004. SDL_assert(0 && "This should never happen");
  1005. return SDL_SetError("Renderer doesn't support querying output size");
  1006. }
  1007. }
  1008. static SDL_bool IsSupportedBlendMode(SDL_Renderer *renderer, SDL_BlendMode blendMode)
  1009. {
  1010. switch (blendMode) {
  1011. /* These are required to be supported by all renderers */
  1012. case SDL_BLENDMODE_NONE:
  1013. case SDL_BLENDMODE_BLEND:
  1014. case SDL_BLENDMODE_ADD:
  1015. case SDL_BLENDMODE_MOD:
  1016. case SDL_BLENDMODE_MUL:
  1017. return SDL_TRUE;
  1018. default:
  1019. return renderer->SupportsBlendMode && renderer->SupportsBlendMode(renderer, blendMode);
  1020. }
  1021. }
  1022. static SDL_bool IsSupportedFormat(SDL_Renderer *renderer, Uint32 format)
  1023. {
  1024. Uint32 i;
  1025. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  1026. if (renderer->info.texture_formats[i] == format) {
  1027. return SDL_TRUE;
  1028. }
  1029. }
  1030. return SDL_FALSE;
  1031. }
  1032. static Uint32 GetClosestSupportedFormat(SDL_Renderer *renderer, Uint32 format)
  1033. {
  1034. Uint32 i;
  1035. if (SDL_ISPIXELFORMAT_FOURCC(format)) {
  1036. /* Look for an exact match */
  1037. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  1038. if (renderer->info.texture_formats[i] == format) {
  1039. return renderer->info.texture_formats[i];
  1040. }
  1041. }
  1042. } else {
  1043. SDL_bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
  1044. /* We just want to match the first format that has the same channels */
  1045. for (i = 0; i < renderer->info.num_texture_formats; ++i) {
  1046. if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
  1047. SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == hasAlpha) {
  1048. return renderer->info.texture_formats[i];
  1049. }
  1050. }
  1051. }
  1052. return renderer->info.texture_formats[0];
  1053. }
  1054. static SDL_ScaleMode SDL_GetScaleMode(void)
  1055. {
  1056. const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
  1057. if (hint == NULL || SDL_strcasecmp(hint, "nearest") == 0) {
  1058. return SDL_ScaleModeNearest;
  1059. } else if (SDL_strcasecmp(hint, "linear") == 0) {
  1060. return SDL_ScaleModeLinear;
  1061. } else if (SDL_strcasecmp(hint, "best") == 0) {
  1062. return SDL_ScaleModeBest;
  1063. } else {
  1064. return (SDL_ScaleMode)SDL_atoi(hint);
  1065. }
  1066. }
  1067. SDL_Texture *
  1068. SDL_CreateTexture(SDL_Renderer *renderer, Uint32 format, int access, int w, int h)
  1069. {
  1070. SDL_Texture *texture;
  1071. SDL_bool texture_is_fourcc_and_target;
  1072. CHECK_RENDERER_MAGIC(renderer, NULL);
  1073. if (!format) {
  1074. format = renderer->info.texture_formats[0];
  1075. }
  1076. if (SDL_BYTESPERPIXEL(format) == 0) {
  1077. SDL_SetError("Invalid texture format");
  1078. return NULL;
  1079. }
  1080. if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1081. if (!IsSupportedFormat(renderer, format)) {
  1082. SDL_SetError("Palettized textures are not supported");
  1083. return NULL;
  1084. }
  1085. }
  1086. if (w <= 0 || h <= 0) {
  1087. SDL_SetError("Texture dimensions can't be 0");
  1088. return NULL;
  1089. }
  1090. if ((renderer->info.max_texture_width && w > renderer->info.max_texture_width) ||
  1091. (renderer->info.max_texture_height && h > renderer->info.max_texture_height)) {
  1092. SDL_SetError("Texture dimensions are limited to %dx%d", renderer->info.max_texture_width, renderer->info.max_texture_height);
  1093. return NULL;
  1094. }
  1095. texture = (SDL_Texture *)SDL_calloc(1, sizeof(*texture));
  1096. if (texture == NULL) {
  1097. SDL_OutOfMemory();
  1098. return NULL;
  1099. }
  1100. texture->magic = &texture_magic;
  1101. texture->format = format;
  1102. texture->access = access;
  1103. texture->w = w;
  1104. texture->h = h;
  1105. texture->color.r = 255;
  1106. texture->color.g = 255;
  1107. texture->color.b = 255;
  1108. texture->color.a = 255;
  1109. texture->scaleMode = SDL_GetScaleMode();
  1110. texture->renderer = renderer;
  1111. texture->next = renderer->textures;
  1112. if (renderer->textures) {
  1113. renderer->textures->prev = texture;
  1114. }
  1115. renderer->textures = texture;
  1116. /* FOURCC format cannot be used directly by renderer back-ends for target texture */
  1117. texture_is_fourcc_and_target = (access == SDL_TEXTUREACCESS_TARGET && SDL_ISPIXELFORMAT_FOURCC(texture->format));
  1118. if (texture_is_fourcc_and_target == SDL_FALSE && IsSupportedFormat(renderer, format)) {
  1119. if (renderer->CreateTexture(renderer, texture) < 0) {
  1120. SDL_DestroyTexture(texture);
  1121. return NULL;
  1122. }
  1123. } else {
  1124. int closest_format;
  1125. if (texture_is_fourcc_and_target == SDL_FALSE) {
  1126. closest_format = GetClosestSupportedFormat(renderer, format);
  1127. } else {
  1128. closest_format = renderer->info.texture_formats[0];
  1129. }
  1130. texture->native = SDL_CreateTexture(renderer, closest_format, access, w, h);
  1131. if (!texture->native) {
  1132. SDL_DestroyTexture(texture);
  1133. return NULL;
  1134. }
  1135. /* Swap textures to have texture before texture->native in the list */
  1136. texture->native->next = texture->next;
  1137. if (texture->native->next) {
  1138. texture->native->next->prev = texture->native;
  1139. }
  1140. texture->prev = texture->native->prev;
  1141. if (texture->prev) {
  1142. texture->prev->next = texture;
  1143. }
  1144. texture->native->prev = texture;
  1145. texture->next = texture->native;
  1146. renderer->textures = texture;
  1147. if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
  1148. #if SDL_HAVE_YUV
  1149. texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
  1150. #else
  1151. SDL_SetError("SDL not built with YUV support");
  1152. #endif
  1153. if (!texture->yuv) {
  1154. SDL_DestroyTexture(texture);
  1155. return NULL;
  1156. }
  1157. } else if (access == SDL_TEXTUREACCESS_STREAMING) {
  1158. /* The pitch is 4 byte aligned */
  1159. texture->pitch = (((w * SDL_BYTESPERPIXEL(format)) + 3) & ~3);
  1160. texture->pixels = SDL_calloc(1, (size_t)texture->pitch * h);
  1161. if (!texture->pixels) {
  1162. SDL_DestroyTexture(texture);
  1163. return NULL;
  1164. }
  1165. }
  1166. }
  1167. return texture;
  1168. }
  1169. SDL_Texture *
  1170. SDL_CreateTextureFromSurface(SDL_Renderer *renderer, SDL_Surface *surface)
  1171. {
  1172. const SDL_PixelFormat *fmt;
  1173. SDL_bool needAlpha;
  1174. SDL_bool direct_update;
  1175. int i;
  1176. Uint32 format = SDL_PIXELFORMAT_UNKNOWN;
  1177. SDL_Texture *texture;
  1178. CHECK_RENDERER_MAGIC(renderer, NULL);
  1179. if (surface == NULL) {
  1180. SDL_InvalidParamError("SDL_CreateTextureFromSurface(): surface");
  1181. return NULL;
  1182. }
  1183. /* See what the best texture format is */
  1184. fmt = surface->format;
  1185. if (fmt->Amask || SDL_SurfaceHasColorKey(surface)) {
  1186. needAlpha = SDL_TRUE;
  1187. } else {
  1188. needAlpha = SDL_FALSE;
  1189. }
  1190. /* If Palette contains alpha values, promotes to alpha format */
  1191. if (fmt->palette) {
  1192. SDL_bool is_opaque, has_alpha_channel;
  1193. SDL_DetectPalette(fmt->palette, &is_opaque, &has_alpha_channel);
  1194. if (!is_opaque) {
  1195. needAlpha = SDL_TRUE;
  1196. }
  1197. }
  1198. /* Try to have the best pixel format for the texture */
  1199. /* No alpha, but a colorkey => promote to alpha */
  1200. if (!fmt->Amask && SDL_SurfaceHasColorKey(surface)) {
  1201. if (fmt->format == SDL_PIXELFORMAT_RGB888) {
  1202. for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
  1203. if (renderer->info.texture_formats[i] == SDL_PIXELFORMAT_ARGB8888) {
  1204. format = SDL_PIXELFORMAT_ARGB8888;
  1205. break;
  1206. }
  1207. }
  1208. } else if (fmt->format == SDL_PIXELFORMAT_BGR888) {
  1209. for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
  1210. if (renderer->info.texture_formats[i] == SDL_PIXELFORMAT_ABGR8888) {
  1211. format = SDL_PIXELFORMAT_ABGR8888;
  1212. break;
  1213. }
  1214. }
  1215. }
  1216. } else {
  1217. /* Exact match would be fine */
  1218. for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
  1219. if (renderer->info.texture_formats[i] == fmt->format) {
  1220. format = fmt->format;
  1221. break;
  1222. }
  1223. }
  1224. }
  1225. /* Fallback, choose a valid pixel format */
  1226. if (format == SDL_PIXELFORMAT_UNKNOWN) {
  1227. format = renderer->info.texture_formats[0];
  1228. for (i = 0; i < (int)renderer->info.num_texture_formats; ++i) {
  1229. if (!SDL_ISPIXELFORMAT_FOURCC(renderer->info.texture_formats[i]) &&
  1230. SDL_ISPIXELFORMAT_ALPHA(renderer->info.texture_formats[i]) == needAlpha) {
  1231. format = renderer->info.texture_formats[i];
  1232. break;
  1233. }
  1234. }
  1235. }
  1236. texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STATIC,
  1237. surface->w, surface->h);
  1238. if (texture == NULL) {
  1239. return NULL;
  1240. }
  1241. if (format == surface->format->format) {
  1242. if (surface->format->Amask && SDL_SurfaceHasColorKey(surface)) {
  1243. /* Surface and Renderer formats are identicals.
  1244. * Intermediate conversion is needed to convert color key to alpha (SDL_ConvertColorkeyToAlpha()). */
  1245. direct_update = SDL_FALSE;
  1246. } else {
  1247. /* Update Texture directly */
  1248. direct_update = SDL_TRUE;
  1249. }
  1250. } else {
  1251. /* Surface and Renderer formats are differents, it needs an intermediate conversion. */
  1252. direct_update = SDL_FALSE;
  1253. }
  1254. if (direct_update) {
  1255. if (SDL_MUSTLOCK(surface)) {
  1256. SDL_LockSurface(surface);
  1257. SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
  1258. SDL_UnlockSurface(surface);
  1259. } else {
  1260. SDL_UpdateTexture(texture, NULL, surface->pixels, surface->pitch);
  1261. }
  1262. } else {
  1263. SDL_PixelFormat *dst_fmt;
  1264. SDL_Surface *temp = NULL;
  1265. /* Set up a destination surface for the texture update */
  1266. dst_fmt = SDL_CreatePixelFormat(format);
  1267. if (dst_fmt == NULL) {
  1268. SDL_DestroyTexture(texture);
  1269. return NULL;
  1270. }
  1271. temp = SDL_ConvertSurface(surface, dst_fmt);
  1272. SDL_DestroyPixelFormat(dst_fmt);
  1273. if (temp) {
  1274. SDL_UpdateTexture(texture, NULL, temp->pixels, temp->pitch);
  1275. SDL_DestroySurface(temp);
  1276. } else {
  1277. SDL_DestroyTexture(texture);
  1278. return NULL;
  1279. }
  1280. }
  1281. {
  1282. Uint8 r, g, b, a;
  1283. SDL_BlendMode blendMode;
  1284. SDL_GetSurfaceColorMod(surface, &r, &g, &b);
  1285. SDL_SetTextureColorMod(texture, r, g, b);
  1286. SDL_GetSurfaceAlphaMod(surface, &a);
  1287. SDL_SetTextureAlphaMod(texture, a);
  1288. if (SDL_SurfaceHasColorKey(surface)) {
  1289. /* We converted to a texture with alpha format */
  1290. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  1291. } else {
  1292. SDL_GetSurfaceBlendMode(surface, &blendMode);
  1293. SDL_SetTextureBlendMode(texture, blendMode);
  1294. }
  1295. }
  1296. return texture;
  1297. }
  1298. int SDL_QueryTexture(SDL_Texture *texture, Uint32 *format, int *access,
  1299. int *w, int *h)
  1300. {
  1301. CHECK_TEXTURE_MAGIC(texture, -1);
  1302. if (format) {
  1303. *format = texture->format;
  1304. }
  1305. if (access) {
  1306. *access = texture->access;
  1307. }
  1308. if (w) {
  1309. *w = texture->w;
  1310. }
  1311. if (h) {
  1312. *h = texture->h;
  1313. }
  1314. return 0;
  1315. }
  1316. int SDL_SetTextureColorMod(SDL_Texture *texture, Uint8 r, Uint8 g, Uint8 b)
  1317. {
  1318. CHECK_TEXTURE_MAGIC(texture, -1);
  1319. if (r < 255 || g < 255 || b < 255) {
  1320. texture->modMode |= SDL_TEXTUREMODULATE_COLOR;
  1321. } else {
  1322. texture->modMode &= ~SDL_TEXTUREMODULATE_COLOR;
  1323. }
  1324. texture->color.r = r;
  1325. texture->color.g = g;
  1326. texture->color.b = b;
  1327. if (texture->native) {
  1328. return SDL_SetTextureColorMod(texture->native, r, g, b);
  1329. }
  1330. return 0;
  1331. }
  1332. int SDL_GetTextureColorMod(SDL_Texture *texture, Uint8 *r, Uint8 *g,
  1333. Uint8 *b)
  1334. {
  1335. CHECK_TEXTURE_MAGIC(texture, -1);
  1336. if (r) {
  1337. *r = texture->color.r;
  1338. }
  1339. if (g) {
  1340. *g = texture->color.g;
  1341. }
  1342. if (b) {
  1343. *b = texture->color.b;
  1344. }
  1345. return 0;
  1346. }
  1347. int SDL_SetTextureAlphaMod(SDL_Texture *texture, Uint8 alpha)
  1348. {
  1349. CHECK_TEXTURE_MAGIC(texture, -1);
  1350. if (alpha < 255) {
  1351. texture->modMode |= SDL_TEXTUREMODULATE_ALPHA;
  1352. } else {
  1353. texture->modMode &= ~SDL_TEXTUREMODULATE_ALPHA;
  1354. }
  1355. texture->color.a = alpha;
  1356. if (texture->native) {
  1357. return SDL_SetTextureAlphaMod(texture->native, alpha);
  1358. }
  1359. return 0;
  1360. }
  1361. int SDL_GetTextureAlphaMod(SDL_Texture *texture, Uint8 *alpha)
  1362. {
  1363. CHECK_TEXTURE_MAGIC(texture, -1);
  1364. if (alpha) {
  1365. *alpha = texture->color.a;
  1366. }
  1367. return 0;
  1368. }
  1369. int SDL_SetTextureBlendMode(SDL_Texture *texture, SDL_BlendMode blendMode)
  1370. {
  1371. SDL_Renderer *renderer;
  1372. CHECK_TEXTURE_MAGIC(texture, -1);
  1373. renderer = texture->renderer;
  1374. if (!IsSupportedBlendMode(renderer, blendMode)) {
  1375. return SDL_Unsupported();
  1376. }
  1377. texture->blendMode = blendMode;
  1378. if (texture->native) {
  1379. return SDL_SetTextureBlendMode(texture->native, blendMode);
  1380. }
  1381. return 0;
  1382. }
  1383. int SDL_GetTextureBlendMode(SDL_Texture *texture, SDL_BlendMode *blendMode)
  1384. {
  1385. CHECK_TEXTURE_MAGIC(texture, -1);
  1386. if (blendMode) {
  1387. *blendMode = texture->blendMode;
  1388. }
  1389. return 0;
  1390. }
  1391. int SDL_SetTextureScaleMode(SDL_Texture *texture, SDL_ScaleMode scaleMode)
  1392. {
  1393. SDL_Renderer *renderer;
  1394. CHECK_TEXTURE_MAGIC(texture, -1);
  1395. renderer = texture->renderer;
  1396. texture->scaleMode = scaleMode;
  1397. if (texture->native) {
  1398. return SDL_SetTextureScaleMode(texture->native, scaleMode);
  1399. } else {
  1400. renderer->SetTextureScaleMode(renderer, texture, scaleMode);
  1401. }
  1402. return 0;
  1403. }
  1404. int SDL_GetTextureScaleMode(SDL_Texture *texture, SDL_ScaleMode *scaleMode)
  1405. {
  1406. CHECK_TEXTURE_MAGIC(texture, -1);
  1407. if (scaleMode) {
  1408. *scaleMode = texture->scaleMode;
  1409. }
  1410. return 0;
  1411. }
  1412. int SDL_SetTextureUserData(SDL_Texture *texture, void *userdata)
  1413. {
  1414. CHECK_TEXTURE_MAGIC(texture, -1);
  1415. texture->userdata = userdata;
  1416. return 0;
  1417. }
  1418. void *
  1419. SDL_GetTextureUserData(SDL_Texture *texture)
  1420. {
  1421. CHECK_TEXTURE_MAGIC(texture, NULL);
  1422. return texture->userdata;
  1423. }
  1424. #if SDL_HAVE_YUV
  1425. static int SDL_UpdateTextureYUV(SDL_Texture *texture, const SDL_Rect *rect,
  1426. const void *pixels, int pitch)
  1427. {
  1428. SDL_Texture *native = texture->native;
  1429. SDL_Rect full_rect;
  1430. if (SDL_SW_UpdateYUVTexture(texture->yuv, rect, pixels, pitch) < 0) {
  1431. return -1;
  1432. }
  1433. full_rect.x = 0;
  1434. full_rect.y = 0;
  1435. full_rect.w = texture->w;
  1436. full_rect.h = texture->h;
  1437. rect = &full_rect;
  1438. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1439. /* We can lock the texture and copy to it */
  1440. void *native_pixels = NULL;
  1441. int native_pitch = 0;
  1442. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1443. return -1;
  1444. }
  1445. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1446. rect->w, rect->h, native_pixels, native_pitch);
  1447. SDL_UnlockTexture(native);
  1448. } else {
  1449. /* Use a temporary buffer for updating */
  1450. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1451. const size_t alloclen = (size_t)rect->h * temp_pitch;
  1452. if (alloclen > 0) {
  1453. void *temp_pixels = SDL_malloc(alloclen);
  1454. if (temp_pixels == NULL) {
  1455. return SDL_OutOfMemory();
  1456. }
  1457. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1458. rect->w, rect->h, temp_pixels, temp_pitch);
  1459. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1460. SDL_free(temp_pixels);
  1461. }
  1462. }
  1463. return 0;
  1464. }
  1465. #endif /* SDL_HAVE_YUV */
  1466. static int SDL_UpdateTextureNative(SDL_Texture *texture, const SDL_Rect *rect,
  1467. const void *pixels, int pitch)
  1468. {
  1469. SDL_Texture *native = texture->native;
  1470. if (!rect->w || !rect->h) {
  1471. return 0; /* nothing to do. */
  1472. }
  1473. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1474. /* We can lock the texture and copy to it */
  1475. void *native_pixels = NULL;
  1476. int native_pitch = 0;
  1477. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1478. return -1;
  1479. }
  1480. SDL_ConvertPixels(rect->w, rect->h,
  1481. texture->format, pixels, pitch,
  1482. native->format, native_pixels, native_pitch);
  1483. SDL_UnlockTexture(native);
  1484. } else {
  1485. /* Use a temporary buffer for updating */
  1486. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1487. const size_t alloclen = (size_t)rect->h * temp_pitch;
  1488. if (alloclen > 0) {
  1489. void *temp_pixels = SDL_malloc(alloclen);
  1490. if (temp_pixels == NULL) {
  1491. return SDL_OutOfMemory();
  1492. }
  1493. SDL_ConvertPixels(rect->w, rect->h,
  1494. texture->format, pixels, pitch,
  1495. native->format, temp_pixels, temp_pitch);
  1496. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1497. SDL_free(temp_pixels);
  1498. }
  1499. }
  1500. return 0;
  1501. }
  1502. int SDL_UpdateTexture(SDL_Texture *texture, const SDL_Rect *rect,
  1503. const void *pixels, int pitch)
  1504. {
  1505. SDL_Rect real_rect;
  1506. CHECK_TEXTURE_MAGIC(texture, -1);
  1507. if (pixels == NULL) {
  1508. return SDL_InvalidParamError("pixels");
  1509. }
  1510. if (!pitch) {
  1511. return SDL_InvalidParamError("pitch");
  1512. }
  1513. real_rect.x = 0;
  1514. real_rect.y = 0;
  1515. real_rect.w = texture->w;
  1516. real_rect.h = texture->h;
  1517. if (rect) {
  1518. if (!SDL_GetRectIntersection(rect, &real_rect, &real_rect)) {
  1519. return 0;
  1520. }
  1521. }
  1522. if (real_rect.w == 0 || real_rect.h == 0) {
  1523. return 0; /* nothing to do. */
  1524. #if SDL_HAVE_YUV
  1525. } else if (texture->yuv) {
  1526. return SDL_UpdateTextureYUV(texture, &real_rect, pixels, pitch);
  1527. #endif
  1528. } else if (texture->native) {
  1529. return SDL_UpdateTextureNative(texture, &real_rect, pixels, pitch);
  1530. } else {
  1531. SDL_Renderer *renderer = texture->renderer;
  1532. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1533. return -1;
  1534. }
  1535. return renderer->UpdateTexture(renderer, texture, &real_rect, pixels, pitch);
  1536. }
  1537. }
  1538. #if SDL_HAVE_YUV
  1539. static int SDL_UpdateTextureYUVPlanar(SDL_Texture *texture, const SDL_Rect *rect,
  1540. const Uint8 *Yplane, int Ypitch,
  1541. const Uint8 *Uplane, int Upitch,
  1542. const Uint8 *Vplane, int Vpitch)
  1543. {
  1544. SDL_Texture *native = texture->native;
  1545. SDL_Rect full_rect;
  1546. if (SDL_SW_UpdateYUVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch) < 0) {
  1547. return -1;
  1548. }
  1549. full_rect.x = 0;
  1550. full_rect.y = 0;
  1551. full_rect.w = texture->w;
  1552. full_rect.h = texture->h;
  1553. rect = &full_rect;
  1554. if (!rect->w || !rect->h) {
  1555. return 0; /* nothing to do. */
  1556. }
  1557. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1558. /* We can lock the texture and copy to it */
  1559. void *native_pixels = NULL;
  1560. int native_pitch = 0;
  1561. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1562. return -1;
  1563. }
  1564. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1565. rect->w, rect->h, native_pixels, native_pitch);
  1566. SDL_UnlockTexture(native);
  1567. } else {
  1568. /* Use a temporary buffer for updating */
  1569. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1570. const size_t alloclen = (size_t)rect->h * temp_pitch;
  1571. if (alloclen > 0) {
  1572. void *temp_pixels = SDL_malloc(alloclen);
  1573. if (temp_pixels == NULL) {
  1574. return SDL_OutOfMemory();
  1575. }
  1576. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1577. rect->w, rect->h, temp_pixels, temp_pitch);
  1578. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1579. SDL_free(temp_pixels);
  1580. }
  1581. }
  1582. return 0;
  1583. }
  1584. static int SDL_UpdateTextureNVPlanar(SDL_Texture *texture, const SDL_Rect *rect,
  1585. const Uint8 *Yplane, int Ypitch,
  1586. const Uint8 *UVplane, int UVpitch)
  1587. {
  1588. SDL_Texture *native = texture->native;
  1589. SDL_Rect full_rect;
  1590. if (SDL_SW_UpdateNVTexturePlanar(texture->yuv, rect, Yplane, Ypitch, UVplane, UVpitch) < 0) {
  1591. return -1;
  1592. }
  1593. full_rect.x = 0;
  1594. full_rect.y = 0;
  1595. full_rect.w = texture->w;
  1596. full_rect.h = texture->h;
  1597. rect = &full_rect;
  1598. if (!rect->w || !rect->h) {
  1599. return 0; /* nothing to do. */
  1600. }
  1601. if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
  1602. /* We can lock the texture and copy to it */
  1603. void *native_pixels = NULL;
  1604. int native_pitch = 0;
  1605. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1606. return -1;
  1607. }
  1608. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1609. rect->w, rect->h, native_pixels, native_pitch);
  1610. SDL_UnlockTexture(native);
  1611. } else {
  1612. /* Use a temporary buffer for updating */
  1613. const int temp_pitch = (((rect->w * SDL_BYTESPERPIXEL(native->format)) + 3) & ~3);
  1614. const size_t alloclen = (size_t)rect->h * temp_pitch;
  1615. if (alloclen > 0) {
  1616. void *temp_pixels = SDL_malloc(alloclen);
  1617. if (temp_pixels == NULL) {
  1618. return SDL_OutOfMemory();
  1619. }
  1620. SDL_SW_CopyYUVToRGB(texture->yuv, rect, native->format,
  1621. rect->w, rect->h, temp_pixels, temp_pitch);
  1622. SDL_UpdateTexture(native, rect, temp_pixels, temp_pitch);
  1623. SDL_free(temp_pixels);
  1624. }
  1625. }
  1626. return 0;
  1627. }
  1628. #endif /* SDL_HAVE_YUV */
  1629. int SDL_UpdateYUVTexture(SDL_Texture *texture, const SDL_Rect *rect,
  1630. const Uint8 *Yplane, int Ypitch,
  1631. const Uint8 *Uplane, int Upitch,
  1632. const Uint8 *Vplane, int Vpitch)
  1633. {
  1634. #if SDL_HAVE_YUV
  1635. SDL_Renderer *renderer;
  1636. SDL_Rect real_rect;
  1637. CHECK_TEXTURE_MAGIC(texture, -1);
  1638. if (Yplane == NULL) {
  1639. return SDL_InvalidParamError("Yplane");
  1640. }
  1641. if (!Ypitch) {
  1642. return SDL_InvalidParamError("Ypitch");
  1643. }
  1644. if (Uplane == NULL) {
  1645. return SDL_InvalidParamError("Uplane");
  1646. }
  1647. if (!Upitch) {
  1648. return SDL_InvalidParamError("Upitch");
  1649. }
  1650. if (Vplane == NULL) {
  1651. return SDL_InvalidParamError("Vplane");
  1652. }
  1653. if (!Vpitch) {
  1654. return SDL_InvalidParamError("Vpitch");
  1655. }
  1656. if (texture->format != SDL_PIXELFORMAT_YV12 &&
  1657. texture->format != SDL_PIXELFORMAT_IYUV) {
  1658. return SDL_SetError("Texture format must by YV12 or IYUV");
  1659. }
  1660. real_rect.x = 0;
  1661. real_rect.y = 0;
  1662. real_rect.w = texture->w;
  1663. real_rect.h = texture->h;
  1664. if (rect) {
  1665. SDL_GetRectIntersection(rect, &real_rect, &real_rect);
  1666. }
  1667. if (real_rect.w == 0 || real_rect.h == 0) {
  1668. return 0; /* nothing to do. */
  1669. }
  1670. if (texture->yuv) {
  1671. return SDL_UpdateTextureYUVPlanar(texture, &real_rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
  1672. } else {
  1673. SDL_assert(!texture->native);
  1674. renderer = texture->renderer;
  1675. SDL_assert(renderer->UpdateTextureYUV);
  1676. if (renderer->UpdateTextureYUV) {
  1677. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1678. return -1;
  1679. }
  1680. return renderer->UpdateTextureYUV(renderer, texture, &real_rect, Yplane, Ypitch, Uplane, Upitch, Vplane, Vpitch);
  1681. } else {
  1682. return SDL_Unsupported();
  1683. }
  1684. }
  1685. #else
  1686. return -1;
  1687. #endif
  1688. }
  1689. int SDL_UpdateNVTexture(SDL_Texture *texture, const SDL_Rect *rect,
  1690. const Uint8 *Yplane, int Ypitch,
  1691. const Uint8 *UVplane, int UVpitch)
  1692. {
  1693. #if SDL_HAVE_YUV
  1694. SDL_Renderer *renderer;
  1695. SDL_Rect real_rect;
  1696. CHECK_TEXTURE_MAGIC(texture, -1);
  1697. if (Yplane == NULL) {
  1698. return SDL_InvalidParamError("Yplane");
  1699. }
  1700. if (!Ypitch) {
  1701. return SDL_InvalidParamError("Ypitch");
  1702. }
  1703. if (UVplane == NULL) {
  1704. return SDL_InvalidParamError("UVplane");
  1705. }
  1706. if (!UVpitch) {
  1707. return SDL_InvalidParamError("UVpitch");
  1708. }
  1709. if (texture->format != SDL_PIXELFORMAT_NV12 &&
  1710. texture->format != SDL_PIXELFORMAT_NV21) {
  1711. return SDL_SetError("Texture format must by NV12 or NV21");
  1712. }
  1713. real_rect.x = 0;
  1714. real_rect.y = 0;
  1715. real_rect.w = texture->w;
  1716. real_rect.h = texture->h;
  1717. if (rect) {
  1718. SDL_GetRectIntersection(rect, &real_rect, &real_rect);
  1719. }
  1720. if (real_rect.w == 0 || real_rect.h == 0) {
  1721. return 0; /* nothing to do. */
  1722. }
  1723. if (texture->yuv) {
  1724. return SDL_UpdateTextureNVPlanar(texture, &real_rect, Yplane, Ypitch, UVplane, UVpitch);
  1725. } else {
  1726. SDL_assert(!texture->native);
  1727. renderer = texture->renderer;
  1728. SDL_assert(renderer->UpdateTextureNV);
  1729. if (renderer->UpdateTextureNV) {
  1730. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1731. return -1;
  1732. }
  1733. return renderer->UpdateTextureNV(renderer, texture, &real_rect, Yplane, Ypitch, UVplane, UVpitch);
  1734. } else {
  1735. return SDL_Unsupported();
  1736. }
  1737. }
  1738. #else
  1739. return -1;
  1740. #endif
  1741. }
  1742. #if SDL_HAVE_YUV
  1743. static int SDL_LockTextureYUV(SDL_Texture *texture, const SDL_Rect *rect,
  1744. void **pixels, int *pitch)
  1745. {
  1746. return SDL_SW_LockYUVTexture(texture->yuv, rect, pixels, pitch);
  1747. }
  1748. #endif /* SDL_HAVE_YUV */
  1749. static int SDL_LockTextureNative(SDL_Texture *texture, const SDL_Rect *rect,
  1750. void **pixels, int *pitch)
  1751. {
  1752. texture->locked_rect = *rect;
  1753. *pixels = (void *)((Uint8 *)texture->pixels +
  1754. rect->y * texture->pitch +
  1755. rect->x * SDL_BYTESPERPIXEL(texture->format));
  1756. *pitch = texture->pitch;
  1757. return 0;
  1758. }
  1759. int SDL_LockTexture(SDL_Texture *texture, const SDL_Rect *rect,
  1760. void **pixels, int *pitch)
  1761. {
  1762. SDL_Rect full_rect;
  1763. CHECK_TEXTURE_MAGIC(texture, -1);
  1764. if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1765. return SDL_SetError("SDL_LockTexture(): texture must be streaming");
  1766. }
  1767. if (rect == NULL) {
  1768. full_rect.x = 0;
  1769. full_rect.y = 0;
  1770. full_rect.w = texture->w;
  1771. full_rect.h = texture->h;
  1772. rect = &full_rect;
  1773. }
  1774. #if SDL_HAVE_YUV
  1775. if (texture->yuv) {
  1776. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1777. return -1;
  1778. }
  1779. return SDL_LockTextureYUV(texture, rect, pixels, pitch);
  1780. } else
  1781. #endif
  1782. if (texture->native) {
  1783. /* Calls a real SDL_LockTexture/SDL_UnlockTexture on unlock, flushing then. */
  1784. return SDL_LockTextureNative(texture, rect, pixels, pitch);
  1785. } else {
  1786. SDL_Renderer *renderer = texture->renderer;
  1787. if (FlushRenderCommandsIfTextureNeeded(texture) < 0) {
  1788. return -1;
  1789. }
  1790. return renderer->LockTexture(renderer, texture, rect, pixels, pitch);
  1791. }
  1792. }
  1793. int SDL_LockTextureToSurface(SDL_Texture *texture, const SDL_Rect *rect,
  1794. SDL_Surface **surface)
  1795. {
  1796. SDL_Rect real_rect;
  1797. void *pixels = NULL;
  1798. int pitch = 0; /* fix static analysis */
  1799. int ret;
  1800. if (texture == NULL || surface == NULL) {
  1801. return -1;
  1802. }
  1803. real_rect.x = 0;
  1804. real_rect.y = 0;
  1805. real_rect.w = texture->w;
  1806. real_rect.h = texture->h;
  1807. if (rect) {
  1808. SDL_GetRectIntersection(rect, &real_rect, &real_rect);
  1809. }
  1810. ret = SDL_LockTexture(texture, &real_rect, &pixels, &pitch);
  1811. if (ret < 0) {
  1812. return ret;
  1813. }
  1814. texture->locked_surface = SDL_CreateSurfaceFrom(pixels, real_rect.w, real_rect.h, pitch, texture->format);
  1815. if (texture->locked_surface == NULL) {
  1816. SDL_UnlockTexture(texture);
  1817. return -1;
  1818. }
  1819. *surface = texture->locked_surface;
  1820. return 0;
  1821. }
  1822. #if SDL_HAVE_YUV
  1823. static void SDL_UnlockTextureYUV(SDL_Texture *texture)
  1824. {
  1825. SDL_Texture *native = texture->native;
  1826. void *native_pixels = NULL;
  1827. int native_pitch = 0;
  1828. SDL_Rect rect;
  1829. rect.x = 0;
  1830. rect.y = 0;
  1831. rect.w = texture->w;
  1832. rect.h = texture->h;
  1833. if (SDL_LockTexture(native, &rect, &native_pixels, &native_pitch) < 0) {
  1834. return;
  1835. }
  1836. SDL_SW_CopyYUVToRGB(texture->yuv, &rect, native->format,
  1837. rect.w, rect.h, native_pixels, native_pitch);
  1838. SDL_UnlockTexture(native);
  1839. }
  1840. #endif /* SDL_HAVE_YUV */
  1841. static void SDL_UnlockTextureNative(SDL_Texture *texture)
  1842. {
  1843. SDL_Texture *native = texture->native;
  1844. void *native_pixels = NULL;
  1845. int native_pitch = 0;
  1846. const SDL_Rect *rect = &texture->locked_rect;
  1847. const void *pixels = (void *)((Uint8 *)texture->pixels +
  1848. rect->y * texture->pitch +
  1849. rect->x * SDL_BYTESPERPIXEL(texture->format));
  1850. int pitch = texture->pitch;
  1851. if (SDL_LockTexture(native, rect, &native_pixels, &native_pitch) < 0) {
  1852. return;
  1853. }
  1854. SDL_ConvertPixels(rect->w, rect->h,
  1855. texture->format, pixels, pitch,
  1856. native->format, native_pixels, native_pitch);
  1857. SDL_UnlockTexture(native);
  1858. }
  1859. void SDL_UnlockTexture(SDL_Texture *texture)
  1860. {
  1861. CHECK_TEXTURE_MAGIC(texture, );
  1862. if (texture->access != SDL_TEXTUREACCESS_STREAMING) {
  1863. return;
  1864. }
  1865. #if SDL_HAVE_YUV
  1866. if (texture->yuv) {
  1867. SDL_UnlockTextureYUV(texture);
  1868. } else
  1869. #endif
  1870. if (texture->native) {
  1871. SDL_UnlockTextureNative(texture);
  1872. } else {
  1873. SDL_Renderer *renderer = texture->renderer;
  1874. renderer->UnlockTexture(renderer, texture);
  1875. }
  1876. SDL_DestroySurface(texture->locked_surface);
  1877. texture->locked_surface = NULL;
  1878. }
  1879. SDL_bool
  1880. SDL_RenderTargetSupported(SDL_Renderer *renderer)
  1881. {
  1882. if (renderer == NULL || !renderer->SetRenderTarget) {
  1883. return SDL_FALSE;
  1884. }
  1885. return (renderer->info.flags & SDL_RENDERER_TARGETTEXTURE) != 0;
  1886. }
  1887. int SDL_SetRenderTarget(SDL_Renderer *renderer, SDL_Texture *texture)
  1888. {
  1889. if (!SDL_RenderTargetSupported(renderer)) {
  1890. return SDL_Unsupported();
  1891. }
  1892. /* texture == NULL is valid and means reset the target to the window */
  1893. if (texture) {
  1894. CHECK_TEXTURE_MAGIC(texture, -1);
  1895. if (renderer != texture->renderer) {
  1896. return SDL_SetError("Texture was not created with this renderer");
  1897. }
  1898. if (texture->access != SDL_TEXTUREACCESS_TARGET) {
  1899. return SDL_SetError("Texture not created with SDL_TEXTUREACCESS_TARGET");
  1900. }
  1901. if (texture->native) {
  1902. /* Always render to the native texture */
  1903. texture = texture->native;
  1904. }
  1905. }
  1906. if (texture == renderer->target) {
  1907. /* Nothing to do! */
  1908. return 0;
  1909. }
  1910. FlushRenderCommands(renderer); /* time to send everything to the GPU! */
  1911. SDL_LockMutex(renderer->target_mutex);
  1912. if (texture && !renderer->target) {
  1913. /* Make a backup of the viewport */
  1914. renderer->viewport_backup = renderer->viewport;
  1915. renderer->clip_rect_backup = renderer->clip_rect;
  1916. renderer->clipping_enabled_backup = renderer->clipping_enabled;
  1917. renderer->scale_backup = renderer->scale;
  1918. renderer->logical_w_backup = renderer->logical_w;
  1919. renderer->logical_h_backup = renderer->logical_h;
  1920. }
  1921. renderer->target = texture;
  1922. if (renderer->SetRenderTarget(renderer, texture) < 0) {
  1923. SDL_UnlockMutex(renderer->target_mutex);
  1924. return -1;
  1925. }
  1926. if (texture) {
  1927. renderer->viewport.x = (double)0;
  1928. renderer->viewport.y = (double)0;
  1929. renderer->viewport.w = (double)texture->w;
  1930. renderer->viewport.h = (double)texture->h;
  1931. SDL_zero(renderer->clip_rect);
  1932. renderer->clipping_enabled = SDL_FALSE;
  1933. renderer->scale.x = 1.0f;
  1934. renderer->scale.y = 1.0f;
  1935. renderer->logical_w = texture->w;
  1936. renderer->logical_h = texture->h;
  1937. } else {
  1938. renderer->viewport = renderer->viewport_backup;
  1939. renderer->clip_rect = renderer->clip_rect_backup;
  1940. renderer->clipping_enabled = renderer->clipping_enabled_backup;
  1941. renderer->scale = renderer->scale_backup;
  1942. renderer->logical_w = renderer->logical_w_backup;
  1943. renderer->logical_h = renderer->logical_h_backup;
  1944. }
  1945. SDL_UnlockMutex(renderer->target_mutex);
  1946. if (QueueCmdSetViewport(renderer) < 0) {
  1947. return -1;
  1948. }
  1949. if (QueueCmdSetClipRect(renderer) < 0) {
  1950. return -1;
  1951. }
  1952. /* All set! */
  1953. return FlushRenderCommandsIfNotBatching(renderer);
  1954. }
  1955. SDL_Texture *
  1956. SDL_GetRenderTarget(SDL_Renderer *renderer)
  1957. {
  1958. CHECK_RENDERER_MAGIC(renderer, NULL);
  1959. return renderer->target;
  1960. }
  1961. static int UpdateLogicalSize(SDL_Renderer *renderer, SDL_bool flush_viewport_cmd)
  1962. {
  1963. int w = 1, h = 1;
  1964. float want_aspect;
  1965. float real_aspect;
  1966. float scale;
  1967. SDL_Rect viewport;
  1968. /* 0 is for letterbox, 1 is for overscan */
  1969. int scale_policy = 0;
  1970. const char *hint;
  1971. if (!renderer->logical_w || !renderer->logical_h) {
  1972. return 0;
  1973. }
  1974. if (SDL_GetRendererOutputSize(renderer, &w, &h) < 0) {
  1975. return -1;
  1976. }
  1977. hint = SDL_GetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE);
  1978. if (hint && (*hint == '1' || SDL_strcasecmp(hint, "overscan") == 0)) {
  1979. #if SDL_VIDEO_RENDER_D3D
  1980. SDL_bool overscan_supported = SDL_TRUE;
  1981. /* Unfortunately, Direct3D 9 doesn't support negative viewport numbers
  1982. which the overscan implementation relies on.
  1983. */
  1984. if (SDL_strcasecmp(SDL_GetCurrentVideoDriver(), "direct3d") == 0) {
  1985. overscan_supported = SDL_FALSE;
  1986. }
  1987. if (overscan_supported) {
  1988. scale_policy = 1;
  1989. }
  1990. #else
  1991. scale_policy = 1;
  1992. #endif
  1993. }
  1994. want_aspect = (float)renderer->logical_w / renderer->logical_h;
  1995. real_aspect = (float)w / h;
  1996. /* Clear the scale because we're setting viewport in output coordinates */
  1997. SDL_SetRenderScale(renderer, 1.0f, 1.0f);
  1998. if (renderer->integer_scale) {
  1999. if (want_aspect > real_aspect) {
  2000. scale = (float)(w / renderer->logical_w); /* This an integer division! */
  2001. } else {
  2002. scale = (float)(h / renderer->logical_h); /* This an integer division! */
  2003. }
  2004. if (scale < 1.0f) {
  2005. scale = 1.0f;
  2006. }
  2007. viewport.w = (int)SDL_floor(renderer->logical_w * scale);
  2008. viewport.x = (w - viewport.w) / 2;
  2009. viewport.h = (int)SDL_floor(renderer->logical_h * scale);
  2010. viewport.y = (h - viewport.h) / 2;
  2011. } else if (SDL_fabs(want_aspect - real_aspect) < 0.0001) {
  2012. /* The aspect ratios are the same, just scale appropriately */
  2013. scale = (float)w / renderer->logical_w;
  2014. SDL_zero(viewport);
  2015. SDL_GetRendererOutputSize(renderer, &viewport.w, &viewport.h);
  2016. } else if (want_aspect > real_aspect) {
  2017. if (scale_policy == 1) {
  2018. /* We want a wider aspect ratio than is available -
  2019. zoom so logical height matches the real height
  2020. and the width will grow off the screen
  2021. */
  2022. scale = (float)h / renderer->logical_h;
  2023. viewport.y = 0;
  2024. viewport.h = h;
  2025. viewport.w = (int)SDL_floor(renderer->logical_w * scale);
  2026. viewport.x = (w - viewport.w) / 2;
  2027. } else {
  2028. /* We want a wider aspect ratio than is available - letterbox it */
  2029. scale = (float)w / renderer->logical_w;
  2030. viewport.x = 0;
  2031. viewport.w = w;
  2032. viewport.h = (int)SDL_floor(renderer->logical_h * scale);
  2033. viewport.y = (h - viewport.h) / 2;
  2034. }
  2035. } else {
  2036. if (scale_policy == 1) {
  2037. /* We want a narrower aspect ratio than is available -
  2038. zoom so logical width matches the real width
  2039. and the height will grow off the screen
  2040. */
  2041. scale = (float)w / renderer->logical_w;
  2042. viewport.x = 0;
  2043. viewport.w = w;
  2044. viewport.h = (int)SDL_floor(renderer->logical_h * scale);
  2045. viewport.y = (h - viewport.h) / 2;
  2046. } else {
  2047. /* We want a narrower aspect ratio than is available - use side-bars */
  2048. scale = (float)h / renderer->logical_h;
  2049. viewport.y = 0;
  2050. viewport.h = h;
  2051. viewport.w = (int)SDL_floor(renderer->logical_w * scale);
  2052. viewport.x = (w - viewport.w) / 2;
  2053. }
  2054. }
  2055. /* Set the new viewport */
  2056. renderer->viewport.x = (double)viewport.x * renderer->scale.x;
  2057. renderer->viewport.y = (double)viewport.y * renderer->scale.y;
  2058. renderer->viewport.w = (double)viewport.w * renderer->scale.x;
  2059. renderer->viewport.h = (double)viewport.h * renderer->scale.y;
  2060. QueueCmdSetViewport(renderer);
  2061. if (flush_viewport_cmd) {
  2062. FlushRenderCommandsIfNotBatching(renderer);
  2063. }
  2064. /* Set the new scale */
  2065. SDL_SetRenderScale(renderer, scale, scale);
  2066. return 0;
  2067. }
  2068. int SDL_SetRenderLogicalSize(SDL_Renderer *renderer, int w, int h)
  2069. {
  2070. CHECK_RENDERER_MAGIC(renderer, -1);
  2071. if (!w || !h) {
  2072. /* Clear any previous logical resolution */
  2073. renderer->logical_w = 0;
  2074. renderer->logical_h = 0;
  2075. SDL_SetRenderViewport(renderer, NULL);
  2076. SDL_SetRenderScale(renderer, 1.0f, 1.0f);
  2077. return 0;
  2078. }
  2079. renderer->logical_w = w;
  2080. renderer->logical_h = h;
  2081. return UpdateLogicalSize(renderer, SDL_TRUE);
  2082. }
  2083. void SDL_GetRenderLogicalSize(SDL_Renderer *renderer, int *w, int *h)
  2084. {
  2085. CHECK_RENDERER_MAGIC(renderer, );
  2086. if (w) {
  2087. *w = renderer->logical_w;
  2088. }
  2089. if (h) {
  2090. *h = renderer->logical_h;
  2091. }
  2092. }
  2093. int SDL_SetRenderIntegerScale(SDL_Renderer *renderer, SDL_bool enable)
  2094. {
  2095. CHECK_RENDERER_MAGIC(renderer, -1);
  2096. renderer->integer_scale = enable;
  2097. return UpdateLogicalSize(renderer, SDL_TRUE);
  2098. }
  2099. SDL_bool
  2100. SDLCALL
  2101. SDL_GetRenderIntegerScale(SDL_Renderer *renderer)
  2102. {
  2103. CHECK_RENDERER_MAGIC(renderer, SDL_FALSE);
  2104. return renderer->integer_scale;
  2105. }
  2106. int SDL_SetRenderViewport(SDL_Renderer *renderer, const SDL_Rect *rect)
  2107. {
  2108. int retval;
  2109. CHECK_RENDERER_MAGIC(renderer, -1);
  2110. if (rect) {
  2111. renderer->viewport.x = (double)rect->x * renderer->scale.x;
  2112. renderer->viewport.y = (double)rect->y * renderer->scale.y;
  2113. renderer->viewport.w = (double)rect->w * renderer->scale.x;
  2114. renderer->viewport.h = (double)rect->h * renderer->scale.y;
  2115. } else {
  2116. int w, h;
  2117. if (SDL_GetRendererOutputSize(renderer, &w, &h) < 0) {
  2118. return -1;
  2119. }
  2120. renderer->viewport.x = 0.0;
  2121. renderer->viewport.y = 0.0;
  2122. /* NOLINTBEGIN(clang-analyzer-core.uninitialized.Assign): SDL_GetRendererOutputSize cannot fail */
  2123. renderer->viewport.w = (double)w;
  2124. renderer->viewport.h = (double)h;
  2125. /* NOLINTEND(clang-analyzer-core.uninitialized.Assign) */
  2126. }
  2127. retval = QueueCmdSetViewport(renderer);
  2128. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2129. }
  2130. void SDL_GetRenderViewport(SDL_Renderer *renderer, SDL_Rect *rect)
  2131. {
  2132. CHECK_RENDERER_MAGIC(renderer, );
  2133. if (rect) {
  2134. rect->x = (int)SDL_floor(renderer->viewport.x / renderer->scale.x);
  2135. rect->y = (int)SDL_floor(renderer->viewport.y / renderer->scale.y);
  2136. rect->w = (int)SDL_floor(renderer->viewport.w / renderer->scale.x);
  2137. rect->h = (int)SDL_floor(renderer->viewport.h / renderer->scale.y);
  2138. }
  2139. }
  2140. static void GetRenderViewportSize(SDL_Renderer *renderer, SDL_FRect *rect)
  2141. {
  2142. rect->x = 0.0f;
  2143. rect->y = 0.0f;
  2144. rect->w = (float)(renderer->viewport.w / renderer->scale.x);
  2145. rect->h = (float)(renderer->viewport.h / renderer->scale.y);
  2146. }
  2147. int SDL_SetRenderClipRect(SDL_Renderer *renderer, const SDL_Rect *rect)
  2148. {
  2149. int retval;
  2150. CHECK_RENDERER_MAGIC(renderer, -1)
  2151. if (rect && rect->w > 0 && rect->h > 0) {
  2152. renderer->clipping_enabled = SDL_TRUE;
  2153. renderer->clip_rect.x = (double)rect->x * renderer->scale.x;
  2154. renderer->clip_rect.y = (double)rect->y * renderer->scale.y;
  2155. renderer->clip_rect.w = (double)rect->w * renderer->scale.x;
  2156. renderer->clip_rect.h = (double)rect->h * renderer->scale.y;
  2157. } else {
  2158. renderer->clipping_enabled = SDL_FALSE;
  2159. SDL_zero(renderer->clip_rect);
  2160. }
  2161. retval = QueueCmdSetClipRect(renderer);
  2162. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2163. }
  2164. void SDL_GetRenderClipRect(SDL_Renderer *renderer, SDL_Rect *rect)
  2165. {
  2166. CHECK_RENDERER_MAGIC(renderer, )
  2167. if (rect) {
  2168. rect->x = (int)SDL_floor(renderer->clip_rect.x / renderer->scale.x);
  2169. rect->y = (int)SDL_floor(renderer->clip_rect.y / renderer->scale.y);
  2170. rect->w = (int)SDL_floor(renderer->clip_rect.w / renderer->scale.x);
  2171. rect->h = (int)SDL_floor(renderer->clip_rect.h / renderer->scale.y);
  2172. }
  2173. }
  2174. SDL_bool
  2175. SDL_RenderClipEnabled(SDL_Renderer *renderer)
  2176. {
  2177. CHECK_RENDERER_MAGIC(renderer, SDL_FALSE)
  2178. return renderer->clipping_enabled;
  2179. }
  2180. int SDL_SetRenderScale(SDL_Renderer *renderer, float scaleX, float scaleY)
  2181. {
  2182. CHECK_RENDERER_MAGIC(renderer, -1);
  2183. renderer->scale.x = scaleX;
  2184. renderer->scale.y = scaleY;
  2185. return 0;
  2186. }
  2187. void SDL_GetRenderScale(SDL_Renderer *renderer, float *scaleX, float *scaleY)
  2188. {
  2189. CHECK_RENDERER_MAGIC(renderer, );
  2190. if (scaleX) {
  2191. *scaleX = renderer->scale.x;
  2192. }
  2193. if (scaleY) {
  2194. *scaleY = renderer->scale.y;
  2195. }
  2196. }
  2197. void SDL_RenderWindowToLogical(SDL_Renderer *renderer, int windowX, int windowY, float *logicalX, float *logicalY)
  2198. {
  2199. float window_physical_x, window_physical_y;
  2200. CHECK_RENDERER_MAGIC(renderer, );
  2201. window_physical_x = ((float)windowX) / renderer->dpi_scale.x;
  2202. window_physical_y = ((float)windowY) / renderer->dpi_scale.y;
  2203. if (logicalX) {
  2204. *logicalX = (float)((window_physical_x - renderer->viewport.x) / renderer->scale.x);
  2205. }
  2206. if (logicalY) {
  2207. *logicalY = (float)((window_physical_y - renderer->viewport.y) / renderer->scale.y);
  2208. }
  2209. }
  2210. void SDL_RenderLogicalToWindow(SDL_Renderer *renderer, float logicalX, float logicalY, int *windowX, int *windowY)
  2211. {
  2212. float window_physical_x, window_physical_y;
  2213. CHECK_RENDERER_MAGIC(renderer, );
  2214. window_physical_x = (float)((logicalX * renderer->scale.x) + renderer->viewport.x);
  2215. window_physical_y = (float)((logicalY * renderer->scale.y) + renderer->viewport.y);
  2216. if (windowX) {
  2217. *windowX = (int)(window_physical_x * renderer->dpi_scale.x);
  2218. }
  2219. if (windowY) {
  2220. *windowY = (int)(window_physical_y * renderer->dpi_scale.y);
  2221. }
  2222. }
  2223. int SDL_SetRenderDrawColor(SDL_Renderer *renderer,
  2224. Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2225. {
  2226. CHECK_RENDERER_MAGIC(renderer, -1);
  2227. renderer->color.r = r;
  2228. renderer->color.g = g;
  2229. renderer->color.b = b;
  2230. renderer->color.a = a;
  2231. return 0;
  2232. }
  2233. int SDL_GetRenderDrawColor(SDL_Renderer *renderer,
  2234. Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
  2235. {
  2236. CHECK_RENDERER_MAGIC(renderer, -1);
  2237. if (r) {
  2238. *r = renderer->color.r;
  2239. }
  2240. if (g) {
  2241. *g = renderer->color.g;
  2242. }
  2243. if (b) {
  2244. *b = renderer->color.b;
  2245. }
  2246. if (a) {
  2247. *a = renderer->color.a;
  2248. }
  2249. return 0;
  2250. }
  2251. int SDL_SetRenderDrawBlendMode(SDL_Renderer *renderer, SDL_BlendMode blendMode)
  2252. {
  2253. CHECK_RENDERER_MAGIC(renderer, -1);
  2254. if (!IsSupportedBlendMode(renderer, blendMode)) {
  2255. return SDL_Unsupported();
  2256. }
  2257. renderer->blendMode = blendMode;
  2258. return 0;
  2259. }
  2260. int SDL_GetRenderDrawBlendMode(SDL_Renderer *renderer, SDL_BlendMode *blendMode)
  2261. {
  2262. CHECK_RENDERER_MAGIC(renderer, -1);
  2263. *blendMode = renderer->blendMode;
  2264. return 0;
  2265. }
  2266. int SDL_RenderClear(SDL_Renderer *renderer)
  2267. {
  2268. int retval;
  2269. CHECK_RENDERER_MAGIC(renderer, -1);
  2270. retval = QueueCmdClear(renderer);
  2271. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2272. }
  2273. /* !!! FIXME: delete all the duplicate code for the integer versions in 2.1,
  2274. !!! FIXME: making the floating point versions the only available APIs. */
  2275. int SDL_RenderPoint(SDL_Renderer *renderer, int x, int y)
  2276. {
  2277. SDL_FPoint fpoint;
  2278. fpoint.x = (float)x;
  2279. fpoint.y = (float)y;
  2280. return SDL_RenderPointsF(renderer, &fpoint, 1);
  2281. }
  2282. int SDL_RenderPointF(SDL_Renderer *renderer, float x, float y)
  2283. {
  2284. SDL_FPoint fpoint;
  2285. fpoint.x = x;
  2286. fpoint.y = y;
  2287. return SDL_RenderPointsF(renderer, &fpoint, 1);
  2288. }
  2289. static int RenderPointsWithRects(SDL_Renderer *renderer,
  2290. const SDL_Point *points, const int count)
  2291. {
  2292. int retval;
  2293. SDL_bool isstack;
  2294. SDL_FRect *frects;
  2295. int i;
  2296. if (count < 1) {
  2297. return 0;
  2298. }
  2299. frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  2300. if (frects == NULL) {
  2301. return SDL_OutOfMemory();
  2302. }
  2303. for (i = 0; i < count; ++i) {
  2304. frects[i].x = points[i].x * renderer->scale.x;
  2305. frects[i].y = points[i].y * renderer->scale.y;
  2306. frects[i].w = renderer->scale.x;
  2307. frects[i].h = renderer->scale.y;
  2308. }
  2309. retval = QueueCmdFillRects(renderer, frects, count);
  2310. SDL_small_free(frects, isstack);
  2311. return retval;
  2312. }
  2313. int SDL_RenderPoints(SDL_Renderer *renderer,
  2314. const SDL_Point *points, int count)
  2315. {
  2316. SDL_FPoint *fpoints;
  2317. int i;
  2318. int retval;
  2319. SDL_bool isstack;
  2320. CHECK_RENDERER_MAGIC(renderer, -1);
  2321. if (points == NULL) {
  2322. return SDL_InvalidParamError("SDL_RenderPoints(): points");
  2323. }
  2324. if (count < 1) {
  2325. return 0;
  2326. }
  2327. #if DONT_DRAW_WHILE_HIDDEN
  2328. /* Don't draw while we're hidden */
  2329. if (renderer->hidden) {
  2330. return 0;
  2331. }
  2332. #endif
  2333. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  2334. retval = RenderPointsWithRects(renderer, points, count);
  2335. } else {
  2336. fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
  2337. if (fpoints == NULL) {
  2338. return SDL_OutOfMemory();
  2339. }
  2340. for (i = 0; i < count; ++i) {
  2341. fpoints[i].x = (float)points[i].x;
  2342. fpoints[i].y = (float)points[i].y;
  2343. }
  2344. retval = QueueCmdDrawPoints(renderer, fpoints, count);
  2345. SDL_small_free(fpoints, isstack);
  2346. }
  2347. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2348. }
  2349. static int RenderPointsWithRectsF(SDL_Renderer *renderer,
  2350. const SDL_FPoint *fpoints, const int count)
  2351. {
  2352. int retval;
  2353. SDL_bool isstack;
  2354. SDL_FRect *frects;
  2355. int i;
  2356. if (count < 1) {
  2357. return 0;
  2358. }
  2359. frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  2360. if (frects == NULL) {
  2361. return SDL_OutOfMemory();
  2362. }
  2363. for (i = 0; i < count; ++i) {
  2364. frects[i].x = fpoints[i].x * renderer->scale.x;
  2365. frects[i].y = fpoints[i].y * renderer->scale.y;
  2366. frects[i].w = renderer->scale.x;
  2367. frects[i].h = renderer->scale.y;
  2368. }
  2369. retval = QueueCmdFillRects(renderer, frects, count);
  2370. SDL_small_free(frects, isstack);
  2371. return retval;
  2372. }
  2373. int SDL_RenderPointsF(SDL_Renderer *renderer,
  2374. const SDL_FPoint *points, int count)
  2375. {
  2376. int retval;
  2377. CHECK_RENDERER_MAGIC(renderer, -1);
  2378. if (points == NULL) {
  2379. return SDL_InvalidParamError("SDL_RenderPointsF(): points");
  2380. }
  2381. if (count < 1) {
  2382. return 0;
  2383. }
  2384. #if DONT_DRAW_WHILE_HIDDEN
  2385. /* Don't draw while we're hidden */
  2386. if (renderer->hidden) {
  2387. return 0;
  2388. }
  2389. #endif
  2390. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  2391. retval = RenderPointsWithRectsF(renderer, points, count);
  2392. } else {
  2393. retval = QueueCmdDrawPoints(renderer, points, count);
  2394. }
  2395. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2396. }
  2397. int SDL_RenderLine(SDL_Renderer *renderer, int x1, int y1, int x2, int y2)
  2398. {
  2399. SDL_FPoint points[2];
  2400. points[0].x = (float)x1;
  2401. points[0].y = (float)y1;
  2402. points[1].x = (float)x2;
  2403. points[1].y = (float)y2;
  2404. return SDL_RenderLinesF(renderer, points, 2);
  2405. }
  2406. int SDL_RenderLineF(SDL_Renderer *renderer, float x1, float y1, float x2, float y2)
  2407. {
  2408. SDL_FPoint points[2];
  2409. points[0].x = x1;
  2410. points[0].y = y1;
  2411. points[1].x = x2;
  2412. points[1].y = y2;
  2413. return SDL_RenderLinesF(renderer, points, 2);
  2414. }
  2415. static int RenderLineBresenham(SDL_Renderer *renderer, int x1, int y1, int x2, int y2, SDL_bool draw_last)
  2416. {
  2417. int i, deltax, deltay, numpixels;
  2418. int d, dinc1, dinc2;
  2419. int x, xinc1, xinc2;
  2420. int y, yinc1, yinc2;
  2421. int retval;
  2422. SDL_bool isstack;
  2423. SDL_FPoint *points;
  2424. deltax = SDL_abs(x2 - x1);
  2425. deltay = SDL_abs(y2 - y1);
  2426. if (deltax >= deltay) {
  2427. numpixels = deltax + 1;
  2428. d = (2 * deltay) - deltax;
  2429. dinc1 = deltay * 2;
  2430. dinc2 = (deltay - deltax) * 2;
  2431. xinc1 = 1;
  2432. xinc2 = 1;
  2433. yinc1 = 0;
  2434. yinc2 = 1;
  2435. } else {
  2436. numpixels = deltay + 1;
  2437. d = (2 * deltax) - deltay;
  2438. dinc1 = deltax * 2;
  2439. dinc2 = (deltax - deltay) * 2;
  2440. xinc1 = 0;
  2441. xinc2 = 1;
  2442. yinc1 = 1;
  2443. yinc2 = 1;
  2444. }
  2445. if (x1 > x2) {
  2446. xinc1 = -xinc1;
  2447. xinc2 = -xinc2;
  2448. }
  2449. if (y1 > y2) {
  2450. yinc1 = -yinc1;
  2451. yinc2 = -yinc2;
  2452. }
  2453. x = x1;
  2454. y = y1;
  2455. if (!draw_last) {
  2456. --numpixels;
  2457. }
  2458. points = SDL_small_alloc(SDL_FPoint, numpixels, &isstack);
  2459. if (points == NULL) {
  2460. return SDL_OutOfMemory();
  2461. }
  2462. for (i = 0; i < numpixels; ++i) {
  2463. points[i].x = (float)x;
  2464. points[i].y = (float)y;
  2465. if (d < 0) {
  2466. d += dinc1;
  2467. x += xinc1;
  2468. y += yinc1;
  2469. } else {
  2470. d += dinc2;
  2471. x += xinc2;
  2472. y += yinc2;
  2473. }
  2474. }
  2475. if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  2476. retval = RenderPointsWithRectsF(renderer, points, numpixels);
  2477. } else {
  2478. retval = QueueCmdDrawPoints(renderer, points, numpixels);
  2479. }
  2480. SDL_small_free(points, isstack);
  2481. return retval;
  2482. }
  2483. static int RenderLinesWithRectsF(SDL_Renderer *renderer,
  2484. const SDL_FPoint *points, const int count)
  2485. {
  2486. const float scale_x = renderer->scale.x;
  2487. const float scale_y = renderer->scale.y;
  2488. SDL_FRect *frect;
  2489. SDL_FRect *frects;
  2490. int i, nrects = 0;
  2491. int retval = 0;
  2492. SDL_bool isstack;
  2493. SDL_bool drew_line = SDL_FALSE;
  2494. SDL_bool draw_last = SDL_FALSE;
  2495. frects = SDL_small_alloc(SDL_FRect, count - 1, &isstack);
  2496. if (frects == NULL) {
  2497. return SDL_OutOfMemory();
  2498. }
  2499. for (i = 0; i < count - 1; ++i) {
  2500. SDL_bool same_x = (points[i].x == points[i + 1].x);
  2501. SDL_bool same_y = (points[i].y == points[i + 1].y);
  2502. if (i == (count - 2)) {
  2503. if (!drew_line || points[i + 1].x != points[0].x || points[i + 1].y != points[0].y) {
  2504. draw_last = SDL_TRUE;
  2505. }
  2506. } else {
  2507. if (same_x && same_y) {
  2508. continue;
  2509. }
  2510. }
  2511. if (same_x) {
  2512. const float minY = SDL_min(points[i].y, points[i + 1].y);
  2513. const float maxY = SDL_max(points[i].y, points[i + 1].y);
  2514. frect = &frects[nrects++];
  2515. frect->x = points[i].x * scale_x;
  2516. frect->y = minY * scale_y;
  2517. frect->w = scale_x;
  2518. frect->h = (maxY - minY + draw_last) * scale_y;
  2519. if (!draw_last && points[i + 1].y < points[i].y) {
  2520. frect->y += scale_y;
  2521. }
  2522. } else if (same_y) {
  2523. const float minX = SDL_min(points[i].x, points[i + 1].x);
  2524. const float maxX = SDL_max(points[i].x, points[i + 1].x);
  2525. frect = &frects[nrects++];
  2526. frect->x = minX * scale_x;
  2527. frect->y = points[i].y * scale_y;
  2528. frect->w = (maxX - minX + draw_last) * scale_x;
  2529. frect->h = scale_y;
  2530. if (!draw_last && points[i + 1].x < points[i].x) {
  2531. frect->x += scale_x;
  2532. }
  2533. } else {
  2534. retval += RenderLineBresenham(renderer, (int)SDL_roundf(points[i].x), (int)SDL_roundf(points[i].y),
  2535. (int)SDL_roundf(points[i + 1].x), (int)SDL_roundf(points[i + 1].y), draw_last);
  2536. }
  2537. drew_line = SDL_TRUE;
  2538. }
  2539. if (nrects) {
  2540. retval += QueueCmdFillRects(renderer, frects, nrects);
  2541. }
  2542. SDL_small_free(frects, isstack);
  2543. if (retval < 0) {
  2544. retval = -1;
  2545. }
  2546. return retval;
  2547. }
  2548. int SDL_RenderLines(SDL_Renderer *renderer,
  2549. const SDL_Point *points, int count)
  2550. {
  2551. SDL_FPoint *fpoints;
  2552. int i;
  2553. int retval;
  2554. SDL_bool isstack;
  2555. CHECK_RENDERER_MAGIC(renderer, -1);
  2556. if (points == NULL) {
  2557. return SDL_InvalidParamError("SDL_RenderLines(): points");
  2558. }
  2559. if (count < 2) {
  2560. return 0;
  2561. }
  2562. #if DONT_DRAW_WHILE_HIDDEN
  2563. /* Don't draw while we're hidden */
  2564. if (renderer->hidden) {
  2565. return 0;
  2566. }
  2567. #endif
  2568. fpoints = SDL_small_alloc(SDL_FPoint, count, &isstack);
  2569. if (fpoints == NULL) {
  2570. return SDL_OutOfMemory();
  2571. }
  2572. for (i = 0; i < count; ++i) {
  2573. fpoints[i].x = (float)points[i].x;
  2574. fpoints[i].y = (float)points[i].y;
  2575. }
  2576. retval = SDL_RenderLinesF(renderer, fpoints, count);
  2577. SDL_small_free(fpoints, isstack);
  2578. return retval;
  2579. }
  2580. int SDL_RenderLinesF(SDL_Renderer *renderer,
  2581. const SDL_FPoint *points, int count)
  2582. {
  2583. int retval = 0;
  2584. CHECK_RENDERER_MAGIC(renderer, -1);
  2585. if (points == NULL) {
  2586. return SDL_InvalidParamError("SDL_RenderLinesF(): points");
  2587. }
  2588. if (count < 2) {
  2589. return 0;
  2590. }
  2591. #if DONT_DRAW_WHILE_HIDDEN
  2592. /* Don't draw while we're hidden */
  2593. if (renderer->hidden) {
  2594. return 0;
  2595. }
  2596. #endif
  2597. if (renderer->line_method == SDL_RENDERLINEMETHOD_POINTS) {
  2598. retval = RenderLinesWithRectsF(renderer, points, count);
  2599. } else if (renderer->line_method == SDL_RENDERLINEMETHOD_GEOMETRY) {
  2600. SDL_bool isstack1;
  2601. SDL_bool isstack2;
  2602. const float scale_x = renderer->scale.x;
  2603. const float scale_y = renderer->scale.y;
  2604. float *xy = SDL_small_alloc(float, 4 * 2 * count, &isstack1);
  2605. int *indices = SDL_small_alloc(int,
  2606. (4) * 3 * (count - 1) + (2) * 3 * (count), &isstack2);
  2607. if (xy && indices) {
  2608. int i;
  2609. float *ptr_xy = xy;
  2610. int *ptr_indices = indices;
  2611. const int xy_stride = 2 * sizeof(float);
  2612. int num_vertices = 4 * count;
  2613. int num_indices = 0;
  2614. const int size_indices = 4;
  2615. int cur_index = -4;
  2616. const int is_looping = (points[0].x == points[count - 1].x && points[0].y == points[count - 1].y);
  2617. SDL_FPoint p; /* previous point */
  2618. p.x = p.y = 0.0f;
  2619. /* p q
  2620. 0----1------ 4----5
  2621. | \ |``\ | \ |
  2622. | \ | ` `\| \ |
  2623. 3----2-------7----6
  2624. */
  2625. for (i = 0; i < count; ++i) {
  2626. SDL_FPoint q = points[i]; /* current point */
  2627. q.x *= scale_x;
  2628. q.y *= scale_y;
  2629. *ptr_xy++ = q.x;
  2630. *ptr_xy++ = q.y;
  2631. *ptr_xy++ = q.x + scale_x;
  2632. *ptr_xy++ = q.y;
  2633. *ptr_xy++ = q.x + scale_x;
  2634. *ptr_xy++ = q.y + scale_y;
  2635. *ptr_xy++ = q.x;
  2636. *ptr_xy++ = q.y + scale_y;
  2637. #define ADD_TRIANGLE(i1, i2, i3) \
  2638. *ptr_indices++ = cur_index + (i1); \
  2639. *ptr_indices++ = cur_index + (i2); \
  2640. *ptr_indices++ = cur_index + (i3); \
  2641. num_indices += 3;
  2642. /* closed polyline, don´t draw twice the point */
  2643. if (i || is_looping == 0) {
  2644. ADD_TRIANGLE(4, 5, 6)
  2645. ADD_TRIANGLE(4, 6, 7)
  2646. }
  2647. /* first point only, no segment */
  2648. if (i == 0) {
  2649. p = q;
  2650. cur_index += 4;
  2651. continue;
  2652. }
  2653. /* draw segment */
  2654. if (p.y == q.y) {
  2655. if (p.x < q.x) {
  2656. ADD_TRIANGLE(1, 4, 7)
  2657. ADD_TRIANGLE(1, 7, 2)
  2658. } else {
  2659. ADD_TRIANGLE(5, 0, 3)
  2660. ADD_TRIANGLE(5, 3, 6)
  2661. }
  2662. } else if (p.x == q.x) {
  2663. if (p.y < q.y) {
  2664. ADD_TRIANGLE(2, 5, 4)
  2665. ADD_TRIANGLE(2, 4, 3)
  2666. } else {
  2667. ADD_TRIANGLE(6, 1, 0)
  2668. ADD_TRIANGLE(6, 0, 7)
  2669. }
  2670. } else {
  2671. if (p.y < q.y) {
  2672. if (p.x < q.x) {
  2673. ADD_TRIANGLE(1, 5, 4)
  2674. ADD_TRIANGLE(1, 4, 2)
  2675. ADD_TRIANGLE(2, 4, 7)
  2676. ADD_TRIANGLE(2, 7, 3)
  2677. } else {
  2678. ADD_TRIANGLE(4, 0, 5)
  2679. ADD_TRIANGLE(5, 0, 3)
  2680. ADD_TRIANGLE(5, 3, 6)
  2681. ADD_TRIANGLE(6, 3, 2)
  2682. }
  2683. } else {
  2684. if (p.x < q.x) {
  2685. ADD_TRIANGLE(0, 4, 7)
  2686. ADD_TRIANGLE(0, 7, 1)
  2687. ADD_TRIANGLE(1, 7, 6)
  2688. ADD_TRIANGLE(1, 6, 2)
  2689. } else {
  2690. ADD_TRIANGLE(6, 5, 1)
  2691. ADD_TRIANGLE(6, 1, 0)
  2692. ADD_TRIANGLE(7, 6, 0)
  2693. ADD_TRIANGLE(7, 0, 3)
  2694. }
  2695. }
  2696. }
  2697. p = q;
  2698. cur_index += 4;
  2699. }
  2700. retval = QueueCmdGeometry(renderer, NULL,
  2701. xy, xy_stride, &renderer->color, 0 /* color_stride */, NULL, 0,
  2702. num_vertices, indices, num_indices, size_indices,
  2703. 1.0f, 1.0f);
  2704. }
  2705. SDL_small_free(xy, isstack1);
  2706. SDL_small_free(indices, isstack2);
  2707. } else if (renderer->scale.x != 1.0f || renderer->scale.y != 1.0f) {
  2708. retval = RenderLinesWithRectsF(renderer, points, count);
  2709. } else {
  2710. retval = QueueCmdDrawLines(renderer, points, count);
  2711. }
  2712. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2713. }
  2714. int SDL_RenderRect(SDL_Renderer *renderer, const SDL_Rect *rect)
  2715. {
  2716. SDL_FRect frect;
  2717. SDL_FRect *prect = NULL;
  2718. if (rect) {
  2719. frect.x = (float)rect->x;
  2720. frect.y = (float)rect->y;
  2721. frect.w = (float)rect->w;
  2722. frect.h = (float)rect->h;
  2723. prect = &frect;
  2724. }
  2725. return SDL_RenderRectF(renderer, prect);
  2726. }
  2727. int SDL_RenderRectF(SDL_Renderer *renderer, const SDL_FRect *rect)
  2728. {
  2729. SDL_FRect frect;
  2730. SDL_FPoint points[5];
  2731. CHECK_RENDERER_MAGIC(renderer, -1);
  2732. /* If 'rect' == NULL, then outline the whole surface */
  2733. if (rect == NULL) {
  2734. GetRenderViewportSize(renderer, &frect);
  2735. rect = &frect;
  2736. }
  2737. points[0].x = rect->x;
  2738. points[0].y = rect->y;
  2739. points[1].x = rect->x + rect->w - 1;
  2740. points[1].y = rect->y;
  2741. points[2].x = rect->x + rect->w - 1;
  2742. points[2].y = rect->y + rect->h - 1;
  2743. points[3].x = rect->x;
  2744. points[3].y = rect->y + rect->h - 1;
  2745. points[4].x = rect->x;
  2746. points[4].y = rect->y;
  2747. return SDL_RenderLinesF(renderer, points, 5);
  2748. }
  2749. int SDL_RenderRects(SDL_Renderer *renderer,
  2750. const SDL_Rect *rects, int count)
  2751. {
  2752. int i;
  2753. CHECK_RENDERER_MAGIC(renderer, -1);
  2754. if (rects == NULL) {
  2755. return SDL_InvalidParamError("SDL_RenderRects(): rects");
  2756. }
  2757. if (count < 1) {
  2758. return 0;
  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. for (i = 0; i < count; ++i) {
  2767. if (SDL_RenderRect(renderer, &rects[i]) < 0) {
  2768. return -1;
  2769. }
  2770. }
  2771. return 0;
  2772. }
  2773. int SDL_RenderRectsF(SDL_Renderer *renderer,
  2774. const SDL_FRect *rects, int count)
  2775. {
  2776. int i;
  2777. CHECK_RENDERER_MAGIC(renderer, -1);
  2778. if (rects == NULL) {
  2779. return SDL_InvalidParamError("SDL_RenderRectsF(): rects");
  2780. }
  2781. if (count < 1) {
  2782. return 0;
  2783. }
  2784. #if DONT_DRAW_WHILE_HIDDEN
  2785. /* Don't draw while we're hidden */
  2786. if (renderer->hidden) {
  2787. return 0;
  2788. }
  2789. #endif
  2790. for (i = 0; i < count; ++i) {
  2791. if (SDL_RenderRectF(renderer, &rects[i]) < 0) {
  2792. return -1;
  2793. }
  2794. }
  2795. return 0;
  2796. }
  2797. int SDL_RenderFillRect(SDL_Renderer *renderer, const SDL_Rect *rect)
  2798. {
  2799. SDL_FRect frect;
  2800. CHECK_RENDERER_MAGIC(renderer, -1);
  2801. /* If 'rect' == NULL, then outline the whole surface */
  2802. if (rect) {
  2803. frect.x = (float)rect->x;
  2804. frect.y = (float)rect->y;
  2805. frect.w = (float)rect->w;
  2806. frect.h = (float)rect->h;
  2807. } else {
  2808. GetRenderViewportSize(renderer, &frect);
  2809. }
  2810. return SDL_RenderFillRectsF(renderer, &frect, 1);
  2811. }
  2812. int SDL_RenderFillRectF(SDL_Renderer *renderer, const SDL_FRect *rect)
  2813. {
  2814. SDL_FRect frect;
  2815. CHECK_RENDERER_MAGIC(renderer, -1);
  2816. /* If 'rect' == NULL, then outline the whole surface */
  2817. if (rect == NULL) {
  2818. GetRenderViewportSize(renderer, &frect);
  2819. rect = &frect;
  2820. }
  2821. return SDL_RenderFillRectsF(renderer, rect, 1);
  2822. }
  2823. int SDL_RenderFillRects(SDL_Renderer *renderer,
  2824. const SDL_Rect *rects, int count)
  2825. {
  2826. SDL_FRect *frects;
  2827. int i;
  2828. int retval;
  2829. SDL_bool isstack;
  2830. CHECK_RENDERER_MAGIC(renderer, -1);
  2831. if (rects == NULL) {
  2832. return SDL_InvalidParamError("SDL_RenderFillRects(): rects");
  2833. }
  2834. if (count < 1) {
  2835. return 0;
  2836. }
  2837. #if DONT_DRAW_WHILE_HIDDEN
  2838. /* Don't draw while we're hidden */
  2839. if (renderer->hidden) {
  2840. return 0;
  2841. }
  2842. #endif
  2843. frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  2844. if (frects == NULL) {
  2845. return SDL_OutOfMemory();
  2846. }
  2847. for (i = 0; i < count; ++i) {
  2848. frects[i].x = rects[i].x * renderer->scale.x;
  2849. frects[i].y = rects[i].y * renderer->scale.y;
  2850. frects[i].w = rects[i].w * renderer->scale.x;
  2851. frects[i].h = rects[i].h * renderer->scale.y;
  2852. }
  2853. retval = QueueCmdFillRects(renderer, frects, count);
  2854. SDL_small_free(frects, isstack);
  2855. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2856. }
  2857. int SDL_RenderFillRectsF(SDL_Renderer *renderer,
  2858. const SDL_FRect *rects, int count)
  2859. {
  2860. SDL_FRect *frects;
  2861. int i;
  2862. int retval;
  2863. SDL_bool isstack;
  2864. CHECK_RENDERER_MAGIC(renderer, -1);
  2865. if (rects == NULL) {
  2866. return SDL_InvalidParamError("SDL_RenderFillRectsF(): rects");
  2867. }
  2868. if (count < 1) {
  2869. return 0;
  2870. }
  2871. #if DONT_DRAW_WHILE_HIDDEN
  2872. /* Don't draw while we're hidden */
  2873. if (renderer->hidden) {
  2874. return 0;
  2875. }
  2876. #endif
  2877. frects = SDL_small_alloc(SDL_FRect, count, &isstack);
  2878. if (frects == NULL) {
  2879. return SDL_OutOfMemory();
  2880. }
  2881. for (i = 0; i < count; ++i) {
  2882. frects[i].x = rects[i].x * renderer->scale.x;
  2883. frects[i].y = rects[i].y * renderer->scale.y;
  2884. frects[i].w = rects[i].w * renderer->scale.x;
  2885. frects[i].h = rects[i].h * renderer->scale.y;
  2886. }
  2887. retval = QueueCmdFillRects(renderer, frects, count);
  2888. SDL_small_free(frects, isstack);
  2889. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2890. }
  2891. int SDL_RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture,
  2892. const SDL_Rect *srcrect, const SDL_Rect *dstrect)
  2893. {
  2894. SDL_FRect dstfrect;
  2895. SDL_FRect *pdstfrect = NULL;
  2896. if (dstrect) {
  2897. dstfrect.x = (float)dstrect->x;
  2898. dstfrect.y = (float)dstrect->y;
  2899. dstfrect.w = (float)dstrect->w;
  2900. dstfrect.h = (float)dstrect->h;
  2901. pdstfrect = &dstfrect;
  2902. }
  2903. return SDL_RenderTextureF(renderer, texture, srcrect, pdstfrect);
  2904. }
  2905. int SDL_RenderTextureF(SDL_Renderer *renderer, SDL_Texture *texture,
  2906. const SDL_Rect *srcrect, const SDL_FRect *dstrect)
  2907. {
  2908. SDL_Rect real_srcrect;
  2909. SDL_FRect real_dstrect;
  2910. int retval;
  2911. int use_rendergeometry;
  2912. CHECK_RENDERER_MAGIC(renderer, -1);
  2913. CHECK_TEXTURE_MAGIC(texture, -1);
  2914. if (renderer != texture->renderer) {
  2915. return SDL_SetError("Texture was not created with this renderer");
  2916. }
  2917. #if DONT_DRAW_WHILE_HIDDEN
  2918. /* Don't draw while we're hidden */
  2919. if (renderer->hidden) {
  2920. return 0;
  2921. }
  2922. #endif
  2923. use_rendergeometry = (renderer->QueueCopy == NULL);
  2924. real_srcrect.x = 0;
  2925. real_srcrect.y = 0;
  2926. real_srcrect.w = texture->w;
  2927. real_srcrect.h = texture->h;
  2928. if (srcrect) {
  2929. if (!SDL_GetRectIntersection(srcrect, &real_srcrect, &real_srcrect)) {
  2930. return 0;
  2931. }
  2932. }
  2933. GetRenderViewportSize(renderer, &real_dstrect);
  2934. if (dstrect) {
  2935. if (!SDL_HasRectIntersectionF(dstrect, &real_dstrect)) {
  2936. return 0;
  2937. }
  2938. real_dstrect = *dstrect;
  2939. }
  2940. if (texture->native) {
  2941. texture = texture->native;
  2942. }
  2943. texture->last_command_generation = renderer->render_command_generation;
  2944. if (use_rendergeometry) {
  2945. float xy[8];
  2946. const int xy_stride = 2 * sizeof(float);
  2947. float uv[8];
  2948. const int uv_stride = 2 * sizeof(float);
  2949. const int num_vertices = 4;
  2950. const int *indices = renderer->rect_index_order;
  2951. const int num_indices = 6;
  2952. const int size_indices = 4;
  2953. float minu, minv, maxu, maxv;
  2954. float minx, miny, maxx, maxy;
  2955. minu = (float)(real_srcrect.x) / (float)texture->w;
  2956. minv = (float)(real_srcrect.y) / (float)texture->h;
  2957. maxu = (float)(real_srcrect.x + real_srcrect.w) / (float)texture->w;
  2958. maxv = (float)(real_srcrect.y + real_srcrect.h) / (float)texture->h;
  2959. minx = real_dstrect.x;
  2960. miny = real_dstrect.y;
  2961. maxx = real_dstrect.x + real_dstrect.w;
  2962. maxy = real_dstrect.y + real_dstrect.h;
  2963. uv[0] = minu;
  2964. uv[1] = minv;
  2965. uv[2] = maxu;
  2966. uv[3] = minv;
  2967. uv[4] = maxu;
  2968. uv[5] = maxv;
  2969. uv[6] = minu;
  2970. uv[7] = maxv;
  2971. xy[0] = minx;
  2972. xy[1] = miny;
  2973. xy[2] = maxx;
  2974. xy[3] = miny;
  2975. xy[4] = maxx;
  2976. xy[5] = maxy;
  2977. xy[6] = minx;
  2978. xy[7] = maxy;
  2979. retval = QueueCmdGeometry(renderer, texture,
  2980. xy, xy_stride, &texture->color, 0 /* color_stride */, uv, uv_stride,
  2981. num_vertices,
  2982. indices, num_indices, size_indices,
  2983. renderer->scale.x, renderer->scale.y);
  2984. } else {
  2985. real_dstrect.x *= renderer->scale.x;
  2986. real_dstrect.y *= renderer->scale.y;
  2987. real_dstrect.w *= renderer->scale.x;
  2988. real_dstrect.h *= renderer->scale.y;
  2989. retval = QueueCmdCopy(renderer, texture, &real_srcrect, &real_dstrect);
  2990. }
  2991. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  2992. }
  2993. int SDL_RenderTextureRotated(SDL_Renderer *renderer, SDL_Texture *texture,
  2994. const SDL_Rect *srcrect, const SDL_Rect *dstrect,
  2995. const double angle, const SDL_Point *center, const SDL_RendererFlip flip)
  2996. {
  2997. SDL_FRect dstfrect;
  2998. SDL_FRect *pdstfrect = NULL;
  2999. SDL_FPoint fcenter;
  3000. SDL_FPoint *pfcenter = NULL;
  3001. if (dstrect) {
  3002. dstfrect.x = (float)dstrect->x;
  3003. dstfrect.y = (float)dstrect->y;
  3004. dstfrect.w = (float)dstrect->w;
  3005. dstfrect.h = (float)dstrect->h;
  3006. pdstfrect = &dstfrect;
  3007. }
  3008. if (center) {
  3009. fcenter.x = (float)center->x;
  3010. fcenter.y = (float)center->y;
  3011. pfcenter = &fcenter;
  3012. }
  3013. return SDL_RenderTextureRotatedF(renderer, texture, srcrect, pdstfrect, angle, pfcenter, flip);
  3014. }
  3015. int SDL_RenderTextureRotatedF(SDL_Renderer *renderer, SDL_Texture *texture,
  3016. const SDL_Rect *srcrect, const SDL_FRect *dstrect,
  3017. const double angle, const SDL_FPoint *center, const SDL_RendererFlip flip)
  3018. {
  3019. SDL_Rect real_srcrect;
  3020. SDL_FRect real_dstrect;
  3021. SDL_FPoint real_center;
  3022. int retval;
  3023. int use_rendergeometry;
  3024. if (flip == SDL_FLIP_NONE && (int)(angle / 360) == angle / 360) { /* fast path when we don't need rotation or flipping */
  3025. return SDL_RenderTextureF(renderer, texture, srcrect, dstrect);
  3026. }
  3027. CHECK_RENDERER_MAGIC(renderer, -1);
  3028. CHECK_TEXTURE_MAGIC(texture, -1);
  3029. if (renderer != texture->renderer) {
  3030. return SDL_SetError("Texture was not created with this renderer");
  3031. }
  3032. if (!renderer->QueueCopyEx && !renderer->QueueGeometry) {
  3033. return SDL_SetError("Renderer does not support RenderCopyEx");
  3034. }
  3035. #if DONT_DRAW_WHILE_HIDDEN
  3036. /* Don't draw while we're hidden */
  3037. if (renderer->hidden) {
  3038. return 0;
  3039. }
  3040. #endif
  3041. use_rendergeometry = (renderer->QueueCopyEx == NULL);
  3042. real_srcrect.x = 0;
  3043. real_srcrect.y = 0;
  3044. real_srcrect.w = texture->w;
  3045. real_srcrect.h = texture->h;
  3046. if (srcrect) {
  3047. if (!SDL_GetRectIntersection(srcrect, &real_srcrect, &real_srcrect)) {
  3048. return 0;
  3049. }
  3050. }
  3051. /* We don't intersect the dstrect with the viewport as RenderCopy does because of potential rotation clipping issues... TODO: should we? */
  3052. if (dstrect) {
  3053. real_dstrect = *dstrect;
  3054. } else {
  3055. GetRenderViewportSize(renderer, &real_dstrect);
  3056. }
  3057. if (texture->native) {
  3058. texture = texture->native;
  3059. }
  3060. if (center) {
  3061. real_center = *center;
  3062. } else {
  3063. real_center.x = real_dstrect.w / 2.0f;
  3064. real_center.y = real_dstrect.h / 2.0f;
  3065. }
  3066. texture->last_command_generation = renderer->render_command_generation;
  3067. if (use_rendergeometry) {
  3068. float xy[8];
  3069. const int xy_stride = 2 * sizeof(float);
  3070. float uv[8];
  3071. const int uv_stride = 2 * sizeof(float);
  3072. const int num_vertices = 4;
  3073. const int *indices = renderer->rect_index_order;
  3074. const int num_indices = 6;
  3075. const int size_indices = 4;
  3076. float minu, minv, maxu, maxv;
  3077. float minx, miny, maxx, maxy;
  3078. float centerx, centery;
  3079. float s_minx, s_miny, s_maxx, s_maxy;
  3080. float c_minx, c_miny, c_maxx, c_maxy;
  3081. const float radian_angle = (float)((SDL_PI_D * angle) / 180.0);
  3082. const float s = SDL_sinf(radian_angle);
  3083. const float c = SDL_cosf(radian_angle);
  3084. minu = (float)(real_srcrect.x) / (float)texture->w;
  3085. minv = (float)(real_srcrect.y) / (float)texture->h;
  3086. maxu = (float)(real_srcrect.x + real_srcrect.w) / (float)texture->w;
  3087. maxv = (float)(real_srcrect.y + real_srcrect.h) / (float)texture->h;
  3088. centerx = real_center.x + real_dstrect.x;
  3089. centery = real_center.y + real_dstrect.y;
  3090. if (flip & SDL_FLIP_HORIZONTAL) {
  3091. minx = real_dstrect.x + real_dstrect.w;
  3092. maxx = real_dstrect.x;
  3093. } else {
  3094. minx = real_dstrect.x;
  3095. maxx = real_dstrect.x + real_dstrect.w;
  3096. }
  3097. if (flip & SDL_FLIP_VERTICAL) {
  3098. miny = real_dstrect.y + real_dstrect.h;
  3099. maxy = real_dstrect.y;
  3100. } else {
  3101. miny = real_dstrect.y;
  3102. maxy = real_dstrect.y + real_dstrect.h;
  3103. }
  3104. uv[0] = minu;
  3105. uv[1] = minv;
  3106. uv[2] = maxu;
  3107. uv[3] = minv;
  3108. uv[4] = maxu;
  3109. uv[5] = maxv;
  3110. uv[6] = minu;
  3111. uv[7] = maxv;
  3112. /* apply rotation with 2x2 matrix ( c -s )
  3113. * ( s c ) */
  3114. s_minx = s * (minx - centerx);
  3115. s_miny = s * (miny - centery);
  3116. s_maxx = s * (maxx - centerx);
  3117. s_maxy = s * (maxy - centery);
  3118. c_minx = c * (minx - centerx);
  3119. c_miny = c * (miny - centery);
  3120. c_maxx = c * (maxx - centerx);
  3121. c_maxy = c * (maxy - centery);
  3122. /* (minx, miny) */
  3123. xy[0] = (c_minx - s_miny) + centerx;
  3124. xy[1] = (s_minx + c_miny) + centery;
  3125. /* (maxx, miny) */
  3126. xy[2] = (c_maxx - s_miny) + centerx;
  3127. xy[3] = (s_maxx + c_miny) + centery;
  3128. /* (maxx, maxy) */
  3129. xy[4] = (c_maxx - s_maxy) + centerx;
  3130. xy[5] = (s_maxx + c_maxy) + centery;
  3131. /* (minx, maxy) */
  3132. xy[6] = (c_minx - s_maxy) + centerx;
  3133. xy[7] = (s_minx + c_maxy) + centery;
  3134. retval = QueueCmdGeometry(renderer, texture,
  3135. xy, xy_stride, &texture->color, 0 /* color_stride */, uv, uv_stride,
  3136. num_vertices,
  3137. indices, num_indices, size_indices,
  3138. renderer->scale.x, renderer->scale.y);
  3139. } else {
  3140. retval = QueueCmdCopyEx(renderer, texture, &real_srcrect, &real_dstrect, angle, &real_center, flip, renderer->scale.x, renderer->scale.y);
  3141. }
  3142. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  3143. }
  3144. int SDL_RenderGeometry(SDL_Renderer *renderer,
  3145. SDL_Texture *texture,
  3146. const SDL_Vertex *vertices, int num_vertices,
  3147. const int *indices, int num_indices)
  3148. {
  3149. if (vertices) {
  3150. const float *xy = &vertices->position.x;
  3151. int xy_stride = sizeof(SDL_Vertex);
  3152. const SDL_Color *color = &vertices->color;
  3153. int color_stride = sizeof(SDL_Vertex);
  3154. const float *uv = &vertices->tex_coord.x;
  3155. int uv_stride = sizeof(SDL_Vertex);
  3156. int size_indices = 4;
  3157. return SDL_RenderGeometryRaw(renderer, texture, xy, xy_stride, color, color_stride, uv, uv_stride, num_vertices, indices, num_indices, size_indices);
  3158. } else {
  3159. return SDL_InvalidParamError("vertices");
  3160. }
  3161. }
  3162. static int remap_one_indice(
  3163. int prev,
  3164. int k,
  3165. SDL_Texture *texture,
  3166. const float *xy, int xy_stride,
  3167. const SDL_Color *color, int color_stride,
  3168. const float *uv, int uv_stride)
  3169. {
  3170. const float *xy0_, *xy1_, *uv0_, *uv1_;
  3171. int col0_, col1_;
  3172. xy0_ = (const float *)((const char *)xy + prev * xy_stride);
  3173. xy1_ = (const float *)((const char *)xy + k * xy_stride);
  3174. if (xy0_[0] != xy1_[0]) {
  3175. return k;
  3176. }
  3177. if (xy0_[1] != xy1_[1]) {
  3178. return k;
  3179. }
  3180. if (texture) {
  3181. uv0_ = (const float *)((const char *)uv + prev * uv_stride);
  3182. uv1_ = (const float *)((const char *)uv + k * uv_stride);
  3183. if (uv0_[0] != uv1_[0]) {
  3184. return k;
  3185. }
  3186. if (uv0_[1] != uv1_[1]) {
  3187. return k;
  3188. }
  3189. }
  3190. col0_ = *(const int *)((const char *)color + prev * color_stride);
  3191. col1_ = *(const int *)((const char *)color + k * color_stride);
  3192. if (col0_ != col1_) {
  3193. return k;
  3194. }
  3195. return prev;
  3196. }
  3197. static int remap_indices(
  3198. int prev[3],
  3199. int k,
  3200. SDL_Texture *texture,
  3201. const float *xy, int xy_stride,
  3202. const SDL_Color *color, int color_stride,
  3203. const float *uv, int uv_stride)
  3204. {
  3205. int i;
  3206. if (prev[0] == -1) {
  3207. return k;
  3208. }
  3209. for (i = 0; i < 3; i++) {
  3210. int new_k = remap_one_indice(prev[i], k, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
  3211. if (new_k != k) {
  3212. return new_k;
  3213. }
  3214. }
  3215. return k;
  3216. }
  3217. #define DEBUG_SW_RENDER_GEOMETRY 0
  3218. /* For the software renderer, try to reinterpret triangles as SDL_Rect */
  3219. static int SDLCALL SDL_SW_RenderGeometryRaw(SDL_Renderer *renderer,
  3220. SDL_Texture *texture,
  3221. const float *xy, int xy_stride,
  3222. const SDL_Color *color, int color_stride,
  3223. const float *uv, int uv_stride,
  3224. int num_vertices,
  3225. const void *indices, int num_indices, int size_indices)
  3226. {
  3227. int i;
  3228. int retval = 0;
  3229. int count = indices ? num_indices : num_vertices;
  3230. int prev[3]; /* Previous triangle vertex indices */
  3231. int texw = 0, texh = 0;
  3232. SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  3233. Uint8 r = 0, g = 0, b = 0, a = 0;
  3234. /* Save */
  3235. SDL_GetRenderDrawBlendMode(renderer, &blendMode);
  3236. SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
  3237. if (texture) {
  3238. SDL_QueryTexture(texture, NULL, NULL, &texw, &texh);
  3239. }
  3240. prev[0] = -1;
  3241. prev[1] = -1;
  3242. prev[2] = -1;
  3243. size_indices = indices ? size_indices : 0;
  3244. for (i = 0; i < count; i += 3) {
  3245. int k0, k1, k2; /* Current triangle indices */
  3246. int is_quad = 1;
  3247. #if DEBUG_SW_RENDER_GEOMETRY
  3248. int is_uniform = 1;
  3249. int is_rectangle = 1;
  3250. #endif
  3251. int A = -1; /* Top left vertex */
  3252. int B = -1; /* Bottom right vertex */
  3253. int C = -1; /* Third vertex of current triangle */
  3254. int C2 = -1; /* Last, vertex of previous triangle */
  3255. if (size_indices == 4) {
  3256. k0 = ((const Uint32 *)indices)[i];
  3257. k1 = ((const Uint32 *)indices)[i + 1];
  3258. k2 = ((const Uint32 *)indices)[i + 2];
  3259. } else if (size_indices == 2) {
  3260. k0 = ((const Uint16 *)indices)[i];
  3261. k1 = ((const Uint16 *)indices)[i + 1];
  3262. k2 = ((const Uint16 *)indices)[i + 2];
  3263. } else if (size_indices == 1) {
  3264. k0 = ((const Uint8 *)indices)[i];
  3265. k1 = ((const Uint8 *)indices)[i + 1];
  3266. k2 = ((const Uint8 *)indices)[i + 2];
  3267. } else {
  3268. /* Vertices were not provided by indices. Maybe some are duplicated.
  3269. * We try to indentificate the duplicates by comparing with the previous three vertices */
  3270. k0 = remap_indices(prev, i, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
  3271. k1 = remap_indices(prev, i + 1, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
  3272. k2 = remap_indices(prev, i + 2, texture, xy, xy_stride, color, color_stride, uv, uv_stride);
  3273. }
  3274. if (prev[0] == -1) {
  3275. prev[0] = k0;
  3276. prev[1] = k1;
  3277. prev[2] = k2;
  3278. continue;
  3279. }
  3280. /* Two triangles forming a quadialateral,
  3281. * prev and current triangles must have exactly 2 common vertices */
  3282. {
  3283. int cnt = 0, j = 3;
  3284. while (j--) {
  3285. int p = prev[j];
  3286. if (p == k0 || p == k1 || p == k2) {
  3287. cnt++;
  3288. }
  3289. }
  3290. is_quad = (cnt == 2);
  3291. }
  3292. /* Identify vertices */
  3293. if (is_quad) {
  3294. const float *xy0_, *xy1_, *xy2_;
  3295. float x0, x1, x2;
  3296. float y0, y1, y2;
  3297. xy0_ = (const float *)((const char *)xy + k0 * xy_stride);
  3298. xy1_ = (const float *)((const char *)xy + k1 * xy_stride);
  3299. xy2_ = (const float *)((const char *)xy + k2 * xy_stride);
  3300. x0 = xy0_[0];
  3301. y0 = xy0_[1];
  3302. x1 = xy1_[0];
  3303. y1 = xy1_[1];
  3304. x2 = xy2_[0];
  3305. y2 = xy2_[1];
  3306. /* Find top-left */
  3307. if (x0 <= x1 && y0 <= y1) {
  3308. if (x0 <= x2 && y0 <= y2) {
  3309. A = k0;
  3310. } else {
  3311. A = k2;
  3312. }
  3313. } else {
  3314. if (x1 <= x2 && y1 <= y2) {
  3315. A = k1;
  3316. } else {
  3317. A = k2;
  3318. }
  3319. }
  3320. /* Find bottom-right */
  3321. if (x0 >= x1 && y0 >= y1) {
  3322. if (x0 >= x2 && y0 >= y2) {
  3323. B = k0;
  3324. } else {
  3325. B = k2;
  3326. }
  3327. } else {
  3328. if (x1 >= x2 && y1 >= y2) {
  3329. B = k1;
  3330. } else {
  3331. B = k2;
  3332. }
  3333. }
  3334. /* Find C */
  3335. if (k0 != A && k0 != B) {
  3336. C = k0;
  3337. } else if (k1 != A && k1 != B) {
  3338. C = k1;
  3339. } else {
  3340. C = k2;
  3341. }
  3342. /* Find C2 */
  3343. if (prev[0] != A && prev[0] != B) {
  3344. C2 = prev[0];
  3345. } else if (prev[1] != A && prev[1] != B) {
  3346. C2 = prev[1];
  3347. } else {
  3348. C2 = prev[2];
  3349. }
  3350. xy0_ = (const float *)((const char *)xy + A * xy_stride);
  3351. xy1_ = (const float *)((const char *)xy + B * xy_stride);
  3352. xy2_ = (const float *)((const char *)xy + C * xy_stride);
  3353. x0 = xy0_[0];
  3354. y0 = xy0_[1];
  3355. x1 = xy1_[0];
  3356. y1 = xy1_[1];
  3357. x2 = xy2_[0];
  3358. y2 = xy2_[1];
  3359. /* Check if triangle A B C is rectangle */
  3360. if ((x0 == x2 && y1 == y2) || (y0 == y2 && x1 == x2)) {
  3361. /* ok */
  3362. } else {
  3363. is_quad = 0;
  3364. #if DEBUG_SW_RENDER_GEOMETRY
  3365. is_rectangle = 0;
  3366. #endif
  3367. }
  3368. xy2_ = (const float *)((const char *)xy + C2 * xy_stride);
  3369. x2 = xy2_[0];
  3370. y2 = xy2_[1];
  3371. /* Check if triangle A B C2 is rectangle */
  3372. if ((x0 == x2 && y1 == y2) || (y0 == y2 && x1 == x2)) {
  3373. /* ok */
  3374. } else {
  3375. is_quad = 0;
  3376. #if DEBUG_SW_RENDER_GEOMETRY
  3377. is_rectangle = 0;
  3378. #endif
  3379. }
  3380. }
  3381. /* Check if uniformly colored */
  3382. if (is_quad) {
  3383. const int col0_ = *(const int *)((const char *)color + A * color_stride);
  3384. const int col1_ = *(const int *)((const char *)color + B * color_stride);
  3385. const int col2_ = *(const int *)((const char *)color + C * color_stride);
  3386. const int col3_ = *(const int *)((const char *)color + C2 * color_stride);
  3387. if (col0_ == col1_ && col0_ == col2_ && col0_ == col3_) {
  3388. /* ok */
  3389. } else {
  3390. is_quad = 0;
  3391. #if DEBUG_SW_RENDER_GEOMETRY
  3392. is_uniform = 0;
  3393. #endif
  3394. }
  3395. }
  3396. /* Start rendering rect */
  3397. if (is_quad) {
  3398. SDL_Rect s;
  3399. SDL_FRect d;
  3400. const float *xy0_, *xy1_, *uv0_, *uv1_;
  3401. SDL_Color col0_ = *(const SDL_Color *)((const char *)color + k0 * color_stride);
  3402. xy0_ = (const float *)((const char *)xy + A * xy_stride);
  3403. xy1_ = (const float *)((const char *)xy + B * xy_stride);
  3404. if (texture) {
  3405. uv0_ = (const float *)((const char *)uv + A * uv_stride);
  3406. uv1_ = (const float *)((const char *)uv + B * uv_stride);
  3407. s.x = (int)(uv0_[0] * texw);
  3408. s.y = (int)(uv0_[1] * texh);
  3409. s.w = (int)(uv1_[0] * texw - s.x);
  3410. s.h = (int)(uv1_[1] * texh - s.y);
  3411. }
  3412. d.x = xy0_[0];
  3413. d.y = xy0_[1];
  3414. d.w = xy1_[0] - d.x;
  3415. d.h = xy1_[1] - d.y;
  3416. /* Rect + texture */
  3417. if (texture && s.w != 0 && s.h != 0) {
  3418. SDL_SetTextureAlphaMod(texture, col0_.a);
  3419. SDL_SetTextureColorMod(texture, col0_.r, col0_.g, col0_.b);
  3420. if (s.w > 0 && s.h > 0) {
  3421. SDL_RenderTextureF(renderer, texture, &s, &d);
  3422. } else {
  3423. int flags = 0;
  3424. if (s.w < 0) {
  3425. flags |= SDL_FLIP_HORIZONTAL;
  3426. s.w *= -1;
  3427. s.x -= s.w;
  3428. }
  3429. if (s.h < 0) {
  3430. flags |= SDL_FLIP_VERTICAL;
  3431. s.h *= -1;
  3432. s.y -= s.h;
  3433. }
  3434. SDL_RenderTextureRotatedF(renderer, texture, &s, &d, 0, NULL, flags);
  3435. }
  3436. #if DEBUG_SW_RENDER_GEOMETRY
  3437. SDL_Log("Rect-COPY: RGB %d %d %d - Alpha:%d - texture=%p: src=(%d,%d, %d x %d) dst (%f, %f, %f x %f)", col0_.r, col0_.g, col0_.b, col0_.a,
  3438. (void *)texture, s.x, s.y, s.w, s.h, d.x, d.y, d.w, d.h);
  3439. #endif
  3440. } else if (d.w != 0.0f && d.h != 0.0f) { /* Rect, no texture */
  3441. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  3442. SDL_SetRenderDrawColor(renderer, col0_.r, col0_.g, col0_.b, col0_.a);
  3443. SDL_RenderFillRectF(renderer, &d);
  3444. #if DEBUG_SW_RENDER_GEOMETRY
  3445. SDL_Log("Rect-FILL: RGB %d %d %d - Alpha:%d - texture=%p: dst (%f, %f, %f x %f)", col0_.r, col0_.g, col0_.b, col0_.a,
  3446. (void *)texture, d.x, d.y, d.w, d.h);
  3447. } else {
  3448. SDL_Log("Rect-DISMISS: RGB %d %d %d - Alpha:%d - texture=%p: src=(%d,%d, %d x %d) dst (%f, %f, %f x %f)", col0_.r, col0_.g, col0_.b, col0_.a,
  3449. (void *)texture, s.x, s.y, s.w, s.h, d.x, d.y, d.w, d.h);
  3450. #endif
  3451. }
  3452. prev[0] = -1;
  3453. } else {
  3454. /* Render triangles */
  3455. if (prev[0] != -1) {
  3456. #if DEBUG_SW_RENDER_GEOMETRY
  3457. SDL_Log("Triangle %d %d %d - is_uniform:%d is_rectangle:%d", prev[0], prev[1], prev[2], is_uniform, is_rectangle);
  3458. #endif
  3459. retval = QueueCmdGeometry(renderer, texture,
  3460. xy, xy_stride, color, color_stride, uv, uv_stride,
  3461. num_vertices, prev, 3, 4, renderer->scale.x, renderer->scale.y);
  3462. if (retval < 0) {
  3463. goto end;
  3464. } else {
  3465. FlushRenderCommandsIfNotBatching(renderer);
  3466. }
  3467. }
  3468. prev[0] = k0;
  3469. prev[1] = k1;
  3470. prev[2] = k2;
  3471. }
  3472. } /* End for (), next triangle */
  3473. if (prev[0] != -1) {
  3474. /* flush the last triangle */
  3475. #if DEBUG_SW_RENDER_GEOMETRY
  3476. SDL_Log("Last triangle %d %d %d", prev[0], prev[1], prev[2]);
  3477. #endif
  3478. retval = QueueCmdGeometry(renderer, texture,
  3479. xy, xy_stride, color, color_stride, uv, uv_stride,
  3480. num_vertices, prev, 3, 4, renderer->scale.x, renderer->scale.y);
  3481. if (retval < 0) {
  3482. goto end;
  3483. } else {
  3484. FlushRenderCommandsIfNotBatching(renderer);
  3485. }
  3486. }
  3487. end:
  3488. /* Restore */
  3489. SDL_SetRenderDrawBlendMode(renderer, blendMode);
  3490. SDL_SetRenderDrawColor(renderer, r, g, b, a);
  3491. return retval;
  3492. }
  3493. int SDL_RenderGeometryRaw(SDL_Renderer *renderer,
  3494. SDL_Texture *texture,
  3495. const float *xy, int xy_stride,
  3496. const SDL_Color *color, int color_stride,
  3497. const float *uv, int uv_stride,
  3498. int num_vertices,
  3499. const void *indices, int num_indices, int size_indices)
  3500. {
  3501. int i;
  3502. int retval = 0;
  3503. int count = indices ? num_indices : num_vertices;
  3504. CHECK_RENDERER_MAGIC(renderer, -1);
  3505. if (!renderer->QueueGeometry) {
  3506. return SDL_Unsupported();
  3507. }
  3508. if (texture) {
  3509. CHECK_TEXTURE_MAGIC(texture, -1);
  3510. if (renderer != texture->renderer) {
  3511. return SDL_SetError("Texture was not created with this renderer");
  3512. }
  3513. }
  3514. if (xy == NULL) {
  3515. return SDL_InvalidParamError("xy");
  3516. }
  3517. if (color == NULL) {
  3518. return SDL_InvalidParamError("color");
  3519. }
  3520. if (texture && uv == NULL) {
  3521. return SDL_InvalidParamError("uv");
  3522. }
  3523. if (count % 3 != 0) {
  3524. return SDL_InvalidParamError(indices ? "num_indices" : "num_vertices");
  3525. }
  3526. if (indices) {
  3527. if (size_indices != 1 && size_indices != 2 && size_indices != 4) {
  3528. return SDL_InvalidParamError("size_indices");
  3529. }
  3530. } else {
  3531. size_indices = 0;
  3532. }
  3533. #if DONT_DRAW_WHILE_HIDDEN
  3534. /* Don't draw while we're hidden */
  3535. if (renderer->hidden) {
  3536. return 0;
  3537. }
  3538. #endif
  3539. if (num_vertices < 3) {
  3540. return 0;
  3541. }
  3542. if (texture && texture->native) {
  3543. texture = texture->native;
  3544. }
  3545. if (texture) {
  3546. for (i = 0; i < num_vertices; ++i) {
  3547. const float *uv_ = (const float *)((const char *)uv + i * uv_stride);
  3548. float u = uv_[0];
  3549. float v = uv_[1];
  3550. if (u < 0.0f || v < 0.0f || u > 1.0f || v > 1.0f) {
  3551. return SDL_SetError("Values of 'uv' out of bounds %f %f at %d/%d", u, v, i, num_vertices);
  3552. }
  3553. }
  3554. }
  3555. if (indices) {
  3556. for (i = 0; i < num_indices; ++i) {
  3557. int j;
  3558. if (size_indices == 4) {
  3559. j = ((const Uint32 *)indices)[i];
  3560. } else if (size_indices == 2) {
  3561. j = ((const Uint16 *)indices)[i];
  3562. } else {
  3563. j = ((const Uint8 *)indices)[i];
  3564. }
  3565. if (j < 0 || j >= num_vertices) {
  3566. return SDL_SetError("Values of 'indices' out of bounds");
  3567. }
  3568. }
  3569. }
  3570. if (texture) {
  3571. texture->last_command_generation = renderer->render_command_generation;
  3572. }
  3573. /* For the software renderer, try to reinterpret triangles as SDL_Rect */
  3574. if (renderer->info.flags & SDL_RENDERER_SOFTWARE) {
  3575. return SDL_SW_RenderGeometryRaw(renderer, texture,
  3576. xy, xy_stride, color, color_stride, uv, uv_stride, num_vertices,
  3577. indices, num_indices, size_indices);
  3578. }
  3579. retval = QueueCmdGeometry(renderer, texture,
  3580. xy, xy_stride, color, color_stride, uv, uv_stride,
  3581. num_vertices,
  3582. indices, num_indices, size_indices,
  3583. renderer->scale.x, renderer->scale.y);
  3584. return retval < 0 ? retval : FlushRenderCommandsIfNotBatching(renderer);
  3585. }
  3586. int SDL_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect,
  3587. Uint32 format, void *pixels, int pitch)
  3588. {
  3589. SDL_Rect real_rect;
  3590. CHECK_RENDERER_MAGIC(renderer, -1);
  3591. if (!renderer->RenderReadPixels) {
  3592. return SDL_Unsupported();
  3593. }
  3594. FlushRenderCommands(renderer); /* we need to render before we read the results. */
  3595. if (!format) {
  3596. if (renderer->target == NULL) {
  3597. format = SDL_GetWindowPixelFormat(renderer->window);
  3598. } else {
  3599. format = renderer->target->format;
  3600. }
  3601. }
  3602. real_rect.x = (int)SDL_floor(renderer->viewport.x);
  3603. real_rect.y = (int)SDL_floor(renderer->viewport.y);
  3604. real_rect.w = (int)SDL_floor(renderer->viewport.w);
  3605. real_rect.h = (int)SDL_floor(renderer->viewport.h);
  3606. if (rect) {
  3607. if (!SDL_GetRectIntersection(rect, &real_rect, &real_rect)) {
  3608. return 0;
  3609. }
  3610. if (real_rect.y > rect->y) {
  3611. pixels = (Uint8 *)pixels + pitch * (real_rect.y - rect->y);
  3612. }
  3613. if (real_rect.x > rect->x) {
  3614. int bpp = SDL_BYTESPERPIXEL(format);
  3615. pixels = (Uint8 *)pixels + bpp * (real_rect.x - rect->x);
  3616. }
  3617. }
  3618. return renderer->RenderReadPixels(renderer, &real_rect,
  3619. format, pixels, pitch);
  3620. }
  3621. static void SDL_SimulateRenderVSync(SDL_Renderer *renderer)
  3622. {
  3623. Uint64 now, elapsed;
  3624. const Uint64 interval = renderer->simulate_vsync_interval_ns;
  3625. if (!interval) {
  3626. /* We can't do sub-ns delay, so just return here */
  3627. return;
  3628. }
  3629. now = SDL_GetTicksNS();
  3630. elapsed = (now - renderer->last_present);
  3631. if (elapsed < interval) {
  3632. Uint64 duration = (interval - elapsed);
  3633. SDL_DelayNS(duration);
  3634. now = SDL_GetTicksNS();
  3635. }
  3636. elapsed = (now - renderer->last_present);
  3637. if (!renderer->last_present || elapsed > SDL_MS_TO_NS(1000)) {
  3638. /* It's been too long, reset the presentation timeline */
  3639. renderer->last_present = now;
  3640. } else {
  3641. renderer->last_present += (elapsed / interval) * interval;
  3642. }
  3643. }
  3644. void SDL_RenderPresent(SDL_Renderer *renderer)
  3645. {
  3646. SDL_bool presented = SDL_TRUE;
  3647. CHECK_RENDERER_MAGIC(renderer, );
  3648. FlushRenderCommands(renderer); /* time to send everything to the GPU! */
  3649. #if DONT_DRAW_WHILE_HIDDEN
  3650. /* Don't present while we're hidden */
  3651. if (renderer->hidden) {
  3652. presented = SDL_FALSE;
  3653. } else
  3654. #endif
  3655. if (renderer->RenderPresent(renderer) < 0) {
  3656. presented = SDL_FALSE;
  3657. }
  3658. if (renderer->simulate_vsync ||
  3659. (!presented && renderer->wanted_vsync)) {
  3660. SDL_SimulateRenderVSync(renderer);
  3661. }
  3662. }
  3663. void SDL_DestroyTexture(SDL_Texture *texture)
  3664. {
  3665. SDL_Renderer *renderer;
  3666. CHECK_TEXTURE_MAGIC(texture, );
  3667. renderer = texture->renderer;
  3668. if (texture == renderer->target) {
  3669. SDL_SetRenderTarget(renderer, NULL); /* implies command queue flush */
  3670. } else {
  3671. FlushRenderCommandsIfTextureNeeded(texture);
  3672. }
  3673. texture->magic = NULL;
  3674. if (texture->next) {
  3675. texture->next->prev = texture->prev;
  3676. }
  3677. if (texture->prev) {
  3678. texture->prev->next = texture->next;
  3679. } else {
  3680. renderer->textures = texture->next;
  3681. }
  3682. if (texture->native) {
  3683. SDL_DestroyTexture(texture->native);
  3684. }
  3685. #if SDL_HAVE_YUV
  3686. if (texture->yuv) {
  3687. SDL_SW_DestroyYUVTexture(texture->yuv);
  3688. }
  3689. #endif
  3690. SDL_free(texture->pixels);
  3691. renderer->DestroyTexture(renderer, texture);
  3692. SDL_DestroySurface(texture->locked_surface);
  3693. texture->locked_surface = NULL;
  3694. SDL_free(texture);
  3695. }
  3696. void SDL_DestroyRenderer(SDL_Renderer *renderer)
  3697. {
  3698. SDL_RenderCommand *cmd;
  3699. CHECK_RENDERER_MAGIC(renderer, );
  3700. SDL_DelEventWatch(SDL_RendererEventWatch, renderer);
  3701. if (renderer->render_commands_tail != NULL) {
  3702. renderer->render_commands_tail->next = renderer->render_commands_pool;
  3703. cmd = renderer->render_commands;
  3704. } else {
  3705. cmd = renderer->render_commands_pool;
  3706. }
  3707. renderer->render_commands_pool = NULL;
  3708. renderer->render_commands_tail = NULL;
  3709. renderer->render_commands = NULL;
  3710. while (cmd != NULL) {
  3711. SDL_RenderCommand *next = cmd->next;
  3712. SDL_free(cmd);
  3713. cmd = next;
  3714. }
  3715. SDL_free(renderer->vertex_data);
  3716. /* Free existing textures for this renderer */
  3717. while (renderer->textures) {
  3718. SDL_Texture *tex = renderer->textures;
  3719. (void)tex;
  3720. SDL_DestroyTexture(renderer->textures);
  3721. SDL_assert(tex != renderer->textures); /* satisfy static analysis. */
  3722. }
  3723. if (renderer->window) {
  3724. SDL_SetWindowData(renderer->window, SDL_WINDOWRENDERDATA, NULL);
  3725. }
  3726. /* It's no longer magical... */
  3727. renderer->magic = NULL;
  3728. /* Free the target mutex */
  3729. SDL_DestroyMutex(renderer->target_mutex);
  3730. renderer->target_mutex = NULL;
  3731. /* Free the renderer instance */
  3732. renderer->DestroyRenderer(renderer);
  3733. }
  3734. int SDL_GL_BindTexture(SDL_Texture *texture, float *texw, float *texh)
  3735. {
  3736. SDL_Renderer *renderer;
  3737. CHECK_TEXTURE_MAGIC(texture, -1);
  3738. renderer = texture->renderer;
  3739. if (texture->native) {
  3740. return SDL_GL_BindTexture(texture->native, texw, texh);
  3741. } else if (renderer && renderer->GL_BindTexture) {
  3742. FlushRenderCommandsIfTextureNeeded(texture); /* in case the app is going to mess with it. */
  3743. return renderer->GL_BindTexture(renderer, texture, texw, texh);
  3744. } else {
  3745. return SDL_Unsupported();
  3746. }
  3747. }
  3748. int SDL_GL_UnbindTexture(SDL_Texture *texture)
  3749. {
  3750. SDL_Renderer *renderer;
  3751. CHECK_TEXTURE_MAGIC(texture, -1);
  3752. renderer = texture->renderer;
  3753. if (texture->native) {
  3754. return SDL_GL_UnbindTexture(texture->native);
  3755. } else if (renderer && renderer->GL_UnbindTexture) {
  3756. FlushRenderCommandsIfTextureNeeded(texture); /* in case the app messed with it. */
  3757. return renderer->GL_UnbindTexture(renderer, texture);
  3758. }
  3759. return SDL_Unsupported();
  3760. }
  3761. void *
  3762. SDL_GetRenderMetalLayer(SDL_Renderer *renderer)
  3763. {
  3764. CHECK_RENDERER_MAGIC(renderer, NULL);
  3765. if (renderer->GetMetalLayer) {
  3766. FlushRenderCommands(renderer); /* in case the app is going to mess with it. */
  3767. return renderer->GetMetalLayer(renderer);
  3768. }
  3769. return NULL;
  3770. }
  3771. void *
  3772. SDL_GetRenderMetalCommandEncoder(SDL_Renderer *renderer)
  3773. {
  3774. CHECK_RENDERER_MAGIC(renderer, NULL);
  3775. if (renderer->GetMetalCommandEncoder) {
  3776. FlushRenderCommands(renderer); /* in case the app is going to mess with it. */
  3777. return renderer->GetMetalCommandEncoder(renderer);
  3778. }
  3779. return NULL;
  3780. }
  3781. static SDL_BlendMode SDL_GetShortBlendMode(SDL_BlendMode blendMode)
  3782. {
  3783. if (blendMode == SDL_BLENDMODE_NONE_FULL) {
  3784. return SDL_BLENDMODE_NONE;
  3785. }
  3786. if (blendMode == SDL_BLENDMODE_BLEND_FULL) {
  3787. return SDL_BLENDMODE_BLEND;
  3788. }
  3789. if (blendMode == SDL_BLENDMODE_ADD_FULL) {
  3790. return SDL_BLENDMODE_ADD;
  3791. }
  3792. if (blendMode == SDL_BLENDMODE_MOD_FULL) {
  3793. return SDL_BLENDMODE_MOD;
  3794. }
  3795. if (blendMode == SDL_BLENDMODE_MUL_FULL) {
  3796. return SDL_BLENDMODE_MUL;
  3797. }
  3798. return blendMode;
  3799. }
  3800. static SDL_BlendMode SDL_GetLongBlendMode(SDL_BlendMode blendMode)
  3801. {
  3802. if (blendMode == SDL_BLENDMODE_NONE) {
  3803. return SDL_BLENDMODE_NONE_FULL;
  3804. }
  3805. if (blendMode == SDL_BLENDMODE_BLEND) {
  3806. return SDL_BLENDMODE_BLEND_FULL;
  3807. }
  3808. if (blendMode == SDL_BLENDMODE_ADD) {
  3809. return SDL_BLENDMODE_ADD_FULL;
  3810. }
  3811. if (blendMode == SDL_BLENDMODE_MOD) {
  3812. return SDL_BLENDMODE_MOD_FULL;
  3813. }
  3814. if (blendMode == SDL_BLENDMODE_MUL) {
  3815. return SDL_BLENDMODE_MUL_FULL;
  3816. }
  3817. return blendMode;
  3818. }
  3819. SDL_BlendMode
  3820. SDL_ComposeCustomBlendMode(SDL_BlendFactor srcColorFactor, SDL_BlendFactor dstColorFactor,
  3821. SDL_BlendOperation colorOperation,
  3822. SDL_BlendFactor srcAlphaFactor, SDL_BlendFactor dstAlphaFactor,
  3823. SDL_BlendOperation alphaOperation)
  3824. {
  3825. SDL_BlendMode blendMode = SDL_COMPOSE_BLENDMODE(srcColorFactor, dstColorFactor, colorOperation,
  3826. srcAlphaFactor, dstAlphaFactor, alphaOperation);
  3827. return SDL_GetShortBlendMode(blendMode);
  3828. }
  3829. SDL_BlendFactor
  3830. SDL_GetBlendModeSrcColorFactor(SDL_BlendMode blendMode)
  3831. {
  3832. blendMode = SDL_GetLongBlendMode(blendMode);
  3833. return (SDL_BlendFactor)(((Uint32)blendMode >> 4) & 0xF);
  3834. }
  3835. SDL_BlendFactor
  3836. SDL_GetBlendModeDstColorFactor(SDL_BlendMode blendMode)
  3837. {
  3838. blendMode = SDL_GetLongBlendMode(blendMode);
  3839. return (SDL_BlendFactor)(((Uint32)blendMode >> 8) & 0xF);
  3840. }
  3841. SDL_BlendOperation
  3842. SDL_GetBlendModeColorOperation(SDL_BlendMode blendMode)
  3843. {
  3844. blendMode = SDL_GetLongBlendMode(blendMode);
  3845. return (SDL_BlendOperation)(((Uint32)blendMode >> 0) & 0xF);
  3846. }
  3847. SDL_BlendFactor
  3848. SDL_GetBlendModeSrcAlphaFactor(SDL_BlendMode blendMode)
  3849. {
  3850. blendMode = SDL_GetLongBlendMode(blendMode);
  3851. return (SDL_BlendFactor)(((Uint32)blendMode >> 20) & 0xF);
  3852. }
  3853. SDL_BlendFactor
  3854. SDL_GetBlendModeDstAlphaFactor(SDL_BlendMode blendMode)
  3855. {
  3856. blendMode = SDL_GetLongBlendMode(blendMode);
  3857. return (SDL_BlendFactor)(((Uint32)blendMode >> 24) & 0xF);
  3858. }
  3859. SDL_BlendOperation
  3860. SDL_GetBlendModeAlphaOperation(SDL_BlendMode blendMode)
  3861. {
  3862. blendMode = SDL_GetLongBlendMode(blendMode);
  3863. return (SDL_BlendOperation)(((Uint32)blendMode >> 16) & 0xF);
  3864. }
  3865. int SDL_SetRenderVSync(SDL_Renderer *renderer, int vsync)
  3866. {
  3867. CHECK_RENDERER_MAGIC(renderer, -1);
  3868. if (vsync != 0 && vsync != 1) {
  3869. return SDL_Unsupported();
  3870. }
  3871. renderer->wanted_vsync = vsync ? SDL_TRUE : SDL_FALSE;
  3872. if (!renderer->SetVSync ||
  3873. renderer->SetVSync(renderer, vsync) < 0) {
  3874. renderer->simulate_vsync = vsync ? SDL_TRUE : SDL_FALSE;
  3875. } else {
  3876. renderer->simulate_vsync = SDL_FALSE;
  3877. }
  3878. return 0;
  3879. }