SDL_render.c 140 KB

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