summaryrefslogtreecommitdiff
path: root/gst-libs/gst/vaapi/gstvaapisurface_drm.c
blob: 7587553a219a92a3e3709e8b855ffe1d227a1d5c (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
/*
 *  gstvaapisurface_drm.c - VA surface abstraction (DRM interop)
 *
 *  Copyright (C) 2014 Intel Corporation
 *    Author: Gwenole Beauchesne <gwenole.beauchesne@intel.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1
 *  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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser 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 "sysdeps.h"
#include "gstvaapisurface_drm.h"
#include "gstvaapisurface_priv.h"
#include "gstvaapiimage_priv.h"
#include "gstvaapibufferproxy_priv.h"

static GstVaapiBufferProxy *
gst_vaapi_surface_get_drm_buf_handle (GstVaapiSurface * surface, guint type)
{
  GstVaapiBufferProxy *proxy;
  GstVaapiImage *image;

  image = gst_vaapi_surface_derive_image (surface);
  if (!image)
    goto error_derive_image;

  /* The proxy takes ownership if the image, even creation failure. */
  proxy =
      gst_vaapi_buffer_proxy_new_from_object (GST_MINI_OBJECT_CAST (surface),
      image->internal_image.buf, type, (GDestroyNotify) gst_vaapi_image_unref,
      image);
  if (!proxy)
    goto error_alloc_export_buffer;
  return proxy;

  /* ERRORS */
error_derive_image:
  {
    GST_ERROR ("failed to extract image handle from surface");
    return NULL;
  }
error_alloc_export_buffer:
  {
    GST_ERROR ("failed to allocate export buffer proxy");
    return NULL;
  }
}

/**
 * gst_vaapi_surface_get_dma_buf_handle:
 * @surface: a #GstVaapiSurface
 *
 * If the underlying VA driver implementation supports it, this
 * function allows for returning a suitable dma_buf (DRM) buffer
 * handle as a #GstVaapiBufferProxy instance. The resulting buffer
 * handle is live until the last reference to the proxy gets
 * released. Besides, any further change to the parent VA @surface may
 * fail.
 *
 * Return value: the underlying buffer as a #GstVaapiBufferProxy
 * instance.
 */
GstVaapiBufferProxy *
gst_vaapi_surface_get_dma_buf_handle (GstVaapiSurface * surface)
{
  g_return_val_if_fail (surface != NULL, NULL);

  return gst_vaapi_surface_get_drm_buf_handle (surface,
      GST_VAAPI_BUFFER_MEMORY_TYPE_DMA_BUF);
}

/**
 * gst_vaapi_surface_get_gem_buf_handle:
 * @surface: a #GstVaapiSurface
 *
 * If the underlying VA driver implementation supports it, this
 * function allows for returning a suitable GEM buffer handle as a
 * #GstVaapiBufferProxy instance. The resulting buffer handle is live
 * until the last reference to the proxy gets released. Besides, any
 * further change to the parent VA @surface may fail.
 *
 * Return value: the underlying buffer as a #GstVaapiBufferProxy
 * instance.
 */
GstVaapiBufferProxy *
gst_vaapi_surface_get_gem_buf_handle (GstVaapiSurface * surface)
{
  g_return_val_if_fail (surface != NULL, NULL);

  return gst_vaapi_surface_get_drm_buf_handle (surface,
      GST_VAAPI_BUFFER_MEMORY_TYPE_GEM_BUF);
}

static void
fill_video_info (GstVideoInfo * vip, GstVideoFormat format, guint width,
    guint height, gsize offset[GST_VIDEO_MAX_PLANES],
    gint stride[GST_VIDEO_MAX_PLANES])
{
  guint i;

  gst_video_info_set_format (vip, format, width, height);
  for (i = 0; i < GST_VIDEO_INFO_N_PLANES (vip); i++) {
    GST_VIDEO_INFO_PLANE_OFFSET (vip, i) = offset[i];
    GST_VIDEO_INFO_PLANE_STRIDE (vip, i) = stride[i];
  }
}

/**
 * gst_vaapi_surface_new_with_dma_buf_handle:
 * @display: a #GstVaapiDisplay
 * @fd: the DRM PRIME file descriptor
 * @size: the underlying DRM buffer size
 * @format: the desired surface format
 * @width: the desired surface width in pixels
 * @height: the desired surface height in pixels
 * @offset: the offsets to each plane
 * @stride: the pitches for each plane
 *
 * Creates a new #GstVaapiSurface with an external DRM PRIME file
 * descriptor. The newly created VA surfaces owns the supplied buffer
 * handle.
 *
 * Return value: the newly allocated #GstVaapiSurface object, or %NULL
 *   if creation from DRM PRIME fd failed, or is not supported
 */
GstVaapiSurface *
gst_vaapi_surface_new_with_dma_buf_handle (GstVaapiDisplay * display, gint fd,
    GstVideoInfo * vi)
{
  GstVaapiBufferProxy *proxy;
  GstVaapiSurface *surface;

  proxy = gst_vaapi_buffer_proxy_new ((gintptr) fd,
      GST_VAAPI_BUFFER_MEMORY_TYPE_DMA_BUF, GST_VIDEO_INFO_SIZE (vi), NULL,
      NULL);
  if (!proxy)
    return NULL;

  surface = gst_vaapi_surface_new_from_buffer_proxy (display, proxy, vi);
  gst_vaapi_buffer_proxy_unref (proxy);
  return surface;
}

/**
 * gst_vaapi_surface_new_with_gem_buf_handle:
 * @display: a #GstVaapiDisplay
 * @name: the DRM GEM buffer name
 * @size: the underlying DRM buffer size
 * @format: the desired surface format
 * @width: the desired surface width in pixels
 * @height: the desired surface height in pixels
 * @offset: the offsets to each plane
 * @stride: the pitches for each plane
 *
 * Creates a new #GstVaapiSurface with an external DRM GEM buffer
 * name. The newly created VA surfaces owns the supplied buffer
 * handle.
 *
 * Return value: the newly allocated #GstVaapiSurface object, or %NULL
 *   if creation from GEM @name failed, or is not supported
 */
GstVaapiSurface *
gst_vaapi_surface_new_with_gem_buf_handle (GstVaapiDisplay * display,
    guint32 name, guint size, GstVideoFormat format, guint width, guint height,
    gsize offset[GST_VIDEO_MAX_PLANES], gint stride[GST_VIDEO_MAX_PLANES])
{
  GstVaapiBufferProxy *proxy;
  GstVaapiSurface *surface;
  GstVideoInfo vi;

  proxy = gst_vaapi_buffer_proxy_new ((guintptr) name,
      GST_VAAPI_BUFFER_MEMORY_TYPE_GEM_BUF, size, NULL, NULL);
  if (!proxy)
    return NULL;

  fill_video_info (&vi, format, width, height, offset, stride);
  surface = gst_vaapi_surface_new_from_buffer_proxy (display, proxy, &vi);
  gst_vaapi_buffer_proxy_unref (proxy);
  return surface;
}