xcompcap-main.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. #include <glad/glad.h>
  2. #include <glad/glad_glx.h>
  3. #include <X11/Xlib.h>
  4. #include <X11/extensions/Xcomposite.h>
  5. #include <pthread.h>
  6. #include <vector>
  7. #include <obs-module.h>
  8. #include <graphics/vec4.h>
  9. #include <util/platform.h>
  10. #include "xcompcap-main.hpp"
  11. #include "xcompcap-helper.hpp"
  12. #include "xcursor.h"
  13. #define xdisp (XCompcap::disp())
  14. #define WIN_STRING_DIV "\r\n"
  15. bool XCompcapMain::init()
  16. {
  17. if (!xdisp) {
  18. blog(LOG_ERROR, "failed opening display");
  19. return false;
  20. }
  21. int eventBase, errorBase;
  22. if (!XCompositeQueryExtension(xdisp, &eventBase, &errorBase)) {
  23. blog(LOG_ERROR, "Xcomposite extension not supported");
  24. return false;
  25. }
  26. int major = 0, minor = 2;
  27. XCompositeQueryVersion(xdisp, &major, &minor);
  28. if (major == 0 && minor < 2) {
  29. blog(LOG_ERROR, "Xcomposite extension is too old: %d.%d < 0.2",
  30. major, minor);
  31. return false;
  32. }
  33. return true;
  34. }
  35. void XCompcapMain::deinit()
  36. {
  37. XCompcap::cleanupDisplay();
  38. }
  39. obs_properties_t *XCompcapMain::properties()
  40. {
  41. obs_properties_t *props = obs_properties_create();
  42. obs_property_t *wins = obs_properties_add_list(props, "capture_window",
  43. obs_module_text("Window"),
  44. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  45. for (Window win: XCompcap::getTopLevelWindows()) {
  46. std::string wname = XCompcap::getWindowName(win);
  47. std::string cls = XCompcap::getWindowClass(win);
  48. std::string winid = std::to_string((long long)win);
  49. std::string desc =
  50. (winid + WIN_STRING_DIV + wname +
  51. WIN_STRING_DIV + cls);
  52. obs_property_list_add_string(wins, wname.c_str(),
  53. desc.c_str());
  54. }
  55. obs_properties_add_int(props, "cut_top", obs_module_text("CropTop"),
  56. 0, 4096, 1);
  57. obs_properties_add_int(props, "cut_left", obs_module_text("CropLeft"),
  58. 0, 4096, 1);
  59. obs_properties_add_int(props, "cut_right", obs_module_text("CropRight"),
  60. 0, 4096, 1);
  61. obs_properties_add_int(props, "cut_bot", obs_module_text("CropBottom"),
  62. 0, 4096, 1);
  63. obs_properties_add_bool(props, "swap_redblue",
  64. obs_module_text("SwapRedBlue"));
  65. obs_properties_add_bool(props, "lock_x", obs_module_text("LockX"));
  66. obs_properties_add_bool(props, "show_cursor",
  67. obs_module_text("CaptureCursor"));
  68. obs_properties_add_bool(props, "include_border",
  69. obs_module_text("IncludeXBorder"));
  70. obs_properties_add_bool(props, "exclude_alpha",
  71. obs_module_text("ExcludeAlpha"));
  72. return props;
  73. }
  74. void XCompcapMain::defaults(obs_data_t *settings)
  75. {
  76. obs_data_set_default_string(settings, "capture_window", "");
  77. obs_data_set_default_int(settings, "cut_top", 0);
  78. obs_data_set_default_int(settings, "cut_left", 0);
  79. obs_data_set_default_int(settings, "cut_right", 0);
  80. obs_data_set_default_int(settings, "cut_bot", 0);
  81. obs_data_set_default_bool(settings, "swap_redblue", false);
  82. obs_data_set_default_bool(settings, "lock_x", false);
  83. obs_data_set_default_bool(settings, "show_cursor", true);
  84. obs_data_set_default_bool(settings, "include_border", false);
  85. obs_data_set_default_bool(settings, "exclude_alpha", false);
  86. }
  87. #define FIND_WINDOW_INTERVAL 2.0
  88. struct XCompcapMain_private
  89. {
  90. XCompcapMain_private()
  91. :win(0)
  92. ,cut_top(0), cur_cut_top(0)
  93. ,cut_left(0), cur_cut_left(0)
  94. ,cut_right(0), cur_cut_right(0)
  95. ,cut_bot(0), cur_cut_bot(0)
  96. ,inverted(false)
  97. ,width(0),height(0)
  98. ,pixmap(0)
  99. ,glxpixmap(0)
  100. ,tex(0)
  101. ,gltex(0)
  102. {
  103. pthread_mutexattr_init(&lockattr);
  104. pthread_mutexattr_settype(&lockattr, PTHREAD_MUTEX_RECURSIVE);
  105. pthread_mutex_init(&lock, &lockattr);
  106. }
  107. ~XCompcapMain_private()
  108. {
  109. pthread_mutex_destroy(&lock);
  110. pthread_mutexattr_destroy(&lockattr);
  111. }
  112. obs_source_t *source;
  113. std::string windowName;
  114. Window win = 0;
  115. int cut_top, cur_cut_top;
  116. int cut_left, cur_cut_left;
  117. int cut_right, cur_cut_right;
  118. int cut_bot, cur_cut_bot;
  119. bool inverted;
  120. bool swapRedBlue;
  121. bool lockX;
  122. bool include_border;
  123. bool exclude_alpha;
  124. bool draw_opaque;
  125. double window_check_time = 0.0;
  126. uint32_t width;
  127. uint32_t height;
  128. uint32_t border;
  129. Pixmap pixmap;
  130. GLXPixmap glxpixmap;
  131. gs_texture_t *tex;
  132. gs_texture_t *gltex;
  133. pthread_mutex_t lock;
  134. pthread_mutexattr_t lockattr;
  135. bool show_cursor = true;
  136. bool cursor_outside = false;
  137. xcursor_t *cursor = nullptr;
  138. };
  139. XCompcapMain::XCompcapMain(obs_data_t *settings, obs_source_t *source)
  140. {
  141. p = new XCompcapMain_private;
  142. p->source = source;
  143. obs_enter_graphics();
  144. p->cursor = xcursor_init(xdisp);
  145. obs_leave_graphics();
  146. updateSettings(settings);
  147. }
  148. static void xcc_cleanup(XCompcapMain_private *p);
  149. XCompcapMain::~XCompcapMain()
  150. {
  151. ObsGsContextHolder obsctx;
  152. if (p->tex) {
  153. gs_texture_destroy(p->tex);
  154. p->tex = 0;
  155. }
  156. xcc_cleanup(p);
  157. if (p->cursor) {
  158. xcursor_destroy(p->cursor);
  159. p->cursor = nullptr;
  160. }
  161. delete p;
  162. }
  163. static Window getWindowFromString(std::string wstr)
  164. {
  165. XErrorLock xlock;
  166. if (wstr == "") {
  167. return XCompcap::getTopLevelWindows().front();
  168. }
  169. if (wstr.substr(0, 4) == "root") {
  170. int i = std::stoi("0" + wstr.substr(4));
  171. return RootWindow(xdisp, i);
  172. }
  173. size_t firstMark = wstr.find(WIN_STRING_DIV);
  174. size_t markSize = strlen(WIN_STRING_DIV);
  175. if (firstMark == std::string::npos)
  176. return (Window)std::stol(wstr);
  177. Window wid = 0;
  178. wstr = wstr.substr(firstMark + markSize);
  179. size_t lastMark = wstr.rfind(WIN_STRING_DIV);
  180. std::string wname = wstr.substr(0, lastMark);
  181. std::string wcls = wstr.substr(lastMark + markSize);
  182. Window matchedNameWin = wid;
  183. for (Window cwin: XCompcap::getTopLevelWindows()) {
  184. std::string cwinname = XCompcap::getWindowName(cwin);
  185. std::string ccls = XCompcap::getWindowClass(cwin);
  186. if (cwin == wid && wname == cwinname && wcls == ccls)
  187. return wid;
  188. if (wname == cwinname ||
  189. (!matchedNameWin && !wcls.empty() && wcls == ccls))
  190. matchedNameWin = cwin;
  191. }
  192. return matchedNameWin;
  193. }
  194. static void xcc_cleanup(XCompcapMain_private *p)
  195. {
  196. PLock lock(&p->lock);
  197. XDisplayLock xlock;
  198. if (p->gltex) {
  199. GLuint gltex = *(GLuint*)gs_texture_get_obj(p->gltex);
  200. glBindTexture(GL_TEXTURE_2D, gltex);
  201. glXReleaseTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_LEFT_EXT);
  202. gs_texture_destroy(p->gltex);
  203. p->gltex = 0;
  204. }
  205. if (p->glxpixmap) {
  206. glXDestroyPixmap(xdisp, p->glxpixmap);
  207. p->glxpixmap = 0;
  208. }
  209. if (p->pixmap) {
  210. XFreePixmap(xdisp, p->pixmap);
  211. p->pixmap = 0;
  212. }
  213. if (p->win) {
  214. XCompositeUnredirectWindow(xdisp, p->win,
  215. CompositeRedirectAutomatic);
  216. XSelectInput(xdisp, p->win, 0);
  217. p->win = 0;
  218. }
  219. }
  220. void XCompcapMain::updateSettings(obs_data_t *settings)
  221. {
  222. PLock lock(&p->lock);
  223. XErrorLock xlock;
  224. ObsGsContextHolder obsctx;
  225. blog(LOG_DEBUG, "Settings updating");
  226. Window prevWin = p->win;
  227. xcc_cleanup(p);
  228. if (settings) {
  229. const char *windowName = obs_data_get_string(settings,
  230. "capture_window");
  231. p->windowName = windowName;
  232. p->win = getWindowFromString(windowName);
  233. p->cut_top = obs_data_get_int(settings, "cut_top");
  234. p->cut_left = obs_data_get_int(settings, "cut_left");
  235. p->cut_right = obs_data_get_int(settings, "cut_right");
  236. p->cut_bot = obs_data_get_int(settings, "cut_bot");
  237. p->lockX = obs_data_get_bool(settings, "lock_x");
  238. p->swapRedBlue = obs_data_get_bool(settings, "swap_redblue");
  239. p->show_cursor = obs_data_get_bool(settings, "show_cursor");
  240. p->include_border = obs_data_get_bool(settings, "include_border");
  241. p->exclude_alpha = obs_data_get_bool(settings, "exclude_alpha");
  242. p->draw_opaque = false;
  243. } else {
  244. p->win = prevWin;
  245. }
  246. xlock.resetError();
  247. if (p->win)
  248. XCompositeRedirectWindow(xdisp, p->win,
  249. CompositeRedirectAutomatic);
  250. if (xlock.gotError()) {
  251. blog(LOG_ERROR, "XCompositeRedirectWindow failed: %s",
  252. xlock.getErrorText().c_str());
  253. return;
  254. }
  255. if (p->win)
  256. XSelectInput(xdisp, p->win,
  257. StructureNotifyMask
  258. | ExposureMask
  259. | VisibilityChangeMask);
  260. XSync(xdisp, 0);
  261. XWindowAttributes attr;
  262. if (!p->win || !XGetWindowAttributes(xdisp, p->win, &attr)) {
  263. p->win = 0;
  264. p->width = 0;
  265. p->height = 0;
  266. return;
  267. }
  268. if (p->win && p->cursor && p->show_cursor) {
  269. Window child;
  270. int x, y;
  271. XTranslateCoordinates(xdisp, p->win, attr.root, 0, 0, &x, &y,
  272. &child);
  273. xcursor_offset(p->cursor, x, y);
  274. }
  275. gs_color_format cf = GS_RGBA;
  276. if (p->exclude_alpha) {
  277. cf = GS_BGRX;
  278. }
  279. bool has_alpha = true;
  280. const int attrs[] =
  281. {
  282. GLX_BIND_TO_TEXTURE_RGBA_EXT, GL_TRUE,
  283. GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
  284. GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
  285. GLX_ALPHA_SIZE, 8,
  286. GLX_DOUBLEBUFFER, GL_FALSE,
  287. None
  288. };
  289. int nelem = 0;
  290. GLXFBConfig *configs = glXGetFBConfigs(xdisp,
  291. XCompcap::getRootWindowScreen(attr.root),
  292. &nelem);
  293. if (nelem <= 0) {
  294. blog(LOG_ERROR, "no fb configs available");
  295. p->win = 0;
  296. p->height = 0;
  297. p->width = 0;
  298. return;
  299. }
  300. GLXFBConfig config;
  301. for (int i = 0; i < nelem; i++) {
  302. config = configs[i];
  303. XVisualInfo *visual = glXGetVisualFromFBConfig(xdisp, config);
  304. if (!visual)
  305. continue;
  306. if (attr.visual->visualid != visual->visualid) {
  307. XFree(visual);
  308. continue;
  309. }
  310. XFree(visual);
  311. int value;
  312. glXGetFBConfigAttrib(xdisp, config, GLX_ALPHA_SIZE, &value);
  313. if (value != 8)
  314. has_alpha = false;
  315. break;
  316. }
  317. XFree(configs);
  318. configs = glXChooseFBConfig(xdisp,
  319. XCompcap::getRootWindowScreen(attr.root),
  320. attrs, &nelem);
  321. if (nelem <= 0) {
  322. blog(LOG_ERROR, "no matching fb config found");
  323. p->win = 0;
  324. p->height = 0;
  325. p->width = 0;
  326. return;
  327. }
  328. bool found = false;
  329. for (int i = 0; i < nelem; i++) {
  330. config = configs[i];
  331. XVisualInfo *visual = glXGetVisualFromFBConfig(xdisp, config);
  332. if (!visual)
  333. continue;
  334. if (attr.depth != visual->depth) {
  335. XFree(visual);
  336. continue;
  337. }
  338. XFree(visual);
  339. found = true;
  340. break;
  341. }
  342. if (!found)
  343. config = configs[0];
  344. if (cf == GS_BGRX || !has_alpha) {
  345. p->draw_opaque = true;
  346. }
  347. int inverted;
  348. glXGetFBConfigAttrib(xdisp, config, GLX_Y_INVERTED_EXT, &inverted);
  349. p->inverted = inverted != 0;
  350. p->border = attr.border_width;
  351. if (p->include_border) {
  352. p->width = attr.width + p->border * 2;
  353. p->height = attr.height + p->border * 2;
  354. } else {
  355. p->width = attr.width;
  356. p->height = attr.height;
  357. }
  358. if (p->cut_top + p->cut_bot < (int)p->height) {
  359. p->cur_cut_top = p->cut_top;
  360. p->cur_cut_bot = p->cut_bot;
  361. } else {
  362. p->cur_cut_top = 0;
  363. p->cur_cut_bot = 0;
  364. }
  365. if (p->cut_left + p->cut_right < (int)p->width) {
  366. p->cur_cut_left = p->cut_left;
  367. p->cur_cut_right = p->cut_right;
  368. } else {
  369. p->cur_cut_left = 0;
  370. p->cur_cut_right = 0;
  371. }
  372. if (p->tex)
  373. gs_texture_destroy(p->tex);
  374. uint8_t *texData = new uint8_t[width() * height() * 4];
  375. memset(texData, 0, width() * height() * 4);
  376. const uint8_t* texDataArr[] = { texData, 0 };
  377. p->tex = gs_texture_create(width(), height(), cf, 1,
  378. texDataArr, 0);
  379. delete[] texData;
  380. if (p->swapRedBlue) {
  381. GLuint tex = *(GLuint*)gs_texture_get_obj(p->tex);
  382. glBindTexture(GL_TEXTURE_2D, tex);
  383. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
  384. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
  385. glBindTexture(GL_TEXTURE_2D, 0);
  386. }
  387. xlock.resetError();
  388. p->pixmap = XCompositeNameWindowPixmap(xdisp, p->win);
  389. if (xlock.gotError()) {
  390. blog(LOG_ERROR, "XCompositeNameWindowPixmap failed: %s",
  391. xlock.getErrorText().c_str());
  392. p->pixmap = 0;
  393. XFree(configs);
  394. return;
  395. }
  396. const int attribs_alpha[] =
  397. {
  398. GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
  399. GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT,
  400. None
  401. };
  402. const int attribs_no_alpha[] =
  403. {
  404. GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
  405. GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGB_EXT,
  406. None
  407. };
  408. const int *attribs = cf == GS_RGBA ? attribs_alpha : attribs_no_alpha;
  409. p->glxpixmap = glXCreatePixmap(xdisp, config, p->pixmap, attribs);
  410. if (xlock.gotError()) {
  411. blog(LOG_ERROR, "glXCreatePixmap failed: %s",
  412. xlock.getErrorText().c_str());
  413. XFreePixmap(xdisp, p->pixmap);
  414. XFree(configs);
  415. p->pixmap = 0;
  416. p->glxpixmap = 0;
  417. return;
  418. }
  419. XFree(configs);
  420. p->gltex = gs_texture_create(p->width, p->height, cf, 1, 0,
  421. GS_GL_DUMMYTEX);
  422. GLuint gltex = *(GLuint*)gs_texture_get_obj(p->gltex);
  423. glBindTexture(GL_TEXTURE_2D, gltex);
  424. glXBindTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_LEFT_EXT, NULL);
  425. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  426. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  427. if (!p->windowName.empty()) {
  428. blog(LOG_INFO, "[window-capture: '%s'] update settings:\n"
  429. "\ttitle: %s\n"
  430. "\tclass: %s\n"
  431. "\tHas alpha: %s\n"
  432. "\tFound proper GLXFBConfig: %s\n",
  433. obs_source_get_name(p->source),
  434. XCompcap::getWindowName(p->win).c_str(),
  435. XCompcap::getWindowClass(p->win).c_str(),
  436. has_alpha ? "yes" : "no",
  437. found ? "yes" : "no");
  438. blog(LOG_DEBUG, "\n"
  439. "\tid: %s",
  440. std::to_string((long long)p->win).c_str());
  441. }
  442. }
  443. void XCompcapMain::tick(float seconds)
  444. {
  445. if (!obs_source_showing(p->source))
  446. return;
  447. PLock lock(&p->lock, true);
  448. if (!lock.isLocked())
  449. return;
  450. XCompcap::processEvents();
  451. if (p->win && XCompcap::windowWasReconfigured(p->win)) {
  452. p->window_check_time = FIND_WINDOW_INTERVAL;
  453. p->win = 0;
  454. }
  455. XDisplayLock xlock;
  456. XWindowAttributes attr;
  457. if (!p->win || !XGetWindowAttributes(xdisp, p->win, &attr)) {
  458. p->window_check_time += (double)seconds;
  459. if (p->window_check_time < FIND_WINDOW_INTERVAL)
  460. return;
  461. Window newWin = getWindowFromString(p->windowName);
  462. p->window_check_time = 0.0;
  463. if (newWin && XGetWindowAttributes(xdisp, newWin, &attr)) {
  464. p->win = newWin;
  465. updateSettings(0);
  466. } else {
  467. return;
  468. }
  469. }
  470. if (!p->tex || !p->gltex)
  471. return;
  472. obs_enter_graphics();
  473. if (p->lockX) {
  474. XLockDisplay(xdisp);
  475. XSync(xdisp, 0);
  476. }
  477. if (p->include_border) {
  478. gs_copy_texture_region(
  479. p->tex, 0, 0,
  480. p->gltex,
  481. p->cur_cut_left,
  482. p->cur_cut_top,
  483. width(), height());
  484. } else {
  485. gs_copy_texture_region(
  486. p->tex, 0, 0,
  487. p->gltex,
  488. p->cur_cut_left + p->border,
  489. p->cur_cut_top + p->border,
  490. width(), height());
  491. }
  492. if (p->cursor && p->show_cursor) {
  493. xcursor_tick(p->cursor);
  494. p->cursor_outside =
  495. p->cursor->x < p->cur_cut_left ||
  496. p->cursor->y < p->cur_cut_top ||
  497. p->cursor->x > int(p->width - p->cur_cut_right) ||
  498. p->cursor->y > int(p->height - p->cur_cut_bot);
  499. }
  500. if (p->lockX)
  501. XUnlockDisplay(xdisp);
  502. obs_leave_graphics();
  503. }
  504. void XCompcapMain::render(gs_effect_t *effect)
  505. {
  506. if (!p->win)
  507. return;
  508. PLock lock(&p->lock, true);
  509. if (p->draw_opaque)
  510. effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  511. else
  512. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  513. if (!lock.isLocked() || !p->tex)
  514. return;
  515. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  516. gs_effect_set_texture(image, p->tex);
  517. while (gs_effect_loop(effect, "Draw")) {
  518. gs_draw_sprite(p->tex, 0, 0, 0);
  519. }
  520. if (p->cursor && p->gltex && p->show_cursor && !p->cursor_outside) {
  521. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  522. while (gs_effect_loop(effect, "Draw")) {
  523. xcursor_render(p->cursor);
  524. }
  525. }
  526. }
  527. uint32_t XCompcapMain::width()
  528. {
  529. if (!p->win)
  530. return 0;
  531. return p->width - p->cur_cut_left - p->cur_cut_right;
  532. }
  533. uint32_t XCompcapMain::height()
  534. {
  535. if (!p->win)
  536. return 0;
  537. return p->height - p->cur_cut_bot - p->cur_cut_top;
  538. }