1
/**************************************************************************
3
* Copyright 2013 Advanced Micro Devices, Inc.
6
* Permission is hereby granted, free of charge, to any person obtaining a
7
* copy of this software and associated documentation files (the
8
* "Software"), to deal in the Software without restriction, including
9
* without limitation the rights to use, copy, modify, merge, publish,
10
* distribute, sub license, and/or sell copies of the Software, and to
11
* permit persons to whom the Software is furnished to do so, subject to
12
* the following conditions:
14
* The above copyright notice and this permission notice (including the
15
* next paragraph) shall be included in all copies or substantial portions
18
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21
* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
**************************************************************************/
30
* Christian König <christian.koenig@amd.com>
36
#include "main/texobj.h"
37
#include "main/teximage.h"
38
#include "main/errors.h"
39
#include "program/prog_instruction.h"
41
#include "pipe/p_state.h"
42
#include "pipe/p_video_codec.h"
44
#include "util/u_inlines.h"
47
#include "st_context.h"
48
#include "st_sampler_view.h"
49
#include "st_texture.h"
50
#include "st_format.h"
51
#include "st_cb_flush.h"
53
#include "frontend/vdpau_interop.h"
54
#include "frontend/vdpau_dmabuf.h"
55
#include "frontend/vdpau_funcs.h"
56
#include "frontend/drm_driver.h"
58
#include "drm-uapi/drm_fourcc.h"
60
static struct pipe_resource *
61
st_vdpau_video_surface_gallium(struct gl_context *ctx, const void *vdpSurface,
64
int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
65
uint32_t device = (uintptr_t)ctx->vdpDevice;
66
struct pipe_sampler_view *sv;
67
VdpVideoSurfaceGallium *f;
69
struct pipe_video_buffer *buffer;
70
struct pipe_sampler_view **samplers;
71
struct pipe_resource *res = NULL;
73
getProcAddr = (void *)ctx->vdpGetProcAddress;
74
if (getProcAddr(device, VDP_FUNC_ID_VIDEO_SURFACE_GALLIUM, (void**)&f))
77
buffer = f((uintptr_t)vdpSurface);
81
samplers = buffer->get_sampler_view_planes(buffer);
85
sv = samplers[index >> 1];
89
pipe_resource_reference(&res, sv->texture);
93
static struct pipe_resource *
94
st_vdpau_output_surface_gallium(struct gl_context *ctx, const void *vdpSurface)
96
int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
97
uint32_t device = (uintptr_t)ctx->vdpDevice;
98
struct pipe_resource *res = NULL;
99
VdpOutputSurfaceGallium *f;
101
getProcAddr = (void *)ctx->vdpGetProcAddress;
102
if (getProcAddr(device, VDP_FUNC_ID_OUTPUT_SURFACE_GALLIUM, (void**)&f))
105
pipe_resource_reference(&res, f((uintptr_t)vdpSurface));
109
static struct pipe_resource *
110
st_vdpau_resource_from_description(struct gl_context *ctx,
111
const struct VdpSurfaceDMABufDesc *desc)
113
struct st_context *st = st_context(ctx);
114
struct pipe_resource templ, *res;
115
struct winsys_handle whandle;
117
if (desc->handle == -1)
120
memset(&templ, 0, sizeof(templ));
121
templ.target = PIPE_TEXTURE_2D;
122
templ.last_level = 0;
124
templ.array_size = 1;
125
templ.width0 = desc->width;
126
templ.height0 = desc->height;
127
templ.format = VdpFormatRGBAToPipe(desc->format);
128
templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
129
templ.usage = PIPE_USAGE_DEFAULT;
131
memset(&whandle, 0, sizeof(whandle));
132
whandle.type = WINSYS_HANDLE_TYPE_FD;
133
whandle.handle = desc->handle;
134
whandle.modifier = DRM_FORMAT_MOD_INVALID;
135
whandle.offset = desc->offset;
136
whandle.stride = desc->stride;
138
res = st->screen->resource_from_handle(st->screen, &templ, &whandle,
139
PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
145
static struct pipe_resource *
146
st_vdpau_output_surface_dma_buf(struct gl_context *ctx, const void *vdpSurface)
148
int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
149
uint32_t device = (uintptr_t)ctx->vdpDevice;
151
struct VdpSurfaceDMABufDesc desc;
152
VdpOutputSurfaceDMABuf *f;
154
getProcAddr = (void *)ctx->vdpGetProcAddress;
155
if (getProcAddr(device, VDP_FUNC_ID_OUTPUT_SURFACE_DMA_BUF, (void**)&f))
158
if (f((uintptr_t)vdpSurface, &desc) != VDP_STATUS_OK)
161
return st_vdpau_resource_from_description(ctx, &desc);
164
static struct pipe_resource *
165
st_vdpau_video_surface_dma_buf(struct gl_context *ctx, const void *vdpSurface,
168
int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
169
uint32_t device = (uintptr_t)ctx->vdpDevice;
171
struct VdpSurfaceDMABufDesc desc;
172
VdpVideoSurfaceDMABuf *f;
174
getProcAddr = (void *)ctx->vdpGetProcAddress;
175
if (getProcAddr(device, VDP_FUNC_ID_VIDEO_SURFACE_DMA_BUF, (void**)&f))
178
if (f((uintptr_t)vdpSurface, index, &desc) != VDP_STATUS_OK)
181
return st_vdpau_resource_from_description(ctx, &desc);
185
st_vdpau_map_surface(struct gl_context *ctx, GLenum target, GLenum access,
186
GLboolean output, struct gl_texture_object *texObj,
187
struct gl_texture_image *texImage,
188
const void *vdpSurface, GLuint index)
190
struct st_context *st = st_context(ctx);
191
struct pipe_screen *screen = st->screen;
193
struct pipe_resource *res;
194
mesa_format texFormat;
195
int layer_override = -1;
198
res = st_vdpau_output_surface_dma_buf(ctx, vdpSurface);
201
res = st_vdpau_output_surface_gallium(ctx, vdpSurface);
204
res = st_vdpau_video_surface_dma_buf(ctx, vdpSurface, index);
207
res = st_vdpau_video_surface_gallium(ctx, vdpSurface, index);
208
layer_override = index & 1;
212
/* If the resource is from a different screen, try re-importing it */
213
if (res && res->screen != screen) {
214
struct pipe_resource *new_res = NULL;
215
struct winsys_handle whandle = { .type = WINSYS_HANDLE_TYPE_FD };
216
unsigned usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
218
if (screen->get_param(screen, PIPE_CAP_DMABUF) &&
219
res->screen->get_param(res->screen, PIPE_CAP_DMABUF) &&
220
res->screen->resource_get_handle(res->screen, NULL, res, &whandle,
222
whandle.modifier = DRM_FORMAT_MOD_INVALID;
223
new_res = screen->resource_from_handle(screen, res, &whandle, usage);
224
close(whandle.handle);
227
pipe_resource_reference(&res, NULL);
232
_mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUMapSurfacesNV");
236
/* switch to surface based */
237
if (!texObj->surface_based) {
238
_mesa_clear_texture_object(ctx, texObj, NULL);
239
texObj->surface_based = GL_TRUE;
242
texFormat = st_pipe_format_to_mesa_format(res->format);
244
_mesa_init_teximage_fields(ctx, texImage,
245
res->width0, res->height0, 1, 0, GL_RGBA,
248
pipe_resource_reference(&texObj->pt, res);
249
st_texture_release_all_sampler_views(st, texObj);
250
pipe_resource_reference(&texImage->pt, res);
252
texObj->surface_format = res->format;
253
texObj->level_override = -1;
254
texObj->layer_override = layer_override;
256
_mesa_dirty_texobj(ctx, texObj);
257
pipe_resource_reference(&res, NULL);
261
st_vdpau_unmap_surface(struct gl_context *ctx, GLenum target, GLenum access,
262
GLboolean output, struct gl_texture_object *texObj,
263
struct gl_texture_image *texImage,
264
const void *vdpSurface, GLuint index)
266
struct st_context *st = st_context(ctx);
268
pipe_resource_reference(&texObj->pt, NULL);
269
st_texture_release_all_sampler_views(st, texObj);
270
pipe_resource_reference(&texImage->pt, NULL);
272
texObj->level_override = -1;
273
texObj->layer_override = -1;
275
_mesa_dirty_texobj(ctx, texObj);
277
/* NV_vdpau_interop does not specify an explicit synchronization mechanism
278
* between the GL and VDPAU contexts. Provide automatic synchronization here.
280
st_flush(st, NULL, 0);