video_thread_wrapper.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /* RetroArch - A frontend for libretro.
  2. * Copyright (C) 2010-2014 - Hans-Kristian Arntzen
  3. * Copyright (C) 2011-2017 - 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 <stdlib.h>
  17. #include <string.h>
  18. #include <limits.h>
  19. #include <math.h>
  20. #include <compat/strl.h>
  21. #include <features/features_cpu.h>
  22. #include <string/stdstring.h>
  23. #include "video_driver.h"
  24. #include "video_thread_wrapper.h"
  25. #ifdef HAVE_FONT
  26. #include "font_driver.h"
  27. #endif
  28. #include "../retroarch.h"
  29. #include "../runloop.h"
  30. #include "../verbosity.h"
  31. static void *video_thread_init_never_call(const video_info_t *video,
  32. input_driver_t **input, void **input_data)
  33. {
  34. (void)video;
  35. (void)input;
  36. (void)input_data;
  37. RARCH_ERR("Sanity check fail! Threaded mustn't be reinit.\n");
  38. abort();
  39. return NULL;
  40. }
  41. /* thread -> user */
  42. static void video_thread_reply(thread_video_t *thr, const thread_packet_t *pkt)
  43. {
  44. slock_lock(thr->lock);
  45. thr->cmd_data = *pkt;
  46. thr->reply_cmd = pkt->type;
  47. thr->send_cmd = CMD_VIDEO_NONE;
  48. scond_signal(thr->cond_cmd);
  49. slock_unlock(thr->lock);
  50. }
  51. /* user -> thread */
  52. static void video_thread_send_packet(thread_video_t *thr,
  53. const thread_packet_t *pkt)
  54. {
  55. slock_lock(thr->lock);
  56. thr->cmd_data = *pkt;
  57. thr->send_cmd = pkt->type;
  58. thr->reply_cmd = CMD_VIDEO_NONE;
  59. scond_signal(thr->cond_thread);
  60. slock_unlock(thr->lock);
  61. }
  62. /* user -> thread */
  63. static void video_thread_wait_reply(thread_video_t *thr, thread_packet_t *pkt)
  64. {
  65. slock_lock(thr->lock);
  66. while (pkt->type != thr->reply_cmd)
  67. scond_wait(thr->cond_cmd, thr->lock);
  68. *pkt = thr->cmd_data;
  69. thr->cmd_data.type = CMD_VIDEO_NONE;
  70. slock_unlock(thr->lock);
  71. }
  72. /* user -> thread */
  73. static void video_thread_send_and_wait_user_to_thread(thread_video_t *thr, thread_packet_t *pkt)
  74. {
  75. video_thread_send_packet(thr, pkt);
  76. video_thread_wait_reply(thr, pkt);
  77. }
  78. static void thread_update_driver_state(thread_video_t *thr)
  79. {
  80. #ifdef HAVE_MENU
  81. if (thr->texture.frame_updated)
  82. {
  83. if (thr->driver_data && thr->poke && thr->poke->set_texture_frame)
  84. thr->poke->set_texture_frame(thr->driver_data,
  85. thr->texture.frame, thr->texture.rgb32,
  86. thr->texture.width, thr->texture.height,
  87. thr->texture.alpha);
  88. thr->texture.frame_updated = false;
  89. }
  90. if (thr->driver_data && thr->poke && thr->poke->set_texture_enable)
  91. thr->poke->set_texture_enable(thr->driver_data,
  92. thr->texture.enable, thr->texture.full_screen);
  93. #endif
  94. #ifdef HAVE_OVERLAY
  95. slock_lock(thr->alpha_lock);
  96. if (thr->alpha_update)
  97. {
  98. if (thr->driver_data && thr->overlay && thr->overlay->set_alpha)
  99. {
  100. int i;
  101. for (i = 0; i < (int)thr->alpha_mods; i++)
  102. thr->overlay->set_alpha(thr->driver_data, i, thr->alpha_mod[i]);
  103. }
  104. thr->alpha_update = false;
  105. }
  106. slock_unlock(thr->alpha_lock);
  107. #endif
  108. if (thr->apply_state_changes)
  109. {
  110. if (thr->driver_data && thr->poke && thr->poke->apply_state_changes)
  111. thr->poke->apply_state_changes(thr->driver_data);
  112. thr->apply_state_changes = false;
  113. }
  114. }
  115. /* returns true when video_thread_loop should quit */
  116. static bool video_thread_handle_packet(
  117. thread_video_t *thr,
  118. const thread_packet_t *incoming)
  119. {
  120. thread_packet_t pkt = *incoming;
  121. switch (pkt.type)
  122. {
  123. case CMD_INIT:
  124. if (thr->driver && thr->driver->init)
  125. {
  126. thr->driver_data = thr->driver->init(&thr->info,
  127. thr->input, thr->input_data);
  128. if (thr->driver_data && thr->driver->viewport_info)
  129. thr->driver->viewport_info(thr->driver_data, &thr->vp);
  130. }
  131. else
  132. thr->driver_data = NULL;
  133. pkt.data.b = (thr->driver_data != NULL);
  134. video_thread_reply(thr, &pkt);
  135. break;
  136. case CMD_FREE:
  137. if (thr->driver_data && thr->driver && thr->driver->free)
  138. thr->driver->free(thr->driver_data);
  139. thr->driver_data = NULL;
  140. video_thread_reply(thr, &pkt);
  141. return true;
  142. case CMD_SET_ROTATION:
  143. if (thr->driver_data && thr->driver && thr->driver->set_rotation)
  144. thr->driver->set_rotation(thr->driver_data, pkt.data.i);
  145. video_thread_reply(thr, &pkt);
  146. break;
  147. case CMD_READ_VIEWPORT:
  148. if (thr->driver_data && thr->driver &&
  149. thr->driver->viewport_info && thr->driver->read_viewport)
  150. {
  151. struct video_viewport vp;
  152. vp.x = 0;
  153. vp.y = 0;
  154. vp.width = 0;
  155. vp.height = 0;
  156. vp.full_width = 0;
  157. vp.full_height = 0;
  158. thr->driver->viewport_info(thr->driver_data, &vp);
  159. if (!memcmp(&vp, &thr->read_vp, sizeof(vp)))
  160. {
  161. /* We can read safely
  162. *
  163. * read_viewport() in GL driver calls
  164. * 'cached frame render' to be able to read from
  165. * back buffer.
  166. *
  167. * This means frame() callback in threaded wrapper will
  168. * be called from this thread, causing a timeout, and
  169. * no frame to be rendered.
  170. *
  171. * To avoid this, set a flag so wrapper can see if
  172. * it's called in this "special" way. */
  173. thr->frame.within_thread = true;
  174. pkt.data.b = thr->driver->read_viewport(thr->driver_data,
  175. (uint8_t*)pkt.data.v, thr->is_idle);
  176. thr->frame.within_thread = false;
  177. }
  178. else
  179. {
  180. /* Viewport dimensions changed right after main
  181. * thread read the async value. Cannot read safely. */
  182. pkt.data.b = false;
  183. }
  184. }
  185. else
  186. pkt.data.b = false;
  187. video_thread_reply(thr, &pkt);
  188. break;
  189. case CMD_SET_SHADER:
  190. if (thr->driver_data && thr->driver && thr->driver->set_shader)
  191. pkt.data.b = thr->driver->set_shader(thr->driver_data,
  192. pkt.data.set_shader.type, pkt.data.set_shader.path);
  193. else
  194. pkt.data.b = false;
  195. video_thread_reply(thr, &pkt);
  196. break;
  197. case CMD_ALIVE:
  198. if (thr->driver_data && thr->driver && thr->driver->alive)
  199. pkt.data.b = thr->driver->alive(thr->driver_data);
  200. else
  201. pkt.data.b = false;
  202. video_thread_reply(thr, &pkt);
  203. break;
  204. #ifdef HAVE_OVERLAY
  205. case CMD_OVERLAY_ENABLE:
  206. if (thr->driver_data && thr->overlay && thr->overlay->enable)
  207. thr->overlay->enable(thr->driver_data, pkt.data.b);
  208. video_thread_reply(thr, &pkt);
  209. break;
  210. case CMD_OVERLAY_LOAD:
  211. {
  212. unsigned tmp_alpha_mods = pkt.data.image.num;
  213. if (thr->driver_data && thr->overlay && thr->overlay->load)
  214. pkt.data.b = thr->overlay->load(thr->driver_data,
  215. pkt.data.image.data, pkt.data.image.num);
  216. else
  217. pkt.data.b = false;
  218. if (tmp_alpha_mods > 0)
  219. {
  220. float *tmp_alpha_mod = (float*)realloc(thr->alpha_mod,
  221. tmp_alpha_mods * sizeof(float));
  222. if (tmp_alpha_mod)
  223. {
  224. /* Avoid temporary garbage data. */
  225. int i;
  226. for (i = 0; i < (int)tmp_alpha_mods; i++)
  227. tmp_alpha_mod[i] = 1.0f;
  228. thr->alpha_mods = tmp_alpha_mods;
  229. thr->alpha_mod = tmp_alpha_mod;
  230. }
  231. }
  232. else
  233. {
  234. free(thr->alpha_mod);
  235. thr->alpha_mods = 0;
  236. thr->alpha_mod = NULL;
  237. }
  238. }
  239. video_thread_reply(thr, &pkt);
  240. break;
  241. case CMD_OVERLAY_TEX_GEOM:
  242. if (thr->driver_data && thr->overlay && thr->overlay->tex_geom)
  243. thr->overlay->tex_geom(thr->driver_data,
  244. pkt.data.rect.index,
  245. pkt.data.rect.x,
  246. pkt.data.rect.y,
  247. pkt.data.rect.w,
  248. pkt.data.rect.h);
  249. video_thread_reply(thr, &pkt);
  250. break;
  251. case CMD_OVERLAY_VERTEX_GEOM:
  252. if (thr->driver_data && thr->overlay && thr->overlay->vertex_geom)
  253. thr->overlay->vertex_geom(thr->driver_data,
  254. pkt.data.rect.index,
  255. pkt.data.rect.x,
  256. pkt.data.rect.y,
  257. pkt.data.rect.w,
  258. pkt.data.rect.h);
  259. video_thread_reply(thr, &pkt);
  260. break;
  261. case CMD_OVERLAY_FULL_SCREEN:
  262. if (thr->driver_data && thr->overlay && thr->overlay->full_screen)
  263. thr->overlay->full_screen(thr->driver_data, pkt.data.b);
  264. video_thread_reply(thr, &pkt);
  265. break;
  266. #endif
  267. case CMD_POKE_SET_VIDEO_MODE:
  268. if (thr->driver_data && thr->poke && thr->poke->set_video_mode)
  269. thr->poke->set_video_mode(thr->driver_data,
  270. pkt.data.new_mode.width,
  271. pkt.data.new_mode.height,
  272. pkt.data.new_mode.fullscreen);
  273. video_thread_reply(thr, &pkt);
  274. break;
  275. case CMD_POKE_SET_FILTERING:
  276. if (thr->driver_data && thr->poke && thr->poke->set_filtering)
  277. thr->poke->set_filtering(thr->driver_data,
  278. pkt.data.filtering.index,
  279. pkt.data.filtering.smooth,
  280. pkt.data.filtering.ctx_scaling);
  281. video_thread_reply(thr, &pkt);
  282. break;
  283. case CMD_POKE_SET_ASPECT_RATIO:
  284. if (thr->driver_data && thr->poke && thr->poke->set_aspect_ratio)
  285. thr->poke->set_aspect_ratio(thr->driver_data, pkt.data.i);
  286. video_thread_reply(thr, &pkt);
  287. break;
  288. case CMD_FONT_INIT:
  289. if (pkt.data.font_init.method)
  290. pkt.data.font_init.return_value = pkt.data.font_init.method(
  291. pkt.data.font_init.font_driver,
  292. pkt.data.font_init.font_handle,
  293. pkt.data.font_init.video_data,
  294. pkt.data.font_init.font_path,
  295. pkt.data.font_init.font_size,
  296. pkt.data.font_init.api,
  297. pkt.data.font_init.is_threaded
  298. );
  299. video_thread_reply(thr, &pkt);
  300. break;
  301. case CMD_CUSTOM_COMMAND:
  302. if (pkt.data.custom_command.method)
  303. pkt.data.custom_command.return_value =
  304. pkt.data.custom_command.method(pkt.data.custom_command.data);
  305. video_thread_reply(thr, &pkt);
  306. break;
  307. case CMD_POKE_SHOW_MOUSE:
  308. if (thr->driver_data && thr->poke && thr->poke->show_mouse)
  309. thr->poke->show_mouse(thr->driver_data, pkt.data.b);
  310. video_thread_reply(thr, &pkt);
  311. break;
  312. case CMD_POKE_GRAB_MOUSE_TOGGLE:
  313. if (thr->driver_data && thr->poke && thr->poke->grab_mouse_toggle)
  314. thr->poke->grab_mouse_toggle(thr->driver_data);
  315. video_thread_reply(thr, &pkt);
  316. break;
  317. case CMD_VIDEO_NONE:
  318. /* Never reply on no command. Possible deadlock if
  319. * thread sends command right after frame update. */
  320. break;
  321. case CMD_POKE_SET_HDR_MAX_NITS:
  322. if (thr->driver_data && thr->poke && thr->poke->set_hdr_max_nits)
  323. thr->poke->set_hdr_max_nits(
  324. thr->driver_data,
  325. pkt.data.hdr.max_nits
  326. );
  327. video_thread_reply(thr, &pkt);
  328. break;
  329. case CMD_POKE_SET_HDR_PAPER_WHITE_NITS:
  330. if (thr->driver_data &&
  331. thr->poke && thr->poke->set_hdr_paper_white_nits)
  332. thr->poke->set_hdr_paper_white_nits(
  333. thr->driver_data,
  334. pkt.data.hdr.paper_white_nits
  335. );
  336. video_thread_reply(thr, &pkt);
  337. break;
  338. case CMD_POKE_SET_HDR_CONTRAST:
  339. if (thr->driver_data && thr->poke && thr->poke->set_hdr_contrast)
  340. thr->poke->set_hdr_contrast(
  341. thr->driver_data,
  342. pkt.data.hdr.contrast
  343. );
  344. video_thread_reply(thr, &pkt);
  345. break;
  346. case CMD_POKE_SET_HDR_EXPAND_GAMUT:
  347. if (thr->driver_data && thr->poke && thr->poke->set_hdr_expand_gamut)
  348. thr->poke->set_hdr_expand_gamut(
  349. thr->driver_data,
  350. pkt.data.hdr.expand_gamut
  351. );
  352. video_thread_reply(thr, &pkt);
  353. break;
  354. default:
  355. video_thread_reply(thr, &pkt);
  356. break;
  357. }
  358. return false;
  359. }
  360. static void video_thread_loop(void *data)
  361. {
  362. thread_packet_t pkt;
  363. bool updated;
  364. thread_video_t *thr = (thread_video_t*)data;
  365. for (;;)
  366. {
  367. slock_lock(thr->lock);
  368. while (thr->send_cmd == CMD_VIDEO_NONE && !thr->frame.updated)
  369. scond_wait(thr->cond_thread, thr->lock);
  370. updated = thr->frame.updated;
  371. /* To avoid race condition where send_cmd is updated
  372. * right after the switch is checked. */
  373. pkt = thr->cmd_data;
  374. slock_unlock(thr->lock);
  375. if (video_thread_handle_packet(thr, &pkt))
  376. return;
  377. if (updated)
  378. {
  379. struct video_viewport vp;
  380. bool alive = false;
  381. bool focus = false;
  382. bool has_windowed = false;
  383. vp.x = 0;
  384. vp.y = 0;
  385. vp.width = 0;
  386. vp.height = 0;
  387. vp.full_width = 0;
  388. vp.full_height = 0;
  389. slock_lock(thr->frame.lock);
  390. thread_update_driver_state(thr);
  391. if (thr->driver_data && thr->driver)
  392. {
  393. if (thr->driver->frame)
  394. {
  395. video_frame_info_t video_info;
  396. bool ret;
  397. /* TODO/FIXME - not thread-safe - should get
  398. * rid of this */
  399. video_driver_build_info(&video_info);
  400. ret = thr->driver->frame(thr->driver_data,
  401. thr->frame.buffer, thr->frame.width, thr->frame.height,
  402. thr->frame.count, thr->frame.pitch,
  403. *thr->frame.msg ? thr->frame.msg : NULL,
  404. &video_info);
  405. slock_unlock(thr->frame.lock);
  406. if (ret)
  407. {
  408. if (thr->driver->alive)
  409. alive = thr->driver->alive(thr->driver_data);
  410. if (thr->driver->focus)
  411. focus = thr->driver->focus(thr->driver_data);
  412. if (thr->driver->has_windowed)
  413. has_windowed = thr->driver->has_windowed(thr->driver_data);
  414. }
  415. }
  416. else
  417. slock_unlock(thr->frame.lock);
  418. if (thr->driver->viewport_info)
  419. thr->driver->viewport_info(thr->driver_data, &vp);
  420. }
  421. else
  422. slock_unlock(thr->frame.lock);
  423. slock_lock(thr->lock);
  424. thr->alive = alive;
  425. thr->focus = focus;
  426. thr->has_windowed = has_windowed;
  427. thr->vp = vp;
  428. thr->frame.updated = false;
  429. scond_signal(thr->cond_cmd);
  430. slock_unlock(thr->lock);
  431. }
  432. }
  433. }
  434. static bool video_thread_alive(void *data)
  435. {
  436. bool ret;
  437. uint32_t runloop_flags;
  438. thread_video_t *thr = (thread_video_t*)data;
  439. if (!thr)
  440. return false;
  441. runloop_flags = runloop_get_flags();
  442. if (runloop_flags & RUNLOOP_FLAG_PAUSED)
  443. {
  444. thread_packet_t pkt;
  445. pkt.type = CMD_ALIVE;
  446. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  447. return pkt.data.b;
  448. }
  449. slock_lock(thr->lock);
  450. ret = thr->alive;
  451. slock_unlock(thr->lock);
  452. return ret;
  453. }
  454. static bool video_thread_focus(void *data)
  455. {
  456. bool ret;
  457. thread_video_t *thr = (thread_video_t*)data;
  458. if (!thr)
  459. return false;
  460. slock_lock(thr->lock);
  461. ret = thr->focus;
  462. slock_unlock(thr->lock);
  463. return ret;
  464. }
  465. static bool video_thread_suppress_screensaver(void *data, bool enable)
  466. {
  467. bool ret;
  468. thread_video_t *thr = (thread_video_t*)data;
  469. if (!thr)
  470. return false;
  471. slock_lock(thr->lock);
  472. ret = thr->suppress_screensaver;
  473. slock_unlock(thr->lock);
  474. return ret;
  475. }
  476. static bool video_thread_has_windowed(void *data)
  477. {
  478. bool ret;
  479. thread_video_t *thr = (thread_video_t*)data;
  480. if (!thr)
  481. return false;
  482. slock_lock(thr->lock);
  483. ret = thr->has_windowed;
  484. slock_unlock(thr->lock);
  485. return ret;
  486. }
  487. static bool video_thread_frame(void *data, const void *frame_,
  488. unsigned width, unsigned height, uint64_t frame_count,
  489. unsigned pitch, const char *msg, video_frame_info_t *video_info)
  490. {
  491. thread_video_t *thr = (thread_video_t*)data;
  492. if (!thr)
  493. return false;
  494. /* If called from within read_viewport, we're actually in the
  495. * driver thread, so just render directly. */
  496. if (thr->frame.within_thread)
  497. {
  498. thread_update_driver_state(thr);
  499. if (thr->driver_data && thr->driver && thr->driver->frame)
  500. return thr->driver->frame(thr->driver_data, frame_,
  501. width, height, frame_count, pitch, msg, video_info);
  502. return false;
  503. }
  504. slock_lock(thr->lock);
  505. if (!thr->nonblock)
  506. {
  507. retro_time_t target_frame_time =
  508. (retro_time_t)roundf(1000000 / video_info->refresh_rate);
  509. retro_time_t target = thr->last_time + target_frame_time;
  510. /* Ideally, use absolute time, but that is only a good idea on POSIX. */
  511. while (thr->frame.updated)
  512. {
  513. retro_time_t current = cpu_features_get_time_usec();
  514. retro_time_t delta = target - current;
  515. if (delta <= 0)
  516. break;
  517. if (!scond_wait_timeout(thr->cond_cmd, thr->lock, delta))
  518. break;
  519. }
  520. }
  521. /* Drop frame if updated flag is still set, as thread is
  522. * still working on last frame. */
  523. if (!thr->frame.updated)
  524. {
  525. const uint8_t *src = (const uint8_t*)frame_;
  526. uint8_t *dst = thr->frame.buffer;
  527. unsigned copy_stride = width *
  528. (thr->info.rgb32 ? sizeof(uint32_t) : sizeof(uint16_t));
  529. if (src)
  530. {
  531. int i; /* TODO/FIXME - increment counter never meaningfully used */
  532. for (i = 0; i < (int)height; i++, src += pitch, dst += copy_stride)
  533. memcpy(dst, src, copy_stride);
  534. }
  535. thr->frame.updated = true;
  536. thr->frame.width = width;
  537. thr->frame.height = height;
  538. thr->frame.count = frame_count;
  539. thr->frame.pitch = copy_stride;
  540. if (msg)
  541. strlcpy(thr->frame.msg, msg, sizeof(thr->frame.msg));
  542. else
  543. *thr->frame.msg = '\0';
  544. scond_signal(thr->cond_thread);
  545. #ifdef HAVE_MENU
  546. if (thr->texture.enable)
  547. {
  548. do
  549. {
  550. scond_wait(thr->cond_cmd, thr->lock);
  551. } while (thr->frame.updated);
  552. }
  553. #endif
  554. thr->hit_count++;
  555. }
  556. else
  557. thr->miss_count++;
  558. slock_unlock(thr->lock);
  559. thr->last_time = cpu_features_get_time_usec();
  560. return true;
  561. }
  562. static void video_thread_set_nonblock_state(void *data, bool state,
  563. bool adaptive_vsync_enabled,
  564. unsigned swap_interval)
  565. {
  566. thread_video_t *thr = (thread_video_t*)data;
  567. if (thr)
  568. thr->nonblock = state;
  569. }
  570. static bool video_thread_init(thread_video_t *thr,
  571. const video_info_t info,
  572. input_driver_t **input, void **input_data)
  573. {
  574. thread_packet_t pkt;
  575. if (!(thr->lock = slock_new()))
  576. return false;
  577. if (!(thr->alpha_lock = slock_new()))
  578. return false;
  579. if (!(thr->frame.lock = slock_new()))
  580. return false;
  581. if (!(thr->cond_cmd = scond_new()))
  582. return false;
  583. if (!(thr->cond_thread = scond_new()))
  584. return false;
  585. {
  586. size_t max_size = info.input_scale * RARCH_SCALE_BASE;
  587. max_size *= max_size;
  588. max_size *= info.rgb32 ?
  589. sizeof(uint32_t) : sizeof(uint16_t);
  590. #ifdef _3DS
  591. thr->frame.buffer = linearMemAlign(max_size, 0x80);
  592. #else
  593. thr->frame.buffer = (uint8_t*)malloc(max_size);
  594. #endif
  595. if (!thr->frame.buffer)
  596. return false;
  597. memset(thr->frame.buffer, 0x80, max_size);
  598. }
  599. thr->input = input;
  600. thr->input_data = input_data;
  601. thr->info = info;
  602. thr->alive = true;
  603. thr->focus = true;
  604. thr->has_windowed = true;
  605. thr->suppress_screensaver = true;
  606. thr->last_time = cpu_features_get_time_usec();
  607. if (!(thr->thread = sthread_create(video_thread_loop, thr)))
  608. return false;
  609. pkt.type = CMD_INIT;
  610. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  611. return pkt.data.b;
  612. }
  613. static bool video_thread_set_shader(void *data,
  614. enum rarch_shader_type type, const char *path)
  615. {
  616. thread_packet_t pkt;
  617. thread_video_t *thr = (thread_video_t*)data;
  618. if (!thr)
  619. return false;
  620. pkt.type = CMD_SET_SHADER;
  621. pkt.data.set_shader.type = type;
  622. pkt.data.set_shader.path = path;
  623. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  624. return pkt.data.b;
  625. }
  626. static void video_thread_set_viewport(void *data, unsigned width,
  627. unsigned height, bool force_full, bool video_allow_rotate)
  628. {
  629. thread_video_t *thr = (thread_video_t*)data;
  630. if (thr && thr->driver_data && thr->driver && thr->driver->set_viewport)
  631. {
  632. slock_lock(thr->lock);
  633. thr->driver->set_viewport(thr->driver_data, width, height,
  634. force_full, video_allow_rotate);
  635. slock_unlock(thr->lock);
  636. }
  637. }
  638. static void video_thread_set_rotation(void *data, unsigned rotation)
  639. {
  640. thread_video_t *thr = (thread_video_t*)data;
  641. if (thr)
  642. {
  643. thread_packet_t pkt;
  644. pkt.type = CMD_SET_ROTATION;
  645. pkt.data.i = rotation;
  646. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  647. }
  648. }
  649. /* This value is set async as stalling on the video driver for
  650. * every query is too slow.
  651. *
  652. * This means this value might not be correct, so viewport
  653. * reads are not supported for now. */
  654. static void video_thread_viewport_info(void *data, struct video_viewport *vp)
  655. {
  656. thread_video_t *thr = (thread_video_t*)data;
  657. if (thr)
  658. {
  659. slock_lock(thr->lock);
  660. *vp = thr->vp;
  661. /* Explicitly mem-copied so we can use memcmp correctly later. */
  662. memcpy(&thr->read_vp, &thr->vp, sizeof(thr->read_vp));
  663. slock_unlock(thr->lock);
  664. }
  665. }
  666. static bool video_thread_read_viewport(void *data,
  667. uint8_t *buffer, bool is_idle)
  668. {
  669. thread_packet_t pkt;
  670. thread_video_t *thr = (thread_video_t*)data;
  671. if (!thr)
  672. return false;
  673. pkt.type = CMD_READ_VIEWPORT;
  674. pkt.data.v = buffer;
  675. thr->is_idle = is_idle;
  676. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  677. return pkt.data.b;
  678. }
  679. static void video_thread_free(void *data)
  680. {
  681. thread_video_t *thr = (thread_video_t*)data;
  682. if (thr)
  683. {
  684. if (thr->thread)
  685. {
  686. thread_packet_t pkt;
  687. pkt.type = CMD_FREE;
  688. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  689. sthread_join(thr->thread);
  690. }
  691. else
  692. {
  693. /* If we don't have a thread,
  694. we must call the driver's free function ourselves. */
  695. if (thr->driver_data && thr->driver && thr->driver->free)
  696. thr->driver->free(thr->driver_data);
  697. }
  698. free(thr->texture.frame);
  699. #ifdef _3DS
  700. linearFree(thr->frame.buffer);
  701. #else
  702. free(thr->frame.buffer);
  703. #endif
  704. free(thr->alpha_mod);
  705. slock_free(thr->frame.lock);
  706. slock_free(thr->alpha_lock);
  707. slock_free(thr->lock);
  708. scond_free(thr->cond_cmd);
  709. scond_free(thr->cond_thread);
  710. RARCH_LOG(
  711. "Threaded video stats: Frames pushed: %u, Frames dropped: %u.\n",
  712. thr->hit_count, thr->miss_count);
  713. free(thr);
  714. }
  715. }
  716. #ifdef HAVE_OVERLAY
  717. static void thread_overlay_enable(void *data, bool state)
  718. {
  719. thread_video_t *thr = (thread_video_t*)data;
  720. if (thr)
  721. {
  722. thread_packet_t pkt;
  723. pkt.type = CMD_OVERLAY_ENABLE;
  724. pkt.data.b = state;
  725. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  726. }
  727. }
  728. static bool thread_overlay_load(void *data,
  729. const void *image_data, unsigned num_images)
  730. {
  731. thread_packet_t pkt;
  732. thread_video_t *thr = (thread_video_t*)data;
  733. if (!thr)
  734. return false;
  735. pkt.type = CMD_OVERLAY_LOAD;
  736. pkt.data.image.data = (const struct texture_image*)image_data;
  737. pkt.data.image.num = num_images;
  738. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  739. return pkt.data.b;
  740. }
  741. static void thread_overlay_tex_geom(void *data,
  742. unsigned idx, float x, float y, float w, float h)
  743. {
  744. thread_video_t *thr = (thread_video_t*)data;
  745. if (thr)
  746. {
  747. thread_packet_t pkt;
  748. pkt.type = CMD_OVERLAY_TEX_GEOM;
  749. pkt.data.rect.index = idx;
  750. pkt.data.rect.x = x;
  751. pkt.data.rect.y = y;
  752. pkt.data.rect.w = w;
  753. pkt.data.rect.h = h;
  754. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  755. }
  756. }
  757. static void thread_overlay_vertex_geom(void *data,
  758. unsigned idx, float x, float y, float w, float h)
  759. {
  760. thread_video_t *thr = (thread_video_t*)data;
  761. if (thr)
  762. {
  763. thread_packet_t pkt;
  764. pkt.type = CMD_OVERLAY_VERTEX_GEOM;
  765. pkt.data.rect.index = idx;
  766. pkt.data.rect.x = x;
  767. pkt.data.rect.y = y;
  768. pkt.data.rect.w = w;
  769. pkt.data.rect.h = h;
  770. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  771. }
  772. }
  773. static void thread_overlay_full_screen(void *data, bool enable)
  774. {
  775. thread_video_t *thr = (thread_video_t*)data;
  776. if (thr)
  777. {
  778. thread_packet_t pkt;
  779. pkt.type = CMD_OVERLAY_FULL_SCREEN;
  780. pkt.data.b = enable;
  781. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  782. }
  783. }
  784. /* We cannot wait for this to complete. Totally blocks the main thread. */
  785. static void thread_overlay_set_alpha(void *data, unsigned idx, float mod)
  786. {
  787. thread_video_t *thr = (thread_video_t*)data;
  788. if (thr)
  789. {
  790. slock_lock(thr->alpha_lock);
  791. thr->alpha_mod[idx] = mod;
  792. thr->alpha_update = true;
  793. slock_unlock(thr->alpha_lock);
  794. }
  795. }
  796. static const video_overlay_interface_t thread_overlay = {
  797. thread_overlay_enable,
  798. thread_overlay_load,
  799. thread_overlay_tex_geom,
  800. thread_overlay_vertex_geom,
  801. thread_overlay_full_screen,
  802. thread_overlay_set_alpha,
  803. };
  804. static void video_thread_get_overlay_interface(void *data,
  805. const video_overlay_interface_t **iface)
  806. {
  807. thread_video_t *thr = (thread_video_t*)data;
  808. if (thr && thr->driver_data &&
  809. thr->driver && thr->driver->overlay_interface)
  810. {
  811. thr->driver->overlay_interface(thr->driver_data, &thr->overlay);
  812. *iface = &thread_overlay;
  813. }
  814. else
  815. *iface = NULL;
  816. }
  817. #endif
  818. static void thread_set_video_mode(void *data,
  819. unsigned width, unsigned height, bool video_fullscreen)
  820. {
  821. thread_video_t *thr = (thread_video_t*)data;
  822. if (thr)
  823. {
  824. thread_packet_t pkt;
  825. pkt.type = CMD_POKE_SET_VIDEO_MODE;
  826. pkt.data.new_mode.width = width;
  827. pkt.data.new_mode.height = height;
  828. pkt.data.new_mode.fullscreen = video_fullscreen;
  829. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  830. }
  831. }
  832. static void thread_set_filtering(void *data,
  833. unsigned idx, bool smooth, bool ctx_scaling)
  834. {
  835. thread_video_t *thr = (thread_video_t*)data;
  836. if (thr)
  837. {
  838. thread_packet_t pkt;
  839. pkt.type = CMD_POKE_SET_FILTERING;
  840. pkt.data.filtering.index = idx;
  841. pkt.data.filtering.smooth = smooth;
  842. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  843. }
  844. }
  845. static void thread_set_hdr_max_nits(void *data, float max_nits)
  846. {
  847. thread_video_t *thr = (thread_video_t*)data;
  848. if (thr)
  849. {
  850. thread_packet_t pkt;
  851. pkt.type = CMD_POKE_SET_HDR_MAX_NITS;
  852. pkt.data.hdr.max_nits = max_nits;
  853. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  854. }
  855. }
  856. static void thread_set_hdr_paper_white_nits(void *data, float paper_white_nits)
  857. {
  858. thread_video_t *thr = (thread_video_t*)data;
  859. if (thr)
  860. {
  861. thread_packet_t pkt;
  862. pkt.type = CMD_POKE_SET_HDR_PAPER_WHITE_NITS;
  863. pkt.data.hdr.paper_white_nits = paper_white_nits;
  864. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  865. }
  866. }
  867. static void thread_set_hdr_contrast(void *data, float contrast)
  868. {
  869. thread_video_t *thr = (thread_video_t*)data;
  870. if (thr)
  871. {
  872. thread_packet_t pkt;
  873. pkt.type = CMD_POKE_SET_HDR_CONTRAST;
  874. pkt.data.hdr.contrast = contrast;
  875. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  876. }
  877. }
  878. static void thread_set_hdr_expand_gamut(void *data, bool expand_gamut)
  879. {
  880. thread_video_t *thr = (thread_video_t*)data;
  881. if (thr)
  882. {
  883. thread_packet_t pkt;
  884. pkt.type = CMD_POKE_SET_HDR_EXPAND_GAMUT;
  885. pkt.data.hdr.expand_gamut = expand_gamut;
  886. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  887. }
  888. }
  889. static void thread_get_video_output_size(void *data,
  890. unsigned *width, unsigned *height, char *desc, size_t desc_len)
  891. {
  892. thread_video_t *thr = (thread_video_t*)data;
  893. if (thr && thr->driver_data &&
  894. thr->poke && thr->poke->get_video_output_size)
  895. thr->poke->get_video_output_size(thr->driver_data,
  896. width, height, desc, desc_len);
  897. }
  898. static void thread_get_video_output_prev(void *data)
  899. {
  900. thread_video_t *thr = (thread_video_t*)data;
  901. if (thr && thr->driver_data &&
  902. thr->poke && thr->poke->get_video_output_prev)
  903. thr->poke->get_video_output_prev(thr->driver_data);
  904. }
  905. static void thread_get_video_output_next(void *data)
  906. {
  907. thread_video_t *thr = (thread_video_t*)data;
  908. if (thr && thr->driver_data &&
  909. thr->poke && thr->poke->get_video_output_next)
  910. thr->poke->get_video_output_next(thr->driver_data);
  911. }
  912. static void thread_set_aspect_ratio(void *data, unsigned aspect_ratio_idx)
  913. {
  914. thread_video_t *thr = (thread_video_t*)data;
  915. if (thr)
  916. {
  917. thread_packet_t pkt;
  918. pkt.type = CMD_POKE_SET_ASPECT_RATIO;
  919. pkt.data.i = aspect_ratio_idx;
  920. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  921. }
  922. }
  923. static void thread_set_texture_frame(void *data, const void *frame,
  924. bool rgb32, unsigned width, unsigned height, float alpha)
  925. {
  926. thread_video_t *thr = (thread_video_t*)data;
  927. size_t required = width * height *
  928. (rgb32 ? sizeof(uint32_t) : sizeof(uint16_t));
  929. if (!thr)
  930. return;
  931. slock_lock(thr->frame.lock);
  932. if (!thr->texture.frame || required > thr->texture.frame_cap)
  933. {
  934. void *tmp_frame = realloc(thr->texture.frame, required);
  935. if (!tmp_frame)
  936. goto end;
  937. thr->texture.frame = tmp_frame;
  938. thr->texture.frame_cap = required;
  939. }
  940. memcpy(thr->texture.frame, frame, required);
  941. thr->texture.rgb32 = rgb32;
  942. thr->texture.width = width;
  943. thr->texture.height = height;
  944. thr->texture.alpha = alpha;
  945. thr->texture.frame_updated = true;
  946. end:
  947. slock_unlock(thr->frame.lock);
  948. }
  949. static void thread_set_texture_enable(void *data, bool state, bool full_screen)
  950. {
  951. thread_video_t *thr = (thread_video_t*)data;
  952. if (thr)
  953. {
  954. slock_lock(thr->frame.lock);
  955. thr->texture.enable = state;
  956. thr->texture.full_screen = full_screen;
  957. slock_unlock(thr->frame.lock);
  958. }
  959. }
  960. static void thread_set_osd_msg(void *data,
  961. const char *msg, const struct font_params *params, void *font)
  962. {
  963. thread_video_t *thr = (thread_video_t*)data;
  964. /* TODO : find a way to determine if the calling
  965. * thread is the driver thread or not. */
  966. if (thr && thr->driver_data && thr->poke && thr->poke->set_osd_msg)
  967. thr->poke->set_osd_msg(thr->driver_data, msg, params, font);
  968. }
  969. static void thread_show_mouse(void *data, bool state)
  970. {
  971. thread_video_t *thr = (thread_video_t*)data;
  972. if (thr)
  973. {
  974. thread_packet_t pkt;
  975. pkt.type = CMD_POKE_SHOW_MOUSE;
  976. pkt.data.b = state;
  977. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  978. }
  979. }
  980. static void thread_grab_mouse_toggle(void *data)
  981. {
  982. thread_video_t *thr = (thread_video_t*)data;
  983. if (thr)
  984. {
  985. thread_packet_t pkt;
  986. pkt.type = CMD_POKE_GRAB_MOUSE_TOGGLE;
  987. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  988. }
  989. }
  990. static uintptr_t thread_load_texture(void *video_data, void *data,
  991. bool threaded, enum texture_filter_type filter_type)
  992. {
  993. thread_video_t *thr = (thread_video_t*)video_data;
  994. if (thr && thr->driver_data && thr->poke && thr->poke->load_texture)
  995. return thr->poke->load_texture(thr->driver_data,
  996. data, threaded, filter_type);
  997. return 0;
  998. }
  999. static void thread_unload_texture(void *data,
  1000. bool threaded, uintptr_t id)
  1001. {
  1002. thread_video_t *thr = (thread_video_t*)data;
  1003. if (thr && thr->driver_data && thr->poke && thr->poke->unload_texture)
  1004. thr->poke->unload_texture(thr->driver_data, threaded, id);
  1005. }
  1006. static void thread_apply_state_changes(void *data)
  1007. {
  1008. thread_video_t *thr = (thread_video_t*)data;
  1009. if (thr)
  1010. {
  1011. slock_lock(thr->frame.lock);
  1012. thr->apply_state_changes = true;
  1013. slock_unlock(thr->frame.lock);
  1014. }
  1015. }
  1016. /* This is read-only state which should not
  1017. * have any kind of race condition. */
  1018. static struct video_shader *thread_get_current_shader(void *data)
  1019. {
  1020. thread_video_t *thr = (thread_video_t*)data;
  1021. if (thr && thr->driver_data && thr->poke && thr->poke->get_current_shader)
  1022. return thr->poke->get_current_shader(thr->driver_data);
  1023. return NULL;
  1024. }
  1025. static uint32_t thread_get_flags(void *data)
  1026. {
  1027. thread_video_t *thr = (thread_video_t*)data;
  1028. if (thr && thr->driver_data && thr->poke && thr->poke->get_flags)
  1029. return thr->poke->get_flags(thr->driver_data);
  1030. return 0;
  1031. }
  1032. static const video_poke_interface_t thread_poke = {
  1033. thread_get_flags,
  1034. thread_load_texture,
  1035. thread_unload_texture,
  1036. thread_set_video_mode,
  1037. NULL, /* get_refresh_rate */
  1038. thread_set_filtering,
  1039. thread_get_video_output_size,
  1040. thread_get_video_output_prev,
  1041. thread_get_video_output_next,
  1042. NULL, /* get_current_framebuffer */
  1043. NULL, /* get_proc_address */
  1044. thread_set_aspect_ratio,
  1045. thread_apply_state_changes,
  1046. thread_set_texture_frame,
  1047. thread_set_texture_enable,
  1048. thread_set_osd_msg,
  1049. thread_show_mouse,
  1050. thread_grab_mouse_toggle,
  1051. thread_get_current_shader,
  1052. NULL, /* get_current_software_framebuffer */
  1053. NULL, /* get_hw_render_interface */
  1054. thread_set_hdr_max_nits,
  1055. thread_set_hdr_paper_white_nits,
  1056. thread_set_hdr_contrast,
  1057. thread_set_hdr_expand_gamut
  1058. };
  1059. static void video_thread_get_poke_interface(void *data,
  1060. const video_poke_interface_t **iface)
  1061. {
  1062. thread_video_t *thr = (thread_video_t*)data;
  1063. if (thr && thr->driver_data &&
  1064. thr->driver && thr->driver->poke_interface)
  1065. {
  1066. thr->driver->poke_interface(thr->driver_data, &thr->poke);
  1067. *iface = &thread_poke;
  1068. }
  1069. else
  1070. *iface = NULL;
  1071. }
  1072. #ifdef HAVE_GFX_WIDGETS
  1073. static bool video_thread_wrapper_gfx_widgets_enabled(void *data)
  1074. {
  1075. thread_video_t *thr = (thread_video_t*)data;
  1076. if (thr && thr->driver_data &&
  1077. thr->driver && thr->driver->gfx_widgets_enabled)
  1078. return thr->driver->gfx_widgets_enabled(thr->driver_data);
  1079. return false;
  1080. }
  1081. #endif
  1082. static const video_driver_t video_thread = {
  1083. video_thread_init_never_call, /* Should never be called directly. */
  1084. video_thread_frame,
  1085. video_thread_set_nonblock_state,
  1086. video_thread_alive,
  1087. video_thread_focus,
  1088. video_thread_suppress_screensaver,
  1089. video_thread_has_windowed,
  1090. video_thread_set_shader,
  1091. video_thread_free,
  1092. "Thread wrapper",
  1093. video_thread_set_viewport,
  1094. video_thread_set_rotation,
  1095. video_thread_viewport_info,
  1096. video_thread_read_viewport,
  1097. NULL, /* read_frame_raw */
  1098. #ifdef HAVE_OVERLAY
  1099. video_thread_get_overlay_interface,
  1100. #endif
  1101. video_thread_get_poke_interface,
  1102. NULL, /* wrap_type_to_enum */
  1103. #ifdef HAVE_GFX_WIDGETS
  1104. video_thread_wrapper_gfx_widgets_enabled
  1105. #endif
  1106. };
  1107. static void video_thread_set_callbacks(thread_video_t *thr,
  1108. const video_driver_t *drv)
  1109. {
  1110. thr->video_thread = video_thread;
  1111. thr->driver = drv;
  1112. if (drv)
  1113. {
  1114. /* Disable optional features if not present. */
  1115. if (!drv->read_viewport)
  1116. thr->video_thread.read_viewport = NULL;
  1117. if (!drv->set_viewport)
  1118. thr->video_thread.set_viewport = NULL;
  1119. if (!drv->set_rotation)
  1120. thr->video_thread.set_rotation = NULL;
  1121. if (!drv->set_shader)
  1122. thr->video_thread.set_shader = NULL;
  1123. #ifdef HAVE_OVERLAY
  1124. if (!drv->overlay_interface)
  1125. thr->video_thread.overlay_interface = NULL;
  1126. #endif
  1127. if (!drv->poke_interface)
  1128. thr->video_thread.poke_interface = NULL;
  1129. }
  1130. }
  1131. /**
  1132. * video_init_thread:
  1133. * @out_driver : Output video driver
  1134. * @out_data : Output video data
  1135. * @input : Input input driver
  1136. * @input_data : Input input data
  1137. * @driver : Input Video driver
  1138. * @info : Video info handle.
  1139. *
  1140. * Creates, initializes and starts a video driver in a new thread.
  1141. * Access to video driver will be mediated through this driver.
  1142. *
  1143. * Returns: true (1) if successful, otherwise false (0).
  1144. **/
  1145. bool video_init_thread(const video_driver_t **out_driver, void **out_data,
  1146. input_driver_t **input, void **input_data,
  1147. const video_driver_t *drv, const video_info_t info)
  1148. {
  1149. thread_video_t *thr = (thread_video_t*)calloc(1, sizeof(*thr));
  1150. if (!thr)
  1151. return false;
  1152. video_thread_set_callbacks(thr, drv);
  1153. thr->driver = drv;
  1154. *out_driver = &thr->video_thread;
  1155. *out_data = thr;
  1156. return video_thread_init(thr, info, input, input_data);
  1157. }
  1158. bool video_thread_font_init(const void **font_driver, void **font_handle,
  1159. void *data, const char *font_path, float video_font_size,
  1160. enum font_driver_render_api api, custom_font_command_method_t func,
  1161. bool is_threaded)
  1162. {
  1163. thread_packet_t pkt;
  1164. video_driver_state_t *video_st = video_state_get_ptr();
  1165. thread_video_t *thr = (thread_video_t*)video_st->data;
  1166. if (!thr)
  1167. return false;
  1168. pkt.type = CMD_FONT_INIT;
  1169. pkt.data.font_init.method = func;
  1170. pkt.data.font_init.font_driver = font_driver;
  1171. pkt.data.font_init.font_handle = font_handle;
  1172. pkt.data.font_init.video_data = data;
  1173. pkt.data.font_init.font_path = font_path;
  1174. pkt.data.font_init.font_size = video_font_size;
  1175. pkt.data.font_init.is_threaded = is_threaded;
  1176. pkt.data.font_init.api = api;
  1177. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  1178. return pkt.data.font_init.return_value;
  1179. }
  1180. unsigned video_thread_texture_load(void *data, custom_command_method_t func)
  1181. {
  1182. thread_packet_t pkt;
  1183. video_driver_state_t *video_st = video_state_get_ptr();
  1184. thread_video_t *thr = (thread_video_t*)video_st->data;
  1185. if (!thr)
  1186. return 0;
  1187. pkt.type = CMD_CUSTOM_COMMAND;
  1188. pkt.data.custom_command.method = func;
  1189. pkt.data.custom_command.data = data;
  1190. video_thread_send_and_wait_user_to_thread(thr, &pkt);
  1191. return pkt.data.custom_command.return_value;
  1192. }