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 static struct pipe_resource *
st_vdpau_video_surface_gallium(struct gl_context * ctx,const void * vdpSurface,GLuint index)59 st_vdpau_video_surface_gallium(struct gl_context *ctx, const void *vdpSurface,
60 GLuint index)
61 {
62 int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
63 uint32_t device = (uintptr_t)ctx->vdpDevice;
64 struct pipe_sampler_view *sv;
65 VdpVideoSurfaceGallium *f;
66
67 struct pipe_video_buffer *buffer;
68 struct pipe_sampler_view **samplers;
69 struct pipe_resource *res = NULL;
70
71 getProcAddr = (void *)ctx->vdpGetProcAddress;
72 if (getProcAddr(device, VDP_FUNC_ID_VIDEO_SURFACE_GALLIUM, (void**)&f))
73 return NULL;
74
75 buffer = f((uintptr_t)vdpSurface);
76 if (!buffer)
77 return NULL;
78
79 samplers = buffer->get_sampler_view_planes(buffer);
80 if (!samplers)
81 return NULL;
82
83 sv = samplers[index >> 1];
84 if (!sv)
85 return NULL;
86
87 pipe_resource_reference(&res, sv->texture);
88 return res;
89 }
90
91 static struct pipe_resource *
st_vdpau_output_surface_gallium(struct gl_context * ctx,const void * vdpSurface)92 st_vdpau_output_surface_gallium(struct gl_context *ctx, const void *vdpSurface)
93 {
94 int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
95 uint32_t device = (uintptr_t)ctx->vdpDevice;
96 struct pipe_resource *res = NULL;
97 VdpOutputSurfaceGallium *f;
98
99 getProcAddr = (void *)ctx->vdpGetProcAddress;
100 if (getProcAddr(device, VDP_FUNC_ID_OUTPUT_SURFACE_GALLIUM, (void**)&f))
101 return NULL;
102
103 pipe_resource_reference(&res, f((uintptr_t)vdpSurface));
104 return res;
105 }
106
107 static struct pipe_resource *
st_vdpau_resource_from_description(struct gl_context * ctx,const struct VdpSurfaceDMABufDesc * desc)108 st_vdpau_resource_from_description(struct gl_context *ctx,
109 const struct VdpSurfaceDMABufDesc *desc)
110 {
111 struct st_context *st = st_context(ctx);
112 struct pipe_resource templ, *res;
113 struct winsys_handle whandle;
114
115 if (desc->handle == -1)
116 return NULL;
117
118 memset(&templ, 0, sizeof(templ));
119 templ.target = PIPE_TEXTURE_2D;
120 templ.last_level = 0;
121 templ.depth0 = 1;
122 templ.array_size = 1;
123 templ.width0 = desc->width;
124 templ.height0 = desc->height;
125 templ.format = VdpFormatRGBAToPipe(desc->format);
126 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
127 templ.usage = PIPE_USAGE_DEFAULT;
128
129 memset(&whandle, 0, sizeof(whandle));
130 whandle.type = WINSYS_HANDLE_TYPE_FD;
131 whandle.handle = desc->handle;
132 whandle.offset = desc->offset;
133 whandle.stride = desc->stride;
134
135 res = st->pipe->screen->resource_from_handle(st->pipe->screen, &templ, &whandle,
136 PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
137 close(desc->handle);
138
139 return res;
140 }
141
142 static struct pipe_resource *
st_vdpau_output_surface_dma_buf(struct gl_context * ctx,const void * vdpSurface)143 st_vdpau_output_surface_dma_buf(struct gl_context *ctx, const void *vdpSurface)
144 {
145 int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
146 uint32_t device = (uintptr_t)ctx->vdpDevice;
147
148 struct VdpSurfaceDMABufDesc desc;
149 VdpOutputSurfaceDMABuf *f;
150
151 getProcAddr = (void *)ctx->vdpGetProcAddress;
152 if (getProcAddr(device, VDP_FUNC_ID_OUTPUT_SURFACE_DMA_BUF, (void**)&f))
153 return NULL;
154
155 if (f((uintptr_t)vdpSurface, &desc) != VDP_STATUS_OK)
156 return NULL;
157
158 return st_vdpau_resource_from_description(ctx, &desc);
159 }
160
161 static struct pipe_resource *
st_vdpau_video_surface_dma_buf(struct gl_context * ctx,const void * vdpSurface,GLuint index)162 st_vdpau_video_surface_dma_buf(struct gl_context *ctx, const void *vdpSurface,
163 GLuint index)
164 {
165 int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
166 uint32_t device = (uintptr_t)ctx->vdpDevice;
167
168 struct VdpSurfaceDMABufDesc desc;
169 VdpVideoSurfaceDMABuf *f;
170
171 getProcAddr = (void *)ctx->vdpGetProcAddress;
172 if (getProcAddr(device, VDP_FUNC_ID_VIDEO_SURFACE_DMA_BUF, (void**)&f))
173 return NULL;
174
175 if (f((uintptr_t)vdpSurface, index, &desc) != VDP_STATUS_OK)
176 return NULL;
177
178 return st_vdpau_resource_from_description(ctx, &desc);
179 }
180
181 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)182 st_vdpau_map_surface(struct gl_context *ctx, GLenum target, GLenum access,
183 GLboolean output, struct gl_texture_object *texObj,
184 struct gl_texture_image *texImage,
185 const void *vdpSurface, GLuint index)
186 {
187 struct st_context *st = st_context(ctx);
188 struct pipe_screen *screen = st->pipe->screen;
189 struct st_texture_object *stObj = st_texture_object(texObj);
190 struct st_texture_image *stImage = st_texture_image(texImage);
191
192 struct pipe_resource *res;
193 mesa_format texFormat;
194 int layer_override = -1;
195
196 if (output) {
197 res = st_vdpau_output_surface_dma_buf(ctx, vdpSurface);
198
199 if (!res)
200 res = st_vdpau_output_surface_gallium(ctx, vdpSurface);
201
202 } else {
203 res = st_vdpau_video_surface_dma_buf(ctx, vdpSurface, index);
204
205 if (!res) {
206 res = st_vdpau_video_surface_gallium(ctx, vdpSurface, index);
207 layer_override = index & 1;
208 }
209 }
210
211 /* If the resource is from a different screen, try re-importing it */
212 if (res && res->screen != screen) {
213 struct pipe_resource *new_res = NULL;
214 struct winsys_handle whandle = { .type = WINSYS_HANDLE_TYPE_FD };
215 unsigned usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
216
217 if (screen->get_param(screen, PIPE_CAP_DMABUF) &&
218 res->screen->get_param(res->screen, PIPE_CAP_DMABUF) &&
219 res->screen->resource_get_handle(res->screen, NULL, res, &whandle,
220 usage)) {
221 new_res = screen->resource_from_handle(screen, res, &whandle, usage);
222 close(whandle.handle);
223 }
224
225 pipe_resource_reference(&res, NULL);
226 res = new_res;
227 }
228
229 if (!res) {
230 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUMapSurfacesNV");
231 return;
232 }
233
234 /* switch to surface based */
235 if (!stObj->surface_based) {
236 _mesa_clear_texture_object(ctx, texObj, NULL);
237 stObj->surface_based = GL_TRUE;
238 }
239
240 texFormat = st_pipe_format_to_mesa_format(res->format);
241
242 _mesa_init_teximage_fields(ctx, texImage,
243 res->width0, res->height0, 1, 0, GL_RGBA,
244 texFormat);
245
246 pipe_resource_reference(&stObj->pt, res);
247 st_texture_release_all_sampler_views(st, stObj);
248 pipe_resource_reference(&stImage->pt, res);
249
250 stObj->surface_format = res->format;
251 stObj->level_override = -1;
252 stObj->layer_override = layer_override;
253
254 _mesa_dirty_texobj(ctx, texObj);
255 pipe_resource_reference(&res, NULL);
256 }
257
258 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)259 st_vdpau_unmap_surface(struct gl_context *ctx, GLenum target, GLenum access,
260 GLboolean output, struct gl_texture_object *texObj,
261 struct gl_texture_image *texImage,
262 const void *vdpSurface, GLuint index)
263 {
264 struct st_context *st = st_context(ctx);
265 struct st_texture_object *stObj = st_texture_object(texObj);
266 struct st_texture_image *stImage = st_texture_image(texImage);
267
268 pipe_resource_reference(&stObj->pt, NULL);
269 st_texture_release_all_sampler_views(st, stObj);
270 pipe_resource_reference(&stImage->pt, NULL);
271
272 stObj->level_override = -1;
273 stObj->layer_override = -1;
274
275 _mesa_dirty_texobj(ctx, texObj);
276
277 /* NV_vdpau_interop does not specify an explicit synchronization mechanism
278 * between the GL and VDPAU contexts. Provide automatic synchronization here.
279 */
280 st_flush(st, NULL, 0);
281 }
282
283 void
st_init_vdpau_functions(struct dd_function_table * functions)284 st_init_vdpau_functions(struct dd_function_table *functions)
285 {
286 functions->VDPAUMapSurface = st_vdpau_map_surface;
287 functions->VDPAUUnmapSurface = st_vdpau_unmap_surface;
288 }
289 #endif
290