3597576076f3d36abb9d01dbe2dfbd467bda6c90
[mesa.git] / src / mesa / main / 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 <stdbool.h>
35 #include "context.h"
36 #include "glformats.h"
37 #include "hash_table.h"
38 #include "set.h"
39 #include "texobj.h"
40 #include "teximage.h"
41 #include "vdpau.h"
42
43 #define MAX_TEXTURES 4
44
45 struct vdp_surface
46 {
47 GLenum target;
48 struct gl_texture_object *textures[MAX_TEXTURES];
49 GLenum access, state;
50 GLboolean output;
51 const GLvoid *vdpSurface;
52 };
53
54 void GLAPIENTRY
55 _mesa_VDPAUInitNV(const GLvoid *vdpDevice, const GLvoid *getProcAddress)
56 {
57 GET_CURRENT_CONTEXT(ctx);
58
59 if (!vdpDevice) {
60 _mesa_error(ctx, GL_INVALID_VALUE, "vdpDevice");
61 return;
62 }
63
64 if (!getProcAddress) {
65 _mesa_error(ctx, GL_INVALID_VALUE, "getProcAddress");
66 return;
67 }
68
69 if (ctx->vdpDevice || ctx->vdpGetProcAddress || ctx->vdpSurfaces) {
70 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUInitNV");
71 return;
72 }
73
74 ctx->vdpDevice = vdpDevice;
75 ctx->vdpGetProcAddress = getProcAddress;
76 ctx->vdpSurfaces = _mesa_set_create(NULL, _mesa_key_pointer_equal);
77 }
78
79 static void
80 unregister_surface(struct set_entry *entry)
81 {
82 struct vdp_surface *surf = (struct vdp_surface *)entry->key;
83 GET_CURRENT_CONTEXT(ctx);
84
85 if (surf->state == GL_SURFACE_MAPPED_NV) {
86 GLintptr surfaces[] = { (GLintptr)surf };
87 _mesa_VDPAUUnmapSurfacesNV(1, surfaces);
88 }
89
90 _mesa_set_remove(ctx->vdpSurfaces, entry);
91 FREE(surf);
92 }
93
94 void GLAPIENTRY
95 _mesa_VDPAUFiniNV(void)
96 {
97 GET_CURRENT_CONTEXT(ctx);
98
99 if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
100 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUFiniNV");
101 return;
102 }
103
104 _mesa_set_destroy(ctx->vdpSurfaces, unregister_surface);
105
106 ctx->vdpDevice = 0;
107 ctx->vdpGetProcAddress = 0;
108 ctx->vdpSurfaces = NULL;
109 }
110
111 static GLintptr
112 register_surface(struct gl_context *ctx, GLboolean isOutput,
113 const GLvoid *vdpSurface, GLenum target,
114 GLsizei numTextureNames, const GLuint *textureNames)
115 {
116 struct vdp_surface *surf;
117 int i;
118
119 if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
120 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAURegisterSurfaceNV");
121 return (GLintptr)NULL;
122 }
123
124 if (target != GL_TEXTURE_2D && target != GL_TEXTURE_RECTANGLE) {
125 _mesa_error(ctx, GL_INVALID_ENUM, "VDPAURegisterSurfaceNV");
126 return (GLintptr)NULL;
127 }
128
129 if (target == GL_TEXTURE_RECTANGLE && !ctx->Extensions.NV_texture_rectangle) {
130 _mesa_error(ctx, GL_INVALID_ENUM, "VDPAURegisterSurfaceNV");
131 return (GLintptr)NULL;
132 }
133
134 surf = CALLOC_STRUCT( vdp_surface );
135 surf->vdpSurface = vdpSurface;
136 surf->target = target;
137 surf->access = GL_READ_WRITE;
138 surf->state = GL_SURFACE_REGISTERED_NV;
139 surf->output = isOutput;
140 for (i = 0; i < numTextureNames; ++i) {
141 struct gl_texture_object *tex;
142 tex = _mesa_lookup_texture(ctx, textureNames[i]);
143
144 _mesa_lock_texture(ctx, tex);
145
146 if (tex->Immutable) {
147 _mesa_unlock_texture(ctx, tex);
148 FREE(surf);
149 _mesa_error(ctx, GL_INVALID_OPERATION,
150 "VDPAURegisterSurfaceNV(texture is immutable)");
151 return (GLintptr)NULL;
152 }
153
154 if (tex->Target == 0)
155 tex->Target = target;
156 else if (tex->Target != target) {
157 _mesa_unlock_texture(ctx, tex);
158 FREE(surf);
159 _mesa_error(ctx, GL_INVALID_OPERATION,
160 "VDPAURegisterSurfaceNV(target mismatch)");
161 return (GLintptr)NULL;
162 }
163
164 /* This will disallow respecifying the storage. */
165 tex->Immutable = GL_TRUE;
166 _mesa_unlock_texture(ctx, tex);
167
168 _mesa_reference_texobj(&surf->textures[i], tex);
169 }
170
171 _mesa_set_add(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf);
172
173 return (GLintptr)surf;
174 }
175
176 GLintptr GLAPIENTRY
177 _mesa_VDPAURegisterVideoSurfaceNV(const GLvoid *vdpSurface, GLenum target,
178 GLsizei numTextureNames,
179 const GLuint *textureNames)
180 {
181 GET_CURRENT_CONTEXT(ctx);
182
183 if (numTextureNames != 4) {
184 _mesa_error(ctx, GL_INVALID_VALUE, "VDPAURegisterVideoSurfaceNV");
185 return (GLintptr)NULL;
186 }
187
188 return register_surface(ctx, false, vdpSurface, target,
189 numTextureNames, textureNames);
190 }
191
192 GLintptr GLAPIENTRY
193 _mesa_VDPAURegisterOutputSurfaceNV(const GLvoid *vdpSurface, GLenum target,
194 GLsizei numTextureNames,
195 const GLuint *textureNames)
196 {
197 GET_CURRENT_CONTEXT(ctx);
198
199 if (numTextureNames != 1) {
200 _mesa_error(ctx, GL_INVALID_VALUE, "VDPAURegisterVideoSurfaceNV");
201 return (GLintptr)NULL;
202 }
203
204 return register_surface(ctx, true, vdpSurface, target,
205 numTextureNames, textureNames);
206 }
207
208 void GLAPIENTRY
209 _mesa_VDPAUIsSurfaceNV(GLintptr surface)
210 {
211 struct vdp_surface *surf = (struct vdp_surface *)surface;
212 GET_CURRENT_CONTEXT(ctx);
213
214 if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
215 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUIsSurfaceNV");
216 return;
217 }
218
219 if (!_mesa_set_search(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf)) {
220 _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUIsSurfaceNV");
221 return;
222 }
223 }
224
225 void GLAPIENTRY
226 _mesa_VDPAUUnregisterSurfaceNV(GLintptr surface)
227 {
228 struct vdp_surface *surf = (struct vdp_surface *)surface;
229 struct set_entry *entry;
230 int i;
231 GET_CURRENT_CONTEXT(ctx);
232
233 if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
234 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUUnregisterSurfaceNV");
235 return;
236 }
237
238 /* according to the spec it's ok when this is zero */
239 if (surface == 0)
240 return;
241
242 entry = _mesa_set_search(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf);
243 if (!entry) {
244 _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUUnregisterSurfaceNV");
245 return;
246 }
247
248 for (i = 0; i < MAX_TEXTURES; i++) {
249 if (surf->textures[i]) {
250 surf->textures[i]->Immutable = GL_FALSE;
251 _mesa_reference_texobj(&surf->textures[i], NULL);
252 }
253 }
254
255 _mesa_set_remove(ctx->vdpSurfaces, entry);
256 FREE(surf);
257 }
258
259 void GLAPIENTRY
260 _mesa_VDPAUGetSurfaceivNV(GLintptr surface, GLenum pname, GLsizei bufSize,
261 GLsizei *length, GLint *values)
262 {
263 struct vdp_surface *surf = (struct vdp_surface *)surface;
264 GET_CURRENT_CONTEXT(ctx);
265
266 if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
267 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUGetSurfaceivNV");
268 return;
269 }
270
271 if (!_mesa_set_search(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf)) {
272 _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUGetSurfaceivNV");
273 return;
274 }
275
276 if (pname != GL_SURFACE_STATE_NV) {
277 _mesa_error(ctx, GL_INVALID_ENUM, "VDPAUGetSurfaceivNV");
278 return;
279 }
280
281 if (bufSize < 1) {
282 _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUGetSurfaceivNV");
283 return;
284 }
285
286 values[0] = surf->state;
287
288 if (length != NULL)
289 *length = 1;
290 }
291
292 void GLAPIENTRY
293 _mesa_VDPAUSurfaceAccessNV(GLintptr surface, GLenum access)
294 {
295 struct vdp_surface *surf = (struct vdp_surface *)surface;
296 GET_CURRENT_CONTEXT(ctx);
297
298 if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
299 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUSurfaceAccessNV");
300 return;
301 }
302
303 if (!_mesa_set_search(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf)) {
304 _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUSurfaceAccessNV");
305 return;
306 }
307
308 if (access != GL_READ_ONLY && access != GL_WRITE_ONLY &&
309 access != GL_READ_WRITE) {
310
311 _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUSurfaceAccessNV");
312 return;
313 }
314
315 if (surf->state == GL_SURFACE_MAPPED_NV) {
316 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUSurfaceAccessNV");
317 return;
318 }
319
320 surf->access = access;
321 }
322
323 void GLAPIENTRY
324 _mesa_VDPAUMapSurfacesNV(GLsizei numSurfaces, const GLintptr *surfaces)
325 {
326 GET_CURRENT_CONTEXT(ctx);
327 int i;
328
329 if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
330 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUUnmapSurfacesNV");
331 return;
332 }
333
334 for (i = 0; i < numSurfaces; ++i) {
335 struct vdp_surface *surf = (struct vdp_surface *)surfaces[i];
336
337 if (!_mesa_set_search(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf)) {
338 _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUSurfaceAccessNV");
339 return;
340 }
341
342 if (surf->state == GL_SURFACE_MAPPED_NV) {
343 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUSurfaceAccessNV");
344 return;
345 }
346 }
347
348 for (i = 0; i < numSurfaces; ++i) {
349 struct vdp_surface *surf = (struct vdp_surface *)surfaces[i];
350 unsigned numTextureNames = surf->output ? 1 : 4;
351 unsigned j;
352
353 for (j = 0; j < numTextureNames; ++j) {
354 struct gl_texture_object *tex = surf->textures[j];
355 struct gl_texture_image *image;
356
357 _mesa_lock_texture(ctx, tex);
358 image = _mesa_get_tex_image(ctx, tex, surf->target, 0);
359 if (!image) {
360 _mesa_error(ctx, GL_OUT_OF_MEMORY, "VDPAUMapSurfacesNV");
361 _mesa_unlock_texture(ctx, tex);
362 return;
363 }
364
365 ctx->Driver.FreeTextureImageBuffer(ctx, image);
366
367 ctx->Driver.VDPAUMapSurface(ctx, surf->target, surf->access,
368 surf->output, tex, image,
369 surf->vdpSurface, j);
370
371 _mesa_unlock_texture(ctx, tex);
372 }
373 surf->state = GL_SURFACE_MAPPED_NV;
374 }
375 }
376
377 void GLAPIENTRY
378 _mesa_VDPAUUnmapSurfacesNV(GLsizei numSurfaces, const GLintptr *surfaces)
379 {
380 GET_CURRENT_CONTEXT(ctx);
381 int i;
382
383 if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
384 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUUnmapSurfacesNV");
385 return;
386 }
387
388 for (i = 0; i < numSurfaces; ++i) {
389 struct vdp_surface *surf = (struct vdp_surface *)surfaces[i];
390
391 if (!_mesa_set_search(ctx->vdpSurfaces, _mesa_hash_pointer(surf), surf)) {
392 _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUSurfaceAccessNV");
393 return;
394 }
395
396 if (surf->state != GL_SURFACE_MAPPED_NV) {
397 _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUSurfaceAccessNV");
398 return;
399 }
400 }
401
402 for (i = 0; i < numSurfaces; ++i) {
403 struct vdp_surface *surf = (struct vdp_surface *)surfaces[i];
404 unsigned numTextureNames = surf->output ? 1 : 4;
405 unsigned j;
406
407 for (j = 0; j < numTextureNames; ++j) {
408 struct gl_texture_object *tex = surf->textures[j];
409 struct gl_texture_image *image;
410
411 _mesa_lock_texture(ctx, tex);
412
413 image = _mesa_select_tex_image(ctx, tex, surf->target, 0);
414
415 ctx->Driver.VDPAUUnmapSurface(ctx, surf->target, surf->access,
416 surf->output, tex, image,
417 surf->vdpSurface, j);
418
419 if (image)
420 ctx->Driver.FreeTextureImageBuffer(ctx, image);
421
422 _mesa_unlock_texture(ctx, tex);
423 }
424 surf->state = GL_SURFACE_REGISTERED_NV;
425 }
426 }