xcompcap-main.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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.h"
  11. #include "xcompcap-helper.h"
  12. #define xdisp (XCompcap::disp())
  13. #define WIN_STRING_DIV "\r\n"
  14. bool XCompcapMain::init()
  15. {
  16. if (!xdisp) {
  17. blog(LOG_ERROR, "failed opening display");
  18. return false;
  19. }
  20. int eventBase, errorBase;
  21. if (!XCompositeQueryExtension(xdisp, &eventBase, &errorBase)) {
  22. blog(LOG_ERROR, "Xcomposite extension not supported");
  23. return false;
  24. }
  25. int major = 0, minor = 2;
  26. XCompositeQueryVersion(xdisp, &major, &minor);
  27. if (major == 0 && minor < 2) {
  28. blog(LOG_ERROR, "Xcomposite extension is too old: %d.%d < 0.2",
  29. major, minor);
  30. return false;
  31. }
  32. return true;
  33. }
  34. void XCompcapMain::deinit()
  35. {
  36. XCompcap::cleanupDisplay();
  37. }
  38. obs_properties_t XCompcapMain::properties()
  39. {
  40. obs_properties_t props = obs_properties_create();
  41. obs_property_t wins = obs_properties_add_list(props, "capture_window",
  42. "Captured Window", OBS_COMBO_TYPE_LIST,
  43. OBS_COMBO_FORMAT_STRING);
  44. for (Window win: XCompcap::getTopLevelWindows()) {
  45. std::string wname = XCompcap::getWindowName(win);
  46. std::string progpath = XCompcap::getWindowCommand(win);
  47. std::string winid = std::to_string((long long)win);
  48. std::string desc =
  49. (winid + WIN_STRING_DIV + wname +
  50. WIN_STRING_DIV + progpath);
  51. obs_property_list_add_string(wins, wname.c_str(),
  52. desc.c_str());
  53. }
  54. obs_properties_add_int(props, "cut_top", "Cut top pixels",
  55. 0, 4096, 1);
  56. obs_properties_add_int(props, "cut_left", "Cut left pixels",
  57. 0, 4096, 1);
  58. obs_properties_add_int(props, "cut_right", "Cut right pixels",
  59. 0, 4096, 1);
  60. obs_properties_add_int(props, "cut_bot", "Cut bottom pixels",
  61. 0, 4096, 1);
  62. obs_properties_add_bool(props, "swap_redblue", "Swap red and blue");
  63. obs_properties_add_bool(props, "lock_x", "Lock X server when "
  64. "capturing");
  65. return props;
  66. }
  67. void XCompcapMain::defaults(obs_data_t settings)
  68. {
  69. obs_data_set_default_string(settings, "capture_window", "");
  70. obs_data_set_default_int(settings, "cut_top", 0);
  71. obs_data_set_default_int(settings, "cut_left", 0);
  72. obs_data_set_default_int(settings, "cut_right", 0);
  73. obs_data_set_default_int(settings, "cut_bot", 0);
  74. obs_data_set_default_bool(settings, "swap_redblue", false);
  75. obs_data_set_default_bool(settings, "lock_x", false);
  76. }
  77. struct XCompcapMain_private
  78. {
  79. XCompcapMain_private()
  80. :win(0)
  81. ,cut_top(0), cur_cut_top(0)
  82. ,cut_left(0), cur_cut_left(0)
  83. ,cut_right(0), cur_cut_right(0)
  84. ,cut_bot(0), cur_cut_bot(0)
  85. ,inverted(false)
  86. ,width(0),height(0)
  87. ,pixmap(0)
  88. ,glxpixmap(0)
  89. ,tex(0)
  90. ,gltex(0)
  91. {
  92. pthread_mutexattr_init(&lockattr);
  93. pthread_mutexattr_settype(&lockattr, PTHREAD_MUTEX_RECURSIVE);
  94. pthread_mutex_init(&lock, &lockattr);
  95. }
  96. ~XCompcapMain_private()
  97. {
  98. pthread_mutex_destroy(&lock);
  99. pthread_mutexattr_destroy(&lockattr);
  100. }
  101. obs_source_t source;
  102. Window win;
  103. int cut_top, cur_cut_top;
  104. int cut_left, cur_cut_left;
  105. int cut_right, cur_cut_right;
  106. int cut_bot, cur_cut_bot;
  107. bool inverted;
  108. bool swapRedBlue;
  109. bool lockX;
  110. uint32_t width;
  111. uint32_t height;
  112. Pixmap pixmap;
  113. GLXPixmap glxpixmap;
  114. texture_t tex;
  115. texture_t gltex;
  116. pthread_mutex_t lock;
  117. pthread_mutexattr_t lockattr;
  118. };
  119. XCompcapMain::XCompcapMain(obs_data_t settings, obs_source_t source)
  120. {
  121. p = new XCompcapMain_private;
  122. p->source = source;
  123. updateSettings(settings);
  124. }
  125. static void xcc_cleanup(XCompcapMain_private *p);
  126. XCompcapMain::~XCompcapMain()
  127. {
  128. ObsGsContextHolder obsctx;
  129. if (p->tex) {
  130. texture_destroy(p->tex);
  131. p->tex = 0;
  132. }
  133. xcc_cleanup(p);
  134. delete p;
  135. }
  136. static Window getWindowFromString(std::string wstr)
  137. {
  138. if (wstr == "") {
  139. return XCompcap::getTopLevelWindows().front();
  140. }
  141. if (wstr.substr(0, 4) == "root") {
  142. int i = std::stoi("0" + wstr.substr(4));
  143. return RootWindow(xdisp, i);
  144. }
  145. size_t firstMark = wstr.find(WIN_STRING_DIV);
  146. if (firstMark == std::string::npos)
  147. return (Window)std::stol(wstr);
  148. std::string widstr = wstr.substr(0, firstMark);
  149. Window wid = (Window)std::stol(widstr);
  150. wstr = wstr.substr(firstMark + strlen(WIN_STRING_DIV));
  151. size_t lastMark = wstr.rfind(WIN_STRING_DIV);
  152. std::string wname = wstr.substr(0, lastMark);
  153. Window matchedNameWin = wid;
  154. for (Window cwin: XCompcap::getTopLevelWindows()) {
  155. std::string cwinname = XCompcap::getWindowName(cwin);
  156. if (cwin == wid && wname == cwinname)
  157. return wid;
  158. if (wname == cwinname)
  159. matchedNameWin = cwin;
  160. }
  161. return matchedNameWin;
  162. }
  163. static void xcc_cleanup(XCompcapMain_private *p)
  164. {
  165. if (p->gltex) {
  166. texture_destroy(p->gltex);
  167. p->gltex = 0;
  168. }
  169. if (p->glxpixmap) {
  170. glXDestroyPixmap(xdisp, p->glxpixmap);
  171. p->glxpixmap = 0;
  172. }
  173. if (p->pixmap) {
  174. XFreePixmap(xdisp, p->pixmap);
  175. p->pixmap = 0;
  176. }
  177. if (p->win) {
  178. XCompositeUnredirectWindow(xdisp, p->win,
  179. CompositeRedirectAutomatic);
  180. XSelectInput(xdisp, p->win, 0);
  181. p->win = 0;
  182. }
  183. }
  184. void XCompcapMain::updateSettings(obs_data_t settings)
  185. {
  186. PLock lock(&p->lock);
  187. XErrorLock xlock;
  188. ObsGsContextHolder obsctx;
  189. blog(LOG_DEBUG, "Settings updating");
  190. Window prevWin = p->win;
  191. xcc_cleanup(p);
  192. if (settings) {
  193. const char *windowName = obs_data_getstring(settings,
  194. "capture_window");
  195. p->win = getWindowFromString(windowName);
  196. p->cut_top = obs_data_getint(settings, "cut_top");
  197. p->cut_left = obs_data_getint(settings, "cut_left");
  198. p->cut_right = obs_data_getint(settings, "cut_right");
  199. p->cut_bot = obs_data_getint(settings, "cut_bot");
  200. p->lockX = obs_data_getbool(settings, "lock_x");
  201. p->swapRedBlue = obs_data_getbool(settings, "swap_redblue");
  202. } else {
  203. p->win = prevWin;
  204. }
  205. xlock.resetError();
  206. XCompositeRedirectWindow(xdisp, p->win, CompositeRedirectAutomatic);
  207. if (xlock.gotError()) {
  208. blog(LOG_ERROR, "XCompositeRedirectWindow failed: %s",
  209. xlock.getErrorText().c_str());
  210. return;
  211. }
  212. XSelectInput(xdisp, p->win, StructureNotifyMask);
  213. XSync(xdisp, 0);
  214. XWindowAttributes attr;
  215. if (!XGetWindowAttributes(xdisp, p->win, &attr)) {
  216. p->win = 0;
  217. p->width = 0;
  218. p->height = 0;
  219. return;
  220. }
  221. gs_color_format cf = GS_RGBA;
  222. p->width = attr.width;
  223. p->height = attr.height;
  224. if (p->cut_top + p->cut_bot < (int)p->height) {
  225. p->cur_cut_top = p->cut_top;
  226. p->cur_cut_bot = p->cut_bot;
  227. } else {
  228. p->cur_cut_top = 0;
  229. p->cur_cut_bot = 0;
  230. }
  231. if (p->cut_left + p->cut_right < (int)p->width) {
  232. p->cur_cut_left = p->cut_left;
  233. p->cur_cut_right = p->cut_right;
  234. } else {
  235. p->cur_cut_left = 0;
  236. p->cur_cut_right = 0;
  237. }
  238. if (p->tex)
  239. texture_destroy(p->tex);
  240. uint8_t *texData = new uint8_t[width() * height() * 4];
  241. for (unsigned int i = 0; i < width() * height() * 4; i += 4) {
  242. texData[i + 0] = p->swapRedBlue ? 0xFF : 0;
  243. texData[i + 1] = 0;
  244. texData[i + 2] = p->swapRedBlue ? 0 : 0xFF;
  245. texData[i + 3] = 0xFF;
  246. }
  247. const uint8_t* texDataArr[] = { texData, 0 };
  248. p->tex = gs_create_texture(width(), height(), cf, 1,
  249. (const void**)texDataArr, 0);
  250. delete[] texData;
  251. if (!p->swapRedBlue) {
  252. GLuint tex = *(GLuint*)texture_getobj(p->tex);
  253. glBindTexture(GL_TEXTURE_2D, tex);
  254. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
  255. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
  256. glBindTexture(GL_TEXTURE_2D, 0);
  257. }
  258. const int attrs[] =
  259. {
  260. GLX_BIND_TO_TEXTURE_RGBA_EXT, GL_TRUE,
  261. GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
  262. GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
  263. GLX_DOUBLEBUFFER, GL_FALSE,
  264. None
  265. };
  266. int nelem = 0;
  267. GLXFBConfig* configs = glXChooseFBConfig(xdisp,
  268. XCompcap::getRootWindowScreen(attr.root),
  269. attrs, &nelem);
  270. if (nelem <= 0) {
  271. blog(LOG_ERROR, "no matching fb config found");
  272. p->win = 0;
  273. p->height = 0;
  274. p->width = 0;
  275. return;
  276. }
  277. glXGetFBConfigAttrib(xdisp, configs[0], GLX_Y_INVERTED_EXT, &nelem);
  278. p->inverted = nelem != 0;
  279. xlock.resetError();
  280. p->pixmap = XCompositeNameWindowPixmap(xdisp, p->win);
  281. if (xlock.gotError()) {
  282. blog(LOG_ERROR, "XCompositeNameWindowPixmap failed: %s",
  283. xlock.getErrorText().c_str());
  284. p->pixmap = 0;
  285. XFree(configs);
  286. return;
  287. }
  288. const int attribs[] =
  289. {
  290. GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
  291. GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT,
  292. None
  293. };
  294. p->glxpixmap = glXCreatePixmap(xdisp, configs[0], p->pixmap, attribs);
  295. if (xlock.gotError()) {
  296. blog(LOG_ERROR, "glXCreatePixmap failed: %s",
  297. xlock.getErrorText().c_str());
  298. XFreePixmap(xdisp, p->pixmap);
  299. XFree(configs);
  300. p->pixmap = 0;
  301. p->glxpixmap = 0;
  302. return;
  303. }
  304. XFree(configs);
  305. p->gltex = gs_create_texture(p->width, p->height, cf, 1, 0,
  306. GS_GL_DUMMYTEX);
  307. GLuint gltex = *(GLuint*)texture_getobj(p->gltex);
  308. glBindTexture(GL_TEXTURE_2D, gltex);
  309. glXBindTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_LEFT_EXT, NULL);
  310. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  311. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  312. }
  313. void XCompcapMain::tick(float seconds)
  314. {
  315. UNUSED_PARAMETER(seconds);
  316. PLock lock(&p->lock, true);
  317. if (!lock.isLocked())
  318. return;
  319. XCompcap::processEvents();
  320. if (XCompcap::windowWasReconfigured(p->win))
  321. updateSettings(0);
  322. if (!p->tex || !p->gltex)
  323. return;
  324. gs_entercontext(obs_graphics());
  325. if (p->lockX) {
  326. XLockDisplay(xdisp);
  327. XSync(xdisp, 0);
  328. }
  329. gs_copy_texture_region(p->tex, 0, 0, p->gltex, p->cur_cut_left,
  330. p->cur_cut_top, width(), height());
  331. if (p->lockX)
  332. XUnlockDisplay(xdisp);
  333. gs_leavecontext();
  334. }
  335. void XCompcapMain::render(effect_t effect)
  336. {
  337. PLock lock(&p->lock, true);
  338. if (!lock.isLocked() || !p->tex)
  339. return;
  340. eparam_t image = effect_getparambyname(effect, "image");
  341. effect_settexture(effect, image, p->tex);
  342. gs_enable_blending(false);
  343. gs_draw_sprite(p->tex, 0, 0, 0);
  344. }
  345. uint32_t XCompcapMain::width()
  346. {
  347. return p->width - p->cur_cut_left - p->cur_cut_right;
  348. }
  349. uint32_t XCompcapMain::height()
  350. {
  351. return p->height - p->cur_cut_bot - p->cur_cut_top;
  352. }