Ejemplo n.º 1
0
void XCompcapMain::updateSettings(obs_data_t *settings)
{
	PLock lock(&p->lock);
	XErrorLock xlock;
	ObsGsContextHolder obsctx;

	blog(LOG_DEBUG, "Settings updating");

	Window prevWin = p->win;

	xcc_cleanup(p);

	if (settings) {
		const char *windowName = obs_data_get_string(settings,
				"capture_window");

		p->windowName = windowName;
		p->win = getWindowFromString(windowName);

		p->cut_top = obs_data_get_int(settings, "cut_top");
		p->cut_left = obs_data_get_int(settings, "cut_left");
		p->cut_right = obs_data_get_int(settings, "cut_right");
		p->cut_bot = obs_data_get_int(settings, "cut_bot");
		p->lockX = obs_data_get_bool(settings, "lock_x");
		p->swapRedBlue = obs_data_get_bool(settings, "swap_redblue");
		p->show_cursor = obs_data_get_bool(settings, "show_cursor");
		p->include_border = obs_data_get_bool(settings, "include_border");
		p->exclude_alpha = obs_data_get_bool(settings, "exclude_alpha");
	} else {
		p->win = prevWin;
	}

	xlock.resetError();

	if (p->win)
		XCompositeRedirectWindow(xdisp, p->win,
				CompositeRedirectAutomatic);

	if (xlock.gotError()) {
		blog(LOG_ERROR, "XCompositeRedirectWindow failed: %s",
				xlock.getErrorText().c_str());
		return;
	}

	if (p->win)
		XSelectInput(xdisp, p->win, StructureNotifyMask | ExposureMask);
	XSync(xdisp, 0);

	XWindowAttributes attr;
	if (!p->win || !XGetWindowAttributes(xdisp, p->win, &attr)) {
		p->win = 0;
		p->width = 0;
		p->height = 0;
		return;
	}

	if (p->win && p->cursor && p->show_cursor) {
		Window child;
		int x, y;

		XTranslateCoordinates(xdisp, p->win, attr.root, 0, 0, &x, &y,
				&child);
		xcursor_offset(p->cursor, x, y);
	}

	gs_color_format cf = GS_RGBA;

	if (p->exclude_alpha) {
		cf = GS_BGRX;
	}

	p->border = attr.border_width;

	if (p->include_border) {
		p->width = attr.width + p->border * 2;
		p->height = attr.height + p->border * 2;
	} else {
		p->width = attr.width;
		p->height = attr.height;
	}

	if (p->cut_top + p->cut_bot < (int)p->height) {
		p->cur_cut_top = p->cut_top;
		p->cur_cut_bot = p->cut_bot;
	} else {
		p->cur_cut_top = 0;
		p->cur_cut_bot = 0;
	}

	if (p->cut_left + p->cut_right < (int)p->width) {
		p->cur_cut_left = p->cut_left;
		p->cur_cut_right = p->cut_right;
	} else {
		p->cur_cut_left = 0;
		p->cur_cut_right = 0;
	}

	if (p->tex)
		gs_texture_destroy(p->tex);

	uint8_t *texData = new uint8_t[width() * height() * 4];

	memset(texData, 0, width() * height() * 4);

	const uint8_t* texDataArr[] = { texData, 0 };

	p->tex = gs_texture_create(width(), height(), cf, 1,
			texDataArr, 0);

	delete[] texData;

	if (p->swapRedBlue) {
		GLuint tex = *(GLuint*)gs_texture_get_obj(p->tex);
		glBindTexture(GL_TEXTURE_2D, tex);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);
		glBindTexture(GL_TEXTURE_2D, 0);
	}

	const int attrs[] =
	{
		GLX_BIND_TO_TEXTURE_RGBA_EXT, GL_TRUE,
		GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
		GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
		GLX_DOUBLEBUFFER, GL_FALSE,
		None
	};

	int nelem = 0;
	GLXFBConfig* configs = glXChooseFBConfig(xdisp,
			XCompcap::getRootWindowScreen(attr.root),
			attrs, &nelem);

	if (nelem <= 0) {
		blog(LOG_ERROR, "no matching fb config found");
		p->win = 0;
		p->height = 0;
		p->width = 0;
		return;
	}

	glXGetFBConfigAttrib(xdisp, configs[0], GLX_Y_INVERTED_EXT, &nelem);
	p->inverted = nelem != 0;

	xlock.resetError();

	p->pixmap = XCompositeNameWindowPixmap(xdisp, p->win);

	if (xlock.gotError()) {
		blog(LOG_ERROR, "XCompositeNameWindowPixmap failed: %s",
				xlock.getErrorText().c_str());
		p->pixmap = 0;
		XFree(configs);
		return;
	}

	const int attribs[] =
	{
		GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
		GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT,
		None
	};

	p->glxpixmap = glXCreatePixmap(xdisp, configs[0], p->pixmap, attribs);

	if (xlock.gotError()) {
		blog(LOG_ERROR, "glXCreatePixmap failed: %s",
				xlock.getErrorText().c_str());
		XFreePixmap(xdisp, p->pixmap);
		XFree(configs);
		p->pixmap = 0;
		p->glxpixmap = 0;
		return;
	}

	XFree(configs);

	p->gltex = gs_texture_create(p->width, p->height, cf, 1, 0,
			GS_GL_DUMMYTEX);

	GLuint gltex = *(GLuint*)gs_texture_get_obj(p->gltex);
	glBindTexture(GL_TEXTURE_2D, gltex);
	glXBindTexImageEXT(xdisp, p->glxpixmap, GLX_FRONT_LEFT_EXT, NULL);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
