xcompcap-main.cpp 9.6 KB

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