xcompcap-main.cpp 15 KB

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