Blame sys/xvimage/xvimageallocator.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) <2005> Julien Moutte <julien@moutte.net>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#ifdef HAVE_XSHM
Packit 971217
#include <sys/types.h>
Packit 971217
#include <sys/ipc.h>
Packit 971217
#include <sys/shm.h>
Packit 971217
#endif /* HAVE_XSHM */
Packit 971217
Packit 971217
#include <X11/Xlib.h>
Packit 971217
#include <X11/Xutil.h>
Packit 971217
Packit 971217
#ifdef HAVE_XSHM
Packit 971217
#include <X11/extensions/XShm.h>
Packit 971217
#endif /* HAVE_XSHM */
Packit 971217
Packit 971217
#include <string.h>
Packit 971217
#include <math.h>
Packit 971217
Packit 971217
/* Object header */
Packit 971217
#include "xvimageallocator.h"
Packit 971217
Packit 971217
/* Debugging category */
Packit 971217
#include <gst/gstinfo.h>
Packit 971217
Packit 971217
/* Helper functions */
Packit 971217
#include <gst/video/video.h>
Packit 971217
Packit 971217
Packit 971217
GST_DEBUG_CATEGORY_STATIC (gst_debug_xvimageallocator);
Packit 971217
#define GST_CAT_DEFAULT gst_debug_xvimageallocator
Packit 971217
Packit 971217
struct _GstXvImageMemory
Packit 971217
{
Packit 971217
  GstMemory parent;
Packit 971217
Packit 971217
  gint im_format;
Packit 971217
  GstVideoInfo info;
Packit 971217
  GstVideoRectangle crop;
Packit 971217
Packit 971217
  XvImage *xvimage;
Packit 971217
Packit 971217
#ifdef HAVE_XSHM
Packit 971217
  XShmSegmentInfo SHMInfo;
Packit 971217
#endif                          /* HAVE_XSHM */
Packit 971217
};
Packit 971217
Packit 971217
Packit 971217
struct _GstXvImageAllocator
Packit 971217
{
Packit 971217
  GstAllocator parent;
Packit 971217
Packit 971217
  GstXvContext *context;
Packit 971217
};
Packit 971217
Packit 971217
struct _GstXvImageAllocatorClass
Packit 971217
{
Packit 971217
  GstAllocatorClass parent_class;
Packit 971217
};
Packit 971217
Packit 971217
gboolean
Packit 971217
gst_xvimage_memory_is_from_context (GstMemory * mem, GstXvContext * context)
Packit 971217
{
Packit 971217
  GstXvImageAllocator *alloc;
Packit 971217
Packit 971217
  if (!GST_IS_XVIMAGE_ALLOCATOR (mem->allocator))
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  alloc = GST_XVIMAGE_ALLOCATOR_CAST (mem->allocator);
Packit 971217
Packit 971217
  if (alloc->context != context)
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
gint
Packit 971217
gst_xvimage_memory_get_format (GstXvImageMemory * xvmem)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (xvmem != NULL, FALSE);
Packit 971217
Packit 971217
  return xvmem->im_format;
Packit 971217
}
Packit 971217
Packit 971217
XvImage *
Packit 971217
gst_xvimage_memory_get_xvimage (GstXvImageMemory * xvmem)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (xvmem != NULL, FALSE);
Packit 971217
Packit 971217
  return xvmem->xvimage;
Packit 971217
}
Packit 971217
Packit 971217
gboolean
Packit 971217
gst_xvimage_memory_get_crop (GstXvImageMemory * xvmem, GstVideoRectangle * crop)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (xvmem != NULL, FALSE);
Packit 971217
Packit 971217
  if (crop)
Packit 971217
    *crop = xvmem->crop;
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
Packit 971217
/* X11 stuff */
Packit 971217
static gboolean error_caught = FALSE;
Packit 971217
Packit 971217
static int
Packit 971217
gst_xvimage_handle_xerror (Display * display, XErrorEvent * xevent)
Packit 971217
{
Packit 971217
  char error_msg[1024];
Packit 971217
Packit 971217
  XGetErrorText (display, xevent->error_code, error_msg, 1024);
Packit 971217
  GST_DEBUG ("xvimage triggered an XError. error: %s", error_msg);
Packit 971217
  error_caught = TRUE;
Packit 971217
  return 0;
Packit 971217
}
Packit 971217
Packit 971217
static GstMemory *
Packit 971217
gst_xvimage_allocator_dummy_alloc (GstAllocator * allocator, gsize size,
Packit 971217
    GstAllocationParams * params)
