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