xcompcap-main.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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 <algorithm>
  7. #include <vector>
  8. #include <obs-module.h>
  9. #include <graphics/vec4.h>
  10. #include <util/platform.h>
  11. #include "xcompcap-main.hpp"
  12. #include "xcompcap-helper.hpp"
  13. #include "xcursor.h"
  14. #define xdisp (XCompcap::disp())
  15. #define WIN_STRING_DIV "\r\n"
  16. bool XCompcapMain::init()
  17. {
  18. if (!xdisp) {
  19. blog(LOG_ERROR, "failed opening display");
  20. return false;
  21. }
  22. XInitThreads();
  23. int eventBase, errorBase;
  24. if (!XCompositeQueryExtension(xdisp, &eventBase, &errorBase)) {
  25. blog(LOG_ERROR, "Xcomposite extension not supported");
  26. return false;
  27. }
  28. int major = 0, minor = 2;
  29. XCompositeQueryVersion(xdisp, &major, &minor);
  30. if (major == 0 && minor < 2) {
  31. blog(LOG_ERROR, "Xcomposite extension is too old: %d.%d < 0.2",
  32. major, minor);
  33. return false;
  34. }
  35. return true;
  36. }
  37. void XCompcapMain::deinit()
  38. {
  39. XCompcap::cleanupDisplay();
  40. }
  41. obs_properties_t *XCompcapMain::properties()
  42. {
  43. obs_properties_t *props = obs_properties_create();
  44. obs_property_t *wins = obs_properties_add_list(
  45. props, "capture_window", obs_module_text("Window"),
  46. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  47. struct WindowInfo {
  48. std::string lex_comparable;
  49. std::string name;
  50. std::string desc;
  51. };
  52. std::vector<WindowInfo> window_strings;
  53. for (Window win : XCompcap::getTopLevelWindows()) {
  54. std::string wname = XCompcap::getWindowName(win);
  55. std::string cls = XCompcap::getWindowClass(win);
  56. std::string winid = std::to_string((long long)win);
  57. std::string desc =
  58. (winid + WIN_STRING_DIV + wname + WIN_STRING_DIV + cls);
  59. std::string wname_lowercase = wname;
  60. std::transform(wname_lowercase.begin(), wname_lowercase.end(),
  61. wname_lowercase.begin(),
  62. [](unsigned char c) { return std::tolower(c); });
  63. window_strings.push_back({.lex_comparable = wname_lowercase,
  64. .name = wname,
  65. .desc = desc});
  66. }
  67. std::sort(window_strings.begin(), window_strings.end(),
  68. [](const WindowInfo &a, const WindowInfo &b) -> bool {
  69. return std::lexicographical_compare(
  70. a.lex_comparable.begin(),
  71. a.lex_comparable.end(),
  72. b.lex_comparable.begin(),
  73. b.lex_comparable.end());
  74. });
  75. for (auto s : window_strings) {
  76. obs_property_list_add_string(wins, s.name.c_str(),
  77. s.desc.c_str());
  78. }
  79. obs_properties_add_int(props, "cut_top", obs_module_text("CropTop"), 0,
  80. 4096, 1);
  81. obs_properties_add_int(props, "cut_left", obs_module_text("CropLeft"),
  82. 0, 4096, 1);
  83. obs_properties_add_int(props, "cut_right", obs_module_text("CropRight"),
  84. 0, 4096, 1);
  85. obs_properties_add_int(props, "cut_bot", obs_module_text("CropBottom"),
  86. 0, 4096, 1);
  87. obs_properties_add_bool(props, "swap_redblue",
  88. obs_module_text("SwapRedBlue"));
  89. obs_properties_add_bool(props, "lock_x", obs_module_text("LockX"));
  90. obs_properties_add_bool(props, "show_cursor",
  91. obs_module_text("CaptureCursor"));
  92. obs_properties_add_bool(props, "include_border",
  93. obs_module_text("IncludeXBorder"));
  94. obs_properties_add_bool(props, "exclude_alpha",
  95. obs_module_text("ExcludeAlpha"));
  96. return props;
  97. }
  98. void XCompcapMain::defaults(obs_data_t *settings)
  99. {
  100. obs_data_set_default_string(settings, "capture_window", "");
  101. obs_data_set_default_int(settings, "cut_top", 0);
  102. obs_data_set_default_int(settings, "cut_left", 0);
  103. obs_data_set_default_int(settings, "cut_right", 0);
  104. obs_data_set_default_int(settings, "cut_bot", 0);
  105. obs_data_set_default_bool(settings, "swap_redblue", false);
  106. obs_data_set_default_bool(settings, "lock_x", false);
  107. obs_data_set_default_bool(settings, "show_cursor", true);
  108. obs_data_set_default_bool(settings, "include_border", false);
  109. obs_data_set_default_bool(settings, "exclude_alpha", false);
  110. }
  111. #define FIND_WINDOW_INTERVAL 0.5
  112. struct XCompcapMain_private {
  113. XCompcapMain_private()
  114. : win(0),
  115. cut_top(0),
  116. cur_cut_top(0),
  117. cut_left(0),
  118. cur_cut_left(0),
  119. cut_right(0),
  120. cur_cut_right(0),
  121. cut_bot(0),
  122. cur_cut_bot(0),
  123. inverted(false),
  124. width(0),
  125. height(0),
  126. pixmap(0),
  127. glxpixmap(0),
  128. tex(0),
  129. gltex(0)
  130. {
  131. pthread_mutexattr_init(&lockattr);
  132. pthread_mutexattr_settype(&lockattr, PTHREAD_MUTEX_RECURSIVE);
  133. pthread_mutex_init(&lock, &lockattr);
  134. }
  135. ~XCompcapMain_private()
  136. {
  137. pthread_mutex_destroy(&lock);
  138. pthread_mutexattr_destroy(&lockattr);
  139. }
  140. obs_source_t *source;
  141. std::string windowName;
  142. Window win = 0;
  143. int cut_top, cur_cut_top;
  144. int cut_left, cur_cut_left;
  145. int cut_right, cur_cut_right;
  146. int cut_bot, cur_cut_bot;
  147. bool inverted;
  148. bool swapRedBlue;
  149. bool lockX;
  150. bool include_border;
  151. bool exclude_alpha;
  152. bool draw_opaque;
  153. double window_check_time = 0.0;
  154. uint32_t width;
  155. uint32_t height;
  156. uint32_t border;
  157. Pixmap pixmap;
  158. GLXPixmap glxpixmap;
  159. gs_texture_t *tex;
  160. gs_texture_t *gltex;
  161. pthread_mutex_t lock;
  162. pthread_mutexattr_t lockattr;
  163. bool show_cursor = true;
  164. bool cursor_outside = false;
  165. xcursor_t *cursor = nullptr;
  166. bool tick_error_suppressed = false;
  167. // Whether to rebind the GLX Pixmap on every tick. This is the correct
  168. // mode of operation, according to GLX_EXT_texture_from_pixmap. However
  169. // certain drivers exhibits poor performance when this is done, so
  170. // setting this to false allows working around it.
  171. bool strict_binding = true;
  172. };
  173. XCompcapMain::XCompcapMain(obs_data_t *settings, obs_source_t *source)
  174. {
  175. p = new XCompcapMain_private;
  176. p->source = source;
  177. obs_enter_graphics();
  178. if (strcmp(reinterpret_cast<const char *>(glGetString(GL_VENDOR)),
  179. "NVIDIA Corporation") == 0) {
  180. // Pixmap binds are extremely slow on NVIDIA cards (https://github.com/obsproject/obs-studio/issues/5685)
  181. p->strict_binding = false;
  182. }
  183. p->cursor = xcursor_init(xdisp);
  184. obs_leave_graphics();
  185. updateSettings(settings);
  186. }
  187. static void xcc_cleanup(XCompcapMain_private *p);
  188. XCompcapMain::~XCompcapMain()
  189. {
  190. ObsGsContextHolder obsctx;
  191. XCompcap::unregisterSource(this);
  192. if (p->tex) {
  193. gs_texture_destroy(p->tex);
  194. p->tex = 0;
  195. }
  196. xcc_cleanup(p);
  197. if (p->cursor) {
  198. xcursor_destroy(p->cursor);
  199. p->cursor = nullptr;
  200. }
  201. delete p;
  202. }
  203. static Window getWindowFromString(std::string wstr)
  204. {
  205. XErrorLock xlock;
  206. if (wstr == "") {
  207. return XCompcap::getTopLevelWindows().front();
  208. }
  209. if (wstr.substr(0, 4) == "root") {
  210. int i = std::stoi("0" + wstr.substr(4));
  211. return RootWindow(xdisp, i);
  212. }
  213. size_t firstMark = wstr.find(WIN_STRING_DIV);
  214. size_t lastMark = wstr.rfind(WIN_STRING_DIV);
  215. size_t markSize = strlen(WIN_STRING_DIV);
  216. // wstr only consists of the window-id
  217. if (firstMark == std::string::npos)
  218. return (Window)std::stol(wstr);
  219. // wstr also contains window-name and window-class
  220. std::string wid = wstr.substr(0, firstMark);
  221. std::string wname = wstr.substr(firstMark + markSize,
  222. lastMark - firstMark - markSize);
  223. std::string wcls = wstr.substr(lastMark + markSize);
  224. Window winById = (Window)std::stol(wid);
  225. // first try to find a match by the window-id
  226. for (Window cwin : XCompcap::getTopLevelWindows()) {
  227. // match by window-id
  228. if (cwin == winById) {
  229. return cwin;
  230. }
  231. }
  232. // then try to find a match by name & class
  233. for (Window cwin : XCompcap::getTopLevelWindows()) {
  234. std::string cwinname = XCompcap::getWindowName(cwin);
  235. std::string ccls = XCompcap::getWindowClass(cwin);
  236. // match by name and class
  237. if (wname == cwinname && wcls == ccls) {
  238. return cwin;
  239. }
  240. }
  241. // no match
  242. blog(LOG_DEBUG, "Did not find Window By ID %s, Name '%s' or Class '%s'",
  243. wid.c_str(), wname.c_str(), wcls.c_str());
  244. return 0;
  245. }
  246. static void xcc_cleanup(XCompcapMain_private *p)
  247. {
  248. PLock lock(&p->lock);
  249. XErrorLock xlock;
  250. if (p->gltex) {
  251. GLuint gltex = *(GLuint *)gs_texture_get_obj(p->gltex);
  252. glBindTexture(GL_TEXTURE_2D, gltex);
  253. if (p->glxpixmap) {
  254. glXReleaseTexImageEXT(xdisp, p->glxpixmap,
  255. GLX_FRONT_EXT);
  256. if (xlock.gotError()) {
  257. blog(LOG_ERROR,
  258. "cleanup glXReleaseTexImageEXT failed: %s",
  259. xlock.getErrorText().c_str());
  260. xlock.resetError();
  261. }
  262. glXDestroyPixmap(xdisp, p->glxpixmap);
  263. if (xlock.gotError()) {
  264. blog(LOG_ERROR,
  265. "cleanup glXDestroyPixmap failed: %s",
  266. xlock.getErrorText().c_str());
  267. xlock.resetError();
  268. }
  269. p->glxpixmap = 0;
  270. }
  271. gs_texture_destroy(p->gltex);
  272. p->gltex = 0;
  273. }
  274. if (p->pixmap) {
  275. XFreePixmap(xdisp, p->pixmap);
  276. if (xlock.gotError()) {
  277. blog(LOG_ERROR, "cleanup glXDestroyPixmap failed: %s",
  278. xlock.getErrorText().c_str());
  279. xlock.resetError();
  280. }
  281. p->pixmap = 0;
  282. }
  283. if (p->win) {
  284. p->win = 0;
  285. }
  286. if (p->tex) {
  287. gs_texture_destroy(p->tex);
  288. p->tex = 0;
  289. }
  290. }
  291. static gs_color_format gs_format_from_tex()
  292. {
  293. GLint iformat = 0;
  294. // consider GL_ARB_internalformat_query
  295. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT,
  296. &iformat);
  297. // These formats are known to be wrong on Intel platforms. We intentionally
  298. // use swapped internal formats here to preserve historic behavior which
  299. // swapped colors accidentally and because D3D11 would not support a
  300. // GS_RGBX format
  301. switch (iformat) {
  302. case GL_RGB:
  303. return GS_BGRX_UNORM;
  304. case GL_RGBA:
  305. return GS_RGBA_UNORM;
  306. default:
  307. return GS_RGBA_UNORM;
  308. }
  309. }
  310. // from libobs-opengl/gl-subsystem.h because we need to handle GLX modifying textures outside libobs.
  311. struct fb_info;
  312. struct gs_texture {
  313. gs_device_t *device;
  314. enum gs_texture_type type;
  315. enum gs_color_format format;
  316. GLenum gl_format;
  317. GLenum gl_target;
  318. GLenum gl_internal_format;
  319. GLenum gl_type;
  320. GLuint texture;
  321. uint32_t levels;
  322. bool is_dynamic;
  323. bool is_render_target;
  324. bool is_dummy;
  325. bool gen_mipmaps;
  326. gs_samplerstate_t *cur_sampler;
  327. struct fbo_info *fbo;
  328. };
  329. // End shitty hack.
  330. void XCompcapMain::updateSettings(obs_data_t *settings)
  331. {
  332. ObsGsContextHolder obsctx;
  333. XErrorLock xlock;
  334. PLock lock(&p->lock);
  335. blog(LOG_DEBUG, "Settings updating");
  336. Window prevWin = p->win;
  337. xcc_cleanup(p);
  338. p->tick_error_suppressed = false;
  339. if (settings) {
  340. /* Settings initialized or changed */
  341. const char *windowName =
  342. obs_data_get_string(settings, "capture_window");
  343. p->windowName = windowName;
  344. p->win = getWindowFromString(windowName);
  345. XCompcap::registerSource(this, p->win);
  346. p->cut_top = obs_data_get_int(settings, "cut_top");
  347. p->cut_left = obs_data_get_int(settings, "cut_left");
  348. p->cut_right = obs_data_get_int(settings, "cut_right");
  349. p->cut_bot = obs_data_get_int(settings, "cut_bot");
  350. p->lockX = obs_data_get_bool(settings, "lock_x");
  351. p->swapRedBlue = obs_data_get_bool(settings, "swap_redblue");
  352. p->show_cursor = obs_data_get_bool(settings, "show_cursor");
  353. p->include_border =
  354. obs_data_get_bool(settings, "include_border");
  355. p->exclude_alpha = obs_data_get_bool(settings, "exclude_alpha");
  356. p->draw_opaque = false;
  357. } else {
  358. /* New Window found (stored in p->win), just re-initialize GL-Mapping */
  359. p->win = prevWin;
  360. }
  361. if (xlock.gotError()) {
  362. blog(LOG_ERROR, "registeringSource failed: %s",
  363. xlock.getErrorText().c_str());
  364. return;
  365. }
  366. XSync(xdisp, 0);
  367. XWindowAttributes attr;
  368. if (!p->win || !XGetWindowAttributes(xdisp, p->win, &attr)) {
  369. p->win = 0;
  370. p->width = 0;
  371. p->height = 0;
  372. return;
  373. }
  374. if (p->win && p->cursor && p->show_cursor) {
  375. Window child;
  376. int x, y;
  377. XTranslateCoordinates(xdisp, p->win, attr.root, 0, 0, &x, &y,
  378. &child);
  379. xcursor_offset(p->cursor, x, y);
  380. }
  381. const int config_attrs[] = {GLX_BIND_TO_TEXTURE_RGBA_EXT,
  382. GL_TRUE,
  383. GLX_DRAWABLE_TYPE,
  384. GLX_PIXMAP_BIT,
  385. GLX_BIND_TO_TEXTURE_TARGETS_EXT,
  386. GLX_TEXTURE_2D_BIT_EXT,
  387. GLX_DOUBLEBUFFER,
  388. GL_FALSE,
  389. None};
  390. int nelem = 0;
  391. GLXFBConfig *configs = glXChooseFBConfig(
  392. xdisp, XCompcap::getRootWindowScreen(attr.root), config_attrs,
  393. &nelem);
  394. bool found = false;
  395. GLXFBConfig config;
  396. for (int i = 0; i < nelem; i++) {
  397. config = configs[i];
  398. XVisualInfo *visual = glXGetVisualFromFBConfig(xdisp, config);
  399. if (!visual)
  400. continue;
  401. if (attr.depth != visual->depth) {
  402. XFree(visual);
  403. continue;
  404. }
  405. XFree(visual);
  406. found = true;
  407. break;
  408. }
  409. if (!found) {
  410. blog(LOG_ERROR, "no matching fb config found");
  411. p->win = 0;
  412. p->height = 0;
  413. p->width = 0;
  414. XFree(configs);
  415. return;
  416. }
  417. if (p->exclude_alpha || attr.depth != 32) {
  418. p->draw_opaque = true;
  419. }
  420. int inverted;
  421. glXGetFBConfigAttrib(xdisp, config, GLX_Y_INVERTED_EXT, &inverted);
  422. p->inverted = inverted != 0;
  423. p->border = attr.border_width;
  424. if (p->include_border) {
  425. p->width = attr.width + p->border * 2;
  426. p->height = attr.height + p->border * 2;
  427. } else {
  428. p->width = attr.width;
  429. p->height = attr.height;
  430. }
  431. if (p->cut_top + p->cut_bot < (int)p->height) {
  432. p->cur_cut_top = p->cut_top;
  433. p->cur_cut_bot = p->cut_bot;
  434. } else {
  435. p->cur_cut_top = 0;
  436. p->cur_cut_bot = 0;
  437. }
  438. if (p->cut_left + p->cut_right < (int)p->width) {
  439. p->cur_cut_left = p->cut_left;
  440. p->cur_cut_right = p->cut_right;
  441. } else {
  442. p->cur_cut_left = 0;
  443. p->cur_cut_right = 0;
  444. }
  445. // Precautionary since we dont error check every GLX call above.
  446. xlock.resetError();
  447. p->pixmap = XCompositeNameWindowPixmap(xdisp, p->win);
  448. if (xlock.gotError()) {
  449. blog(LOG_ERROR, "XCompositeNameWindowPixmap failed: %s",
  450. xlock.getErrorText().c_str());
  451. p->pixmap = 0;
  452. XFree(configs);
  453. return;
  454. }
  455. // Should be consistent format with config we are using. Since we searched on RGBA lets use RGBA here.
  456. const int pixmap_attrs[] = {GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
  457. GLX_TEXTURE_FORMAT_EXT,
  458. GLX_TEXTURE_FORMAT_RGBA_EXT, None};
  459. p->glxpixmap = glXCreatePixmap(xdisp, config, p->pixmap, pixmap_attrs);
  460. if (xlock.gotError()) {
  461. blog(LOG_ERROR, "glXCreatePixmap failed: %s",
  462. xlock.getErrorText().c_str());
  463. XFreePixmap(xdisp, p->pixmap);
  464. XFree(configs);
  465. p->pixmap = 0;
  466. p->glxpixmap = 0;
  467. return;
  468. }
  469. XFree(configs);
  470. // Build an OBS texture to bind the pixmap to.
  471. p->gltex = gs_texture_create(p->width, p->height, GS_RGBA_UNORM, 1, 0,
  472. GS_GL_DUMMYTEX);
  473. GLuint gltex = *(GLuint *)gs_texture_get_obj(p->gltex);
  474. glBindTexture(GL_TEXTURE_2D, gltex);
  475. glXBindTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_EXT, nullptr);
  476. if (xlock.gotError()) {
  477. blog(LOG_ERROR, "glXBindTexImageEXT failed: %s",
  478. xlock.getErrorText().c_str());
  479. XFreePixmap(xdisp, p->pixmap);
  480. XFree(configs);
  481. p->pixmap = 0;
  482. p->glxpixmap = 0;
  483. return;
  484. }
  485. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  486. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  487. // glxBindTexImageEXT might modify the textures format.
  488. gs_color_format format = gs_format_from_tex();
  489. glBindTexture(GL_TEXTURE_2D, 0);
  490. // sync OBS texture format based on any glxBindTexImageEXT changes
  491. p->gltex->format = format;
  492. // Create a pure OBS texture to use for rendering. Using the same
  493. // format so we can copy instead of drawing from the source gltex.
  494. if (p->tex)
  495. gs_texture_destroy(p->tex);
  496. p->tex = gs_texture_create(width(), height(), format, 1, 0,
  497. GS_GL_DUMMYTEX);
  498. if (p->swapRedBlue) {
  499. GLuint tex = *(GLuint *)gs_texture_get_obj(p->tex);
  500. glBindTexture(GL_TEXTURE_2D, tex);
  501. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
  502. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
  503. glBindTexture(GL_TEXTURE_2D, 0);
  504. }
  505. if (!p->windowName.empty()) {
  506. blog(LOG_INFO,
  507. "[window-capture: '%s'] update settings:\n"
  508. "\ttitle: %s\n"
  509. "\tclass: %s\n"
  510. "\tBit depth: %i\n"
  511. "\tFound proper GLXFBConfig (in %i): %s\n",
  512. obs_source_get_name(p->source),
  513. XCompcap::getWindowName(p->win).c_str(),
  514. XCompcap::getWindowClass(p->win).c_str(), attr.depth,
  515. nelem, found ? "yes" : "no");
  516. }
  517. }
  518. void XCompcapMain::tick(float seconds)
  519. {
  520. if (!obs_source_showing(p->source))
  521. return;
  522. // Must be taken before xlock to prevent deadlock on shutdown
  523. ObsGsContextHolder obsctx;
  524. PLock lock(&p->lock, true);
  525. if (!lock.isLocked())
  526. return;
  527. XCompcap::processEvents();
  528. if (p->win && XCompcap::sourceWasReconfigured(this)) {
  529. p->window_check_time = FIND_WINDOW_INTERVAL;
  530. p->win = 0;
  531. }
  532. XErrorLock xlock;
  533. XWindowAttributes attr;
  534. if (!p->win || !XGetWindowAttributes(xdisp, p->win, &attr)) {
  535. p->window_check_time += (double)seconds;
  536. if (p->window_check_time < FIND_WINDOW_INTERVAL)
  537. return;
  538. Window newWin = getWindowFromString(p->windowName);
  539. p->window_check_time = 0.0;
  540. if (newWin && XGetWindowAttributes(xdisp, newWin, &attr)) {
  541. p->win = newWin;
  542. XCompcap::registerSource(this, p->win);
  543. updateSettings(0);
  544. } else {
  545. return;
  546. }
  547. }
  548. if (!p->tex || !p->gltex)
  549. return;
  550. if (p->lockX) {
  551. // XDisplayLock is still live so we should already be locked.
  552. XLockDisplay(xdisp);
  553. XSync(xdisp, 0);
  554. }
  555. glBindTexture(GL_TEXTURE_2D, *(GLuint *)gs_texture_get_obj(p->gltex));
  556. if (p->strict_binding) {
  557. glXReleaseTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_EXT);
  558. if (xlock.gotError() && !p->tick_error_suppressed) {
  559. blog(LOG_ERROR, "glXReleaseTexImageEXT failed: %s",
  560. xlock.getErrorText().c_str());
  561. p->tick_error_suppressed = true;
  562. }
  563. glXBindTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_EXT, nullptr);
  564. if (xlock.gotError() && !p->tick_error_suppressed) {
  565. blog(LOG_ERROR, "glXBindTexImageEXT failed: %s",
  566. xlock.getErrorText().c_str());
  567. p->tick_error_suppressed = true;
  568. }
  569. }
  570. if (p->include_border) {
  571. gs_copy_texture_region(p->tex, 0, 0, p->gltex, p->cur_cut_left,
  572. p->cur_cut_top, width(), height());
  573. } else {
  574. gs_copy_texture_region(p->tex, 0, 0, p->gltex,
  575. p->cur_cut_left + p->border,
  576. p->cur_cut_top + p->border, width(),
  577. height());
  578. }
  579. glBindTexture(GL_TEXTURE_2D, 0);
  580. if (p->cursor && p->show_cursor) {
  581. xcursor_tick(p->cursor);
  582. p->cursor_outside =
  583. p->cursor->x < p->cur_cut_left ||
  584. p->cursor->y < p->cur_cut_top ||
  585. p->cursor->x > int(p->width - p->cur_cut_right) ||
  586. p->cursor->y > int(p->height - p->cur_cut_bot);
  587. }
  588. if (p->lockX)
  589. XUnlockDisplay(xdisp);
  590. }
  591. void XCompcapMain::render(gs_effect_t *effect)
  592. {
  593. if (!p->win)
  594. return;
  595. PLock lock(&p->lock, true);
  596. if (p->draw_opaque)
  597. effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  598. else
  599. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  600. if (!lock.isLocked() || !p->tex)
  601. return;
  602. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  603. gs_effect_set_texture(image, p->tex);
  604. while (gs_effect_loop(effect, "Draw")) {
  605. gs_draw_sprite(p->tex, 0, 0, 0);
  606. }
  607. if (p->cursor && p->gltex && p->show_cursor && !p->cursor_outside) {
  608. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  609. while (gs_effect_loop(effect, "Draw")) {
  610. xcursor_render(p->cursor, -p->cur_cut_left,
  611. -p->cur_cut_top);
  612. }
  613. }
  614. }
  615. uint32_t XCompcapMain::width()
  616. {
  617. if (!p->win)
  618. return 0;
  619. return p->width - p->cur_cut_left - p->cur_cut_right;
  620. }
  621. uint32_t XCompcapMain::height()
  622. {
  623. if (!p->win)
  624. return 0;
  625. return p->height - p->cur_cut_bot - p->cur_cut_top;
  626. }