PageRenderTime 1396ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/video_output/xcb/xvideo.c

https://bitbucket.org/sbwilson/vlc
C | 850 lines | 667 code | 104 blank | 79 comment | 175 complexity | e11d96716957c7620ec2008909439e94 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, WTFPL
  1. /**
  2. * @file xvideo.c
  3. * @brief X C Bindings video output module for VLC media player
  4. */
  5. /*****************************************************************************
  6. * Copyright © 2009 Rémi Denis-Courmont
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program; if not, write to the Free Software Foundation,
  20. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  21. *****************************************************************************/
  22. #ifdef HAVE_CONFIG_H
  23. # include <config.h>
  24. #endif
  25. #include <stdlib.h>
  26. #include <limits.h>
  27. #include <assert.h>
  28. #include <xcb/xcb.h>
  29. #include <xcb/shm.h>
  30. #include <xcb/xv.h>
  31. #include <vlc_common.h>
  32. #include <vlc_plugin.h>
  33. #include <vlc_vout_display.h>
  34. #include <vlc_picture_pool.h>
  35. #include <vlc_dialog.h>
  36. #include "xcb_vlc.h"
  37. #define ADAPTOR_TEXT N_("XVideo adaptor number")
  38. #define ADAPTOR_LONGTEXT N_( \
  39. "XVideo hardware adaptor to use. By default, VLC will " \
  40. "use the first functional adaptor.")
  41. #define FORMAT_TEXT N_("XVideo format id")
  42. #define FORMAT_LONGTEXT N_( \
  43. "XVideo image format id to use. By default, VLC will " \
  44. "try to use the best match for the video being played.")
  45. static int Open (vlc_object_t *);
  46. static void Close (vlc_object_t *);
  47. /*
  48. * Module descriptor
  49. */
  50. vlc_module_begin ()
  51. set_shortname (N_("XVideo"))
  52. set_description (N_("XVideo output (XCB)"))
  53. set_category (CAT_VIDEO)
  54. set_subcategory (SUBCAT_VIDEO_VOUT)
  55. set_capability ("vout display", 200)
  56. set_callbacks (Open, Close)
  57. add_integer ("xvideo-adaptor", -1,
  58. ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true)
  59. add_integer ("xvideo-format-id", 0,
  60. FORMAT_TEXT, FORMAT_LONGTEXT, true)
  61. add_obsolete_bool ("xvideo-shm") /* removed in 2.0.0 */
  62. add_shortcut ("xcb-xv", "xv", "xvideo", "xid")
  63. vlc_module_end ()
  64. #define MAX_PICTURES (128)
  65. struct vout_display_sys_t
  66. {
  67. xcb_connection_t *conn;
  68. vout_window_t *embed;/* VLC window */
  69. xcb_cursor_t cursor; /* blank cursor */
  70. xcb_window_t window; /* drawable X window */
  71. xcb_gcontext_t gc; /* context to put images */
  72. xcb_xv_port_t port; /* XVideo port */
  73. uint32_t id; /* XVideo format */
  74. uint16_t width; /* display width */
  75. uint16_t height; /* display height */
  76. uint32_t data_size; /* picture byte size (for non-SHM) */
  77. bool swap_uv; /* U/V pointer must be swapped in a picture */
  78. bool shm; /* whether to use MIT-SHM */
  79. bool visible; /* whether it makes sense to draw at all */
  80. xcb_xv_query_image_attributes_reply_t *att;
  81. picture_pool_t *pool; /* picture pool */
  82. picture_resource_t resource[MAX_PICTURES];
  83. };
  84. static picture_pool_t *Pool (vout_display_t *, unsigned);
  85. static void Display (vout_display_t *, picture_t *, subpicture_t *subpicture);
  86. static int Control (vout_display_t *, int, va_list);
  87. static void Manage (vout_display_t *);
  88. /**
  89. * Check that the X server supports the XVideo extension.
  90. */
  91. static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
  92. {
  93. xcb_xv_query_extension_reply_t *r;
  94. xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
  95. bool ok = false;
  96. /* We need XVideo 2.2 for PutImage */
  97. r = xcb_xv_query_extension_reply (conn, ck, NULL);
  98. if (r == NULL)
  99. msg_Dbg (vd, "XVideo extension not available");
  100. else
  101. if (r->major != 2)
  102. msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" unknown",
  103. r->major, r->minor);
  104. else
  105. if (r->minor < 2)
  106. msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" too old",
  107. r->major, r->minor);
  108. else
  109. {
  110. msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
  111. r->major, r->minor);
  112. ok = true;
  113. }
  114. free (r);
  115. return ok;
  116. }
  117. static vlc_fourcc_t ParseFormat (vlc_object_t *obj,
  118. const xcb_xv_image_format_info_t *restrict f)
  119. {
  120. switch (f->type)
  121. {
  122. case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
  123. switch (f->num_planes)
  124. {
  125. case 1:
  126. switch (popcount (f->red_mask | f->green_mask | f->blue_mask))
  127. {
  128. case 24:
  129. if (f->bpp == 32 && f->depth == 32)
  130. return VLC_CODEC_RGBA;
  131. if (f->bpp == 32 && f->depth == 24)
  132. return VLC_CODEC_RGB32;
  133. if (f->bpp == 24 && f->depth == 24)
  134. return VLC_CODEC_RGB24;
  135. break;
  136. case 16:
  137. if (f->byte_order != ORDER)
  138. return 0; /* Mixed endian! */
  139. if (f->bpp == 16 && f->depth == 16)
  140. return VLC_CODEC_RGB16;
  141. break;
  142. case 15:
  143. if (f->byte_order != ORDER)
  144. return 0; /* Mixed endian! */
  145. if (f->bpp == 16 && f->depth == 16)
  146. return VLC_CODEC_RGBT;
  147. if (f->bpp == 16 && f->depth == 15)
  148. return VLC_CODEC_RGB15;
  149. break;
  150. case 12:
  151. if (f->bpp == 16 && f->depth == 16)
  152. return VLC_CODEC_RGBA16;
  153. if (f->bpp == 16 && f->depth == 12)
  154. return VLC_CODEC_RGB12;
  155. case 8:
  156. if (f->bpp == 8 && f->depth == 8)
  157. return VLC_CODEC_RGB8;
  158. break;
  159. }
  160. break;
  161. }
  162. msg_Err (obj, "unknown XVideo RGB format %"PRIx32" (%.4s)",
  163. f->id, f->guid);
  164. msg_Dbg (obj, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
  165. "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
  166. break;
  167. case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
  168. if (f->u_sample_bits != f->v_sample_bits
  169. || f->vhorz_u_period != f->vhorz_v_period
  170. || f->vvert_u_period != f->vvert_v_period
  171. || f->y_sample_bits != 8 || f->u_sample_bits != 8
  172. || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
  173. goto bad;
  174. switch (f->num_planes)
  175. {
  176. case 1:
  177. switch (f->bpp)
  178. {
  179. /*untested: case 24:
  180. if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
  181. return VLC_CODEC_I444;
  182. break;*/
  183. case 16:
  184. if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
  185. {
  186. if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
  187. return VLC_CODEC_YUYV;
  188. if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
  189. return VLC_CODEC_UYVY;
  190. }
  191. break;
  192. }
  193. break;
  194. case 3:
  195. switch (f->bpp)
  196. {
  197. case 12:
  198. if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
  199. {
  200. if (!strcmp ((const char *)f->vcomp_order, "YVU"))
  201. return VLC_CODEC_YV12;
  202. if (!strcmp ((const char *)f->vcomp_order, "YUV"))
  203. return VLC_CODEC_I420;
  204. }
  205. }
  206. break;
  207. }
  208. bad:
  209. msg_Err (obj, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
  210. f->guid);
  211. msg_Dbg (obj, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
  212. "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
  213. f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
  214. msg_Dbg (obj, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
  215. "%"PRIu32"/%"PRIu32"/%"PRIu32,
  216. f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
  217. f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
  218. msg_Warn (obj, " order: %.32s", f->vcomp_order);
  219. break;
  220. }
  221. return 0;
  222. }
  223. static bool BetterFormat (vlc_fourcc_t a, const vlc_fourcc_t *tab,
  224. unsigned *rankp)
  225. {
  226. for (unsigned i = 0, max = *rankp; i < max && tab[i] != 0; i++)
  227. if (tab[i] == a)
  228. {
  229. *rankp = i;
  230. return true;
  231. }
  232. return false;
  233. }
  234. static xcb_xv_query_image_attributes_reply_t *
  235. FindFormat (vlc_object_t *obj, xcb_connection_t *conn, video_format_t *fmt,
  236. const xcb_xv_adaptor_info_t *a, uint32_t *idp)
  237. {
  238. /* Order chromas by preference */
  239. vlc_fourcc_t tab[7];
  240. const vlc_fourcc_t *chromav = tab;
  241. vlc_fourcc_t chroma = var_InheritInteger (obj, "xvideo-format-id");
  242. if (chroma != 0) /* Forced chroma */
  243. {
  244. tab[0] = chroma;
  245. tab[1] = 0;
  246. }
  247. else if (vlc_fourcc_IsYUV (fmt->i_chroma)) /* YUV chroma */
  248. {
  249. chromav = vlc_fourcc_GetYUVFallback (fmt->i_chroma);
  250. }
  251. else /* RGB chroma */
  252. {
  253. tab[0] = fmt->i_chroma;
  254. tab[1] = VLC_CODEC_RGB32;
  255. tab[2] = VLC_CODEC_RGB24;
  256. tab[3] = VLC_CODEC_RGB16;
  257. tab[4] = VLC_CODEC_RGB15;
  258. tab[5] = VLC_CODEC_YUYV;
  259. tab[6] = 0;
  260. }
  261. /* Get available image formats */
  262. xcb_xv_list_image_formats_reply_t *list =
  263. xcb_xv_list_image_formats_reply (conn,
  264. xcb_xv_list_image_formats (conn, a->base_id), NULL);
  265. if (list == NULL)
  266. return NULL;
  267. /* Check available XVideo chromas */
  268. xcb_xv_query_image_attributes_reply_t *attr = NULL;
  269. unsigned rank = UINT_MAX;
  270. for (const xcb_xv_image_format_info_t *f =
  271. xcb_xv_list_image_formats_format (list),
  272. *f_end =
  273. f + xcb_xv_list_image_formats_format_length (list);
  274. f < f_end;
  275. f++)
  276. {
  277. chroma = ParseFormat (obj, f);
  278. if (chroma == 0)
  279. continue;
  280. /* Oink oink! */
  281. if ((chroma == VLC_CODEC_I420 || chroma == VLC_CODEC_YV12)
  282. && a->name_size >= 4
  283. && !memcmp ("OMAP", xcb_xv_adaptor_info_name (a), 4))
  284. {
  285. msg_Dbg (obj, "skipping slow I420 format");
  286. continue; /* OMAP framebuffer sucks at YUV 4:2:0 */
  287. }
  288. if (!BetterFormat (chroma, chromav, &rank))
  289. continue;
  290. /* VLC pads scanline to 16 pixels internally */
  291. unsigned width = fmt->i_width;
  292. unsigned height = fmt->i_height;
  293. xcb_xv_query_image_attributes_reply_t *i;
  294. i = xcb_xv_query_image_attributes_reply (conn,
  295. xcb_xv_query_image_attributes (conn, a->base_id, f->id,
  296. width, height), NULL);
  297. if (i == NULL)
  298. continue;
  299. if (i->width != width || i->height != height)
  300. {
  301. msg_Warn (obj, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
  302. fmt->i_width, fmt->i_height,
  303. i->width, i->height);
  304. var_Create (obj->p_libvlc, "xvideo-res-error", VLC_VAR_BOOL);
  305. if (!var_GetBool (obj->p_libvlc, "xvideo-res-error"))
  306. {
  307. dialog_FatalWait (obj, _("Video acceleration not available"),
  308. _("The XVideo rendering acceleration driver does not "
  309. "support the required resolution of %ux%u pixels but "
  310. "%"PRIu32"x%"PRIu32" pixels instead.\n"
  311. "Acceleration will thus be disabled. Performance may "
  312. "be degraded severely if the resolution is large."),
  313. width, height, i->width, i->height);
  314. var_SetBool (obj->p_libvlc, "xvideo-res-error", true);
  315. }
  316. free (i);
  317. continue;
  318. }
  319. fmt->i_chroma = chroma;
  320. if (f->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
  321. {
  322. fmt->i_rmask = f->red_mask;
  323. fmt->i_gmask = f->green_mask;
  324. fmt->i_bmask = f->blue_mask;
  325. }
  326. *idp = f->id;
  327. free (attr);
  328. attr = i;
  329. if (rank == 0)
  330. break; /* shortcut for perfect match */
  331. }
  332. free (list);
  333. return attr;
  334. }
  335. /**
  336. * Probe the X server.
  337. */
  338. static int Open (vlc_object_t *obj)
  339. {
  340. vout_display_t *vd = (vout_display_t *)obj;
  341. vout_display_sys_t *p_sys;
  342. if (!var_InheritBool (obj, "overlay"))
  343. return VLC_EGENERIC;
  344. p_sys = malloc (sizeof (*p_sys));
  345. if (p_sys == NULL)
  346. return VLC_ENOMEM;
  347. vd->sys = p_sys;
  348. /* Connect to X */
  349. xcb_connection_t *conn;
  350. const xcb_screen_t *screen;
  351. uint8_t depth;
  352. p_sys->embed = GetWindow (vd, &conn, &screen, &depth);
  353. if (p_sys->embed == NULL)
  354. {
  355. free (p_sys);
  356. return VLC_EGENERIC;
  357. }
  358. p_sys->conn = conn;
  359. p_sys->att = NULL;
  360. p_sys->pool = NULL;
  361. if (!CheckXVideo (vd, conn))
  362. {
  363. msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
  364. goto error;
  365. }
  366. p_sys->window = xcb_generate_id (conn);
  367. xcb_pixmap_t pixmap = xcb_generate_id (conn);
  368. /* Cache adaptors infos */
  369. xcb_xv_query_adaptors_reply_t *adaptors =
  370. xcb_xv_query_adaptors_reply (conn,
  371. xcb_xv_query_adaptors (conn, p_sys->embed->handle.xid), NULL);
  372. if (adaptors == NULL)
  373. goto error;
  374. int forced_adaptor = var_InheritInteger (obj, "xvideo-adaptor");
  375. /* */
  376. video_format_t fmt;
  377. p_sys->port = 0;
  378. xcb_xv_adaptor_info_iterator_t it;
  379. for (it = xcb_xv_query_adaptors_info_iterator (adaptors);
  380. it.rem > 0;
  381. xcb_xv_adaptor_info_next (&it))
  382. {
  383. const xcb_xv_adaptor_info_t *a = it.data;
  384. char *name;
  385. if (forced_adaptor != -1 && forced_adaptor != 0)
  386. {
  387. forced_adaptor--;
  388. continue;
  389. }
  390. if (!(a->type & XCB_XV_TYPE_INPUT_MASK)
  391. || !(a->type & XCB_XV_TYPE_IMAGE_MASK))
  392. continue;
  393. /* Look for an image format */
  394. fmt = vd->fmt;
  395. free (p_sys->att);
  396. p_sys->att = FindFormat (obj, conn, &fmt, a, &p_sys->id);
  397. if (p_sys->att == NULL) /* No acceptable image formats */
  398. continue;
  399. /* Grab a port */
  400. for (unsigned i = 0; i < a->num_ports; i++)
  401. {
  402. xcb_xv_port_t port = a->base_id + i;
  403. xcb_xv_grab_port_reply_t *gr =
  404. xcb_xv_grab_port_reply (conn,
  405. xcb_xv_grab_port (conn, port, XCB_CURRENT_TIME), NULL);
  406. uint8_t result = gr ? gr->result : 0xff;
  407. free (gr);
  408. if (result == 0)
  409. {
  410. p_sys->port = port;
  411. goto grabbed_port;
  412. }
  413. msg_Dbg (vd, "cannot grab port %"PRIu32": Xv error %"PRIu8, port,
  414. result);
  415. }
  416. continue; /* No usable port */
  417. grabbed_port:
  418. /* Found port - initialize selected format */
  419. name = strndup (xcb_xv_adaptor_info_name (a), a->name_size);
  420. if (name != NULL)
  421. {
  422. msg_Dbg (vd, "using adaptor %s", name);
  423. free (name);
  424. }
  425. msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
  426. msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
  427. /* Look for an X11 visual, create a window */
  428. xcb_xv_format_t *f = xcb_xv_adaptor_info_formats (a);
  429. for (uint_fast16_t i = a->num_formats; i > 0; i--, f++)
  430. {
  431. if (f->depth != screen->root_depth)
  432. continue; /* this would fail anyway */
  433. uint32_t mask =
  434. XCB_CW_BACK_PIXMAP |
  435. XCB_CW_BACK_PIXEL |
  436. XCB_CW_BORDER_PIXMAP |
  437. XCB_CW_BORDER_PIXEL |
  438. XCB_CW_EVENT_MASK |
  439. XCB_CW_COLORMAP;
  440. const uint32_t list[] = {
  441. /* XCB_CW_BACK_PIXMAP */
  442. pixmap,
  443. /* XCB_CW_BACK_PIXEL */
  444. screen->black_pixel,
  445. /* XCB_CW_BORDER_PIXMAP */
  446. pixmap,
  447. /* XCB_CW_BORDER_PIXEL */
  448. screen->black_pixel,
  449. /* XCB_CW_EVENT_MASK */
  450. XCB_EVENT_MASK_VISIBILITY_CHANGE,
  451. /* XCB_CW_COLORMAP */
  452. screen->default_colormap,
  453. };
  454. xcb_void_cookie_t c;
  455. xcb_create_pixmap (conn, f->depth, pixmap, screen->root, 1, 1);
  456. c = xcb_create_window_checked (conn, f->depth, p_sys->window,
  457. p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
  458. XCB_WINDOW_CLASS_INPUT_OUTPUT, f->visual, mask, list);
  459. if (!CheckError (vd, conn, "cannot create X11 window", c))
  460. {
  461. msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32
  462. " (depth: %"PRIu8")", f->visual, f->depth);
  463. msg_Dbg (vd, "using X11 window 0x%08"PRIx32, p_sys->window);
  464. goto created_window;
  465. }
  466. }
  467. xcb_xv_ungrab_port (conn, p_sys->port, XCB_CURRENT_TIME);
  468. p_sys->port = 0;
  469. msg_Dbg (vd, "no usable X11 visual");
  470. continue; /* No workable XVideo format (visual/depth) */
  471. created_window:
  472. break;
  473. }
  474. free (adaptors);
  475. if (!p_sys->port)
  476. {
  477. msg_Err (vd, "no available XVideo adaptor");
  478. goto error;
  479. }
  480. /* Compute video (window) placement within the parent window */
  481. {
  482. xcb_map_window (conn, p_sys->window);
  483. vout_display_place_t place;
  484. vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
  485. p_sys->width = place.width;
  486. p_sys->height = place.height;
  487. /* */
  488. const uint32_t values[] = {
  489. place.x, place.y, place.width, place.height };
  490. xcb_configure_window (conn, p_sys->window,
  491. XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
  492. XCB_CONFIG_WINDOW_WIDTH |
  493. XCB_CONFIG_WINDOW_HEIGHT,
  494. values);
  495. }
  496. p_sys->visible = false;
  497. /* Create graphic context */
  498. p_sys->gc = xcb_generate_id (conn);
  499. xcb_create_gc (conn, p_sys->gc, p_sys->window, 0, NULL);
  500. msg_Dbg (vd, "using X11 graphic context 0x%08"PRIx32, p_sys->gc);
  501. /* Disable color keying if applicable */
  502. {
  503. xcb_intern_atom_reply_t *r =
  504. xcb_intern_atom_reply (conn,
  505. xcb_intern_atom (conn, 1, 21, "XV_AUTOPAINT_COLORKEY"), NULL);
  506. if (r != NULL && r->atom != 0)
  507. xcb_xv_set_port_attribute(conn, p_sys->port, r->atom, 1);
  508. }
  509. /* Create cursor */
  510. p_sys->cursor = CreateBlankCursor (conn, screen);
  511. p_sys->shm = CheckSHM (obj, conn);
  512. /* */
  513. vout_display_info_t info = vd->info;
  514. info.has_pictures_invalid = false;
  515. info.has_event_thread = true;
  516. /* Setup vout_display_t once everything is fine */
  517. p_sys->swap_uv = vlc_fourcc_AreUVPlanesSwapped (fmt.i_chroma,
  518. vd->fmt.i_chroma);
  519. if (p_sys->swap_uv)
  520. fmt.i_chroma = vd->fmt.i_chroma;
  521. vd->fmt = fmt;
  522. vd->info = info;
  523. vd->pool = Pool;
  524. vd->prepare = NULL;
  525. vd->display = Display;
  526. vd->control = Control;
  527. vd->manage = Manage;
  528. /* */
  529. bool is_fullscreen = vd->cfg->is_fullscreen;
  530. if (is_fullscreen && vout_window_SetFullScreen (p_sys->embed, true))
  531. is_fullscreen = false;
  532. vout_display_SendEventFullscreen (vd, is_fullscreen);
  533. unsigned width, height;
  534. if (!GetWindowSize (p_sys->embed, conn, &width, &height))
  535. vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
  536. return VLC_SUCCESS;
  537. error:
  538. Close (obj);
  539. return VLC_EGENERIC;
  540. }
  541. /**
  542. * Disconnect from the X server.
  543. */
  544. static void Close (vlc_object_t *obj)
  545. {
  546. vout_display_t *vd = (vout_display_t *)obj;
  547. vout_display_sys_t *p_sys = vd->sys;
  548. if (p_sys->pool)
  549. {
  550. for (unsigned i = 0; i < MAX_PICTURES; i++)
  551. {
  552. picture_resource_t *res = &p_sys->resource[i];
  553. if (!res->p->p_pixels)
  554. break;
  555. PictureResourceFree (res, NULL);
  556. }
  557. picture_pool_Delete (p_sys->pool);
  558. }
  559. /* show the default cursor */
  560. xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid, XCB_CW_CURSOR,
  561. &(uint32_t) { XCB_CURSOR_NONE });
  562. xcb_flush (p_sys->conn);
  563. free (p_sys->att);
  564. xcb_disconnect (p_sys->conn);
  565. vout_display_DeleteWindow (vd, p_sys->embed);
  566. free (p_sys);
  567. }
  568. static void PoolAlloc (vout_display_t *vd, unsigned requested_count)
  569. {
  570. vout_display_sys_t *p_sys = vd->sys;
  571. memset (p_sys->resource, 0, sizeof(p_sys->resource));
  572. const uint32_t *pitches= xcb_xv_query_image_attributes_pitches (p_sys->att);
  573. const uint32_t *offsets= xcb_xv_query_image_attributes_offsets (p_sys->att);
  574. const unsigned num_planes= __MIN(p_sys->att->num_planes, PICTURE_PLANE_MAX);
  575. p_sys->data_size = p_sys->att->data_size;
  576. picture_t *pic_array[MAX_PICTURES];
  577. requested_count = __MIN(requested_count, MAX_PICTURES);
  578. unsigned count;
  579. for (count = 0; count < requested_count; count++)
  580. {
  581. picture_resource_t *res = &p_sys->resource[count];
  582. for (unsigned i = 0; i < num_planes; i++)
  583. {
  584. uint32_t data_size;
  585. data_size = (i < num_planes - 1) ? offsets[i+1] : p_sys->data_size;
  586. res->p[i].i_lines = (data_size - offsets[i]) / pitches[i];
  587. res->p[i].i_pitch = pitches[i];
  588. }
  589. if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
  590. p_sys->conn, p_sys->shm))
  591. break;
  592. /* Allocate further planes as specified by XVideo */
  593. /* We assume that offsets[0] is zero */
  594. for (unsigned i = 1; i < num_planes; i++)
  595. res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
  596. if (p_sys->swap_uv)
  597. { /* YVU: swap U and V planes */
  598. uint8_t *buf = res->p[2].p_pixels;
  599. res->p[2].p_pixels = res->p[1].p_pixels;
  600. res->p[1].p_pixels = buf;
  601. }
  602. pic_array[count] = picture_NewFromResource (&vd->fmt, res);
  603. if (!pic_array[count])
  604. {
  605. PictureResourceFree (res, p_sys->conn);
  606. memset (res, 0, sizeof(*res));
  607. break;
  608. }
  609. }
  610. if (count == 0)
  611. return;
  612. p_sys->pool = picture_pool_New (count, pic_array);
  613. /* TODO release picture resources if NULL */
  614. xcb_flush (p_sys->conn);
  615. }
  616. /**
  617. * Return a direct buffer
  618. */
  619. static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
  620. {
  621. vout_display_sys_t *p_sys = vd->sys;
  622. if (!p_sys->pool)
  623. PoolAlloc (vd, requested_count);
  624. return p_sys->pool;
  625. }
  626. /**
  627. * Sends an image to the X server.
  628. */
  629. static void Display (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
  630. {
  631. vout_display_sys_t *p_sys = vd->sys;
  632. xcb_shm_seg_t segment = pic->p_sys->segment;
  633. xcb_void_cookie_t ck;
  634. if (!p_sys->visible)
  635. goto out;
  636. if (segment)
  637. ck = xcb_xv_shm_put_image_checked (p_sys->conn, p_sys->port,
  638. p_sys->window, p_sys->gc, segment, p_sys->id, 0,
  639. /* Src: */ vd->source.i_x_offset,
  640. vd->source.i_y_offset,
  641. vd->source.i_visible_width,
  642. vd->source.i_visible_height,
  643. /* Dst: */ 0, 0, p_sys->width, p_sys->height,
  644. /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
  645. pic->p->i_lines, false);
  646. else
  647. ck = xcb_xv_put_image_checked (p_sys->conn, p_sys->port, p_sys->window,
  648. p_sys->gc, p_sys->id,
  649. vd->source.i_x_offset,
  650. vd->source.i_y_offset,
  651. vd->source.i_visible_width,
  652. vd->source.i_visible_height,
  653. 0, 0, p_sys->width, p_sys->height,
  654. pic->p->i_pitch / pic->p->i_pixel_pitch,
  655. pic->p->i_lines,
  656. p_sys->data_size, pic->p->p_pixels);
  657. /* Wait for reply. See x11.c for rationale. */
  658. xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
  659. if (e != NULL)
  660. {
  661. msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
  662. free (e);
  663. }
  664. out:
  665. picture_Release (pic);
  666. (void)subpicture;
  667. }
  668. static int Control (vout_display_t *vd, int query, va_list ap)
  669. {
  670. vout_display_sys_t *p_sys = vd->sys;
  671. switch (query)
  672. {
  673. case VOUT_DISPLAY_CHANGE_FULLSCREEN:
  674. {
  675. const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
  676. return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
  677. }
  678. case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
  679. case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
  680. case VOUT_DISPLAY_CHANGE_ZOOM:
  681. case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
  682. case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
  683. {
  684. const vout_display_cfg_t *cfg;
  685. const video_format_t *source;
  686. bool is_forced = false;
  687. if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
  688. || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
  689. {
  690. source = (const video_format_t *)va_arg (ap, const video_format_t *);
  691. cfg = vd->cfg;
  692. }
  693. else
  694. {
  695. source = &vd->source;
  696. cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
  697. if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
  698. is_forced = (bool)va_arg (ap, int);
  699. }
  700. /* */
  701. if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
  702. && is_forced
  703. && (cfg->display.width != vd->cfg->display.width
  704. ||cfg->display.height != vd->cfg->display.height)
  705. && vout_window_SetSize (p_sys->embed,
  706. cfg->display.width,
  707. cfg->display.height))
  708. return VLC_EGENERIC;
  709. vout_display_place_t place;
  710. vout_display_PlacePicture (&place, source, cfg, false);
  711. p_sys->width = place.width;
  712. p_sys->height = place.height;
  713. /* Move the picture within the window */
  714. const uint32_t values[] = { place.x, place.y,
  715. place.width, place.height, };
  716. xcb_configure_window (p_sys->conn, p_sys->window,
  717. XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
  718. | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
  719. values);
  720. xcb_flush (p_sys->conn);
  721. return VLC_SUCCESS;
  722. }
  723. case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
  724. {
  725. unsigned state = va_arg (ap, unsigned);
  726. return vout_window_SetState (p_sys->embed, state);
  727. }
  728. /* Hide the mouse. It will be send when
  729. * vout_display_t::info.b_hide_mouse is false */
  730. case VOUT_DISPLAY_HIDE_MOUSE:
  731. xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
  732. XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
  733. xcb_flush (p_sys->conn);
  734. return VLC_SUCCESS;
  735. case VOUT_DISPLAY_RESET_PICTURES:
  736. assert(0);
  737. default:
  738. msg_Err (vd, "Unknown request in XCB vout display");
  739. return VLC_EGENERIC;
  740. }
  741. }
  742. static void Manage (vout_display_t *vd)
  743. {
  744. vout_display_sys_t *p_sys = vd->sys;
  745. ManageEvent (vd, p_sys->conn, &p_sys->visible);
  746. }