OSDN Git Service

Remove dependency on X11/Xlib.h
[android-x86/hardware-intel-common-libva.git] / va / 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 #define _GNU_SOURCE 1
26 #include "sysdeps.h"
27 #include "va.h"
28 #include "va_backend.h"
29 #include "va_trace.h"
30 #include "va_fool.h"
31
32 #include <assert.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <dlfcn.h>
38 #include <unistd.h>
39
40 #define DRIVER_EXTENSION        "_drv_video.so"
41
42 #define CTX(dpy) (((VADisplayContextP)dpy)->pDriverContext)
43 #define CHECK_DISPLAY(dpy) if( !vaDisplayIsValid(dpy) ) { return VA_STATUS_ERROR_INVALID_DISPLAY; }
44
45 #define ASSERT          assert
46 #define CHECK_VTABLE(s, ctx, func) if (!va_checkVtable(ctx->vtable->va##func, #func)) s = VA_STATUS_ERROR_UNKNOWN;
47 #define CHECK_MAXIMUM(s, ctx, var) if (!va_checkMaximum(ctx->max_##var, #var)) s = VA_STATUS_ERROR_UNKNOWN;
48 #define CHECK_STRING(s, ctx, var) if (!va_checkString(ctx->str_##var, #var)) s = VA_STATUS_ERROR_UNKNOWN;
49
50 #define Bool int
51 #define True 1
52 #define False 0
53
54 /*
55  * read a config "env" for libva.conf or from environment setting
56  * liva.conf has higher priority
57  * return 0: the "env" is set, and the value is copied into env_value
58  *        1: the env is not set
59  */
60 int va_parseConfig(char *env, char *env_value)
61 {
62     char *token, *value, *saveptr;
63     char oneline[1024];
64     FILE *fp=NULL;
65
66     if (env == NULL)
67         return 1;
68     
69     fp = fopen("/etc/libva.conf", "r");
70     while (fp && (fgets(oneline, 1024, fp) != NULL)) {
71         if (strlen(oneline) == 1)
72             continue;
73         token = strtok_r(oneline, "=\n", &saveptr);
74         value = strtok_r(NULL, "=\n", &saveptr);
75
76         if (NULL == token || NULL == value)
77             continue;
78
79         if (strcmp(token, env) == 0) {
80             if (env_value)
81                 strncpy(env_value,value, 1024);
82
83             fclose(fp);
84
85             return 0;
86         }
87     }
88     if (fp)
89         fclose(fp);
90
91     /* no setting in config file, use env setting */
92     if (getenv(env)) {
93         if (env_value)
94             strncpy(env_value, getenv(env), 1024);
95
96         return 0;
97     }
98     
99     return 1;
100 }
101
102 int vaDisplayIsValid(VADisplay dpy)
103 {
104     VADisplayContextP pDisplayContext = (VADisplayContextP)dpy;
105     return pDisplayContext && (pDisplayContext->vadpy_magic == VA_DISPLAY_MAGIC) && pDisplayContext->vaIsValid(pDisplayContext);
106 }
107
108 void va_errorMessage(const char *msg, ...)
109 {
110     va_list args;
111
112     fprintf(stderr, "libva error: ");
113     va_start(args, msg);
114     vfprintf(stderr, msg, args);
115     va_end(args);
116 }
117
118 void va_infoMessage(const char *msg, ...)
119 {
120     va_list args;
121
122     fprintf(stderr, "libva: ");
123     va_start(args, msg);
124     vfprintf(stderr, msg, args);
125     va_end(args);
126 }
127
128 static Bool va_checkVtable(void *ptr, char *function)
129 {
130     if (!ptr) {
131         va_errorMessage("No valid vtable entry for va%s\n", function);
132         return False;
133     }
134     return True;
135 }
136
137 static Bool va_checkMaximum(int value, char *variable)
138 {
139     if (!value) {
140         va_errorMessage("Failed to define max_%s in init\n", variable);
141         return False;
142     }
143     return True;
144 }
145
146 static Bool va_checkString(const char* value, char *variable)
147 {
148     if (!value) {
149         va_errorMessage("Failed to define str_%s in init\n", variable);
150         return False;
151     }
152     return True;
153 }
154
155 static inline int
156 va_getDriverInitName(char *name, int namelen, int major, int minor)
157 {
158     int ret = snprintf(name, namelen, "__vaDriverInit_%d_%d", major, minor);
159     return ret > 0 && ret < namelen;
160 }
161
162 static VAStatus va_getDriverName(VADisplay dpy, char **driver_name)
163 {
164     VADisplayContextP pDisplayContext = (VADisplayContextP)dpy;
165
166     return pDisplayContext->vaGetDriverName(pDisplayContext, driver_name);
167 }
168
169 static VAStatus va_openDriver(VADisplay dpy, char *driver_name)
170 {
171     VADriverContextP ctx = CTX(dpy);
172     VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
173     char *search_path = NULL;
174     char *saveptr;
175     char *driver_dir;
176     
177     if (geteuid() == getuid())
178         /* don't allow setuid apps to use LIBVA_DRIVERS_PATH */
179         search_path = getenv("LIBVA_DRIVERS_PATH");
180     if (!search_path)
181         search_path = VA_DRIVERS_PATH;
182
183     search_path = strdup((const char *)search_path);
184     driver_dir = strtok_r(search_path, ":", &saveptr);
185     while (driver_dir) {
186         void *handle = NULL;
187         char *driver_path = (char *) malloc( strlen(driver_dir) +
188                                              strlen(driver_name) +
189                                              strlen(DRIVER_EXTENSION) + 2 );
190         strncpy( driver_path, driver_dir, strlen(driver_dir) + 1);
191         strncat( driver_path, "/", strlen("/") );
192         strncat( driver_path, driver_name, strlen(driver_name) );
193         strncat( driver_path, DRIVER_EXTENSION, strlen(DRIVER_EXTENSION) );
194         
195         va_infoMessage("Trying to open %s\n", driver_path);
196 #ifndef ANDROID
197         handle = dlopen( driver_path, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE );
198 #else
199         handle = dlopen( driver_path, RTLD_NOW| RTLD_GLOBAL);
200 #endif
201         if (!handle) {
202             /* Don't give errors for non-existing files */
203             if (0 == access( driver_path, F_OK))
204                 va_errorMessage("dlopen of %s failed: %s\n", driver_path, dlerror());
205         } else {
206             VADriverInit init_func = NULL;
207             char init_func_s[256];
208             if (va_getDriverInitName(init_func_s, sizeof(init_func_s),
209                                      VA_MAJOR_VERSION, VA_MINOR_VERSION))
210                 init_func = (VADriverInit) dlsym(handle, init_func_s);
211             if (!init_func) {
212                 va_errorMessage("%s has no function %s\n",
213                                 driver_path, init_func_s);
214                 dlclose(handle);
215             } else {
216                 struct VADriverVTable *vtable = ctx->vtable;
217
218                 vaStatus = VA_STATUS_SUCCESS;
219                 if (!vtable) {
220                     vtable = calloc(1, sizeof(*vtable));
221                     if (!vtable)
222                         vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
223                 }
224                 ctx->vtable = vtable;
225
226                 if (VA_STATUS_SUCCESS == vaStatus)
227                     vaStatus = (*init_func)(ctx);
228
229                 if (VA_STATUS_SUCCESS == vaStatus) {
230                     CHECK_MAXIMUM(vaStatus, ctx, profiles);
231                     CHECK_MAXIMUM(vaStatus, ctx, entrypoints);
232                     CHECK_MAXIMUM(vaStatus, ctx, attributes);
233                     CHECK_MAXIMUM(vaStatus, ctx, image_formats);
234                     CHECK_MAXIMUM(vaStatus, ctx, subpic_formats);
235                     CHECK_MAXIMUM(vaStatus, ctx, display_attributes);
236                     CHECK_STRING(vaStatus, ctx, vendor);
237                     CHECK_VTABLE(vaStatus, ctx, Terminate);
238                     CHECK_VTABLE(vaStatus, ctx, QueryConfigProfiles);
239                     CHECK_VTABLE(vaStatus, ctx, QueryConfigEntrypoints);
240                     CHECK_VTABLE(vaStatus, ctx, QueryConfigAttributes);
241                     CHECK_VTABLE(vaStatus, ctx, CreateConfig);
242                     CHECK_VTABLE(vaStatus, ctx, DestroyConfig);
243                     CHECK_VTABLE(vaStatus, ctx, GetConfigAttributes);
244                     CHECK_VTABLE(vaStatus, ctx, CreateSurfaces);
245                     CHECK_VTABLE(vaStatus, ctx, DestroySurfaces);
246                     CHECK_VTABLE(vaStatus, ctx, CreateContext);
247                     CHECK_VTABLE(vaStatus, ctx, DestroyContext);
248                     CHECK_VTABLE(vaStatus, ctx, CreateBuffer);
249                     CHECK_VTABLE(vaStatus, ctx, BufferSetNumElements);
250                     CHECK_VTABLE(vaStatus, ctx, MapBuffer);
251                     CHECK_VTABLE(vaStatus, ctx, UnmapBuffer);
252                     CHECK_VTABLE(vaStatus, ctx, DestroyBuffer);
253                     CHECK_VTABLE(vaStatus, ctx, BeginPicture);
254                     CHECK_VTABLE(vaStatus, ctx, RenderPicture);
255                     CHECK_VTABLE(vaStatus, ctx, EndPicture);
256                     CHECK_VTABLE(vaStatus, ctx, SyncSurface);
257                     CHECK_VTABLE(vaStatus, ctx, QuerySurfaceStatus);
258                     CHECK_VTABLE(vaStatus, ctx, PutSurface);
259                     CHECK_VTABLE(vaStatus, ctx, QueryImageFormats);
260                     CHECK_VTABLE(vaStatus, ctx, CreateImage);
261                     CHECK_VTABLE(vaStatus, ctx, DeriveImage);
262                     CHECK_VTABLE(vaStatus, ctx, DestroyImage);
263                     CHECK_VTABLE(vaStatus, ctx, SetImagePalette);
264                     CHECK_VTABLE(vaStatus, ctx, GetImage);
265                     CHECK_VTABLE(vaStatus, ctx, PutImage);
266                     CHECK_VTABLE(vaStatus, ctx, QuerySubpictureFormats);
267                     CHECK_VTABLE(vaStatus, ctx, CreateSubpicture);
268                     CHECK_VTABLE(vaStatus, ctx, DestroySubpicture);
269                     CHECK_VTABLE(vaStatus, ctx, SetSubpictureImage);
270                     CHECK_VTABLE(vaStatus, ctx, SetSubpictureChromakey);
271                     CHECK_VTABLE(vaStatus, ctx, SetSubpictureGlobalAlpha);
272                     CHECK_VTABLE(vaStatus, ctx, AssociateSubpicture);
273                     CHECK_VTABLE(vaStatus, ctx, DeassociateSubpicture);
274                     CHECK_VTABLE(vaStatus, ctx, QueryDisplayAttributes);
275                     CHECK_VTABLE(vaStatus, ctx, GetDisplayAttributes);
276                     CHECK_VTABLE(vaStatus, ctx, SetDisplayAttributes);
277                 }
278                 if (VA_STATUS_SUCCESS != vaStatus) {
279                     va_errorMessage("%s init failed\n", driver_path);
280                     dlclose(handle);
281                 }
282                 if (VA_STATUS_SUCCESS == vaStatus)
283                     ctx->handle = handle;
284                 free(driver_path);
285                 break;
286             }
287         }
288         free(driver_path);
289         
290         driver_dir = strtok_r(NULL, ":", &saveptr);
291     }
292     
293     free(search_path);    
294     
295     return vaStatus;
296 }
297
298 VAPrivFunc vaGetLibFunc(VADisplay dpy, const char *func)
299 {
300     VADriverContextP ctx;
301     if (!vaDisplayIsValid(dpy))
302         return NULL;
303     ctx = CTX(dpy);
304
305     if (NULL == ctx->handle)
306         return NULL;
307         
308     return (VAPrivFunc) dlsym(ctx->handle, func);
309 }
310
311
312 /*
313  * Returns a short english description of error_status
314  */
315 const char *vaErrorStr(VAStatus error_status)
316 {
317     switch(error_status) {
318         case VA_STATUS_SUCCESS:
319             return "success (no error)";
320         case VA_STATUS_ERROR_OPERATION_FAILED:
321             return "operation failed";
322         case VA_STATUS_ERROR_ALLOCATION_FAILED:
323             return "resource allocation failed";
324         case VA_STATUS_ERROR_INVALID_DISPLAY:
325             return "invalid VADisplay";
326         case VA_STATUS_ERROR_INVALID_CONFIG:
327             return "invalid VAConfigID";
328         case VA_STATUS_ERROR_INVALID_CONTEXT:
329             return "invalid VAContextID";
330         case VA_STATUS_ERROR_INVALID_SURFACE:
331             return "invalid VASurfaceID";
332         case VA_STATUS_ERROR_INVALID_BUFFER:
333             return "invalid VABufferID";
334         case VA_STATUS_ERROR_INVALID_IMAGE:
335             return "invalid VAImageID";
336         case VA_STATUS_ERROR_INVALID_SUBPICTURE:
337             return "invalid VASubpictureID";
338         case VA_STATUS_ERROR_ATTR_NOT_SUPPORTED:
339             return "attribute not supported";
340         case VA_STATUS_ERROR_MAX_NUM_EXCEEDED:
341             return "list argument exceeds maximum number";
342         case VA_STATUS_ERROR_UNSUPPORTED_PROFILE:
343             return "the requested VAProfile is not supported";
344         case VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT:
345             return "the requested VAEntryPoint is not supported";
346         case VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT:
347             return "the requested RT Format is not supported";
348         case VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE:
349             return "the requested VABufferType is not supported";
350         case VA_STATUS_ERROR_SURFACE_BUSY:
351             return "surface is in use";
352         case VA_STATUS_ERROR_FLAG_NOT_SUPPORTED:
353             return "flag not supported";
354         case VA_STATUS_ERROR_INVALID_PARAMETER:
355             return "invalid parameter";
356         case VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED:
357             return "resolution not supported";
358         case VA_STATUS_ERROR_UNIMPLEMENTED:
359             return "the requested function is not implemented";
360         case VA_STATUS_ERROR_SURFACE_IN_DISPLAYING:
361             return "surface is in displaying (may by overlay)" ;
362         case VA_STATUS_ERROR_INVALID_IMAGE_FORMAT:
363             return "invalid VAImageFormat";
364         case VA_STATUS_ERROR_UNKNOWN:
365             return "unknown libva error";
366     }
367     return "unknown libva error / description missing";
368 }
369       
370 VAStatus vaInitialize (
371     VADisplay dpy,
372     int *major_version,  /* out */
373     int *minor_version   /* out */
374 )
375 {
376     const char *driver_name_env = NULL;
377     char *driver_name = NULL;
378     VAStatus vaStatus;
379
380     CHECK_DISPLAY(dpy);
381
382     va_TraceInit(dpy);
383
384     va_FoolInit(dpy);
385
386     va_infoMessage("VA-API version %s\n", VA_VERSION_S);
387
388     driver_name_env = getenv("LIBVA_DRIVER_NAME");
389     if (driver_name_env && geteuid() == getuid()) {
390         /* Don't allow setuid apps to use LIBVA_DRIVER_NAME */
391         driver_name = strdup(driver_name_env);
392         vaStatus = VA_STATUS_SUCCESS;
393         va_infoMessage("User requested driver '%s'\n", driver_name);
394     } else {
395         vaStatus = va_getDriverName(dpy, &driver_name);
396         va_infoMessage("va_getDriverName() returns %d\n", vaStatus);
397     }
398
399     if (VA_STATUS_SUCCESS == vaStatus) {
400         vaStatus = va_openDriver(dpy, driver_name);
401         va_infoMessage("va_openDriver() returns %d\n", vaStatus);
402
403         *major_version = VA_MAJOR_VERSION;
404         *minor_version = VA_MINOR_VERSION;
405     }
406
407     if (driver_name)
408         free(driver_name);
409     
410     VA_TRACE_LOG(va_TraceInitialize, dpy, major_version, minor_version);
411
412     return vaStatus;
413 }
414
415
416 /*
417  * After this call, all library internal resources will be cleaned up
418  */ 
419 VAStatus vaTerminate (
420     VADisplay dpy
421 )
422 {
423   VAStatus vaStatus = VA_STATUS_SUCCESS;
424   VADisplayContextP pDisplayContext = (VADisplayContextP)dpy;
425   VADriverContextP old_ctx;
426
427   CHECK_DISPLAY(dpy);
428   old_ctx = CTX(dpy);
429
430   if (old_ctx->handle) {
431       vaStatus = old_ctx->vtable->vaTerminate(old_ctx);
432       dlclose(old_ctx->handle);
433       old_ctx->handle = NULL;
434   }
435   free(old_ctx->vtable);
436   old_ctx->vtable = NULL;
437
438   if (VA_STATUS_SUCCESS == vaStatus)
439       pDisplayContext->vaDestroy(pDisplayContext);
440
441   VA_TRACE_LOG(va_TraceTerminate, dpy);
442
443   va_TraceEnd(dpy);
444
445   va_FoolEnd(dpy);
446
447   return vaStatus;
448 }
449
450 /*
451  * vaQueryVendorString returns a pointer to a zero-terminated string
452  * describing some aspects of the VA implemenation on a specific
453  * hardware accelerator. The format of the returned string is:
454  * <vendorname>-<major_version>-<minor_version>-<addtional_info>
455  * e.g. for the Intel GMA500 implementation, an example would be:
456  * "IntelGMA500-1.0-0.2-patch3
457  */
458 const char *vaQueryVendorString (
459     VADisplay dpy
460 )
461 {
462   if (!vaDisplayIsValid(dpy))
463       return NULL;
464   
465   return CTX(dpy)->str_vendor;
466 }
467
468
469 /* Get maximum number of profiles supported by the implementation */
470 int vaMaxNumProfiles (
471     VADisplay dpy
472 )
473 {
474   if (!vaDisplayIsValid(dpy))
475       return 0;
476   
477   return CTX(dpy)->max_profiles;
478 }
479
480 /* Get maximum number of entrypoints supported by the implementation */
481 int vaMaxNumEntrypoints (
482     VADisplay dpy
483 )
484 {
485   if (!vaDisplayIsValid(dpy))
486       return 0;
487   
488   return CTX(dpy)->max_entrypoints;
489 }
490
491
492 /* Get maximum number of attributs supported by the implementation */
493 int vaMaxNumConfigAttributes (
494     VADisplay dpy
495 )
496 {
497   if (!vaDisplayIsValid(dpy))
498       return 0;
499   
500   return CTX(dpy)->max_attributes;
501 }
502
503 VAStatus vaQueryConfigEntrypoints (
504     VADisplay dpy,
505     VAProfile profile,
506     VAEntrypoint *entrypoints,  /* out */
507     int *num_entrypoints        /* out */
508 )
509 {
510   VADriverContextP ctx;
511   CHECK_DISPLAY(dpy);
512   ctx = CTX(dpy);
513
514   return ctx->vtable->vaQueryConfigEntrypoints ( ctx, profile, entrypoints, num_entrypoints);
515 }
516
517 VAStatus vaGetConfigAttributes (
518     VADisplay dpy,
519     VAProfile profile,
520     VAEntrypoint entrypoint,
521     VAConfigAttrib *attrib_list, /* in/out */
522     int num_attribs
523 )
524 {
525   VADriverContextP ctx;
526   CHECK_DISPLAY(dpy);
527   ctx = CTX(dpy);
528
529   return ctx->vtable->vaGetConfigAttributes ( ctx, profile, entrypoint, attrib_list, num_attribs );
530 }
531
532 VAStatus vaQueryConfigProfiles (
533     VADisplay dpy,
534     VAProfile *profile_list,    /* out */
535     int *num_profiles           /* out */
536 )
537 {
538   VADriverContextP ctx;
539   CHECK_DISPLAY(dpy);
540   ctx = CTX(dpy);
541
542   return ctx->vtable->vaQueryConfigProfiles ( ctx, profile_list, num_profiles );
543 }
544
545 VAStatus vaCreateConfig (
546     VADisplay dpy,
547     VAProfile profile, 
548     VAEntrypoint entrypoint, 
549     VAConfigAttrib *attrib_list,
550     int num_attribs,
551     VAConfigID *config_id /* out */
552 )
553 {
554   VADriverContextP ctx;
555   VAStatus vaStatus = VA_STATUS_SUCCESS;
556   int ret = 0;
557   
558   CHECK_DISPLAY(dpy);
559   ctx = CTX(dpy);
560
561   vaStatus = ctx->vtable->vaCreateConfig ( ctx, profile, entrypoint, attrib_list, num_attribs, config_id );
562
563   /* record the current entrypoint for further trace/fool determination */
564   VA_TRACE_FUNC(va_TraceCreateConfig, dpy, profile, entrypoint, attrib_list, num_attribs, config_id);
565   VA_FOOL_FUNC(va_FoolCreateConfig, dpy, profile, entrypoint, attrib_list, num_attribs, config_id);
566   
567   return vaStatus;
568 }
569
570 VAStatus vaDestroyConfig (
571     VADisplay dpy,
572     VAConfigID config_id
573 )
574 {
575   VADriverContextP ctx;
576   CHECK_DISPLAY(dpy);
577   ctx = CTX(dpy);
578
579   return ctx->vtable->vaDestroyConfig ( ctx, config_id );
580 }
581
582 VAStatus vaQueryConfigAttributes (
583     VADisplay dpy,
584     VAConfigID config_id, 
585     VAProfile *profile,         /* out */
586     VAEntrypoint *entrypoint,   /* out */
587     VAConfigAttrib *attrib_list,/* out */
588     int *num_attribs            /* out */
589 )
590 {
591   VADriverContextP ctx;
592   CHECK_DISPLAY(dpy);
593   ctx = CTX(dpy);
594
595   return ctx->vtable->vaQueryConfigAttributes( ctx, config_id, profile, entrypoint, attrib_list, num_attribs);
596 }
597
598 VAStatus vaCreateSurfaces (
599     VADisplay dpy,
600     int width,
601     int height,
602     int format,
603     int num_surfaces,
604     VASurfaceID *surfaces       /* out */
605 )
606 {
607   VADriverContextP ctx;
608   VAStatus vaStatus;
609
610   CHECK_DISPLAY(dpy);
611   ctx = CTX(dpy);
612
613   vaStatus = ctx->vtable->vaCreateSurfaces( ctx, width, height, format, num_surfaces, surfaces );
614
615   VA_TRACE_LOG(va_TraceCreateSurface, dpy, width, height, format, num_surfaces, surfaces);
616   
617   return vaStatus;
618 }
619
620
621 VAStatus vaDestroySurfaces (
622     VADisplay dpy,
623     VASurfaceID *surface_list,
624     int num_surfaces
625 )
626 {
627   VADriverContextP ctx;
628   CHECK_DISPLAY(dpy);
629   ctx = CTX(dpy);
630
631   return ctx->vtable->vaDestroySurfaces( ctx, surface_list, num_surfaces );
632 }
633
634 VAStatus vaCreateContext (
635     VADisplay dpy,
636     VAConfigID config_id,
637     int picture_width,
638     int picture_height,
639     int flag,
640     VASurfaceID *render_targets,
641     int num_render_targets,
642     VAContextID *context                /* out */
643 )
644 {
645   VADriverContextP ctx;
646   VAStatus vaStatus;
647   
648   CHECK_DISPLAY(dpy);
649   ctx = CTX(dpy);
650
651   vaStatus = ctx->vtable->vaCreateContext( ctx, config_id, picture_width, picture_height,
652                                       flag, render_targets, num_render_targets, context );
653
654   /* keep current encode/decode resoluton */
655   VA_TRACE_FUNC(va_TraceCreateContext, dpy, config_id, picture_width, picture_height, flag, render_targets, num_render_targets, context);
656
657   return vaStatus;
658 }
659
660 VAStatus vaDestroyContext (
661     VADisplay dpy,
662     VAContextID context
663 )
664 {
665   VADriverContextP ctx;
666   CHECK_DISPLAY(dpy);
667   ctx = CTX(dpy);
668
669   return ctx->vtable->vaDestroyContext( ctx, context );
670 }
671
672 VAStatus vaCreateBuffer (
673     VADisplay dpy,
674     VAContextID context,        /* in */
675     VABufferType type,          /* in */
676     unsigned int size,          /* in */
677     unsigned int num_elements,  /* in */
678     void *data,                 /* in */
679     VABufferID *buf_id          /* out */
680 )
681 {
682   VADriverContextP ctx;
683   CHECK_DISPLAY(dpy);
684   ctx = CTX(dpy);
685   int ret = 0;
686
687   VA_FOOL_FUNC(va_FoolCreateBuffer, dpy, context, type, size, num_elements, data, buf_id);
688   if (ret)
689       return VA_STATUS_SUCCESS;
690
691   return ctx->vtable->vaCreateBuffer( ctx, context, type, size, num_elements, data, buf_id);
692 }
693
694 VAStatus vaBufferSetNumElements (
695     VADisplay dpy,
696     VABufferID buf_id,  /* in */
697     unsigned int num_elements /* in */
698 )
699 {
700   VADriverContextP ctx;
701   CHECK_DISPLAY(dpy);
702   ctx = CTX(dpy);
703   
704   VA_FOOL_RETURN();
705   
706   return ctx->vtable->vaBufferSetNumElements( ctx, buf_id, num_elements );
707 }
708
709
710 VAStatus vaMapBuffer (
711     VADisplay dpy,
712     VABufferID buf_id,  /* in */
713     void **pbuf         /* out */
714 )
715 {
716   VADriverContextP ctx;
717   VAStatus va_status;
718   int ret = 0;
719   
720   CHECK_DISPLAY(dpy);
721   ctx = CTX(dpy);
722
723   VA_FOOL_FUNC(va_FoolMapBuffer, dpy, buf_id, pbuf);
724   if (ret)
725       return VA_STATUS_SUCCESS;
726   
727   va_status = ctx->vtable->vaMapBuffer( ctx, buf_id, pbuf );
728
729   VA_TRACE_LOG(va_TraceMapBuffer, dpy, buf_id, pbuf);
730   
731   return va_status;
732 }
733
734 VAStatus vaUnmapBuffer (
735     VADisplay dpy,
736     VABufferID buf_id   /* in */
737 )
738 {
739   VADriverContextP ctx;
740   CHECK_DISPLAY(dpy);
741   ctx = CTX(dpy);
742   int ret = 0;
743
744   VA_FOOL_FUNC(va_FoolUnmapBuffer, dpy, buf_id);
745   if (ret)
746       return VA_STATUS_SUCCESS;
747
748   return ctx->vtable->vaUnmapBuffer( ctx, buf_id );
749 }
750
751 VAStatus vaDestroyBuffer (
752     VADisplay dpy,
753     VABufferID buffer_id
754 )
755 {
756   VADriverContextP ctx;
757   CHECK_DISPLAY(dpy);
758   ctx = CTX(dpy);
759
760   VA_FOOL_RETURN();
761   
762   return ctx->vtable->vaDestroyBuffer( ctx, buffer_id );
763 }
764
765 VAStatus vaBufferInfo (
766     VADisplay dpy,
767     VAContextID context,        /* in */
768     VABufferID buf_id,          /* in */
769     VABufferType *type,         /* out */
770     unsigned int *size,         /* out */
771     unsigned int *num_elements  /* out */
772 )
773 {
774   VADriverContextP ctx;
775   int ret = 0;
776   
777   CHECK_DISPLAY(dpy);
778   ctx = CTX(dpy);
779
780   VA_FOOL_FUNC(va_FoolBufferInfo, dpy, buf_id, type, size, num_elements);
781   if (ret)
782       return VA_STATUS_SUCCESS;
783   
784   return ctx->vtable->vaBufferInfo( ctx, buf_id, type, size, num_elements );
785 }
786
787 VAStatus vaBeginPicture (
788     VADisplay dpy,
789     VAContextID context,
790     VASurfaceID render_target
791 )
792 {
793   VADriverContextP ctx;
794   VAStatus va_status;
795
796   CHECK_DISPLAY(dpy);
797   ctx = CTX(dpy);
798
799   VA_TRACE_FUNC(va_TraceBeginPicture, dpy, context, render_target);
800   VA_FOOL_RETURN();
801   
802   va_status = ctx->vtable->vaBeginPicture( ctx, context, render_target );
803   
804   return va_status;
805 }
806
807 VAStatus vaRenderPicture (
808     VADisplay dpy,
809     VAContextID context,
810     VABufferID *buffers,
811     int num_buffers
812 )
813 {
814   VADriverContextP ctx;
815
816   CHECK_DISPLAY(dpy);
817   ctx = CTX(dpy);
818
819   VA_TRACE_LOG(va_TraceRenderPicture, dpy, context, buffers, num_buffers);
820   VA_FOOL_RETURN();
821
822   return ctx->vtable->vaRenderPicture( ctx, context, buffers, num_buffers );
823 }
824
825 VAStatus vaEndPicture (
826     VADisplay dpy,
827     VAContextID context
828 )
829 {
830   VAStatus va_status;
831   VADriverContextP ctx;
832
833   CHECK_DISPLAY(dpy);
834   ctx = CTX(dpy);
835
836   /* dump encode source surface */
837   VA_TRACE_SURFACE(va_TraceEndPicture, dpy, context, 0);
838   /* return directly if do dummy operation */
839   VA_FOOL_RETURN();
840   
841   va_status = ctx->vtable->vaEndPicture( ctx, context );
842   /* dump decode dest surface */
843   VA_TRACE_SURFACE(va_TraceEndPicture, dpy, context, 1);
844
845   return va_status;
846 }
847
848 VAStatus vaSyncSurface (
849     VADisplay dpy,
850     VASurfaceID render_target
851 )
852 {
853   VAStatus va_status;
854   VADriverContextP ctx;
855
856   CHECK_DISPLAY(dpy);
857   ctx = CTX(dpy);
858
859   va_status = ctx->vtable->vaSyncSurface( ctx, render_target );
860   VA_TRACE_LOG(va_TraceSyncSurface, dpy, render_target);
861
862   return va_status;
863 }
864
865 VAStatus vaQuerySurfaceStatus (
866     VADisplay dpy,
867     VASurfaceID render_target,
868     VASurfaceStatus *status     /* out */
869 )
870 {
871   VAStatus va_status;
872   VADriverContextP ctx;
873   CHECK_DISPLAY(dpy);
874   ctx = CTX(dpy);
875
876   va_status = ctx->vtable->vaQuerySurfaceStatus( ctx, render_target, status );
877
878   VA_TRACE_LOG(va_TraceQuerySurfaceStatus, dpy, render_target, status);
879
880   return va_status;
881 }
882
883 VAStatus vaQuerySurfaceError (
884         VADisplay dpy,
885         VASurfaceID surface,
886         VAStatus error_status,
887         void **error_info /*out*/
888 )
889 {
890   VAStatus va_status;
891   VADriverContextP ctx;
892   CHECK_DISPLAY(dpy);
893   ctx = CTX(dpy);
894
895   va_status = ctx->vtable->vaQuerySurfaceError( ctx, surface, error_status, error_info );
896
897   VA_TRACE_LOG(va_TraceQuerySurfaceError, dpy, surface, error_status, error_info);
898
899   return va_status;
900 }
901
902 /* Get maximum number of image formats supported by the implementation */
903 int vaMaxNumImageFormats (
904     VADisplay dpy
905 )
906 {
907   if (!vaDisplayIsValid(dpy))
908       return 0;
909   
910   return CTX(dpy)->max_image_formats;
911 }
912
913 VAStatus vaQueryImageFormats (
914     VADisplay dpy,
915     VAImageFormat *format_list, /* out */
916     int *num_formats            /* out */
917 )
918 {
919   VADriverContextP ctx;
920   CHECK_DISPLAY(dpy);
921   ctx = CTX(dpy);
922
923   return ctx->vtable->vaQueryImageFormats ( ctx, format_list, num_formats);
924 }
925
926 /* 
927  * The width and height fields returned in the VAImage structure may get 
928  * enlarged for some YUV formats. The size of the data buffer that needs
929  * to be allocated will be given in the "data_size" field in VAImage.
930  * Image data is not allocated by this function.  The client should
931  * allocate the memory and fill in the VAImage structure's data field
932  * after looking at "data_size" returned from the library.
933  */
934 VAStatus vaCreateImage (
935     VADisplay dpy,
936     VAImageFormat *format,
937     int width,
938     int height,
939     VAImage *image      /* out */
940 )
941 {
942   VADriverContextP ctx;
943   CHECK_DISPLAY(dpy);
944   ctx = CTX(dpy);
945
946   return ctx->vtable->vaCreateImage ( ctx, format, width, height, image);
947 }
948
949 /*
950  * Should call DestroyImage before destroying the surface it is bound to
951  */
952 VAStatus vaDestroyImage (
953     VADisplay dpy,
954     VAImageID image
955 )
956 {
957   VADriverContextP ctx;
958   CHECK_DISPLAY(dpy);
959   ctx = CTX(dpy);
960
961   return ctx->vtable->vaDestroyImage ( ctx, image);
962 }
963
964 VAStatus vaSetImagePalette (
965     VADisplay dpy,
966     VAImageID image,
967     unsigned char *palette
968 )
969 {
970   VADriverContextP ctx;
971   CHECK_DISPLAY(dpy);
972   ctx = CTX(dpy);
973
974   return ctx->vtable->vaSetImagePalette ( ctx, image, palette);
975 }
976
977 /*
978  * Retrieve surface data into a VAImage
979  * Image must be in a format supported by the implementation
980  */
981 VAStatus vaGetImage (
982     VADisplay dpy,
983     VASurfaceID surface,
984     int x,      /* coordinates of the upper left source pixel */
985     int y,
986     unsigned int width, /* width and height of the region */
987     unsigned int height,
988     VAImageID image
989 )
990 {
991   VADriverContextP ctx;
992   CHECK_DISPLAY(dpy);
993   ctx = CTX(dpy);
994
995   return ctx->vtable->vaGetImage ( ctx, surface, x, y, width, height, image);
996 }
997
998 /*
999  * Copy data from a VAImage to a surface
1000  * Image must be in a format supported by the implementation
1001  */
1002 VAStatus vaPutImage (
1003     VADisplay dpy,
1004     VASurfaceID surface,
1005     VAImageID image,
1006     int src_x,
1007     int src_y,
1008     unsigned int src_width,
1009     unsigned int src_height,
1010     int dest_x,
1011     int dest_y,
1012     unsigned int dest_width,
1013     unsigned int dest_height
1014 )
1015 {
1016   VADriverContextP ctx;
1017   CHECK_DISPLAY(dpy);
1018   ctx = CTX(dpy);
1019
1020   return ctx->vtable->vaPutImage ( ctx, surface, image, src_x, src_y, src_width, src_height, dest_x, dest_y, dest_width, dest_height );
1021 }
1022
1023 /*
1024  * Derive an VAImage from an existing surface.
1025  * This interface will derive a VAImage and corresponding image buffer from
1026  * an existing VA Surface. The image buffer can then be mapped/unmapped for
1027  * direct CPU access. This operation is only possible on implementations with
1028  * direct rendering capabilities and internal surface formats that can be
1029  * represented with a VAImage. When the operation is not possible this interface
1030  * will return VA_STATUS_ERROR_OPERATION_FAILED. Clients should then fall back
1031  * to using vaCreateImage + vaPutImage to accomplish the same task in an
1032  * indirect manner.
1033  *
1034  * Implementations should only return success when the resulting image buffer
1035  * would be useable with vaMap/Unmap.
1036  *
1037  * When directly accessing a surface special care must be taken to insure
1038  * proper synchronization with the graphics hardware. Clients should call
1039  * vaQuerySurfaceStatus to insure that a surface is not the target of concurrent
1040  * rendering or currently being displayed by an overlay.
1041  *
1042  * Additionally nothing about the contents of a surface should be assumed
1043  * following a vaPutSurface. Implementations are free to modify the surface for
1044  * scaling or subpicture blending within a call to vaPutImage.
1045  *
1046  * Calls to vaPutImage or vaGetImage using the same surface from which the image
1047  * has been derived will return VA_STATUS_ERROR_SURFACE_BUSY. vaPutImage or
1048  * vaGetImage with other surfaces is supported.
1049  *
1050  * An image created with vaDeriveImage should be freed with vaDestroyImage. The
1051  * image and image buffer structures will be destroyed; however, the underlying
1052  * surface will remain unchanged until freed with vaDestroySurfaces.
1053  */
1054 VAStatus vaDeriveImage (
1055     VADisplay dpy,
1056     VASurfaceID surface,
1057     VAImage *image      /* out */
1058 )
1059 {
1060   VADriverContextP ctx;
1061   CHECK_DISPLAY(dpy);
1062   ctx = CTX(dpy);
1063
1064   return ctx->vtable->vaDeriveImage ( ctx, surface, image );
1065 }
1066
1067
1068 /* Get maximum number of subpicture formats supported by the implementation */
1069 int vaMaxNumSubpictureFormats (
1070     VADisplay dpy
1071 )
1072 {
1073   if (!vaDisplayIsValid(dpy))
1074       return 0;
1075   
1076   return CTX(dpy)->max_subpic_formats;
1077 }
1078
1079 /* 
1080  * Query supported subpicture formats 
1081  * The caller must provide a "format_list" array that can hold at
1082  * least vaMaxNumSubpictureFormats() entries. The flags arrary holds the flag 
1083  * for each format to indicate additional capabilities for that format. The actual 
1084  * number of formats returned in "format_list" is returned in "num_formats".
1085  */
1086 VAStatus vaQuerySubpictureFormats (
1087     VADisplay dpy,
1088     VAImageFormat *format_list, /* out */
1089     unsigned int *flags,        /* out */
1090     unsigned int *num_formats   /* out */
1091 )
1092 {
1093   VADriverContextP ctx;
1094
1095   CHECK_DISPLAY(dpy);
1096   ctx = CTX(dpy);
1097
1098   return ctx->vtable->vaQuerySubpictureFormats ( ctx, format_list, flags, num_formats);
1099 }
1100
1101 /* 
1102  * Subpictures are created with an image associated. 
1103  */
1104 VAStatus vaCreateSubpicture (
1105     VADisplay dpy,
1106     VAImageID image,
1107     VASubpictureID *subpicture  /* out */
1108 )
1109 {
1110   VADriverContextP ctx;
1111   CHECK_DISPLAY(dpy);
1112   ctx = CTX(dpy);
1113
1114   return ctx->vtable->vaCreateSubpicture ( ctx, image, subpicture );
1115 }
1116
1117 /*
1118  * Destroy the subpicture before destroying the image it is assocated to
1119  */
1120 VAStatus vaDestroySubpicture (
1121     VADisplay dpy,
1122     VASubpictureID subpicture
1123 )
1124 {
1125   VADriverContextP ctx;
1126   CHECK_DISPLAY(dpy);
1127   ctx = CTX(dpy);
1128
1129   return ctx->vtable->vaDestroySubpicture ( ctx, subpicture);
1130 }
1131
1132 VAStatus vaSetSubpictureImage (
1133     VADisplay dpy,
1134     VASubpictureID subpicture,
1135     VAImageID image
1136 )
1137 {
1138   VADriverContextP ctx;
1139   CHECK_DISPLAY(dpy);
1140   ctx = CTX(dpy);
1141
1142   return ctx->vtable->vaSetSubpictureImage ( ctx, subpicture, image);
1143 }
1144
1145
1146 /*
1147  * If chromakey is enabled, then the area where the source value falls within
1148  * the chromakey [min, max] range is transparent
1149  */
1150 VAStatus vaSetSubpictureChromakey (
1151     VADisplay dpy,
1152     VASubpictureID subpicture,
1153     unsigned int chromakey_min,
1154     unsigned int chromakey_max,
1155     unsigned int chromakey_mask
1156 )
1157 {
1158   VADriverContextP ctx;
1159   CHECK_DISPLAY(dpy);
1160   ctx = CTX(dpy);
1161
1162   return ctx->vtable->vaSetSubpictureChromakey ( ctx, subpicture, chromakey_min, chromakey_max, chromakey_mask );
1163 }
1164
1165
1166 /*
1167  * Global alpha value is between 0 and 1. A value of 1 means fully opaque and 
1168  * a value of 0 means fully transparent. If per-pixel alpha is also specified then
1169  * the overall alpha is per-pixel alpha multiplied by the global alpha
1170  */
1171 VAStatus vaSetSubpictureGlobalAlpha (
1172     VADisplay dpy,
1173     VASubpictureID subpicture,
1174     float global_alpha 
1175 )
1176 {
1177   VADriverContextP ctx;
1178   CHECK_DISPLAY(dpy);
1179   ctx = CTX(dpy);
1180
1181   return ctx->vtable->vaSetSubpictureGlobalAlpha ( ctx, subpicture, global_alpha );
1182 }
1183
1184 /*
1185   vaAssociateSubpicture associates the subpicture with the target_surface.
1186   It defines the region mapping between the subpicture and the target 
1187   surface through source and destination rectangles (with the same width and height).
1188   Both will be displayed at the next call to vaPutSurface.  Additional
1189   associations before the call to vaPutSurface simply overrides the association.
1190 */
1191 VAStatus vaAssociateSubpicture (
1192     VADisplay dpy,
1193     VASubpictureID subpicture,
1194     VASurfaceID *target_surfaces,
1195     int num_surfaces,
1196     short src_x, /* upper left offset in subpicture */
1197     short src_y,
1198     unsigned short src_width,
1199     unsigned short src_height,
1200     short dest_x, /* upper left offset in surface */
1201     short dest_y,
1202     unsigned short dest_width,
1203     unsigned short dest_height,
1204     /*
1205      * whether to enable chroma-keying or global-alpha
1206      * see VA_SUBPICTURE_XXX values
1207      */
1208     unsigned int flags
1209 )
1210 {
1211   VADriverContextP ctx;
1212   CHECK_DISPLAY(dpy);
1213   ctx = CTX(dpy);
1214
1215   return ctx->vtable->vaAssociateSubpicture ( ctx, subpicture, target_surfaces, num_surfaces, src_x, src_y, src_width, src_height, dest_x, dest_y, dest_width, dest_height, flags );
1216 }
1217
1218 /*
1219  * vaDeassociateSubpicture removes the association of the subpicture with target_surfaces.
1220  */
1221 VAStatus vaDeassociateSubpicture (
1222     VADisplay dpy,
1223     VASubpictureID subpicture,
1224     VASurfaceID *target_surfaces,
1225     int num_surfaces
1226 )
1227 {
1228   VADriverContextP ctx;
1229   CHECK_DISPLAY(dpy);
1230   ctx = CTX(dpy);
1231
1232   return ctx->vtable->vaDeassociateSubpicture ( ctx, subpicture, target_surfaces, num_surfaces );
1233 }
1234
1235
1236 /* Get maximum number of display attributes supported by the implementation */
1237 int vaMaxNumDisplayAttributes (
1238     VADisplay dpy
1239 )
1240 {
1241   int tmp;
1242     
1243   if (!vaDisplayIsValid(dpy))
1244       return 0;
1245   
1246   tmp = CTX(dpy)->max_display_attributes;
1247
1248   VA_TRACE_LOG(va_TraceMaxNumDisplayAttributes, dpy, tmp);
1249   
1250   return tmp;
1251 }
1252
1253 /* 
1254  * Query display attributes 
1255  * The caller must provide a "attr_list" array that can hold at
1256  * least vaMaxNumDisplayAttributes() entries. The actual number of attributes
1257  * returned in "attr_list" is returned in "num_attributes".
1258  */
1259 VAStatus vaQueryDisplayAttributes (
1260     VADisplay dpy,
1261     VADisplayAttribute *attr_list,      /* out */
1262     int *num_attributes                 /* out */
1263 )
1264 {
1265   VADriverContextP ctx;
1266   VAStatus va_status;
1267   
1268   CHECK_DISPLAY(dpy);
1269   ctx = CTX(dpy);
1270   va_status = ctx->vtable->vaQueryDisplayAttributes ( ctx, attr_list, num_attributes );
1271
1272   VA_TRACE_LOG(va_TraceQueryDisplayAttributes, dpy, attr_list, num_attributes);
1273
1274   return va_status;
1275   
1276 }
1277
1278 /* 
1279  * Get display attributes 
1280  * This function returns the current attribute values in "attr_list".
1281  * Only attributes returned with VA_DISPLAY_ATTRIB_GETTABLE set in the "flags" field
1282  * from vaQueryDisplayAttributes() can have their values retrieved.  
1283  */
1284 VAStatus vaGetDisplayAttributes (
1285     VADisplay dpy,
1286     VADisplayAttribute *attr_list,      /* in/out */
1287     int num_attributes
1288 )
1289 {
1290   VADriverContextP ctx;
1291   VAStatus va_status;
1292
1293   CHECK_DISPLAY(dpy);
1294   ctx = CTX(dpy);
1295   va_status = ctx->vtable->vaGetDisplayAttributes ( ctx, attr_list, num_attributes );
1296
1297   VA_TRACE_LOG(va_TraceGetDisplayAttributes, dpy, attr_list, num_attributes);
1298   
1299   return va_status;
1300 }
1301
1302 /* 
1303  * Set display attributes 
1304  * Only attributes returned with VA_DISPLAY_ATTRIB_SETTABLE set in the "flags" field
1305  * from vaQueryDisplayAttributes() can be set.  If the attribute is not settable or 
1306  * the value is out of range, the function returns VA_STATUS_ERROR_ATTR_NOT_SUPPORTED
1307  */
1308 VAStatus vaSetDisplayAttributes (
1309     VADisplay dpy,
1310     VADisplayAttribute *attr_list,
1311     int num_attributes
1312 )
1313 {
1314   VADriverContextP ctx;
1315   VAStatus va_status;
1316   CHECK_DISPLAY(dpy);
1317   ctx = CTX(dpy);
1318
1319   va_status = ctx->vtable->vaSetDisplayAttributes ( ctx, attr_list, num_attributes );
1320   VA_TRACE_LOG(va_TraceSetDisplayAttributes, dpy, attr_list, num_attributes);
1321   
1322   return va_status;
1323 }
1324
1325 VAStatus vaLockSurface(VADisplay dpy,
1326     VASurfaceID surface,
1327     unsigned int *fourcc, /* following are output argument */
1328     unsigned int *luma_stride,
1329     unsigned int *chroma_u_stride,
1330     unsigned int *chroma_v_stride,
1331     unsigned int *luma_offset,
1332     unsigned int *chroma_u_offset,
1333     unsigned int *chroma_v_offset,
1334     unsigned int *buffer_name,
1335     void **buffer 
1336 )
1337 {
1338   VADriverContextP ctx;
1339   CHECK_DISPLAY(dpy);
1340   ctx = CTX(dpy);
1341
1342   return ctx->vtable->vaLockSurface( ctx, surface, fourcc, luma_stride, chroma_u_stride, chroma_v_stride, luma_offset, chroma_u_offset, chroma_v_offset, buffer_name, buffer);
1343 }
1344
1345
1346 VAStatus vaUnlockSurface(VADisplay dpy,
1347     VASurfaceID surface
1348 )
1349 {
1350   VADriverContextP ctx;
1351   CHECK_DISPLAY(dpy);
1352   ctx = CTX(dpy);
1353
1354   return ctx->vtable->vaUnlockSurface( ctx, surface );
1355 }