Packit 971217
{
Packit 971217
  return NULL;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_xvimage_allocator_free (GstAllocator * allocator, GstMemory * gmem)
Packit 971217
{
Packit 971217
  GstXvImageMemory *mem = (GstXvImageMemory *) gmem;
Packit 971217
  GstXvImageAllocator *alloc = (GstXvImageAllocator *) allocator;
Packit 971217
  GstXvContext *context;
Packit 971217
Packit 971217
  if (gmem->parent)
Packit 971217
    goto sub_mem;
Packit 971217
Packit 971217
  context = alloc->context;
Packit 971217
Packit 971217
  GST_DEBUG_OBJECT (allocator, "free memory %p", mem);
Packit 971217
Packit 971217
  g_mutex_lock (&context->lock);
Packit 971217
Packit 971217
#ifdef HAVE_XSHM
Packit 971217
  if (context->use_xshm) {
Packit 971217
    if (mem->SHMInfo.shmaddr != ((void *) -1)) {
Packit 971217
      GST_DEBUG_OBJECT (allocator, "XServer ShmDetaching from 0x%x id 0x%lx",
Packit 971217
          mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
Packit 971217
      XShmDetach (context->disp, &mem->SHMInfo);
Packit 971217
      XSync (context->disp, FALSE);
Packit 971217
      shmdt (mem->SHMInfo.shmaddr);
Packit 971217
      mem->SHMInfo.shmaddr = (void *) -1;
Packit 971217
    }
Packit 971217
    if (mem->xvimage)
Packit 971217
      XFree (mem->xvimage);
Packit 971217
  } else
Packit 971217
#endif /* HAVE_XSHM */
Packit 971217
  {
Packit 971217
    if (mem->xvimage) {
Packit 971217
      g_free (mem->xvimage->data);
Packit 971217
      XFree (mem->xvimage);
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
  XSync (context->disp, FALSE);
Packit 971217
Packit 971217
  g_mutex_unlock (&context->lock);
Packit 971217
Packit 971217
sub_mem:
Packit 971217
  g_slice_free (GstXvImageMemory, mem);
Packit 971217
}
Packit 971217
Packit 971217
static gpointer
Packit 971217
gst_xvimage_memory_map (GstXvImageMemory * mem, gsize maxsize,
Packit 971217
    GstMapFlags flags)
Packit 971217
{
Packit 971217
  return mem->xvimage->data + mem->parent.offset;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_xvimage_memory_unmap (GstXvImageMemory * mem)
Packit 971217
{
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static GstXvImageMemory *
Packit 971217
gst_xvimage_memory_share (GstXvImageMemory * mem, gssize offset, gsize size)
Packit 971217
{
Packit 971217
  GstXvImageMemory *sub;
Packit 971217
  GstMemory *parent;
Packit 971217
Packit 971217
  /* We can only share the complete memory */
Packit 971217
  if (offset != 0)
Packit 971217
    return NULL;
Packit 971217
  if (size != -1 && size != mem->xvimage->data_size)
Packit 971217
    return NULL;
Packit 971217
Packit 971217
  GST_DEBUG ("share memory %p", mem);
Packit 971217
Packit 971217
  /* find the real parent */
Packit 971217
  if ((parent = mem->parent.parent) == NULL)
Packit 971217
    parent = (GstMemory *) mem;
Packit 971217
Packit 971217
  if (size == -1)
Packit 971217
    size = mem->parent.size - offset;
Packit 971217
Packit 971217
  /* the shared memory is always readonly */
Packit 971217
  sub = g_slice_new (GstXvImageMemory);
Packit 971217
Packit 971217
  gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
Packit 971217
      GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->parent.allocator,
Packit 971217
      &mem->parent, mem->parent.maxsize, mem->parent.align,
Packit 971217
      mem->parent.offset + offset, size);
Packit 971217
Packit 971217
  sub->info = mem->info;
Packit 971217
  sub->im_format = mem->im_format;
Packit 971217
  sub->crop = mem->crop;
Packit 971217
  sub->xvimage = mem->xvimage;
Packit 971217
#ifdef HAVE_XSHM
Packit 971217
  sub->SHMInfo = mem->SHMInfo;
Packit 971217
#endif
Packit 971217
Packit 971217
  return sub;
Packit 971217
}
Packit 971217
Packit 971217
static GstXvImageMemory *
Packit 971217
gst_xvimage_memory_copy (GstMemory * gmem, gssize offset, gsize size)
Packit 971217
{
Packit 971217
  GstXvImageMemory *mem, *copy;
Packit 971217
Packit 971217
  mem = (GstXvImageMemory *) gmem;
Packit 971217
Packit 971217
  /* We can only copy the complete memory */
Packit 971217
  if (offset != 0)
Packit 971217
    return NULL;
Packit 971217
  if (size != -1 && size != mem->xvimage->data_size)
Packit 971217
    return NULL;
Packit 971217
Packit 971217
  GST_DEBUG ("copy memory %p", mem);
Packit 971217
Packit 971217
  copy = (GstXvImageMemory *)
Packit 971217
      gst_xvimage_allocator_alloc (GST_XVIMAGE_ALLOCATOR_CAST (gmem->allocator),
Packit 971217
      mem->im_format, &mem->info, mem->xvimage->width,
Packit 971217
      mem->xvimage->height, &mem->crop, NULL);
Packit 971217
Packit 971217
  memcpy (copy->xvimage->data + copy->parent.offset,
Packit 971217
      mem->xvimage->data + mem->parent.offset, mem->xvimage->data_size);
Packit 971217
Packit 971217
  return copy;
Packit 971217
}
Packit 971217
Packit 971217
#define gst_xvimage_allocator_parent_class parent_class
Packit 971217
G_DEFINE_TYPE (GstXvImageAllocator, gst_xvimage_allocator, GST_TYPE_ALLOCATOR);
Packit 971217
Packit 971217
static void gst_xvimage_allocator_finalize (GObject * object);
Packit 971217
Packit 971217
#define GST_XVIMAGE_ALLOCATOR_NAME "xvimage"
Packit 971217
Packit 971217
static void
Packit 971217
gst_xvimage_allocator_class_init (GstXvImageAllocatorClass * klass)
Packit 971217
{
Packit 971217
  GObjectClass *gobject_class;
Packit 971217
  GstAllocatorClass *allocator_class;
Packit 971217
Packit 971217
  gobject_class = (GObjectClass *) klass;
Packit 971217
  allocator_class = (GstAllocatorClass *) klass;
Packit 971217
Packit 971217
  gobject_class->finalize = gst_xvimage_allocator_finalize;
Packit 971217
Packit 971217
  allocator_class->alloc = gst_xvimage_allocator_dummy_alloc;
Packit 971217
  allocator_class->free = gst_xvimage_allocator_free;
Packit 971217
Packit 971217
  GST_DEBUG_CATEGORY_INIT (gst_debug_xvimageallocator, "xvimageallocator", 0,
Packit 971217
      "xvimageallocator object");
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_xvimage_allocator_init (GstXvImageAllocator * allocator)
Packit 971217
{
Packit 971217
  GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
Packit 971217
Packit 971217
  alloc->mem_type = GST_XVIMAGE_ALLOCATOR_NAME;
Packit 971217
  alloc->mem_map = (GstMemoryMapFunction) gst_xvimage_memory_map;
Packit 971217
  alloc->mem_unmap = (GstMemoryUnmapFunction) gst_xvimage_memory_unmap;
Packit 971217
  alloc->mem_share = (GstMemoryShareFunction) gst_xvimage_memory_share;
Packit 971217
  alloc->mem_copy = (GstMemoryShareFunction) gst_xvimage_memory_copy;
Packit 971217
  /* fallback is_span */
Packit 971217
Packit 971217
  GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_xvimage_allocator_finalize (GObject * object)
Packit 971217
{
Packit 971217
  GstXvImageAllocator *alloc = GST_XVIMAGE_ALLOCATOR (object);
Packit 971217
Packit 971217
  GST_DEBUG_OBJECT (object, "finalize");
Packit 971217
Packit 971217
  gst_xvcontext_unref (alloc->context);
Packit 971217
Packit 971217
  G_OBJECT_CLASS (parent_class)->finalize (object);
Packit 971217
}
Packit 971217
Packit 971217
GstXvImageAllocator *
Packit 971217
gst_xvimage_allocator_new (GstXvContext * context)
Packit 971217
{
Packit 971217
  GstXvImageAllocator *alloc;
Packit 971217
Packit 971217
  g_return_val_if_fail (GST_IS_XVCONTEXT (context), NULL);
Packit 971217
Packit 971217
  alloc = g_object_new (GST_TYPE_XVIMAGE_ALLOCATOR, NULL);
Packit 971217
  alloc->context = gst_xvcontext_ref (context);
Packit 971217
  gst_object_ref_sink (alloc);
Packit 971217
Packit 971217
  return alloc;
Packit 971217
}
Packit 971217
Packit 971217
GstXvContext *
Packit 971217
gst_xvimage_allocator_peek_context (GstXvImageAllocator * allocator)
Packit 971217
{
Packit 971217
  g_return_val_if_fail (GST_IS_XVIMAGE_ALLOCATOR (allocator), NULL);
Packit 971217
Packit 971217
  return allocator->context;
Packit 971217
}
Packit 971217
Packit 971217
GstMemory *
Packit 971217
gst_xvimage_allocator_alloc (GstXvImageAllocator * allocator, gint im_format,
Packit 971217
    const GstVideoInfo * info, gint padded_width, gint padded_height,
Packit 971217
    const GstVideoRectangle * crop, GError ** error)
Packit 971217
{
Packit 971217
  int (*handler) (Display *, XErrorEvent *);
Packit 971217
  gboolean success = FALSE;
Packit 971217
  GstXvContext *context;
Packit 971217
  gint align, offset;
Packit 971217
  GstXvImageMemory *mem;
Packit 971217
#ifdef HAVE_XSHM
Packit 971217
  gint expected_size = 0;
Packit 971217
#endif
Packit 971217
Packit 971217
  context = allocator->context;
Packit 971217
Packit 971217
  mem = g_slice_new (GstXvImageMemory);
Packit 971217
Packit 971217
  mem->im_format = im_format;
Packit 971217
  mem->info = *info;
Packit 971217
#ifdef HAVE_XSHM
Packit 971217
  mem->SHMInfo.shmaddr = ((void *) -1);
Packit 971217
  mem->SHMInfo.shmid = -1;
Packit 971217
#endif
Packit 971217
  mem->crop = *crop;
Packit 971217
Packit 971217
  GST_DEBUG_OBJECT (allocator, "creating image %p (%dx%d) cropped %dx%d-%dx%d",
Packit 971217
      mem, padded_width, padded_height, crop->x, crop->y, crop->w, crop->h);
Packit 971217
Packit 971217
  g_mutex_lock (&context->lock);
Packit 971217
Packit 971217
  /* Setting an error handler to catch failure */
Packit 971217
  error_caught = FALSE;
Packit 971217
  handler = XSetErrorHandler (gst_xvimage_handle_xerror);
Packit 971217
Packit 971217
#ifdef HAVE_XSHM
Packit 971217
  if (context->use_xshm) {
Packit 971217
Packit 971217
    mem->xvimage = XvShmCreateImage (context->disp,
Packit 971217
        context->xv_port_id, im_format, NULL, padded_width, padded_height,
Packit 971217
        &mem->SHMInfo);
Packit 971217
    if (!mem->xvimage || error_caught) {
Packit 971217
      g_mutex_unlock (&context->lock);
Packit 971217
Packit 971217
      /* Reset error flag */
Packit 971217
      error_caught = FALSE;
Packit 971217
Packit 971217
      /* Push a warning */
Packit 971217
      GST_WARNING_OBJECT (allocator,
Packit 971217
          "could not XShmCreateImage a %dx%d image", padded_width,
Packit 971217
          padded_height);
Packit 971217
Packit 971217
      /* Retry without XShm */
Packit 971217
      context->use_xshm = FALSE;
Packit 971217
Packit 971217
      /* Hold X mutex again to try without XShm */
Packit 971217
      g_mutex_lock (&context->lock);
Packit 971217
      goto no_xshm;
Packit 971217
    }
Packit 971217
Packit 971217
    /* we have to use the returned data_size for our shm size */
Packit 971217
    GST_LOG_OBJECT (allocator, "XShm image size is %d",
Packit 971217
        mem->xvimage->data_size);
Packit 971217
Packit 971217
    /* calculate the expected size.  This is only for sanity checking the
Packit 971217
     * number we get from X. */
Packit 971217
    if (GST_VIDEO_FORMAT_INFO_IS_YUV (info->finfo)) {
Packit 971217
      switch (GST_VIDEO_FORMAT_INFO_FORMAT (info->finfo)) {
Packit 971217
        case GST_VIDEO_FORMAT_I420:
Packit 971217
        case GST_VIDEO_FORMAT_YV12:
Packit 971217
        {
Packit 971217
          gint pitches[3];
Packit 971217
          gint offsets[3];
Packit 971217
          guint plane;
Packit 971217
Packit 971217
          offsets[0] = 0;
Packit 971217
          pitches[0] = GST_ROUND_UP_4 (padded_width);
Packit 971217
          offsets[1] = offsets[0] + pitches[0] * GST_ROUND_UP_2 (padded_height);
Packit 971217
          pitches[1] = GST_ROUND_UP_8 (padded_width) / 2;
Packit 971217
          offsets[2] =
Packit 971217
              offsets[1] + pitches[1] * GST_ROUND_UP_2 (padded_height) / 2;
Packit 971217
          pitches[2] = GST_ROUND_UP_8 (pitches[0]) / 2;
Packit 971217
Packit 971217
          expected_size =
Packit 971217
              offsets[2] + pitches[2] * GST_ROUND_UP_2 (padded_height) / 2;
Packit 971217
Packit 971217
          for (plane = 0; plane < mem->xvimage->num_planes; plane++) {
Packit 971217
            GST_DEBUG_OBJECT (allocator,
Packit 971217
                "Plane %u has a expected pitch of %d bytes, " "offset of %d",
Packit 971217
                plane, pitches[plane], offsets[plane]);
Packit 971217
          }
Packit 971217
          break;
Packit 971217
        }
Packit 971217
        case GST_VIDEO_FORMAT_YUY2:
Packit 971217
        case GST_VIDEO_FORMAT_UYVY:
Packit 971217
          expected_size = padded_height * GST_ROUND_UP_4 (padded_width * 2);
Packit 971217
          break;
Packit 971217
        default:
Packit 971217
          break;
Packit 971217
      }
Packit 971217
    } else if (GST_VIDEO_FORMAT_INFO_IS_RGB (info->finfo) &&
Packit 971217
        GST_VIDEO_FORMAT_INFO_N_PLANES (info->finfo) == 1) {
Packit 971217
      expected_size = padded_height * GST_ROUND_UP_4 (padded_width *
Packit 971217
          GST_VIDEO_FORMAT_INFO_PSTRIDE (info->finfo, 0));
Packit 971217
    }
Packit 971217
    if (expected_size != 0 && mem->xvimage->data_size < expected_size)
Packit 971217
      goto unexpected_size;
Packit 971217
Packit 971217
    /* Be verbose about our XvImage stride */
Packit 971217
    {
Packit 971217
      guint plane;
Packit 971217
Packit 971217
      for (plane = 0; plane < mem->xvimage->num_planes; plane++) {
Packit 971217
        GST_DEBUG_OBJECT (allocator, "Plane %u has a pitch of %d bytes, "
Packit 971217
            "offset of %d", plane, mem->xvimage->pitches[plane],
Packit 971217
            mem->xvimage->offsets[plane]);
Packit 971217
      }
Packit 971217
    }
Packit 971217
Packit 971217
    /* get shared memory */
Packit 971217
    align = 0;
Packit 971217
    mem->SHMInfo.shmid =
Packit 971217
        shmget (IPC_PRIVATE, mem->xvimage->data_size, IPC_CREAT | 0777);
Packit 971217
    if (mem->SHMInfo.shmid == -1)
Packit 971217
      goto shmget_failed;
Packit 971217
Packit 971217
    /* attach */
Packit 971217
    mem->SHMInfo.shmaddr = shmat (mem->SHMInfo.shmid, NULL, 0);
Packit 971217
    if (mem->SHMInfo.shmaddr == ((void *) -1))
Packit 971217
      goto shmat_failed;
Packit 971217
Packit 971217
    /* now we can set up the image data */
Packit 971217
    mem->xvimage->data = mem->SHMInfo.shmaddr;
Packit 971217
    mem->SHMInfo.readOnly = FALSE;
Packit 971217
Packit 971217
    if (XShmAttach (context->disp, &mem->SHMInfo) == 0)
Packit 971217
      goto xattach_failed;
Packit 971217
Packit 971217
    XSync (context->disp, FALSE);
Packit 971217
Packit 971217
    /* Delete the shared memory segment as soon as we everyone is attached.
Packit 971217
     * This way, it will be deleted as soon as we detach later, and not
Packit 971217
     * leaked if we crash. */
Packit 971217
    shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
Packit 971217
Packit 971217
    GST_DEBUG_OBJECT (allocator, "XServer ShmAttached to 0x%x, id 0x%lx",
Packit 971217
        mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
Packit 971217
  } else
Packit 971217
  no_xshm:
Packit 971217
#endif /* HAVE_XSHM */
Packit 971217
  {
Packit 971217
    mem->xvimage = XvCreateImage (context->disp,
Packit 971217
        context->xv_port_id, im_format, NULL, padded_width, padded_height);
Packit 971217
    if (!mem->xvimage || error_caught)
Packit 971217
      goto create_failed;
Packit 971217
Packit 971217
    /* we have to use the returned data_size for our image size */
Packit 971217
    align = 15;                 /* g_malloc aligns to 8, we need 16 */
Packit 971217
    mem->xvimage->data = g_malloc (mem->xvimage->data_size + align);
Packit 971217
Packit 971217
    XSync (context->disp, FALSE);
Packit 971217
  }
Packit 971217
Packit 971217
  if ((offset = ((guintptr) mem->xvimage->data & align)))
Packit 971217
    offset = (align + 1) - offset;
Packit 971217
Packit 971217
  GST_DEBUG_OBJECT (allocator, "memory %p, align %d, offset %d",
Packit 971217
      mem->xvimage->data, align, offset);
Packit 971217
Packit 971217
  /* Reset error handler */
Packit 971217
  error_caught = FALSE;
Packit 971217
  XSetErrorHandler (handler);
Packit 971217
Packit 971217
  gst_memory_init (GST_MEMORY_CAST (mem), 0,
Packit 971217
      GST_ALLOCATOR_CAST (allocator), NULL, mem->xvimage->data_size + align,
Packit 971217
      align, offset, mem->xvimage->data_size);
Packit 971217
Packit 971217
  g_mutex_unlock (&context->lock);
Packit 971217
Packit 971217
  success = TRUE;
Packit 971217
Packit 971217
beach:
Packit 971217
  if (!success) {
Packit 971217
    g_slice_free (GstXvImageMemory, mem);
Packit 971217
    mem = NULL;
Packit 971217
  }
Packit 971217
Packit 971217
  return GST_MEMORY_CAST (mem);
Packit 971217
Packit 971217
  /* ERRORS */
Packit 971217
create_failed:
Packit 971217
  {
Packit 971217
    g_mutex_unlock (&context->lock);
Packit 971217
    /* Reset error handler */
Packit 971217
    error_caught = FALSE;
Packit 971217
    XSetErrorHandler (handler);
Packit 971217
    /* Push an error */
Packit 971217
    g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
Packit 971217
        "could not XvShmCreateImage a %dx%d image", padded_width,
Packit 971217
        padded_height);
Packit 971217
    goto beach;
Packit 971217
  }
Packit 971217
#ifdef HAVE_XSHM
Packit 971217
unexpected_size:
Packit 971217
  {
Packit 971217
    g_mutex_unlock (&context->lock);
Packit 971217
    g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
Packit 971217
        "unexpected XShm image size (got %d, expected %d)",
Packit 971217
        mem->xvimage->data_size, expected_size);
Packit 971217
    goto beach;
Packit 971217
  }
Packit 971217
shmget_failed:
Packit 971217
  {
Packit 971217
    g_mutex_unlock (&context->lock);
Packit 971217
    g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
Packit 971217
        "could not get shared memory of %d bytes", mem->xvimage->data_size);
Packit 971217
    goto beach;
Packit 971217
  }
Packit 971217
shmat_failed:
Packit 971217
  {
Packit 971217
    g_mutex_unlock (&context->lock);
Packit 971217
    g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
Packit 971217
        "Failed to shmat: %s", g_strerror (errno));
Packit 971217
    /* Clean up the shared memory segment */
Packit 971217
    shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
Packit 971217
    goto beach;
Packit 971217
  }
Packit 971217
xattach_failed:
Packit 971217
  {
Packit 971217
    /* Clean up the shared memory segment */
Packit 971217
    shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
Packit 971217
    g_mutex_unlock (&context->lock);
Packit 971217
Packit 971217
    g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
Packit 971217
        "Failed to XShmAttach");
Packit 971217
    goto beach;
Packit 971217
  }
Packit 971217
#endif
Packit 971217
}
Packit 971217
Packit 971217
/* We are called with the x_lock taken */
Packit 971217
static void
Packit 971217
gst_xwindow_draw_borders (GstXWindow * window, GstVideoRectangle * rect)
Packit 971217
{
Packit 971217
  gint t1, t2;
Packit 971217
  GstXvContext *context;
Packit 971217
Packit 971217
  g_return_if_fail (window != NULL);
Packit 971217
  g_return_if_fail (rect != NULL);
Packit 971217
Packit 971217
  context = window->context;
Packit 971217
Packit 971217
  XSetForeground (context->disp, window->gc, context->black);
Packit 971217
Packit 971217
  /* Left border */
Packit 971217
  if (rect->x > window->render_rect.x) {
Packit 971217
    XFillRectangle (context->disp, window->win, window->gc,
Packit 971217
        window->render_rect.x, window->render_rect.y,
Packit 971217
        rect->x - window->render_rect.x, window->render_rect.h);
Packit 971217
  }
Packit 971217
Packit 971217
  /* Right border */
Packit 971217
  t1 = rect->x + rect->w;
Packit 971217
  t2 = window->render_rect.x + window->render_rect.w;
Packit 971217
  if (t1 < t2) {
Packit 971217
    XFillRectangle (context->disp, window->win, window->gc,
Packit 971217
        t1, window->render_rect.y, t2 - t1, window->render_rect.h);
Packit 971217
  }
Packit 971217
Packit 971217
  /* Top border */
Packit 971217
  if (rect->y > window->render_rect.y) {
Packit 971217
    XFillRectangle (context->disp, window->win, window->gc,
Packit 971217
        window->render_rect.x, window->render_rect.y,
Packit 971217
        window->render_rect.w, rect->y - window->render_rect.y);
Packit 971217
  }
Packit 971217
Packit 971217
  /* Bottom border */
Packit 971217
  t1 = rect->y + rect->h;
Packit 971217
  t2 = window->render_rect.y + window->render_rect.h;
Packit 971217
  if (t1 < t2) {
Packit 971217
    XFillRectangle (context->disp, window->win, window->gc,
Packit 971217
        window->render_rect.x, t1, window->render_rect.w, t2 - t1);
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
void
Packit 971217
gst_xvimage_memory_render (GstXvImageMemory * mem, GstVideoRectangle * src_crop,
Packit 971217
    GstXWindow * window, GstVideoRectangle * dst_crop, gboolean draw_border)
Packit 971217
{
Packit 971217
  GstXvContext *context;
Packit 971217
  XvImage *xvimage;
Packit 971217
Packit 971217
  context = window->context;
Packit 971217
Packit 971217
  g_mutex_lock (&context->lock);
Packit 971217
  xvimage = gst_xvimage_memory_get_xvimage (mem);
Packit 971217
Packit 971217
  if (draw_border) {
Packit 971217
    gst_xwindow_draw_borders (window, dst_crop);
Packit 971217
  }
Packit 971217
#ifdef HAVE_XSHM
Packit 971217
  if (context->use_xshm) {
Packit 971217
    GST_LOG ("XvShmPutImage with image %dx%d and window %dx%d, from xvimage %p",
Packit 971217
        src_crop->w, src_crop->h, window->render_rect.w, window->render_rect.h,
Packit 971217
        mem);
Packit 971217
Packit 971217
    XvShmPutImage (context->disp,
Packit 971217
        context->xv_port_id,
Packit 971217
        window->win,
Packit 971217
        window->gc, xvimage,
Packit 971217
        src_crop->x, src_crop->y, src_crop->w, src_crop->h,
Packit 971217
        dst_crop->x, dst_crop->y, dst_crop->w, dst_crop->h, FALSE);
Packit 971217
  } else
Packit 971217
#endif /* HAVE_XSHM */
Packit 971217
  {
Packit 971217
    XvPutImage (context->disp,
Packit 971217
        context->xv_port_id,
Packit 971217
        window->win,
Packit 971217
        window->gc, xvimage,
Packit 971217
        src_crop->x, src_crop->y, src_crop->w, src_crop->h,
Packit 971217
        dst_crop->x, dst_crop->y, dst_crop->w, dst_crop->h);
Packit 971217
  }
Packit 971217
  XSync (context->disp, FALSE);
Packit 971217
Packit 971217
  g_mutex_unlock (&context->lock);
Packit 971217
}