~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/mesa/state_tracker/st_vdpau.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 *
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 *
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 *
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 *
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 *
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
 
void
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
 
 
193
 
   struct pipe_resource *res;
194
 
   mesa_format texFormat;
195
 
   int layer_override = -1;
196
 
 
197
 
   if (output) {
198
 
      res = st_vdpau_output_surface_dma_buf(ctx, vdpSurface);
199
 
 
200
 
      if (!res)
201
 
         res = st_vdpau_output_surface_gallium(ctx, vdpSurface);
202
 
 
203
 
   } else {
204
 
      res = st_vdpau_video_surface_dma_buf(ctx, vdpSurface, index);
205
 
 
206
 
      if (!res) {
207
 
         res = st_vdpau_video_surface_gallium(ctx, vdpSurface, index);
208
 
         layer_override = index & 1;
209
 
      }
210
 
   }
211
 
 
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;
217
 
 
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,
221
 
                                           usage)) {
222
 
         whandle.modifier = DRM_FORMAT_MOD_INVALID;
223
 
         new_res = screen->resource_from_handle(screen, res, &whandle, usage);
224
 
         close(whandle.handle);
225
 
      }
226
 
 
227
 
      pipe_resource_reference(&res, NULL);
228
 
      res = new_res;
229
 
   }
230
 
 
231
 
   if (!res) {
232
 
      _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUMapSurfacesNV");
233
 
      return;
234
 
   }
235
 
 
236
 
   /* switch to surface based */
237
 
   if (!texObj->surface_based) {
238
 
      _mesa_clear_texture_object(ctx, texObj, NULL);
239
 
      texObj->surface_based = GL_TRUE;
240
 
   }
241
 
 
242
 
   texFormat = st_pipe_format_to_mesa_format(res->format);
243
 
 
244
 
   _mesa_init_teximage_fields(ctx, texImage,
245
 
                              res->width0, res->height0, 1, 0, GL_RGBA,
246
 
                              texFormat);
247
 
 
248
 
   pipe_resource_reference(&texObj->pt, res);
249
 
   st_texture_release_all_sampler_views(st, texObj);
250
 
   pipe_resource_reference(&texImage->pt, res);
251
 
 
252
 
   texObj->surface_format = res->format;
253
 
   texObj->level_override = -1;
254
 
   texObj->layer_override = layer_override;
255
 
 
256
 
   _mesa_dirty_texobj(ctx, texObj);
257
 
   pipe_resource_reference(&res, NULL);
258
 
}
259
 
 
260
 
void
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)
265
 
{
266
 
   struct st_context *st = st_context(ctx);
267
 
 
268
 
   pipe_resource_reference(&texObj->pt, NULL);
269
 
   st_texture_release_all_sampler_views(st, texObj);
270
 
   pipe_resource_reference(&texImage->pt, NULL);
271
 
 
272
 
   texObj->level_override = -1;
273
 
   texObj->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
 
#endif