2 * Copyright (C) <2005> Julien Moutte <julien@moutte.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
25 #include <sys/types.h>
28 #endif /* HAVE_XSHM */
31 #include <X11/Xutil.h>
34 #include <X11/extensions/XShm.h>
35 #endif /* HAVE_XSHM */
41 #include "xvimageallocator.h"
43 /* Debugging category */
44 #include <gst/gstinfo.h>
46 /* Helper functions */
47 #include <gst/video/video.h>
50 GST_DEBUG_CATEGORY_STATIC (gst_debug_xvimageallocator);
51 #define GST_CAT_DEFAULT gst_debug_xvimageallocator
53 struct _GstXvImageMemory
58 GstVideoRectangle crop;
63 XShmSegmentInfo SHMInfo;
64 #endif /* HAVE_XSHM */
68 struct _GstXvImageAllocator
72 GstXvContext *context;
75 struct _GstXvImageAllocatorClass
77 GstAllocatorClass parent_class;
81 gst_xvimage_memory_is_from_context (GstMemory * mem, GstXvContext * context)
83 GstXvImageAllocator *alloc;
85 if (!GST_IS_XVIMAGE_ALLOCATOR (mem->allocator))
88 alloc = GST_XVIMAGE_ALLOCATOR_CAST (mem->allocator);
90 if (alloc->context != context)
97 gst_xvimage_memory_get_format (GstXvImageMemory * xvmem)
99 g_return_val_if_fail (xvmem != NULL, FALSE);
101 return xvmem->im_format;
105 gst_xvimage_memory_get_xvimage (GstXvImageMemory * xvmem)
107 g_return_val_if_fail (xvmem != NULL, FALSE);
109 return xvmem->xvimage;
113 gst_xvimage_memory_get_crop (GstXvImageMemory * xvmem, GstVideoRectangle * crop)
115 g_return_val_if_fail (xvmem != NULL, FALSE);
125 static gboolean error_caught = FALSE;
128 gst_xvimage_handle_xerror (Display * display, XErrorEvent * xevent)
130 char error_msg[1024];
132 XGetErrorText (display, xevent->error_code, error_msg, 1024);
133 GST_DEBUG ("xvimage triggered an XError. error: %s", error_msg);
139 gst_xvimage_allocator_dummy_alloc (GstAllocator * allocator, gsize size,
140 GstAllocationParams * params)
146 gst_xvimage_allocator_free (GstAllocator * allocator, GstMemory * gmem)
148 GstXvImageMemory *mem = (GstXvImageMemory *) gmem;
149 GstXvImageAllocator *alloc = (GstXvImageAllocator *) allocator;
150 GstXvContext *context;
155 context = alloc->context;
157 GST_DEBUG_OBJECT (allocator, "free memory %p", mem);
159 g_mutex_lock (&context->lock);
162 if (context->use_xshm) {
163 if (mem->SHMInfo.shmaddr != ((void *) -1)) {
164 GST_DEBUG_OBJECT (allocator, "XServer ShmDetaching from 0x%x id 0x%lx",
165 mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
166 XShmDetach (context->disp, &mem->SHMInfo);
167 XSync (context->disp, FALSE);
168 shmdt (mem->SHMInfo.shmaddr);
169 mem->SHMInfo.shmaddr = (void *) -1;
172 XFree (mem->xvimage);
174 #endif /* HAVE_XSHM */
177 g_free (mem->xvimage->data);
178 XFree (mem->xvimage);
182 XSync (context->disp, FALSE);
184 g_mutex_unlock (&context->lock);
187 g_slice_free (GstXvImageMemory, mem);
191 gst_xvimage_memory_map (GstXvImageMemory * mem, gsize maxsize,
194 return mem->xvimage->data + mem->parent.offset;
198 gst_xvimage_memory_unmap (GstXvImageMemory * mem)
203 static GstXvImageMemory *
204 gst_xvimage_memory_share (GstXvImageMemory * mem, gssize offset, gsize size)
206 GstXvImageMemory *sub;
209 /* We can only share the complete memory */
212 if (size != -1 && size != mem->xvimage->data_size)
215 GST_DEBUG ("share memory %p", mem);
217 /* find the real parent */
218 if ((parent = mem->parent.parent) == NULL)
219 parent = (GstMemory *) mem;
222 size = mem->parent.size - offset;
224 /* the shared memory is always readonly */
225 sub = g_slice_new (GstXvImageMemory);
227 gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
228 GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->parent.allocator,
229 &mem->parent, mem->parent.maxsize, mem->parent.align,
230 mem->parent.offset + offset, size);
232 sub->im_format = mem->im_format;
233 sub->crop = mem->crop;
234 sub->xvimage = mem->xvimage;
236 sub->SHMInfo = mem->SHMInfo;
242 static GstXvImageMemory *
243 gst_xvimage_memory_copy (GstMemory * gmem, gssize offset, gsize size)
245 GstXvImageMemory *mem, *copy;
247 mem = (GstXvImageMemory *) gmem;
249 /* We can only copy the complete memory */
252 if (size != -1 && size != mem->xvimage->data_size)
255 GST_DEBUG ("copy memory %p", mem);
257 copy = (GstXvImageMemory *)
258 gst_xvimage_allocator_alloc (GST_XVIMAGE_ALLOCATOR_CAST (gmem->allocator),
259 mem->im_format, mem->xvimage->width, mem->xvimage->height, &mem->crop,
262 memcpy (copy->xvimage->data + copy->parent.offset,
263 mem->xvimage->data + mem->parent.offset, mem->xvimage->data_size);
268 #define gst_xvimage_allocator_parent_class parent_class
269 G_DEFINE_TYPE (GstXvImageAllocator, gst_xvimage_allocator, GST_TYPE_ALLOCATOR);
271 static void gst_xvimage_allocator_finalize (GObject * object);
273 #define GST_XVIMAGE_ALLOCATOR_NAME "xvimage"
276 gst_xvimage_allocator_class_init (GstXvImageAllocatorClass * klass)
278 GObjectClass *gobject_class;
279 GstAllocatorClass *allocator_class;
281 gobject_class = (GObjectClass *) klass;
282 allocator_class = (GstAllocatorClass *) klass;
284 gobject_class->finalize = gst_xvimage_allocator_finalize;
286 allocator_class->alloc = gst_xvimage_allocator_dummy_alloc;
287 allocator_class->free = gst_xvimage_allocator_free;
289 GST_DEBUG_CATEGORY_INIT (gst_debug_xvimageallocator, "xvimageallocator", 0,
290 "xvimageallocator object");
294 gst_xvimage_allocator_init (GstXvImageAllocator * allocator)
296 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
298 alloc->mem_type = GST_XVIMAGE_ALLOCATOR_NAME;
299 alloc->mem_map = (GstMemoryMapFunction) gst_xvimage_memory_map;
300 alloc->mem_unmap = (GstMemoryUnmapFunction) gst_xvimage_memory_unmap;
301 alloc->mem_share = (GstMemoryShareFunction) gst_xvimage_memory_share;
302 alloc->mem_copy = (GstMemoryShareFunction) gst_xvimage_memory_copy;
303 /* fallback is_span */
305 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
309 gst_xvimage_allocator_finalize (GObject * object)
311 GstXvImageAllocator *alloc = GST_XVIMAGE_ALLOCATOR (object);
313 GST_DEBUG_OBJECT (object, "finalize");
315 gst_xvcontext_unref (alloc->context);
317 G_OBJECT_CLASS (parent_class)->finalize (object);
320 GstXvImageAllocator *
321 gst_xvimage_allocator_new (GstXvContext * context)
323 GstXvImageAllocator *alloc;
325 g_return_val_if_fail (GST_IS_XVCONTEXT (context), NULL);
327 alloc = g_object_new (GST_TYPE_XVIMAGE_ALLOCATOR, NULL);
328 alloc->context = gst_xvcontext_ref (context);
334 gst_xvimage_allocator_peek_context (GstXvImageAllocator * allocator)
336 g_return_val_if_fail (GST_IS_XVIMAGE_ALLOCATOR (allocator), NULL);
338 return allocator->context;
342 gst_xvimage_allocator_alloc (GstXvImageAllocator * allocator, gint im_format,
343 gint padded_width, gint padded_height, GstVideoRectangle * crop,
346 int (*handler) (Display *, XErrorEvent *);
347 gboolean success = FALSE;
348 GstXvContext *context;
349 gint align = 15, offset;
350 GstXvImageMemory *mem;
352 context = allocator->context;
354 mem = g_slice_new (GstXvImageMemory);
356 mem->im_format = im_format;
358 mem->SHMInfo.shmaddr = ((void *) -1);
359 mem->SHMInfo.shmid = -1;
363 GST_DEBUG_OBJECT (allocator, "creating image %p (%dx%d) cropped %dx%d-%dx%d",
364 mem, padded_width, padded_height, crop->x, crop->y, crop->w, crop->h);
366 g_mutex_lock (&context->lock);
368 /* Setting an error handler to catch failure */
369 error_caught = FALSE;
370 handler = XSetErrorHandler (gst_xvimage_handle_xerror);
373 if (context->use_xshm) {
376 mem->xvimage = XvShmCreateImage (context->disp,
377 context->xv_port_id, im_format, NULL, padded_width, padded_height,
379 if (!mem->xvimage || error_caught) {
380 g_mutex_unlock (&context->lock);
382 /* Reset error flag */
383 error_caught = FALSE;
386 GST_WARNING_OBJECT (allocator,
387 "could not XShmCreateImage a %dx%d image", padded_width,
390 /* Retry without XShm */
391 context->use_xshm = FALSE;
393 /* Hold X mutex again to try without XShm */
394 g_mutex_lock (&context->lock);
398 /* we have to use the returned data_size for our shm size */
399 GST_LOG_OBJECT (allocator, "XShm image size is %d",
400 mem->xvimage->data_size);
402 /* calculate the expected size. This is only for sanity checking the
403 * number we get from X. */
405 case GST_MAKE_FOURCC ('I', '4', '2', '0'):
406 case GST_MAKE_FOURCC ('Y', 'V', '1', '2'):
413 pitches[0] = GST_ROUND_UP_4 (padded_width);
414 offsets[1] = offsets[0] + pitches[0] * GST_ROUND_UP_2 (padded_height);
415 pitches[1] = GST_ROUND_UP_8 (padded_width) / 2;
417 offsets[1] + pitches[1] * GST_ROUND_UP_2 (padded_height) / 2;
418 pitches[2] = GST_ROUND_UP_8 (pitches[0]) / 2;
421 offsets[2] + pitches[2] * GST_ROUND_UP_2 (padded_height) / 2;
423 for (plane = 0; plane < mem->xvimage->num_planes; plane++) {
424 GST_DEBUG_OBJECT (allocator,
425 "Plane %u has a expected pitch of %d bytes, " "offset of %d",
426 plane, pitches[plane], offsets[plane]);
430 case GST_MAKE_FOURCC ('Y', 'U', 'Y', '2'):
431 case GST_MAKE_FOURCC ('U', 'Y', 'V', 'Y'):
432 expected_size = padded_height * GST_ROUND_UP_4 (padded_width * 2);
438 if (expected_size != 0 && mem->xvimage->data_size != expected_size) {
439 GST_WARNING_OBJECT (allocator,
440 "unexpected XShm image size (got %" G_GSIZE_FORMAT ", expected %d)",
441 mem->xvimage->data_size, expected_size);
444 /* Be verbose about our XvImage stride */
448 for (plane = 0; plane < mem->xvimage->num_planes; plane++) {
449 GST_DEBUG_OBJECT (allocator, "Plane %u has a pitch of %d bytes, "
450 "offset of %d", plane, mem->xvimage->pitches[plane],
451 mem->xvimage->offsets[plane]);
455 /* get shared memory */
457 shmget (IPC_PRIVATE, mem->xvimage->data_size + align, IPC_CREAT | 0777);
458 if (mem->SHMInfo.shmid == -1)
462 mem->SHMInfo.shmaddr = shmat (mem->SHMInfo.shmid, NULL, 0);
463 if (mem->SHMInfo.shmaddr == ((void *) -1))
466 /* now we can set up the image data */
467 mem->xvimage->data = mem->SHMInfo.shmaddr;
468 mem->SHMInfo.readOnly = FALSE;
470 if (XShmAttach (context->disp, &mem->SHMInfo) == 0)
473 XSync (context->disp, FALSE);
475 /* Delete the shared memory segment as soon as we everyone is attached.
476 * This way, it will be deleted as soon as we detach later, and not
477 * leaked if we crash. */
478 shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
480 GST_DEBUG_OBJECT (allocator, "XServer ShmAttached to 0x%x, id 0x%lx",
481 mem->SHMInfo.shmid, mem->SHMInfo.shmseg);
484 #endif /* HAVE_XSHM */
486 mem->xvimage = XvCreateImage (context->disp,
487 context->xv_port_id, im_format, NULL, padded_width, padded_height);
488 if (!mem->xvimage || error_caught)
491 /* we have to use the returned data_size for our image size */
492 mem->xvimage->data = g_malloc (mem->xvimage->data_size + align);
494 XSync (context->disp, FALSE);
497 if ((offset = ((guintptr) mem->xvimage->data & align)))
498 offset = (align + 1) - offset;
500 GST_DEBUG_OBJECT (allocator, "memory %p, align %d, offset %d",
501 mem->xvimage->data, align, offset);
503 /* Reset error handler */
504 error_caught = FALSE;
505 XSetErrorHandler (handler);
507 gst_memory_init (GST_MEMORY_CAST (mem), 0,
508 GST_ALLOCATOR_CAST (allocator), NULL, mem->xvimage->data_size + align,
509 align, offset, mem->xvimage->data_size);
511 g_mutex_unlock (&context->lock);
517 g_slice_free (GstXvImageMemory, mem);
521 return GST_MEMORY_CAST (mem);
526 g_mutex_unlock (&context->lock);
527 /* Reset error handler */
528 error_caught = FALSE;
529 XSetErrorHandler (handler);
531 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
532 "could not XvShmCreateImage a %dx%d image", padded_width,
539 g_mutex_unlock (&context->lock);
540 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
541 "could not get shared memory of %d bytes", mem->xvimage->data_size);
546 g_mutex_unlock (&context->lock);
547 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
548 "Failed to shmat: %s", g_strerror (errno));
549 /* Clean up the shared memory segment */
550 shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
555 /* Clean up the shared memory segment */
556 shmctl (mem->SHMInfo.shmid, IPC_RMID, NULL);
557 g_mutex_unlock (&context->lock);
559 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_WRITE,
560 "Failed to XShmAttach");
566 /* We are called with the x_lock taken */
568 gst_xwindow_draw_borders (GstXWindow * window, GstVideoRectangle * rect)
571 GstXvContext *context;
573 g_return_if_fail (window != NULL);
574 g_return_if_fail (rect != NULL);
576 context = window->context;
578 XSetForeground (context->disp, window->gc, context->black);
581 if (rect->x > window->render_rect.x) {
582 XFillRectangle (context->disp, window->win, window->gc,
583 window->render_rect.x, window->render_rect.y,
584 rect->x - window->render_rect.x, window->render_rect.h);
588 t1 = rect->x + rect->w;
589 t2 = window->render_rect.x + window->render_rect.w;
591 XFillRectangle (context->disp, window->win, window->gc,
592 t1, window->render_rect.y, t2 - t1, window->render_rect.h);
596 if (rect->y > window->render_rect.y) {
597 XFillRectangle (context->disp, window->win, window->gc,
598 window->render_rect.x, window->render_rect.y,
599 window->render_rect.w, rect->y - window->render_rect.y);
603 t1 = rect->y + rect->h;
604 t2 = window->render_rect.y + window->render_rect.h;
606 XFillRectangle (context->disp, window->win, window->gc,
607 window->render_rect.x, t1, window->render_rect.w, t2 - t1);
612 gst_xvimage_memory_render (GstXvImageMemory * mem, GstVideoRectangle * src_crop,
613 GstXWindow * window, GstVideoRectangle * dst_crop, gboolean draw_border)
615 GstXvContext *context;
618 context = window->context;
620 g_mutex_lock (&context->lock);
621 xvimage = gst_xvimage_memory_get_xvimage (mem);
624 gst_xwindow_draw_borders (window, dst_crop);
627 if (context->use_xshm) {
628 GST_LOG ("XvShmPutImage with image %dx%d and window %dx%d, from xvimage %"
629 GST_PTR_FORMAT, src_crop->w, src_crop->h,
630 window->render_rect.w, window->render_rect.h, mem);
632 XvShmPutImage (context->disp,
636 src_crop->x, src_crop->y, src_crop->w, src_crop->h,
637 dst_crop->x, dst_crop->y, dst_crop->w, dst_crop->h, FALSE);
639 #endif /* HAVE_XSHM */
641 XvPutImage (context->disp,
645 src_crop->x, src_crop->y, src_crop->w, src_crop->h,
646 dst_crop->x, dst_crop->y, dst_crop->w, dst_crop->h);
648 XSync (context->disp, FALSE);
650 g_mutex_unlock (&context->lock);