Merge remote-tracking branch 'public/master' into vulkan
[mesa.git] / src / mesa / state_tracker / st_vdpau.c
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 #include "main/texobj.h"
35 #include "main/teximage.h"
36 #include "main/errors.h"
37 #include "program/prog_instruction.h"
38
39 #include "pipe/p_state.h"
40 #include "pipe/p_video_codec.h"
41
42 #include "util/u_inlines.h"
43
44 #include "st_vdpau.h"
45 #include "st_context.h"
46 #include "st_texture.h"
47 #include "st_format.h"
48 #include "st_cb_flush.h"
49
50 #ifdef HAVE_ST_VDPAU
51
52 #include "state_tracker/vdpau_interop.h"
53 #include "state_tracker/vdpau_dmabuf.h"
54 #include "state_tracker/vdpau_funcs.h"
55 #include "state_tracker/drm_driver.h"
56
57 static struct pipe_resource *
58 st_vdpau_video_surface_gallium(struct gl_context *ctx, const GLvoid *vdpSurface,
59 GLuint index)
60 {
61 int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
62 uint32_t device = (uintptr_t)ctx->vdpDevice;
63 struct pipe_sampler_view *sv;
64 VdpVideoSurfaceGallium *f;
65
66 struct pipe_video_buffer *buffer;
67 struct pipe_sampler_view **samplers;
68
69 getProcAddr = (void *)ctx->vdpGetProcAddress;
70 if (getProcAddr(device, VDP_FUNC_ID_VIDEO_SURFACE_GALLIUM, (void**)&f))
71 return NULL;
72
73 buffer = f((uintptr_t)vdpSurface);
74 if (!buffer)
75 return NULL;
76
77 samplers = buffer->get_sampler_view_planes(buffer);
78 if (!samplers)
79 return NULL;
80
81 sv = samplers[index >> 1];
82 if (!sv)
83 return NULL;
84
85 return sv->texture;
86 }
87
88 static struct pipe_resource *
89 st_vdpau_output_surface_gallium(struct gl_context *ctx, const GLvoid *vdpSurface)
90 {
91 int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
92 uint32_t device = (uintptr_t)ctx->vdpDevice;
93 VdpOutputSurfaceGallium *f;
94
95 getProcAddr = (void *)ctx->vdpGetProcAddress;
96 if (getProcAddr(device, VDP_FUNC_ID_OUTPUT_SURFACE_GALLIUM, (void**)&f))
97 return NULL;
98
99 return f((uintptr_t)vdpSurface);
100 }
101
102 static struct pipe_resource *
103 st_vdpau_resource_from_description(struct gl_context *ctx,
104 const struct VdpSurfaceDMABufDesc *desc)
105 {
106 struct st_context *st = st_context(ctx);
107 struct pipe_resource templ, *res;
108 struct winsys_handle whandle;
109
110 if (desc->handle == -1)
111 return NULL;
112
113 memset(&templ, 0, sizeof(templ));
114 templ.target = PIPE_TEXTURE_2D;
115 templ.last_level = 0;
116 templ.depth0 = 1;
117 templ.array_size = 1;
118 templ.width0 = desc->width;
119 templ.height0 = desc->height;
120 templ.format = VdpFormatRGBAToPipe(desc->format);
121 templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
122 templ.usage = PIPE_USAGE_DEFAULT;
123
124 memset(&whandle, 0, sizeof(whandle));
125 whandle.type = DRM_API_HANDLE_TYPE_FD;
126 whandle.handle = desc->handle;
127 whandle.offset = desc->offset;
128 whandle.stride = desc->stride;
129
130 res = st->pipe->screen->resource_from_handle(st->pipe->screen, &templ, &whandle,
131 PIPE_HANDLE_USAGE_READ_WRITE);
132 close(desc->handle);
133
134 return res;
135 }
136
137 static struct pipe_resource *
138 st_vdpau_output_surface_dma_buf(struct gl_context *ctx, const GLvoid *vdpSurface)
139 {
140 int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
141 uint32_t device = (uintptr_t)ctx->vdpDevice;
142
143 struct VdpSurfaceDMABufDesc desc;
144 VdpOutputSurfaceDMABuf *f;
145
146 getProcAddr = (void *)ctx->vdpGetProcAddress;
147 if (getProcAddr(device, VDP_FUNC_ID_OUTPUT_SURFACE_DMA_BUF, (void**)&f))
148 return NULL;
149
150 if (f((uintptr_t)vdpSurface, &desc) != VDP_STATUS_OK)
151 return NULL;
152
153 return st_vdpau_resource_from_description(ctx, &desc);
154 }
155
156 static struct pipe_resource *
157 st_vdpau_video_surface_dma_buf(struct gl_context *ctx, const GLvoid *vdpSurface,
158 GLuint index)
159 {
160 int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
161 uint32_t device = (uintptr_t)ctx->vdpDevice;
162
163 struct VdpSurfaceDMABufDesc desc;
164 VdpVideoSurfaceDMABuf *f;
165
166 getProcAddr = (void *)ctx->vdpGetProcAddress;
167 if (getProcAddr(device, VDP_FUNC_ID_VIDEO_SURFACE_DMA_BUF, (void**)&f))
168 return NULL;
169
170 if (f((uintptr_t)vdpSurface, index, &desc) != VDP_STATUS_OK)
171 return NULL;
172
173 return st_vdpau_resource_from_description(ctx, &desc);
174 }
175
176 static void
177 st_vdpau_map_surface(struct gl_context *ctx, GLenum target, GLenum access,
178 GLboolean output, struct gl_texture_object *texObj,
179 struct gl_texture_image *texImage,
180 const GLvoid *vdpSurface, GLuint index)
181 {
182 struct st_context *st = st_context(ctx);
183 struct st_texture_object *stObj = st_texture_object(texObj);
184 struct st_texture_image *stImage = st_texture_image(texImage);
185
186 struct pipe_resource *res;
187 struct pipe_sampler_view templ, **sampler_view;
188 mesa_format texFormat;
189
190 if (output) {
191 res = st_vdpau_output_surface_dma_buf(ctx, vdpSurface);
192
193 if (!res)
194 res = st_vdpau_output_surface_gallium(ctx, vdpSurface);
195
196 } else {
197 res = st_vdpau_video_surface_dma_buf(ctx, vdpSurface, index);
198
199 if (!res)
200 res = st_vdpau_video_surface_gallium(ctx, vdpSurface, index);
201 }
202
203 if (!res) {
204 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUMapSurfacesNV");
205 return;
206 }
207
208 /* do we have different screen objects ? */
209 if (res->screen != st->pipe->screen) {
210 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUMapSurfacesNV");
211 return;
212 }
213
214 /* switch to surface based */
215 if (!stObj->surface_based) {
216 _mesa_clear_texture_object(ctx, texObj);
217 stObj->surface_based = GL_TRUE;
218 }
219
220 texFormat = st_pipe_format_to_mesa_format(res->format);
221
222 _mesa_init_teximage_fields(ctx, texImage,
223 res->width0, res->height0, 1, 0, GL_RGBA,
224 texFormat);
225
226 pipe_resource_reference(&stObj->pt, res);
227 st_texture_release_all_sampler_views(st, stObj);
228 pipe_resource_reference(&stImage->pt, res);
229
230 u_sampler_view_default_template(&templ, res, res->format);
231 templ.u.tex.first_layer = index & 1;
232 templ.u.tex.last_layer = index & 1;
233 templ.swizzle_r = GET_SWZ(stObj->base._Swizzle, 0);
234 templ.swizzle_g = GET_SWZ(stObj->base._Swizzle, 1);
235 templ.swizzle_b = GET_SWZ(stObj->base._Swizzle, 2);
236 templ.swizzle_a = GET_SWZ(stObj->base._Swizzle, 3);
237
238 sampler_view = st_texture_get_sampler_view(st, stObj);
239 *sampler_view = st->pipe->create_sampler_view(st->pipe, res, &templ);
240
241 stObj->width0 = res->width0;
242 stObj->height0 = res->height0;
243 stObj->depth0 = 1;
244 stObj->surface_format = res->format;
245
246 _mesa_dirty_texobj(ctx, texObj);
247 }
248
249 static void
250 st_vdpau_unmap_surface(struct gl_context *ctx, GLenum target, GLenum access,
251 GLboolean output, struct gl_texture_object *texObj,
252 struct gl_texture_image *texImage,
253 const GLvoid *vdpSurface, GLuint index)
254 {
255 struct st_context *st = st_context(ctx);
256 struct st_texture_object *stObj = st_texture_object(texObj);
257 struct st_texture_image *stImage = st_texture_image(texImage);
258
259 pipe_resource_reference(&stObj->pt, NULL);
260 st_texture_release_all_sampler_views(st, stObj);
261 pipe_resource_reference(&stImage->pt, NULL);
262
263 _mesa_dirty_texobj(ctx, texObj);
264
265 st_flush(st, NULL, 0);
266 }
267
268 #endif
269
270 void
271 st_init_vdpau_functions(struct dd_function_table *functions)
272 {
273 #ifdef HAVE_ST_VDPAU
274 functions->VDPAUMapSurface = st_vdpau_map_surface;
275 functions->VDPAUUnmapSurface = st_vdpau_unmap_surface;
276 #endif
277 }