xcompcap-main.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. };
  168. XCompcapMain::XCompcapMain(obs_data_t *settings, obs_source_t *source)
  169. {
  170. p = new XCompcapMain_private;
  171. p->source = source;
  172. obs_enter_graphics();
  173. p->cursor = xcursor_init(xdisp);
  174. obs_leave_graphics();
  175. updateSettings(settings);
  176. }
  177. static void xcc_cleanup(XCompcapMain_private *p);
  178. XCompcapMain::~XCompcapMain()
  179. {
  180. ObsGsContextHolder obsctx;
  181. XCompcap::unregisterSource(this);
  182. if (p->tex) {
  183. gs_texture_destroy(p->tex);
  184. p->tex = 0;
  185. }
  186. xcc_cleanup(p);
  187. if (p->cursor) {
  188. xcursor_destroy(p->cursor);
  189. p->cursor = nullptr;
  190. }
  191. delete p;
  192. }
  193. static Window getWindowFromString(std::string wstr)
  194. {
  195. XErrorLock xlock;
  196. if (wstr == "") {
  197. return XCompcap::getTopLevelWindows().front();
  198. }
  199. if (wstr.substr(0, 4) == "root") {
  200. int i = std::stoi("0" + wstr.substr(4));
  201. return RootWindow(xdisp, i);
  202. }
  203. size_t firstMark = wstr.find(WIN_STRING_DIV);
  204. size_t lastMark = wstr.rfind(WIN_STRING_DIV);
  205. size_t markSize = strlen(WIN_STRING_DIV);
  206. // wstr only consists of the window-id
  207. if (firstMark == std::string::npos)
  208. return (Window)std::stol(wstr);
  209. // wstr also contains window-name and window-class
  210. std::string wid = wstr.substr(0, firstMark);
  211. std::string wname = wstr.substr(firstMark + markSize,
  212. lastMark - firstMark - markSize);
  213. std::string wcls = wstr.substr(lastMark + markSize);
  214. Window winById = (Window)std::stol(wid);
  215. // first try to find a match by the window-id
  216. for (Window cwin : XCompcap::getTopLevelWindows()) {
  217. // match by window-id
  218. if (cwin == winById) {
  219. return cwin;
  220. }
  221. }
  222. // then try to find a match by name & class
  223. for (Window cwin : XCompcap::getTopLevelWindows()) {
  224. std::string cwinname = XCompcap::getWindowName(cwin);
  225. std::string ccls = XCompcap::getWindowClass(cwin);
  226. // match by name and class
  227. if (wname == cwinname && wcls == ccls) {
  228. return cwin;
  229. }
  230. }
  231. // no match
  232. blog(LOG_DEBUG, "Did not find Window By ID %s, Name '%s' or Class '%s'",
  233. wid.c_str(), wname.c_str(), wcls.c_str());
  234. return 0;
  235. }
  236. static void xcc_cleanup(XCompcapMain_private *p)
  237. {
  238. PLock lock(&p->lock);
  239. XErrorLock xlock;
  240. if (p->gltex) {
  241. GLuint gltex = *(GLuint *)gs_texture_get_obj(p->gltex);
  242. glBindTexture(GL_TEXTURE_2D, gltex);
  243. if (p->glxpixmap) {
  244. glXDestroyPixmap(xdisp, p->glxpixmap);
  245. if (xlock.gotError()) {
  246. blog(LOG_ERROR,
  247. "cleanup glXDestroyPixmap failed: %s",
  248. xlock.getErrorText().c_str());
  249. xlock.resetError();
  250. }
  251. p->glxpixmap = 0;
  252. }
  253. gs_texture_destroy(p->gltex);
  254. p->gltex = 0;
  255. }
  256. if (p->pixmap) {
  257. XFreePixmap(xdisp, p->pixmap);
  258. if (xlock.gotError()) {
  259. blog(LOG_ERROR, "cleanup glXDestroyPixmap failed: %s",
  260. xlock.getErrorText().c_str());
  261. xlock.resetError();
  262. }
  263. p->pixmap = 0;
  264. }
  265. if (p->win) {
  266. p->win = 0;
  267. }
  268. if (p->tex) {
  269. gs_texture_destroy(p->tex);
  270. p->tex = 0;
  271. }
  272. }
  273. static gs_color_format gs_format_from_tex()
  274. {
  275. GLint iformat = 0;
  276. // consider GL_ARB_internalformat_query
  277. glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT,
  278. &iformat);
  279. // These formats are known to be wrong on Intel platforms. We intentionally
  280. // use swapped internal formats here to preserve historic behavior which
  281. // swapped colors accidentally and because D3D11 would not support a
  282. // GS_RGBX format
  283. switch (iformat) {
  284. case GL_RGB:
  285. return GS_BGRX_UNORM;
  286. case GL_RGBA:
  287. return GS_RGBA_UNORM;
  288. default:
  289. return GS_RGBA_UNORM;
  290. }
  291. }
  292. // from libobs-opengl/gl-subsystem.h because we need to handle GLX modifying textures outside libobs.
  293. struct fb_info;
  294. struct gs_texture {
  295. gs_device_t *device;
  296. enum gs_texture_type type;
  297. enum gs_color_format format;
  298. GLenum gl_format;
  299. GLenum gl_target;
  300. GLenum gl_internal_format;
  301. GLenum gl_type;
  302. GLuint texture;
  303. uint32_t levels;
  304. bool is_dynamic;
  305. bool is_render_target;
  306. bool is_dummy;
  307. bool gen_mipmaps;
  308. gs_samplerstate_t *cur_sampler;
  309. struct fbo_info *fbo;
  310. };
  311. // End shitty hack.
  312. void XCompcapMain::updateSettings(obs_data_t *settings)
  313. {
  314. ObsGsContextHolder obsctx;
  315. XErrorLock xlock;
  316. PLock lock(&p->lock);
  317. blog(LOG_DEBUG, "Settings updating");
  318. Window prevWin = p->win;
  319. xcc_cleanup(p);
  320. p->tick_error_suppressed = false;
  321. if (settings) {
  322. /* Settings initialized or changed */
  323. const char *windowName =
  324. obs_data_get_string(settings, "capture_window");
  325. p->windowName = windowName;
  326. p->win = getWindowFromString(windowName);
  327. XCompcap::registerSource(this, p->win);
  328. p->cut_top = obs_data_get_int(settings, "cut_top");
  329. p->cut_left = obs_data_get_int(settings, "cut_left");
  330. p->cut_right = obs_data_get_int(settings, "cut_right");
  331. p->cut_bot = obs_data_get_int(settings, "cut_bot");
  332. p->lockX = obs_data_get_bool(settings, "lock_x");
  333. p->swapRedBlue = obs_data_get_bool(settings, "swap_redblue");
  334. p->show_cursor = obs_data_get_bool(settings, "show_cursor");
  335. p->include_border =
  336. obs_data_get_bool(settings, "include_border");
  337. p->exclude_alpha = obs_data_get_bool(settings, "exclude_alpha");
  338. p->draw_opaque = false;
  339. } else {
  340. /* New Window found (stored in p->win), just re-initialize GL-Mapping */
  341. p->win = prevWin;
  342. }
  343. if (xlock.gotError()) {
  344. blog(LOG_ERROR, "registeringSource failed: %s",
  345. xlock.getErrorText().c_str());
  346. return;
  347. }
  348. XSync(xdisp, 0);
  349. XWindowAttributes attr;
  350. if (!p->win || !XGetWindowAttributes(xdisp, p->win, &attr)) {
  351. p->win = 0;
  352. p->width = 0;
  353. p->height = 0;
  354. return;
  355. }
  356. if (p->win && p->cursor && p->show_cursor) {
  357. Window child;
  358. int x, y;
  359. XTranslateCoordinates(xdisp, p->win, attr.root, 0, 0, &x, &y,
  360. &child);
  361. xcursor_offset(p->cursor, x, y);
  362. }
  363. const int config_attrs[] = {GLX_BIND_TO_TEXTURE_RGBA_EXT,
  364. GL_TRUE,
  365. GLX_DRAWABLE_TYPE,
  366. GLX_PIXMAP_BIT,
  367. GLX_BIND_TO_TEXTURE_TARGETS_EXT,
  368. GLX_TEXTURE_2D_BIT_EXT,
  369. GLX_DOUBLEBUFFER,
  370. GL_FALSE,
  371. None};
  372. int nelem = 0;
  373. GLXFBConfig *configs = glXChooseFBConfig(
  374. xdisp, XCompcap::getRootWindowScreen(attr.root), config_attrs,
  375. &nelem);
  376. bool found = false;
  377. GLXFBConfig config;
  378. for (int i = 0; i < nelem; i++) {
  379. config = configs[i];
  380. XVisualInfo *visual = glXGetVisualFromFBConfig(xdisp, config);
  381. if (!visual)
  382. continue;
  383. if (attr.depth != visual->depth) {
  384. XFree(visual);
  385. continue;
  386. }
  387. XFree(visual);
  388. found = true;
  389. break;
  390. }
  391. if (!found) {
  392. blog(LOG_ERROR, "no matching fb config found");
  393. p->win = 0;
  394. p->height = 0;
  395. p->width = 0;
  396. XFree(configs);
  397. return;
  398. }
  399. if (p->exclude_alpha || attr.depth != 32) {
  400. p->draw_opaque = true;
  401. }
  402. int inverted;
  403. glXGetFBConfigAttrib(xdisp, config, GLX_Y_INVERTED_EXT, &inverted);
  404. p->inverted = inverted != 0;
  405. p->border = attr.border_width;
  406. if (p->include_border) {
  407. p->width = attr.width + p->border * 2;
  408. p->height = attr.height + p->border * 2;
  409. } else {
  410. p->width = attr.width;
  411. p->height = attr.height;
  412. }
  413. if (p->cut_top + p->cut_bot < (int)p->height) {
  414. p->cur_cut_top = p->cut_top;
  415. p->cur_cut_bot = p->cut_bot;
  416. } else {
  417. p->cur_cut_top = 0;
  418. p->cur_cut_bot = 0;
  419. }
  420. if (p->cut_left + p->cut_right < (int)p->width) {
  421. p->cur_cut_left = p->cut_left;
  422. p->cur_cut_right = p->cut_right;
  423. } else {
  424. p->cur_cut_left = 0;
  425. p->cur_cut_right = 0;
  426. }
  427. // Precautionary since we dont error check every GLX call above.
  428. xlock.resetError();
  429. p->pixmap = XCompositeNameWindowPixmap(xdisp, p->win);
  430. if (xlock.gotError()) {
  431. blog(LOG_ERROR, "XCompositeNameWindowPixmap failed: %s",
  432. xlock.getErrorText().c_str());
  433. p->pixmap = 0;
  434. XFree(configs);
  435. return;
  436. }
  437. // Should be consistent format with config we are using. Since we searched on RGBA lets use RGBA here.
  438. const int pixmap_attrs[] = {GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
  439. GLX_TEXTURE_FORMAT_EXT,
  440. GLX_TEXTURE_FORMAT_RGBA_EXT, None};
  441. p->glxpixmap = glXCreatePixmap(xdisp, config, p->pixmap, pixmap_attrs);
  442. if (xlock.gotError()) {
  443. blog(LOG_ERROR, "glXCreatePixmap failed: %s",
  444. xlock.getErrorText().c_str());
  445. XFreePixmap(xdisp, p->pixmap);
  446. XFree(configs);
  447. p->pixmap = 0;
  448. p->glxpixmap = 0;
  449. return;
  450. }
  451. XFree(configs);
  452. // Build an OBS texture to bind the pixmap to.
  453. p->gltex = gs_texture_create(p->width, p->height, GS_RGBA_UNORM, 1, 0,
  454. GS_GL_DUMMYTEX);
  455. GLuint gltex = *(GLuint *)gs_texture_get_obj(p->gltex);
  456. glBindTexture(GL_TEXTURE_2D, gltex);
  457. glXBindTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_EXT, nullptr);
  458. if (xlock.gotError()) {
  459. blog(LOG_ERROR, "glXBindTexImageEXT failed: %s",
  460. xlock.getErrorText().c_str());
  461. XFreePixmap(xdisp, p->pixmap);
  462. XFree(configs);
  463. p->pixmap = 0;
  464. p->glxpixmap = 0;
  465. return;
  466. }
  467. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  468. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  469. // glxBindTexImageEXT might modify the textures format.
  470. gs_color_format format = gs_format_from_tex();
  471. glXReleaseTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_EXT);
  472. if (xlock.gotError()) {
  473. blog(LOG_ERROR, "glXReleaseTexImageEXT failed: %s",
  474. xlock.getErrorText().c_str());
  475. xlock.resetError();
  476. }
  477. glBindTexture(GL_TEXTURE_2D, 0);
  478. // sync OBS texture format based on any glxBindTexImageEXT changes
  479. p->gltex->format = format;
  480. // Create a pure OBS texture to use for rendering. Using the same
  481. // format so we can copy instead of drawing from the source gltex.
  482. if (p->tex)
  483. gs_texture_destroy(p->tex);
  484. p->tex = gs_texture_create(width(), height(), format, 1, 0,
  485. GS_GL_DUMMYTEX);
  486. if (p->swapRedBlue) {
  487. GLuint tex = *(GLuint *)gs_texture_get_obj(p->tex);
  488. glBindTexture(GL_TEXTURE_2D, tex);
  489. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
  490. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
  491. glBindTexture(GL_TEXTURE_2D, 0);
  492. }
  493. if (!p->windowName.empty()) {
  494. blog(LOG_INFO,
  495. "[window-capture: '%s'] update settings:\n"
  496. "\ttitle: %s\n"
  497. "\tclass: %s\n"
  498. "\tBit depth: %i\n"
  499. "\tFound proper GLXFBConfig (in %i): %s\n",
  500. obs_source_get_name(p->source),
  501. XCompcap::getWindowName(p->win).c_str(),
  502. XCompcap::getWindowClass(p->win).c_str(), attr.depth,
  503. nelem, found ? "yes" : "no");
  504. }
  505. }
  506. void XCompcapMain::tick(float seconds)
  507. {
  508. if (!obs_source_showing(p->source))
  509. return;
  510. // Must be taken before xlock to prevent deadlock on shutdown
  511. ObsGsContextHolder obsctx;
  512. PLock lock(&p->lock, true);
  513. if (!lock.isLocked())
  514. return;
  515. XCompcap::processEvents();
  516. if (p->win && XCompcap::sourceWasReconfigured(this)) {
  517. p->window_check_time = FIND_WINDOW_INTERVAL;
  518. p->win = 0;
  519. }
  520. XErrorLock xlock;
  521. XWindowAttributes attr;
  522. if (!p->win || !XGetWindowAttributes(xdisp, p->win, &attr)) {
  523. p->window_check_time += (double)seconds;
  524. if (p->window_check_time < FIND_WINDOW_INTERVAL)
  525. return;
  526. Window newWin = getWindowFromString(p->windowName);
  527. p->window_check_time = 0.0;
  528. if (newWin && XGetWindowAttributes(xdisp, newWin, &attr)) {
  529. p->win = newWin;
  530. XCompcap::registerSource(this, p->win);
  531. updateSettings(0);
  532. } else {
  533. return;
  534. }
  535. }
  536. if (!p->tex || !p->gltex)
  537. return;
  538. if (p->lockX) {
  539. // XDisplayLock is still live so we should already be locked.
  540. XLockDisplay(xdisp);
  541. XSync(xdisp, 0);
  542. }
  543. glBindTexture(GL_TEXTURE_2D, *(GLuint *)gs_texture_get_obj(p->gltex));
  544. glXBindTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_EXT, nullptr);
  545. if (xlock.gotError() && !p->tick_error_suppressed) {
  546. blog(LOG_ERROR, "glXBindTexImageEXT failed: %s",
  547. xlock.getErrorText().c_str());
  548. p->tick_error_suppressed = true;
  549. }
  550. if (p->include_border) {
  551. gs_copy_texture_region(p->tex, 0, 0, p->gltex, p->cur_cut_left,
  552. p->cur_cut_top, width(), height());
  553. } else {
  554. gs_copy_texture_region(p->tex, 0, 0, p->gltex,
  555. p->cur_cut_left + p->border,
  556. p->cur_cut_top + p->border, width(),
  557. height());
  558. }
  559. glXReleaseTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_EXT);
  560. if (xlock.gotError() && !p->tick_error_suppressed) {
  561. blog(LOG_ERROR, "glXReleaseTexImageEXT failed: %s",
  562. xlock.getErrorText().c_str());
  563. p->tick_error_suppressed = true;
  564. }
  565. glBindTexture(GL_TEXTURE_2D, 0);
  566. if (p->cursor && p->show_cursor) {
  567. xcursor_tick(p->cursor);
  568. p->cursor_outside =
  569. p->cursor->x < p->cur_cut_left ||
  570. p->cursor->y < p->cur_cut_top ||
  571. p->cursor->x > int(p->width - p->cur_cut_right) ||
  572. p->cursor->y > int(p->height - p->cur_cut_bot);
  573. }
  574. if (p->lockX)
  575. XUnlockDisplay(xdisp);
  576. }
  577. void XCompcapMain::render(gs_effect_t *effect)
  578. {
  579. if (!p->win)
  580. return;
  581. PLock lock(&p->lock, true);
  582. if (p->draw_opaque)
  583. effect = obs_get_base_effect(OBS_EFFECT_OPAQUE);
  584. else
  585. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  586. if (!lock.isLocked() || !p->tex)
  587. return;
  588. gs_eparam_t *image = gs_effect_get_param_by_name(effect, "image");
  589. gs_effect_set_texture(image, p->tex);
  590. while (gs_effect_loop(effect, "Draw")) {
  591. gs_draw_sprite(p->tex, 0, 0, 0);
  592. }
  593. if (p->cursor && p->gltex && p->show_cursor && !p->cursor_outside) {
  594. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  595. while (gs_effect_loop(effect, "Draw")) {
  596. xcursor_render(p->cursor, -p->cur_cut_left,
  597. -p->cur_cut_top);
  598. }
  599. }
  600. }
  601. uint32_t XCompcapMain::width()
  602. {
  603. if (!p->win)
  604. return 0;
  605. return p->width - p->cur_cut_left - p->cur_cut_right;
  606. }
  607. uint32_t XCompcapMain::height()
  608. {
  609. if (!p->win)
  610. return 0;
  611. return p->height - p->cur_cut_bot - p->cur_cut_top;
  612. }