summaryrefslogtreecommitdiff
path: root/sys/inteldrm/intelbufmgrpool.c
blob: c38039741a6ca7ddf335fd17203db031dc09b59e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * GStreamer intel bufmgr upload element
 *
 * Copyright (C) 2014 Collabora Ltd.
 *
 * 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 */

#include "intelbufmgrpool.h"

#include <libdrm/i915_drm.h>

GST_DEBUG_CATEGORY_EXTERN (gstinteldmabufupload_debug);
#define GST_CAT_DEFAULT gstinteldmabufupload_debug

#define parent_class gst_intel_bufmgr_pool_parent_class
G_DEFINE_TYPE (GstIntelBufmgrPool, gst_intel_bufmgr_pool, GST_TYPE_BUFFER_POOL);

static G_DEFINE_QUARK (GstIntelBufmgrBoQDataQuark, gst_intel_bufmgr_bo_qdata);

static GstFlowReturn gst_intel_bufmgr_pool_alloc_buffer (GstBufferPool * pool,
    GstBuffer ** buffer, GstBufferPoolAcquireParams * params);

static gpointer
gst_intel_bufmgr_mem_map (GstMemory * mem, gsize maxsize, GstMapFlags flags)
{
  drm_intel_bo *bo;

  /* make memory sharing work, theoretically */
  if (mem->parent)
    return gst_intel_bufmgr_mem_map (mem->parent, maxsize, flags);

  bo = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
      gst_intel_bufmgr_bo_qdata_quark ());

  if (drm_intel_gem_bo_map_gtt (bo) != 0) {
    GST_ERROR ("failed to map drm_intel_bo");
    return NULL;
  }

  return bo->virtual;
}

static void
gst_intel_bufmgr_mem_unmap (GstMemory * mem)
{
  drm_intel_bo *bo;

  /* make memory sharing work, theoretically */
  if (mem->parent) {
    gst_intel_bufmgr_mem_unmap (mem->parent);
    return;
  }

  bo = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem),
      gst_intel_bufmgr_bo_qdata_quark ());

  drm_intel_gem_bo_unmap_gtt (bo);
}

static gboolean
gst_intel_bufmgr_pool_set_config (GstBufferPool * pool, GstStructure * config)
{
  GstIntelBufmgrPool *self = GST_INTEL_BUFMGR_POOL (pool);
  GstVideoInfo info;
  GstCaps *caps;
  GstBuffer *tmp;
  guint size, min_bufs, max_bufs;
  gboolean ret;

  if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_bufs,
          &max_bufs))
    goto wrong_config;

  if (caps == NULL)
    goto no_caps;

  /* now parse the caps from the config */
  if (!gst_video_info_from_caps (&info, caps))
    goto wrong_caps;

  self->info = info;

  /* Acquire a buffer and configure the size in the base class.
   * This is necessary to allow buffers to be reused in GstBufferPool,
   * because if the size does not match, GstBufferPool thinks that the
   * buffer has been modified and it discards it instead of reusing it.
   * Unfortunately, the size is determined by the downstream drm_intel_bo
   * allocator and not by us, so we have to allocate a buffer to find
   * out what its size ends up being.
   */
  if (gst_intel_bufmgr_pool_alloc_buffer (pool, &tmp, NULL) != GST_FLOW_OK)
    return FALSE;

  size = gst_buffer_get_size (tmp);
  gst_buffer_pool_config_set_params (config, caps, size, min_bufs, max_bufs);

  ret = GST_CALL_PARENT_WITH_DEFAULT
      (GST_BUFFER_POOL_CLASS, set_config, (pool, config), TRUE);

  gst_buffer_unref (tmp);

  return ret;

  /* ERRORS */
wrong_config:
  {
    GST_WARNING_OBJECT (pool, "invalid config");
    return FALSE;
  }
no_caps:
  {
    GST_WARNING_OBJECT (pool, "no caps in config");
    return FALSE;
  }