Ejemplo n.º 2
0
void XCompcapMain::tick(float seconds)
{
	if (!obs_source_showing(p->source))
		return;

	PLock lock(&p->lock, true);

	if (!lock.isLocked())
		return;

	XCompcap::processEvents();

	if (p->win && XCompcap::windowWasReconfigured(p->win)) {
		p->window_check_time = FIND_WINDOW_INTERVAL;
		p->win = 0;
	}

	XDisplayLock xlock;
	XWindowAttributes attr;

	if (!p->win || !XGetWindowAttributes(xdisp, p->win, &attr)) {
		p->window_check_time += (double)seconds;

		if (p->window_check_time < FIND_WINDOW_INTERVAL)
			return;

		Window newWin = getWindowFromString(p->windowName);

		p->window_check_time = 0.0;

		if (newWin && XGetWindowAttributes(xdisp, newWin, &attr)) {
			p->win = newWin;
			updateSettings(0);
		} else {
			return;
		}
	}

	if (!p->tex || !p->gltex)
		return;

	obs_enter_graphics();

	if (p->lockX) {
		XLockDisplay(xdisp);
		XSync(xdisp, 0);
	}

	if (p->include_border) {
		gs_copy_texture_region(
				p->tex, 0, 0,
				p->gltex,
				p->cur_cut_left,
				p->cur_cut_top,
				width(), height());
	} else {
		gs_copy_texture_region(
				p->tex, 0, 0,
				p->gltex,
				p->cur_cut_left + p->border,
				p->cur_cut_top + p->border,
				width(), height());
	}

	if (p->cursor && p->show_cursor) {
		xcursor_tick(p->cursor);

		p->cursor_outside =
			p->cursor->x < p->cur_cut_left                   ||
			p->cursor->y < p->cur_cut_top                    ||
			p->cursor->x > int(p->width  - p->cur_cut_right) ||
			p->cursor->y > int(p->height - p->cur_cut_bot);
	}

	if (p->lockX)
		XUnlockDisplay(xdisp);

	obs_leave_graphics();
}
Ejemplo n.º 3
0
void XCompcapMain::tick(float seconds)
{
	UNUSED_PARAMETER(seconds);

	if (!obs_source_showing(p->source))
		return;

	PLock lock(&p->lock, true);

	if (!lock.isLocked())
		return;

	XCompcap::processEvents();

	if (XCompcap::windowWasReconfigured(p->win))
		updateSettings(0);

	XErrorLock xlock;
	xlock.resetError();
	XWindowAttributes attr;

	if (!XGetWindowAttributes(xdisp, p->win, &attr)) {
		Window newWin = getWindowFromString(p->windowName);

		if (XGetWindowAttributes(xdisp, newWin, &attr)) {
			p->win = newWin;
			updateSettings(0);
		}
		return;
	}

	if (!p->tex || !p->gltex)
		return;

	obs_enter_graphics();

	if (p->lockX) {
		XLockDisplay(xdisp);
		XSync(xdisp, 0);
	}

	if (p->include_border) {
		gs_copy_texture_region(
				p->tex, 0, 0,
				p->gltex,
				p->cur_cut_left,
				p->cur_cut_top,
				width(), height());
	} else {
		gs_copy_texture_region(
				p->tex, 0, 0,
				p->gltex,
				p->cur_cut_left + p->border,
				p->cur_cut_top + p->border,
				width(), height());
	}

	if (p->cursor && p->show_cursor) {
		xcursor_tick(p->cursor);

		p->cursor_outside =
			p->cursor->x < p->cur_cut_left                   ||
			p->cursor->y < p->cur_cut_top                    ||
			p->cursor->x > int(p->width  - p->cur_cut_right) ||
			p->cursor->y > int(p->height - p->cur_cut_bot);
	}

	if (p->lockX)
		XUnlockDisplay(xdisp);

	obs_leave_graphics();
}