video_driver.c 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360
  1. /* RetroArch - A frontend for libretro.
  2. * Copyright (C) 2010-2014 - Hans-Kristian Arntzen
  3. * Copyright (C) 2011-2021 - Daniel De Matteis
  4. *
  5. * RetroArch is free software: you can redistribute it and/or modify it under the terms
  6. * of the GNU General Public License as published by the Free Software Found-
  7. * ation, either version 3 of the License, or (at your option) any later version.
  8. *
  9. * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. * PURPOSE. See the GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with RetroArch.
  14. * If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <math.h>
  17. #include <string/stdstring.h>
  18. #include <retro_math.h>
  19. #ifdef HAVE_CONFIG_H
  20. #include "../config.h"
  21. #endif
  22. #include <gfx/video_frame.h>
  23. #include "../config.def.h"
  24. #include "video_driver.h"
  25. #include "video_filter.h"
  26. #include "video_display_server.h"
  27. #include "gfx_animation.h"
  28. #ifdef HAVE_GFX_WIDGETS
  29. #include "gfx_widgets.h"
  30. #endif
  31. #ifdef HAVE_THREADS
  32. #include "video_thread_wrapper.h"
  33. #endif
  34. #ifdef HAVE_MENU
  35. #include "../menu/menu_driver.h"
  36. #endif
  37. #ifdef _WIN32
  38. #include "common/win32_common.h"
  39. #endif
  40. #include "../audio/audio_driver.h"
  41. #include "../frontend/frontend_driver.h"
  42. #include "../record/record_driver.h"
  43. #include "../ui/ui_companion_driver.h"
  44. #include "../driver.h"
  45. #include "../file_path_special.h"
  46. #include "../list_special.h"
  47. #include "../retroarch.h"
  48. #include "../verbosity.h"
  49. #include "gfx_display.h"
  50. #define TIME_TO_FPS(last_time, new_time, frames) ((1000000.0f * (frames)) / ((new_time) - (last_time)))
  51. typedef struct
  52. {
  53. struct string_list *list;
  54. enum gfx_ctx_api api;
  55. } gfx_api_gpu_map;
  56. static gfx_api_gpu_map gpu_map[] = {
  57. { NULL, GFX_CTX_VULKAN_API },
  58. { NULL, GFX_CTX_DIRECT3D10_API },
  59. { NULL, GFX_CTX_DIRECT3D11_API },
  60. { NULL, GFX_CTX_DIRECT3D12_API }
  61. };
  62. static const video_display_server_t dispserv_null = {
  63. NULL, /* init */
  64. NULL, /* destroy */
  65. NULL, /* set_window_opacity */
  66. NULL, /* set_window_progress */
  67. NULL, /* set_window_decorations */
  68. NULL, /* set_resolution */
  69. NULL, /* get_resolution_list */
  70. NULL, /* get_output_options */
  71. NULL, /* set_screen_orientation */
  72. NULL, /* get_screen_orientation */
  73. NULL, /* get_flags */
  74. "null"
  75. };
  76. #ifdef HAVE_VULKAN
  77. static const gfx_ctx_driver_t *gfx_ctx_vk_drivers[] = {
  78. #if defined(__APPLE__)
  79. &gfx_ctx_cocoavk,
  80. #endif
  81. #if defined(_WIN32) && !defined(__WINRT__)
  82. &gfx_ctx_w_vk,
  83. #endif
  84. #if defined(ANDROID)
  85. &gfx_ctx_vk_android,
  86. #endif
  87. #if defined(HAVE_WAYLAND)
  88. &gfx_ctx_vk_wayland,
  89. #endif
  90. #if defined(HAVE_X11)
  91. &gfx_ctx_vk_x,
  92. #endif
  93. #if defined(HAVE_VULKAN_DISPLAY)
  94. &gfx_ctx_khr_display,
  95. #endif
  96. &gfx_ctx_null,
  97. NULL
  98. };
  99. #endif
  100. static const gfx_ctx_driver_t *gfx_ctx_gl_drivers[] = {
  101. #if defined(ORBIS)
  102. &orbis_ctx,
  103. #endif
  104. #if defined(HAVE_VITAGL) | defined(HAVE_VITAGLES)
  105. &vita_ctx,
  106. #endif
  107. #if defined(__PS3__)
  108. &gfx_ctx_ps3,
  109. #endif
  110. #if defined(HAVE_LIBNX) && defined(HAVE_OPENGL)
  111. &switch_ctx,
  112. #endif
  113. #if defined(HAVE_VIDEOCORE)
  114. &gfx_ctx_videocore,
  115. #endif
  116. #if defined(HAVE_MALI_FBDEV)
  117. &gfx_ctx_mali_fbdev,
  118. #endif
  119. #if defined(HAVE_VIVANTE_FBDEV)
  120. &gfx_ctx_vivante_fbdev,
  121. #endif
  122. #if defined(HAVE_OPENDINGUX_FBDEV)
  123. &gfx_ctx_opendingux_fbdev,
  124. #endif
  125. #if defined(_WIN32) && !defined(__WINRT__) && (defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE))
  126. &gfx_ctx_wgl,
  127. #endif
  128. #if defined(__WINRT__) && defined(HAVE_OPENGLES)
  129. &gfx_ctx_uwp,
  130. #endif
  131. #if defined(HAVE_WAYLAND)
  132. &gfx_ctx_wayland,
  133. #endif
  134. #if defined(HAVE_X11) && !defined(HAVE_OPENGLES)
  135. #if defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE)
  136. &gfx_ctx_x,
  137. #endif
  138. #endif
  139. #if defined(HAVE_X11) && defined(HAVE_OPENGL) && defined(HAVE_EGL)
  140. &gfx_ctx_x_egl,
  141. #endif
  142. #if defined(HAVE_KMS)
  143. #if defined(HAVE_ODROIDGO2)
  144. &gfx_ctx_go2_drm,
  145. #endif
  146. &gfx_ctx_drm,
  147. #endif
  148. #if defined(ANDROID)
  149. &gfx_ctx_android,
  150. #endif
  151. #if defined(__QNX__)
  152. &gfx_ctx_qnx,
  153. #endif
  154. #if defined(HAVE_COCOA) || defined(HAVE_COCOATOUCH) || defined(HAVE_COCOA_METAL)
  155. #if defined(HAVE_OPENGL) || defined(HAVE_OPENGLES)
  156. &gfx_ctx_cocoagl,
  157. #endif
  158. #endif
  159. #if (defined(HAVE_SDL) || defined(HAVE_SDL2)) && (defined(HAVE_OPENGL) || defined(HAVE_OPENGL1) || defined(HAVE_OPENGL_CORE))
  160. &gfx_ctx_sdl_gl,
  161. #endif
  162. #ifdef HAVE_OSMESA
  163. &gfx_ctx_osmesa,
  164. #endif
  165. #ifdef EMSCRIPTEN
  166. &gfx_ctx_emscripten,
  167. #endif
  168. &gfx_ctx_null,
  169. NULL
  170. };
  171. /* Beware when changing this - it must match with enum aspect_ratio order */
  172. struct aspect_ratio_elem aspectratio_lut[ASPECT_RATIO_END] = {
  173. { 4.0f / 3.0f , "4:3" },
  174. { 16.0f / 9.0f , "16:9" },
  175. { 16.0f / 10.0f, "16:10" },
  176. { 16.0f / 15.0f, "16:15" },
  177. { 21.0f / 9.0f , "21:9" },
  178. { 1.0f / 1.0f , "1:1" },
  179. { 2.0f / 1.0f , "2:1" },
  180. { 3.0f / 2.0f , "3:2" },
  181. { 3.0f / 4.0f , "3:4" },
  182. { 4.0f / 1.0f , "4:1" },
  183. { 9.0f / 16.0f , "9:16" },
  184. { 5.0f / 4.0f , "5:4" },
  185. { 6.0f / 5.0f , "6:5" },
  186. { 7.0f / 9.0f , "7:9" },
  187. { 8.0f / 3.0f , "8:3" },
  188. { 8.0f / 7.0f , "8:7" },
  189. { 19.0f / 12.0f, "19:12" },
  190. { 19.0f / 14.0f, "19:14" },
  191. { 30.0f / 17.0f, "30:17" },
  192. { 32.0f / 9.0f , "32:9" },
  193. { 0.0f , "" }, /* config - initialized in video_driver_init_internal */
  194. { 1.0f , "" }, /* square pixel - initialized in video_driver_set_viewport_square_pixel */
  195. { 1.0f , "" }, /* core provided - initialized in video_driver_init_internal */
  196. { 0.0f , "" }, /* custom - initialized in video_driver_init_internal */
  197. { 4.0f / 3.0f , "" } /* full - initialized in video_driver_init_internal */
  198. };
  199. static void *video_null_init(const video_info_t *video,
  200. input_driver_t **input, void **input_data)
  201. {
  202. *input = NULL;
  203. *input_data = NULL;
  204. frontend_driver_install_signal_handler();
  205. return (void*)-1;
  206. }
  207. static bool video_null_frame(void *a, const void *b, unsigned c, unsigned d,
  208. uint64_t e,
  209. unsigned f, const char *g, video_frame_info_t *h) { return true; }
  210. static void video_null_free(void *a) { }
  211. static void video_null_set_nonblock_state(void *a, bool b, bool c, unsigned d) { }
  212. static bool video_null_alive(void *a) { return frontend_driver_get_signal_handler_state() != 1; }
  213. static bool video_null_focus(void *a) { return true; }
  214. static bool video_null_has_windowed(void *a) { return true; }
  215. static bool video_null_suppress_screensaver(void *a, bool b) { return false; }
  216. static bool video_null_set_shader(void *a, enum rarch_shader_type b, const char
  217. *c) { return false; }
  218. video_driver_t video_null = {
  219. video_null_init,
  220. video_null_frame,
  221. video_null_set_nonblock_state,
  222. video_null_alive,
  223. video_null_focus,
  224. video_null_suppress_screensaver,
  225. video_null_has_windowed,
  226. video_null_set_shader,
  227. video_null_free,
  228. "null",
  229. NULL, /* set_viewport */
  230. NULL, /* set_rotation */
  231. NULL, /* viewport_info */
  232. NULL, /* read_viewport */
  233. NULL, /* read_frame_raw */
  234. #ifdef HAVE_OVERLAY
  235. NULL, /* overlay_interface */
  236. #endif
  237. NULL, /* get_poke_interface */
  238. };
  239. const video_driver_t *video_drivers[] = {
  240. #ifdef HAVE_GCM
  241. &video_gcm,
  242. #endif
  243. #ifdef HAVE_VITA2D
  244. &video_vita2d,
  245. #endif
  246. #ifdef HAVE_VULKAN
  247. &video_vulkan,
  248. #endif
  249. #if defined(HAVE_OPENGL_CORE)
  250. &video_gl3,
  251. #endif
  252. #ifdef HAVE_OPENGL
  253. &video_gl2,
  254. #endif
  255. #ifdef HAVE_OPENGL1
  256. &video_gl1,
  257. #endif
  258. #ifdef HAVE_METAL
  259. &video_metal,
  260. #endif
  261. #ifdef XENON
  262. &video_xenon360,
  263. #endif
  264. #if defined(HAVE_D3D12)
  265. &video_d3d12,
  266. #endif
  267. #if defined(HAVE_D3D11)
  268. &video_d3d11,
  269. #endif
  270. #if defined(HAVE_D3D10)
  271. &video_d3d10,
  272. #endif
  273. #if defined(HAVE_D3D9)
  274. #if defined(HAVE_HLSL)
  275. &video_d3d9_hlsl,
  276. #endif
  277. #if defined(HAVE_CG)
  278. &video_d3d9_cg,
  279. #endif
  280. #endif
  281. #if defined(HAVE_D3D8)
  282. &video_d3d8,
  283. #endif
  284. #ifdef PSP
  285. &video_psp1,
  286. #endif
  287. #ifdef PS2
  288. &video_ps2,
  289. #endif
  290. #ifdef _3DS
  291. &video_ctr,
  292. #endif
  293. #ifdef SWITCH
  294. &video_switch,
  295. #endif
  296. #ifdef HAVE_ODROIDGO2
  297. &video_oga,
  298. #endif
  299. #if defined(HAVE_SDL) && !defined(HAVE_SDL_DINGUX)
  300. &video_sdl,
  301. #endif
  302. #ifdef HAVE_SDL2
  303. &video_sdl2,
  304. #endif
  305. #ifdef HAVE_SDL_DINGUX
  306. #if defined(RS90) || defined(MIYOO)
  307. &video_sdl_rs90,
  308. #else
  309. &video_sdl_dingux,
  310. #endif
  311. #endif
  312. #ifdef HAVE_XVIDEO
  313. &video_xvideo,
  314. #endif
  315. #ifdef GEKKO
  316. &video_gx,
  317. #endif
  318. #ifdef WIIU
  319. &video_wiiu,
  320. #endif
  321. #ifdef HAVE_VG
  322. &video_vg,
  323. #endif
  324. #ifdef HAVE_OMAP
  325. &video_omap,
  326. #endif
  327. #ifdef HAVE_EXYNOS
  328. &video_exynos,
  329. #endif
  330. #ifdef HAVE_DISPMANX
  331. &video_dispmanx,
  332. #endif
  333. #ifdef HAVE_SUNXI
  334. &video_sunxi,
  335. #endif
  336. #ifdef HAVE_PLAIN_DRM
  337. &video_drm,
  338. #endif
  339. #ifdef HAVE_XSHM
  340. &video_xshm,
  341. #endif
  342. #if defined(_WIN32) && !defined(_XBOX) && !defined(__WINRT__)
  343. #ifdef HAVE_GDI
  344. &video_gdi,
  345. #endif
  346. #endif
  347. #ifdef DJGPP
  348. &video_vga,
  349. #endif
  350. #ifdef HAVE_FPGA
  351. &video_fpga,
  352. #endif
  353. #ifdef HAVE_SIXEL
  354. &video_sixel,
  355. #endif
  356. #ifdef HAVE_CACA
  357. &video_caca,
  358. #endif
  359. #ifdef HAVE_NETWORK_VIDEO
  360. &video_network,
  361. #endif
  362. &video_null,
  363. NULL,
  364. };
  365. static video_driver_state_t video_driver_st = {};
  366. static const video_display_server_t *current_display_server =
  367. &dispserv_null;
  368. struct retro_hw_render_callback *video_driver_get_hw_context(void)
  369. {
  370. video_driver_state_t *video_st = &video_driver_st;
  371. return VIDEO_DRIVER_GET_HW_CONTEXT_INTERNAL(video_st);
  372. }
  373. video_driver_state_t *video_state_get_ptr(void)
  374. {
  375. return &video_driver_st;
  376. }
  377. void crt_switch_driver_refresh(void)
  378. {
  379. video_driver_reinit(DRIVER_VIDEO_MASK);
  380. }
  381. #ifdef HAVE_THREADS
  382. void *video_thread_get_ptr(video_driver_state_t *video_st)
  383. {
  384. const thread_video_t *thr = (const thread_video_t*)video_st->data;
  385. if (thr)
  386. return thr->driver_data;
  387. return NULL;
  388. }
  389. #endif
  390. /**
  391. * video_driver_get_ptr:
  392. *
  393. * Use this if you need the real video driver
  394. * and driver data pointers.
  395. *
  396. * Returns: video driver's userdata.
  397. **/
  398. void *video_driver_get_ptr(void)
  399. {
  400. video_driver_state_t *video_st = &video_driver_st;
  401. return VIDEO_DRIVER_GET_PTR_INTERNAL(video_st);
  402. }
  403. video_driver_t *hw_render_context_driver(
  404. enum retro_hw_context_type type, int major, int minor)
  405. {
  406. switch (type)
  407. {
  408. case RETRO_HW_CONTEXT_OPENGL_CORE:
  409. #ifdef HAVE_OPENGL_CORE
  410. return &video_gl3;
  411. #else
  412. break;
  413. #endif
  414. case RETRO_HW_CONTEXT_OPENGL:
  415. #ifdef HAVE_OPENGL
  416. return &video_gl2;
  417. #else
  418. break;
  419. #endif
  420. case RETRO_HW_CONTEXT_D3D10:
  421. #if defined(HAVE_D3D10)
  422. return &video_d3d10;
  423. #else
  424. break;
  425. #endif
  426. case RETRO_HW_CONTEXT_D3D11:
  427. #if defined(HAVE_D3D11)
  428. return &video_d3d11;
  429. #else
  430. break;
  431. #endif
  432. case RETRO_HW_CONTEXT_D3D12:
  433. #if defined(HAVE_D3D12)
  434. return &video_d3d12;
  435. #else
  436. break;
  437. #endif
  438. case RETRO_HW_CONTEXT_D3D9:
  439. #if defined(HAVE_D3D9) && defined(HAVE_HLSL)
  440. return &video_d3d9_hlsl;
  441. #else
  442. break;
  443. #endif
  444. case RETRO_HW_CONTEXT_VULKAN:
  445. #if defined(HAVE_VULKAN)
  446. return &video_vulkan;
  447. #else
  448. break;
  449. #endif
  450. default:
  451. case RETRO_HW_CONTEXT_NONE:
  452. break;
  453. }
  454. return NULL;
  455. }
  456. const char *hw_render_context_name(
  457. enum retro_hw_context_type type, int major, int minor)
  458. {
  459. #ifdef HAVE_OPENGL_CORE
  460. if (type == RETRO_HW_CONTEXT_OPENGL_CORE)
  461. return "glcore";
  462. #endif
  463. #ifdef HAVE_OPENGL
  464. switch (type)
  465. {
  466. case RETRO_HW_CONTEXT_OPENGLES2:
  467. case RETRO_HW_CONTEXT_OPENGLES3:
  468. case RETRO_HW_CONTEXT_OPENGLES_VERSION:
  469. case RETRO_HW_CONTEXT_OPENGL:
  470. #ifndef HAVE_OPENGL_CORE
  471. case RETRO_HW_CONTEXT_OPENGL_CORE:
  472. #endif
  473. return "gl";
  474. default:
  475. break;
  476. }
  477. #endif
  478. #ifdef HAVE_VULKAN
  479. if (type == RETRO_HW_CONTEXT_VULKAN)
  480. return "vulkan";
  481. #endif
  482. #if defined(HAVE_D3D9) && defined(HAVE_HLSL)
  483. if (type == RETRO_HW_CONTEXT_D3D9)
  484. return "d3d9_hlsl";
  485. #endif
  486. #ifdef HAVE_D3D10
  487. if (type == RETRO_HW_CONTEXT_D3D10)
  488. return "d3d10";
  489. #endif
  490. #ifdef HAVE_D3D11
  491. if (type == RETRO_HW_CONTEXT_D3D11)
  492. return "d3d11";
  493. #endif
  494. #ifdef HAVE_D3D12
  495. if (type == RETRO_HW_CONTEXT_D3D12)
  496. return "d3d12";
  497. #endif
  498. return "N/A";
  499. }
  500. enum retro_hw_context_type hw_render_context_type(const char *s)
  501. {
  502. #ifdef HAVE_OPENGL_CORE
  503. if (string_is_equal(s, "glcore"))
  504. return RETRO_HW_CONTEXT_OPENGL_CORE;
  505. #endif
  506. #ifdef HAVE_OPENGL
  507. if (string_is_equal(s, "gl"))
  508. return RETRO_HW_CONTEXT_OPENGL;
  509. #endif
  510. #ifdef HAVE_VULKAN
  511. if (string_is_equal(s, "vulkan"))
  512. return RETRO_HW_CONTEXT_VULKAN;
  513. #endif
  514. #if defined(HAVE_D3D9) && defined(HAVE_HLSL)
  515. if (string_is_equal(s, "d3d9_hlsl"))
  516. return RETRO_HW_CONTEXT_D3D9;
  517. #endif
  518. #ifdef HAVE_D3D10
  519. if (string_is_equal(s, "d3d10"))
  520. return RETRO_HW_CONTEXT_D3D10;
  521. #endif
  522. #ifdef HAVE_D3D11
  523. if (string_is_equal(s, "d3d11"))
  524. return RETRO_HW_CONTEXT_D3D11;
  525. #endif
  526. #ifdef HAVE_D3D12
  527. if (string_is_equal(s, "d3d12"))
  528. return RETRO_HW_CONTEXT_D3D12;
  529. #endif
  530. return RETRO_HW_CONTEXT_NONE;
  531. }
  532. /* string list stays owned by the caller and must be available at
  533. * all times after the video driver is inited */
  534. void video_driver_set_gpu_api_devices(
  535. enum gfx_ctx_api api, struct string_list *list)
  536. {
  537. int i;
  538. for (i = 0; i < ARRAY_SIZE(gpu_map); i++)
  539. {
  540. if (api == gpu_map[i].api)
  541. {
  542. gpu_map[i].list = list;
  543. break;
  544. }
  545. }
  546. }
  547. struct string_list* video_driver_get_gpu_api_devices(enum gfx_ctx_api api)
  548. {
  549. int i;
  550. for (i = 0; i < ARRAY_SIZE(gpu_map); i++)
  551. {
  552. if (api == gpu_map[i].api)
  553. return gpu_map[i].list;
  554. }
  555. return NULL;
  556. }
  557. /**
  558. * video_driver_translate_coord_viewport:
  559. * @mouse_x : Pointer X coordinate.
  560. * @mouse_y : Pointer Y coordinate.
  561. * @res_x : Scaled X coordinate.
  562. * @res_y : Scaled Y coordinate.
  563. * @res_screen_x : Scaled screen X coordinate.
  564. * @res_screen_y : Scaled screen Y coordinate.
  565. *
  566. * Translates pointer [X,Y] coordinates into scaled screen
  567. * coordinates based on viewport info.
  568. *
  569. * Returns: true (1) if successful, false if video driver doesn't support
  570. * viewport info.
  571. **/
  572. bool video_driver_translate_coord_viewport(
  573. struct video_viewport *vp,
  574. int mouse_x, int mouse_y,
  575. int16_t *res_x, int16_t *res_y,
  576. int16_t *res_screen_x, int16_t *res_screen_y)
  577. {
  578. int norm_vp_width = (int)vp->width;
  579. int norm_vp_height = (int)vp->height;
  580. int norm_full_vp_width = (int)vp->full_width;
  581. int norm_full_vp_height = (int)vp->full_height;
  582. int scaled_screen_x = -0x8000; /* OOB */
  583. int scaled_screen_y = -0x8000; /* OOB */
  584. int scaled_x = -0x8000; /* OOB */
  585. int scaled_y = -0x8000; /* OOB */
  586. if (norm_vp_width <= 0 ||
  587. norm_vp_height <= 0 ||
  588. norm_full_vp_width <= 0 ||
  589. norm_full_vp_height <= 0)
  590. return false;
  591. if (mouse_x >= 0 && mouse_x <= norm_full_vp_width)
  592. scaled_screen_x = ((2 * mouse_x * 0x7fff)
  593. / norm_full_vp_width) - 0x7fff;
  594. if (mouse_y >= 0 && mouse_y <= norm_full_vp_height)
  595. scaled_screen_y = ((2 * mouse_y * 0x7fff)
  596. / norm_full_vp_height) - 0x7fff;
  597. mouse_x -= vp->x;
  598. mouse_y -= vp->y;
  599. if (mouse_x >= 0 && mouse_x <= norm_vp_width)
  600. scaled_x = ((2 * mouse_x * 0x7fff)
  601. / norm_vp_width) - 0x7fff;
  602. else
  603. scaled_x = -0x8000; /* OOB */
  604. if (mouse_y >= 0 && mouse_y <= norm_vp_height)
  605. scaled_y = ((2 * mouse_y * 0x7fff)
  606. / norm_vp_height) - 0x7fff;
  607. *res_x = scaled_x;
  608. *res_y = scaled_y;
  609. *res_screen_x = scaled_screen_x;
  610. *res_screen_y = scaled_screen_y;
  611. return true;
  612. }
  613. void video_monitor_compute_fps_statistics(uint64_t
  614. frame_time_count)
  615. {
  616. double avg_fps = 0.0;
  617. double stddev = 0.0;
  618. unsigned samples = 0;
  619. if (frame_time_count <
  620. (2 * MEASURE_FRAME_TIME_SAMPLES_COUNT))
  621. {
  622. RARCH_LOG(
  623. "[Video]: Does not have enough samples for monitor refresh rate"
  624. " estimation. Requires to run for at least %u frames.\n",
  625. 2 * MEASURE_FRAME_TIME_SAMPLES_COUNT);
  626. return;
  627. }
  628. if (video_monitor_fps_statistics(&avg_fps, &stddev, &samples))
  629. {
  630. RARCH_LOG("[Video]: Average monitor Hz: %.6f Hz. (%.3f %% frame time"
  631. " deviation, based on %u last samples).\n",
  632. avg_fps, 100.0f * stddev, samples);
  633. }
  634. }
  635. void video_monitor_set_refresh_rate(float hz)
  636. {
  637. char msg[256];
  638. char rate[8];
  639. settings_t *settings = config_get_ptr();
  640. /* Avoid message spamming if there is no change. */
  641. if (settings->floats.video_refresh_rate == hz)
  642. return;
  643. snprintf(rate, sizeof(rate), "%.3f", hz);
  644. snprintf(msg, sizeof(msg),
  645. msg_hash_to_str(MSG_VIDEO_REFRESH_RATE_CHANGED), rate);
  646. /* Message is visible for twice the usual duration */
  647. /* as modeswitch will cause monitors to go blank for a while */
  648. if (settings->bools.notification_show_refresh_rate)
  649. runloop_msg_queue_push(msg, 1, 360, false, NULL,
  650. MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
  651. RARCH_LOG("[Video]: %s\n", msg);
  652. configuration_set_float(settings,
  653. settings->floats.video_refresh_rate,
  654. hz);
  655. }
  656. void video_driver_force_fallback(const char *driver)
  657. {
  658. settings_t *settings = config_get_ptr();
  659. ui_msg_window_t *msg_window = NULL;
  660. configuration_set_string(settings,
  661. settings->arrays.video_driver,
  662. driver);
  663. command_event(CMD_EVENT_MENU_SAVE_CURRENT_CONFIG, NULL);
  664. #if defined(_WIN32) && !defined(_XBOX) && !defined(__WINRT__) && !defined(WINAPI_FAMILY)
  665. /* UI companion driver is not inited yet, just call into it directly */
  666. msg_window = &ui_msg_window_win32;
  667. #endif
  668. if (msg_window)
  669. {
  670. char text[PATH_MAX_LENGTH];
  671. ui_msg_window_state window_state;
  672. char *title = strdup(msg_hash_to_str(MSG_ERROR));
  673. text[0] = '\0';
  674. snprintf(text, sizeof(text),
  675. msg_hash_to_str(MENU_ENUM_LABEL_VALUE_VIDEO_DRIVER_FALLBACK),
  676. driver);
  677. window_state.buttons = UI_MSG_WINDOW_OK;
  678. window_state.text = strdup(text);
  679. window_state.title = title;
  680. window_state.window = NULL;
  681. msg_window->error(&window_state);
  682. free(title);
  683. }
  684. exit(1);
  685. }
  686. static bool get_metrics_null(void *data, enum display_metric_types type,
  687. float *value) { return false; }
  688. static bool video_context_driver_get_metrics_null(
  689. void *data, enum display_metric_types type,
  690. float *value) { return false; }
  691. void video_context_driver_destroy(gfx_ctx_driver_t *ctx_driver)
  692. {
  693. if (!ctx_driver)
  694. return;
  695. ctx_driver->init = NULL;
  696. ctx_driver->bind_api = NULL;
  697. ctx_driver->swap_interval = NULL;
  698. ctx_driver->set_video_mode = NULL;
  699. ctx_driver->get_video_size = NULL;
  700. ctx_driver->get_video_output_size = NULL;
  701. ctx_driver->get_video_output_prev = NULL;
  702. ctx_driver->get_video_output_next = NULL;
  703. ctx_driver->get_metrics =
  704. video_context_driver_get_metrics_null;
  705. ctx_driver->translate_aspect = NULL;
  706. ctx_driver->update_window_title = NULL;
  707. ctx_driver->check_window = NULL;
  708. ctx_driver->set_resize = NULL;
  709. ctx_driver->suppress_screensaver = NULL;
  710. ctx_driver->swap_buffers = NULL;
  711. ctx_driver->input_driver = NULL;
  712. ctx_driver->get_proc_address = NULL;
  713. ctx_driver->image_buffer_init = NULL;
  714. ctx_driver->image_buffer_write = NULL;
  715. ctx_driver->show_mouse = NULL;
  716. ctx_driver->ident = NULL;
  717. ctx_driver->get_flags = NULL;
  718. ctx_driver->set_flags = NULL;
  719. ctx_driver->bind_hw_render = NULL;
  720. ctx_driver->get_context_data = NULL;
  721. ctx_driver->make_current = NULL;
  722. }
  723. const gfx_ctx_driver_t *video_context_driver_init(
  724. bool core_set_shared_context,
  725. settings_t *settings,
  726. void *data,
  727. const gfx_ctx_driver_t *ctx,
  728. const char *ident,
  729. enum gfx_ctx_api api, unsigned major,
  730. unsigned minor, bool hw_render_ctx,
  731. void **ctx_data)
  732. {
  733. if (!ctx->bind_api(data, api, major, minor))
  734. {
  735. RARCH_WARN("Failed to bind API (#%u, version %u.%u)"
  736. " on context driver \"%s\".\n",
  737. (unsigned)api, major, minor, ctx->ident);
  738. return NULL;
  739. }
  740. if (!(*ctx_data = ctx->init(data)))
  741. return NULL;
  742. if (ctx->bind_hw_render)
  743. {
  744. bool video_shared_context =
  745. settings->bools.video_shared_context || core_set_shared_context;
  746. ctx->bind_hw_render(*ctx_data,
  747. video_shared_context && hw_render_ctx);
  748. }
  749. return ctx;
  750. }
  751. /**
  752. * config_get_video_driver_options:
  753. *
  754. * Get an enumerated list of all video driver names, separated by '|'.
  755. *
  756. * Returns: string listing of all video driver names, separated by '|'.
  757. **/
  758. const char* config_get_video_driver_options(void)
  759. {
  760. return char_list_new_special(STRING_LIST_VIDEO_DRIVERS, NULL);
  761. }
  762. void video_driver_pixel_converter_free(
  763. video_pixel_scaler_t *scalr)
  764. {
  765. if (!scalr)
  766. return;
  767. if (scalr->scaler)
  768. {
  769. scaler_ctx_gen_reset(scalr->scaler);
  770. free(scalr->scaler);
  771. }
  772. if (scalr->scaler_out)
  773. free(scalr->scaler_out);
  774. scalr->scaler = NULL;
  775. scalr->scaler_out = NULL;
  776. free(scalr);
  777. }
  778. video_pixel_scaler_t *video_driver_pixel_converter_init(
  779. const enum retro_pixel_format video_driver_pix_fmt,
  780. struct retro_hw_render_callback *hwr,
  781. unsigned size)
  782. {
  783. void *scalr_out = NULL;
  784. video_pixel_scaler_t *scalr = NULL;
  785. struct scaler_ctx *scalr_ctx = NULL;
  786. /* If pixel format is not 0RGB1555, we don't need to do
  787. * any internal pixel conversion. */
  788. if (video_driver_pix_fmt != RETRO_PIXEL_FORMAT_0RGB1555)
  789. return NULL;
  790. /* No need to perform pixel conversion for HW rendering contexts. */
  791. if (hwr && hwr->context_type != RETRO_HW_CONTEXT_NONE)
  792. return NULL;
  793. RARCH_WARN("[Video]: 0RGB1555 pixel format is deprecated,"
  794. " and will be slower. For 15/16-bit, RGB565"
  795. " format is preferred.\n");
  796. if (!(scalr = (video_pixel_scaler_t*)malloc(sizeof(*scalr))))
  797. goto error;
  798. scalr->scaler = NULL;
  799. scalr->scaler_out = NULL;
  800. if (!(scalr_ctx = (struct scaler_ctx*)calloc(1, sizeof(*scalr_ctx))))
  801. goto error;
  802. scalr->scaler = scalr_ctx;
  803. scalr->scaler->scaler_type = SCALER_TYPE_POINT;
  804. scalr->scaler->in_fmt = SCALER_FMT_0RGB1555;
  805. /* TODO/FIXME: Pick either ARGB8888 or RGB565 depending on driver. */
  806. scalr->scaler->out_fmt = SCALER_FMT_RGB565;
  807. if (!scaler_ctx_gen_filter(scalr_ctx))
  808. goto error;
  809. if (!(scalr_out = calloc(sizeof(uint16_t), size * size)))
  810. goto error;
  811. scalr->scaler_out = scalr_out;
  812. return scalr;
  813. error:
  814. video_driver_pixel_converter_free(scalr);
  815. #ifdef HAVE_VIDEO_FILTER
  816. video_driver_filter_free();
  817. #endif
  818. return NULL;
  819. }
  820. struct video_viewport *video_viewport_get_custom(void)
  821. {
  822. return &config_get_ptr()->video_viewport_custom;
  823. }
  824. bool video_driver_monitor_adjust_system_rates(
  825. float timing_skew_hz,
  826. float video_refresh_rate,
  827. bool vrr_runloop_enable,
  828. float audio_max_timing_skew,
  829. unsigned video_swap_interval,
  830. double input_fps)
  831. {
  832. settings_t *settings = config_get_ptr();
  833. float target_video_sync_rate = timing_skew_hz;
  834. /* Divide target rate only when using Auto interval */
  835. if (settings->uints.video_swap_interval == 0)
  836. target_video_sync_rate /= (float)video_swap_interval;
  837. if (!vrr_runloop_enable)
  838. {
  839. float timing_skew = fabs(
  840. 1.0f - input_fps / target_video_sync_rate);
  841. /* We don't want to adjust pitch too much. If we have extreme cases,
  842. * just don't readjust at all. */
  843. if (timing_skew <= audio_max_timing_skew)
  844. return true;
  845. RARCH_LOG("[Video]: Timings deviate too much. Will not adjust."
  846. " (Target = %.2f Hz, Game = %.2f Hz)\n",
  847. target_video_sync_rate,
  848. (float)input_fps);
  849. }
  850. return input_fps <= target_video_sync_rate;
  851. }
  852. void video_driver_reset_custom_viewport(settings_t *settings)
  853. {
  854. struct video_viewport *custom_vp = &settings->video_viewport_custom;
  855. custom_vp->width = 0;
  856. custom_vp->height = 0;
  857. custom_vp->x = 0;
  858. custom_vp->y = 0;
  859. }
  860. struct retro_system_av_info *video_viewport_get_system_av_info(void)
  861. {
  862. return &video_driver_st.av_info;
  863. }
  864. void video_driver_gpu_record_deinit(void)
  865. {
  866. video_driver_state_t *video_st = &video_driver_st;
  867. if (video_st->record_gpu_buffer)
  868. free(video_st->record_gpu_buffer);
  869. video_st->record_gpu_buffer = NULL;
  870. }
  871. void recording_dump_frame(
  872. const void *data, unsigned width,
  873. unsigned height, size_t pitch, bool is_idle)
  874. {
  875. struct record_video_data ffemu_data;
  876. video_driver_state_t *video_st = &video_driver_st;
  877. recording_state_t *record_st = recording_state_get_ptr();
  878. ffemu_data.data = data;
  879. ffemu_data.width = width;
  880. ffemu_data.height = height;
  881. ffemu_data.pitch = (int)pitch;
  882. ffemu_data.is_dupe = false;
  883. if (video_st->record_gpu_buffer)
  884. {
  885. struct video_viewport vp;
  886. vp.x = 0;
  887. vp.y = 0;
  888. vp.width = 0;
  889. vp.height = 0;
  890. vp.full_width = 0;
  891. vp.full_height = 0;
  892. if (video_st->current_video && video_st->current_video->viewport_info)
  893. video_st->current_video->viewport_info(video_st->data, &vp);
  894. if (!vp.width || !vp.height)
  895. {
  896. RARCH_WARN("[Recording]: %s\n",
  897. msg_hash_to_str(MSG_VIEWPORT_SIZE_CALCULATION_FAILED));
  898. video_driver_gpu_record_deinit();
  899. recording_dump_frame(
  900. data, width, height, pitch, is_idle);
  901. return;
  902. }
  903. /* User has resized. We kinda have a problem now. */
  904. if ( vp.width != record_st->gpu_width ||
  905. vp.height != record_st->gpu_height)
  906. {
  907. const char *recording_failed_str =
  908. msg_hash_to_str(MSG_RECORDING_TERMINATED_DUE_TO_RESIZE);
  909. RARCH_WARN("[Recording]: %s\n", recording_failed_str);
  910. runloop_msg_queue_push(recording_failed_str,
  911. 1, 180, true,
  912. NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
  913. command_event(CMD_EVENT_RECORD_DEINIT, NULL);
  914. return;
  915. }
  916. /* Big bottleneck.
  917. * Since we might need to do read-backs asynchronously,
  918. * it might take 3-4 times before this returns true. */
  919. if (!video_driver_read_viewport(video_st->record_gpu_buffer, is_idle))
  920. return;
  921. ffemu_data.pitch = (int)(record_st->gpu_width * 3);
  922. ffemu_data.width = (unsigned)record_st->gpu_width;
  923. ffemu_data.height = (unsigned)record_st->gpu_height;
  924. ffemu_data.data = video_st->record_gpu_buffer + (ffemu_data.height - 1) * ffemu_data.pitch;
  925. ffemu_data.pitch = -ffemu_data.pitch;
  926. }
  927. else
  928. ffemu_data.is_dupe = !data;
  929. record_st->driver->push_video(record_st->data, &ffemu_data);
  930. }
  931. const char *video_display_server_get_ident(void)
  932. {
  933. if (!current_display_server)
  934. return FILE_PATH_UNKNOWN;
  935. return current_display_server->ident;
  936. }
  937. void* video_display_server_init(enum rarch_display_type type)
  938. {
  939. video_driver_state_t *video_st = &video_driver_st;
  940. video_display_server_destroy();
  941. switch (type)
  942. {
  943. case RARCH_DISPLAY_WIN32:
  944. #if defined(_WIN32) && !defined(_XBOX) && !defined(__WINRT__)
  945. current_display_server = &dispserv_win32;
  946. #endif
  947. break;
  948. case RARCH_DISPLAY_X11:
  949. #if defined(HAVE_X11)
  950. current_display_server = &dispserv_x11;
  951. #endif
  952. break;
  953. case RARCH_DISPLAY_KMS:
  954. #if defined(HAVE_KMS)
  955. current_display_server = &dispserv_kms;
  956. #endif
  957. break;
  958. default:
  959. #if defined(ANDROID)
  960. current_display_server = &dispserv_android;
  961. #else
  962. current_display_server = &dispserv_null;
  963. #endif
  964. break;
  965. }
  966. if (current_display_server)
  967. {
  968. if (current_display_server->init)
  969. video_st->current_display_server_data = current_display_server->init();
  970. if (!string_is_empty(current_display_server->ident))
  971. {
  972. RARCH_LOG("[Video]: Found display server: \"%s\".\n",
  973. current_display_server->ident);
  974. }
  975. }
  976. video_st->initial_screen_orientation =
  977. video_display_server_get_screen_orientation();
  978. video_st->current_screen_orientation =
  979. video_st->initial_screen_orientation;
  980. return video_st->current_display_server_data;
  981. }
  982. void video_display_server_destroy(void)
  983. {
  984. video_driver_state_t *video_st = &video_driver_st;
  985. const enum rotation initial_screen_orientation = video_st->initial_screen_orientation;
  986. const enum rotation current_screen_orientation = video_st->current_screen_orientation;
  987. if (initial_screen_orientation != current_screen_orientation)
  988. video_display_server_set_screen_orientation(initial_screen_orientation);
  989. if (current_display_server)
  990. if (video_st->current_display_server_data)
  991. current_display_server->destroy(video_st->current_display_server_data);
  992. }
  993. bool video_display_server_set_window_opacity(unsigned opacity)
  994. {
  995. video_driver_state_t *video_st = &video_driver_st;
  996. if (current_display_server && current_display_server->set_window_opacity)
  997. return current_display_server->set_window_opacity(
  998. video_st->current_display_server_data, opacity);
  999. return false;
  1000. }
  1001. bool video_display_server_set_window_progress(int progress, bool finished)
  1002. {
  1003. video_driver_state_t *video_st = &video_driver_st;
  1004. if (current_display_server && current_display_server->set_window_progress)
  1005. return current_display_server->set_window_progress(
  1006. video_st->current_display_server_data, progress, finished);
  1007. return false;
  1008. }
  1009. bool video_display_server_set_window_decorations(bool on)
  1010. {
  1011. video_driver_state_t *video_st = &video_driver_st;
  1012. if (current_display_server && current_display_server->set_window_decorations)
  1013. return current_display_server->set_window_decorations(
  1014. video_st->current_display_server_data, on);
  1015. return false;
  1016. }
  1017. bool video_display_server_set_resolution(unsigned width, unsigned height,
  1018. int int_hz, float hz, int center, int monitor_index, int xoffset, int padjust)
  1019. {
  1020. video_driver_state_t *video_st = &video_driver_st;
  1021. RARCH_DBG("[Video]: Display server set resolution, hz: %f\n",hz);
  1022. if (current_display_server && current_display_server->set_resolution)
  1023. return current_display_server->set_resolution(
  1024. video_st->current_display_server_data, width, height, int_hz,
  1025. hz, center, monitor_index, xoffset, padjust);
  1026. return false;
  1027. }
  1028. bool video_display_server_has_resolution_list(void)
  1029. {
  1030. return (current_display_server
  1031. && current_display_server->get_resolution_list);
  1032. }
  1033. void *video_display_server_get_resolution_list(unsigned *size)
  1034. {
  1035. video_driver_state_t *video_st = &video_driver_st;
  1036. if (video_display_server_has_resolution_list())
  1037. return current_display_server->get_resolution_list(
  1038. video_st->current_display_server_data, size);
  1039. return NULL;
  1040. }
  1041. bool video_display_server_has_refresh_rate(float hz)
  1042. {
  1043. unsigned i, size = 0;
  1044. bool rate_exists = false;
  1045. struct video_display_config *video_list = (struct video_display_config*)
  1046. video_display_server_get_resolution_list(&size);
  1047. if (video_list)
  1048. {
  1049. video_driver_state_t *video_st = &video_driver_st;
  1050. unsigned video_driver_width = video_st->width;
  1051. unsigned video_driver_height = video_st->height;
  1052. for (i = 0; i < size && !rate_exists; i++)
  1053. {
  1054. if (video_list[i].width == video_driver_width &&
  1055. video_list[i].height == video_driver_height &&
  1056. video_list[i].refreshrate == floor(hz))
  1057. rate_exists = true;
  1058. }
  1059. free(video_list);
  1060. }
  1061. return rate_exists;
  1062. }
  1063. void video_switch_refresh_rate_maybe(
  1064. float *refresh_rate_suggest,
  1065. bool *video_switch_refresh_rate)
  1066. {
  1067. settings_t *settings = config_get_ptr();
  1068. video_driver_state_t *video_st = video_state_get_ptr();
  1069. float refresh_rate = *refresh_rate_suggest;
  1070. float video_refresh_rate = settings->floats.video_refresh_rate;
  1071. unsigned crt_switch_resolution = settings->uints.crt_switch_resolution;
  1072. unsigned autoswitch_refresh_rate = settings->uints.video_autoswitch_refresh_rate;
  1073. unsigned video_swap_interval = runloop_get_video_swap_interval(
  1074. settings->uints.video_swap_interval);
  1075. unsigned video_bfi = settings->uints.video_black_frame_insertion;
  1076. bool vrr_runloop_enable = settings->bools.vrr_runloop_enable;
  1077. bool exclusive_fullscreen = settings->bools.video_fullscreen && !settings->bools.video_windowed_fullscreen;
  1078. bool windowed_fullscreen = settings->bools.video_fullscreen && settings->bools.video_windowed_fullscreen;
  1079. bool all_fullscreen = settings->bools.video_fullscreen || settings->bools.video_windowed_fullscreen;
  1080. /* Roundings to PAL & NTSC standards */
  1081. if (refresh_rate > 49.00 && refresh_rate < 54.50)
  1082. refresh_rate = 50.00f;
  1083. else if (refresh_rate > 54.00 && refresh_rate < 60.00)
  1084. refresh_rate = 59.94f;
  1085. else if (refresh_rate > 60.00 && refresh_rate < 61.00)
  1086. refresh_rate = 60.00f;
  1087. /* Black frame insertion + swap interval multiplier */
  1088. refresh_rate = (refresh_rate * (video_bfi + 1.0f) * video_swap_interval);
  1089. /* Fallback when target refresh rate is not exposed or when below standards */
  1090. if (!video_display_server_has_refresh_rate(refresh_rate) || refresh_rate < 50)
  1091. refresh_rate = video_refresh_rate;
  1092. /* Replace target rate */
  1093. *refresh_rate_suggest = refresh_rate;
  1094. /* Try to switch display rate for the desired screen mode(s) when:
  1095. * - Not already at correct rate
  1096. * - 'CRT SwitchRes' OFF & 'Sync to Exact Content Framerate' OFF
  1097. * - Automatic refresh rate switching not OFF
  1098. */
  1099. if ( refresh_rate != video_refresh_rate
  1100. && !crt_switch_resolution
  1101. && !vrr_runloop_enable
  1102. && (autoswitch_refresh_rate != AUTOSWITCH_REFRESH_RATE_OFF))
  1103. {
  1104. *video_switch_refresh_rate = (
  1105. ((autoswitch_refresh_rate == AUTOSWITCH_REFRESH_RATE_EXCLUSIVE_FULLSCREEN) && exclusive_fullscreen) ||
  1106. ((autoswitch_refresh_rate == AUTOSWITCH_REFRESH_RATE_WINDOWED_FULLSCREEN) && windowed_fullscreen) ||
  1107. ((autoswitch_refresh_rate == AUTOSWITCH_REFRESH_RATE_ALL_FULLSCREEN) && all_fullscreen));
  1108. /* Store original refresh rate on automatic change, and
  1109. * restore it in deinit_core and main_quit, because not all
  1110. * cores announce refresh rate via SET_SYSTEM_AV_INFO */
  1111. if ( *video_switch_refresh_rate
  1112. && !video_st->video_refresh_rate_original)
  1113. video_st->video_refresh_rate_original = video_refresh_rate;
  1114. }
  1115. }
  1116. bool video_display_server_set_refresh_rate(float hz)
  1117. {
  1118. video_driver_state_t *video_st = &video_driver_st;
  1119. RARCH_DBG("[Video]: Display server set refresh rate %f\n", hz);
  1120. if (current_display_server && current_display_server->set_resolution)
  1121. return current_display_server->set_resolution(
  1122. video_st->current_display_server_data, 0, 0, (int)hz,
  1123. hz, 0, 0, 0, 0);
  1124. return false;
  1125. }
  1126. void video_display_server_restore_refresh_rate(void)
  1127. {
  1128. video_driver_state_t *video_st = &video_driver_st;
  1129. settings_t *settings = config_get_ptr();
  1130. float refresh_rate_original = video_st->video_refresh_rate_original;
  1131. float refresh_rate_current = settings->floats.video_refresh_rate;
  1132. if (!refresh_rate_original || refresh_rate_current == refresh_rate_original)
  1133. return;
  1134. RARCH_DBG("[Video]: Display server restore refresh rate %f\n", refresh_rate_original);
  1135. video_monitor_set_refresh_rate(refresh_rate_original);
  1136. video_display_server_set_refresh_rate(refresh_rate_original);
  1137. }
  1138. const char *video_display_server_get_output_options(void)
  1139. {
  1140. video_driver_state_t *video_st = &video_driver_st;
  1141. if (current_display_server && current_display_server->get_output_options)
  1142. return current_display_server->get_output_options(
  1143. video_st->current_display_server_data);
  1144. return NULL;
  1145. }
  1146. void video_display_server_set_screen_orientation(enum rotation rotation)
  1147. {
  1148. video_driver_state_t *video_st = &video_driver_st;
  1149. if (current_display_server && current_display_server->set_screen_orientation)
  1150. {
  1151. RARCH_LOG("[Video]: Setting screen orientation to %d.\n", rotation);
  1152. video_st->current_screen_orientation = rotation;
  1153. current_display_server->set_screen_orientation(video_st->current_display_server_data, rotation);
  1154. }
  1155. }
  1156. bool video_display_server_can_set_screen_orientation(void)
  1157. {
  1158. return (current_display_server && current_display_server->set_screen_orientation);
  1159. }
  1160. enum rotation video_display_server_get_screen_orientation(void)
  1161. {
  1162. video_driver_state_t *video_st = &video_driver_st;
  1163. if (current_display_server && current_display_server->get_screen_orientation)
  1164. return current_display_server->get_screen_orientation(
  1165. video_st->current_display_server_data);
  1166. return ORIENTATION_NORMAL;
  1167. }
  1168. bool video_display_server_get_flags(gfx_ctx_flags_t *flags)
  1169. {
  1170. video_driver_state_t *video_st = &video_driver_st;
  1171. if (!flags || !current_display_server || !current_display_server->get_flags)
  1172. return false;
  1173. flags->flags = current_display_server->get_flags(
  1174. video_st->current_display_server_data);
  1175. return true;
  1176. }
  1177. bool video_driver_is_threaded(void)
  1178. {
  1179. #ifdef HAVE_THREADS
  1180. video_driver_state_t *video_st = &video_driver_st;
  1181. return VIDEO_DRIVER_IS_THREADED_INTERNAL(video_st);
  1182. #else
  1183. return false;
  1184. #endif
  1185. }
  1186. bool *video_driver_get_threaded(void)
  1187. {
  1188. video_driver_state_t *video_st = &video_driver_st;
  1189. #if defined(__MACH__) && defined(__APPLE__)
  1190. /* TODO/FIXME - force threaded video to disabled on Apple for now
  1191. * until NSWindow/UIWindow concurrency issues are taken care of */
  1192. video_st->threaded = false;
  1193. #endif
  1194. return &video_st->threaded;
  1195. }
  1196. void video_driver_set_threaded(bool val)
  1197. {
  1198. video_driver_state_t *video_st = &video_driver_st;
  1199. #if defined(__MACH__) && defined(__APPLE__)
  1200. /* TODO/FIXME - force threaded video to disabled on Apple for now
  1201. * until NSWindow/UIWindow concurrency issues are taken care of */
  1202. video_st->threaded = false;
  1203. #else
  1204. video_st->threaded = val;
  1205. #endif
  1206. }
  1207. const char *video_driver_get_ident(void)
  1208. {
  1209. video_driver_state_t *video_st = &video_driver_st;
  1210. if (!video_st->current_video)
  1211. return NULL;
  1212. #ifdef HAVE_THREADS
  1213. if (VIDEO_DRIVER_IS_THREADED_INTERNAL(video_st))
  1214. {
  1215. const thread_video_t *thr = (const thread_video_t*)video_st->data;
  1216. if (!thr || !thr->driver)
  1217. return NULL;
  1218. return thr->driver->ident;
  1219. }
  1220. #endif
  1221. return video_st->current_video->ident;
  1222. }
  1223. void video_context_driver_reset(void)
  1224. {
  1225. video_driver_state_t *video_st = &video_driver_st;
  1226. if (!video_st->current_video_context.get_metrics)
  1227. video_st->current_video_context.get_metrics = get_metrics_null;
  1228. }
  1229. bool video_context_driver_set(const gfx_ctx_driver_t *data)
  1230. {
  1231. video_driver_state_t *video_st = &video_driver_st;
  1232. if (!data)
  1233. return false;
  1234. video_st->current_video_context = *data;
  1235. video_context_driver_reset();
  1236. return true;
  1237. }
  1238. uintptr_t video_driver_get_current_framebuffer(void)
  1239. {
  1240. video_driver_state_t *video_st = &video_driver_st;
  1241. if ( video_st->poke
  1242. && video_st->poke->get_current_framebuffer)
  1243. return video_st->poke->get_current_framebuffer(video_st->data);
  1244. return 0;
  1245. }
  1246. retro_proc_address_t video_driver_get_proc_address(const char *sym)
  1247. {
  1248. video_driver_state_t *video_st = &video_driver_st;
  1249. if ( video_st->poke
  1250. && video_st->poke->get_proc_address)
  1251. return video_st->poke->get_proc_address(video_st->data, sym);
  1252. return NULL;
  1253. }
  1254. #ifdef HAVE_VIDEO_FILTER
  1255. void video_driver_filter_free(void)
  1256. {
  1257. RARCH_LOG("[VideoFilter]: video_driver_filter_free");
  1258. video_driver_state_t *video_st = &video_driver_st;
  1259. if (video_st->state_filter)
  1260. rarch_softfilter_free(video_st->state_filter);
  1261. video_st->state_filter = NULL;
  1262. if (video_st->state_buffer)
  1263. {
  1264. #ifdef _3DS
  1265. linearFree(video_st->state_buffer);
  1266. #else
  1267. free(video_st->state_buffer);
  1268. #endif
  1269. }
  1270. video_st->state_buffer = NULL;
  1271. video_st->state_scale = 0;
  1272. video_st->state_out_bpp = 0;
  1273. video_st->flags &= ~(VIDEO_FLAG_STATE_OUT_RGB32);
  1274. }
  1275. void video_driver_filter_init(enum retro_pixel_format colfmt_int,
  1276. settings_t *settings)
  1277. {
  1278. RARCH_LOG("[VideoFilter]: video_driver_filter_init");
  1279. unsigned pow2_x, pow2_y, maxsize;
  1280. void *buf = NULL;
  1281. video_driver_state_t *video_st = &video_driver_st;
  1282. struct retro_game_geometry *geom = &video_st->av_info.geometry;
  1283. unsigned width = geom->max_width;
  1284. unsigned height = geom->max_height;
  1285. /* Deprecated format. Gets pre-converted. */
  1286. enum retro_pixel_format colfmt =
  1287. (colfmt_int == RETRO_PIXEL_FORMAT_0RGB1555) ?
  1288. RETRO_PIXEL_FORMAT_RGB565 : colfmt_int;
  1289. if (video_driver_is_hw_context())
  1290. {
  1291. RARCH_WARN("[Video]: Cannot use CPU filters when hardware rendering is used.\n");
  1292. return;
  1293. }
  1294. video_st->state_filter = rarch_softfilter_new(
  1295. settings->paths.path_softfilter_plugin,
  1296. RARCH_SOFTFILTER_THREADS_AUTO, colfmt, width, height);
  1297. if (!video_st->state_filter)
  1298. {
  1299. RARCH_ERR("[Video]: Failed to load filter.\n");
  1300. return;
  1301. }
  1302. rarch_softfilter_get_max_output_size(
  1303. video_st->state_filter,
  1304. &width, &height);
  1305. pow2_x = next_pow2(width);
  1306. pow2_y = next_pow2(height);
  1307. maxsize = MAX(pow2_x, pow2_y);
  1308. #ifdef _3DS
  1309. /* On 3DS, video is disabled if the output resolution
  1310. * exceeds 2048x2048. To avoid the user being presented
  1311. * with a black screen, we therefore have to check that
  1312. * the filter upscaling buffer fits within this limit. */
  1313. if (maxsize >= 2048)
  1314. {
  1315. RARCH_ERR("[Video]: Softfilter initialization failed."
  1316. " Upscaling buffer exceeds hardware limitations.\n");
  1317. video_driver_filter_free();
  1318. return;
  1319. }
  1320. #endif
  1321. video_st->state_scale = maxsize / RARCH_SCALE_BASE;
  1322. if (rarch_softfilter_get_output_format(
  1323. video_st->state_filter) == RETRO_PIXEL_FORMAT_XRGB8888)
  1324. video_st->flags |= VIDEO_FLAG_STATE_OUT_RGB32;
  1325. else
  1326. video_st->flags &= ~VIDEO_FLAG_STATE_OUT_RGB32;
  1327. video_st->state_out_bpp = (video_st->flags & VIDEO_FLAG_STATE_OUT_RGB32)
  1328. ? sizeof(uint32_t) : sizeof(uint16_t);
  1329. /* TODO: Aligned output. */
  1330. #ifdef _3DS
  1331. buf = linearMemAlign(
  1332. width * height * video_st->state_out_bpp, 0x80);
  1333. #else
  1334. buf = malloc(
  1335. width * height * video_st->state_out_bpp);
  1336. #endif
  1337. if (!buf)
  1338. {
  1339. RARCH_ERR("[Video]: Softfilter initialization failed.\n");
  1340. video_driver_filter_free();
  1341. return;
  1342. }
  1343. video_st->state_buffer = buf;
  1344. }
  1345. #endif
  1346. void video_driver_free_hw_context(void)
  1347. {
  1348. video_driver_state_t *video_st = &video_driver_st;
  1349. VIDEO_DRIVER_CONTEXT_LOCK(video_st);
  1350. if (video_st->hw_render.context_destroy)
  1351. video_st->hw_render.context_destroy();
  1352. memset(&video_st->hw_render, 0, sizeof(video_st->hw_render));
  1353. VIDEO_DRIVER_CONTEXT_UNLOCK(video_st);
  1354. video_st->hw_render_context_negotiation = NULL;
  1355. }
  1356. void video_driver_free_internal(void)
  1357. {
  1358. input_driver_state_t *input_st = input_state_get_ptr();
  1359. video_driver_state_t *video_st = &video_driver_st;
  1360. #ifdef HAVE_THREADS
  1361. bool is_threaded = VIDEO_DRIVER_IS_THREADED_INTERNAL(video_st);
  1362. #endif
  1363. #ifdef HAVE_OVERLAY
  1364. command_event(CMD_EVENT_OVERLAY_DEINIT, NULL);
  1365. #endif
  1366. if (!(video_st->flags & VIDEO_FLAG_CACHE_CONTEXT))
  1367. video_driver_free_hw_context();
  1368. if (input_st->current_data != video_st->data) {
  1369. video_driver_free_input();
  1370. }
  1371. if ( video_st->data
  1372. && video_st->current_video
  1373. && video_st->current_video->free)
  1374. video_st->current_video->free(video_st->data);
  1375. if (video_st->scaler_ptr)
  1376. video_driver_pixel_converter_free(video_st->scaler_ptr);
  1377. video_st->scaler_ptr = NULL;
  1378. #ifdef HAVE_VIDEO_FILTER
  1379. video_driver_filter_free();
  1380. #endif
  1381. #if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_SLANG) || defined(HAVE_HLSL)
  1382. video_shader_dir_free_shader(
  1383. (struct rarch_dir_shader_list*)&video_st->dir_shader_list,
  1384. config_get_ptr()->bools.video_shader_remember_last_dir);
  1385. #endif
  1386. #ifdef HAVE_THREADS
  1387. if (is_threaded)
  1388. return;
  1389. #endif
  1390. if (video_st->data)
  1391. video_monitor_compute_fps_statistics(video_st->frame_time_count);
  1392. }
  1393. void video_driver_set_viewport_config(
  1394. struct retro_game_geometry *geom,
  1395. float video_aspect_ratio,
  1396. bool video_aspect_ratio_auto)
  1397. {
  1398. if (video_aspect_ratio < 0.0f)
  1399. {
  1400. if (geom->aspect_ratio > 0.0f && video_aspect_ratio_auto)
  1401. aspectratio_lut[ASPECT_RATIO_CONFIG].value = geom->aspect_ratio;
  1402. else
  1403. {
  1404. unsigned base_width = geom->base_width;
  1405. unsigned base_height = geom->base_height;
  1406. /* Get around division by zero errors */
  1407. if (base_width == 0)
  1408. base_width = 1;
  1409. if (base_height == 0)
  1410. base_height = 1;
  1411. aspectratio_lut[ASPECT_RATIO_CONFIG].value =
  1412. (float)base_width / base_height; /* 1:1 PAR. */
  1413. }
  1414. }
  1415. else
  1416. aspectratio_lut[ASPECT_RATIO_CONFIG].value = video_aspect_ratio;
  1417. }
  1418. void video_driver_set_viewport_square_pixel(struct retro_game_geometry *geom)
  1419. {
  1420. unsigned len, i, aspect_x, aspect_y;
  1421. unsigned int rotation = 0;
  1422. unsigned highest = 1;
  1423. unsigned width = geom->base_width;
  1424. unsigned height = geom->base_height;
  1425. if (width == 0 || height == 0)
  1426. return;
  1427. rotation = retroarch_get_rotation();
  1428. len = MIN(width, height);
  1429. for (i = 1; i < len; i++)
  1430. {
  1431. if ((width % i) == 0 && (height % i) == 0)
  1432. highest = i;
  1433. }
  1434. if (rotation % 2)
  1435. {
  1436. aspect_x = height / highest;
  1437. aspect_y = width / highest;
  1438. }
  1439. else
  1440. {
  1441. aspect_x = width / highest;
  1442. aspect_y = height / highest;
  1443. }
  1444. snprintf(aspectratio_lut[ASPECT_RATIO_SQUARE].name,
  1445. sizeof(aspectratio_lut[ASPECT_RATIO_SQUARE].name),
  1446. msg_hash_to_str(MENU_ENUM_LABEL_VALUE_VIDEO_ASPECT_RATIO_SQUARE_PIXEL),
  1447. aspect_x, aspect_y);
  1448. aspectratio_lut[ASPECT_RATIO_SQUARE].value = (float)aspect_x / aspect_y;
  1449. }
  1450. void video_driver_set_viewport(unsigned width, unsigned height,
  1451. bool force_fullscreen, bool allow_rotate)
  1452. {
  1453. video_driver_state_t *video_st = &video_driver_st;
  1454. if (video_st->current_video && video_st->current_video->set_viewport)
  1455. video_st->current_video->set_viewport(
  1456. video_st->data, width, height,
  1457. force_fullscreen, allow_rotate);
  1458. }
  1459. bool video_driver_set_rotation(unsigned rotation)
  1460. {
  1461. video_driver_state_t *video_st = &video_driver_st;
  1462. if (!video_st->current_video || !video_st->current_video->set_rotation)
  1463. return false;
  1464. video_st->current_video->set_rotation(video_st->data, rotation);
  1465. return true;
  1466. }
  1467. bool video_driver_set_video_mode(unsigned width,
  1468. unsigned height, bool fullscreen)
  1469. {
  1470. video_driver_state_t *video_st = &video_driver_st;
  1471. if ( video_st->poke
  1472. && video_st->poke->set_video_mode)
  1473. {
  1474. RARCH_LOG("[SUBS] video_driver_set_video_mode\n");
  1475. video_st->poke->set_video_mode(video_st->data,
  1476. width, height, fullscreen);
  1477. return true;
  1478. }
  1479. return false;
  1480. }
  1481. bool video_driver_get_video_output_size(unsigned *width, unsigned *height, char *desc, size_t desc_len)
  1482. {
  1483. video_driver_state_t *video_st = &video_driver_st;
  1484. if (!video_st->poke || !video_st->poke->get_video_output_size)
  1485. return false;
  1486. video_st->poke->get_video_output_size(video_st->data,
  1487. width, height, desc, desc_len);
  1488. return true;
  1489. }
  1490. void video_driver_set_texture_enable(bool enable, bool fullscreen)
  1491. {
  1492. video_driver_state_t *video_st = &video_driver_st;
  1493. if (video_st->poke && video_st->poke->set_texture_enable)
  1494. video_st->poke->set_texture_enable(video_st->data,
  1495. enable, fullscreen);
  1496. }
  1497. void video_driver_set_texture_frame(const void *frame, bool rgb32,
  1498. unsigned width, unsigned height, float alpha)
  1499. {
  1500. video_driver_state_t *video_st = &video_driver_st;
  1501. if ( video_st->poke
  1502. && video_st->poke->set_texture_frame)
  1503. video_st->poke->set_texture_frame(video_st->data,
  1504. frame, rgb32, width, height, alpha);
  1505. }
  1506. void *video_driver_read_frame_raw(unsigned *width,
  1507. unsigned *height, size_t *pitch)
  1508. {
  1509. video_driver_state_t *video_st = &video_driver_st;
  1510. if ( video_st->current_video
  1511. && video_st->current_video->read_frame_raw)
  1512. return video_st->current_video->read_frame_raw(
  1513. video_st->data, width,
  1514. height, pitch);
  1515. return NULL;
  1516. }
  1517. void video_driver_set_filtering(unsigned index,
  1518. bool smooth, bool ctx_scaling)
  1519. {
  1520. video_driver_state_t *video_st = &video_driver_st;
  1521. if ( video_st->poke
  1522. && video_st->poke->set_filtering)
  1523. video_st->poke->set_filtering(
  1524. video_st->data,
  1525. index, smooth, ctx_scaling);
  1526. }
  1527. void video_driver_cached_frame_set(const void *data, unsigned width,
  1528. unsigned height, size_t pitch)
  1529. {
  1530. video_driver_state_t *video_st= &video_driver_st;
  1531. if (data)
  1532. video_st->frame_cache_data = data;
  1533. video_st->frame_cache_width = width;
  1534. video_st->frame_cache_height = height;
  1535. video_st->frame_cache_pitch = pitch;
  1536. }
  1537. void video_driver_cached_frame_get(const void **data, unsigned *width,
  1538. unsigned *height, size_t *pitch)
  1539. {
  1540. video_driver_state_t *video_st= &video_driver_st;
  1541. if (data)
  1542. *data = video_st->frame_cache_data;
  1543. if (width)
  1544. *width = video_st->frame_cache_width;
  1545. if (height)
  1546. *height = video_st->frame_cache_height;
  1547. if (pitch)
  1548. *pitch = video_st->frame_cache_pitch;
  1549. }
  1550. void video_driver_get_size(unsigned *width, unsigned *height)
  1551. {
  1552. video_driver_state_t *video_st = &video_driver_st;
  1553. #ifdef HAVE_THREADS
  1554. bool is_threaded = VIDEO_DRIVER_IS_THREADED_INTERNAL(video_st);
  1555. VIDEO_DRIVER_THREADED_LOCK(video_st, is_threaded);
  1556. #endif
  1557. if (width)
  1558. *width = video_st->width;
  1559. if (height)
  1560. *height = video_st->height;
  1561. #ifdef HAVE_THREADS
  1562. VIDEO_DRIVER_THREADED_UNLOCK(video_st, is_threaded);
  1563. #endif
  1564. }
  1565. void video_driver_set_size(unsigned width, unsigned height)
  1566. {
  1567. video_driver_state_t *video_st = &video_driver_st;
  1568. #ifdef HAVE_THREADS
  1569. bool is_threaded = VIDEO_DRIVER_IS_THREADED_INTERNAL(video_st);
  1570. VIDEO_DRIVER_THREADED_LOCK(video_st, is_threaded);
  1571. #endif
  1572. video_st->width = width;
  1573. video_st->height = height;
  1574. #ifdef HAVE_THREADS
  1575. VIDEO_DRIVER_THREADED_UNLOCK(video_st, is_threaded);
  1576. #endif
  1577. }
  1578. /**
  1579. * video_monitor_fps_statistics
  1580. * @refresh_rate : Monitor refresh rate.
  1581. * @deviation : Deviation from measured refresh rate.
  1582. * @sample_points : Amount of sampled points.
  1583. *
  1584. * Gets the monitor FPS statistics based on the current
  1585. * runtime.
  1586. *
  1587. * Returns: true (1) on success.
  1588. * false (0) if:
  1589. * a) threaded video mode is enabled
  1590. * b) less than 2 frame time samples.
  1591. * c) FPS monitor enable is off.
  1592. **/
  1593. bool video_monitor_fps_statistics(double *refresh_rate,
  1594. double *deviation, unsigned *sample_points)
  1595. {
  1596. unsigned i;
  1597. retro_time_t accum = 0;
  1598. retro_time_t avg = 0;
  1599. retro_time_t accum_var = 0;
  1600. unsigned samples = 0;
  1601. video_driver_state_t *video_st = &video_driver_st;
  1602. #ifdef HAVE_THREADS
  1603. if (VIDEO_DRIVER_IS_THREADED_INTERNAL(video_st))
  1604. return false;
  1605. #endif
  1606. if ((samples = MIN(MEASURE_FRAME_TIME_SAMPLES_COUNT,
  1607. (unsigned)video_st->frame_time_count)) < 2)
  1608. return false;
  1609. /* Measure statistics on frame time (microsecs), *not* FPS. */
  1610. for (i = 0; i < samples; i++)
  1611. {
  1612. accum += video_st->frame_time_samples[i];
  1613. #if 0
  1614. RARCH_LOG("[Video]: Interval #%u: %d usec / frame.\n",
  1615. i, (int)frame_time_samples[i]);
  1616. #endif
  1617. }
  1618. avg = accum / samples;
  1619. /* Drop first measurement. It is likely to be bad. */
  1620. for (i = 0; i < samples; i++)
  1621. {
  1622. retro_time_t diff = video_st->frame_time_samples[i] - avg;
  1623. accum_var += diff * diff;
  1624. }
  1625. *deviation = sqrt((double)accum_var / (samples - 1)) / avg;
  1626. if (refresh_rate)
  1627. *refresh_rate = 1000000.0 / avg;
  1628. if (sample_points)
  1629. *sample_points = samples;
  1630. return true;
  1631. }
  1632. float video_driver_get_aspect_ratio(void)
  1633. {
  1634. video_driver_state_t *video_st = &video_driver_st;
  1635. return video_st->aspect_ratio;
  1636. }
  1637. void video_driver_set_aspect_ratio_value(float value)
  1638. {
  1639. video_driver_state_t *video_st = &video_driver_st;
  1640. video_st->aspect_ratio = value;
  1641. }
  1642. enum retro_pixel_format video_driver_get_pixel_format(void)
  1643. {
  1644. video_driver_state_t *video_st = &video_driver_st;
  1645. return video_st->pix_fmt;
  1646. }
  1647. void video_driver_lock_new(void)
  1648. {
  1649. #ifdef HAVE_THREADS
  1650. video_driver_state_t *video_st = &video_driver_st;
  1651. #endif
  1652. VIDEO_DRIVER_LOCK_FREE(video_st);
  1653. #ifdef HAVE_THREADS
  1654. if (!video_st->display_lock)
  1655. video_st->display_lock = slock_new();
  1656. if (!video_st->context_lock)
  1657. video_st->context_lock = slock_new();
  1658. #endif
  1659. }
  1660. void video_driver_set_cached_frame_ptr(const void *data)
  1661. {
  1662. video_driver_state_t *video_st = &video_driver_st;
  1663. video_st->frame_cache_data = data;
  1664. }
  1665. void video_driver_set_stub_frame(void)
  1666. {
  1667. video_driver_state_t *video_st = &video_driver_st;
  1668. video_st->frame_bak = video_st->current_video->frame;
  1669. video_st->current_video->frame = video_null.frame;
  1670. }
  1671. void video_driver_unset_stub_frame(void)
  1672. {
  1673. video_driver_state_t *video_st = &video_driver_st;
  1674. if (video_st->frame_bak)
  1675. video_st->current_video->frame = video_st->frame_bak;
  1676. video_st->frame_bak = NULL;
  1677. }
  1678. bool video_driver_supports_viewport_read(void)
  1679. {
  1680. video_driver_state_t *video_st = &video_driver_st;
  1681. return video_st->current_video->read_viewport
  1682. && video_st->current_video->viewport_info;
  1683. }
  1684. bool video_driver_prefer_viewport_read(void)
  1685. {
  1686. video_driver_state_t *video_st = &video_driver_st;
  1687. return (video_driver_is_hw_context() &&
  1688. !video_st->current_video->read_frame_raw);
  1689. }
  1690. bool video_driver_supports_read_frame_raw(void)
  1691. {
  1692. video_driver_state_t *video_st = &video_driver_st;
  1693. if (video_st->current_video->read_frame_raw)
  1694. return true;
  1695. return false;
  1696. }
  1697. /* Get aspect ratio (DAR) requested by the core */
  1698. float video_driver_get_core_aspect(void)
  1699. {
  1700. video_driver_state_t *video_st = &video_driver_st;
  1701. struct retro_game_geometry *geom = &video_st->av_info.geometry;
  1702. float out_aspect = 0;
  1703. if (!geom || geom->base_width <= 0.0f || geom->base_height <= 0.0f)
  1704. return out_aspect;
  1705. /* Fallback to 1:1 pixel ratio if none provided */
  1706. if (geom->aspect_ratio > 0.0f)
  1707. out_aspect = geom->aspect_ratio;
  1708. else
  1709. out_aspect = (float)geom->base_width / geom->base_height;
  1710. /* Flip rotated aspect */
  1711. if ((retroarch_get_rotation() + retroarch_get_core_requested_rotation()) % 2)
  1712. out_aspect = 1.0f / out_aspect;
  1713. return out_aspect;
  1714. }
  1715. void video_driver_set_viewport_core(void)
  1716. {
  1717. float core_aspect = video_driver_get_core_aspect();
  1718. if (core_aspect != 0)
  1719. aspectratio_lut[ASPECT_RATIO_CORE].value = core_aspect;
  1720. }
  1721. void video_driver_set_rgba(void)
  1722. {
  1723. video_driver_state_t *video_st = &video_driver_st;
  1724. VIDEO_DRIVER_LOCK(video_st);
  1725. video_st->flags |= VIDEO_FLAG_USE_RGBA;
  1726. VIDEO_DRIVER_UNLOCK(video_st);
  1727. }
  1728. void video_driver_unset_rgba(void)
  1729. {
  1730. video_driver_state_t *video_st = &video_driver_st;
  1731. VIDEO_DRIVER_LOCK(video_st);
  1732. video_st->flags &= ~VIDEO_FLAG_USE_RGBA;
  1733. VIDEO_DRIVER_UNLOCK(video_st);
  1734. }
  1735. bool video_driver_supports_rgba(void)
  1736. {
  1737. bool tmp;
  1738. video_driver_state_t *video_st = &video_driver_st;
  1739. VIDEO_DRIVER_LOCK(video_st);
  1740. tmp = video_st->flags & VIDEO_FLAG_USE_RGBA;
  1741. VIDEO_DRIVER_UNLOCK(video_st);
  1742. return tmp;
  1743. }
  1744. void video_driver_set_hdr_support(void)
  1745. {
  1746. video_driver_state_t *video_st = &video_driver_st;
  1747. VIDEO_DRIVER_LOCK(video_st);
  1748. video_st->flags |= VIDEO_FLAG_HDR_SUPPORT;
  1749. VIDEO_DRIVER_UNLOCK(video_st);
  1750. }
  1751. void video_driver_unset_hdr_support(void)
  1752. {
  1753. video_driver_state_t *video_st = &video_driver_st;
  1754. VIDEO_DRIVER_LOCK(video_st);
  1755. video_st->flags &= ~VIDEO_FLAG_HDR_SUPPORT;
  1756. VIDEO_DRIVER_UNLOCK(video_st);
  1757. }
  1758. bool video_driver_supports_hdr(void)
  1759. {
  1760. bool tmp;
  1761. video_driver_state_t *video_st = &video_driver_st;
  1762. VIDEO_DRIVER_LOCK(video_st);
  1763. tmp = video_st->flags & VIDEO_FLAG_HDR_SUPPORT;
  1764. VIDEO_DRIVER_UNLOCK(video_st);
  1765. return tmp;
  1766. }
  1767. bool video_driver_get_next_video_out(void)
  1768. {
  1769. video_driver_state_t *video_st = &video_driver_st;
  1770. if ( !video_st->poke
  1771. || !video_st->poke->get_video_output_next
  1772. )
  1773. return false;
  1774. video_st->poke->get_video_output_next(video_st->data);
  1775. return true;
  1776. }
  1777. bool video_driver_get_prev_video_out(void)
  1778. {
  1779. video_driver_state_t *video_st = &video_driver_st;
  1780. if (
  1781. !video_st->poke
  1782. || !video_st->poke->get_video_output_prev
  1783. )
  1784. return false;
  1785. video_st->poke->get_video_output_prev(video_st->data);
  1786. return true;
  1787. }
  1788. void video_driver_monitor_reset(void)
  1789. {
  1790. video_driver_state_t *video_st = &video_driver_st;
  1791. video_st->frame_time_count = 0;
  1792. }
  1793. void video_driver_set_aspect_ratio(void)
  1794. {
  1795. settings_t *settings = config_get_ptr();
  1796. video_driver_state_t *video_st = &video_driver_st;
  1797. unsigned aspect_ratio_idx = settings->uints.video_aspect_ratio_idx;
  1798. switch (aspect_ratio_idx)
  1799. {
  1800. case ASPECT_RATIO_SQUARE:
  1801. video_driver_set_viewport_square_pixel(&video_st->av_info.geometry);
  1802. break;
  1803. case ASPECT_RATIO_CORE:
  1804. video_driver_set_viewport_core();
  1805. break;
  1806. case ASPECT_RATIO_CONFIG:
  1807. video_driver_set_viewport_config(
  1808. &video_st->av_info.geometry,
  1809. settings->floats.video_aspect_ratio,
  1810. settings->bools.video_aspect_ratio_auto);
  1811. break;
  1812. case ASPECT_RATIO_FULL:
  1813. {
  1814. unsigned width = video_st->width;
  1815. unsigned height = video_st->height;
  1816. if (width != 0 && height != 0)
  1817. aspectratio_lut[ASPECT_RATIO_FULL].value = (float)width / (float)height;
  1818. }
  1819. break;
  1820. default:
  1821. break;
  1822. }
  1823. video_driver_set_aspect_ratio_value(
  1824. aspectratio_lut[aspect_ratio_idx].value);
  1825. if ( video_st->poke
  1826. && video_st->poke->set_aspect_ratio)
  1827. video_st->poke->set_aspect_ratio(video_st->data, aspect_ratio_idx);
  1828. }
  1829. void video_driver_update_viewport(
  1830. struct video_viewport* vp, bool force_full, bool keep_aspect)
  1831. {
  1832. float device_aspect = (float)vp->full_width / vp->full_height;
  1833. settings_t *settings = config_get_ptr();
  1834. bool video_scale_integer = settings->bools.video_scale_integer;
  1835. unsigned video_aspect_ratio_idx = settings->uints.video_aspect_ratio_idx;
  1836. video_driver_state_t *video_st = &video_driver_st;
  1837. float video_driver_aspect_ratio = video_st->aspect_ratio;
  1838. vp->x = 0;
  1839. vp->y = 0;
  1840. vp->width = vp->full_width;
  1841. vp->height = vp->full_height;
  1842. if (video_scale_integer && !force_full)
  1843. video_viewport_get_scaled_integer(
  1844. vp,
  1845. vp->full_width,
  1846. vp->full_height,
  1847. video_driver_aspect_ratio, keep_aspect);
  1848. else if (keep_aspect && !force_full)
  1849. {
  1850. float desired_aspect = video_driver_aspect_ratio;
  1851. #if defined(HAVE_MENU)
  1852. if (video_aspect_ratio_idx == ASPECT_RATIO_CUSTOM)
  1853. {
  1854. const struct video_viewport *custom = &settings->video_viewport_custom;
  1855. vp->x = custom->x;
  1856. vp->y = custom->y;
  1857. vp->width = custom->width;
  1858. vp->height = custom->height;
  1859. }
  1860. else
  1861. #endif
  1862. {
  1863. float delta;
  1864. if (fabsf(device_aspect - desired_aspect) < 0.0001f)
  1865. {
  1866. /* If the aspect ratios of screen and desired aspect
  1867. * ratio are sufficiently equal (floating point stuff),
  1868. * assume they are actually equal.
  1869. */
  1870. }
  1871. else if (device_aspect > desired_aspect)
  1872. {
  1873. delta = (desired_aspect / device_aspect - 1.0f)
  1874. / 2.0f + 0.5f;
  1875. vp->x = (int)roundf(vp->full_width * (0.5f - delta));
  1876. vp->width = (unsigned)roundf(2.0f * vp->full_width * delta);
  1877. vp->y = 0;
  1878. vp->height = vp->full_height;
  1879. }
  1880. else
  1881. {
  1882. vp->x = 0;
  1883. vp->width = vp->full_width;
  1884. delta = (device_aspect / desired_aspect - 1.0f)
  1885. / 2.0f + 0.5f;
  1886. vp->y = (int)roundf(vp->full_height * (0.5f - delta));
  1887. vp->height = (unsigned)roundf(2.0f * vp->full_height * delta);
  1888. }
  1889. }
  1890. }
  1891. #if defined(RARCH_MOBILE)
  1892. /* In portrait mode, we want viewport to gravitate to top of screen. */
  1893. if (device_aspect < 1.0f)
  1894. vp->y = 0;
  1895. #endif
  1896. }
  1897. void video_driver_show_mouse(void)
  1898. {
  1899. video_driver_state_t *video_st = &video_driver_st;
  1900. if ( video_st->poke
  1901. && video_st->poke->show_mouse)
  1902. video_st->poke->show_mouse(video_st->data, true);
  1903. }
  1904. void video_driver_hide_mouse(void)
  1905. {
  1906. video_driver_state_t *video_st = &video_driver_st;
  1907. if ( video_st->poke
  1908. && video_st->poke->show_mouse)
  1909. video_st->poke->show_mouse(video_st->data, false);
  1910. }
  1911. void video_driver_restore_cached(void *settings_data)
  1912. {
  1913. settings_t *settings = (settings_t*)settings_data;
  1914. video_driver_state_t *video_st = &video_driver_st;
  1915. if (video_st->cached_driver_id[0])
  1916. {
  1917. configuration_set_string(settings,
  1918. settings->arrays.video_driver, video_st->cached_driver_id);
  1919. video_st->cached_driver_id[0] = 0;
  1920. RARCH_LOG("[Video]: Restored video driver to \"%s\".\n",
  1921. settings->arrays.video_driver);
  1922. }
  1923. }
  1924. bool video_driver_find_driver(
  1925. void *settings_data,
  1926. const char *prefix, bool verbosity_enabled)
  1927. {
  1928. int i;
  1929. settings_t *settings = (settings_t*)settings_data;
  1930. video_driver_state_t *video_st = &video_driver_st;
  1931. if (video_driver_is_hw_context())
  1932. {
  1933. struct retro_hw_render_callback *hwr =
  1934. VIDEO_DRIVER_GET_HW_CONTEXT_INTERNAL(video_st);
  1935. int rdr_major = hwr->version_major;
  1936. int rdr_minor = hwr->version_minor;
  1937. const char *rdr_context_name = hw_render_context_name(hwr->context_type, rdr_major, rdr_minor);
  1938. enum retro_hw_context_type rdr_type = hw_render_context_type(rdr_context_name);
  1939. video_st->current_video = NULL;
  1940. if (hwr)
  1941. {
  1942. switch (rdr_type)
  1943. {
  1944. case RETRO_HW_CONTEXT_OPENGL_CORE:
  1945. case RETRO_HW_CONTEXT_VULKAN:
  1946. case RETRO_HW_CONTEXT_D3D9:
  1947. case RETRO_HW_CONTEXT_D3D10:
  1948. case RETRO_HW_CONTEXT_D3D11:
  1949. case RETRO_HW_CONTEXT_D3D12:
  1950. #if defined(HAVE_VULKAN) || defined(HAVE_D3D9) || defined(HAVE_D3D10) || defined(HAVE_D3D11) || defined(HAVE_D3D12) || defined(HAVE_OPENGL_CORE)
  1951. RARCH_LOG("[Video]: Using HW render, %s driver forced.\n",
  1952. rdr_context_name);
  1953. if (!string_is_equal(settings->arrays.video_driver,
  1954. rdr_context_name))
  1955. {
  1956. strlcpy(video_st->cached_driver_id,
  1957. settings->arrays.video_driver,
  1958. sizeof(video_st->cached_driver_id));
  1959. configuration_set_string(settings,
  1960. settings->arrays.video_driver,
  1961. rdr_context_name);
  1962. RARCH_LOG("[Video]: \"%s\" saved as cached driver.\n",
  1963. settings->arrays.video_driver);
  1964. }
  1965. video_st->current_video = hw_render_context_driver(rdr_type, rdr_major, rdr_minor);
  1966. return true;
  1967. #else
  1968. break;
  1969. #endif
  1970. case RETRO_HW_CONTEXT_OPENGL:
  1971. #if defined(HAVE_OPENGL)
  1972. RARCH_LOG("[Video]: Using HW render, OpenGL driver forced.\n");
  1973. /* If we have configured one of the HW render
  1974. * capable GL drivers, go with that. */
  1975. #if defined(HAVE_OPENGL_CORE)
  1976. if ( !string_is_equal(settings->arrays.video_driver, "gl") &&
  1977. !string_is_equal(settings->arrays.video_driver, "glcore"))
  1978. {
  1979. strlcpy(video_st->cached_driver_id,
  1980. settings->arrays.video_driver,
  1981. sizeof(video_st->cached_driver_id));
  1982. configuration_set_string(settings,
  1983. settings->arrays.video_driver,
  1984. "glcore");
  1985. RARCH_LOG("[Video]: \"%s\" saved as cached driver.\n",
  1986. settings->arrays.video_driver);
  1987. video_st->current_video = &video_gl3;
  1988. return true;
  1989. }
  1990. #else
  1991. if ( !string_is_equal(settings->arrays.video_driver, "gl"))
  1992. {
  1993. strlcpy(video_st->cached_driver_id,
  1994. settings->arrays.video_driver,
  1995. sizeof(video_st->cached_driver_id));
  1996. configuration_set_string(settings,
  1997. settings->arrays.video_driver,
  1998. "gl");
  1999. RARCH_LOG("[Video]: \"%s\" saved as cached driver.\n",
  2000. settings->arrays.video_driver);
  2001. video_st->current_video = &video_gl2;
  2002. return true;
  2003. }
  2004. #endif
  2005. RARCH_LOG("[Video]: Using configured \"%s\""
  2006. " driver for GL HW render.\n",
  2007. settings->arrays.video_driver);
  2008. break;
  2009. #endif
  2010. default:
  2011. case RETRO_HW_CONTEXT_NONE:
  2012. break;
  2013. }
  2014. }
  2015. }
  2016. if (frontend_driver_has_get_video_driver_func())
  2017. {
  2018. if ((video_st->current_video = (video_driver_t*)
  2019. frontend_driver_get_video_driver()))
  2020. return true;
  2021. RARCH_WARN("[Video]: Frontend supports get_video_driver() but did not specify one.\n");
  2022. }
  2023. i = (int)driver_find_index(
  2024. "video_driver",
  2025. settings->arrays.video_driver);
  2026. if (i >= 0)
  2027. video_st->current_video = (video_driver_t*)video_drivers[i];
  2028. else
  2029. {
  2030. if (verbosity_enabled)
  2031. {
  2032. unsigned d;
  2033. RARCH_ERR("Couldn't find any %s named \"%s\"\n", prefix,
  2034. settings->arrays.video_driver);
  2035. RARCH_LOG_OUTPUT("Available %ss are:\n", prefix);
  2036. for (d = 0; video_drivers[d]; d++)
  2037. RARCH_LOG_OUTPUT("\t%s\n", video_drivers[d]->ident);
  2038. RARCH_WARN("Going to default to first %s..\n", prefix);
  2039. }
  2040. if (!(video_st->current_video = (video_driver_t*)video_drivers[0]))
  2041. return false;
  2042. }
  2043. return true;
  2044. }
  2045. void video_driver_apply_state_changes(void)
  2046. {
  2047. video_driver_state_t *video_st = &video_driver_st;
  2048. if ( video_st->poke
  2049. && video_st->poke->apply_state_changes)
  2050. video_st->poke->apply_state_changes(video_st->data);
  2051. }
  2052. bool video_driver_read_viewport(uint8_t *buffer, bool is_idle)
  2053. {
  2054. video_driver_state_t *video_st = &video_driver_st;
  2055. if ( video_st->current_video->read_viewport
  2056. && video_st->current_video->read_viewport(
  2057. video_st->data, buffer, is_idle))
  2058. return true;
  2059. return false;
  2060. }
  2061. bool video_driver_is_hw_context(void)
  2062. {
  2063. bool is_hw_context = false;
  2064. video_driver_state_t *video_st = &video_driver_st;
  2065. VIDEO_DRIVER_CONTEXT_LOCK(video_st);
  2066. is_hw_context = (video_st->hw_render.context_type
  2067. != RETRO_HW_CONTEXT_NONE);
  2068. VIDEO_DRIVER_CONTEXT_UNLOCK(video_st);
  2069. return is_hw_context;
  2070. }
  2071. const struct retro_hw_render_context_negotiation_interface *
  2072. video_driver_get_context_negotiation_interface(void)
  2073. {
  2074. video_driver_state_t *video_st = &video_driver_st;
  2075. return video_st->hw_render_context_negotiation;
  2076. }
  2077. bool video_driver_get_viewport_info(struct video_viewport *viewport)
  2078. {
  2079. video_driver_state_t *video_st = &video_driver_st;
  2080. if (!video_st->current_video || !video_st->current_video->viewport_info)
  2081. return false;
  2082. video_st->current_video->viewport_info(video_st->data, viewport);
  2083. return true;
  2084. }
  2085. /**
  2086. * video_viewport_get_scaled_integer:
  2087. * @vp : Viewport handle
  2088. * @width : Width.
  2089. * @height : Height.
  2090. * @aspect_ratio : Aspect ratio (in float).
  2091. * @keep_aspect : Preserve aspect ratio?
  2092. *
  2093. * Gets viewport scaling dimensions based on
  2094. * scaled integer aspect ratio.
  2095. **/
  2096. void video_viewport_get_scaled_integer(struct video_viewport *vp,
  2097. unsigned width, unsigned height,
  2098. float aspect_ratio, bool keep_aspect)
  2099. {
  2100. int padding_x = 0;
  2101. int padding_y = 0;
  2102. settings_t *settings = config_get_ptr();
  2103. video_driver_state_t *video_st = &video_driver_st;
  2104. unsigned video_aspect_ratio_idx = settings->uints.video_aspect_ratio_idx;
  2105. bool overscale = settings->bools.video_scale_integer_overscale;
  2106. if (video_aspect_ratio_idx == ASPECT_RATIO_CUSTOM)
  2107. {
  2108. struct video_viewport *custom = &settings->video_viewport_custom;
  2109. if (custom)
  2110. {
  2111. padding_x = width - custom->width;
  2112. padding_y = height - custom->height;
  2113. width = custom->width;
  2114. height = custom->height;
  2115. }
  2116. }
  2117. else
  2118. {
  2119. unsigned base_width;
  2120. /* Use system reported sizes as these define the
  2121. * geometry for the "normal" case. */
  2122. unsigned base_height =
  2123. video_st->av_info.geometry.base_height;
  2124. unsigned int rotation = retroarch_get_rotation();
  2125. if (rotation % 2)
  2126. base_height = video_st->av_info.geometry.base_width;
  2127. if (base_height == 0)
  2128. base_height = 1;
  2129. /* Account for non-square pixels.
  2130. * This is sort of contradictory with the goal of integer scale,
  2131. * but it is desirable in some cases.
  2132. *
  2133. * If square pixels are used, base_height will be equal to
  2134. * system->av_info.base_height. */
  2135. base_width = (unsigned)roundf(base_height * aspect_ratio);
  2136. /* Make sure that we don't get 0x scale ... */
  2137. if (width >= base_width && height >= base_height)
  2138. {
  2139. if (keep_aspect)
  2140. {
  2141. /* X/Y scale must be same. */
  2142. unsigned max_scale = 1;
  2143. if (overscale)
  2144. max_scale = MIN((width / base_width) + !!(width % base_width),
  2145. (height / base_height) + !!(height % base_height));
  2146. else
  2147. max_scale = MIN(width / base_width,
  2148. height / base_height);
  2149. padding_x = width - base_width * max_scale;
  2150. padding_y = height - base_height * max_scale;
  2151. }
  2152. else
  2153. {
  2154. /* X/Y can be independent, each scaled as much as possible. */
  2155. padding_x = width % base_width;
  2156. padding_y = height % base_height;
  2157. }
  2158. }
  2159. width -= padding_x;
  2160. height -= padding_y;
  2161. }
  2162. vp->width = width;
  2163. vp->height = height;
  2164. vp->x = padding_x / 2;
  2165. vp->y = padding_y / 2;
  2166. }
  2167. void video_driver_display_type_set(enum rarch_display_type type)
  2168. {
  2169. video_driver_state_t *video_st = &video_driver_st;
  2170. video_st->display_type = type;
  2171. }
  2172. uintptr_t video_driver_display_get(void)
  2173. {
  2174. video_driver_state_t *video_st = &video_driver_st;
  2175. return video_st->display;
  2176. }
  2177. uintptr_t video_driver_display_userdata_get(void)
  2178. {
  2179. video_driver_state_t *video_st = &video_driver_st;
  2180. return video_st->display_userdata;
  2181. }
  2182. void video_driver_display_userdata_set(uintptr_t idx)
  2183. {
  2184. video_driver_state_t *video_st = &video_driver_st;
  2185. video_st->display_userdata = idx;
  2186. }
  2187. void video_driver_display_set(uintptr_t idx)
  2188. {
  2189. video_driver_state_t *video_st = &video_driver_st;
  2190. video_st->display = idx;
  2191. }
  2192. enum rarch_display_type video_driver_display_type_get(void)
  2193. {
  2194. video_driver_state_t *video_st = &video_driver_st;
  2195. return video_st->display_type;
  2196. }
  2197. void video_driver_window_set(uintptr_t idx)
  2198. {
  2199. video_driver_state_t *video_st = &video_driver_st;
  2200. video_st->window = idx;
  2201. }
  2202. uintptr_t video_driver_window_get(void)
  2203. {
  2204. video_driver_state_t *video_st = &video_driver_st;
  2205. return video_st->window;
  2206. }
  2207. bool video_driver_texture_load(void *data,
  2208. enum texture_filter_type filter_type,
  2209. uintptr_t *id)
  2210. {
  2211. video_driver_state_t *video_st = &video_driver_st;
  2212. if ( !id
  2213. || !video_st->poke
  2214. || !video_st->poke->load_texture)
  2215. return false;
  2216. *id = video_st->poke->load_texture(
  2217. video_st->data, data,
  2218. VIDEO_DRIVER_IS_THREADED_INTERNAL(video_st),
  2219. filter_type);
  2220. return true;
  2221. }
  2222. bool video_driver_texture_unload(uintptr_t *id)
  2223. {
  2224. video_driver_state_t *video_st = &video_driver_st;
  2225. if ( !video_st->poke
  2226. || !video_st->poke->unload_texture)
  2227. return false;
  2228. video_st->poke->unload_texture(
  2229. video_st->data,
  2230. VIDEO_DRIVER_IS_THREADED_INTERNAL(video_st),
  2231. *id);
  2232. *id = 0;
  2233. return true;
  2234. }
  2235. /**
  2236. * video_driver_cached_frame:
  2237. *
  2238. * Renders the current video frame.
  2239. **/
  2240. void video_driver_cached_frame(void)
  2241. {
  2242. runloop_state_t *runloop_st = runloop_state_get_ptr();
  2243. recording_state_t *recording_st= recording_state_get_ptr();
  2244. video_driver_state_t *video_st = &video_driver_st;
  2245. void *recording = recording_st->data;
  2246. struct retro_callbacks *cbs = &runloop_st->retro_ctx;
  2247. /* Cannot allow recording when pushing duped frames. */
  2248. recording_st->data = NULL;
  2249. if (runloop_st->current_core.flags & RETRO_CORE_FLAG_INITED)
  2250. cbs->frame_cb(
  2251. (video_st->frame_cache_data != RETRO_HW_FRAME_BUFFER_VALID)
  2252. ? video_st->frame_cache_data
  2253. : NULL,
  2254. video_st->frame_cache_width,
  2255. video_st->frame_cache_height,
  2256. video_st->frame_cache_pitch);
  2257. recording_st->data = recording;
  2258. }
  2259. bool video_driver_has_focus(void)
  2260. {
  2261. video_driver_state_t *video_st = &video_driver_st;
  2262. return VIDEO_HAS_FOCUS(video_st);
  2263. }
  2264. size_t video_driver_get_window_title(char *buf, unsigned len)
  2265. {
  2266. video_driver_state_t *video_st = &video_driver_st;
  2267. if (buf && (video_st->flags & VIDEO_FLAG_WINDOW_TITLE_UPDATE))
  2268. {
  2269. strlcpy(buf, video_st->window_title, len);
  2270. video_st->flags &= ~VIDEO_FLAG_WINDOW_TITLE_UPDATE;
  2271. }
  2272. return video_st->window_title_len;
  2273. }
  2274. void video_driver_build_info(video_frame_info_t *video_info)
  2275. {
  2276. video_viewport_t *custom_vp = NULL;
  2277. runloop_state_t *runloop_st = runloop_state_get_ptr();
  2278. settings_t *settings = config_get_ptr();
  2279. video_driver_state_t *video_st = &video_driver_st;
  2280. input_driver_state_t *input_st = input_state_get_ptr();
  2281. #ifdef HAVE_MENU
  2282. struct menu_state *menu_st = menu_state_get_ptr();
  2283. #endif
  2284. #ifdef HAVE_GFX_WIDGETS
  2285. dispgfx_widget_t *p_dispwidget = dispwidget_get_ptr();
  2286. #endif
  2287. #ifdef HAVE_THREADS
  2288. bool is_threaded =
  2289. VIDEO_DRIVER_IS_THREADED_INTERNAL(video_st);
  2290. VIDEO_DRIVER_THREADED_LOCK(video_st, is_threaded);
  2291. #endif
  2292. custom_vp = &settings->video_viewport_custom;
  2293. #ifdef HAVE_GFX_WIDGETS
  2294. video_info->widgets_active = p_dispwidget->active;
  2295. #else
  2296. video_info->widgets_active = false;
  2297. #endif
  2298. #ifdef HAVE_MENU
  2299. video_info->notifications_hidden = settings->bools.notification_show_when_menu_is_alive
  2300. && !(menu_st->flags & MENU_ST_FLAG_ALIVE);
  2301. #endif
  2302. video_info->refresh_rate = settings->floats.video_refresh_rate;
  2303. video_info->crt_switch_resolution = settings->uints.crt_switch_resolution;
  2304. video_info->crt_switch_resolution_super = settings->uints.crt_switch_resolution_super;
  2305. video_info->crt_switch_center_adjust = settings->ints.crt_switch_center_adjust;
  2306. video_info->crt_switch_porch_adjust = settings->ints.crt_switch_porch_adjust;
  2307. video_info->crt_switch_hires_menu = settings->bools.crt_switch_hires_menu;
  2308. video_info->black_frame_insertion = settings->uints.video_black_frame_insertion;
  2309. video_info->hard_sync = settings->bools.video_hard_sync;
  2310. video_info->hard_sync_frames = settings->uints.video_hard_sync_frames;
  2311. video_info->runahead = settings->bools.run_ahead_enabled;
  2312. video_info->runahead_second_instance = settings->bools.run_ahead_secondary_instance;
  2313. video_info->preemptive_frames = settings->bools.preemptive_frames_enable;
  2314. video_info->runahead_frames = settings->uints.run_ahead_frames;
  2315. video_info->fps_show = settings->bools.video_fps_show;
  2316. video_info->memory_show = settings->bools.video_memory_show;
  2317. video_info->statistics_show = settings->bools.video_statistics_show;
  2318. video_info->framecount_show = settings->bools.video_framecount_show;
  2319. video_info->core_status_msg_show = runloop_st->core_status_msg.set;
  2320. video_info->aspect_ratio_idx = settings->uints.video_aspect_ratio_idx;
  2321. video_info->post_filter_record = settings->bools.video_post_filter_record;
  2322. video_info->input_menu_swap_ok_cancel_buttons
  2323. = settings->bools.input_menu_swap_ok_cancel_buttons;
  2324. video_info->max_swapchain_images = settings->uints.video_max_swapchain_images;
  2325. video_info->windowed_fullscreen = settings->bools.video_windowed_fullscreen;
  2326. video_info->fullscreen = settings->bools.video_fullscreen
  2327. || (video_st->flags & VIDEO_FLAG_FORCE_FULLSCREEN);
  2328. video_info->menu_mouse_enable = settings->bools.menu_mouse_enable;
  2329. video_info->monitor_index = settings->uints.video_monitor_index;
  2330. video_info->font_enable = settings->bools.video_font_enable;
  2331. video_info->font_size = settings->floats.video_font_size;
  2332. video_info->font_msg_pos_x = settings->floats.video_msg_pos_x;
  2333. video_info->font_msg_pos_y = settings->floats.video_msg_pos_y;
  2334. video_info->font_msg_color_r = settings->floats.video_msg_color_r;
  2335. video_info->font_msg_color_g = settings->floats.video_msg_color_g;
  2336. video_info->font_msg_color_b = settings->floats.video_msg_color_b;
  2337. video_info->custom_vp_x = custom_vp->x;
  2338. video_info->custom_vp_y = custom_vp->y;
  2339. video_info->custom_vp_width = custom_vp->width;
  2340. video_info->custom_vp_height = custom_vp->height;
  2341. video_info->custom_vp_full_width = custom_vp->full_width;
  2342. video_info->custom_vp_full_height = custom_vp->full_height;
  2343. #if defined(HAVE_GFX_WIDGETS)
  2344. video_info->widgets_userdata = p_dispwidget;
  2345. video_info->widgets_is_paused = video_st->flags & VIDEO_FLAG_WIDGETS_PAUSED;
  2346. video_info->widgets_is_fast_forwarding = video_st->flags & VIDEO_FLAG_WIDGETS_FAST_FORWARD;
  2347. video_info->widgets_is_rewinding = video_st->flags & VIDEO_FLAG_WIDGETS_REWINDING;
  2348. #else
  2349. video_info->widgets_userdata = NULL;
  2350. video_info->widgets_is_paused = false;
  2351. video_info->widgets_is_fast_forwarding = false;
  2352. video_info->widgets_is_rewinding = false;
  2353. #endif
  2354. video_info->width = video_st->width;
  2355. video_info->height = video_st->height;
  2356. video_info->use_rgba = video_st->flags & VIDEO_FLAG_USE_RGBA;
  2357. video_info->hdr_enable = settings->bools.video_hdr_enable;
  2358. video_info->libretro_running = false;
  2359. video_info->msg_bgcolor_enable = settings->bools.video_msg_bgcolor_enable;
  2360. video_info->fps_update_interval = settings->uints.fps_update_interval;
  2361. video_info->memory_update_interval = settings->uints.memory_update_interval;
  2362. #ifdef HAVE_MENU
  2363. video_info->menu_is_alive = menu_st->flags & MENU_ST_FLAG_ALIVE;
  2364. video_info->menu_screensaver_active = menu_st->flags & MENU_ST_FLAG_SCREENSAVER_ACTIVE;
  2365. video_info->menu_footer_opacity = settings->floats.menu_footer_opacity;
  2366. video_info->menu_header_opacity = settings->floats.menu_header_opacity;
  2367. video_info->materialui_color_theme = settings->uints.menu_materialui_color_theme;
  2368. video_info->ozone_color_theme = settings->uints.menu_ozone_color_theme;
  2369. video_info->menu_shader_pipeline = settings->uints.menu_xmb_shader_pipeline;
  2370. video_info->xmb_theme = settings->uints.menu_xmb_theme;
  2371. video_info->xmb_color_theme = settings->uints.menu_xmb_color_theme;
  2372. video_info->timedate_enable = settings->bools.menu_timedate_enable;
  2373. video_info->battery_level_enable = settings->bools.menu_battery_level_enable;
  2374. video_info->xmb_shadows_enable = settings->bools.menu_xmb_shadows_enable;
  2375. video_info->xmb_alpha_factor = settings->uints.menu_xmb_alpha_factor;
  2376. video_info->menu_wallpaper_opacity = settings->floats.menu_wallpaper_opacity;
  2377. video_info->menu_framebuffer_opacity = settings->floats.menu_framebuffer_opacity;
  2378. video_info->overlay_behind_menu = settings->bools.input_overlay_behind_menu;
  2379. video_info->libretro_running = runloop_st->current_core.flags & RETRO_CORE_FLAG_GAME_LOADED;
  2380. #else
  2381. video_info->menu_is_alive = false;
  2382. video_info->menu_screensaver_active = false;
  2383. video_info->menu_footer_opacity = 0.0f;
  2384. video_info->menu_header_opacity = 0.0f;
  2385. video_info->materialui_color_theme = 0;
  2386. video_info->menu_shader_pipeline = 0;
  2387. video_info->xmb_color_theme = 0;
  2388. video_info->xmb_theme = 0;
  2389. video_info->timedate_enable = false;
  2390. video_info->battery_level_enable = false;
  2391. video_info->xmb_shadows_enable = false;
  2392. video_info->xmb_alpha_factor = 0.0f;
  2393. video_info->menu_framebuffer_opacity = 0.0f;
  2394. video_info->menu_wallpaper_opacity = 0.0f;
  2395. video_info->overlay_behind_menu = false;
  2396. #endif
  2397. video_info->msg_queue_delay = runloop_st->msg_queue_delay;
  2398. video_info->runloop_is_paused = runloop_st->flags & RUNLOOP_FLAG_PAUSED;
  2399. video_info->runloop_is_slowmotion = runloop_st->flags & RUNLOOP_FLAG_SLOWMOTION;
  2400. video_info->fastforward_frameskip = settings->bools.fastforward_frameskip;
  2401. video_info->input_driver_nonblock_state = input_st
  2402. ? (input_st->flags & INP_FLAG_NONBLOCKING) : false;
  2403. video_info->input_driver_grab_mouse_state = (input_st->flags & INP_FLAG_GRAB_MOUSE_STATE);
  2404. video_info->disp_userdata = disp_get_ptr();
  2405. video_info->userdata = VIDEO_DRIVER_GET_PTR_INTERNAL(video_st);
  2406. #ifdef HAVE_THREADS
  2407. VIDEO_DRIVER_THREADED_UNLOCK(video_st, is_threaded);
  2408. #endif
  2409. }
  2410. #ifdef HAVE_VULKAN
  2411. static const gfx_ctx_driver_t *vk_context_driver_init_first(
  2412. uint32_t runloop_flags,
  2413. settings_t *settings,
  2414. void *data,
  2415. const char *ident, enum gfx_ctx_api api, unsigned major,
  2416. unsigned minor, bool hw_render_ctx, void **ctx_data)
  2417. {
  2418. unsigned j;
  2419. int i = -1;
  2420. video_driver_state_t *video_st = &video_driver_st;
  2421. for (j = 0; gfx_ctx_vk_drivers[j]; j++)
  2422. {
  2423. if (string_is_equal_noncase(ident, gfx_ctx_vk_drivers[j]->ident))
  2424. {
  2425. i = j;
  2426. break;
  2427. }
  2428. }
  2429. if (i >= 0)
  2430. {
  2431. const gfx_ctx_driver_t *ctx = video_context_driver_init(
  2432. runloop_flags & RUNLOOP_FLAG_CORE_SET_SHARED_CONTEXT,
  2433. settings,
  2434. data,
  2435. gfx_ctx_vk_drivers[i], ident,
  2436. api, major, minor, hw_render_ctx, ctx_data);
  2437. if (ctx)
  2438. {
  2439. video_st->context_data = *ctx_data;
  2440. return ctx;
  2441. }
  2442. }
  2443. for (i = 0; gfx_ctx_vk_drivers[i]; i++)
  2444. {
  2445. const gfx_ctx_driver_t *ctx =
  2446. video_context_driver_init(
  2447. runloop_flags & RUNLOOP_FLAG_CORE_SET_SHARED_CONTEXT,
  2448. settings,
  2449. data,
  2450. gfx_ctx_vk_drivers[i], ident,
  2451. api, major, minor, hw_render_ctx, ctx_data);
  2452. if (ctx)
  2453. {
  2454. video_st->context_data = *ctx_data;
  2455. return ctx;
  2456. }
  2457. }
  2458. return NULL;
  2459. }
  2460. #endif
  2461. static const gfx_ctx_driver_t *gl_context_driver_init_first(
  2462. uint32_t runloop_flags,
  2463. settings_t *settings,
  2464. void *data,
  2465. const char *ident, enum gfx_ctx_api api, unsigned major,
  2466. unsigned minor, bool hw_render_ctx, void **ctx_data)
  2467. {
  2468. unsigned j;
  2469. int i = -1;
  2470. video_driver_state_t *video_st = &video_driver_st;
  2471. for (j = 0; gfx_ctx_gl_drivers[j]; j++)
  2472. {
  2473. if (string_is_equal_noncase(ident, gfx_ctx_gl_drivers[j]->ident))
  2474. {
  2475. i = j;
  2476. break;
  2477. }
  2478. }
  2479. if (i >= 0)
  2480. {
  2481. const gfx_ctx_driver_t *ctx = video_context_driver_init(
  2482. runloop_flags & RUNLOOP_FLAG_CORE_SET_SHARED_CONTEXT,
  2483. settings,
  2484. data,
  2485. gfx_ctx_gl_drivers[i], ident,
  2486. api, major, minor, hw_render_ctx, ctx_data);
  2487. if (ctx)
  2488. {
  2489. video_st->context_data = *ctx_data;
  2490. return ctx;
  2491. }
  2492. }
  2493. for (i = 0; gfx_ctx_gl_drivers[i]; i++)
  2494. {
  2495. const gfx_ctx_driver_t *ctx =
  2496. video_context_driver_init(
  2497. runloop_flags & RUNLOOP_FLAG_CORE_SET_SHARED_CONTEXT,
  2498. settings,
  2499. data,
  2500. gfx_ctx_gl_drivers[i], ident,
  2501. api, major, minor, hw_render_ctx, ctx_data);
  2502. if (ctx)
  2503. {
  2504. video_st->context_data = *ctx_data;
  2505. return ctx;
  2506. }
  2507. }
  2508. return NULL;
  2509. }
  2510. /**
  2511. * video_context_driver_init_first:
  2512. * @data : Input data.
  2513. * @ident : Identifier of graphics context driver to find.
  2514. * @api : API of higher-level graphics API.
  2515. * @major : Major version number of higher-level graphics API.
  2516. * @minor : Minor version number of higher-level graphics API.
  2517. * @hw_render_ctx : Request a graphics context driver capable of
  2518. * hardware rendering?
  2519. *
  2520. * Finds first suitable graphics context driver and initializes.
  2521. *
  2522. * Returns: graphics context driver if found, otherwise NULL.
  2523. **/
  2524. const gfx_ctx_driver_t *video_context_driver_init_first(void *data,
  2525. const char *ident, enum gfx_ctx_api api, unsigned major,
  2526. unsigned minor, bool hw_render_ctx, void **ctx_data)
  2527. {
  2528. uint32_t runloop_flags = runloop_get_flags();
  2529. settings_t *settings = config_get_ptr();
  2530. switch (api)
  2531. {
  2532. case GFX_CTX_VULKAN_API:
  2533. #ifdef HAVE_VULKAN
  2534. {
  2535. const gfx_ctx_driver_t *ptr = vk_context_driver_init_first(
  2536. runloop_flags, settings,
  2537. data, ident, api, major, minor, hw_render_ctx, ctx_data);
  2538. if (ptr && !string_is_equal(ptr->ident, "null"))
  2539. return ptr;
  2540. /* fall-through if no valid driver was found */
  2541. }
  2542. #endif
  2543. case GFX_CTX_OPENGL_API:
  2544. case GFX_CTX_OPENGL_ES_API:
  2545. case GFX_CTX_OPENVG_API:
  2546. case GFX_CTX_METAL_API:
  2547. case GFX_CTX_RSX_API:
  2548. return gl_context_driver_init_first(
  2549. runloop_flags, settings,
  2550. data, ident, api, major, minor,
  2551. hw_render_ctx, ctx_data);
  2552. case GFX_CTX_NONE:
  2553. default:
  2554. break;
  2555. }
  2556. return NULL;
  2557. }
  2558. void video_context_driver_free(void)
  2559. {
  2560. video_driver_state_t *video_st = &video_driver_st;
  2561. video_context_driver_destroy(&video_st->current_video_context);
  2562. video_st->context_data = NULL;
  2563. }
  2564. bool video_context_driver_get_metrics(gfx_ctx_metrics_t *metrics)
  2565. {
  2566. video_driver_state_t *video_st = &video_driver_st;
  2567. if (video_st->current_video_context.get_metrics)
  2568. return video_st->current_video_context.get_metrics(
  2569. video_st->context_data,
  2570. metrics->type,
  2571. metrics->value);
  2572. return false;
  2573. }
  2574. bool video_context_driver_get_refresh_rate(float *refresh_rate)
  2575. {
  2576. video_driver_state_t *video_st = &video_driver_st;
  2577. if (!video_st->current_video_context.get_refresh_rate || !refresh_rate)
  2578. return false;
  2579. if (!video_st->context_data)
  2580. return false;
  2581. if (video_st->flags & VIDEO_FLAG_CRT_SWITCHING_ACTIVE)
  2582. {
  2583. float refresh_holder = 0;
  2584. if (refresh_rate)
  2585. refresh_holder =
  2586. video_st->current_video_context.get_refresh_rate(
  2587. video_st->context_data);
  2588. /* Fix for incorrect interlacing detection --
  2589. * HARD SET VSYNC TO REQUIRED REFRESH FOR CRT*/
  2590. if (refresh_holder != video_st->core_hz)
  2591. *refresh_rate = video_st->core_hz;
  2592. }
  2593. else
  2594. {
  2595. if (refresh_rate)
  2596. *refresh_rate =
  2597. video_st->current_video_context.get_refresh_rate(
  2598. video_st->context_data);
  2599. }
  2600. return true;
  2601. }
  2602. bool video_context_driver_get_ident(gfx_ctx_ident_t *ident)
  2603. {
  2604. video_driver_state_t *video_st = &video_driver_st;
  2605. if (!ident)
  2606. return false;
  2607. ident->ident = video_st->current_video_context.ident;
  2608. return true;
  2609. }
  2610. bool video_context_driver_get_flags(gfx_ctx_flags_t *flags)
  2611. {
  2612. video_driver_state_t *video_st = &video_driver_st;
  2613. if (!video_st->current_video_context.get_flags)
  2614. return false;
  2615. if (video_st->flags & VIDEO_FLAG_DEFERRED_VIDEO_CTX_DRIVER_SET_FLAGS)
  2616. {
  2617. flags->flags = video_st->deferred_flag_data.flags;
  2618. video_st->flags &= ~VIDEO_FLAG_DEFERRED_VIDEO_CTX_DRIVER_SET_FLAGS;
  2619. return true;
  2620. }
  2621. flags->flags = video_st->current_video_context.get_flags(
  2622. video_st->context_data);
  2623. return true;
  2624. }
  2625. static bool video_driver_get_flags(gfx_ctx_flags_t *flags)
  2626. {
  2627. video_driver_state_t *video_st = &video_driver_st;
  2628. if (!video_st->poke || !video_st->poke->get_flags)
  2629. return false;
  2630. flags->flags = video_st->poke->get_flags(video_st->data);
  2631. return true;
  2632. }
  2633. gfx_ctx_flags_t video_driver_get_flags_wrapper(void)
  2634. {
  2635. gfx_ctx_flags_t flags;
  2636. flags.flags = 0;
  2637. if (!video_driver_get_flags(&flags))
  2638. video_context_driver_get_flags(&flags);
  2639. return flags;
  2640. }
  2641. /**
  2642. * video_driver_test_all_flags:
  2643. * @testflag : flag to test
  2644. *
  2645. * Poll both the video and context driver's flags and test
  2646. * whether @testflag is set or not.
  2647. **/
  2648. bool video_driver_test_all_flags(enum display_flags testflag)
  2649. {
  2650. gfx_ctx_flags_t flags;
  2651. if (video_driver_get_flags(&flags))
  2652. if (BIT32_GET(flags.flags, testflag))
  2653. return true;
  2654. if (video_context_driver_get_flags(&flags))
  2655. if (BIT32_GET(flags.flags, testflag))
  2656. return true;
  2657. return false;
  2658. }
  2659. bool video_context_driver_set_flags(gfx_ctx_flags_t *flags)
  2660. {
  2661. video_driver_state_t *video_st = &video_driver_st;
  2662. if (!flags)
  2663. return false;
  2664. if (!video_st->current_video_context.set_flags)
  2665. {
  2666. video_st->deferred_flag_data.flags = flags->flags;
  2667. video_st->flags |= VIDEO_FLAG_DEFERRED_VIDEO_CTX_DRIVER_SET_FLAGS;
  2668. return false;
  2669. }
  2670. video_st->current_video_context.set_flags(
  2671. video_st->context_data, flags->flags);
  2672. return true;
  2673. }
  2674. enum gfx_ctx_api video_context_driver_get_api(void)
  2675. {
  2676. video_driver_state_t *video_st = &video_driver_st;
  2677. enum gfx_ctx_api ctx_api = video_st->context_data ?
  2678. video_st->current_video_context.get_api(
  2679. video_st->context_data) : GFX_CTX_NONE;
  2680. if (ctx_api == GFX_CTX_NONE)
  2681. {
  2682. const char *video_ident = (video_st->current_video)
  2683. ? video_st->current_video->ident
  2684. : NULL;
  2685. if (string_starts_with_size(video_ident, "d3d", STRLEN_CONST("d3d")))
  2686. {
  2687. if (string_is_equal(video_ident, "d3d9_hlsl"))
  2688. return GFX_CTX_DIRECT3D9_API;
  2689. else if (string_is_equal(video_ident, "d3d10"))
  2690. return GFX_CTX_DIRECT3D10_API;
  2691. else if (string_is_equal(video_ident, "d3d11"))
  2692. return GFX_CTX_DIRECT3D11_API;
  2693. else if (string_is_equal(video_ident, "d3d12"))
  2694. return GFX_CTX_DIRECT3D12_API;
  2695. }
  2696. if (string_starts_with_size(video_ident, "gl", STRLEN_CONST("gl")))
  2697. {
  2698. if (string_is_equal(video_ident, "gl"))
  2699. return GFX_CTX_OPENGL_API;
  2700. else if (string_is_equal(video_ident, "gl1"))
  2701. return GFX_CTX_OPENGL_API;
  2702. else if (string_is_equal(video_ident, "glcore"))
  2703. return GFX_CTX_OPENGL_API;
  2704. }
  2705. else if (string_is_equal(video_ident, "vulkan"))
  2706. return GFX_CTX_VULKAN_API;
  2707. else if (string_is_equal(video_ident, "metal"))
  2708. return GFX_CTX_METAL_API;
  2709. else if (string_is_equal(video_ident, "rsx"))
  2710. return GFX_CTX_RSX_API;
  2711. return GFX_CTX_NONE;
  2712. }
  2713. return ctx_api;
  2714. }
  2715. bool video_driver_has_windowed(void)
  2716. {
  2717. #if !(defined(RARCH_CONSOLE) || defined(RARCH_MOBILE))
  2718. video_driver_state_t *video_st = &video_driver_st;
  2719. if (video_st->data && video_st->current_video->has_windowed)
  2720. return video_st->current_video->has_windowed(video_st->data);
  2721. #endif
  2722. return false;
  2723. }
  2724. bool video_driver_cached_frame_has_valid_framebuffer(void)
  2725. {
  2726. video_driver_state_t *video_st = &video_driver_st;
  2727. if (video_st->frame_cache_data)
  2728. return (video_st->frame_cache_data == RETRO_HW_FRAME_BUFFER_VALID);
  2729. return false;
  2730. }
  2731. bool video_shader_driver_get_current_shader(video_shader_ctx_t *shader)
  2732. {
  2733. video_driver_state_t *video_st = &video_driver_st;
  2734. void *video_driver = video_st->data;
  2735. const video_poke_interface_t *video_poke = video_st->poke;
  2736. shader->data = NULL;
  2737. if (!video_poke || !video_driver || !video_poke->get_current_shader)
  2738. return false;
  2739. shader->data = video_poke->get_current_shader(video_driver);
  2740. return true;
  2741. }
  2742. float video_driver_get_refresh_rate(void)
  2743. {
  2744. video_driver_state_t *video_st = &video_driver_st;
  2745. if (video_st->poke && video_st->poke->get_refresh_rate)
  2746. return video_st->poke->get_refresh_rate(video_st->data);
  2747. return 0.0f;
  2748. }
  2749. #if defined(HAVE_GFX_WIDGETS)
  2750. bool video_driver_has_widgets(void)
  2751. {
  2752. video_driver_state_t *video_st = &video_driver_st;
  2753. return video_st->current_video
  2754. && video_st->current_video->gfx_widgets_enabled
  2755. && video_st->current_video->gfx_widgets_enabled(video_st->data);
  2756. }
  2757. #endif
  2758. void video_driver_set_gpu_device_string(const char *str)
  2759. {
  2760. video_driver_state_t *video_st = &video_driver_st;
  2761. strlcpy(video_st->gpu_device_string, str,
  2762. sizeof(video_st->gpu_device_string));
  2763. }
  2764. const char* video_driver_get_gpu_device_string(void)
  2765. {
  2766. video_driver_state_t *video_st = &video_driver_st;
  2767. return video_st->gpu_device_string;
  2768. }
  2769. void video_driver_set_gpu_api_version_string(const char *str)
  2770. {
  2771. video_driver_state_t *video_st = &video_driver_st;
  2772. strlcpy(video_st->gpu_api_version_string, str,
  2773. sizeof(video_st->gpu_api_version_string));
  2774. }
  2775. const char* video_driver_get_gpu_api_version_string(void)
  2776. {
  2777. video_driver_state_t *video_st = &video_driver_st;
  2778. return video_st->gpu_api_version_string;
  2779. }
  2780. bool video_driver_init_internal(bool *video_is_threaded, bool verbosity_enabled)
  2781. {
  2782. video_info_t video;
  2783. unsigned max_dim, scale, width, height;
  2784. video_viewport_t *custom_vp = NULL;
  2785. input_driver_t *tmp = NULL;
  2786. static uint16_t dummy_pixels[32] = {0};
  2787. runloop_state_t *runloop_st = runloop_state_get_ptr();
  2788. settings_t *settings = config_get_ptr();
  2789. input_driver_state_t *input_st = input_state_get_ptr();
  2790. video_driver_state_t *video_st = &video_driver_st;
  2791. struct retro_game_geometry *geom = &video_st->av_info.geometry;
  2792. const enum retro_pixel_format
  2793. video_driver_pix_fmt = video_st->pix_fmt;
  2794. #ifdef HAVE_VIDEO_FILTER
  2795. const char *path_softfilter_plugin = settings->paths.path_softfilter_plugin;
  2796. /* Init video filter only when game is running */
  2797. if (!string_is_empty(path_softfilter_plugin))
  2798. video_driver_filter_init(video_driver_pix_fmt, settings);
  2799. #endif
  2800. max_dim = MAX(geom->max_width, geom->max_height);
  2801. scale = next_pow2(max_dim) / RARCH_SCALE_BASE;
  2802. scale = MAX(scale, 1);
  2803. #ifdef HAVE_VIDEO_FILTER
  2804. if (video_st->state_filter)
  2805. scale = video_st->state_scale;
  2806. #endif
  2807. strlcpy(aspectratio_lut[ASPECT_RATIO_CONFIG].name,
  2808. msg_hash_to_str(MENU_ENUM_LABEL_VALUE_VIDEO_ASPECT_RATIO_CONFIG),
  2809. sizeof(aspectratio_lut[ASPECT_RATIO_CONFIG].name));
  2810. strlcpy(aspectratio_lut[ASPECT_RATIO_CORE].name,
  2811. msg_hash_to_str(MENU_ENUM_LABEL_VALUE_VIDEO_ASPECT_RATIO_CORE_PROVIDED),
  2812. sizeof(aspectratio_lut[ASPECT_RATIO_CORE].name));
  2813. strlcpy(aspectratio_lut[ASPECT_RATIO_CUSTOM].name,
  2814. msg_hash_to_str(MENU_ENUM_LABEL_VALUE_VIDEO_ASPECT_RATIO_CUSTOM),
  2815. sizeof(aspectratio_lut[ASPECT_RATIO_CUSTOM].name));
  2816. strlcpy(aspectratio_lut[ASPECT_RATIO_FULL].name,
  2817. msg_hash_to_str(MENU_ENUM_LABEL_VALUE_VIDEO_ASPECT_RATIO_FULL),
  2818. sizeof(aspectratio_lut[ASPECT_RATIO_FULL].name));
  2819. /* Update core-dependent aspect ratio values. */
  2820. video_driver_set_viewport_square_pixel(geom);
  2821. video_driver_set_viewport_core();
  2822. video_driver_set_viewport_config(geom,
  2823. settings->floats.video_aspect_ratio,
  2824. settings->bools.video_aspect_ratio_auto);
  2825. /* Update CUSTOM viewport. */
  2826. custom_vp = &settings->video_viewport_custom;
  2827. if (settings->uints.video_aspect_ratio_idx == ASPECT_RATIO_CUSTOM)
  2828. {
  2829. float default_aspect = aspectratio_lut[ASPECT_RATIO_CORE].value;
  2830. aspectratio_lut[ASPECT_RATIO_CUSTOM].value =
  2831. (custom_vp->width && custom_vp->height) ?
  2832. (float)custom_vp->width / custom_vp->height : default_aspect;
  2833. }
  2834. {
  2835. /* Guard against aspect ratio index possibly being out of bounds */
  2836. unsigned new_aspect_idx = settings->uints.video_aspect_ratio_idx;
  2837. if (new_aspect_idx > ASPECT_RATIO_END)
  2838. new_aspect_idx = settings->uints.video_aspect_ratio_idx = 0;
  2839. video_driver_set_aspect_ratio_value(
  2840. aspectratio_lut[new_aspect_idx].value);
  2841. }
  2842. if ( settings->bools.video_fullscreen
  2843. || (video_st->flags & VIDEO_FLAG_FORCE_FULLSCREEN))
  2844. {
  2845. width = settings->uints.video_fullscreen_x;
  2846. height = settings->uints.video_fullscreen_y;
  2847. }
  2848. else
  2849. {
  2850. #ifdef __WINRT__
  2851. if (is_running_on_xbox())
  2852. {
  2853. width = uwp_get_width();
  2854. height = uwp_get_height();
  2855. }
  2856. else
  2857. #endif
  2858. {
  2859. #if (defined(_WIN32) && !defined(_XBOX) && !defined(__WINRT__)) || \
  2860. (defined(HAVE_COCOA_METAL) && !defined(HAVE_COCOATOUCH))
  2861. bool window_custom_size_enable = settings->bools.video_window_save_positions;
  2862. #else
  2863. bool window_custom_size_enable = settings->bools.video_window_custom_size_enable;
  2864. #endif
  2865. /* TODO: remove when the new window resizing core is hooked */
  2866. if (window_custom_size_enable &&
  2867. settings->uints.window_position_width &&
  2868. settings->uints.window_position_height)
  2869. {
  2870. width = settings->uints.window_position_width;
  2871. height = settings->uints.window_position_height;
  2872. }
  2873. else
  2874. {
  2875. unsigned video_scale = settings->uints.video_scale;
  2876. /* Determine maximum allowed window dimensions
  2877. * NOTE: We cannot read the actual display
  2878. * metrics here, because the context driver
  2879. * has not yet been initialised... */
  2880. /* > Try explicitly configured values */
  2881. unsigned max_win_width = settings->uints.window_auto_width_max;
  2882. unsigned max_win_height = settings->uints.window_auto_height_max;
  2883. /* > Handle invalid settings */
  2884. if ((max_win_width == 0) || (max_win_height == 0))
  2885. {
  2886. /* > Try configured fullscreen width/height */
  2887. max_win_width = settings->uints.video_fullscreen_x;
  2888. max_win_height = settings->uints.video_fullscreen_y;
  2889. if ((max_win_width == 0) || (max_win_height == 0))
  2890. {
  2891. /* Maximum window width/size *must* be non-zero;
  2892. * if all else fails, used defined default
  2893. * maximum window size */
  2894. max_win_width = DEFAULT_WINDOW_AUTO_WIDTH_MAX;
  2895. max_win_height = DEFAULT_WINDOW_AUTO_HEIGHT_MAX;
  2896. }
  2897. }
  2898. /* rotated games send unrotated width/height and
  2899. * require special handling here because of it */
  2900. if ((retroarch_get_rotation() % 2))
  2901. {
  2902. /* Determine nominal window size based on
  2903. * core geometry */
  2904. if (settings->bools.video_force_aspect)
  2905. {
  2906. /* Do rounding here to simplify integer
  2907. * scale correctness. */
  2908. unsigned base_width = roundf(geom->base_width *
  2909. video_st->aspect_ratio);
  2910. width = base_width * video_scale;
  2911. }
  2912. else
  2913. width = geom->base_height * video_scale;
  2914. height = geom->base_width * video_scale;
  2915. }
  2916. else
  2917. {
  2918. /* Determine nominal window size based on
  2919. * core geometry */
  2920. if (settings->bools.video_force_aspect)
  2921. {
  2922. /* Do rounding here to simplify integer
  2923. * scale correctness. */
  2924. unsigned base_width = roundf(geom->base_height *
  2925. video_st->aspect_ratio);
  2926. width = base_width * video_scale;
  2927. }
  2928. else
  2929. width = geom->base_width * video_scale;
  2930. height = geom->base_height * video_scale;
  2931. }
  2932. /* Cap window size to maximum allowed values */
  2933. if ((width > max_win_width) || (height > max_win_height))
  2934. {
  2935. unsigned geom_width = (width > 0) ? width : 1;
  2936. unsigned geom_height = (height > 0) ? height : 1;
  2937. float geom_aspect = (float)geom_width / (float)geom_height;
  2938. float max_win_aspect = (float)max_win_width / (float)max_win_height;
  2939. if (geom_aspect > max_win_aspect)
  2940. {
  2941. width = max_win_width;
  2942. height = geom_height * max_win_width / geom_width;
  2943. /* Account for any possible rounding errors... */
  2944. if (height < 1)
  2945. height = 1;
  2946. else if (height > max_win_height)
  2947. height = max_win_height;
  2948. }
  2949. else
  2950. {
  2951. height = max_win_height;
  2952. width = geom_width * max_win_height / geom_height;
  2953. /* Account for any possible rounding errors... */
  2954. if (width < 1)
  2955. width = 1;
  2956. else if (width > max_win_width)
  2957. width = max_win_width;
  2958. }
  2959. }
  2960. }
  2961. }
  2962. }
  2963. if (width && height)
  2964. RARCH_LOG("[Video]: Set video size to: %ux%u.\n", width, height);
  2965. else
  2966. RARCH_LOG("[Video]: Set video size to: fullscreen.\n");
  2967. video_st->display_type = RARCH_DISPLAY_NONE;
  2968. video_st->display = 0;
  2969. video_st->display_userdata = 0;
  2970. video_st->window = 0;
  2971. video_st->scaler_ptr = video_driver_pixel_converter_init(
  2972. video_st->pix_fmt,
  2973. VIDEO_DRIVER_GET_HW_CONTEXT_INTERNAL(video_st),
  2974. RARCH_SCALE_BASE * scale);
  2975. video.width = width;
  2976. video.height = height;
  2977. video.fullscreen = settings->bools.video_fullscreen
  2978. || (video_st->flags & VIDEO_FLAG_FORCE_FULLSCREEN);
  2979. video.vsync = settings->bools.video_vsync
  2980. && (!(runloop_st->flags & RUNLOOP_FLAG_FORCE_NONBLOCK));
  2981. video.force_aspect = settings->bools.video_force_aspect;
  2982. video.swap_interval = runloop_get_video_swap_interval(
  2983. settings->uints.video_swap_interval);
  2984. video.adaptive_vsync = settings->bools.video_adaptive_vsync;
  2985. #ifdef GEKKO
  2986. video.viwidth = settings->uints.video_viwidth;
  2987. video.vfilter = settings->bools.video_vfilter;
  2988. #endif
  2989. video.smooth = settings->bools.video_smooth;
  2990. video.ctx_scaling = settings->bools.video_ctx_scaling;
  2991. video.input_scale = scale;
  2992. video.font_enable = settings->bools.video_font_enable;
  2993. video.font_size = settings->floats.video_font_size;
  2994. video.path_font = settings->paths.path_font;
  2995. #ifdef HAVE_VIDEO_FILTER
  2996. video.rgb32 = video_st->state_filter
  2997. ? (video_st->flags & VIDEO_FLAG_STATE_OUT_RGB32)
  2998. : (video_driver_pix_fmt == RETRO_PIXEL_FORMAT_XRGB8888);
  2999. #else
  3000. video.rgb32 =
  3001. (video_driver_pix_fmt == RETRO_PIXEL_FORMAT_XRGB8888);
  3002. #endif
  3003. video.parent = 0;
  3004. if (video.fullscreen)
  3005. video_st->flags |= VIDEO_FLAG_STARTED_FULLSCREEN;
  3006. else
  3007. video_st->flags &= ~VIDEO_FLAG_STARTED_FULLSCREEN;
  3008. /* Reset video frame count */
  3009. video_st->frame_count = 0;
  3010. tmp = input_state_get_ptr()->current_driver;
  3011. /* Need to grab the "real" video driver interface on a reinit. */
  3012. video_driver_find_driver(settings, "video driver", verbosity_enabled);
  3013. #ifdef HAVE_THREADS
  3014. video.is_threaded = VIDEO_DRIVER_IS_THREADED_INTERNAL(video_st);
  3015. *video_is_threaded = video.is_threaded;
  3016. if (video.is_threaded)
  3017. {
  3018. bool ret;
  3019. /* Can't do hardware rendering with threaded driver currently. */
  3020. RARCH_LOG("[Video]: Starting threaded video driver..\n");
  3021. ret = video_init_thread(
  3022. (const video_driver_t**)&video_st->current_video,
  3023. &video_st->data,
  3024. &input_state_get_ptr()->current_driver,
  3025. (void**)&input_state_get_ptr()->current_data,
  3026. video_st->current_video,
  3027. video);
  3028. if (!ret)
  3029. {
  3030. RARCH_ERR("[Video]: Cannot open threaded video driver.. Exiting..\n");
  3031. return false;
  3032. }
  3033. }
  3034. else
  3035. #endif
  3036. video_st->data = video_st->current_video->init(
  3037. &video,
  3038. &input_state_get_ptr()->current_driver,
  3039. (void**)&input_state_get_ptr()->current_data);
  3040. if (!video_st->data)
  3041. {
  3042. RARCH_ERR("[Video]: Cannot open video driver.. Exiting..\n");
  3043. return false;
  3044. }
  3045. video_st->poke = NULL;
  3046. if (video_st->current_video->poke_interface)
  3047. video_st->current_video->poke_interface(
  3048. video_st->data, &video_st->poke);
  3049. if (video_st->current_video->viewport_info &&
  3050. (!custom_vp->width ||
  3051. !custom_vp->height))
  3052. {
  3053. /* Force custom viewport to have sane parameters. */
  3054. custom_vp->width = width;
  3055. custom_vp->height = height;
  3056. video_st->current_video->viewport_info(video_st->data, custom_vp);
  3057. }
  3058. video_driver_set_rotation(retroarch_get_rotation() % 4);
  3059. video_st->current_video->suppress_screensaver(video_st->data,
  3060. settings->bools.ui_suspend_screensaver_enable);
  3061. if (!video_driver_init_input(tmp, settings, verbosity_enabled))
  3062. return false;
  3063. #ifdef HAVE_OVERLAY
  3064. input_overlay_deinit();
  3065. input_overlay_init();
  3066. #endif
  3067. if (!(runloop_st->current_core.flags & RETRO_CORE_FLAG_GAME_LOADED))
  3068. video_driver_cached_frame_set(&dummy_pixels, 4, 4, 8);
  3069. #if defined(PSP)
  3070. video_driver_set_texture_frame(&dummy_pixels, false, 1, 1, 1.0f);
  3071. #endif
  3072. video_context_driver_reset();
  3073. video_display_server_init(video_st->display_type);
  3074. if ((enum rotation)settings->uints.screen_orientation != ORIENTATION_NORMAL)
  3075. video_display_server_set_screen_orientation((enum rotation)settings->uints.screen_orientation);
  3076. /* Ensure that we preserve the 'grab mouse'
  3077. * state if it was enabled prior to driver
  3078. * (re-)initialisation */
  3079. if (input_st->flags & INP_FLAG_GRAB_MOUSE_STATE)
  3080. {
  3081. video_driver_hide_mouse();
  3082. if (input_driver_grab_mouse())
  3083. input_st->flags |= INP_FLAG_GRAB_MOUSE_STATE;
  3084. }
  3085. else if (video.fullscreen)
  3086. {
  3087. video_driver_hide_mouse();
  3088. if (!settings->bools.video_windowed_fullscreen)
  3089. if (input_driver_grab_mouse())
  3090. input_st->flags |= INP_FLAG_GRAB_MOUSE_STATE;
  3091. }
  3092. return true;
  3093. }
  3094. void video_driver_frame(const void *data, unsigned width,
  3095. unsigned height, size_t pitch)
  3096. {
  3097. char status_text[128];
  3098. static char video_driver_msg[256];
  3099. static retro_time_t last_time;
  3100. static retro_time_t curr_time;
  3101. static retro_time_t fps_time;
  3102. static retro_time_t frame_time_accumulator;
  3103. static float last_fps, frame_time;
  3104. static uint64_t last_used_memory, last_total_memory;
  3105. /* Mark the start of nonblock state for
  3106. * ignoring initial previous frame time */
  3107. static int8_t nonblock_active;
  3108. /* Initialise 'last_frame_duped' to 'true'
  3109. * to ensure that the first frame is rendered */
  3110. static bool last_frame_duped = true;
  3111. bool render_frame = true;
  3112. retro_time_t new_time;
  3113. video_frame_info_t video_info;
  3114. size_t buf_pos = 0;
  3115. video_driver_state_t *video_st = &video_driver_st;
  3116. runloop_state_t *runloop_st = runloop_state_get_ptr();
  3117. const enum retro_pixel_format
  3118. video_driver_pix_fmt = video_st->pix_fmt;
  3119. bool runloop_idle = runloop_st->flags & RUNLOOP_FLAG_IDLE;
  3120. bool video_driver_active = video_st->flags & VIDEO_FLAG_ACTIVE;
  3121. #if defined(HAVE_GFX_WIDGETS)
  3122. dispgfx_widget_t *p_dispwidget = dispwidget_get_ptr();
  3123. bool widgets_active = p_dispwidget->active;
  3124. #endif
  3125. recording_state_t *recording_st= recording_state_get_ptr();
  3126. status_text[0] = '\0';
  3127. video_driver_msg[0] = '\0';
  3128. if (!video_driver_active)
  3129. return;
  3130. new_time = cpu_features_get_time_usec();
  3131. if (data)
  3132. video_st->frame_cache_data = data;
  3133. video_st->frame_cache_width = width;
  3134. video_st->frame_cache_height = height;
  3135. video_st->frame_cache_pitch = pitch;
  3136. if (
  3137. video_st->scaler_ptr
  3138. && data
  3139. && (video_driver_pix_fmt == RETRO_PIXEL_FORMAT_0RGB1555)
  3140. && (data != RETRO_HW_FRAME_BUFFER_VALID)
  3141. && video_pixel_frame_scale(
  3142. video_st->scaler_ptr->scaler,
  3143. video_st->scaler_ptr->scaler_out,
  3144. data, width, height, pitch)
  3145. )
  3146. {
  3147. data = video_st->scaler_ptr->scaler_out;
  3148. pitch = video_st->scaler_ptr->scaler->out_stride;
  3149. }
  3150. video_driver_build_info(&video_info);
  3151. /* If fast forward is active and fast forward
  3152. * frame skipping is enabled, drop any frames
  3153. * that occur at a rate higher than the core-set
  3154. * refresh rate. However: We must always render
  3155. * the current frame when:
  3156. * - The menu is open
  3157. * - The last frame was NULL and the
  3158. * current frame is not (i.e. if core was
  3159. * previously sending duped frames, ensure
  3160. * that the next frame update is captured) */
  3161. if (video_info.input_driver_nonblock_state &&
  3162. video_info.fastforward_frameskip &&
  3163. !(video_info.menu_is_alive ||
  3164. (last_frame_duped && !!data)))
  3165. {
  3166. retro_time_t frame_time_accumulator_prev = frame_time_accumulator;
  3167. retro_time_t frame_time_delta = new_time - last_time;
  3168. /* Ignore initial previous frame time
  3169. * to prevent rubber band startup */
  3170. if (!nonblock_active)
  3171. nonblock_active = -1;
  3172. else if (nonblock_active < 0)
  3173. nonblock_active = 1;
  3174. /* Accumulate the elapsed time since the
  3175. * last frame */
  3176. if (nonblock_active > 0)
  3177. frame_time_accumulator += frame_time_delta;
  3178. /* Render frame if the accumulated time is
  3179. * greater than or equal to the expected
  3180. * core frame time */
  3181. render_frame = frame_time_accumulator >=
  3182. video_st->core_frame_time;
  3183. /* If frame is to be rendered, subtract
  3184. * expected frame time from accumulator */
  3185. if (render_frame)
  3186. {
  3187. frame_time_accumulator -= video_st->core_frame_time;
  3188. /* Prevent external frame limiters from
  3189. * pushing fast forward ratio down to 1x */
  3190. if (frame_time_accumulator + frame_time_accumulator_prev < video_st->core_frame_time)
  3191. frame_time_accumulator -= frame_time_delta;
  3192. /* If fast forward is working correctly,
  3193. * the actual frame time will always be
  3194. * less than the expected frame time.
  3195. * But if the host cannot run the core
  3196. * fast enough to achieve at least 1x
  3197. * speed then the frame time accumulator
  3198. * will never empty and may potentially
  3199. * overflow. If a 'runaway' accumulator
  3200. * is detected, we simply reset it */
  3201. if (frame_time_accumulator > video_st->core_frame_time)
  3202. frame_time_accumulator = 0;
  3203. }
  3204. }
  3205. else
  3206. {
  3207. nonblock_active = 0;
  3208. frame_time_accumulator = 0;
  3209. }
  3210. last_time = new_time;
  3211. last_frame_duped = !data;
  3212. /* Get the amount of frames per seconds. */
  3213. if (video_st->frame_count)
  3214. {
  3215. unsigned fps_update_interval =
  3216. video_info.fps_update_interval;
  3217. unsigned memory_update_interval =
  3218. video_info.memory_update_interval;
  3219. /* set this to 1 to avoid an offset issue */
  3220. unsigned write_index =
  3221. video_st->frame_time_count++ &
  3222. (MEASURE_FRAME_TIME_SAMPLES_COUNT - 1);
  3223. frame_time = new_time - fps_time;
  3224. video_st->frame_time_samples
  3225. [write_index] = frame_time;
  3226. fps_time = new_time;
  3227. if (video_info.fps_show)
  3228. {
  3229. status_text[0] = 'F';
  3230. status_text[1] = 'P';
  3231. status_text[2] = 'S';
  3232. status_text[3] = ':';
  3233. status_text[4] = ' ';
  3234. status_text[5] = '\0';
  3235. buf_pos = snprintf(
  3236. status_text + 5, sizeof(status_text) - 5,
  3237. "%6.2f", last_fps) + 5;
  3238. }
  3239. if (video_info.framecount_show)
  3240. {
  3241. if (status_text[buf_pos-1] != '\0')
  3242. {
  3243. status_text[buf_pos ] = ' ';
  3244. status_text[buf_pos+1] = '|';
  3245. status_text[buf_pos+2] = '|';
  3246. status_text[buf_pos+3] = ' ';
  3247. status_text[buf_pos+4] = '\0';
  3248. }
  3249. buf_pos = strlcat(status_text,
  3250. msg_hash_to_str(MSG_FRAMES),
  3251. sizeof(status_text));
  3252. status_text[buf_pos ] = ':';
  3253. status_text[++buf_pos] = ' ';
  3254. status_text[++buf_pos] = '\0';
  3255. buf_pos += snprintf(status_text + buf_pos,
  3256. sizeof(status_text) - buf_pos,
  3257. "%" PRIu64, (uint64_t)video_st->frame_count);
  3258. }
  3259. if (video_info.memory_show)
  3260. {
  3261. if ((video_st->frame_count % memory_update_interval) == 0)
  3262. {
  3263. last_total_memory = frontend_driver_get_total_memory();
  3264. last_used_memory = last_total_memory - frontend_driver_get_free_memory();
  3265. }
  3266. if (status_text[buf_pos-1] != '\0')
  3267. {
  3268. status_text[buf_pos] = ' ';
  3269. status_text[++buf_pos] = '|';
  3270. status_text[++buf_pos] = '|';
  3271. status_text[++buf_pos] = ' ';
  3272. status_text[++buf_pos] = '\0';
  3273. }
  3274. status_text[buf_pos ] = 'M';
  3275. status_text[++buf_pos] = 'E';
  3276. status_text[++buf_pos] = 'M';
  3277. status_text[++buf_pos] = ':';
  3278. status_text[++buf_pos] = ' ';
  3279. status_text[++buf_pos] = '\0';
  3280. buf_pos += snprintf(
  3281. status_text + buf_pos, sizeof(status_text) - buf_pos, "%.2f/%.2f", last_used_memory / (1024.0f * 1024.0f),
  3282. last_total_memory / (1024.0f * 1024.0f));
  3283. status_text[buf_pos ] = 'M';
  3284. status_text[++buf_pos] = 'B';
  3285. status_text[++buf_pos] = '\0';
  3286. }
  3287. if ((video_st->frame_count % fps_update_interval) == 0)
  3288. {
  3289. last_fps = TIME_TO_FPS(curr_time, new_time,
  3290. fps_update_interval);
  3291. video_st->window_title_len = strlcpy(video_st->window_title,
  3292. video_st->title_buf,
  3293. sizeof(video_st->window_title));
  3294. if (!string_is_empty(status_text))
  3295. {
  3296. video_st->window_title[video_st->window_title_len ] = ' ';
  3297. video_st->window_title[video_st->window_title_len+1] = '|';
  3298. video_st->window_title[video_st->window_title_len+2] = '|';
  3299. video_st->window_title[video_st->window_title_len+3] = ' ';
  3300. video_st->window_title[video_st->window_title_len+4] = '\0';
  3301. video_st->window_title_len = strlcat(video_st->window_title,
  3302. status_text, sizeof(video_st->window_title));
  3303. }
  3304. curr_time = new_time;
  3305. video_st->flags |= VIDEO_FLAG_WINDOW_TITLE_UPDATE;
  3306. }
  3307. }
  3308. else
  3309. {
  3310. curr_time = fps_time = new_time;
  3311. video_st->window_title_len = strlcpy(
  3312. video_st->window_title,
  3313. video_st->title_buf,
  3314. sizeof(video_st->window_title));
  3315. if (video_info.fps_show)
  3316. buf_pos = strlcpy(status_text,
  3317. msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NOT_AVAILABLE),
  3318. sizeof(status_text));
  3319. video_st->flags |= VIDEO_FLAG_WINDOW_TITLE_UPDATE;
  3320. }
  3321. /* Add core status message to status text */
  3322. if (video_info.core_status_msg_show)
  3323. {
  3324. /* Note: We need to lock a mutex here. Strictly
  3325. * speaking, runloop_core_status_msg is not part
  3326. * of the message queue, but:
  3327. * - It may be implemented as a queue in the future
  3328. * - It seems unnecessary to create a new slock_t
  3329. * object for this type of message when
  3330. * _runloop_msg_queue_lock is already available
  3331. * We therefore just call runloop_msg_queue_lock()/
  3332. * runloop_msg_queue_unlock() in this case */
  3333. RUNLOOP_MSG_QUEUE_LOCK(runloop_st);
  3334. /* Check whether duration timer has elapsed */
  3335. runloop_st->core_status_msg.duration -= anim_get_ptr()->delta_time;
  3336. if (runloop_st->core_status_msg.duration < 0.0f)
  3337. {
  3338. runloop_st->core_status_msg.str[0] = '\0';
  3339. runloop_st->core_status_msg.priority = 0;
  3340. runloop_st->core_status_msg.duration = 0.0f;
  3341. runloop_st->core_status_msg.set = false;
  3342. }
  3343. else
  3344. {
  3345. /* If status text is already set, add status
  3346. * message at the end */
  3347. if (!string_is_empty(status_text))
  3348. {
  3349. status_text[buf_pos ] = ' ';
  3350. status_text[buf_pos+1] = '|';
  3351. status_text[buf_pos+2] = '|';
  3352. status_text[buf_pos+3] = ' ';
  3353. status_text[buf_pos+4] = '\0';
  3354. buf_pos = strlcat(status_text,
  3355. runloop_st->core_status_msg.str,
  3356. sizeof(status_text));
  3357. }
  3358. else
  3359. buf_pos = strlcpy(status_text,
  3360. runloop_st->core_status_msg.str,
  3361. sizeof(status_text));
  3362. }
  3363. RUNLOOP_MSG_QUEUE_UNLOCK(runloop_st);
  3364. }
  3365. /* Slightly messy code,
  3366. * but we really need to do processing before blocking on VSync
  3367. * for best possible scheduling.
  3368. */
  3369. if (
  3370. (
  3371. #ifdef HAVE_VIDEO_FILTER
  3372. !video_st->state_filter ||
  3373. #endif
  3374. !video_info.post_filter_record
  3375. || !data
  3376. || video_st->record_gpu_buffer
  3377. ) && recording_st->data
  3378. && recording_st->driver
  3379. && recording_st->driver->push_video)
  3380. recording_dump_frame(
  3381. data, width, height,
  3382. pitch, runloop_idle);
  3383. #ifdef HAVE_VIDEO_FILTER
  3384. if (render_frame && data && video_st->state_filter)
  3385. {
  3386. unsigned output_width = 0;
  3387. unsigned output_height = 0;
  3388. unsigned output_pitch = 0;
  3389. rarch_softfilter_get_output_size(video_st->state_filter,
  3390. &output_width, &output_height, width, height);
  3391. output_pitch = (output_width) * video_st->state_out_bpp;
  3392. rarch_softfilter_process(video_st->state_filter,
  3393. video_st->state_buffer, output_pitch,
  3394. data, width, height, pitch);
  3395. if (video_info.post_filter_record
  3396. && recording_st->data
  3397. && recording_st->driver
  3398. && recording_st->driver->push_video)
  3399. recording_dump_frame(
  3400. video_st->state_buffer,
  3401. output_width, output_height, output_pitch,
  3402. runloop_idle);
  3403. data = video_st->state_buffer;
  3404. width = output_width;
  3405. height = output_height;
  3406. pitch = output_pitch;
  3407. }
  3408. #endif
  3409. if (runloop_st->msg_queue_delay > 0)
  3410. runloop_st->msg_queue_delay--;
  3411. else if (runloop_st->msg_queue_size > 0)
  3412. {
  3413. /* If widgets are currently enabled, then
  3414. * messages were pushed to the queue before
  3415. * widgets were initialised - in this case, the
  3416. * first item in the message queue should be
  3417. * extracted and pushed to the widget message
  3418. * queue instead */
  3419. #if defined(HAVE_GFX_WIDGETS)
  3420. if (widgets_active)
  3421. {
  3422. msg_queue_entry_t msg_entry;
  3423. bool msg_found = false;
  3424. RUNLOOP_MSG_QUEUE_LOCK(runloop_st);
  3425. msg_found = msg_queue_extract(
  3426. &runloop_st->msg_queue, &msg_entry);
  3427. runloop_st->msg_queue_size = msg_queue_size(
  3428. &runloop_st->msg_queue);
  3429. RUNLOOP_MSG_QUEUE_UNLOCK(runloop_st);
  3430. if (msg_found)
  3431. gfx_widgets_msg_queue_push(
  3432. NULL,
  3433. msg_entry.msg,
  3434. roundf((float)msg_entry.duration / 60.0f * 1000.0f),
  3435. msg_entry.title,
  3436. msg_entry.icon,
  3437. msg_entry.category,
  3438. msg_entry.prio,
  3439. false,
  3440. video_info.menu_is_alive
  3441. );
  3442. }
  3443. /* ...otherwise, just output message via
  3444. * regular OSD notification text (if enabled) */
  3445. else if (video_info.font_enable)
  3446. #else
  3447. if (video_info.font_enable)
  3448. #endif
  3449. {
  3450. const char *msg = NULL;
  3451. RUNLOOP_MSG_QUEUE_LOCK(runloop_st);
  3452. msg = msg_queue_pull(&runloop_st->msg_queue);
  3453. runloop_st->msg_queue_size = msg_queue_size(&runloop_st->msg_queue);
  3454. if (msg)
  3455. strlcpy(video_driver_msg, msg, sizeof(video_driver_msg));
  3456. RUNLOOP_MSG_QUEUE_UNLOCK(runloop_st);
  3457. }
  3458. }
  3459. if (render_frame && video_info.statistics_show)
  3460. {
  3461. audio_statistics_t audio_stats;
  3462. char latency_stats[128];
  3463. char tmp[128];
  3464. size_t len;
  3465. double stddev = 0.0;
  3466. float scale = 1.0f;
  3467. float font_size_scale = video_info.font_size / 100;
  3468. struct retro_system_av_info *av_info = &video_st->av_info;
  3469. unsigned red = 235;
  3470. unsigned green = 235;
  3471. unsigned blue = 235;
  3472. unsigned alpha = 255;
  3473. scale = ((float)video_info.height / 480)
  3474. * 0.50f * (DEFAULT_FONT_SIZE / video_info.font_size);
  3475. scale = (scale < font_size_scale)
  3476. ? font_size_scale : scale;
  3477. scale = (scale > 1.00f)
  3478. ? 1.00f : scale;
  3479. if (scale > font_size_scale)
  3480. {
  3481. scale *= 100;
  3482. scale = ceil(scale);
  3483. scale /= 100;
  3484. }
  3485. audio_stats.samples = 0;
  3486. audio_stats.average_buffer_saturation = 0.0f;
  3487. audio_stats.std_deviation_percentage = 0.0f;
  3488. audio_stats.close_to_underrun = 0.0f;
  3489. audio_stats.close_to_blocking = 0.0f;
  3490. video_monitor_fps_statistics(NULL, &stddev, NULL);
  3491. video_info.osd_stat_params.x = 0.008f;
  3492. video_info.osd_stat_params.y = 0.960f;
  3493. video_info.osd_stat_params.text_align = TEXT_ALIGN_LEFT;
  3494. video_info.osd_stat_params.scale = scale;
  3495. video_info.osd_stat_params.full_screen = true;
  3496. video_info.osd_stat_params.drop_x = (video_info.font_size / DEFAULT_FONT_SIZE) * 3;
  3497. video_info.osd_stat_params.drop_y = (video_info.font_size / DEFAULT_FONT_SIZE) * -3;
  3498. video_info.osd_stat_params.drop_mod = 0.1f;
  3499. video_info.osd_stat_params.drop_alpha = 0.9f;
  3500. video_info.osd_stat_params.color = COLOR_ABGR(
  3501. alpha, blue, green, red);
  3502. audio_compute_buffer_statistics(&audio_stats);
  3503. latency_stats[0] = '\0';
  3504. tmp[0] = '\0';
  3505. len = 0;
  3506. /* TODO/FIXME - localize */
  3507. if (video_st->frame_delay_target > 0)
  3508. len = snprintf(tmp, sizeof(latency_stats),
  3509. " Frame Delay: %2u ms\n"
  3510. " - Target: %2u ms\n",
  3511. video_st->frame_delay_effective,
  3512. video_st->frame_delay_target);
  3513. if (video_info.runahead && !video_info.runahead_second_instance)
  3514. len = snprintf(tmp + len, sizeof(latency_stats),
  3515. " Run-Ahead: %2u frames\n"
  3516. " - Single Instance\n",
  3517. video_info.runahead_frames);
  3518. else if (video_info.runahead && video_info.runahead_second_instance)
  3519. len = snprintf(tmp + len, sizeof(latency_stats),
  3520. " Run-Ahead: %2u frames\n"
  3521. " - Second Instance\n",
  3522. video_info.runahead_frames);
  3523. else if (video_info.preemptive_frames)
  3524. len = snprintf(tmp + len, sizeof(latency_stats),
  3525. " Run-Ahead: %2u frames\n"
  3526. " - Preemptive Frames\n",
  3527. video_info.runahead_frames);
  3528. if (len)
  3529. {
  3530. /* TODO/FIXME - localize */
  3531. strlcpy(latency_stats, "LATENCY\n", sizeof(latency_stats));
  3532. strlcat(latency_stats, tmp, sizeof(latency_stats));
  3533. }
  3534. /* TODO/FIXME - localize */
  3535. snprintf(video_info.stat_text,
  3536. sizeof(video_info.stat_text),
  3537. "CORE AV_INFO\n"
  3538. " Size: %u x %u\n"
  3539. " Max Size: %u x %u\n"
  3540. " Aspect: %3.3f\n"
  3541. " FPS: %3.2f\n"
  3542. " Sample Rate: %6.2f\n"
  3543. "VIDEO: %s\n"
  3544. " Viewport: %d x %d\n"
  3545. " Refresh: %5.2f hz\n"
  3546. " Frame Rate: %5.2f fps\n"
  3547. " Frame Time: %5.2f ms\n"
  3548. " - Deviation: %5.2f %%\n"
  3549. " Frames: %5" PRIu64"\n"
  3550. "AUDIO: %s\n"
  3551. " Saturation: %5.2f %%\n"
  3552. " Deviation: %5.2f %%\n"
  3553. " Underrun: %5.2f %%\n"
  3554. " Blocking: %5.2f %%\n"
  3555. " Samples: %5d\n"
  3556. "%s",
  3557. av_info->geometry.base_width,
  3558. av_info->geometry.base_height,
  3559. av_info->geometry.max_width,
  3560. av_info->geometry.max_height,
  3561. av_info->geometry.aspect_ratio,
  3562. av_info->timing.fps,
  3563. av_info->timing.sample_rate,
  3564. video_st->current_video->ident,
  3565. video_info.width,
  3566. video_info.height,
  3567. video_info.refresh_rate,
  3568. last_fps,
  3569. frame_time / 1000.0f,
  3570. 100.0f * stddev,
  3571. video_st->frame_count,
  3572. audio_state_get_ptr()->current_audio->ident,
  3573. audio_stats.average_buffer_saturation,
  3574. audio_stats.std_deviation_percentage,
  3575. audio_stats.close_to_underrun,
  3576. audio_stats.close_to_blocking,
  3577. audio_stats.samples,
  3578. latency_stats);
  3579. /* TODO/FIXME - add OSD chat text here */
  3580. }
  3581. if (render_frame
  3582. && video_st->current_video
  3583. && video_st->current_video->frame)
  3584. {
  3585. if (video_st->current_video->frame(
  3586. video_st->data, data, width, height,
  3587. video_st->frame_count, (unsigned)pitch,
  3588. video_info.menu_screensaver_active || video_info.notifications_hidden ? "" : video_driver_msg,
  3589. &video_info))
  3590. video_st->flags |= VIDEO_FLAG_ACTIVE;
  3591. else
  3592. video_st->flags &= ~VIDEO_FLAG_ACTIVE;
  3593. }
  3594. video_st->frame_count++;
  3595. /* Display the status text, with a higher priority. */
  3596. if ( ( video_info.fps_show
  3597. || video_info.framecount_show
  3598. || video_info.memory_show
  3599. || video_info.core_status_msg_show
  3600. )
  3601. && !video_info.menu_screensaver_active
  3602. && !video_info.notifications_hidden
  3603. )
  3604. {
  3605. #if defined(HAVE_GFX_WIDGETS)
  3606. if (widgets_active)
  3607. strlcpy(
  3608. p_dispwidget->gfx_widgets_status_text,
  3609. status_text,
  3610. sizeof(p_dispwidget->gfx_widgets_status_text)
  3611. );
  3612. else
  3613. #endif
  3614. {
  3615. runloop_msg_queue_push(status_text, 2, 1, true, NULL,
  3616. MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
  3617. }
  3618. }
  3619. #if defined(HAVE_CRTSWITCHRES)
  3620. /* trigger set resolution*/
  3621. if (video_info.crt_switch_resolution)
  3622. {
  3623. unsigned native_width = width;
  3624. bool dynamic_super_width = false;
  3625. video_st->flags |= VIDEO_FLAG_CRT_SWITCHING_ACTIVE;
  3626. switch (video_info.crt_switch_resolution_super)
  3627. {
  3628. case 2560:
  3629. case 3840:
  3630. case 1920:
  3631. width = video_info.crt_switch_resolution_super;
  3632. dynamic_super_width = false;
  3633. break;
  3634. case 1:
  3635. dynamic_super_width = true;
  3636. break;
  3637. default:
  3638. break;
  3639. }
  3640. crt_switch_res_core(
  3641. &video_st->crt_switch_st,
  3642. native_width, width,
  3643. height,
  3644. video_st->core_hz,
  3645. video_st->av_info.geometry.aspect_ratio < 1.0 ? true : false,
  3646. video_info.crt_switch_resolution,
  3647. video_info.crt_switch_center_adjust,
  3648. video_info.crt_switch_porch_adjust,
  3649. video_info.monitor_index,
  3650. dynamic_super_width,
  3651. video_info.crt_switch_resolution_super,
  3652. video_info.crt_switch_hires_menu);
  3653. }
  3654. else if (!video_info.crt_switch_resolution)
  3655. #endif
  3656. video_st->flags &= ~VIDEO_FLAG_CRT_SWITCHING_ACTIVE;
  3657. }
  3658. static void video_driver_reinit_context(settings_t *settings, int flags)
  3659. {
  3660. /* RARCH_DRIVER_CTL_UNINIT clears the callback struct so we
  3661. * need to make sure to keep a copy */
  3662. struct retro_hw_render_callback hwr_copy;
  3663. video_driver_state_t *video_st = &video_driver_st;
  3664. struct retro_hw_render_callback *hwr =
  3665. VIDEO_DRIVER_GET_HW_CONTEXT_INTERNAL(video_st);
  3666. const struct retro_hw_render_context_negotiation_interface *iface =
  3667. video_st->hw_render_context_negotiation;
  3668. memcpy(&hwr_copy, hwr, sizeof(hwr_copy));
  3669. driver_uninit(flags);
  3670. memcpy(hwr, &hwr_copy, sizeof(*hwr));
  3671. video_st->hw_render_context_negotiation = iface;
  3672. drivers_init(settings, flags, verbosity_is_enabled());
  3673. }
  3674. void video_driver_reinit(int flags)
  3675. {
  3676. settings_t *settings = config_get_ptr();
  3677. video_driver_state_t *video_st = &video_driver_st;
  3678. struct retro_hw_render_callback *hwr =
  3679. VIDEO_DRIVER_GET_HW_CONTEXT_INTERNAL(video_st);
  3680. if (hwr->cache_context != false)
  3681. video_st->flags |= VIDEO_FLAG_CACHE_CONTEXT;
  3682. else
  3683. video_st->flags &= ~VIDEO_FLAG_CACHE_CONTEXT;
  3684. video_st->flags &= ~VIDEO_FLAG_CACHE_CONTEXT_ACK;
  3685. video_driver_reinit_context(settings, flags);
  3686. video_st->flags &= ~VIDEO_FLAG_CACHE_CONTEXT;
  3687. }
  3688. uint32_t video_driver_get_st_flags(void)
  3689. {
  3690. video_driver_state_t *video_st = &video_driver_st;
  3691. return video_st->flags;
  3692. }
  3693. #define FRAME_DELAY_AUTO_DEBUG 0
  3694. void video_frame_delay_auto(video_driver_state_t *video_st, video_frame_delay_auto_t *vfda)
  3695. {
  3696. int i;
  3697. retro_time_t frame_time_avg = 0;
  3698. retro_time_t frame_time_delta = 0;
  3699. uint16_t frame_time_index = (video_st->frame_time_count & (MEASURE_FRAME_TIME_SAMPLES_COUNT - 1));
  3700. retro_time_t frame_time_target = 1000000.0f / vfda->refresh_rate;
  3701. retro_time_t frame_time_limit_mar = frame_time_target * 1.15f;
  3702. retro_time_t frame_time_limit_min = frame_time_target * 1.30f;
  3703. retro_time_t frame_time_limit_med = frame_time_target * 1.50f;
  3704. retro_time_t frame_time_limit_max = frame_time_target * 1.85f;
  3705. retro_time_t frame_time_limit_cap = frame_time_target * 3.00f;
  3706. retro_time_t frame_time_limit_ign = frame_time_target * 3.25f;
  3707. uint8_t frame_time_frames = vfda->frame_time_interval;
  3708. uint8_t frame_time_count_pos = 0;
  3709. uint8_t frame_time_count_min = 0;
  3710. uint8_t frame_time_count_med = 0;
  3711. uint8_t frame_time_count_max = 0;
  3712. uint8_t frame_time_count_ign = 0;
  3713. retro_time_t frame_time_min = frame_time_target;
  3714. retro_time_t frame_time_max = frame_time_target;
  3715. /* Calculate average frame time */
  3716. for (i = 1; i < frame_time_frames + 1; i++)
  3717. {
  3718. retro_time_t frame_time_i = 0;
  3719. if (i > frame_time_index)
  3720. continue;
  3721. frame_time_i = video_st->frame_time_samples[frame_time_index - i];
  3722. if (frame_time_max < frame_time_i)
  3723. frame_time_max = frame_time_i;
  3724. if (frame_time_min > frame_time_i)
  3725. frame_time_min = frame_time_i;
  3726. /* Count frames over the target */
  3727. if (frame_time_i > frame_time_target)
  3728. {
  3729. frame_time_count_pos++;
  3730. if (frame_time_i > frame_time_limit_min)
  3731. frame_time_count_min++;
  3732. if (frame_time_i > frame_time_limit_med)
  3733. frame_time_count_med++;
  3734. if (frame_time_i > frame_time_limit_max)
  3735. frame_time_count_max++;
  3736. if (frame_time_i > frame_time_limit_ign)
  3737. frame_time_count_ign++;
  3738. /* Limit maximum to prevent false positives */
  3739. if (frame_time_i > frame_time_limit_cap)
  3740. frame_time_i = frame_time_limit_cap;
  3741. }
  3742. frame_time_avg += frame_time_i;
  3743. }
  3744. frame_time_avg /= frame_time_frames;
  3745. frame_time_delta = frame_time_max - frame_time_min;
  3746. /* Ignore values when core is doing internal frame skipping */
  3747. if (frame_time_count_ign > 0)
  3748. frame_time_avg = frame_time_target;
  3749. /* Special handlings for different video driver frame timings */
  3750. if ( ( frame_time_avg > frame_time_target
  3751. && frame_time_avg < frame_time_limit_med)
  3752. || (frame_time_delta > frame_time_limit_max))
  3753. {
  3754. uint8_t frame_time_frames_half = frame_time_frames / 2;
  3755. /* Ensure outcome on certain conditions */
  3756. int8_t mode = 0;
  3757. /* All interval frames are above the target */
  3758. if (frame_time_count_pos == frame_time_frames)
  3759. mode = 1;
  3760. /* At least half of interval frames are above minimum level */
  3761. else if (frame_time_count_min >= frame_time_frames_half)
  3762. mode = 2;
  3763. /* D3Dx stripe equalizer */
  3764. else if (
  3765. (frame_time_count_pos == frame_time_frames_half)
  3766. && ( frame_time_count_min > 0
  3767. || frame_time_count_med > 0)
  3768. && (frame_time_delta > frame_time_target / 2.50f)
  3769. )
  3770. mode = 3;
  3771. /* Boost med/max spikes */
  3772. else if (
  3773. (frame_time_count_pos > frame_time_frames_half)
  3774. && ( frame_time_count_med > 1
  3775. || frame_time_count_max > 0)
  3776. && (frame_time_avg > frame_time_limit_mar)
  3777. && (frame_time_delta < frame_time_limit_min)
  3778. )
  3779. mode = 4;
  3780. /* Boost min/med spikes */
  3781. else if (
  3782. (frame_time_count_pos >= frame_time_frames_half)
  3783. && ( frame_time_count_min > 1
  3784. || frame_time_count_med > 0)
  3785. && (frame_time_count_max == 0)
  3786. && (frame_time_avg > frame_time_limit_mar)
  3787. )
  3788. mode = 5;
  3789. /* Ignore */
  3790. else if (
  3791. (frame_time_delta > frame_time_target)
  3792. && (frame_time_count_med == 0)
  3793. )
  3794. mode = -1;
  3795. if (mode > 0 && frame_time_avg < frame_time_limit_med)
  3796. {
  3797. #if FRAME_DELAY_AUTO_DEBUG
  3798. RARCH_LOG("[Video]: Frame delay nudge %d by mode %d.\n", frame_time_avg, mode);
  3799. #endif
  3800. frame_time_avg = frame_time_limit_med;
  3801. }
  3802. else if (mode < 0)
  3803. {
  3804. #if FRAME_DELAY_AUTO_DEBUG
  3805. RARCH_LOG("[Video]: Frame delay ignore %d.\n", frame_time_avg);
  3806. #endif
  3807. frame_time_avg = frame_time_target;
  3808. }
  3809. }
  3810. /* Final output decision */
  3811. if (frame_time_avg > frame_time_limit_min)
  3812. {
  3813. uint8_t delay_decrease = 1;
  3814. /* Increase decrease the more frame time is off target */
  3815. if ( frame_time_avg > frame_time_limit_med
  3816. && video_st->frame_delay_effective > delay_decrease)
  3817. {
  3818. delay_decrease++;
  3819. if ( frame_time_avg > frame_time_limit_max
  3820. && video_st->frame_delay_effective > delay_decrease)
  3821. delay_decrease++;
  3822. }
  3823. vfda->delay_decrease = delay_decrease;
  3824. }
  3825. vfda->frame_time_avg = frame_time_avg;
  3826. vfda->frame_time_target = frame_time_target;
  3827. #if FRAME_DELAY_AUTO_DEBUG
  3828. if (frame_time_index > frame_time_frames)
  3829. RARCH_LOG("[Video]: %5d / pos:%d min:%d med:%d max:%d ign:%d / delta:%5d = %5d %5d %5d %5d %5d %5d %5d %5d\n",
  3830. frame_time_avg,
  3831. frame_time_count_pos,
  3832. frame_time_count_min,
  3833. frame_time_count_med,
  3834. frame_time_count_max,
  3835. frame_time_count_ign,
  3836. frame_time_max - frame_time_min,
  3837. video_st->frame_time_samples[frame_time_index - 1],
  3838. video_st->frame_time_samples[frame_time_index - 2],
  3839. video_st->frame_time_samples[frame_time_index - 3],
  3840. video_st->frame_time_samples[frame_time_index - 4],
  3841. video_st->frame_time_samples[frame_time_index - 5],
  3842. video_st->frame_time_samples[frame_time_index - 6],
  3843. video_st->frame_time_samples[frame_time_index - 7],
  3844. video_st->frame_time_samples[frame_time_index - 8]
  3845. );
  3846. #endif
  3847. }