wrong_caps:
  {
    GST_WARNING_OBJECT (pool,
        "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
    return FALSE;
  }
}

static GstFlowReturn
gst_intel_bufmgr_pool_alloc_buffer (GstBufferPool * pool, GstBuffer ** buffer,
    GstBufferPoolAcquireParams * params)
{
  GstIntelBufmgrPool *self = GST_INTEL_BUFMGR_POOL (pool);
  GstMemory *mem;
  drm_intel_bo *bo;
  gint dmabuf_fd;
  uint32_t tiling = I915_TILING_NONE;
  gulong stride;

  bo = drm_intel_bo_alloc_tiled (self->context->bufmgr, "gst", self->info.width,
      self->info.height, self->info.finfo->pixel_stride[0], &tiling, &stride,
      0);

  if (!bo) {
    GST_ERROR_OBJECT (pool, "Allocating a drm_intel_bo failed");
    return GST_FLOW_ERROR;
  }

  if (drm_intel_bo_gem_export_to_prime (bo, &dmabuf_fd) != 0) {
    GST_ERROR_OBJECT (pool, "Failed to export drm_intel_bo to a dmabuf");
    drm_intel_bo_unreference (bo);
    return GST_FLOW_ERROR;
  }

  mem = gst_dmabuf_allocator_alloc (self->allocator, dmabuf_fd, bo->size);
  gst_mini_object_set_qdata (GST_MINI_OBJECT (mem),
      gst_intel_bufmgr_bo_qdata_quark (), bo,
      (GDestroyNotify) drm_intel_bo_unreference);

  *buffer = gst_buffer_new ();
  gst_buffer_append_memory (*buffer, mem);

  self->info.stride[0] = stride;
  (void) gst_buffer_add_video_meta_full (*buffer, 0, self->info.finfo->format,
      self->info.width, self->info.height, self->info.finfo->n_planes,
      self->info.offset, self->info.stride);

  GST_DEBUG_OBJECT (pool, "Allocated buffer. width %d, height %d, format %s, "
      "stride %d", self->info.width, self->info.height,
      gst_video_format_to_string (self->info.finfo->format),
      self->info.stride[0]);

  return GST_FLOW_OK;
}

static void
gst_intel_bufmgr_pool_finalize (GObject * object)
{
  GstIntelBufmgrPool *self = GST_INTEL_BUFMGR_POOL (object);

  g_object_unref (self->allocator);
  gst_mini_object_unref ((GstMiniObject *) self->context);
}

static void
gst_intel_bufmgr_pool_class_init (GstIntelBufmgrPoolClass * klass)
{
  GstBufferPoolClass *bpool_class = (GstBufferPoolClass *) klass;
  GObjectClass *object_class = (GObjectClass *) klass;

  bpool_class->set_config =
      GST_DEBUG_FUNCPTR (gst_intel_bufmgr_pool_set_config);
  bpool_class->alloc_buffer =
      GST_DEBUG_FUNCPTR (gst_intel_bufmgr_pool_alloc_buffer);

  object_class->finalize = GST_DEBUG_FUNCPTR (gst_intel_bufmgr_pool_finalize);
}

static void
gst_intel_bufmgr_pool_init (GstIntelBufmgrPool * self)
{
  self->allocator = gst_dmabuf_allocator_new ();
  self->allocator->mem_map = gst_intel_bufmgr_mem_map;
  self->allocator->mem_unmap = gst_intel_bufmgr_mem_unmap;
}

GstBufferPool *
gst_intel_bufmgr_pool_new (GstIntelBufmgrContext * context)
{
  GstIntelBufmgrPool *self = g_object_new (GST_TYPE_INTEL_BUFMGR_POOL, NULL);
  self->context =
      (GstIntelBufmgrContext *) gst_mini_object_ref ((GstMiniObject *) context);
  return (GstBufferPool *) self;
}