[033/906] * sys/glsink/Makefile.am: * sys/glsink/glimagesink.c: * sys/glsink/glvideo.c: * sys/glsink/glvideo.h: Split out gl-related code into a separate file with a sensible API. Major cleanup. Still crashes occasionally due to different threads touching bits at the same time.

This commit is contained in:
David Schleef 2007-12-12 02:33:12 +00:00 committed by Matthew Waters
parent 7e83b698c4
commit 40c4ec0e6a
4 changed files with 503 additions and 393 deletions

View file

@ -1,7 +1,7 @@
plugin_LTLIBRARIES = libgstglimagesink.la
libgstglimagesink_la_SOURCES = glimagesink.c
libgstglimagesink_la_SOURCES = glimagesink.c glvideo.c
libgstglimagesink_la_CFLAGS = $(GST_CFLAGS) $(X_CFLAGS) $(GST_BASE_CFLAGS) \
$(GST_PLUGINS_BASE_CFLAGS)
libgstglimagesink_la_LIBADD = $(X_LIBS) $(XSHM_LIBS) -lGL \

View file

@ -1,6 +1,6 @@
/* GStreamer
* Copyright (C) 2003 Julien Moutte <julien@moutte.net>
* Copyright (C) 2005,2006 David A. Schleef <ds@schleef.org>
* Copyright (C) 2005,2006,2007 David A. Schleef <ds@schleef.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
@ -30,8 +30,7 @@
#include <string.h>
#include <GL/glx.h>
#include <GL/gl.h>
#include "glvideo.h"
GST_DEBUG_CATEGORY_STATIC (gst_debug_glimage_sink);
#define GST_CAT_DEFAULT gst_debug_glimage_sink
@ -61,22 +60,12 @@ struct _GstGLImageSink
GstCaps *caps;
int fps_n, fps_d;
int par_n, par_d;
int height, width;
Window window;
Window parent_window;
XVisualInfo *visinfo;
GLVideoDisplay *display;
GLVideoDrawable *drawable;
GLVideoImageType type;
Display *display;
GLXContext context;
gboolean internal;
int max_texture_size;
gboolean have_yuv;
gboolean use_rgb;
gboolean use_rgbx;
gboolean use_yuy2;
XID window_id;
};
struct _GstGLImageSinkClass
@ -118,11 +107,7 @@ static gboolean gst_glimage_sink_interface_supported (GstImplementsInterface *
static void gst_glimage_sink_implements_init (GstImplementsInterfaceClass *
klass);
static void gst_glimage_sink_create_window (GstGLImageSink * glimage_sink);
static gboolean gst_glimage_sink_init_display (GstGLImageSink * glimage_sink);
static void gst_glimage_sink_update_caps (GstGLImageSink * glimage_sink);
static void gst_glimage_sink_push_image (GstGLImageSink * glimage_sink,
GstBuffer * buf);
static const GstElementDetails gst_glimage_sink_details =
GST_ELEMENT_DETAILS ("OpenGL video sink",
@ -218,9 +203,6 @@ gst_glimage_sink_init (GstGLImageSink * glimage_sink,
GstGLImageSinkClass * glimage_sink_class)
{
glimage_sink->width = -1;
glimage_sink->height = -1;
glimage_sink->display_name = NULL;
gst_glimage_sink_update_caps (glimage_sink);
}
@ -338,13 +320,32 @@ static gboolean
gst_glimage_sink_start (GstBaseSink * bsink)
{
GstGLImageSink *glimage_sink;
gboolean ret;
GST_DEBUG ("start");
glimage_sink = GST_GLIMAGE_SINK (bsink);
ret = gst_glimage_sink_init_display (glimage_sink);
glimage_sink->display = glv_display_new (glimage_sink->display_name);
if (glimage_sink->display == NULL) {
GST_ERROR ("failed to open display");
return FALSE;
}
return ret;
if (glimage_sink->window_id) {
glimage_sink->drawable =
glv_drawable_new_from_window (glimage_sink->display,
glimage_sink->window_id);
} else {
glimage_sink->drawable = glv_drawable_new_window (glimage_sink->display);
}
if (glimage_sink->drawable == NULL) {
GST_ERROR ("failed to create window");
return FALSE;
}
GST_DEBUG ("start done");
return TRUE;
}
static gboolean
@ -352,19 +353,16 @@ gst_glimage_sink_stop (GstBaseSink * bsink)
{
GstGLImageSink *glimage_sink;
GST_DEBUG ("stop");
glimage_sink = GST_GLIMAGE_SINK (bsink);
if (glimage_sink->display) {
if (glimage_sink->context) {
glXDestroyContext (glimage_sink->display, glimage_sink->context);
glimage_sink->context = NULL;
}
XSync (glimage_sink->display, False);
XCloseDisplay (glimage_sink->display);
glimage_sink->display = NULL;
}
glimage_sink->context = NULL;
glimage_sink->window = 0;
glv_drawable_free (glimage_sink->drawable);
glv_display_free (glimage_sink->display);
glimage_sink->display = NULL;
glimage_sink->drawable = NULL;
return TRUE;
}
@ -373,6 +371,8 @@ gst_glimage_sink_unlock (GstBaseSink * bsink)
{
//GstGLImageSink *glimage_sink;
GST_DEBUG ("unlock");
//glimage_sink = GST_GLIMAGE_SINK (bsink);
/* FIXME */
@ -450,8 +450,6 @@ gst_glimage_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
if (!ret)
return FALSE;
glimage_sink->width = width;
glimage_sink->height = height;
glimage_sink->fps_n = gst_value_get_fraction_numerator (fps);
glimage_sink->fps_d = gst_value_get_fraction_denominator (fps);
if (par) {
@ -469,35 +467,31 @@ gst_glimage_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
int red_mask;
GST_DEBUG ("using RGB");
glimage_sink->use_rgb = TRUE;
gst_structure_get_int (structure, "red_mask", &red_mask);
if (red_mask == 0xff000000) {
glimage_sink->use_rgbx = TRUE;
glimage_sink->type = GLVIDEO_IMAGE_TYPE_RGBA;
} else {
glimage_sink->use_rgbx = FALSE;
glimage_sink->type = GLVIDEO_IMAGE_TYPE_BGRA;
}
} else {
unsigned int fourcc;
GST_DEBUG ("using YUV");
glimage_sink->use_rgb = FALSE;
gst_structure_get_fourcc (structure, "format", &fourcc);
if (fourcc == GST_MAKE_FOURCC ('Y', 'U', 'Y', '2')) {
glimage_sink->use_yuy2 = TRUE;
glimage_sink->type = GLVIDEO_IMAGE_TYPE_YUY2;
} else {
glimage_sink->use_yuy2 = FALSE;
glimage_sink->type = GLVIDEO_IMAGE_TYPE_UYVY;
}
}
if (!glimage_sink->window) {
gst_x_overlay_prepare_xwindow_id (GST_X_OVERLAY (glimage_sink));
}
#if 0
if (!glimage_sink->window) {
gst_glimage_sink_create_window (glimage_sink);
}
#endif
return TRUE;
}
@ -507,9 +501,14 @@ gst_glimage_sink_render (GstBaseSink * bsink, GstBuffer * buf)
{
GstGLImageSink *glimage_sink;
GST_DEBUG ("render");
glimage_sink = GST_GLIMAGE_SINK (bsink);
gst_glimage_sink_push_image (glimage_sink, buf);
glv_drawable_draw_image (glimage_sink->drawable,
glimage_sink->type, GST_BUFFER_DATA (buf),
GST_VIDEO_SINK_WIDTH (glimage_sink),
GST_VIDEO_SINK_HEIGHT (glimage_sink));
return GST_FLOW_OK;
}
@ -532,36 +531,19 @@ gst_glimage_sink_set_xwindow_id (GstXOverlay * overlay, XID window_id)
g_return_if_fail (GST_IS_GLIMAGE_SINK (overlay));
GST_DEBUG ("set_xwindow_id");
glimage_sink = GST_GLIMAGE_SINK (overlay);
if (glimage_sink->window == window_id) {
if (glimage_sink->window_id == window_id) {
return;
}
/* FIXME check if display inited */
if (window_id == 0) {
/* go back to independent window */
/* FIXME */
glimage_sink->internal = TRUE;
} else {
XWindowAttributes attr;
glimage_sink->window = window_id;
XGetWindowAttributes (glimage_sink->display, window_id, &attr);
glimage_sink->width = attr.width;
glimage_sink->height = attr.height;
glimage_sink->internal = FALSE;
#if 0
/* FIXME */
if (glimage_sink->handle_events) {
XSelectInput (glimage_sink->display, window_id,
ExposureMask | StructureNotifyMask | PointerMotionMask | KeyPressMask
| KeyReleaseMask);
}
#endif
glimage_sink->window_id = window_id;
if (glimage_sink->drawable) {
glv_drawable_free (glimage_sink->drawable);
glimage_sink->drawable =
glv_drawable_new_from_window (glimage_sink->display, window_id);
}
}
static void
@ -631,7 +613,7 @@ gst_glimage_sink_update_caps (GstGLImageSink * glimage_sink)
caps = gst_caps_from_string (GST_VIDEO_CAPS_RGBx ";" GST_VIDEO_CAPS_BGRx);
#ifdef GL_YCBCR_MESA
if (glimage_sink->have_yuv) {
if (glimage_sink->display->have_ycbcr_texture) {
GstCaps *ycaps =
gst_caps_from_string (GST_VIDEO_CAPS_YUV ("{ UYVY, YUY2 }"));
gst_caps_append (ycaps, caps);
@ -639,7 +621,7 @@ gst_glimage_sink_update_caps (GstGLImageSink * glimage_sink)
}
#endif
max_size = glimage_sink->max_texture_size;
max_size = glimage_sink->display->max_texture_size;
if (max_size == 0) {
max_size = 1024;
}
@ -651,319 +633,6 @@ gst_glimage_sink_update_caps (GstGLImageSink * glimage_sink)
gst_caps_replace (&glimage_sink->caps, caps);
}
static void
gst_glimage_sink_create_window (GstGLImageSink * glimage_sink)
{
gboolean ret;
Window root;
XSetWindowAttributes attr;
Screen *screen;
int scrnum;
int mask;
int width, height;
screen = XDefaultScreenOfDisplay (glimage_sink->display);
scrnum = XScreenNumberOfScreen (screen);
root = XRootWindow (glimage_sink->display, scrnum);
if (glimage_sink->parent_window) {
XWindowAttributes pattr;
XGetWindowAttributes (glimage_sink->display, glimage_sink->parent_window,
&pattr);
width = pattr.width;
height = pattr.height;
} else {
width = GST_VIDEO_SINK (glimage_sink)->width;
height = GST_VIDEO_SINK (glimage_sink)->height;
}
attr.background_pixel = 0;
attr.border_pixel = 0;
attr.colormap = XCreateColormap (glimage_sink->display, root,
glimage_sink->visinfo->visual, AllocNone);
if (glimage_sink->parent_window) {
attr.override_redirect = True;
} else {
attr.override_redirect = False;
}
mask = CWBackPixel | CWBorderPixel | CWColormap | CWOverrideRedirect;
GST_DEBUG ("creating window with size %d x %d", width, height);
glimage_sink->window = XCreateWindow (glimage_sink->display, root, 0, 0,
width, height,
0, glimage_sink->visinfo->depth, InputOutput,
glimage_sink->visinfo->visual, mask, &attr);
if (glimage_sink->parent_window) {
ret = XReparentWindow (glimage_sink->display, glimage_sink->window,
glimage_sink->parent_window, 0, 0);
XMapWindow (glimage_sink->display, glimage_sink->window);
} else {
XMapWindow (glimage_sink->display, glimage_sink->window);
}
XSync (glimage_sink->display, False);
gst_x_overlay_got_xwindow_id (GST_X_OVERLAY (glimage_sink),
glimage_sink->window);
glXMakeCurrent (glimage_sink->display, glimage_sink->window,
glimage_sink->context);
glDepthFunc (GL_LESS);
glEnable (GL_DEPTH_TEST);
glClearColor (0.2, 0.2, 0.2, 1.0);
glViewport (0, 0, width, height);
}
static gboolean
gst_glimage_sink_init_display (GstGLImageSink * glimage_sink)
{
gboolean ret;
XVisualInfo *visinfo;
Screen *screen;
Window root;
int scrnum;
int attrib[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, None
};
XSetWindowAttributes attr;
int error_base;
int event_base;
int mask;
const char *extstring;
Window window;
GST_DEBUG_OBJECT (glimage_sink, "initializing display");
glimage_sink->display = XOpenDisplay (NULL);
if (glimage_sink->display == NULL) {
GST_DEBUG_OBJECT (glimage_sink, "Could not open display");
return FALSE;
}
screen = XDefaultScreenOfDisplay (glimage_sink->display);
scrnum = XScreenNumberOfScreen (screen);
root = XRootWindow (glimage_sink->display, scrnum);
ret = glXQueryExtension (glimage_sink->display, &error_base, &event_base);
if (!ret) {
GST_DEBUG_OBJECT (glimage_sink, "No GLX extension");
return FALSE;
}
visinfo = glXChooseVisual (glimage_sink->display, scrnum, attrib);
if (visinfo == NULL) {
GST_DEBUG_OBJECT (glimage_sink, "No usable visual");
return FALSE;
}
glimage_sink->visinfo = visinfo;
glimage_sink->context = glXCreateContext (glimage_sink->display,
visinfo, NULL, True);
attr.background_pixel = 0;
attr.border_pixel = 0;
attr.colormap = XCreateColormap (glimage_sink->display, root,
visinfo->visual, AllocNone);
attr.event_mask = StructureNotifyMask | ExposureMask;
attr.override_redirect = True;
//mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
mask = CWBackPixel | CWBorderPixel | CWColormap | CWOverrideRedirect;
window = XCreateWindow (glimage_sink->display, root, 0, 0,
100, 100, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
XSync (glimage_sink->display, FALSE);
glXMakeCurrent (glimage_sink->display, window, glimage_sink->context);
glGetIntegerv (GL_MAX_TEXTURE_SIZE, &glimage_sink->max_texture_size);
extstring = (const char *) glGetString (GL_EXTENSIONS);
#ifdef GL_YCBCR_MESA
if (strstr (extstring, "GL_MESA_ycbcr_texture")) {
glimage_sink->have_yuv = TRUE;
} else {
glimage_sink->have_yuv = FALSE;
}
#else
glimage_sink->have_yuv = FALSE;
#endif
gst_glimage_sink_update_caps (glimage_sink);
glXMakeCurrent (glimage_sink->display, None, NULL);
XDestroyWindow (glimage_sink->display, window);
return TRUE;
}
static void
gst_glimage_sink_push_image (GstGLImageSink * glimage_sink, GstBuffer * buf)
{
int texture_size;
XWindowAttributes attr;
g_return_if_fail (buf != NULL);
if (glimage_sink->display == NULL || glimage_sink->window == 0) {
g_warning ("display or window not set up\n");
}
glXMakeCurrent (glimage_sink->display, glimage_sink->window,
glimage_sink->context);
if (glimage_sink->parent_window) {
XGetWindowAttributes (glimage_sink->display, glimage_sink->parent_window,
&attr);
//gst_glimage_sink_set_window_size (glimage_sink, attr.width, attr.height);
} else {
XGetWindowAttributes (glimage_sink->display, glimage_sink->window, &attr);
glViewport (0, 0, attr.width, attr.height);
}
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glDisable (GL_CULL_FACE);
glEnable (GL_TEXTURE_2D);
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
glColor4f (1, 1, 1, 1);
#define TEXID 1000
glBindTexture (GL_TEXTURE_2D, TEXID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
for (texture_size = 64;
(texture_size < GST_VIDEO_SINK (glimage_sink)->width ||
texture_size < GST_VIDEO_SINK (glimage_sink)->height) &&
(texture_size > 0); texture_size <<= 1);
if (glimage_sink->use_rgb) {
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, texture_size,
texture_size, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
if (glimage_sink->use_rgbx) {
glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0,
GST_VIDEO_SINK (glimage_sink)->width,
GST_VIDEO_SINK (glimage_sink)->height,
GL_RGBA, GL_UNSIGNED_BYTE, GST_BUFFER_DATA (buf));
} else {
glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0,
GST_VIDEO_SINK (glimage_sink)->width,
GST_VIDEO_SINK (glimage_sink)->height,
GL_BGRA, GL_UNSIGNED_BYTE, GST_BUFFER_DATA (buf));
}
} else {
#ifdef GL_YCBCR_MESA
glTexImage2D (GL_TEXTURE_2D, 0, GL_YCBCR_MESA, texture_size,
texture_size, 0, GL_YCBCR_MESA, GL_UNSIGNED_SHORT_8_8_REV_MESA, NULL);
if (glimage_sink->use_yuy2) {
glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0,
GST_VIDEO_SINK (glimage_sink)->width,
GST_VIDEO_SINK (glimage_sink)->height,
GL_YCBCR_MESA, GL_UNSIGNED_SHORT_8_8_REV_MESA, GST_BUFFER_DATA (buf));
} else {
glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0,
GST_VIDEO_SINK (glimage_sink)->width,
GST_VIDEO_SINK (glimage_sink)->height,
GL_YCBCR_MESA, GL_UNSIGNED_SHORT_8_8_MESA, GST_BUFFER_DATA (buf));
}
#else
g_assert_not_reached ();
#endif
}
glColor4f (1, 0, 1, 1);
glBegin (GL_QUADS);
glNormal3f (0, 0, -1);
{
double xmax = GST_VIDEO_SINK (glimage_sink)->width / (double) texture_size;
double ymax = GST_VIDEO_SINK (glimage_sink)->height / (double) texture_size;
glTexCoord2f (xmax, 0);
glVertex3f (1.0, 1.0, 0);
glTexCoord2f (0, 0);
glVertex3f (-1.0, 1.0, 0);
glTexCoord2f (0, ymax);
glVertex3f (-1.0, -1.0, 0);
glTexCoord2f (xmax, ymax);
glVertex3f (1.0, -1.0, 0);
glEnd ();
}
glFlush ();
glXSwapBuffers (glimage_sink->display, glimage_sink->window);
}
#ifdef unused
static void gst_glimage_sink_set_window_size (GstGLImageSink * glimage_sink,
int width, int height);
static void
gst_glimage_sink_set_window_size (GstGLImageSink * glimage_sink,
int width, int height)
{
GST_DEBUG ("resizing to %d x %d",
GST_VIDEO_SINK_WIDTH (glimage_sink),
GST_VIDEO_SINK_HEIGHT (glimage_sink));
if (glimage_sink->display && glimage_sink->window) {
XResizeWindow (glimage_sink->display, glimage_sink->window, width, height);
XSync (glimage_sink->display, False);
glViewport (0, 0, width, height);
}
}
static void
gst_glimage_sink_set_xwindow_id (GstXOverlay * overlay, XID xwindow_id)
{
GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (overlay);
GST_DEBUG ("set_xwindow_id %ld", xwindow_id);
g_return_if_fail (GST_IS_GLIMAGE_SINK (glimage_sink));
/* If the element has not initialized the X11 context try to do so */
if (!glimage_sink->display) {
g_warning ("X display not inited\n");
}
if (glimage_sink->parent_window == xwindow_id)
return;
glimage_sink->parent_window = xwindow_id;
XSync (glimage_sink->display, False);
gst_glimage_sink_create_window (glimage_sink);
}
#endif
static gboolean
plugin_init (GstPlugin * plugin)

