Blame gst-libs/gst/gl/gstglbasememory.c

Packit 971217
/*
Packit 971217
 * GStreamer
Packit 971217
 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
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
#include <string.h>
Packit 971217
Packit 971217
#include "gstglbasememory.h"
Packit 971217
Packit 971217
#include "gstglcontext.h"
Packit 971217
#include "gstglquery.h"
Packit 971217
Packit 971217
/**
Packit 971217
 * SECTION:gstglbasememory
Packit 971217
 * @title: GstGLBaseMemory
Packit 971217
 * @short_description: memory subclass for GL buffers
Packit 971217
 * @see_also: #GstMemory, #GstAllocator
Packit 971217
 *
Packit 971217
 * GstGLBaseMemory is a #GstMemory subclass providing the basis of support
Packit 971217
 * for the mapping of GL buffers.
Packit 971217
 *
Packit 971217
 * Data is uploaded or downloaded from the GPU as is necessary.
Packit 971217
 */
Packit 971217
Packit 971217
#define USING_OPENGL(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL, 1, 0))
Packit 971217
#define USING_OPENGL3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 1))
Packit 971217
#define USING_GLES(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES, 1, 0))
Packit 971217
#define USING_GLES2(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 2, 0))
Packit 971217
#define USING_GLES3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0))
Packit 971217
Packit 971217
GST_DEBUG_CATEGORY_STATIC (GST_CAT_GL_BASE_MEMORY);
Packit 971217
#define GST_CAT_DEFUALT GST_CAT_GL_BASE_MEMORY
Packit 971217
Packit 971217
GST_DEFINE_MINI_OBJECT_TYPE (GstGLBaseMemory, gst_gl_base_memory);
Packit 971217
Packit 971217
GQuark
Packit 971217
gst_gl_base_memory_error_quark (void)
Packit 971217
{
Packit 971217
  return g_quark_from_static_string ("gst-gl-base-buffer-error-quark");
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
_default_create (GstGLBaseMemory * mem, GError ** error)
Packit 971217
{
Packit 971217
  g_set_error (error, GST_GL_BASE_MEMORY_ERROR, GST_GL_BASE_MEMORY_ERROR_FAILED,
Packit 971217
      "subclass should define create() vfunc");
Packit 971217
Packit 971217
  g_critical ("subclass should override "
Packit 971217
      "GstGLBaseMemoryAllocatorClass::create() function");
Packit 971217
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
struct create_data
Packit 971217
{
Packit 971217
  GstGLBaseMemory *mem;
Packit 971217
  gboolean result;
Packit 971217
};
Packit 971217
Packit 971217
static void
Packit 971217
_mem_create_gl (GstGLContext * context, struct create_data *transfer)
Packit 971217
{
Packit 971217
  GstGLBaseMemoryAllocatorClass *alloc_class;
Packit 971217
  GError *error = NULL;
Packit 971217
Packit 971217
  GST_CAT_TRACE (GST_CAT_GL_BASE_MEMORY, "Create memory %p", transfer->mem);
Packit 971217
Packit 971217
  alloc_class =
Packit 971217
      GST_GL_BASE_MEMORY_ALLOCATOR_GET_CLASS (transfer->mem->mem.allocator);
Packit 971217
Packit 971217
  g_return_if_fail (alloc_class->create != NULL);
Packit 971217
Packit 971217
  transfer->mem->query = gst_gl_query_new (context, GST_GL_QUERY_TIME_ELAPSED);
Packit 971217
Packit 971217
  if ((transfer->result = alloc_class->create (transfer->mem, &error)))
Packit 971217
    return;
Packit 971217
Packit 971217
  g_assert (error != NULL);
Packit 971217
Packit 971217
  GST_CAT_ERROR (GST_CAT_GL_BASE_MEMORY, "Failed to create GL buffer: %s",
Packit 971217
      error->message);
Packit 971217
  g_clear_error (&error);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_base_memory_init:
Packit 971217
 * @mem: the #GstGLBaseMemory to initialize
Packit 971217
 * @allocator: the #GstAllocator to initialize with
Packit 971217
 * @parent: (allow-none): the parent #GstMemory to initialize with
Packit 971217
 * @context: the #GstGLContext to initialize with
Packit 971217
 * @params: (allow-none): the @GstAllocationParams to initialize with
Packit 971217
 * @size: the number of bytes to be allocated
Packit 971217
 * @user_data: (allow-none): user data to call @notify with
Packit 971217
 * @notify: (allow-none): a #GDestroyNotify
Packit 971217
 *
Packit 971217
 * Initializes @mem with the required parameters
Packit 971217
 *
Packit 971217
 * Since: 1.8
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_gl_base_memory_init (GstGLBaseMemory * mem, GstAllocator * allocator,
Packit 971217
    GstMemory * parent, GstGLContext * context, GstAllocationParams * params,
Packit 971217
    gsize size, gpointer user_data, GDestroyNotify notify)
Packit 971217
{
Packit 971217
  gsize align = gst_memory_alignment, offset = 0, maxsize;
Packit 971217
  GstMemoryFlags flags = 0;
Packit 971217
  struct create_data data;
Packit 971217
Packit 971217
  /* A note on sizes.
Packit 971217
   * gl_mem->alloc_size: the size to allocate when we control the allocation.
Packit 971217
   *                     Size of the unaligned allocation.
Packit 971217
   * mem->maxsize: the size that is used by GstMemory for mapping, to map the
Packit 971217
   *               entire memory. The size of the aligned allocation
Packit 971217
   * mem->size: represents the size of the valid data. Can be reduced with
Packit 971217
   *            gst_memory_resize()
Packit 971217
   *
Packit 971217
   * It holds that:
Packit 971217
   * mem->size + mem->offset <= mem->maxsize
Packit 971217
   * and
Packit 971217
   * mem->maxsize + alignment offset <= gl_mem->alloc_size
Packit 971217
   *
Packit 971217
   * We need to add the alignment mask to the allocated size in order to have
Packit 971217
   * the freedom to align the gl_mem->data pointer correctly which may be offset
Packit 971217
   * by at most align bytes in the alloc_data pointer.
Packit 971217
   *
Packit 971217
   * maxsize is not suitable for this as it is used by GstMemory as the size
Packit 971217
   * to map with.
Packit 971217
   */
Packit 971217
  mem->alloc_size = maxsize = size;
Packit 971217
  if (params) {
Packit 971217
    flags = params->flags;
Packit 971217
    align |= params->align;
Packit 971217
    offset = params->prefix;
Packit 971217
    maxsize += params->prefix + params->padding;
Packit 971217
Packit 971217
    /* deals with any alignment */
Packit 971217
    mem->alloc_size = maxsize + align;
Packit 971217
  }
Packit 971217
Packit 971217
  gst_memory_init (GST_MEMORY_CAST (mem), flags, allocator, parent, maxsize,
Packit 971217
      align, offset, size);
Packit 971217
Packit 971217
  mem->context = gst_object_ref (context);
Packit 971217
  mem->notify = notify;
Packit 971217
  mem->user_data = user_data;
Packit 971217
Packit 971217
  g_mutex_init (&mem->lock);
Packit 971217
Packit 971217
  data.mem = mem;
Packit 971217
Packit 971217
  gst_gl_context_thread_add (context,
Packit 971217
      (GstGLContextThreadFunc) _mem_create_gl, &data);
Packit 971217
  if (!data.result) {
Packit 971217
    GST_CAT_ERROR (GST_CAT_GL_BASE_MEMORY,
Packit 971217
        "Could not create GL buffer with context:%p", context);
Packit 971217
  }
Packit 971217
Packit 971217
  GST_CAT_DEBUG (GST_CAT_GL_BASE_MEMORY, "new GL buffer memory:%p size:%"
Packit 971217
      G_GSIZE_FORMAT, mem, maxsize);
Packit 971217
}
Packit 971217
Packit 971217
static gpointer
Packit 971217
_align_data (gpointer data, gsize align)
Packit 971217
{
Packit 971217
  guint8 *ret = data;
Packit 971217
  gsize aoffset;
Packit 971217
Packit 971217
  /* do alignment, data must have enough padding at the end to move at most
Packit 971217
   * align bytes */
Packit 971217
  if ((aoffset = ((guintptr) ret & align))) {
Packit 971217
    aoffset = (align + 1) - aoffset;
Packit 971217
    ret += aoffset;
Packit 971217
  }
Packit 971217
Packit 971217
  return ret;
Packit 971217
}
Packit 971217
Packit 971217
/* subclass usage only */
Packit 971217
/**
Packit 971217
 * gst_gl_base_memory_alloc_data:
Packit 971217
 * @gl_mem: a #GstGLBaseMemory
Packit 971217
 *
Packit 971217
 * Note: only intended for subclass usage to allocate the sytem memory buffer
Packit 971217
 * on demand.  If there is already a non-NULL data pointer in @gl_mem->data,
Packit 971217
 * then this function imply returns TRUE.
Packit 971217
 *
Packit 971217
 * Returns: whether the system memory could be allocated
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_gl_base_memory_alloc_data (GstGLBaseMemory * gl_mem)
Packit 971217
{
Packit 971217
  GstMemory *mem = (GstMemory *) gl_mem;
Packit 971217
Packit 971217
  if (gl_mem->data)
Packit 971217
    return TRUE;
Packit 971217
Packit 971217
  GST_CAT_LOG (GST_CAT_GL_BASE_MEMORY, "%p attempting allocation of data "
Packit 971217
      "pointer of size %" G_GSIZE_FORMAT, gl_mem, gl_mem->alloc_size);
Packit 971217
  gl_mem->alloc_data = g_try_malloc (gl_mem->alloc_size);
Packit 971217
Packit 971217
  if (gl_mem->alloc_data == NULL)
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  gl_mem->data = _align_data (gl_mem->alloc_data, mem->align);
Packit 971217
Packit 971217
  GST_CAT_DEBUG (GST_CAT_GL_BASE_MEMORY, "%p allocated data pointer alloc %p, "
Packit 971217
      "data %p", gl_mem, gl_mem->alloc_data, gl_mem->data);
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
struct map_data
Packit 971217
{
Packit 971217
  GstGLBaseMemory *mem;
Packit 971217
  GstMapInfo *info;
Packit 971217
  gsize size;
Packit 971217
  gpointer data;
Packit 971217
};
Packit 971217
Packit 971217
static void
Packit 971217
_map_data_gl (GstGLContext * context, struct map_data *transfer)
Packit 971217
{
Packit 971217
  GstGLBaseMemoryAllocatorClass *alloc_class;
Packit 971217
  GstGLBaseMemory *mem = transfer->mem;
Packit 971217
  GstMapInfo *info = transfer->info;
Packit 971217
  guint prev_map_flags;
Packit 971217
  guint prev_gl_map_count;
Packit 971217
Packit 971217
  alloc_class =
Packit 971217
      GST_GL_BASE_MEMORY_ALLOCATOR_GET_CLASS (transfer->mem->mem.allocator);
Packit 971217
Packit 971217
  g_return_if_fail (alloc_class->map != NULL);
Packit 971217
Packit 971217
  g_mutex_lock (&mem->lock);
Packit 971217
Packit 971217
  prev_map_flags = mem->map_flags;
Packit 971217
  prev_gl_map_count = mem->gl_map_count;
Packit 971217
Packit 971217
  GST_CAT_LOG (GST_CAT_GL_BASE_MEMORY, "mapping mem %p flags %04x", mem,
Packit 971217
      info->flags);
Packit 971217
Packit 971217
  /* FIXME: validate map flags based on the memory domain */
Packit 971217
  if (mem->map_count++ == 0)
Packit 971217
    mem->map_flags = info->flags;
Packit 971217
  else {
Packit 971217
    /* assert that the flags are a subset of the first map flags */
Packit 971217
    g_assert ((((GST_MAP_GL - 1) & info->flags) & mem->map_flags) != 0);
Packit 971217
    GST_CAT_LOG (GST_CAT_GL_BASE_MEMORY, "multiple map no %d flags %04x "
Packit 971217
        "all flags %04x", mem->map_count, info->flags, mem->map_flags);
Packit 971217
  }
Packit 971217
Packit 971217
  if ((info->flags & GST_MAP_GL) != (mem->map_flags & GST_MAP_GL))
Packit 971217
    mem->map_flags |= GST_MAP_GL;
Packit 971217
Packit 971217
  if (info->flags & GST_MAP_GL)
Packit 971217
    mem->gl_map_count++;
Packit 971217
Packit 971217
  transfer->data = alloc_class->map (transfer->mem, transfer->info,
Packit 971217
      transfer->size);
Packit 971217
Packit 971217
  if (transfer->data) {
Packit 971217
    if (info->flags & GST_MAP_GL) {
Packit 971217
      if (info->flags & GST_MAP_WRITE)
Packit 971217
        GST_MINI_OBJECT_FLAG_SET (mem,
Packit 971217
            GST_GL_BASE_MEMORY_TRANSFER_NEED_DOWNLOAD);
Packit 971217
      GST_MEMORY_FLAG_UNSET (mem, GST_GL_BASE_MEMORY_TRANSFER_NEED_UPLOAD);
Packit 971217
    } else {
Packit 971217
      if (info->flags & GST_MAP_WRITE)
Packit 971217
        GST_MINI_OBJECT_FLAG_SET (mem, GST_GL_BASE_MEMORY_TRANSFER_NEED_UPLOAD);
Packit 971217
      GST_MEMORY_FLAG_UNSET (mem, GST_GL_BASE_MEMORY_TRANSFER_NEED_DOWNLOAD);
Packit 971217
    }
Packit 971217
  } else {
Packit 971217
    /* undo state tracking on error */
Packit 971217
    mem->map_flags = prev_map_flags;
Packit 971217
    mem->gl_map_count = prev_gl_map_count;
Packit 971217
    mem->map_count--;
Packit 971217
  }
Packit 971217
Packit 971217
  g_mutex_unlock (&mem->lock);
Packit 971217
}
Packit 971217
Packit 971217
static gpointer
Packit 971217
_mem_map_full (GstGLBaseMemory * mem, GstMapInfo * info, gsize size)
Packit 971217
{
Packit 971217
  struct map_data transfer;
Packit 971217
Packit 971217
  transfer.mem = mem;
Packit 971217
  transfer.info = info;
Packit 971217
  transfer.size = size;
Packit 971217
  transfer.data = NULL;
Packit 971217
Packit 971217
  gst_gl_context_thread_add (mem->context,
Packit 971217
      (GstGLContextThreadFunc) _map_data_gl, &transfer);
Packit 971217
Packit 971217
  return transfer.data;
Packit 971217
}
Packit 971217
Packit 971217
struct unmap_data
Packit 971217
{
Packit 971217
  GstGLBaseMemory *mem;
Packit 971217
  GstMapInfo *info;
Packit 971217
};
Packit 971217
Packit 971217
static void
Packit 971217
_unmap_data_gl (GstGLContext * context, struct unmap_data *transfer)
Packit 971217
{
Packit 971217
  GstGLBaseMemoryAllocatorClass *alloc_class;
Packit 971217
  GstGLBaseMemory *mem = transfer->mem;
Packit 971217
  GstMapInfo *info = transfer->info;
Packit 971217
Packit 971217
  alloc_class =
Packit 971217
      GST_GL_BASE_MEMORY_ALLOCATOR_GET_CLASS (transfer->mem->mem.allocator);
Packit 971217
Packit 971217
  g_return_if_fail (alloc_class->unmap != NULL);
Packit 971217
Packit 971217
  g_mutex_lock (&mem->lock);
Packit 971217
Packit 971217
  GST_CAT_LOG (GST_CAT_GL_BASE_MEMORY, "unmapping mem %p flags %04x", mem,
Packit 971217
      info->flags);
Packit 971217
Packit 971217
  alloc_class->unmap (transfer->mem, transfer->info);
Packit 971217
Packit 971217
  if (info->flags & GST_MAP_GL && --mem->gl_map_count)
Packit 971217
    /* unset the gl flag */
Packit 971217
    mem->map_flags &= ~GST_MAP_GL;
Packit 971217
Packit 971217
  if (--mem->map_count <= 0) {
Packit 971217
    mem->map_flags = 0;
Packit 971217
  }
Packit 971217
Packit 971217
  if (info->flags & GST_MAP_GL) {
Packit 971217
    if (info->flags & GST_MAP_WRITE)
Packit 971217
      GST_MINI_OBJECT_FLAG_SET (mem, GST_GL_BASE_MEMORY_TRANSFER_NEED_DOWNLOAD);
Packit 971217
  } else {
Packit 971217
    if (info->flags & GST_MAP_WRITE)
Packit 971217
      GST_MINI_OBJECT_FLAG_SET (mem, GST_GL_BASE_MEMORY_TRANSFER_NEED_UPLOAD);
Packit 971217
  }
Packit 971217
Packit 971217
  g_mutex_unlock (&mem->lock);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
_mem_unmap_full (GstGLBaseMemory * mem, GstMapInfo * info)
Packit 971217
{
Packit 971217
  struct unmap_data transfer;
Packit 971217
Packit 971217
  transfer.mem = mem;
Packit 971217
  transfer.info = info;
Packit 971217
Packit 971217
  gst_gl_context_thread_add (mem->context,
Packit 971217
      (GstGLContextThreadFunc) _unmap_data_gl, &transfer);
Packit 971217
}
Packit 971217
Packit 971217
static GstGLBaseMemory *
Packit 971217
_default_copy (GstGLBaseMemory * src, gssize offset, gssize size)
Packit 971217
{
Packit 971217
  return NULL;
Packit 971217
}
Packit 971217
Packit 971217
struct copy_params
Packit 971217
{
Packit 971217
  GstGLBaseMemory *src;
Packit 971217
  GstGLBaseMemory *dest;
Packit 971217
  gssize offset;
Packit 971217
  gssize size;
Packit 971217
  gboolean result;
Packit 971217
};
Packit 971217
Packit 971217
static void
Packit 971217
_mem_copy_gl (GstGLContext * context, struct copy_params *transfer)
Packit 971217
{
Packit 971217
  GstGLBaseMemoryAllocatorClass *alloc_class;
Packit 971217
Packit 971217
  alloc_class =
Packit 971217
      GST_GL_BASE_MEMORY_ALLOCATOR_GET_CLASS (transfer->src->mem.allocator);
Packit 971217
Packit 971217
  g_return_if_fail (alloc_class->copy != NULL);
Packit 971217
Packit 971217
  transfer->dest =
Packit 971217
      alloc_class->copy (transfer->src, transfer->offset, transfer->size);
Packit 971217
}
Packit 971217
Packit 971217
static GstMemory *
Packit 971217
_mem_copy (GstGLBaseMemory * src, gssize offset, gssize size)
Packit 971217
{
Packit 971217
  struct copy_params transfer;
Packit 971217
Packit 971217
  transfer.dest = NULL;
Packit 971217
  transfer.src = src;
Packit 971217
  transfer.offset = offset;
Packit 971217
  transfer.size = size;
Packit 971217
  if (size == -1 || size > 0)
Packit 971217
    gst_gl_context_thread_add (src->context,
Packit 971217
        (GstGLContextThreadFunc) _mem_copy_gl, &transfer);
Packit 971217
Packit 971217
  return (GstMemory *) transfer.dest;
Packit 971217
}
Packit 971217
Packit 971217
static GstMemory *
Packit 971217
_mem_share (GstGLBaseMemory * mem, gssize offset, gssize size)
Packit 971217
{
Packit 971217
  return NULL;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
_mem_is_span (GstGLBaseMemory * mem1, GstGLBaseMemory * mem2, gsize * offset)
Packit 971217
{
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
static GstMemory *
Packit 971217
_mem_alloc (GstAllocator * allocator, gsize size, GstAllocationParams * params)
Packit 971217
{
Packit 971217
  g_critical ("Subclass should override GstAllocatorClass::alloc() function");
Packit 971217
Packit 971217
  return NULL;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
_default_destroy (GstGLBaseMemory * mem)
Packit 971217
{
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
_destroy_gl_objects (GstGLContext * context, GstGLBaseMemory * mem)
Packit 971217
{
Packit 971217
  GstGLBaseMemoryAllocatorClass *alloc_class;
Packit 971217
Packit 971217
  alloc_class = GST_GL_BASE_MEMORY_ALLOCATOR_GET_CLASS (mem->mem.allocator);
Packit 971217
Packit 971217
  g_return_if_fail (alloc_class->destroy != NULL);
Packit 971217
Packit 971217
  alloc_class->destroy (mem);
Packit 971217
Packit 971217
  if (mem->query)
Packit 971217
    gst_gl_query_free (mem->query);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
_mem_free (GstAllocator * allocator, GstMemory * memory)
Packit 971217
{
Packit 971217
  GstGLBaseMemory *mem = (GstGLBaseMemory *) memory;
Packit 971217
Packit 971217
  GST_CAT_TRACE (GST_CAT_GL_BASE_MEMORY, "freeing buffer memory:%p", mem);
Packit 971217
Packit 971217
  gst_gl_context_thread_add (mem->context,
Packit 971217
      (GstGLContextThreadFunc) _destroy_gl_objects, mem);
Packit 971217
Packit 971217
  g_mutex_clear (&mem->lock);
Packit 971217
Packit 971217
  if (mem->alloc_data) {
Packit 971217
    g_free (mem->alloc_data);
Packit 971217
    mem->alloc_data = NULL;
Packit 971217
  }
Packit 971217
  mem->data = NULL;
Packit 971217
Packit 971217
  if (mem->notify)
Packit 971217
    mem->notify (mem->user_data);
Packit 971217
Packit 971217
  gst_object_unref (mem->context);
Packit 971217
Packit 971217
  g_free (memory);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_base_memory_init_once:
Packit 971217
 *
Packit 971217
 * Initializes the GL Base Memory allocator. It is safe to call this function
Packit 971217
 * multiple times.  This must be called before any other GstGLBaseMemory operation.
Packit 971217
 *
Packit 971217
 * Since: 1.8
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_gl_base_memory_init_once (void)
Packit 971217
{
Packit 971217
  static volatile gsize _init = 0;
Packit 971217
Packit 971217
  if (g_once_init_enter (&_init)) {
Packit 971217
    GST_DEBUG_CATEGORY_INIT (GST_CAT_GL_BASE_MEMORY, "glbasememory", 0,
Packit 971217
        "OpenGL BaseMemory");
Packit 971217
Packit 971217
    g_once_init_leave (&_init, 1);
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
G_DEFINE_ABSTRACT_TYPE (GstGLBaseMemoryAllocator, gst_gl_base_memory_allocator,
Packit 971217
    GST_TYPE_ALLOCATOR);
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_base_memory_allocator_class_init (GstGLBaseMemoryAllocatorClass * klass)
Packit 971217
{
Packit 971217
  GstAllocatorClass *allocator_class = (GstAllocatorClass *) klass;
Packit 971217
Packit 971217
  allocator_class->alloc = _mem_alloc;
Packit 971217
  allocator_class->free = _mem_free;
Packit 971217
Packit 971217
  klass->create = _default_create;
Packit 971217
  klass->copy = _default_copy;
Packit 971217
  klass->destroy = _default_destroy;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_base_memory_allocator_init (GstGLBaseMemoryAllocator * allocator)
Packit 971217
{
Packit 971217
  GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
Packit 971217
Packit 971217
  /* Keep the fallback copy function around, we will need it when copying with
Packit 971217
   * at an offset or smaller size */
Packit 971217
  allocator->fallback_mem_copy = alloc->mem_copy;
Packit 971217
Packit 971217
  alloc->mem_map_full = (GstMemoryMapFullFunction) _mem_map_full;
Packit 971217
  alloc->mem_unmap_full = (GstMemoryUnmapFullFunction) _mem_unmap_full;
Packit 971217
  alloc->mem_copy = (GstMemoryCopyFunction) _mem_copy;
Packit 971217
  alloc->mem_share = (GstMemoryShareFunction) _mem_share;
Packit 971217
  alloc->mem_is_span = (GstMemoryIsSpanFunction) _mem_is_span;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_is_gl_base_memory:
Packit 971217
 * @mem:a #GstMemory
Packit 971217
 *
Packit 971217
 * Returns: whether the memory at @mem is a #GstGLBaseMemory
Packit 971217
 *
Packit 971217
 * Since: 1.8
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_is_gl_base_memory (GstMemory * mem)
Packit 971217
{
Packit 971217
  return mem != NULL && mem->allocator != NULL &&
Packit 971217
      g_type_is_a (G_OBJECT_TYPE (mem->allocator),
Packit 971217
      GST_TYPE_GL_BASE_MEMORY_ALLOCATOR);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_base_memory_memcpy:
Packit 971217
 * @src: the source #GstGLBaseMemory
Packit 971217
 * @dest: the destination #GstGLBaseMemory
Packit 971217
 * @offset: the offset to start at
Packit 971217
 * @size: the number of bytes to copy
Packit 971217
 *
Packit 971217
 * Returns: whether the copy suceeded.
Packit 971217
 *
Packit 971217
 * Since: 1.8
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_gl_base_memory_memcpy (GstGLBaseMemory * src, GstGLBaseMemory * dest,
Packit 971217
    gssize offset, gssize size)
Packit 971217
{
Packit 971217
  GstMapInfo sinfo, dinfo;
Packit 971217
Packit 971217
  if (!gst_gl_base_memory_alloc_data (GST_GL_BASE_MEMORY_CAST (dest)))
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  if (!gst_memory_map ((GstMemory *) src, &sinfo, GST_MAP_READ)) {
Packit 971217
    GST_CAT_WARNING (GST_CAT_GL_BASE_MEMORY,
Packit 971217
        "could not read map source memory %p", src);
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
Packit 971217
  if (!gst_memory_map ((GstMemory *) dest, &dinfo, GST_MAP_WRITE)) {
Packit 971217
    GST_CAT_WARNING (GST_CAT_GL_BASE_MEMORY,
Packit 971217
        "could not write map dest memory %p", dest);
Packit 971217
    gst_memory_unmap ((GstMemory *) src, &sinfo);
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
Packit 971217
  if (size == -1)
Packit 971217
    size = sinfo.size > offset ? sinfo.size - offset : 0;
Packit 971217
Packit 971217
  GST_CAT_DEBUG (GST_CAT_GL_BASE_MEMORY,
Packit 971217
      "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, src, dest);
Packit 971217
  memcpy (dinfo.data, sinfo.data + offset, size);
Packit 971217
  gst_memory_unmap ((GstMemory *) dest, &dinfo);
Packit 971217
  gst_memory_unmap ((GstMemory *) src, &sinfo);
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_allocation_params_init: (skip)
Packit 971217
 * @params: the #GstGLAllocationParams to initialize
Packit 971217
 * @struct_size: the struct size of the implementation
Packit 971217
 * @alloc_flags: some alloc flags
Packit 971217
 * @copy: a copy function
Packit 971217
 * @free: a free function
Packit 971217
 * @context: (transfer none): a #GstGLContext
Packit 971217
 * @alloc_size: the number of bytes to allocate.
Packit 971217
 * @alloc_params: (transfer none) (allow-none): a #GstAllocationParams to apply
Packit 971217
 * @wrapped_data: (transfer none) (allow-none): a sysmem data pointer to initialize the allocation with
Packit 971217
 * @gl_handle: (transfer none): a GL handle to initialize the allocation with
Packit 971217
 * @user_data: (transfer none) (allow-none): user data to call @notify with
Packit 971217
 * @notify: (allow-none): a #GDestroyNotify
Packit 971217
 *
Packit 971217
 * @notify will be called once for each allocated memory using these @params
Packit 971217
 * when freeing the memory.
Packit 971217
 *
Packit 971217
 * Returns: whether the paramaters could be initialized
Packit 971217
 *
Packit 971217
 * Since: 1.8
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_gl_allocation_params_init (GstGLAllocationParams * params,
Packit 971217
    gsize struct_size, guint alloc_flags, GstGLAllocationParamsCopyFunc copy,
Packit 971217
    GstGLAllocationParamsFreeFunc free, GstGLContext * context,
Packit 971217
    gsize alloc_size, GstAllocationParams * alloc_params,
Packit 971217
    gpointer wrapped_data, gpointer gl_handle, gpointer user_data,
Packit 971217
    GDestroyNotify notify)
Packit 971217
{
Packit 971217
  memset (params, 0, sizeof (*params));
Packit 971217
Packit 971217
  g_return_val_if_fail (struct_size > 0, FALSE);
Packit 971217
  g_return_val_if_fail (copy != NULL, FALSE);
Packit 971217
  g_return_val_if_fail (free != NULL, FALSE);
Packit 971217
  g_return_val_if_fail (GST_IS_GL_CONTEXT (context), FALSE);
Packit 971217
Packit 971217
  params->struct_size = struct_size;
Packit 971217
  params->alloc_size = alloc_size;
Packit 971217
  params->copy = copy;
Packit 971217
  params->free = free;
Packit 971217
  params->alloc_flags = alloc_flags;
Packit 971217
  params->context = gst_object_ref (context);
Packit 971217
  if (alloc_params)
Packit 971217
    params->alloc_params = gst_allocation_params_copy (alloc_params);
Packit 971217
  params->notify = notify;
Packit 971217
  params->user_data = user_data;
Packit 971217
  params->wrapped_data = wrapped_data;
Packit 971217
  params->gl_handle = gl_handle;
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_allocation_params_copy:
Packit 971217
 * @src: the #GstGLAllocationParams to initialize
Packit 971217
 *
Packit 971217
 * Returns: (transfer full): a copy of the #GstGLAllocationParams specified by
Packit 971217
 *          @src or %NULL on failure
Packit 971217
 *
Packit 971217
 * Since: 1.8
Packit 971217
 */
Packit 971217
GstGLAllocationParams *
Packit 971217
gst_gl_allocation_params_copy (GstGLAllocationParams * src)
Packit 971217
{
Packit 971217
  GstGLAllocationParams *dest;
Packit 971217
Packit 971217
  g_return_val_if_fail (src != NULL, NULL);
Packit 971217
Packit 971217
  dest = g_malloc0 (src->struct_size);
Packit 971217
Packit 971217
  if (src->copy)
Packit 971217
    src->copy (src, dest);
Packit 971217
Packit 971217
  return dest;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_allocation_params_free:
Packit 971217
 * @params: the #GstGLAllocationParams to initialize
Packit 971217
 *
Packit 971217
 * Frees the #GstGLAllocationParams and all associated data.
Packit 971217
 *
Packit 971217
 * Since: 1.8
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_gl_allocation_params_free (GstGLAllocationParams * params)
Packit 971217
{
Packit 971217
  if (params->free)
Packit 971217
    params->free (params);
Packit 971217
Packit 971217
  g_free (params);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_allocation_params_free_data:
Packit 971217
 * @params: the source #GstGLAllocationParams
Packit 971217
 *
Packit 971217
 * Frees the dynamically allocated data in @params.  Direct subclasses
Packit 971217
 * should call this function in their own overriden free function.
Packit 971217
 *
Packit 971217
 * Since: 1.8
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_gl_allocation_params_free_data (GstGLAllocationParams * params)
Packit 971217
{
Packit 971217
  if (params->context)
Packit 971217
    gst_object_unref (params->context);
Packit 971217
  if (params->alloc_params)
Packit 971217
    gst_allocation_params_free (params->alloc_params);
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_allocation_params_copy_data:
Packit 971217
 * @src: the source #GstGLAllocationParams
Packit 971217
 * @dest: the destination #GstGLAllocationParams
Packit 971217
 *
Packit 971217
 * Copies the dynamically allocated data from @src to @dest.  Direct subclasses
Packit 971217
 * should call this function in their own overriden copy function.
Packit 971217
 *
Packit 971217
 * Since: 1.8
Packit 971217
 */
Packit 971217
void
Packit 971217
gst_gl_allocation_params_copy_data (GstGLAllocationParams * src,
Packit 971217
    GstGLAllocationParams * dest)
Packit 971217
{
Packit 971217
  gst_gl_allocation_params_init (dest, src->struct_size, src->alloc_flags,
Packit 971217
      src->copy, src->free, src->context, src->alloc_size, NULL,
Packit 971217
      src->wrapped_data, src->gl_handle, src->user_data, src->notify);
Packit 971217
Packit 971217
  if (src->alloc_params)
Packit 971217
    dest->alloc_params = gst_allocation_params_copy (src->alloc_params);
Packit 971217
}
Packit 971217
Packit 971217
G_DEFINE_BOXED_TYPE (GstGLAllocationParams, gst_gl_allocation_params,
Packit 971217
    (GBoxedCopyFunc) gst_gl_allocation_params_copy,
Packit 971217
    (GBoxedFreeFunc) gst_gl_allocation_params_free);
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_gl_base_memory_alloc:
Packit 971217
 * @allocator: a #GstGLBaseMemoryAllocator
Packit 971217
 * @params: the #GstGLAllocationParams to allocate the memory with
Packit 971217
 *
Packit 971217
 * Returns: a new #GstGLBaseMemory from @allocator with the requested @params.
Packit 971217
 *
Packit 971217
 * Since: 1.8
Packit 971217
 */
Packit 971217
GstGLBaseMemory *
Packit 971217
gst_gl_base_memory_alloc (GstGLBaseMemoryAllocator * allocator,
Packit 971217
    GstGLAllocationParams * params)
Packit 971217
{
Packit 971217
  GstGLBaseMemoryAllocatorClass *alloc_class;
Packit 971217
Packit 971217
  g_return_val_if_fail (GST_IS_GL_BASE_MEMORY_ALLOCATOR (allocator), NULL);
Packit 971217
Packit 971217
  alloc_class = GST_GL_BASE_MEMORY_ALLOCATOR_GET_CLASS (allocator);
Packit 971217
Packit 971217
  g_return_val_if_fail (alloc_class != NULL, NULL);
Packit 971217
  g_return_val_if_fail (alloc_class->alloc != NULL, NULL);
Packit 971217
Packit 971217
  return alloc_class->alloc (allocator, params);
Packit 971217
}