1af69d88dSmrg/**************************************************************************
2af69d88dSmrg *
3af69d88dSmrg * Copyright 2013 Advanced Micro Devices, Inc.
4af69d88dSmrg * All Rights Reserved.
5af69d88dSmrg *
6af69d88dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
7af69d88dSmrg * copy of this software and associated documentation files (the
8af69d88dSmrg * "Software"), to deal in the Software without restriction, including
9af69d88dSmrg * without limitation the rights to use, copy, modify, merge, publish,
10af69d88dSmrg * distribute, sub license, and/or sell copies of the Software, and to
11af69d88dSmrg * permit persons to whom the Software is furnished to do so, subject to
12af69d88dSmrg * the following conditions:
13af69d88dSmrg *
14af69d88dSmrg * The above copyright notice and this permission notice (including the
15af69d88dSmrg * next paragraph) shall be included in all copies or substantial portions
16af69d88dSmrg * of the Software.
17af69d88dSmrg *
18af69d88dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19af69d88dSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20af69d88dSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21af69d88dSmrg * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22af69d88dSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23af69d88dSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24af69d88dSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25af69d88dSmrg *
26af69d88dSmrg **************************************************************************/
27af69d88dSmrg
28af69d88dSmrg/*
29af69d88dSmrg * Authors:
30af69d88dSmrg *      Christian König <christian.koenig@amd.com>
31af69d88dSmrg *
32af69d88dSmrg */
33af69d88dSmrg
34af69d88dSmrg#include <stdbool.h>
35af69d88dSmrg#include "util/hash_table.h"
3601e04c3fSmrg#include "util/set.h"
377ec681f3Smrg#include "util/u_memory.h"
38af69d88dSmrg#include "context.h"
39af69d88dSmrg#include "glformats.h"
40af69d88dSmrg#include "texobj.h"
41af69d88dSmrg#include "teximage.h"
42af69d88dSmrg#include "vdpau.h"
43af69d88dSmrg
44af69d88dSmrg#define MAX_TEXTURES 4
45af69d88dSmrg
46af69d88dSmrgstruct vdp_surface
47af69d88dSmrg{
48af69d88dSmrg   GLenum target;
49af69d88dSmrg   struct gl_texture_object *textures[MAX_TEXTURES];
50af69d88dSmrg   GLenum access, state;
51af69d88dSmrg   GLboolean output;
52af69d88dSmrg   const GLvoid *vdpSurface;
53af69d88dSmrg};
54af69d88dSmrg
55af69d88dSmrgvoid GLAPIENTRY
56af69d88dSmrg_mesa_VDPAUInitNV(const GLvoid *vdpDevice, const GLvoid *getProcAddress)
57af69d88dSmrg{
58af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
59af69d88dSmrg
60af69d88dSmrg   if (!vdpDevice) {
61af69d88dSmrg      _mesa_error(ctx, GL_INVALID_VALUE, "vdpDevice");
62af69d88dSmrg      return;
63af69d88dSmrg   }
64af69d88dSmrg
65af69d88dSmrg   if (!getProcAddress) {
66af69d88dSmrg      _mesa_error(ctx, GL_INVALID_VALUE, "getProcAddress");
67af69d88dSmrg      return;
68af69d88dSmrg   }
69af69d88dSmrg
70af69d88dSmrg   if (ctx->vdpDevice || ctx->vdpGetProcAddress || ctx->vdpSurfaces) {
71af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUInitNV");
72af69d88dSmrg      return;
73af69d88dSmrg   }
74af69d88dSmrg
75af69d88dSmrg   ctx->vdpDevice = vdpDevice;
76af69d88dSmrg   ctx->vdpGetProcAddress = getProcAddress;
7701e04c3fSmrg   ctx->vdpSurfaces = _mesa_set_create(NULL, _mesa_hash_pointer,
7801e04c3fSmrg                                       _mesa_key_pointer_equal);
79af69d88dSmrg}
80af69d88dSmrg
81af69d88dSmrgstatic void
82af69d88dSmrgunregister_surface(struct set_entry *entry)
83af69d88dSmrg{
84af69d88dSmrg   struct vdp_surface *surf = (struct vdp_surface *)entry->key;
85af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
86af69d88dSmrg
87af69d88dSmrg   if (surf->state == GL_SURFACE_MAPPED_NV) {
88af69d88dSmrg      GLintptr surfaces[] = { (GLintptr)surf };
89af69d88dSmrg      _mesa_VDPAUUnmapSurfacesNV(1, surfaces);
90af69d88dSmrg   }
91af69d88dSmrg
92af69d88dSmrg   _mesa_set_remove(ctx->vdpSurfaces, entry);
93af69d88dSmrg   free(surf);
94af69d88dSmrg}
95af69d88dSmrg
96af69d88dSmrgvoid GLAPIENTRY
97af69d88dSmrg_mesa_VDPAUFiniNV(void)
98af69d88dSmrg{
99af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
100af69d88dSmrg
101af69d88dSmrg   if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
102af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUFiniNV");
103af69d88dSmrg      return;
104af69d88dSmrg   }
105af69d88dSmrg
106af69d88dSmrg   _mesa_set_destroy(ctx->vdpSurfaces, unregister_surface);
107af69d88dSmrg
108af69d88dSmrg   ctx->vdpDevice = 0;
109af69d88dSmrg   ctx->vdpGetProcAddress = 0;
110af69d88dSmrg   ctx->vdpSurfaces = NULL;
111af69d88dSmrg}
112af69d88dSmrg
113af69d88dSmrgstatic GLintptr
114af69d88dSmrgregister_surface(struct gl_context *ctx, GLboolean isOutput,
115af69d88dSmrg                 const GLvoid *vdpSurface, GLenum target,
116af69d88dSmrg                 GLsizei numTextureNames, const GLuint *textureNames)
117af69d88dSmrg{
118af69d88dSmrg   struct vdp_surface *surf;
119af69d88dSmrg   int i;
120af69d88dSmrg
121af69d88dSmrg   if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
122af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAURegisterSurfaceNV");
123af69d88dSmrg      return (GLintptr)NULL;
124af69d88dSmrg   }
125af69d88dSmrg
126af69d88dSmrg   if (target != GL_TEXTURE_2D && target != GL_TEXTURE_RECTANGLE) {
127af69d88dSmrg      _mesa_error(ctx, GL_INVALID_ENUM, "VDPAURegisterSurfaceNV");
128af69d88dSmrg      return (GLintptr)NULL;
129af69d88dSmrg   }
130af69d88dSmrg
131af69d88dSmrg   if (target == GL_TEXTURE_RECTANGLE && !ctx->Extensions.NV_texture_rectangle) {
132af69d88dSmrg      _mesa_error(ctx, GL_INVALID_ENUM, "VDPAURegisterSurfaceNV");
133af69d88dSmrg      return (GLintptr)NULL;
134af69d88dSmrg   }
135af69d88dSmrg
136af69d88dSmrg   surf = CALLOC_STRUCT( vdp_surface );
137af69d88dSmrg   if (surf == NULL) {
138af69d88dSmrg      _mesa_error_no_memory("VDPAURegisterSurfaceNV");
139af69d88dSmrg      return (GLintptr)NULL;
140af69d88dSmrg   }
141af69d88dSmrg
142af69d88dSmrg   surf->vdpSurface = vdpSurface;
143af69d88dSmrg   surf->target = target;
144af69d88dSmrg   surf->access = GL_READ_WRITE;
145af69d88dSmrg   surf->state = GL_SURFACE_REGISTERED_NV;
146af69d88dSmrg   surf->output = isOutput;
147af69d88dSmrg   for (i = 0; i < numTextureNames; ++i) {
148af69d88dSmrg      struct gl_texture_object *tex;
14901e04c3fSmrg
15001e04c3fSmrg      tex = _mesa_lookup_texture_err(ctx, textureNames[i],
15101e04c3fSmrg                                     "VDPAURegisterSurfaceNV");
152af69d88dSmrg      if (tex == NULL) {
153af69d88dSmrg         free(surf);
154af69d88dSmrg         return (GLintptr)NULL;
155af69d88dSmrg      }
156af69d88dSmrg
157af69d88dSmrg      _mesa_lock_texture(ctx, tex);
158af69d88dSmrg
159af69d88dSmrg      if (tex->Immutable) {
160af69d88dSmrg         _mesa_unlock_texture(ctx, tex);
161af69d88dSmrg         free(surf);
162af69d88dSmrg         _mesa_error(ctx, GL_INVALID_OPERATION,
163af69d88dSmrg                     "VDPAURegisterSurfaceNV(texture is immutable)");
164af69d88dSmrg         return (GLintptr)NULL;
165af69d88dSmrg      }
166af69d88dSmrg
16701e04c3fSmrg      if (tex->Target == 0) {
168af69d88dSmrg         tex->Target = target;
16901e04c3fSmrg         tex->TargetIndex = _mesa_tex_target_to_index(ctx, target);
17001e04c3fSmrg      } else if (tex->Target != target) {
171af69d88dSmrg         _mesa_unlock_texture(ctx, tex);
172af69d88dSmrg         free(surf);
173af69d88dSmrg         _mesa_error(ctx, GL_INVALID_OPERATION,
174af69d88dSmrg                     "VDPAURegisterSurfaceNV(target mismatch)");
175af69d88dSmrg         return (GLintptr)NULL;
176af69d88dSmrg      }
177af69d88dSmrg
178af69d88dSmrg      /* This will disallow respecifying the storage. */
179af69d88dSmrg      tex->Immutable = GL_TRUE;
180af69d88dSmrg      _mesa_unlock_texture(ctx, tex);
181af69d88dSmrg
182af69d88dSmrg      _mesa_reference_texobj(&surf->textures[i], tex);
183af69d88dSmrg   }
184af69d88dSmrg
18501e04c3fSmrg   _mesa_set_add(ctx->vdpSurfaces, surf);
186af69d88dSmrg
187af69d88dSmrg   return (GLintptr)surf;
188af69d88dSmrg}
189af69d88dSmrg
190af69d88dSmrgGLintptr GLAPIENTRY
191af69d88dSmrg_mesa_VDPAURegisterVideoSurfaceNV(const GLvoid *vdpSurface, GLenum target,
192af69d88dSmrg                                  GLsizei numTextureNames,
193af69d88dSmrg                                  const GLuint *textureNames)
194af69d88dSmrg{
195af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
196af69d88dSmrg
197af69d88dSmrg   if (numTextureNames != 4) {
198af69d88dSmrg      _mesa_error(ctx, GL_INVALID_VALUE, "VDPAURegisterVideoSurfaceNV");
199af69d88dSmrg      return (GLintptr)NULL;
200af69d88dSmrg   }
201af69d88dSmrg
202af69d88dSmrg   return register_surface(ctx, false, vdpSurface, target,
203af69d88dSmrg                           numTextureNames, textureNames);
204af69d88dSmrg}
205af69d88dSmrg
206af69d88dSmrgGLintptr GLAPIENTRY
207af69d88dSmrg_mesa_VDPAURegisterOutputSurfaceNV(const GLvoid *vdpSurface, GLenum target,
208af69d88dSmrg                                   GLsizei numTextureNames,
209af69d88dSmrg                                   const GLuint *textureNames)
210af69d88dSmrg{
211af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
212af69d88dSmrg
213af69d88dSmrg   if (numTextureNames != 1) {
214af69d88dSmrg      _mesa_error(ctx, GL_INVALID_VALUE, "VDPAURegisterVideoSurfaceNV");
215af69d88dSmrg      return (GLintptr)NULL;
216af69d88dSmrg   }
217af69d88dSmrg
218af69d88dSmrg   return register_surface(ctx, true, vdpSurface, target,
219af69d88dSmrg                           numTextureNames, textureNames);
220af69d88dSmrg}
221af69d88dSmrg
222af69d88dSmrgGLboolean GLAPIENTRY
223af69d88dSmrg_mesa_VDPAUIsSurfaceNV(GLintptr surface)
224af69d88dSmrg{
225af69d88dSmrg   struct vdp_surface *surf = (struct vdp_surface *)surface;
226af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
227af69d88dSmrg
228af69d88dSmrg   if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
229af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUIsSurfaceNV");
230af69d88dSmrg      return false;
231af69d88dSmrg   }
232af69d88dSmrg
23301e04c3fSmrg   if (!_mesa_set_search(ctx->vdpSurfaces, surf)) {
234af69d88dSmrg      return false;
235af69d88dSmrg   }
236af69d88dSmrg
237af69d88dSmrg   return true;
238af69d88dSmrg}
239af69d88dSmrg
240af69d88dSmrgvoid GLAPIENTRY
241af69d88dSmrg_mesa_VDPAUUnregisterSurfaceNV(GLintptr surface)
242af69d88dSmrg{
243af69d88dSmrg   struct vdp_surface *surf = (struct vdp_surface *)surface;
244af69d88dSmrg   struct set_entry *entry;
245af69d88dSmrg   int i;
246af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
247af69d88dSmrg
248af69d88dSmrg   if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
249af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUUnregisterSurfaceNV");
250af69d88dSmrg      return;
251af69d88dSmrg   }
252af69d88dSmrg
253af69d88dSmrg   /* according to the spec it's ok when this is zero */
254af69d88dSmrg   if (surface == 0)
255af69d88dSmrg      return;
256af69d88dSmrg
25701e04c3fSmrg   entry = _mesa_set_search(ctx->vdpSurfaces, surf);
258af69d88dSmrg   if (!entry) {
259af69d88dSmrg      _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUUnregisterSurfaceNV");
260af69d88dSmrg      return;
261af69d88dSmrg   }
262af69d88dSmrg
263af69d88dSmrg   for (i = 0; i < MAX_TEXTURES; i++) {
264af69d88dSmrg      if (surf->textures[i]) {
265af69d88dSmrg         surf->textures[i]->Immutable = GL_FALSE;
266af69d88dSmrg         _mesa_reference_texobj(&surf->textures[i], NULL);
267af69d88dSmrg      }
268af69d88dSmrg   }
269af69d88dSmrg
270af69d88dSmrg   _mesa_set_remove(ctx->vdpSurfaces, entry);
271af69d88dSmrg   free(surf);
272af69d88dSmrg}
273af69d88dSmrg
274af69d88dSmrgvoid GLAPIENTRY
275af69d88dSmrg_mesa_VDPAUGetSurfaceivNV(GLintptr surface, GLenum pname, GLsizei bufSize,
276af69d88dSmrg                          GLsizei *length, GLint *values)
277af69d88dSmrg{
278af69d88dSmrg   struct vdp_surface *surf = (struct vdp_surface *)surface;
279af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
280af69d88dSmrg
281af69d88dSmrg   if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
282af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUGetSurfaceivNV");
283af69d88dSmrg      return;
284af69d88dSmrg   }
285af69d88dSmrg
28601e04c3fSmrg   if (!_mesa_set_search(ctx->vdpSurfaces, surf)) {
287af69d88dSmrg      _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUGetSurfaceivNV");
288af69d88dSmrg      return;
289af69d88dSmrg   }
290af69d88dSmrg
291af69d88dSmrg   if (pname != GL_SURFACE_STATE_NV) {
292af69d88dSmrg      _mesa_error(ctx, GL_INVALID_ENUM, "VDPAUGetSurfaceivNV");
293af69d88dSmrg      return;
294af69d88dSmrg   }
295af69d88dSmrg
296af69d88dSmrg   if (bufSize < 1) {
297af69d88dSmrg      _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUGetSurfaceivNV");
298af69d88dSmrg      return;
299af69d88dSmrg   }
300af69d88dSmrg
301af69d88dSmrg   values[0] = surf->state;
302af69d88dSmrg
303af69d88dSmrg   if (length != NULL)
304af69d88dSmrg      *length = 1;
305af69d88dSmrg}
306af69d88dSmrg
307af69d88dSmrgvoid GLAPIENTRY
308af69d88dSmrg_mesa_VDPAUSurfaceAccessNV(GLintptr surface, GLenum access)
309af69d88dSmrg{
310af69d88dSmrg   struct vdp_surface *surf = (struct vdp_surface *)surface;
311af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
312af69d88dSmrg
313af69d88dSmrg   if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
314af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUSurfaceAccessNV");
315af69d88dSmrg      return;
316af69d88dSmrg   }
317af69d88dSmrg
31801e04c3fSmrg   if (!_mesa_set_search(ctx->vdpSurfaces, surf)) {
319af69d88dSmrg      _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUSurfaceAccessNV");
320af69d88dSmrg      return;
321af69d88dSmrg   }
322af69d88dSmrg
323af69d88dSmrg   if (access != GL_READ_ONLY && access != GL_WRITE_ONLY &&
324af69d88dSmrg       access != GL_READ_WRITE) {
325af69d88dSmrg
326af69d88dSmrg      _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUSurfaceAccessNV");
327af69d88dSmrg      return;
328af69d88dSmrg   }
329af69d88dSmrg
330af69d88dSmrg   if (surf->state == GL_SURFACE_MAPPED_NV) {
331af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUSurfaceAccessNV");
332af69d88dSmrg      return;
333af69d88dSmrg   }
334af69d88dSmrg
335af69d88dSmrg   surf->access = access;
336af69d88dSmrg}
337af69d88dSmrg
338af69d88dSmrgvoid GLAPIENTRY
339af69d88dSmrg_mesa_VDPAUMapSurfacesNV(GLsizei numSurfaces, const GLintptr *surfaces)
340af69d88dSmrg{
341af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
342af69d88dSmrg   int i;
343af69d88dSmrg
344af69d88dSmrg   if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
345af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUUnmapSurfacesNV");
346af69d88dSmrg      return;
347af69d88dSmrg   }
348af69d88dSmrg
349af69d88dSmrg   for (i = 0; i < numSurfaces; ++i) {
350af69d88dSmrg      struct vdp_surface *surf = (struct vdp_surface *)surfaces[i];
351af69d88dSmrg
35201e04c3fSmrg      if (!_mesa_set_search(ctx->vdpSurfaces, surf)) {
353af69d88dSmrg         _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUSurfaceAccessNV");
354af69d88dSmrg         return;
355af69d88dSmrg      }
356af69d88dSmrg
357af69d88dSmrg      if (surf->state == GL_SURFACE_MAPPED_NV) {
358af69d88dSmrg         _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUSurfaceAccessNV");
359af69d88dSmrg         return;
360af69d88dSmrg      }
361af69d88dSmrg   }
362af69d88dSmrg
363af69d88dSmrg   for (i = 0; i < numSurfaces; ++i) {
364af69d88dSmrg      struct vdp_surface *surf = (struct vdp_surface *)surfaces[i];
365af69d88dSmrg      unsigned numTextureNames = surf->output ? 1 : 4;
366af69d88dSmrg      unsigned j;
367af69d88dSmrg
368af69d88dSmrg      for (j = 0; j < numTextureNames; ++j) {
369af69d88dSmrg         struct gl_texture_object *tex = surf->textures[j];
370af69d88dSmrg         struct gl_texture_image *image;
371af69d88dSmrg
372af69d88dSmrg         _mesa_lock_texture(ctx, tex);
373af69d88dSmrg         image = _mesa_get_tex_image(ctx, tex, surf->target, 0);
374af69d88dSmrg         if (!image) {
375af69d88dSmrg            _mesa_error(ctx, GL_OUT_OF_MEMORY, "VDPAUMapSurfacesNV");
376af69d88dSmrg            _mesa_unlock_texture(ctx, tex);
377af69d88dSmrg            return;
378af69d88dSmrg         }
379af69d88dSmrg
380af69d88dSmrg         ctx->Driver.FreeTextureImageBuffer(ctx, image);
381af69d88dSmrg
382af69d88dSmrg         ctx->Driver.VDPAUMapSurface(ctx, surf->target, surf->access,
383af69d88dSmrg                                     surf->output, tex, image,
384af69d88dSmrg                                     surf->vdpSurface, j);
385af69d88dSmrg
386af69d88dSmrg         _mesa_unlock_texture(ctx, tex);
387af69d88dSmrg      }
388af69d88dSmrg      surf->state = GL_SURFACE_MAPPED_NV;
389af69d88dSmrg   }
390af69d88dSmrg}
391af69d88dSmrg
392af69d88dSmrgvoid GLAPIENTRY
393af69d88dSmrg_mesa_VDPAUUnmapSurfacesNV(GLsizei numSurfaces, const GLintptr *surfaces)
394af69d88dSmrg{
395af69d88dSmrg   GET_CURRENT_CONTEXT(ctx);
396af69d88dSmrg   int i;
397af69d88dSmrg
398af69d88dSmrg   if (!ctx->vdpDevice || !ctx->vdpGetProcAddress || !ctx->vdpSurfaces) {
399af69d88dSmrg      _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUUnmapSurfacesNV");
400af69d88dSmrg      return;
401af69d88dSmrg   }
402af69d88dSmrg
403af69d88dSmrg   for (i = 0; i < numSurfaces; ++i) {
404af69d88dSmrg      struct vdp_surface *surf = (struct vdp_surface *)surfaces[i];
405af69d88dSmrg
40601e04c3fSmrg      if (!_mesa_set_search(ctx->vdpSurfaces, surf)) {
407af69d88dSmrg         _mesa_error(ctx, GL_INVALID_VALUE, "VDPAUSurfaceAccessNV");
408af69d88dSmrg         return;
409af69d88dSmrg      }
410af69d88dSmrg
411af69d88dSmrg      if (surf->state != GL_SURFACE_MAPPED_NV) {
412af69d88dSmrg         _mesa_error(ctx, GL_INVALID_OPERATION, "VDPAUSurfaceAccessNV");
413af69d88dSmrg         return;
414af69d88dSmrg      }
415af69d88dSmrg   }
416af69d88dSmrg
417af69d88dSmrg   for (i = 0; i < numSurfaces; ++i) {
418af69d88dSmrg      struct vdp_surface *surf = (struct vdp_surface *)surfaces[i];
419af69d88dSmrg      unsigned numTextureNames = surf->output ? 1 : 4;
420af69d88dSmrg      unsigned j;
421af69d88dSmrg
422af69d88dSmrg      for (j = 0; j < numTextureNames; ++j) {
423af69d88dSmrg         struct gl_texture_object *tex = surf->textures[j];
424af69d88dSmrg         struct gl_texture_image *image;
425af69d88dSmrg
426af69d88dSmrg         _mesa_lock_texture(ctx, tex);
427af69d88dSmrg
42801e04c3fSmrg         image = _mesa_select_tex_image(tex, surf->target, 0);
429af69d88dSmrg
430af69d88dSmrg         ctx->Driver.VDPAUUnmapSurface(ctx, surf->target, surf->access,
431af69d88dSmrg                                       surf->output, tex, image,
432af69d88dSmrg                                       surf->vdpSurface, j);
433af69d88dSmrg
434af69d88dSmrg         if (image)
435af69d88dSmrg            ctx->Driver.FreeTextureImageBuffer(ctx, image);
436af69d88dSmrg
437af69d88dSmrg         _mesa_unlock_texture(ctx, tex);
438af69d88dSmrg      }
439af69d88dSmrg      surf->state = GL_SURFACE_REGISTERED_NV;
440af69d88dSmrg   }
441af69d88dSmrg}
442