377
gst/gl/glvideo.c Normal file
View file

@ -0,0 +1,377 @@
/* glvideo.c
* Copyright (C) 2007 David A. Schleef <ds@schleef.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "glvideo.h"
/* only use gst for debugging */
#include <gst/gst.h>
#include <string.h>
static gboolean glv_display_check_features (GLVideoDisplay * display);
GLVideoDisplay *
glv_display_new (const char *display_name)
{
GLVideoDisplay *display;
gboolean usable;
display = g_malloc0 (sizeof (GLVideoDisplay));
display->display = XOpenDisplay (display_name);
if (display->display == NULL) {
g_free (display);
return NULL;
}
usable = glv_display_check_features (display);
if (!usable) {
g_free (display);
return NULL;
}
display->lock = g_mutex_new ();
return display;
}
static gboolean
glv_display_check_features (GLVideoDisplay * display)
{
gboolean ret;
XVisualInfo *visinfo;
Screen *screen;
Window root;
int scrnum;
int attrib[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 8,
GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, None
};
XSetWindowAttributes attr;
int error_base;
int event_base;
int mask;
const char *extstring;
Window window;
screen = XDefaultScreenOfDisplay (display->display);
scrnum = XScreenNumberOfScreen (screen);
root = XRootWindow (display->display, scrnum);
ret = glXQueryExtension (display->display, &error_base, &event_base);
if (!ret) {
GST_DEBUG ("No GLX extension");
return FALSE;
}
visinfo = glXChooseVisual (display->display, scrnum, attrib);
if (visinfo == NULL) {
GST_DEBUG ("No usable visual");
return FALSE;
}
display->visinfo = visinfo;
display->context = glXCreateContext (display->display, visinfo, NULL, True);
attr.background_pixel = 0;
attr.border_pixel = 0;
attr.colormap = XCreateColormap (display->display, root,
visinfo->visual, AllocNone);
attr.event_mask = StructureNotifyMask | ExposureMask;
attr.override_redirect = True;
mask = CWBackPixel | CWBorderPixel | CWColormap | CWOverrideRedirect;
window = XCreateWindow (display->display, root, 0, 0,
100, 100, 0, visinfo->depth, InputOutput, visinfo->visual, mask, &attr);
XSync (display->display, FALSE);
glXMakeCurrent (display->display, window, display->context);
glGetIntegerv (GL_MAX_TEXTURE_SIZE, &display->max_texture_size);
extstring = (const char *) glGetString (GL_EXTENSIONS);
#ifdef GL_YCBCR_MESA
if (strstr (extstring, "GL_MESA_ycbcr_texture")) {
display->have_ycbcr_texture = TRUE;
} else {
display->have_ycbcr_texture = FALSE;
}
#else
display->have_ycbcr_texture = FALSE;
#endif
glXMakeCurrent (display->display, None, NULL);
XDestroyWindow (display->display, window);
return TRUE;
}
void
glv_display_free (GLVideoDisplay * display)
{
/* sure hope nobody is using it as it's being freed */
g_mutex_lock (display->lock);
g_mutex_unlock (display->lock);
if (display->context) {
glXDestroyContext (display->display, display->context);
}
if (display->visinfo) {
XFree (display->visinfo);
}
if (display->display) {
XCloseDisplay (display->display);
}
g_mutex_free (display->lock);
g_free (display);
}
/* drawable */
GLVideoDrawable *
glv_drawable_new_window (GLVideoDisplay * display)
{
GLVideoDrawable *drawable;
XSetWindowAttributes attr = { 0 };
int scrnum;
int mask;
Window root;
Screen *screen;
drawable = g_malloc0 (sizeof (GLVideoDrawable));
g_mutex_lock (display->lock);
drawable->display = display;
screen = XDefaultScreenOfDisplay (display->display);
scrnum = XScreenNumberOfScreen (screen);
root = XRootWindow (display->display, scrnum);
attr.background_pixel = 0;
attr.border_pixel = 0;
attr.colormap = XCreateColormap (display->display, root,
display->visinfo->visual, AllocNone);
attr.override_redirect = False;
#if 0
if (display->parent_window) {
attr.override_redirect = True;
}
#endif
mask = CWBackPixel | CWBorderPixel | CWColormap | CWOverrideRedirect;
drawable->window = XCreateWindow (display->display,
root, 0, 0, 100, 100,
0, display->visinfo->depth, InputOutput,
display->visinfo->visual, mask, &attr);
XMapWindow (display->display, drawable->window);
drawable->destroy_on_free = TRUE;
g_mutex_unlock (display->lock);
return drawable;
}
GLVideoDrawable *
glv_drawable_new_root_window (GLVideoDisplay * display)
{
GLVideoDrawable *drawable;
int scrnum;
Screen *screen;
drawable = g_malloc0 (sizeof (GLVideoDrawable));
g_mutex_lock (display->lock);
drawable->display = display;
screen = XDefaultScreenOfDisplay (display->display);
scrnum = XScreenNumberOfScreen (screen);
drawable->window = XRootWindow (display->display, scrnum);
drawable->destroy_on_free = FALSE;
g_mutex_unlock (display->lock);
return drawable;
}
GLVideoDrawable *
glv_drawable_new_from_window (GLVideoDisplay * display, Window window)
{
GLVideoDrawable *drawable;
drawable = g_malloc0 (sizeof (GLVideoDrawable));
g_mutex_lock (display->lock);
drawable->display = display;
drawable->window = window;
drawable->destroy_on_free = FALSE;
g_mutex_unlock (display->lock);
return drawable;
}
void
glv_drawable_free (GLVideoDrawable * drawable)
{
g_mutex_lock (drawable->display->lock);
if (drawable->destroy_on_free) {
XDestroyWindow (drawable->display->display, drawable->window);
}
g_mutex_unlock (drawable->display->lock);
g_free (drawable);
}
void
glv_drawable_lock (GLVideoDrawable * drawable)
{
g_mutex_lock (drawable->display->lock);
glXMakeCurrent (drawable->display->display, drawable->window,
drawable->display->context);
}
void
glv_drawable_unlock (GLVideoDrawable * drawable)
{
glXMakeCurrent (drawable->display->display, None, NULL);
g_mutex_unlock (drawable->display->lock);
}
void
glv_drawable_update_attributes (GLVideoDrawable * drawable)
{
XWindowAttributes attr;
XGetWindowAttributes (drawable->display->display, drawable->window, &attr);
drawable->win_width = attr.width;
drawable->win_height = attr.height;
}
void
glv_drawable_clear (GLVideoDrawable * drawable)
{
glv_drawable_lock (drawable);
glDepthFunc (GL_LESS);
glEnable (GL_DEPTH_TEST);
glClearColor (0.2, 0.2, 0.2, 1.0);
glViewport (0, 0, drawable->win_width, drawable->win_height);
glv_drawable_unlock (drawable);
}
void
glv_drawable_draw_image (GLVideoDrawable * drawable, GLVideoImageType type,
void *data, int width, int height)
{
g_return_if_fail (data != NULL);
g_return_if_fail (width > 0);
g_return_if_fail (height > 0);
glv_drawable_lock (drawable);
glv_drawable_update_attributes (drawable);
glViewport (0, 0, drawable->win_width, drawable->win_height);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glDisable (GL_CULL_FACE);
glEnable (GL_TEXTURE_RECTANGLE_ARB);
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
glColor4f (1, 1, 1, 1);
glBindTexture (GL_TEXTURE_RECTANGLE_ARB, 1);
glTexParameteri (GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
switch (type) {
case GLVIDEO_IMAGE_TYPE_RGBA:
glTexImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, width, height,
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, 0, 0, width, height,
GL_RGBA, GL_UNSIGNED_BYTE, data);
break;
case GLVIDEO_IMAGE_TYPE_BGRA:
glTexImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, width, height,
0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, 0, 0, width, height,
GL_BGRA, GL_UNSIGNED_BYTE, data);
break;
case GLVIDEO_IMAGE_TYPE_YUY2:
glTexImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, GL_YCBCR_MESA, width, height,
0, GL_YCBCR_MESA, GL_UNSIGNED_SHORT_8_8_REV_MESA, NULL);
glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, 0, 0, width, height,
GL_YCBCR_MESA, GL_UNSIGNED_SHORT_8_8_REV_MESA, data);
break;
case GLVIDEO_IMAGE_TYPE_UYVY:
glTexImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, GL_YCBCR_MESA, width, height,
0, GL_YCBCR_MESA, GL_UNSIGNED_SHORT_8_8_REV_MESA, NULL);
glTexSubImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, 0, 0, width, height,
GL_YCBCR_MESA, GL_UNSIGNED_SHORT_8_8_MESA, data);
default:
g_assert_not_reached ();
}
glColor4f (1, 0, 1, 1);
glBegin (GL_QUADS);
glNormal3f (0, 0, -1);
glTexCoord2f (width, 0);
glVertex3f (1.0, 1.0, 0);
glTexCoord2f (0, 0);
glVertex3f (-1.0, 1.0, 0);
glTexCoord2f (0, height);
glVertex3f (-1.0, -1.0, 0);
glTexCoord2f (width, height);
glVertex3f (1.0, -1.0, 0);
glEnd ();
glFlush ();
glXSwapBuffers (drawable->display->display, drawable->window);
glv_drawable_unlock (drawable);
}

