OSDN Git Service

Update VA API to v0.24
[android-x86/hardware-intel-common-libva.git] / src / va.c
1 /*
2  * Copyright (c) 2007 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  * 
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include "X11/Xlib.h"
26 #include "va.h"
27 #include "va_backend.h"
28
29 #include "assert.h"
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <dlfcn.h>
34 #include <unistd.h>
35 #include "va_dri.h"
36
37 #define DEFAULT_DRIVER_DIR      "/usr/X11R6/lib/modules/dri"
38 #define DRIVER_EXTENSION        "_drv_video.so"
39 #define DRIVER_INIT_FUNC        "__vaDriverInit_0_24"
40
41 #define CTX(dpy) ((VADriverContextP) dpy );
42 #define ASSERT_CONTEXT(dpy) assert( vaDbgContextIsValid(dpy) )
43 #define ASSERT          assert
44 #define CHECK_VTABLE(s, ctx, func) if (!va_checkVtable(ctx->vtable.va##func, #func)) s = VA_STATUS_ERROR_UNKNOWN;
45 #define CHECK_MAXIMUM(s, ctx, var) if (!va_checkMaximum(ctx->max_##var, #var)) s = VA_STATUS_ERROR_UNKNOWN;
46
47 #define TRACE(func) if (va_debug_trace) va_infoMessage("[TR] %s\n", #func);
48
49 static VADriverContextP pDriverContexts = NULL;
50 static int va_debug_trace = 0;
51
52 static Bool vaDbgContextIsValid(VADriverContextP arg_ctx)
53 {
54   VADriverContextP ctx = pDriverContexts;
55   
56   while (ctx)
57   {
58       if (ctx == arg_ctx)
59       {
60           return True;
61       }
62       ctx = ctx->pNext;
63   }
64   return False;
65 }
66
67 VADisplay vaGetDisplay (
68     NativeDisplay native_dpy    /* implementation specific */
69 )
70 {
71   VADisplay dpy = NULL;
72   VADriverContextP ctx = pDriverContexts;
73   
74   while (ctx)
75   {
76       if (ctx->x11_dpy == (Display *)native_dpy)
77       {
78           dpy = (VADisplay) ctx;
79           break;
80       }
81       ctx = ctx->pNext;
82   }
83   
84   if (!dpy)
85   {
86       /* create new entry */
87       ctx = (VADriverContextP) calloc(1, sizeof(struct VADriverContext));
88       ctx->pNext = pDriverContexts;
89       ctx->x11_dpy = (Display *) native_dpy;
90       pDriverContexts = ctx;
91       dpy = (VADisplay) ctx;
92   }
93   
94   return dpy;
95 }
96
97 static void va_errorMessage(const char *msg, ...)
98 {
99     va_list args;
100
101     fprintf(stderr, "libva error: ");
102     va_start(args, msg);
103     vfprintf(stderr, msg, args);
104     va_end(args);
105 }
106
107 static void va_infoMessage(const char *msg, ...)
108 {
109     va_list args;
110
111     fprintf(stderr, "libva: ");
112     va_start(args, msg);
113     vfprintf(stderr, msg, args);
114     va_end(args);
115 }
116
117 static Bool va_checkVtable(void *ptr, char *function)
118 {
119     if (!ptr)
120     {
121         va_errorMessage("No valid vtable entry for va%s\n", function);
122         return False;
123     }
124     return True;
125 }
126
127 static Bool va_checkMaximum(int value, char *variable)
128 {
129     if (!value)
130     {
131         va_errorMessage("Failed to define max_%s in init\n", variable);
132         return False;
133     }
134     return True;
135 }
136
137 static VAStatus va_getDriverName(VADriverContextP ctx, char **driver_name)
138 {
139     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
140     int direct_capable;
141     int driver_major;
142     int driver_minor;
143     int driver_patch;
144     Bool result = True;
145     
146     *driver_name = NULL;
147     if (geteuid() == getuid())
148     {
149         /* don't allow setuid apps to use LIBVA_DRIVER_NAME */
150         if (getenv("LIBVA_DRIVER_NAME"))
151         {
152             /* For easier debugging */
153             *driver_name = strdup(getenv("LIBVA_DRIVER_NAME"));
154             return VA_STATUS_SUCCESS;
155         }
156     }
157     if (result)
158     {
159         result = VA_DRIQueryDirectRenderingCapable(ctx->x11_dpy, ctx->x11_screen, &direct_capable);
160         if (!result)
161         {
162             va_errorMessage("VA_DRIQueryDirectRenderingCapable failed\n");
163         }
164     }
165     if (result)
166     {
167         result = direct_capable;
168         if (!result)
169         {
170             va_errorMessage("VA_DRIQueryDirectRenderingCapable returned false\n");
171         }
172     }
173     if (result)
174     {
175         result = VA_DRIGetClientDriverName(ctx->x11_dpy, ctx->x11_screen, &driver_major, &driver_minor,
176                                             &driver_patch, driver_name);
177         if (!result)
178         {
179             va_errorMessage("VA_DRIGetClientDriverName returned false\n");
180         }
181     }
182     if (result)
183     {
184         vaStatus = VA_STATUS_SUCCESS;
185         va_infoMessage("VA_DRIGetClientDriverName: %d.%d.%d %s (screen %d)\n",
186              driver_major, driver_minor, driver_patch, *driver_name, ctx->x11_screen);
187     }
188
189     return vaStatus;
190 }
191
192 static VAStatus va_openDriver(VADriverContextP ctx, char *driver_name)
193 {
194     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
195     char *search_path;
196     char *saveptr;
197     char *driver_dir;
198     
199     if (geteuid() == getuid())
200     {
201         /* don't allow setuid apps to use LIBVA_DRIVERS_PATH */
202         search_path = getenv("LIBVA_DRIVERS_PATH");
203         if (!search_path)
204         {
205             search_path = getenv("LIBGL_DRIVERS_PATH");
206         }
207     }
208     if (!search_path)
209     {
210         search_path = DEFAULT_DRIVER_DIR;
211     }
212
213     search_path = strdup(search_path);
214     driver_dir = strtok_r(search_path, ":", &saveptr);
215     while(driver_dir)
216     {
217         void *handle = NULL;
218         char *driver_path = (char *) malloc( strlen(driver_dir) +
219                                              strlen(driver_name) +
220                                              strlen(DRIVER_EXTENSION) + 2 );
221         strcpy( driver_path, driver_dir );
222         strcat( driver_path, "/" );
223         strcat( driver_path, driver_name );
224         strcat( driver_path, DRIVER_EXTENSION );
225         
226         va_infoMessage("Trying to open %s\n", driver_path);
227
228         handle = dlopen( driver_path, RTLD_NOW | RTLD_GLOBAL );
229         if (!handle)
230         {
231             /* Don't give errors for non-existing files */
232             if (0 == access( driver_path, F_OK))
233             {   
234                 va_errorMessage("dlopen of %s failed: %s\n", driver_path, dlerror());
235             }
236         }
237         else
238         {
239             VADriverInit init_func;
240             init_func = (VADriverInit) dlsym(handle, DRIVER_INIT_FUNC);
241             if (!init_func)
242             {
243                 va_errorMessage("%s has no function %s\n", driver_path, DRIVER_INIT_FUNC);
244                 dlclose(handle);
245             }
246             else
247             {
248                 vaStatus = (*init_func)(ctx);
249
250                 if (VA_STATUS_SUCCESS == vaStatus)
251                 {
252                     CHECK_MAXIMUM(vaStatus, ctx, profiles);
253                     CHECK_MAXIMUM(vaStatus, ctx, entrypoints);
254                     CHECK_MAXIMUM(vaStatus, ctx, attributes);
255                     CHECK_MAXIMUM(vaStatus, ctx, image_formats);
256                     CHECK_MAXIMUM(vaStatus, ctx, subpic_formats);
257                     CHECK_MAXIMUM(vaStatus, ctx, display_attributes);
258                     CHECK_VTABLE(vaStatus, ctx, Terminate);
259                     CHECK_VTABLE(vaStatus, ctx, QueryConfigProfiles);
260                     CHECK_VTABLE(vaStatus, ctx, QueryConfigEntrypoints);
261                     CHECK_VTABLE(vaStatus, ctx, QueryConfigAttributes);
262                     CHECK_VTABLE(vaStatus, ctx, CreateConfig);
263                     CHECK_VTABLE(vaStatus, ctx, DestroyConfig);
264                     CHECK_VTABLE(vaStatus, ctx, GetConfigAttributes);
265                     CHECK_VTABLE(vaStatus, ctx, CreateSurfaces);
266                     CHECK_VTABLE(vaStatus, ctx, DestroySurface);
267                     CHECK_VTABLE(vaStatus, ctx, CreateContext);
268                     CHECK_VTABLE(vaStatus, ctx, DestroyContext);
269                     CHECK_VTABLE(vaStatus, ctx, CreateBuffer);
270                     CHECK_VTABLE(vaStatus, ctx, BufferData);
271                     CHECK_VTABLE(vaStatus, ctx, BufferSetNumElements);
272                     CHECK_VTABLE(vaStatus, ctx, MapBuffer);
273                     CHECK_VTABLE(vaStatus, ctx, UnmapBuffer);
274                     CHECK_VTABLE(vaStatus, ctx, DestroyBuffer);
275                     CHECK_VTABLE(vaStatus, ctx, BeginPicture);
276                     CHECK_VTABLE(vaStatus, ctx, RenderPicture);
277                     CHECK_VTABLE(vaStatus, ctx, EndPicture);
278                     CHECK_VTABLE(vaStatus, ctx, SyncSurface);
279                     CHECK_VTABLE(vaStatus, ctx, QuerySurfaceStatus);
280                     CHECK_VTABLE(vaStatus, ctx, PutSurface);
281                     CHECK_VTABLE(vaStatus, ctx, QueryImageFormats);
282                     CHECK_VTABLE(vaStatus, ctx, CreateImage);
283                     CHECK_VTABLE(vaStatus, ctx, DestroyImage);
284                     CHECK_VTABLE(vaStatus, ctx, GetImage);
285                     CHECK_VTABLE(vaStatus, ctx, PutImage);
286                     CHECK_VTABLE(vaStatus, ctx, QuerySubpictureFormats);
287                     CHECK_VTABLE(vaStatus, ctx, CreateSubpicture);
288                     CHECK_VTABLE(vaStatus, ctx, DestroySubpicture);
289                     CHECK_VTABLE(vaStatus, ctx, SetSubpictureImage);
290                     CHECK_VTABLE(vaStatus, ctx, SetSubpicturePalette);
291                     CHECK_VTABLE(vaStatus, ctx, SetSubpictureChromakey);
292                     CHECK_VTABLE(vaStatus, ctx, SetSubpictureGlobalAlpha);
293                     CHECK_VTABLE(vaStatus, ctx, AssociateSubpicture);
294                     CHECK_VTABLE(vaStatus, ctx, QueryDisplayAttributes);
295                     CHECK_VTABLE(vaStatus, ctx, GetDisplayAttributes);
296                     CHECK_VTABLE(vaStatus, ctx, SetDisplayAttributes);
297                     CHECK_VTABLE(vaStatus, ctx, DbgCopySurfaceToBuffer);
298                 }
299                 if (VA_STATUS_SUCCESS != vaStatus)
300                 {
301                     va_errorMessage("%s init failed\n", driver_path);
302                     dlclose(handle);
303                 }
304                 if (VA_STATUS_SUCCESS == vaStatus)
305                 {
306                     ctx->handle = handle;
307                 }
308                 free(driver_path);
309                 break;
310             }
311         }
312         free(driver_path);
313         
314         driver_dir = strtok_r(NULL, ":", &saveptr);
315     }
316     
317     free(search_path);    
318     
319     return vaStatus;
320 }
321
322
323 /*
324  * Returns a short english description of error_status
325  */
326 const char *vaErrorStr(VAStatus error_status)
327 {
328     switch(error_status)
329     {
330         case VA_STATUS_SUCCESS:
331             return "success (no error)";
332         case VA_STATUS_ERROR_ALLOCATION_FAILED:
333             return "resource allocation failed";
334         case VA_STATUS_ERROR_INVALID_CONFIG:
335             return "invalid VAConfigID";
336         case VA_STATUS_ERROR_INVALID_CONTEXT:
337             return "invalid VAContextID";
338         case VA_STATUS_ERROR_INVALID_SURFACE:
339             return "invalid VASurfaceID";
340         case VA_STATUS_ERROR_INVALID_BUFFER:
341             return "invalid VABufferID";
342         case VA_STATUS_ERROR_ATTR_NOT_SUPPORTED:
343             return "attribute not supported";
344         case VA_STATUS_ERROR_MAX_NUM_EXCEEDED:
345             return "list argument exceeds maximum number";
346         case VA_STATUS_ERROR_UNSUPPORTED_PROFILE:
347             return "the requested VAProfile is not supported";
348         case VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT:
349             return "the requested VAEntryPoint is not supported";
350         case VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT:
351             return "the requested RT Format is not supported";
352         case VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE:
353             return "the requested VABufferType is not supported";
354         case VA_STATUS_ERROR_UNKNOWN:
355             return "unknown libva error";
356     }
357     return "unknown libva error / description missing";
358 }
359       
360 VAStatus vaInitialize (
361     VADisplay dpy,
362     int *major_version,  /* out */
363     int *minor_version   /* out */
364 )
365 {
366   VADriverContextP ctx = CTX(dpy);
367   char *driver_name = NULL;
368   VAStatus vaStatus;
369   
370   ASSERT_CONTEXT(ctx);
371
372   va_debug_trace = (getenv("LIBVA_DEBUG_TRACE") != NULL);
373
374   vaStatus = va_getDriverName(ctx, &driver_name);
375   va_infoMessage("va_getDriverName() returns %d\n", vaStatus);
376   
377   if (VA_STATUS_SUCCESS == vaStatus)
378   {
379       vaStatus = va_openDriver(ctx, driver_name);
380       va_infoMessage("va_openDriver() returns %d\n", vaStatus);
381       
382       *major_version = ctx->version_major;
383       *minor_version = ctx->version_minor;
384   }
385
386   if (driver_name)
387   {
388       XFree(driver_name);
389   }
390   return vaStatus;
391 }
392
393
394 /*
395  * After this call, all library internal resources will be cleaned up
396  */ 
397 VAStatus vaTerminate (
398     VADisplay dpy
399 )
400 {
401   VAStatus vaStatus = VA_STATUS_SUCCESS;
402   VADriverContextP old_ctx = CTX(dpy);
403   ASSERT_CONTEXT(old_ctx);
404
405   if (old_ctx->handle)
406   {
407       vaStatus = old_ctx->vtable.vaTerminate(old_ctx);
408       dlclose(old_ctx->handle);
409       old_ctx->handle = NULL;
410   }
411
412   if (VA_STATUS_SUCCESS == vaStatus)
413   {
414       VADriverContextP *ctx = &pDriverContexts;
415
416       /* Throw away old_ctx */
417       while (*ctx)
418       {
419           if (*ctx == old_ctx)
420           {
421               *ctx = old_ctx->pNext;
422               old_ctx->pNext = NULL;
423               break;
424           }
425           ctx = &((*ctx)->pNext);
426       }
427       free(old_ctx);
428   }
429   return vaStatus;
430 }
431
432 /* Get maximum number of profiles supported by the implementation */
433 int vaMaxNumProfiles (
434     VADisplay dpy
435 )
436 {
437   VADriverContextP ctx = CTX(dpy);
438   ASSERT_CONTEXT(ctx);
439   
440   return ctx->max_profiles;
441 }
442
443 /* Get maximum number of entrypoints supported by the implementation */
444 int vaMaxNumEntrypoints (
445     VADisplay dpy
446 )
447 {
448   VADriverContextP ctx = CTX(dpy);
449   ASSERT_CONTEXT(ctx);
450   
451   return ctx->max_entrypoints;
452 }
453
454
455 /* Get maximum number of attributs supported by the implementation */
456 int vaMaxNumConfigAttributes (
457     VADisplay dpy
458 )
459 {
460   VADriverContextP ctx = CTX(dpy);
461   ASSERT_CONTEXT(ctx);
462   
463   return ctx->max_attributes;
464 }
465
466 VAStatus vaQueryConfigEntrypoints (
467     VADisplay dpy,
468     VAProfile profile,
469     VAEntrypoint *entrypoints,  /* out */
470     int *num_entrypoints        /* out */
471 )
472 {
473   VADriverContextP ctx = CTX(dpy);
474   ASSERT_CONTEXT(ctx);
475
476   TRACE(vaQueryConfigEntrypoints);
477   return ctx->vtable.vaQueryConfigEntrypoints ( ctx, profile, entrypoints, num_entrypoints);
478 }
479
480 VAStatus vaGetConfigAttributes (
481     VADisplay dpy,
482     VAProfile profile,
483     VAEntrypoint entrypoint,
484     VAConfigAttrib *attrib_list, /* in/out */
485     int num_attribs
486 )
487 {
488   VADriverContextP ctx = CTX(dpy);
489   ASSERT_CONTEXT(ctx);
490
491   TRACE(vaGetConfigAttributes);
492   return ctx->vtable.vaGetConfigAttributes ( ctx, profile, entrypoint, attrib_list, num_attribs );
493 }
494
495 VAStatus vaQueryConfigProfiles (
496     VADisplay dpy,
497     VAProfile *profile_list,    /* out */
498     int *num_profiles           /* out */
499 )
500 {
501   VADriverContextP ctx = CTX(dpy);
502   ASSERT_CONTEXT(ctx);
503
504   TRACE(vaQueryConfigProfiles);
505   return ctx->vtable.vaQueryConfigProfiles ( ctx, profile_list, num_profiles );
506 }
507
508 VAStatus vaCreateConfig (
509     VADisplay dpy,
510     VAProfile profile, 
511     VAEntrypoint entrypoint, 
512     VAConfigAttrib *attrib_list,
513     int num_attribs,
514     VAConfigID *config_id /* out */
515 )
516 {
517   VADriverContextP ctx = CTX(dpy);
518   ASSERT_CONTEXT(ctx);
519
520   TRACE(vaCreateConfig);
521   return ctx->vtable.vaCreateConfig ( ctx, profile, entrypoint, attrib_list, num_attribs, config_id );
522 }
523
524 VAStatus vaDestroyConfig (
525     VADisplay dpy,
526     VAConfigID config_id
527 )
528 {
529   VADriverContextP ctx = CTX(dpy);
530   ASSERT_CONTEXT(ctx);
531
532   TRACE(vaDestroyConfig);
533   return ctx->vtable.vaDestroyConfig ( ctx, config_id );
534 }
535
536 VAStatus vaQueryConfigAttributes (
537     VADisplay dpy,
538     VAConfigID config_id, 
539     VAProfile *profile,         /* out */
540     VAEntrypoint *entrypoint,   /* out */
541     VAConfigAttrib *attrib_list,/* out */
542     int *num_attribs            /* out */
543 )
544 {
545   VADriverContextP ctx = CTX(dpy);
546   ASSERT_CONTEXT(ctx);
547
548   TRACE(vaQueryConfigAttributes);
549   return ctx->vtable.vaQueryConfigAttributes( ctx, config_id, profile, entrypoint, attrib_list, num_attribs);
550 }
551
552 VAStatus vaCreateSurfaces (
553     VADisplay dpy,
554     int width,
555     int height,
556     int format,
557     int num_surfaces,
558     VASurface *surfaces /* out */
559 )
560 {
561   VADriverContextP ctx = CTX(dpy);
562   ASSERT_CONTEXT(ctx);
563
564   TRACE(vaCreateSurfaces);
565   return ctx->vtable.vaCreateSurfaces( ctx, width, height, format, num_surfaces, surfaces );
566 }
567
568 VAStatus vaDestroySurface (
569     VADisplay dpy,
570     VASurface *surface_list,
571     int num_surfaces
572 )
573 {
574   VADriverContextP ctx = CTX(dpy);
575   ASSERT_CONTEXT(ctx);
576
577   TRACE(vaDestroySurface);
578   return ctx->vtable.vaDestroySurface( ctx, surface_list, num_surfaces );
579 }
580
581 VAStatus vaCreateContext (
582     VADisplay dpy,
583     VAConfigID config_id,
584     int picture_width,
585     int picture_height,
586     int flag,
587     VASurface *render_targets,
588     int num_render_targets,
589     VAContext *context          /* out */
590 )
591 {
592   VADriverContextP ctx = CTX(dpy);
593   ASSERT_CONTEXT(ctx);
594
595   TRACE(vaCreateContext);
596   return ctx->vtable.vaCreateContext( ctx, config_id, picture_width, picture_height,
597                                       flag, render_targets, num_render_targets, context );
598 }
599
600 VAStatus vaDestroyContext (
601     VADisplay dpy,
602     VAContext *context
603 )
604 {
605   VADriverContextP ctx = CTX(dpy);
606   ASSERT_CONTEXT(ctx);
607
608   TRACE(vaDestroyContext);
609   return ctx->vtable.vaDestroyContext( ctx, context );
610 }
611
612 VAStatus vaCreateBuffer (
613     VADisplay dpy,
614     VABufferType type,  /* in */
615     VABufferID *buf_id  /* out */
616 )
617 {
618   VADriverContextP ctx = CTX(dpy);
619   ASSERT_CONTEXT(ctx);
620
621   TRACE(vaCreateBuffer);
622   return ctx->vtable.vaCreateBuffer( ctx, type, buf_id);
623 }
624
625 VAStatus vaBufferData (
626     VADisplay dpy,
627     VABufferID buf_id,  /* in */
628     unsigned int size,  /* in */
629     unsigned int num_elements, /* in */
630     void *data          /* in */
631 )
632 {
633   VADriverContextP ctx = CTX(dpy);
634   ASSERT_CONTEXT(ctx);
635
636   TRACE(vaBufferData);
637   return ctx->vtable.vaBufferData( ctx, buf_id, size, num_elements, data);
638 }
639
640 VAStatus vaBufferSetNumElements (
641     VADisplay dpy,
642     VABufferID buf_id,  /* in */
643     unsigned int num_elements /* in */
644 )
645 {
646   VADriverContextP ctx = CTX(dpy);
647   ASSERT_CONTEXT(ctx);
648
649   TRACE(vaBufferSetNumElements);
650   return ctx->vtable.vaBufferSetNumElements( ctx, buf_id, num_elements );
651 }
652
653
654 VAStatus vaMapBuffer (
655     VADisplay dpy,
656     VABufferID buf_id,  /* in */
657     void **pbuf         /* out */
658 )
659 {
660   VADriverContextP ctx = CTX(dpy);
661   ASSERT_CONTEXT(ctx);
662
663   TRACE(vaMapBuffer);
664   return ctx->vtable.vaMapBuffer( ctx, buf_id, pbuf );
665 }
666
667 VAStatus vaUnmapBuffer (
668     VADisplay dpy,
669     VABufferID buf_id   /* in */
670 )
671 {
672   VADriverContextP ctx = CTX(dpy);
673   ASSERT_CONTEXT(ctx);
674
675   TRACE(vaUnmapBuffer);
676   return ctx->vtable.vaUnmapBuffer( ctx, buf_id );
677 }
678
679 VAStatus vaDestroyBuffer (
680     VADisplay dpy,
681     VABufferID buffer_id
682 )
683 {
684   VADriverContextP ctx = CTX(dpy);
685   ASSERT_CONTEXT(ctx);
686
687   TRACE(vaDestroyBuffer);
688   return ctx->vtable.vaDestroyBuffer( ctx, buffer_id );
689 }
690
691 VAStatus vaBeginPicture (
692     VADisplay dpy,
693     VAContext *context,
694     VASurface *render_target
695 )
696 {
697   VADriverContextP ctx = CTX(dpy);
698   ASSERT_CONTEXT(ctx);
699
700   TRACE(vaBeginPicture);
701   return ctx->vtable.vaBeginPicture( ctx, context, render_target );
702 }
703
704 VAStatus vaRenderPicture (
705     VADisplay dpy,
706     VAContext *context,
707     VABufferID *buffers,
708     int num_buffers
709 )
710 {
711   VADriverContextP ctx = CTX(dpy);
712   ASSERT_CONTEXT(ctx);
713
714   TRACE(vaRenderPicture);
715   return ctx->vtable.vaRenderPicture( ctx, context, buffers, num_buffers );
716 }
717
718 VAStatus vaEndPicture (
719     VADisplay dpy,
720     VAContext *context
721 )
722 {
723   VADriverContextP ctx = CTX(dpy);
724   ASSERT_CONTEXT(ctx);
725
726   TRACE(vaEndPicture);
727   return ctx->vtable.vaEndPicture( ctx, context );
728 }
729
730 VAStatus vaSyncSurface (
731     VADisplay dpy,
732     VAContext *context,
733     VASurface *render_target
734 )
735 {
736   VADriverContextP ctx = CTX(dpy);
737   ASSERT_CONTEXT(ctx);
738
739   TRACE(vaSyncSurface);
740   return ctx->vtable.vaSyncSurface( ctx, context, render_target );
741 }
742
743 VAStatus vaQuerySurfaceStatus (
744     VADisplay dpy,
745     VAContext *context,
746     VASurface *render_target,
747     VASurfaceStatus *status     /* out */
748 )
749 {
750   VADriverContextP ctx = CTX(dpy);
751   ASSERT_CONTEXT(ctx);
752
753   TRACE(vaQuerySurfaceStatus);
754   return ctx->vtable.vaQuerySurfaceStatus( ctx, context, render_target, status );
755 }
756
757 VAStatus vaPutSurface (
758     VADisplay dpy,
759     VASurface *surface,
760     Drawable draw, /* X Drawable */
761     short srcx,
762     short srcy,
763     unsigned short srcw,
764     unsigned short srch,
765     short destx,
766     short desty,
767     unsigned short destw,
768     unsigned short desth,
769     VARectangle *cliprects, /* client supplied clip list */
770     unsigned int number_cliprects, /* number of clip rects in the clip list */
771     unsigned int flags /* de-interlacing flags */
772 )
773 {
774   VADriverContextP ctx = CTX(dpy);
775   ASSERT_CONTEXT(ctx);
776
777   TRACE(vaPutSurface);
778   return ctx->vtable.vaPutSurface( ctx, surface, draw, srcx, srcy, srcw, srch,
779                                    destx, desty, destw, desth,
780                                    cliprects, number_cliprects, flags );
781 }
782
783 /* Get maximum number of image formats supported by the implementation */
784 int vaMaxNumImageFormats (
785     VADisplay dpy
786 )
787 {
788   VADriverContextP ctx = CTX(dpy);
789   ASSERT_CONTEXT(ctx);
790   
791   return ctx->max_image_formats;
792 }
793
794 VAStatus vaQueryImageFormats (
795     VADisplay dpy,
796     VAImageFormat *format_list, /* out */
797     int *num_formats            /* out */
798 )
799 {
800   VADriverContextP ctx = CTX(dpy);
801   ASSERT_CONTEXT(ctx);
802
803   TRACE(vaQueryImageFormats);
804   return ctx->vtable.vaQueryImageFormats ( ctx, format_list, num_formats);
805 }
806
807 /* 
808  * The width and height fields returned in the VAImage structure may get 
809  * enlarged for some YUV formats. The size of the data buffer that needs
810  * to be allocated will be given in the "data_size" field in VAImage.
811  * Image data is not allocated by this function.  The client should
812  * allocate the memory and fill in the VAImage structure's data field
813  * after looking at "data_size" returned from the library.
814  */
815 VAStatus vaCreateImage (
816     VADisplay dpy,
817     VAImageFormat *format,
818     int width,
819     int height,
820     VAImage *image      /* out */
821 )
822 {
823   VADriverContextP ctx = CTX(dpy);
824   ASSERT_CONTEXT(ctx);
825
826   TRACE(vaCreateImage);
827   return ctx->vtable.vaCreateImage ( ctx, format, width, height, image);
828 }
829
830 /*
831  * Should call DestroyImage before destroying the surface it is bound to
832  */
833 VAStatus vaDestroyImage (
834     VADisplay dpy,
835     VAImage *image
836 )
837 {
838   VADriverContextP ctx = CTX(dpy);
839   ASSERT_CONTEXT(ctx);
840
841   TRACE(vaDestroyImage);
842   return ctx->vtable.vaDestroyImage ( ctx, image);
843 }
844
845 /*
846  * Retrieve surface data into a VAImage
847  * Image must be in a format supported by the implementation
848  */
849 VAStatus vaGetImage (
850     VADisplay dpy,
851     VASurface *surface,
852     int x,      /* coordinates of the upper left source pixel */
853     int y,
854     unsigned int width, /* width and height of the region */
855     unsigned int height,
856     VAImage *image
857 )
858 {
859   VADriverContextP ctx = CTX(dpy);
860   ASSERT_CONTEXT(ctx);
861
862   TRACE(vaGetImage);
863   return ctx->vtable.vaGetImage ( ctx, surface, x, y, width, height, image);
864 }
865
866 /*
867  * Copy data from a VAImage to a surface
868  * Image must be in a format supported by the implementation
869  */
870 VAStatus vaPutImage (
871     VADisplay dpy,
872     VASurface *surface,
873     VAImage *image,
874     int src_x,
875     int src_y,
876     unsigned int width,
877     unsigned int height,
878     int dest_x,
879     int dest_y
880 )
881 {
882   VADriverContextP ctx = CTX(dpy);
883   ASSERT_CONTEXT(ctx);
884
885   TRACE(vaPutImage);
886   return ctx->vtable.vaPutImage ( ctx, surface, image, src_x, src_y, width, height, dest_x, dest_y );
887 }
888
889 /* Get maximum number of subpicture formats supported by the implementation */
890 int vaMaxNumSubpictureFormats (
891     VADisplay dpy
892 )
893 {
894   VADriverContextP ctx = CTX(dpy);
895   ASSERT_CONTEXT(ctx);
896   
897   return ctx->max_subpic_formats;
898 }
899
900 /* 
901  * Query supported subpicture formats 
902  * The caller must provide a "format_list" array that can hold at
903  * least vaMaxNumSubpictureFormats() entries. The flags arrary holds the flag 
904  * for each format to indicate additional capabilities for that format. The actual 
905  * number of formats returned in "format_list" is returned in "num_formats".
906  */
907 VAStatus vaQuerySubpictureFormats (
908     VADisplay dpy,
909     VAImageFormat *format_list, /* out */
910     unsigned int *flags,        /* out */
911     unsigned int *num_formats   /* out */
912 )
913 {
914   VADriverContextP ctx = CTX(dpy);
915   ASSERT_CONTEXT(ctx);
916
917   TRACE(vaQuerySubpictureFormats);
918   return ctx->vtable.vaQuerySubpictureFormats ( ctx, format_list, flags, num_formats);
919 }
920
921 /* 
922  * Subpictures are created with an image associated. 
923  */
924 VAStatus vaCreateSubpicture (
925     VADisplay dpy,
926     VAImage *image,
927     VASubpicture *subpicture    /* out */
928 )
929 {
930   VADriverContextP ctx = CTX(dpy);
931   ASSERT_CONTEXT(ctx);
932
933   TRACE(vaCreateSubpicture);
934   return ctx->vtable.vaCreateSubpicture ( ctx, image, subpicture );
935 }
936
937 /*
938  * Destroy the subpicture before destroying the image it is assocated to
939  */
940 VAStatus vaDestroySubpicture (
941     VADisplay dpy,
942     VASubpicture *subpicture
943 )
944 {
945   VADriverContextP ctx = CTX(dpy);
946   ASSERT_CONTEXT(ctx);
947
948   TRACE(vaDestroySubpicture);
949   return ctx->vtable.vaDestroySubpicture ( ctx, subpicture);
950 }
951
952 VAStatus vaSetSubpictureImage (
953     VADisplay dpy,
954     VASubpicture *subpicture,
955     VAImage *image
956 )
957 {
958   VADriverContextP ctx = CTX(dpy);
959   ASSERT_CONTEXT(ctx);
960
961   TRACE(vaSetSubpictureImage);
962   return ctx->vtable.vaSetSubpictureImage ( ctx, subpicture, image);
963 }
964
965
966 VAStatus vaSetSubpicturePalette (
967     VADisplay dpy,
968     VASubpicture *subpicture,
969     /* 
970      * pointer to an array holding the palette data.  The size of the array is 
971      * num_palette_entries * entry_bytes in size.  The order of the components
972      * in the palette is described by the component_order in VASubpicture struct
973      */
974     unsigned char *palette 
975 )
976 {
977   VADriverContextP ctx = CTX(dpy);
978   ASSERT_CONTEXT(ctx);
979
980   TRACE(vaSetSubpicturePalette);
981   return ctx->vtable.vaSetSubpicturePalette ( ctx, subpicture, palette);
982 }
983
984 /*
985  * If chromakey is enabled, then the area where the source value falls within
986  * the chromakey [min, max] range is transparent
987  */
988 VAStatus vaSetSubpictureChromakey (
989     VADisplay dpy,
990     VASubpicture *subpicture,
991     unsigned int chromakey_min,
992     unsigned int chromakey_max 
993 )
994 {
995   VADriverContextP ctx = CTX(dpy);
996   ASSERT_CONTEXT(ctx);
997
998   TRACE(vaSetSubpictureChromakey);
999   return ctx->vtable.vaSetSubpictureChromakey ( ctx, subpicture, chromakey_min, chromakey_max );
1000 }
1001
1002
1003 /*
1004  * Global alpha value is between 0 and 1. A value of 1 means fully opaque and 
1005  * a value of 0 means fully transparent. If per-pixel alpha is also specified then
1006  * the overall alpha is per-pixel alpha multiplied by the global alpha
1007  */
1008 VAStatus vaSetSubpictureGlobalAlpha (
1009     VADisplay dpy,
1010     VASubpicture *subpicture,
1011     float global_alpha 
1012 )
1013 {
1014   VADriverContextP ctx = CTX(dpy);
1015   ASSERT_CONTEXT(ctx);
1016
1017   TRACE(vaSetSubpictureGlobalAlpha);
1018   return ctx->vtable.vaSetSubpictureGlobalAlpha ( ctx, subpicture, global_alpha );
1019 }
1020
1021 /*
1022   vaAssociateSubpicture associates the subpicture with the target_surface.
1023   It defines the region mapping between the subpicture and the target 
1024   surface through source and destination rectangles (with the same width and height).
1025   Both will be displayed at the next call to vaPutSurface.  Additional
1026   associations before the call to vaPutSurface simply overrides the association.
1027 */
1028 VAStatus vaAssociateSubpicture (
1029     VADisplay dpy,
1030     VASurface *target_surface,
1031     VASubpicture *subpicture,
1032     short src_x, /* upper left offset in subpicture */
1033     short src_y,
1034     short dest_x, /* upper left offset in surface */
1035     short dest_y,
1036     unsigned short width,
1037     unsigned short height,
1038     /*
1039      * whether to enable chroma-keying or global-alpha
1040      * see VA_SUBPICTURE_XXX values
1041      */
1042     unsigned int flags
1043 )
1044 {
1045   VADriverContextP ctx = CTX(dpy);
1046   ASSERT_CONTEXT(ctx);
1047
1048   TRACE(vaAssociateSubpicture);
1049   return ctx->vtable.vaAssociateSubpicture ( ctx, target_surface, subpicture, src_x, src_y, dest_x, dest_y, width, height, flags );
1050 }
1051
1052 /* Get maximum number of display attributes supported by the implementation */
1053 int vaMaxNumDisplayAttributes (
1054     VADisplay dpy
1055 )
1056 {
1057   VADriverContextP ctx = CTX(dpy);
1058   ASSERT_CONTEXT(ctx);
1059   
1060   return ctx->max_display_attributes;
1061 }
1062
1063 /* 
1064  * Query display attributes 
1065  * The caller must provide a "attr_list" array that can hold at
1066  * least vaMaxNumDisplayAttributes() entries. The actual number of attributes
1067  * returned in "attr_list" is returned in "num_attributes".
1068  */
1069 VAStatus vaQueryDisplayAttributes (
1070     VADisplay dpy,
1071     VADisplayAttribute *attr_list,      /* out */
1072     int *num_attributes                 /* out */
1073 )
1074 {
1075   VADriverContextP ctx = CTX(dpy);
1076   ASSERT_CONTEXT(ctx);
1077
1078   TRACE(vaQueryDisplayAttributes);
1079   return ctx->vtable.vaQueryDisplayAttributes ( ctx, attr_list, num_attributes );
1080 }
1081
1082 /* 
1083  * Get display attributes 
1084  * This function returns the current attribute values in "attr_list".
1085  * Only attributes returned with VA_DISPLAY_ATTRIB_GETTABLE set in the "flags" field
1086  * from vaQueryDisplayAttributes() can have their values retrieved.  
1087  */
1088 VAStatus vaGetDisplayAttributes (
1089     VADisplay dpy,
1090     VADisplayAttribute *attr_list,      /* in/out */
1091     int num_attributes
1092 )
1093 {
1094   VADriverContextP ctx = CTX(dpy);
1095   ASSERT_CONTEXT(ctx);
1096
1097   TRACE(vaGetDisplayAttributes);
1098   return ctx->vtable.vaGetDisplayAttributes ( ctx, attr_list, num_attributes );
1099 }
1100
1101 /* 
1102  * Set display attributes 
1103  * Only attributes returned with VA_DISPLAY_ATTRIB_SETTABLE set in the "flags" field
1104  * from vaQueryDisplayAttributes() can be set.  If the attribute is not settable or 
1105  * the value is out of range, the function returns VA_STATUS_ERROR_ATTR_NOT_SUPPORTED
1106  */
1107 VAStatus vaSetDisplayAttributes (
1108     VADisplay dpy,
1109     VADisplayAttribute *attr_list,
1110     int num_attributes
1111 )
1112 {
1113   VADriverContextP ctx = CTX(dpy);
1114   ASSERT_CONTEXT(ctx);
1115
1116   TRACE(vaSetDisplayAttributes);
1117   return ctx->vtable.vaSetDisplayAttributes ( ctx, attr_list, num_attributes );
1118 }
1119
1120
1121
1122 VAStatus vaDbgCopySurfaceToBuffer(VADisplay dpy,
1123     VASurface *surface,
1124     void **buffer, /* out */
1125     unsigned int *stride /* out */
1126 )
1127 {
1128   VADriverContextP ctx = CTX(dpy);
1129   ASSERT_CONTEXT(ctx);
1130
1131   TRACE(vaDbgCopySurfaceToBuffer);
1132   return ctx->vtable.vaDbgCopySurfaceToBuffer( ctx, surface, buffer, stride );
1133 }
1134