1 /**************************************************************************
2 *
3 * Copyright 2013 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Christian König <christian.koenig@amd.com>
31 *
32 */
33
34 #ifdef HAVE_ST_VDPAU
35
36 #include "main/texobj.h"
37 #include "main/teximage.h"
38 #include "main/errors.h"
39 #include "program/prog_instruction.h"
40
41 #include "pipe/p_state.h"
42 #include "pipe/p_video_codec.h"
43
44 #include "util/u_inlines.h"
45
46 #include "st_vdpau.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"
52
53 #include "frontend/vdpau_interop.h"
54 #include "frontend/vdpau_dmabuf.h"
55 #include "frontend/vdpau_funcs.h"
56 #include "frontend/drm_driver.h"
57
58 #include "drm-uapi/drm_fourcc.h"
59
60 static struct pipe_resource *
st_vdpau_video_surface_gallium(struct gl_context * ctx,const void * vdpSurface,GLuint index)61 st_vdpau_video_surface_gallium(struct gl_context *ctx, const void *vdpSurface,
62 GLuint index)
63 {
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;
68
69 struct pipe_video_buffer *buffer;
70 struct pipe_sampler_view **samplers;
71 struct pipe_resource *res = NULL;
72
73 getProcAddr = (void *)ctx->vdpGetProcAddress;
74 if (getProcAddr(device, VDP_FUNC_ID_VIDEO_SURFACE_GALLIUM, (void**)&f))
75 return NULL;
76
77 buffer = f((uintptr_t)vdpSurface);
78 if (!buffer)
79 return NULL;
80
81 samplers = buffer->get_sampler_view_planes(buffer);
82 if (!samplers)
83 return NULL;
84
85 sv = samplers[index >> 1];
86 if (!sv)
87 return NULL;
88
89 pipe_resource_reference(&res, sv->texture);
90 return res;
91 }
92
93 static struct pipe_resource *
st_vdpau_output_surface_gallium(struct gl_context * ctx,const void * vdpSurface)94 st_vdpau_output_surface_gallium(struct gl_context *ctx, const void *vdpSurface)
95 {
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;
100
101 getProcAddr = (void *)ctx->vdpGetProcAddress;
102 if (getProcAddr(device, VDP_FUNC_ID_OUTPUT_SURFACE_GALLIUM, (void**)&f))
103 return NULL;
104
105 pipe_resource_reference(&res, f((uintptr_t)vdpSurface));
106 return res;
107 }
108
109 static struct pipe_resource *
st_vdpau_resource_from_description(struct gl_context * ctx,const struct VdpSurfaceDMABufDesc * desc)110 st_vdpau_resource_from_description(struct gl_context *ctx,
111 const struct VdpSurfaceDMABufDesc *desc)
112 {
113 struct st_context *st = st_context(ctx);
114 struct pipe_resource templ, *res;
115 struct winsys_handle whandle;
116
117 if (desc->handle == -1)
118 return NULL;
119
120 memset(&templ, 0, sizeof(templ));
121 templ.target = PIPE_TEXTURE_2D;
122 templ.last_level = 0;
123 templ.depth0 = 1;
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;
130
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;
137
138 res = st->screen->resource_from_handle(st->screen, &templ, &whandle,
139 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
140 close(desc->handle);
141
142 return res;
143 }
144
145 static struct pipe_resource *
st_vdpau_output_surface_dma_buf(struct gl_context * ctx,const void * vdpSurface)146 st_vdpau_output_surface_dma_buf(struct gl_context *ctx, const void *vdpSurface)
147 {
148 int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
149 uint32_t device = (uintptr_t)ctx->vdpDevice;
150
151 struct VdpSurfaceDMABufDesc desc;
152 VdpOutputSurfaceDMABuf *f;
153
154 getProcAddr = (void *)ctx->vdpGetProcAddress;
155 if (getProcAddr(device, VDP_FUNC_ID_OUTPUT_SURFACE_DMA_BUF, (void**)&f))
156 return NULL;
157
158 if (f((uintptr_t)vdpSurface, &desc) != VDP_STATUS_OK)
159 return NULL;
160
161 return st_vdpau_resource_from_description(ctx, &desc);
162 }
163
164 static struct pipe_resource *
st_vdpau_video_surface_dma_buf(struct gl_context * ctx,const void * vdpSurface,GLuint index)165 st_vdpau_video_surface_dma_buf(struct gl_context *ctx, const void *vdpSurface,
166 GLuint index)
167 {
168 int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
169 uint32_t device = (uintptr_t)ctx->vdpDevice;
170
171 struct VdpSurfaceDMABufDesc desc;
172 VdpVideoSurfaceDMABuf *f;
173
174 getProcAddr = (void *)ctx->vdpGetProcAddress;
175 if (getProcAddr(device, VDP_FUNC_ID_VIDEO_SURFACE_DMA_BUF, (void**)&f))
176 return NULL;
177
178 if (f((uintptr_t)vdpSurface, index, &desc) != VDP_STATUS_OK)
179 return NULL;
180
181 return st_vdpau_resource_from_description(ctx, &desc);
182 }
183
184 static void
st_vdpau_map_surface(struct gl_context * ctx,GLenum target,GLenum access,GLboolean output,struct gl_texture_object * texObj,struct gl_texture_image * texImage,const void * vdpSurface,GLuint index)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)
189 {
190 struct st_context *st = st_context(ctx);
191 struct pipe_screen *screen = st->screen;
192 struct st_texture_object *stObj = st_texture_object(texObj);
193 struct st_texture_image *stImage = st_texture_image(texImage);
194
195 struct pipe_resource *res;
196 mesa_format texFormat;
197 int layer_override = -1;
198
199 if (output) {
200 res = st_vdpau_output_surface_dma_buf(ctx, vdpSurface);
201
202 if (!res)
203 res = st_vdpau_output_surface_gallium(ctx, vdpSurface);
204
205 } else {
206 res = st_vdpau_video_surface_dma_buf(ctx, vdpSurface, index);
207
208 if (!res) {
209 res = st_vdpau_video_surface_gallium(ctx, vdpSurface, index);
210 layer_override = index & 1;
211 }
212 }
213
214 /* If the resource is from a different screen, try re-importing it */
215 if (res && res->screen != screen) {
216 struct pipe_resource *new_res = NULL;
217 struct winsys_handle whandle = { .type = WINSYS_HANDLE_TYPE_FD };
218 unsigned usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
219
220 if (screen->get_param(screen, PIPE_CAP_DMABUF) &&
221 res->screen->get_param(res->screen, PIPE_CAP_DMABUF) &&
222 res->screen->resource_get_handle(res->screen, NULL, res, &whandle,
223 usage)) {
224 whandle.modifier = DRM_FORMAT_MOD_INVALID;
225 new_res = screen->resource_from_handle(screen, res, &whandle, usage);
226 close(whandle.handle);
227 }
228
229 pipe_resource_reference(&res, NULL);
230 res = new_res;
231 }
232
233 if (!res) {
234 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUMapSurfacesNV");
235 return;
236 }
237
238 /* switch to surface based */
239 if (!stObj->surface_based) {
240 _mesa_clear_texture_object(ctx, texObj, NULL);
241 stObj->surface_based = GL_TRUE;
242 }
243
244 texFormat = st_pipe_format_to_mesa_format(res->format);
245
246 _mesa_init_teximage_fields(ctx, texImage,
247 res->width0, res->height0, 1, 0, GL_RGBA,
248 texFormat);
249
250 pipe_resource_reference(&stObj->pt, res);
251 st_texture_release_all_sampler_views(st, stObj);
252 pipe_resource_reference(&stImage->pt, res);
253
254 stObj->surface_format = res->format;
255 stObj->level_override = -1;
256 stObj->layer_override = layer_override;
257
258 _mesa_dirty_texobj(ctx, texObj);
259 pipe_resource_reference(&res, NULL);
260 }
261
262 static void
st_vdpau_unmap_surface(struct gl_context * ctx,GLenum target,GLenum access,GLboolean output,struct gl_texture_object * texObj,struct gl_texture_image * texImage,const void * vdpSurface,GLuint index)263 st_vdpau_unmap_surface(struct gl_context *ctx, GLenum target, GLenum access,
264 GLboolean output, struct gl_texture_object *texObj,
265 struct gl_texture_image *texImage,
266 const void *vdpSurface, GLuint index)
267 {
268 struct st_context *st = st_context(ctx);
269 struct st_texture_object *stObj = st_texture_object(texObj);
270 struct st_texture_image *stImage = st_texture_image(texImage);
271
272 pipe_resource_reference(&stObj->pt, NULL);
273 st_texture_release_all_sampler_views(st, stObj);
274 pipe_resource_reference(&stImage->pt, NULL);
275
276 stObj->level_override = -1;
277 stObj->layer_override = -1;
278
279 _mesa_dirty_texobj(ctx, texObj);
280
281 /* NV_vdpau_interop does not specify an explicit synchronization mechanism
282 * between the GL and VDPAU contexts. Provide automatic synchronization here.
283 */
284 st_flush(st, NULL, 0);
285 }
286
287 void
st_init_vdpau_functions(struct dd_function_table * functions)288 st_init_vdpau_functions(struct dd_function_table *functions)
289 {
290 functions->VDPAUMapSurface = st_vdpau_map_surface;
291 functions->VDPAUUnmapSurface = st_vdpau_unmap_surface;
292 }
293 #endif
294