64
gst/gl/glvideo.h Normal file
View file

@ -0,0 +1,64 @@
#ifndef __GLVIDEO_H__
#define __GLVIDEO_H__
#include <GL/glx.h>
#include <GL/gl.h>
#include <glib.h>
typedef struct _GLVideoDisplay GLVideoDisplay;
typedef struct _GLVideoDrawable GLVideoDrawable;
typedef enum {
GLVIDEO_IMAGE_TYPE_RGBA,
GLVIDEO_IMAGE_TYPE_BGRA,
GLVIDEO_IMAGE_TYPE_YUY2,
GLVIDEO_IMAGE_TYPE_UYVY,
} GLVideoImageType;
struct _GLVideoDisplay {
Display *display;
XVisualInfo *visinfo;
GLXContext context;
GMutex *lock;
Screen *screen;
int scrnum;
Window root;
int max_texture_size;
gboolean have_ycbcr_texture;
};
struct _GLVideoDrawable {
GLVideoDisplay *display;
Window window;
gboolean destroy_on_free;
int win_width;
int win_height;
};
GLVideoDisplay *glv_display_new (const char *display_name);
void glv_display_free (GLVideoDisplay *display);
/* drawable */
GLVideoDrawable * glv_drawable_new_window (GLVideoDisplay *display);
GLVideoDrawable * glv_drawable_new_root_window (GLVideoDisplay *display);
GLVideoDrawable * glv_drawable_new_from_window (GLVideoDisplay *display, Window window);
void glv_drawable_free (GLVideoDrawable *drawable);
void glv_drawable_lock (GLVideoDrawable *drawable);
void glv_drawable_unlock (GLVideoDrawable *drawable);
void glv_drawable_update_attributes (GLVideoDrawable *drawable);
void glv_drawable_clear (GLVideoDrawable *drawable);
void glv_drawable_draw_image (GLVideoDrawable *drawable, GLVideoImageType type, void *data, int width, int height);
#endif