SDL_render.c 132 KB

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