xcompcap-main.cpp 17 KB

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