xref: /aosp_15_r20/external/mesa3d/src/mesa/state_tracker/st_vdpau.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 <[email protected]>
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    whandle.format = VdpFormatRGBAToPipe(desc->format);
138 
139    res = st->screen->resource_from_handle(st->screen, &templ, &whandle,
140                                           PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE);
141    close(desc->handle);
142 
143    return res;
144 }
145 
146 static struct pipe_resource *
st_vdpau_output_surface_dma_buf(struct gl_context * ctx,const void * vdpSurface)147 st_vdpau_output_surface_dma_buf(struct gl_context *ctx, const void *vdpSurface)
148 {
149    int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
150    uint32_t device = (uintptr_t)ctx->vdpDevice;
151 
152    struct VdpSurfaceDMABufDesc desc;
153    VdpOutputSurfaceDMABuf *f;
154 
155    getProcAddr = (void *)ctx->vdpGetProcAddress;
156    if (getProcAddr(device, VDP_FUNC_ID_OUTPUT_SURFACE_DMA_BUF, (void**)&f))
157       return NULL;
158 
159    if (f((uintptr_t)vdpSurface, &desc) != VDP_STATUS_OK)
160       return NULL;
161 
162    return st_vdpau_resource_from_description(ctx, &desc);
163 }
164 
165 static struct pipe_resource *
st_vdpau_video_surface_dma_buf(struct gl_context * ctx,const void * vdpSurface,GLuint index)166 st_vdpau_video_surface_dma_buf(struct gl_context *ctx, const void *vdpSurface,
167                                GLuint index)
168 {
169    int (*getProcAddr)(uint32_t device, uint32_t id, void **ptr);
170    uint32_t device = (uintptr_t)ctx->vdpDevice;
171 
172    struct VdpSurfaceDMABufDesc desc;
173    VdpVideoSurfaceDMABuf *f;
174 
175    getProcAddr = (void *)ctx->vdpGetProcAddress;
176    if (getProcAddr(device, VDP_FUNC_ID_VIDEO_SURFACE_DMA_BUF, (void**)&f))
177       return NULL;
178 
179    if (f((uintptr_t)vdpSurface, index, &desc) != VDP_STATUS_OK)
180       return NULL;
181 
182    return st_vdpau_resource_from_description(ctx, &desc);
183 }
184 
185 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)186 st_vdpau_map_surface(struct gl_context *ctx, GLenum target, GLenum access,
187                      GLboolean output, struct gl_texture_object *texObj,
188                      struct gl_texture_image *texImage,
189                      const void *vdpSurface, GLuint index)
190 {
191    struct st_context *st = st_context(ctx);
192    struct pipe_screen *screen = st->screen;
193 
194    struct pipe_resource *res;
195    mesa_format texFormat;
196    int layer_override = -1;
197 
198    if (output) {
199       res = st_vdpau_output_surface_dma_buf(ctx, vdpSurface);
200 
201       if (!res)
202          res = st_vdpau_output_surface_gallium(ctx, vdpSurface);
203 
204    } else {
205       res = st_vdpau_video_surface_dma_buf(ctx, vdpSurface, index);
206 
207       if (!res) {
208          res = st_vdpau_video_surface_gallium(ctx, vdpSurface, index);
209          layer_override = index & 1;
210       }
211    }
212 
213    /* If the resource is from a different screen, try re-importing it */
214    if (res && res->screen != screen) {
215       struct pipe_resource *new_res = NULL;
216       struct winsys_handle whandle = { .type = WINSYS_HANDLE_TYPE_FD };
217       unsigned usage = PIPE_HANDLE_USAGE_FRAMEBUFFER_WRITE;
218 
219       if (screen->get_param(screen, PIPE_CAP_DMABUF) &&
220           res->screen->get_param(res->screen, PIPE_CAP_DMABUF) &&
221           res->screen->resource_get_handle(res->screen, NULL, res, &whandle,
222                                            usage)) {
223          whandle.modifier = DRM_FORMAT_MOD_INVALID;
224          new_res = screen->resource_from_handle(screen, res, &whandle, usage);
225          close(whandle.handle);
226       }
227 
228       pipe_resource_reference(&res, NULL);
229       res = new_res;
230    }
231 
232    if (!res) {
233       _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUMapSurfacesNV");
234       return;
235    }
236 
237    /* switch to surface based */
238    if (!texObj->surface_based) {
239       _mesa_clear_texture_object(ctx, texObj, NULL);
240       texObj->surface_based = GL_TRUE;
241    }
242 
243    texFormat = st_pipe_format_to_mesa_format(res->format);
244 
245    _mesa_init_teximage_fields(ctx, texImage,
246                               res->width0, res->height0, 1, 0, GL_RGBA,
247                               texFormat);
248    _mesa_update_texture_object_swizzle(ctx, texObj);
249 
250    pipe_resource_reference(&texObj->pt, res);
251    st_texture_release_all_sampler_views(st, texObj);
252    pipe_resource_reference(&texImage->pt, res);
253 
254    texObj->surface_format = res->format;
255    texObj->level_override = -1;
256    texObj->layer_override = layer_override;
257 
258    _mesa_dirty_texobj(ctx, texObj);
259    pipe_resource_reference(&res, NULL);
260 }
261 
262 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 
270    pipe_resource_reference(&texObj->pt, NULL);
271    st_texture_release_all_sampler_views(st, texObj);
272    pipe_resource_reference(&texImage->pt, NULL);
273 
274    texObj->level_override = -1;
275    texObj->layer_override = -1;
276 
277    _mesa_dirty_texobj(ctx, texObj);
278 
279    /* NV_vdpau_interop does not specify an explicit synchronization mechanism
280     * between the GL and VDPAU contexts. Provide automatic synchronization here.
281     */
282    st_flush(st, NULL, 0);
283 }
284 
285 #endif
286